MaCh3  2.4.2
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<MaCh3Plotting::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 DynamicLabelSize(TH2D* Hist) {
63  if (Hist->GetNbinsX() < 20) {
64  Hist->SetMarkerSize(1.0);
65  Hist->GetXaxis()->SetLabelSize(0.02);
66  Hist->GetYaxis()->SetLabelSize(0.02);
67  } else {
68  Hist->SetMarkerSize(0.5);
69  Hist->GetXaxis()->SetLabelSize(0.015);
70  Hist->GetYaxis()->SetLabelSize(0.015);
71  }
72 }
73 
74 void SetupInfo(const std::string& Config, std::vector<std::string>& Title, std::vector<std::vector<std::string>>& Params)
75 {
76  // Load the YAML file
77  YAML::Node config = M3OpenConfig(Config);
78 
79  // Access the "MatrixPlotter" section
80  YAML::Node settings = config["MatrixPlotter"];
81 
82  // Retrieve the list of Titles
83  Title = settings["Titles"].as<std::vector<std::string>>();
84  Params.resize(Title.size());
85 
86  // Retrieve parameters for each title (using the titles as keys)
87  for(size_t it = 0; it < Title.size(); it++)
88  {
89  if (settings[Title[it]]) {
90  Params[it] = settings[Title[it]].as<std::vector<std::string>>();
91  } else {
92  MACH3LOG_ERROR("Missing key in YAML for: {}", Title[it]);
93  throw MaCh3Exception(__FILE__ , __LINE__ );
94  }
95  }
96 
97  // Check if sizes match
98  if(Title.size() != Params.size())
99  {
100  MACH3LOG_ERROR("Size doesn't match");
101  throw MaCh3Exception(__FILE__ , __LINE__ );
102  }
103 }
104 
105 void PlotMatrix(const std::unique_ptr<MaCh3Plotting::PlottingManager>& man, const std::string& Config, const std::string& File)
106 {
107  // Open the ROOT file
108  TFile *file = M3::Open(File, "UPDATE", __FILE__, __LINE__);
109  TH2D *MatrixFull = nullptr;
110  file->GetObject("Correlation_plot", MatrixFull);
111 
112  if (!MatrixFull) {
113  MACH3LOG_ERROR("Error: Could not retrieve histogram");
114  throw MaCh3Exception(__FILE__ , __LINE__ );
115  }
116 
117  auto MatrixPlot = std::make_unique<TCanvas>("MatrixPlot", "MatrixPlot", 0, 0, 1024, 1024);
118  MatrixPlot->SetGrid();
119  gStyle->SetOptStat(0);
120  gStyle->SetOptTitle(0);
121  MatrixPlot->SetTickx();
122  MatrixPlot->SetTicky();
123  MatrixPlot->SetBottomMargin(0.2);
124  MatrixPlot->SetTopMargin(0.1);
125  MatrixPlot->SetRightMargin(0.15);
126  MatrixPlot->SetLeftMargin(0.15);
127  gStyle->SetOptTitle(1);
128  gStyle->SetPaintTextFormat("4.1f");
129 
130  // Make pretty Correlation colors (red to blue)
131  constexpr int NRGBs = 5;
132  TColor::InitializeColors();
133  Double_t stops[NRGBs] = { 0.00, 0.25, 0.50, 0.75, 1.00 };
134  Double_t red[NRGBs] = { 0.00, 0.25, 1.00, 1.00, 0.50 };
135  Double_t green[NRGBs] = { 0.00, 0.25, 1.00, 0.25, 0.00 };
136  Double_t blue[NRGBs] = { 0.50, 1.00, 1.00, 0.25, 0.00 };
137  TColor::CreateGradientColorTable(5, stops, red, green, blue, 255);
138  gStyle->SetNumberContours(255);
139 
140  //To avoid TCanvas::Print> messages
141  gErrorIgnoreLevel = kWarning;
142  DynamicLabelSize(MatrixFull);
143  MatrixFull->GetXaxis()->LabelsOption("v");
144  MatrixPlot->Print("MatrixPlot.pdf[");
145  MatrixFull->SetTitle("");
146  MatrixFull->Draw("COLZ");
147  MatrixPlot->Print("MatrixPlot.pdf");
148  std::vector<std::string> Title;
149  std::vector<std::vector<std::string>> Params;
150 
151  SetupInfo(Config, Title, Params);
152 
153  for(size_t it = 0; it < Title.size(); it++)
154  {
155  std::unique_ptr<TH2D> Hist = GetSubMatrix(MatrixFull, Title[it], Params[it], man);
156  Hist->GetXaxis()->LabelsOption("v");
157  DynamicLabelSize(Hist.get());
158 
159  if(Hist->GetNbinsX() < 50) {
160  Hist->Draw("COLZ TEXT");
161  } else {
162  Hist->Draw("COLZ");
163  }
164  MatrixPlot->Print("MatrixPlot.pdf");
165  }
166 
167  MatrixPlot->Print("MatrixPlot.pdf]");
168 
169  delete MatrixFull;
170  file->Close();
171  delete file;
172 }
173 
174 void CompareMatrices(const std::unique_ptr<MaCh3Plotting::PlottingManager>& man,
175  const std::string& Config, const std::string& File1, const std::string& Title1,
176  const std::string& File2, const std::string& Title2)
177 {
178  (void) man;
179  // Open the ROOT file
180  constexpr int NFiles = 2;
181  TFile *file[NFiles];
182  file[0] = M3::Open(File1, "UPDATE", __FILE__, __LINE__);
183  file[1] = M3::Open(File2, "UPDATE", __FILE__, __LINE__);
184 
185  TH2D *MatrixFull[NFiles] = {nullptr};
186  for(int i = 0; i < NFiles; i++) {
187  file[i]->GetObject("Correlation_plot", MatrixFull[i]);
188  }
189  auto MatrixPlot = std::make_unique<TCanvas>("MatrixPlot", "MatrixPlot", 0, 0, 1024, 1024);
190  MatrixPlot->SetGrid();
191  gStyle->SetOptStat(0);
192  gStyle->SetOptTitle(0);
193  MatrixPlot->SetTickx();
194  MatrixPlot->SetTicky();
195  MatrixPlot->SetBottomMargin(0.2);
196  MatrixPlot->SetTopMargin(0.1);
197  MatrixPlot->SetRightMargin(0.15);
198  MatrixPlot->SetLeftMargin(0.15);
199  gStyle->SetOptTitle(1);
200 
201  //KS: Fancy colors
202  constexpr int NRGBs = 10;
203  TColor::InitializeColors();
204  Double_t stops[NRGBs] = { 0.00, 0.10, 0.25, 0.35, 0.50, 0.60, 0.65, 0.75, 0.90, 1.00 };
205  Double_t red[NRGBs] = { 0.50, 1.00, 1.00, 0.25, 0.00, 0.10, 0.50, 1.00, 0.75, 0.55 };
206  Double_t green[NRGBs] = { 0.00, 0.25, 1.00, 0.25, 0.00, 0.60, 0.90, 1.00, 0.75, 0.75 };
207  Double_t blue[NRGBs] = { 0.00, 0.25, 1.00, 1.00, 0.50, 0.60, 0.90, 1.00, 0.05, 0.05 };
208  TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, 255);
209  gStyle->SetNumberContours(255);
210 
211  //To avoid TCanvas::Print> messages
212  gErrorIgnoreLevel = kWarning;
213 
214  MatrixPlot->Print("MatrixComparePlot.pdf[");
215 
216  std::vector<std::string> Title;
217  std::vector<std::vector<std::string>> Params;
218 
219  SetupInfo(Config, Title, Params);
220 
221  for(size_t it = 0; it < Title.size(); it++)
222  {
223  std::unique_ptr<TH2D> Hist[2];
224  for(int i = 0; i < NFiles; i++)
225  {
226  Hist[i] = GetSubMatrix(MatrixFull[i], Title[it], Params[it], man);
227  }
228  Hist[0]->GetZaxis()->SetTitle( (Title1 + "/" + Title2).c_str());
229  Hist[0]->GetXaxis()->LabelsOption("v");
230  Hist[0]->Divide(Hist[1].get());
231 
232  DynamicLabelSize(Hist[0].get());
233 
234  Hist[0]->Draw("COLZ");
235  MatrixPlot->Print("MatrixComparePlot.pdf");
236  }
237  MatrixPlot->Print("MatrixComparePlot.pdf]");
238 
239  for(int i = 0; i < NFiles; i++)
240  {
241  delete MatrixFull[i];
242  file[i]->Close();
243  delete file[i];
244  }
245 }
246 
247 int main(int argc, char *argv[])
248 {
250 
251  auto man = std::make_unique<MaCh3Plotting::PlottingManager>();
252  man->initialise();
253 
254  if (argc != 3 && argc != 6)
255  {
256  MACH3LOG_INFO("How to use: {} config.yaml MCMC_Processor_Output.root", argv[0]);
257  throw MaCh3Exception(__FILE__ , __LINE__ );
258  }
259 
260  if (argc == 3)
261  {
262  PlotMatrix(man, std::string(argv[1]), std::string(argv[2]));
263  }
264 
265  if (argc == 6)
266  {
267  MACH3LOG_INFO("Comparing matrices");
268  CompareMatrices(man, std::string(argv[1]), std::string(argv[2]), std::string(argv[3]), std::string(argv[4]), std::string(argv[5]));
269  }
270 
271  MACH3LOG_INFO("Finished plotting matrices");
272  return 0;
273 }
#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:61
void DynamicLabelSize(TH2D *Hist)
int main(int argc, char *argv[])
void SetupInfo(const std::string &Config, std::vector< std::string > &Title, std::vector< std::vector< std::string >> &Params)
void CompareMatrices(const std::unique_ptr< MaCh3Plotting::PlottingManager > &man, const std::string &Config, const std::string &File1, const std::string &Title1, const std::string &File2, const std::string &Title2)
void PlotMatrix(const std::unique_ptr< MaCh3Plotting::PlottingManager > &man, const std::string &Config, const std::string &File)
std::unique_ptr< TH2D > GetSubMatrix(TH2D *MatrixFull, const std::string &Title, const std::vector< std::string > &Params, const std::unique_ptr< MaCh3Plotting::PlottingManager > &man)
Grab large Matrix only only extract submatrix based on label naming.
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