ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/SUSYScan.C
Revision: 1.42
Committed: Wed Sep 28 19:45:35 2011 UTC (13 years, 7 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.41: +20 -7 lines
Log Message:
Loading only the scan files we need and not all of them; this optimizes both memory usage and startup times

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.39 jzbSel=jzb_cut[ibin];
145     geqleq="geq";
146     automatized=true;
147     mcjzbexpression=mcjzb;
148    
149 buchmann 1.31 string prefix="SMS_";
150     // up to here, everything is set up for SMS; now we need to switch stuff around if we're dealing with an mSUGRA scan!
151     bool ismSUGRA=false;
152     TIter nextkey(fsyst->GetListOfKeys());
153     TKey *key;
154     while ((key = (TKey*)nextkey()))
155     {
156     TObject *obj = key->ReadObj();
157     if(Contains((string)(obj->GetName()),"mSUGRA")) ismSUGRA=true;
158     }
159 buchmann 1.35
160     map < pair<float, float>, map<string, float> > xsec;
161 buchmann 1.31
162     if(ismSUGRA) {
163     massgluname="M0"; // this is the "x axis" in the limit plot (like the gluino in the SMS case)
164     massLSPname="M12"; // this is the "y axis" in the limit plot (like the LSP in the SMS case)
165     mglustart=m0start;
166 buchmann 1.35 xsec=getXsec("/scratch/buchmann/C/scale_xsection_nlo1.0_m0_m12_10_0_1v1.txt");
167     write_warning(__FUNCTION__,"Don't have the correct XS file yet");
168 buchmann 1.31 mgluend=m0end;
169     mglustep=m0step;
170     mLSPstart=m12start;
171     mLSPend=m12end;
172     mLSPstep=m12step;
173     prefix="mSUGRA_";
174     cout << "mSUGRA scan has been set up." << endl;
175     } else {
176     cout << "SMS scan has been set up." << endl;
177 buchmann 1.35 write_warning(__FUNCTION__,"Don't have the correct XS file yet");
178     xsec=getXsec("/scratch/buchmann/C/scale_xsection_nlo1.0_m0_m12_10_0_1v1.txt");
179 buchmann 1.31 }
180 buchmann 1.35
181 buchmann 1.31 set_SUSY_style();
182 buchmann 1.35
183 buchmann 1.31 TCanvas *limcanvas = new TCanvas("limcanvas","Limit canvas");
184 buchmann 1.35
185 buchmann 1.31 int Npoints=0;
186     for(int mglu=mglustart;mglu<=mgluend;mglu+=mglustep) {
187     for (int mlsp=mLSPstart;mlsp<=mLSPend&&mlsp<=mglu;mlsp+=mLSPstep) Npoints++;
188     }
189 buchmann 1.37 TH2F *mceff = (TH2F*)fsyst->Get((prefix+"efficiencymap"+any2string(jzb_cut[ibin])).c_str());
190     //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());
191 buchmann 1.31 TH2F *fullerr = (TH2F*)fsyst->Get((prefix+"systotmap"+any2string(jzb_cut[ibin])).c_str());
192 buchmann 1.33 TH2F *NEvents = (TH2F*)fsyst->Get((prefix+"Neventsmap"+any2string(jzb_cut[ibin])).c_str());
193 buchmann 1.35
194 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
195     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
196     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
197     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
198     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
199     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
200    
201 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);
202     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);
203 buchmann 1.35
204 buchmann 1.32 if(!fullerr || !mceff || !NEvents) {
205 buchmann 1.31 write_error(__FUNCTION__,"The supplied systematics file did not contain the correct histograms - please check the file");
206 buchmann 1.34 dout << "mc eff address: " << mceff << " , error address: " << fullerr << " , NEvents address: " << NEvents << endl;
207 buchmann 1.31 delete limcanvas;
208     return;
209     }
210 buchmann 1.37
211     bool doexpected=true;
212 buchmann 1.35
213 buchmann 1.31 int ipoint=-1;
214     for(int mglu=mglustart;mglu<=mgluend;mglu+=mglustep) {
215     for (int mlsp=mLSPstart;mlsp<=mLSPend&&mlsp<=mglu;mlsp+=mLSPstep)
216     {
217     ipoint++;
218     if(!runninglocally&&!do_this_point(ipoint,Npoints,jobnumber,njobs)) continue;
219     float currmceff=mceff->GetBinContent(mceff->FindBin(mglu,mlsp));
220 buchmann 1.33 float currtoterr=(fullerr->GetBinContent(fullerr->FindBin(mglu,mlsp)))*currmceff;
221 buchmann 1.32 float nevents=NEvents->GetBinContent(NEvents->FindBin(mglu,mlsp));
222 buchmann 1.34 dout << "Looking at point " << ipoint << " / " << Npoints << " with masses " << massgluname << " = " << mglu << " and " << massLSPname << " = " << mlsp << endl;
223 buchmann 1.35 dout << "Found : MCeff=" << currmceff << " and total error=" << currtoterr << " and Nevents=" << nevents << endl;
224 buchmann 1.31 string plotfilename=(string)(TString(massgluname)+TString(any2string(mglu))+TString("__")+TString(massLSPname)+TString(any2string(mlsp))+TString(".png"));
225 buchmann 1.34 if(currmceff<=0||currtoterr<=0||nevents==0) {
226     dout << " Nothing to work with, skipping this point." << endl;
227     continue;
228     }
229 buchmann 1.31 vector<float> sigmas;
230 buchmann 1.32 //do_limit_wrapper(currmceff,currtoterr,ibin,mcjzb,sigmas,plotfilename); // no threading right now.
231     sigmas=compute_one_upper_limit(currmceff,currtoterr,ibin,mcjzb,plotfilename,true);
232 buchmann 1.34 if(sigmas[0]>0) limitmap->Fill(mglu,mlsp,sigmas[0]); //anything else is an error code
233 buchmann 1.37 if(sigmas.size()>1) {
234     explimitmap->Fill(mglu,mlsp,sigmas[1]);
235     exp1plimitmap->Fill(mglu,mlsp,sigmas[2]);
236     exp1mlimitmap->Fill(mglu,mlsp,sigmas[3]);
237     exp2plimitmap->Fill(mglu,mlsp,sigmas[4]);
238     exp2mlimitmap->Fill(mglu,mlsp,sigmas[5]);
239     }
240    
241 buchmann 1.35 dout << "An upper limit has been added for this point ( " << massgluname << " = " << mglu << " and " << massLSPname << " = " << mlsp << " ) at " << sigmas[0] << endl;
242 buchmann 1.39 if(ismSUGRA) { // for SMS this is a bit easier at the moment - we have a reference XS file which we use when plotting
243 buchmann 1.35 dout << "Computing exclusion status" << endl;
244     float rel_limit=0;
245     float xs=get_xs(mglu,mlsp,massgluname,massLSPname,xsec,mcjzb,requireZ);
246     if(xs>0) rel_limit=sigmas[0]/xs;
247     // stringstream addcut;
248     // addcut << "(TMath::Abs("<<massgluname<<"-"<<mglu<<")<5)&&(TMath::Abs("<<massLSPname<<"-"<<mlsp<<")<5)";
249     // vector<float> xs_weights = processMCefficiency((scansample.collection)[0].events,mcjzb,requireZ,nevents, addcut.str());
250     exclmap->Fill(mglu,mlsp,rel_limit);
251     xsmap->Fill(mglu,mlsp,xs);
252 buchmann 1.32 }
253 buchmann 1.31 }
254     }
255 buchmann 1.35
256 buchmann 1.31 prepare_scan_axis(limitmap,ismSUGRA);
257 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 :-)
258     outputfile->cd();
259 buchmann 1.31 limitmap->Write();
260 buchmann 1.37 if(doexpected) {
261     explimitmap->Write();
262     exp1plimitmap->Write();
263     exp1mlimitmap->Write();
264     exp2plimitmap->Write();
265     exp2mlimitmap->Write();
266     }
267 buchmann 1.32 if(ismSUGRA) {
268     exclmap->Write();
269     xsmap->Write();
270     }
271     outputfile->Close();
272 buchmann 1.31 delete limcanvas;
273     }
274    
275     void establish_SUSY_limits(string mcjzb,string datajzb,vector<float> jzb_cut,bool requireZ, float peakerror, string fsystfilename, float njobs=-1, float jobnumber=-1) {
276     dout << "Starting the SUSY scan from systematics file now with all " << jzb_cut.size() << " bin(s)" << " and using " << fsystfilename << endl;
277     TFile *fsyst = new TFile(fsystfilename.c_str());
278     if(!fsyst) {
279     write_error(__FUNCTION__,"You provided an invalid systematics file. Please change it ... ");
280     return;
281     }
282     for(int ibin=0;ibin<jzb_cut.size();ibin++) {
283     establish_SUSY_limits(mcjzb,datajzb,jzb_cut,requireZ, peakerror, fsyst, ibin,njobs, jobnumber);
284     }
285     }
286    
287    
288    
289    
290 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) {
291 buchmann 1.6 bool runninglocally=true;
292     if(njobs>-1&&jobnumber>-1) {
293     runninglocally=false;
294 buchmann 1.13 dout << "Running on the GRID (this is job " << jobnumber << "/" << njobs << ") for a jzb cut at " << jzb_cut[ibin] << endl;
295 buchmann 1.6 } else {
296 buchmann 1.7 dout << "Running locally " << endl;
297 buchmann 1.40 dout << "This will take a really, really long time - if you want to see the results THIS month try running the LimitWorkerScript on the grid (DistributedModelCalculation/Limits/)" << endl;
298 buchmann 1.6 }
299 buchmann 1.35
300 buchmann 1.4 jzbSel=jzb_cut[ibin];
301     geqleq="geq";
302     automatized=true;
303     mcjzbexpression=mcjzb;
304 buchmann 1.35
305 buchmann 1.4 string massgluname="MassGlu";
306     string massLSPname="MassLSP";
307 buchmann 1.35
308 buchmann 1.23 string prefix="SMS_";
309 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!
310     bool ismSUGRA=false;
311 buchmann 1.24 if(Contains((scansample.collection)[0].samplename,"mSUGRA")) ismSUGRA=true;
312 buchmann 1.22 if(ismSUGRA) {
313     massgluname="M0"; // this is the "x axis" in the limit plot (like the gluino in the SMS case)
314     massLSPname="M12"; // this is the "y axis" in the limit plot (like the LSP in the SMS case)
315     mglustart=m0start;
316     mgluend=m0end;
317     mglustep=m0step;
318     mLSPstart=m12start;
319     mLSPend=m12end;
320     mLSPstep=m12step;
321 buchmann 1.23 prefix="mSUGRA_";
322 buchmann 1.24 cout << "mSUGRA scan has been set up." << endl;
323     } else {
324     cout << "SMS scan has been set up." << endl;
325     }
326 buchmann 1.35
327 buchmann 1.4 Int_t MyPalette[100];
328     Double_t r[] = {0., 0.0, 1.0, 1.0, 1.0};
329     Double_t g[] = {0., 0.0, 0.0, 1.0, 1.0};
330     Double_t b[] = {0., 1.0, 0.0, 0.0, 1.0};
331     Double_t stop[] = {0., .25, .50, .75, 1.0};
332     Int_t FI = TColor::CreateGradientColorTable(5, stop, r, g, b, 110);
333     for (int i=0;i<100;i++) MyPalette[i] = FI+i;
334 buchmann 1.35
335 buchmann 1.4 gStyle->SetPalette(100, MyPalette);
336 buchmann 1.35
337 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
338     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
339     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
340     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
341     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
342     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
343    
344     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);
345     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);
346     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);
347     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);
348     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);
349     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);
350     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);
351 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);
352 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);
353     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);
354     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);
355 buchmann 1.27
356 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);
357 buchmann 1.36
358 buchmann 1.29 write_warning(__FUNCTION__,"CURRENTLY SWITCHING AUTOMATIZED MODE OFF!");automatized=false;
359 buchmann 1.35
360 buchmann 1.4 float rightmargin=gStyle->GetPadRightMargin();
361     gStyle->SetPadRightMargin(0.15);
362 buchmann 1.35
363 buchmann 1.4 TCanvas *limcanvas = new TCanvas("limcanvas","Limit canvas");
364 buchmann 1.35
365 buchmann 1.37
366     bool doexpected=true;
367    
368 buchmann 1.6 int Npoints=0;
369     for(int mglu=mglustart;mglu<=mgluend;mglu+=mglustep) {
370     for (int mlsp=mLSPstart;mlsp<=mLSPend&&mlsp<=mglu;mlsp+=mLSPstep) Npoints++;
371     }
372 buchmann 1.35
373 buchmann 1.6 int ipoint=-1;
374 buchmann 1.4 for(int mglu=mglustart;mglu<=mgluend;mglu+=mglustep) {
375     for (int mlsp=mLSPstart;mlsp<=mLSPend&&mlsp<=mglu;mlsp+=mLSPstep)
376     {
377 buchmann 1.6 ipoint++;
378     if(!runninglocally&&!do_this_point(ipoint,Npoints,jobnumber,njobs)) continue;
379 buchmann 1.19 float result=-987,resulterr=-987;
380 buchmann 1.40 int scanfileindex=0;
381 buchmann 1.42 int m0trees = PlottingSetup::m0end;
382     int m12trees = PlottingSetup::m12end;
383     if(!ismSUGRA) {
384     m0trees = PlottingSetup::mgluend;
385     m12trees = PlottingSetup::mLSPend;
386     }
387    
388     int a = int((PlottingSetup::ScanXzones*mlsp)/(m12trees+1));
389     int b = int((PlottingSetup::ScanYzones*mglu)/(m0trees+1));
390     scanfileindex=PlottingSetup::ScanYzones*a+b;
391    
392     stringstream filetoload;
393     filetoload << "/shome/buchmann/ntuples/";
394 buchmann 1.40 if(ismSUGRA) {
395 buchmann 1.42 filetoload << "mSUGRA/mSUGRA_clean_splitup_" << any2string(a) << "_" << any2string(b) << ".root";
396     } else {
397     filetoload << "SMS/SMS_clean_splitup_" << any2string(a) << "_" << any2string(b) << ".root";
398 buchmann 1.40 }
399 buchmann 1.42 scanfileindex=(scansample.collection).size();
400     //New: Loading file when necessary, not before (avoiding high memory usage and startup times)
401     if(scanfileindex!=0) scansample.AddSample(filetoload.str(),"scansample",1,1,false,true,scanfileindex,kRed);
402    
403 buchmann 1.36 clock_t start,finish;
404     start = clock(); // starting the clock to measure how long the computation takes!
405 buchmann 1.4 stringstream addcut;
406     addcut << "(TMath::Abs("<<massgluname<<"-"<<mglu<<")<5)&&(TMath::Abs("<<massLSPname<<"-"<<mlsp<<")<5)";
407 buchmann 1.40 (scansample.collection)[scanfileindex].events->Draw("eventNum",addcut.str().c_str(),"goff");
408     float nevents = (scansample.collection)[scanfileindex].events->GetSelectedRows();
409 buchmann 1.4 vector<vector<float> > systematics;
410 buchmann 1.30 if(nevents<10) {
411 buchmann 1.40 dout << "This point ("<<ipoint<<") with configuration ("<<massgluname<<"="<<mglu<<" , "<<massLSPname<<"="<<mlsp << ") does not contain enough events and will be skipped."<< endl;
412     continue;
413 buchmann 1.6 } else {
414 buchmann 1.40 dout << "OK! This point ("<<ipoint<<") with configuration ("<<massgluname<<"="<<mglu<<" , "<<massLSPname<<"="<<mlsp << ") contains " << nevents << " and will therefore not be skipped."<< endl;
415 buchmann 1.22 }
416     if(nevents!=0&&efficiencyonly) {
417 buchmann 1.40 Value effwosigcont = MCefficiency((scansample.collection)[scanfileindex].events,result,resulterr,mcjzb,requireZ,nevents,addcut.str(),-1);
418 buchmann 1.36 efficiencymap->Fill(mglu,mlsp,result);
419 buchmann 1.37 noscefficiencymap->Fill(mglu,mlsp,effwosigcont.getValue());
420     noscefficiencymap->SetBinError(mglu,mlsp,effwosigcont.getError());
421 buchmann 1.36 finish = clock();
422     timemap->Fill(mglu,mlsp,((float(finish)-float(start))/CLOCKS_PER_SEC));
423 buchmann 1.13 }
424 buchmann 1.17 Neventsmap->Fill(mglu,mlsp,nevents);
425 buchmann 1.24 ipointmap->Fill(mglu,mlsp,ipoint);
426 buchmann 1.17 if(efficiencyonly) continue;
427    
428 buchmann 1.40 do_systematics_for_one_file((scansample.collection)[scanfileindex].events,nevents,"SUSY SCAN", systematics,mcjzb,datajzb,peakerror,requireZ, addcut.str(),ismSUGRA);
429 buchmann 1.9 float JZBcutat = systematics[0][0];
430     float mceff = systematics[0][1];
431 buchmann 1.22 float mcefferr = systematics[0][2];//MC stat error
432 buchmann 1.9 float toterr = systematics[0][4];
433     float sys_jes = systematics[0][5]; // Jet Energy Scale
434     float sys_jsu = systematics[0][6]; // JZB scale uncertainty
435     float sys_res = systematics[0][7]; // resolution
436 buchmann 1.37 float mcwoscef = systematics[0][8]; // efficiency without signal contamination
437     float mcwoscefr= systematics[0][9]; // error on efficiency without signal contamination
438 buchmann 1.27 float sys_pdf = 0;
439 buchmann 1.37 if(systematics[0].size()>10) sys_pdf = systematics[0][10]; // PDF
440 buchmann 1.22 efficiencymap->Fill(mglu,mlsp,mceff);
441 buchmann 1.37 efficiencymap->SetBinError(efficiencymap->FindBin(mglu,mlsp),mcefferr);
442     noscefficiencymap->Fill(mglu,mlsp,mcwoscef);
443     noscefficiencymap->SetBinError(efficiencymap->FindBin(mglu,mlsp),mcwoscefr);
444 buchmann 1.35
445 buchmann 1.15 if(mceff!=mceff||toterr!=toterr||mceff<0) {
446 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;
447     continue;
448 buchmann 1.5 } else {
449 buchmann 1.35 if(!systematicsonly&&!efficiencyonly) {
450     dout << "Calculating limit now for "<<massgluname<<"="<<mglu<<" , "<<massLSPname<<"="<<mlsp <<endl;
451     vector<float> sigmas;
452 buchmann 1.40 string plotfilename=(string)(TString((scansample.collection)[scanfileindex].samplename)+TString(massgluname)+TString(any2string(mglu))+TString("__")+TString(massLSPname)+TString(any2string(mlsp))+TString(".png"));
453 buchmann 1.35 do_limit_wrapper(mceff,toterr,ibin,mcjzb,sigmas,plotfilename);
454     cout << "back in " << __FUNCTION__ << endl;
455     if(sigmas[0]>-0.5) { // negative sigmas are the error signature of do_limit_wrapper, so we want to exclude them.
456     limitmap->Fill(mglu,mlsp,sigmas[0]);
457 buchmann 1.37 if(sigmas.size()>1) {
458     explimitmap->Fill(mglu,mlsp,sigmas[1]);
459     exp1plimitmap->Fill(mglu,mlsp,sigmas[2]);
460     exp1mlimitmap->Fill(mglu,mlsp,sigmas[3]);
461     exp2plimitmap->Fill(mglu,mlsp,sigmas[4]);
462     exp2mlimitmap->Fill(mglu,mlsp,sigmas[5]);
463 buchmann 1.38 }
464 buchmann 1.37
465 buchmann 1.35 sysjesmap->Fill(mglu,mlsp,sys_jes);
466     sysjsumap->Fill(mglu,mlsp,sys_jsu);
467     sysresmap->Fill(mglu,mlsp,sys_res);
468     syspdfmap->Fill(mglu,mlsp,sys_pdf);
469     systotmap->Fill(mglu,mlsp,toterr/mceff);//total relative (!) error
470     sysstatmap->Fill(mglu,mlsp,mcefferr);//total relative (!) error
471     dout << "A limit has been added at " << sigmas[0] << " for m_{glu}="<<mglu << " and m_{lsp}="<<mlsp<<endl;
472     } //end of if sigma is positive
473 buchmann 1.36 finish = clock();
474     timemap->Fill(mglu,mlsp,((float(finish)-float(start))/CLOCKS_PER_SEC));
475    
476 buchmann 1.35 //end of not systematics only condition
477     }
478     if(systematicsonly) {
479     sysjesmap->Fill(mglu,mlsp,sys_jes);
480     sysjsumap->Fill(mglu,mlsp,sys_jsu);
481     sysresmap->Fill(mglu,mlsp,sys_res);
482     syspdfmap->Fill(mglu,mlsp,sys_pdf);
483     systotmap->Fill(mglu,mlsp,toterr/mceff);//total relative (!) error
484     sysstatmap->Fill(mglu,mlsp,mcefferr);//total relative (!) error
485 buchmann 1.36 finish = clock();
486     timemap->Fill(mglu,mlsp,((float(finish)-float(start))/CLOCKS_PER_SEC));
487 buchmann 1.35 }
488 buchmann 1.15 }//efficiency is valid
489 buchmann 1.4 }
490     }
491 buchmann 1.35
492 buchmann 1.22 prepare_scan_axis(limitmap,ismSUGRA);
493     prepare_scan_axis(sysjesmap,ismSUGRA);
494     prepare_scan_axis(sysjsumap,ismSUGRA);
495     prepare_scan_axis(sysresmap,ismSUGRA);
496 buchmann 1.27 prepare_scan_axis(syspdfmap,ismSUGRA);
497     prepare_scan_axis(systotmap,ismSUGRA);
498     prepare_scan_axis(sysstatmap,ismSUGRA);
499 buchmann 1.36 prepare_scan_axis(timemap,ismSUGRA);
500 buchmann 1.35
501 buchmann 1.13 if(!systematicsonly&&!efficiencyonly) {
502 buchmann 1.9 limcanvas->cd();
503     limitmap->Draw("COLZ");
504 buchmann 1.22 string titletobewritten="Limits in LSP-Glu plane";
505     if(ismSUGRA) titletobewritten="Limits in m_{1/2}-m_{0} plane";
506     TText *title = write_title(titletobewritten);
507 buchmann 1.9 title->Draw();
508     if(runninglocally) {
509     CompleteSave(limcanvas,"SUSYScan/Limits_JZB_geq"+any2string(jzb_cut[ibin]));
510     } else {
511 buchmann 1.12 TFile *outputfile;
512     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 :-)
513 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
514 buchmann 1.9 limitmap->Write();
515 buchmann 1.37 if(doexpected) {
516     explimitmap->Write();
517     exp1plimitmap->Write();
518     exp1mlimitmap->Write();
519     exp2plimitmap->Write();
520     exp2mlimitmap->Write();
521     }
522 buchmann 1.18 sysjesmap->Write();
523     sysjsumap->Write();
524 buchmann 1.28 sysresmap->Write();
525 buchmann 1.18 efficiencymap->Write();
526 buchmann 1.37 noscefficiencymap->Write();
527 buchmann 1.27 syspdfmap->Write();
528     systotmap->Write();
529     sysstatmap->Write();
530 buchmann 1.18 Neventsmap->Write();
531 buchmann 1.24 ipointmap->Write();
532 buchmann 1.36 timemap->Write();
533 buchmann 1.9 outputfile->Close();
534     }
535 buchmann 1.35 }
536 buchmann 1.13 if(systematicsonly) { // systematics only :
537 buchmann 1.9 limcanvas->cd();
538     sysjesmap->Draw("COLZ");
539 buchmann 1.22 string titletobewritten="Jet Energy scale in LSP-Glu plane";
540     if(ismSUGRA) titletobewritten="Limits in m_{1/2}-m_{0} plane";
541     TText *title = write_title(titletobewritten);
542 buchmann 1.9 title->Draw();
543     if(runninglocally) {
544     CompleteSave(limcanvas,"SUSYScan/JES_geq"+any2string(jzb_cut[ibin]));
545     }
546     sysjsumap->Draw("COLZ");
547 buchmann 1.22 titletobewritten="JZB Scale Uncertainty in LSP-Glu plane";
548     if(ismSUGRA) titletobewritten="JZB Scale Uncertainty in m_{1/2}-m_{0} plane";
549     TText *title2 = write_title(titletobewritten);
550 buchmann 1.9 title2->Draw();
551     if(runninglocally) {
552     CompleteSave(limcanvas,"SUSYScan/JSU_geq"+any2string(jzb_cut[ibin]));
553     }
554     sysresmap->Draw("COLZ");
555 buchmann 1.22 titletobewritten="Resolution in LSP-Glu plane";
556     if(ismSUGRA) titletobewritten="Resolution in m_{1/2}-m_{0} plane";
557     TText *title3 = write_title(titletobewritten);
558 buchmann 1.9 title3->Draw();
559     if(runninglocally) {
560     CompleteSave(limcanvas,"SUSYScan/Resolution_geq"+any2string(jzb_cut[ibin]));
561     }
562 buchmann 1.35
563 buchmann 1.9 if(!runninglocally) {
564     TFile *outputfile=new TFile(("output/DistributedSystematics_job"+string(any2string(jobnumber))+"_of_"+string(any2string(njobs))+".root").c_str(),"UPDATE");
565     sysjesmap->Write();
566     sysjsumap->Write();
567     sysresmap->Write();
568 buchmann 1.18 efficiencymap->Write();
569 buchmann 1.37 noscefficiencymap->Write();
570 buchmann 1.18 Neventsmap->Write();
571 buchmann 1.24 ipointmap->Write();
572 buchmann 1.27 syspdfmap->Write();
573     systotmap->Write();
574     sysstatmap->Write();
575 buchmann 1.36 timemap->Write();
576 buchmann 1.9 outputfile->Close();
577     }
578     }//end of systematics only
579 buchmann 1.13 if(efficiencyonly) {
580     limcanvas->cd();
581     efficiencymap->Draw("COLZ");
582 buchmann 1.22 string titletobewritten="Efficiencies in LSP-Glu plane";
583     if(ismSUGRA) titletobewritten="Efficiencies in m_{1/2}-m_{0} plane";
584     TText *title = write_title(titletobewritten);
585 buchmann 1.13 title->Draw();
586     if(runninglocally) {
587     CompleteSave(limcanvas,"SUSYScan/Efficiency_geq"+any2string(jzb_cut[ibin]));
588     }
589     limcanvas->cd();
590     sysjesmap->Draw("COLZ");
591 buchmann 1.22 titletobewritten="N(events) in LSP-Glu plane";
592     if(ismSUGRA) titletobewritten="N(events) in m_{1/2}-m_{0} plane";
593     TText *title2 = write_title(titletobewritten);
594 buchmann 1.13 title2->Draw();
595     if(runninglocally) {
596     CompleteSave(limcanvas,"SUSYScan/Nevents_geq"+any2string(jzb_cut[ibin]));
597     }
598     if(!runninglocally) {
599     TFile *outputfile=new TFile(("output/DistributedSystematics_job"+string(any2string(jobnumber))+"_of_"+string(any2string(njobs))+".root").c_str(),"UPDATE");
600 buchmann 1.24 ipointmap->Write();
601 buchmann 1.13 Neventsmap->Write();
602 buchmann 1.37 noscefficiencymap->Write();
603 buchmann 1.13 efficiencymap->Write();
604 buchmann 1.36 timemap->Write();
605 buchmann 1.13 outputfile->Close();
606     }
607     }//end of efficiencies only
608 buchmann 1.35
609 buchmann 1.31 delete limitmap;
610     delete exclmap;
611     delete sysjesmap;
612     delete sysjsumap;
613     delete sysresmap;
614 buchmann 1.37 delete noscefficiencymap;
615 buchmann 1.31 delete efficiencymap;
616     delete Neventsmap;
617     delete ipointmap;
618     delete syspdfmap;
619     delete systotmap;
620     delete sysstatmap;
621 buchmann 1.13 delete limcanvas;
622 buchmann 1.1 }
623 buchmann 1.4
624 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) {
625 buchmann 1.10 dout << "Starting the SUSY scan now with all " << jzb_cut.size() << " bin(s)" << endl;
626 buchmann 1.4 for(int ibin=0;ibin<jzb_cut.size();ibin++) {
627 buchmann 1.13 scan_SUSY_parameter_space(mcjzb,datajzb,jzb_cut,requireZ, peakerror, ibin, njobs, jobnumber,systonly,effonly);
628 buchmann 1.4 }
629 buchmann 1.6 }
630 buchmann 1.35