Allows you to combine multiple chains or ROOT files into a single output.
More...
#include <CLI/Modules/CombineModule.hpp>
|
| virtual | ~CombineModule () |
| | Destructor. More...
|
| |
| MaCh3ArgumentParser * | get_parser () override |
| | Get the argument parser for this module. More...
|
| |
| int | Run () override |
| | Execute the application. More...
|
| |
| 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 doesn't invalidate chain merging. More...
|
| |
| 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 nsteps or output name might be different and this is still fine to merge More...
|
| |
| 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. More...
|
| |
| void | CopyDir (TDirectory *source) |
| | When we merge two chains they have TDirectory ROOT didn't provide method for this so here we have this bad boy. More...
|
| |
| bool | CompareHistograms (const TH1 *h1, const TH1 *h2, const std::string &histName, const std::string &folderName) |
| | Compare two histograms if they are identical. More...
|
| |
| 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'. More...
|
| |
| 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 More...
|
| |
| void | CombineChain () |
| |
| virtual | ~PluginBase ()=default |
| |
| virtual | ~IPlugin ()=default |
| |
Allows you to combine multiple chains or ROOT files into a single output.
Definition at line 29 of file CombineModule.hpp.
◆ ~CombineModule()
| M3::CombineModule::~CombineModule |
( |
| ) |
|
|
virtualdefault |
◆ CheckFolder()
| bool M3::CombineModule::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'.
Definition at line 189 of file CombineModule.cpp.
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);
std::string TMacroToString(const TMacro ¯o)
KS: Convert a ROOT TMacro object to a string representation.
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.
◆ checkSoftwareVersions()
| bool M3::CombineModule::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.
Definition at line 111 of file CombineModule.cpp.
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");
◆ CombineChain()
| void M3::CombineModule::CombineChain |
( |
| ) |
|
Definition at line 254 of file CombineModule.cpp.
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");
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
void CopyDir(TDirectory *source)
When we merge two chains they have TDirectory ROOT didn't provide method for this so here we have thi...
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...
Custom exception class used throughout MaCh3.
◆ CompareHistograms()
| bool M3::CombineModule::CompareHistograms |
( |
const TH1 * |
h1, |
|
|
const TH1 * |
h2, |
|
|
const std::string & |
histName, |
|
|
const std::string & |
folderName |
|
) |
| |
Compare two histograms if they are identical.
- Todo:
- add checks for stuff like bin content etc
- Todo:
- add checks for stuff like bin content etc
Definition at line 171 of file CombineModule.cpp.
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);
◆ CompareTwoConfigs()
| bool M3::CombineModule::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 nsteps or output name might be different and this is still fine to merge
- Parameters
-
| File1 | Config from chain1 |
| File2 | Config from chain2 |
| SkipVector | Fields in yaml file to skip |
Definition at line 77 of file CombineModule.cpp.
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);
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...
◆ CopyDir()
| void M3::CombineModule::CopyDir |
( |
TDirectory * |
source | ) |
|
When we merge two chains they have TDirectory ROOT didn't provide method for this so here we have this bad boy.
Definition at line 129 of file CombineModule.cpp.
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);
◆ FastMergeTTrees()
| void M3::CombineModule::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
- Warning
- KS: for some reason if "fast" is enable then I cannot open in ROOT5, no one should use R5 at this point..
Definition at line 240 of file CombineModule.cpp.
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);
◆ get_parser()
Get the argument parser for this module.
- Returns
- Pointer to the configured MaCh3ArgumentParser
Implements M3::IPlugin.
Definition at line 13 of file CombineModule.cpp.
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)
std::unique_ptr< MaCh3ArgumentParser > m_parser
Argument parser for this plugin.
◆ Run()
| int M3::CombineModule::Run |
( |
| ) |
|
|
overridevirtual |
Execute the application.
Main function for combining chains or ROOT files.
- Returns
- Exit code (0 on success)
Implements M3::IPlugin.
Definition at line 43 of file CombineModule.cpp.
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
void MaCh3Welcome()
KS: Prints welcome message with MaCh3 logo.
◆ ShouldSkipLine()
| bool M3::CombineModule::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 doesn't invalidate chain merging.
Definition at line 61 of file CombineModule.cpp.
63 for (
const auto& word : SkipVector) {
65 if (line.find(word) != std::string::npos) {
◆ forceMerge
| bool M3::CombineModule::forceMerge = false |
|
private |
◆ forceOverwrite
| bool M3::CombineModule::forceOverwrite = false |
|
private |
◆ inpFileList
| std::vector<std::string> M3::CombineModule::inpFileList |
|
private |
◆ OutFileName
| std::string M3::CombineModule::OutFileName = "" |
|
private |
◆ targetCompression
| int M3::CombineModule::targetCompression = 1 |
|
private |
The documentation for this class was generated from the following files: