void exampleTMVA_histogram() { // fill some histogram from signal and background auto hs1 = new TH1D("hs1","signal training histo",1000,-3,3); auto hs2 = new TH1D("hs2","signal test histo",1000,-3,3); auto hb1 = new TH1D("hb1","background training histo",1000,-3,3); auto hb2 = new TH1D("hb2","background test histo",1000,-3,3); auto f1 = new TF1("f1","gaus"); f1->SetParameters(1,1,1); // signal hs1->FillRandom("f1",10000); hs2->FillRandom("f1",10000); f1->SetParameters(1,-1,1); // signal hb1->FillRandom("f1",10000); hb2->FillRandom("f1",10000); //now make a classifier TMVA::Tools::Instance(); auto outputFile = TFile::Open("TMVA_BDT_Results_FromHistograms.root", "RECREATE"); // Create a factory and a DataLoader for handling data TMVA::Factory factory("TMVAClassification", outputFile, "!V:!Silent:Color:DrawProgressBar:AnalysisType=Classification"); TMVA::DataLoader loader("dataset"); loader.AddVariable("x",'F'); // add events (assume all histograms have same number of bins, otherwise do in separate loops int nevts = hs1->GetNbinsX(); for (int i = 1; i <= nevts; i++) { if (hs1->GetBinContent(i) > 0) loader.AddSignalTrainingEvent( {hs1->GetBinCenter(i) }, hs1->GetBinContent(i)); //if (hs2->GetBinContent(i) > 0) //loader.AddSignalTestEvent( {hs2->GetBinCenter(i) }, hs2->GetBinContent(i)); // background if (hb1->GetBinContent(i) > 0) loader.AddBackgroundTrainingEvent( {hb1->GetBinCenter(i) }, hb1->GetBinContent(i)); //if (hb2->GetBinContent(i) > 0) //loader.AddBackgroundTestEvent( {hb2->GetBinCenter(i) }, hb2->GetBinContent(i)); } TCut cut; loader.PrepareTrainingAndTestTree(cut,""); // Setup BDT configuration factory.BookMethod(&loader, TMVA::Types::kBDT, "BDT", "!H:!V:NTrees=100:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20"); // Train the model factory.TrainAllMethods(); factory.TestAllMethods(); factory.EvaluateAllMethods(); // Clean up outputFile->Close(); std::cout << "BDT training completed using histograms!" << std::endl; return; }