Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QuickTuplePlotter.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file QuickTuplePlotter.C
1 // ----------------------------------------------------------------------------
2 // 'QuickTuplePlotter.C
3 // Derek Anderson
4 // 06.02.2023
5 //
6 // Use to quick plot some leaves from an ntuple.
7 // ----------------------------------------------------------------------------
8 
9 #include <vector>
10 #include <iostream>
11 #include "TH1.h"
12 #include "TH2.h"
13 #include "TFile.h"
14 #include "TError.h"
15 #include "TString.h"
16 #include "TNtuple.h"
17 #include "TDirectory.h"
18 
19 using namespace std;
20 
21 
22 
24 
25  // lower verbosity
26  gErrorIgnoreLevel = kError;
27  cout << "\n Beginning quick tuple plotting..." << endl;
28 
29  // i/o parameters
30  const TString sOutput = "truthPhiCheck.allVsWeirdVsNormal.pt2040n100weird2pim.d10m7y2023.root";
31  const TString sInput = "input/embed_only/final_merge/sPhenixG4_forPtCheck_embedScanOn_embedOnly.pt2040n100pim.d8m5y2023.root";
32  const TString sInTuple = "ntp_track";
33 
34  // cuts to apply and labels
35  const vector<TString> vecCutsToApply = {
36  "(abs(vz)<10)&&(nintt>=1)&&(nmaps>2)&&(ntpc>35)&&(pt<100)&&(quality<10)",
37  "(abs(vz)<10)&&(nintt>=1)&&(nmaps>2)&&(ntpc>35)&&(pt<100)&&(quality<10)&&((pt/gpt<0.2)||(pt/gpt>1.2))",
38  "(abs(vz)<10)&&(nintt>=1)&&(nmaps>2)&&(ntpc>35)&&(pt<100)&&(quality<10)&&((pt/gpt>=0.2)&&(pt/gpt<=1.2))",
39  "(abs(vz)<10)&&(nintt>=1)&&(nmaps>2)&&(ntpc>35)&&(pt<20)&&(quality<10)",
40  "(abs(vz)<10)&&(nintt>=1)&&(nmaps>2)&&(ntpc>35)&&(pt<20)&&(quality<10)&&((pt/gpt<0.2)||(pt/gpt>1.2))",
41  "(abs(vz)<10)&&(nintt>=1)&&(nmaps>2)&&(ntpc>35)&&(pt<20)&&(quality<10)&&((pt/gpt>=0.2)&&(pt/gpt<=1.2))",
42  "(abs(vz)<10)&&(nintt>=1)&&(nmaps>2)&&(ntpc>35)&&(pt>20)&&(pt<100)&&(quality<10)",
43  "(abs(vz)<10)&&(nintt>=1)&&(nmaps>2)&&(ntpc>35)&&(pt>20)&&(pt<100)&&(quality<10)&&((pt/gpt<0.2)||(pt/gpt>1.2))",
44  "(abs(vz)<10)&&(nintt>=1)&&(nmaps>2)&&(ntpc>35)&&(pt>20)&&(pt<100)&&(quality<10)&&((pt/gpt>=0.2)&&(pt/gpt<=1.2))"
45  };
46  const vector<TString> vecCutLabels = {
47  "All",
48  "Weird",
49  "Normal",
50  "AllPtL20",
51  "WeirdPtL20",
52  "NormalPtL20",
53  "AllPtG20",
54  "WeirdPtG20",
55  "NormalPtG20"
56  };
57 
58  // 1d things to draw and histogram names
59  const vector<TString> vecToDraw1D = {
60  "deltapt/pt",
61  "pt/gpt",
62  "gphi"
63  };
64  const vector<TString> vecHistNames1D = {
65  "hDeltaPtOverPt",
66  "hFracPt",
67  "hTruthPhi"
68  };
69 
70  // 2d things to draw, histogram names, and options
71  const vector<TString> vecToDraw2D = {
72  "deltapt/pt:pt",
73  "deltapt/pt:pt/gpt",
74  "gphi:pt/gpt"
75  };
76  const vector<TString> vecHistNames2D = {
77  "hDeltaPtOverPtVsPt",
78  "hDeltaPtOverPtVsFracPt",
79  "hTruthPhiVsFracPt"
80  };
81  const vector<TString> vecHistOpts2D = {
82  "colz",
83  "colz",
84  "colz"
85  };
86 
87  // construct list of output histograms
88  const Ssiz_t nToDraw1D = vecToDraw1D.size();
89  const Ssiz_t nToDraw2D = vecToDraw2D.size();
90  const Ssiz_t nCutsToApply = vecCutsToApply.size();
91 
92  TString sHistToDraw1D;
93  TString sHistToDraw2D;
94  vector<TString> vecHistToDraw1D;
95  vector<TString> vecHistToDraw2D;
96  for (Ssiz_t iCutToApply = 0; iCutToApply < nCutsToApply; iCutToApply++) {
97  for (Ssiz_t iToDraw1D = 0; iToDraw1D < nToDraw1D; iToDraw1D++) {
98 
99  // construct 1d name
100  sHistToDraw1D = vecHistNames1D[iToDraw1D].Data();
101  sHistToDraw1D.Append("_");
102  sHistToDraw1D.Append(vecCutLabels[iCutToApply].Data());
103 
104  // add to list
105  vecHistToDraw1D.push_back(sHistToDraw1D.Data());
106  }
107  for (Ssiz_t iToDraw2D = 0; iToDraw2D < nToDraw2D; iToDraw2D++) {
108 
109  // construct 2d name
110  sHistToDraw2D = vecHistNames2D[iToDraw2D].Data();
111  sHistToDraw2D.Append("_");
112  sHistToDraw2D.Append(vecCutLabels[iCutToApply].Data());
113 
114  // add to list
115  vecHistToDraw2D.push_back(sHistToDraw2D.Data());
116  }
117  } // end cut loop
118  cout << " Constructed histogram lists." << endl;
119 
120  // open files
121  TFile *fOutput = new TFile(sOutput.Data(), "recreate");
122  TFile *fInput = new TFile(sInput.Data(), "read");
123  if (!fOutput || !fInput) {
124  cerr << "PANIC: couldn't open a file!\n"
125  << " fOutput = " << fOutput << ", fInput = " << fInput << "\n"
126  << endl;
127  return;
128  }
129  cout << " Opened files." << endl;
130 
131  // grab input tuple
132  TNtuple *ntToDrawFrom = (TNtuple*) fInput -> Get(sInTuple.Data());
133  if (!ntToDrawFrom) {
134  cerr << "PANIC: couldn't grab input tuple!\n" << endl;
135  return;
136  }
137  cout << " Grabbed input tuple.\n"
138  << " Beginning draw loop..."
139  << endl;
140 
141  // draw histograms
142  Ssiz_t iHistToDraw1D = 0;
143  Ssiz_t iHistToDraw2D = 0;
144  TString sDrawArg1D = "";
145  TString sDrawArg2D = "";
146  for (Ssiz_t iCutToApply = 0; iCutToApply < nCutsToApply; iCutToApply++) {
147  for (Ssiz_t iToDraw1D = 0; iToDraw1D < nToDraw1D; iToDraw1D++) {
148 
149  // construct draw arg
150  sDrawArg1D = vecToDraw1D[iToDraw1D].Data();
151  sDrawArg1D.Append(">>");
152  sDrawArg1D.Append(vecHistToDraw1D[iHistToDraw1D].Data());
153  cout << " Drawing '" << sDrawArg1D.Data() << "'..." << endl;
154 
155  // draw to histogram
156  ntToDrawFrom -> Draw(sDrawArg1D.Data(), vecCutsToApply[iCutToApply].Data());
157  ++iHistToDraw1D;
158  }
159  for (Ssiz_t iToDraw2D = 0; iToDraw2D < nToDraw2D; iToDraw2D++) {
160 
161  // construct draw arg
162  sDrawArg2D = vecToDraw2D[iToDraw2D].Data();
163  sDrawArg2D.Append(">>");
164  sDrawArg2D.Append(vecHistToDraw2D[iHistToDraw2D].Data());
165  cout << " Drawing '" << sDrawArg2D.Data() << "'..." << endl;
166 
167  // draw to histogram
168  ntToDrawFrom -> Draw(sDrawArg2D.Data(), vecCutsToApply[iCutToApply].Data());
169  ++iHistToDraw2D;
170  }
171  } // end cut loop
172  cout << " Drew histograms from tuple." << endl;
173 
174  // grab histograms and save
175  const Ssiz_t nHistToDraw1D = vecHistToDraw1D.size();
176  const Ssiz_t nHistToDraw2D = vecHistToDraw2D.size();
177 
178  TH1D *hHistToDraw1D[nHistToDraw1D];
179  TH2D *hHistToDraw2D[nHistToDraw2D];
180  for (Ssiz_t iHist1D = 0; iHist1D < nHistToDraw1D; iHist1D++) {
181 
182  // grab histogram
183  fInput -> cd();
184  hHistToDraw1D[iHist1D] = (TH1D*) gDirectory -> Get(vecHistToDraw1D[iHist1D].Data());
185 
186  // save histogram
187  fOutput -> cd();
188  hHistToDraw1D[iHist1D] -> Write();
189  }
190  for (Ssiz_t iHist2D = 0; iHist2D < nHistToDraw2D; iHist2D++) {
191 
192  // grab histogram
193  fInput -> cd();
194  hHistToDraw2D[iHist2D] = (TH2D*) gDirectory -> Get(vecHistToDraw2D[iHist2D].Data());
195 
196  // save histogram
197  fOutput -> cd();
198  hHistToDraw2D[iHist2D] -> Write();
199  }
200  cout << " Saved histograms." << endl;
201 
202  // close files
203  fOutput -> cd();
204  fOutput -> Close();
205  fInput -> cd();
206  fInput -> Close();
207  cout << " Closed files." << endl;
208 
209  // announce end and exit
210  cout << " Finished quick tuple plotting!\n" << endl;
211  return;
212 
213 }
214 
215 // end ------------------------------------------------------------------------