ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/Systematics.C
Revision: 1.6
Committed: Wed Jul 20 12:28:08 2011 UTC (13 years, 9 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.5: +86 -20 lines
Log Message:
Added possibly superfluous additional systematic uncertainty; the value is calculated but currently deactivated, thus doesn't go into the total systematic at the moment (this can be changed easily by removing // on line 362

File Contents

# User Rev Content
1 buchmann 1.1 #include <iostream>
2     #include <vector>
3     #include <sys/stat.h>
4    
5     #include <TMath.h>
6     #include <TColor.h>
7     #include <TPaveText.h>
8     #include <TRandom.h>
9     #include <TF1.h>
10    
11     #ifndef SampleClassLoaded
12     #include "ActiveSamples.C"
13     #endif
14    
15     #ifndef Verbosity
16     #define Verbosity 0
17     #endif
18    
19     #include <TFile.h>
20     #include <TTree.h>
21     #include <TH1.h>
22     #include <TCut.h>
23     #include <TMath.h>
24     #include <TLine.h>
25     #include <TCanvas.h>
26     #include <TProfile.h>
27     #include <TF1.h>
28    
29    
30    
31     Int_t nBins = 100;
32     Float_t jzbMin = -207;
33     Float_t jzbMax = 243;
34     Float_t jzbSel = 100;
35     int iplot=0;
36     int verbose=0;
37 buchmann 1.2 string geqleq;
38     string mcjzbexpression;
39 buchmann 1.4 bool automatized=false;//if we're running this fully automatized we don't want each function to flood the screen
40 buchmann 1.2
41     TString geq_or_leq() {
42     if(geqleq=="geq") return TString(">=");
43     if(geqleq=="leq") return TString("<=");
44     return TString("GEQ_OR_LEQ_ERROR");
45     }
46 buchmann 1.1
47 buchmann 1.6 TString ngeq_or_leq() {
48     if(geqleq=="geq") return TString("<=");
49     if(geqleq=="leq") return TString(">=");
50     return TString("NGEQ_OR_LEQ_ERROR");
51     }
52    
53 buchmann 1.1 //______________________________________________________________________________
54     Double_t Interpolate(Double_t x, TH1 *histo)
55     {
56     // Given a point x, approximates the value via linear interpolation
57     // based on the two nearest bin centers
58     // Andy Mastbaum 10/21/08
59     // in newer ROOT versions but not in the one I have so I had to work around that ...
60    
61     Int_t xbin = histo->FindBin(x);
62     Double_t x0,x1,y0,y1;
63    
64     if(x<=histo->GetBinCenter(1)) {
65     return histo->GetBinContent(1);
66     } else if(x>=histo->GetBinCenter(histo->GetNbinsX())) {
67     return histo->GetBinContent(histo->GetNbinsX());
68     } else {
69     if(x<=histo->GetBinCenter(xbin)) {
70     y0 = histo->GetBinContent(xbin-1);
71     x0 = histo->GetBinCenter(xbin-1);
72     y1 = histo->GetBinContent(xbin);
73     x1 = histo->GetBinCenter(xbin);
74     } else {
75     y0 = histo->GetBinContent(xbin);
76     x0 = histo->GetBinCenter(xbin);
77     y1 = histo->GetBinContent(xbin+1);
78     x1 = histo->GetBinCenter(xbin+1);
79     }
80     return y0 + (x-x0)*((y1-y0)/(x1-x0));
81     }
82     }
83    
84    
85     //____________________________________________________________________________________
86     // Efficiency plot
87     TH1F* plotEff(TTree* events, TCut kbase, TString informalname) {
88     iplot++;
89     int count=iplot;
90     // Define new histogram
91     char hname[30]; sprintf(hname,"hJzbEff%d",count);
92     TH1F* hJzbEff = new TH1F(hname,"JZB selection efficiency ; JZB (GeV/c); Efficiency",
93     nBins,jzbMin,jzbMax);
94     Float_t step = (jzbMax-jzbMin)/static_cast<Float_t>(nBins);
95    
96 buchmann 1.6 events->Draw(mcjzbexpression.c_str(),"genJZB>-400"&&kbase,"goff");
97 buchmann 1.1 Float_t maxEff = events->GetSelectedRows();
98 buchmann 1.5 if(verbose>0) dout << hname << " (" << informalname <<") " << maxEff << std::endl;
99 buchmann 1.1
100 buchmann 1.5 if(verbose>0) dout << "JZB max = " << jzbMax << std::endl;
101 buchmann 1.1 // Loop over steps to get efficiency curve
102     char cut[256];
103     for ( Int_t iBin = 0; iBin<nBins; ++iBin ) {
104 buchmann 1.6 sprintf(cut,"genJZB>%3f",jzbMin+iBin*step);
105 buchmann 1.2 events->Draw(mcjzbexpression.c_str(),TCut(cut)&&kbase,"goff");
106 buchmann 1.1 Float_t eff = static_cast<Float_t>(events->GetSelectedRows())/maxEff;
107 buchmann 1.5 // dout << "COUCOU " << __LINE__ << std::endl;
108 buchmann 1.1 hJzbEff->SetBinContent(iBin+1,eff);
109     hJzbEff->SetBinError(iBin+1,TMath::Sqrt(eff*(1-eff)/maxEff));
110     }
111     return hJzbEff;
112    
113    
114     }
115    
116    
117     //________________________________________________________________________________________
118     // Pile-up efficiency
119     float pileup(TTree *events, string informalname, Float_t myJzbMax = 140. ) {
120     nBins = 16;
121     jzbMax = myJzbMax;
122    
123     // Acceptance cuts
124 buchmann 1.6 TCut kbase("abs(genMll-91.2)<20&&genNjets>2&&genZPt>0&&abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
125 buchmann 1.1
126     TH1F* hLM4 = plotEff(events,kbase,informalname);
127     hLM4->SetMinimum(0.);
128    
129     // Nominal function
130     TF1* func = new TF1("func","0.5*TMath::Erfc([0]*x-[1])",jzbMin,jzbMax);
131     func->SetParameter(0,0.03);
132     func->SetParameter(1,0.);
133     hLM4->Fit(func,"Q");
134    
135     // Pimped-up function
136     TF1* funcUp = (TF1*)func->Clone();
137     funcUp->SetParameter( 0., func->GetParameter(0)/1.1); // 10% systematic error (up in sigma => 0.1 in erfc)
138 buchmann 1.5 if(!automatized) dout << " PU: " << funcUp->Eval(jzbSel) << " " << func->Eval(jzbSel)
139 buchmann 1.1 << "(" << (funcUp->Eval(jzbSel)-func->Eval(jzbSel))/func->Eval(jzbSel)*100. << "%)" << std::endl;
140    
141     return (funcUp->Eval(jzbSel)-func->Eval(jzbSel))/func->Eval(jzbSel)*100.;
142    
143     }
144    
145     //____________________________________________________________________________________
146 buchmann 1.6 // Effect of peak shifting
147     void PeakError(TTree *events,float &result, string mcjzb, float peakerr) {
148     TString peakup("("+TString(mcjzb)+"+"+TString(any2string(TMath::Abs(peakerr)))+")"+geq_or_leq()+TString(any2string(jzbSel)));
149     TString peakdown("("+TString(mcjzb)+"-"+TString(any2string(TMath::Abs(peakerr)))+")"+geq_or_leq()+TString(any2string(jzbSel)));
150     TString peakcentral("("+TString(mcjzb)+")"+geq_or_leq()+TString(any2string(jzbSel)));
151     TString npeakup("("+TString(mcjzb)+"+"+TString(any2string(TMath::Abs(peakerr)))+")"+ngeq_or_leq()+"-"+TString(any2string(jzbSel)));
152     TString npeakdown("("+TString(mcjzb)+"-"+TString(any2string(TMath::Abs(peakerr)))+")"+ngeq_or_leq()+"-"+TString(any2string(jzbSel)));
153     TString npeakcentral("("+TString(mcjzb)+")"+ngeq_or_leq()+"-"+TString(any2string(jzbSel)));
154    
155     nBins = 1;
156     string informalname="PeakErrorCalculation";
157     float resup,resdown,rescent;
158     for(int i=0;i<3;i++) {
159     string poscut,negcut;
160     if(i==0) {
161     poscut=peakcentral;
162     negcut=npeakcentral;
163     } else if(i==1) {
164     poscut=peakdown;
165     negcut=npeakdown;
166     } else if(i==2) {
167     poscut=peakup;
168     negcut=npeakup;
169     }
170     TH1F* histossfp = plotEff(events,(cutnJets&&cutOSSF&&cutmass&&poscut.c_str()),informalname);
171     TH1F* histossfm = plotEff(events,(cutnJets&&cutOSSF&&cutmass&&negcut.c_str()),informalname);
172     TH1F* histosofp = plotEff(events,(cutnJets&&cutOSOF&&cutmass&&poscut.c_str()),informalname);
173     TH1F* histosofm = plotEff(events,(cutnJets&&cutOSOF&&cutmass&&negcut.c_str()),informalname);
174    
175     TH1F* sbhistossfp = plotEff(events,(cutnJets&&cutOSSF&&sidebandcut&&poscut.c_str()),informalname);
176     TH1F* sbhistossfm = plotEff(events,(cutnJets&&cutOSSF&&sidebandcut&&negcut.c_str()),informalname);
177     TH1F* sbhistosofp = plotEff(events,(cutnJets&&cutOSOF&&sidebandcut&&poscut.c_str()),informalname);
178     TH1F* sbhistosofm = plotEff(events,(cutnJets&&cutOSOF&&sidebandcut&&negcut.c_str()),informalname);
179    
180     float obs = histossfp->Integral();
181     float pre = histossfm->Integral() + 1.0/3*(histosofp->Integral()-histosofm->Integral() + sbhistossfp->Integral() - sbhistossfm->Integral() + sbhistosofp->Integral() - sbhistosofm->Integral());
182     if(i==0) rescent=obs-pre;
183     else if(i==1) resdown=obs-pre;
184     else if(i==2) resup=obs-pre;
185    
186     delete histossfp;
187     delete histossfm;
188     delete histosofp;
189     delete histosofm;
190    
191     delete sbhistossfp;
192     delete sbhistossfm;
193     delete sbhistosofp;
194     delete sbhistosofm;
195    
196     }
197     if(TMath::Abs((rescent-resup))>TMath::Abs(rescent-resdown)) result=(TMath::Abs(rescent-resup)/rescent)*100;
198     else result=(TMath::Abs(rescent-resdown)/rescent)*100;
199     }
200    
201     //____________________________________________________________________________________
202 buchmann 1.1 // Total selection efficiency (MC)
203 buchmann 1.4 void MCefficiency(TTree *events,float &result, float &resulterr,string mcjzb) {
204 buchmann 1.1
205     char jzbSelStr[256]; sprintf(jzbSelStr,"%f",jzbSel);
206     // All acceptance cuts at gen. level
207 buchmann 1.6 TCut kbase("abs(genMll-91.2)<20&&genNjets>2&&genZPt>0&&genJZB"+geq_or_leq()+TString(jzbSelStr)+"&&genId1==-genId2");
208 buchmann 1.1 // Corresponding reco. cuts
209 buchmann 1.2 TCut ksel("abs(mll-91.2)<20&&id1==id2&&"+TString(mcjzb)+geq_or_leq()+TString(jzbSelStr));
210 buchmann 1.1
211 buchmann 1.2 events->Draw(mcjzbexpression.c_str(),kbase&&ksel,"goff");
212 buchmann 1.1 Float_t sel = events->GetSelectedRows();
213 buchmann 1.2 events->Draw(mcjzbexpression.c_str(),kbase,"goff");
214 buchmann 1.1 Float_t tot = events->GetSelectedRows();
215    
216 buchmann 1.4 result=sel/tot;
217     resulterr=TMath::Sqrt(sel/tot*(1-sel/tot)/tot);
218 buchmann 1.5 if(!automatized) dout << " MC efficiency: " << result << "+-" << resulterr << std::endl;
219 buchmann 1.1 }
220    
221     float JZBefficiency(TTree *events, string informalname) {
222 buchmann 1.6 TCut kbase("abs(genMll-91.2)<20&&genNjets>2&&genZPt>0&&abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
223 buchmann 1.1 TH1F* hLM4 = plotEff(events,kbase,informalname);
224     Int_t bin = hLM4->FindBin(jzbSel); // To get the error
225 buchmann 1.5 if(!automatized) dout << " Efficiency at JZB==" << jzbSel << std::endl;
226     if(!automatized) dout << " " << Interpolate(jzbSel,hLM4) << "+-" << hLM4->GetBinError(bin) << std::endl;
227 buchmann 1.1 return -1;
228     }
229    
230     //________________________________________________________________________
231     // Effect of energy scale on efficiency
232     void JZBjetScale(TTree *events, float &jesdown, float &jesup, string informalname="",float syst=0.1, Float_t jzbSelection=-1, TString plotName = "" ) {
233 buchmann 1.6 TCut kbase("abs(genMll-91.2)<20&&genZPt>0");
234 buchmann 1.1 TCut ksel("abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
235     TCut nJets("pfJetGoodNum>2");
236     stringstream down,up;
237     down << "pfJetGoodNum"<<30*(1-syst)<<">=3";
238     up << "pfJetGoodNum"<<30*(1+syst)<<">=3";
239    
240     TCut nJetsP(up.str().c_str());
241     TCut nJetsM(down.str().c_str());
242    
243     if ( !(plotName.Length()>1) ) plotName = informalname;
244    
245     nBins = 1; jzbMin = jzbSel*0.95; jzbMax = jzbSel*1.05;
246     TH1F* hist = plotEff(events,(kbase&&ksel&&nJets),informalname);
247    
248     TH1F* histp = plotEff(events,(kbase&&ksel&&nJetsP),informalname);
249    
250     TH1F* histm = plotEff(events,(kbase&&ksel&&nJetsM),informalname);
251    
252     // Dump some information
253     Float_t eff = Interpolate(jzbSel,hist);
254     Float_t effp = Interpolate(jzbSel,histp);
255     Float_t effm = Interpolate(jzbSel,histm);
256 buchmann 1.5 if(!automatized) dout << " Efficiency at JZB==" << jzbSel << std::endl;
257     if(!automatized) dout << " JESup: " << effp << " (" << (effp-eff)/eff*100. << "%)" << std::endl;
258     if(!automatized) dout << " central: " << eff << std::endl;
259     if(!automatized) dout << " JESdown: " << effm << " (" << (effm-eff)/eff*100. << "%)" << std::endl;
260 buchmann 1.1 jesup=(effp-eff)/eff*100.;
261     jesdown=(effm-eff)/eff*100.;
262     }
263    
264     //________________________________________________________________________
265     // Effect of energy scale on JZB efficiency
266     void doJZBscale(TTree *events, float &down, float &up, float &syst, float systematic, string informalname) {
267    
268 buchmann 1.6 TCut kbase("abs(genMll-91.2)<20&&genZPt>0&&genNjets>2");
269 buchmann 1.1 TCut ksel("abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
270    
271     nBins = 50;
272     jzbMin = 0.5*jzbSel;
273     jzbMax = 2.0*jzbSel;
274    
275     TH1F* hist = plotEff(events,kbase&&ksel,informalname);
276    
277     // Dump some information
278     Float_t eff = Interpolate(jzbSel,hist);
279     Float_t effp = Interpolate(jzbSel*(1.+systematic),hist);
280     Float_t effm = Interpolate(jzbSel*(1.-systematic),hist);
281 buchmann 1.5 if(!automatized) dout << " efficiency at JZB==" << jzbSel*(1.+systematic) << "(-"<<syst*100<<"%) : " << effp << " (" << ((effp-eff)/eff)*100. << "%)" << std::endl;
282     if(!automatized) dout << " efficiency at JZB==" << jzbSel << ": " << eff << std::endl;
283     if(!automatized) dout << " efficiency at JZB==" << jzbSel*(1.-systematic) << "(-"<<syst*100<<"%) : " << effm << " (" << ((effm-eff)/eff)*100. << "%)" << std::endl;
284 buchmann 1.1 up=((effp-eff)/eff)*100;
285     down=((effm-eff)/eff)*100;
286     }
287    
288     //________________________________________________________________________
289     // JZB response (true/reco. vs. true)
290     void JZBresponse(TTree *events, bool isMET = kFALSE, Float_t myJzbMax = 200., Int_t nPeriods = 9 ) {
291    
292     jzbMin = 20;
293 buchmann 1.6 TCut kbase("abs(genMll-91.2)<20&&genZPt>0&&genNjets>2");
294 buchmann 1.1 TCut ksel("abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
295    
296     TProfile* hJzbResp = new TProfile("hJzbResp","JZB response ; JZB true (GeV/c); JZB reco. / JZB true",
297     nPeriods, jzbMin, myJzbMax, "" );
298    
299 buchmann 1.6 if (!isMET) events->Project("hJzbResp","("+TString(mcjzbexpression)+")/genJZB:genJZB",kbase&&ksel);
300 buchmann 1.1 else events->Project("hJzbResp","met[4]/genMET:genMET",kbase&&ksel);
301    
302     hJzbResp->SetMaximum(1.2);
303     hJzbResp->SetMinimum(0.2);
304     hJzbResp->Fit("pol0","Q");
305     TF1 *fittedfunction = hJzbResp->GetFunction("pol0");
306 buchmann 1.5 if(!automatized) dout << " Response: " << fittedfunction->GetParameter(0) << " +/- " << fittedfunction->GetParError(0) << endl;
307     delete hJzbResp;
308 buchmann 1.1 }
309    
310    
311 buchmann 1.6 void do_systematics_for_one_file(TTree *events,string informalname, vector<vector<float> > &results,string mcjzb,string datajzb,float peakerror) {
312 buchmann 1.1
313     float JetEnergyScaleUncert=0.1;
314     float JZBScaleUncert=0.1;
315 buchmann 1.2 mcjzbexpression=mcjzb;
316 buchmann 1.1
317     float triggereff=4;//percent!
318 buchmann 1.5 dout << "Trigger efficiency not implemented in this script yet, still using external one" << endl;
319 buchmann 1.1 float leptonseleff=2;//percent!
320 buchmann 1.5 dout << "Lepton selection efficiency not implemented in this script yet, still using external one" << endl;
321 buchmann 1.1
322     float mceff,mcefferr;
323 buchmann 1.5 if(!automatized) dout << "MC efficiencies:" << endl;
324 buchmann 1.2 MCefficiency(events,mceff,mcefferr,mcjzb);
325 buchmann 1.1 JZBefficiency(events,informalname);
326    
327 buchmann 1.6 if(!automatized) dout << "Error from Peak position:" << endl;
328     float sysfrompeak=0;
329     PeakError(events,sysfrompeak,mcjzb,peakerror);
330    
331 buchmann 1.5 if(!automatized) dout << "Jet energy scale: " << std::endl;
332 buchmann 1.1 float jesup,jesdown;
333     JZBjetScale(events,jesdown,jesup,informalname,JetEnergyScaleUncert);
334    
335 buchmann 1.5 if(!automatized) dout << "JZB scale: " << std::endl;
336 buchmann 1.1 float scaleup,scaledown,scalesyst;
337     doJZBscale(events,scaledown,scaleup,scalesyst,JZBScaleUncert,informalname);
338    
339 buchmann 1.5 if(!automatized) dout << "JZB response: " << std::endl;
340 buchmann 1.1 JZBresponse(events);
341    
342 buchmann 1.5 if(!automatized) dout << "Pileup: " << std::endl;
343 buchmann 1.1 float resolution=pileup(events,informalname);
344    
345 buchmann 1.5 dout << "_______________________________________________" << endl;
346 buchmann 1.6 dout << " SUMMARY FOR " << informalname << " with JZB>" << jzbSel << " (all in %) " << endl;
347     dout << "MC efficiency: " << 100*mceff << "+/-" << 100*mcefferr << endl;
348 buchmann 1.5 dout << "Trigger efficiency: " << triggereff << endl;
349     dout << "Lepton Sel Eff: " << leptonseleff << endl;
350 buchmann 1.6 dout << "Jet energy scale: " << jesup << " " << jesdown << endl;
351     dout << "JZB Scale Uncert: " << scaledown << " " << scaleup << endl;
352 buchmann 1.5 dout << "Resolution : " << resolution << endl;
353 buchmann 1.6 dout << "From peak : " << sysfrompeak << endl;
354 buchmann 1.1
355    
356 buchmann 1.4 float toterr=0;
357     toterr+=(triggereff/100)*(triggereff/100);
358     toterr+=(leptonseleff/100)*(leptonseleff/100);
359     if(fabs(jesup)>fabs(jesdown)) toterr+=(jesup/100)*(jesup/100); else toterr+=(jesdown/100)*(jesdown/100);
360     if(fabs(scaleup)>fabs(scaledown)) toterr+=(scaleup/100)*(scaleup/100); else toterr+=(scaledown/100)*(scaledown/100);
361     toterr+=(resolution/100)*(resolution/100);
362 buchmann 1.6 // toterr+=(sysfrompeak/100)*(sysfrompeak/100);
363     cout << "WATCH OUT, PEAK SYSTEMATIC NOT INCLUDED ATM" << endl;
364 buchmann 1.4 toterr=TMath::Sqrt(toterr);
365 buchmann 1.6 dout << "FINAL RESULT : " << 100*mceff << " +/- "<< 100*mcefferr << " (stat) +/- " << 100*toterr << " (syst) %" << endl;
366 buchmann 1.5 dout << " we thus use the sqrt of the sum of the squares which is : " << 100*TMath::Sqrt(mcefferr*mcefferr+(toterr*toterr)) << endl;
367 buchmann 1.4 vector<float> res;
368     res.push_back(jzbSel);
369     res.push_back(mceff);
370     res.push_back(mcefferr);
371     res.push_back(toterr);
372     res.push_back(TMath::Sqrt((mcefferr)*(mcefferr)+(toterr*toterr)));
373    
374     results.push_back(res);
375 buchmann 1.1 }
376    
377 buchmann 1.6 vector<vector<float> > compute_systematics(string mcjzb, float mcpeakerror, string datajzb, samplecollection &signalsamples, vector<float> bins) {
378 buchmann 1.4 automatized=true;
379     vector< vector<float> > systematics;
380 buchmann 1.1 for (int isignal=0; isignal<signalsamples.collection.size();isignal++) {
381 buchmann 1.5 dout << "Looking at signal " << (signalsamples.collection)[isignal].filename << endl;
382 buchmann 1.1 for(int ibin=0;ibin<bins.size();ibin++) {
383     jzbSel=bins[ibin];
384 buchmann 1.2 geqleq="geq";
385 buchmann 1.6 do_systematics_for_one_file((signalsamples.collection)[isignal].events,(signalsamples.collection)[isignal].samplename,systematics,mcjzb,datajzb,mcpeakerror);
386 buchmann 1.1 }//end of bin loop
387     }//end of signal loop
388 buchmann 1.4 return systematics;
389 buchmann 1.1 }