14 m_parser = std::make_unique<MaCh3ArgumentParser>(
"combine",
"1.0", argparse::default_arguments::help);
15 m_parser->add_description(
"Tool for combining multiple MaCh3 chains into a single output.");
16 m_parser->add_argument(
"-o",
"--output")
17 .help(
"Output ROOT file.")
19 .metavar(
"OUTPUT_FILE");
20 m_parser->add_argument(
"-c",
"--compression-level")
21 .help(
"Compression level for the output ROOT file.")
23 .metavar(
"COMPRESSION_LEVEL")
25 m_parser->add_argument(
"-f",
"--force")
26 .help(
"Force overwrite of the output file if it exists.")
29 m_parser->add_argument(
"-m",
"--merge-mode")
30 .help(
"Merge files in-spite of differences")
34 .help(
"Input ROOT files.")
37 .nargs(argparse::nargs_pattern::at_least_one)
63 for (
const auto& word : SkipVector) {
65 if (line.find(word) != std::string::npos) {
78 std::istringstream file1(File1);
79 std::istringstream file2(File2);
81 std::string line1, line2;
85 while (std::getline(file1, line1) && std::getline(file2, line2)) {
99 while (std::getline(file1, line1)) {
100 MACH3LOG_WARN(
"Extra line in {} on line {}: {}", File1, lineNumber, line1);
103 while (std::getline(file2, line2)) {
104 MACH3LOG_WARN(
"Extra line in {} on line {}: {}", File2, lineNumber, line2);
113 bool weirdFile =
false;
115 TMacro *versionHeader = file->Get<TMacro>(ConfigName.c_str());
116 TMacro *prevVersionHeader = prevFile->Get<TMacro>(ConfigName.c_str());
120 MACH3LOG_ERROR(
"Looks like the {} embedded config for file {} is different to the previous ones", ConfigName, file->GetName());
121 MACH3LOG_ERROR(
"This strongly suggests that this file was made with different software versions than the previous ones");
132 TDirectory *savdir = gDirectory;
133 TDirectory *adir = savdir->Get<TDirectory>(source->GetName());
136 adir = savdir->mkdir(source->GetName());
141 TIter nextkey(source->GetListOfKeys());
142 while ((key =
static_cast<TKey*
>(nextkey()))) {
143 const char *classname = key->GetClassName();
144 TClass *cl = gROOT->GetClass(classname);
146 if (cl->InheritsFrom(
"TDirectory")) {
147 source->cd(key->GetName());
148 TDirectory *subdir = gDirectory;
152 }
else if (cl->InheritsFrom(
"TTree")) {
153 TTree *T = source->Get<TTree>(key->GetName());
155 TTree *newT = T->CloneTree();
159 TObject *obj = key->ReadObj();
165 adir->SaveSelf(kTRUE);
174 MACH3LOG_ERROR(
"Null pointer passed to CompareHistograms for '{}'", histName);
178 const double int1 = h1->Integral();
179 const double int2 = h2->Integral();
180 if (std::abs(int1 - int2) > 1e-6) {
181 MACH3LOG_ERROR(
"Histogram '{}' in folder '{}' has different integrals: current = {}, previous = {}",
182 histName, folderName, int1, int2);
191 bool mismatch =
false;
192 TDirectory* dir = file->GetDirectory(FolderName.c_str());
193 TDirectory* prevDir = prevFile->GetDirectory(FolderName.c_str());
195 if (!dir || !prevDir) {
196 MACH3LOG_ERROR(
"Could not find folder '{}' in one or both files", FolderName);
200 TIter nextKey(dir->GetListOfKeys());
203 while ((key =
static_cast<TKey*
>(nextKey()))) {
204 const std::string objName = key->GetName();
205 TObject* obj = key->ReadObj();
209 if (obj->InheritsFrom(
"TH1")) {
210 TH1* hist =
static_cast<TH1*
>(obj);
211 TH1* prevHist =
dynamic_cast<TH1*
>(prevDir->Get(objName.c_str()));
213 MACH3LOG_ERROR(
"Missing histogram '{}' in previous file (folder '{}')", objName, FolderName);
222 else if (obj->InheritsFrom(
"TMacro")) {
223 TMacro* macro =
static_cast<TMacro*
>(obj);
224 TMacro* prevMacro =
dynamic_cast<TMacro*
>(prevDir->Get(objName.c_str()));
226 MACH3LOG_ERROR(
"Missing TMacro '{}' in previous file (folder '{}')", objName, FolderName);
241 TChain chain(TTreeName.c_str());
242 for (
const auto& f : files) chain.Add(f.c_str());
244 TFile* outF = TFile::Open(outFile.c_str(),
"UPDATE");
246 TTree* newTree = chain.CloneTree(-1,
"fast");
247 newTree->SetName(TTreeName.c_str());
249 newTree->Write(
"", TObject::kOverwrite);
256 std::string outFileOption;
258 else outFileOption =
"CREATE";
260 TFile *prevFile =
nullptr;
265 for(uint fileId = 0; fileId <
inpFileList.size(); fileId++)
268 TFile *file =
new TFile(fileName.c_str());
270 if(file->Get<TTree>(
"posteriors")->GetEntries() == 0){
271 MACH3LOG_WARN(
"Hmmm, file {} Doesn't seem to have any entries", fileName.c_str());
272 MACH3LOG_WARN(
"That's weird but I guess there's no rule that says a file can't be empty");
273 MACH3LOG_WARN(
"I'll skip it but maybe double check that this doesn't indicate some deeper problem");
278 if(prevFile ==
nullptr) {
284 bool weirdFile =
false;
286 if(
checkSoftwareVersions(file, prevFile,
"MaCh3_Config", {
"OutputFile:",
"NSteps:"})) weirdFile =
true;
287 if(
CheckFolder(file, prevFile,
"SampleFolder")) weirdFile =
true;
288 if(
CheckFolder(file, prevFile,
"CovarianceFolder")) weirdFile =
true;
292 MACH3LOG_ERROR(
"=====================================================================================");
293 MACH3LOG_ERROR(
"This is not a great idea and could lead to weird outputs and cause some big headaches");
294 MACH3LOG_ERROR(
"further down the road. But if you reeeeally wanna do it and you know what you're");
298 MACH3LOG_ERROR(
"=====================================================================================");
302 if(prevFile != file) {
316 TFile* outputFile = M3::Open(
OutFileName,
"recreate", __FILE__, __LINE__);
328 MACH3LOG_INFO(
"Merging of took {:.2f}s to finish", clock.RealTime());
331 outputFile = M3::Open(
OutFileName,
"UPDATE", __FILE__, __LINE__);
335 std::vector<std::string> configNames = {
"MaCh3_Config",
"Reweight_Config",
"Smearing_Config"};
336 for (std::size_t i = 0; i < configNames.size(); ++i) {
337 const std::string& name = configNames[i];
338 TMacro* macro = prevFile->Get<TMacro>(name.c_str());
339 if (macro !=
nullptr) {
346 TDirectory *MaCh3EngineDir = prevFile->Get<TDirectory>(
"MaCh3Engine");
347 TDirectory *CovarianceFolderDir = prevFile->Get<TDirectory>(
"CovarianceFolder");
348 TDirectory *SampleFolderDir = prevFile->Get<TDirectory>(
"SampleFolder");
Allows you to combine multiple chains or ROOT files into a single output.
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
std::string TMacroToString(const TMacro ¯o)
KS: Convert a ROOT TMacro object to a string representation.
void FastMergeTTrees(const std::vector< std::string > &files, const std::string &outFile, const std::string &TTreeName)
custom function for merging TTree, should be similar to what HADD is using
std::vector< std::string > inpFileList
int Run() override
Execute the application.
virtual ~CombineModule()
Destructor.
bool ShouldSkipLine(const std::string &line, const std::vector< std::string > &SkipVector)
KS: This allow us to skip output name etc in config. We expect Output name will be different but this...
bool CompareTwoConfigs(const std::string &File1, const std::string &File2, const std::vector< std::string > &SkipVector)
make sure two configs are identical but skip specified fields. For example when comparing two chains ...
bool CompareHistograms(const TH1 *h1, const TH1 *h2, const std::string &histName, const std::string &folderName)
Compare two histograms if they are identical.
void CopyDir(TDirectory *source)
When we merge two chains they have TDirectory ROOT didn't provide method for this so here we have thi...
MaCh3ArgumentParser * get_parser() override
Get the argument parser for this module.
bool CheckFolder(TFile *file, TFile *prevFile, const std::string &FolderName, const std::vector< std::string > &SkipVector={})
Loop through TH1 and TMacro objects in FolderName in 'file' and compare with those in 'prevFile'.
bool checkSoftwareVersions(TFile *file, TFile *prevFile, const std::string &ConfigName, const std::vector< std::string > &SkipVector={})
EM: Will compare the version header contained in the two provided files and shout if they don't match...
Extended ArgumentParser with MaCh3-specific functionality.
std::unique_ptr< MaCh3ArgumentParser > m_parser
Argument parser for this plugin.
Custom exception class used throughout MaCh3.
void MaCh3Welcome()
KS: Prints welcome message with MaCh3 logo.
Main namespace for MaCh3 software.