MaCh3  2.6.0
Reference Guide
MatrixPlotter.cpp
Go to the documentation of this file.
1 //MaCh3 Includes
2 #include "PlottingUtils/PlottingUtils.h"
3 #include "PlottingUtils/PlottingManager.h"
4 
5 //this file has lots of usage of the ROOT plotting interface that only takes floats, turn this warning off for this CU for now
6 #pragma GCC diagnostic ignored "-Wfloat-conversion"
7 #pragma GCC diagnostic ignored "-Wconversion"
8 
12 
14 std::unique_ptr<TH2D> GetSubMatrix(TH2D *MatrixFull,
15  const std::string& Title,
16  const std::vector<std::string>& Params,
17  const std::unique_ptr<M3::Plotting::PlottingManager>& man)
18 {
19  std::vector<int> ParamIndex(Params.size(), M3::_BAD_INT_);
20 
21  for(size_t i = 0; i < Params.size(); i++)
22  {
23  for(int j = 0; j < MatrixFull->GetNbinsX(); j++)
24  {
25  if(MatrixFull->GetXaxis()->GetBinLabel(j+1) == Params[i])
26  {
27  ParamIndex[i] = j;
28  break;
29  }
30  }
31  }
32 
33  for(size_t i = 0; i < Params.size(); i++)
34  {
35  if(ParamIndex[i] == M3::_BAD_INT_)
36  MACH3LOG_ERROR("Didn't find param {} in matrix within {} sub-block", Params[i], Title);
37  }
38 
39  auto new_end = std::remove(ParamIndex.begin(), ParamIndex.end(), M3::_BAD_INT_);
40  ParamIndex.erase(new_end, ParamIndex.end());
41 
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.);
46  Hist->SetMaximum(1.);
47 
48  for(size_t x = 0; x < ParamIndex.size(); x++)
49  {
50  for(size_t y = 0; y < ParamIndex.size(); y++)
51  {
52  Hist->SetBinContent(x+1, y+1, MatrixFull->GetBinContent(ParamIndex[x]+1, ParamIndex[y]+1));
53  }
54 
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());
58  }
59  return Hist;
60 }
61 
62 void SetupCanvas(TCanvas* canv) {
63  canv->SetGrid();
64  gStyle->SetOptStat(0);
65  canv->SetTickx();
66  canv->SetTicky();
67  canv->SetBottomMargin(0.2);
68  canv->SetTopMargin(0.1);
69  canv->SetRightMargin(0.15);
70  canv->SetLeftMargin(0.15);
71  gStyle->SetOptTitle(1);
72 }
73 
74 void DynamicLabelSize(TH2D* Hist) {
75  if (Hist->GetNbinsX() < 20) {
76  Hist->SetMarkerSize(1.0);
77  Hist->GetXaxis()->SetLabelSize(0.02);
78  Hist->GetYaxis()->SetLabelSize(0.02);
79  } else {
80  Hist->SetMarkerSize(0.5);
81  Hist->GetXaxis()->SetLabelSize(0.015);
82  Hist->GetYaxis()->SetLabelSize(0.015);
83  }
84 }
85 
86 void SetupInfo(const std::string& Config, std::vector<std::string>& Title, std::vector<std::vector<std::string>>& Params)
87 {
88  // Load the YAML file
89  YAML::Node config = M3OpenConfig(Config);
90 
91  // Access the "MatrixPlotter" section
92  YAML::Node settings = config["MatrixPlotter"];
93 
94  // Retrieve the list of Titles
95  Title = settings["Titles"].as<std::vector<std::string>>();
96  Params.resize(Title.size());
97 
98  // Retrieve parameters for each title (using the titles as keys)
99  for(size_t it = 0; it < Title.size(); it++)
100  {
101  if (settings[Title[it]]) {
102  Params[it] = settings[Title[it]].as<std::vector<std::string>>();
103  } else {
104  MACH3LOG_ERROR("Missing key in YAML for: {}", Title[it]);
105  throw MaCh3Exception(__FILE__ , __LINE__ );
106  }
107  }
108 
109  // Check if sizes match
110  if(Title.size() != Params.size())
111  {
112  MACH3LOG_ERROR("Size doesn't match");
113  throw MaCh3Exception(__FILE__ , __LINE__ );
114  }
115 }
116 
117 void PlotMatrix(const std::unique_ptr<M3::Plotting::PlottingManager>& man, const std::string& Config, const std::string& File)
118 {
119  // Open the ROOT file
120  TFile *file = M3::Open(File, "UPDATE", __FILE__, __LINE__);
121  TH2D *MatrixFull = nullptr;
122  file->GetObject("Correlation_plot", MatrixFull);
123 
124  if (!MatrixFull) {
125  MACH3LOG_ERROR("Error: Could not retrieve histogram");
126  throw MaCh3Exception(__FILE__ , __LINE__ );
127  }
128 
129  auto MatrixPlot = std::make_unique<TCanvas>("MatrixPlot", "MatrixPlot", 0, 0, 1024, 1024);
130  SetupCanvas(MatrixPlot.get());
131  gStyle->SetPaintTextFormat("4.1f");
132 
133  // Make pretty Correlation colors (red to blue)
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);
142 
143  //To avoid TCanvas::Print> messages
144  gErrorIgnoreLevel = kWarning;
145  DynamicLabelSize(MatrixFull);
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;
153 
154  SetupInfo(Config, Title, Params);
155 
156  for(size_t it = 0; it < Title.size(); it++)
157  {
158  std::unique_ptr<TH2D> Hist = GetSubMatrix(MatrixFull, Title[it], Params[it], man);
159  Hist->GetXaxis()->LabelsOption("v");
160  DynamicLabelSize(Hist.get());
161 
162  if(Hist->GetNbinsX() < 50) {
163  Hist->Draw("COLZ TEXT");
164  } else {
165  Hist->Draw("COLZ");
166  }
167  MatrixPlot->Print("MatrixPlot.pdf");
168  }
169 
170  MatrixPlot->Print("MatrixPlot.pdf]");
171 
172  delete MatrixFull;
173  file->Close();
174  delete file;
175 }
176 
177 void CompareMatrices(const std::unique_ptr<M3::Plotting::PlottingManager>& man,
178  const std::string& Config, const std::string& File1, const std::string& Title1,
179  const std::string& File2, const std::string& Title2)
180 {
181  (void) man;
182  // Open the ROOT file
183  constexpr int NFiles = 2;
184  TFile *file[NFiles];
185  file[0] = M3::Open(File1, "UPDATE", __FILE__, __LINE__);
186  file[1] = M3::Open(File2, "UPDATE", __FILE__, __LINE__);
187 
188  TH2D *MatrixFull[NFiles] = {nullptr};
189  for(int i = 0; i < NFiles; i++) {
190  file[i]->GetObject("Correlation_plot", MatrixFull[i]);
191  }
192  auto MatrixPlot = std::make_unique<TCanvas>("MatrixPlot", "MatrixPlot", 0, 0, 1024, 1024);
193  SetupCanvas(MatrixPlot.get());
194 
195  //KS: Fancy colors
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);
204 
205  //To avoid TCanvas::Print> messages
206  gErrorIgnoreLevel = kWarning;
207 
208  MatrixPlot->Print("MatrixComparePlot.pdf[");
209 
210  std::vector<std::string> Title;
211  std::vector<std::vector<std::string>> Params;
212 
213  SetupInfo(Config, Title, Params);
214 
215  for(size_t it = 0; it < Title.size(); it++)
216  {
217  std::unique_ptr<TH2D> Hist[2];
218  for(int i = 0; i < NFiles; i++)
219  {
220  Hist[i] = GetSubMatrix(MatrixFull[i], Title[it], Params[it], man);
221  }
222  Hist[0]->GetZaxis()->SetTitle( (Title1 + "/" + Title2).c_str());
223  Hist[0]->GetXaxis()->LabelsOption("v");
224  Hist[0]->Divide(Hist[1].get());
225 
226  DynamicLabelSize(Hist[0].get());
227 
228  Hist[0]->Draw("COLZ");
229  MatrixPlot->Print("MatrixComparePlot.pdf");
230  }
231  MatrixPlot->Print("MatrixComparePlot.pdf]");
232 
233  for(int i = 0; i < NFiles; i++)
234  {
235  delete MatrixFull[i];
236  file[i]->Close();
237  delete file[i];
238  }
239 }
240 
241 int main(int argc, char *argv[])
242 {
244 
245  auto man = std::make_unique<M3::Plotting::PlottingManager>();
246  man->initialise();
247 
248  if (argc != 3 && argc != 6)
249  {
250  MACH3LOG_INFO("How to use: {} config.yaml MCMC_Processor_Output.root", argv[0]);
251  throw MaCh3Exception(__FILE__ , __LINE__ );
252  }
253 
254  if (argc == 3)
255  {
256  PlotMatrix(man, std::string(argv[1]), std::string(argv[2]));
257  }
258 
259  if (argc == 6)
260  {
261  MACH3LOG_INFO("Comparing matrices");
262  CompareMatrices(man, std::string(argv[1]), std::string(argv[2]), std::string(argv[3]), std::string(argv[4]), std::string(argv[5]));
263  }
264 
265  MACH3LOG_INFO("Finished plotting matrices");
266  return 0;
267 }
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
Definition: MaCh3Logger.h:60
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)
std::string config
Definition: ProcessMCMC.cpp:30
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Definition: YamlHelper.h:589
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.
Definition: Core.h:55