Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
numericInverse.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file numericInverse.C
1 #include "sPhenixStyle.h"
2 #include "sPhenixStyle.C"
3 
4 const int nIso = 1;
5 const int nIter = 6;
6 
7 const float isoParams[] = {0, 1, 1.5, 2, 2.5};
8 
9 string generateFit(int order, string function);
10 
11 void numericInverse(int save = 0, int isUESubtracted = 1, int is10GeV = 0)
12 {
14 
15  TGraphErrors *lin[nIso];
16 
17  TFile *fin[nIso];
18  TCanvas *c1 = new TCanvas("linearity","linearity");
19  TCanvas *cUnity = new TCanvas("UnityDiff","UnityDiff");
20  TCanvas *cDiff = new TCanvas("fitDiff","FitDiff");
21  float fitStart = 15;
22  float fitEnd;
23  if(!is10GeV) fitEnd = 60;
24  else fitEnd = 45;
25  TLegend *leg = new TLegend(0.6,0.2,0.9,0.5);
26  leg -> SetFillStyle(0);
27  leg -> SetBorderSize(0);
28  //draw and fit our jet energy linearity
29  int colors[] = {1, 2, 4, kGreen +2, kViolet, kCyan, kOrange, kMagenta+2, kAzure-2};
30  TF1 *linFit[nIso];
31  TH2F *pt_true_matched[nIso];
32  TF1 *unity = new TF1("unity","x",0,80);
33  unity -> SetLineColor(1);
34  unity -> SetLineStyle(8);
35  TGraphErrors *unityDiff[nIso];
36  TGraphErrors *chi2NDF[nIso];
37  TGraphErrors *fitDiff[nIso][3];
38  for(int i = 0; i < nIso; i++)
39  {
40 
41  linFit[i] = new TF1(Form("linFit_iso%d",i),"[0] + [1]*x",fitStart,fitEnd);
42  linFit[i] -> SetLineColor(colors[i]);
43  fin[i] = new TFile(Form("hists_R04_dR%g_Corr0_isLin1.root",isoParams[i]));
44  pt_true_matched[i] = (TH2F*)fin[i] -> Get("h_pt_true_matched");
45 
46  lin[i] = (TGraphErrors*)fin[i] -> Get("g_jes_cent0");
47  lin[i] -> SetMarkerColor(colors[i]);
48  lin[i] -> GetYaxis() -> SetRangeUser(0,70);
49  lin[i] -> GetXaxis() -> SetLimits(0,80);
50  lin[i] -> SetTitle(";p_{T,truth} [GeV/c];#LTp_{T,reco}#GT [GeV/c]");
51  c1 -> cd();
52  if(i == 0) lin[i] -> Draw("ap");
53  else lin[i] -> Draw("samep");
54  unityDiff[i] = new TGraphErrors();
55  for(int j = 0; j < lin[i] -> GetN(); j++)
56  {
57  Double_t x, y;
58  lin[i] -> GetPoint(j,x,y);
59 
60  unityDiff[i] -> SetPoint(j,x,y-x);
61  }
62  lin[i] -> Fit(linFit[i],"RQ");
63  fitDiff[i][0] = new TGraphErrors();
64  for(int n = 0; n < lin[i] -> GetN(); n++)
65  {
66  Double_t x,y;
67  lin[i] -> GetPoint(n,x,y);
68 
69  if(x < fitEnd)
70  {
71  fitDiff[i][0] -> SetPoint(n, x, (y - linFit[i] -> Eval(x))/y);
72  }
73  }
74  fitDiff[i][0] -> SetTitle(";p_{T,truth} [GeV/c];#frac{(Data - Fit)}{Data}");
75  fitDiff[i][0] -> SetMarkerColor(colors[i]);
76  fitDiff[i][0] -> GetYaxis() -> SetRangeUser(-1,1);
77  cUnity -> cd();
78  unityDiff[i] -> SetTitle(";p_{T,truth} [GeV/c];#LTp_{T,reco}#GT - p_{T,Truth} [GeV/c]");
79  unityDiff[i] -> GetYaxis() -> SetRangeUser(-30,5);
80  unityDiff[i] -> SetMarkerColor(colors[i]);
81  if(i == 0)unityDiff[i] -> Draw("ap");
82  else unityDiff[i] -> Draw("samep");
83  leg -> AddEntry(lin[i],Form("#DeltaR = %gR",isoParams[i]),"p");
84 
85  cDiff -> cd();
86  if(i == 0) fitDiff[i][0] -> Draw("ap");
87  else fitDiff[i][0] -> Draw("samep");
88 
89  }
90  c1 -> cd(); leg -> Draw("same"); unity -> Draw("same");
91  cUnity -> cd(); leg -> Draw("same");
92  cDiff -> cd(); leg -> Draw("same");
93  if(save)
94  {
95  c1 -> SaveAs("plots/lin_isoStack.pdf");
96  cUnity -> SaveAs("plots/unityDiff.pdf");
97  cDiff -> SaveAs("plots/fitDiff0.pdf");
98  }
99 
100  //for the numerical inversion, first we must compute
101  //the function R, which is the avaerage JES
102 
103  TGraphErrors *r_lin[nIso];
104  const int nPoints = lin[0] -> GetN();
105  TF1 *rFit[nIso][nIter];
106  TCanvas *cChi2 = new TCanvas("chi2","resolution");
107  TCanvas *c2 = new TCanvas("resolution","resolution");
108  int bestIter[nIso] = {-1};
109  TLine *bestIterLn[nIso];
110 
111  for(int i = 0; i < nIso; i++)
112  {
113  r_lin[i] = new TGraphErrors();
114  c2 -> cd();
115 
116  for(int j = 0; j < nPoints; j++)
117  {
118  Double_t x, y, erry, errx;
119  lin[i] -> GetPoint(j, x, y);
120  erry = lin[i] -> GetErrorY(j);
121  errx = lin[i] -> GetErrorX(j);
122  TH1F *pt_true_proj = (TH1F*)pt_true_matched[i] -> ProjectionX();
123  pt_true_proj -> GetXaxis() -> SetRangeUser(x - errx, x + errx);
124  x = pt_true_proj -> GetMean();
125 
126  r_lin[i] -> SetPoint(j, x, y/x);
127  float err = y/x * sqrt(pow(erry/y,2) /*+ pow((errx/2)/x,2)*/);
128  r_lin[i] -> SetPointError(j, errx, err);
129 
130  }
131  r_lin[i] -> SetMarkerColor(colors[i]);
132  r_lin[i] -> SetTitle(";p_{T,truth} [GeV/c];#LTp_{T,reco}#GT/#LTp_{T,truth}#GT");
133  r_lin[i] -> GetYaxis() -> SetRangeUser(0.5,1);
134 
135  string fit = "[0]";
136  chi2NDF[i] = new TGraphErrors();
137  chi2NDF[i] -> SetTitle(";Iterations;chi2/NDF");
138  chi2NDF[i] -> SetMarkerColor(colors[i]);
139  float minChi2NDF = 9999;
140  //Double_t xstart, ystart;
141  //r_lin[i] -> GetPoint(1,xstart,ystart);
142  for(int l = 0; l < nIter; l++)
143  {
144  rFit[i][l] = new TF1("polyFitBase",fit.c_str(),fitStart,fitEnd);
145  rFit[i][l] -> SetLineColor(colors[i]);
146 
147  r_lin[i] -> Fit(rFit[i][l],"Q0","0",fitStart,fitEnd);
148  r_lin[i] -> GetYaxis() -> SetRangeUser(0.5,1);
149  float chi2, ndf;
150  chi2 = rFit[i][l] -> GetChisquare();
151  ndf = rFit[i][l] -> GetNDF();
152  chi2NDF[i] -> SetPoint(l,l,chi2/ndf);
153  if(chi2/ndf < minChi2NDF)
154  {
155  minChi2NDF = chi2/ndf;
156  bestIter[i] = l;
157  }
158 
159 
160  fit = generateFit(l+1,fit);
161  }
162 
163  fitDiff[i][1] = new TGraphErrors();
164  for(int n = 0; n < r_lin[i] -> GetN(); n++)
165  {
166  Double_t x,y;
167  r_lin[i] -> GetPoint(n,x,y);
168  if(x < fitEnd)
169  {
170  fitDiff[i][1] -> SetPoint(n, x, (y - rFit[i][bestIter[i]] -> Eval(x))/y);
171  }
172  }
173  fitDiff[i][1] -> SetMarkerColor(colors[i]);
174  fitDiff[i][1] -> SetTitle(";p_{T,truth} [GeV/c];#frac{(Data - Fit)}{Data}");
175  fitDiff[i][1] -> GetYaxis() -> SetRangeUser(-1,1);
176 
177  bestIterLn[i] = new TLine(bestIter[i],0,bestIter[i],20);
178  bestIterLn[i] -> SetLineColor(colors[i]);
179  bestIterLn[i] -> SetLineStyle(8);
180 
181 
182  c2 -> cd();
183  if(i == 0)
184  {
185  r_lin[i] -> GetXaxis() -> SetLimits(0,80);
186  r_lin[i] -> Draw("ap");
187  }
188  else r_lin[i] -> Draw("samep");
189  rFit[i][bestIter[i]] -> Draw("same");
190 
191  cChi2 -> cd();
192  if(i == 0)
193  {
194  chi2NDF[i] -> GetYaxis() -> SetRangeUser(0,20);
195  chi2NDF[i] -> Draw("ap");
196 
197  }
198  else chi2NDF[i] -> Draw("samep");
199  bestIterLn[i] -> Draw("same");
200 
201  cDiff -> cd();
202  if(i == 0)fitDiff[i][1] -> Draw("ap");
203  else fitDiff[i][1] -> Draw("samep");
204  }
205  TLine *thirty = new TLine(30,0.5,30,1);
206  thirty -> SetLineStyle(8);
207  thirty -> SetLineColor(1);
208  c2 -> cd();
209  thirty -> Draw("same");
210  leg -> Draw("same");
211  cChi2 -> cd();
212  //gPad -> SetLogy();
213  leg -> Draw("same");
214  cDiff -> cd(); leg -> Draw("same");
215 
216  if(save)
217  {
218  c2 -> SaveAs("plots/fMeanScaled.pdf");
219  cChi2 -> SaveAs("plots/chi2NdffMean.pdf");
220  cDiff -> SaveAs("plots/fitDiff1.pdf");
221  }
222  //Now, in order to get the calibration factor, we have to compute R^-1
223  //Which is defined by R^-1(y) = R(f^-1(y))
224 
225  TGraphErrors *rTilde[nIso];
226  TGraphErrors *gfInv[nIso];
227  TF1 *correctionFit[nIso][nIter];
228  TCanvas *c3 = new TCanvas();
229  TCanvas *c4 = new TCanvas();
230  TCanvas *cChi22 = new TCanvas();
231 
232  for(int i = 0; i < nIso; i++)
233  {
234  rTilde[i] = new TGraphErrors();
235  gfInv[i] = new TGraphErrors();
236  //if(!isUESubtracted)correctionFit[i] = new TF1(Form("corrFit_Iso%g",isoParams[i]),"[0] + [1]*x");
237  //else{ correctionFit[i] = new TF1(Form("corrFit_Iso%g",isoParams[i]),"[0]*exp([1]*x) + [2]");
238  for(int j = 0; j < nPoints; j++)
239  {
240  Double_t x, y;
241  lin[i] -> GetPoint(j, x, y);
242  if(x < fitStart || x > fitEnd) continue;
243  float fInv = linFit[i] -> GetX(y);
244  gfInv[i] -> SetPoint(j, y, fInv);
245  //gfInv[i] -> SetPointError(j, lin
246  float R = rFit[i][bestIter[i]] -> Eval(fInv);
247  //if(y <= fitStart)continue;
248  rTilde[i] -> SetPoint(j, y, 1/R);
249  }
250  rTilde[i] -> SetMarkerColor(colors[i]);
251  gfInv[i] -> SetMarkerColor(colors[i]);
252 
253  rTilde[i] -> SetTitle(";#LTp_{T,reco}#GT [GeV/c];1/#tilde{R}(p_{T,reco})");
254  gfInv[i] -> SetTitle(";#LTp_{T,reco}#GT [GeV/c];p_{T,truth} [GeV/c]");
255 
256  rTilde[i] -> GetYaxis() -> SetRangeUser(0,2);
257  string fit = "[0]";
258  chi2NDF[i] = new TGraphErrors();
259  chi2NDF[i] -> SetTitle(";Iterations;chi2/NDF");
260  chi2NDF[i] -> SetMarkerColor(colors[i]);
261  float minChi2NDF = 9999;
262  for(int l = 0; l < nIter; l++)
263  {
264  correctionFit[i][l] = new TF1("polyFitBas2",fit.c_str(),fitStart,52);
265 
266  rTilde[i] -> Fit(correctionFit[i][l],"Q0","",5,70);
267  float chi2, ndf;
268  chi2 = correctionFit[i][l] -> GetChisquare();
269  ndf = correctionFit[i][l] -> GetNDF();
270  chi2NDF[i] -> SetPoint(l,l,chi2/ndf);
271  if(chi2/ndf < minChi2NDF)
272  {
273  minChi2NDF = chi2/ndf;
274  bestIter[i] = l;
275  }
276 
277  fit = generateFit(l+1,fit);
278  }
279 
280  correctionFit[i][bestIter[i]] -> SetLineColor(colors[i]);
281  fitDiff[i][2] = new TGraphErrors();
282  for(int n = 0; n < rTilde[i] -> GetN(); n++)
283  {
284  Double_t x,y;
285  rTilde[i] -> GetPoint(n,x,y);
286  if(x < fitEnd)
287  {
288  fitDiff[i][2] -> SetPoint(n, x, (y - correctionFit[i][bestIter[i]] -> Eval(x))/y);
289  }
290  }
291 
292  fitDiff[i][2] -> SetTitle(";p_{T,reco} [GeV/c];#frac{(Data - Fit)}{Data}");
293  fitDiff[i][2] -> SetMarkerColor(colors[i]);
294  fitDiff[i][2] -> GetYaxis() -> SetRangeUser(-1,1);
295 
296  c3 -> cd();
297  if(i == 0)rTilde[i] -> Draw("ap");
298  else rTilde[i] -> Draw("samep");
299  correctionFit[i][bestIter[i]] -> Draw("same");
300 
301  c4 -> cd();
302  if(i == 0)gfInv[i] -> Draw("ap");
303  else gfInv[i] -> Draw("samep");
304 
305  bestIterLn[i] = new TLine(bestIter[i],-0.005,bestIter[i],0.03);
306  bestIterLn[i] -> SetLineColor(colors[i]);
307  bestIterLn[i] -> SetLineStyle(8);
308 
309  cChi22 -> cd();
310  if(i == 0)
311  {
312  chi2NDF[i] -> GetYaxis() -> SetRangeUser(-0.005,0.03);
313  chi2NDF[i] -> GetXaxis() -> SetLimits(-1,14.5);
314 
315  chi2NDF[i] -> Draw("ap");
316  }
317  else chi2NDF[i] -> Draw("samep");
318  bestIterLn[i] -> Draw("same");
319 
320  cDiff -> cd();
321  if(i == 0)fitDiff[i][2] -> Draw("ap");
322  else fitDiff[i][2] -> Draw("samep");
323  }
324  c3 -> cd();
325  leg -> Draw("same");
326  c4 -> cd();
327  leg -> Draw("same");
328  cDiff -> cd();
329  leg -> Draw("same");
330  if(save)
331  {
332  c3 -> SaveAs("plots/jesCorrection.pdf");
333  c4 -> SaveAs("plots/jesInv.pdf");
334  cChi22 -> SaveAs("plots/chi2NdfCorrFit.pdf");
335  cDiff -> SaveAs("plots/fitDiff2.pdf");
336 
337  TFile *corrOut = new TFile("JES_IsoCorr_NumInv.root","RECREATE");
338  corrOut -> cd();
339  for(int i = 0; i < nIso; i++)
340  {
341  correctionFit[i][bestIter[i]] -> SetName(Form("corrFit_Iso%g",isoParams[i]));
342  correctionFit[i][bestIter[i]] -> Write();
343  }
344  corrOut -> Close();
345  }
346 }
347 
348 string generateFit(int order, string function)
349 {
350  string nextTerm = Form("+ [%d]*pow(log(x),%d)", order, order);
351 
352  function += nextTerm;
353 
354  //TF1 *fit = new TF1("polyLog",function.c_str(),11,60);
355 
356  return function;
357 }