From 12b1086e4a479ab918c4071c6d2daa1fddbfb7ff Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 19 Sep 2023 00:58:38 +0200 Subject: [PATCH 01/14] merge name --- macros/REST_Geant4_MergeRestG4Files.C | 2 +- src/TRestGeant4Metadata.cxx | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 3cf1b77..c37d4c0 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -119,8 +119,8 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF gGeoManager->Write("Geometry", TObject::kOverwrite); - mergeMetadata.SetName("geant4Metadata"); mergeMetadata.Write(); + mergeRun->UpdateOutputFile(); mergeRun->CloseFile(); diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 1ddd038..54c1c24 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1576,6 +1576,10 @@ void TRestGeant4Metadata::Merge(const TRestGeant4Metadata& metadata) { fIsMerge = true; fSeed = 0; // seed makes no sense in a merged file + if (fName.IsNull()) { + fName = metadata.fName; + } + fNEvents += metadata.fNEvents; fNRequestedEntries += metadata.fNRequestedEntries; fSimulationTime += metadata.fSimulationTime; @@ -1585,6 +1589,7 @@ TRestGeant4Metadata::TRestGeant4Metadata(const TRestGeant4Metadata& metadata) { TRestGeant4Metadata& TRestGeant4Metadata::operator=(const TRestGeant4Metadata& metadata) { fIsMerge = metadata.fIsMerge; + fName = metadata.fName; fGeant4GeometryInfo = metadata.fGeant4GeometryInfo; fGeant4PhysicsInfo = metadata.fGeant4PhysicsInfo; fGeant4PrimaryGeneratorInfo = metadata.fGeant4PrimaryGeneratorInfo; From f95e3097eaa608131b1f7b36c98db538e2a4b027 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 19 Sep 2023 00:58:46 +0200 Subject: [PATCH 02/14] add other metadata keys --- macros/REST_Geant4_MergeRestG4Files.C | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index c37d4c0..8911e02 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -110,6 +110,25 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF eventCounter++; } + + // add the remaining metadata of the first file to the merged file + if (i == 0) { + // iterate over all keys of the file to find inheritance of TRestMetadata + TIter nextkey(run.GetInputFile()->GetListOfKeys()); + TKey* key; + while ((key = (TKey*)nextkey())) { + const auto obj = key->ReadObj(); + if (obj->InheritsFrom("TRestMetadata")) { + if (obj->InheritsFrom("TRestGeant4Metadata")) { + // This is merged and added later + continue; + } + const auto metadata = (TRestMetadata*)obj; + mergeRun->GetOutputFile()->cd(); + metadata->Write(metadata->GetName(), TObject::kOverwrite); + } + } + } } cout << "Output filename: " << mergeRun->GetOutputFileName() << endl; From bd54e401fab298f52d3de9746e4dc9a0bb67d5d5 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 19 Sep 2023 01:12:52 +0200 Subject: [PATCH 03/14] fix crash on analysis files --- macros/REST_Geant4_MergeRestG4Files.C | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 8911e02..9b8a075 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -136,7 +136,9 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF mergeRun->GetOutputFile()->cd(); - gGeoManager->Write("Geometry", TObject::kOverwrite); + if (gGeoManager != nullptr) { + gGeoManager->Write("Geometry", TObject::kOverwrite); + } mergeMetadata.Write(); @@ -150,8 +152,6 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF << ") does not match the number of events in the input files (" << eventCounter << ")" << endl; exit(1); } - cout << "Number of events in the output file: " << runCheck.GetEntries() << " matches internal count" - << endl; } #endif From 99393b49d4bffeb7b5aa6d29582d9be25e484616 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 19 Sep 2023 01:28:57 +0200 Subject: [PATCH 04/14] add TODO --- macros/REST_Geant4_MergeRestG4Files.C | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 9b8a075..0800e8c 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -59,6 +59,7 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF TRestGeant4Event* mergeEvent = nullptr; auto mergeEventTree = mergeRun->GetEventTree(); + auto mergeAnalysisTree = mergeRun->GetAnalysisTree(); mergeEventTree->Branch("TRestGeant4EventBranch", "TRestGeant4Event", &mergeEvent); set eventIds; // std::set is sorted from lower to higher automatically @@ -80,6 +81,7 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF } TRestGeant4Event* event = nullptr; auto eventTree = run.GetEventTree(); + // auto analysisTree = run.GetAnalysisTree(); eventTree->SetBranchAddress("TRestGeant4EventBranch", &event); for (int j = 0; j < eventTree->GetEntries(); j++) { eventTree->GetEntry(j); @@ -106,7 +108,8 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF eventIds.insert(mergeEvent->GetID()); mergeEventTree->Fill(); - mergeRun->GetAnalysisTree()->Fill(); + // TODO: this just adds empty entries to the analysis tree. It should be merged + mergeAnalysisTree->Fill(); eventCounter++; } @@ -123,9 +126,9 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF // This is merged and added later continue; } - const auto metadata = (TRestMetadata*)obj; + const auto metadataKey = (TRestMetadata*)obj; mergeRun->GetOutputFile()->cd(); - metadata->Write(metadata->GetName(), TObject::kOverwrite); + metadataKey->Write(); } } } From e09868355c0dcd09d376b5bd8775cb641d02fd4d Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 19 Sep 2023 14:26:42 +0200 Subject: [PATCH 05/14] add Merge to base metadata class --- inc/TRestGeant4Metadata.h | 2 +- src/TRestGeant4Metadata.cxx | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index fcbe445..a8693eb 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -383,7 +383,7 @@ class TRestGeant4Metadata : public TRestMetadata { void PrintMetadata() override; - void Merge(const TRestGeant4Metadata&); + void Merge(const TRestMetadata&) override; TRestGeant4Metadata(); TRestGeant4Metadata(const char* configFilename, const std::string& name = ""); diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 54c1c24..e18f6b9 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1572,17 +1572,16 @@ size_t TRestGeant4Metadata::GetGeant4VersionMajor() const { return std::stoi(majorVersion.Data()); } -void TRestGeant4Metadata::Merge(const TRestGeant4Metadata& metadata) { +void TRestGeant4Metadata::Merge(const TRestMetadata& metadata) { + TRestMetadata::Merge(metadata); + fIsMerge = true; fSeed = 0; // seed makes no sense in a merged file - if (fName.IsNull()) { - fName = metadata.fName; - } - - fNEvents += metadata.fNEvents; - fNRequestedEntries += metadata.fNRequestedEntries; - fSimulationTime += metadata.fSimulationTime; + const auto geant4Metadata = dynamic_cast(&metadata); + fNEvents += geant4Metadata->fNEvents; + fNRequestedEntries += geant4Metadata->fNRequestedEntries; + fSimulationTime += geant4Metadata->fSimulationTime; } TRestGeant4Metadata::TRestGeant4Metadata(const TRestGeant4Metadata& metadata) { *this = metadata; } From 2c6bb5b2158a016bf3a51561eae60131ec9d0f88 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 19 Sep 2023 18:29:19 +0200 Subject: [PATCH 06/14] Revert "add Merge to base metadata class" This reverts commit e09868355c0dcd09d376b5bd8775cb641d02fd4d. --- inc/TRestGeant4Metadata.h | 2 +- src/TRestGeant4Metadata.cxx | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index a8693eb..fcbe445 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -383,7 +383,7 @@ class TRestGeant4Metadata : public TRestMetadata { void PrintMetadata() override; - void Merge(const TRestMetadata&) override; + void Merge(const TRestGeant4Metadata&); TRestGeant4Metadata(); TRestGeant4Metadata(const char* configFilename, const std::string& name = ""); diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index e18f6b9..54c1c24 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1572,16 +1572,17 @@ size_t TRestGeant4Metadata::GetGeant4VersionMajor() const { return std::stoi(majorVersion.Data()); } -void TRestGeant4Metadata::Merge(const TRestMetadata& metadata) { - TRestMetadata::Merge(metadata); - +void TRestGeant4Metadata::Merge(const TRestGeant4Metadata& metadata) { fIsMerge = true; fSeed = 0; // seed makes no sense in a merged file - const auto geant4Metadata = dynamic_cast(&metadata); - fNEvents += geant4Metadata->fNEvents; - fNRequestedEntries += geant4Metadata->fNRequestedEntries; - fSimulationTime += geant4Metadata->fSimulationTime; + if (fName.IsNull()) { + fName = metadata.fName; + } + + fNEvents += metadata.fNEvents; + fNRequestedEntries += metadata.fNRequestedEntries; + fSimulationTime += metadata.fSimulationTime; } TRestGeant4Metadata::TRestGeant4Metadata(const TRestGeant4Metadata& metadata) { *this = metadata; } From f52517532737fd6d99365198ef47179037ff6020 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 19 Sep 2023 23:14:46 +0200 Subject: [PATCH 07/14] use TFileMerger --- macros/REST_Geant4_MergeRestG4Files.C | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 0800e8c..27f1798 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -1,3 +1,5 @@ +#include + #include #include @@ -62,6 +64,18 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF auto mergeAnalysisTree = mergeRun->GetAnalysisTree(); mergeEventTree->Branch("TRestGeant4EventBranch", "TRestGeant4Event", &mergeEvent); + const string outputTempFilename = + string(outputFilename).substr(0, string(outputFilename).size() - 5) + ".temp.root"; + { + TFileMerger merger; + merger.OutputFile(outputTempFilename.c_str()); + merger.AddObjectNames("EventTree AnalysisTree"); + for (int i = 0; i < inputFiles.size(); i++) { + merger.AddFile(inputFiles[i].c_str()); + } + merger.PartialMerge(TFileMerger::kAll | TFileMerger::kIncremental | TFileMerger::kOnlyListed); + } + set eventIds; // std::set is sorted from lower to higher automatically long long eventCounter = 0; From 2742baa73e615050a4f88ba80d0eded5114c4cdd Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Thu, 21 Sep 2023 12:48:58 +0200 Subject: [PATCH 08/14] use new virtual Merge method --- inc/TRestGeant4Metadata.h | 10 +++++++++- src/TRestGeant4Metadata.cxx | 15 +++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index fcbe445..2c59463 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -137,6 +137,14 @@ class TRestGeant4Metadata : public TRestMetadata { /// If it is zero its value will be assigned using the system timestamp. Long_t fSeed = 0; + /// \brief If the simulation is produced as a merge of multiple files, this stored the seeds of the + /// individual files + std::vector fMergeSeeds; + + /// \brief If the simulation is produced as a merge of multiple files, this stored the number primaries + /// for the individual files + std::vector fMergeNEvents; + /// \brief If this parameter is set to 'true' it will save all events even if they leave no energy in the /// sensitive volume (used for debugging purposes). It is set to 'false' by default. Bool_t fSaveAllEvents = false; @@ -383,7 +391,7 @@ class TRestGeant4Metadata : public TRestMetadata { void PrintMetadata() override; - void Merge(const TRestGeant4Metadata&); + void Merge(const TRestMetadata&) override; TRestGeant4Metadata(); TRestGeant4Metadata(const char* configFilename, const std::string& name = ""); diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index e03ab10..1e9b746 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1580,17 +1580,16 @@ size_t TRestGeant4Metadata::GetGeant4VersionMajor() const { return std::stoi(majorVersion.Data()); } -void TRestGeant4Metadata::Merge(const TRestGeant4Metadata& metadata) { +void TRestGeant4Metadata::Merge(const TRestMetadata& metadata) { + const auto restMetadata = *dynamic_cast(&metadata); fIsMerge = true; fSeed = 0; // seed makes no sense in a merged file + fMergeSeeds.push_back(restMetadata.fSeed); + fMergeNEvents.push_back(restMetadata.fNEvents); - if (fName.IsNull()) { - fName = metadata.fName; - } - - fNEvents += metadata.fNEvents; - fNRequestedEntries += metadata.fNRequestedEntries; - fSimulationTime += metadata.fSimulationTime; + fNEvents += restMetadata.fNEvents; + fNRequestedEntries += restMetadata.fNRequestedEntries; + fSimulationTime += restMetadata.fSimulationTime; } TRestGeant4Metadata::TRestGeant4Metadata(const TRestGeant4Metadata& metadata) { *this = metadata; } From 3f1c51f6ebcd8b1bf52ff4dcdb2b956084336f9d Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Thu, 21 Sep 2023 12:49:50 +0200 Subject: [PATCH 09/14] random brackets --- src/TRestGeant4Metadata.cxx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 1e9b746..f247d3e 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1496,7 +1496,9 @@ void TRestGeant4Metadata::PrintMetadata() { 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; + if (fActiveVolumes[id] == name) { + return id; + } } return -1; } @@ -1547,7 +1549,9 @@ Bool_t TRestGeant4Metadata::isVolumeStored(const TString& volume) const { 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]; + if (fActiveVolumes[id] == volume) { + return fChance[id]; + } } RESTWarning << "TRestGeant4Metadata::GetStorageChance. Volume " << volume << " not found" << RESTendl; From 3da813db3caba2bc21c598cbab9ed83f50e272a3 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Thu, 21 Sep 2023 13:17:42 +0200 Subject: [PATCH 10/14] increase class version --- inc/TRestGeant4Metadata.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 2c59463..43207df 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -401,7 +401,7 @@ class TRestGeant4Metadata : public TRestMetadata { TRestGeant4Metadata(const TRestGeant4Metadata& metadata); TRestGeant4Metadata& operator=(const TRestGeant4Metadata& metadata); - ClassDefOverride(TRestGeant4Metadata, 15); + ClassDefOverride(TRestGeant4Metadata, 16); // Allow modification of otherwise inaccessible / immutable members that shouldn't be modified by the user friend class SteppingAction; From 99cd885edc6ca074932f52204c7fa620c45a872b Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Thu, 21 Sep 2023 13:20:03 +0200 Subject: [PATCH 11/14] remove unused method --- inc/TRestGeant4Metadata.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 43207df..e962ece 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -387,8 +387,6 @@ class TRestGeant4Metadata : public TRestMetadata { void SetActiveVolume(const TString& name, Double_t chance, Double_t maxStep = 0); - void SetSimulationTime(Long64_t time) { fSimulationTime = time; } - void PrintMetadata() override; void Merge(const TRestMetadata&) override; From e027996703512ed63b0bb23de20888102ebe07ab Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Thu, 21 Sep 2023 14:00:15 +0200 Subject: [PATCH 12/14] types --- macros/REST_Geant4_MergeRestG4Files.C | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 27f1798..b060367 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -70,7 +70,7 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF TFileMerger merger; merger.OutputFile(outputTempFilename.c_str()); merger.AddObjectNames("EventTree AnalysisTree"); - for (int i = 0; i < inputFiles.size(); i++) { + for (unsigned int i = 0; i < inputFiles.size(); i++) { merger.AddFile(inputFiles[i].c_str()); } merger.PartialMerge(TFileMerger::kAll | TFileMerger::kIncremental | TFileMerger::kOnlyListed); @@ -80,7 +80,7 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF long long eventCounter = 0; // iterate over all other files - for (int i = 0; i < inputFiles.size(); i++) { + for (unsigned int i = 0; i < inputFiles.size(); i++) { cout << "Processing file " << i + 1 << "/" << inputFiles.size() << endl; map @@ -97,7 +97,7 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF auto eventTree = run.GetEventTree(); // auto analysisTree = run.GetAnalysisTree(); eventTree->SetBranchAddress("TRestGeant4EventBranch", &event); - for (int j = 0; j < eventTree->GetEntries(); j++) { + for (unsigned int j = 0; j < eventTree->GetEntries(); j++) { eventTree->GetEntry(j); *mergeEvent = *event; From 9f98a3b2d7ebdee00bcc0ec8c1ebe8689a24da04 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Thu, 21 Sep 2023 17:12:33 +0200 Subject: [PATCH 13/14] working merge with the analysis tree --- macros/REST_Geant4_MergeRestG4Files.C | 36 ++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index b060367..3d09d33 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -95,9 +95,11 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF } TRestGeant4Event* event = nullptr; auto eventTree = run.GetEventTree(); - // auto analysisTree = run.GetAnalysisTree(); eventTree->SetBranchAddress("TRestGeant4EventBranch", &event); for (unsigned int j = 0; j < eventTree->GetEntries(); j++) { + eventCounter++; + continue; // not working at this time (this logic works, but we are using TFileMerger which does + // not work with this) eventTree->GetEntry(j); *mergeEvent = *event; @@ -124,8 +126,6 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF mergeEventTree->Fill(); // TODO: this just adds empty entries to the analysis tree. It should be merged mergeAnalysisTree->Fill(); - - eventCounter++; } // add the remaining metadata of the first file to the merged file @@ -162,7 +162,35 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF mergeRun->UpdateOutputFile(); mergeRun->CloseFile(); - // Open the file again to check the number of events + // At this point we have two files: the "mergeRun" file with the updated metadata and the "temp" file with + // the event and analysis tree (the event tree here does not have updated event ids) Open the file again + + { + auto fileWithMetadata = TFile::Open(mergeRun->GetOutputFileName()); + auto fileWithTrees = TFile::Open(outputTempFilename.c_str(), "UPDATE"); + + // copy all objects from the file with metadata except "EventTree" and "AnalysisTree" + TIter nextkey(fileWithMetadata->GetListOfKeys()); + TKey* key; + while ((key = (TKey*)nextkey())) { + const auto obj = key->ReadObj(); + if (obj->InheritsFrom("TTree")) { + continue; + } + fileWithTrees->cd(); + obj->Write(); + } + + // close files + fileWithMetadata->Close(); + fileWithTrees->Close(); + } + + // replace the "run" file by the temp file + remove(mergeRun->GetOutputFileName()); + rename(outputTempFilename.c_str(), mergeRun->GetOutputFileName()); + + // to check the number of events TRestRun runCheck(outputFilename); if (runCheck.GetEntries() != eventCounter) { cerr << "ERROR: number of events in the output file (" << runCheck.GetEntries() From c614c64d88ef67f7cdb7b23863fa98aebdf8fe11 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Thu, 21 Sep 2023 17:20:52 +0200 Subject: [PATCH 14/14] update metadata print for merge --- macros/REST_Geant4_MergeRestG4Files.C | 3 --- src/TRestGeant4Metadata.cxx | 12 +++++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/macros/REST_Geant4_MergeRestG4Files.C b/macros/REST_Geant4_MergeRestG4Files.C index 3d09d33..e52a524 100644 --- a/macros/REST_Geant4_MergeRestG4Files.C +++ b/macros/REST_Geant4_MergeRestG4Files.C @@ -148,9 +148,6 @@ void REST_Geant4_MergeRestG4Files(const char* outputFilename, const char* inputF } } - cout << "Output filename: " << mergeRun->GetOutputFileName() << endl; - cout << "Output file: " << mergeRun->GetOutputFile() << endl; - mergeRun->GetOutputFile()->cd(); if (gGeoManager != nullptr) { diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index f247d3e..6125e76 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1433,7 +1433,15 @@ void TRestGeant4Metadata::PrintMetadata() { TRestMetadata::PrintMetadata(); RESTMetadata << "Geant4 version: " << GetGeant4Version() << RESTendl; - RESTMetadata << "Random seed: " << GetSeed() << RESTendl; + if (!fIsMerge) { + RESTMetadata << "Random seed: " << GetSeed() << RESTendl; + } else { + RESTMetadata << "This Geant4 simulation is a merge of " << fMergeSeeds.size() << " files" << RESTendl; + for (unsigned int i = 0; i < fMergeSeeds.size(); i++) { + RESTMetadata << " - File " << i << " Random seed: " << fMergeSeeds[i] + << " - Number of primaries: " << fMergeNEvents[i] << RESTendl; + } + } RESTMetadata << "GDML geometry: " << GetGdmlReference() << RESTendl; RESTMetadata << "GDML materials reference: " << GetMaterialsReference() << RESTendl; RESTMetadata << "Sub-event time delay: " << GetSubEventTimeDelay() << " us" << RESTendl; @@ -1629,5 +1637,7 @@ TRestGeant4Metadata& TRestGeant4Metadata::operator=(const TRestGeant4Metadata& m fKillVolumes = metadata.fKillVolumes; fRegisterEmptyTracks = metadata.fRegisterEmptyTracks; fMagneticField = metadata.fMagneticField; + fMergeNEvents = metadata.fMergeNEvents; + fMergeSeeds = metadata.fMergeSeeds; return *this; }