ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/Systematics.C
Revision: 1.1
Committed: Fri Jul 15 10:40:43 2011 UTC (13 years, 9 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Log Message:
Initial commit of systematics script

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     #ifndef HUSH
19     #define HUSH 1
20     #endif
21    
22     #include <TFile.h>
23     #include <TTree.h>
24     #include <TH1.h>
25     #include <TCut.h>
26     #include <TMath.h>
27     #include <TLine.h>
28     #include <TCanvas.h>
29     #include <TProfile.h>
30     #include <TF1.h>
31    
32    
33    
34     Int_t nBins = 100;
35     Float_t jzbMin = -207;
36     Float_t jzbMax = 243;
37     Float_t jzbSel = 100;
38     int iplot=0;
39     int verbose=0;
40    
41     //______________________________________________________________________________
42     Double_t Interpolate(Double_t x, TH1 *histo)
43     {
44     // Given a point x, approximates the value via linear interpolation
45     // based on the two nearest bin centers
46     // Andy Mastbaum 10/21/08
47     // in newer ROOT versions but not in the one I have so I had to work around that ...
48    
49     Int_t xbin = histo->FindBin(x);
50     Double_t x0,x1,y0,y1;
51    
52     if(x<=histo->GetBinCenter(1)) {
53     return histo->GetBinContent(1);
54     } else if(x>=histo->GetBinCenter(histo->GetNbinsX())) {
55     return histo->GetBinContent(histo->GetNbinsX());
56     } else {
57     if(x<=histo->GetBinCenter(xbin)) {
58     y0 = histo->GetBinContent(xbin-1);
59     x0 = histo->GetBinCenter(xbin-1);
60     y1 = histo->GetBinContent(xbin);
61     x1 = histo->GetBinCenter(xbin);
62     } else {
63     y0 = histo->GetBinContent(xbin);
64     x0 = histo->GetBinCenter(xbin);
65     y1 = histo->GetBinContent(xbin+1);
66     x1 = histo->GetBinCenter(xbin+1);
67     }
68     return y0 + (x-x0)*((y1-y0)/(x1-x0));
69     }
70     }
71    
72    
73     //____________________________________________________________________________________
74     // Efficiency plot
75     TH1F* plotEff(TTree* events, TCut kbase, TString informalname) {
76     iplot++;
77     int count=iplot;
78     // Define new histogram
79     char hname[30]; sprintf(hname,"hJzbEff%d",count);
80     TH1F* hJzbEff = new TH1F(hname,"JZB selection efficiency ; JZB (GeV/c); Efficiency",
81     nBins,jzbMin,jzbMax);
82     Float_t step = (jzbMax-jzbMin)/static_cast<Float_t>(nBins);
83    
84     events->Draw("jzb[1]","genJZBSel>-400"&&kbase,"goff");
85     Float_t maxEff = events->GetSelectedRows();
86     if(verbose>0) std::cout << hname << " (" << informalname <<") " << maxEff << std::endl;
87    
88     if(verbose>0) std::cout << "JZB max = " << jzbMax << std::endl;
89     // Loop over steps to get efficiency curve
90     char cut[256];
91     for ( Int_t iBin = 0; iBin<nBins; ++iBin ) {
92     sprintf(cut,"genJZBSel>%3f",jzbMin+iBin*step);
93     events->Draw("jzb[1]",TCut(cut)&&kbase,"goff");
94     Float_t eff = static_cast<Float_t>(events->GetSelectedRows())/maxEff;
95     // std::cout << "COUCOU " << __LINE__ << std::endl;
96     hJzbEff->SetBinContent(iBin+1,eff);
97     hJzbEff->SetBinError(iBin+1,TMath::Sqrt(eff*(1-eff)/maxEff));
98     }
99     return hJzbEff;
100    
101    
102     }
103    
104    
105     //________________________________________________________________________________________
106     // Pile-up efficiency
107     float pileup(TTree *events, string informalname, Float_t myJzbMax = 140. ) {
108     nBins = 16;
109     jzbMax = myJzbMax;
110    
111     // Acceptance cuts
112     TCut kbase("abs(genMllSel-91.2)<20&&pfJetGoodNum>2&&genZPtSel>0&&abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
113    
114     TH1F* hLM4 = plotEff(events,kbase,informalname);
115     hLM4->SetMinimum(0.);
116    
117     // Nominal function
118     TF1* func = new TF1("func","0.5*TMath::Erfc([0]*x-[1])",jzbMin,jzbMax);
119     func->SetParameter(0,0.03);
120     func->SetParameter(1,0.);
121     hLM4->Fit(func,"Q");
122    
123     // Pimped-up function
124     TF1* funcUp = (TF1*)func->Clone();
125     funcUp->SetParameter( 0., func->GetParameter(0)/1.1); // 10% systematic error (up in sigma => 0.1 in erfc)
126     std::cout << " PU: " << funcUp->Eval(jzbSel) << " " << func->Eval(jzbSel)
127     << "(" << (funcUp->Eval(jzbSel)-func->Eval(jzbSel))/func->Eval(jzbSel)*100. << "%)" << std::endl;
128    
129     return (funcUp->Eval(jzbSel)-func->Eval(jzbSel))/func->Eval(jzbSel)*100.;
130    
131     }
132    
133     //____________________________________________________________________________________
134     // Total selection efficiency (MC)
135     void MCefficiency(TTree *events,float &res, float &reserr) {
136    
137     char jzbSelStr[256]; sprintf(jzbSelStr,"%f",jzbSel);
138     // All acceptance cuts at gen. level
139     TCut kbase("abs(genMllSel-91.2)<20&&pfJetGoodNum>2&&genZPt>0&&genJZB>"+TString(jzbSelStr)+"&&genId1==-genId2");
140     // Corresponding reco. cuts
141     TCut ksel("abs(mll-91.2)<20&&id1==id2&&jzb[1]+0.9>"+TString(jzbSelStr));
142    
143     events->Draw("jzb[1]",kbase&&ksel,"goff");
144     Float_t sel = events->GetSelectedRows();
145     events->Draw("jzb[1]",kbase,"goff");
146     Float_t tot = events->GetSelectedRows();
147    
148     res=sel/tot;
149     reserr=TMath::Sqrt(sel/tot*(1-sel/tot)/tot);
150     std::cout << " MC efficiency: " << res << "+-" << reserr << std::endl;
151     }
152    
153     float JZBefficiency(TTree *events, string informalname) {
154     TCut kbase("abs(genMllSel-91.2)<20&&pfJetGoodNum>2&&genZPt>0&&abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
155     TH1F* hLM4 = plotEff(events,kbase,informalname);
156     Int_t bin = hLM4->FindBin(jzbSel); // To get the error
157     std::cout << " Efficiency at JZB==" << jzbSel << std::endl;
158     std::cout << " " << Interpolate(jzbSel,hLM4) << "+-" << hLM4->GetBinError(bin) << std::endl;
159     return -1;
160     }
161    
162     //________________________________________________________________________
163     // Effect of energy scale on efficiency
164     void JZBjetScale(TTree *events, float &jesdown, float &jesup, string informalname="",float syst=0.1, Float_t jzbSelection=-1, TString plotName = "" ) {
165     TCut kbase("abs(genMllSel-91.2)<20&&genZPt>0");
166     TCut ksel("abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
167     TCut nJets("pfJetGoodNum>2");
168     stringstream down,up;
169     down << "pfJetGoodNum"<<30*(1-syst)<<">=3";
170     up << "pfJetGoodNum"<<30*(1+syst)<<">=3";
171    
172     TCut nJetsP(up.str().c_str());
173     TCut nJetsM(down.str().c_str());
174    
175     if ( jzbSelection>0 ) jzbSel = jzbSelection;
176    
177     if ( !(plotName.Length()>1) ) plotName = informalname;
178    
179     nBins = 1; jzbMin = jzbSel*0.95; jzbMax = jzbSel*1.05;
180     TH1F* hist = plotEff(events,(kbase&&ksel&&nJets),informalname);
181    
182     TH1F* histp = plotEff(events,(kbase&&ksel&&nJetsP),informalname);
183    
184     TH1F* histm = plotEff(events,(kbase&&ksel&&nJetsM),informalname);
185    
186     // Dump some information
187     Float_t eff = Interpolate(jzbSel,hist);
188     Float_t effp = Interpolate(jzbSel,histp);
189     Float_t effm = Interpolate(jzbSel,histm);
190     std::cout << " Efficiency at JZB==" << jzbSel << std::endl;
191     std::cout << " JESup: " << effp << " (" << (effp-eff)/eff*100. << "%)" << std::endl;
192     std::cout << " central: " << eff << std::endl;
193     std::cout << " JESdown: " << effm << " (" << (effm-eff)/eff*100. << "%)" << std::endl;
194     jesup=(effp-eff)/eff*100.;
195     jesdown=(effm-eff)/eff*100.;
196     }
197    
198     //________________________________________________________________________
199     // Effect of energy scale on JZB efficiency
200     void doJZBscale(TTree *events, float &down, float &up, float &syst, float systematic, string informalname) {
201    
202     TCut kbase("abs(genMllSel-91.2)<20&&genZPt>0&&pfJetGoodNum>2");
203     TCut ksel("abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
204    
205     nBins = 50;
206     jzbMin = 0.5*jzbSel;
207     jzbMax = 2.0*jzbSel;
208    
209     TH1F* hist = plotEff(events,kbase&&ksel,informalname);
210    
211     // Dump some information
212     Float_t eff = Interpolate(jzbSel,hist);
213     Float_t effp = Interpolate(jzbSel*(1.+systematic),hist);
214     Float_t effm = Interpolate(jzbSel*(1.-systematic),hist);
215     std::cout << " efficiency at JZB==" << jzbSel*(1.+systematic) << "(-"<<syst*100<<"%) : " << effp << " (" << ((effp-eff)/eff)*100. << "%)" << std::endl;
216     std::cout << " efficiency at JZB==" << jzbSel << ": " << eff << std::endl;
217     std::cout << " efficiency at JZB==" << jzbSel*(1.-systematic) << "(-"<<syst*100<<"%) : " << effm << " (" << ((effm-eff)/eff)*100. << "%)" << std::endl;
218     up=((effp-eff)/eff)*100;
219     down=((effm-eff)/eff)*100;
220     }
221    
222     //________________________________________________________________________
223     // JZB response (true/reco. vs. true)
224     void JZBresponse(TTree *events, bool isMET = kFALSE, Float_t myJzbMax = 200., Int_t nPeriods = 9 ) {
225    
226     jzbMin = 20;
227     TCut kbase("abs(genMllSel-91.2)<20&&genZPtSel>0&&pfJetGoodNum>2");
228     TCut ksel("abs(mll-91.2)<20&&((id1+1)*(id2+1)*ch1*ch2)!=-2");
229    
230     TProfile* hJzbResp = new TProfile("hJzbResp","JZB response ; JZB true (GeV/c); JZB reco. / JZB true",
231     nPeriods, jzbMin, myJzbMax, "" );
232    
233     if (!isMET) events->Project("hJzbResp","jzb[1]/genJZBSel:genJZBSel",kbase&&ksel);
234     else events->Project("hJzbResp","met[4]/genMET:genMET",kbase&&ksel);
235    
236     hJzbResp->SetMaximum(1.2);
237     hJzbResp->SetMinimum(0.2);
238     hJzbResp->Fit("pol0","Q");
239     TF1 *fittedfunction = hJzbResp->GetFunction("pol0");
240     cout << " Response: " << fittedfunction->GetParameter(0) << " +/- " << fittedfunction->GetParError(0) << endl;
241     }
242    
243    
244     void do_systematics_for_one_file(TTree *events,string informalname, vector<vector<float> > &uncertainties) {
245    
246     float JetEnergyScaleUncert=0.1;
247     float JZBScaleUncert=0.1;
248    
249     float triggereff=4;//percent!
250     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;
253    
254     float mceff,mcefferr;
255     cout << "MC efficiencies:" << endl;
256     MCefficiency(events,mceff,mcefferr);
257     JZBefficiency(events,informalname);
258    
259     std::cout << "Jet energy scale: " << std::endl;
260     float jesup,jesdown;
261     JZBjetScale(events,jesdown,jesup,informalname,JetEnergyScaleUncert);
262    
263     std::cout << "JZB scale: " << std::endl;
264     float scaleup,scaledown,scalesyst;
265     doJZBscale(events,scaledown,scaleup,scalesyst,JZBScaleUncert,informalname);
266    
267     std::cout << "JZB response: " << std::endl;
268     JZBresponse(events);
269    
270     std::cout << "Pileup: " << std::endl;
271     float resolution=pileup(events,informalname);
272    
273     cout << "_______________________________________________" << endl;
274     cout << " SUMMARY FOR " << informalname << " with JZB>" << jzbSel << endl;
275     cout << "Trigger efficiency: " << triggereff << endl;
276     cout << "Lepton Sel Eff: " << leptonseleff << endl;
277     cout << "For JZB>" << jzbSel << endl;
278     cout << "Jet energy scale: " << jesup << " " << jesdown << " --> suggesting: " << Round(0.5*(fabs(jesup)+fabs(jesdown)),1) << endl;
279     cout << "JZB Scale Uncert: " << scaledown << " " << scaleup << " --> suggesting: " << Round(0.5*(fabs(scaledown)+fabs(scaleup)),1) << endl;
280     cout << "Resolution : " << resolution << endl;
281    
282     vector<float> uncert;
283     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);
289    
290     uncertainties.push_back(uncert);
291     }
292    
293     vector<vector<float> > compute_systematics(string mcjzb, string datajzb, samplecollection &signalsamples, vector<float> bins) {
294     vector< vector<float> > uncertainties;
295     for (int isignal=0; isignal<signalsamples.collection.size();isignal++) {
296     cout << "Looking at signal " << (signalsamples.collection)[isignal].filename << endl;
297     for(int ibin=0;ibin<bins.size();ibin++) {
298     jzbSel=bins[ibin];
299     do_systematics_for_one_file((signalsamples.collection)[isignal].events,(signalsamples.collection)[isignal].samplename,uncertainties);
300     }//end of bin loop
301     }//end of signal loop
302     return uncertainties;
303     }