49 std::string factoryOptions(
"!V:!Silent:Transformations=I;D;P;G,D:AnalysisType=Classification" );
50 TString fname =
"./tmva_example_multiple_background.root";
56 TTree *background0 = (
TTree*)input->Get(
"TreeB0");
57 TTree *background1 = (
TTree*)input->Get(
"TreeB1");
58 TTree *background2 = (
TTree*)input->Get(
"TreeB2");
67 TString outfileName(
"TMVASignalBackground0.root" );
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" );
188void ApplicationCreateCombinedTree(){
191 TString outfileName(
"tmva_example_multiple_backgrounds__applied.root" );
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");
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;
281 std::cout <<
"--- Processing: " << theTree->
GetEntries() <<
" events" << std::endl;
286 for (
Long64_t ievt=0; ievt<nEvent; ievt++) {
289 std::cout <<
"--- ... Processing event: " << ievt << std::endl;
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;
421void 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");
447 const TString opts(
"PopSize=100:Steps=30" );
452 std::vector<Double_t> result;
453 Double_t estimator = mg.Run(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;
470void TMVAMultipleBackgroundExample()
475 cout <<
"Start Test TMVAGAexample" << endl
476 <<
"========================" << endl
479 TString createDataMacro =
gROOT->GetTutorialDir() +
"/tmva/createData.C";
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();
500int main(
int argc,
char** argv ) {
501 TMVAMultipleBackgroundExample();
char * Form(const char *fmt,...)
A chain is a collection of files containing TTree objects.
virtual Int_t Add(TChain *chain)
Add all files referenced by the passed chain to this chain.
A specialized string object used for TTree selections.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Int_t Write(const char *name=nullptr, Int_t opt=0, Int_t bufsiz=0) override
Write memory objects to this file.
void Close(Option_t *option="") override
Close a file.
1-D histogram with a float per channel (see TH1 documentation)}
void AddSignalTree(TTree *signal, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
prepare the training and test trees -> same cuts for signal and background
void AddBackgroundTree(TTree *background, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
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
This is the main MVA steering class.
void TrainAllMethods()
Iterates through all booked methods and calls training.
MethodBase * BookMethod(DataLoader *loader, TString theMethodName, TString methodTitle, TString theOption="")
Book a classifier or regression method.
void TestAllMethods()
Evaluates all booked methods on the testing data and adds the output to the Results in the corresponi...
void EvaluateAllMethods(void)
Iterates over all MVAs that have been booked, and calls their evaluation methods.
Fitter using a Genetic Algorithm.
Interface for a fitter 'target'.
The TMVA::Interval Class.
The Reader class serves to use the MVAs in a specific analysis context.
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...
IMethod * BookMVA(const TString &methodTag, const TString &weightfile)
read method name from weight file
void AddVariable(const TString &expression, Float_t *)
Add a float variable or expression to the reader.
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
void Stop()
Stop the stopwatch.
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
const char * Data() const
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
A TTree represents a columnar dataset.
virtual Int_t Fill()
Fill all branches.
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
virtual Long64_t GetEntries() const
TBranch * Branch(const char *name, T *obj, Int_t bufsize=32000, Int_t splitlevel=99)
Add a new branch, and infer the data type from the type of obj being passed.
void Print(std::ostream &os, const OptionType &opt)
create variable transformations