6 #include "PlottingUtils/PlottingManager.h"
9 #pragma GCC diagnostic ignored "-Wfloat-conversion"
10 #pragma GCC diagnostic ignored "-Wconversion"
32 int originalErrorLevel = gErrorIgnoreLevel;
33 gErrorIgnoreLevel = kFatal;
36 LLHPad =
new TPad(
"LLHPad",
"LLHPad", 0.0,
ratioPlotSplit, 1.0, 1.0);
37 LLHPad->SetBottomMargin(0.0);
38 ratioPad =
new TPad(
"ratioPad",
"ratioPad", 0.0, 0.0, 1.0,
ratioPlotSplit);
39 ratioPad->SetTopMargin(0.0);
40 ratioPad->SetBottomMargin(0.3);
42 LLHPad =
new TPad(
"AllSampPad",
"AllSampPad", 0.0, 0.0, 1.0, 1.0);
43 ratioPad =
new TPad(
"AllSampRatioPad",
"AllSampRatioPad", 0.0, 0.0, 0.0, 0.0);
46 ratioPad->AppendPad();
47 gErrorIgnoreLevel = originalErrorLevel;
51 std::vector<float> &cumSums, std::vector<bool> &drawLabel,
52 THStack *sampleStack, TLegend *splitSamplesLegend,
53 float baselineLLH_main = 0.00001)
55 std::vector<std::string> sampNames =
PlotMan->input().getTaggedSamples(
PlotMan->getOption<std::vector<std::string>>(
"sampleTags"));
56 size_t nSamples = sampNames.size();
58 cumSums.resize(nSamples);
59 drawLabel.resize(nSamples);
61 float LLH_main_integ = LLH_allSams.Integral();
63 int nBins = LLH_allSams.GetNbinsX();
65 MACH3LOG_DEBUG(
"getting split sample THStack for {} known samples", nSamples);
66 for (uint i = 0; i < nSamples; i++)
68 std::string sampName = sampNames[i];
73 new TH1D(
PlotMan->input().getSampleSpecificLLHScan_TH1D(fileIdx, parameterName, sampName));
74 LLH_indivSam->SetName(Form(
"%i_%s_%s", fileIdx, parameterName.c_str(), sampName.c_str()));
75 LLH_indivSam->SetBit(kCanDelete);
78 if (LLH_indivSam->GetNbinsX() == 1)
80 MACH3LOG_DEBUG(
" sample hist had only 1 bin - assuming it doesn't exist");
87 LLH_indivSam->SetStats(0);
88 LLH_indivSam->SetLineColor(TColor::GetColorPalette(
89 floor(
static_cast<float>(i) * TColor::GetNumberOfColors() /
static_cast<float>(nSamples))));
90 LLH_indivSam->SetFillColor(TColor::GetColorPalette(
91 floor(
static_cast<float>(i) * TColor::GetNumberOfColors() /
static_cast<float>(nSamples))));
92 sampleStack->Add(LLH_indivSam);
93 splitSamplesLegend->AddEntry(LLH_indivSam,
PlotMan->style().prettifySampleName(sampName).c_str(),
"lf");
95 float lastBinLLH = LLH_indivSam->GetBinContent(
nBins);
98 MACH3LOG_DEBUG(
" Last bin LLH = {} :: cumulative LLH = {}", lastBinLLH, cumSum);
99 MACH3LOG_DEBUG(
" LLH fraction = {} / {} = {}", LLH_indivSam->Integral(), LLH_main_integ, LLH_indivSam->Integral() / LLH_main_integ);
109 drawLabel[i] =
false;
119 double stackMax = ratioCompStack->GetMaximum(
"NOSTACK");
120 double stackMin = ratioCompStack->GetMinimum(
"NOSTACK");
122 double stackLim = std::max(std::abs(1.0 - stackMax), std::abs(1.0 - stackMin));
124 ratioCompStack->SetMinimum(1.0 - 1.05 * stackLim);
125 ratioCompStack->SetMaximum(1.0 + 1.05 * stackLim);
128 ratioCompStack->Draw(Form(
"NOSTACK%s",
PlotMan->getDrawOptions().c_str()));
132 ratioCompStack->GetXaxis()->GetLabelSize());
134 ratioCompStack->GetXaxis()->GetLabelSize());
135 ratioCompStack->GetXaxis()->SetTitle(
"Parameter Variation");
138 ratioCompStack->GetYaxis()->GetLabelSize());
140 ratioCompStack->GetYaxis()->GetLabelSize());
141 ratioCompStack->GetYaxis()->SetTitleOffset(
yTitleOffset);
142 ratioCompStack->GetYaxis()->SetNdivisions(5, 2, 0);
146 const std::string& LLHType,
147 const std::string& outputFileName,
148 const std::unique_ptr<TCanvas>&
canv) {
150 auto compStack = std::make_unique<THStack>(
"LLH_Stack",
"");
151 auto ratioCompStack = std::make_unique<THStack>(
"LLH_Ratio_Stack",
"");
152 auto legend = std::make_unique<TLegend>(0.3, 0.6, 0.7, 0.8);
154 TPad* LLHPad =
nullptr;
155 TPad* ratioPad =
nullptr;
159 TH1D LLH_main =
PlotMan->input().getLLHScan_TH1D(0, paramName, LLHType);
160 LLH_main.SetStats(0);
162 LLH_main.SetLineColor(kBlack);
163 compStack->Add(&LLH_main);
164 legend->AddEntry(&LLH_main,
PlotMan->getFileLabel(0).c_str(),
"l");
166 int nBins = LLH_main.GetNbinsX();
169 for (
unsigned int extraFileIdx = 1; extraFileIdx <
PlotMan->input().getNInputFiles(); extraFileIdx++)
171 int originalErrorLevel = gErrorIgnoreLevel;
172 gErrorIgnoreLevel = kFatal;
174 TH1D *compHist =
new TH1D(
PlotMan->input().getLLHScan_TH1D(extraFileIdx, paramName, LLHType));
175 compHist->SetName(Form(
"LLHScan_%s_%s_%d", paramName.c_str(), LLHType.c_str(), extraFileIdx));
176 compHist->SetBit(kCanDelete);
177 gErrorIgnoreLevel = originalErrorLevel;
178 if (compHist->GetNbinsX() == 0)
182 compHist->SetLineColor(
183 TColor::GetColorPalette(floor(
static_cast<float>(extraFileIdx) * TColor::GetNumberOfColors() /
184 static_cast<float>(
PlotMan->input().getNInputFiles()))));
185 compHist->SetLineStyle(2 + extraFileIdx % 9);
188 TH1D *divHist =
static_cast<TH1D*
>(compHist->Clone(Form(
"RatioHist_%i", extraFileIdx)));
189 divHist->SetBit(kCanDelete);
194 divHist->Divide(compHist, &LLH_main);
195 ratioCompStack->Add(divHist);
199 compStack->Add(compHist);
200 legend->AddEntry(compHist,
PlotMan->getFileLabel(extraFileIdx).c_str(),
"l");
210 compStack->Draw(Form(
"NOSTACK%s",
PlotMan->getDrawOptions().c_str()));
212 compStack->GetXaxis()->SetTitle(
"Parameter Variation");
213 compStack->GetYaxis()->SetTitle(Form(
"-2LLH_{%s}", LLHType.c_str()));
214 compStack->SetTitle(
PlotMan->style().prettifyParamName(paramName).c_str());
230 TLine line = TLine();
231 line.SetLineColor(kBlack);
233 line.DrawLine(LLH_main.GetBinLowEdge(1), 1.0, LLH_main.GetBinLowEdge(
nBins + 1), 1.0);
237 canv->SaveAs(outputFileName.c_str());
243 const std::string& outputFileName,
244 const std::unique_ptr<TCanvas>&
canv) {
247 TPad* LLHPad =
nullptr;
248 TPad* ratioPad =
nullptr;
256 TH1D LLH_main =
PlotMan->input().getLLHScan_TH1D(0, paramName,
"sample");
257 if (LLH_main.GetNbinsX() == 1)
259 MACH3LOG_DEBUG(
" Main LLH had only 1 bin, assuming it doesn't exist");
263 auto baseSplitSamplesStack = std::make_unique<THStack>(
264 paramName.c_str(), Form(
"%s - %s", paramName.c_str(),
PlotMan->getFileLabel(0).c_str()));
266 auto baseSplitSamplesLegend = std::make_unique<TLegend>(0.37, 0.475, 0.63, 0.9);
269 canv->cd(1)->SetGrid();
273 canv->cd(1)->SetLeftMargin(0.15);
274 std::vector<float> cumSums;
275 std::vector<bool> drawLabel;
277 getSplitSampleStack(0, paramName, LLH_main, cumSums, drawLabel, baseSplitSamplesStack.get(),
278 baseSplitSamplesLegend.get());
279 baseSplitSamplesStack->Draw(
PlotMan->getDrawOptions().c_str());
281 baseSplitSamplesStack->GetYaxis()->SetTitle(
"-2LLH_{sam}");
282 if (
PlotMan->getPlotRatios() ==
false) baseSplitSamplesStack->GetXaxis()->SetTitle(
"Parameter Variation");
288 LLH_main.SetLineWidth(1);
289 LLH_main.Draw(Form(
"same%s",
PlotMan->getDrawOptions().c_str()));
290 baseSplitSamplesLegend->AddEntry(&LLH_main,
"All Samples",
"l");
293 baseSplitSamplesLegend->Draw();
295 auto label = std::make_unique<TLatex>();
297 label->SetTextAlign(11);
298 label->SetTextAngle(-55);
299 label->SetTextSize(0.012);
302 for (uint i = 0; i <
PlotMan->input().getTaggedSamples(
PlotMan->getOption<std::vector<std::string>>(
"sampleTags")).size(); i++)
305 std::string sampName =
PlotMan->input().getTaggedSamples(
PlotMan->getOption<std::vector<std::string>>(
"sampleTags"))[i];
313 label->DrawLatex(LLH_main.GetBinLowEdge(LLH_main.GetNbinsX() + 1), cumSums[i],
314 Form(
"#leftarrow%s",
PlotMan->style().prettifySampleName(sampName).c_str()));
319 for (
unsigned int extraFileIdx = 1; extraFileIdx <
PlotMan->getNFiles(); extraFileIdx++)
321 MACH3LOG_DEBUG(
" - Adding plot for additional file {}", extraFileIdx);
322 canv->cd(1 + extraFileIdx);
324 std::vector<float> extraCumSums;
325 std::vector<bool> extraDrawLabel;
327 THStack *splitSamplesStack =
328 new THStack(paramName.c_str(),
329 Form(
"%s - %s", paramName.c_str(),
PlotMan->getFileLabel(extraFileIdx).c_str()));
331 auto splitSamplesLegend = std::make_unique<TLegend>(0.37, 0.475, 0.63, 0.9);
332 splitSamplesStack->SetBit(kCanDelete);
333 splitSamplesLegend->SetBit(kCanDelete);
335 int originalErrorLevel = gErrorIgnoreLevel;
336 gErrorIgnoreLevel = kFatal;
337 TH1D compLLH_main =
PlotMan->input().getLLHScan_TH1D(extraFileIdx, paramName,
"sample");
338 compLLH_main.SetName((compLLH_main.GetName() + std::string(
"_file_") + extraFileIdx).Data());
339 gErrorIgnoreLevel = originalErrorLevel;
340 if (compLLH_main.GetNbinsX() == 1)
342 delete splitSamplesStack;
351 splitSamplesStack, splitSamplesLegend.get(), LLH_main.Integral());
354 splitSamplesStack, splitSamplesLegend.get());
362 splitSamplesStack->Draw(
PlotMan->getDrawOptions().c_str());
364 splitSamplesStack->SetMaximum(baseSplitSamplesStack->GetMaximum());
368 compLLH_main.Draw(Form(
"same%s",
PlotMan->getDrawOptions().c_str()));
369 compLLH_main.SetLineColor(kBlack);
370 splitSamplesLegend->AddEntry(&compLLH_main,
"All Samples",
"l");
372 splitSamplesLegend->Draw();
375 baseSplitSamplesStack->GetYaxis()->SetTitle(
"-2LLH_{sam}");
376 if (
PlotMan->getPlotRatios() ==
false) baseSplitSamplesStack->GetXaxis()->SetTitle(
"Parameter Variation");
381 for (uint i = 0; i <
PlotMan->input().getTaggedSamples(
PlotMan->getOption<std::vector<std::string>>(
"sampleTags")).size(); i++)
383 std::string sampName =
PlotMan->input().getTaggedSamples(
PlotMan->getOption<std::vector<std::string>>(
"sampleTags"))[i];
386 label->DrawLatex(compLLH_main.GetBinLowEdge(compLLH_main.GetNbinsX() + 1), extraCumSums[i],
387 Form(
"#leftarrow%s",
PlotMan->style().prettifySampleName(sampName).c_str()));
392 THStack *splitSamplesStackRatios =
new THStack(paramName.c_str(),
"");
394 TList *baselineHistList = baseSplitSamplesStack->GetHists();
395 TList *compHistList = splitSamplesStack->GetHists();
396 for (uint sampleIdx = 0; sampleIdx <
PlotMan->input().getTaggedSamples(
PlotMan->getOption<std::vector<std::string>>(
"sampleTags")).size(); sampleIdx++)
398 TH1D *divHist =
new TH1D(
399 Form(
"%s_%s_splitDiv_%i", paramName.c_str(),
PlotMan->getFileLabel(extraFileIdx).c_str(),
401 Form(
"%s_%s_splitDiv", paramName.c_str(),
PlotMan->getFileLabel(extraFileIdx).c_str()),
402 compLLH_main.GetNbinsX(), compLLH_main.GetBinLowEdge(1),
403 compLLH_main.GetBinLowEdge(compLLH_main.GetNbinsX() + 1));
404 divHist->Divide(
static_cast<TH1D*
>(compHistList->At(sampleIdx)),
405 static_cast<TH1D*
>(baselineHistList->At(sampleIdx)));
406 splitSamplesStackRatios->Add(divHist);
407 divHist->SetLineColor((
static_cast<TH1D*
>(compHistList->At(sampleIdx))->GetLineColor()));
410 canv->cd(2 + extraFileIdx);
418 canv->SaveAs(outputFileName.c_str());
423 PlotMan->style().setPalette(
PlotMan->getOption<std::string>(
"colorPalette"));
426 auto canv = std::make_unique<TCanvas>(
"canv",
"", 1024, 1024);
427 gStyle->SetOptTitle(2);
429 auto splitSamplesCanv = std::make_unique<TCanvas>(
"splitSampCanv",
"", 4096 *
PlotMan->getNFiles(), 4096);
431 canv->SaveAs((
PlotMan->getOutputName(
"_Sample") +
"[").c_str());
432 canv->SaveAs((
PlotMan->getOutputName(
"_Penalty") +
"[").c_str());
433 canv->SaveAs((
PlotMan->getOutputName(
"_Total") +
"[").c_str());
435 if (
PlotMan->getSplitBySample())
436 canv->SaveAs((
PlotMan->getOutputName(
"_bySample") +
"[").c_str());
438 for( std::string par:
PlotMan->getOption<std::vector<std::string>>(
"parameterTags")) std::cout << par <<
", ";
440 for (std::string paramName :
PlotMan->input().getTaggedParameters(
PlotMan->getOption<std::vector<std::string>>(
"parameterTags")))
452 if (
PlotMan->getSplitBySample())
458 canv->SaveAs((
PlotMan->getOutputName(
"_Sample") +
"]").c_str());
459 canv->SaveAs((
PlotMan->getOutputName(
"_Penalty") +
"]").c_str());
460 canv->SaveAs((
PlotMan->getOutputName(
"_Total") +
"]").c_str());
461 if (
PlotMan->getSplitBySample())
462 canv->SaveAs((
PlotMan->getOutputName(
"_bySample") +
"]").c_str());
469 int main(
int argc,
char **argv) {
473 PlotMan =
new MaCh3Plotting::PlottingManager();
474 PlotMan->parseInputs(argc, argv);
488 if(
PlotMan->getPlotRatios() &&
PlotMan->input().getNInputFiles() == 1){
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
void drawRatioStack(THStack *ratioCompStack)
int main(int argc, char **argv)
MaCh3Plotting::PlottingManager * PlotMan
double sampleLabelThreshold
void SetTPads(TPad *&LLHPad, TPad *&ratioPad)
TPad is SUPER FRAGILE, it is safer to just make raw pointer, while ROOT behave weirdly with smart poi...
void makeSplitSampleLLHScanComparisons(const std::string ¶mName, const std::string &outputFileName, const std::unique_ptr< TCanvas > &canv)
void getSplitSampleStack(int fileIdx, std::string parameterName, TH1D LLH_allSams, std::vector< float > &cumSums, std::vector< bool > &drawLabel, THStack *sampleStack, TLegend *splitSamplesLegend, float baselineLLH_main=0.00001)
void makeLLHScanComparisons(const std::string ¶mName, const std::string &LLHType, const std::string &outputFileName, const std::unique_ptr< TCanvas > &canv)
Custom exception class used throughout MaCh3.
void MaCh3Welcome()
KS: Prints welcome message with MaCh3 logo.