#include #include #include #include #include "TChain.h" #include "TFile.h" #include "TTree.h" #include "TString.h" #include "TObjString.h" #include "TSystem.h" #include "TROOT.h" #include "TPluginManager.h" #if not defined(__CINT__) || defined(__MAKECINT__) // needs to be included when makecint runs (ACLIC) #include "TMVA/Factory.h" #include "TMVA/Tools.h" #endif void TMVA_L3BDT(TString path = "./") { // this loads the library TMVA::Tools::Instance(); // Create a new root output file. TString baseName = "L3"; TString outfileName = baseName+"_BDT.root"; TFile* outputFile = TFile::Open( outfileName, "RECREATE" ); TMVA::Factory *factory = new TMVA::Factory( baseName, outputFile,"!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P" ); // Define input variables const int maxVar = 8; TString name[maxVar] = {"Nstart_counter","Nfcal_showers","EfcalShowers","Ntof_point","Nbcal_showers","EbcalShowers","Ntrack_candidates","Ptot_tracks_cand"}; //"Ntrack_wires","Ptot_tracks_wire"}; // TString title[maxVar] = {"# SC hits","# FCAL showers","FCAL shower energy sum","# TOF hits","# BCAL showers","BCAL shower energy sum","# track candidates after cuts","track momentum scalar sum after cuts"}; TString units[maxVar] = {"","","GeV","","","GeV","","GeV/c"}; bool integer[maxVar] = {1,1,0,1,1,0,1}; for(int j = 0; j < maxVar; j++){ TString var = name[j]; TString varTitle =title[j]; cout<AddVariable(var,varTitle,units[j],'I' ); else factory->AddVariable(var,varTitle,units[j],'F' ); } // load the signal and background event samples from ROOT trees TChain *signal = new TChain("signal"); TChain *background = new TChain("background"); signal->AddFile(path+"hltTree.root",-1,"TrainingL3_Tree"); background->AddFile(path+"hltTree.root",-1,"TrainingL3_Tree"); //signal->AddFriend("dL3Trigger_FriendTree",path+"hltFriendTree.root"); //background->AddFriend("dL3Trigger_FriendTree",path+"hltFriendTree.root"); // global event weights per tree (see below for setting event-wise weights) Double_t signalWeight = 1.0; Double_t backgroundWeight = 1.0; // ====== register trees ==================================================== // // the following method is the prefered one: // you can add an arbitrary number of signal or background trees factory->AddSignalTree ( signal, signalWeight ); factory->AddBackgroundTree( background, backgroundWeight ); // Apply additional cuts on the signal and background samples (can be different) TCut mycuts = "L1_fired && Ephoton_truth > 8.0 && Ptot_tracks_cand<1e2"; TCut mycutb = "L1_fired && Ephoton_truth < 5.0 && Ptot_tracks_cand<1e2"; // tell the factory to use all remaining events in the trees after training for testing: factory->PrepareTrainingAndTestTree( mycuts, mycutb,"nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" ); factory->BookMethod( TMVA::Types::kBDT, "BDT","!H:!V:NTrees=400:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=2000:MaxDepth=4" ); // -------------------------------------------------------------------------------------------------- // ---- Now you can tell the factory to train, test, and evaluate the MVAs // Train MVAs using the set of training events factory->TrainAllMethods(); // ---- Evaluate all MVAs using the set of test events factory->TestAllMethods(); // ----- Evaluate and compare performance of all configured MVAs factory->EvaluateAllMethods(); // -------------------------------------------------------------- // Save the output outputFile->Close(); std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl; std::cout << "==> TMVAClassification is done!" << std::endl; //delete factory; }