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