ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/Systematics.C
Revision: 1.24
Committed: Wed Aug 31 11:34:16 2011 UTC (13 years, 8 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.23: +34 -18 lines
Log Message:
Adapted PDF uncertainty; this is note definitive yet, only a first study

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 buchmann 1.7 //____________________________________________________________________________________
85     // Plotting with all contributions, i.e. sidebands, peak, osof,ossf ... (for a systematic)
86     float allcontributionsplot(TTree* events, TCut kBaseCut, TCut kMassCut, TCut kSidebandCut, TCut JZBPosCut, TCut JZBNegCut) {
87     iplot++;
88     int count=iplot;
89     // Define new histogram
90     string hname=GetNumericHistoName();
91     TH1F* hossfp = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
92     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kMassCut&&JZBPosCut&&cutOSSF,"goff");
93     hname=GetNumericHistoName();
94     TH1F* hossfn = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
95     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kMassCut&&JZBNegCut&&cutOSSF,"goff");
96    
97     hname=GetNumericHistoName();
98     TH1F* hosofp = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
99     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kMassCut&&JZBPosCut&&cutOSOF,"goff");
100     hname=GetNumericHistoName();
101     TH1F* hosofn = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
102     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kMassCut&&JZBNegCut&&cutOSOF,"goff");
103    
104     hname=GetNumericHistoName();
105     TH1F* sbhossfp = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
106     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kSidebandCut&&JZBPosCut&&cutOSSF,"goff");
107     hname=GetNumericHistoName();
108     TH1F* sbhossfn = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
109     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kSidebandCut&&JZBNegCut&&cutOSSF,"goff");
110    
111     hname=GetNumericHistoName();
112     TH1F* sbhosofp = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
113     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kSidebandCut&&JZBPosCut&&cutOSOF,"goff");
114     hname=GetNumericHistoName();
115     TH1F* sbhosofn = new TH1F(hname.c_str(),hname.c_str(),1,-14000,14000);
116     events->Draw(TString(mcjzbexpression)+">>"+TString(hname),kBaseCut&&kSidebandCut&&JZBNegCut&&cutOSOF,"goff");
117    
118     float obs = hossfp->Integral();
119     float pred= hossfn->Integral() + (1.0/3)*( hosofp->Integral() - hosofn->Integral() + sbhossfp->Integral() - sbhossfn->Integral() + sbhosofp->Integral() - sbhosofn->Integral());
120    
121     delete hossfp,hossfn,hosofp,hosofn;
122     delete sbhossfp,sbhossfn,sbhosofp,sbhosofn;
123     return obs-pred;
124     }
125    
126 buchmann 1.1
127     //____________________________________________________________________________________
128     // Efficiency plot
129     TH1F* plotEff(TTree* events, TCut kbase, TString informalname) {
130     iplot++;
131     int count=iplot;
132     // Define new histogram
133     char hname[30]; sprintf(hname,"hJzbEff%d",count);
134     TH1F* hJzbEff = new TH1F(hname,"JZB selection efficiency ; JZB (GeV/c); Efficiency",
135     nBins,jzbMin,jzbMax);
136     Float_t step = (jzbMax-jzbMin)/static_cast<Float_t>(nBins);
137    
138 buchmann 1.6 events->Draw(mcjzbexpression.c_str(),"genJZB>-400"&&kbase,"goff");
139 buchmann 1.1 Float_t maxEff = events->GetSelectedRows();
140 buchmann 1.5 if(verbose>0) dout << hname << " (" << informalname <<") " << maxEff << std::endl;
141 buchmann 1.1
142 buchmann 1.5 if(verbose>0) dout << "JZB max = " << jzbMax << std::endl;
143 buchmann 1.1 // Loop over steps to get efficiency curve
144     char cut[256];
145     for ( Int_t iBin = 0; iBin<nBins; ++iBin ) {
146 buchmann 1.6 sprintf(cut,"genJZB>%3f",jzbMin+iBin*step);
147 buchmann 1.2 events->Draw(mcjzbexpression.c_str(),TCut(cut)&&kbase,"goff");
148 buchmann 1.1 Float_t eff = static_cast<Float_t>(events->GetSelectedRows())/maxEff;
149 buchmann 1.5 // dout << "COUCOU " << __LINE__ << std::endl;
150 buchmann 1.1 hJzbEff->SetBinContent(iBin+1,eff);
151     hJzbEff->SetBinError(iBin+1,TMath::Sqrt(eff*(1-eff)/maxEff));
152     }
153     return hJzbEff;
154    
155    
156     }
157    
158    
159     //________________________________________________________________________________________
160 pablom 1.23 // Master Formula
161     void master_formula(std::vector<float> eff, float &errHi, float &errLo) {
162    
163     float x0 = eff[0];
164     float deltaPos = 0, deltaNeg = 0;
165     for(int k = 0; k < (eff.size()-1)/2; k++) {
166     float xneg = eff[2*k+2];
167     float xpos = eff[2*k+1];
168     if(xpos-x0>0 || xneg-x0>0) {
169     if(xpos-x0 > xneg-x0) {
170     deltaPos += (xpos-x0)*(xpos-x0);
171     } else {
172     deltaPos += (xneg-x0)*(xneg-x0);
173     }
174     }
175     if(x0-xpos>0 || x0-xneg>0) {
176     if(x0-xpos > x0-xneg) {
177     deltaNeg += (xpos-x0)*(xpos-x0);
178     } else {
179     deltaNeg += (xneg-x0)*(xneg-x0);
180     }
181     }
182     }
183     errHi = sqrt(deltaPos);
184     errLo = sqrt(deltaNeg);
185    
186     }
187    
188    
189     //________________________________________________________________________________________
190     // Get normalization factor for the PDFs
191     float get_norm_pdf_factor(TTree *events, int k) {
192    
193 buchmann 1.24 TH1F *haux = new TH1F("haux", "", 1, 0, 20);
194 pablom 1.23 char nameVar[20];
195     sprintf(nameVar, "pdfW[%d]", k);
196     events->Project("haux", nameVar);
197 buchmann 1.24 float thisW = haux->Integral();
198     events->Project("haux", "pdfW[0]");
199     float normW = haux->Integral();
200    
201     float factor=thisW/normW;
202 pablom 1.23
203     delete haux;
204    
205     return factor;
206    
207     }
208    
209    
210    
211     //________________________________________________________________________________________
212 buchmann 1.1 // Pile-up efficiency
213 buchmann 1.10 float pileup(TTree *events, bool requireZ, string informalname, string addcut="",Float_t myJzbMax = 140. ) {
214 buchmann 1.1 nBins = 16;
215     jzbMax = myJzbMax;
216    
217     // Acceptance cuts
218 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");
219 buchmann 1.10 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
220    
221 buchmann 1.8 if(requireZ) kbase=kbase&&"TMath::Abs(genMID)==23";
222 buchmann 1.1 TH1F* hLM4 = plotEff(events,kbase,informalname);
223     hLM4->SetMinimum(0.);
224    
225     // Nominal function
226     TF1* func = new TF1("func","0.5*TMath::Erfc([0]*x-[1])",jzbMin,jzbMax);
227     func->SetParameter(0,0.03);
228     func->SetParameter(1,0.);
229     hLM4->Fit(func,"Q");
230    
231     // Pimped-up function
232     TF1* funcUp = (TF1*)func->Clone();
233     funcUp->SetParameter( 0., func->GetParameter(0)/1.1); // 10% systematic error (up in sigma => 0.1 in erfc)
234 buchmann 1.5 if(!automatized) dout << " PU: " << funcUp->Eval(jzbSel) << " " << func->Eval(jzbSel)
235 buchmann 1.1 << "(" << (funcUp->Eval(jzbSel)-func->Eval(jzbSel))/func->Eval(jzbSel)*100. << "%)" << std::endl;
236    
237 buchmann 1.18 return (funcUp->Eval(jzbSel)-func->Eval(jzbSel))/func->Eval(jzbSel);
238 buchmann 1.1
239     }
240    
241     //____________________________________________________________________________________
242 buchmann 1.6 // Effect of peak shifting
243 buchmann 1.10 void PeakError(TTree *events,float &result, string mcjzb, float peakerr,string addcut="") {
244 buchmann 1.6 TString peakup("("+TString(mcjzb)+"+"+TString(any2string(TMath::Abs(peakerr)))+")"+geq_or_leq()+TString(any2string(jzbSel)));
245     TString peakdown("("+TString(mcjzb)+"-"+TString(any2string(TMath::Abs(peakerr)))+")"+geq_or_leq()+TString(any2string(jzbSel)));
246     TString peakcentral("("+TString(mcjzb)+")"+geq_or_leq()+TString(any2string(jzbSel)));
247     TString npeakup("("+TString(mcjzb)+"+"+TString(any2string(TMath::Abs(peakerr)))+")"+ngeq_or_leq()+"-"+TString(any2string(jzbSel)));
248     TString npeakdown("("+TString(mcjzb)+"-"+TString(any2string(TMath::Abs(peakerr)))+")"+ngeq_or_leq()+"-"+TString(any2string(jzbSel)));
249     TString npeakcentral("("+TString(mcjzb)+")"+ngeq_or_leq()+"-"+TString(any2string(jzbSel)));
250    
251     nBins = 1;
252     string informalname="PeakErrorCalculation";
253     float resup,resdown,rescent;
254     for(int i=0;i<3;i++) {
255     string poscut,negcut;
256     if(i==0) {
257     poscut=peakcentral;
258     negcut=npeakcentral;
259     } else if(i==1) {
260     poscut=peakdown;
261     negcut=npeakdown;
262     } else if(i==2) {
263     poscut=peakup;
264     negcut=npeakup;
265     }
266 buchmann 1.10 float res;
267     if(addcut=="") res=allcontributionsplot(events,cutnJets,cutmass,sidebandcut,poscut.c_str(),negcut.c_str());
268     else res=allcontributionsplot(events,cutnJets&&addcut.c_str(),cutmass,sidebandcut,poscut.c_str(),negcut.c_str());
269 buchmann 1.7 if(i==0) rescent=res;
270     else if(i==1) resdown=res;
271     else if(i==2) resup=res;
272 buchmann 1.6 }
273 buchmann 1.18 if(TMath::Abs(rescent-resup)>TMath::Abs(rescent-resdown)) result=(TMath::Abs(rescent-resup)/rescent);
274     else result=(TMath::Abs(rescent-resdown)/rescent);
275 buchmann 1.6 }
276    
277     //____________________________________________________________________________________
278 buchmann 1.1 // Total selection efficiency (MC)
279 pablom 1.23 void MCefficiency(TTree *events,float &result, float &resulterr,string mcjzb,bool requireZ,int Neventsinfile, string addcut="", int k = 0) {
280 buchmann 1.1
281     char jzbSelStr[256]; sprintf(jzbSelStr,"%f",jzbSel);
282     // All acceptance cuts at gen. level
283 buchmann 1.14 //TCut kbase("abs(genMll-91.2)<20&&genNjets>2&&genZPt>0&&genJZB"+geq_or_leq()+TString(jzbSelStr)+"&&genId1==-genId2");
284     TCut kbase("");
285 buchmann 1.8 if(requireZ) kbase=kbase&&"TMath::Abs(genMID)==23";
286 buchmann 1.9 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
287 buchmann 1.1 // Corresponding reco. cuts
288 buchmann 1.11 TCut ksel("pfJetGoodNum>2&&abs(mll-91.2)<20&&id1==id2&&"+TString(mcjzb)+geq_or_leq()+TString(jzbSelStr));
289 buchmann 1.15 TCut ksel2("pfJetGoodNum>2&&abs(mll-91.2)<20&&id1==id2&&"+TString(mcjzb)+ngeq_or_leq()+TString("-")+TString(jzbSelStr));
290 pablom 1.23 TCut posSide = kbase&&ksel;
291     TCut negSide = kbase&&ksel2;
292     string sposSide(posSide);
293     string snegSide(negSide);
294     char var[20];
295     sprintf(var, "pdfW[%d]", k);
296     string svar(var);
297     string newPosSide = "(" + sposSide + ")*" + svar;
298     string newNegSide = "(" + snegSide + ")*" + svar;
299    
300 buchmann 1.24 TH1F *effh= new TH1F("effh","effh",1,-14000,14000);
301     events->Draw((mcjzbexpression+">>effh").c_str(), newPosSide.c_str(),"goff");
302     Float_t sel = effh->Integral();
303 buchmann 1.21 Float_t nsel=0;
304     if(ConsiderSignalContaminationForLimits) {
305 buchmann 1.24 events->Draw((mcjzbexpression+">>effh").c_str(), newNegSide.c_str(),"goff");
306     nsel = effh->Integral();
307 buchmann 1.21 }
308 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.
309     float normFactor = get_norm_pdf_factor(events, k);
310     sel = sel/normFactor;
311     nsel = nsel/normFactor;
312    
313 buchmann 1.13 // events->Draw(mcjzbexpression.c_str(),kbase,"goff");
314     // Float_t tot = events->GetSelectedRows();
315     Float_t tot = Neventsinfile;
316 buchmann 1.1
317 buchmann 1.21 if(ConsiderSignalContaminationForLimits) {
318     result=(sel-nsel)/tot;
319     resulterr=(1.0/tot)*TMath::Sqrt(sel+nsel+(sel-nsel)*(sel-nsel)/tot);
320     } else {//no signal contamination considered:
321     result=(sel)/tot;
322     resulterr=TMath::Sqrt(sel/tot*(1+sel/tot)/tot);
323     }
324 buchmann 1.24 if(!automatized) dout << " MC efficiency: " << result << "+-" << resulterr << " ( JZB>" << jzbSel << " : " << sel << " , JZB<-" << jzbSel << " : " << nsel << " and nevents=" << tot << ") with normFact=" << normFactor << std::endl;
325     delete effh;
326 buchmann 1.1 }
327 buchmann 1.24 //Pablo: MC efficiency: 6.90539e-08+-6.73798e-06 ( JZB>100 : 0.000105031 , JZB<-100 : 0 and nevents=1521)
328     //corre: MC efficiency: 0.000657462+-0.000657678 ( JZB>100 : 1 , JZB<-100 : 0 and nevents=1521)
329    
330 buchmann 1.1
331 buchmann 1.10 void JZBefficiency(TTree *events, string informalname, float &jzbeff, float &jzbefferr, bool requireZ, string addcut="") {
332 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");
333 buchmann 1.10 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
334 buchmann 1.8 if(requireZ) kbase=kbase&&"TMath::Abs(genMID)==23";
335 buchmann 1.1 TH1F* hLM4 = plotEff(events,kbase,informalname);
336     Int_t bin = hLM4->FindBin(jzbSel); // To get the error
337 buchmann 1.8 jzbeff=Interpolate(jzbSel,hLM4);
338     jzbefferr=hLM4->GetBinError(bin);
339 buchmann 1.5 if(!automatized) dout << " Efficiency at JZB==" << jzbSel << std::endl;
340 buchmann 1.8 if(!automatized) dout << " " << jzbeff << "+-" << jzbefferr << std::endl;
341 buchmann 1.1 }
342    
343     //________________________________________________________________________
344     // Effect of energy scale on efficiency
345 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 = "" ) {
346 buchmann 1.6 TCut kbase("abs(genMll-91.2)<20&&genZPt>0");
347 buchmann 1.10 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
348 buchmann 1.8 if(requireZ) kbase=kbase&&"TMath::Abs(genMID)==23";
349    
350 buchmann 1.1 TCut ksel("abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
351     TCut nJets("pfJetGoodNum>2");
352     stringstream down,up;
353     down << "pfJetGoodNum"<<30*(1-syst)<<">=3";
354     up << "pfJetGoodNum"<<30*(1+syst)<<">=3";
355    
356     TCut nJetsP(up.str().c_str());
357     TCut nJetsM(down.str().c_str());
358    
359     if ( !(plotName.Length()>1) ) plotName = informalname;
360    
361     nBins = 1; jzbMin = jzbSel*0.95; jzbMax = jzbSel*1.05;
362     TH1F* hist = plotEff(events,(kbase&&ksel&&nJets),informalname);
363    
364     TH1F* histp = plotEff(events,(kbase&&ksel&&nJetsP),informalname);
365    
366     TH1F* histm = plotEff(events,(kbase&&ksel&&nJetsM),informalname);
367    
368     // Dump some information
369     Float_t eff = Interpolate(jzbSel,hist);
370     Float_t effp = Interpolate(jzbSel,histp);
371     Float_t effm = Interpolate(jzbSel,histm);
372 buchmann 1.5 if(!automatized) dout << " Efficiency at JZB==" << jzbSel << std::endl;
373     if(!automatized) dout << " JESup: " << effp << " (" << (effp-eff)/eff*100. << "%)" << std::endl;
374     if(!automatized) dout << " central: " << eff << std::endl;
375     if(!automatized) dout << " JESdown: " << effm << " (" << (effm-eff)/eff*100. << "%)" << std::endl;
376 buchmann 1.18 jesup=(effp-eff)/eff;
377     jesdown=(effm-eff)/eff;
378 buchmann 1.1 }
379    
380     //________________________________________________________________________
381     // Effect of energy scale on JZB efficiency
382 buchmann 1.10 void doJZBscale(TTree *events, float &down, float &up, float &syst, float systematic, string informalname, bool requireZ, string addcut) {
383 buchmann 1.1
384 buchmann 1.6 TCut kbase("abs(genMll-91.2)<20&&genZPt>0&&genNjets>2");
385 buchmann 1.10 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
386 buchmann 1.8 if(requireZ) kbase=kbase&&"TMath::Abs(genMID)==23";
387 buchmann 1.1 TCut ksel("abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
388    
389     nBins = 50;
390     jzbMin = 0.5*jzbSel;
391     jzbMax = 2.0*jzbSel;
392    
393     TH1F* hist = plotEff(events,kbase&&ksel,informalname);
394    
395     // Dump some information
396     Float_t eff = Interpolate(jzbSel,hist);
397     Float_t effp = Interpolate(jzbSel*(1.+systematic),hist);
398     Float_t effm = Interpolate(jzbSel*(1.-systematic),hist);
399 buchmann 1.5 if(!automatized) dout << " efficiency at JZB==" << jzbSel*(1.+systematic) << "(-"<<syst*100<<"%) : " << effp << " (" << ((effp-eff)/eff)*100. << "%)" << std::endl;
400     if(!automatized) dout << " efficiency at JZB==" << jzbSel << ": " << eff << std::endl;
401     if(!automatized) dout << " efficiency at JZB==" << jzbSel*(1.-systematic) << "(-"<<syst*100<<"%) : " << effm << " (" << ((effm-eff)/eff)*100. << "%)" << std::endl;
402 buchmann 1.18 up=((effp-eff)/eff);
403     down=((effm-eff)/eff);
404 buchmann 1.1 }
405    
406     //________________________________________________________________________
407     // JZB response (true/reco. vs. true)
408 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 ) {
409 buchmann 1.1
410     jzbMin = 20;
411 buchmann 1.6 TCut kbase("abs(genMll-91.2)<20&&genZPt>0&&genNjets>2");
412 buchmann 1.10 if(addcut!="") kbase=kbase&&addcut.c_str();//this is mostly for SUSY scans (adding requirements on masses)
413 buchmann 1.8 if(requireZ) kbase=kbase&&"TMath::Abs(genMID)==23";
414 buchmann 1.1 TCut ksel("abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
415    
416 buchmann 1.8 TProfile* hJzbResp = new TProfile("hJzbResp","JZB response ; JZB true (GeV/c); JZB reco. / JZB true", nPeriods, jzbMin, myJzbMax, "" );
417 buchmann 1.1
418 buchmann 1.6 if (!isMET) events->Project("hJzbResp","("+TString(mcjzbexpression)+")/genJZB:genJZB",kbase&&ksel);
419 buchmann 1.1 else events->Project("hJzbResp","met[4]/genMET:genMET",kbase&&ksel);
420    
421     hJzbResp->SetMaximum(1.2);
422     hJzbResp->SetMinimum(0.2);
423     hJzbResp->Fit("pol0","Q");
424     TF1 *fittedfunction = hJzbResp->GetFunction("pol0");
425 buchmann 1.8 resp=fittedfunction->GetParameter(0);
426     resperr=fittedfunction->GetParError(0);
427     if(!automatized) dout << " Response: " << resp << " +/- " << resperr << endl;
428 buchmann 1.5 delete hJzbResp;
429 buchmann 1.1 }
430    
431    
432 pablom 1.23 //________________________________________________________________________________________
433     // PDF uncertainty
434 buchmann 1.24 float get_pdf_uncertainty(TTree *events, string mcjzb, bool requireZ, int Neventsinfile, int NPdfs, string addcut="") {
435 pablom 1.23 std::vector<float> efficiency;
436 buchmann 1.24 for(int k = 1; k < NPdfs; k++) {
437 pablom 1.23 float result, resulterr;
438     MCefficiency(events, result, resulterr, mcjzb, requireZ, Neventsinfile, addcut, k);
439     efficiency.push_back(result);
440     }
441 buchmann 1.24 float errHi, errLow,err;
442 pablom 1.23 master_formula(efficiency, errHi, errLow);
443 buchmann 1.24 err=errLow;
444     if(errHi>errLow) err=errHi;
445     if(!automatized) dout << " Uncertainty from PDF: " << errLow << " (low) and " << errHi << "(high) ---> Picked " << err << endl;
446     return err;
447 pablom 1.23
448     }
449    
450 buchmann 1.24 int get_npdfs(TTree *events) {
451     int NPDFs;
452     events->SetBranchAddress("NPdfs",&NPDFs);
453     events->GetEntry(1);
454     return NPDFs;
455     }
456    
457 pablom 1.23
458 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) {
459 buchmann 1.1 float JetEnergyScaleUncert=0.1;
460     float JZBScaleUncert=0.1;
461 buchmann 1.2 mcjzbexpression=mcjzb;
462 buchmann 1.18 float triggereff=4.0/100;// in range [0,1]
463 buchmann 1.5 dout << "Trigger efficiency not implemented in this script yet, still using external one" << endl;
464 buchmann 1.18 float leptonseleff=2.0/100;// in range [0,1]
465 buchmann 1.5 dout << "Lepton selection efficiency not implemented in this script yet, still using external one" << endl;
466 buchmann 1.1
467 buchmann 1.24 int NPdfs=get_npdfs(events);
468    
469 buchmann 1.8 float mceff,mcefferr,jzbeff,jzbefferr;
470 buchmann 1.5 if(!automatized) dout << "MC efficiencies:" << endl;
471 buchmann 1.13 MCefficiency(events,mceff,mcefferr,mcjzb,requireZ,Neventsinfile,addcut);
472 buchmann 1.10 JZBefficiency(events,informalname,jzbeff,jzbefferr,requireZ,addcut);
473 buchmann 1.8 if(!automatized) dout << "JZB efficiency: " << jzbeff << "+/-" << jzbefferr << endl;
474 buchmann 1.1
475 buchmann 1.6 if(!automatized) dout << "Error from Peak position:" << endl;
476     float sysfrompeak=0;
477 buchmann 1.10 PeakError(events,sysfrompeak,mcjzb,peakerror,addcut);
478 buchmann 1.6
479 buchmann 1.5 if(!automatized) dout << "Jet energy scale: " << std::endl;
480 buchmann 1.1 float jesup,jesdown;
481 buchmann 1.10 JZBjetScale(events,jesdown,jesup,informalname,requireZ,addcut,JetEnergyScaleUncert);
482 buchmann 1.1
483 buchmann 1.5 if(!automatized) dout << "JZB scale: " << std::endl;
484 buchmann 1.1 float scaleup,scaledown,scalesyst;
485 buchmann 1.10 doJZBscale(events,scaledown,scaleup,scalesyst,JZBScaleUncert,informalname,requireZ,addcut);
486 buchmann 1.1
487 buchmann 1.5 if(!automatized) dout << "JZB response: " << std::endl;
488 buchmann 1.8 float resp,resperr;
489 buchmann 1.10 JZBresponse(events,requireZ,resp,resperr,addcut);
490 buchmann 1.1
491 buchmann 1.5 if(!automatized) dout << "Pileup: " << std::endl;
492 buchmann 1.10 float resolution=pileup(events,requireZ,informalname,addcut);
493 pablom 1.23
494 buchmann 1.22 float PDFuncert=0;
495 buchmann 1.24 if(ismSUGRA) PDFuncert = get_pdf_uncertainty(events, mcjzb, requireZ, Neventsinfile, NPdfs, addcut);
496 pablom 1.23
497 buchmann 1.5 dout << "_______________________________________________" << endl;
498 buchmann 1.10 dout << " SUMMARY FOR " << informalname << " with JZB>" << jzbSel << " (all in %) ";
499     if(addcut!="") dout << "With additional cut: " << addcut;
500     dout << endl;
501 buchmann 1.18 dout << "MC efficiency: " << mceff << "+/-" << mcefferr << endl; // in range [0,1]
502     dout << "Trigger efficiency: " << triggereff << endl; // in range [0,1]
503     dout << "Lepton Sel Eff: " << leptonseleff << endl; // in range [0,1]
504     dout << "Jet energy scale: " << jesup << " " << jesdown << endl; // in range [0,1]
505     dout << "JZB Scale Uncert: " << scaledown << " " << scaleup << endl; // in range [0,1]
506     dout << "Resolution : " << resolution << endl; // in range [0,1]
507     dout << "From peak : " << sysfrompeak << endl; // in range [0,1]
508 buchmann 1.24 if(ismSUGRA) dout << "PDF uncertainty : " << PDFuncert << endl; // in range [0,1]
509 buchmann 1.18 dout << "JZB efficiency: " << jzbeff << "+/-" << jzbefferr << " (not yet included below) " << endl; // in range [0,1]
510     dout << "JZB response : " << resp << " +/-" << resperr << " (not yet included below) " << endl; // in range [0,1]
511 buchmann 1.1
512 buchmann 1.4 float toterr=0;
513 buchmann 1.18 toterr+=(triggereff)*(triggereff);
514     toterr+=(leptonseleff)*(leptonseleff);
515     if(fabs(jesup)>fabs(jesdown)) toterr+=(jesup*jesup); else toterr+=(jesdown*jesdown);
516     if(fabs(scaleup)>fabs(scaledown)) toterr+=(scaleup*scaleup); else toterr+=(scaledown*scaledown);
517     toterr+=(resolution*resolution);
518     toterr+=(sysfrompeak*sysfrompeak);
519 buchmann 1.22 if(ismSUGRA) toterr+=(PDFuncert*PDFuncert);
520 buchmann 1.18 dout << "TOTAL SYSTEMATICS: " << TMath::Sqrt(toterr) << " --> " << TMath::Sqrt(toterr)*mceff << endl;
521 buchmann 1.19 float systerr=TMath::Sqrt(toterr)*mceff;
522     toterr=TMath::Sqrt(toterr*mceff*mceff+mcefferr*mcefferr);//also includes stat err!
523    
524     dout << "FINAL RESULT : " << 100*mceff << " +/- "<< 100*mcefferr << " (stat) +/- " << 100*systerr << " (syst) %" << endl;
525     dout << " we thus use the sqrt of the sum of the squares of the stat & syst err, which is : " << 100*toterr << endl;
526    
527     //Do not modify the lines below or mess with the order; this order is expected by all limit calculating functions!
528 buchmann 1.4 vector<float> res;
529     res.push_back(jzbSel);
530     res.push_back(mceff);
531     res.push_back(mcefferr);
532     res.push_back(toterr);
533     res.push_back(TMath::Sqrt((mcefferr)*(mcefferr)+(toterr*toterr)));
534 buchmann 1.18 if(fabs(jesup)>fabs(jesdown)) res.push_back(fabs(jesup)); else res.push_back(fabs(jesdown));
535     if(fabs(scaleup)>fabs(scaledown)) res.push_back(fabs(scaleup)); else res.push_back(fabs(scaledown));
536     res.push_back(fabs(resolution));
537 buchmann 1.22 if(ismSUGRA) res.push_back(PDFuncert);
538 buchmann 1.4 results.push_back(res);
539 buchmann 1.1 }
540    
541 buchmann 1.8 vector<vector<float> > compute_systematics(string mcjzb, float mcpeakerror, string datajzb, samplecollection &signalsamples, vector<float> bins, bool requireZ=false) {
542 buchmann 1.4 automatized=true;
543     vector< vector<float> > systematics;
544 buchmann 1.1 for (int isignal=0; isignal<signalsamples.collection.size();isignal++) {
545 buchmann 1.5 dout << "Looking at signal " << (signalsamples.collection)[isignal].filename << endl;
546 buchmann 1.1 for(int ibin=0;ibin<bins.size();ibin++) {
547     jzbSel=bins[ibin];
548 buchmann 1.2 geqleq="geq";
549 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);
550 buchmann 1.1 }//end of bin loop
551     }//end of signal loop
552 buchmann 1.4 return systematics;
553 buchmann 1.1 }