ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/Development/Plotting/Modules/LimitCalculation.C
Revision: 1.7
Committed: Wed Aug 15 13:52:12 2012 UTC (12 years, 8 months ago) by buchmann
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +5 -18 lines
Log Message:
Added option to switch off sidebands for classic JZB

File Contents

# User Rev Content
1 buchmann 1.1 #include <iostream>
2     #include <vector>
3     #include <sys/stat.h>
4     #include <fstream>
5    
6     #include <TCut.h>
7     #include <TROOT.h>
8     #include <TCanvas.h>
9     #include <TMath.h>
10     #include <TColor.h>
11     #include <TPaveText.h>
12     #include <TRandom.h>
13     #include <TH1.h>
14     #include <TH2.h>
15     #include <TF1.h>
16     #include <TSQLResult.h>
17     #include <TProfile.h>
18     #include <TSystem.h>
19     #include "LimitDroplet.C"
20    
21     //#include "TTbar_stuff.C"
22     using namespace std;
23    
24     using namespace PlottingSetup;
25    
26    
27     void rediscover_the_top(string mcjzb, string datajzb) {
28     dout << "Hi! today we are going to (try to) rediscover the top!" << endl;
29     TCanvas *c3 = new TCanvas("c3","c3");
30     c3->SetLogy(1);
31     vector<float> binning;
32     //binning=allsamples.get_optimal_binsize(mcjzb,cutmass&&cutOSSF&&cutnJets,20,50,800);
33     /*
34     binning.push_back(50);
35     binning.push_back(100);
36     binning.push_back(150);
37     binning.push_back(200);
38     binning.push_back(500);
39    
40    
41     TH1F *dataprediction = allsamples.Draw("dataprediction", "-"+datajzb, binning, "JZB [GeV]", "events", cutmass&&cutOSSF&&cutnJets,data, luminosity);
42     TH1F *puresignal = allsamples.Draw("puresignal", datajzb, binning, "JZB [GeV]", "events", cutmass&&cutOSOF&&cutnJets,data, luminosity);
43     // TH1F *puresignal = allsamples.Draw("puresignal", mcjzb, binning, "JZB [GeV]", "events", cutmass&&cutOSSF&&cutnJets,mc, luminosity,allsamples.FindSample("TTJets"));
44     TH1F *observed = allsamples.Draw("observed", datajzb,binning, "JZB [GeV]", "events", cutmass&&cutOSSF&&cutnJets,data,luminosity);
45 buchmann 1.6
46 buchmann 1.1 ofstream myfile;
47     TH1F *ratio = (TH1F*)observed->Clone();
48     ratio->Divide(dataprediction);
49     ratio->GetYaxis()->SetTitle("Ratio obs/pred");
50     ratio->Draw();
51     c3->SaveAs("testratio.png");
52     myfile.open ("ShapeFit_log.txt");
53     establish_upper_limits(observed,dataprediction,puresignal,"LM4",myfile);
54     myfile.close();
55     */
56    
57    
58     int nbins=100;
59     float low=0;
60     float hi=500;
61     TCanvas *c4 = new TCanvas("c4","c4",900,900);
62     c4->Divide(2,2);
63     c4->cd(1);
64     c4->cd(1)->SetLogy(1);
65     TH1F *datapredictiont = allsamples.Draw("datapredictiont", "-"+datajzb, nbins,low,hi, "JZB [GeV]", "events", cutmass&&cutOSSF&&cutnJets,data, luminosity);
66     TH1F *datapredictiono = allsamples.Draw("datapredictiono", "-"+datajzb, nbins,low,hi, "JZB [GeV]", "events", cutmass&&cutOSOF&&cutnJets,data, luminosity);
67     datapredictiont->Add(datapredictiono,-1);
68     dout << "Second way of doing this !!!! Analytical shape to the left :-D" << endl;
69     vector<TF1*> functions = do_cb_fit_to_plot(datapredictiont,10);
70     datapredictiont->SetMarkerColor(kRed);
71     datapredictiont->SetLineColor(kRed);
72     datapredictiont->Draw();
73     functions[1]->Draw("same");
74     TText *title1 = write_title("Top Background Prediction (JZB<0, with osof subtr)");
75     title1->Draw();
76    
77     c4->cd(2);
78     c4->cd(2)->SetLogy(1);
79     TH1F *observedt = allsamples.Draw("observedt", datajzb, nbins,low,hi, "JZB [GeV]", "events", cutmass&&cutOSSF&&cutnJets,data, luminosity);
80     observedt->Draw();
81     datapredictiont->Draw("histo,same");
82     functions[1]->Draw("same");
83     TText *title2 = write_title("Observed and predicted background");
84     title2->Draw();
85    
86     c4->cd(3);
87     c4->cd(3)->SetLogy(1);
88     // TH1F *ratio = (TH1F*)observedt->Clone();
89    
90     TH1F *analytical_background_prediction= new TH1F("analytical_background_prediction","",nbins,low,hi);
91     for(int i=0;i<=nbins;i++) {
92     analytical_background_prediction->SetBinContent(i+1,functions[1]->Eval(((hi-low)/((float)nbins))*(i+0.5)));
93     analytical_background_prediction->SetBinError(i+1,TMath::Sqrt(functions[1]->Eval(((hi-low)/((float)nbins))*(i+0.5))));
94     }
95     analytical_background_prediction->GetYaxis()->SetTitle("JZB [GeV]");
96     analytical_background_prediction->GetYaxis()->CenterTitle();
97     TH1F *analyticaldrawonly = (TH1F*)analytical_background_prediction->Clone();
98     analytical_background_prediction->SetFillColor(TColor::GetColor("#3399FF"));
99     analytical_background_prediction->SetMarkerSize(0);
100     analytical_background_prediction->Draw("e5");
101     analyticaldrawonly->Draw("histo,same");
102     functions[1]->Draw("same");
103     TText *title3 = write_title("Analytical bg pred histo");
104     title3->Draw();
105    
106     c4->cd(4);
107     // c4->cd(4)->SetLogy(1);
108     vector<float> ratio_binning;
109     ratio_binning.push_back(0);
110     ratio_binning.push_back(5);
111     ratio_binning.push_back(10);
112     ratio_binning.push_back(20);
113     ratio_binning.push_back(50);
114     // ratio_binning.push_back(60);
115     /*
116     ratio_binning.push_back(51);
117     ratio_binning.push_back(52);
118     ratio_binning.push_back(53);
119     ratio_binning.push_back(54);
120     ratio_binning.push_back(55);
121     ratio_binning.push_back(56);
122     ratio_binning.push_back(57);
123     ratio_binning.push_back(58);
124     ratio_binning.push_back(59);
125     ratio_binning.push_back(60);
126     // ratio_binning.push_back(70);*/
127     // ratio_binning.push_back(80);
128     // ratio_binning.push_back(90);
129     ratio_binning.push_back(80);
130     // ratio_binning.push_back(110);
131     ratio_binning.push_back(500);
132    
133     TH1F *observedtb = allsamples.Draw("observedtb", datajzb, ratio_binning, "JZB [GeV]", "events", cutmass&&cutOSSF&&cutnJets,data, luminosity);
134     TH1F *datapredictiontb = allsamples.Draw("datapredictiontb", "-"+datajzb, ratio_binning, "JZB [GeV]", "events", cutmass&&cutOSSF&&cutnJets,data, luminosity);
135     TH1F *datapredictiontbo = allsamples.Draw("datapredictiontbo", "-"+datajzb, ratio_binning, "JZB [GeV]", "events", cutmass&&cutOSOF&&cutnJets,data, luminosity);
136     datapredictiontb->Add(datapredictiontbo,-1);
137     TH1F *analytical_background_predictionb = allsamples.Draw("analytical_background_predictionb",datajzb, ratio_binning, "JZB [GeV]", "events", cutmass&&cutOSSF&&cutnJets&&"mll<2",data, luminosity);
138 buchmann 1.6 for(int i=0;i<=(int)ratio_binning.size();i++) {
139 buchmann 1.1 analytical_background_predictionb->SetBinContent(i+1,functions[1]->Eval(analytical_background_predictionb->GetBinCenter(i)));
140     analytical_background_predictionb->SetBinError(i+1,TMath::Sqrt(functions[1]->Eval(analytical_background_predictionb->GetBinCenter(i))));
141     }
142    
143     TH1F *ratio = (TH1F*) observedtb->Clone();
144     ratio->Divide(datapredictiontb);
145    
146 buchmann 1.6 for (int i=0;i<=(int)ratio_binning.size();i++) {
147 buchmann 1.1 dout << observedtb->GetBinLowEdge(i+1) << ";"<<observedtb->GetBinContent(i+1) << ";" << datapredictiontb->GetBinContent(i+1) << " --> " << ratio->GetBinContent(i+1) << "+/-" << ratio->GetBinError(i+1) << endl;
148     }
149    
150     // ratio->Divide(datapredictiontb);
151     // ratio->Divide(analytical_background_predictionb);
152     // TGraphAsymmErrors *JZBratio= histRatio(observedtb,analytical_background_predictionb,data,ratio_binning);
153     // ratio->Divide(analytical_background_prediction);
154     // ratio->Divide(datapredictiont);
155     // ratio->GetYaxis()->SetTitle("obs/pred");
156     // JZBratio->Draw("AP");
157     ratio->GetYaxis()->SetRangeUser(0,10);
158     ratio->Draw();
159     //analytical_background_predictionb->Draw();
160     // JZBratio->SetTitle("");
161     TText *title4 = write_title("Ratio of observed to predicted");
162     title4->Draw();
163    
164     // CompleteSave(c4,"test/ttbar_discovery_dataprediction___analytical_function");
165     CompleteSave(c4,"test/ttbar_discovery_dataprediction__analytical__new_binning_one_huge_bin_from_80");
166    
167    
168    
169    
170    
171     }
172    
173 buchmann 1.2 vector<float> compute_one_upper_limit(float mceff,float mcefferr, int ibin, string mcjzb, string plotfilename, bool doexpected, int flipped, bool doasymptotic=false) {
174 buchmann 1.6 float sigma95=-9.9;
175 buchmann 1.1 /*
176     USAGE OF ROOSTATS_CL95
177     " Double_t limit = roostats_cl95(ilum, slum, eff, seff, bck, sbck, n, gauss = false, nuisanceModel, method, plotFileName, seed); \n"
178     " LimitResult expected_limit = roostats_clm(ilum, slum, eff, seff, bck, sbck, ntoys, nuisanceModel, method, seed); \n"
179     " Double_t average_limit = roostats_cla(ilum, slum, eff, seff, bck, sbck, nuisanceModel, method, seed); \n"
180     " \n"
181     "
182     " Double_t obs_limit = limit.GetObservedLimit(); \n"
183     " Double_t exp_limit = limit.GetExpectedLimit(); \n"
184     " Double_t exp_up = limit.GetOneSigmaHighRange(); \n"
185     " Double_t exp_down = limit.GetOneSigmaLowRange(); \n"
186     " Double_t exp_2up = limit.GetTwoSigmaHighRange(); \n"
187     " Double_t exp_2down = limit.GetTwoSigmaLowRange(); \n"
188     */
189     if(mceff<=0) {
190     write_warning(__FUNCTION__,"Cannot compute upper limit in this configuration as the efficiency is negative:");
191     dout << "mc efficiency=" << mceff << " +/- " << mcefferr;
192     vector<float> sigmas;
193     sigmas.push_back(-1);
194     sigmas.push_back(-1);
195     return sigmas;
196     } else {
197     ///------------------------------------------ < NEW > ----------------------------------------------------------
198    
199     int secondssince1970=time(NULL);
200     stringstream repname;
201     repname << PlottingSetup::cbafbasedir << "/exchange/report_" << secondssince1970 << "_"<<plotfilename<< "__"<< ".txt";
202    
203     /* - report filename [1]
204     - luminosity [2]
205     - lumi uncert [3]
206     - MC efficiency [4]
207     - MC efficiency error [5]
208     - Npred [6]
209     - Nprederr [7]
210     - Nobs [8]
211     - JZB cut [9]
212     - plot name [10]*/
213    
214     if(flipped==0) dout << "Calling limit capsule instead of calling : CL95(" << luminosity << "," << lumiuncert*luminosity << "," << mceff << "," << mcefferr << "," << Npred[ibin] << "," << Nprederr[ibin] << "," << Nobs[ibin] << "," << false << "," << nuisancemodel<< ") " << endl;
215     if(flipped>0) dout << "Calling limit capsule instead of calling : CL95(" << luminosity << "," << lumiuncert*luminosity << "," << mceff << "," << mcefferr << "," << flippedNpred[ibin] << "," << flippedNprederr[ibin] << "," << flippedNobs[ibin] << "," << false << "," << nuisancemodel<< ") " << endl;
216 buchmann 1.4
217 buchmann 1.3 stringstream command;
218     if(flipped==0) command << PlottingSetup::cbafbasedir << "/DistributedModelCalculations/Limits/TimedLimitCapsule.exec " << repname.str() << " " << luminosity << " " << luminosity*lumiuncert << " " << mceff << " " << mcefferr << " " << Npred[ibin] << " " << Nprederr[ibin] << " " << Nobs[ibin] << " " << -1 << " " << PlottingSetup::basedirectory << "/" << plotfilename << " " << doexpected << " " << doasymptotic;
219     if(flipped>0) command << PlottingSetup::cbafbasedir << "/DistributedModelCalculations/Limits/TimedLimitCapsule.exec " << repname.str() << " " << luminosity << " " << luminosity*lumiuncert << " " << mceff << " " << mcefferr << " " << flippedNpred[ibin] << " " << flippedNprederr[ibin] << " " << flippedNobs[ibin] << " " << -1 << " " << PlottingSetup::basedirectory << "/" << plotfilename << " " << doexpected<< " " << doasymptotic;
220 buchmann 1.4
221 buchmann 1.3 dout << command.str() << endl;
222 buchmann 1.1
223 buchmann 1.3 if(doasymptotic) write_warning(__FUNCTION__, "DOING ASYMPTOTIC LIMIT!");
224    
225     int retval = 256;
226     int attempts=0;
227     while(!(retval==0||attempts>=3)) {//try up to 3 times
228 buchmann 1.1 attempts++;
229     dout << "Starting limit calculation (TimedLimitCapsule) now : Attempt " << attempts << endl;
230     retval=gSystem->Exec(command.str().c_str());
231     }
232     char hostname[1023];
233     gethostname(hostname,1023);
234     if((!((Contains(hostname,"t3ui")||Contains(hostname,"t3wn"))))&&retval==256) {
235     //running via CRAB and encountered the same problem too often: place a problem file to mark this problem!
236     stringstream markproblem;
237     markproblem << "touch " << PlottingSetup::basedirectory << "/exchange/problemswhilesettinglimits.txt";
238     gSystem->Exec(markproblem.str().c_str());
239     }
240     LimitDroplet limres;
241     limres.readDroplet(repname.str());
242     dout << limres << endl;
243     remove(repname.str().c_str());
244     sigma95=limres.observed;
245    
246     ///------------------------------------------ < /NEW > ----------------------------------------------------------
247     vector<float> sigmas;
248     sigmas.push_back(sigma95);
249     if(doexpected) {
250     sigmas.push_back(limres.expected);
251     sigmas.push_back(limres.upper68);
252     sigmas.push_back(limres.lower68);
253     sigmas.push_back(limres.upper95);
254     sigmas.push_back(limres.lower95);
255     }
256    
257     return sigmas;
258    
259    
260     }//end of mc efficiency is ok
261     }
262    
263 buchmann 1.2 vector<float> compute_upper_limits_from_counting_experiment(vector<vector<float> > uncertainties,vector<float> jzbcuts, string mcjzb, bool doexpected, int flipped) {
264 buchmann 1.1 dout << "Doing counting experiment ... " << endl;
265     vector<vector<string> > limits;
266     vector<vector<float> > vlimits;
267    
268    
269 buchmann 1.6 for(int isample=0;isample<(int)signalsamples.collection.size();isample++) {
270 buchmann 1.1 vector<string> rows;
271     vector<float> vrows;
272     dout << "Considering sample " << signalsamples.collection[isample].samplename << endl;
273     rows.push_back(signalsamples.collection[isample].samplename);
274 buchmann 1.6 for(int ibin=0;ibin<(int)jzbcuts.size();ibin++) {
275 buchmann 1.1 dout << "_________________________________________________________________________________" << endl;
276     float JZBcutat=uncertainties[isample*jzbcuts.size()+ibin][0];
277     float mceff=uncertainties[isample*jzbcuts.size()+ibin][1];
278     float staterr=uncertainties[isample*jzbcuts.size()+ibin][2];
279     float systerr=uncertainties[isample*jzbcuts.size()+ibin][3];
280     float toterr =uncertainties[isample*jzbcuts.size()+ibin][4];
281 buchmann 1.6 // float observed,observederr,null,result;
282 buchmann 1.1
283     string plotfilename=(string)(TString(signalsamples.collection[isample].samplename)+TString("___JZB_geq_")+TString(any2string(JZBcutat))+TString(".png"));
284     dout << "Sample: " << signalsamples.collection[isample].samplename << ", JZB>"<<JZBcutat<< " : " << mceff << " +/- " << staterr << " (stat) +/- " << systerr << " (syst) --> toterr = " << toterr << endl;
285     vector<float> sigmas = compute_one_upper_limit(mceff,toterr,ibin,mcjzb,plotfilename,doexpected,flipped);
286    
287     tripple LibraryUpperLimits;
288     LibraryUpperLimits.name=signalsamples.collection[isample].samplename;
289     LibraryUpperLimits.first=mceff*signalsamples.collection[isample].xs * PlottingSetup::luminosity;
290     LibraryUpperLimits.second=staterr*signalsamples.collection[isample].xs * PlottingSetup::luminosity;
291     int resultindex=PlottingSetup::allresults.Find(jzbcuts[ibin]);
292     (allresults.predictions[resultindex]).SignalYield.push_back(LibraryUpperLimits);
293    
294     if(doexpected) {
295     rows.push_back(any2string(sigmas[0])+";"+any2string(sigmas[1])+";"+"("+any2string(signalsamples.collection[isample].xs)+")");
296     vrows.push_back(sigmas[0]);
297     vrows.push_back(sigmas[1]);
298     vrows.push_back(signalsamples.collection[isample].xs);
299     }
300     else {
301     rows.push_back(any2string(sigmas[0]));
302     vrows.push_back(sigmas[0]);
303     vrows.push_back(signalsamples.collection[isample].xs);
304     }
305     }//end of bin loop
306     limits.push_back(rows);
307     vlimits.push_back(vrows);
308     }//end of sample loop
309    
310     dout << endl << endl << "_______________________________________________________________________________________" << endl;
311     dout << "Going to store upper limit on event yield in result library: " << endl;
312 buchmann 1.6 for(int ibin=0;ibin<(int)jzbcuts.size();ibin++) {
313 buchmann 1.1 int resultindex=PlottingSetup::allresults.Find(jzbcuts[ibin]);
314     vector<float> Normsigmas = compute_one_upper_limit(1.0,0.0, resultindex, mcjzb, "UPPERLIMIT", false, 0);
315     (allresults.predictions[resultindex]).UpperLimit=Normsigmas[0]*PlottingSetup::luminosity;
316     }
317     dout << endl << "_______________________________________________________________________________________" << endl;
318    
319     dout << endl << endl << endl << "_________________________________________________________________________________________________" << endl << endl;
320     dout << endl << endl << "PAS table 3: (notation: limit [95%CL])" << endl << endl;
321     dout << "\t";
322 buchmann 1.6 for (int irow=0;irow<(int)jzbcuts.size();irow++) {
323 buchmann 1.1 dout << jzbcuts[irow] << "\t";
324     }
325     dout << endl;
326 buchmann 1.6 for(int irow=0;irow<(int)limits.size();irow++) {
327     for(int ientry=0;ientry<(int)limits[irow].size();ientry++) {
328 buchmann 1.1 if (limits[irow][ientry]>0) dout << limits[irow][ientry] << "\t";
329     else dout << " (N/A) \t";
330     }
331     dout << endl;
332     }
333    
334     if(!doexpected) {
335     dout << endl << endl << "LIMITS: (Tex)" << endl;
336     tout << "\\begin{table}[hbtp]" << endl;
337     tout << "\\renewcommand{\\arraystretch}{1.3}" << endl;
338     tout << "\\begin{center}" << endl;
339     tout << "\\caption{Observed upper limits on the cross section of different LM benchmark points " << (ConsiderSignalContaminationForLimits?" (accounting for signal contamination)":" (not accounting for signal contamination)") << "}\\label{tab:lmresults}" << endl;
340     tout << "" << endl;
341     tout << "\\begin{tabular}{ | l | ";
342 buchmann 1.6 for (int irow=0;irow<(int)jzbcuts.size();irow++) tout << " l |";
343 buchmann 1.1 tout << "} " << endl << " \\hline " << endl << "& \t ";
344 buchmann 1.6 for (int irow=0;irow<(int)jzbcuts.size();irow++) {
345 buchmann 1.1 tout << "JZB $>$ " << jzbcuts[irow] << " GeV & \t ";
346     }
347     tout << " \\\\ \\hline " << endl;
348 buchmann 1.6 for(int irow=0;irow<(int)limits.size();irow++) {
349 buchmann 1.1 tout << limits[irow][0] << " \t";
350 buchmann 1.6 for(int ientry=0;ientry<(int)jzbcuts.size();ientry++) {
351 buchmann 1.1 if(vlimits[irow][2*ientry]>0) tout << " & " << Round(vlimits[irow][2*ientry],2) << " \t (" << Round(vlimits[irow][2*ientry] / vlimits[irow][2*ientry+1],3)<< "x \\sigma ) \t";
352     else tout << " & ( N / A ) \t";
353     // dout << Round(vlimits[irow][2*ientry],3) << " / " << Round(vlimits[irow][2*ientry+1],3)<< "\t";
354     }
355     tout << " \\\\ \\hline " << endl;
356     }
357     tout << "\\end{tabular}" << endl;
358     tout << " \\end{tabular}"<< endl;
359     tout << "\\end{center}"<< endl;
360     tout << "\\end{table} "<< endl;
361    
362     }//do observed
363    
364     dout << endl << endl << "Final selection efficiencies with total statistical and systematic errors, and corresponding observed and expected upper limits (UL) on ($\\sigma\\times$ BR $\\times$ acceptance) for the LM4 and LM8 scenarios, in the different regions. The last column contains the predicted ($\\sigma \\times $BR$\\times$ acceptance) at NLO obtained from Monte Carlo simulation." << endl;
365     dout << "Scenario \t Efficiency [%] \t Upper limits [pb] \t \\sigma [pb]" << endl;
366 buchmann 1.6 for(int icut=0;icut<(int)jzbcuts.size();icut++) {
367 buchmann 1.1 dout << "Region with JZB>" << jzbcuts[icut] << (ConsiderSignalContaminationForLimits?" (accounting for signal contamination)":" (not accounting for signal contamination)") << endl;
368 buchmann 1.6 for(int isample=0;isample<(int)signalsamples.collection.size();isample++) {
369 buchmann 1.1 dout << limits[isample][0] << "\t" << Round(100*uncertainties[isample*jzbcuts.size()+icut][1],3) << "+/-" << Round(100*uncertainties[isample*jzbcuts.size()+icut][2],3) << " (stat) +/- " << Round(100*uncertainties[isample*jzbcuts.size()+icut][3],3) << " (syst) \t" << Round((vlimits[isample][2*icut]),3) << "\t" << Round(vlimits[isample][2*icut+1],3) << endl;
370     }
371     dout << endl;
372     }
373     allresults.Print();
374    
375 buchmann 1.2 //---------------------------------------------
376 buchmann 1.1
377 buchmann 1.2 vector<float> lowestULs;
378 buchmann 1.6 for(int isample=0;isample<(int)signalsamples.collection.size();isample++) {
379 buchmann 1.2 float lowestUL=-1;
380 buchmann 1.6 for(int icut=0;icut<(int)jzbcuts.size();icut++) {
381 buchmann 1.2 float currUL=Round((vlimits[isample][2*icut]),3);
382     if(currUL>0) {
383     if(lowestUL<0) lowestUL=currUL;
384     if(currUL<lowestUL) lowestUL=currUL;
385     }
386     lowestULs.push_back(lowestUL);
387     }
388     }
389    
390     //---------------------------------------------
391     return lowestULs;
392 buchmann 1.1 }
393    
394    
395    
396     /********************************************************************** new : Limits using SHAPES ***********************************
397    
398    
399     SSSSSSSSSSSSSSS hhhhhhh
400     SS:::::::::::::::Sh:::::h
401     S:::::SSSSSS::::::Sh:::::h
402     S:::::S SSSSSSSh:::::h
403     S:::::S h::::h hhhhh aaaaaaaaaaaaa ppppp ppppppppp eeeeeeeeeeee ssssssssss
404     S:::::S h::::hh:::::hhh a::::::::::::a p::::ppp:::::::::p ee::::::::::::ee ss::::::::::s
405     S::::SSSS h::::::::::::::hh aaaaaaaaa:::::ap:::::::::::::::::p e::::::eeeee:::::eess:::::::::::::s
406     SS::::::SSSSS h:::::::hhh::::::h a::::app::::::ppppp::::::pe::::::e e:::::es::::::ssss:::::s
407     SSS::::::::SS h::::::h h::::::h aaaaaaa:::::a p:::::p p:::::pe:::::::eeeee::::::e s:::::s ssssss
408     SSSSSS::::S h:::::h h:::::h aa::::::::::::a p:::::p p:::::pe:::::::::::::::::e s::::::s
409     S:::::S h:::::h h:::::h a::::aaaa::::::a p:::::p p:::::pe::::::eeeeeeeeeee s::::::s
410     S:::::S h:::::h h:::::ha::::a a:::::a p:::::p p::::::pe:::::::e ssssss s:::::s
411     SSSSSSS S:::::S h:::::h h:::::ha::::a a:::::a p:::::ppppp:::::::pe::::::::e s:::::ssss::::::s
412     S::::::SSSSSS:::::S h:::::h h:::::ha:::::aaaa::::::a p::::::::::::::::p e::::::::eeeeeeee s::::::::::::::s
413     S:::::::::::::::SS h:::::h h:::::h a::::::::::aa:::ap::::::::::::::pp ee:::::::::::::e s:::::::::::ss
414     SSSSSSSSSSSSSSS hhhhhhh hhhhhhh aaaaaaaaaa aaaap::::::pppppppp eeeeeeeeeeeeee sssssssssss
415     p:::::p
416     p:::::p
417     p:::::::p
418     p:::::::p
419     p:::::::p
420     ppppppppp
421    
422    
423     *********************************************************************** new : Limits using SHAPES ***********************************/
424    
425    
426     void limit_shapes_for_systematic_effect(TFile *limfile, string identifier, string mcjzb, string datajzb, int JES,vector<float> binning, TCanvas *limcan) {
427     dout << "Creatig shape templates ... ";
428     if(identifier!="") dout << "for systematic called "<<identifier;
429     dout << endl;
430     int dataormc=mcwithsignal;//this is only for tests - for real life you want dataormc=data !!!
431     if(dataormc!=data) write_warning(__FUNCTION__,"WATCH OUT! Not using data for limits!!!! this is ok for tests, but not ok for anything official!");
432    
433     TCut limitnJetcut;
434     if(JES==noJES) limitnJetcut=cutnJets;
435     else {
436     if(JES==JESdown) limitnJetcut=cutnJetsJESdown;
437     if(JES==JESup) limitnJetcut=cutnJetsJESup;
438     }
439     TH1F *ZOSSFP = allsamples.Draw("ZOSSFP",datajzb,binning, "JZB4limits", "events",cutmass&&cutOSSF&&limitnJetcut&&basiccut,dataormc,luminosity);
440     TH1F *ZOSOFP = allsamples.Draw("ZOSOFP",datajzb,binning, "JZB4limits", "events",cutmass&&cutOSOF&&limitnJetcut&&basiccut,dataormc,luminosity);
441     TH1F *ZOSSFN = allsamples.Draw("ZOSSFN","-"+datajzb,binning, "JZB4limits", "events",cutmass&&cutOSSF&&limitnJetcut&&basiccut,dataormc,luminosity);
442     TH1F *ZOSOFN = allsamples.Draw("ZOSOFN","-"+datajzb,binning, "JZB4limits", "events",cutmass&&cutOSOF&&limitnJetcut&&basiccut,dataormc,luminosity);
443    
444     TH1F *SBOSSFP;
445     TH1F *SBOSOFP;
446     TH1F *SBOSSFN;
447     TH1F *SBOSOFN;
448    
449     TH1F *LZOSSFP = allsamples.Draw("LZOSSFP",mcjzb,binning, "JZB4limits", "events",cutmass&&cutOSSF&&limitnJetcut&&basiccut,mc,luminosity,allsamples.FindSample("LM4"));
450     TH1F *LZOSOFP = allsamples.Draw("LZOSOFP",mcjzb,binning, "JZB4limits", "events",cutmass&&cutOSOF&&limitnJetcut&&basiccut,mc,luminosity,allsamples.FindSample("LM4"));
451     TH1F *LZOSSFN = allsamples.Draw("LZOSSFN","-"+mcjzb,binning, "JZB4limits", "events",cutmass&&cutOSSF&&limitnJetcut&&basiccut,mc,luminosity,allsamples.FindSample("LM4"));
452     TH1F *LZOSOFN = allsamples.Draw("LZOSOFN","-"+mcjzb,binning, "JZB4limits", "events",cutmass&&cutOSOF&&limitnJetcut&&basiccut,mc,luminosity,allsamples.FindSample("LM4"));
453    
454     TH1F *LSBOSSFP;
455     TH1F *LSBOSOFP;
456     TH1F *LSBOSSFN;
457     TH1F *LSBOSOFN;
458    
459     flag_this_change(__FUNCTION__,__LINE__,false);//PlottingSetup::RestrictToMassPeak
460 buchmann 1.7 if(PlottingSetup::RestrictToMassPeak&&PlottingSetup::UseSidebandsForcJZB) {
461 buchmann 1.1 SBOSSFP = allsamples.Draw("SBOSSFP",datajzb,binning, "JZB4limits", "events",cutOSSF&&limitnJetcut&&basiccut&&sidebandcut,dataormc,luminosity);
462     SBOSOFP = allsamples.Draw("SBOSOFP",datajzb,binning, "JZB4limits", "events",cutOSOF&&limitnJetcut&&basiccut&&sidebandcut,dataormc,luminosity);
463     SBOSSFN = allsamples.Draw("SBOSSFN","-"+datajzb,binning, "JZB4limits", "events",cutOSSF&&limitnJetcut&&basiccut&&sidebandcut,dataormc,luminosity);
464     SBOSOFN = allsamples.Draw("SBOSOFN","-"+datajzb,binning, "JZB4limits", "events",cutOSOF&&limitnJetcut&&basiccut&&sidebandcut,dataormc,luminosity);
465    
466     LSBOSSFP = allsamples.Draw("LSBOSSFP",mcjzb,binning, "JZB4limits", "events",cutOSSF&&limitnJetcut&&basiccut&&sidebandcut,mc,luminosity,allsamples.FindSample("LM4"));
467     LSBOSOFP = allsamples.Draw("LSBOSOFP",mcjzb,binning, "JZB4limits", "events",cutOSOF&&limitnJetcut&&basiccut&&sidebandcut,mc,luminosity,allsamples.FindSample("LM4"));
468     LSBOSSFN = allsamples.Draw("LSBOSSFN","-"+mcjzb,binning, "JZB4limits", "events",cutOSSF&&limitnJetcut&&basiccut&&sidebandcut,mc,luminosity,allsamples.FindSample("LM4"));
469     LSBOSOFN = allsamples.Draw("LSBOSOFN","-"+mcjzb,binning, "JZB4limits", "events",cutOSOF&&limitnJetcut&&basiccut&&sidebandcut,mc,luminosity,allsamples.FindSample("LM4"));
470     }
471    
472     string obsname="data_obs";
473     string predname="background";
474     string signalname="signal";
475     if(identifier!="") {
476     obsname=("data_"+identifier);
477     predname=("background_"+identifier);
478     signalname="signal_"+identifier;
479     }
480    
481     TH1F *obs = (TH1F*)ZOSSFP->Clone("observation");
482     obs->SetName(obsname.c_str());
483     obs->Write();
484     TH1F *pred = (TH1F*)ZOSSFN->Clone("prediction");
485     flag_this_change(__FUNCTION__,__LINE__,false);//PlottingSetup::RestrictToMassPeak
486 buchmann 1.7 if(PlottingSetup::RestrictToMassPeak&&PlottingSetup::UseSidebandsForcJZB) {
487 buchmann 1.1 pred->Add(ZOSOFP,1.0/3);
488     pred->Add(ZOSOFN,-1.0/3);
489     pred->Add(SBOSSFP,1.0/3);
490     pred->Add(SBOSSFN,-1.0/3);
491     pred->Add(SBOSOFP,1.0/3);
492     pred->Add(SBOSOFN,-1.0/3);
493     } else {
494     pred->Add(ZOSOFP,1.0);
495     pred->Add(ZOSOFN,-1.0);
496     }
497    
498     pred->SetName(predname.c_str());
499     pred->Write();
500    
501     // TH1F *Lobs = (TH1F*)LZOSSFP->Clone();
502     // TH1F *Lpred = (TH1F*)LZOSSFN->Clone();
503    
504     TH1F *Lobs = new TH1F("Lobs","Lobs",binning.size()-1,&binning[0]);
505     TH1F *Lpred = new TH1F("Lpred","Lpred",binning.size()-1,&binning[0]);
506     Lobs->Add(LZOSSFP);
507     Lpred->Add(LZOSSFN);
508     flag_this_change(__FUNCTION__,__LINE__,false);//PlottingSetup::RestrictToMassPeak
509 buchmann 1.7 if(PlottingSetup::RestrictToMassPeak&&PlottingSetup::UseSidebandsForcJZB) {
510 buchmann 1.1 Lpred->Add(LZOSOFP,1.0/3);
511     Lpred->Add(LZOSOFN,-1.0/3);
512     Lpred->Add(LSBOSSFP,1.0/3);
513     Lpred->Add(LSBOSSFN,-1.0/3);
514     Lpred->Add(LSBOSOFP,1.0/3);
515     Lpred->Add(LSBOSOFN,-1.0/3);
516     } else {
517     Lpred->Add(LZOSOFP,1.0);
518     Lpred->Add(LZOSOFN,-1.0);
519     }
520    
521     TH1F *signal = (TH1F*)Lobs->Clone();
522     signal->Add(Lpred,-1);
523     signal->SetName(signalname.c_str());
524     signal->Write();
525    
526     delete Lobs;
527     delete Lpred;
528    
529     delete ZOSSFP;
530     delete ZOSOFP;
531     delete ZOSSFN;
532     delete ZOSOFN;
533    
534 buchmann 1.7 if(PlottingSetup::RestrictToMassPeak&&PlottingSetup::UseSidebandsForcJZB) {
535 buchmann 1.1 delete SBOSSFP;
536     delete SBOSOFP;
537     delete SBOSSFN;
538     delete SBOSOFN;
539     }
540    
541     delete LZOSSFP;
542     delete LZOSOFP;
543     delete LZOSSFN;
544     delete LZOSOFN;
545    
546 buchmann 1.7 if(PlottingSetup::RestrictToMassPeak&&PlottingSetup::UseSidebandsForcJZB) {
547 buchmann 1.1 delete LSBOSSFP;
548     delete LSBOSOFP;
549     delete LSBOSSFN;
550     delete LSBOSOFN;
551     }
552    
553     }
554    
555     void prepare_datacard(TFile *f) {
556     TH1F *dataob = (TH1F*)f->Get("data_obs");
557     TH1F *signal = (TH1F*)f->Get("signal");
558     TH1F *background = (TH1F*)f->Get("background");
559    
560     ofstream datacard;
561     ensure_directory_exists(get_directory()+"/limits");
562     datacard.open ((get_directory()+"/limits/susydatacard.txt").c_str());
563     datacard << "Writing this to a file.\n";
564     datacard << "imax 1\n";
565     datacard << "jmax 1\n";
566     datacard << "kmax *\n";
567     datacard << "---------------\n";
568     datacard << "shapes * * limitfile.root $PROCESS $PROCESS_$SYSTEMATIC\n";
569     datacard << "---------------\n";
570     datacard << "bin 1\n";
571     datacard << "observation "<<dataob->Integral()<<"\n";
572     datacard << "------------------------------\n";
573     datacard << "bin 1 1\n";
574     datacard << "process signal background\n";
575     datacard << "process 0 1\n";
576     datacard << "rate "<<signal->Integral()<<" "<<background->Integral()<<"\n";
577     datacard << "--------------------------------\n";
578     datacard << "lumi lnN 1.10 1.0\n";
579     datacard << "bgnorm lnN 1.00 1.4 uncertainty on our prediction (40%)\n";
580     datacard << "JES shape 1 1 uncertainty on background shape and normalization\n";
581     datacard << "peak shape 1 1 uncertainty on signal resolution. Assume the histogram is a 2 sigma shift, \n";
582     datacard << "# so divide the unit gaussian by 2 before doing the interpolation\n";
583     datacard.close();
584     }
585    
586    
587     void prepare_limits(string mcjzb, string datajzb, float jzbpeakerrordata, float jzbpeakerrormc, vector<float> jzbbins) {
588     ensure_directory_exists(get_directory()+"/limits");
589     TFile *limfile = new TFile((get_directory()+"/limits/limitfile.root").c_str(),"RECREATE");
590     TCanvas *limcan = new TCanvas("limcan","Canvas for calculating limits");
591     limit_shapes_for_systematic_effect(limfile,"",mcjzb,datajzb,noJES,jzbbins,limcan);
592     limit_shapes_for_systematic_effect(limfile,"peakUp",newjzbexpression(mcjzb,jzbpeakerrormc),newjzbexpression(datajzb,jzbpeakerrordata),noJES,jzbbins,limcan);
593     limit_shapes_for_systematic_effect(limfile,"peakDown",newjzbexpression(mcjzb,-jzbpeakerrormc),newjzbexpression(datajzb,-jzbpeakerrordata),noJES,jzbbins,limcan);
594     limit_shapes_for_systematic_effect(limfile,"JESUp",mcjzb,datajzb,JESup,jzbbins,limcan);
595     limit_shapes_for_systematic_effect(limfile,"JESDown",mcjzb,datajzb,JESdown,jzbbins,limcan);
596    
597     prepare_datacard(limfile);
598     limfile->Close();
599     write_info("prepare_limits","limitfile.root and datacard.txt have been generated. You can now use them to calculate limits!");
600    
601     }