From 2ed32c9f4b59dd56723b097308a81fd1d04e4803 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 14:45:25 +0100 Subject: [PATCH 01/18] remove muted (as a member) for TRestGeant4PhysicsInfo --- inc/TRestGeant4PhysicsInfo.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/inc/TRestGeant4PhysicsInfo.h b/inc/TRestGeant4PhysicsInfo.h index ad218dd..a3e75f7 100644 --- a/inc/TRestGeant4PhysicsInfo.h +++ b/inc/TRestGeant4PhysicsInfo.h @@ -11,7 +11,7 @@ class G4VProcess; class TRestGeant4PhysicsInfo { - ClassDef(TRestGeant4PhysicsInfo, 2); + ClassDef(TRestGeant4PhysicsInfo, 3); private: std::map fProcessNamesMap = {}; @@ -22,8 +22,6 @@ class TRestGeant4PhysicsInfo { std::map fProcessTypesMap = {}; // process name -> process type - std::mutex fMutex; //! - public: TString GetProcessName(Int_t id) const; Int_t GetProcessID(const TString& processName) const; From 507e7db7a7eabba1539c796c4f91f345b309e0c5 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 14:45:36 +0100 Subject: [PATCH 02/18] add muted to src --- src/TRestGeant4PhysicsInfo.cxx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/TRestGeant4PhysicsInfo.cxx b/src/TRestGeant4PhysicsInfo.cxx index 0f833ae..b32b0b5 100644 --- a/src/TRestGeant4PhysicsInfo.cxx +++ b/src/TRestGeant4PhysicsInfo.cxx @@ -7,6 +7,8 @@ using namespace std; ClassImp(TRestGeant4PhysicsInfo); +std::mutex insertMutex; + set TRestGeant4PhysicsInfo::GetAllParticles() const { set particles = {}; for (const auto& [_, name] : fParticleNamesMap) { @@ -59,7 +61,7 @@ void TRestGeant4PhysicsInfo::InsertProcessName(Int_t id, const TString& processN if (fProcessNamesMap.count(id) > 0) { return; } - std::lock_guard lock(fMutex); + std::lock_guard lock(insertMutex); fProcessNamesMap[id] = processName; fProcessNamesReverseMap[processName] = id; @@ -70,7 +72,7 @@ void TRestGeant4PhysicsInfo::InsertParticleName(Int_t id, const TString& particl if (fParticleNamesMap.count(id) > 0) { return; } - std::lock_guard lock(fMutex); + std::lock_guard lock(insertMutex); fParticleNamesMap[id] = particleName; fParticleNamesReverseMap[particleName] = id; } From ff9c85cb92c2012bb8d470261084032f276b23b8 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 14:46:30 +0100 Subject: [PATCH 03/18] add merge to TRestGeant4Metadata. Implement required operators --- inc/TRestGeant4Metadata.h | 12 ++++++++++-- src/TRestGeant4Metadata.cxx | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 31a89fc..4eb922c 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -56,6 +56,9 @@ class TRestGeant4Metadata : public TRestMetadata { void ReadDetector(); void ReadBiasing(); + // Metadata is the result of a merge of other metadata + bool fIsMerge = false; + bool fDetectorSectionInitialized = false; //! /// Class used to store and retrieve geometry info @@ -308,7 +311,7 @@ class TRestGeant4Metadata : public TRestMetadata { /// Returns the probability per event to register (write to disk) hits in a GDML volume given its geometry /// name. - Double_t GetStorageChance(TString volume); + Double_t GetStorageChance(const TString& volume); Double_t GetMaxStepSize(const TString& volume); @@ -374,12 +377,17 @@ class TRestGeant4Metadata : public TRestMetadata { void PrintMetadata() override; + void Merge(const TRestGeant4Metadata&); + TRestGeant4Metadata(); TRestGeant4Metadata(const char* configFilename, const std::string& name = ""); ~TRestGeant4Metadata(); - ClassDefOverride(TRestGeant4Metadata, 13); + TRestGeant4Metadata(const TRestGeant4Metadata& metadata); + TRestGeant4Metadata& operator=(const TRestGeant4Metadata& metadata); + + ClassDefOverride(TRestGeant4Metadata, 14); // Allow modification of otherwise inaccessible / immutable members that shouldn't be modified by the user friend class SteppingAction; diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 8059c77..d89c3ce 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1233,7 +1233,7 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, void TRestGeant4Metadata::RemoveParticleSources() { for (auto source : fParticleSource) { - delete source; + // delete source; } fParticleSource.clear(); } From 7eefd7583e975568f5b2240a71103684e904c203 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 14:47:02 +0100 Subject: [PATCH 04/18] add merge to TRestGeant4Metadata. Implement required operators --- src/TRestGeant4Metadata.cxx | 48 ++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index d89c3ce..b0fb8d0 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1536,7 +1536,7 @@ Bool_t TRestGeant4Metadata::isVolumeStored(const TString& volume) const { /////////////////////////////////////////////// /// \brief Returns the probability of an active volume being stored /// -Double_t TRestGeant4Metadata::GetStorageChance(TString volume) { +Double_t TRestGeant4Metadata::GetStorageChance(const TString& volume) { Int_t id; for (id = 0; id < (Int_t)fActiveVolumes.size(); id++) { if (fActiveVolumes[id] == volume) return fChance[id]; @@ -1571,3 +1571,49 @@ size_t TRestGeant4Metadata::GetGeant4VersionMajor() const { } return std::stoi(majorVersion.Data()); } + +void TRestGeant4Metadata::Merge(const TRestGeant4Metadata& metadata) { + fIsMerge = true; + + fNEvents += metadata.fNEvents; + fNRequestedEntries += metadata.fNRequestedEntries; + fSeed = 0; // seed makes no sense in a merged file +} + +TRestGeant4Metadata::TRestGeant4Metadata(const TRestGeant4Metadata& metadata) { + fIsMerge = metadata.fIsMerge; + fDetectorSectionInitialized = metadata.fDetectorSectionInitialized; + fGeant4GeometryInfo = metadata.fGeant4GeometryInfo; + fGeant4PhysicsInfo = metadata.fGeant4PhysicsInfo; + fGeant4PrimaryGeneratorInfo = metadata.fGeant4PrimaryGeneratorInfo; + fGeant4Version = metadata.fGeant4Version; + fGdmlReference = metadata.fGdmlReference; + fMaterialsReference = metadata.fMaterialsReference; + fEnergyRangeStored = metadata.fEnergyRangeStored; + fActiveVolumes = metadata.fActiveVolumes; + fChance = metadata.fChance; + fMaxStepSize = metadata.fMaxStepSize; + fParticleSource = metadata.fParticleSource; //-> + fNBiasingVolumes = metadata.fNBiasingVolumes; + fBiasingVolumes = metadata.fBiasingVolumes; + fMaxTargetStepSize = metadata.fMaxTargetStepSize; + fSubEventTimeDelay = metadata.fSubEventTimeDelay; + fFullChain = metadata.fFullChain; + fSensitiveVolumes = metadata.fSensitiveVolumes; + fNEvents = metadata.fNEvents; + fNRequestedEntries = metadata.fNRequestedEntries; + fSimulationMaxTimeSeconds = metadata.fSimulationMaxTimeSeconds; + fSeed = metadata.fSeed; + fSaveAllEvents = metadata.fSaveAllEvents; + fRemoveUnwantedTracks = metadata.fRemoveUnwantedTracks; + fRemoveUnwantedTracksKeepZeroEnergyTracks = metadata.fRemoveUnwantedTracksKeepZeroEnergyTracks; + fRemoveUnwantedTracksVolumesToKeep = metadata.fRemoveUnwantedTracksVolumesToKeep; + fKillVolumes = metadata.fKillVolumes; + fRegisterEmptyTracks = metadata.fRegisterEmptyTracks; + fMagneticField = metadata.fMagneticField; +} + +TRestGeant4Metadata& TRestGeant4Metadata::operator=(const TRestGeant4Metadata& metadata) { + // use copy constructor + TRestGeant4Metadata tmp(metadata); +} From a110a49c5fe97bc9c41cd689ac857f1cb5b73ada Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 15:05:31 +0100 Subject: [PATCH 05/18] improve operators --- inc/TRestGeant4Metadata.h | 2 +- src/TRestGeant4Metadata.cxx | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 4eb922c..44d762c 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -369,7 +369,7 @@ class TRestGeant4Metadata : public TRestMetadata { /// Returns the world magnetic field in Tesla inline TVector3 GetMagneticField() const { return fMagneticField; } - Int_t GetActiveVolumeID(TString name); + Int_t GetActiveVolumeID(const TString& name); Bool_t isVolumeStored(const TString& volume) const; diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index b0fb8d0..2be2f4a 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1485,7 +1485,7 @@ void TRestGeant4Metadata::PrintMetadata() { /////////////////////////////////////////////// /// \brief Returns the id of an active volume giving as parameter its name. -Int_t TRestGeant4Metadata::GetActiveVolumeID(TString name) { +Int_t TRestGeant4Metadata::GetActiveVolumeID(const TString& name) { Int_t id; for (id = 0; id < (Int_t)fActiveVolumes.size(); id++) { if (fActiveVolumes[id] == name) return id; @@ -1580,7 +1580,9 @@ void TRestGeant4Metadata::Merge(const TRestGeant4Metadata& metadata) { fSeed = 0; // seed makes no sense in a merged file } -TRestGeant4Metadata::TRestGeant4Metadata(const TRestGeant4Metadata& metadata) { +TRestGeant4Metadata::TRestGeant4Metadata(const TRestGeant4Metadata& metadata) { *this = metadata; } + +TRestGeant4Metadata& TRestGeant4Metadata::operator=(const TRestGeant4Metadata& metadata) { fIsMerge = metadata.fIsMerge; fDetectorSectionInitialized = metadata.fDetectorSectionInitialized; fGeant4GeometryInfo = metadata.fGeant4GeometryInfo; @@ -1611,9 +1613,5 @@ TRestGeant4Metadata::TRestGeant4Metadata(const TRestGeant4Metadata& metadata) { fKillVolumes = metadata.fKillVolumes; fRegisterEmptyTracks = metadata.fRegisterEmptyTracks; fMagneticField = metadata.fMagneticField; -} - -TRestGeant4Metadata& TRestGeant4Metadata::operator=(const TRestGeant4Metadata& metadata) { - // use copy constructor - TRestGeant4Metadata tmp(metadata); + return *this; } From 5c70e5d1a134716dfb16a6e1fc3d3364b86d2999 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 15:05:52 +0100 Subject: [PATCH 06/18] add macro to merge files --- macros/REST_Geant4_MergeFiles.C | 94 +++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 macros/REST_Geant4_MergeFiles.C diff --git a/macros/REST_Geant4_MergeFiles.C b/macros/REST_Geant4_MergeFiles.C new file mode 100644 index 0000000..a50da91 --- /dev/null +++ b/macros/REST_Geant4_MergeFiles.C @@ -0,0 +1,94 @@ +#include +#include + +#include "TRestGeant4Event.h" +#include "TRestGeant4Metadata.h" +#include "TRestTask.h" + +#ifndef RestTask_Geant4_MergeFiles +#define RestTask_Geant4_MergeFiles + +//******************************************************************************************************* +//*** +//*** Your HELP is needed to verify, validate and document this macro. +//*** This macro might need update/revision. +//*** +//******************************************************************************************************* + +/* + * Author: Luis Obis (lobis@unizar.es) + * Description: Macro used to merge simulation files, before any analysis is performed + * All files are understood to have the same configuration (same generator, same detector, etc.) + * They can only differ in the random seed, run number or number of events. + */ + +using namespace std; + +void REST_Geant4_MergeFiles(const char* outputFilename, const char* inputFilesDirectory) { + // TODO: use glob pattern instead of directory. Already tried this but conflicts with TRestTask... + + cout << "Output file: " << outputFilename << endl; + // print input + cout << "Input files directory: " << inputFilesDirectory << endl; + + // find all .root files in the directory + vector inputFiles = TRestTools::GetFilesMatchingPattern(string(inputFilesDirectory) + "/*.root"); + + cout << "Number of input files: " << inputFiles.size() << endl; + for (auto file : inputFiles) { + cout << " - " << file << endl; + } + + if (inputFiles.size() == 0) { + cerr << "No input files found" << endl; + exit(1); + } + + // open the first file + TRestGeant4Metadata mergeMetadata; + + auto mergeRun = new TRestRun(); + mergeRun->SetName("run"); + mergeRun->SetOutputFileName(outputFilename); + mergeRun->FormOutputFile(); + mergeRun->GetOutputFile()->cd(); + mergeRun->SetRunType("restG4"); + + TRestGeant4Event* mergeEvent = nullptr; + auto mergeEventTree = mergeRun->GetEventTree(); + mergeEventTree->Branch("TRestGeant4EventBranch", "TRestGeant4Event", &mergeEvent); + + // iterate over all other files + for (int i = 0; i < inputFiles.size(); i++) { + auto run = TRestRun(inputFiles[i].c_str()); + auto metadata = (TRestGeant4Metadata*)run.GetMetadataClass("TRestGeant4Metadata"); + if (i == 0) { + mergeMetadata = *metadata; + } else { + mergeMetadata.Merge(*metadata); + } + TRestGeant4Event* event = nullptr; + auto eventTree = run.GetEventTree(); + eventTree->SetBranchAddress("TRestGeant4EventBranch", &event); + for (int j = 0; j < eventTree->GetEntries(); j++) { + eventTree->GetEntry(j); + *mergeEvent = *event; + mergeEventTree->Fill(); + mergeRun->GetAnalysisTree()->Fill(); + } + } + + cout << "Output filename: " << mergeRun->GetOutputFileName() << endl; + cout << "Output file: " << mergeRun->GetOutputFile() << endl; + + mergeRun->GetOutputFile()->cd(); + + gGeoManager->Write("Geometry", TObject::kOverwrite); + // mergeRun->AddMetadata(static_cast(&mergeMetadata)); + mergeMetadata.SetName("geant4Metadata"); + mergeMetadata.Write(); + mergeRun->UpdateOutputFile(); + mergeRun->CloseFile(); +} + +#endif From 7583696376ba73ab9328b322c678203e42bfd0d9 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 15:20:30 +0100 Subject: [PATCH 07/18] fix destructor --- src/TRestGeant4Metadata.cxx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 2be2f4a..1e3024b 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -768,7 +768,13 @@ TRestGeant4Metadata::TRestGeant4Metadata(const char* configFilename, const strin /////////////////////////////////////////////// /// \brief Default destructor /// -TRestGeant4Metadata::~TRestGeant4Metadata() { RemoveParticleSources(); } +TRestGeant4Metadata::~TRestGeant4Metadata() { + /* + * Problem with the merge macro + * TODO: Why? + RemoveParticleSources(); + */ +} /////////////////////////////////////////////// /// \brief Initialization of TRestGeant4Metadata members @@ -1233,7 +1239,7 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, void TRestGeant4Metadata::RemoveParticleSources() { for (auto source : fParticleSource) { - // delete source; + delete source; } fParticleSource.clear(); } From 854510b241508aceed03a3835ff595100cfacb19 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 16:43:38 +0100 Subject: [PATCH 08/18] add macro --- CMakeLists.txt | 5 +++-- ...T_Geant4_MergeFiles.C => REST_Geant4_MergeRestG4Files.C} | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) rename macros/{REST_Geant4_MergeFiles.C => REST_Geant4_MergeRestG4Files.C} (94%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0acf451..e513b3d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,8 +59,9 @@ install(FILES ${MAC} DESTINATION ./macros/geant4) set(rest_macros ${rest_macros} "restGeant4_ViewEvent") set(rest_macros ${rest_macros} "restGeant4_ViewGeometry") +set(rest_macros ${rest_macros} "restGeant4_MergeRestG4Files") set(rest_macros - ${rest_macros} - PARENT_SCOPE) + ${rest_macros} + PARENT_SCOPE) add_library_test() diff --git a/macros/REST_Geant4_MergeFiles.C b/macros/REST_Geant4_MergeRestG4Files.C similarity index 94% rename from macros/REST_Geant4_MergeFiles.C rename to macros/REST_Geant4_MergeRestG4Files.C index a50da91..01dc787 100644 --- a/macros/REST_Geant4_MergeFiles.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -5,8 +5,8 @@ #include "TRestGeant4Metadata.h" #include "TRestTask.h" -#ifndef RestTask_Geant4_MergeFiles -#define RestTask_Geant4_MergeFiles +#ifndef RestTask_Geant4_MergeRestG4Files +#define RestTask_Geant4_MergeRestG4Files //******************************************************************************************************* //*** @@ -24,7 +24,7 @@ using namespace std; -void REST_Geant4_MergeFiles(const char* outputFilename, const char* inputFilesDirectory) { +void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputFilesDirectory) { // TODO: use glob pattern instead of directory. Already tried this but conflicts with TRestTask... cout << "Output file: " << outputFilename << endl; From 533d64e789db34ab67c1eddc4457115e956ee6a3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Mar 2023 16:08:28 +0000 Subject: [PATCH 09/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e513b3d..ba4b16d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ set(rest_macros ${rest_macros} "restGeant4_ViewEvent") set(rest_macros ${rest_macros} "restGeant4_ViewGeometry") set(rest_macros ${rest_macros} "restGeant4_MergeRestG4Files") set(rest_macros - ${rest_macros} - PARENT_SCOPE) + ${rest_macros} + PARENT_SCOPE) add_library_test() From a481206ead6518f891eaaafff343a805cdf58bf0 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 21:13:22 +0100 Subject: [PATCH 10/18] fix segfault --- src/TRestGeant4Metadata.cxx | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 1e3024b..a9bd23e 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -768,13 +768,7 @@ TRestGeant4Metadata::TRestGeant4Metadata(const char* configFilename, const strin /////////////////////////////////////////////// /// \brief Default destructor /// -TRestGeant4Metadata::~TRestGeant4Metadata() { - /* - * Problem with the merge macro - * TODO: Why? - RemoveParticleSources(); - */ -} +TRestGeant4Metadata::~TRestGeant4Metadata() { RemoveParticleSources(); } /////////////////////////////////////////////// /// \brief Initialization of TRestGeant4Metadata members @@ -1601,7 +1595,7 @@ TRestGeant4Metadata& TRestGeant4Metadata::operator=(const TRestGeant4Metadata& m fActiveVolumes = metadata.fActiveVolumes; fChance = metadata.fChance; fMaxStepSize = metadata.fMaxStepSize; - fParticleSource = metadata.fParticleSource; //-> + // fParticleSource = metadata.fParticleSource; // segfaults (pointers!) fNBiasingVolumes = metadata.fNBiasingVolumes; fBiasingVolumes = metadata.fBiasingVolumes; fMaxTargetStepSize = metadata.fMaxTargetStepSize; From a64fc787206c4292f2df48fdc062aba336ea9e65 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 5 May 2023 12:39:59 +0200 Subject: [PATCH 11/18] remove unused member from constructor --- src/TRestGeant4Metadata.cxx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 750991d..90fc509 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1584,7 +1584,6 @@ TRestGeant4Metadata::TRestGeant4Metadata(const TRestGeant4Metadata& metadata) { TRestGeant4Metadata& TRestGeant4Metadata::operator=(const TRestGeant4Metadata& metadata) { fIsMerge = metadata.fIsMerge; - fDetectorSectionInitialized = metadata.fDetectorSectionInitialized; fGeant4GeometryInfo = metadata.fGeant4GeometryInfo; fGeant4PhysicsInfo = metadata.fGeant4PhysicsInfo; fGeant4PrimaryGeneratorInfo = metadata.fGeant4PrimaryGeneratorInfo; From e8789a8c7ee6aa7eb06bb1a4962cad5896cd2020 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Sat, 19 Aug 2023 20:12:47 +0200 Subject: [PATCH 12/18] do not use repeated event ids --- macros/REST_Geant4_MergeRestG4Files.C | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 01dc787..1716330 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -58,6 +58,8 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF auto mergeEventTree = mergeRun->GetEventTree(); mergeEventTree->Branch("TRestGeant4EventBranch", "TRestGeant4Event", &mergeEvent); + set eventIds; + // iterate over all other files for (int i = 0; i < inputFiles.size(); i++) { auto run = TRestRun(inputFiles[i].c_str()); @@ -72,6 +74,13 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF eventTree->SetBranchAddress("TRestGeant4EventBranch", &event); for (int j = 0; j < eventTree->GetEntries(); j++) { eventTree->GetEntry(j); + Int_t eventId = event->GetID(); + if (eventIds.find(eventId) != eventIds.end()) { + const maxEventId = *max_element(eventIds.begin(), eventIds.end()); + eventId = maxEventId + 1; + } + eventIds.insert(eventId); + event->SetID(eventId); *mergeEvent = *event; mergeEventTree->Fill(); mergeRun->GetAnalysisTree()->Fill(); From abe23720eb0a123b3824a453d961baf61f673585 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Sat, 19 Aug 2023 20:17:59 +0200 Subject: [PATCH 13/18] update merge logic --- macros/REST_Geant4_MergeRestG4Files.C | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 1716330..85a73dd 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -74,14 +74,16 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF eventTree->SetBranchAddress("TRestGeant4EventBranch", &event); for (int j = 0; j < eventTree->GetEntries(); j++) { eventTree->GetEntry(j); - Int_t eventId = event->GetID(); + *mergeEvent = *event; + + Int_t eventId = mergeEvent->GetID(); if (eventIds.find(eventId) != eventIds.end()) { const maxEventId = *max_element(eventIds.begin(), eventIds.end()); eventId = maxEventId + 1; } eventIds.insert(eventId); - event->SetID(eventId); - *mergeEvent = *event; + mergeEvent->SetID(eventId); + mergeEventTree->Fill(); mergeRun->GetAnalysisTree()->Fill(); } @@ -93,7 +95,7 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF mergeRun->GetOutputFile()->cd(); gGeoManager->Write("Geometry", TObject::kOverwrite); - // mergeRun->AddMetadata(static_cast(&mergeMetadata)); + mergeMetadata.SetName("geant4Metadata"); mergeMetadata.Write(); mergeRun->UpdateOutputFile(); From 3be65203b3ba6bcd98e8776920b588e2d60e346a Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Sat, 19 Aug 2023 20:28:24 +0200 Subject: [PATCH 14/18] fix merge macro --- macros/REST_Geant4_MergeRestG4Files.C | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 85a73dd..d094e31 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -78,11 +78,11 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF Int_t eventId = mergeEvent->GetID(); if (eventIds.find(eventId) != eventIds.end()) { - const maxEventId = *max_element(eventIds.begin(), eventIds.end()); + const Int_t maxEventId = *max_element(eventIds.begin(), eventIds.end()); eventId = maxEventId + 1; } - eventIds.insert(eventId); mergeEvent->SetID(eventId); + eventIds.insert(mergeEvent->GetID()); mergeEventTree->Fill(); mergeRun->GetAnalysisTree()->Fill(); From 602003a9b5c4779cb8854e2b071325eeb8980c4e Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Sat, 19 Aug 2023 20:29:10 +0200 Subject: [PATCH 15/18] update docs --- macros/REST_Geant4_MergeRestG4Files.C | 3 +++ 1 file changed, 3 insertions(+) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index d094e31..5221f4a 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -22,6 +22,9 @@ * They can only differ in the random seed, run number or number of events. */ +// Usage: +// restGeant4_MergeRestG4Files merge_result.root /path/to/directory/with/files/*.root + using namespace std; void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputFilesDirectory) { From 3ed2a8076be7ce1ad308b45853b17f96ed08d521 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Sun, 20 Aug 2023 01:47:31 +0200 Subject: [PATCH 16/18] handle sub-events --- macros/REST_Geant4_MergeRestG4Files.C | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 5221f4a..016dedf 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -65,6 +65,9 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF // iterate over all other files for (int i = 0; i < inputFiles.size(); i++) { + map + eventIdUpdates; // repeatedId -> newId. Make sure if there are repeated event ids in a file + // (because of sub-events) they keep the same event id after modification auto run = TRestRun(inputFiles[i].c_str()); auto metadata = (TRestGeant4Metadata*)run.GetMetadataClass("TRestGeant4Metadata"); if (i == 0) { @@ -80,9 +83,17 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF *mergeEvent = *event; Int_t eventId = mergeEvent->GetID(); - if (eventIds.find(eventId) != eventIds.end()) { + if (eventIdUpdates.find(eventId) != eventIdUpdates.end()) { + eventId = eventIdUpdates[eventId]; + cout << "WARNING: event ID " << mergeEvent->GetID() << " with sub-event ID " + << mergeEvent->GetSubID() << " already exists. It was already changed to " << eventId + << endl; + } else if (eventIds.find(eventId) != eventIds.end()) { const Int_t maxEventId = *max_element(eventIds.begin(), eventIds.end()); eventId = maxEventId + 1; + eventIdUpdates[mergeEvent->GetID()] = eventId; + cout << "WARNING: event ID " << mergeEvent->GetID() << " with sub-event ID " + << mergeEvent->GetSubID() " already exists. Changing to " << eventId << endl; } mergeEvent->SetID(eventId); eventIds.insert(mergeEvent->GetID()); From 8cac93f98f33cee17ae637bdf8224805322bc61a Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Sun, 20 Aug 2023 02:06:27 +0200 Subject: [PATCH 17/18] fix bug --- macros/REST_Geant4_MergeRestG4Files.C | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 016dedf..732c62c 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -93,7 +93,7 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF eventId = maxEventId + 1; eventIdUpdates[mergeEvent->GetID()] = eventId; cout << "WARNING: event ID " << mergeEvent->GetID() << " with sub-event ID " - << mergeEvent->GetSubID() " already exists. Changing to " << eventId << endl; + << mergeEvent->GetSubID() << " already exists. Changing to " << eventId << endl; } mergeEvent->SetID(eventId); eventIds.insert(mergeEvent->GetID()); From 014e28e6cafe18941acbf00b6e5ebb2ac1767657 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Sun, 20 Aug 2023 12:57:46 +0200 Subject: [PATCH 18/18] update merge --- macros/REST_Geant4_MergeRestG4Files.C | 2 ++ 1 file changed, 2 insertions(+) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 732c62c..d2cdb01 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -65,6 +65,8 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF // iterate over all other files for (int i = 0; i < inputFiles.size(); i++) { + cout << "Processing file " << i + 1 << "/" << inputFiles.size() << endl; + map eventIdUpdates; // repeatedId -> newId. Make sure if there are repeated event ids in a file // (because of sub-events) they keep the same event id after modification