Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MakePrettyPlot.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MakePrettyPlot.C
1 // 'MakePrettyPlot.C'
2 // Derek Anderson
3 // 04.18.2021
4 //
5 // Use this quickly plot a set of numerator
6 // distributions against a denominator
7 // distribution
8 
9 #include <iostream>
10 #include "TH1.h"
11 #include "TPad.h"
12 #include "TFile.h"
13 #include "TLine.h"
14 #include "TError.h"
15 #include "TString.h"
16 #include "TLegend.h"
17 #include "TCanvas.h"
18 #include "TPaveText.h"
19 
20 using namespace std;
21 
22 // global constants
23 static const UInt_t NNumer(14);
24 static const UInt_t NPlot(2);
25 static const UInt_t NPad(2);
26 static const UInt_t NVtx(4);
27 
28 
29 
31 
32  // lower verbosity
33  gErrorIgnoreLevel = kError;
34  cout << "\n Beginning plot macro..." << endl;
35 
36  // output and denominator parameters
37  const TString sOutput("recoCorrelator.forDebug_checkingHowCorrChanges.d9m4y2023.root");
38  const TString sDenom("reference/alex_output/pp200run6jet40.SphenixRecoUnscaled_output_trksAndChrgPars.root");
39  const TString sHistDenom("EEC0");
40  const TString sNameDenom("hReferenceReco");
41  const TString sLabelDenom("Reference output [Reco]");
42 
43  // numerator parameters
44  const TString sNumer[NNumer] = {"reference/alex_code/SphenixRecoUnscaled_output_derekTestRun.root",
45  "reference/alex_code/SphenixRecoUnscaled_output_derekTestRun2.root",
46  "reference/alex_code/SphenixRecoUnscaled_output_derekTestRun3.root",
47  "pp200run6jet40.forDebug_reco_initialRun.d7m4y2023.root",
48  "pp200run6jet40.forDebug_reco_usingTVectors.d7m4y2023.root",
49  "pp200run6jet40.forDebug_reco_testingMinDr.d7m4y2023.root",
50  "pp200run6jet40.forDebug_reco_noCstCuts.d7m4y2023.root",
51  "pp200run6jet40.forDebug_reco_beingMoreCarefulWithJetPt.d7m4y2023.root",
52  "pp200run6jet40.forDebug_reco_onlyOnePtJetRange.d9m4y2023.root",
53  "pp200run6jet40.forDebug_reco_checkingJetPtRange.d9m4y2023.root",
54  "pp200run6jet40.forDebug_reco_usingAbsValEta.d9m4y2023.root",
55  "pp200run6jet40.forDebug_reco_numCstsG0.d9m4y2023.root",
56  "pp200run6jet40.forDebug_reco_checkingNumCst.d10m4y2023.root",
57  "pp200run6jet40.forDebug_reco_hardCodingNPoint.d10m4y2023.root"};
58  const TString sHistNumer[NNumer] = {"EEC0",
59  "EEC0",
60  "EEC0",
61  "hCorrelatorDrAxis_ptJet15",
62  "hCorrelatorDrAxis_ptJet15",
63  "hCorrelatorDrAxis_ptJet15",
64  "hCorrelatorDrAxis_ptJet15",
65  "hCorrelatorDrAxis_ptJet15",
66  "hCorrelatorDrAxis_ptJet15",
67  "hCorrelatorDrAxis_ptJet15",
68  "hCorrelatorDrAxis_ptJet15",
69  "hCorrelatorDrAxis_ptJet15",
70  "hCorrelatorDrAxis_ptJet15",
71  "hCorrelatorDrAxis_ptJet15"};
72  const TString sNameNumer[NNumer] = {"hLocalAlexTest",
73  "hLocalAlexTest_WithMyEECInit",
74  "hLocalAlexTest_Only1520",
75  "hF4A_InitialRun",
76  "hF4A_UsingTVectors",
77  "hF4A_TestingMinDr",
78  "hF4A_NoCstCuts",
79  "hF4A_BeingMoreCarefulWithJetPt",
80  "hF4A_OnlyOnePtJetRange",
81  "hF4A_CheckingPtJetRange",
82  "hF4A_UsingAbsValEta",
83  "hF4A_NumCstsG0",
84  "hF4A_CheckingNumCst",
85  "hF4A_HardCodedNPoint"};
86  const TString sNameRatio[NNumer] = {"hRatio_LocalAlexTest",
87  "hRatio_LocalAlexTest_WithMyEECInit",
88  "hRatio_LocalAlexTest_Only1520",
89  "hRatio_InitialRun",
90  "hRatio_UsingTVectors",
91  "hRatio_TestingMinDr",
92  "hRatio_NoCstCuts",
93  "hRatio_BeingMoreCarefulWithJetPt",
94  "hRatio_OnlyOnePtJetRange",
95  "hRatio_CheckingPtJetRange",
96  "hRatio_UsingAbsValEta",
97  "hRatio_NumCstsG0",
98  "hRatio_CheckingNumCst",
99  "hRatio_HardCodedNPoint"};
100  const TString sLabelNumer[NNumer] = {"Local test of Alex's Code",
101  "Local test of Alex's Code (using my EEC initialization/header)",
102  "Local test of Alex's Code (only pTjet = 15 - 20 GeV/c)",
103  "Initial F4A run",
104  "Using TVectors for pTcst calc",
105  "Testing minimum #DeltaR",
106  "No cst. cuts",
107  "Being careful with pTjet selection",
108  "Only 1 pTjet range",
109  "Checking pTjet range",
110  "Using |eta| in cut",
111  "N_{cst} must be > 0",
112  "Checking N_{cst} (no truth calc done)",
113  "Hard-coded N-point"};
114 
115  // rebin parameters
116  const UInt_t nRebin(2);
117  const Bool_t doRebin(false);
118 
119  // plot parameters
120  const TString sOptDenom("");
121  const TString sOptNumer[NNumer] = {"SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME"};
122  const TString sOptRatio[NNumer] = {"", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME", "SAME"};
123  const Float_t xPlotRange[NPlot] = {0.001, 1.};
124 
125  // style parameters
126  const TString sTitle("");
127  const TString sTitleX("#DeltaR");
128  const TString sTitleY("EEC");
129  const TString sTitleR("[change] / [reference]");
130  const UInt_t fColDen(923);
131  const UInt_t fMarDen(20);
132  const UInt_t fColNum[NNumer] = {799, 809, 899, 909, 879, 889, 859, 869, 839, 849, 819, 829, 921, 922};
133  const UInt_t fMarNum[NNumer] = {24, 26, 32, 25, 27, 28, 30, 30, 46, 24, 26, 32, 25, 26};
134 
135  // text parameters
136  const TString sSys("#bf{#it{sPHENIX}} Simulation [Run6]");
137  const TString sTrg("JS 40 GeV Jet Sample");
138  const TString sJet("p_{T}^{jet} #in (15, 20) GeV");
139  const TString sTyp("");
140  const TString sHeader("");
141 
142  // open output and target files
143  TFile *fOutput = new TFile(sOutput.Data(), "recreate");
144  TFile *fDenom = new TFile(sDenom.Data(), "read");
145  if (!fOutput || !fDenom) {
146  cerr << "PANIC: couldn't open output or denominator file!\n"
147  << " fOutput = " << fOutput << ", fDenom = " << fDenom
148  << endl;
149  return;
150  }
151  cout << " Opened output and denominator files." << endl;
152 
153  // open unfolding files
154  TFile *fNumer[NNumer];
155  for (UInt_t iNumer = 0; iNumer < NNumer; iNumer++) {
156  fNumer[iNumer] = new TFile(sNumer[iNumer].Data(), "read");
157  if (!fNumer[iNumer]) {
158  cerr << "PANIC: couldn't open numerator file #" << iNumer << "!" << endl;
159  return;
160  }
161  }
162  cout << " Opened numerator files." << endl;
163 
164  // grab target histogram
165  TH1D *hDenom = (TH1D*) fDenom -> Get(sHistDenom.Data());
166  if (!hDenom) {
167  cerr << "PANIC: couldn't grab denominator histogram!" << endl;
168  return;
169  }
170  hDenom -> SetName(sNameDenom.Data());
171  cout << " Grabbed denominator histogram." << endl;
172 
173  // grab unfolding histograms
174  TH1D *hNumer[NNumer];
175  for (UInt_t iNumer = 0; iNumer < NNumer; iNumer++) {
176  hNumer[iNumer] = (TH1D*) fNumer[iNumer] -> Get(sHistNumer[iNumer]);
177  if (!hNumer[iNumer]) {
178  cerr << "PANIC: couldn't grab numerator histogram #" << iNumer << "!" << endl;
179  return;
180  }
181  hNumer[iNumer] -> SetName(sNameNumer[iNumer].Data());
182  }
183  cout << " Grabbed numerator histograms." << endl;
184 
185  // rebin histograms (if needed)
186  if (doRebin) {
187  for (UInt_t iNumer = 0; iNumer < NNumer; iNumer++) {
188  hNumer[iNumer] -> Rebin(nRebin);
189  }
190  hDenom -> Rebin(nRebin);
191  cout << " Rebinned histograms." << endl;
192  }
193 
194  // calculate ratios
195  TH1D *hRatio[NNumer];
196  for (UInt_t iNumer = 0; iNumer < NNumer; iNumer++) {
197  hRatio[iNumer] = (TH1D*) hDenom -> Clone();
198  hRatio[iNumer] -> Reset("ICE");
199  hRatio[iNumer] -> Divide(hNumer[iNumer], hDenom, 1., 1.);
200  hRatio[iNumer] -> SetName(sNameRatio[iNumer]);
201  }
202  cout << " Calculated ratios." << endl;
203 
204  // set styles
205  const UInt_t fFil(0);
206  const UInt_t fLin(1);
207  const UInt_t fWid(1);
208  const UInt_t fTxt(42);
209  const UInt_t fAln(12);
210  const UInt_t fCnt(1);
211  const Float_t fLab[NPad] = {0.074, 0.04};
212  const Float_t fTit[NPad] = {0.074, 0.04};
213  const Float_t fOffX[NPad] = {1.1, 1.};
214  const Float_t fOffY[NPad] = {0.7, 1.3};
215  hDenom -> SetMarkerColor(fColDen);
216  hDenom -> SetMarkerStyle(fMarDen);
217  hDenom -> SetFillColor(fColDen);
218  hDenom -> SetFillStyle(fFil);
219  hDenom -> SetLineColor(fColDen);
220  hDenom -> SetLineStyle(fLin);
221  hDenom -> SetLineWidth(fWid);
222  hDenom -> SetTitle(sTitle.Data());
223  hDenom -> SetTitleFont(fTxt);
224  hDenom -> GetXaxis() -> SetRangeUser(xPlotRange[0], xPlotRange[1]);
225  hDenom -> GetXaxis() -> SetTitle(sTitleX.Data());
226  hDenom -> GetXaxis() -> SetTitleFont(fTxt);
227  hDenom -> GetXaxis() -> SetTitleSize(fTit[1]);
228  hDenom -> GetXaxis() -> SetTitleOffset(fOffX[1]);
229  hDenom -> GetXaxis() -> SetLabelFont(fTxt);
230  hDenom -> GetXaxis() -> SetLabelSize(fLab[1]);
231  hDenom -> GetXaxis() -> CenterTitle(fCnt);
232  hDenom -> GetYaxis() -> SetTitle(sTitleY.Data());
233  hDenom -> GetYaxis() -> SetTitleFont(fTxt);
234  hDenom -> GetYaxis() -> SetTitleSize(fTit[1]);
235  hDenom -> GetYaxis() -> SetTitleOffset(fOffY[1]);
236  hDenom -> GetYaxis() -> SetLabelFont(fTxt);
237  hDenom -> GetYaxis() -> SetLabelSize(fLab[1]);
238  hDenom -> GetYaxis() -> CenterTitle(fCnt);
239  for (UInt_t iNumer = 0; iNumer < NNumer; iNumer++) {
240  hNumer[iNumer] -> SetMarkerColor(fColNum[iNumer]);
241  hNumer[iNumer] -> SetMarkerStyle(fMarNum[iNumer]);
242  hNumer[iNumer] -> SetFillColor(fColNum[iNumer]);
243  hNumer[iNumer] -> SetFillStyle(fFil);
244  hNumer[iNumer] -> SetLineColor(fColNum[iNumer]);
245  hNumer[iNumer] -> SetLineStyle(fLin);
246  hNumer[iNumer] -> SetLineWidth(fWid);
247  hNumer[iNumer] -> SetTitle(sTitle.Data());
248  hNumer[iNumer] -> SetTitleFont(fTxt);
249  hNumer[iNumer] -> GetXaxis() -> SetRangeUser(xPlotRange[0], xPlotRange[1]);
250  hNumer[iNumer] -> GetXaxis() -> SetTitle(sTitleX.Data());
251  hNumer[iNumer] -> GetXaxis() -> SetTitleFont(fTxt);
252  hNumer[iNumer] -> GetXaxis() -> SetTitleSize(fTit[1]);
253  hNumer[iNumer] -> GetXaxis() -> SetTitleOffset(fOffX[1]);
254  hNumer[iNumer] -> GetXaxis() -> SetLabelFont(fTxt);
255  hNumer[iNumer] -> GetXaxis() -> SetLabelSize(fLab[1]);
256  hNumer[iNumer] -> GetXaxis() -> CenterTitle(fCnt);
257  hNumer[iNumer] -> GetYaxis() -> SetTitle(sTitleY.Data());
258  hNumer[iNumer] -> GetYaxis() -> SetTitleFont(fTxt);
259  hNumer[iNumer] -> GetYaxis() -> SetTitleSize(fTit[1]);
260  hNumer[iNumer] -> GetYaxis() -> SetTitleOffset(fOffY[1]);
261  hNumer[iNumer] -> GetYaxis() -> SetLabelFont(fTxt);
262  hNumer[iNumer] -> GetYaxis() -> SetLabelSize(fLab[1]);
263  hNumer[iNumer] -> GetYaxis() -> CenterTitle(fCnt);
264  hRatio[iNumer] -> SetMarkerColor(fColNum[iNumer]);
265  hRatio[iNumer] -> SetMarkerStyle(fMarNum[iNumer]);
266  hRatio[iNumer] -> SetFillColor(fColNum[iNumer]);
267  hRatio[iNumer] -> SetFillStyle(fFil);
268  hRatio[iNumer] -> SetLineColor(fColNum[iNumer]);
269  hRatio[iNumer] -> SetLineStyle(fLin);
270  hRatio[iNumer] -> SetLineWidth(fWid);
271  hRatio[iNumer] -> SetTitle(sTitle.Data());
272  hRatio[iNumer] -> SetTitleFont(fTxt);
273  hRatio[iNumer] -> GetXaxis() -> SetRangeUser(xPlotRange[0], xPlotRange[1]);
274  hRatio[iNumer] -> GetXaxis() -> SetTitle(sTitleX.Data());
275  hRatio[iNumer] -> GetXaxis() -> SetTitleFont(fTxt);
276  hRatio[iNumer] -> GetXaxis() -> SetTitleSize(fTit[0]);
277  hRatio[iNumer] -> GetXaxis() -> SetTitleOffset(fOffX[0]);
278  hRatio[iNumer] -> GetXaxis() -> SetLabelFont(fTxt);
279  hRatio[iNumer] -> GetXaxis() -> SetLabelSize(fLab[0]);
280  hRatio[iNumer] -> GetXaxis() -> CenterTitle(fCnt);
281  hRatio[iNumer] -> GetYaxis() -> SetTitle(sTitleR.Data());
282  hRatio[iNumer] -> GetYaxis() -> SetTitleFont(fTxt);
283  hRatio[iNumer] -> GetYaxis() -> SetTitleSize(fTit[0]);
284  hRatio[iNumer] -> GetYaxis() -> SetTitleOffset(fOffY[0]);
285  hRatio[iNumer] -> GetYaxis() -> SetLabelFont(fTxt);
286  hRatio[iNumer] -> GetYaxis() -> SetLabelSize(fLab[0]);
287  hRatio[iNumer] -> GetYaxis() -> CenterTitle(fCnt);
288  }
289  cout << " Set styles." << endl;
290 
291  // make legend
292  const UInt_t fColLe(0);
293  const UInt_t fFilLe(0);
294  const UInt_t fLinLe(0);
295  const Float_t fLegXY[NVtx] = {0.1, 0.1, 0.3, 0.3};
296  TLegend *leg = new TLegend(fLegXY[0], fLegXY[1], fLegXY[2], fLegXY[3], sHeader.Data());
297  leg -> SetFillColor(fColLe);
298  leg -> SetFillStyle(fFilLe);
299  leg -> SetLineColor(fColLe);
300  leg -> SetLineStyle(fLinLe);
301  leg -> SetTextFont(fTxt);
302  leg -> SetTextAlign(fAln);
303  leg -> AddEntry(hDenom, sLabelDenom.Data(), "pf");
304  for (UInt_t iNumer = 0; iNumer < NNumer; iNumer++) {
305  leg -> AddEntry(hNumer[iNumer], sLabelNumer[iNumer], "pf");
306  }
307  cout << " Made legend." << endl;
308 
309  // make text
310  const UInt_t fColTx(0);
311  const UInt_t fFilTx(0);
312  const UInt_t fLinTx(0);
313  const Float_t fTxtXY[NVtx] = {0.3, 0.1, 0.5, 0.3};
314  TPaveText *txt = new TPaveText(fTxtXY[0], fTxtXY[1], fTxtXY[2], fTxtXY[3], "NDC NB");
315  txt -> SetFillColor(fColTx);
316  txt -> SetFillStyle(fFilTx);
317  txt -> SetLineColor(fColTx);
318  txt -> SetLineStyle(fLinTx);
319  txt -> SetTextFont(fTxt);
320  txt -> SetTextAlign(fAln);
321  txt -> AddText(sSys.Data());
322  txt -> AddText(sTrg.Data());
323  txt -> AddText(sJet.Data());
324  txt -> AddText(sTyp.Data());
325  cout << " Made text." << endl;
326 
327  // make line
328  const UInt_t fColLi(1);
329  const UInt_t fLinLi(9);
330  const UInt_t fWidLi(1);
331  const Float_t fLinXY[NVtx] = {xPlotRange[0], 1., xPlotRange[1], 1.};
332  TLine *line = new TLine(fLinXY[0], fLinXY[1], fLinXY[2], fLinXY[3]);
333  line -> SetLineColor(fColLi);
334  line -> SetLineStyle(fLinLi);
335  line -> SetLineWidth(fWidLi);
336  cout << " Made line." << endl;
337 
338  // make plot
339  const UInt_t width(750);
340  const UInt_t height(950);
341  const UInt_t heightNR(750);
342  const UInt_t fMode(0);
343  const UInt_t fBord(2);
344  const UInt_t fGrid(0);
345  const UInt_t fTick(1);
346  const UInt_t fLogX(1);
347  const UInt_t fLogY1(0);
348  const UInt_t fLogY2(1);
349  const UInt_t fLogYNR(1);
350  const UInt_t fFrame(0);
351  const Float_t fMarginL(0.15);
352  const Float_t fMarginR(0.02);
353  const Float_t fMarginT1(0.005);
354  const Float_t fMarginT2(0.02);
355  const Float_t fMarginTNR(0.02);
356  const Float_t fMarginB1(0.25);
357  const Float_t fMarginB2(0.005);
358  const Float_t fMarginBNR(0.15);
359  const Float_t fPadXY1[NVtx] = {0., 0., 1., 0.35};
360  const Float_t fPadXY2[NVtx] = {0., 0.35, 1., 1.};
361 
362  TCanvas *cPlot = new TCanvas("cPlot", "", width, height);
363  TPad *pPad1 = new TPad("pPad1", "", fPadXY1[0], fPadXY1[1], fPadXY1[2], fPadXY1[3]);
364  TPad *pPad2 = new TPad("pPad2", "", fPadXY2[0], fPadXY2[1], fPadXY2[2], fPadXY2[3]);
365  cPlot -> SetGrid(fGrid, fGrid);
366  cPlot -> SetTicks(fTick, fTick);
367  cPlot -> SetBorderMode(fMode);
368  cPlot -> SetBorderSize(fBord);
369  pPad1 -> SetGrid(fGrid, fGrid);
370  pPad1 -> SetTicks(fTick, fTick);
371  pPad1 -> SetLogx(fLogX);
372  pPad1 -> SetLogy(fLogY1);
373  pPad1 -> SetBorderMode(fMode);
374  pPad1 -> SetBorderSize(fBord);
375  pPad1 -> SetFrameBorderMode(fFrame);
376  pPad1 -> SetLeftMargin(fMarginL);
377  pPad1 -> SetRightMargin(fMarginR);
378  pPad1 -> SetTopMargin(fMarginT1);
379  pPad1 -> SetBottomMargin(fMarginB1);
380  pPad2 -> SetGrid(fGrid, fGrid);
381  pPad2 -> SetTicks(fTick, fTick);
382  pPad2 -> SetLogx(fLogX);
383  pPad2 -> SetLogy(fLogY2);
384  pPad2 -> SetBorderMode(fMode);
385  pPad2 -> SetBorderSize(fBord);
386  pPad2 -> SetFrameBorderMode(fFrame);
387  pPad2 -> SetLeftMargin(fMarginL);
388  pPad2 -> SetRightMargin(fMarginR);
389  pPad2 -> SetTopMargin(fMarginT2);
390  pPad2 -> SetBottomMargin(fMarginB2);
391  cPlot -> cd();
392  pPad1 -> Draw();
393  pPad2 -> Draw();
394  pPad1 -> cd();
395  hRatio[0] -> Draw(sOptRatio[0].Data());
396  for (UInt_t iNumer = 1; iNumer < NNumer; iNumer++) {
397  hRatio[iNumer] -> Draw(sOptRatio[iNumer].Data());
398  }
399  line -> Draw();
400  pPad2 -> cd();
401  hDenom -> Draw(sOptDenom.Data());
402  for(UInt_t iNumer = 0; iNumer < NNumer; iNumer++) {
403  hNumer[iNumer] -> Draw(sOptNumer[iNumer].Data());
404  }
405  leg -> Draw();
406  txt -> Draw();
407  fOutput -> cd();
408  cPlot -> Write();
409  cPlot -> Close();
410 
411  TCanvas *cPlotNR = new TCanvas("cPlotNoRatio", "", width, heightNR);
412  cPlotNR -> SetGrid(fGrid, fGrid);
413  cPlotNR -> SetTicks(fTick, fTick);
414  cPlotNR -> SetBorderMode(fMode);
415  cPlotNR -> SetBorderSize(fBord);
416  cPlotNR -> SetFrameBorderMode(fFrame);
417  cPlotNR -> SetLeftMargin(fMarginL);
418  cPlotNR -> SetRightMargin(fMarginR);
419  cPlotNR -> SetTopMargin(fMarginTNR);
420  cPlotNR -> SetBottomMargin(fMarginBNR);
421  cPlotNR -> SetLogx(fLogX);
422  cPlotNR -> SetLogy(fLogYNR);
423  cPlotNR -> cd();
424  hDenom -> Draw(sOptDenom.Data());
425  for(UInt_t iNumer = 0; iNumer < NNumer; iNumer++) {
426  hNumer[iNumer] -> Draw(sOptNumer[iNumer].Data());
427  }
428  leg -> Draw();
429  txt -> Draw();
430  fOutput -> cd();
431  cPlotNR -> Write();
432  cPlotNR -> Close();
433  cout << " Made plot." << endl;
434 
435  // save histograms
436  fOutput -> cd();
437  hDenom -> Write();
438  for (UInt_t iNumer = 0; iNumer < NNumer; iNumer++) {
439  hNumer[iNumer] -> Write();
440  hRatio[iNumer] -> Write();
441  }
442  cout << " Saved histograms." << endl;
443 
444  // close files
445  fOutput -> cd();
446  fOutput -> Close();
447  fDenom -> cd();
448  fDenom -> Close();
449  for (UInt_t iNumer = 0; iNumer < NNumer; iNumer++) {
450  fNumer[iNumer] -> cd();
451  fNumer[iNumer] -> Close();
452  }
453  cout << " Finished plot!\n" << endl;
454 
455 }
456 
457 // end ------------------------------------------------------------------------