ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/SUSYScan.C
Revision: 1.29
Committed: Fri Sep 2 12:52:56 2011 UTC (13 years, 8 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.28: +4 -2 lines
Log Message:
Updated limit patience algorithm; it now floods the log files about 80% less

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