1 #include "TMVA/Factory.h"
6 #include "TEventList.h"
20 if (t->GetBranch(varname)==0)
return -1;
22 TString leaftype = t->GetLeaf(varname)->GetTypeName();
24 if (leaftype==
"Float_t")
return 0;
25 else if (leaftype==
"Int_t")
return 1;
26 else if (leaftype==
"Bool_t")
return 2;
35 TObjArray *tok = s.Tokenize(delim);
36 int N = tok->GetEntries();
40 toks[
i] = ((TObjString*)tok->At(
i))->String();
41 toks[
i].ReplaceAll(
"\t",
"");
42 toks[
i] = toks[
i].Strip(TString::kBoth);
54 TRegexp rvar(
"[_a-zA-Z][_a-zA-Z0-9]*");
63 if (res.Contains(
" "+v+
" "))
continue;
68 res = res.Strip(TString::kBoth);
78 if ( fname==
"" || treename==
"" || sigcut==
"" ||
vars==
"" )
81 cout <<
"TMVATrainer.C( <input>, <tree>, <sigcut>, <vars>, [method], [precut] )\n\n";
82 cout <<
" <input> : input file name containing TTree <tree>\n";
83 cout <<
" <tree> : name of the TTree containing signal and background\n";
84 cout <<
" <sigcut> : cut separating signal from background -> bgcut = !(sigcut)\n";
85 cout <<
" <vars> : blank separated list with variables for training\n";
86 cout <<
" [method] : optional method: 'BDT' (default), 'MLP' or 'LH'\n";
87 cout <<
" [precut] : optional precut before training; has to be applied also before testing!\n\n";
89 cout <<
"root -l -b -q 'TMVATrainer.C(\"demodata.root\",\"ntp\",\"signal>0\",\"v1 v2 v3 v4 v5\",\"\",\"MLP\")'\n\n";
93 if (algo==
"LH") algo=
"Likelihood";
96 cout <<
"Vars : "<<
vars<<endl;
102 cout <<
"Precut : "<<precut<<endl;
103 sigcut +=
"&&" + precut;
104 bkgcut +=
"&&" + precut;
107 TFile *
f = TFile::Open(fname);
108 TTree *
t =(TTree*) f->Get(treename);
111 outfname.ReplaceAll(
".root",
"");
112 TString tmvaname = outfname+
"_"+treename;
113 outfname=outfname+
"_tmva.root";
115 TFile* outputFile = TFile::Open(outfname,
"RECREATE" );
117 TMVA::Factory *factory =
new TMVA::Factory( tmvaname, outputFile,
"Silent:!V:Transformations=I;N;D");
122 for (
int i=0;
i<N;++
i)
125 if (btype==0 || btype==1)
126 factory->AddVariable(toks[i], btype==0?
'F':
'I');
129 factory->AddTree( t,
"Signal", 1.0, sigcut.Data());
130 factory->AddTree( t,
"Background", 1.0, bkgcut.Data());
132 int nsig = t->GetEntries(sigcut);
133 int nbkg = t->GetEntries(bkgcut);
135 factory->PrepareTrainingAndTestTree(
"",
int(nsig*0.8),
int(nbkg*0.8),
int(nsig*0.19),
int(nbkg*0.19));
138 if (algo==
"BDT") factory->BookMethod( TMVA::Types::kBDT,
"BDT",
"!V:nTrees=400:BoostType=AdaBoost:nCuts=10:NNodesMax=10" );
141 else if (algo==
"MLP") factory->BookMethod( TMVA::Types::kMLP,
"MLP",
"!V:NCycles=50:HiddenLayers=10,10:TestRate=5" );
142 else if (algo==
"Likelihood") factory->BookMethod( TMVA::Types::kLikelihood,
"Likelihood",
"!V:NAvEvtPerBin=50" );
143 else {cout <<
"Unconfigured algorithm! Exiting..."<<endl;
return;}
145 factory->TrainAllMethods();
void TMVATrainer(TString fname="", TString treename="", TString sigcut="", TString vars="", TString algo="BDT", TString precut="")
int SplitString(TString s, TString delim, TString *toks, int maxtoks)
int gettype(TTree *t, TString varname)
TString getFromCut(TString vars)