diff --git a/NPAnalysis/must2/src/Analysis.cc b/NPAnalysis/must2/src/Analysis.cc index f29486ec0fa83e2c29691dedfb56cbe1da63315b..7aef0fccfa560774160ac47c0cf8ff6e868a54c7 100644 --- a/NPAnalysis/must2/src/Analysis.cc +++ b/NPAnalysis/must2/src/Analysis.cc @@ -10,18 +10,30 @@ int main(int argc,char** argv) { // command line parsing NPOptionManager* myOptionManager = NPOptionManager::getInstance(argc,argv); - string reactionfileName = myOptionManager->GetReactionFile(); - string detectorfileName = myOptionManager->GetDetectorFile(); - string calibrationfileName = myOptionManager->GetCalibrationFile(); - string runToReadfileName = myOptionManager->GetRunToReadFile(); - string OutputfileName = myOptionManager->GetOutputFile(); - - ///////////////////////////////////////////////////////////////////////////////////////////////////// - // First of All instantiate RootInput and Output - // Detector will be attached later + + // Instantiate RootInput + string runToReadfileName = myOptionManager->GetRunToReadFile(); RootInput:: getInstance(runToReadfileName); - RootOutput::getInstance("Analysis/Must2_AnalysedData", "AnalysedTree"); + // if input files are not given, use those from TAsciiFile + if (myOptionManager->IsDefault("EventGenerator")) { + string name = RootInput::getInstance()->DumpAsciiFile("EventGenerator"); + myOptionManager->SetReactionFile(name); + } + if (myOptionManager->IsDefault("DetectorConfiguration")) { + string name = RootInput::getInstance()->DumpAsciiFile("DetectorConfiguration"); + myOptionManager->SetDetectorFile(name); + } + + // Instantiate RootOutput + RootOutput::getInstance("Analysis/Must2_AnalysedData", "AnalysedTree"); + + // get input files from NPOptionManager + string reactionfileName = myOptionManager->GetReactionFile(); + string detectorfileName = myOptionManager->GetDetectorFile(); + string calibrationfileName = myOptionManager->GetCalibrationFile(); + string OutputfileName = myOptionManager->GetOutputFile(); + // Instantiate some Reaction cout << endl << "/////////// Event generator ///////////" << endl; NPL::Reaction* myReaction = new Reaction(); @@ -176,6 +188,7 @@ int main(int argc,char** argv) cout << " A total of " << N << " event has been analysed " << endl ; cout << endl << " ///////////////////////////////////// "<< endl<< endl ; RootOutput::getInstance()->Destroy(); + RootInput::getInstance()->Destroy(); return 0; } diff --git a/NPLib/IORoot/RootInput.cxx b/NPLib/IORoot/RootInput.cxx index 5b3fbe1e06bca84e28d934241eb5c294154dcc30..a0e06ed2a158f1058d3188e39ca2dd2e7b1208c2 100644 --- a/NPLib/IORoot/RootInput.cxx +++ b/NPLib/IORoot/RootInput.cxx @@ -23,8 +23,10 @@ #include <sstream> #include <fstream> #include <limits> +#include <sys/stat.h> #include "RootInput.h" +#include "TAsciiFile.h" RootInput *RootInput::instance = 0; @@ -66,6 +68,7 @@ RootInput::RootInput(string configFileName) ifstream inputConfigFile; inputConfigFile.open(configFileName.c_str()); + pRootFile = NULL; pRootChain = new TChain(); cout << endl; @@ -103,6 +106,7 @@ RootInput::RootInput(string configFileName) else if (!inputConfigFile.eof()) { pRootChain->Add(dataBuffer.c_str()); cout << "Adding file " << dataBuffer << " to TChain" << endl; + if (!pRootFile) pRootFile = new TFile(dataBuffer.c_str()); } } } @@ -187,7 +191,61 @@ void RootInput::AddFriendChain(string RunToAdd) +string RootInput::DumpAsciiFile(const char* type, const char* folder) +{ + string name; + + string sfolder = folder; + // create folder if not existing + // first test if folder exist + struct stat dirInfo; + int res = stat(folder, &dirInfo); + if (res != 0) { // if folder does not exist, create it + string cmd = "mkdir " + sfolder; + if (system(cmd.c_str()) != 0) cout << "RootInput::DumpAsciiFile() problem creating directory" << endl; + } + + string stype = type; + if (stype == "EventGenerator") { + TAsciiFile *aFile = (TAsciiFile*)pRootFile->Get(stype.c_str()); + // build file name + string title = aFile->GetTitle(); + unsigned int pos = title.rfind("/"); + if (pos != string::npos) name = sfolder + title.substr(pos); + else name = sfolder + "/" + title; + aFile->WriteToFile(name.c_str()); + } + else if (stype == "DetectorConfiguration") { + TAsciiFile *aFile = (TAsciiFile*)pRootFile->Get(stype.c_str()); + // build file name + string title = aFile->GetTitle(); + unsigned int pos = title.rfind("/"); + if (pos != string::npos) name = sfolder + title.substr(pos); + else name = sfolder + "/" + title; + aFile->WriteToFile(name.c_str()); + } + else if (stype == "Calibration") { + } + else if (stype == "RunToTreat") { + } + else { + cout << "RootInput::DumpAsciiFile() unkwown keyword" << endl; + } + + return name; +} + + + RootInput::~RootInput() { + // delete default directory ./.tmp + struct stat dirInfo; + int res = stat("./.tmp", &dirInfo); + if (res == 0) { // if does exist, delete it + if (system("rm -rf ./.tmp") != 0) cout << "RootInput::~RootInput() problem deleting ./.tmp directory" << endl; + } + + delete pRootFile; delete pRootChain; } diff --git a/NPLib/IORoot/RootInput.h b/NPLib/IORoot/RootInput.h index 8d0fb023a2ccd542d661d63451585ff6e3314720..4572c2937b4d3ee4d303e9c450f271357c9fbad0 100644 --- a/NPLib/IORoot/RootInput.h +++ b/NPLib/IORoot/RootInput.h @@ -28,7 +28,6 @@ // ROOT headers #include "TFile.h" #include "TChain.h" -#include "TList.h" using namespace std; @@ -64,14 +63,19 @@ private: static RootInput* instance; public: - // Return the private chain - TChain* GetChain() {return pRootChain;} + string DumpAsciiFile(const char* type, const char* folder = "./.tmp"); + +public: + // Return the private chain and file + TChain* GetChain() {return pRootChain;} + TFile* GetFile() {return pRootFile;} // Add a Friend chain to the input chain void AddFriendChain(string RunToAdd); private: TChain *pRootChain; + TFile *pRootFile; int NumberOfFriend; }; diff --git a/NPLib/Tools/NPOptionManager.cxx b/NPLib/Tools/NPOptionManager.cxx index 11f743eca8e5a5f23bb3815f2e9b5c162092969e..c9d607d1f528fafcb33b784a055886bbcee23198 100644 --- a/NPLib/Tools/NPOptionManager.cxx +++ b/NPLib/Tools/NPOptionManager.cxx @@ -97,7 +97,9 @@ void NPOptionManager::CheckArguments() void NPOptionManager::CheckEventGenerator() -{ +{ + bool checkFile = true; + // NPTool path string GlobalPath = getenv("NPTOOL"); string StandardPath = GlobalPath + "/Inputs/EventGenerator/" + fReactionFileName; @@ -105,22 +107,19 @@ void NPOptionManager::CheckEventGenerator() // ifstream to configfile ifstream ConfigFile; - // test if config file is in standard path - ConfigFile.open(StandardPath.c_str()); - if (ConfigFile.is_open()) { - fReactionFileName = StandardPath; - } - else { // if not, assume config file is in current directory - ConfigFile.open(fReactionFileName.c_str()); - if (!ConfigFile.is_open()) { // if not, assign default value - fReactionFileName = fDefaultReactionFileName; -/* cout << endl; - cout << "********************************** Error **********************************" << endl; - cout << "* No event generator file found in $NPTool/Inputs/EventGenerator or local directories *" << endl; - cout << "***************************************************************************************" << endl; - cout << endl; - exit(1);*/ + // test if config file is in local path + ConfigFile.open(fReactionFileName.c_str()); + if (!ConfigFile.is_open()) { + ConfigFile.open(StandardPath.c_str()); + if (!ConfigFile.is_open()) { // if not, assign standard path + checkFile = false; } + else { + fReactionFileName = StandardPath; + } + } + if (!checkFile && fReactionFileName != fDefaultReactionFileName) { // if file does not exist + SendErrorAndExit("EventGenerator"); } // close ConfigFile @@ -131,6 +130,8 @@ void NPOptionManager::CheckEventGenerator() void NPOptionManager::CheckDetectorConfiguration() { + bool checkFile = true; + // NPTool path string GlobalPath = getenv("NPTOOL"); string StandardPath = GlobalPath + "/Inputs/DetectorConfiguration/" + fDetectorFileName; @@ -138,23 +139,20 @@ void NPOptionManager::CheckDetectorConfiguration() // ifstream to configfile ifstream ConfigFile; - // test if config file is in standard path - ConfigFile.open(StandardPath.c_str()); - if (ConfigFile.is_open()) { - fDetectorFileName = StandardPath; - } - else { // if not, assume config file is in current directory - ConfigFile.open(fDetectorFileName.c_str()); - if (!ConfigFile.is_open()) { // if not, assign default value - fDetectorFileName = fDefaultDetectorFileName; -/* cout << endl; - cout << "*********************************** Error ***********************************" << endl; - cout << "* No detector geometry file found in $NPTool/Inputs/EventGenerator or local directories *" << endl; - cout << "*****************************************************************************************" << endl; - cout << endl; - exit(1);*/ + // test if config file is in local path + ConfigFile.open(fDetectorFileName.c_str()); + if (!ConfigFile.is_open()) { + ConfigFile.open(StandardPath.c_str()); + if (!ConfigFile.is_open()) { // if not, assign standard path + checkFile = false; + } + else { + fDetectorFileName = StandardPath; } } + if (!checkFile && fDetectorFileName != fDefaultDetectorFileName) { // if file does not exist + SendErrorAndExit("DetectorConfiguration"); + } // close ConfigFile ConfigFile.close(); @@ -214,7 +212,7 @@ void NPOptionManager::SendErrorAndExit(const char* type) const else if (stype == "RunToTreat") { } else { - cout << "NPOptionManager::SendErrorAndAbort() unkwown keyword" << endl; + cout << "NPOptionManager::SendErrorAndExit() unkwown keyword" << endl; } } diff --git a/NPLib/Tools/NPOptionManager.h b/NPLib/Tools/NPOptionManager.h index 704fbf661af6c7f46f21395159acb635699fc255..ad55564b5d3dd443a62b31f990e2674b47567018 100644 --- a/NPLib/Tools/NPOptionManager.h +++ b/NPLib/Tools/NPOptionManager.h @@ -69,6 +69,7 @@ class NPOptionManager void SendErrorAndExit(const char* type) const; public: + // Getters // default values string GetDefaultReactionFile() {return fDefaultReactionFileName;} string GetDefaultDetectorFile() {return fDefaultDetectorFileName;} @@ -83,6 +84,11 @@ class NPOptionManager string GetOutputFile() {return fOutputFileName;} bool GetDisableAllBranchOption() {return fDisableAllBranchOption;} + // Setters + void SetReactionFile(string name) {fReactionFileName = name;} + void SetDetectorFile(string name) {fDetectorFileName = name;} + void SetRunToReadFile(string name) {fRunToReadFileName = name;} + private: // default values string fDefaultReactionFileName;