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