ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/Systematics.C
Revision: 1.43
Committed: Thu Oct 27 10:19:41 2011 UTC (13 years, 6 months ago) by pablom
Content type: text/plain
Branch: MAIN
Changes since 1.42: +70 -2 lines
Log Message:
Calculation of partial efficiencies.

File Contents

# User Rev Content
1 buchmann 1.1 #include <iostream>
2     #include <vector>
3     #include <sys/stat.h>
4 buchmann 1.31 #include <algorithm>
5     #include <cmath>
6 buchmann 1.1
7     #include <TMath.h>
8     #include <TColor.h>
9     #include <TPaveText.h>
10     #include <TRandom.h>
11     #include <TF1.h>
12    
13     #ifndef SampleClassLoaded
14     #include "ActiveSamples.C"
15     #endif
16    
17     #ifndef Verbosity
18     #define Verbosity 0
19     #endif
20    
21     #include <TFile.h>
22     #include <TTree.h>
23     #include <TH1.h>
24     #include <TCut.h>
25     #include <TMath.h>
26     #include <TLine.h>
27     #include <TCanvas.h>
28     #include <TProfile.h>
29     #include <TF1.h>
30    
31    
32    
33     Int_t nBins = 100;
34     Float_t jzbMin = -207;
35     Float_t jzbMax = 243;
36     Float_t jzbSel = 100;
37     int iplot=0;
38     int verbose=0;
39 buchmann 1.2 string geqleq;
40     string mcjzbexpression;
41 buchmann 1.4 bool automatized=false;//if we're running this fully automatized we don't want each function to flood the screen
42 buchmann 1.2
43     TString geq_or_leq() {
44     if(geqleq=="geq") return TString(">=");
45     if(geqleq=="leq") return TString("<=");
46     return TString("GEQ_OR_LEQ_ERROR");
47     }
48 buchmann 1.1
49 buchmann 1.6 TString ngeq_or_leq() {
50     if(geqleq=="geq") return TString("<=");
51     if(geqleq=="leq") return TString(">=");
52     return TString("NGEQ_OR_LEQ_ERROR");
53     }
54    
55 buchmann 1.1 //______________________________________________________________________________
56     Double_t Interpolate(Double_t x, TH1 *histo)
57     {
58     // Given a point x, approximates the value via linear interpolation
59     // based on the two nearest bin centers
60     // Andy Mastbaum 10/21/08
61     // in newer ROOT versions but not in the one I have so I had to work around that ...
62    
63     Int_t xbin = histo->FindBin(x);
64     Double_t x0,x1,y0,y1;
65    
66     if(x<=histo->GetBinCenter(1)) {
67     return histo->GetBinContent(1);
68     } else if(x>=histo->GetBinCenter(histo->GetNbinsX())) {
69     return histo->GetBinContent(histo->GetNbinsX());
70     } else {
71     if(x<=histo->GetBinCenter(xbin)) {
72     y0 = histo->GetBinContent(xbin-1);
73     x0 = histo->GetBinCenter(xbin-1);
74     y1 = histo->GetBinContent(xbin);
75     x1 = histo->GetBinCenter(xbin);
76     } else {
77     y0 = histo->GetBinContent(xbin);
78     x0 = histo->GetBinCenter(xbin);
79     y1 = histo->GetBinContent(xbin+1);
80     x1 = histo->GetBinCenter(xbin+1);
81     }
82     return y0 + (x-x0)*((y1-y0)/(x1-x0));
83     }
84     }
85    
86 buchmann 1.7 //____________________________________________________________________________________
87     // Plotting with all contributions, i.e. sidebands, peak, osof,ossf ... (for a systematic)
88     float allcontributionsplot(TTree* events, TCut kBaseCut, TCut kMassCut, TCut kSidebandCut, TCut JZBPosCut, TCut JZBNegCut) {
89     iplot++;
90     int count=iplot;
91     // Define new histogram
92     string hname=GetNumericHistoName();
93     TH1F* hossfp = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
94     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kMassCut&&JZBPosCut&&cutOSSF,"goff");
95     hname=GetNumericHistoName();
96     TH1F* hossfn = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
97     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kMassCut&&JZBNegCut&&cutOSSF,"goff");
98    
99     hname=GetNumericHistoName();
100     TH1F* hosofp = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
101     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kMassCut&&JZBPosCut&&cutOSOF,"goff");
102     hname=GetNumericHistoName();
103     TH1F* hosofn = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
104     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kMassCut&&JZBNegCut&&cutOSOF,"goff");
105    
106 buchmann 1.42 float obs=0;
107     float pred=0;
108     flag_this_change(__FUNCTION__,__LINE__,true);//PlottingSetup::RestrictToMassPeak
109     if(PlottingSetup::RestrictToMassPeak) {
110     hname=GetNumericHistoName();
111     TH1F* sbhossfp = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
112     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kSidebandCut&&JZBPosCut&&cutOSSF,"goff");
113     hname=GetNumericHistoName();
114     TH1F* sbhossfn = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
115     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kSidebandCut&&JZBNegCut&&cutOSSF,"goff");
116    
117     hname=GetNumericHistoName();
118     TH1F* sbhosofp = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
119     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kSidebandCut&&JZBPosCut&&cutOSOF,"goff");
120     hname=GetNumericHistoName();
121     TH1F* sbhosofn = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
122     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kSidebandCut&&JZBNegCut&&cutOSOF,"goff");
123    
124     obs = hossfp->Integral();
125     pred= hossfn->Integral() + (1.0/3)*( hosofp->Integral() - hosofn->Integral() + sbhossfp->Integral() - sbhossfn->Integral() + sbhosofp->Integral() - sbhosofn->Integral());
126     delete sbhossfp,sbhossfn,sbhosofp,sbhosofn;
127     } else {
128     obs = hossfp->Integral();
129     pred= hossfn->Integral() + (hosofp->Integral() - hosofn->Integral());
130     }
131 buchmann 1.7
132     delete hossfp,hossfn,hosofp,hosofn;
133     return obs-pred;
134     }
135    
136 buchmann 1.1
137     //____________________________________________________________________________________
138     // Efficiency plot
139     TH1F* plotEff(TTree* events, TCut kbase, TString informalname) {
140     iplot++;
141     int count=iplot;
142     // Define new histogram
143     char hname[30]; sprintf(hname,"hJzbEff%d",count);
144     TH1F* hJzbEff = new TH1F(hname,"JZB selection efficiency ; JZB (GeV/c); Efficiency",
145     nBins,jzbMin,jzbMax);
146     Float_t step = (jzbMax-jzbMin)/static_cast<Float_t>(nBins);
147    
148 buchmann 1.6 events->Draw(mcjzbexpression.c_str(),"genJZB>-400"&&kbase,"goff");
149 buchmann 1.1 Float_t maxEff = events->GetSelectedRows();
150 buchmann 1.5 if(verbose>0) dout << hname << " (" << informalname <<") " << maxEff << std::endl;
151 buchmann 1.1
152 buchmann 1.5 if(verbose>0) dout << "JZB max = " << jzbMax << std::endl;
153 buchmann 1.1 // Loop over steps to get efficiency curve
154     char cut[256];
155     for ( Int_t iBin = 0; iBin<nBins; ++iBin ) {
156 buchmann 1.6 sprintf(cut,"genJZB>%3f",jzbMin+iBin*step);
157 buchmann 1.2 events->Draw(mcjzbexpression.c_str(),TCut(cut)&&kbase,"goff");
158 buchmann 1.1 Float_t eff = static_cast<Float_t>(events->GetSelectedRows())/maxEff;
159 buchmann 1.5 // dout << "COUCOU " << __LINE__ << std::endl;
160 buchmann 1.1 hJzbEff->SetBinContent(iBin+1,eff);
161     hJzbEff->SetBinError(iBin+1,TMath::Sqrt(eff*(1-eff)/maxEff));
162     }
163     return hJzbEff;
164    
165    
166     }
167    
168    
169     //________________________________________________________________________________________
170 pablom 1.23 // Master Formula
171     void master_formula(std::vector<float> eff, float &errHi, float &errLo) {
172    
173     float x0 = eff[0];
174     float deltaPos = 0, deltaNeg = 0;
175     for(int k = 0; k < (eff.size()-1)/2; k++) {
176     float xneg = eff[2*k+2];
177     float xpos = eff[2*k+1];
178     if(xpos-x0>0 || xneg-x0>0) {
179     if(xpos-x0 > xneg-x0) {
180     deltaPos += (xpos-x0)*(xpos-x0);
181     } else {
182     deltaPos += (xneg-x0)*(xneg-x0);
183     }
184     }
185     if(x0-xpos>0 || x0-xneg>0) {
186     if(x0-xpos > x0-xneg) {
187     deltaNeg += (xpos-x0)*(xpos-x0);
188     } else {
189     deltaNeg += (xneg-x0)*(xneg-x0);
190     }
191     }
192     }
193     errHi = sqrt(deltaPos);
194     errLo = sqrt(deltaNeg);
195    
196     }
197    
198    
199     //________________________________________________________________________________________
200     // Get normalization factor for the PDFs
201 buchmann 1.42 float get_norm_pdf_factor(TTree *events, int k, string addcut) {
202 pablom 1.23
203 buchmann 1.25 TH1F *haux = new TH1F("haux", "", 10000, 0, 5);
204 pablom 1.23 char nameVar[20];
205     sprintf(nameVar, "pdfW[%d]", k);
206 buchmann 1.42 events->Project("haux", nameVar, addcut.c_str());
207 buchmann 1.24 float thisW = haux->Integral();
208     events->Project("haux", "pdfW[0]");
209     float normW = haux->Integral();
210    
211     float factor=thisW/normW;
212 pablom 1.23
213     delete haux;
214    
215     return factor;
216    
217     }
218    
219    
220    
221     //________________________________________________________________________________________
222 buchmann 1.1 // Pile-up efficiency
223 buchmann 1.10 float pileup(TTree *events, bool requireZ, string informalname, string addcut="",Float_t myJzbMax = 140. ) {
224 buchmann 1.1 nBins = 16;
225     jzbMax = myJzbMax;
226    
227     // Acceptance cuts
228 buchmann 1.42 flag_this_change(__FUNCTION__,__LINE__,true);//PlottingSetup::RestrictToMassPeak
229     TCut kbase(PlottingSetup::genMassCut&&"genNjets>2&&genZPt>0"&&cutmass&&cutOSSF);
230 buchmann 1.10 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
231    
232 buchmann 1.42 if(requireZ&&PlottingSetup::RestrictToMassPeak) kbase=kbase&&"TMath::Abs(genMID)==23";
233 buchmann 1.1 TH1F* hLM4 = plotEff(events,kbase,informalname);
234     hLM4->SetMinimum(0.);
235    
236     // Nominal function
237     TF1* func = new TF1("func","0.5*TMath::Erfc([0]*x-[1])",jzbMin,jzbMax);
238     func->SetParameter(0,0.03);
239     func->SetParameter(1,0.);
240     hLM4->Fit(func,"Q");
241    
242     // Pimped-up function
243     TF1* funcUp = (TF1*)func->Clone();
244     funcUp->SetParameter( 0., func->GetParameter(0)/1.1); // 10% systematic error (up in sigma => 0.1 in erfc)
245 buchmann 1.5 if(!automatized) dout << " PU: " << funcUp->Eval(jzbSel) << " " << func->Eval(jzbSel)
246 buchmann 1.1 << "(" << (funcUp->Eval(jzbSel)-func->Eval(jzbSel))/func->Eval(jzbSel)*100. << "%)" << std::endl;
247    
248 buchmann 1.18 return (funcUp->Eval(jzbSel)-func->Eval(jzbSel))/func->Eval(jzbSel);
249 buchmann 1.1
250     }
251    
252     //____________________________________________________________________________________
253 buchmann 1.6 // Effect of peak shifting
254 buchmann 1.10 void PeakError(TTree *events,float &result, string mcjzb, float peakerr,string addcut="") {
255 buchmann 1.42 //Note: the cut used here is something like (JZBEXPRESSION+(peakerr)>50) without all the other cuts, to increase statistics (particularly for scans)
256 buchmann 1.6 TString peakup("("+TString(mcjzb)+"+"+TString(any2string(TMath::Abs(peakerr)))+")"+geq_or_leq()+TString(any2string(jzbSel)));
257     TString peakdown("("+TString(mcjzb)+"-"+TString(any2string(TMath::Abs(peakerr)))+")"+geq_or_leq()+TString(any2string(jzbSel)));
258     TString peakcentral("("+TString(mcjzb)+")"+geq_or_leq()+TString(any2string(jzbSel)));
259     TString npeakup("("+TString(mcjzb)+"+"+TString(any2string(TMath::Abs(peakerr)))+")"+ngeq_or_leq()+"-"+TString(any2string(jzbSel)));
260     TString npeakdown("("+TString(mcjzb)+"-"+TString(any2string(TMath::Abs(peakerr)))+")"+ngeq_or_leq()+"-"+TString(any2string(jzbSel)));
261     TString npeakcentral("("+TString(mcjzb)+")"+ngeq_or_leq()+"-"+TString(any2string(jzbSel)));
262     nBins = 1;
263     string informalname="PeakErrorCalculation";
264     float resup,resdown,rescent;
265     for(int i=0;i<3;i++) {
266     string poscut,negcut;
267     if(i==0) {
268     poscut=peakcentral;
269     negcut=npeakcentral;
270     } else if(i==1) {
271     poscut=peakdown;
272     negcut=npeakdown;
273     } else if(i==2) {
274     poscut=peakup;
275     negcut=npeakup;
276     }
277 buchmann 1.10 float res;
278     if(addcut=="") res=allcontributionsplot(events,cutnJets,cutmass,sidebandcut,poscut.c_str(),negcut.c_str());
279     else res=allcontributionsplot(events,cutnJets&&addcut.c_str(),cutmass,sidebandcut,poscut.c_str(),negcut.c_str());
280 buchmann 1.7 if(i==0) rescent=res;
281     else if(i==1) resdown=res;
282     else if(i==2) resup=res;
283 buchmann 1.6 }
284 buchmann 1.41 if(TMath::Abs(rescent-resup)>TMath::Abs(rescent-resdown)) result=(TMath::Abs(rescent-resup)/(float)rescent);
285     else result=(TMath::Abs(rescent-resdown)/(float)rescent);
286 buchmann 1.6 }
287    
288 pablom 1.43
289     void MCPartialefficiency(TTree *events,float &result, float &resulterr,string mcjzb,bool requireZ,int Neventsinfile, string addcut="", int k = 0, int type = 0) {
290     if(!events) {
291     write_error(__FUNCTION__,"Tree passed for efficiency calculation is invalid!");
292     result=0;resulterr=0;
293     return;
294     }
295    
296     char jzbSelStr[256]; sprintf(jzbSelStr,"%f",jzbSel);
297     // All acceptance cuts at gen. level
298     //TCut kbase("abs(genMll-91.2)<20&&genNjets>2&&genZPt>0&&genJZB"+geq_or_leq()+TString(jzbSelStr)+"&&genId1==-genId2");
299     TCut kbase("");
300     if(requireZ&&PlottingSetup::RestrictToMassPeak) kbase=kbase&&"TMath::Abs(genMID)==23";
301     if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
302     // Corresponding reco. cuts
303    
304     TCut acceptance("genPt2 != 0");
305     TCut massId(cutmass&&cutOSSF);
306     TCut njets(cutnJets);
307     TCut jzbp(TString(mcjzb)+geq_or_leq()+TString(jzbSelStr));
308     TCut jzbn(TString(mcjzb)+ngeq_or_leq()+TString("-")+TString(jzbSelStr));
309     dout << jzbp << endl;
310     dout << jzbn << endl;
311    
312     float ntotal = events->Draw("pt1", kbase, "goff");
313     TCut theCut;
314     switch(type) {
315     case 1:
316     theCut = kbase+acceptance;
317     break;
318     case 2:
319     theCut = kbase+massId;
320     break;
321     case 3:
322     theCut = kbase+massId+njets;
323     break;
324     case 4:
325     theCut = kbase+massId+njets+jzbn;
326     break;
327     default:
328     theCut = kbase+massId+njets+jzbn;
329     break;
330     }
331    
332     string stheCut(theCut);
333     char var[20];
334     sprintf(var, "pdfW[%d]", k);
335    
336     string svar(var);
337     string newtheCut;
338     if(k>0) newtheCut = "(" + stheCut + ")*" + svar;
339     else newtheCut = "(" + stheCut + ")"; // for k==0 or even k==-1 we don't need to evaluate PDFs
340    
341     TH1F *effh= new TH1F("effh","effh",1,-14000,14000);
342     if(k>=0) events->Draw((mcjzbexpression+">>effh").c_str(), newtheCut.c_str(),"goff");
343     else events->Draw((mcjzbexpression+">>effh").c_str(), theCut,"goff");
344     Float_t sel = effh->Integral();
345     Float_t nsel=0;
346     //Corrections due to normalization in the PDF. This has to be applied as well to the number of events in a file if the definition changes at some point.
347     float normFactor = 1;
348     if(k>=0) get_norm_pdf_factor(events, k, addcut);
349     sel = sel/normFactor;
350    
351     result=(sel)/ntotal;
352     resulterr=TMath::Sqrt(sel/ntotal*(1+sel/ntotal)/ntotal);
353    
354     }
355    
356 buchmann 1.6 //____________________________________________________________________________________
357 buchmann 1.1 // Total selection efficiency (MC)
358 buchmann 1.33 //returns the efficiency WITHOUT signal contamination, and the result and resulterr contain the result and the corresponding error
359     Value MCefficiency(TTree *events,float &result, float &resulterr,string mcjzb,bool requireZ,int Neventsinfile, string addcut="", int k = 0) {
360 pablom 1.43 if(k>1) write_warning(__FUNCTION__,"Setting automatized to off!"); automatized=false; // only do this once when computing pdf uncertainties
361 buchmann 1.40 if(!events) {
362 buchmann 1.42 write_error(__FUNCTION__,"Tree passed for efficiency calculation is invalid!");
363     result=0;
364     resulterr=0;
365     return Value(0,0);
366 buchmann 1.40 }
367 buchmann 1.42
368 buchmann 1.1 char jzbSelStr[256]; sprintf(jzbSelStr,"%f",jzbSel);
369     // All acceptance cuts at gen. level
370 buchmann 1.14 //TCut kbase("abs(genMll-91.2)<20&&genNjets>2&&genZPt>0&&genJZB"+geq_or_leq()+TString(jzbSelStr)+"&&genId1==-genId2");
371     TCut kbase("");
372 buchmann 1.42
373     flag_this_change(__FUNCTION__,__LINE__,true);//PlottingSetup::RestrictToMassPeak
374     if(requireZ&&PlottingSetup::RestrictToMassPeak) kbase=kbase&&"TMath::Abs(genMID)==23";
375 buchmann 1.9 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
376 buchmann 1.1 // Corresponding reco. cuts
377 buchmann 1.42
378     flag_this_change(__FUNCTION__,__LINE__,true);//PlottingSetup::RestrictToMassPeak
379     TCut ksel;//("pfJetGoodNum>2"&&cutmass&&"id1==id2&&"+TString(mcjzb)+geq_or_leq()+TString(jzbSelStr));
380     TCut ksel2;//("pfJetGoodNum>2"&&cutmass&&"id1==id2&&"+TString(mcjzb)+ngeq_or_leq()+TString("-")+TString(jzbSelStr));
381     flag_this_change(__FUNCTION__,__LINE__,true);//PlottingSetup::RestrictToMassPeak
382     if(PlottingSetup::RestrictToMassPeak||!ConsiderSignalContaminationForLimits) {
383     ksel=TCut("pfJetGoodNum>2"&&cutmass&&"id1==id2&&"+TString(mcjzb)+geq_or_leq()+TString(jzbSelStr));
384     ksel2=TCut("pfJetGoodNum>2"&&cutmass&&"id1==id2&&"+TString(mcjzb)+ngeq_or_leq()+TString("-")+TString(jzbSelStr));
385     } else {
386     //for off peak analysis we don't use the OSSF condition here yet so we can recycle these two cuts for the em condition!
387     ksel=TCut("pfJetGoodNum>2"&&cutmass&&(TString(mcjzb)+geq_or_leq()+TString(jzbSelStr)));
388     ksel2=TCut("pfJetGoodNum>2"&&cutmass&&(TString(mcjzb)+ngeq_or_leq()+TString("-")+TString(jzbSelStr)));
389     }
390    
391 pablom 1.23 TCut posSide = kbase&&ksel;
392     TCut negSide = kbase&&ksel2;
393     string sposSide(posSide);
394     string snegSide(negSide);
395     char var[20];
396     sprintf(var, "pdfW[%d]", k);
397 buchmann 1.42 if(k==-1) sprintf(var,"1.0");//case in which we don't want to evaluate PDFs
398 pablom 1.23 string svar(var);
399 buchmann 1.42 string newPosSide = "((id1==id2)&&(" + sposSide + "))*" + svar;
400     string newNegSide = "((id1==id2)&&(" + snegSide + "))*" + svar;
401     string emnewPosSide = "((id1!=id2)&&(" + sposSide + "))*" + svar; // only used for off peak analysis
402     string emnewNegSide = "((id1!=id2)&&(" + snegSide + "))*" + svar; // only used for off peak analysis
403 pablom 1.23
404 buchmann 1.24 TH1F *effh= new TH1F("effh","effh",1,-14000,14000);
405 buchmann 1.37 if(k>=0)events->Draw((mcjzbexpression+">>effh").c_str(), newPosSide.c_str(),"goff");
406 buchmann 1.42 else events->Draw((mcjzbexpression+">>effh").c_str(), (sposSide+"&&(id1==id2)").c_str(),"goff");//the OSSF condition is added for the offpeak analysis, in onpeak case it's there already but doesn't change anything.
407 buchmann 1.24 Float_t sel = effh->Integral();
408 buchmann 1.21 Float_t nsel=0;
409 buchmann 1.42
410     ///----------------------------------------------- THIS PART REQUIRES STUDYING! -------------------------
411    
412 buchmann 1.21 if(ConsiderSignalContaminationForLimits) {
413 buchmann 1.42 flag_this_change(__FUNCTION__,__LINE__,true);//PlottingSetup::RestrictToMassPeak
414     if(PlottingSetup::RestrictToMassPeak) {
415     events->Draw((mcjzbexpression+">>effh").c_str(), newNegSide.c_str(),"goff");
416     nsel += effh->Integral();
417     } else {
418     events->Draw((mcjzbexpression+">>effh").c_str(), newNegSide.c_str(),"goff");
419     nsel += effh->Integral();
420     events->Draw((mcjzbexpression+">>effh").c_str(), emnewPosSide.c_str(),"goff");
421     nsel += effh->Integral();
422     events->Draw((mcjzbexpression+">>effh").c_str(), emnewNegSide.c_str(),"goff");
423     nsel -= effh->Integral();
424     }
425 buchmann 1.21 }
426 buchmann 1.42
427 pablom 1.23 //Corrections due to normalization in the PDF. This has to be applied as well to the number of events in a file if the definition changes at some point.
428 buchmann 1.26 float normFactor = 1;
429 buchmann 1.42 if(k>=0) get_norm_pdf_factor(events, k, addcut);
430 pablom 1.23 sel = sel/normFactor;
431     nsel = nsel/normFactor;
432    
433 buchmann 1.13 // events->Draw(mcjzbexpression.c_str(),kbase,"goff");
434     // Float_t tot = events->GetSelectedRows();
435     Float_t tot = Neventsinfile;
436 buchmann 1.1
437 buchmann 1.33 Value result_wo_signalcont;
438    
439 buchmann 1.21 if(ConsiderSignalContaminationForLimits) {
440     result=(sel-nsel)/tot;
441     resulterr=(1.0/tot)*TMath::Sqrt(sel+nsel+(sel-nsel)*(sel-nsel)/tot);
442 buchmann 1.33 result_wo_signalcont=Value(sel/tot,TMath::Sqrt(sel/tot*(1+sel/tot)/tot));
443 buchmann 1.21 } else {//no signal contamination considered:
444     result=(sel)/tot;
445     resulterr=TMath::Sqrt(sel/tot*(1+sel/tot)/tot);
446 buchmann 1.33 result_wo_signalcont=Value(result,resulterr);
447 buchmann 1.21 }
448 pablom 1.43 if(!automatized && k>0 ) dout << "PDF assessment [" << k << "] : ";
449 buchmann 1.42 if(!automatized) dout << " MC efficiency: " << result << "+-" << resulterr << " ( JZB>" << jzbSel << " : " << sel << " , signal contamination : " << nsel << " and nevents=" << tot << ") with normFact=" << normFactor << std::endl;
450 buchmann 1.24 delete effh;
451 buchmann 1.34 return result_wo_signalcont;
452 buchmann 1.1 }
453    
454 buchmann 1.29
455 buchmann 1.42
456 buchmann 1.29 //____________________________________________________________________________________
457 buchmann 1.30 // Selection efficiency for one process (MC)
458 buchmann 1.29 vector<float> processMCefficiency(TTree *events,string mcjzb,bool requireZ,int Neventsinfile, string addcut) {
459     vector<float> process_efficiencies;
460     for(int iprocess=0;iprocess<=10;iprocess++) {
461     float this_process_efficiency,efferr;
462     stringstream addcutplus;
463     addcutplus<<addcut<<"&&(process=="<<iprocess<<")";
464     MCefficiency(events,this_process_efficiency, efferr,mcjzb,requireZ,Neventsinfile, addcutplus.str(),-1);
465     process_efficiencies.push_back(this_process_efficiency);
466     }
467     return process_efficiencies;
468     }
469    
470    
471 buchmann 1.10 void JZBefficiency(TTree *events, string informalname, float &jzbeff, float &jzbefferr, bool requireZ, string addcut="") {
472 buchmann 1.42 TCut kbase(genMassCut&&"genNjets>2&&genZPt>0"&&cutmass&&cutOSSF);
473 buchmann 1.10 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
474 buchmann 1.42 if(requireZ&&PlottingSetup::RestrictToMassPeak) kbase=kbase&&"TMath::Abs(genMID)==23";
475 buchmann 1.1 TH1F* hLM4 = plotEff(events,kbase,informalname);
476     Int_t bin = hLM4->FindBin(jzbSel); // To get the error
477 buchmann 1.8 jzbeff=Interpolate(jzbSel,hLM4);
478     jzbefferr=hLM4->GetBinError(bin);
479 buchmann 1.5 if(!automatized) dout << " Efficiency at JZB==" << jzbSel << std::endl;
480 buchmann 1.8 if(!automatized) dout << " " << jzbeff << "+-" << jzbefferr << std::endl;
481 buchmann 1.1 }
482    
483     //________________________________________________________________________
484     // Effect of energy scale on efficiency
485 buchmann 1.10 void JZBjetScale(TTree *events, float &jesdown, float &jesup, string informalname,bool requireZ,string addcut="",float syst=0.1, Float_t jzbSelection=-1, TString plotName = "" ) {
486 buchmann 1.42 TCut kbase(genMassCut&&"genZPt>0");
487 buchmann 1.10 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
488 buchmann 1.42 flag_this_change(__FUNCTION__,__LINE__,true);//PlottingSetup::RestrictToMassPeak
489     if(requireZ&&PlottingSetup::RestrictToMassPeak) kbase=kbase&&"TMath::Abs(genMID)==23";
490 buchmann 1.8
491 buchmann 1.42 TCut ksel(cutmass&&cutOSSF);
492 buchmann 1.1 TCut nJets("pfJetGoodNum>2");
493     stringstream down,up;
494     down << "pfJetGoodNum"<<30*(1-syst)<<">=3";
495     up << "pfJetGoodNum"<<30*(1+syst)<<">=3";
496    
497     TCut nJetsP(up.str().c_str());
498     TCut nJetsM(down.str().c_str());
499    
500     if ( !(plotName.Length()>1) ) plotName = informalname;
501    
502     nBins = 1; jzbMin = jzbSel*0.95; jzbMax = jzbSel*1.05;
503     TH1F* hist = plotEff(events,(kbase&&ksel&&nJets),informalname);
504    
505     TH1F* histp = plotEff(events,(kbase&&ksel&&nJetsP),informalname);
506    
507     TH1F* histm = plotEff(events,(kbase&&ksel&&nJetsM),informalname);
508    
509     // Dump some information
510     Float_t eff = Interpolate(jzbSel,hist);
511     Float_t effp = Interpolate(jzbSel,histp);
512     Float_t effm = Interpolate(jzbSel,histm);
513 buchmann 1.5 if(!automatized) dout << " Efficiency at JZB==" << jzbSel << std::endl;
514     if(!automatized) dout << " JESup: " << effp << " (" << (effp-eff)/eff*100. << "%)" << std::endl;
515     if(!automatized) dout << " central: " << eff << std::endl;
516     if(!automatized) dout << " JESdown: " << effm << " (" << (effm-eff)/eff*100. << "%)" << std::endl;
517 buchmann 1.18 jesup=(effp-eff)/eff;
518     jesdown=(effm-eff)/eff;
519 buchmann 1.1 }
520    
521     //________________________________________________________________________
522     // Effect of energy scale on JZB efficiency
523 buchmann 1.10 void doJZBscale(TTree *events, float &down, float &up, float &syst, float systematic, string informalname, bool requireZ, string addcut) {
524 buchmann 1.1
525 buchmann 1.42 TCut kbase(genMassCut&&"genZPt>0&&genNjets>2");
526 buchmann 1.10 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
527 buchmann 1.42 flag_this_change(__FUNCTION__,__LINE__,true);//PlottingSetup::RestrictToMassPeak
528     if(requireZ&&PlottingSetup::RestrictToMassPeak) kbase=kbase&&"TMath::Abs(genMID)==23";
529     TCut ksel(cutmass&&cutOSSF);
530 buchmann 1.1
531     nBins = 50;
532     jzbMin = 0.5*jzbSel;
533     jzbMax = 2.0*jzbSel;
534    
535     TH1F* hist = plotEff(events,kbase&&ksel,informalname);
536    
537     // Dump some information
538     Float_t eff = Interpolate(jzbSel,hist);
539     Float_t effp = Interpolate(jzbSel*(1.+systematic),hist);
540     Float_t effm = Interpolate(jzbSel*(1.-systematic),hist);
541 buchmann 1.33 if(!automatized) dout << " efficiency at JZB==" << jzbSel*(1.+systematic) << "(-"<<systematic*100<<"%) : " << effp << " (" << ((effp-eff)/eff)*100. << "%)" << std::endl;
542 buchmann 1.5 if(!automatized) dout << " efficiency at JZB==" << jzbSel << ": " << eff << std::endl;
543 buchmann 1.33 if(!automatized) dout << " efficiency at JZB==" << jzbSel*(1.-systematic) << "(-"<<systematic*100<<"%) : " << effm << " (" << ((effm-eff)/eff)*100. << "%)" << std::endl;
544 buchmann 1.18 up=((effp-eff)/eff);
545     down=((effm-eff)/eff);
546 buchmann 1.1 }
547    
548     //________________________________________________________________________
549     // JZB response (true/reco. vs. true)
550 buchmann 1.10 void JZBresponse(TTree *events, bool requireZ, float &resp, float &resperr, string addcut="",bool isMET = kFALSE, Float_t myJzbMax = 200., Int_t nPeriods = 9 ) {
551 buchmann 1.1
552     jzbMin = 20;
553 buchmann 1.42 flag_this_change(__FUNCTION__,__LINE__,false);//PlottingSetup::RestrictToMassPeak
554     TCut kbase(genMassCut&&"genZPt>0&&genNjets>2");
555 buchmann 1.10 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
556 buchmann 1.42 flag_this_change(__FUNCTION__,__LINE__,false);//PlottingSetup::RestrictToMassPeak
557     if(requireZ&&PlottingSetup::RestrictToMassPeak) kbase=kbase&&"TMath::Abs(genMID)==23";
558     flag_this_change(__FUNCTION__,__LINE__,false);//PlottingSetup::RestrictToMassPeak
559     TCut ksel(cutmass&&cutOSSF);
560 buchmann 1.1
561 buchmann 1.8 TProfile* hJzbResp = new TProfile("hJzbResp","JZB response ; JZB true (GeV/c); JZB reco. / JZB true", nPeriods, jzbMin, myJzbMax, "" );
562 buchmann 1.1
563 buchmann 1.6 if (!isMET) events->Project("hJzbResp","("+TString(mcjzbexpression)+")/genJZB:genJZB",kbase&&ksel);
564 buchmann 1.1 else events->Project("hJzbResp","met[4]/genMET:genMET",kbase&&ksel);
565    
566     hJzbResp->SetMaximum(1.2);
567     hJzbResp->SetMinimum(0.2);
568     hJzbResp->Fit("pol0","Q");
569     TF1 *fittedfunction = hJzbResp->GetFunction("pol0");
570 buchmann 1.32 if(!fittedfunction) {
571     // in case there are not enough points passing our selection
572     cout << "OOPS response function invalid, assuming 100% error !!!!" << endl;
573     resp=1;
574     resperr=1;
575     } else {
576     resp=fittedfunction->GetParameter(0);
577     resperr=fittedfunction->GetParError(0);
578     if(!automatized) dout << " Response: " << resp << " +/- " << resperr << endl;
579     }
580 buchmann 1.5 delete hJzbResp;
581 buchmann 1.1 }
582    
583    
584 pablom 1.23 //________________________________________________________________________________________
585     // PDF uncertainty
586 buchmann 1.24 float get_pdf_uncertainty(TTree *events, string mcjzb, bool requireZ, int Neventsinfile, int NPdfs, string addcut="") {
587 pablom 1.23 std::vector<float> efficiency;
588 buchmann 1.24 for(int k = 1; k < NPdfs; k++) {
589 pablom 1.23 float result, resulterr;
590     MCefficiency(events, result, resulterr, mcjzb, requireZ, Neventsinfile, addcut, k);
591     efficiency.push_back(result);
592     }
593 buchmann 1.24 float errHi, errLow,err;
594 pablom 1.23 master_formula(efficiency, errHi, errLow);
595 buchmann 1.24 err=errLow;
596     if(errHi>errLow) err=errHi;
597     if(!automatized) dout << " Uncertainty from PDF: " << errLow << " (low) and " << errHi << "(high) ---> Picked " << err << endl;
598     return err;
599 pablom 1.23
600     }
601    
602 buchmann 1.24 int get_npdfs(TTree *events) {
603     int NPDFs;
604     events->SetBranchAddress("NPdfs",&NPDFs);
605     events->GetEntry(1);
606     return NPDFs;
607     }
608    
609 pablom 1.23
610 buchmann 1.22 void do_systematics_for_one_file(TTree *events,int Neventsinfile,string informalname, vector<vector<float> > &results,string mcjzb,string datajzb,float peakerror,bool requireZ=false, string addcut="", bool ismSUGRA=false) {
611 buchmann 1.1 float JetEnergyScaleUncert=0.1;
612     float JZBScaleUncert=0.1;
613 buchmann 1.2 mcjzbexpression=mcjzb;
614 buchmann 1.28 float triggereff=5.0/100;// in range [0,1]
615 buchmann 1.5 dout << "Trigger efficiency not implemented in this script yet, still using external one" << endl;
616 buchmann 1.18 float leptonseleff=2.0/100;// in range [0,1]
617 buchmann 1.32 leptonseleff=TMath::Sqrt(leptonseleff*leptonseleff+leptonseleff*leptonseleff); // because the 2% is per lepton
618 buchmann 1.5 dout << "Lepton selection efficiency not implemented in this script yet, still using external one" << endl;
619 buchmann 1.1
620 buchmann 1.27 int NPdfs=0;
621     if(ismSUGRA) NPdfs = get_npdfs(events);
622 buchmann 1.29
623 buchmann 1.8 float mceff,mcefferr,jzbeff,jzbefferr;
624 buchmann 1.5 if(!automatized) dout << "MC efficiencies:" << endl;
625 buchmann 1.34 Value mceff_nosigcont = MCefficiency(events,mceff,mcefferr,mcjzb,requireZ,Neventsinfile,addcut,-1);
626 buchmann 1.35 if(!automatized) cout << " Without signal contamination, we find an efficiency of " << mceff_nosigcont << endl;
627    
628 buchmann 1.32 if(PlottingSetup::computeJZBefficiency) JZBefficiency(events,informalname,jzbeff,jzbefferr,requireZ,addcut);
629 buchmann 1.8 if(!automatized) dout << "JZB efficiency: " << jzbeff << "+/-" << jzbefferr << endl;
630 buchmann 1.1
631 buchmann 1.6 if(!automatized) dout << "Error from Peak position:" << endl;
632     float sysfrompeak=0;
633 buchmann 1.42 PeakError(events,sysfrompeak,mcjzb,peakerror,addcut);
634 buchmann 1.6
635 buchmann 1.5 if(!automatized) dout << "Jet energy scale: " << std::endl;
636 buchmann 1.42 float jesup,jesdown;
637     JZBjetScale(events,jesdown,jesup,informalname,requireZ,addcut,JetEnergyScaleUncert);
638 buchmann 1.1
639 buchmann 1.5 if(!automatized) dout << "JZB scale: " << std::endl;
640 buchmann 1.42 float scaleup,scaledown,scalesyst;
641     doJZBscale(events,scaledown,scaleup,scalesyst,JZBScaleUncert,informalname,requireZ,addcut);
642 buchmann 1.1
643 buchmann 1.5 if(!automatized) dout << "JZB response: " << std::endl;
644 buchmann 1.42 float resp,resperr;
645     if(PlottingSetup::computeJZBresponse) {
646 buchmann 1.32 if(!automatized) dout << "JZB response: " << std::endl;
647     JZBresponse(events,requireZ,resp,resperr,addcut);
648     }
649 buchmann 1.1
650 buchmann 1.5 if(!automatized) dout << "Pileup: " << std::endl;
651 buchmann 1.42 float resolution;
652     resolution=pileup(events,requireZ,informalname,addcut);
653 pablom 1.23
654 buchmann 1.22 float PDFuncert=0;
655 buchmann 1.42 if(!automatized) dout << "Assessing PDF uncertainty: " << std::endl;
656     if(ismSUGRA) PDFuncert = get_pdf_uncertainty(events, mcjzb, requireZ, Neventsinfile, NPdfs, addcut);
657 pablom 1.23
658 buchmann 1.5 dout << "_______________________________________________" << endl;
659 buchmann 1.10 dout << " SUMMARY FOR " << informalname << " with JZB>" << jzbSel << " (all in %) ";
660     if(addcut!="") dout << "With additional cut: " << addcut;
661     dout << endl;
662 buchmann 1.18 dout << "MC efficiency: " << mceff << "+/-" << mcefferr << endl; // in range [0,1]
663     dout << "Trigger efficiency: " << triggereff << endl; // in range [0,1]
664     dout << "Lepton Sel Eff: " << leptonseleff << endl; // in range [0,1]
665     dout << "Jet energy scale: " << jesup << " " << jesdown << endl; // in range [0,1]
666     dout << "JZB Scale Uncert: " << scaledown << " " << scaleup << endl; // in range [0,1]
667     dout << "Resolution : " << resolution << endl; // in range [0,1]
668     dout << "From peak : " << sysfrompeak << endl; // in range [0,1]
669 buchmann 1.24 if(ismSUGRA) dout << "PDF uncertainty : " << PDFuncert << endl; // in range [0,1]
670 buchmann 1.32 if(PlottingSetup::computeJZBefficiency) dout << "JZB efficiency: " << jzbeff << "+/-" << jzbefferr << " (not yet included below) " << endl; // in range [0,1]
671     if(PlottingSetup::computeJZBresponse)dout << "JZB response : " << resp << " +/-" << resperr << " (not yet included below) " << endl; // in range [0,1]
672 buchmann 1.1
673 buchmann 1.4 float toterr=0;
674 buchmann 1.18 toterr+=(triggereff)*(triggereff);
675     toterr+=(leptonseleff)*(leptonseleff);
676     if(fabs(jesup)>fabs(jesdown)) toterr+=(jesup*jesup); else toterr+=(jesdown*jesdown);
677     if(fabs(scaleup)>fabs(scaledown)) toterr+=(scaleup*scaleup); else toterr+=(scaledown*scaledown);
678     toterr+=(resolution*resolution);
679     toterr+=(sysfrompeak*sysfrompeak);
680 buchmann 1.22 if(ismSUGRA) toterr+=(PDFuncert*PDFuncert);
681 buchmann 1.18 dout << "TOTAL SYSTEMATICS: " << TMath::Sqrt(toterr) << " --> " << TMath::Sqrt(toterr)*mceff << endl;
682 buchmann 1.19 float systerr=TMath::Sqrt(toterr)*mceff;
683     toterr=TMath::Sqrt(toterr*mceff*mceff+mcefferr*mcefferr);//also includes stat err!
684    
685     dout << "FINAL RESULT : " << 100*mceff << " +/- "<< 100*mcefferr << " (stat) +/- " << 100*systerr << " (syst) %" << endl;
686     dout << " we thus use the sqrt of the sum of the squares of the stat & syst err, which is : " << 100*toterr << endl;
687 buchmann 1.36 dout << "_______________________________________________" << endl;
688 buchmann 1.19
689     //Do not modify the lines below or mess with the order; this order is expected by all limit calculating functions!
690 buchmann 1.4 vector<float> res;
691     res.push_back(jzbSel);
692     res.push_back(mceff);
693     res.push_back(mcefferr);
694     res.push_back(toterr);
695     res.push_back(TMath::Sqrt((mcefferr)*(mcefferr)+(toterr*toterr)));
696 buchmann 1.18 if(fabs(jesup)>fabs(jesdown)) res.push_back(fabs(jesup)); else res.push_back(fabs(jesdown));
697     if(fabs(scaleup)>fabs(scaledown)) res.push_back(fabs(scaleup)); else res.push_back(fabs(scaledown));
698     res.push_back(fabs(resolution));
699 buchmann 1.35 res.push_back(mceff_nosigcont.getValue());
700     res.push_back(mceff_nosigcont.getError());
701     if(ismSUGRA) res.push_back(PDFuncert);
702 buchmann 1.34 results.push_back(res);
703 buchmann 1.1 }
704    
705 buchmann 1.8 vector<vector<float> > compute_systematics(string mcjzb, float mcpeakerror, string datajzb, samplecollection &signalsamples, vector<float> bins, bool requireZ=false) {
706 buchmann 1.4 automatized=true;
707     vector< vector<float> > systematics;
708 buchmann 1.1 for (int isignal=0; isignal<signalsamples.collection.size();isignal++) {
709 buchmann 1.5 dout << "Looking at signal " << (signalsamples.collection)[isignal].filename << endl;
710 buchmann 1.1 for(int ibin=0;ibin<bins.size();ibin++) {
711     jzbSel=bins[ibin];
712 buchmann 1.2 geqleq="geq";
713 buchmann 1.13 do_systematics_for_one_file((signalsamples.collection)[isignal].events,(signalsamples.collection)[isignal].Nentries,(signalsamples.collection)[isignal].samplename,systematics,mcjzb,datajzb,mcpeakerror,requireZ);
714 buchmann 1.1 }//end of bin loop
715     }//end of signal loop
716 buchmann 1.4 return systematics;
717 buchmann 1.1 }