ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/SUSYScan.C
Revision: 1.28
Committed: Fri Sep 2 09:45:40 2011 UTC (13 years, 8 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.27: +2 -1 lines
Log Message:
Now also saving pileup information in standard limit scenario. Corrected remaining time output (only user experience modification, no 'active code')

File Contents

# Content
1 #include <iostream>
2 #include <vector>
3 #include <sys/stat.h>
4 #include <fstream>
5 #include <pthread.h>
6
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
20 //#include "TTbar_stuff.C"
21 using namespace std;
22
23 using namespace PlottingSetup;
24
25 void susy_scan_axis_labeling(TH2F *histo) {
26 histo->GetXaxis()->SetTitle("#Chi_{2}^{0}-LSP");
27 histo->GetXaxis()->CenterTitle();
28 histo->GetYaxis()->SetTitle("m_{#tilde{q}}");
29 histo->GetYaxis()->CenterTitle();
30 }
31
32 bool isThreadActive=false;
33
34 struct limit_args {
35 float mceff;
36 float toterr;
37 int ibin;
38 string mcjzb;
39 vector<float> sigmas;
40 string plotfilename;
41 };
42
43 void* compute_one_upper_limit_wrapper(void* data) {
44 isThreadActive=true;
45 limit_args* args = (limit_args*) data;
46 std::cout << "Thread to compute limits has been started" << std::endl;
47 args->sigmas=compute_one_upper_limit(args->mceff,args->toterr,args->ibin,args->mcjzb,args->plotfilename,true);
48 std::cout << "Thread to compute limits has finished" << std::endl;
49 isThreadActive=false;
50 return NULL; // oder in C++: return 0;// Damit kann man Werte zurückgeben
51 }
52
53 void do_limit_wrapper(float mceff,float toterr,int ibin,string mcjzb,vector<float> &sigmas,string plotfilename) {
54 pthread_t limitthread;
55 limit_args limargs={mceff,toterr,ibin,mcjzb,sigmas,plotfilename};
56 pthread_create( &limitthread, NULL, compute_one_upper_limit_wrapper, (void*) &limargs);
57 int counter=0;
58 sleep(1); //waiting a second for the process to become active
59 while(counter<limitpatience*60 && isThreadActive) {
60 std::cout << "Limits are being calculated; Checking round " << counter << " ( corresponds to " << seconds_to_time(counter) << " ) , patience will end in " << seconds_to_time(60*limitpatience-counter) << std::endl;
61 counter++;
62 sleep(1);
63 }
64
65 if(!isThreadActive) {
66 cout << "Thread finished sucessfully" << endl;
67 pthread_join( limitthread, NULL );
68 std::cout<< __FUNCTION__ << " : going to save sigmas " << std::endl;
69 sigmas=limargs.sigmas;
70 } else {
71 pthread_cancel(limitthread);
72 std::cout << "DID NOT TERMINATE IN TIME - ABORTED!" << std::endl;
73 sigmas.push_back(-1);sigmas.push_back(-1);sigmas.push_back(-1);
74 }
75 }
76
77 void prepare_scan_axis(TH2 *variablemap,bool ismSUGRA) {
78 variablemap->GetXaxis()->SetTitle("m_{glu}");
79 variablemap->GetYaxis()->SetTitle("m_{LSP}");
80
81 if(ismSUGRA) {
82 variablemap->GetXaxis()->SetTitle("m_{0}");
83 variablemap->GetYaxis()->SetTitle("m_{1/2}");
84 }
85
86 variablemap->GetXaxis()->CenterTitle();
87 variablemap->GetYaxis()->CenterTitle();
88 }
89
90 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) {
91 bool runninglocally=true;
92 if(njobs>-1&&jobnumber>-1) {
93 runninglocally=false;
94 dout << "Running on the GRID (this is job " << jobnumber << "/" << njobs << ") for a jzb cut at " << jzb_cut[ibin] << endl;
95 } else {
96 dout << "Running locally " << endl;
97 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;
98 }
99
100 cout << "FILE USED: " << (scansample.collection)[0].filename << endl;
101 cout << "FILE USED (name): " << (scansample.collection)[0].samplename << endl;
102 jzbSel=jzb_cut[ibin];
103 geqleq="geq";
104 automatized=true;
105 mcjzbexpression=mcjzb;
106
107 string massgluname="MassGlu";
108 string massLSPname="MassLSP";
109
110 string prefix="SMS_";
111 // up to here, everything is set up for SMS; now we need to switch stuff around if we're dealing with an mSUGRA scan!
112 bool ismSUGRA=false;
113 if(Contains((scansample.collection)[0].samplename,"mSUGRA")) ismSUGRA=true;
114 if(ismSUGRA) {
115 massgluname="M0"; // this is the "x axis" in the limit plot (like the gluino in the SMS case)
116 massLSPname="M12"; // this is the "y axis" in the limit plot (like the LSP in the SMS case)
117 mglustart=m0start;
118 mgluend=m0end;
119 mglustep=m0step;
120 mLSPstart=m12start;
121 mLSPend=m12end;
122 mLSPstep=m12step;
123 prefix="mSUGRA_";
124 cout << "mSUGRA scan has been set up." << endl;
125 } else {
126 cout << "SMS scan has been set up." << endl;
127 }
128
129 Int_t MyPalette[100];
130 Double_t r[] = {0., 0.0, 1.0, 1.0, 1.0};
131 Double_t g[] = {0., 0.0, 0.0, 1.0, 1.0};
132 Double_t b[] = {0., 1.0, 0.0, 0.0, 1.0};
133 Double_t stop[] = {0., .25, .50, .75, 1.0};
134 Int_t FI = TColor::CreateGradientColorTable(5, stop, r, g, b, 110);
135 for (int i=0;i<100;i++) MyPalette[i] = FI+i;
136
137 gStyle->SetPalette(100, MyPalette);
138
139 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);
140 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);
141 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);
142 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);
143 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);
144 TH2F *efficiencymap = new TH2F((prefix+"efficiencymap").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);
145 TH2F *Neventsmap = new TH2F((prefix+"Neventsmap").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);
146 TH2F *ipointmap = new TH2F((prefix+"ipointmap").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);
147 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);
148 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);
149 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);
150
151
152
153
154 float rightmargin=gStyle->GetPadRightMargin();
155 gStyle->SetPadRightMargin(0.15);
156
157 TCanvas *limcanvas = new TCanvas("limcanvas","Limit canvas");
158
159 int Npoints=0;
160 for(int mglu=mglustart;mglu<=mgluend;mglu+=mglustep) {
161 for (int mlsp=mLSPstart;mlsp<=mLSPend&&mlsp<=mglu;mlsp+=mLSPstep) Npoints++;
162 }
163
164 int ipoint=-1;
165 for(int mglu=mglustart;mglu<=mgluend;mglu+=mglustep) {
166 for (int mlsp=mLSPstart;mlsp<=mLSPend&&mlsp<=mglu;mlsp+=mLSPstep)
167 {
168 ipoint++;
169 if(!runninglocally&&!do_this_point(ipoint,Npoints,jobnumber,njobs)) continue;
170 float result=-987,resulterr=-987;
171 stringstream addcut;
172 addcut << "(TMath::Abs("<<massgluname<<"-"<<mglu<<")<5)&&(TMath::Abs("<<massLSPname<<"-"<<mlsp<<")<5)";
173 (scansample.collection)[0].events->Draw("eventNum",addcut.str().c_str(),"goff");
174 float nevents = (scansample.collection)[0].events->GetSelectedRows();
175 vector<vector<float> > systematics;
176 if(nevents==0) {
177 dout << "This point ("<<ipoint<<") with configuration ("<<massgluname<<"="<<mglu<<" , "<<massLSPname<<"="<<mlsp << ") contains no events and will be skipped."<< endl;
178 continue;
179 } else {
180 dout << "OK! This point ("<<ipoint<<") with configuration ("<<massgluname<<"="<<mglu<<" , "<<massLSPname<<"="<<mlsp << ") contains " << nevents << " and will therefore not be skipped."<< endl;
181 }
182 if(nevents!=0&&efficiencyonly) {
183 MCefficiency((scansample.collection)[0].events,result,resulterr,mcjzb,requireZ,nevents,addcut.str());
184 efficiencymap->Fill(mglu,mlsp,result);
185 }
186 Neventsmap->Fill(mglu,mlsp,nevents);
187 ipointmap->Fill(mglu,mlsp,ipoint);
188 if(efficiencyonly) continue;
189
190 do_systematics_for_one_file((scansample.collection)[0].events,nevents,"SUSY SCAN", systematics,mcjzb,datajzb,peakerror,requireZ, addcut.str(),ismSUGRA);
191 float JZBcutat = systematics[0][0];
192 float mceff = systematics[0][1];
193 float mcefferr = systematics[0][2];//MC stat error
194 float toterr = systematics[0][4];
195 float sys_jes = systematics[0][5]; // Jet Energy Scale
196 float sys_jsu = systematics[0][6]; // JZB scale uncertainty
197 float sys_res = systematics[0][7]; // resolution
198 float sys_pdf = 0;
199 if(systematics[0].size()>9) sys_pdf = systematics[0][9]; // PDF
200 efficiencymap->Fill(mglu,mlsp,mceff);
201 efficiencymap->SetBinError(efficiencymap->FindBin(mglu,mlsp),mcefferr);//blublu
202
203 if(mceff!=mceff||toterr!=toterr||mceff<0) {
204 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;
205 continue;
206 } else {
207 if(!systematicsonly&&!efficiencyonly) {
208 dout << "Calculating limit now for "<<massgluname<<"="<<mglu<<" , "<<massLSPname<<"="<<mlsp <<endl;
209 vector<float> sigmas;
210 string plotfilename=(string)(TString((scansample.collection)[0].samplename)+TString(massgluname)+TString(any2string(mglu))+TString("__")+TString(massLSPname)+TString(any2string(mlsp))+TString(".png"));
211 do_limit_wrapper(mceff,toterr,ibin,mcjzb,sigmas,plotfilename);
212 cout << "back in " << __FUNCTION__ << endl;
213 if(sigmas[0]>-0.5) { // negative sigmas are the error signature of do_limit_wrapper, so we want to exclude them.
214 limitmap->Fill(mglu,mlsp,sigmas[0]);
215 sysjesmap->Fill(mglu,mlsp,sys_jes);
216 sysjsumap->Fill(mglu,mlsp,sys_jsu);
217 sysresmap->Fill(mglu,mlsp,sys_res);
218 syspdfmap->Fill(mglu,mlsp,sys_pdf);
219 systotmap->Fill(mglu,mlsp,toterr/mceff);//total relative (!) error
220 sysstatmap->Fill(mglu,mlsp,mcefferr);//total relative (!) error
221 efficiencymap->Fill(mglu,mlsp,result);
222 dout << "A limit has been added at " << sigmas[0] << " for m_{glu}="<<mglu << " and m_{lsp}="<<mlsp<<endl;
223 } //end of if sigma is positive
224 //end of not systematics only condition
225 }
226 if(systematicsonly) {
227 sysjesmap->Fill(mglu,mlsp,sys_jes);
228 sysjsumap->Fill(mglu,mlsp,sys_jsu);
229 sysresmap->Fill(mglu,mlsp,sys_res);
230 syspdfmap->Fill(mglu,mlsp,sys_pdf);
231 systotmap->Fill(mglu,mlsp,toterr/mceff);//total relative (!) error
232 sysstatmap->Fill(mglu,mlsp,mcefferr);//total relative (!) error
233 efficiencymap->Fill(mglu,mlsp,result);
234 }
235 }//efficiency is valid
236 }
237 }
238
239 prepare_scan_axis(limitmap,ismSUGRA);
240 prepare_scan_axis(sysjesmap,ismSUGRA);
241 prepare_scan_axis(sysjsumap,ismSUGRA);
242 prepare_scan_axis(sysresmap,ismSUGRA);
243 prepare_scan_axis(syspdfmap,ismSUGRA);
244 prepare_scan_axis(systotmap,ismSUGRA);
245 prepare_scan_axis(sysstatmap,ismSUGRA);
246
247 if(!systematicsonly&&!efficiencyonly) {
248 limcanvas->cd();
249 limitmap->Draw("COLZ");
250 string titletobewritten="Limits in LSP-Glu plane";
251 if(ismSUGRA) titletobewritten="Limits in m_{1/2}-m_{0} plane";
252 TText *title = write_title(titletobewritten);
253 title->Draw();
254 if(runninglocally) {
255 CompleteSave(limcanvas,"SUSYScan/Limits_JZB_geq"+any2string(jzb_cut[ibin]));
256 } else {
257 TFile *outputfile;
258 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 :-)
259 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
260 limitmap->Write();
261 sysjesmap->Write();
262 sysjsumap->Write();
263 sysresmap->Write();
264 efficiencymap->Write();
265 syspdfmap->Write();
266 systotmap->Write();
267 sysstatmap->Write();
268 Neventsmap->Write();
269 ipointmap->Write();
270 outputfile->Close();
271 }
272 }
273 if(systematicsonly) { // systematics only :
274 limcanvas->cd();
275 sysjesmap->Draw("COLZ");
276 string titletobewritten="Jet Energy scale in LSP-Glu plane";
277 if(ismSUGRA) titletobewritten="Limits in m_{1/2}-m_{0} plane";
278 TText *title = write_title(titletobewritten);
279 title->Draw();
280 if(runninglocally) {
281 CompleteSave(limcanvas,"SUSYScan/JES_geq"+any2string(jzb_cut[ibin]));
282 }
283 sysjsumap->Draw("COLZ");
284 titletobewritten="JZB Scale Uncertainty in LSP-Glu plane";
285 if(ismSUGRA) titletobewritten="JZB Scale Uncertainty in m_{1/2}-m_{0} plane";
286 TText *title2 = write_title(titletobewritten);
287 title2->Draw();
288 if(runninglocally) {
289 CompleteSave(limcanvas,"SUSYScan/JSU_geq"+any2string(jzb_cut[ibin]));
290 }
291 sysresmap->Draw("COLZ");
292 titletobewritten="Resolution in LSP-Glu plane";
293 if(ismSUGRA) titletobewritten="Resolution in m_{1/2}-m_{0} plane";
294 TText *title3 = write_title(titletobewritten);
295 title3->Draw();
296 if(runninglocally) {
297 CompleteSave(limcanvas,"SUSYScan/Resolution_geq"+any2string(jzb_cut[ibin]));
298 }
299
300 if(!runninglocally) {
301 TFile *outputfile=new TFile(("output/DistributedSystematics_job"+string(any2string(jobnumber))+"_of_"+string(any2string(njobs))+".root").c_str(),"UPDATE");
302 sysjesmap->Write();
303 sysjsumap->Write();
304 sysresmap->Write();
305 efficiencymap->Write();
306 Neventsmap->Write();
307 ipointmap->Write();
308 syspdfmap->Write();
309 systotmap->Write();
310 sysstatmap->Write();
311 outputfile->Close();
312 }
313 }//end of systematics only
314 if(efficiencyonly) {
315 limcanvas->cd();
316 efficiencymap->Draw("COLZ");
317 string titletobewritten="Efficiencies in LSP-Glu plane";
318 if(ismSUGRA) titletobewritten="Efficiencies in m_{1/2}-m_{0} plane";
319 TText *title = write_title(titletobewritten);
320 title->Draw();
321 if(runninglocally) {
322 CompleteSave(limcanvas,"SUSYScan/Efficiency_geq"+any2string(jzb_cut[ibin]));
323 }
324 limcanvas->cd();
325 sysjesmap->Draw("COLZ");
326 titletobewritten="N(events) in LSP-Glu plane";
327 if(ismSUGRA) titletobewritten="N(events) in m_{1/2}-m_{0} plane";
328 TText *title2 = write_title(titletobewritten);
329 title2->Draw();
330 if(runninglocally) {
331 CompleteSave(limcanvas,"SUSYScan/Nevents_geq"+any2string(jzb_cut[ibin]));
332 }
333 if(!runninglocally) {
334 TFile *outputfile=new TFile(("output/DistributedSystematics_job"+string(any2string(jobnumber))+"_of_"+string(any2string(njobs))+".root").c_str(),"UPDATE");
335 ipointmap->Write();
336 Neventsmap->Write();
337 efficiencymap->Write();
338 outputfile->Close();
339 }
340 }//end of efficiencies only
341 delete limcanvas;
342 }
343
344 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) {
345 dout << "Starting the SUSY scan now with all " << jzb_cut.size() << " bin(s)" << endl;
346 for(int ibin=0;ibin<jzb_cut.size();ibin++) {
347 scan_SUSY_parameter_space(mcjzb,datajzb,jzb_cut,requireZ, peakerror, ibin, njobs, jobnumber,systonly,effonly);
348 }
349 }