Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Jet_reso_Iso.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Jet_reso_Iso.C
1 #include "sPhenixStyle.h"
2 #include "sPhenixStyle.C"
3 
4 bool isIso(int thisRjet, int thisTjet, vector<float> *rJetpT, vector<float> *tJetpT, vector<float> *rJetEta, vector<float> *tJetEta, vector<float> *rJetPhi, vector<float> *tJetPhi, float minDr, TH1F *&rDist, TH1F *&tDist);
5 // This macro analyzes output from the jet validation sub module of the
6 // JS-Jet module. To first order, it simply computes the jet energy scale (JES)
7 // and jet energy resolution (JER), but also compute the jet energy linearity
8 // necessary for the numerical inversion which is used to calibrate the MC
9 // to itself.
10 
11 void Jet_reso_Iso(float jetR = 4, float isoParam = 1, int applyCorr = 0, int isLin = 0)
12 {
13  //jetR: jet radius
14  //
15  //isoParameter: used to compute distance required to consider a jet "isolated"
16  //and is multipled by the jet radius. See minDist variable below.
17  //
18  //applyCorr: determines whether or not to apply calibrations.
19  //
20  //isLin: measure the jet linearity instead of the response
21  float minDist = .1*(jetR*isoParam);
22  if(minDist)std::cout << "isolation requirement: dR > " << minDist << " [rad]" << std::endl;
23 
25  TH1::SetDefaultSumw2();
26  TH2::SetDefaultSumw2();
27  TH3::SetDefaultSumw2();
28 
29  TChain * ct = new TChain("T");
30  //ct->Add("run40_30GeV_10GeV_Spliced_RecoJets_test.root");
31  ct->Add("run40_30GeV_10GeV_Spliced_RecoJets.root");
32  //if you want to combine multiple files use this
33  //for(int i = 0; i < 15000; i++){
34  // ct->Add(Form("../macro/condor/output/jetIso_%d/isoJetCalibOut_%d.root",i,i));
35  // }
36 
37  TH1F *recoDist = new TH1F("recoDist","distance between reco jets",200,0,5);
38  TH1F *truthDist = new TH1F("truthDist","distance between truth jets",200,0,5);
39 
40  TFile *f_out = new TFile(Form("hists_R0%g_dR%g_Corr%d_isLin%d.root",jetR,isoParam,applyCorr,isLin),"RECREATE");
41 
42  vector<float> *eta = 0;
43  vector<float> *phi = 0;
44  vector<float> *pt = 0;
45  //vector<float> *e = 0;
46  //vector<float> *subtracted_et = 0;
47  vector<float> *truthEta = 0;
48  vector<float> *truthPhi = 0;
49  vector<float> *truthPt = 0;
50  //vector<float> *truthE = 0;
51 
52  vector<float> *subseedEta = 0;
53  vector<float> *subseedPhi = 0;
54  vector<float> *subseedPt = 0;
55  vector<float> *subseedE = 0;
56  vector<int> *subseedCut = 0;
57  vector<float> *rawseedEta = 0;
58  vector<float> *rawseedPhi = 0;
59  vector<float> *rawseedPt = 0;
60  //vector<float> *rawseedE = 0;
61  //vector<int> *rawseedCut = 0;
62  float *totalCalo = 0;
63  int cent;
64  Float_t rWeight, tWeight, RawWeight, SubWeight;
65 
66 
67  ct->SetBranchAddress("eta",&eta);
68  ct->SetBranchAddress("phi",&phi);
69  ct->SetBranchAddress("pt",&pt);
70  //ct->SetBranchAddress("e",&e);
71  //ct->SetBranchAddress("subtracted_et",&subtracted_et);
72  ct->SetBranchAddress("truthEta",&truthEta);
73  ct->SetBranchAddress("truthPhi",&truthPhi);
74  ct->SetBranchAddress("truthPt",&truthPt);
75  //ct->SetBranchAddress("truthE",&truthE);
76  //ct->SetBranchAddress("cent", &cent);
77 
78  ct->SetBranchAddress("rawseedEta", &rawseedEta);
79  ct->SetBranchAddress("rawseedPhi", &rawseedPhi);
80  ct->SetBranchAddress("rawseedPt", &rawseedPt);
81  //ct->SetBranchAddress("rawseedE", &rawseedE);
82  //ct->SetBranchAddress("rawseedCut", &rawseedCut);
83  ct->SetBranchAddress("subseedEta", &subseedEta);
84  ct->SetBranchAddress("subseedPhi", &subseedPhi);
85  ct->SetBranchAddress("subseedPt", &subseedPt);
86  //ct->SetBranchAddress("subseedE", &subseedE);
87  //ct->SetBranchAddress("subseedCut", &subseedCut);
88 
89  ct->SetBranchAddress("tWeight",&tWeight);
90  ct->SetBranchAddress("rWeight",&rWeight);
91  ct->SetBranchAddress("RawWeight",&RawWeight);
92  ct->SetBranchAddress("SubWeight",&SubWeight);
93 
94  //const Float_t pt_range[] = {10,15,20,25,30,35,40,45,50,60,80};
95  const Float_t pt_range[] = {15,20,25,30,35,40,45,50,60,80};
96 
97  //if(applyCorr) pt_range[] = {10,15,20,25,30,35,40,45,50,60,80};
98  //if(isLin)const Float_t pt_range[] = {10,15,20,25,30,35,40,45,50,55,60,65,70,75,80};
99 
100  const int pt_N = sizeof(pt_range)/sizeof(float) - 1;
101  //const int pt_N_truth = 34;//for including the 10-15 region
102  const int pt_N_truth = 37;
103  const int resp_N = 100;
104  Float_t resp_bins[resp_N];
105  for(int i = 0; i < resp_N + 1; i++){
106  //resp_bins[i] = 1.2/resp_N * i;
107  resp_bins[i] = 2./resp_N * i;
108  }
109 
110 
111  // Float_t pt_range_reco[pt_N_reco];
112  //const int pt_N_reco = 71;
113  // for(int i = 0; i < pt_N_reco + 1; i++)
114  // {
115  // pt_range_reco[i] = 80./pt_N_reco * i;
116  // }
117  vector<Float_t> pt_range_reco1;
118  Float_t ptBin = 0.;
119  // while(ptBin < 80)
120  // {
121  // pt_range_reco1.push_back(ptBin);
122  // if(ptBin <= 20)ptBin += 0.1;
123  // else if(ptBin >20 && ptBin <= 30) ptBin += 1;
124  // else if(ptBin > 30 && ptBin < 40) ptBin +=0.1;
125  // else ptBin += 2.5;
126  // }
127  while(ptBin < 79.9)
128  {
129  pt_range_reco1.push_back(ptBin);
130  ptBin += 0.1;
131  }
132 
133  const int pt_N_reco = pt_range_reco1.size();//sizeof(pt_range_reco1)/sizeof(float) - 1;
134  Float_t pt_range_reco[pt_N_reco];
135  for(int i = 0; i < pt_N_reco+1; i++)
136  {
137  if(i < pt_N_reco)pt_range_reco[i] = pt_range_reco1.at(i);
138  else pt_range_reco[i] = 79.9;
139  }
140  Float_t pt_range_truth[pt_N_truth];
141  Float_t rebin[pt_N_truth];
142  for(int i = 0; i < pt_N_truth+1; i++)
143  {
144  //pt_range_truth[i] = 10+70./pt_N_truth * i;
145  //pt_range_truth[i] = 10+2*i;
146  pt_range_truth[i] = 15+1.5*i;
147  //if( pt_range_truth[i] < 15) rebin[i] = 1;
148  if(pt_range_truth[i] >= 15 && pt_range_truth[i] < 30) rebin[i] = 4;//4;
149  else if(pt_range_truth[i] >= 30 && pt_range_truth[i] < 40) rebin[i] = 1;
150  else rebin[i] = 10;
151  }
152 
153 
154  const int eta_N = 40;
155  Float_t eta_bins[eta_N];
156  for(int i = 0; i < eta_N+1; i++){
157  eta_bins[i] = -1.1 + 2.2/eta_N * i;
158  }
159  const int phi_N = 40;
160  Float_t phi_bins[phi_N];
161  for(int i = 0; i < phi_N+1; i++){
162  phi_bins[i] = -TMath::Pi() + 2*TMath::Pi()/phi_N * i;
163  }
164  const int subet_N = 400;
165  Float_t subet_bins[subet_N];
166  for(int i = 0; i < subet_N+1; i++){
167  subet_bins[i] = i*0.2;
168  }
169  const float cent_bins[] = {-1, 0, 10, 30, 50, 80}; //the first bin is for inclusive in centrality/pp events
170  const int cent_N = 1;//sizeof(cent_bins)/sizeof(float) - 1;
171 
172  TH3F *h_MC_Reso_pt = 0;
173  if(isLin) h_MC_Reso_pt= new TH3F("h_MC_Reso", "" , pt_N_truth, pt_range_truth, pt_N_reco, pt_range_reco, cent_N, cent_bins);
174  else h_MC_Reso_pt= new TH3F("h_MC_Reso", "" , pt_N, pt_range, resp_N, resp_bins, cent_N, cent_bins);
175 
176  h_MC_Reso_pt -> GetXaxis()->SetTitle("p_{T}^{truth} [GeV]");
177  if(isLin) h_MC_Reso_pt->GetYaxis()->SetTitle("p_{T}^{reco}");
178  else h_MC_Reso_pt->GetYaxis()->SetTitle("p_{T}^{reco}/p_{T}^{truth}");
179  TH2F *h_pt_reco = new TH2F("h_pt_reco","",subet_N,subet_bins, cent_N, cent_bins);
180  h_pt_reco->GetXaxis()->SetTitle("p_{T} [GeV]");
181  TH2F *h_pt_true = new TH2F("h_pt_true","",pt_N,pt_range, cent_N, cent_bins);
182  h_pt_true->GetXaxis()->SetTitle("p_{T} [GeV]");
183  TH2F *h_pt_true_matched = new TH2F("h_pt_true_matched","",subet_N,subet_bins, cent_N, cent_bins);
184  h_pt_true_matched->GetXaxis()->SetTitle("p_{T} [GeV]");
185  TH3F *h_eta_phi_reco = new TH3F("h_eta_phi_reco","",eta_N, eta_bins, phi_N, phi_bins, cent_N, cent_bins);
186  h_eta_phi_reco->GetXaxis()->SetTitle("#eta");
187  h_eta_phi_reco->GetYaxis()->SetTitle("#phi");
188  TH3F *h_eta_phi_true = new TH3F("h_eta_phi_true","",eta_N, eta_bins, phi_N, phi_bins, cent_N, cent_bins);
189  TH3F *h_subEt_pt = new TH3F("h_subEt_pt","",pt_N,pt_range, subet_N, subet_bins, cent_N, cent_bins);
190 
191 
192  TGraphErrors *g_jes[cent_N];
193  TGraphErrors *g_jer[cent_N];
194  for(int icent = 0; icent < cent_N; icent++){
195  g_jes[icent] = new TGraphErrors(pt_N);
196  g_jer[icent] = new TGraphErrors(pt_N);
197  }
198 
199  TFile *corrFile;
200  TF1 *correction = new TF1("jet energy correction","1",0,80);
201  if(applyCorr)
202  {
203  corrFile = new TFile("JES_IsoCorr_NumInv.root","READ");
204  corrFile -> cd();
205  correction = (TF1*)corrFile -> Get(Form("corrFit_Iso%g",isoParam));
206  }
207 
208  int nentries = ct->GetEntries();
209  for(int i = 0; i < nentries; i++){
210  ct->GetEntry(i);
211  cent = 101;
212  int njets = truthPt->size();
213  int nrecojets = pt->size();
214  if(applyCorr)
215  {
216  for(int rj = 0; rj < nrecojets; rj++)
217  {
218  pt -> at(rj) *= correction -> Eval(pt -> at(rj));//correct all the jets in one go
219  }
220  }
221  for(int tj = 0; tj < njets; tj++){
222  //fill truth hists
223  //if(truthPt -> at(tj) < 10) continue;
224  h_pt_true->Fill(truthPt->at(tj), cent);
225  h_pt_true->Fill(truthPt->at(tj), -1);
226  h_eta_phi_true->Fill(truthEta->at(tj),truthPhi->at(tj), cent);
227  h_eta_phi_true->Fill(truthEta->at(tj),truthPhi->at(tj), -1);
228  //do reco to truth jet matching
229  float matchEta, matchPhi, matchPt, matchE, matchsubtracted_et, dR;
230  float dRMax = 100;
231  int recoMatchJet = -9999;
232 
233  for(int rj = 0; rj < nrecojets; rj++){
234  float dEta = truthEta->at(tj) - eta->at(rj);
235  float dPhi = truthPhi->at(tj) - phi->at(rj);
236  while(dPhi > TMath::Pi()) dPhi -= 2*TMath::Pi();
237  while(dPhi < -TMath::Pi()) dPhi += 2*TMath::Pi();
238  dR = TMath::Sqrt(dEta*dEta + dPhi*dPhi);
239 
240  if(dR < dRMax){
241  matchEta = eta->at(rj);
242  matchPhi = phi->at(rj);
243  matchPt = pt->at(rj);
244  //matchE = e->at(rj);
245  //matchsubtracted_et = subtracted_et->at(rj);
246  dRMax = dR;
247  recoMatchJet = rj;
248  }
249  }
250 
251  if(dRMax > 0.1*0.75*jetR) continue;
252  if(minDist && !isIso(recoMatchJet, tj, pt, truthPt, eta, truthEta, phi, truthPhi, minDist, recoDist, truthDist)) continue;
253  //if(truthPt->at(tj) >= 30 && rWeight == 1) continue;
254  if(!isLin)
255  {//if we're applying the correction, we're looking at the JES, fill with ptReco/ptTruth
256 
257  h_MC_Reso_pt->Fill(truthPt->at(tj),matchPt/truthPt->at(tj), cent, rWeight);
258  h_MC_Reso_pt->Fill(truthPt->at(tj),matchPt/truthPt->at(tj), -1, rWeight);
259  }
260  else
261  {//We're looking at the linearity, fill with just the matchp
262  h_MC_Reso_pt->Fill(truthPt->at(tj),matchPt, cent, rWeight);
263  h_MC_Reso_pt->Fill(truthPt->at(tj),matchPt, -1, rWeight);
264  }
265 
266  h_pt_true_matched->Fill(truthPt->at(tj), cent, tWeight);
267  h_eta_phi_reco->Fill(matchEta,matchPhi, cent);
268  h_pt_reco->Fill(matchPt, cent, rWeight);
269 
270  h_pt_true_matched->Fill(truthPt->at(tj), -1, tWeight);
271  h_eta_phi_reco->Fill(matchEta,matchPhi, -1);
272  h_pt_reco->Fill(matchPt, -1, rWeight);
273  //h_subEt_pt->Fill(matchPt, matchsubtracted_et, cent);
274  //h_subEt_pt->Fill(matchPt, matchsubtracted_et, -1);
275  }
276  }
277 
278  TCanvas *c = new TCanvas("c","c");
279  //c->Print("fits04.pdf("); //uncomment these to get a pdf with all the fits
280  TLegend *leg = new TLegend(.6,.9,.9,.9);
281  leg->SetFillStyle(0);
282  gStyle->SetOptFit(0);
283  f_out -> cd();
284  int stop;//Sets the number of bins to look at, which are different depending on whether or not you're doing the calibration
285  float fitEnd;//The fit is super generic so it really doesn't care if you're fitting from 0-2 (JES) or 0-80 (linearity), but just to be thorough
286  float fitStart;
287  if(isLin)
288  {
289  stop = pt_N_truth;
290  fitEnd = 80.;
291  fitStart = .1;
292  }
293  else
294  {
295  stop = pt_N;
296  fitEnd = 2.;
297  fitStart = 0.1;
298  }
299  for(int icent = 0; icent < cent_N; ++icent){
300  for (int i = 0; i < stop; ++i){
301  TF1 *func = new TF1("func","gaus",fitStart,fitEnd);
302  h_MC_Reso_pt->GetXaxis()->SetRange(i+1,i+1);
303  h_MC_Reso_pt->GetZaxis()->SetRange(icent+1,icent+1);
304  TH1F *h_temp = (TH1F*) h_MC_Reso_pt->Project3D("y");
305  if(isLin)h_temp -> Rebin(rebin[i]);
306  func -> SetParameter(1, h_temp -> GetBinCenter(h_temp -> GetMaximumBin()));
307 
308  h_temp -> Fit(func,"","",fitStart,fitEnd);
309  h_temp -> Write(Form("hFit_cent%d_pt%d_Fit0",icent+1,i));
310  func -> SetParameter(1, h_temp -> GetBinCenter(h_temp -> GetMaximumBin()));
311 
312  h_temp->Fit(func,"","",func->GetParameter(1)-0.75*func->GetParameter(2),func->GetParameter(1)+0.75*func->GetParameter(2));
313  if(isLin)h_temp -> GetXaxis() -> SetRangeUser(func->GetParameter(1)-4*func->GetParameter(2),func->GetParameter(1)+4*func->GetParameter(2));
314  func -> SetLineColor(2);
315  func -> SetRange(func->GetParameter(1)-func->GetParameter(2),func->GetParameter(1)+func->GetParameter(2));
316  h_temp->Draw();
317  func -> Draw("same");
318  gPad -> SetLogy();
319  func->SetLineColor(2);
320 
321  //gPad -> WaitPrimitive();
322  h_temp -> Write(Form("hFit_cent%d_pt%d_Fit1",icent+1,i));
323  //leg->AddEntry("",Form("%2.0f%%-%2.0f%%",h_MC_Reso_pt->GetZaxis()->GetBinLowEdge(icent+1),h_MC_Reso_pt->GetZaxis()->GetBinLowEdge(icent+2)),"");
324  leg->AddEntry("",Form("%g < p_T < %g GeV",h_MC_Reso_pt->GetXaxis()->GetBinLowEdge(i+1),h_MC_Reso_pt->GetXaxis()->GetBinLowEdge(i+2)),"");
325  leg->Draw("SAME");
326  c->Print(Form("plots/hFit_cent%d_pt%d_Fit1_Corr%d_isLin%d_Param%g.pdf",icent+1,i,applyCorr,isLin,isoParam));
327  leg->Clear();
328  float dsigma = func->GetParError(2);
329  float denergy = func->GetParError(1);
330  float sigma = func->GetParameter(2);
331  float energy = func->GetParameter(1);
332 
333  float djer = dsigma/energy + sigma*denergy/pow(energy,2);//correct way to compute error on the resolution.
334  if(isLin)
335  {
336  g_jes[icent]->SetPoint(i,0.5*(pt_range_truth[i]+pt_range_truth[i+1]),func->GetParameter(1));
337  g_jes[icent]->SetPointError(i,0.5*(pt_range_truth[i+1]-pt_range_truth[i]),func->GetParError(1));
338 
339  g_jer[icent]->SetPoint(i,0.5*(pt_range_truth[i]+pt_range_truth[i+1]),func->GetParameter(2)/func->GetParameter(1));
340  g_jer[icent]->SetPointError(i,0.5*(pt_range_truth[i+1]-pt_range_truth[i]),djer);
341  }
342  else
343  {
344  g_jes[icent]->SetPoint(i,0.5*(pt_range[i]+pt_range[i+1]),func->GetParameter(1));
345  g_jes[icent]->SetPointError(i,0.5*(pt_range[i+1]-pt_range[i]),func->GetParError(1));
346 
347  g_jer[icent]->SetPoint(i,0.5*(pt_range[i]+pt_range[i+1]),func->GetParameter(2)/func->GetParameter(1));
348  g_jer[icent]->SetPointError(i,0.5*(pt_range[i+1]-pt_range[i]),djer);
349  }
350  }
351  }
352  //c->Print("fits04.pdf)");
353 
354 
355 
356  for(int icent = 0; icent < cent_N; ++icent){
357  g_jes[icent]->Write(Form("g_jes_cent%i",icent));
358  g_jer[icent]->Write(Form("g_jer_cent%i",icent));
359  }
360 
361  recoDist -> Write();
362  truthDist -> Write();
363 
364  h_pt_true->Write();
365  h_eta_phi_true->Write();
366  h_pt_true_matched->Write();
367  h_eta_phi_reco->Write();
368  h_pt_reco->Write();
369  h_subEt_pt->Write();
370 
371  f_out -> Close();
372 }
373 
374 bool isIso(int thisRjet, int thisTjet, vector<float> *rJetpT, vector<float> *tJetpT, vector<float> *rJetEta, vector<float> *tJetEta, vector<float> *rJetPhi, vector<float> *tJetPhi, float minDr, TH1F *&rDist, TH1F *&tDist)
375 {
376  if(thisRjet < 0) return false;
377  float thisRJetEta = rJetEta->at(thisRjet);
378  float thisTJetEta = tJetEta->at(thisTjet);
379  float thisRJetPhi = rJetPhi->at(thisRjet);
380  float thisTJetPhi = tJetPhi->at(thisTjet);
381  float minPt = 3.5;
382  float dr = -1;
383 
384  //uncomment these cout statements if you need to convince yourself this makes sense.
385  //std::cout << "On rJet: " << thisRjet << " with size " << rJetpT->size() << std::endl;
386  for(int i = 0; i < rJetpT->size(); i++)
387  {
388  if(i == thisRjet)
389  {
390  //std::cout << "On index: " << i << "; thisRJet: " << thisRjet << std::endl;
391  continue;
392  }
393  if(rJetpT->at(i) < minPt)
394  {
395  //std::cout << "Discarding reco jet with pt: " << rJetpT->at(i) << std::endl;
396  continue;
397  }
398  float deta = pow(thisRJetEta - rJetEta->at(i),2);
399  float dphi = thisRJetPhi - rJetPhi->at(i);
400  while(dphi > TMath::Pi()) dphi -= 2*TMath::Pi();
401  while(dphi < -TMath::Pi()) dphi += 2*TMath::Pi();
402  dphi = pow(dphi,2);
403 
404  dr = sqrt(deta + dphi);
405 
406  if(dr < minDr) return false;
407 
408  }
409 
410  if(dr)rDist -> Fill(dr);//dr will stay at -1 if above loop is totally bypassed, which usually happens becuase of the p_T filter.
411  dr = -1;
412  for(int i = 0; i < tJetpT->size(); i++)
413  {
414  if(i == thisTjet) continue;
415  //if(tJetpT < minPt) continue;
416 
417  float deta = pow(thisTJetEta - tJetEta->at(i),2);
418  float dphi = pow(thisTJetPhi - tJetPhi->at(i),2);
419  while(dphi > TMath::Pi()) dphi -= 2*TMath::Pi();
420  while(dphi < -TMath::Pi()) dphi += 2*TMath::Pi();
421  dphi = pow(dphi,2);
422 
423  dr = sqrt(deta + dphi);
424 
425  if(dr < minDr) return false;
426  }
427 
428  if(dr)tDist -> Fill(dr);
429 
430  return true;
431 }