ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/Systematics.C
(Generate patch)

Comparing UserCode/cbrown/AnalysisFramework/Plotting/Modules/Systematics.C (file contents):
Revision 1.1 by buchmann, Fri Jul 15 10:40:43 2011 UTC vs.
Revision 1.32 by buchmann, Tue Sep 20 12:17:24 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines