ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/Development/Plotting/Modules/LimitCalculation.C
Revision: 1.4
Committed: Wed Mar 21 22:01:32 2012 UTC (13 years, 1 month ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.3: +15 -0 lines
Log Message:
Quick commit of an idea to have individual limit setting sessions. Will remove it in a minute.

File Contents

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