From e3ebc77a2e201f6a05883fd3837535ed3a7c9e41 Mon Sep 17 00:00:00 2001
From: deserevi <deserevi@nptool>
Date: Fri, 21 Jan 2011 00:56:02 +0000
Subject: [PATCH] * Add TAsciiFile.{h,cxx} class    + this class allows to
 store in the ROOT output file the input files      used by NPSimulation and
 NPAnalysis

---
 NPAnalysis/Gaspard/include/ObjectManager.hh |   1 +
 NPAnalysis/Gaspard/src/Analysis.cc          |  18 ++
 NPLib/IORoot/RootOutput.cxx                 |  20 ++
 NPLib/IORoot/RootOutput.h                   |  17 +-
 NPLib/Tools/Makefile                        |  11 +-
 NPLib/Tools/TAsciiFile.cxx                  | 120 ++++++++++++
 NPLib/Tools/TAsciiFile.h                    |  56 ++++++
 NPSimulation/Simulation.cc                  | 193 +++++++++++---------
 8 files changed, 342 insertions(+), 94 deletions(-)
 create mode 100644 NPLib/Tools/TAsciiFile.cxx
 create mode 100644 NPLib/Tools/TAsciiFile.h

diff --git a/NPAnalysis/Gaspard/include/ObjectManager.hh b/NPAnalysis/Gaspard/include/ObjectManager.hh
index 6b3d302ca..e2ce67701 100644
--- a/NPAnalysis/Gaspard/include/ObjectManager.hh
+++ b/NPAnalysis/Gaspard/include/ObjectManager.hh
@@ -32,6 +32,7 @@
 #include "NPReaction.h"
 #include "RootInput.h"
 #include "RootOutput.h"
+#include "TAsciiFile.h"
 #include "TInteractionCoordinates.h"
 #include "TInitialConditions.h"
 
diff --git a/NPAnalysis/Gaspard/src/Analysis.cc b/NPAnalysis/Gaspard/src/Analysis.cc
index 0e4eb1ba6..06e4f5937 100644
--- a/NPAnalysis/Gaspard/src/Analysis.cc
+++ b/NPAnalysis/Gaspard/src/Analysis.cc
@@ -17,6 +17,24 @@ int main(int argc,char** argv)
    RootInput:: getInstance(runToReadfileName);
 //   RootOutput::getInstance("Analysis/"+OutputfileName, "AnalyzedTree")	;
    RootOutput::getInstance("Analysis/Gaspard_AnalyzedData", "AnalyzedTree");
+   // store configuration files
+   TString GlobalPath = getenv("NPTOOL");
+   // event generator file
+   TString PathEG = GlobalPath + "/Inputs/EventGenerator/";
+   TString EGFileName = PathEG + reactionfileName;
+   TAsciiFile *asciiFileEG = RootOutput::getInstance()->GetAsciiFileEventGenerator();
+   asciiFileEG->SetNameTitle("EventGenerator", "EventGenerator input file");
+   asciiFileEG->Append(EGFileName);
+   // detector configuration file
+   TString PathDC = GlobalPath + "/Inputs/DetectorConfiguration/";
+   TString DCFileName = PathDC + detectorfileName;
+   TAsciiFile *asciiFileDC = RootOutput::getInstance()->GetAsciiFileDetectorConfiguration();
+   asciiFileDC->SetNameTitle("DetectorConfiguration", "DetectorConfiguration input file");
+   asciiFileDC->Append(DCFileName);
+   // detector configuration file
+   TAsciiFile *asciiFileRF = RootOutput::getInstance()->GetAsciiFileRunToTreat();
+   asciiFileRF->SetNameTitle("RunToTreat", "RunToTreat input file");
+   asciiFileRF->Append(runToReadfileName.data());
  
    // Initialize the reaction
    NPL::Reaction* myReaction = new Reaction();
diff --git a/NPLib/IORoot/RootOutput.cxx b/NPLib/IORoot/RootOutput.cxx
index f3b03096f..2dba73969 100644
--- a/NPLib/IORoot/RootOutput.cxx
+++ b/NPLib/IORoot/RootOutput.cxx
@@ -60,6 +60,11 @@ RootOutput::RootOutput(TString fileNameBase, TString treeNameBase)
 
    pRootTree = new TTree(treeNameBase, "Data created / analyzed with the NPTool package");
    pRootList = new TList();
+
+   pEventGenerator        = new TAsciiFile();
+   pDetectorConfiguration = new TAsciiFile();
+   pCalibrationFile       = new TAsciiFile();
+   pRunToTreatFile        = new TAsciiFile();
 }
 
 
@@ -71,6 +76,21 @@ RootOutput::~RootOutput()
       cout << endl;
       cout << "Got histograms and Tree !" << endl;
       pRootFile->Write();
+
+      // write TAsciiFile if used
+      // EventGenerator
+      TString EGName = pEventGenerator->GetName();
+      if (!EGName.IsNull()) pEventGenerator->Write();
+      // DetectorConfiguration
+      TString DCName = pDetectorConfiguration->GetName();
+      if (!DCName.IsNull()) pDetectorConfiguration->Write();
+      // CalibrationFile
+      TString CFName = pCalibrationFile->GetName();
+      if (!CFName.IsNull()) pCalibrationFile->Write();
+      // RunToTreatFile
+      TString RTName = pRunToTreatFile->GetName();
+      if (!RTName.IsNull()) pRunToTreatFile->Write();
+
       pRootFile->Close();
    } else {
       cout << "No histograms and Tree !" << endl;
diff --git a/NPLib/IORoot/RootOutput.h b/NPLib/IORoot/RootOutput.h
index 4d25455b1..8765d8ffe 100644
--- a/NPLib/IORoot/RootOutput.h
+++ b/NPLib/IORoot/RootOutput.h
@@ -22,6 +22,9 @@
 #ifndef ROOTOUTPUT_HH
 #define ROOTOUTPUT_HH
 
+// NPL headers
+#include "TAsciiFile.h"
+
 // ROOT headers
 #include "TString.h"
 #include "TFile.h"
@@ -64,11 +67,19 @@ public:
    TFile*	GetFile() {return pRootFile;};
    TTree*	GetTree() {return pRootTree;};
    TList*	GetList() {return pRootList;};
+   TAsciiFile* GetAsciiFileEventGenerator()        {return pEventGenerator;}
+   TAsciiFile* GetAsciiFileDetectorConfiguration() {return pDetectorConfiguration;}
+   TAsciiFile* GetAsciiFileCalibration()           {return pCalibrationFile;}
+   TAsciiFile* GetAsciiFileRunToTreat()            {return pRunToTreatFile;}
 
 private:
-   TFile *pRootFile;
-   TTree *pRootTree;
-   TList *pRootList;
+   TFile      *pRootFile;
+   TTree      *pRootTree;
+   TList      *pRootList;
+   TAsciiFile *pEventGenerator;
+   TAsciiFile *pDetectorConfiguration;
+   TAsciiFile *pCalibrationFile;
+   TAsciiFile *pRunToTreatFile;
 };
 
 #endif // ROOTOUTPUT_HH
diff --git a/NPLib/Tools/Makefile b/NPLib/Tools/Makefile
index 862941b73..20ed56ca4 100644
--- a/NPLib/Tools/Makefile
+++ b/NPLib/Tools/Makefile
@@ -1,7 +1,8 @@
 include ../Makefile.arch
  
 #------------------------------------------------------------------------------
-SHARELIB      = libReaction.so libEnergyLoss.so libTagManager.so libOptionManager.so
+SHARELIB      = libReaction.so libEnergyLoss.so libTagManager.so libOptionManager.so \
+                libAsciiFile.so
 all:            $(SHARELIB)
 #------------------------------------------------------------------------------
 
@@ -26,12 +27,20 @@ NPTagManagerDict.cxx: NPTagManager.h
 libOptionManager.so: NPOptionManager.o
 		$(LD) $(SOFLAGS) $^ $(OutPutOpt) $@
 		
+## AsciiFile ##
+libAsciiFile.so: TAsciiFile.o TAsciiFileDict.o
+		$(LD) $(SOFLAGS) $^ $(OutPutOpt) $@
+
+TAsciiFileDict.cxx: TAsciiFile.h
+			rootcint -f $@ -c $^
+			
 # dependances
 NPReaction.o:NPReaction.cxx NPReaction.h
 NPNucleus.o: NPNucleus.cxx NPNucleus.h
 NPEnergyLoss.o:NPEnergyLoss.cxx NPEnergyLoss.h
 NPTagManager.o:NPTagManager.cxx NPTagManager.h
 NPOptionManager.o:NPOptionManager.cxx NPOptionManager.h
+TAsciiFile.o:  TAsciiFile.cxx TAsciiFile.h
 #######################################
 
 ############# Clean and More ##########
diff --git a/NPLib/Tools/TAsciiFile.cxx b/NPLib/Tools/TAsciiFile.cxx
new file mode 100644
index 000000000..8210921a7
--- /dev/null
+++ b/NPLib/Tools/TAsciiFile.cxx
@@ -0,0 +1,120 @@
+/*****************************************************************************
+ * Copyright (C) 2009-2010   this file is part of the NPTool Project         *
+ *                                                                           *
+ * For the licensing terms see $NPTOOL/Licence/NPTool_Licence                *
+ * For the list of contributors see $NPTOOL/Licence/Contributors             *
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Original Author: N. de Sereville  contact address: deserevi@ipno.in2p3.fr *
+ *                                                                           *
+ * Creation Date  : january 2011                                             *
+ * Last update    :                                                          *
+ *---------------------------------------------------------------------------*
+ * Decription:                                                               *
+ *     This class allows to store any ascii file in a ROOT TTree.            *
+ *                                                                           *
+ *---------------------------------------------------------------------------*
+ * Comment:                                                                  *
+ *                                                                           *
+ *                                                                           *
+ *****************************************************************************/
+
+// Class headers
+#include "TAsciiFile.h"
+
+// C++ headers
+#include <fstream>
+#include <iostream>
+using namespace std;
+
+ClassImp(TAsciiFile)
+
+
+TAsciiFile::TAsciiFile()
+   : TNamed()
+{
+}
+
+
+
+TAsciiFile::TAsciiFile(const char* name, const char* title, const char* inputAsciiFile)
+   : TNamed(name, title)
+{
+   ReadFile(inputAsciiFile);
+}
+
+
+
+TAsciiFile::~TAsciiFile()
+{
+}
+
+
+
+void TAsciiFile::ReadFile(const char* inputAsciiFile)
+{
+   ifstream inputStream(inputAsciiFile);
+   if (!inputStream) {
+      cout << "Problem opening file " << inputAsciiFile << endl;
+   }
+   else {
+      string line;
+      while (!inputStream.eof()) {
+         getline(inputStream, line);
+         fLines.push_back(line);
+      }
+   }
+
+   inputStream.close();
+}
+
+
+
+void TAsciiFile::Append(const char* inputAsciiFile)
+{
+   ReadFile(inputAsciiFile);
+}
+
+
+
+void TAsciiFile::WriteToFile(const char* outputAsciiFile) const
+{
+   ofstream outputStream(outputAsciiFile);
+
+   for (UInt_t i = 0; i < fLines.size(); i++) outputStream << fLines[i] << endl;
+
+   outputStream.close();
+}
+
+
+
+void TAsciiFile::WriteToFile(const char* outputAsciiFile, UInt_t begin, UInt_t end) const
+{
+   ofstream outputStream(outputAsciiFile);
+
+   for (UInt_t i = 0; i < fLines.size(); i++) {
+      if (i < begin || i > end) continue;
+      outputStream << fLines[i] << endl;
+   }
+
+   outputStream.close();
+}
+
+
+
+void TAsciiFile::Print() const
+{
+   for (UInt_t i = 0; i < fLines.size(); i++) cout << fLines[i] << endl;
+}
+
+
+
+void TAsciiFile::Print(UInt_t begin, UInt_t end) const
+{
+   for (UInt_t i = 0; i < fLines.size(); i++) {
+      if (i < begin || i > end) continue;
+      cout << fLines[i] << endl;
+   }
+}
+
diff --git a/NPLib/Tools/TAsciiFile.h b/NPLib/Tools/TAsciiFile.h
new file mode 100644
index 000000000..e578f8c68
--- /dev/null
+++ b/NPLib/Tools/TAsciiFile.h
@@ -0,0 +1,56 @@
+#ifndef __TASCIIFILE__
+#define __TASCIIFILE__
+/*****************************************************************************
+ * Copyright (C) 2009-2010   this file is part of the NPTool Project         *
+ *                                                                           *
+ * For the licensing terms see $NPTOOL/Licence/NPTool_Licence                *
+ * For the list of contributors see $NPTOOL/Licence/Contributors             *
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Original Author: N. de Sereville  contact address: deserevi@ipno.in2p3.fr *
+ *                                                                           *
+ * Creation Date  : january 2011                                             *
+ * Last update    :                                                          *
+ *---------------------------------------------------------------------------*
+ * Decription:                                                               *
+ *     This class allows to store any ascii file in a ROOT TTree.            *
+ *                                                                           *
+ *---------------------------------------------------------------------------*
+ * Comment:                                                                  *
+ *                                                                           *
+ *                                                                           *
+ *****************************************************************************/
+
+// ROOT headers
+#include "TNamed.h"
+
+// C++ headers
+#include <vector>
+#include <string>
+using namespace std;
+
+
+class TAsciiFile : public TNamed {
+ private :
+   vector<string>	fLines;
+
+ protected :
+   void ReadFile(const char* inputAsciiFile);
+
+ public :
+   TAsciiFile();
+   TAsciiFile(const char* name, const char* title, const char* inputAsciiFile);
+   virtual ~TAsciiFile();
+
+   void Append(const char* inputAsciiFile);
+   void WriteToFile(const char* outputAsciiFile) const;
+   void WriteToFile(const char* outputAsciiFile, UInt_t begin, UInt_t end) const;
+   void Print(const Option_t*) const {}; 
+   void Print() const;
+   void Print(UInt_t begin, UInt_t end) const;
+
+   ClassDef(TAsciiFile, 1);	// Class TAsciiFile for storing ascii text
+};
+
+#endif
diff --git a/NPSimulation/Simulation.cc b/NPSimulation/Simulation.cc
index 5b5d13824..85e57bc36 100644
--- a/NPSimulation/Simulation.cc
+++ b/NPSimulation/Simulation.cc
@@ -1,113 +1,126 @@
 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 #include "G4RunManager.hh"
+
 // UI
 #include "G4UImanager.hh"
 #include "G4UIterminal.hh"
 #include "G4UItcsh.hh"
+
 // G4 local source
 #include "DetectorConstruction.hh"
 #include "PhysicsList.hh"
 #include "PrimaryGeneratorAction.hh"
+
 // G4 General Source
 #include "SteppingVerbose.hh"
 #ifdef G4VIS_USE
 #include "G4VisExecutive.hh"
 #endif
+
 // NPS headers
 #include "EventAction.hh"
 #include "VDetector.hh"
+
 //NPL headers
 #include "NPOptionManager.h"
 #include "RootOutput.h"
-// ROOT headers
-#include "TTree.h"
-#include "TFile.h"
-// STL
-#include <vector>
 
 int main(int argc, char** argv)
-  {
-    NPOptionManager* OptionManager = NPOptionManager::getInstance(argc,argv);
-    G4String EventGeneratorFileName = OptionManager->GetReactionFilePath();
-    G4String DetectorFileName = OptionManager->GetDetectorFilePath();
-       
-
-     //my Verbose output class
-     G4VSteppingVerbose::SetInstance(new SteppingVerbose);
-
-     //Construct the default run manager
-     G4RunManager* runManager = new G4RunManager;
-
-     //set mandatory initialization classes
-     DetectorConstruction* detector  = new DetectorConstruction();
-     runManager->SetUserInitialization(detector);
-
-     PhysicsList* physics   = new PhysicsList();
-     runManager->SetUserInitialization(physics);
-     PrimaryGeneratorAction* primary = new PrimaryGeneratorAction(detector);
-
-     //Initialize Geant4 kernel
-     runManager->Initialize();
-     physics->MyOwnConstruction();
-
-     ///////////////////////////////////////////////////////////////
-     ///////////////// Initializing the Root Output ////////////////
-     ///////////////////////////////////////////////////////////////
-     RootOutput::getInstance("Simulation/"+OptionManager->GetOutputFilePath());
-
-     ///////////////////////////////////////////////////////////////
-     ////////////// Reading Detector Configuration /////////////////
-     ///////////////////////////////////////////////////////////////
-     detector->ReadConfigurationFile(DetectorFileName);
-
-     ///////////////////////////////////////////////////////////////
-     ////////////////////// Reading Reaction ///////////////////////
-     ///////////////////////////////////////////////////////////////
-     primary->ReadEventGeneratorFile(EventGeneratorFileName);
-     runManager->SetUserAction(primary);
-
-     ///////////////////////////////////////////////////////////////
-     ////////////////// Starting the Event Action //////////////////
-     ///////////////////////////////////////////////////////////////
-     EventAction* event_action = new EventAction() ;
-     event_action->SetDetector(detector)           ;
-     runManager->SetUserAction(event_action)       ;
-
-     ///////////////////////////////////////////////////////////////
-     ///////  Get the pointer to the User Interface manager ////////
-     ///////////////////////////////////////////////////////////////
-     G4UImanager* UI = G4UImanager::GetUIpointer();
-
-     ///////////////////////////////////////////////////////////////
-     /////////// Define UI terminal for interactive mode ///////////
-     ///////////////////////////////////////////////////////////////
-  #ifdef G4VIS_USE
-     G4VisManager* visManager = new G4VisExecutive;
-     visManager->Initialize();
-  #endif
-
-     G4UIsession* session = 0;
-
-  #ifdef G4UI_USE_TCSH
-     session = new G4UIterminal(new G4UItcsh);
-  #else
-     session = new G4UIterminal();
-  #endif
-
-     UI->ApplyCommand("/control/execute vis.mac");
-     session->SessionStart();
-     delete session;
-
-  #ifdef G4VIS_USE
-     delete visManager;
-  #endif
-
-     ///////////////////////////////////////////////////////////////
-     ////////////////////// Job termination ////////////////////////
-     ///////////////////////////////////////////////////////////////
-     RootOutput::getInstance()->Destroy();
-     NPOptionManager::getInstance()->Destroy();
-
-     delete runManager;
-     return 0;
-  }
+{
+   NPOptionManager* OptionManager = NPOptionManager::getInstance(argc, argv);
+   G4String EventGeneratorFileName = OptionManager->GetReactionFilePath();
+   G4String DetectorFileName = OptionManager->GetDetectorFilePath();
+
+   // my Verbose output class
+   G4VSteppingVerbose::SetInstance(new SteppingVerbose);
+
+   // Construct the default run manager
+   G4RunManager* runManager = new G4RunManager;
+
+   // set mandatory initialization classes
+   DetectorConstruction* detector  = new DetectorConstruction();
+   runManager->SetUserInitialization(detector);
+
+   PhysicsList* physics   = new PhysicsList();
+   runManager->SetUserInitialization(physics);
+   PrimaryGeneratorAction* primary = new PrimaryGeneratorAction(detector);
+
+   // Initialize Geant4 kernel
+   runManager->Initialize();
+   physics->MyOwnConstruction();
+
+   ///////////////////////////////////////////////////////////////
+   ///////////////// Initializing the Root Output ////////////////
+   ///////////////////////////////////////////////////////////////
+   RootOutput::getInstance("Simulation/" + OptionManager->GetOutputFilePath());
+   // store configuration files
+   G4String GlobalPath = getenv("NPTOOL");
+   // event generator file
+   G4String PathEG = GlobalPath + "/Inputs/EventGenerator/";
+   G4String EGFileName = PathEG + EventGeneratorFileName;
+   TAsciiFile *asciiFileEG = RootOutput::getInstance()->GetAsciiFileEventGenerator();
+   asciiFileEG->SetNameTitle("EventGenerator", "EventGenerator input file");
+   asciiFileEG->Append(EGFileName);
+   // detector configuration file
+   G4String PathDC = GlobalPath + "/Inputs/DetectorConfiguration/";
+   G4String DCFileName = PathDC + DetectorFileName;
+   TAsciiFile *asciiFileDC = RootOutput::getInstance()->GetAsciiFileDetectorConfiguration();
+   asciiFileDC->SetNameTitle("DetectorConfiguration", "DetectorConfiguration input file");
+   asciiFileDC->Append(DCFileName);
+
+   ///////////////////////////////////////////////////////////////
+   ////////////// Reading Detector Configuration /////////////////
+   ///////////////////////////////////////////////////////////////
+   detector->ReadConfigurationFile(DetectorFileName);
+
+   ///////////////////////////////////////////////////////////////
+   ////////////////////// Reading Reaction ///////////////////////
+   ///////////////////////////////////////////////////////////////
+   primary->ReadEventGeneratorFile(EventGeneratorFileName);
+   runManager->SetUserAction(primary);
+
+   ///////////////////////////////////////////////////////////////
+   ////////////////// Starting the Event Action //////////////////
+   ///////////////////////////////////////////////////////////////
+   EventAction* event_action = new EventAction() ;
+   event_action->SetDetector(detector)           ;
+   runManager->SetUserAction(event_action)       ;
+
+   ///////////////////////////////////////////////////////////////
+   ///////  Get the pointer to the User Interface manager ////////
+   ///////////////////////////////////////////////////////////////
+   G4UImanager* UI = G4UImanager::GetUIpointer();
+
+   ///////////////////////////////////////////////////////////////
+   /////////// Define UI terminal for interactive mode ///////////
+   ///////////////////////////////////////////////////////////////
+#ifdef G4VIS_USE
+   G4VisManager* visManager = new G4VisExecutive;
+   visManager->Initialize();
+#endif
+
+   G4UIsession* session = 0;
+
+#ifdef G4UI_USE_TCSH
+   session = new G4UIterminal(new G4UItcsh);
+#else
+   session = new G4UIterminal();
+#endif
+
+   UI->ApplyCommand("/control/execute vis.mac");
+   session->SessionStart();
+   delete session;
+
+#ifdef G4VIS_USE
+   delete visManager;
+#endif
+
+   ///////////////////////////////////////////////////////////////
+   ////////////////////// Job termination ////////////////////////
+   ///////////////////////////////////////////////////////////////
+   RootOutput::getInstance()->Destroy();
+   NPOptionManager::getInstance()->Destroy();
+
+   delete runManager;
+   return 0;
+}
-- 
GitLab