49 std::string factoryOptions(
"!V:!Silent:Transformations=I;D;P;G,D:AnalysisType=Classification" );
50 TString fname =
"./tmva_example_multiple_background.root";
55 TTree *
signal = (TTree*)input->Get(
"TreeS");
67 TString outfileName(
"TMVASignalBackground0.root" );
68 TFile* outputFile =
TFile::Open( outfileName,
"RECREATE" );
77 dataloader->
AddVariable(
"var1",
"Variable 1",
"",
'F' );
78 dataloader->
AddVariable(
"var2",
"Variable 2",
"",
'F' );
79 dataloader->
AddVariable(
"var3",
"Variable 3",
"units",
'F' );
80 dataloader->
AddVariable(
"var4",
"Variable 4",
"units",
'F' );
91 "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
95 "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.6:SeparationType=GiniIndex:nCuts=20:MaxDepth=2" );
110 outfileName =
"TMVASignalBackground1.root";
111 outputFile =
TFile::Open( outfileName,
"RECREATE" );
114 factory =
new TMVA::Factory(
"TMVAMultiBkg1", outputFile, factoryOptions );
115 dataloader->
AddVariable(
"var1",
"Variable 1",
"",
'F' );
116 dataloader->
AddVariable(
"var2",
"Variable 2",
"",
'F' );
117 dataloader->
AddVariable(
"var3",
"Variable 3",
"units",
'F' );
118 dataloader->
AddVariable(
"var4",
"Variable 4",
"units",
'F' );
127 "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
131 "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.6:SeparationType=GiniIndex:nCuts=20:MaxDepth=2" );
145 outfileName =
"TMVASignalBackground2.root";
146 outputFile =
TFile::Open( outfileName,
"RECREATE" );
148 factory =
new TMVA::Factory(
"TMVAMultiBkg2", outputFile, factoryOptions );
151 dataloader->
AddVariable(
"var1",
"Variable 1",
"",
'F' );
152 dataloader->
AddVariable(
"var2",
"Variable 2",
"",
'F' );
153 dataloader->
AddVariable(
"var3",
"Variable 3",
"units",
'F' );
154 dataloader->
AddVariable(
"var4",
"Variable 4",
"units",
'F' );
163 "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
167 "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20:MaxDepth=2" );
188 void ApplicationCreateCombinedTree(){
191 TString outfileName(
"tmva_example_multiple_backgrounds__applied.root" );
192 TFile* outputFile =
TFile::Open( outfileName,
"RECREATE" );
193 TTree* outputTree =
new TTree(
"multiBkg",
"multiple backgrounds tree");
200 Float_t classifier0, classifier1, classifier2;
202 outputTree->Branch(
"classID", &classID,
"classID/I");
203 outputTree->Branch(
"var1", &var1,
"var1/F");
204 outputTree->Branch(
"var2", &var2,
"var2/F");
205 outputTree->Branch(
"var3", &var3,
"var3/F");
206 outputTree->Branch(
"var4", &var4,
"var4/F");
207 outputTree->Branch(
"weight", &weight,
"weight/F");
208 outputTree->Branch(
"cls0", &classifier0,
"cls0/F");
209 outputTree->Branch(
"cls1", &classifier1,
"cls1/F");
210 outputTree->Branch(
"cls2", &classifier2,
"cls2/F");
234 TString method =
"BDT method";
235 reader0->
BookMVA(
"BDT method",
"datasetBkg0/weights/TMVAMultiBkg0_BDTG.weights.xml" );
236 reader1->
BookMVA(
"BDT method",
"datasetBkg1/weights/TMVAMultiBkg1_BDTG.weights.xml" );
237 reader2->
BookMVA(
"BDT method",
"datasetBkg2/weights/TMVAMultiBkg2_BDTG.weights.xml" );
241 TString fname =
"./tmva_example_multiple_background.root";
244 TTree* theTree = NULL;
247 for(
int treeNumber = 0; treeNumber < 4; ++treeNumber ) {
248 if( treeNumber == 0 ){
249 theTree = (TTree*)input->Get(
"TreeS");
250 std::cout <<
"--- Select signal sample" << std::endl;
254 }
else if( treeNumber == 1 ){
255 theTree = (TTree*)input->Get(
"TreeB0");
256 std::cout <<
"--- Select background 0 sample" << std::endl;
260 }
else if( treeNumber == 2 ){
261 theTree = (TTree*)input->Get(
"TreeB1");
262 std::cout <<
"--- Select background 1 sample" << std::endl;
266 }
else if( treeNumber == 3 ){
267 theTree = (TTree*)input->Get(
"TreeB2");
268 std::cout <<
"--- Select background 2 sample" << std::endl;
275 theTree->SetBranchAddress(
"var1", &var1 );
276 theTree->SetBranchAddress(
"var2", &var2 );
277 theTree->SetBranchAddress(
"var3", &var3 );
278 theTree->SetBranchAddress(
"var4", &var4 );
281 std::cout <<
"--- Processing: " << theTree->GetEntries() <<
" events" << std::endl;
284 Int_t nEvent = theTree->GetEntries();
286 for (
Long64_t ievt=0; ievt<nEvent; ievt++) {
289 std::cout <<
"--- ... Processing event: " << ievt << std::endl;
292 theTree->GetEntry(ievt);
305 std::cout <<
"--- End of event loop: "; sw.
Print();
317 std::cout <<
"--- Created root file: \"" << outfileName.Data() <<
"\" containing the MVA output histograms" << std::endl;
323 std::cout <<
"==> Application of readers is done! combined tree created" << std::endl << std::endl;
340 hSignal =
new TH1F(
"hsignal",
"hsignal",100,-1,1);
341 hFP =
new TH1F(
"hfp",
"hfp",100,-1,1);
342 hTP =
new TH1F(
"htp",
"htp",100,-1,1);
344 TString cutsAndWeightSignal =
"weight*(classID==0)";
345 nSignal = chain->Draw(
"Entry$/Entries$>>hsignal",cutsAndWeightSignal,
"goff");
346 weightsSignal = hSignal->Integral();
351 Double_t EstimatorFunction( std::vector<Double_t> & factors ){
353 TString cutsAndWeightTruePositive =
Form(
"weight*((classID==0) && cls0>%f && cls1>%f && cls2>%f )",factors.at(0), factors.at(1), factors.at(2));
354 TString cutsAndWeightFalsePositive =
Form(
"weight*((classID >0) && cls0>%f && cls1>%f && cls2>%f )",factors.at(0), factors.at(1), factors.at(2));
357 Float_t nTP = chain->Draw(
"Entry$/Entries$>>htp",cutsAndWeightTruePositive,
"goff");
358 Float_t nFP = chain->Draw(
"Entry$/Entries$>>hfp",cutsAndWeightFalsePositive,
"goff");
360 weightsTruePositive = hTP->Integral();
361 weightsFalsePositive = hFP->Integral();
364 if( weightsSignal > 0 )
365 efficiency = weightsTruePositive/weightsSignal;
368 if( weightsTruePositive+weightsFalsePositive > 0 )
369 purity = weightsTruePositive/(weightsTruePositive+weightsFalsePositive);
371 Float_t effTimesPur = efficiency*purity;
373 Float_t toMinimize = std::numeric_limits<float>::max();
374 if( effTimesPur > 0 )
375 toMinimize = 1./(effTimesPur);
384 std::cout << std::endl;
385 std::cout <<
"======================" << std::endl
386 <<
"Efficiency : " << efficiency << std::endl
387 <<
"Purity : " << purity << std::endl << std::endl
388 <<
"True positive weights : " << weightsTruePositive << std::endl
389 <<
"False positive weights: " << weightsFalsePositive << std::endl
390 <<
"Signal weights : " << weightsSignal << std::endl;
421 void MaximizeSignificance(){
425 vector<Interval*> ranges;
426 ranges.push_back(
new Interval(-1,1) );
427 ranges.push_back(
new Interval(-1,1) );
428 ranges.push_back(
new Interval(-1,1) );
430 std::cout <<
"Classifier ranges (defined by the user)" << std::endl;
431 for( std::vector<Interval*>::iterator it = ranges.begin(); it != ranges.end(); it++ ){
432 std::cout <<
" range: " << (*it)->GetMin() <<
" " << (*it)->GetMax() << std::endl;
436 chain->
Add(
"tmva_example_multiple_backgrounds__applied.root");
446 const TString
name(
"multipleBackgroundGA" );
447 const TString opts(
"PopSize=100:Steps=30" );
452 std::vector<Double_t> result;
455 dynamic_cast<MyFitness*
>(myFitness)->
Print();
456 std::cout << std::endl;
459 for( std::vector<Double_t>::iterator it = result.begin(); it<result.end(); it++ ){
460 std::cout <<
" cutValue[" << n <<
"] = " << (*it) <<
";"<< std::endl;
470 void TMVAMultipleBackgroundExample()
475 cout <<
"Start Test TMVAGAexample" << endl
476 <<
"========================" << endl
481 gROOT->ProcessLine(
"create_MultipleBackground(200)");
485 cout <<
"========================" << endl;
486 cout <<
"--- Training" << endl;
490 cout <<
"========================" << endl;
491 cout <<
"--- Application & create combined tree" << endl;
492 ApplicationCreateCombinedTree();
495 cout <<
"========================" << endl;
496 cout <<
"--- maximize significance" << endl;
497 MaximizeSignificance();
500 int main(
int argc,
char** argv ) {
501 TMVAMultipleBackgroundExample();
void AddBackgroundTree(TTree *background, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
MethodBase * BookMethod(DataLoader *loader, TString theMethodName, TString methodTitle, TString theOption="")
Book a classifier or regression method.
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
void AddVariable(const TString &expression, Float_t *)
Add a float variable or expression to the reader.
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
1-D histogram with a float per channel (see TH1 documentation)}
void TrainAllMethods()
Iterates through all booked methods and calls training.
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
static constexpr double mg
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
void Stop()
Stop the stopwatch.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
int main(int argc, char **argv)
IMethod * BookMVA(const TString &methodTag, const TString &weightfile)
read method name from weight file
The TMVA::Interval Class.
void EvaluateAllMethods(void)
Iterates over all MVAs that have been booked, and calls their evaluation methods. ...
char * Form(const char *fmt,...)
This is the main MVA steering class.
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
prepare the training and test trees -> same cuts for signal and background
void Print(std::ostream &os, const OptionType &opt)
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...
The Reader class serves to use the MVAs in a specific analysis context.
Abstract ClassifierFactory template that handles arbitrary types.
A chain is a collection of files containing TTree objects.
Interface for a fitter 'target'.
Fitter using a Genetic Algorithm.
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 Add(TChain *chain)
Add all files referenced by the passed chain to this chain.