Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QuickWeirdTuplePlotter.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file QuickWeirdTuplePlotter.cxx
1 // ----------------------------------------------------------------------------
2 // 'QuickWeirdTuplePlotter.cxx
3 // Derek Anderson
4 // 10.26.2023
5 //
6 // Use to quick plot some leaves from the weird tracks ntuple.
7 // ----------------------------------------------------------------------------
8 
9 #include <string>
10 #include <vector>
11 #include <utility>
12 #include <iostream>
13 #include <TH2.h>
14 #include <TFile.h>
15 #include <TError.h>
16 #include <TNtuple.h>
17 #include <TDirectory.h>
18 #include <ROOT/RDataFrame.hxx>
19 #include <ROOT/RDF/HistoModels.hxx>
20 
21 // make common namespaces implicit
22 using namespace std;
23 using namespace ROOT;
24 
25 // set up aliases
26 using TH2Def = ROOT::RDF::TH2DModel;
27 
28 
29 
30 // quickly plot variables vs. delta-r froma tuple -----------------------------
31 
33 
34  // lower verbosity
35  gErrorIgnoreLevel = kError;
36  cout << "\n Beginning weird track tuple plotting..." << endl;
37 
38  // options, histogram binning, and histogram definitions --------------------
39 
40  // i/o parameters
41  const string sOutput = "weirdTracksVsDr.pp200py8jet10run8.d27m10y2023.root";
42  const string sInput = "output/condor/final_merge/correlatorJetTree.pp200py8jet10run8_roughCutsWithWeirdTrackTuple.d27m10y2023.root";
43  const string sInTuple = "ntWeirdTracks";
44 
45  // cuts to apply, drawing options & delta-r leaf
46  const string sCut("");
47  const string sDeltaR("deltartrack");
48 
49  // deltra-r binning
50  const size_t nDrBins(75);
51  const vector<double> drBinEdges = {
52  1e-05,
53  1.16591e-05,
54  1.35936e-05,
55  1.58489e-05,
56  1.84785e-05,
57  2.15443e-05,
58  2.51189e-05,
59  2.92864e-05,
60  3.41455e-05,
61  3.98107e-05,
62  4.64159e-05,
63  5.4117e-05,
64  6.30957e-05,
65  7.35642e-05,
66  8.57696e-05,
67  0.0001,
68  0.000116591,
69  0.000135936,
70  0.000158489,
71  0.000184785,
72  0.000215443,
73  0.000251189,
74  0.000292864,
75  0.000341455,
76  0.000398107,
77  0.000464159,
78  0.00054117,
79  0.000630957,
80  0.000735642,
81  0.000857696,
82  0.001,
83  0.00116591,
84  0.00135936,
85  0.00158489,
86  0.00184785,
87  0.00215443,
88  0.00251189,
89  0.00292864,
90  0.00341455,
91  0.00398107,
92  0.00464159,
93  0.0054117,
94  0.00630957,
95  0.00735642,
96  0.00857696,
97  0.01,
98  0.0116591,
99  0.0135936,
100  0.0158489,
101  0.0184785,
102  0.0215443,
103  0.0251189,
104  0.0292864,
105  0.0341455,
106  0.0398107,
107  0.0464159,
108  0.054117,
109  0.0630957,
110  0.0735642,
111  0.0857696,
112  0.1,
113  0.116591,
114  0.135936,
115  0.158489,
116  0.184785,
117  0.215443,
118  0.251189,
119  0.292864,
120  0.341455,
121  0.398107,
122  0.464159,
123  0.54117,
124  0.630957,
125  0.735642,
126  0.857696,
127  1
128  }; // end delta-r bin edges
129 
130  // binning accessor
131  enum Var {VNum, VEne, VEta, VPhi, VDca, VVtx, VQual, VDPt, VFrac};
132 
133  // other variable binning
134  // <0> = no. of bins
135  // <1> = 1st bin lower edge
136  // <2> = last bin upper edge
137  vector<tuple<size_t, double, double>> vecBins = {
138  make_tuple(100, 0., 100.),
139  make_tuple(200, 0., 100.),
140  make_tuple(400, -2., 2.),
141  make_tuple(360, -3.15, 3.15),
142  make_tuple(10000, -10., 10.),
143  make_tuple(10000, -10., 10.),
144  make_tuple(100, 0., 10.),
145  make_tuple(5000, 0., 5.),
146  make_tuple(5000, 0., 5.)
147  };
148 
149  // histogram accessor
150  enum Hist {HPt, HPFrac, HEta, HPhi, HEne, HDcaXY, HDcaZ, HVtxX, HVtxY, HVtxZ, HQual, HDPt, HNLMvtx, HNLIntt, HNLTpc, HNCMvtx, HNCIntt, HNCTpc, HNKey, HNSame, HNFrac};
151 
152  // histogram titles
153  const vector<string> vecTitles = {
154  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};p_{T}^{trk} [GeV]",
155  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};p_{T,i}^{trk} / p_{T,j}^{trk}",
156  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};#eta^{trk}",
157  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};#varphi^{trk}",
158  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};E_{trk}",
159  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};DCA_{xy} [cm]",
160  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};DCZ_{z} [cm]",
161  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};v_{x} [cm]",
162  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};v_{y} [cm]",
163  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};v_{z} [cm]",
164  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};#chi^{2}/ndf",
165  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};#deltap_{T}^{trk}/p_{T}^{trk}",
166  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};N_{layer}^{mvtx}",
167  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};N_{layer}^{intt}",
168  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};N_{layer}^{tpc}",
169  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};N_{clust}^{mvtx}",
170  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};N_{clust}^{intt}",
171  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};N_{clust}^{tpc}",
172  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};N_{clust}^{tot}",
173  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};N_{clust}^{same}",
174  ";#DeltaR = #sqrt{(#Delta#varphi)^{2} + (#Delta#eta)^{2})};N_{clust}^{same} / N_{clust}^{tot}"
175  };
176 
177  // leaves to draw vs. delta-r & histogram definitions
178  // <0> = leaf to draw
179  // <1> = histogram name
180  // <2> = axis titles
181  // <3> = y-axis binning
182  const vector<tuple<string, string, string, tuple<size_t, double, double>>> vecHistDef = {
183  make_tuple("pt_a", "hPtVsDr", vecTitles[Hist::HPt], vecBins[Var::VEne]),
184  make_tuple("ptFrac", "hPtFracVsDr", vecTitles[Hist::HPFrac], vecBins[Var::VFrac]),
185  make_tuple("eta_a", "hEtaVsDr", vecTitles[Hist::HEta], vecBins[Var::VEta]),
186  make_tuple("phi_a", "hPhiVsDr", vecTitles[Hist::HPhi], vecBins[Var::VPhi]),
187  make_tuple("ene_a", "hEneVsDr", vecTitles[Hist::HEne], vecBins[Var::VEne]),
188  make_tuple("dcaxy_a", "hDcaXYVsDr", vecTitles[Hist::HDcaXY], vecBins[Var::VDca]),
189  make_tuple("dcaz_a", "hDcaZVsDr", vecTitles[Hist::HDcaZ], vecBins[Var::VDca]),
190  make_tuple("vtxx_a", "hVtxXVsDr", vecTitles[Hist::HVtxX], vecBins[Var::VVtx]),
191  make_tuple("vtxy_a", "hVtxYVsDr", vecTitles[Hist::HVtxY], vecBins[Var::VVtx]),
192  make_tuple("vtxz_a", "hVtxZVsDr", vecTitles[Hist::HVtxZ], vecBins[Var::VVtx]),
193  make_tuple("quality_a", "hQualityVsDr", vecTitles[Hist::HQual], vecBins[Var::VQual]),
194  make_tuple("deltapt_a", "hDeltaPtVsDr", vecTitles[Hist::HDPt], vecBins[Var::VDPt]),
195  make_tuple("nmvtxlayers_a", "hNMvtxLayerVsDr", vecTitles[Hist::HNLMvtx], vecBins[Var::VNum]),
196  make_tuple("ninttlayers_a", "hNInttLayerVsDr", vecTitles[Hist::HNLIntt], vecBins[Var::VNum]),
197  make_tuple("ntpclayers_a", "hNTpcLayerVsDr", vecTitles[Hist::HNLTpc], vecBins[Var::VNum]),
198  make_tuple("nmvtxclusts_a", "hNMvtxClustVsDr", vecTitles[Hist::HNCMvtx], vecBins[Var::VNum]),
199  make_tuple("ninttclusts_a", "hNInttClustVsDr", vecTitles[Hist::HNCIntt], vecBins[Var::VNum]),
200  make_tuple("ntpcclusts_a", "hNTpcClustVsDr", vecTitles[Hist::HNCTpc], vecBins[Var::VNum]),
201  make_tuple("nclustkey_a", "hNClustKeyVsDr", vecTitles[Hist::HNKey], vecBins[Var::VNum]),
202  make_tuple("nsameclustkey", "hNSameKeyVsDr", vecTitles[Hist::HNSame], vecBins[Var::VNum]),
203  make_tuple("nSameFrac", "hNSameFracVsDr", vecTitles[Hist::HNFrac], vecBins[Var::VFrac])
204  };
205  cout << " Defined output histograms." << endl;
206 
207  // create output histograms & TNtuple arguments
208  // <0> = what to draw on y-axis
209  // <1> = what to draw on x-axis
210  // <2> = pointer to histogram
211  //vector<tuple<string, TH2D*>> vecArgAndHist;
212  vector<tuple<string, string, TH2Def>> vecArgAndHist;
213  for (const auto histDef : vecHistDef) {
214 
215  // make histogram
216  auto bins = get<3>(histDef);
217  TH2Def hist = TH2Def(get<1>(histDef).data(), get<2>(histDef).data(), nDrBins, drBinEdges.data(), get<0>(bins), get<1>(bins), get<2>(bins));
218 
219  // add argument and hist to vector
220  vecArgAndHist.push_back(make_tuple(get<0>(histDef), sDeltaR, hist));
221  }
222  cout << " Created output histograms and TNtuple arguments." << endl;
223 
224  // helper lambdas -----------------------------------------------------------
225 
226  auto getFrac = [](const float a, const float b) {
227  return a / b;
228  };
229 
230  // open files and make frame ------------------------------------------------
231 
232  // open output file
233  TFile* fOutput = new TFile(sOutput.data(), "recreate");
234  if (!fOutput) {
235  cerr << "PANIC: couldn't open a file!\n"
236  << " fOutput = " << fOutput << "\n"
237  << endl;
238  return;
239  }
240  cout << " Opened output file." << endl;
241 
242  // create data frame
243  RDataFrame frame(sInTuple.data(), sInput.data());
244 
245  // make sure file isn't empty
246  auto pairs = frame.Count();
247  if (pairs == 0) {
248  cerr << "Error: No track pairs found!" << endl;
249  return;
250  }
251  cout << " Set up data frame." << endl;
252 
253  // run any analysis and make histograms -------------------------------------
254 
255  // define additional columns
256  auto analysis = frame.Define("ptFrac", getFrac, {"pt_a", "pt_b"})
257  .Define("nSameFrac", getFrac, {"nsameclustkey", "nclustkey_a"});
258  cout << " Defined additional columns.\n"
259  << " Beginning draw loop..."
260  << endl;
261 
262  // draw histograms & save to output
263  for (auto argAndHist : vecArgAndHist) {
264 
265  // draw histogram
266  auto hist = analysis.Histo2D(get<2>(argAndHist), get<1>(argAndHist), get<0>(argAndHist));
267  cout << " Drawing '" << get<0>(argAndHist) << "'..." << endl;
268 
269  // save to output file
270  fOutput -> cd();
271  hist -> Write();
272  }
273  cout << " Finished drawing and saved all histograms." << endl;
274 
275  // close output and exit ----------------------------------------------------
276 
277  // close files
278  fOutput -> cd();
279  fOutput -> Close();
280  cout << " Closed files." << endl;
281 
282  // announce end and exit
283  cout << " Finished weird track tuple plotting!\n" << endl;
284  return;
285 
286 }
287 
288 // end ------------------------------------------------------------------------