5 #include "TGraphAsymmErrors.h"
23 double MinVale = 1234567890.;
24 TAxis *xaxis = hist->GetXaxis();
25 for (
int x = 1; x <= hist->GetNbinsX(); x++)
27 if (xaxis->GetBinLowEdge(x) > minRange && xaxis->GetBinUpEdge(x) < maxRange)
29 if (MinVale > hist->GetBinContent(x))
30 MinVale = hist->GetBinContent(x);
40 int failure_count = 0;
42 for (
int bin = 2; bin <= hist->GetNbinsX(); ++bin)
44 if (std::fabs(hist->GetBinContent(bin) - 1.0) > tolerance)
46 if (++failure_count > max_failures)
55 void MakePlot(TString fname1, TString flabel1, TString fname2, TString flabel2, TString fname3, TString flabel3, TString fname4, TString flabel4)
57 auto c1 = std::make_unique<TCanvas>(
"c1",
" ", 0, 0, 800, 630);
58 gStyle->SetOptStat(0);
60 gErrorIgnoreLevel = kWarning;
63 TFile *infile =
M3::Open(fname1.Data(),
"UPDATE", __FILE__, __LINE__);
65 bool add_legend =
false;
66 TFile *infile2 =
nullptr;
69 infile2 =
M3::Open(fname2.Data(),
"UPDATE", __FILE__, __LINE__);
72 TFile *infile3 =
nullptr;
75 infile3 =
M3::Open(fname3.Data(),
"UPDATE", __FILE__, __LINE__);
78 TFile *infile4 =
nullptr;
81 infile4 =
M3::Open(fname4.Data(),
"UPDATE", __FILE__, __LINE__);
85 TIter next(infile->GetListOfKeys());
86 while ((key =
static_cast<TKey *
>(next())))
88 std::string dirname = std::string(key->GetName());
89 if (std::string(key->GetClassName()) !=
"TDirectoryFile")
92 if ((dirname ==
"LogL") || (dirname ==
"Batched_means") || (dirname ==
"PowerSpectrum"))
97 infile->cd(dirname.c_str());
98 TIter nextsub(gDirectory->GetListOfKeys());
99 c1->Print(Form(
"%s_%s.pdf[", flabel1.Data(), dirname.c_str()),
"pdf");
101 while ((subkey =
static_cast<TKey *
>(nextsub())))
103 auto leg = std::make_unique<TLegend>(0.7, 0.7, 0.9, 0.9);
104 leg->SetFillColorAlpha(kWhite, 0.7f);
105 std::string name = std::string(subkey->GetName());
106 name = dirname +
"/" + name;
108 if (std::string(subkey->GetClassName()) !=
"TH1D") {
114 std::unique_ptr<TH1D> blarb[4];
115 MACH3LOG_INFO(
"Looking for {} from file {}", name.c_str(), fname1.Data());
116 blarb[0] =
M3::Clone(
static_cast<TH1D *
>(infile->Get(name.c_str())));
118 if (std::isnan(blarb[0]->GetBinContent(1)))
122 blarb[0]->SetLineStyle(kSolid);
123 blarb[0]->SetLineColor(kRed);
126 leg->AddEntry(blarb[0].get(), flabel1.Data(),
"l");
128 if (dirname ==
"AccProb")
129 blarb[0]->GetYaxis()->SetRangeUser(0, 1.0);
130 if (name ==
"AccProb/AcceptanceProbability")
132 if (infile2 !=
nullptr)
134 blarb[1] =
M3::Clone(
static_cast<TH1D *
>(infile2->Get(name.c_str())));
136 blarb[1]->SetLineStyle(kDashed);
137 blarb[1]->SetLineColor(kBlue);
138 blarb[1]->Draw(
"same");
139 leg->AddEntry(blarb[1].get(), flabel2.Data(),
"l");
141 if (infile3 !=
nullptr)
143 blarb[2] =
M3::Clone(
static_cast<TH1D *
>(infile3->Get(name.c_str())));
145 blarb[2]->SetLineStyle(kDotted);
146 blarb[2]->SetLineColor(kGreen);
147 blarb[2]->Draw(
"same");
148 leg->AddEntry(blarb[2].get(), flabel3.Data(),
"l");
150 if (infile4 !=
nullptr)
152 blarb[3] =
M3::Clone(
static_cast<TH1D *
>(infile4->Get(name.c_str())));
154 blarb[3]->SetLineStyle(kDashDotted);
155 blarb[3]->SetLineColor(kOrange);
156 blarb[3]->Draw(
"same");
157 leg->AddEntry(blarb[3].get(), flabel4.Data(),
"l");
163 c1->Print(Form(
"%s_%s.pdf", flabel1.Data(), dirname.c_str()),
"pdf");
165 gDirectory->cd(
"..");
166 c1->Print(Form(
"%s_%s.pdf]", flabel1.Data(), dirname.c_str()),
"pdf");
170 if (infile2 !=
nullptr)
172 if (infile3 !=
nullptr)
174 if (infile4 !=
nullptr)
178 void PlotAutoCorr(TString fname1, TString flabel1, TString fname2, TString flabel2, TString fname3, TString flabel3, TString fname4, TString flabel4)
186 std::vector<TString> flabel = {flabel1, flabel2, flabel3, flabel4};
209 auto c1 = std::make_unique<TCanvas>(
"c1",
" ", 0, 0, 800, 630);
210 gStyle->SetOptStat(0);
212 gErrorIgnoreLevel = kWarning;
214 c1->Print(
"Auto_Corr_PerFile.pdf[",
"pdf");
215 for (
int ik = 0; ik < Nfiles; ik++)
217 TIter next(infile[ik]->GetListOfKeys());
221 std::vector<std::unique_ptr<TH1D>> histos;
222 while ((key =
static_cast<TKey *
>(next())))
224 std::string dirname = std::string(key->GetName());
227 if ((dirname !=
"Auto_corr"))
230 infile[ik]->cd(dirname.c_str());
231 TIter nextsub(gDirectory->GetListOfKeys());
234 bool FirstTime =
true;
235 while ((subkey =
static_cast<TKey *
>(nextsub())))
237 std::string name = std::string(subkey->GetName());
238 name = dirname +
"/" + name;
240 if (std::string(subkey->GetClassName()) !=
"TH1D")
243 histos.push_back(
M3::Clone(
static_cast<TH1D *
>(infile[ik]->
Get(name.c_str()))));
245 if (std::isnan(histos.back()->GetBinContent(1)))
252 if (MinValue >= 0.80)
253 histos.back()->SetLineColor(kRed);
254 else if (MinValue >= 0.40 && MinValue < 0.80)
255 histos.back()->SetLineColor(kOrange);
256 else if (MinValue > 0.20 && MinValue < 0.40)
257 histos.back()->SetLineColor(kYellow);
258 else if (MinValue <= 0.20)
259 histos.back()->SetLineColor(kGreen);
260 histos.back()->GetXaxis()->UnZoom();
263 histos.back()->SetTitle(Form(
"Auto_Corr_%s.pdf", fname[ik].Data()));
265 histos.back()->SetLineStyle(kDashed);
268 histos.back()->Draw();
270 TString integral = Form(
"Integral: %.2f", histos.back()->Integral());
273 histos.back()->Draw(
"same");
276 gDirectory->cd(
"..");
278 c1->Print(
"Auto_Corr_PerFile.pdf",
"pdf");
280 c1->Print(
"Auto_Corr_PerFile.pdf]",
"pdf");
286 std::pair<std::unique_ptr<TGraph>, std::unique_ptr<TGraph>>
CreateMinMaxBand(TH1D *hist, Color_t color)
288 int nBins = hist->GetNbinsX();
291 for (
int i = 0; i <
nBins; ++i)
293 x[i] = hist->GetBinCenter(i + 1);
294 ymin[i] = hist->GetBinContent(i + 1);
295 ymax[i] = hist->GetBinContent(i + 1);
298 auto minGraph = std::make_unique<TGraph>(
nBins, x.data(), ymin.data());
299 auto maxGraph = std::make_unique<TGraph>(
nBins, x.data(), ymax.data());
301 minGraph->SetLineColor(color);
302 maxGraph->SetLineColor(color);
303 minGraph->SetFillColorAlpha(color, 0.3f);
304 maxGraph->SetFillColorAlpha(color, 0.3f);
306 return {std::move(minGraph), std::move(maxGraph)};
309 std::unique_ptr<TGraphAsymmErrors>
CalculateMinMaxBand(
const std::vector<TH1D *> &histograms, Color_t color)
311 if (histograms.empty()) {
312 throw MaCh3Exception(__FILE__, __LINE__,
"Empty histogram vector provided");
315 int nBins = histograms[0]->GetNbinsX();
318 for (
int bin = 0; bin <
nBins; bin++)
320 x[bin] = histograms[0]->GetBinCenter(bin + 1);
322 for (
const auto &hist : histograms)
324 double content = hist->GetBinContent(bin + 1);
325 ymin[bin] = std::min(ymin[bin], content);
326 ymax[bin] = std::max(ymax[bin], content);
331 auto band = std::make_unique<TGraphAsymmErrors>(
nBins);
332 for (
int i = 0; i <
nBins; ++i)
334 band->SetPoint(i, x[i], (ymax[i] + ymin[i]) / 2.0);
335 band->SetPointError(i, 0, 0, (ymax[i] - ymin[i]) / 2.0, (ymax[i] - ymin[i]) / 2.0);
338 band->SetFillColorAlpha(color, 0.2f);
339 band->SetLineWidth(1);
346 if (histograms.empty())
348 throw MaCh3Exception(__FILE__, __LINE__,
"Empty histogram vector provided");
350 auto min_hist =
M3::Clone(histograms[0]);
351 auto max_hist =
M3::Clone(histograms[0]);
353 for (
const auto &hist : histograms)
355 for (
int bin = 1; bin <= min_hist->GetNbinsX(); ++bin)
357 double current_min = min_hist->GetBinContent(bin);
358 double current_max = max_hist->GetBinContent(bin);
359 double bin_content = hist->GetBinContent(bin);
361 min_hist->SetBinContent(bin, std::min(current_min, bin_content));
362 max_hist->SetBinContent(bin, std::max(current_max, bin_content));
366 return {std::move(min_hist), std::move(max_hist)};
371 std::unique_ptr<TH1D>& average_hist,
372 int ¶meter_count,
373 std::vector<TH1D *> &histograms)
375 TIter next(autocor_dir->GetListOfKeys());
378 while ((key =
dynamic_cast<TKey *
>(next())))
380 TH1D *current_hist =
nullptr;
381 autocor_dir->GetObject(key->GetName(), current_hist);
384 current_hist->GetMaximum() <= 0 ||
390 current_hist->SetDirectory(
nullptr);
391 histograms.push_back(current_hist);
399 average_hist->Add(current_hist);
406 std::unique_ptr<TH1D>& average_hist,
407 int ¶meter_count,
408 std::vector<TH1D *> &histograms)
410 std::unique_ptr<TFile> input_file(
TFile::Open(file_path));
411 if (!input_file || input_file->IsZombie())
413 throw MaCh3Exception(__FILE__, __LINE__,
"Could not open file: " + std::string(file_path.Data()));
416 TDirectoryFile *autocor_dir =
nullptr;
417 input_file->GetObject(
"Auto_corr", autocor_dir);
422 "Auto_corr directory not found in file: " + std::string(file_path.Data()));
431 std::unique_ptr<TH1D> average_hist =
nullptr;
432 int parameter_count = 0;
438 catch (
const std::exception &e)
440 MACH3LOG_ERROR(
"Error processing file {} : {}", input_file, e.what());
443 if (average_hist && parameter_count > 0)
445 MACH3LOG_INFO(
"Processed {} parameters from {} files", parameter_count, input_file);
446 average_hist->Scale(1.0 / parameter_count);
453 const std::vector<std::unique_ptr<TH1D>> &averages,
454 const std::vector<TString> &hist_labels,
455 const TString &output_name,
456 bool draw_min_max =
true,
457 bool draw_all =
false,
458 bool draw_errors =
true)
460 auto canvas = std::make_unique<TCanvas>(
"AverageAC",
"Average Auto Correlation", 800, 600);
464 constexpr Int_t nb = 255.0;
465 Double_t stops[8] = {0.0, 0.25, 0.5, 0.75};
466 Double_t red[8] = {0.83203125, 0.796875, 0.0, 0.9375};
467 Double_t green[8] = {0.3671875, 0.47265625, 0.4453125, 0.890625};
468 Double_t blue[8] = {0.0, 0.65234375, 0.6953125, 0.2578125};
469 TColor::CreateGradientColorTable(8, stops, red, green, blue, nb);
471 auto leg = std::make_unique<TLegend>(0.5, 0.7, 0.9, 0.9);
472 leg->SetFillColorAlpha(kWhite, 0.7f);
475 for (
size_t i = 0; i < histograms.size(); ++i)
477 auto colour =
static_cast<Color_t
>(TColor::GetColorPalette(
static_cast<Int_t
>(i * nb / histograms.size())));
481 band->SetLineWidth(0);
482 band->SetTitle(
"Average Auto Correlation");
483 band->GetXaxis()->SetTitle(
"lag");
484 band->GetYaxis()->SetTitle(
"Autocorrelation Function");
488 band->Draw(
"3 SAME");
494 for (
size_t i = 0; i < averages.size(); ++i)
496 auto colour =
static_cast<Color_t
>(TColor::GetColorPalette(
static_cast<Int_t
>(i * nb / histograms.size())));
500 auto error_hist =
M3::Clone(averages[i].get());
501 error_hist->SetFillColorAlpha(colour, 0.3f);
502 error_hist->SetLineWidth(0);
503 error_hist->SetTitle(
"Average Auto Correlation");
504 error_hist->GetXaxis()->SetTitle(
"lag");
505 error_hist->GetYaxis()->SetTitle(
"Autocorrelation Function");
506 error_hist->Draw(
"E3 SAME");
510 for (
const auto &hist : histograms[i])
512 hist->SetLineColorAlpha(colour, 0.05f);
513 hist->SetLineWidth(1);
514 hist->Draw(
"HIST SAME");
517 averages[i]->SetLineColor(colour);
518 averages[i]->SetLineWidth(2);
519 averages[i]->SetTitle(
"Average Auto Correlation");
520 averages[i]->GetXaxis()->SetTitle(
"lag");
521 averages[i]->GetYaxis()->SetTitle(
"Autocorrelation Function");
522 averages[i]->Draw(
"HIST SAME");
524 TString int_str = TString::Format(
"Average integrated autocorrelation %.2f", averages[i]->Integral());
525 leg->AddEntry(averages[i].get(), hist_labels[i] + int_str,
"l");
528 if (averages.size() > 1)
533 canvas->SaveAs(output_name +
".pdf");
537 std::vector<TString> hist_labels,
538 const TString &output_name,
539 bool draw_min_max =
true)
544 std::vector<std::vector<TH1D *>> histograms;
545 std::vector<std::unique_ptr<TH1D>> averages;
547 for (
int i = 0; i < static_cast<int>(input_files.size()); i++)
549 TString folder = input_files[i];
552 if (folder.IsNull() || folder ==
DUMMYFILE)
554 MACH3LOG_WARN(
"Skipping empty or dummy folder: {}", folder.Data());
558 std::vector<TH1D *> histograms_i;
560 histograms.push_back(histograms_i);
563 CompareAverageAC(histograms, averages, hist_labels, output_name, draw_min_max);
566 int main(
int argc,
char *argv[])
569 if (argc != 3 && argc != 5 && argc != 7 && argc != 9)
572 MACH3LOG_ERROR(
"How to use: {} DiagMCMC_Output.root Plot Name", argv[0]);
581 PlotAverageACMult({argv[1]}, {argv[2]}, Form(
"%s_Average_Auto_Corr", argv[2]),
true);
587 PlotAverageACMult({argv[1], argv[3]}, {argv[2], argv[4]}, Form(
"%s_Average_Auto_Corr", argv[2]),
true);
593 PlotAverageACMult({argv[1], argv[3], argv[5]}, {argv[2], argv[4], argv[6]}, Form(
"%s_Average_Auto_Corr", argv[2]),
true);
597 MakePlot(argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
598 PlotAutoCorr(argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
599 PlotAverageACMult({argv[1], argv[3], argv[5], argv[7]}, {argv[2], argv[4], argv[6], argv[8]}, Form(
"%s_Average_Auto_Corr", argv[2]),
true);
void RemoveFitter(TH1D *hist, const std::string &name)
KS: Remove fitted TF1 from hist to make comparison easier.
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
int main(int argc, char *argv[])
std::unique_ptr< TGraphAsymmErrors > CalculateMinMaxBand(const std::vector< TH1D * > &histograms, Color_t color)
bool IsHistogramAllOnes(TH1D *hist, double tolerance=0.001, int max_failures=100)
HW: Check if histogram is flat within a given tolerance.
void ProcessDiagnosticFile(const TString &file_path, std::unique_ptr< TH1D > &average_hist, int ¶meter_count, std::vector< TH1D * > &histograms)
double GetMinimumInRange(TH1D *hist, const double minRange, const double maxRange)
KS: function which looks for minimum in given range.
void ProcessAutoCorrelationDirectory(TDirectoryFile *autocor_dir, std::unique_ptr< TH1D > &average_hist, int ¶meter_count, std::vector< TH1D * > &histograms)
std::pair< std::unique_ptr< TGraph >, std::unique_ptr< TGraph > > CreateMinMaxBand(TH1D *hist, Color_t color)
HW: Create a band of minimum and maximum values from a histogram.
void MakePlot(TString fname1, TString flabel1, TString fname2, TString flabel2, TString fname3, TString flabel3, TString fname4, TString flabel4)
void PlotAverageACMult(std::vector< TString > input_files, std::vector< TString > hist_labels, const TString &output_name, bool draw_min_max=true)
void CompareAverageAC(const std::vector< std::vector< TH1D * >> &histograms, const std::vector< std::unique_ptr< TH1D >> &averages, const std::vector< TString > &hist_labels, const TString &output_name, bool draw_min_max=true, bool draw_all=false, bool draw_errors=true)
void PlotAutoCorr(TString fname1, TString flabel1, TString fname2, TString flabel2, TString fname3, TString flabel3, TString fname4, TString flabel4)
std::unique_ptr< TH1D > AutocorrProcessInputs(const TString &input_file, std::vector< TH1D * > &histograms)
std::pair< std::unique_ptr< TH1D >, std::unique_ptr< TH1D > > CalculateMinMaxHistograms(const std::vector< TH1D * > &histograms)
Type Get(const YAML::Node &node, const std::string File, const int Line)
Get content of config file.
Custom exception class for MaCh3 errors.
std::unique_ptr< ObjectType > Clone(const ObjectType *obj, const std::string &name="")
KS: Creates a copy of a ROOT-like object and wraps it in a smart pointer.
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.