From dca7376b9b3f6c1e69193866743b370fa487f84c Mon Sep 17 00:00:00 2001 From: Mark Smith Date: Fri, 6 Feb 2026 15:42:05 +0100 Subject: [PATCH 1/7] add TChain to input files --- shipgen/MuonBackGenerator.cxx | 105 +++++++++++++++++++++++++++++++++- shipgen/MuonBackGenerator.h | 3 + 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/shipgen/MuonBackGenerator.cxx b/shipgen/MuonBackGenerator.cxx index 25988d0844..c22682726d 100644 --- a/shipgen/MuonBackGenerator.cxx +++ b/shipgen/MuonBackGenerator.cxx @@ -21,6 +21,7 @@ #include "TRandom.h" #include "TSystem.h" #include "TVector.h" +#include "TChain.h" #include "vetoPoint.h" using ShipUnit::cm; @@ -38,6 +39,106 @@ MuonBackGenerator::MuonBackGenerator() Bool_t MuonBackGenerator::Init(const char* fileName) { return Init(fileName, 0); } + +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 (testFile == NULL) { +// LOG(fatal) << "Error opening the Signal file: " << fileNames.at(0); +// } + fn = firstEvent; + fPaintBeam = 5 * cm; // default value for painting beam + fSameSeed = 0; + fPhiRandomize = false; // default value for phi randomization + fsmearBeam = 8 * mm; // default value for smearing beam + fdownScaleDiMuon = kFALSE; // only needed for muflux simulation + 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 "<SetBranchAddress("id", &id); // particle id + fTree->SetBranchAddress("parentid", + &parentid); // parent id, could be different + fTree->SetBranchAddress("pythiaid", + &pythiaid); // pythiaid original particle + fTree->SetBranchAddress("ecut", &ecut); // energy cut used in simulation + fTree->SetBranchAddress("w", &w); // weight of event + // check if ntuple has information of momentum at origin + if (fTree->GetListOfLeaves()->GetSize() < 17) { + fTree->SetBranchAddress( + "x", &vx); // position with respect to startOfTarget at -89.27m + fTree->SetBranchAddress("y", &vy); + fTree->SetBranchAddress("z", &vz); + fTree->SetBranchAddress("px", &px); // momentum + fTree->SetBranchAddress("py", &py); + fTree->SetBranchAddress("pz", &pz); + } else { + fTree->SetBranchAddress( + "ox", &vx); // position with respect to startOfTarget at -50m + fTree->SetBranchAddress("oy", &vy); + fTree->SetBranchAddress("oz", &vz); + fTree->SetBranchAddress("opx", &px); // momentum + fTree->SetBranchAddress("opy", &py); + fTree->SetBranchAddress("opz", &pz); + } + } else { + id = -1; +// 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 "<GetBranch("MCTrack"); + if (!mcBranch) { + LOG(fatal) << "MCTrack branch not found in input file"; + } + + if (fTree->GetBranch("PlaneHAPoint")) { + // New STL format + fUseSTL = true; + MCTrack_vec = nullptr; + vetoPoints_vec = nullptr; + auto mcStatus = fTree->SetBranchAddress("MCTrack", &MCTrack_vec); + auto vetoStatus = + fTree->SetBranchAddress("PlaneHAPoint", &vetoPoints_vec); + if (mcStatus < 0 || vetoStatus < 0) { + LOG(fatal) << "Failed to set branch addresses for STL vector format"; + } + LOG(info) << "Using STL vector format (PlaneHAPoint)"; + } else if (fTree->GetBranch("vetoPoint")) { + // Old TClonesArray format + fUseSTL = false; + MCTrack = new TClonesArray("ShipMCTrack"); + vetoPoints = new TClonesArray("vetoPoint"); + auto mcStatus = fTree->SetBranchAddress("MCTrack", &MCTrack); + auto vetoStatus = fTree->SetBranchAddress("vetoPoint", &vetoPoints); + if (mcStatus < 0 || vetoStatus < 0) { + LOG(fatal) << "Failed to set branch addresses for TClonesArray format"; + } + LOG(info) << "Using TClonesArray format (vetoPoint)"; + } else { + LOG(fatal) + << "Neither PlaneHAPoint nor vetoPoint branch found in input file"; + } + } + return kTRUE; +} + // ----- Default constructor ------------------------------------------- Bool_t MuonBackGenerator::Init(const char* fileName, const int firstEvent) { LOG(info) << "Opening input file " << fileName; @@ -228,8 +329,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..ca6ba2b5c2 100644 --- a/shipgen/MuonBackGenerator.h +++ b/shipgen/MuonBackGenerator.h @@ -29,6 +29,9 @@ class MuonBackGenerator : public FairGenerator { Bool_t ReadEvent(FairPrimaryGenerator*); 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; }; From 29dc09061035e8851bf3dc4f86a550fb15e318c1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Feb 2026 14:43:22 +0000 Subject: [PATCH 2/7] style: pre-commit fixes --- shipgen/MuonBackGenerator.cxx | 33 +++++++++++++++++---------------- shipgen/MuonBackGenerator.h | 8 ++++---- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/shipgen/MuonBackGenerator.cxx b/shipgen/MuonBackGenerator.cxx index c22682726d..4201c0835c 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" @@ -21,7 +22,6 @@ #include "TRandom.h" #include "TSystem.h" #include "TVector.h" -#include "TChain.h" #include "vetoPoint.h" using ShipUnit::cm; @@ -40,17 +40,18 @@ Bool_t MuonBackGenerator::Init(const char* fileName) { return Init(fileName, 0); } -Bool_t MuonBackGenerator::Init(std::vector fileNames){ - return Init(fileNames, 0); +Bool_t MuonBackGenerator::Init(std::vector fileNames) { + return Init(fileNames, 0); } -Bool_t MuonBackGenerator::Init(std::vector fileNames, const int firstEvent){ +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 (testFile == NULL) { -// LOG(fatal) << "Error opening the Signal file: " << fileNames.at(0); -// } + // if (testFile == NULL) { + // LOG(fatal) << "Error opening the Signal file: " << fileNames.at(0); + // } fn = firstEvent; fPaintBeam = 5 * cm; // default value for painting beam fSameSeed = 0; @@ -59,12 +60,12 @@ Bool_t MuonBackGenerator::Init(std::vector fileNames, const int fir fdownScaleDiMuon = kFALSE; // only needed for muflux simulation 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); + for (auto& f : fileNames) { + LOG(info) << "Opening input file " << f; + static_cast(fTree)->Add(f); } fNevents = fTree->GetEntries(); - LOG(info) << "Reading "<SetBranchAddress("id", &id); // particle id fTree->SetBranchAddress("parentid", @@ -93,14 +94,14 @@ Bool_t MuonBackGenerator::Init(std::vector fileNames, const int fir } } 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); + for (auto& f : fileNames) { + LOG(info) << "Opening input file " << f; + static_cast(fTree)->Add(f); } fNevents = fTree->GetEntries(); - LOG(info) << "Reading "<GetBranch("MCTrack"); diff --git a/shipgen/MuonBackGenerator.h b/shipgen/MuonBackGenerator.h index ca6ba2b5c2..f3a297804f 100644 --- a/shipgen/MuonBackGenerator.h +++ b/shipgen/MuonBackGenerator.h @@ -27,13 +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*); //! + 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(); //! + Int_t GetNevents(); //! + void CloseFile(); //! void FollowAllParticles() { followMuons = false; }; void SetSmearBeam(Double_t sb) { fsmearBeam = sb; }; void SetPaintRadius(Double_t r) { fPaintBeam = r; }; From e767590644309475cb40adfb7f724f03996b25f7 Mon Sep 17 00:00:00 2001 From: Mark Smith Date: Fri, 6 Feb 2026 16:02:25 +0100 Subject: [PATCH 3/7] check for file --- shipgen/MuonBackGenerator.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/shipgen/MuonBackGenerator.cxx b/shipgen/MuonBackGenerator.cxx index c22682726d..619a4fa39c 100644 --- a/shipgen/MuonBackGenerator.cxx +++ b/shipgen/MuonBackGenerator.cxx @@ -45,12 +45,12 @@ Bool_t MuonBackGenerator::Init(std::vector fileNames){ } Bool_t MuonBackGenerator::Init(std::vector fileNames, const int firstEvent){ - LOG(info) << "Opening input file " << fileNames.at(0); + LOG(info) << "Looking for keys in file " << fileNames.at(0); TFile testFile(fileNames.at(0)); auto testKeys = testFile.GetListOfKeys(); -// if (testFile == NULL) { -// LOG(fatal) << "Error opening the Signal file: " << fileNames.at(0); -// } + if (testFile == nullptr) { + LOG(fatal) << "Error opening the Signal file: " << fileNames.at(0); + } fn = firstEvent; fPaintBeam = 5 * cm; // default value for painting beam fSameSeed = 0; From a491cd5a4a453946ec2af3a876466231892f8afe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Feb 2026 16:28:38 +0000 Subject: [PATCH 4/7] style: pre-commit fixes --- shipgen/MuonBackGenerator.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shipgen/MuonBackGenerator.cxx b/shipgen/MuonBackGenerator.cxx index fcd27ee514..3be00fbaea 100644 --- a/shipgen/MuonBackGenerator.cxx +++ b/shipgen/MuonBackGenerator.cxx @@ -50,7 +50,7 @@ Bool_t MuonBackGenerator::Init(std::vector fileNames, TFile testFile(fileNames.at(0)); auto testKeys = testFile.GetListOfKeys(); if (testKeys == nullptr) { - LOG(fatal) << "Error opening the Signal file: " << fileNames.at(0); + LOG(fatal) << "Error opening the Signal file: " << fileNames.at(0); } fn = firstEvent; fPaintBeam = 5 * cm; // default value for painting beam From 1611dbc2e26ee3a993a990eb7af1de4a170e0436 Mon Sep 17 00:00:00 2001 From: Mark Smith Date: Fri, 6 Feb 2026 17:46:49 +0100 Subject: [PATCH 5/7] remove duplication --- shipgen/MuonBackGenerator.cxx | 83 +---------------------------------- 1 file changed, 2 insertions(+), 81 deletions(-) diff --git a/shipgen/MuonBackGenerator.cxx b/shipgen/MuonBackGenerator.cxx index fcd27ee514..d92f0706bc 100644 --- a/shipgen/MuonBackGenerator.cxx +++ b/shipgen/MuonBackGenerator.cxx @@ -142,87 +142,8 @@ Bool_t MuonBackGenerator::Init(std::vector fileNames, // ----- 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; - } - fn = firstEvent; - fPaintBeam = 5 * cm; // default value for painting beam - fSameSeed = 0; - 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) { - fNevents = fTree->GetEntries(); - // count only events with muons - fTree->SetBranchAddress("id", &id); // particle id - fTree->SetBranchAddress("parentid", - &parentid); // parent id, could be different - fTree->SetBranchAddress("pythiaid", - &pythiaid); // pythiaid original particle - fTree->SetBranchAddress("ecut", &ecut); // energy cut used in simulation - fTree->SetBranchAddress("w", &w); // weight of event - // check if ntuple has information of momentum at origin - if (fTree->GetListOfLeaves()->GetSize() < 17) { - fTree->SetBranchAddress( - "x", &vx); // position with respect to startOfTarget at -89.27m - fTree->SetBranchAddress("y", &vy); - fTree->SetBranchAddress("z", &vz); - fTree->SetBranchAddress("px", &px); // momentum - fTree->SetBranchAddress("py", &py); - fTree->SetBranchAddress("pz", &pz); - } else { - fTree->SetBranchAddress( - "ox", &vx); // position with respect to startOfTarget at -50m - fTree->SetBranchAddress("oy", &vy); - fTree->SetBranchAddress("oz", &vz); - fTree->SetBranchAddress("opx", &px); // momentum - fTree->SetBranchAddress("opy", &py); - fTree->SetBranchAddress("opz", &pz); - } - } else { - id = -1; - fTree = fInputFile->Get("cbmsim"); - fNevents = fTree->GetEntries(); - - // Detect format by checking branch name: - // STL format uses PlaneHAPoint, TClonesArray uses vetoPoint - TBranch* mcBranch = fTree->GetBranch("MCTrack"); - if (!mcBranch) { - LOG(fatal) << "MCTrack branch not found in input file"; - } - - if (fTree->GetBranch("PlaneHAPoint")) { - // New STL format - fUseSTL = true; - MCTrack_vec = nullptr; - vetoPoints_vec = nullptr; - auto mcStatus = fTree->SetBranchAddress("MCTrack", &MCTrack_vec); - auto vetoStatus = - fTree->SetBranchAddress("PlaneHAPoint", &vetoPoints_vec); - if (mcStatus < 0 || vetoStatus < 0) { - LOG(fatal) << "Failed to set branch addresses for STL vector format"; - } - LOG(info) << "Using STL vector format (PlaneHAPoint)"; - } else if (fTree->GetBranch("vetoPoint")) { - // Old TClonesArray format - fUseSTL = false; - MCTrack = new TClonesArray("ShipMCTrack"); - vetoPoints = new TClonesArray("vetoPoint"); - auto mcStatus = fTree->SetBranchAddress("MCTrack", &MCTrack); - auto vetoStatus = fTree->SetBranchAddress("vetoPoint", &vetoPoints); - if (mcStatus < 0 || vetoStatus < 0) { - LOG(fatal) << "Failed to set branch addresses for TClonesArray format"; - } - LOG(info) << "Using TClonesArray format (vetoPoint)"; - } else { - LOG(fatal) - << "Neither PlaneHAPoint nor vetoPoint branch found in input file"; - } - } - return kTRUE; + std::vector fileNames = {fileName}; + return Init(fileNames, firstEvent); } // ----- Destructor ---------------------------------------------------- MuonBackGenerator::~MuonBackGenerator() { From 77dbe1eb88757a0a5efd64c7e9ad2ffaf06b7ad6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Feb 2026 16:49:35 +0000 Subject: [PATCH 6/7] style: pre-commit fixes --- shipgen/MuonBackGenerator.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shipgen/MuonBackGenerator.cxx b/shipgen/MuonBackGenerator.cxx index fbff27e0dc..e5bde3a878 100644 --- a/shipgen/MuonBackGenerator.cxx +++ b/shipgen/MuonBackGenerator.cxx @@ -142,8 +142,8 @@ Bool_t MuonBackGenerator::Init(std::vector fileNames, // ----- Default constructor ------------------------------------------- Bool_t MuonBackGenerator::Init(const char* fileName, const int firstEvent) { - std::vector fileNames = {fileName}; - return Init(fileNames, firstEvent); + std::vector fileNames = {fileName}; + return Init(fileNames, firstEvent); } // ----- Destructor ---------------------------------------------------- MuonBackGenerator::~MuonBackGenerator() { From ae86e68dfaaa9cd49008585e291af19ea4496583 Mon Sep 17 00:00:00 2001 From: Mark Smith Date: Fri, 6 Feb 2026 18:23:24 +0100 Subject: [PATCH 7/7] add list of files --- python/rootUtils.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) 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'