-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from rest-for-physics/lobis-merge
Macro to merge simulation files
- Loading branch information
Showing
6 changed files
with
183 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "TRestGeant4Event.h" | ||
#include "TRestGeant4Metadata.h" | ||
#include "TRestTask.h" | ||
|
||
#ifndef RestTask_Geant4_MergeRestG4Files | ||
#define RestTask_Geant4_MergeRestG4Files | ||
|
||
//******************************************************************************************************* | ||
//*** | ||
//*** Your HELP is needed to verify, validate and document this macro. | ||
//*** This macro might need update/revision. | ||
//*** | ||
//******************************************************************************************************* | ||
|
||
/* | ||
* Author: Luis Obis ([email protected]) | ||
* 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. | ||
*/ | ||
|
||
// 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) { | ||
// 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<string> 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); | ||
|
||
set<Int_t> eventIds; | ||
|
||
// iterate over all other files | ||
for (int i = 0; i < inputFiles.size(); i++) { | ||
cout << "Processing file " << i + 1 << "/" << inputFiles.size() << endl; | ||
|
||
map<Int_t, Int_t> | ||
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) { | ||
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; | ||
|
||
Int_t eventId = mergeEvent->GetID(); | ||
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()); | ||
|
||
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); | ||
|
||
mergeMetadata.SetName("geant4Metadata"); | ||
mergeMetadata.Write(); | ||
mergeRun->UpdateOutputFile(); | ||
mergeRun->CloseFile(); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters