Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Corrections.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Corrections.cxx
1 #define SDMULT 1
2 #define CANVASW 800
3 #define CANVASH 700
4 
5 #include <TCanvas.h>
6 #include <TColor.h>
7 #include <TCut.h>
8 #include <TExec.h>
9 #include <TF1.h>
10 #include <TFile.h>
11 #include <TGraphAsymmErrors.h>
12 #include <TH1F.h>
13 #include <TH2F.h>
14 #include <TH3F.h>
15 #include <TLatex.h>
16 #include <TLegend.h>
17 #include <TLine.h>
18 #include <TMath.h>
19 #include <TObjString.h>
20 #include <TROOT.h>
21 #include <TRandom3.h>
22 #include <TStyle.h>
23 #include <TTree.h>
24 #include <TTreeIndex.h>
25 
26 #include <fstream>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include <vector>
31 
32 #include "/sphenix/user/hjheng/TrackletAna/analysis/plot/sPHENIXStyle/sPhenixStyle.C"
33 
34 using namespace std;
35 
36 void calccorr(const TString infilename, int CentLow = -1, int CentHigh = 10, bool applyc = false, bool applyg = false, bool applym = false, TString estag = "", TString putag = "", bool debug = false)
37 {
38  // Options
39  // int layer = TString(argv[1]).Atoi();
40  // int CentLow = TString(argv[2]).Atoi();
41  // int CentHigh = TString(argv[3]).Atoi();
42  // bool applyc = false; // external corrections
43  // bool applyg = false; // geometric correction
44  // bool applym = false; // acceptance map
45  // TString estag = "";
46  // TString putag = "";
47  // bool debug = false; // printout for debugging
49  gStyle->SetOptTitle(0);
50  gStyle->SetOptStat(0);
51  gStyle->SetPalette(kThermometer);
52 
53  system(Form("mkdir -p ./plot/corrections/CentBin%dto%d", CentLow, CentHigh));
54 
55  TFile *finput = new TFile(infilename, "read");
56  TTree *tinput = (TTree *)finput->Get("minitree");
57 
58  TFile *fcorr = 0;
59  TFile *faccep = 0;
60  TFile *fes = 0;
61  TFile *fpu = 0;
62 
63  if (applyc)
64  {
65  fcorr = new TFile(Form("./plot/corrections/Cent%dto%d/correction_hists.root", CentLow, CentHigh));
66  cout << "[INFO] Applying correction factors" << endl;
67 
68  // if (applyg)
69  // {
70  // // faccep = new TFile(Form("output/acceptances/%s/acceptance-%i.root", accepdir, type));
71  // cout << "[INFO] Applying geometric correction" << endl;
72  // }
73  }
74 
75  // if (applym)
76  // {
77  // cout << "[INFO] Applying external acceptance maps" << endl;
78  // }
79 
80  // if (estag.EqualTo(""))
81  // {
82  // // fes = new TFile(Form("output/correction-%s-%i.root", estag, type));
83  // cout << "[INFO] Applying external event selection corrections" << endl;
84  // }
85 
86  // if (putag.EqualTo(""))
87  // {
88  // // fpu = new TFile(Form("output/pileup-%s.root", putag));
89  // cout << "[INFO] Applying pileup corrections" << endl;
90  // }
91 
92  TCut csel;
93  if (CentLow == CentHigh)
94  {
95  csel = Form("Centrality_mbd==%d", CentLow);
96  }
97  else
98  {
99  csel = Form("Centrality_mbd>=%d && Centrality_mbd<=%d", CentLow, CentHigh);
100  }
101  TCut vsel = "PV_z<=0 && PV_z>=-50";
102  TCut osel = "trig";
103  TCut psel = "process==0";
104  TCut esel = vsel && csel && osel;
105  TCut gsel = vsel && csel && psel;
106 
107  TH1::SetDefaultSumw2();
108 
109  /* Setup bins for correction histograms */
110  int neta = 68;
111  float etamin = -3.4;
112  float etamax = 3.4;
113  float etab[neta + 1];
114  for (int i = 0; i <= neta; i++)
115  etab[i] = i * (etamax - etamin) / neta + etamin;
116  int nvz = 50;
117  float vzmin = -45;
118  float vzmax = 5;
119  float vzb[nvz + 1];
120  for (int i = 0; i <= nvz; i++)
121  vzb[i] = i * (vzmax - vzmin) / nvz + vzmin;
122  int nmult = 15; // Tracklet multiplicity
123  float multb[nmult + 1] = {0, 10, 25, 50, 100, 200, 350, 550, 800, 1100, 1500, 2000, 2500, 3100, 3800, 5000};
124  /* Setup histograms */
125  TH3F *h3WEhadron = new TH3F("h3WEhadron", "h3WEhadron", neta, etab, nmult, multb, nvz, vzb); // Gen-hadron - passing event selection
126  TH3F *h3WGhadron = new TH3F("h3WGhadron", "h3WGhadron", neta, etab, nmult, multb, nvz, vzb); // Gen-hadron - passing gen-level selection
127  TH3F *h3WEraw = new TH3F("h3WEraw", "h3WEraw", neta, etab, nmult, multb, nvz, vzb); // raw reco-tracklets, before alpha correction, passing esel (vz cut, trigger)
128  TH3F *h3WEcorr = new TH3F("h3WEcorr", "h3WEcorr", neta, etab, nmult, multb, nvz, vzb); // after alpha correction; corr = raw * alpha
129  /* Acceptance & efficiency correction - alpha */
130  TH3F *h3alpha = new TH3F("h3alpha", "h3alpha", neta, etab, nmult, multb, nvz, vzb); // h3WEhadron (gen-hadron passing evt selection) / h3WEraw
131  TH3F *h3alphafinal = new TH3F("h3alphafinal", "h3alphafinal", neta, etab, nmult, multb, nvz, vzb);
132  /* Vertex distribution: reweighting */
133  TH1F *h1WEvz = new TH1F("h1WEvz", "h1WEvz", nvz, vzb); // v_z distribution passing event selection
134  TH2F *h2WEvzmult = new TH2F("h2WEvzmult", "h2WEvzmult", nvz, vzb, nmult, multb); // v_z v.s tracklet multiplicity distribution passing event selection
135  /* Acceptance region */
136  TH2F *h2amapxev_prealpha; // Before alpha correction is applied, for checking
137  TH2F *h2amapxev = new TH2F("h2amapxev", "h2amapxev", neta, etab, nvz, vzb);
138  TH3F *h3amapxemv = new TH3F("h3amapxemv", "h3amapxemv", neta, etab, nmult, multb, nvz, vzb);
139  /* Trigger efficiency */
140  TH1F *h1teff = new TH1F("h1teff", "h1teff", nmult, multb);
141  TH1F *h1WGOXteff = new TH1F("h1WGOXteff", "h1WGOXteff", nmult, multb); // passing (gsel && osel)
142  TH1F *h1WGXteff = new TH1F("h1WGXteff", "h1WGXteff", nmult, multb); // passing gsel
143  /* Single diffractive event fraction */
144  TH1F *h1sdf = new TH1F("h1sdf", "h1sdf", nmult, multb);
145  TH1F *h1WEsdf = new TH1F("h1WEsdf", "h1WEsdf", nmult, multb);
146  TH1F *h1WENGsdf = new TH1F("h1WENGsdf", "h1WENGsdf", nmult, multb);
147  /* Empty correction */
148  TH1F *h1empty = new TH1F("h1empty", "h1empty", neta, etab);
149  /* Alpha factor in 1D and 2D (for checking) */
150  TH1F *h1alpha[neta][nvz];
151  TF1 *falpha[neta][nvz];
152  TH2F *h2alpha_multb[nmult];
153  for (int i = 0; i < neta; i++)
154  {
155  for (int j = 0; j < nvz; j++)
156  {
157  h1alpha[i][j] = new TH1F(Form("alpha_etabin%i_vzbin%i", i, j), Form("alpha_etabin%i_vzbin%i", i, j), nmult, multb);
158  falpha[i][j] = new TF1(Form("falpha_etabin%i_vzbin%i", i, j), "[0]+[1]/(x+[2])+[3]/(x*x)", 1, 12000);
159  }
160  }
161  for (int k = 0; k < nmult; k++)
162  {
163  h2alpha_multb[k] = new TH2F(Form("alphafinal2D_multbin%d", k), Form("alphafinal2D_multbin%d", k), neta, etab, nvz, vzb);
164  }
165  /*------------------------------------------------------------------------------------------------------*/
166 
167  if (applyc)
168  {
169  delete h2amapxev;
170  delete h1teff;
171  delete h1sdf;
172  delete h1empty;
173 
174  h2amapxev = (TH2F *)fcorr->Get("h2amapxev")->Clone();
175  h1teff = (TH1F *)fcorr->Get("h1teff")->Clone();
176  h1sdf = (TH1F *)fcorr->Get("h1sdf")->Clone();
177  h1empty = (TH1F *)fcorr->Get("h1empty")->Clone();
178 
179  for (int i = 0; i < neta; i++)
180  {
181  for (int j = 0; j < nvz; j++)
182  {
183  delete h1alpha[i][j];
184  delete falpha[i][j];
185 
186  h1alpha[i][j] = (TH1F *)fcorr->Get(Form("alpha_etabin%i_vzbin%i", i, j));
187  falpha[i][j] = (TF1 *)fcorr->Get(Form("falpha_etabin%i_vzbin%i", i, j));
188  }
189  }
190  }
191 
192  /*------------------------------------------------------------------------------------------------------*/
194  TH2F *haccepmc = 0;
195  TH2F *haccepdata = 0;
196  TH2F *hgaccep = 0;
197 
198  if (applyg)
199  {
200  haccepmc = (TH2F *)faccep->Get("hmccoarse");
201  haccepdata = (TH2F *)faccep->Get("hdatacoarse");
202  hgaccep = (TH2F *)haccepmc->Clone("hgaccep");
203  hgaccep->Divide(haccepdata);
204  }
205  /*------------------------------------------------------------------------------------------------------*/
206  /* nevents: for normalization */
207  // Number of events passing event selection and gen-level selection
208  TH1F *h1WEGevent = new TH1F("h1WEGevent", "", nvz, vzb);
209  int nWEGentry = tinput->Draw("PV_z>>h1WEGevent", "pu0_sel" * (esel && gsel), "goff");
210  float nWEGevent = h1WEGevent->Integral(-1, -1);
211  // Number of events passing gen-level selection
212  TH1F *h1WGevent = new TH1F("h1WGevent", "", nvz, vzb);
213  tinput->Draw("PV_z>>h1WGevent", "pu0_sel" * (gsel), "goff");
214  float nWGevent = h1WGevent->Integral(-1, -1);
215 
216  cout << "[INFO] weighted events: " << nWEGevent << ", entries: " << nWEGentry << endl;
217  if (nWEGentry < 1)
218  {
219  cout << "[ERROR] No events selected - stopping" << endl;
220  return;
221  }
222 
223  cout << "[INFO] Setup the acceptance maps" << endl;
224  /* set acceptance maps */
225  tinput->Project("h1WEvz", "PV_z", "pu0_sel" * (esel));
226  tinput->Project("h2WEvzmult", "NRecotkl_Raw:PV_z", "pu0_sel" * (esel));
227 
228  // const int *amap = 0;
229  // if (applym)
230  // {
231  // amap = ext_accep_map(type);
232  // }
233 
234  for (int i = 1; i <= neta; i++)
235  {
236  for (int j = 1; j <= nvz; j++)
237  {
238  // cout << "[check] setup h2amapxev and h3amapxemv: (nvz - j) * neta + i - 1 = " << (nvz - j) * neta + i - 1 << endl;
239  if (!applyc || h2amapxev->GetBinContent(i, j) != 0)
240  {
241  // if (applym && !amap[(nvz - j) * neta + i - 1])
242  // {
243  // h2amapxev->SetBinContent(i, j, 0);
244  // h2amapxev->SetBinError(i, j, 0);
245  // continue;
246  // }
247 
248  h2amapxev->SetBinContent(i, j, h1WEvz->GetBinContent(j));
249  h2amapxev->SetBinError(i, j, 0);
250  for (int k = 1; k <= nmult; k++)
251  h3amapxemv->SetBinContent(i, k, j, h2WEvzmult->GetBinContent(j, k));
252  }
253  }
254  }
255 
256  h2amapxev_prealpha = (TH2F *)h2amapxev->Clone("h2amapxev_prealpha");
257 
258  /* generator-level hadrons */
259  tinput->Project("h3WEhadron", "PV_z:NRecotkl_Raw:GenHadron_eta", "pu0_sel && abs(GenHadron_eta)<4" * (esel));
260  tinput->Project("h3WGhadron", "PV_z:NRecotkl_Raw:GenHadron_eta", "pu0_sel && abs(GenHadron_eta)<4" * (gsel));
261  /* reconstructed tracklets */
262  tinput->Project("h3WEraw", "PV_z:NRecotkl_Raw:recotklraw_eta", "pu0_sel" * (esel));
263 
264  /* calculate alpha corrections */
265  cout << "[INFO] Calculate the alpha correction" << endl;
266  if (!applyc)
267  {
268  for (int x = 1; x <= neta; x++)
269  {
270  for (int z = 1; z <= nvz; z++)
271  {
272  if (h2amapxev->GetBinContent(x, z) == 0)
273  {
274  if (debug)
275  printf(" # acceptance region: eta: %2i, vz: %2i is 0\n", x, z);
276  continue;
277  }
278 
279  int count = 0;
280 
281  for (int y = 1; y <= nmult; y++)
282  {
283  h1alpha[x - 1][z - 1]->SetBinContent(y, 0);
284  h1alpha[x - 1][z - 1]->SetBinError(y, 0);
285 
286  if (h3WEraw->GetBinContent(x, y, z) != 0 && h3WEhadron->GetBinContent(x, y, z) != 0)
287  {
288  double raw = h3WEraw->GetBinContent(x, y, z);
289  double rawerr = h3WEraw->GetBinError(x, y, z);
290  double truth = h3WEhadron->GetBinContent(x, y, z);
291  double trutherr = h3WEhadron->GetBinError(x, y, z);
292 
293  double alpha = truth / raw;
294  double alphaerr = alpha * sqrt(rawerr / raw * rawerr / raw + trutherr / truth * trutherr / truth);
295  if (debug)
296  printf(" ^ alpha calculation: eta: %2i, vz: %2i, ntl: %2i, alpha: %5.3f [%5.3f], alpha/alphaerr: %5.3f, raw/truth: %9.2f/%9.2f\n", x, z, y, alpha, alphaerr, (alpha / alphaerr), raw, truth);
297 
298  // if (alpha > 0 && ((alpha / alphaerr > 5 && alpha < 3) || (alpha < 2)))
299  if (alpha > 0)
300  {
301  h3alpha->SetBinContent(x, y, z, alpha);
302  h3alpha->SetBinError(x, y, z, alphaerr);
303  h1alpha[x - 1][z - 1]->SetBinContent(y, alpha);
304  h1alpha[x - 1][z - 1]->SetBinError(y, alphaerr);
305  }
306  else
307  {
308  if (debug)
309  printf(" ! alpha not used\n");
310  }
311 
312  ++count;
313  }
314  }
315 
316  // if (cmax - cmin > 16 && count < nmult / 2)
317  // if (count < nmult / 2)
318  if (0)
319  {
320  if (debug)
321  printf(" # ! acceptance map: eta: %2i, vz: %2i set to 0 (bad acceptance/statistics)\n", x, z);
322  h2amapxev->SetBinContent(x, z, 0);
323  }
324  }
325  }
326 
327  for (int j = 0; j < nvz; j++)
328  {
329  for (int i = 0; i < neta; i++)
330  {
331  falpha[i][j]->SetParameter(0, 0.84);
332  falpha[i][j]->SetParLimits(1, 1, 256);
333  falpha[i][j]->SetParLimits(2, 0, 512);
334  falpha[i][j]->SetParLimits(3, -64, 128);
335  falpha[i][j]->SetLineWidth(1);
336  falpha[i][j]->SetLineColor(46);
337  h1alpha[i][j]->Fit(falpha[i][j], "M Q", "", 8, 7000);
338  h1alpha[i][j]->Fit(falpha[i][j], "M E Q", "", 8, 7000);
339  falpha[i][j] = h1alpha[i][j]->GetFunction(Form("falpha_%i_%i", i, j));
340  }
341  }
342 
343  /* Trigger & offline selection */
344  cout << "[INFO] Calculate the trigger & offline selection efficiency" << endl;
345  tinput->Project("h1WGXteff", "NRecotkl_Raw", "pu0_sel" * (gsel));
346  tinput->Project("h1WGOXteff", "NRecotkl_Raw", "pu0_sel" * (gsel && osel));
347  h1teff = (TH1F *)h1WGOXteff->Clone("h1teff");
348  h1teff->Divide(h1WGXteff);
349 
350  /* Single diffractive event fraction (complement of gen selection) */
351  cout << "[INFO] Calculate the single diffractive event fraction" << endl;
352  tinput->Project("h1WENGsdf", "NRecotkl_Raw", "pu0_sel" * (esel && !gsel));
353  tinput->Project("h1WEsdf", "NRecotkl_Raw", "pu0_sel" * (esel));
354  h1sdf = (TH1F *)h1WENGsdf->Clone("h1sdf");
355  h1sdf->Divide(h1WEsdf);
356 
357  /* vertexing efficiency */
358  }
359 
360  // /* external single-diffractive fraction */
361  // if (fes)
362  // {
363  // delete h1sdf;
364  // delete h1teff;
365  // delete h1empty;
366  // h1sdf = (TH1F *)fes->Get("h1sdf")->Clone();
367  // h1teff = (TH1F *)fes->Get("h1teff")->Clone();
368  // h1empty = (TH1F *)fes->Get("h1empty")->Clone();
369  // }
370 
371  /* Apply alpha correction */
372  cout << "-------------------------------------------------------------" << endl << "[INFO] Apply the alpha correction" << endl;
373  for (int x = 1; x <= neta; x++)
374  {
375  for (int z = 1; z <= nvz; z++)
376  {
377  if (h2amapxev->GetBinContent(x, z) == 0)
378  continue;
379 
380  for (int y = 1; y <= nmult; y++)
381  {
382  double raw = h3WEraw->GetBinContent(x, y, z);
383  double rawerr = h3WEraw->GetBinError(x, y, z);
384 
385  double alpha = h1alpha[x - 1][z - 1]->GetBinContent(y);
386  double alphaerr = h1alpha[x - 1][z - 1]->GetBinError(y);
387 
388  if (debug)
389  printf(" ^ alpha application: eta: %2i, vz: %2i, ntl: %2i, alpha: %5.3f [%5.3f], raw: %9.2f\n", x, z, y, alpha, alphaerr, raw);
390 
391  if (alpha == 0 && falpha[x - 1][z - 1] != 0)
392  {
393  alpha = falpha[x - 1][z - 1]->Eval((multb[y] + multb[y - 1]) / 2);
394  if (debug)
395  printf(" # alpha from histogram is 0 -> check fit value: %.3f\n", alpha);
396  }
397 
398  if (alpha == 0)
399  {
400  for (int k = 0; k < y; k++)
401  {
402  alpha = h1alpha[x - 1][z - 1]->GetBinContent(y - k);
403  alphaerr = h1alpha[x - 1][z - 1]->GetBinError(y - k);
404  if (debug)
405  printf(" # check other bin: %.3f, bin: %2i\n", alpha, y - k);
406  if (alpha != 0)
407  break;
408  }
409  }
410 
411  // if (alpha <= 0 || alpha > 3.6)
412  // {
413  // if (debug)
414  // printf(" ! invalid value: %.3f, reset to 1\n", alpha);
415  // alpha = 1;
416  // }
417 
418  if (applyg)
419  {
420  double gaccepdata = haccepdata->GetBinContent(x, z);
421  double gaccepmc = haccepmc->GetBinContent(x, z);
422 
423  if (gaccepdata && gaccepmc)
424  {
425  alpha = alpha * gaccepmc / gaccepdata;
426  alphaerr = alphaerr * gaccepmc / gaccepdata;
427  if (debug)
428  printf(" & apply geo accep: %.3f", gaccepmc / gaccepdata);
429  }
430  else
431  {
432  if (debug)
433  printf(" ! geo accep error: eta: %2i, vz: %2i\n", x, z);
434  }
435  }
436 
437  if (debug)
438  printf(" + alpha applied: [ %.3f ]\n", alpha);
439 
440  h3alphafinal->SetBinContent(x, y, z, alpha);
441  h3alphafinal->SetBinError(x, y, z, alphaerr);
442  h2alpha_multb[y - 1]->SetBinContent(x, z, alpha);
443  h2alpha_multb[y - 1]->SetBinError(x, z, alphaerr);
444 
445  double ncorr = raw * alpha;
446  double ncorrerr = rawerr * alpha;
447 
448  h3WEcorr->SetBinContent(x, y, z, ncorr);
449  h3WEcorr->SetBinError(x, y, z, ncorrerr);
450  }
451 
452  bool valid = false;
453  for (int y = 1; y <= nmult; y++)
454  {
455  if (h3alphafinal->GetBinContent(x, y, z) != 1)
456  valid = true;
457  }
458 
459  if (!valid)
460  {
461  if (debug)
462  printf(" # ! acceptance map: eta: %2i, vz: %2i set to 0 (invalid alpha)\n", x, z);
463  h2amapxev->SetBinContent(x, z, 0);
464  }
465  }
466 
467  for (int z = 1; z <= nvz; z++)
468  {
469  if (h2amapxev->GetBinContent(x, z) == 0)
470  {
471  for (int y = 1; y <= nmult; y++)
472  {
473  h3alphafinal->SetBinContent(x, y, z, 0);
474  h3alphafinal->SetBinError(x, y, z, 0);
475  h2alpha_multb[y - 1]->SetBinContent(x, z, 0);
476  h2alpha_multb[y - 1]->SetBinError(x, z, 0);
477 
478  h3WEcorr->SetBinContent(x, y, z, 0);
479  h3WEcorr->SetBinError(x, y, z, 0);
480  }
481 
482  if (faccep)
483  hgaccep->SetBinContent(x, z, 0);
484  }
485  }
486  }
487 
488  /* 2d corrected, raw tracklets - project to eta and v_z*/
489  TH2D *h2WEcorr = (TH2D *)h3WEcorr->Project3D("zx");
490  h2WEcorr->SetName("h2WEcorr");
491  TH2D *h2WEraw = (TH2D *)h3WEraw->Project3D("zx");
492  h2WEraw->SetName("h2WEraw");
493 
494  /* draw geometric acceptance */
495  // if (faccep)
496  // {
497  // TCanvas *cga = new TCanvas("cga", "", CANVASW, CANVASH);
498  // hgaccep->Draw("colz");
499  // cga->SaveAs(Form("./plot/corrections/layer%d_CentBin%dto%d/ga.png", layer, CentLow + 1, CentHigh - 1));
500  // }
501 
502  /* project 1d acceptance */
503  TH1F *h1accep2xe = (TH1F *)h2amapxev->ProjectionX();
504  h1accep2xe->SetName("h1accep2xe");
505  h1accep2xe->Scale(1. / h1accep2xe->GetMaximum());
506 
507  /* reconstructed tracklets */
508  TH1F *h1WEraw = (TH1F *)h3WEraw->Project3D("x");
509  h1WEraw->SetName("h1WEraw");
510  h1WEraw->Scale(1. / nWEGevent, "width");
511  h1WEraw->Divide(h1accep2xe);
512 
513  TH1F *h1WEcorr = (TH1F *)h3WEcorr->Project3D("x");
514  h1WEcorr->SetName("h1WEcorr");
515  h1WEcorr->Scale(1. / nWEGevent, "width");
516  h1WEcorr->Divide(h1accep2xe);
517 
518  /* generator-level hadrons */
519  TH1F *h1WGhadron = (TH1F *)h3WGhadron->Project3D("x");
520  h1WGhadron->SetName("h1WGhadron");
521  h1WGhadron->Scale(1. / nWGevent, "width");
522 
523  /*------------------------------------------------------------------------------------------------------*/
524  /* calculate final results */
525  cout << "[INFO] Calculate final results with trigger and SDF corrections" << endl;
526  TH2F *h2WEtcorr = new TH2F("h2WEtcorr", "", neta, etab, nmult, multb);
527 
528  for (int x = 1; x <= neta; x++)
529  {
530  for (int y = 1; y <= nmult; y++)
531  {
532  double sum = 0, sumerr = 0;
533  double mcsum = 0, mcsumerr = 0;
534 
535  for (int z = 1; z <= nvz; z++)
536  {
537  if (h2amapxev->GetBinContent(x, z) != 0)
538  {
539  sum += h3WEcorr->GetBinContent(x, y, z);
540  double err = h3WEcorr->GetBinError(x, y, z);
541  sumerr = sqrt(sumerr * sumerr + err * err);
542  }
543  }
544 
545  h2WEtcorr->SetBinContent(x, y, sum);
546  h2WEtcorr->SetBinError(x, y, sumerr);
547  }
548  }
549 
550  /* Apply trigger, sd fraction corrections */
551  for (int y = 1; y <= nmult; y++)
552  {
553  double sdfrac = h1sdf->GetBinContent(y);
554  double trigeff = h1teff->GetBinContent(y);
555 
556  double totalc = (1 - sdfrac * SDMULT) / trigeff;
557 
558  for (int x = 1; x <= neta; x++)
559  {
560  if (trigeff != 0)
561  {
562  h2WEtcorr->SetBinContent(x, y, h2WEtcorr->GetBinContent(x, y) * totalc);
563  h2WEtcorr->SetBinError(x, y, h2WEtcorr->GetBinError(x, y) * totalc);
564  }
565 
566  for (int z = 1; z <= nvz; z++)
567  {
568  if (h2amapxev->GetBinContent(x, z) != 0)
569  {
570  if (trigeff != 0)
571  h3amapxemv->SetBinContent(x, y, z, h3amapxemv->GetBinContent(x, y, z) * totalc);
572  }
573  else
574  {
575  h3amapxemv->SetBinContent(x, y, z, 0);
576  }
577  }
578  }
579  }
580 
581  /* project 2d acceptances */
582  TH1F *h1accep3xe = (TH1F *)h3amapxemv->Project3D("x");
583  h1accep3xe->SetName("h1accep3xe");
584 
585  TH1F *h1WEtcorr = (TH1F *)h2WEtcorr->ProjectionX();
586  h1WEtcorr->SetName("h1WEtcorr");
587  h1WEtcorr->Scale(1., "width");
588  h1WEtcorr->Divide(h1accep3xe);
589 
590  /* Calculate/apply empty correction */
591  cout << "[INFO] Calculate and apply the empty correction" << endl;
592  if (!applyc && !fes)
593  {
594  h1empty = (TH1F *)h1WGhadron->Clone("h1empty");
595  h1empty->Divide(h1WEtcorr);
596  }
597 
598  TH1F *h1WEprefinal = (TH1F *)h1WEtcorr->Clone("h1WEprefinal");
599  h1WEprefinal->Multiply(h1empty);
600 
601  TH1F *h1WEfinal = (TH1F *)h1WEprefinal->Clone("h1WEfinal");
602  if (fpu)
603  {
604  TH1F *h1pu = (TH1F *)fpu->Get("h1pu")->Clone("h1pu");
605  h1WEfinal->Multiply(h1pu);
606  }
607 
608  /*------------------------------------------------------------------------------------------------------*/
609  /* Make checking plots */
610  cout << "[INFO] Making checking plots: intermediate steps" << endl;
611  TGraphAsymmErrors *trigeff = new TGraphAsymmErrors(h1WGXteff, h1WGOXteff);
612  TGraphAsymmErrors *sdfrac = new TGraphAsymmErrors(h1WENGsdf, h1WEsdf);
613  int nw = 3, nh = 3;
614  TCanvas *ccheck = new TCanvas("ccheck", "ccheck", nw * 400, nh * 400);
615  ccheck->Divide(nw, nh);
616  TLatex *t = new TLatex();
617  t->SetTextAlign(23);
618  /* Vertex distribution (passing esel)*/
619  ccheck->cd(1);
620  h1WEvz->GetXaxis()->SetTitle("v_{z} (cm)");
621  h1WEvz->GetYaxis()->SetTitle("Entries");
622  h1WEvz->GetYaxis()->SetRangeUser(0, h1WEvz->GetMaximum() * 1.2);
623  h1WEvz->SetLineColor(1);
624  h1WEvz->Draw("histtext");
625  t->DrawLatexNDC(0.5, 1.0, h1WEvz->GetName());
626  /* Vertex distribution (passing esel) in 2D (eta,v_z), before applying alpha */
627  ccheck->cd(2);
628  gPad->SetRightMargin(0.13);
629  h2amapxev_prealpha->GetXaxis()->SetTitle("#eta");
630  h2amapxev_prealpha->GetYaxis()->SetTitle("v_{z} (cm)");
631  h2amapxev_prealpha->Draw("colztext45");
632  t->DrawLatexNDC(0.5, 1.0, h2amapxev_prealpha->GetName());
633  /* vz v.s tracklet multiplicity distribution passing esel; for 3D acceptance map */
634  ccheck->cd(3);
635  gPad->SetLogy();
636  gPad->SetRightMargin(0.13);
637  gPad->SetLeftMargin(0.13);
638  h2WEvzmult->GetYaxis()->SetMoreLogLabels();
639  h2WEvzmult->GetXaxis()->SetTitle("v_{z} (cm)");
640  h2WEvzmult->GetYaxis()->SetTitle("Tracklet multiplicity");
641  h2WEvzmult->GetYaxis()->SetTitleOffset(2.1);
642  h2WEvzmult->Draw("colztext");
643  t->DrawLatexNDC(0.5, 1.0, h2WEvzmult->GetName());
644  /* Trigger efficiency */
645  ccheck->cd(4);
646  gPad->SetLogx();
647  gPad->SetLeftMargin(0.12);
648  trigeff->GetXaxis()->SetMoreLogLabels();
649  trigeff->GetXaxis()->SetTitle("Tracklet multiplicity");
650  trigeff->GetYaxis()->SetTitle("Efficiency");
651  trigeff->GetXaxis()->SetTitleOffset(1.4);
652  trigeff->SetMarkerStyle(20);
653  trigeff->SetMarkerColor(1);
654  trigeff->SetLineColor(1);
655  trigeff->Draw("ALPE");
656  t->DrawLatexNDC(0.5, 1.0, "Trigger efficiency");
657  /* Single-diffractive fraction */
658  ccheck->cd(5);
659  gPad->SetLogx();
660  gPad->SetLeftMargin(0.12);
661  sdfrac->GetXaxis()->SetMoreLogLabels();
662  sdfrac->GetXaxis()->SetTitle("Tracklet multiplicity");
663  sdfrac->GetYaxis()->SetTitle("SD event fraction");
664  sdfrac->GetXaxis()->SetTitleOffset(1.4);
665  sdfrac->SetMarkerStyle(20);
666  sdfrac->SetMarkerColor(1);
667  sdfrac->SetLineColor(1);
668  sdfrac->Draw("ALPE");
669  t->DrawLatexNDC(0.5, 1.0, "SD event fraction");
670  /* Acceptance, after applying alpha */
671  ccheck->cd(6);
672  gPad->SetRightMargin(0.13);
673  h2amapxev->GetXaxis()->SetTitle("#eta");
674  h2amapxev->GetYaxis()->SetTitle("v_{z} (cm)");
675  h2amapxev->Draw("colztext45");
676  t->DrawLatexNDC(0.5, 1.0, Form("%s_postalpha", h2amapxev->GetName()));
677  /* Project the 2D acceptance to 1D*/
678  ccheck->cd(7);
679  // gStyle->SetPaintTextFormat("1.3f");
680  h1accep2xe->GetXaxis()->SetTitle("#eta");
681  h1accep2xe->GetYaxis()->SetTitle("A.U");
682  h1accep2xe->GetYaxis()->SetRangeUser(0, h1accep2xe->GetMaximum() * 1.2);
683  h1accep2xe->SetLineColor(1);
684  h1accep2xe->Draw("histtext");
685  t->DrawLatexNDC(0.5, 1.0, h1accep2xe->GetName());
686  /* Reco-tracklet after alpha, trigger, and SDF correction in (eta, tracklet multiplicity) */
687  ccheck->cd(8);
688  gPad->SetLogy();
689  gPad->SetRightMargin(0.13);
690  gPad->SetLeftMargin(0.13);
691  h2WEtcorr->GetYaxis()->SetMoreLogLabels();
692  h2WEtcorr->GetXaxis()->SetTitle("#eta");
693  h2WEtcorr->GetYaxis()->SetTitle("Tracklet multiplicity");
694  h2WEtcorr->GetYaxis()->SetTitleOffset(2.1);
695  h2WEtcorr->Draw("colz");
696  t->DrawLatexNDC(0.5, 1.0, h2WEtcorr->GetName());
697  /* Empty event correction */
698  ccheck->cd(9);
699  h1empty->GetXaxis()->SetTitle("#eta");
700  h1empty->GetYaxis()->SetTitle("Ratio");
701  h1empty->GetYaxis()->SetRangeUser(h1empty->GetMinimum(0) * 0.7, h1empty->GetMaximum() * 1.2);
702  h1empty->SetLineColor(1);
703  h1empty->Draw("histtext");
704  t->DrawLatexNDC(0.5, 1.0, h1empty->GetName());
705  for (int i = 0; i < nw * nw; i++)
706  {
707  ccheck->cd(i + 1);
708  gPad->SetTickx();
709  gPad->SetTicky();
710  }
711  ccheck->SaveAs(Form("./plot/corrections/CentBin%dto%d/Check_Intermediate.pdf", CentLow, CentHigh));
712 
713  /* Draw 1D alpha and fits */
714  cout << "[INFO] Making checking plots: 1D alpha and fits" << endl;
715  TLatex *t1 = new TLatex();
716  t1->SetTextAlign(23);
717  TCanvas *cfalphavz = new TCanvas("cfalphavz", "", 2000, 1600);
718  cfalphavz->Divide(5, 4);
719 
720  for (int x = 1; x <= neta; x++)
721  {
722  cfalphavz->Clear("d");
723  for (int z = 1; z <= nvz; z++)
724  {
725  cfalphavz->cd(z);
726  gPad->SetLogx();
727  gPad->SetTickx();
728  gPad->SetTicky();
729  h1alpha[x - 1][z - 1]->GetYaxis()->SetRangeUser(0, h1alpha[x - 1][z - 1]->GetMaximum() * 2.5);
730  h1alpha[x - 1][z - 1]->SetMarkerStyle(20);
731  h1alpha[x - 1][z - 1]->SetMarkerSize(1);
732  h1alpha[x - 1][z - 1]->SetMarkerColor(1);
733  h1alpha[x - 1][z - 1]->SetLineColor(1);
734  h1alpha[x - 1][z - 1]->Draw();
735 
736  TLine *l = new TLine(multb[0], 1, multb[nmult], 1);
737  l->SetLineColorAlpha(38, 0.7);
738  l->Draw("same");
739 
740  t1->DrawLatexNDC(0.5, 1.0, Form("%.1f < #eta < %.1f, %.1f < v_{z} < %.1f", etab[x - 1], etab[x], vzb[z - 1], vzb[z]));
741  }
742 
743  cfalphavz->SaveAs(Form("./plot/corrections/CentBin%dto%d/alpha1Dfit-etabin%i.pdf", CentLow, CentHigh, x - 1));
744  }
745 
746  TCanvas *cfalphaeta = new TCanvas("cfalphaeta", "", 2400, 2400);
747  cfalphaeta->Divide(6, 6);
748 
749  for (int z = 1; z <= nvz; z++)
750  {
751  cfalphaeta->Clear("d");
752  for (int x = 1; x <= neta; x++)
753  {
754  cfalphaeta->cd(x);
755  gPad->SetLogx();
756  gPad->SetTickx();
757  gPad->SetTicky();
758  h1alpha[x - 1][z - 1]->SetMarkerStyle(20);
759  h1alpha[x - 1][z - 1]->SetMarkerSize(1);
760  h1alpha[x - 1][z - 1]->SetMarkerColor(1);
761  h1alpha[x - 1][z - 1]->SetLineColor(1);
762  h1alpha[x - 1][z - 1]->Draw();
763 
764  TLine *l = new TLine(multb[0], 1, multb[nmult], 1);
765  l->SetLineColorAlpha(38, 0.7);
766  l->Draw("same");
767 
768  t1->DrawLatexNDC(0.5, 1.0, Form("%.1f < #eta < %.1f, %.1f < v_{z} < %.1f", etab[x - 1], etab[x], vzb[z - 1], vzb[z]));
769  }
770 
771  cfalphaeta->SaveAs(Form("./plot/corrections/CentBin%dto%d/alpha1Dfit-vzbin%i.pdf", CentLow, CentHigh, z));
772  }
773 
774  /* Draw 2D alpha (eta, vz), inclusive tracklet multiplicity */
775  cout << "[INFO] Making checking plots: 2D alpha (eta, vz), inclusive tracklet multiplicity" << endl;
776  TCanvas *calpha = new TCanvas("calpha", "calpha", CANVASW, CANVASH);
777  gPad->SetRightMargin(0.18);
778  gPad->SetLeftMargin(0.12);
779  calpha->cd();
780  TH2D *h2alphafinal = (TH2D *)h2WEcorr->Clone("h2alphafinal");
781  h2alphafinal->Divide(h2WEraw);
782  h2alphafinal->GetXaxis()->SetTitle("#eta");
783  h2alphafinal->GetYaxis()->SetTitle("v_{z} (cm)");
784  h2alphafinal->GetZaxis()->SetTitle("#alpha (#eta, v_{z})");
785  h2alphafinal->GetZaxis()->SetTitleOffset(1.2);
786  h2alphafinal->GetZaxis()->SetRangeUser(0, 3);
787  gStyle->SetPaintTextFormat("1.3f");
788  h2alphafinal->SetMarkerSize(0.6);
789  h2alphafinal->SetContour(1000);
790  h2alphafinal->Draw("colz");
791  calpha->SaveAs(Form("./plot/corrections/CentBin%dto%d/alpha2D_inclTklMult.pdf", CentLow, CentHigh));
792 
793  for (int k = 0; k < nmult; k++)
794  {
795  calpha->Clear();
796  calpha->cd();
797  h2alpha_multb[k]->GetXaxis()->SetTitle("#eta");
798  h2alpha_multb[k]->GetYaxis()->SetTitle("v_{z} (cm)");
799  h2alpha_multb[k]->GetYaxis()->SetTitleOffset(1.1);
800  h2alpha_multb[k]->GetZaxis()->SetTitle("#alpha (#eta, v_{z})");
801  h2alpha_multb[k]->GetZaxis()->SetTitleOffset(1.2);
802  // h2alpha_multb[k]->GetZaxis()->SetRangeUser(h2alphafinal->GetMinimum(0), h2alphafinal->GetMaximum());
803  h2alpha_multb[k]->GetZaxis()->SetRangeUser(0, 3);
804  gStyle->SetPaintTextFormat("1.3f");
805  h2alpha_multb[k]->SetMarkerSize(0.6);
806  h2alpha_multb[k]->SetContour(1000);
807  h2alpha_multb[k]->Draw("colz");
808  calpha->SaveAs(Form("./plot/corrections/CentBin%dto%d/alpha2D_TklMultb%d.pdf", CentLow, CentHigh, k));
809  }
810 
811  /* Analysis stages - Closure test*/
812  vector<TH1F *> vechist = {h1WGhadron, h1WEraw, h1WEcorr, h1WEtcorr, h1WEfinal};
813  vector<const char *> vcolor{"#20262E", "#1C82AD", "#1F8A70", "#FC7300", "#EFA3C8"};
814  vector<int> vlwidth = {3, 3, 3, 2, 2};
815  vector<int> vstyle = {1, 1, 1, 2, 2};
816  TCanvas *cstage = new TCanvas("cstage", "cstage", CANVASW, CANVASH);
817  gPad->SetRightMargin(0.05);
818  gPad->SetTopMargin(0.05);
819  gPad->SetTickx();
820  gPad->SetTicky();
821  // cstage->SetLogy();
822  TH1F *hframe = new TH1F("hframe", "", 1, etamin, etamax);
823  float ymax = -1;
824  for (size_t i = 0; i < vechist.size(); i++)
825  {
826  if (vechist[i]->GetMaximum() > ymax)
827  ymax = vechist[i]->GetMaximum();
828  }
829  hframe->GetYaxis()->SetRangeUser(0, ymax * 1.6);
830  // hframe->GetYaxis()->SetRangeUser(0.1, ymax * 150);
831  hframe->GetXaxis()->SetTitle("#eta");
832  hframe->GetYaxis()->SetTitle("dN/d#eta");
833  hframe->Draw();
834  for (size_t i = 0; i < vechist.size(); i++)
835  {
836  vechist[i]->SetLineColor(TColor::GetColor(vcolor[i]));
837  vechist[i]->SetLineWidth(vlwidth[i]);
838  vechist[i]->SetLineStyle(vstyle[i]);
839  vechist[i]->Draw("hist same");
840  }
841 
842  TLegend *l1 = new TLegend(0.18, 0.68, 0.5, 0.92);
843  l1->SetHeader(Form("Closure test: Centrality %d to %d", CentLow, CentHigh, "l"));
844  l1->AddEntry(h1WGhadron, "Truth", "l");
845  l1->AddEntry(h1WEraw, "Reco-tracklets before corrections", "l");
846  l1->AddEntry(h1WEcorr, "Corrected for #alpha", "l");
847  l1->AddEntry(h1WEtcorr, "Corrected for #alpha, trigger & sdf", "l");
848  l1->AddEntry(h1WEfinal, "Corrected for #alpha, trigger & sdf, and empty event (final)", "l");
849  l1->SetTextSize(0.033);
850  l1->SetFillStyle(0);
851  l1->SetBorderSize(0);
852  l1->Draw();
853  cstage->Draw();
854  cstage->SaveAs(Form("./plot/corrections/CentBin%dto%d/Closure.pdf", CentLow, CentHigh));
855 
856  /* Write histograms to file*/
857  TFile *outf = new TFile(Form("./plot/corrections/CentBin%dto%d/correction_hists.root", CentLow, CentHigh), "recreate");
858  outf->cd();
859  h2amapxev->Write();
860  h1WGhadron->Write();
861  h1WEraw->Write();
862  h1WEcorr->Write();
863  h1WEtcorr->Write();
864  h1WEfinal->Write(); // Final result, reco-tracklet after alpha, trg&sdf, empty event corrections
865  trigeff->Write();
866  sdfrac->Write();
867  h1teff->Write();
868  h1WGOXteff->Write();
869  h1WGXteff->Write();
870  h1sdf->Write();
871  h1WEsdf->Write();
872  h1WENGsdf->Write();
873  h1empty->Write();
874  for (int i = 0; i < neta; i++)
875  {
876  for (int j = 0; j < nvz; j++)
877  {
878  if (falpha[i][j])
879  falpha[i][j]->Write();
880  if (h1alpha[i][j])
881  h1alpha[i][j]->Write();
882  }
883  }
884 
885  for (int k = 0; k < nmult; k++)
886  h2alpha_multb[k]->Write();
887 
888  outf->Write("", TObject::kOverwrite);
889  outf->Close();
890 }
891 
892 int main(int argc, char *argv[])
893 {
894  // Usage: ./result 12 -1 20
895  // calccorr(int layer = 12, int CentLow = -1, int CentHigh = 20, bool applyc = false, bool applyg = false, bool applym = false, TString estag = "", TString putag = "", bool debug = false)
896 
897  gStyle->SetPaintTextFormat();
898  TString input = "/sphenix/user/hjheng/TrackletAna/minitree/INTT/TrackletMinitree_ana398_zvtx-20cm_dummyAlignParams/sim/TrackletAna_minitree_merged.root";
899  calccorr(input, 5, 95, false, false, false, "", "", false);
900  for (int i = 0; i < 10; i++)
901  {
902  gStyle->SetPaintTextFormat();
903  int cent = 5 * (2 * i + 1);
904  calccorr(input, cent, cent, false, false, false, "", "", false);
905  }
906 
907  return 0;
908 }