9 #include "TEventList.h"
10 #include "TDirectory.h"
15 #include "TObjArray.h"
25 #define MAXVARS 8 //maximum allowed number of variables for one selection
26 #define MAXCUTS 12 //maximum allowed number of cuts for one selection
28 typedef std::vector<pair<double, int> >
ValueMap;
53 TString leaftype = t->GetLeaf(varname)->GetTypeName();
55 if (leaftype==
"Float_t")
return 0;
56 else if (leaftype==
"Int_t")
return 1;
57 else if (leaftype==
"Bool_t")
return 2;
73 t->SetBranchAddress(
"ev",&
ev);
74 t->SetBranchAddress(
"run",&
run);
75 t->SetBranchAddress(
"mode",&
mode);
76 t->SetBranchAddress(
"recmode",&
rec);
78 TObjArray* branches = t->GetListOfBranches();
82 for(
int i=0;
i<=branches->GetLast(); ++
i)
84 TBranch* branch = (TBranch*)branches->UncheckedAt(
i);
87 if ( v==
"ev" || v==
"mode" || v==
"run" || v==
"nsig" )
continue;
88 if ( v.Contains(
"pdg") || v.Contains(
"beam") || v.Contains(
"mct") )
continue;
89 if ( v.BeginsWith(
"t") && v!=
"thr")
continue;
90 if ( v.EndsWith(
"vx") || v.EndsWith(
"vy") || v.EndsWith(
"vz") || v.EndsWith(
"pocmag"))
continue;
94 if ( v.BeginsWith(
"es") || v==
"mmiss" || v.EndsWith(
"d0m") ) ok=
true;
95 if ( v.EndsWith(
"p") || v.EndsWith(
"tht") || v.EndsWith(
"pcm") || v.EndsWith(
"thtcm") || v.EndsWith(
"pt") ) ok=
true;
96 if ( ok || v.EndsWith(
"ang") || v.Contains(
"poc") ) ok=
true;
97 if ( ok || v.Contains(
"pid") || v.Contains(
"min") || v.Contains(
"max") || v.Contains(
"sum") || v.Contains(
"fw") ) ok=
true;
98 if ( ok || v.EndsWith(
"sph") || v.EndsWith(
"apl") || v.EndsWith(
"pla") || v.EndsWith(
"thr") || v.EndsWith(
"cir") ) ok=
true;
100 if ( v==
"xmdif" &&
dstarmode ) ok =
true;
101 if ( v==
"xd0m" &&
dstarmode ) ok =
false;
109 case 1: t->SetBranchAddress(v, &(
ibranch[nbranch]));
break;
110 case 2: t->SetBranchAddress(v, &(
bbranch[nbranch]));
break;
122 int uid(
int lev,
int lrun,
int lmode)
124 return lev+10000*lrun+(((lmode/100)%10)*20+lmode%10)*100000;
132 t->SetBranchStatus(
"*",0);
133 t->SetBranchStatus(
"ev",1);
134 t->SetBranchStatus(
"run",1);
135 t->SetBranchStatus(
"mode",1);
136 t->SetBranchStatus(
"recmode",1);
139 for (
int j=0;j<10;++j)
evcntrec[j].clear();
141 for (
int i=0;
i<el.GetN();++
i)
143 t->GetEntry(el.GetEntry(
i));
147 t->SetBranchStatus(
"*",1);
156 t->SetBranchStatus(
"*",0);
157 t->SetBranchStatus(
"ev",1);
158 t->SetBranchStatus(
"mode",1);
159 t->SetBranchStatus(
"run",1);
160 t->SetBranchStatus(varname,1);
163 Int_t dtype=
gettype(t, varname);
169 for (i=0;i<els.GetN();++
i)
171 t->GetEntry(els.GetEntry(i));
172 switch (dtype) {
case 2: var = (Float_t)
bbranch[
id];
break;
case 1: var = (Float_t)
ibranch[
id];
break;
default: var=
fbranch[id]; }
178 for (i=0;i<elb.GetN();++
i)
180 t->GetEntry(elb.GetEntry(i));
181 switch (dtype) {
case 2: var = (Float_t)
bbranch[
id];
break;
case 1: var = (Float_t)
ibranch[
id];
break;
default: var=
fbranch[id]; }
194 int Nbgval = elb.GetN();
195 int dtype =
gettype(t,varname);
197 makeMaps(t, varname, els, elb, sigvals, bgvals,
id);
201 sort(bgvals.begin(), bgvals.end());
206 while ( (bgcnt.size()/(double)
Nbgev)<(1-supr) && i<Nbgval ) bgcnt[bgvals[i++].second]+=1;
207 double leftcut = bgvals[
i].first;
212 while ( (bgcnt.size()/(double)
Nbgev)<(1-supr) && i>=0) bgcnt[bgvals[i--].second]+=1;
213 double rightcut = bgvals[
i].first;
218 for (i=0;i<(int)sigvals.size();++
i)
220 if (sigvals[i].first<=leftcut) sigcnt[sigvals[
i].second]+=1;
221 if (sigvals[i].first>=rightcut) sigcnt2[sigvals[
i].second]+=1;
224 t->SetBranchStatus(
"*",1);
226 double lefteff = sigcnt.size()/
Nsigev;
227 double righteff = sigcnt2.size()/
Nsigev;
234 if (lefteff>righteff)
238 if (bestcut<1e-3 ||
fabs(1.-bestcut)<1e-3) prec=
"%.6f";
239 if (dtype==0)
cuts[id] = TString::Format(
TString(
"%s<="+prec).Data(),varname.Data(),leftcut);
240 else cuts[id] = TString::Format(
"%s<=%.1f",varname.Data(),leftcut);
247 if (bestcut<1e-3 ||
fabs(1.-bestcut)<1e-3) prec=
"%.6f";
249 if (dtype==0)
cuts[id] = TString::Format(
TString(
"%s>="+prec).Data(),varname.Data(),rightcut);
250 else cuts[id] = TString::Format(
"%s>=%.1f",varname.Data(),rightcut);
264 int Nsigval = els.GetN();
265 int dtype =
gettype(t,varname);
267 makeMaps(t, varname, els, elb, sigvals, bgvals,
id);
270 sort(sigvals.begin(), sigvals.end());
275 while ( (sigcnt.size()/(double)
Nsigev)<eff && i<Nsigval ) sigcnt[sigvals[i++].second]+=1;
276 double leftcut = sigvals[
i].first;
281 while ( (sigcnt.size()/(double)
Nsigev)<eff && i>=0) sigcnt[sigvals[i--].second]+=1;
282 double rightcut = sigvals[
i].first;
287 for (i=0;i<(int)bgvals.size();++
i)
289 if (bgvals[i].first<=leftcut) bgcnt[bgvals[
i].second];
290 if (bgvals[i].first>=rightcut) bgcnt2[bgvals[
i].second];
293 t->SetBranchStatus(
"*",1);
297 double rightsupr = (
Nbgev-bgcnt2.size())/
Nbgev;
301 if (leftcut!=leftcut || rightcut!=rightcut)
return 0;
306 if (leftsupr>rightsupr)
310 if (bestcut<1e-3 ||
fabs(1.-bestcut)<1e-3) prec=
"%.6f";
311 if (dtype==0)
cuts[id] = TString::Format(
TString(
"%s<="+prec).Data(),varname.Data(),leftcut);
312 else cuts[id] = TString::Format(
"%s<=%.1f",varname.Data(),leftcut);
319 if (bestcut<1e-3 ||
fabs(1.-bestcut)<1e-3) prec=
"%.6f";
321 if (dtype==0)
cuts[id] = TString::Format(
TString(
"%s>="+prec).Data(),varname.Data(),rightcut);
322 else cuts[id] = TString::Format(
"%s>=%.1f",varname.Data(),rightcut);
324 qual[id] = rightsupr;
331 int findcut(TTree *
t, TEventList &
els, TEventList &
elb,
double supr,
double &bestqa)
347 if (i%(nbranch/10)==0){ cout <<
"#"<<flush;}
372 TRegexp rvar(
"-[a-zA-Z_][a-zA-Z0-9_]+-");
374 TObjArray *tok = s.Tokenize(
"&&");
375 int N = tok->GetEntries();
376 for (
int i=0;
i<N;++
i)
380 TString toks = ((TObjString*)tok->At(
i))->String();
381 toks.ReplaceAll(
"\t",
"");
382 toks = toks.Strip(TString::kBoth);
383 if (toks.Index(
"<")>0) toks = toks(0,toks.Index(
"<"));
384 if (toks.Index(
">")>0) toks = toks(0,toks.Index(
">"));
385 svar +=
"-"+toks+
"-";
388 while (svar(rvar)!=
"") {svar.ReplaceAll(svar(rvar),
""); cnt++;}
395 TObjArray *tok = s.Tokenize(
"&&");
396 return tok->GetEntries();
401 int autocutx(
TString fname,
TString precut=
"",
double supr=0.95,
double target=0.0001,
double mineff = 0.1,
double minreleff = 0.0,
int evmult=10000,
double norm=1.0,
int n0s=-1)
408 gStyle->SetOptStat(0);
410 TCanvas *
c1=
new TCanvas(
"c1",
"c1",10,10,1800,550);
415 TRegexp ren(
"M[0-9][0-9][0-9]");
416 TRegexp regntp(
"n[0-9]+");
417 TRegexp regS(
"[0-9]+S");
418 TRegexp regB(
"[0-9]+B");
420 TString en = fname(ren); en = en(1,5);
421 TString s = fname(regS); s =
s(0,s.Length()-1);
422 TString b = fname(regB); b =
b(0,b.Length()-1);
425 TString smode = ntp(1,ntp.Length());
426 int dmode = smode.Atoi()/10;
427 dstarmode = (dmode==11 || dmode==13 || dmode==15);
429 if (dmode>=10 && dmode<20) norm = 0.5;
431 if (dmode/10 == 6) norm = 0.2;
433 if (n0s<0) n0s = s.Atoi();
439 cout <<
"tree '"<<ntp.Data()<<
"' with N_sig = "<<
N0_sig<<
", N_bg = "<<
N0_bg<<endl;
441 TFile *
f=
new TFile(fname,
"READ");
442 TTree *
t=(TTree*)f->Get(ntp);
446 TEventList elsall(
"elsall"), elball(
"elball");
447 TEventList
els(
"els"),
elb(
"elb");
449 if (precut==
"") precut = tagcut;
450 else precut = tagcut+
"&&"+precut;
455 t->Draw(
">>elsall",tagcut+
"&&"+sigcut);
456 t->Draw(
">>elball",tagcut+
"&&"+bgcut);
460 double beff=1., seff=1., rseff=1.;
462 TRegexp rnum(
"[\\-]*[0-9]+\\.[0-9]+$");
465 while (cnt<150 && beff>target && seff>=mineff && rseff>=minreleff)
468 t->SetLineColor(1); t->Draw(
"xm",precut);
469 t->SetLineColor(2); t->Draw(
"xm",precut+
"&&"+sigcut,
"same");
470 t->SetLineColor(4); t->Draw(
"xm",precut+
"&&"+bgcut,
"same");
474 t->Draw(
">>els",sigcut+
"&&"+precut);
475 t->Draw(
">>elb",bgcut+
"&&"+precut);
487 if (beff<=target || seff<mineff || rseff<minreleff)
continue;
496 std::sort(myidx.begin(), myidx.end(),
mycompare);
498 if (precut.Length()>3 && precut.Index(
vars[bestid]+
">")<0 && precut.Index(
vars[bestid]+
"<")<0)
507 TString thenum = bestcut(rnum);
508 double cutval = thenum.Atof();
509 TString thevar = bestcut(0,bestcut.Index(thenum));
511 thebarevar.ReplaceAll(
">",
"");
512 thebarevar.ReplaceAll(
"<",
"");
513 thebarevar.ReplaceAll(
"=",
"");
515 TRegexp rcut(thevar+
"[\\-]*[0-9]+\\.[0-9]+");
516 TString theoldcut = precut(rcut);
520 t->SetLineColor(1); t->Draw(thebarevar,precut);
521 t->SetLineColor(2); t->Draw(thebarevar,precut+
"&&"+sigcut,
"same");
522 t->SetLineColor(4); t->Draw(thebarevar,precut+
"&&"+bgcut,
"same");
524 TLine l; l.SetLineColor(6); l.SetLineWidth(2);
525 l.DrawLine(cutval,0,cutval,gPad->GetUymax()*0.9);
530 precut.ReplaceAll(theoldcut,bestcut);
532 precut +=
"&&"+bestcut;
534 cout <<
" : "<<precut<<endl;
538 cout <<
"\nCUT : "<<en<<smode<<
" : "<<precut.Data()<<endl;
539 cout <<
"SIG EVT: "<<
Nsigev<<
" ev BG: "<<
Nbgev<<
" ev"<<endl;
542 printf(
"SIG REL : %6.1f%% BG REL : %7.3f%%\n\n",
Nsigev/nsig*100.,
Nbgev/nbg*100.);
544 cout<<
"Recoil : ";
for (
int k=0;k<10;++k)
printf(
" %02d ",k);cout <<endl;
545 cout<<
"Eff : ";
for (
int k=0;k<10;++k)
printf(
"%6.1f%%",(
double)
evcntrec[k].size()/evmult*norm*100.);cout <<endl<<endl;
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
void makeMaps(TTree *t, TString varname, TEventList &els, TEventList &elb, ValueMap &sig, ValueMap &bg, int id)
int findcut(TTree *t, TEventList &els, TEventList &elb, double supr, double &bestqa)
bool mycompare(int i, int j)
int autocutx(TString fname, TString precut="", double supr=0.95, double target=0.0001, double mineff=0.1, double minreleff=0.0, int evmult=10000, double norm=1.0, int n0s=-1)
double bestSuppressionEvt(TTree *t, TString varname, TEventList &els, TEventList &elb, double &bestcut, double eff, int id)
int uid(int lev, int lrun, int lmode)
std::vector< pair< double, int > > ValueMap
int gettype(TTree *t, TString varname)
friend F32vec4 fabs(const F32vec4 &a)
std::map< TString, TString > mctvar
std::map< int, int > CountMap
int countEvents(TTree *t, TEventList &el)
double bestEffEvt(TTree *t, TString varname, TEventList &els, TEventList &elb, double &bestcut, double supr, int id)