ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/SUSYScan.C
Revision: 1.38
Committed: Tue Sep 27 08:09:32 2011 UTC (13 years, 7 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.37: +4 -8 lines
Log Message:
Corrected some book keeping variable ('ipoint')

File Contents

# User Rev Content
1 buchmann 1.1 #include <iostream>
2     #include <vector>
3     #include <sys/stat.h>
4     #include <fstream>
5 buchmann 1.7 #include <pthread.h>
6 buchmann 1.1
7     #include <TCut.h>
8     #include <TROOT.h>
9     #include <TCanvas.h>
10     #include <TMath.h>
11     #include <TColor.h>
12     #include <TPaveText.h>
13     #include <TRandom.h>
14     #include <TH1.h>
15     #include <TH2.h>
16     #include <TF1.h>
17     #include <TSQLResult.h>
18     #include <TProfile.h>
19 buchmann 1.31 #include <TKey.h>
20 buchmann 1.1
21     //#include "TTbar_stuff.C"
22     using namespace std;
23    
24     using namespace PlottingSetup;
25    
26     void susy_scan_axis_labeling(TH2F *histo) {
27 buchmann 1.37 histo->GetXaxis()->SetTitle("m_{#Chi_{2}^{0}}-m_{LSP}");
28 buchmann 1.1 histo->GetXaxis()->CenterTitle();
29     histo->GetYaxis()->SetTitle("m_{#tilde{q}}");
30     histo->GetYaxis()->CenterTitle();
31     }
32    
33 buchmann 1.7 bool isThreadActive=false;
34    
35     struct limit_args {
36     float mceff;
37     float toterr;
38     int ibin;
39     string mcjzb;
40     vector<float> sigmas;
41 buchmann 1.25 string plotfilename;
42 buchmann 1.35 };
43 buchmann 1.7
44     void* compute_one_upper_limit_wrapper(void* data) {
45     isThreadActive=true;
46     limit_args* args = (limit_args*) data;
47     std::cout << "Thread to compute limits has been started" << std::endl;
48 buchmann 1.25 args->sigmas=compute_one_upper_limit(args->mceff,args->toterr,args->ibin,args->mcjzb,args->plotfilename,true);
49 buchmann 1.7 std::cout << "Thread to compute limits has finished" << std::endl;
50     isThreadActive=false;
51 buchmann 1.37 return NULL;
52 buchmann 1.7 }
53    
54 buchmann 1.25 void do_limit_wrapper(float mceff,float toterr,int ibin,string mcjzb,vector<float> &sigmas,string plotfilename) {
55 buchmann 1.7 pthread_t limitthread;
56 buchmann 1.25 limit_args limargs={mceff,toterr,ibin,mcjzb,sigmas,plotfilename};
57 buchmann 1.7 pthread_create( &limitthread, NULL, compute_one_upper_limit_wrapper, (void*) &limargs);
58     int counter=0;
59 buchmann 1.29 int counterinterval=5;
60 buchmann 1.7 sleep(1); //waiting a second for the process to become active
61     while(counter<limitpatience*60 && isThreadActive) {
62 buchmann 1.38 std::cout << "Limits are being calculated; Checking round " << counter/counterinterval << " ( corresponds to " << seconds_to_time(counter) << " ) , patience will end in " << seconds_to_time(60*limitpatience-counter) << std::endl;
63 buchmann 1.29 counter+=counterinterval;
64     sleep(counterinterval);
65 buchmann 1.7 }
66    
67     if(!isThreadActive) {
68     cout << "Thread finished sucessfully" << endl;
69     pthread_join( limitthread, NULL );
70     std::cout<< __FUNCTION__ << " : going to save sigmas " << std::endl;
71     sigmas=limargs.sigmas;
72     } else {
73     pthread_cancel(limitthread);
74     std::cout << "DID NOT TERMINATE IN TIME - ABORTED!" << std::endl;
75     sigmas.push_back(-1);sigmas.push_back(-1);sigmas.push_back(-1);
76     }
77     }
78    
79 buchmann 1.22 void prepare_scan_axis(TH2 *variablemap,bool ismSUGRA) {
80 buchmann 1.9 variablemap->GetXaxis()->SetTitle("m_{glu}");
81     variablemap->GetYaxis()->SetTitle("m_{LSP}");
82 buchmann 1.35
83 buchmann 1.22 if(ismSUGRA) {
84     variablemap->GetXaxis()->SetTitle("m_{0}");
85     variablemap->GetYaxis()->SetTitle("m_{1/2}");
86     }
87 buchmann 1.35
88 buchmann 1.9 variablemap->GetXaxis()->CenterTitle();
89     variablemap->GetYaxis()->CenterTitle();
90     }
91    
92 buchmann 1.31 void set_SUSY_style() {
93     Int_t MyPalette[100];
94     Double_t r[] = {0., 0.0, 1.0, 1.0, 1.0};
95     Double_t g[] = {0., 0.0, 0.0, 1.0, 1.0};
96     Double_t b[] = {0., 1.0, 0.0, 0.0, 1.0};
97     Double_t stop[] = {0., .25, .50, .75, 1.0};
98     Int_t FI = TColor::CreateGradientColorTable(5, stop, r, g, b, 110);
99     for (int i=0;i<100;i++) MyPalette[i] = FI+i;
100 buchmann 1.35
101 buchmann 1.31 gStyle->SetPalette(100, MyPalette);
102 buchmann 1.35
103 buchmann 1.31 float rightmargin=gStyle->GetPadRightMargin();
104     gStyle->SetPadRightMargin(0.15);
105    
106     }
107    
108 buchmann 1.35 float get_xs(float mglu, float mlsp, string massgluname, string massLSPname, map < pair<float, float>, map<string, float> > &xsec, string mcjzb, bool requireZ) {
109     float weightedpointxs=0;
110     stringstream addcut;
111     addcut << "(TMath::Abs("<<massgluname<<"-"<<mglu<<")<5)&&(TMath::Abs("<<massLSPname<<"-"<<mlsp<<")<5)";
112     cout << "About to calculate the process efficiencies " << endl;
113 buchmann 1.37 /* for(int iproc=1;iproc<=10;iproc++) {
114 buchmann 1.35 float process_xs = GetXSecForPointAndChannel(mglu,mlsp,xsec,iproc);
115     stringstream addcutplus;
116     addcutplus<<addcut.str()<<"&&(pfJetGoodNum=="<<iproc<<")";
117     write_warning(__FUNCTION__,"replaced process with pfJetGoodNum for testing purposes only!");
118     (scansample.collection)[0].events->Draw("eventNum",addcutplus.str().c_str(),"goff");
119     float nprocessevents = (scansample.collection)[0].events->GetSelectedRows();
120     float nselectedprocessevents,nselectedprocesseventserr;
121     cout << "nprocessevents = " << nprocessevents << endl;
122     MCefficiency((scansample.collection)[0].events,nselectedprocessevents,nselectedprocesseventserr,mcjzb,requireZ,1,addcutplus.str(),-1);//1 is the number to normalize to :-)
123     float weight=0;
124     if(nprocessevents>0) weight=(process_xs)/(nprocessevents);//*luminosity;
125     weightedpointxs+=weight*nselectedprocessevents;
126     }
127     return weightedpointxs;
128 buchmann 1.37 */
129     return 1.0;
130 buchmann 1.35 }
131    
132 buchmann 1.31 void establish_SUSY_limits(string mcjzb,string datajzb,vector<float> jzb_cut,bool requireZ, float peakerror, TFile *fsyst, int ibin,float njobs=-1, float jobnumber=-1) {
133     bool runninglocally=true;
134     if(njobs>-1&&jobnumber>-1) {
135     runninglocally=false;
136     dout << "Running on the GRID (this is job " << jobnumber << "/" << njobs << ") for a jzb cut at " << jzb_cut[ibin] << endl;
137     } else {
138     dout << "Running locally " << endl;
139 buchmann 1.37 dout << "This will take a really, really long time - if you want to see the results within hours instead of weeks try running the worker script on the grid (DistributedModelCalculation/Limits/)" << endl;
140 buchmann 1.31 }
141 buchmann 1.35
142 buchmann 1.31 string massgluname="MassGlu";
143     string massLSPname="MassLSP";
144 buchmann 1.35
145 buchmann 1.31 string prefix="SMS_";
146     // up to here, everything is set up for SMS; now we need to switch stuff around if we're dealing with an mSUGRA scan!
147     bool ismSUGRA=false;
148     TIter nextkey(fsyst->GetListOfKeys());
149     TKey *key;
150     while ((key = (TKey*)nextkey()))
151     {
152     TObject *obj = key->ReadObj();
153     if(Contains((string)(obj->GetName()),"mSUGRA")) ismSUGRA=true;
154     }
155 buchmann 1.35
156     map < pair<float, float>, map<string, float> > xsec;
157 buchmann 1.31
158     if(ismSUGRA) {
159     massgluname="M0"; // this is the "x axis" in the limit plot (like the gluino in the SMS case)
160     massLSPname="M12"; // this is the "y axis" in the limit plot (like the LSP in the SMS case)
161     mglustart=m0start;
162 buchmann 1.35 xsec=getXsec("/scratch/buchmann/C/scale_xsection_nlo1.0_m0_m12_10_0_1v1.txt");
163     write_warning(__FUNCTION__,"Don't have the correct XS file yet");
164 buchmann 1.31 mgluend=m0end;
165     mglustep=m0step;
166     mLSPstart=m12start;
167     mLSPend=m12end;
168     mLSPstep=m12step;
169     prefix="mSUGRA_";
170     cout << "mSUGRA scan has been set up." << endl;
171     } else {
172     cout << "SMS scan has been set up." << endl;
173 buchmann 1.35 write_warning(__FUNCTION__,"Don't have the correct XS file yet");
174     xsec=getXsec("/scratch/buchmann/C/scale_xsection_nlo1.0_m0_m12_10_0_1v1.txt");
175 buchmann 1.31 }
176 buchmann 1.35
177 buchmann 1.31 set_SUSY_style();
178 buchmann 1.35
179 buchmann 1.31 TCanvas *limcanvas = new TCanvas("limcanvas","Limit canvas");
180 buchmann 1.35
181 buchmann 1.31 int Npoints=0;
182     for(int mglu=mglustart;mglu<=mgluend;mglu+=mglustep) {
183     for (int mlsp=mLSPstart;mlsp<=mLSPend&&mlsp<=mglu;mlsp+=mLSPstep) Npoints++;
184     }
185 buchmann 1.37 TH2F *mceff = (TH2F*)fsyst->Get((prefix+"efficiencymap"+any2string(jzb_cut[ibin])).c_str());
186     //write_warning(__FUNCTION__,"Currently the efficiencymap name was switched to Pablo's convention. This NEEDS to be switched BACK!");TH2F *mceff = (TH2F*)fsyst->Get(("efficiency_jzbdiff"+any2string(jzb_cut[ibin])).c_str());
187 buchmann 1.31 TH2F *fullerr = (TH2F*)fsyst->Get((prefix+"systotmap"+any2string(jzb_cut[ibin])).c_str());
188 buchmann 1.33 TH2F *NEvents = (TH2F*)fsyst->Get((prefix+"Neventsmap"+any2string(jzb_cut[ibin])).c_str());
189 buchmann 1.35
190 buchmann 1.37 TH2F *limitmap = new TH2F((prefix+"limitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//observed limit
191     TH2F *explimitmap = new TH2F((prefix+"explimitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//expected limit
192     TH2F *exp1plimitmap = new TH2F((prefix+"exp1plimitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//expected limit + 1 sigma
193     TH2F *exp2plimitmap = new TH2F((prefix+"exp2plimitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//expected limit + 2 sigma
194     TH2F *exp1mlimitmap = new TH2F((prefix+"exp1mlimitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//expected limit - 1 sigma
195     TH2F *exp2mlimitmap = new TH2F((prefix+"exp2mlimitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//expected limit - 2 sigma
196    
197 buchmann 1.32 TH2F *exclmap = new TH2F((prefix+"exclusionmap"+any2string(jzb_cut[ibin])).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
198     TH2F *xsmap = new TH2F((prefix+"crosssectionmap"+any2string(jzb_cut[ibin])).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
199 buchmann 1.35
200 buchmann 1.32 if(!fullerr || !mceff || !NEvents) {
201 buchmann 1.31 write_error(__FUNCTION__,"The supplied systematics file did not contain the correct histograms - please check the file");
202 buchmann 1.34 dout << "mc eff address: " << mceff << " , error address: " << fullerr << " , NEvents address: " << NEvents << endl;
203 buchmann 1.31 delete limcanvas;
204     return;
205     }
206 buchmann 1.37
207     bool doexpected=true;
208 buchmann 1.35
209 buchmann 1.31 int ipoint=-1;
210     for(int mglu=mglustart;mglu<=mgluend;mglu+=mglustep) {
211     for (int mlsp=mLSPstart;mlsp<=mLSPend&&mlsp<=mglu;mlsp+=mLSPstep)
212     {
213     ipoint++;
214     if(!runninglocally&&!do_this_point(ipoint,Npoints,jobnumber,njobs)) continue;
215     float currmceff=mceff->GetBinContent(mceff->FindBin(mglu,mlsp));
216 buchmann 1.33 float currtoterr=(fullerr->GetBinContent(fullerr->FindBin(mglu,mlsp)))*currmceff;
217 buchmann 1.32 float nevents=NEvents->GetBinContent(NEvents->FindBin(mglu,mlsp));
218 buchmann 1.34 dout << "Looking at point " << ipoint << " / " << Npoints << " with masses " << massgluname << " = " << mglu << " and " << massLSPname << " = " << mlsp << endl;
219 buchmann 1.35 dout << "Found : MCeff=" << currmceff << " and total error=" << currtoterr << " and Nevents=" << nevents << endl;
220 buchmann 1.31 string plotfilename=(string)(TString(massgluname)+TString(any2string(mglu))+TString("__")+TString(massLSPname)+TString(any2string(mlsp))+TString(".png"));
221 buchmann 1.34 if(currmceff<=0||currtoterr<=0||nevents==0) {
222     dout << " Nothing to work with, skipping this point." << endl;
223     continue;
224     }
225 buchmann 1.31 vector<float> sigmas;
226 buchmann 1.32 //do_limit_wrapper(currmceff,currtoterr,ibin,mcjzb,sigmas,plotfilename); // no threading right now.
227     sigmas=compute_one_upper_limit(currmceff,currtoterr,ibin,mcjzb,plotfilename,true);
228 buchmann 1.34 if(sigmas[0]>0) limitmap->Fill(mglu,mlsp,sigmas[0]); //anything else is an error code
229 buchmann 1.37 if(sigmas.size()>1) {
230     explimitmap->Fill(mglu,mlsp,sigmas[1]);
231     exp1plimitmap->Fill(mglu,mlsp,sigmas[2]);
232     exp1mlimitmap->Fill(mglu,mlsp,sigmas[3]);
233     exp2plimitmap->Fill(mglu,mlsp,sigmas[4]);
234     exp2mlimitmap->Fill(mglu,mlsp,sigmas[5]);
235     }
236    
237 buchmann 1.35 dout << "An upper limit has been added for this point ( " << massgluname << " = " << mglu << " and " << massLSPname << " = " << mlsp << " ) at " << sigmas[0] << endl;
238 buchmann 1.38 if(mSUGRA) { // for SMS this is a bit easier at the moment - we have a reference XS file which we use when plotting
239 buchmann 1.35 dout << "Computing exclusion status" << endl;
240     float rel_limit=0;
241     float xs=get_xs(mglu,mlsp,massgluname,massLSPname,xsec,mcjzb,requireZ);
242     if(xs>0) rel_limit=sigmas[0]/xs;
243     // stringstream addcut;
244     // addcut << "(TMath::Abs("<<massgluname<<"-"<<mglu<<")<5)&&(TMath::Abs("<<massLSPname<<"-"<<mlsp<<")<5)";
245     // vector<float> xs_weights = processMCefficiency((scansample.collection)[0].events,mcjzb,requireZ,nevents, addcut.str());
246     exclmap->Fill(mglu,mlsp,rel_limit);
247     xsmap->Fill(mglu,mlsp,xs);
248 buchmann 1.32 }
249 buchmann 1.31 }
250     }
251 buchmann 1.35
252 buchmann 1.31 prepare_scan_axis(limitmap,ismSUGRA);
253 buchmann 1.32 TFile *outputfile=new TFile(("output/DistributedLimitsFromSystematics_job"+string(any2string(jobnumber))+"_of_"+string(any2string(njobs))+".root").c_str(),"UPDATE");//needs to be "UPDATE" as we can get to this point for different JZB cuts and don't want to erase the previous data :-)
254     outputfile->cd();
255 buchmann 1.31 limitmap->Write();
256 buchmann 1.37 if(doexpected) {
257     explimitmap->Write();
258     exp1plimitmap->Write();
259     exp1mlimitmap->Write();
260     exp2plimitmap->Write();
261     exp2mlimitmap->Write();
262     }
263 buchmann 1.32 if(ismSUGRA) {
264     exclmap->Write();
265     xsmap->Write();
266     }
267     outputfile->Close();
268 buchmann 1.31 delete limcanvas;
269     }
270    
271     void establish_SUSY_limits(string mcjzb,string datajzb,vector<float> jzb_cut,bool requireZ, float peakerror, string fsystfilename, float njobs=-1, float jobnumber=-1) {
272     dout << "Starting the SUSY scan from systematics file now with all " << jzb_cut.size() << " bin(s)" << " and using " << fsystfilename << endl;
273     TFile *fsyst = new TFile(fsystfilename.c_str());
274     if(!fsyst) {
275     write_error(__FUNCTION__,"You provided an invalid systematics file. Please change it ... ");
276     return;
277     }
278     for(int ibin=0;ibin<jzb_cut.size();ibin++) {
279     establish_SUSY_limits(mcjzb,datajzb,jzb_cut,requireZ, peakerror, fsyst, ibin,njobs, jobnumber);
280     }
281     }
282    
283    
284    
285    
286 buchmann 1.13 void scan_SUSY_parameter_space(string mcjzb,string datajzb,vector<float> jzb_cut,bool requireZ, float peakerror, int ibin,float njobs=-1, float jobnumber=-1, bool systematicsonly=false,bool efficiencyonly=false) {
287 buchmann 1.6 bool runninglocally=true;
288     if(njobs>-1&&jobnumber>-1) {
289     runninglocally=false;
290 buchmann 1.13 dout << "Running on the GRID (this is job " << jobnumber << "/" << njobs << ") for a jzb cut at " << jzb_cut[ibin] << endl;
291 buchmann 1.6 } else {
292 buchmann 1.7 dout << "Running locally " << endl;
293     dout << "This will take a really, really long time - if you want to see the results THIS week try running the LimitWorkerScript on the grid (DistributedModelCalculation/Limits/)" << endl;
294 buchmann 1.6 }
295 buchmann 1.35
296 buchmann 1.18 cout << "FILE USED: " << (scansample.collection)[0].filename << endl;
297 buchmann 1.24 cout << "FILE USED (name): " << (scansample.collection)[0].samplename << endl;
298 buchmann 1.4 jzbSel=jzb_cut[ibin];
299     geqleq="geq";
300     automatized=true;
301     mcjzbexpression=mcjzb;
302 buchmann 1.35
303 buchmann 1.4 string massgluname="MassGlu";
304     string massLSPname="MassLSP";
305 buchmann 1.35
306 buchmann 1.23 string prefix="SMS_";
307 buchmann 1.22 // up to here, everything is set up for SMS; now we need to switch stuff around if we're dealing with an mSUGRA scan!
308     bool ismSUGRA=false;
309 buchmann 1.24 if(Contains((scansample.collection)[0].samplename,"mSUGRA")) ismSUGRA=true;
310 buchmann 1.22 if(ismSUGRA) {
311     massgluname="M0"; // this is the "x axis" in the limit plot (like the gluino in the SMS case)
312     massLSPname="M12"; // this is the "y axis" in the limit plot (like the LSP in the SMS case)
313     mglustart=m0start;
314     mgluend=m0end;
315     mglustep=m0step;
316     mLSPstart=m12start;
317     mLSPend=m12end;
318     mLSPstep=m12step;
319 buchmann 1.23 prefix="mSUGRA_";
320 buchmann 1.24 cout << "mSUGRA scan has been set up." << endl;
321     } else {
322     cout << "SMS scan has been set up." << endl;
323     }
324 buchmann 1.35
325 buchmann 1.4 Int_t MyPalette[100];
326     Double_t r[] = {0., 0.0, 1.0, 1.0, 1.0};
327     Double_t g[] = {0., 0.0, 0.0, 1.0, 1.0};
328     Double_t b[] = {0., 1.0, 0.0, 0.0, 1.0};
329     Double_t stop[] = {0., .25, .50, .75, 1.0};
330     Int_t FI = TColor::CreateGradientColorTable(5, stop, r, g, b, 110);
331     for (int i=0;i<100;i++) MyPalette[i] = FI+i;
332 buchmann 1.35
333 buchmann 1.4 gStyle->SetPalette(100, MyPalette);
334 buchmann 1.35
335 buchmann 1.37 TH2F *limitmap = new TH2F((prefix+"limitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//observed limit
336     TH2F *explimitmap = new TH2F((prefix+"explimitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//expected limit
337     TH2F *exp1plimitmap = new TH2F((prefix+"exp1plimitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//expected limit + 1 sigma
338     TH2F *exp2plimitmap = new TH2F((prefix+"exp2plimitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//expected limit + 2 sigma
339     TH2F *exp1mlimitmap = new TH2F((prefix+"exp1mlimitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//expected limit - 1 sigma
340     TH2F *exp2mlimitmap = new TH2F((prefix+"exp2mlimitmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);//expected limit - 2 sigma
341    
342     TH2F *exclmap = new TH2F((prefix+"exclusionmap"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
343     TH2F *sysjesmap = new TH2F((prefix+"sysjes"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
344     TH2F *sysjsumap = new TH2F((prefix+"sysjsu"+any2string(jzbSel)).c_str(), "",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
345     TH2F *sysresmap = new TH2F((prefix+"sysresmap"+any2string(jzbSel)).c_str(),"",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
346     TH2F *efficiencymap = new TH2F((prefix+"efficiencymap"+any2string(jzbSel)).c_str(),"",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
347     TH2F *noscefficiencymap = new TH2F((prefix+"nosc_efficiencymap"+any2string(jzbSel)).c_str(),"",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
348     TH2F *Neventsmap = new TH2F((prefix+"Neventsmap"+any2string(jzbSel)).c_str(),"", (mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
349 buchmann 1.38 TH2F *ipointmap = new TH2F((prefix+"ipointmap"+any2string(jzbSel)).c_str(),"", (mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
350 buchmann 1.37 TH2F *syspdfmap = new TH2F((prefix+"syspdfmap"+any2string(jzbSel)).c_str(),"",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
351     TH2F *systotmap = new TH2F((prefix+"systotmap"+any2string(jzbSel)).c_str(),"",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
352     TH2F *sysstatmap = new TH2F((prefix+"sysstatmap"+any2string(jzbSel)).c_str(),"",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
353 buchmann 1.27
354 buchmann 1.37 TH2F *timemap = new TH2F((prefix+"timemap"+any2string(jzbSel)).c_str(),"",(mgluend-mglustart)/mglustep+1,mglustart-0.5*mglustep,mgluend+0.5*mglustep,(mLSPend-mLSPstart)/mLSPstep+1,mLSPstart-0.5*mLSPstep,mLSPend+0.5*mLSPstep);
355 buchmann 1.36
356 buchmann 1.29 write_warning(__FUNCTION__,"CURRENTLY SWITCHING AUTOMATIZED MODE OFF!");automatized=false;
357 buchmann 1.35
358 buchmann 1.4 float rightmargin=gStyle->GetPadRightMargin();
359     gStyle->SetPadRightMargin(0.15);
360 buchmann 1.35
361 buchmann 1.4 TCanvas *limcanvas = new TCanvas("limcanvas","Limit canvas");
362 buchmann 1.35
363 buchmann 1.37
364     bool doexpected=true;
365    
366 buchmann 1.6 int Npoints=0;
367     for(int mglu=mglustart;mglu<=mgluend;mglu+=mglustep) {
368     for (int mlsp=mLSPstart;mlsp<=mLSPend&&mlsp<=mglu;mlsp+=mLSPstep) Npoints++;
369     }
370 buchmann 1.35
371 buchmann 1.6 int ipoint=-1;
372 buchmann 1.4 for(int mglu=mglustart;mglu<=mgluend;mglu+=mglustep) {
373     for (int mlsp=mLSPstart;mlsp<=mLSPend&&mlsp<=mglu;mlsp+=mLSPstep)
374     {
375 buchmann 1.6 ipoint++;
376     if(!runninglocally&&!do_this_point(ipoint,Npoints,jobnumber,njobs)) continue;
377 buchmann 1.19 float result=-987,resulterr=-987;
378 buchmann 1.36 clock_t start,finish;
379     start = clock(); // starting the clock to measure how long the computation takes!
380 buchmann 1.4 stringstream addcut;
381     addcut << "(TMath::Abs("<<massgluname<<"-"<<mglu<<")<5)&&(TMath::Abs("<<massLSPname<<"-"<<mlsp<<")<5)";
382 buchmann 1.14 (scansample.collection)[0].events->Draw("eventNum",addcut.str().c_str(),"goff");
383     float nevents = (scansample.collection)[0].events->GetSelectedRows();
384 buchmann 1.4 vector<vector<float> > systematics;
385 buchmann 1.30 if(nevents<10) {
386 buchmann 1.35 dout << "This point ("<<ipoint<<") with configuration ("<<massgluname<<"="<<mglu<<" , "<<massLSPname<<"="<<mlsp << ") does not contain enough events and will be skipped."<< endl;
387     continue;
388 buchmann 1.6 } else {
389 buchmann 1.35 dout << "OK! This point ("<<ipoint<<") with configuration ("<<massgluname<<"="<<mglu<<" , "<<massLSPname<<"="<<mlsp << ") contains " << nevents << " and will therefore not be skipped."<< endl;
390 buchmann 1.22 }
391     if(nevents!=0&&efficiencyonly) {
392 buchmann 1.37 Value effwosigcont = MCefficiency((scansample.collection)[0].events,result,resulterr,mcjzb,requireZ,nevents,addcut.str(),-1);
393 buchmann 1.36 efficiencymap->Fill(mglu,mlsp,result);
394 buchmann 1.37 noscefficiencymap->Fill(mglu,mlsp,effwosigcont.getValue());
395     noscefficiencymap->SetBinError(mglu,mlsp,effwosigcont.getError());
396 buchmann 1.36 finish = clock();
397     timemap->Fill(mglu,mlsp,((float(finish)-float(start))/CLOCKS_PER_SEC));
398 buchmann 1.13 }
399 buchmann 1.17 Neventsmap->Fill(mglu,mlsp,nevents);
400 buchmann 1.24 ipointmap->Fill(mglu,mlsp,ipoint);
401 buchmann 1.17 if(efficiencyonly) continue;
402    
403 buchmann 1.22 do_systematics_for_one_file((scansample.collection)[0].events,nevents,"SUSY SCAN", systematics,mcjzb,datajzb,peakerror,requireZ, addcut.str(),ismSUGRA);
404 buchmann 1.9 float JZBcutat = systematics[0][0];
405     float mceff = systematics[0][1];
406 buchmann 1.22 float mcefferr = systematics[0][2];//MC stat error
407 buchmann 1.9 float toterr = systematics[0][4];
408     float sys_jes = systematics[0][5]; // Jet Energy Scale
409     float sys_jsu = systematics[0][6]; // JZB scale uncertainty
410     float sys_res = systematics[0][7]; // resolution
411 buchmann 1.37 float mcwoscef = systematics[0][8]; // efficiency without signal contamination
412     float mcwoscefr= systematics[0][9]; // error on efficiency without signal contamination
413 buchmann 1.27 float sys_pdf = 0;
414 buchmann 1.37 if(systematics[0].size()>10) sys_pdf = systematics[0][10]; // PDF
415 buchmann 1.22 efficiencymap->Fill(mglu,mlsp,mceff);
416 buchmann 1.37 efficiencymap->SetBinError(efficiencymap->FindBin(mglu,mlsp),mcefferr);
417     noscefficiencymap->Fill(mglu,mlsp,mcwoscef);
418     noscefficiencymap->SetBinError(efficiencymap->FindBin(mglu,mlsp),mcwoscefr);
419 buchmann 1.35
420 buchmann 1.15 if(mceff!=mceff||toterr!=toterr||mceff<0) {
421 buchmann 1.35 dout << "Limits can't be calculated for this configuration (mglu="<<mglu<<" , mlsp="<<mlsp << ") as either the efficiency or its error are not positive numbers! (mceff="<<mceff<<" and toterr="<<toterr<<")"<< endl;
422     continue;
423 buchmann 1.5 } else {
424 buchmann 1.35 if(!systematicsonly&&!efficiencyonly) {
425     dout << "Calculating limit now for "<<massgluname<<"="<<mglu<<" , "<<massLSPname<<"="<<mlsp <<endl;
426     vector<float> sigmas;
427     string plotfilename=(string)(TString((scansample.collection)[0].samplename)+TString(massgluname)+TString(any2string(mglu))+TString("__")+TString(massLSPname)+TString(any2string(mlsp))+TString(".png"));
428     do_limit_wrapper(mceff,toterr,ibin,mcjzb,sigmas,plotfilename);
429     cout << "back in " << __FUNCTION__ << endl;
430     if(sigmas[0]>-0.5) { // negative sigmas are the error signature of do_limit_wrapper, so we want to exclude them.
431     limitmap->Fill(mglu,mlsp,sigmas[0]);
432 buchmann 1.37 if(sigmas.size()>1) {
433     explimitmap->Fill(mglu,mlsp,sigmas[1]);
434     exp1plimitmap->Fill(mglu,mlsp,sigmas[2]);
435     exp1mlimitmap->Fill(mglu,mlsp,sigmas[3]);
436     exp2plimitmap->Fill(mglu,mlsp,sigmas[4]);
437     exp2mlimitmap->Fill(mglu,mlsp,sigmas[5]);
438 buchmann 1.38 }
439 buchmann 1.37
440 buchmann 1.35 sysjesmap->Fill(mglu,mlsp,sys_jes);
441     sysjsumap->Fill(mglu,mlsp,sys_jsu);
442     sysresmap->Fill(mglu,mlsp,sys_res);
443     syspdfmap->Fill(mglu,mlsp,sys_pdf);
444     systotmap->Fill(mglu,mlsp,toterr/mceff);//total relative (!) error
445     sysstatmap->Fill(mglu,mlsp,mcefferr);//total relative (!) error
446     dout << "A limit has been added at " << sigmas[0] << " for m_{glu}="<<mglu << " and m_{lsp}="<<mlsp<<endl;
447     } //end of if sigma is positive
448 buchmann 1.36 finish = clock();
449     timemap->Fill(mglu,mlsp,((float(finish)-float(start))/CLOCKS_PER_SEC));
450    
451 buchmann 1.35 //end of not systematics only condition
452     }
453     if(systematicsonly) {
454     sysjesmap->Fill(mglu,mlsp,sys_jes);
455     sysjsumap->Fill(mglu,mlsp,sys_jsu);
456     sysresmap->Fill(mglu,mlsp,sys_res);
457     syspdfmap->Fill(mglu,mlsp,sys_pdf);
458     systotmap->Fill(mglu,mlsp,toterr/mceff);//total relative (!) error
459     sysstatmap->Fill(mglu,mlsp,mcefferr);//total relative (!) error
460 buchmann 1.36 finish = clock();
461     timemap->Fill(mglu,mlsp,((float(finish)-float(start))/CLOCKS_PER_SEC));
462 buchmann 1.35 }
463 buchmann 1.15 }//efficiency is valid
464 buchmann 1.4 }
465     }
466 buchmann 1.35
467 buchmann 1.22 prepare_scan_axis(limitmap,ismSUGRA);
468     prepare_scan_axis(sysjesmap,ismSUGRA);
469     prepare_scan_axis(sysjsumap,ismSUGRA);
470     prepare_scan_axis(sysresmap,ismSUGRA);
471 buchmann 1.27 prepare_scan_axis(syspdfmap,ismSUGRA);
472     prepare_scan_axis(systotmap,ismSUGRA);
473     prepare_scan_axis(sysstatmap,ismSUGRA);
474 buchmann 1.36 prepare_scan_axis(timemap,ismSUGRA);
475 buchmann 1.35
476 buchmann 1.13 if(!systematicsonly&&!efficiencyonly) {
477 buchmann 1.9 limcanvas->cd();
478     limitmap->Draw("COLZ");
479 buchmann 1.22 string titletobewritten="Limits in LSP-Glu plane";
480     if(ismSUGRA) titletobewritten="Limits in m_{1/2}-m_{0} plane";
481     TText *title = write_title(titletobewritten);
482 buchmann 1.9 title->Draw();
483     if(runninglocally) {
484     CompleteSave(limcanvas,"SUSYScan/Limits_JZB_geq"+any2string(jzb_cut[ibin]));
485     } else {
486 buchmann 1.12 TFile *outputfile;
487     if(systematicsonly) outputfile=new TFile(("output/DistributedSystematics_job"+string(any2string(jobnumber))+"_of_"+string(any2string(njobs))+".root").c_str(),"UPDATE");//needs to be "UPDATE" as we can get to this point for different JZB cuts and don't want to erase the previous data :-)
488 buchmann 1.35 else outputfile=new TFile(("output/DistributedLimits_job"+string(any2string(jobnumber))+"_of_"+string(any2string(njobs))+".root").c_str(),"UPDATE");//needs to be "UPDATE" as we can get to this point for
489 buchmann 1.9 limitmap->Write();
490 buchmann 1.37 if(doexpected) {
491     explimitmap->Write();
492     exp1plimitmap->Write();
493     exp1mlimitmap->Write();
494     exp2plimitmap->Write();
495     exp2mlimitmap->Write();
496     }
497 buchmann 1.18 sysjesmap->Write();
498     sysjsumap->Write();
499 buchmann 1.28 sysresmap->Write();
500 buchmann 1.18 efficiencymap->Write();
501 buchmann 1.37 noscefficiencymap->Write();
502 buchmann 1.27 syspdfmap->Write();
503     systotmap->Write();
504     sysstatmap->Write();
505 buchmann 1.18 Neventsmap->Write();
506 buchmann 1.24 ipointmap->Write();
507 buchmann 1.36 timemap->Write();
508 buchmann 1.9 outputfile->Close();
509     }
510 buchmann 1.35 }
511 buchmann 1.13 if(systematicsonly) { // systematics only :
512 buchmann 1.9 limcanvas->cd();
513     sysjesmap->Draw("COLZ");
514 buchmann 1.22 string titletobewritten="Jet Energy scale in LSP-Glu plane";
515     if(ismSUGRA) titletobewritten="Limits in m_{1/2}-m_{0} plane";
516     TText *title = write_title(titletobewritten);
517 buchmann 1.9 title->Draw();
518     if(runninglocally) {
519     CompleteSave(limcanvas,"SUSYScan/JES_geq"+any2string(jzb_cut[ibin]));
520     }
521     sysjsumap->Draw("COLZ");
522 buchmann 1.22 titletobewritten="JZB Scale Uncertainty in LSP-Glu plane";
523     if(ismSUGRA) titletobewritten="JZB Scale Uncertainty in m_{1/2}-m_{0} plane";
524     TText *title2 = write_title(titletobewritten);
525 buchmann 1.9 title2->Draw();
526     if(runninglocally) {
527     CompleteSave(limcanvas,"SUSYScan/JSU_geq"+any2string(jzb_cut[ibin]));
528     }
529     sysresmap->Draw("COLZ");
530 buchmann 1.22 titletobewritten="Resolution in LSP-Glu plane";
531     if(ismSUGRA) titletobewritten="Resolution in m_{1/2}-m_{0} plane";
532     TText *title3 = write_title(titletobewritten);
533 buchmann 1.9 title3->Draw();
534     if(runninglocally) {
535     CompleteSave(limcanvas,"SUSYScan/Resolution_geq"+any2string(jzb_cut[ibin]));
536     }
537 buchmann 1.35
538 buchmann 1.9 if(!runninglocally) {
539     TFile *outputfile=new TFile(("output/DistributedSystematics_job"+string(any2string(jobnumber))+"_of_"+string(any2string(njobs))+".root").c_str(),"UPDATE");
540     sysjesmap->Write();
541     sysjsumap->Write();
542     sysresmap->Write();
543 buchmann 1.18 efficiencymap->Write();
544 buchmann 1.37 noscefficiencymap->Write();
545 buchmann 1.18 Neventsmap->Write();
546 buchmann 1.24 ipointmap->Write();
547 buchmann 1.27 syspdfmap->Write();
548     systotmap->Write();
549     sysstatmap->Write();
550 buchmann 1.36 timemap->Write();
551 buchmann 1.9 outputfile->Close();
552     }
553     }//end of systematics only
554 buchmann 1.13 if(efficiencyonly) {
555     limcanvas->cd();
556     efficiencymap->Draw("COLZ");
557 buchmann 1.22 string titletobewritten="Efficiencies in LSP-Glu plane";
558     if(ismSUGRA) titletobewritten="Efficiencies in m_{1/2}-m_{0} plane";
559     TText *title = write_title(titletobewritten);
560 buchmann 1.13 title->Draw();
561     if(runninglocally) {
562     CompleteSave(limcanvas,"SUSYScan/Efficiency_geq"+any2string(jzb_cut[ibin]));
563     }
564     limcanvas->cd();
565     sysjesmap->Draw("COLZ");
566 buchmann 1.22 titletobewritten="N(events) in LSP-Glu plane";
567     if(ismSUGRA) titletobewritten="N(events) in m_{1/2}-m_{0} plane";
568     TText *title2 = write_title(titletobewritten);
569 buchmann 1.13 title2->Draw();
570     if(runninglocally) {
571     CompleteSave(limcanvas,"SUSYScan/Nevents_geq"+any2string(jzb_cut[ibin]));
572     }
573     if(!runninglocally) {
574     TFile *outputfile=new TFile(("output/DistributedSystematics_job"+string(any2string(jobnumber))+"_of_"+string(any2string(njobs))+".root").c_str(),"UPDATE");
575 buchmann 1.24 ipointmap->Write();
576 buchmann 1.13 Neventsmap->Write();
577 buchmann 1.37 noscefficiencymap->Write();
578 buchmann 1.13 efficiencymap->Write();
579 buchmann 1.36 timemap->Write();
580 buchmann 1.13 outputfile->Close();
581     }
582     }//end of efficiencies only
583 buchmann 1.35
584 buchmann 1.31 delete limitmap;
585     delete exclmap;
586     delete sysjesmap;
587     delete sysjsumap;
588     delete sysresmap;
589 buchmann 1.37 delete noscefficiencymap;
590 buchmann 1.31 delete efficiencymap;
591     delete Neventsmap;
592     delete ipointmap;
593     delete syspdfmap;
594     delete systotmap;
595     delete sysstatmap;
596 buchmann 1.13 delete limcanvas;
597 buchmann 1.1 }
598 buchmann 1.4
599 buchmann 1.13 void scan_SUSY_parameter_space(string mcjzb,string datajzb,vector<float> jzb_cut,bool requireZ, float peakerror, float njobs=-1, float jobnumber=-1, bool systonly=false, bool effonly=false) {
600 buchmann 1.10 dout << "Starting the SUSY scan now with all " << jzb_cut.size() << " bin(s)" << endl;
601 buchmann 1.4 for(int ibin=0;ibin<jzb_cut.size();ibin++) {
602 buchmann 1.13 scan_SUSY_parameter_space(mcjzb,datajzb,jzb_cut,requireZ, peakerror, ibin, njobs, jobnumber,systonly,effonly);
603 buchmann 1.4 }
604 buchmann 1.6 }
605 buchmann 1.35