2 #include "PlottingUtils/PlottingUtils.h"
3 #include "PlottingUtils/PlottingManager.h"
6 #pragma GCC diagnostic ignored "-Wfloat-conversion"
7 #pragma GCC diagnostic ignored "-Wconversion"
15 const std::string& Title,
16 const std::vector<std::string>& Params,
17 const std::unique_ptr<M3::Plotting::PlottingManager>& man)
21 for(
size_t i = 0; i < Params.size(); i++)
23 for(
int j = 0; j < MatrixFull->GetNbinsX(); j++)
25 if(MatrixFull->GetXaxis()->GetBinLabel(j+1) == Params[i])
33 for(
size_t i = 0; i < Params.size(); i++)
36 MACH3LOG_ERROR(
"Didn't find param {} in matrix within {} sub-block", Params[i], Title);
39 auto new_end = std::remove(ParamIndex.begin(), ParamIndex.end(),
M3::_BAD_INT_);
40 ParamIndex.erase(new_end, ParamIndex.end());
42 auto Hist = std::make_unique<TH2D>(Title.c_str(), Title.c_str(), ParamIndex.size(), 0, ParamIndex.size(), ParamIndex.size(), 0, ParamIndex.size());
43 Hist->SetDirectory(
nullptr);
44 Hist->GetZaxis()->SetTitle(
"Correlation");
45 Hist->SetMinimum(-1.);
48 for(
size_t x = 0; x < ParamIndex.size(); x++)
50 for(
size_t y = 0; y < ParamIndex.size(); y++)
52 Hist->SetBinContent(x+1, y+1, MatrixFull->GetBinContent(ParamIndex[x]+1, ParamIndex[y]+1));
55 std::string FancyLabel = man->style().prettifyParamName(MatrixFull->GetXaxis()->GetBinLabel(ParamIndex[x]+1));
56 Hist->GetXaxis()->SetBinLabel(x+1, FancyLabel.c_str());
57 Hist->GetYaxis()->SetBinLabel(x+1, FancyLabel.c_str());
64 gStyle->SetOptStat(0);
67 canv->SetBottomMargin(0.2);
68 canv->SetTopMargin(0.1);
69 canv->SetRightMargin(0.15);
70 canv->SetLeftMargin(0.15);
71 gStyle->SetOptTitle(1);
75 if (Hist->GetNbinsX() < 20) {
76 Hist->SetMarkerSize(1.0);
77 Hist->GetXaxis()->SetLabelSize(0.02);
78 Hist->GetYaxis()->SetLabelSize(0.02);
80 Hist->SetMarkerSize(0.5);
81 Hist->GetXaxis()->SetLabelSize(0.015);
82 Hist->GetYaxis()->SetLabelSize(0.015);
86 void SetupInfo(
const std::string& Config, std::vector<std::string>& Title, std::vector<std::vector<std::string>>& Params)
92 YAML::Node settings =
config[
"MatrixPlotter"];
95 Title = settings[
"Titles"].as<std::vector<std::string>>();
96 Params.resize(Title.size());
99 for(
size_t it = 0; it < Title.size(); it++)
101 if (settings[Title[it]]) {
102 Params[it] = settings[Title[it]].as<std::vector<std::string>>();
110 if(Title.size() != Params.size())
117 void PlotMatrix(
const std::unique_ptr<M3::Plotting::PlottingManager>& man,
const std::string& Config,
const std::string& File)
120 TFile *file =
M3::Open(File,
"UPDATE", __FILE__, __LINE__);
121 TH2D *MatrixFull =
nullptr;
122 file->GetObject(
"Correlation_plot", MatrixFull);
129 auto MatrixPlot = std::make_unique<TCanvas>(
"MatrixPlot",
"MatrixPlot", 0, 0, 1024, 1024);
131 gStyle->SetPaintTextFormat(
"4.1f");
134 constexpr
int NRGBs = 5;
135 TColor::InitializeColors();
136 Double_t stops[NRGBs] = { 0.00, 0.25, 0.50, 0.75, 1.00 };
137 Double_t red[NRGBs] = { 0.00, 0.25, 1.00, 1.00, 0.50 };
138 Double_t green[NRGBs] = { 0.00, 0.25, 1.00, 0.25, 0.00 };
139 Double_t blue[NRGBs] = { 0.50, 1.00, 1.00, 0.25, 0.00 };
140 TColor::CreateGradientColorTable(5, stops, red, green, blue, 255);
141 gStyle->SetNumberContours(255);
144 gErrorIgnoreLevel = kWarning;
146 MatrixFull->GetXaxis()->LabelsOption(
"v");
147 MatrixPlot->Print(
"MatrixPlot.pdf[");
148 MatrixFull->SetTitle(
"");
149 MatrixFull->Draw(
"COLZ");
150 MatrixPlot->Print(
"MatrixPlot.pdf");
151 std::vector<std::string> Title;
152 std::vector<std::vector<std::string>> Params;
156 for(
size_t it = 0; it < Title.size(); it++)
158 std::unique_ptr<TH2D> Hist =
GetSubMatrix(MatrixFull, Title[it], Params[it], man);
159 Hist->GetXaxis()->LabelsOption(
"v");
162 if(Hist->GetNbinsX() < 50) {
163 Hist->Draw(
"COLZ TEXT");
167 MatrixPlot->Print(
"MatrixPlot.pdf");
170 MatrixPlot->Print(
"MatrixPlot.pdf]");
178 const std::string& Config,
const std::string& File1,
const std::string& Title1,
179 const std::string& File2,
const std::string& Title2)
183 constexpr
int NFiles = 2;
185 file[0] =
M3::Open(File1,
"UPDATE", __FILE__, __LINE__);
186 file[1] =
M3::Open(File2,
"UPDATE", __FILE__, __LINE__);
188 TH2D *MatrixFull[NFiles] = {
nullptr};
189 for(
int i = 0; i < NFiles; i++) {
190 file[i]->GetObject(
"Correlation_plot", MatrixFull[i]);
192 auto MatrixPlot = std::make_unique<TCanvas>(
"MatrixPlot",
"MatrixPlot", 0, 0, 1024, 1024);
196 constexpr
int NRGBs = 10;
197 TColor::InitializeColors();
198 Double_t stops[NRGBs] = { 0.00, 0.10, 0.25, 0.35, 0.50, 0.60, 0.65, 0.75, 0.90, 1.00 };
199 Double_t red[NRGBs] = { 0.50, 1.00, 1.00, 0.25, 0.00, 0.10, 0.50, 1.00, 0.75, 0.55 };
200 Double_t green[NRGBs] = { 0.00, 0.25, 1.00, 0.25, 0.00, 0.60, 0.90, 1.00, 0.75, 0.75 };
201 Double_t blue[NRGBs] = { 0.00, 0.25, 1.00, 1.00, 0.50, 0.60, 0.90, 1.00, 0.05, 0.05 };
202 TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, 255);
203 gStyle->SetNumberContours(255);
206 gErrorIgnoreLevel = kWarning;
208 MatrixPlot->Print(
"MatrixComparePlot.pdf[");
210 std::vector<std::string> Title;
211 std::vector<std::vector<std::string>> Params;
215 for(
size_t it = 0; it < Title.size(); it++)
217 std::unique_ptr<TH2D> Hist[2];
218 for(
int i = 0; i < NFiles; i++)
220 Hist[i] =
GetSubMatrix(MatrixFull[i], Title[it], Params[it], man);
222 Hist[0]->GetZaxis()->SetTitle( (Title1 +
"/" + Title2).c_str());
223 Hist[0]->GetXaxis()->LabelsOption(
"v");
224 Hist[0]->Divide(Hist[1].get());
228 Hist[0]->Draw(
"COLZ");
229 MatrixPlot->Print(
"MatrixComparePlot.pdf");
231 MatrixPlot->Print(
"MatrixComparePlot.pdf]");
233 for(
int i = 0; i < NFiles; i++)
235 delete MatrixFull[i];
241 int main(
int argc,
char *argv[])
245 auto man = std::make_unique<M3::Plotting::PlottingManager>();
248 if (argc != 3 && argc != 6)
250 MACH3LOG_INFO(
"How to use: {} config.yaml MCMC_Processor_Output.root", argv[0]);
256 PlotMatrix(man, std::string(argv[1]), std::string(argv[2]));
262 CompareMatrices(man, std::string(argv[1]), std::string(argv[2]), std::string(argv[3]), std::string(argv[4]), std::string(argv[5]));
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
void DynamicLabelSize(TH2D *Hist)
int main(int argc, char *argv[])
void CompareMatrices(const std::unique_ptr< M3::Plotting::PlottingManager > &man, const std::string &Config, const std::string &File1, const std::string &Title1, const std::string &File2, const std::string &Title2)
std::unique_ptr< TH2D > GetSubMatrix(TH2D *MatrixFull, const std::string &Title, const std::vector< std::string > &Params, const std::unique_ptr< M3::Plotting::PlottingManager > &man)
Grab large Matrix only only extract submatrix based on label naming.
void SetupCanvas(TCanvas *canv)
void SetupInfo(const std::string &Config, std::vector< std::string > &Title, std::vector< std::vector< std::string >> &Params)
void PlotMatrix(const std::unique_ptr< M3::Plotting::PlottingManager > &man, const std::string &Config, const std::string &File)
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Custom exception class used throughout MaCh3.
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.
constexpr static const int _BAD_INT_
Default value used for int initialisation.