46 std::string factoryOptions(
"!V:!Silent:Transformations=I;D;P;G,D:AnalysisType=Classification" );
47 TString fname =
"./tmva_example_multiple_background.root";
53 TTree *background0 = (
TTree*)input->Get(
"TreeB0");
54 TTree *background1 = (
TTree*)input->Get(
"TreeB1");
55 TTree *background2 = (
TTree*)input->Get(
"TreeB2");
64 TString outfileName(
"TMVASignalBackground0.root" );
65 TFile* outputFile =
TFile::Open( outfileName,
"RECREATE" );
71 factory->
AddVariable(
"var1",
"Variable 1",
"",
'F' );
72 factory->
AddVariable(
"var2",
"Variable 2",
"",
'F' );
73 factory->
AddVariable(
"var3",
"Variable 3",
"units",
'F' );
74 factory->
AddVariable(
"var4",
"Variable 4",
"units",
'F' );
85 "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
89 "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.6:SeparationType=GiniIndex:nCuts=20:MaxDepth=2" );
102 outfileName =
"TMVASignalBackground1.root";
103 outputFile =
TFile::Open( outfileName,
"RECREATE" );
105 factory =
new TMVA::Factory(
"TMVAMultiBkg1", outputFile, factoryOptions );
106 factory->
AddVariable(
"var1",
"Variable 1",
"",
'F' );
107 factory->
AddVariable(
"var2",
"Variable 2",
"",
'F' );
108 factory->
AddVariable(
"var3",
"Variable 3",
"units",
'F' );
109 factory->
AddVariable(
"var4",
"Variable 4",
"units",
'F' );
118 "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
122 "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.6:SeparationType=GiniIndex:nCuts=20:MaxDepth=2" );
135 outfileName =
"TMVASignalBackground2.root";
136 outputFile =
TFile::Open( outfileName,
"RECREATE" );
138 factory =
new TMVA::Factory(
"TMVAMultiBkg2", outputFile, factoryOptions );
139 factory->
AddVariable(
"var1",
"Variable 1",
"",
'F' );
140 factory->
AddVariable(
"var2",
"Variable 2",
"",
'F' );
141 factory->
AddVariable(
"var3",
"Variable 3",
"units",
'F' );
142 factory->
AddVariable(
"var4",
"Variable 4",
"units",
'F' );
151 "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
155 "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20:MaxDepth=2" );
175 TString outfileName(
"tmva_example_multiple_backgrounds__applied.root" );
176 TFile* outputFile =
TFile::Open( outfileName,
"RECREATE" );
177 TTree* outputTree =
new TTree(
"multiBkg",
"multiple backgrounds tree");
184 Float_t classifier0, classifier1, classifier2;
186 outputTree->
Branch(
"classID", &classID,
"classID/I");
187 outputTree->
Branch(
"var1", &var1,
"var1/F");
188 outputTree->
Branch(
"var2", &var2,
"var2/F");
189 outputTree->
Branch(
"var3", &var3,
"var3/F");
190 outputTree->
Branch(
"var4", &var4,
"var4/F");
191 outputTree->
Branch(
"weight", &weight,
"weight/F");
192 outputTree->
Branch(
"cls0", &classifier0,
"cls0/F");
193 outputTree->
Branch(
"cls1", &classifier1,
"cls1/F");
194 outputTree->
Branch(
"cls2", &classifier2,
"cls2/F");
219 reader0->
BookMVA(
"BDT method",
"weights/TMVAMultiBkg0_BDTG.weights.xml" );
220 reader1->
BookMVA(
"BDT method",
"weights/TMVAMultiBkg1_BDTG.weights.xml" );
221 reader2->
BookMVA(
"BDT method",
"weights/TMVAMultiBkg2_BDTG.weights.xml" );
225 TString fname =
"./tmva_example_multiple_background.root";
231 for(
int treeNumber = 0; treeNumber < 4; ++treeNumber ) {
232 if( treeNumber == 0 ){
233 theTree = (
TTree*)input->Get(
"TreeS");
234 std::cout <<
"--- Select signal sample" << std::endl;
238 }
else if( treeNumber == 1 ){
239 theTree = (
TTree*)input->Get(
"TreeB0");
240 std::cout <<
"--- Select background 0 sample" << std::endl;
244 }
else if( treeNumber == 2 ){
245 theTree = (
TTree*)input->Get(
"TreeB1");
246 std::cout <<
"--- Select background 1 sample" << std::endl;
250 }
else if( treeNumber == 3 ){
251 theTree = (
TTree*)input->Get(
"TreeB2");
252 std::cout <<
"--- Select background 2 sample" << std::endl;
265 std::cout <<
"--- Processing: " << theTree->
GetEntries() <<
" events" << std::endl;
270 for (
Long64_t ievt=0; ievt<nEvent; ievt++) {
273 std::cout <<
"--- ... Processing event: " << ievt << std::endl;
289 std::cout <<
"--- End of event loop: "; sw.
Print();
301 std::cout <<
"--- Created root file: \"" << outfileName.
Data() <<
"\" containing the MVA output histograms" << std::endl;
307 std::cout <<
"==> Application of readers is done! combined tree created" << std::endl << std::endl;
315 class MyFitness :
public IFitterTarget {
318 MyFitness(
TChain* _chain ) : IFitterTarget() {
321 hSignal =
new TH1F(
"hsignal",
"hsignal",100,-1,1);
322 hFP =
new TH1F(
"hfp",
"hfp",100,-1,1);
323 hTP =
new TH1F(
"htp",
"htp",100,-1,1);
325 TString cutsAndWeightSignal =
"weight*(classID==0)";
326 nSignal =
chain->
Draw(
"Entry$/Entries$>>hsignal",cutsAndWeightSignal,
"goff");
327 weightsSignal = hSignal->Integral();
332 Double_t EstimatorFunction( std::vector<Double_t> & factors ){
334 TString cutsAndWeightTruePositive =
Form(
"weight*((classID==0) && cls0>%f && cls1>%f && cls2>%f )",factors.at(0), factors.at(1), factors.at(2));
335 TString cutsAndWeightFalsePositive =
Form(
"weight*((classID >0) && cls0>%f && cls1>%f && cls2>%f )",factors.at(0), factors.at(1), factors.at(2));
338 Float_t nTP =
chain->
Draw(
"Entry$/Entries$>>htp",cutsAndWeightTruePositive,
"goff");
339 Float_t nFP =
chain->
Draw(
"Entry$/Entries$>>hfp",cutsAndWeightFalsePositive,
"goff");
341 weightsTruePositive = hTP->Integral();
342 weightsFalsePositive = hFP->Integral();
345 if( weightsSignal > 0 )
346 efficiency = weightsTruePositive/weightsSignal;
349 if( weightsTruePositive+weightsFalsePositive > 0 )
350 purity = weightsTruePositive/(weightsTruePositive+weightsFalsePositive);
352 Float_t effTimesPur = efficiency*purity;
355 if( effTimesPur > 0 )
356 toMinimize = 1./(effTimesPur);
365 std::cout << std::endl;
366 std::cout <<
"======================" << std::endl
367 <<
"Efficiency : " << efficiency << std::endl
368 <<
"Purity : " << purity << std::endl << std::endl
369 <<
"True positive weights : " << weightsTruePositive << std::endl
370 <<
"False positive weights: " << weightsFalsePositive << std::endl
371 <<
"Signal weights : " << weightsSignal << std::endl;
403 vector<Interval*> ranges;
404 ranges.push_back(
new Interval(-1,1) );
405 ranges.push_back(
new Interval(-1,1) );
406 ranges.push_back(
new Interval(-1,1) );
408 std::cout <<
"Classifier ranges (defined by the user)" << std::endl;
409 for( std::vector<Interval*>::iterator it = ranges.begin(); it != ranges.end(); it++ ){
410 std::cout <<
" range: " << (*it)->GetMin() <<
" " << (*it)->GetMax() << std::endl;
414 chain->
Add(
"tmva_example_multiple_backgrounds__applied.root");
425 const TString opts(
"PopSize=100:Steps=30" );
430 std::vector<Double_t>
result;
433 dynamic_cast<MyFitness*
>(myFitness)->
Print();
434 std::cout << std::endl;
437 for( std::vector<Double_t>::iterator it = result.begin(); it<result.end(); it++ ){
438 std::cout <<
" cutValue[" << n <<
"] = " << (*it) <<
";"<< std::endl;
452 cout <<
"Start Test TMVAGAexample" << endl
453 <<
"========================" << endl
458 gROOT->ProcessLine(
"create_MultipleBackground(2000)");
462 cout <<
"========================" << endl;
463 cout <<
"--- Training" << endl;
467 cout <<
"========================" << endl;
468 cout <<
"--- Application & create combined tree" << endl;
472 cout <<
"========================" << endl;
473 cout <<
"--- maximize significance" << endl;
477 int main(
int argc,
char** argv ) {
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
void AddVariable(const TString &expression, Float_t *)
Add a float variable or expression to the reader.
Double_t Run(std::vector< Double_t > &pars)
Execute fitting.
virtual Int_t Fill()
Fill all branches.
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
void AddVariable(const TString &expression, const TString &title, const TString &unit, char type='F', Double_t min=0, Double_t max=0)
user inserts discriminating variable in data set info
void TrainAllMethods()
iterates through all booked methods and calls training
void TMVAMultipleBackgroundExample()
void AddBackgroundTree(TTree *background, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
const char * Data() const
void Stop()
Stop the stopwatch.
virtual Long64_t Draw(const char *varexp, const TCut &selection, Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Draw expression varexp for selected entries.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
int main(int argc, char **argv)
IMethod * BookMVA(const TString &methodTag, const TString &weightfile)
read method name from weight file
A specialized string object used for TTree selections.
R__EXTERN TSystem * gSystem
MethodBase * BookMethod(TString theMethodName, TString methodTitle, TString theOption="")
Book a classifier or regression method.
void EvaluateAllMethods(void)
iterates over all MVAs that have been booked, and calls their evaluation methods
char * Form(const char *fmt,...)
void Print(std::ostream &os, const OptionType &opt)
void ApplicationCreateCombinedTree()
Double_t EvaluateMVA(const std::vector< Float_t > &, const TString &methodTag, Double_t aux=0)
Evaluate a std::vector<float> of input data for a given method The parameter aux is obligatory for th...
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
void AddSignalTree(TTree *signal, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
void MaximizeSignificance()
A chain is a collection of files containg TTree objects.
virtual Long64_t GetEntries() const
A TTree object has a header with a name and a title.
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
prepare the training and test trees -> same cuts for signal and background
virtual Int_t Add(TChain *chain)
Add all files referenced by the passed chain to this chain.