1 #include "TMVA/Reader.h"
6 #include "TEventList.h"
24 typedef std::vector<pair<float, int> >
ValueMap;
30 if (t->GetBranch(varname)==0)
return -1;
32 TString leaftype = t->GetLeaf(varname)->GetTypeName();
34 if (leaftype==
"Float_t")
return 0;
35 else if (leaftype==
"Int_t")
return 1;
36 else if (leaftype==
"Bool_t")
return 2;
45 TObjArray *tok = s.Tokenize(delim);
46 int N = tok->GetEntries();
50 toks[
i] = ((TObjString*)tok->At(
i))->String();
51 toks[
i].ReplaceAll(
"\t",
"");
52 toks[
i] = toks[
i].Strip(TString::kBoth);
61 t->SetBranchAddress(
"ev",&
ev);
62 t->SetBranchAddress(
"run",&
run);
63 t->SetBranchAddress(
"mode",&
mode);
64 t->SetBranchAddress(
"recmode",&
rec);
75 case 0: t->SetBranchAddress(v, &(
fbranch[i]));
break;
76 case 1: t->SetBranchAddress(v, &(
ibranch[i]));
break;
77 case 2: t->SetBranchAddress(v, &(
bbranch[i]));
break;
87 int uid(
int lev,
int lrun,
int lmode)
89 return lev+10000*lrun+(((lmode/100)%10)*20+lmode%10)*100000;
96 t->SetBranchStatus(
"*",0);
97 t->SetBranchStatus(
"ev",1);
98 t->SetBranchStatus(
"run",1);
99 t->SetBranchStatus(
"mode",1);
100 t->SetBranchStatus(
"recmode",1);
104 for (
int i=0;
i<el.GetN();++
i)
106 t->GetEntry(el.GetEntry(
i));
109 t->SetBranchStatus(
"*",1);
120 TRegexp rvar(
"[_a-zA-Z][_a-zA-Z0-9]*");
124 for (
int i=0;
i<
n;++
i)
129 if (v==
"tag" || res.Contains(
" "+v+
" "))
continue;
134 res = res.Strip(TString::kBoth);
142 if (vars.Contains(
"&&")) vars =
getFromCut(vars);
143 cout <<
"Vars : "<<vars<<endl;
145 TString sigcut =
"tag&&mode%1000!=900";
146 TString bkgcut =
"tag&&mode%1000==900";
150 cout <<
"Precut : "<<precut<<endl;
151 sigcut +=
"&&" + precut;
152 bkgcut +=
"&&" + precut;
156 TRegexp rmode(
"M[0-9][0-9][0-9]");
157 TRegexp rntp(
"n[0-9][0-9][0-9]");
158 TRegexp regS(
"[0-9]+S");
159 TRegexp regB(
"[0-9]+B");
161 TString s = fname(regS); s =
s(0,s.Length()-1);
162 TString b = fname(regB); b =
b(0,b.Length()-1);
163 int n0s = s.Atoi()*10000;
164 int n0b = b.Atoi()*10000;
165 TString treename = fname(rntp);
168 if (wfile==
"") wfile=
"weights/M"+smode(1,3)+treename(1,3)+
"_BDT.weights.xml";
170 TFile *
f = TFile::Open(fname);
171 TTree *
t =(TTree*) f->Get(treename);
174 int Nbr =
init(t,vars);
179 TMVA::Reader *reader =
new TMVA::Reader(
"Silent");
180 for (
int i=0;
i<Nbr;++
i) reader->AddVariable(varname[
i], &
freader[i]);
181 reader->BookMVA(
"BDT",wfile);
184 TEventList elsall(
"elsall"), elball(
"elball");
185 t->Draw(
">>elsall",sigcut);
186 t->Draw(
">>elball",bkgcut);
192 if (precut!=
"") tagcut+=
"&&"+precut;
193 t->Draw(
">>el",tagcut);
194 TEventList *el=(TEventList*)gDirectory->Get(
"el");
202 for (
int i=0;i<N;++
i)
204 t->GetEntry(el->GetEntry(i));
206 if (i%10000==0) cout <<
"#"<<flush;
208 for (
int j=0;j<Nbr;++j)
211 switch (
types[j]) {
case 2: var = (Float_t)
bbranch[j];
break;
case 1: var = (Float_t)
ibranch[j];
break;
default: var=
fbranch[j]; }
215 float mvares = reader->EvaluateMVA(
"BDT");
218 bkgvals.push_back( std::make_pair(mvares,
uid(
ev,
run,
mode) ));
220 sigvals.push_back( std::make_pair(mvares,
uid(
ev,
run,
mode) ));
223 sort(bkgvals.begin(), bkgvals.end());
225 int nbgmax = bglevel*n0b, i = bkgvals.size()-1;
226 while ((
int)bkgcnt.size()<nbgmax && i>=0) bkgcnt[bkgvals[i--].second]+=1;
227 double cut = bkgvals[
i].first;
229 for (i=0;i<(int)sigvals.size();++
i)
230 if (sigvals[i].first>cut) sigcnt[sigvals[
i].second]+=1;
232 printf(
"S : eff = %0.3f N = %5d / %5d N0 = %d\n", (
double)sigcnt.size()/n0s, (int)sigcnt.size(), (int)nsig, n0s);
233 printf(
"B : eff = %0.5f N = %5d / %5d N0 = %d\n", (
double)bkgcnt.size()/n0b, (int)bkgcnt.size(), (int)nbkg, n0b);
234 cout <<
"\nCUT = "<<cut<<endl;
236 if (precut==
"") precut=
"tag";
237 else precut=
"tag&&"+precut;
238 TString cfgline = smode(1,3)+treename(1,3)+
" : "+precut+
" : M"+smode(1,3)+treename(1,3)+
"_BDT "+vars+
" "+TString::Format(
"%f",cut);
239 cout <<
"cfgline -> "<<cfgline<<endl;
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
int TMVAApply(TString fname, TString vars, TString precut="", TString wfile="", Float_t bglevel=0.0001)
TString getFromCut(TString vars)
int uid(int lev, int lrun, int lmode)
std::vector< pair< double, int > > ValueMap
int gettype(TTree *t, TString varname)
int SplitString(TString s, TString delim, StrVec &toks)
std::map< int, int > CountMap
int countEvents(TTree *t, TEventList &el)