{ "cells": [ { "cell_type": "markdown", "id": "da274318", "metadata": {}, "source": [ "# TMVAMultipleBackgroundExample\n", "This example shows the training of signal with three different backgrounds\n", "Then in the application a tree is created with all signal and background\n", "events where the true class ID and the three classifier outputs are added\n", "finally with the application tree, the significance is maximized with the\n", "help of the TMVA genetic algorithm.\n", "- Project : TMVA - a Root-integrated toolkit for multivariate data analysis\n", "- Package : TMVA\n", "- Executable: TMVAGAexample\n", "\n", "\n", "\n", "**Author:** Andreas Hoecker \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:08 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "695c8065", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:08:30.777191Z", "iopub.status.busy": "2026-05-19T20:08:30.777067Z", "iopub.status.idle": "2026-05-19T20:08:30.785051Z", "shell.execute_reply": "2026-05-19T20:08:30.784440Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_44:27:1: error: expected expression\n", "%%cpp -d\n", "^\n", "input_line_44:27:2: error: expected expression\n", "%%cpp -d\n", " ^\n", "input_line_44:27:3: error: use of undeclared identifier 'cpp'\n", "%%cpp -d\n", " ^\n", "input_line_44:27:8: error: use of undeclared identifier 'd'\n", "%%cpp -d\n", " ^\n" ] } ], "source": [ "%%cpp -d\n", "#include // Stream declarations\n", "#include \n", "#include \n", "\n", "#include \"TChain.h\"\n", "#include \"TCut.h\"\n", "#include \"TDirectory.h\"\n", "#include \"TH1F.h\"\n", "#include \"TH1.h\"\n", "#include \"TMath.h\"\n", "#include \"TFile.h\"\n", "#include \"TStopwatch.h\"\n", "#include \"TROOT.h\"\n", "#include \"TSystem.h\"\n", "\n", "#include \"TMVA/GeneticAlgorithm.h\"\n", "#include \"TMVA/GeneticFitter.h\"\n", "#include \"TMVA/IFitterTarget.h\"\n", "#include \"TMVA/Factory.h\"\n", "#include \"TMVA/DataLoader.h\"//required to load dataset\n", "#include \"TMVA/Reader.h\"\n", "\n", "using std::vector, std::cout, std::endl;\n", "\n", "using namespace TMVA;\n", "\n", "%%cpp -d" ] }, { "cell_type": "markdown", "id": "3e10eaa1", "metadata": {}, "source": [ "-----------------------------------------------------------------------------------------\n", "Genetic Algorithm Fitness definition\n", "-----------------------------------------------------------------------------------------" ] }, { "cell_type": "code", "execution_count": 2, "id": "aa619ae6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:08:30.786331Z", "iopub.status.busy": "2026-05-19T20:08:30.786207Z", "iopub.status.idle": "2026-05-19T20:08:31.102630Z", "shell.execute_reply": "2026-05-19T20:08:31.101992Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_45:1:26: error: expected class name\n", "class MyFitness : public IFitterTarget {\n", " ^\n", "input_line_45:4:34: error: member initializer 'IFitterTarget' does not name a non-static data member or base class\n", " MyFitness( TChain* _chain ) : IFitterTarget() {\n", " ^~~~~~~~~~~~~~~\n" ] } ], "source": [ "class MyFitness : public IFitterTarget {\n", "public:\n", " // constructor\n", " MyFitness( TChain* _chain ) : IFitterTarget() {\n", " chain = _chain;\n", "\n", " hSignal = new TH1F(\"hsignal\",\"hsignal\",100,-1,1);\n", " hFP = new TH1F(\"hfp\",\"hfp\",100,-1,1);\n", " hTP = new TH1F(\"htp\",\"htp\",100,-1,1);\n", "\n", " TString cutsAndWeightSignal = \"weight*(classID==0)\";\n", " nSignal = chain->Draw(\"Entry$/Entries$>>hsignal\",cutsAndWeightSignal,\"goff\");\n", " weightsSignal = hSignal->Integral();\n", "\n", " }\n", "\n", " // the output of this function will be minimized\n", " Double_t EstimatorFunction( std::vector & factors ){\n", "\n", " TString cutsAndWeightTruePositive = Form(\"weight*((classID==0) && cls0>%f && cls1>%f && cls2>%f )\",factors.at(0), factors.at(1), factors.at(2));\n", " TString cutsAndWeightFalsePositive = Form(\"weight*((classID >0) && cls0>%f && cls1>%f && cls2>%f )\",factors.at(0), factors.at(1), factors.at(2));\n", "\n", " // Entry$/Entries$ just draws something reasonable. Could in principle anything\n", " Float_t nTP = chain->Draw(\"Entry$/Entries$>>htp\",cutsAndWeightTruePositive,\"goff\");\n", " Float_t nFP = chain->Draw(\"Entry$/Entries$>>hfp\",cutsAndWeightFalsePositive,\"goff\");\n", "\n", " weightsTruePositive = hTP->Integral();\n", " weightsFalsePositive = hFP->Integral();\n", "\n", " efficiency = 0;\n", " if( weightsSignal > 0 )\n", " efficiency = weightsTruePositive/weightsSignal;\n", "\n", " purity = 0;\n", " if( weightsTruePositive+weightsFalsePositive > 0 )\n", " purity = weightsTruePositive/(weightsTruePositive+weightsFalsePositive);\n", "\n", " Float_t effTimesPur = efficiency*purity;\n", "\n", " Float_t toMinimize = std::numeric_limits::max(); // set to the highest existing number\n", " if( effTimesPur > 0 ) // if larger than 0, take 1/x. This is the value to minimize\n", " toMinimize = 1./(effTimesPur); // we want to minimize 1/efficiency*purity\n", "\n", " // Print();\n", "\n", " return toMinimize;\n", " }\n", "\n", "\n", " void Print(){\n", " std::cout << std::endl;\n", " std::cout << \"======================\" << std::endl\n", " << \"Efficiency : \" << efficiency << std::endl\n", " << \"Purity : \" << purity << std::endl << std::endl\n", " << \"True positive weights : \" << weightsTruePositive << std::endl\n", " << \"False positive weights: \" << weightsFalsePositive << std::endl\n", " << \"Signal weights : \" << weightsSignal << std::endl;\n", " }\n", "\n", " Float_t nSignal;\n", "\n", " Float_t efficiency;\n", " Float_t purity;\n", " Float_t weightsTruePositive;\n", " Float_t weightsFalsePositive;\n", " Float_t weightsSignal;\n", "\n", "\n", "private:\n", " TChain* chain;\n", " TH1F* hSignal;\n", " TH1F* hFP;\n", " TH1F* hTP;\n", "\n", "};" ] }, { "cell_type": "markdown", "id": "ca51f8c9", "metadata": {}, "source": [ " ----------------------------------------------------------------------------------------------\n", "Training\n", "----------------------------------------------------------------------------------------------\n", "\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 3, "id": "ad49480e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:08:31.104456Z", "iopub.status.busy": "2026-05-19T20:08:31.104327Z", "iopub.status.idle": "2026-05-19T20:08:31.115334Z", "shell.execute_reply": "2026-05-19T20:08:31.114713Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_54:17:4: error: expected expression\n", " / global event weights per tree (see below for setting event-wise weights)\n", " ^\n", "input_line_54:17:6: error: use of undeclared identifier 'global'\n", " / global event weights per tree (see below for setting event-wise weights)\n", " ^\n", "input_line_54:23:5: error: unknown type name 'Create'\n", " Create a new root output file.\n", " ^\n", "input_line_54:23:13: error: expected ';' at end of declaration\n", " Create a new root output file.\n", " ^\n", " ;\n", "input_line_54:25:37: error: use of undeclared identifier 'outfileName'\n", " TFile* outputFile = TFile::Open( outfileName, \"RECREATE\" );\n", " ^\n", "input_line_54:29:5: error: use of undeclared identifier 'background'\n", " background 0\n", " ^\n", "input_line_54:39:47: error: use of undeclared identifier 'signalWeight'\n", " dataloader->AddSignalTree ( signal, signalWeight );\n", " ^\n", "input_line_54:42:9: error: use of undeclared identifier 'factory'\n", " factory->SetBackgroundWeightExpression(\"weight\");\n", " ^\n", "input_line_54:43:27: error: expected '(' after 'for'\n", " TCut mycuts = \"\"; for example: TCut mycuts = \"abs(var1)<0.5 && abs(var2-0.5)<1\";\n", " ^\n", "input_line_54:44:27: error: expected '(' after 'for'\n", " TCut mycutb = \"\"; for example: TCut mycutb = \"abs(var1)<0.5\";\n", " ^\n", "input_line_54:46:5: error: unknown type name 'tell'\n", " tell the factory to use all remaining events in the trees after training for testing:\n", " ^\n", "input_line_54:46:13: error: expected ';' at end of declaration\n", " tell the factory to use all remaining events in the trees after training for testing:\n", " ^\n", " ;\n", "input_line_54:50:5: error: unknown type name 'Boosted'\n", " Boosted Decision Trees\n", " ^\n", "input_line_54:50:21: error: expected ';' at end of declaration\n", " Boosted Decision Trees\n", " ^\n", " ;\n", "input_line_54:53:4: error: use of undeclared identifier 'factory'\n", " factory->TrainAllMethods();\n", " ^\n", "input_line_54:54:4: error: use of undeclared identifier 'factory'\n", " factory->TestAllMethods();\n", " ^\n", "input_line_54:55:4: error: use of undeclared identifier 'factory'\n", " factory->EvaluateAllMethods();\n", " ^\n", "input_line_54:59:11: error: use of undeclared identifier 'factory'\n", " delete factory;\n", " ^\n", "input_line_54:64:5: error: use of undeclared identifier 'background'\n", " background 1\n", " ^\n", "fatal error: too many errors emitted, stopping now [-ferror-limit=]\n" ] } ], "source": [ "%%cpp -d\n", "void Training(){\n", " std::string factoryOptions( \"!V:!Silent:Transformations=I;D;P;G,D:AnalysisType=Classification\" );\n", " TString fname = \"./tmva_example_multiple_background.root\";\n", "\n", " TFile *input(nullptr);\n", " input = TFile::Open( fname );\n", " if (!input) {\n", " std::cout << \"ERROR: could not open data file\" << std::endl;\n", " exit(1);\n", " }\n", "\n", " TTree *signal = (TTree*)input->Get(\"TreeS\");\n", " TTree *background0 = (TTree*)input->Get(\"TreeB0\");\n", " TTree *background1 = (TTree*)input->Get(\"TreeB1\");\n", " TTree *background2 = (TTree*)input->Get(\"TreeB2\");\n", "\n", " / global event weights per tree (see below for setting event-wise weights)\n", " Double_t signalWeight = 1.0;\n", " Double_t background0Weight = 1.0;\n", " Double_t background1Weight = 1.0;\n", " Double_t background2Weight = 1.0;\n", "\n", " Create a new root output file.\n", " TString outfileName( \"TMVASignalBackground0.root\" );\n", " TFile* outputFile = TFile::Open( outfileName, \"RECREATE\" );\n", "\n", "\n", "\n", " background 0\n", " ____________\n", " TMVA::Factory *factory = new TMVA::Factory( \"TMVAMultiBkg0\", outputFile, factoryOptions );\n", " TMVA::DataLoader *dataloader=new TMVA::DataLoader(\"datasetBkg0\");\n", "\n", " dataloader->AddVariable( \"var1\", \"Variable 1\", \"\", 'F' );\n", " dataloader->AddVariable( \"var2\", \"Variable 2\", \"\", 'F' );\n", " dataloader->AddVariable( \"var3\", \"Variable 3\", \"units\", 'F' );\n", " dataloader->AddVariable( \"var4\", \"Variable 4\", \"units\", 'F' );\n", "\n", " dataloader->AddSignalTree ( signal, signalWeight );\n", " dataloader->AddBackgroundTree( background0, background0Weight );\n", "\n", " factory->SetBackgroundWeightExpression(\"weight\");\n", " TCut mycuts = \"\"; for example: TCut mycuts = \"abs(var1)<0.5 && abs(var2-0.5)<1\";\n", " TCut mycutb = \"\"; for example: TCut mycutb = \"abs(var1)<0.5\";\n", "\n", " tell the factory to use all remaining events in the trees after training for testing:\n", " dataloader->PrepareTrainingAndTestTree( mycuts, mycutb,\n", " \"nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V\" );\n", "\n", " Boosted Decision Trees\n", " factory->BookMethod( dataloader, TMVA::Types::kBDT, \"BDTG\",\n", " \"!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.6:SeparationType=GiniIndex:nCuts=20:MaxDepth=2\" );\n", " factory->TrainAllMethods();\n", " factory->TestAllMethods();\n", " factory->EvaluateAllMethods();\n", "\n", " outputFile->Close();\n", "\n", " delete factory;\n", " delete dataloader;\n", "\n", "\n", "\n", " background 1\n", " ____________\n", "\n", " outfileName = \"TMVASignalBackground1.root\";\n", " outputFile = TFile::Open( outfileName, \"RECREATE\" );\n", " dataloader=new TMVA::DataLoader(\"datasetBkg1\");\n", "\n", " factory = new TMVA::Factory( \"TMVAMultiBkg1\", outputFile, factoryOptions );\n", " dataloader->AddVariable( \"var1\", \"Variable 1\", \"\", 'F' );\n", " dataloader->AddVariable( \"var2\", \"Variable 2\", \"\", 'F' );\n", " dataloader->AddVariable( \"var3\", \"Variable 3\", \"units\", 'F' );\n", " dataloader->AddVariable( \"var4\", \"Variable 4\", \"units\", 'F' );\n", "\n", " dataloader->AddSignalTree ( signal, signalWeight );\n", " dataloader->AddBackgroundTree( background1, background1Weight );\n", "\n", " dataloader->SetBackgroundWeightExpression(\"weight\");\n", "\n", " tell the factory to use all remaining events in the trees after training for testing:\n", " dataloader->PrepareTrainingAndTestTree( mycuts, mycutb,\n", " \"nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V\" );\n", "\n", " Boosted Decision Trees\n", " factory->BookMethod( dataloader, TMVA::Types::kBDT, \"BDTG\",\n", " \"!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.6:SeparationType=GiniIndex:nCuts=20:MaxDepth=2\" );\n", " factory->TrainAllMethods();\n", " factory->TestAllMethods();\n", " factory->EvaluateAllMethods();\n", "\n", " outputFile->Close();\n", "\n", " delete factory;\n", " delete dataloader;\n", "\n", "\n", " background 2\n", " ____________\n", "\n", " outfileName = \"TMVASignalBackground2.root\";\n", " outputFile = TFile::Open( outfileName, \"RECREATE\" );\n", "\n", " factory = new TMVA::Factory( \"TMVAMultiBkg2\", outputFile, factoryOptions );\n", " dataloader=new TMVA::DataLoader(\"datasetBkg2\");\n", "\n", " dataloader->AddVariable( \"var1\", \"Variable 1\", \"\", 'F' );\n", " dataloader->AddVariable( \"var2\", \"Variable 2\", \"\", 'F' );\n", " dataloader->AddVariable( \"var3\", \"Variable 3\", \"units\", 'F' );\n", " dataloader->AddVariable( \"var4\", \"Variable 4\", \"units\", 'F' );\n", "\n", " dataloader->AddSignalTree ( signal, signalWeight );\n", " dataloader->AddBackgroundTree( background2, background2Weight );\n", "\n", " dataloader->SetBackgroundWeightExpression(\"weight\");\n", "\n", " tell the dataloader to use all remaining events in the trees after training for testing:\n", " dataloader->PrepareTrainingAndTestTree( mycuts, mycutb,\n", " \"nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V\" );\n", "\n", " Boosted Decision Trees\n", " factory->BookMethod( dataloader, TMVA::Types::kBDT, \"BDTG\",\n", " \"!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20:MaxDepth=2\" );\n", " factory->TrainAllMethods();\n", " factory->TestAllMethods();\n", " factory->EvaluateAllMethods();\n", "\n", " outputFile->Close();\n", "\n", " delete factory;\n", " delete dataloader;\n", "\n", "}" ] }, { "cell_type": "markdown", "id": "b64fa544", "metadata": {}, "source": [ " ----------------------------------------------------------------------------------------------\n", "Application\n", "----------------------------------------------------------------------------------------------\n", "\n", "\n", "create a summary tree with all signal and background events and for each event the three classifier values and the true classID\n", " " ] }, { "cell_type": "code", "execution_count": 4, "id": "60b12a1a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:08:31.116775Z", "iopub.status.busy": "2026-05-19T20:08:31.116547Z", "iopub.status.idle": "2026-05-19T20:08:31.126163Z", "shell.execute_reply": "2026-05-19T20:08:31.125581Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_55:3:5: error: unknown type name 'Create'\n", " Create a new root output file.\n", " ^\n", "input_line_55:3:13: error: expected ';' at end of declaration\n", " Create a new root output file.\n", " ^\n", " ;\n", "input_line_55:5:37: error: use of undeclared identifier 'outfileName'\n", " TFile* outputFile = TFile::Open( outfileName, \"RECREATE\" );\n", " ^\n", "input_line_55:26:5: error: unknown type name 'create'\n", " create three readers for the three different signal/background classifications, .. one for each background\n", " ^\n", "input_line_55:26:17: error: expected ';' at end of declaration\n", " create three readers for the three different signal/background classifications, .. one for each background\n", " ^\n", " ;\n", "input_line_55:31:4: error: use of undeclared identifier 'reader0'\n", " reader0->AddVariable( \"var1\", &var1 );\n", " ^\n", "input_line_55:32:4: error: use of undeclared identifier 'reader0'\n", " reader0->AddVariable( \"var2\", &var2 );\n", " ^\n", "input_line_55:33:4: error: use of undeclared identifier 'reader0'\n", " reader0->AddVariable( \"var3\", &var3 );\n", " ^\n", "input_line_55:34:4: error: use of undeclared identifier 'reader0'\n", " reader0->AddVariable( \"var4\", &var4 );\n", " ^\n", "input_line_55:46:5: error: unknown type name 'load'\n", " load the weight files for the readers\n", " ^\n", "input_line_55:46:13: error: expected ';' at end of declaration\n", " load the weight files for the readers\n", " ^\n", " ;\n", "input_line_55:48:4: error: use of undeclared identifier 'reader0'\n", " reader0->BookMVA( \"BDT method\", \"datasetBkg0/weights/TMVAMultiBkg0_BDTG.weights.xml\" );\n", " ^\n", "input_line_55:52:5: error: unknown type name 'load'\n", " load the input file\n", " ^\n", "input_line_55:52:13: error: expected ';' at end of declaration\n", " load the input file\n", " ^\n", " ;\n", "input_line_55:55:4: error: use of undeclared identifier 'input'\n", " input = TFile::Open( fname );\n", " ^\n", "input_line_55:59:5: error: unknown type name 'loop'\n", " loop through signal and all background trees\n", " ^\n", "input_line_55:59:17: error: expected ';' at end of declaration\n", " loop through signal and all background trees\n", " ^\n", " ;\n", "input_line_55:120:4: error: use of undeclared identifier 'input'\n", " input->Close();\n", " ^\n", "input_line_55:123:10: error: expected ';' after expression\n", " write output tree\n", " ^\n", " ;\n", "fatal error: too many errors emitted, stopping now [-ferror-limit=]\n" ] } ], "source": [ "%%cpp -d\n", "void ApplicationCreateCombinedTree(){\n", "\n", " Create a new root output file.\n", " TString outfileName( \"tmva_example_multiple_backgrounds__applied.root\" );\n", " TFile* outputFile = TFile::Open( outfileName, \"RECREATE\" );\n", " TTree* outputTree = new TTree(\"multiBkg\",\"multiple backgrounds tree\");\n", "\n", " Float_t var1, var2;\n", " Float_t var3, var4;\n", " Int_t classID = 0;\n", " Float_t weight = 1.f;\n", "\n", " Float_t classifier0, classifier1, classifier2;\n", "\n", " outputTree->Branch(\"classID\", &classID, \"classID/I\");\n", " outputTree->Branch(\"var1\", &var1, \"var1/F\");\n", " outputTree->Branch(\"var2\", &var2, \"var2/F\");\n", " outputTree->Branch(\"var3\", &var3, \"var3/F\");\n", " outputTree->Branch(\"var4\", &var4, \"var4/F\");\n", " outputTree->Branch(\"weight\", &weight, \"weight/F\");\n", " outputTree->Branch(\"cls0\", &classifier0, \"cls0/F\");\n", " outputTree->Branch(\"cls1\", &classifier1, \"cls1/F\");\n", " outputTree->Branch(\"cls2\", &classifier2, \"cls2/F\");\n", "\n", "\n", " create three readers for the three different signal/background classifications, .. one for each background\n", " TMVA::Reader *reader0 = new TMVA::Reader( \"!Color:!Silent\" );\n", " TMVA::Reader *reader1 = new TMVA::Reader( \"!Color:!Silent\" );\n", " TMVA::Reader *reader2 = new TMVA::Reader( \"!Color:!Silent\" );\n", "\n", " reader0->AddVariable( \"var1\", &var1 );\n", " reader0->AddVariable( \"var2\", &var2 );\n", " reader0->AddVariable( \"var3\", &var3 );\n", " reader0->AddVariable( \"var4\", &var4 );\n", "\n", " reader1->AddVariable( \"var1\", &var1 );\n", " reader1->AddVariable( \"var2\", &var2 );\n", " reader1->AddVariable( \"var3\", &var3 );\n", " reader1->AddVariable( \"var4\", &var4 );\n", "\n", " reader2->AddVariable( \"var1\", &var1 );\n", " reader2->AddVariable( \"var2\", &var2 );\n", " reader2->AddVariable( \"var3\", &var3 );\n", " reader2->AddVariable( \"var4\", &var4 );\n", "\n", " load the weight files for the readers\n", " TString method = \"BDT method\";\n", " reader0->BookMVA( \"BDT method\", \"datasetBkg0/weights/TMVAMultiBkg0_BDTG.weights.xml\" );\n", " reader1->BookMVA( \"BDT method\", \"datasetBkg1/weights/TMVAMultiBkg1_BDTG.weights.xml\" );\n", " reader2->BookMVA( \"BDT method\", \"datasetBkg2/weights/TMVAMultiBkg2_BDTG.weights.xml\" );\n", "\n", " load the input file\n", " TFile *input(0);\n", " TString fname = \"./tmva_example_multiple_background.root\";\n", " input = TFile::Open( fname );\n", "\n", " TTree* theTree = NULL;\n", "\n", " loop through signal and all background trees\n", " for( int treeNumber = 0; treeNumber < 4; ++treeNumber ) {\n", " if( treeNumber == 0 ){\n", " theTree = (TTree*)input->Get(\"TreeS\");\n", " std::cout << \"--- Select signal sample\" << std::endl;\n", " theTree->SetBranchAddress( \"weight\", &weight );\n", " weight = 1;\n", " classID = 0;\n", " }else if( treeNumber == 1 ){\n", " theTree = (TTree*)input->Get(\"TreeB0\");\n", " std::cout << \"--- Select background 0 sample\" << std::endl;\n", " theTree->SetBranchAddress( \"weight\", &weight );\n", " weight = 1;\n", " classID = 1;\n", " }else if( treeNumber == 2 ){\n", " theTree = (TTree*)input->Get(\"TreeB1\");\n", " std::cout << \"--- Select background 1 sample\" << std::endl;\n", " theTree->SetBranchAddress( \"weight\", &weight );\n", " weight = 1;\n", " classID = 2;\n", " }else if( treeNumber == 3 ){\n", " theTree = (TTree*)input->Get(\"TreeB2\");\n", " std::cout << \"--- Select background 2 sample\" << std::endl;\n", " theTree->SetBranchAddress( \"weight\", &weight );\n", " weight = 1;\n", " classID = 3;\n", " }\n", "\n", "\n", " theTree->SetBranchAddress( \"var1\", &var1 );\n", " theTree->SetBranchAddress( \"var2\", &var2 );\n", " theTree->SetBranchAddress( \"var3\", &var3 );\n", " theTree->SetBranchAddress( \"var4\", &var4 );\n", "\n", "\n", " std::cout << \"--- Processing: \" << theTree->GetEntries() << \" events\" << std::endl;\n", " TStopwatch sw;\n", " sw.Start();\n", " Int_t nEvent = theTree->GetEntries();\n", " Int_t nEvent = 100;\n", " for (Long64_t ievt=0; ievtGetEntry(ievt);\n", "\n", " get the classifiers for each of the signal/background classifications\n", " classifier0 = reader0->EvaluateMVA( method );\n", " classifier1 = reader1->EvaluateMVA( method );\n", " classifier2 = reader2->EvaluateMVA( method );\n", "\n", " outputTree->Fill();\n", " }\n", "\n", "\n", " get elapsed time\n", " sw.Stop();\n", " std::cout << \"--- End of event loop: \"; sw.Print();\n", " }\n", " input->Close();\n", "\n", "\n", " write output tree\n", "/* outputTree->SetDirectory(outputFile);\n", " outputTree->Write(); */\n", " outputFile->Write();\n", "\n", " outputFile->Close();\n", "\n", " std::cout << \"--- Created root file: \\\"\" << outfileName.Data() << \"\\\" containing the MVA output histograms\" << std::endl;\n", "\n", " delete reader0;\n", " delete reader1;\n", " delete reader2;\n", "\n", " std::cout << \"==> Application of readers is done! combined tree created\" << std::endl << std::endl;\n", "\n", "}" ] }, { "cell_type": "markdown", "id": "8715fd61", "metadata": {}, "source": [ " ----------------------------------------------------------------------------------------------\n", "Call of Genetic algorithm\n", "----------------------------------------------------------------------------------------------\n", "\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 5, "id": "1fdc1364", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:08:31.127421Z", "iopub.status.busy": "2026-05-19T20:08:31.127285Z", "iopub.status.idle": "2026-05-19T20:08:31.138256Z", "shell.execute_reply": "2026-05-19T20:08:31.137627Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_56:3:10: error: unknown type name 'define'\n", " define all the parameters by their minimum and maximum value\n", " ^\n", "input_line_56:3:20: error: expected ';' at end of declaration\n", " define all the parameters by their minimum and maximum value\n", " ^\n", " ;\n", "input_line_56:6:9: error: use of undeclared identifier 'ranges'\n", " ranges.push_back( new Interval(-1,1) ); for some classifiers (especially LD) the ranges have to be taken larger\n", " ^\n", "input_line_56:6:31: error: unknown type name 'Interval'\n", " ranges.push_back( new Interval(-1,1) ); for some classifiers (especially LD) the ranges have to be taken larger\n", " ^\n", "input_line_56:6:54: error: expected '(' after 'for'\n", " ranges.push_back( new Interval(-1,1) ); for some classifiers (especially LD) the ranges have to be taken larger\n", " ^\n", "input_line_56:8:9: error: use of undeclared identifier 'ranges'\n", " ranges.push_back( new Interval(-1,1) );\n", " ^\n", "input_line_56:8:31: error: unknown type name 'Interval'\n", " ranges.push_back( new Interval(-1,1) );\n", " ^\n", "input_line_56:11:26: error: use of undeclared identifier 'Interval'\n", " for( std::vector::iterator it = ranges.begin(); it != ranges.end(); it++ ){\n", " ^\n", "input_line_56:11:35: error: expected expression\n", " for( std::vector::iterator it = ranges.begin(); it != ranges.end(); it++ ){\n", " ^\n", "input_line_56:11:52: error: use of undeclared identifier 'ranges'\n", " for( std::vector::iterator it = ranges.begin(); it != ranges.end(); it++ ){\n", " ^\n", "input_line_56:11:74: error: use of undeclared identifier 'ranges'\n", " for( std::vector::iterator it = ranges.begin(); it != ranges.end(); it++ ){\n", " ^\n", "input_line_56:18:9: error: unknown type name 'IFitterTarget'\n", " IFitterTarget* myFitness = new MyFitness( chain );\n", " ^\n", "input_line_56:18:40: error: unknown type name 'MyFitness'\n", " IFitterTarget* myFitness = new MyFitness( chain );\n", " ^\n", "input_line_56:20:10: error: unknown type name 'prepare'\n", " prepare the genetic algorithm with an initial population size of 20\n", " ^\n", "input_line_56:20:21: error: expected ';' at end of declaration\n", " prepare the genetic algorithm with an initial population size of 20\n", " ^\n", " ;\n", "input_line_56:29:9: error: unknown type name 'GeneticFitter'\n", " GeneticFitter mg( *myFitness, name, ranges, opts);\n", " ^\n", "input_line_56:29:39: error: use of undeclared identifier 'name'\n", " GeneticFitter mg( *myFitness, name, ranges, opts);\n", " ^\n", "input_line_56:29:45: error: use of undeclared identifier 'ranges'\n", " GeneticFitter mg( *myFitness, name, ranges, opts);\n", " ^\n", "input_line_56:35:17: error: unknown type name 'MyFitness'\n", " dynamic_cast(myFitness)->Print();\n", " ^\n" ] } ], "source": [ "%%cpp -d\n", "void MaximizeSignificance(){\n", "\n", " define all the parameters by their minimum and maximum value\n", " in this example 3 parameters (=cuts on the classifiers) are defined.\n", " vector ranges;\n", " ranges.push_back( new Interval(-1,1) ); for some classifiers (especially LD) the ranges have to be taken larger\n", " ranges.push_back( new Interval(-1,1) );\n", " ranges.push_back( new Interval(-1,1) );\n", "\n", " std::cout << \"Classifier ranges (defined by the user)\" << std::endl;\n", " for( std::vector::iterator it = ranges.begin(); it != ranges.end(); it++ ){\n", " std::cout << \" range: \" << (*it)->GetMin() << \" \" << (*it)->GetMax() << std::endl;\n", " }\n", "\n", " TChain* chain = new TChain(\"multiBkg\");\n", " chain->Add(\"tmva_example_multiple_backgrounds__applied.root\");\n", "\n", " IFitterTarget* myFitness = new MyFitness( chain );\n", "\n", " prepare the genetic algorithm with an initial population size of 20\n", " mind: big population sizes will help in searching the domain space of the solution\n", " but you have to weight this out to the number of generations\n", " the extreme case of 1 generation and populationsize n is equal to\n", " a Monte Carlo calculation with n tries\n", "\n", " const TString name( \"multipleBackgroundGA\" );\n", " const TString opts( \"PopSize=100:Steps=30\" );\n", "\n", " GeneticFitter mg( *myFitness, name, ranges, opts);\n", " mg.SetParameters( 4, 30, 200, 10,5, 0.95, 0.001 );\n", "\n", " std::vector result;\n", " Double_t estimator = mg.Run(result);\n", "\n", " dynamic_cast(myFitness)->Print();\n", " std::cout << std::endl;\n", "\n", " int n = 0;\n", " for( std::vector::iterator it = result.begin(); it EVENT:0\n", " var1 = -1.14361\n", " var2 = -0.822373\n", " var3 = -0.395426\n", " var4 = -0.529427\n", "created tree: TreeS\n", "... event: 0 (200)\n", "======> EVENT:0\n", " var1 = -1.54361\n", " var2 = -1.42237\n", " var3 = -1.39543\n", " var4 = -2.02943\n", "created tree: TreeB0\n", "... event: 0 (200)\n", "======> EVENT:0\n", " var1 = -1.54361\n", " var2 = -0.822373\n", " var3 = -0.395426\n", " var4 = -2.02943\n", "created tree: TreeB1\n", "======> EVENT:0\n", " var1 = 0.463304\n", " var2 = 1.37192\n", " var3 = -1.16769\n", " var4 = -1.77551\n", "created tree: TreeB2\n", "created data file: tmva_example_multiple_background.root\n", "\n", "========================\n", "--- Training\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "In file included from input_line_58:1:\n", "/github/home/ROOT-CI/build/tutorials/machine_learning/createData.C:264:17: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]\n", " Float_t xvar[nvar];\n", " ^~~~\n", "/github/home/ROOT-CI/build/tutorials/machine_learning/createData.C:264:17: note: function parameter 'nvar' with unknown value cannot be used in a constant expression\n", "/github/home/ROOT-CI/build/tutorials/machine_learning/createData.C:262:101: note: declared here\n", "TTree* makeTree_lin_Nvar( TString treeName, TString treeTitle, Float_t* x, Float_t* dx, const Int_t nvar, Int_t N )\n", " ^\n", "/github/home/ROOT-CI/build/tutorials/machine_learning/createData.C:323:17: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]\n", " Float_t xvar[nvar]; //variable array size does not work in interactive mode\n", " ^~~~\n", "/github/home/ROOT-CI/build/tutorials/machine_learning/createData.C:323:17: note: function parameter 'nvar' with unknown value cannot be used in a constant expression\n", "/github/home/ROOT-CI/build/tutorials/machine_learning/createData.C:320:65: note: declared here\n", "TTree* makeTree_circ(TString treeName, TString treeTitle, Int_t nvar = 2, Int_t N = 6000, Float_t radius = 1.0, Bool_t distort = false)\n", " ^\n", "input_line_70:2:3: error: use of undeclared identifier 'Training'\n", " (Training())\n", " ^\n", "Error in : Error evaluating expression (Training())\n", "Execution of your code was aborted.\n" ] } ], "source": [ "cout << \"Start Test TMVAGAexample\" << endl\n", " << \"========================\" << endl\n", " << endl;\n", "\n", "TString createDataMacro = gROOT->GetTutorialDir() + \"/machine_learning/createData.C\";\n", "gROOT->ProcessLine(TString::Format(\".L %s\",createDataMacro.Data()));\n", "gROOT->ProcessLine(\"create_MultipleBackground(200)\");\n", "\n", "\n", "cout << endl;\n", "cout << \"========================\" << endl;\n", "cout << \"--- Training\" << endl;\n", "Training();\n", "\n", "cout << endl;\n", "cout << \"========================\" << endl;\n", "cout << \"--- Application & create combined tree\" << endl;\n", "ApplicationCreateCombinedTree();\n", "\n", "cout << endl;\n", "cout << \"========================\" << endl;\n", "cout << \"--- maximize significance\" << endl;\n", "MaximizeSignificance();" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }