22#include "FileMapper.hh"
24#include "HistogramAccumulatorMerge.hh"
25#include "HistogramAccumulatorSum.hh"
26#include "RBDSException.hh"
28#include "BDSOutputROOTEventHeader.hh"
30#include "TDirectory.h"
44int main(
int argc,
char* argv[])
48 std::cout <<
"usage: rebdsimCombine result.root file1.root file2.root ..." << std::endl;
53 std::vector<std::string> inputFiles;
54 for (
int i = 2; i < argc; ++i)
55 {inputFiles.emplace_back(std::string(argv[i]));}
58 if (inputFiles.size() == 1)
60 if (inputFiles[0].find(
'*') != std::string::npos)
61 {std::cout <<
"Glob with * did not match any files" << std::endl;}
63 {std::cout <<
"Only one input file provided \"" << inputFiles[0] <<
"\" - no point." << std::endl;}
67 std::set<std::string> inputFilesSet = {inputFiles.begin(), inputFiles.end()};
68 if (inputFiles.size() > inputFilesSet.size())
69 {std::cout <<
"Warning: at least 1 duplicate name in list of files provided to combine." << std::endl;}
71 std::string outputFile = std::string(argv[1]);
72 if (outputFile.find(
'*') != std::string::npos)
74 std::cerr <<
"First argument for output file \"" << outputFile <<
"\" contains an *." << std::endl;
75 std::cerr <<
"Should only be a singular file - check order of arguments." << std::endl;
81 TFile* output =
new TFile(outputFile.c_str(),
"RECREATE");
86 headerOut->
Fill(std::vector<std::string>(), inputFiles);
88 TTree* headerTree =
new TTree(
"Header",
"REBDSIM Header");
89 headerTree->Branch(
"Header.",
"BDSOutputROOTEventHeader", headerOut);
92 TH1::AddDirectory(
true);
93 TH2::AddDirectory(
true);
94 TH3::AddDirectory(
true);
100 {f =
new TFile(inputFiles[0].c_str(),
"READ");}
101 catch (
const std::exception& e)
102 {std::cerr << e.what() << std::endl;
return 1;}
107 {std::cerr << error.
what() << std::endl;
return 1;}
108 catch (
const std::exception& error)
109 {std::cerr << error.what() << std::endl;
return 1;}
112 TTree* oldModelTree =
dynamic_cast<TTree*
>(f->Get(
"ModelTree"));
114 {oldModelTree =
dynamic_cast<TTree*
>(f->Get(
"Model"));}
118 auto newTree = oldModelTree->CloneTree();
119 newTree->SetName(
"ModelTree");
120 newTree->Write(
"", TObject::kOverwrite);
126 std::vector<RBDS::HistogramPath> histograms = histMap->
Histograms();
128 unsigned long long int nOriginalEvents = 0;
129 unsigned long long int nEventsInFile = 0;
130 unsigned long long int nEventsInFileSkipped = 0;
131 unsigned long long int nEventsRequested = 0;
133 std::cout <<
"Combination of " << inputFiles.size() <<
" files beginning" << std::endl;
135 for (
const auto& file : inputFiles)
137 f =
new TFile(file.c_str());
140 std::cout <<
"Accumulating> " << file << std::endl;
141 for (
const auto& hist : histograms)
143 std::string histPath = hist.path + hist.name;
145 TH1* h =
dynamic_cast<TH1*
>(f->Get(histPath.c_str()));
149 hist.accumulator->Accumulate(h);
153 TTree* ht = (TTree*)f->Get(
"Header");
159 for (
int i = 0; i < (int)ht->GetEntries(); i++)
169 {std::cout <<
"Skipping " << file <<
" as not a rebdsim output file" << std::endl;}
175 for (
const auto& hist : histograms)
177 TH1* result = hist.accumulator->Terminate();
178 result->SetDirectory(hist.outputDir);
179 hist.outputDir->Add(result);
180 delete hist.accumulator;
189 output->Write(
nullptr,TObject::kOverwrite);
192 std::cout <<
"Combined result of " << inputFiles.size() <<
" files written to: " << outputFile << std::endl;
Class to map a rebdsim file structure and create duplicate in output.
const std::vector< RBDS::HistogramPath > & Histograms() const
Access full vector of histograms.
General exception with possible name of object and message.
const char * what() const noexcept override
Override message in std::exception.
bool IsREBDSIMOrCombineOutputFile(TFile *file)
void WarningMissingHistogram(const std::string &histName, const std::string &fileName)
Common print out method.