ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/Systematics.C
Revision: 1.45
Committed: Mon Nov 7 15:08:39 2011 UTC (13 years, 6 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.44: +81 -49 lines
Log Message:
Updated Systematics to allow 'flipping' (BZJ)

File Contents

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