10 MACH3LOG_DEBUG(
"InputManager: have loaded translation config file");
25 MACH3LOG_DEBUG(
"Will now check the specified parameters for tags");
30 std::vector<std::string> tags;
37 tags = Get<std::vector<std::string>>(
_parametersConfig[param][
"tags"], __FILE__, __LINE__);
49 std::vector<std::string> tags;
56 tags = Get<std::vector<std::string>>(
_samplesConfig[samp][
"tags"], __FILE__, __LINE__);
63 for (std::string fileName : _fileNames) {
92 if (printLevel ==
"dump")
105 if (printLevel ==
"summary")
108 }
else if (printLevel ==
"dump")
117 std::string errorType)
const {
126 MACH3LOG_CRITICAL(
"Requested error type, {} does not exist in the specified file: ", errorType);
133 if (inputFileDef.
postFitErrors.at(errorType).find(paramName) !=
136 return inputFileDef.
postFitErrors.at(errorType).at(paramName);
145 std::string errorType)
const {
154 MACH3LOG_CRITICAL(
"Requested error type, {} does not exist in the specified file: ", errorType);
161 if (inputFileDef.
postFitValues.at(errorType).find(paramName) !=
164 return inputFileDef.
postFitValues.at(errorType).at(paramName);
178 const std::unordered_map<std::string, std::vector<std::string>> &tagMap,
179 const std::vector<std::string> &tags, std::string checkType)
const {
182 checkType !=
"all" &&
183 checkType !=
"any" &&
187 MACH3LOG_ERROR(
"Invalid tag check type specified: {}. Will instead use the default type: 'all'", checkType);
192 if (tags.size() == 0)
return values;
194 std::vector<std::string> retVec;
196 MACH3LOG_DEBUG(
"Getting tagged values using checkType {}", checkType);
197 for ( std::string val: values )
201 const std::vector<std::string> &valTags = tagMap.at(val);
204 if (valTags.size() == 0)
continue;
207 unsigned int tagCount = 0;
208 for (
const std::string &tag: tags )
210 if ( std::find( valTags.begin(), valTags.end(), tag ) != valTags.end() )
218 if ( checkType ==
"all" )
220 if ( tagCount == tags.size() ) retVec.push_back(val);
222 else if ( checkType ==
"any" )
224 if ( tagCount > 0 ) retVec.push_back(val);
226 else if ( checkType ==
"exact" )
229 if ( tagCount == valTags.size() ) retVec.push_back(val);
232 MACH3LOG_DEBUG(
"Found {} values matching the specified tags", retVec.size());
238 const std::string &sample,
const std::string ¶meter2)
const {
239 std::string locationString(rawLocationString);
240 std::vector<std::string> tokens;
243 std::string toReplace =
"";
247 toReplace =
"{PARAMETER}";
248 while ((pos = locationString.find(toReplace)) != std::string::npos)
250 locationString.replace(pos, toReplace.length(),
255 toReplace =
"{SAMPLE}";
256 while ((pos = locationString.find(toReplace)) != std::string::npos)
258 locationString.replace(pos, toReplace.length(),
264 toReplace =
"{PARAMETER2}";
265 while ((pos = locationString.find(toReplace)) != std::string::npos)
267 locationString.replace(pos, toReplace.length(),
275 std::string delimiter =
":";
276 while ((pos = locationString.find(delimiter)) != std::string::npos)
278 token = locationString.substr(0, pos);
279 tokens.push_back(token);
280 locationString.erase(0, pos + delimiter.length());
282 tokens.push_back(locationString);
286 if (tokens.size() > 2)
288 throw MaCh3Exception(__FILE__ , __LINE__,
"Too many : tokens in location string: " + rawLocationString);
295 const std::vector<std::string> &locationVec)
const {
296 std::shared_ptr<TObject>
object =
nullptr;
299 if (locationVec.size() == 1)
301 object = std::shared_ptr<TObject>(fileDef.
file->Get(locationVec[0].c_str()));
306 else if (locationVec.size() == 2)
308 TDirectoryFile *directory = fileDef.
file->Get<TDirectoryFile>(locationVec[0].c_str());
309 size_t nMatchingObjects = 0;
312 if (directory ==
nullptr)
320 TIter next(directory->GetListOfKeys());
321 while (TKey *key =
static_cast<TKey*
>(next()))
323 if (
strEndsWith(std::string(key->GetName()), locationVec[1]))
325 object = std::shared_ptr<TObject>(directory->Get(key->GetName()));
331 if (nMatchingObjects > 1)
333 MACH3LOG_CRITICAL(
"Too many objects match the pattern specified by {} {}", locationVec[0], locationVec.size()==2 ? locationVec[1] :
"");
334 MACH3LOG_CRITICAL(
"Found {} matching objects, should just be one", nMatchingObjects);
342 MACH3LOG_CRITICAL(
"Should have two elements: [ (directory to look in), (end of the name of the object) ]");
343 MACH3LOG_CRITICAL(
"Or one element that is just the absolute path to the object");
351 std::string &fitter,
const std::string &sample,
bool setInputFileScan) {
355 YAML::Node testLLHConfig = thisFitterSpec_config[
"bySample_LLH"];
356 auto testLLHRawLocations = Get<std::vector<std::string>>(testLLHConfig[
"location"], __FILE__, __LINE__);
358 auto LLHObjType = Get<std::string>(thisFitterSpec_config[
"LLHObjectType"], __FILE__, __LINE__);
360 std::shared_ptr<TObject> LLHObj =
nullptr;
361 for (std::string rawLocation : testLLHRawLocations)
365 if (LLHObj !=
nullptr)
372 if (LLHObj ==
nullptr)
376 if (setInputFileScan)
378 std::shared_ptr<TGraph> LLHGraph = std::make_shared<TGraph>();
380 if (LLHObjType ==
"TH1D")
382 LLHGraph = std::make_shared<TGraph>(
static_cast<TH1D*
>(LLHObj.get()));
383 }
else if (LLHObjType ==
"TGraph")
385 LLHGraph = std::shared_ptr<TGraph>(
static_cast<TGraph*
>(LLHObj->Clone()));
388 throw MaCh3Exception(__FILE__ , __LINE__,
"uknown type of LLH object specified: " + LLHObjType);
399 bool wasFound =
false;
404 const std::vector<TString> branchNames = inputFileDef.
mcmcProc->GetBranchNames();
409 for (
int paramIdx = 0; paramIdx < inputFileDef.
mcmcProc->GetNParams() -1 ; paramIdx ++ )
412 double prior, priorError;
413 inputFileDef.
mcmcProc->GetNthParameter(paramIdx, prior, priorError, title);
416 if (!inputFileDef.
posteriorTree->GetBranch(branchNames[paramIdx])) {
417 MACH3LOG_DEBUG(
"Branch {} not found in chain, skipping!", branchNames[paramIdx]);
424 if ( setInputBranch )
439 bool wasFound =
false;
443 if ( thisFitterSpec_config[
"1dPosteriors"] ) {
444 auto rawLocations = Get<std::vector<std::string>>(thisFitterSpec_config[
"1dPosteriors"][
"location"],
446 for (
const std::string &rawLoc : rawLocations)
450 if ( posterior1d !=
nullptr )
456 inputFileDef.
posteriors1d_map[parameter] = std::make_shared<TGraph>(posterior1d.get());
466 std::string &fitter,
const std::string &errorType,
467 const bool setInputFileError) {
472 YAML::Node postFitErrorTypes = thisFitterSpec_config[
"postFitErrorTypes"];
473 YAML::Node specificErrorType = postFitErrorTypes[errorType];
474 auto postFitLocations = Get<std::vector<std::string>>(specificErrorType[
"location"], __FILE__, __LINE__);
477 std::vector<std::string> postFitLocations_override;
479 postFitLocations_override, parameter))
481 postFitLocations = postFitLocations_override;
484 for (std::string postFitLoc : postFitLocations)
489 if (postFitErrors ==
nullptr)
493 for (
int binIdx = 0; binIdx <= postFitErrors->GetNbinsX(); binIdx++)
495 std::string binLabel = std::string(postFitErrors->GetXaxis()->GetBinLabel(binIdx));
498 if (setInputFileError)
501 inputFileDef.
postFitErrors[errorType][parameter] = postFitErrors->GetBinError(binIdx);
502 inputFileDef.
postFitValues[errorType][parameter] = postFitErrors->GetBinContent(binIdx);
528 bool foundFitter =
false;
535 throw MaCh3Exception(__FILE__ , __LINE__,
"translation config doesnt contain a definition for fitter " + fitter);
544 for (std::string LLHType : {
"sample",
"penalty",
"total"})
547 MACH3LOG_INFO(
".... searching for {} LLH scans... ", LLHType);
550 YAML::Node testLLHConfig = thisFitterSpec_config[LLHType +
"_LLH"];
551 auto testLLHRawLocations = Get<std::vector<std::string>>(testLLHConfig[
"location"], __FILE__, __LINE__);
556 std::vector<std::string> enabledLLHParams;
564 for (
const std::string &rawLocation : testLLHRawLocations)
570 enabledLLHParams.push_back(parameter);
581 if (numLLHParams > 0)
601 if (thisFitterSpec_config[
"defaultPostFitErrorType"])
603 MACH3LOG_DEBUG(
" Default type specified with possible locations: ");
604 YAML::Node postFitErrorSpec = thisFitterSpec_config[
"postFitErrorTypes"];
605 YAML::Node defaultErrorType =
606 postFitErrorSpec[thisFitterSpec_config[
"defaultPostFitErrorType"].as<std::string>()];
607 auto locations = Get<std::vector<std::string>>(defaultErrorType[
"location"], __FILE__, __LINE__);
608 for (std::string loc : locations)
617 int numPostFitParams = 0;
618 std::vector<std::string> enabledPostFitParams;
620 auto defaultErrorType =
621 Get<std::string>(thisFitterSpec_config[
"defaultPostFitErrorType"], __FILE__, __LINE__);
627 enabledPostFitParams.push_back(parameter);
634 if (numPostFitParams > 0)
645 MACH3LOG_INFO(
"....Searching for LLH scans broken down by sample");
649 size_t numLLHBySampleParams = 0;
656 numLLHBySampleParams++;
660 if (numLLHBySampleParams != 0)
665 MACH3LOG_INFO(
"........ Found {} LLH scans for sample {}", numLLHBySampleParams, sample);
667 if ((numLLHParams != numLLHBySampleParams))
670 MACH3LOG_ERROR(
" I have {} LLH scans for sample {}", numLLHBySampleParams, sample);
671 MACH3LOG_ERROR(
" But {} parameters with Total_LLH scals", numLLHParams);
682 if ( thisFitterSpec_config[
"MCMCsteps"] )
685 auto posteriorTreeRawLocations =
686 Get<std::vector<std::string>>(thisFitterSpec_config[
"MCMCsteps"][
"location"], __FILE__, __LINE__);
688 TTree *postTree =
nullptr;
689 for (
const std::string &rawLoc: posteriorTreeRawLocations )
691 MACH3LOG_DEBUG(
" - Looking for MCMC chain parameter values at: {}", rawLoc);
693 postTree = inputFileDef.
file->Get<TTree>(rawLoc.c_str());
695 if ( postTree !=
nullptr && (postTree->GetNbranches() != 0) )
697 inputFileDef.
mcmcProc = std::make_unique<MCMCProcessor>(inputFileDef.
fileName);
699 inputFileDef.
mcmcProc->SetOutputSuffix(
"_PlottingTemp");
700 inputFileDef.
mcmcProc->Initialise();
707 if ( postTree !=
nullptr && (postTree->GetNbranches() != 0) )
710 inputFileDef.
nMCMCentries = int(postTree->GetEntries());
719 size_t num1dPosteriors = 0;
720 std::vector<std::string> enabled1dPosteriorParams;
722 size_t numMCMCchainParams = 0;
723 std::vector<std::string> enabledMCMCchainParams;
730 if ( thisFitterSpec_config[
"1dPosteriors"] &&
find1dPosterior(inputFileDef, parameter, fitter) )
733 enabled1dPosteriorParams.push_back(parameter);
739 if ( thisFitterSpec_config[
"MCMCsteps"] &&
findRawChainSteps(inputFileDef, parameter, fitter) )
742 enabledMCMCchainParams.push_back(parameter);
743 numMCMCchainParams++;
748 if (num1dPosteriors > 0 )
755 MACH3LOG_INFO(
"........ Found {} 1d processed posteriors", num1dPosteriors);
757 if ( numMCMCchainParams > 0 )
764 MACH3LOG_INFO(
"........ Found {} parameters in MCMC chain", numMCMCchainParams);
779 inputFileDef.
fitter = fitter;
786 MACH3LOG_WARN(
"I don't know what kinda fitter this came from, will proceed with caution");
787 inputFileDef.
fitter =
"UNKNOWN_FITTER";
800 Get<std::string>(thisFitterSpec_config[
"defaultPostFitErrorType"], __FILE__, __LINE__);
803 for (
const std::string LLHType : {
"sample",
"penalty",
"total"})
809 YAML::Node testLLHConfig = thisFitterSpec_config[LLHType +
"_LLH"];
810 const std::vector<std::string> testLLHRawLocations =
811 Get<std::vector<std::string>>(testLLHConfig[
"location"], __FILE__, __LINE__);
814 auto LLHObjType = Get<std::string>(thisFitterSpec_config[
"LLHObjectType"], __FILE__, __LINE__);
818 std::shared_ptr<TObject> LLHObj =
nullptr;
821 for (
const std::string &rawLocation : testLLHRawLocations)
826 if (LLHObj !=
nullptr)
831 if (LLHObj ==
nullptr)
833 MACH3LOG_ERROR(
"Hmmm, something seems to have gone wrong and I couldn't find the {} LLH scan for {} when attempting to read the data for it", LLHType, parameter);
834 MACH3LOG_ERROR(
"This will very likely cause segfaults and sadness");
838 std::shared_ptr<TGraph> LLHGraph = std::make_shared<TGraph>();
842 if (LLHObjType ==
"TH1D")
844 LLHGraph = std::make_shared<TGraph>(
static_cast<TH1D*
>(LLHObj.get()));
847 else if (LLHObjType ==
"TGraph")
849 LLHGraph = std::shared_ptr<TGraph>(
static_cast<TGraph*
>(LLHObj->Clone()));
858 inputFileDef.
LLHScans_map[LLHType][parameter] = LLHGraph;
874 auto availableErrorTypes = Get<std::vector<std::string>>(thisFitterSpec_config[
"AvailablePostFitErrorTypes"],
877 for (
const std::string &errorType : availableErrorTypes)
895 std::vector<std::string> ParamNames;
896 auto file = std::unique_ptr<TFile>(
TFile::Open(fileName.c_str()));
898 if (
auto* ProcessorDir =
dynamic_cast<TDirectory*
>(file->Get(
"Post"));
899 ProcessorDir !=
nullptr)
901 TIter next(ProcessorDir->GetListOfKeys());
903 while (
auto* key =
dynamic_cast<TKey*
>(next())) {
904 ParamNames.emplace_back(key->GetName());
905 MACH3LOG_DEBUG(
"Adding name {}, assuming MCMC Processor file", ParamNames.back());
912 if (
auto* llhDir =
dynamic_cast<TDirectory*
>(file->Get(
"Total_LLH"));
915 TIter next(llhDir->GetListOfKeys());
917 while (
auto* key =
dynamic_cast<TKey*
>(next())) {
918 std::string name = key->GetName();
920 const char suffix[] =
"_full";
921 const std::size_t suffix_len =
sizeof(suffix) - 1;
922 name.erase(name.size() - suffix_len);
923 ParamNames.push_back(std::move(name));
924 MACH3LOG_DEBUG(
"Adding name {}, assuming LLH scan file", ParamNames.back());
930 MACH3LOG_ERROR(
"You didn't specify any parameter in universalTranslator.yaml");
931 MACH3LOG_ERROR(
"Also you specified file I don't know format of so couldn't atomically find params");
#define MACH3LOG_CRITICAL
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Custom exception class used throughout MaCh3.
fileTypeEnum
Types of possible file that can be read.
@ kLLH
Log Likelihood scan.
@ kPostFit
Processed post fit errors.
Main namespace for MaCh3 software.
constexpr static const double _BAD_DOUBLE_
Default value used for double initialisation.
TFile * Open(const std::string &Name, const std::string &Type, const std::string &File, const int Line)
Opens a ROOT file with the given name and mode.