diff --git a/python/rootUtils.py b/python/rootUtils.py index b31bee3210..82a0539d4e 100644 --- a/python/rootUtils.py +++ b/python/rootUtils.py @@ -81,14 +81,30 @@ def errorSummary(): print("Summary of recorded incidents:") for e in _error_log: print(e, ':', _error_log[e]) + def checkFileExists(x): - if x[0:4] == "/eos": f=gSystem.Getenv("EOSSHIP")+x - else: f=x - test = TFile.Open(f) - if not test: - print("input file",f," does not exist. Missing authentication?") - os._exit(1) - if test.FindObjectAny('cbmsim'): - return 'tree' + + if isinstance(x, list): + print("I'm a list") + for _f in x: + if _f[0:4] == "/eos": f=gSystem.Getenv("EOSSHIP")+_f + else: f=_f + test = TFile.Open(f) + if not test: + print("input file",f," does not exist. Missing authentication?") + os._exit(1) + if test.FindObjectAny('cbmsim'): + return 'tree' + else: + return 'ntuple' else: - return 'ntuple' + if x[0:4] == "/eos": f=gSystem.Getenv("EOSSHIP")+x + else: f=x + test = TFile.Open(f) + if not test: + print("input file",f," does not exist. Missing authentication?") + os._exit(1) + if test.FindObjectAny('cbmsim'): + return 'tree' + else: + return 'ntuple' diff --git a/shipgen/MuonBackGenerator.cxx b/shipgen/MuonBackGenerator.cxx index 25988d0844..e5bde3a878 100644 --- a/shipgen/MuonBackGenerator.cxx +++ b/shipgen/MuonBackGenerator.cxx @@ -13,6 +13,7 @@ #include "FairPrimaryGenerator.h" #include "ShipMCTrack.h" #include "ShipUnit.h" +#include "TChain.h" #include "TDatabasePDG.h" // for TDatabasePDG #include "TFile.h" #include "TMCProcess.h" @@ -38,12 +39,18 @@ MuonBackGenerator::MuonBackGenerator() Bool_t MuonBackGenerator::Init(const char* fileName) { return Init(fileName, 0); } -// ----- Default constructor ------------------------------------------- -Bool_t MuonBackGenerator::Init(const char* fileName, const int firstEvent) { - LOG(info) << "Opening input file " << fileName; - fInputFile = TFile::Open(fileName); - if (!fInputFile) { - LOG(fatal) << "Error opening the Signal file: " << fileName; + +Bool_t MuonBackGenerator::Init(std::vector fileNames) { + return Init(fileNames, 0); +} + +Bool_t MuonBackGenerator::Init(std::vector fileNames, + const int firstEvent) { + LOG(info) << "Opening input file " << fileNames.at(0); + TFile testFile(fileNames.at(0)); + auto testKeys = testFile.GetListOfKeys(); + if (testKeys == nullptr) { + LOG(fatal) << "Error opening the Signal file: " << fileNames.at(0); } fn = firstEvent; fPaintBeam = 5 * cm; // default value for painting beam @@ -51,9 +58,14 @@ Bool_t MuonBackGenerator::Init(const char* fileName, const int firstEvent) { fPhiRandomize = false; // default value for phi randomization fsmearBeam = 8 * mm; // default value for smearing beam fdownScaleDiMuon = kFALSE; // only needed for muflux simulation - fTree = fInputFile->Get("pythia8-Geant4"); - if (fTree) { + if (testKeys->FindObject("pythia8-Geant4")) { + fTree = new TChain("pythia8-Geant4"); + for (auto& f : fileNames) { + LOG(info) << "Opening input file " << f; + static_cast(fTree)->Add(f); + } fNevents = fTree->GetEntries(); + LOG(info) << "Reading " << fNevents << " entries"; // count only events with muons fTree->SetBranchAddress("id", &id); // particle id fTree->SetBranchAddress("parentid", @@ -82,9 +94,14 @@ Bool_t MuonBackGenerator::Init(const char* fileName, const int firstEvent) { } } else { id = -1; - fTree = fInputFile->Get("cbmsim"); + // fTree = fInputFile->Get("cbmsim"); + fTree = new TChain("cbmsim"); + for (auto& f : fileNames) { + LOG(info) << "Opening input file " << f; + static_cast(fTree)->Add(f); + } fNevents = fTree->GetEntries(); - + LOG(info) << "Reading " << fNevents << " entries"; // Detect format by checking branch name: // STL format uses PlaneHAPoint, TClonesArray uses vetoPoint TBranch* mcBranch = fTree->GetBranch("MCTrack"); @@ -122,6 +139,12 @@ Bool_t MuonBackGenerator::Init(const char* fileName, const int firstEvent) { } return kTRUE; } + +// ----- Default constructor ------------------------------------------- +Bool_t MuonBackGenerator::Init(const char* fileName, const int firstEvent) { + std::vector fileNames = {fileName}; + return Init(fileNames, firstEvent); +} // ----- Destructor ---------------------------------------------------- MuonBackGenerator::~MuonBackGenerator() { if (!fUseSTL) { @@ -228,8 +251,8 @@ Bool_t MuonBackGenerator::ReadEvent(FairPrimaryGenerator* cpg) { } } } - if (fn > fNevents - 1) { - LOGF(info, "End of file reached %i", fNevents); + if (fn == fNevents) { + LOGF(info, "End of tree reached %i", fNevents); return kFALSE; } if (fSameSeed) { diff --git a/shipgen/MuonBackGenerator.h b/shipgen/MuonBackGenerator.h index 169477129c..f3a297804f 100644 --- a/shipgen/MuonBackGenerator.h +++ b/shipgen/MuonBackGenerator.h @@ -27,10 +27,13 @@ class MuonBackGenerator : public FairGenerator { /** public method ReadEvent **/ Bool_t ReadEvent(FairPrimaryGenerator*); - virtual Bool_t Init(const char*, int); //! - virtual Bool_t Init(const char*); //! - Int_t GetNevents(); //! - void CloseFile(); //! + virtual Bool_t Init(const char*, int); //! + virtual Bool_t Init(const char*); //! + virtual Bool_t Init(std::vector, int); //! + virtual Bool_t Init(std::vector); //! + + Int_t GetNevents(); //! + void CloseFile(); //! void FollowAllParticles() { followMuons = false; }; void SetSmearBeam(Double_t sb) { fsmearBeam = sb; }; void SetPaintRadius(Double_t r) { fPaintBeam = r; };