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.18 by buchmann, Wed Aug 17 17:07:18 2011 UTC vs.
Revision 1.36 by buchmann, Wed Sep 28 15:58:09 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 157 | 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, bool requireZ, string informalname, string addcut="",Float_t myJzbMax = 140. ) {
216          nBins = 16;
# Line 224 | Line 278 | void PeakError(TTree *events,float &resu
278  
279   //____________________________________________________________________________________
280   // Total selection efficiency (MC)
281 < void MCefficiency(TTree *events,float &result, float &resulterr,string mcjzb,bool requireZ,int Neventsinfile, string addcut="") {
281 > //returns the efficiency WITHOUT signal contamination, and the result and resulterr contain the result and the corresponding error
282 > Value MCefficiency(TTree *events,float &result, float &resulterr,string mcjzb,bool requireZ,int Neventsinfile, string addcut="", int k = 0) {
283 >        
284 >        if(!events) {
285 >          write_error(__FUNCTION__,"Tree passed for efficiency calculation is invalid!");
286 >          return Value(0,0);
287 >        }
288          
289          char jzbSelStr[256]; sprintf(jzbSelStr,"%f",jzbSel);
290          // All acceptance cuts at gen. level
# Line 235 | Line 295 | void MCefficiency(TTree *events,float &r
295          // Corresponding reco. cuts
296          TCut ksel("pfJetGoodNum>2&&abs(mll-91.2)<20&&id1==id2&&"+TString(mcjzb)+geq_or_leq()+TString(jzbSelStr));
297          TCut ksel2("pfJetGoodNum>2&&abs(mll-91.2)<20&&id1==id2&&"+TString(mcjzb)+ngeq_or_leq()+TString("-")+TString(jzbSelStr));
298 <        events->Draw(mcjzbexpression.c_str(),kbase&&ksel,"goff");
299 <        Float_t sel = events->GetSelectedRows();
300 <        events->Draw(mcjzbexpression.c_str(),kbase&&ksel2,"goff");
301 <        Float_t nsel = events->GetSelectedRows();
298 >        TCut posSide = kbase&&ksel;
299 >        TCut negSide = kbase&&ksel2;
300 >        string sposSide(posSide);
301 >        string snegSide(negSide);
302 >        char var[20];
303 >        sprintf(var, "pdfW[%d]", k);
304 >        string svar(var);
305 >        string newPosSide = "(" + sposSide + ")*" + svar;
306 >        string newNegSide = "(" + snegSide + ")*" + svar;
307 >
308 >        TH1F *effh= new TH1F("effh","effh",1,-14000,14000);
309 >        cout << "right before drawing" << endl;
310 >        if(k>=0)events->Draw((mcjzbexpression+">>effh").c_str(), newPosSide.c_str(),"");
311 >        else events->Draw((mcjzbexpression+">>effh").c_str(), sposSide.c_str(),"");
312 >        cout << "right after drawing" << endl;
313 >        cout << "consider sigcont?" << ConsiderSignalContaminationForLimits << endl;
314 >        Float_t sel = effh->Integral();
315 >        Float_t nsel=0;
316 >        if(ConsiderSignalContaminationForLimits) {
317 >          if(k>=0)events->Draw((mcjzbexpression+">>effh").c_str(), newNegSide.c_str(),"");
318 >          else events->Draw((mcjzbexpression+">>effh").c_str(), snegSide.c_str(),"");
319 >          nsel = effh->Integral();
320 >        }
321 >        //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.
322 >        float normFactor = 1;
323 >        if(k>=0) get_norm_pdf_factor(events, k);
324 >        sel = sel/normFactor;
325 >        nsel = nsel/normFactor;
326 >
327   //      events->Draw(mcjzbexpression.c_str(),kbase,"goff");
328   //      Float_t tot = events->GetSelectedRows();
329          Float_t tot = Neventsinfile;
330          
331 <        result=(sel-nsel)/tot;
332 <        resulterr=TMath::Sqrt(sel/tot*(1-sel/tot)/tot);
333 <        dout << "  MC efficiency: " << result << "+-" << resulterr << "  ( JZB>" << jzbSel << " : " << sel << " , JZB<-" << jzbSel << " : " << nsel << " and nevents=" << tot << ")" << std::endl;
331 >        Value result_wo_signalcont;
332 >
333 >        if(ConsiderSignalContaminationForLimits) {
334 >          result=(sel-nsel)/tot;
335 >          resulterr=(1.0/tot)*TMath::Sqrt(sel+nsel+(sel-nsel)*(sel-nsel)/tot);
336 >          result_wo_signalcont=Value(sel/tot,TMath::Sqrt(sel/tot*(1+sel/tot)/tot));
337 >        } else {//no signal contamination considered:
338 >          result=(sel)/tot;
339 >          resulterr=TMath::Sqrt(sel/tot*(1+sel/tot)/tot);
340 >          result_wo_signalcont=Value(result,resulterr);
341 >        }
342 >        if(!automatized && k>0 ) dout << "PDF assessment: ";
343 >        if(!automatized) dout << "  MC efficiency: " << result << "+-" << resulterr << "  ( JZB>" << jzbSel << " : " << sel << " , JZB<-" << jzbSel << " : " << nsel << " and nevents=" << tot << ") with normFact=" << normFactor << std::endl;
344 >        delete effh;
345 >        return result_wo_signalcont;
346 > }
347 >
348 >
349 > //____________________________________________________________________________________
350 > // Selection efficiency for one process (MC)
351 > vector<float> processMCefficiency(TTree *events,string mcjzb,bool requireZ,int Neventsinfile, string addcut) {
352 >  vector<float> process_efficiencies;
353 >  for(int iprocess=0;iprocess<=10;iprocess++) {
354 >    float this_process_efficiency,efferr;
355 >    stringstream addcutplus;
356 >    addcutplus<<addcut<<"&&(process=="<<iprocess<<")";
357 >    MCefficiency(events,this_process_efficiency, efferr,mcjzb,requireZ,Neventsinfile, addcutplus.str(),-1);
358 >    process_efficiencies.push_back(this_process_efficiency);
359 >  }
360 >  return process_efficiencies;
361   }
362 +        
363  
364   void JZBefficiency(TTree *events, string informalname, float &jzbeff, float &jzbefferr, bool requireZ, string addcut="") {
365          TCut kbase("abs(genMll-91.2)<20&&genNjets>2&&genZPt>0&&abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
253        cout << "Getting started with JZB efficiency" << endl;
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          TH1F* hLM4 = plotEff(events,kbase,informalname);
# Line 317 | Line 429 | void doJZBscale(TTree *events, float &do
429          Float_t eff  = Interpolate(jzbSel,hist);
430          Float_t effp = Interpolate(jzbSel*(1.+systematic),hist);
431          Float_t effm = Interpolate(jzbSel*(1.-systematic),hist);
432 <        if(!automatized) dout << "  efficiency at JZB==" << jzbSel*(1.+systematic)  << "(-"<<syst*100<<"%) : " << effp << " (" << ((effp-eff)/eff)*100. << "%)"  << std::endl;
432 >        if(!automatized) dout << "  efficiency at JZB==" << jzbSel*(1.+systematic)  << "(-"<<systematic*100<<"%) : " << effp << " (" << ((effp-eff)/eff)*100. << "%)"  << std::endl;
433          if(!automatized) dout << "  efficiency at JZB==" << jzbSel  << ": " << eff << std::endl;
434 <        if(!automatized) dout << "  efficiency at JZB==" << jzbSel*(1.-systematic)  << "(-"<<syst*100<<"%) : " << effm << " (" << ((effm-eff)/eff)*100. << "%)"  << std::endl;
434 >        if(!automatized) dout << "  efficiency at JZB==" << jzbSel*(1.-systematic)  << "(-"<<systematic*100<<"%) : " << effm << " (" << ((effm-eff)/eff)*100. << "%)"  << std::endl;
435          up=((effp-eff)/eff);
436          down=((effm-eff)/eff);
437   }
# Line 343 | Line 455 | void JZBresponse(TTree *events, bool req
455          hJzbResp->SetMinimum(0.2);
456          hJzbResp->Fit("pol0","Q");
457          TF1 *fittedfunction = hJzbResp->GetFunction("pol0");
458 <        resp=fittedfunction->GetParameter(0);
459 <        resperr=fittedfunction->GetParError(0);
460 <        if(!automatized) dout << "  Response: " << resp << " +/- " << resperr << endl;
458 >        if(!fittedfunction) {
459 >                // in case there are not enough points passing our selection
460 >                cout << "OOPS response function invalid, assuming 100% error !!!!" << endl;
461 >                resp=1;
462 >                resperr=1;
463 >        } else {
464 >                resp=fittedfunction->GetParameter(0);
465 >                resperr=fittedfunction->GetParError(0);
466 >                if(!automatized) dout << "  Response: " << resp << " +/- " << resperr << endl;
467 >        }
468          delete hJzbResp;
469   }
470  
471  
472 < 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="") {
472 > //________________________________________________________________________________________
473 > // PDF uncertainty  
474 > float get_pdf_uncertainty(TTree *events, string mcjzb, bool requireZ, int Neventsinfile, int NPdfs, string addcut="") {
475 >  std::vector<float> efficiency;
476 >  for(int k = 1; k < NPdfs; k++) {
477 >    float result, resulterr;
478 >    MCefficiency(events, result, resulterr, mcjzb, requireZ, Neventsinfile, addcut, k);  
479 >    efficiency.push_back(result);
480 >  }
481 >  float errHi, errLow,err;
482 >  master_formula(efficiency, errHi, errLow);
483 >  err=errLow;
484 >  if(errHi>errLow) err=errHi;
485 >  if(!automatized) dout << "  Uncertainty from PDF: " << errLow << " (low) and " << errHi << "(high) ---> Picked " << err << endl;
486 >  return err;
487 >
488 > }
489 >
490 > int get_npdfs(TTree *events) {
491 >  int NPDFs;
492 >  events->SetBranchAddress("NPdfs",&NPDFs);
493 >  events->GetEntry(1);
494 >  return NPDFs;
495 > }
496    
497 +
498 + 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) {
499    float JetEnergyScaleUncert=0.1;
500    float JZBScaleUncert=0.1;
501    mcjzbexpression=mcjzb;
502 <  float triggereff=4.0/100;// in range [0,1]
502 >  float triggereff=5.0/100;// in range [0,1]
503    dout << "Trigger efficiency not implemented in this script  yet, still using external one" << endl;
504    float leptonseleff=2.0/100;// in range [0,1]
505 +  leptonseleff=TMath::Sqrt(leptonseleff*leptonseleff+leptonseleff*leptonseleff); // because the 2% is per lepton
506    dout << "Lepton selection efficiency not implemented in this script  yet, still using external one" << endl;
507    
508 +  int NPdfs=0;
509 +  if(ismSUGRA) NPdfs = get_npdfs(events);
510 +  
511    float mceff,mcefferr,jzbeff,jzbefferr;
512    if(!automatized) dout << "MC efficiencies:" << endl;
513 <  MCefficiency(events,mceff,mcefferr,mcjzb,requireZ,Neventsinfile,addcut);
514 <  JZBefficiency(events,informalname,jzbeff,jzbefferr,requireZ,addcut);
513 >  Value mceff_nosigcont = MCefficiency(events,mceff,mcefferr,mcjzb,requireZ,Neventsinfile,addcut,-1);
514 >  if(!automatized) cout << "   Without signal contamination, we find an efficiency of " << mceff_nosigcont << endl;
515 >
516 >  if(PlottingSetup::computeJZBefficiency) JZBefficiency(events,informalname,jzbeff,jzbefferr,requireZ,addcut);
517    if(!automatized) dout << "JZB efficiency: " << jzbeff << "+/-" << jzbefferr << endl;
518    
519    if(!automatized) dout << "Error from Peak position:" << endl;
# Line 380 | Line 530 | void do_systematics_for_one_file(TTree *
530    
531    if(!automatized) dout << "JZB response: " << std::endl;
532    float resp,resperr;
533 <  JZBresponse(events,requireZ,resp,resperr,addcut);
533 >  if(PlottingSetup::computeJZBresponse) {
534 >        if(!automatized) dout << "JZB response: " << std::endl;
535 >        JZBresponse(events,requireZ,resp,resperr,addcut);
536 >  }
537  
538    if(!automatized) dout << "Pileup: " << std::endl;
539 <  float resolution=pileup(events,requireZ,informalname,addcut);
540 <  
539 >  float resolution;
540 >  resolution=pileup(events,requireZ,informalname,addcut);
541 >
542 >  float PDFuncert=0;
543 >  if(!automatized) dout << "Assessing PDF uncertainty: " << std::endl;
544 >  if(ismSUGRA) PDFuncert = get_pdf_uncertainty(events, mcjzb, requireZ, Neventsinfile, NPdfs, addcut);
545 >
546    dout << "_______________________________________________" << endl;
547    dout << "                 SUMMARY FOR " << informalname << " with JZB>" << jzbSel << "  (all in %) ";
548    if(addcut!="") dout << "With additional cut: " << addcut;
# Line 396 | Line 554 | void do_systematics_for_one_file(TTree *
554    dout << "JZB Scale Uncert: " << scaledown << " " << scaleup << endl; // in range [0,1]
555    dout << "Resolution : " << resolution << endl; // in range [0,1]
556    dout << "From peak : " << sysfrompeak << endl; // in range [0,1]
557 <  dout << "JZB efficiency: " << jzbeff << "+/-" << jzbefferr << " (not yet included below) " << endl; // in range [0,1]
558 <  dout << "JZB response  : " << resp << " +/-" << resperr << " (not yet included below) " << endl; // in range [0,1]
557 >  if(ismSUGRA) dout << "PDF uncertainty  : " << PDFuncert << endl; // in range [0,1]
558 >  if(PlottingSetup::computeJZBefficiency) dout << "JZB efficiency: " << jzbeff << "+/-" << jzbefferr << " (not yet included below) " << endl; // in range [0,1]
559 >  if(PlottingSetup::computeJZBresponse)dout << "JZB response  : " << resp << " +/-" << resperr << " (not yet included below) " << endl; // in range [0,1]
560    
561    float toterr=0;
562    toterr+=(triggereff)*(triggereff);
# Line 406 | Line 565 | void do_systematics_for_one_file(TTree *
565    if(fabs(scaleup)>fabs(scaledown)) toterr+=(scaleup*scaleup); else toterr+=(scaledown*scaledown);
566    toterr+=(resolution*resolution);
567    toterr+=(sysfrompeak*sysfrompeak);
568 +  if(ismSUGRA) toterr+=(PDFuncert*PDFuncert);
569    dout << "TOTAL SYSTEMATICS: " << TMath::Sqrt(toterr) << " --> " << TMath::Sqrt(toterr)*mceff << endl;
570 <  toterr=TMath::Sqrt(toterr)*mceff;
571 <  dout << "FINAL RESULT : " << 100*mceff << " +/- "<< 100*mcefferr << " (stat) +/- " << 100*toterr << " (syst)   %" << endl;
572 <  dout << "     we thus use the sqrt of the sum of the squares which is : " << 100*toterr << endl;
570 >  float systerr=TMath::Sqrt(toterr)*mceff;
571 >  toterr=TMath::Sqrt(toterr*mceff*mceff+mcefferr*mcefferr);//also includes stat err!
572 >  
573 >  dout << "FINAL RESULT : " << 100*mceff << " +/- "<< 100*mcefferr << " (stat) +/- " << 100*systerr << " (syst)   %" << endl;
574 >  dout << "     we thus use the sqrt of the sum of the squares of the stat & syst err, which is : " << 100*toterr << endl;
575 >  dout << "_______________________________________________" << endl;
576 >  
577 >  //Do not modify the lines below or mess with the order; this order is expected by all limit calculating functions!
578    vector<float> res;
579    res.push_back(jzbSel);
580    res.push_back(mceff);
# Line 419 | Line 584 | void do_systematics_for_one_file(TTree *
584    if(fabs(jesup)>fabs(jesdown)) res.push_back(fabs(jesup)); else res.push_back(fabs(jesdown));
585    if(fabs(scaleup)>fabs(scaledown)) res.push_back(fabs(scaleup)); else res.push_back(fabs(scaledown));
586    res.push_back(fabs(resolution));
587 +  res.push_back(mceff_nosigcont.getValue());
588 +  res.push_back(mceff_nosigcont.getError());
589 +  if(ismSUGRA) res.push_back(PDFuncert);
590    results.push_back(res);
591   }
592  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines