40using std::vector, std::cout, std::endl;
49 std::string factoryOptions(
"!V:!Silent:Transformations=I;D;P;G,D:AnalysisType=Classification" );
50 TString fname =
"./tmva_example_multiple_background.root";
52 TFile *input(
nullptr);
55 std::cout <<
"ERROR: could not open data file" << std::endl;
60 TTree *background0 = (
TTree*)input->Get(
"TreeB0");
61 TTree *background1 = (
TTree*)input->Get(
"TreeB1");
62 TTree *background2 = (
TTree*)input->Get(
"TreeB2");
71 TString outfileName(
"TMVASignalBackground0.root" );
81 dataloader->
AddVariable(
"var1",
"Variable 1",
"",
'F' );
82 dataloader->
AddVariable(
"var2",
"Variable 2",
"",
'F' );
83 dataloader->
AddVariable(
"var3",
"Variable 3",
"units",
'F' );
84 dataloader->
AddVariable(
"var4",
"Variable 4",
"units",
'F' );
95 "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
99 "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.6:SeparationType=GiniIndex:nCuts=20:MaxDepth=2" );
114 outfileName =
"TMVASignalBackground1.root";
115 outputFile =
TFile::Open( outfileName,
"RECREATE" );
118 factory =
new TMVA::Factory(
"TMVAMultiBkg1", outputFile, factoryOptions );
119 dataloader->
AddVariable(
"var1",
"Variable 1",
"",
'F' );
120 dataloader->
AddVariable(
"var2",
"Variable 2",
"",
'F' );
121 dataloader->
AddVariable(
"var3",
"Variable 3",
"units",
'F' );
122 dataloader->
AddVariable(
"var4",
"Variable 4",
"units",
'F' );
131 "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
135 "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.6:SeparationType=GiniIndex:nCuts=20:MaxDepth=2" );
149 outfileName =
"TMVASignalBackground2.root";
150 outputFile =
TFile::Open( outfileName,
"RECREATE" );
152 factory =
new TMVA::Factory(
"TMVAMultiBkg2", outputFile, factoryOptions );
155 dataloader->
AddVariable(
"var1",
"Variable 1",
"",
'F' );
156 dataloader->
AddVariable(
"var2",
"Variable 2",
"",
'F' );
157 dataloader->
AddVariable(
"var3",
"Variable 3",
"units",
'F' );
158 dataloader->
AddVariable(
"var4",
"Variable 4",
"units",
'F' );
167 "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
171 "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20:MaxDepth=2" );
192void ApplicationCreateCombinedTree(){
195 TString outfileName(
"tmva_example_multiple_backgrounds__applied.root" );
197 TTree* outputTree =
new TTree(
"multiBkg",
"multiple backgrounds tree");
204 Float_t classifier0, classifier1, classifier2;
206 outputTree->
Branch(
"classID", &classID,
"classID/I");
207 outputTree->
Branch(
"var1", &var1,
"var1/F");
208 outputTree->
Branch(
"var2", &var2,
"var2/F");
209 outputTree->
Branch(
"var3", &var3,
"var3/F");
210 outputTree->
Branch(
"var4", &var4,
"var4/F");
211 outputTree->
Branch(
"weight", &weight,
"weight/F");
212 outputTree->
Branch(
"cls0", &classifier0,
"cls0/F");
213 outputTree->
Branch(
"cls1", &classifier1,
"cls1/F");
214 outputTree->
Branch(
"cls2", &classifier2,
"cls2/F");
239 reader0->
BookMVA(
"BDT method",
"datasetBkg0/weights/TMVAMultiBkg0_BDTG.weights.xml" );
240 reader1->
BookMVA(
"BDT method",
"datasetBkg1/weights/TMVAMultiBkg1_BDTG.weights.xml" );
241 reader2->
BookMVA(
"BDT method",
"datasetBkg2/weights/TMVAMultiBkg2_BDTG.weights.xml" );
245 TString fname =
"./tmva_example_multiple_background.root";
248 TTree* theTree = NULL;
251 for(
int treeNumber = 0; treeNumber < 4; ++treeNumber ) {
252 if( treeNumber == 0 ){
253 theTree = (
TTree*)input->Get(
"TreeS");
254 std::cout <<
"--- Select signal sample" << std::endl;
258 }
else if( treeNumber == 1 ){
259 theTree = (
TTree*)input->Get(
"TreeB0");
260 std::cout <<
"--- Select background 0 sample" << std::endl;
264 }
else if( treeNumber == 2 ){
265 theTree = (
TTree*)input->Get(
"TreeB1");
266 std::cout <<
"--- Select background 1 sample" << std::endl;
270 }
else if( treeNumber == 3 ){
271 theTree = (
TTree*)input->Get(
"TreeB2");
272 std::cout <<
"--- Select background 2 sample" << std::endl;
285 std::cout <<
"--- Processing: " << theTree->
GetEntries() <<
" events" << std::endl;
290 for (
Long64_t ievt=0; ievt<nEvent; ievt++) {
293 std::cout <<
"--- ... Processing event: " << ievt << std::endl;
309 std::cout <<
"--- End of event loop: "; sw.
Print();
321 std::cout <<
"--- Created root file: \"" << outfileName.Data() <<
"\" containing the MVA output histograms" << std::endl;
327 std::cout <<
"==> Application of readers is done! combined tree created" << std::endl << std::endl;
341 MyFitness( TChain* _chain ) : IFitterTarget() {
344 hSignal =
new TH1F(
"hsignal",
"hsignal",100,-1,1);
345 hFP =
new TH1F(
"hfp",
"hfp",100,-1,1);
346 hTP =
new TH1F(
"htp",
"htp",100,-1,1);
348 TString cutsAndWeightSignal =
"weight*(classID==0)";
349 nSignal = chain->Draw(
"Entry$/Entries$>>hsignal",cutsAndWeightSignal,
"goff");
350 weightsSignal = hSignal->Integral();
355 Double_t EstimatorFunction( std::vector<Double_t> & factors ){
357 TString cutsAndWeightTruePositive =
Form(
"weight*((classID==0) && cls0>%f && cls1>%f && cls2>%f )",factors.at(0), factors.at(1), factors.at(2));
358 TString cutsAndWeightFalsePositive =
Form(
"weight*((classID >0) && cls0>%f && cls1>%f && cls2>%f )",factors.at(0), factors.at(1), factors.at(2));
361 Float_t nTP = chain->Draw(
"Entry$/Entries$>>htp",cutsAndWeightTruePositive,
"goff");
362 Float_t nFP = chain->Draw(
"Entry$/Entries$>>hfp",cutsAndWeightFalsePositive,
"goff");
364 weightsTruePositive = hTP->Integral();
365 weightsFalsePositive = hFP->Integral();
368 if( weightsSignal > 0 )
369 efficiency = weightsTruePositive/weightsSignal;
372 if( weightsTruePositive+weightsFalsePositive > 0 )
373 purity = weightsTruePositive/(weightsTruePositive+weightsFalsePositive);
377 Float_t toMinimize = std::numeric_limits<float>::max();
378 if( effTimesPur > 0 )
379 toMinimize = 1./(effTimesPur);
388 std::cout << std::endl;
389 std::cout <<
"======================" << std::endl
391 <<
"Purity : " << purity << std::endl << std::endl
392 <<
"True positive weights : " << weightsTruePositive << std::endl
393 <<
"False positive weights: " << weightsFalsePositive << std::endl
394 <<
"Signal weights : " << weightsSignal << std::endl;
425void MaximizeSignificance(){
430 ranges.push_back(
new Interval(-1,1) );
431 ranges.push_back(
new Interval(-1,1) );
432 ranges.push_back(
new Interval(-1,1) );
434 std::cout <<
"Classifier ranges (defined by the user)" << std::endl;
435 for( std::vector<Interval*>::iterator it = ranges.begin(); it != ranges.end(); it++ ){
436 std::cout <<
" range: " << (*it)->GetMin() <<
" " << (*it)->GetMax() << std::endl;
440 chain->Add(
"tmva_example_multiple_backgrounds__applied.root");
451 const TString opts(
"PopSize=100:Steps=30" );
456 std::vector<Double_t> result;
457 Double_t estimator = mg.Run(result);
459 dynamic_cast<MyFitness*
>(myFitness)->
Print();
460 std::cout << std::endl;
463 for( std::vector<Double_t>::iterator it = result.begin(); it<result.end(); it++ ){
464 std::cout <<
" cutValue[" <<
n <<
"] = " << (*it) <<
";"<< std::endl;
474void TMVAMultipleBackgroundExample()
479 cout <<
"Start Test TMVAGAexample" << endl
480 <<
"========================" << endl
483 TString createDataMacro =
gROOT->GetTutorialDir() +
"/machine_learning/createData.C";
485 gROOT->ProcessLine(
"create_MultipleBackground(200)");
489 cout <<
"========================" << endl;
490 cout <<
"--- Training" << endl;
494 cout <<
"========================" << endl;
495 cout <<
"--- Application & create combined tree" << endl;
496 ApplicationCreateCombinedTree();
499 cout <<
"========================" << endl;
500 cout <<
"--- maximize significance" << endl;
501 MaximizeSignificance();
504int main(
int argc,
char** argv ) {
505 TMVAMultipleBackgroundExample();
int Int_t
Signed integer 4 bytes (int).
double Double_t
Double 8 bytes.
long long Long64_t
Portable signed long integer 8 bytes.
float Float_t
Float 4 bytes (float).
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
A chain is a collection of files containing TTree objects.
A specialized string object used for TTree selections.
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Int_t Write(const char *name=nullptr, Int_t opt=0, Int_t bufsize=0) override
Write memory objects to this file.
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.
void Close(Option_t *option="") override
Close a file.
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.
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.
MethodBase * BookMethod(DataLoader *loader, MethodName theMethodName, TString methodTitle, TString theOption="")
Books an MVA classifier or regression method.
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 override
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 SetBranchAddress(const char *bname, void *add, TBranch **ptr, TClass *realClass, EDataType datatype, bool isptr, bool suppressMissingBranchError)
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
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)
double efficiency(double effFuncVal, int catIndex, int sigCatIndex)
create variable transformations