Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CheckDeltaPtWithBoundaryMasks.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CheckDeltaPtWithBoundaryMasks.cxx
1 // ----------------------------------------------------------------------------
2 // 'CheckDeltaPtWithBoundaryMasks.cxx
3 // Derek Anderson
4 // 10.30.2023
5 //
6 // Use to quickly check what the delta-pt/pt distribution
7 // looks like if we mask the TPC sector boundaries.
8 // ----------------------------------------------------------------------------
9 
10 // standard c libraries
11 #include <array>
12 #include <string>
13 #include <vector>
14 #include <utility>
15 #include <iostream>
16 // ROOT libraries
17 #include <TH1.h>
18 #include <TH2.h>
19 #include <TF1.h>
20 #include <TFile.h>
21 #include <TError.h>
22 #include <TNtuple.h>
23 #include <TDirectory.h>
24 #include <ROOT/RDataFrame.hxx>
25 #include <ROOT/RDF/HistoModels.hxx>
26 
27 // make common namespaces implicit
28 using namespace std;
29 using namespace ROOT;
30 
31 // set up RDF aliases
32 using TH1Mod = ROOT::RDF::TH1DModel;
33 using TH2Mod = ROOT::RDF::TH2DModel;
34 using RH1Ptr = ROOT::RDF::RResultPtr<::TH1D>;
35 using RH2Ptr = ROOT::RDF::RResultPtr<::TH2D>;
36 
37 // set up tuple aliases
38 using THBins = tuple<size_t, double, double>;
39 using TH1Def = tuple<string, string, THBins>;
40 using TH2Def = tuple<string, string, THBins, THBins>;
41 using Leaves = pair<string, string>;
42 
43 // global constants
44 static const size_t NSectors = 12;
45 
46 
47 
48 // check delta-pt/pt after masking sector boundaries --------------------------
49 
51 
52  // lower verbosity
53  gErrorIgnoreLevel = kError;
54  cout << "\n Beginning sector boundary-masking check..." << endl;
55 
56  // options ------------------------------------------------------------------
57 
58  // i/o parameters
59  const string sOutput = "test.root";
60  const string sInput = "../TruthMatching/input/merged/sPhenixG4_oneMatchPerParticle_oldEvaluator.pt020num5evt500pim.d19m10y2023.root";
61  const string sInTuple = "ntp_track";
62 
63  // mask parameters
64  const double maskSize = 0.02;
65 
66  // fit guesses
67  const array<float, NSectors> arrMeanGuess = {
68  -2.877,
69  -2.354,
70  -1.831,
71  -1.308,
72  -0.785,
73  -0.262,
74  0.262,
75  0.785,
76  1.308,
77  1.831,
78  2.354,
79  2.877
80  };
81  const float sigmaGuess = maskSize;
82  const float ampGuess = 10.;
83  const float fitRange = 2. * maskSize;
84 
85  // histogram definitions ----------------------------------------------------
86 
87  // binning accessor
88  enum Var {VEne, VPhi, VDPt, VFrac};
89 
90  // other variable binning
91  // <0> = no. of bins
92  // <1> = 1st bin lower edge
93  // <2> = last bin upper edge
94  const vector<THBins> vecBins = {
95  make_tuple(200, 0., 100.),
96  make_tuple(360, -3.15, 3.15),
97  make_tuple(5000, 0., 5.),
98  make_tuple(5000, 0., 5.)
99  };
100 
101  // histogram accessors
102  enum Cut {Before, Left, Cut};
103  enum Hist {HPtReco, HPtTrue, HPtFrac, HPhi, HDPt, HDPtVsPhi};
104 
105  // before vs. after labels
106  const vector<string> vecCutLabels = {
107  "_beforeMask",
108  "_afterMask_leftIn",
109  "_afterMask_cutOut"
110  };
111 
112  // axis titles
113  const vector<string> vecAxisTitles = {
114  ";p_{T}^{reco} [GeV/c];counts",
115  ";p_{T}^{true} [GeV/c];counts",
116  ";p_{T}^{reco}/p_{T}^{true};counts",
117  ";#varphi^{trk};counts",
118  ";#deltap_{T}^{reco}/p_{T}^{reco}",
119  ";#varphi^{trk};#deltap_{T}^{reco}/p_{T}^{reco}"
120  };
121 
122  // leaves to draw
123  const vector<Leaves> vecLeaves = {
124  make_pair("pt", ""),
125  make_pair("gpt", ""),
126  make_pair("ptFrac", ""),
127  make_pair("phi", ""),
128  make_pair("ptErr", ""),
129  make_pair("phi", "ptErr")
130  };
131 
132  // 1d histogram definitions
133  // first = leaves to draw
134  // second = hist definition
135  // <0> = histogram name
136  // <1> = axis titles
137  // <2> = x-axis binning
138  const vector<pair<Leaves, TH1Def>> vecHistDef1D = {
139  make_pair(vecLeaves[Hist::HPtReco], make_tuple("hPtReco", vecAxisTitles[Hist::HPtReco].data(), vecBins[Var::VEne])),
140  make_pair(vecLeaves[Hist::HPtTrue], make_tuple("hPtTrue", vecAxisTitles[Hist::HPtTrue].data(), vecBins[Var::VEne])),
141  make_pair(vecLeaves[Hist::HPtFrac], make_tuple("hPtFrac", vecAxisTitles[Hist::HPtFrac].data(), vecBins[Var::VFrac])),
142  make_pair(vecLeaves[Hist::HPhi], make_tuple("hPhi", vecAxisTitles[Hist::HPhi].data(), vecBins[Var::VPhi])),
143  make_pair(vecLeaves[Hist::HDPt], make_tuple("hDeltaPt", vecAxisTitles[Hist::HDPt].data(), vecBins[Var::VDPt]))
144  };
145 
146  // 2d histogram definitions
147  // first = leaves to draw
148  // second = hist definition
149  // <0> = histogram name
150  // <1> = axis titles
151  // <2> = x-axis binning
152  // <3> = y-axis binning
153  const vector<pair<Leaves, TH2Def>> vecHistDef2D = {
154  make_pair(vecLeaves[Hist::HDPtVsPhi], make_tuple("hDPtVsPhi", vecAxisTitles[Hist::HDPtVsPhi].data(), vecBins[Var::VPhi], vecBins[Var::VDPt]))
155  };
156  cout << " Defined histograms." << endl;
157 
158  // create histograms
159  const size_t nCuts = vecCutLabels.size();
160 
161  // for storing vectors
162  vector<pair<Leaves, TH1Mod>> vecArgAndHistRow1D;
163  vector<pair<Leaves, TH2Mod>> vecArgAndHistRow2D;
164  vector<vector<pair<Leaves, TH1Mod>>> vecArgAndHist1D(nCuts);
165  vector<vector<pair<Leaves, TH2Mod>>> vecArgAndHist2D(nCuts);
166 
167  // instantiate histograms & load into vectors
168  for (const string cut : vecCutLabels) {
169 
170  // make 1d hists
171  vecArgAndHistRow1D.clear();
172  for (const auto histDef1D : vecHistDef1D) {
173 
174  // make name
175  string name = get<0>(histDef1D.second);
176  name += cut;
177 
178  // instantiate hist
179  THBins bins = get<2>(histDef1D.second);
180  TH1Mod hist = TH1Mod(name.data(), get<1>(histDef1D.second).data(), get<0>(bins), get<1>(bins), get <2>(bins));
181  vecArgAndHistRow1D.push_back(make_pair(histDef1D.first, hist));
182  }
183  vecArgAndHist1D.push_back(vecArgAndHistRow1D);
184 
185  // make 2d hists
186  vecArgAndHistRow2D.clear();
187  for (const auto histDef2D : vecHistDef2D) {
188 
189  // make name
190  string name = get<0>(histDef2D.second);
191  name += cut;
192 
193  // instantiate hist
194  THBins xBins = get<2>(histDef2D.second);
195  THBins yBins = get<3>(histDef2D.second);
196  TH2Mod hist = TH2Mod(name.data(), get<1>(histDef2D.second).data(), get<0>(xBins), get<1>(xBins), get <2>(xBins), get<0>(yBins), get<1>(yBins), get<2>(yBins));
197  vecArgAndHistRow2D.push_back(make_pair(histDef2D.first, hist));
198  }
199  vecArgAndHist2D.push_back(vecArgAndHistRow2D);
200  } // end cut loop
201  cout << " Created histograms." << endl;
202 
203  // open files and make frames -----------------------------------------------
204 
205  // open output file
206  TFile* fOutput = new TFile(sOutput.data(), "recreate");
207  if (!fOutput) {
208  cerr << "PANIC: couldn't open a file!\n"
209  << " fOutput = " << fOutput << "\n"
210  << endl;
211  return;
212  }
213  cout << " Opened output file." << endl;
214 
215  // create data frame
216  RDataFrame frame(sInTuple.data(), sInput.data());
217 
218  // make sure file isn't empty
219  auto tracks = frame.Count();
220  if (tracks == 0) {
221  cerr << "Error: No tracks found!" << endl;
222  return;
223  }
224  cout << " Set up data frame." << endl;
225 
226  // analysis lambdas ---------------------------------------------------------
227 
228  auto getFrac = [](const float a, const float b) {
229  return a / b;
230  }; // end 'getFrac(float, float)'
231 
232  auto isInMask = [](const float phi, const double mask, const auto& sectors) {
233  bool inMask = false;
234  for (const float sector : sectors) {
235  if ((phi > (sector - (mask / 2.))) && (phi < (sector + (mask / 2.)))) {
236  inMask = true;
237  break;
238  }
239  }
240  return inMask;
241  }; // end 'isInMask(float)'
242 
243  // run analyses -------------------------------------------------------------
244 
245  // get histograms before masking
246  auto before = frame.Define("ptFrac", getFrac, {"pt", "gpt"})
247  .Define("ptErr", getFrac, {"deltapt", "pt"});
248 
249  // get initial 1d histograms
250  vector<vector<RH1Ptr>> vecHistResult1D(nCuts);
251  vector<vector<RH2Ptr>> vecHistResult2D(nCuts);
252  for (const auto argAndHistBefore1D : vecArgAndHist1D[Cut::Before]) {
253  auto hist1D = before.Histo1D(argAndHistBefore1D.second, argAndHistBefore1D.first.first.data());
254  vecHistResult1D[Cut::Before].push_back(hist1D);
255  }
256  for (const auto argAndHistBefore2D : vecArgAndHist2D[Cut::Before]) {
257  auto hist2D = before.Histo2D(argAndHistBefore2D.second, argAndHistBefore2D.first.first.data(), argAndHistBefore2D.first.second.data());
258  vecHistResult2D[Cut::Before].push_back(hist2D);
259  }
260 
261  // retrieve results
262  vector<vector<TH1D*>> vecOutHist1D(nCuts);
263  vector<vector<TH2D*>> vecOutHist2D(nCuts);
264  for (auto histResultBefore1D : vecHistResult1D[Cut::Before]) {
265  TH1D* hist1D = (TH1D*) histResultBefore1D -> Clone();
266  vecOutHist1D[Cut::Before].push_back(hist1D);
267  }
268  for (auto histResultBefore2D : vecHistResult2D[Cut::Before]) {
269  TH2D* hist2D = (TH2D*) histResultBefore2D -> Clone();
270  vecOutHist2D[Cut::Before].push_back(hist2D);
271  }
272 
273  /* TODO
274  * - get sector bourndaries with fits
275  */
276 
277  // for masking sector boundaries
278  array<float, NSectors> arrSectors;
279  arrSectors.fill(0.);
280 
281  // save & close -------------------------------------------------------------
282 
283  // save histograms
284  fOutput -> cd();
285  for (auto histRow1D : vecOutHist1D) {
286  for (auto hist1D : histRow1D) {
287  hist1D -> Write();
288  }
289  }
290  for (auto histRow2D : vecOutHist2D) {
291  for (auto hist2D : histRow2D) {
292  hist2D -> Write();
293  }
294  }
295  cout << " Saved histograms." << endl;
296 
297  // close files
298  fOutput -> cd();
299  fOutput -> Close();
300  cout << " Finished sector boundary-masking check!\n" << endl;
301 
302 }
303 
304 // end ------------------------------------------------------------------------