16#include "TMatrixDSym.h"
22#include "TObjString.h"
45 TFile *TempFile =
new TFile(inputFile.c_str(),
"open");
48 TDirectory* CovarianceFolder = TempFile->Get<TDirectory>(
"CovarianceFolder");
52 TMacro *Config = TempFile->Get<TMacro>(
"MaCh3_Config");
53 if (Config ==
nullptr) {
54 MACH3LOG_ERROR(
"Didn't find MaCh3_Config tree in MCMC file! {}", inputFile);
62 std::vector<std::string> XsecCovPos = GetFromManager<std::vector<std::string>>(Settings[
"General"][
"Systematics"][
"XsecCovFile"], {
"none"});
63 if(XsecCovPos.back() ==
"none")
71 if (std::getenv(
"MACH3") !=
nullptr) {
72 MACH3LOG_INFO(
"Found MACH3 environment variable: {}", std::getenv(
"MACH3"));
73 for(
unsigned int i = 0; i < XsecCovPos.size(); i++)
74 XsecCovPos[i].insert(0, std::string(std::getenv(
"MACH3"))+
"/");
78 XSecFile[
"Systematics"] = YAML::Node(YAML::NodeType::Sequence);
79 for(
unsigned int i = 0; i < XsecCovPos.size(); i++)
82 for (
const auto& item : YAMLDocTemp[
"Systematics"]) {
83 XSecFile[
"Systematics"].push_back(item);
87 size = XSecMatrix->GetNrows();
89 auto systematics = XSecFile[
"Systematics"];
90 for (
auto it = systematics.begin(); it != systematics.end(); ++it)
92 auto const ¶m = *it;
94 ParamNames.push_back(param[
"Systematic"][
"Names"][
"FancyName"].as<std::string>());
95 nominal.push_back( param[
"Systematic"][
"ParameterValues"][
"PreFitValue"].as<double>() );
98 if (param[
"Systematic"][
"FlatPrior"]) { flat = param[
"Systematic"][
"FlatPrior"].as<
bool>(); }
102 XSecMatrix->Invert();
105 for (
int i = 0; i <
size; i++)
108 for (
int j = 0; j <
size; ++j)
114 #pragma omp parallel for collapse(2)
116 for (
int i = 0; i <
size; i++)
118 for (
int j = 0; j <
size; ++j)
130 auto canvas = std::make_unique<TCanvas>(
"canvas",
"canvas", 0, 0, 1024, 1024);
135 canvas->SetBottomMargin(0.1f);
136 canvas->SetTopMargin(0.02f);
137 canvas->SetRightMargin(0.08f);
138 canvas->SetLeftMargin(0.15f);
140 gStyle->SetOptTitle(0);
141 gStyle->SetOptStat(0);
142 gStyle->SetPalette(51);
147 TChain* Chain =
new TChain(
"posteriors",
"");
148 Chain->Add(inputFile.c_str());
151 TObjArray* brlis = Chain->GetListOfBranches();
154 int nBranches = brlis->GetEntries();
155 int RelevantBranches = 0;
156 for (
int i = 0; i < nBranches; i++)
159 TBranch* br =
static_cast<TBranch*
>(brlis->At(i));
164 TString bname = br->GetName();
167 if(bname.BeginsWith(
"xsec_"))
175 Chain->SetBranchStatus(
"*",
false);
177 std::vector<double> fParProp(RelevantBranches);
179 for (
int i = 0; i < RelevantBranches; ++i)
181 Chain->SetBranchStatus(
BranchNames[i].Data(),
true);
182 Chain->SetBranchAddress(
BranchNames[i].Data(), &fParProp[i]);
185 YAML::Node Settings = YAML::LoadFile(configFile.c_str());
187 std::vector<std::string> SetsNames;
188 std::vector<std::vector<std::string>> RemoveNames;
189 std::vector<bool> Exclude;
190 std::vector<std::string> FancyTittle;
192 std::vector<std::vector<bool>> isRelevantParam;
193 std::vector<std::string> node = Settings[
"GetPenaltyTerm"][
"PenaltySets"].as<std::vector<std::string>>();
194 for (
unsigned int i = 0; i < node.size(); i++)
196 std::string ParName = node[i];
197 SetsNames.push_back(ParName);
199 const auto& Set = Settings[
"GetPenaltyTerm"][ParName];
201 RemoveNames.push_back(Set[0].as<std::vector<std::string>>());
202 Exclude.push_back(Set[1].as<bool>());
203 FancyTittle.push_back(Set[2].as<std::string>());
206 const int NSets = int(SetsNames.size());
208 isRelevantParam.resize(NSets);
210 for(
int i = 0; i < NSets; i++)
212 isRelevantParam[i].resize(
size);
215 for (
int j = 0; j <
size; j++)
217 isRelevantParam[i][j] =
false;
223 for (
unsigned int k = 0; k < RemoveNames[i].size(); k++)
225 if (
ParamNames[j].rfind(RemoveNames[i][k], 0) == 0)
232 isRelevantParam[i][j] =
true;
239 for (
unsigned int k = 0; k < RemoveNames[i].size(); k++)
241 if (
ParamNames[j].rfind(RemoveNames[i][k], 0) == 0)
243 isRelevantParam[i][j] =
true;
250 MACH3LOG_INFO(
" Found {} params for set {}", counter, SetsNames[i]);
253 int AllEvents = int(Chain->GetEntries());
254 std::vector<std::unique_ptr<TH1D>> hLogL(NSets);
255 for (
int i = 0; i < NSets; i++) {
256 std::string NameTemp =
"LogL_" + SetsNames[i];
257 hLogL[i] = std::make_unique<TH1D>(NameTemp.c_str(), NameTemp.c_str(), AllEvents, 0, AllEvents);
258 hLogL[i]->SetLineColor(kBlue);
260 std::vector<double> logL(NSets, 0.0);
261 for(
int n = 0; n < AllEvents; ++n)
267 for(
int k = 0; k < NSets; ++k) logL[k] = 0.;
270 double *logL_private =
nullptr;
274 #pragma omp parallel private(logL_private)
276 logL_private =
new double[NSets]();
277 for(
int k = 0; k < NSets; ++k) logL_private[k] = 0.;
280 for (
int i = 0; i <
size; i++)
282 for (
int j = 0; j <= i; ++j)
287 for(
int k = 0; k < NSets; ++k)
290 if (isRelevantParam[k][i] && isRelevantParam[k][j])
294 if(i != j) scale = 2;
302 for(
int k = 0; k < NSets; ++k)
305 logL[k] += logL_private[k];
308 delete[] logL_private;
312 for (
int i = 0; i <
size; i++)
314 for (
int j = 0; j <= i; ++j)
319 for(
int k = 0; k < NSets; ++k)
322 if (isRelevantParam[k][i] && isRelevantParam[k][j])
326 if(i != j) scale = 2;
334 for(
int k = 0; k < NSets; ++k)
336 hLogL[k]->SetBinContent(n, logL[k]);
341 std::string OutputName = inputFile +
"_PenaltyTerm" +
".root";
342 TFile* OutputFile =
new TFile(OutputName.c_str(),
"recreate");
343 TDirectory *PenaltyTermDir = OutputFile->mkdir(
"PenaltyTerm");
345 canvas->Print(Form(
"%s_PenaltyTerm.pdf[",inputFile.c_str()),
"pdf");
346 for(
int i = 0; i < NSets; i++)
348 const double Maximum = hLogL[i]->GetMaximum();
349 hLogL[i]->GetYaxis()->SetRangeUser(0., Maximum*1.2);
350 hLogL[i]->SetTitle(FancyTittle[i].c_str());
351 hLogL[i]->GetXaxis()->SetTitle(
"Step");
352 hLogL[i]->GetYaxis()->SetTitle(FancyTittle[i].c_str());
353 hLogL[i]->GetYaxis()->SetTitleOffset(1.4f);
357 PenaltyTermDir->cd();
360 canvas->Print(Form(
"%s_PenaltyTerm.pdf",inputFile.c_str()),
"pdf");
362 canvas->Print(Form(
"%s_PenaltyTerm.pdf]",inputFile.c_str()),
"pdf");
365 for (
int i = 0; i <
size; i++)
374int main(
int argc,
char *argv[])
381 MACH3LOG_WARN(
"./GetPenaltyTerm root_file_to_analyse.root ");
384 std::string filename = argv[1];
385 std::string
config = argv[2];
#define _MaCh3_Safe_Include_Start_
KS: Avoiding warning checking for headers.
#define _MaCh3_Safe_Include_End_
KS: Restore warning checking after including external headers.
int main(int argc, char *argv[])
std::vector< double > nominal
std::vector< std::string > ParamNames
void GetPenaltyTerm(const std::string &inputFile, const std::string &configFile)
void ReadXSecFile(const std::string &inputFile)
std::vector< TString > BranchNames
std::vector< bool > isFlat
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
YAML::Node TMacroToYAML(const TMacro ¯o)
KS: Convert a ROOT TMacro object to a YAML node.
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Custom exception class for MaCh3 errors.
TMatrixDSym * GetCovMatrixFromChain(TDirectory *TempFile)
KS: Retrieve the cross-section covariance matrix from the given TDirectory. Historically,...
void PrintProgressBar(const Long64_t Done, const Long64_t All)
KS: Simply print progress bar.
void PrintConfig(const YAML::Node &node)
KS: Print Yaml config using logger.
void MaCh3Welcome()
KS: Prints welcome message with MaCh3 logo.