// //First attempt at structuring a classifier based on the BDT and ANN trainings in round2 // //This version is simply a copy-paste-adjust of the TMVA tutorial, TMVAClassificationApplication.C // #include #include #include #include #include #include "TFile.h" #include "TTree.h" #include "TString.h" #include "TSystem.h" #include "TROOT.h" #include "TStopwatch.h" #include "TMVA/Tools.h" #include "TMVA/Reader.h" #include "TMVA/MethodCuts.h" using namespace TMVA; void Classification1_1( TString myMethodList = "" ) { //--------------------------------------------------------------- // This loads the library TMVA::Tools::Instance(); std::cout << std::endl; std::cout << "==> Start TMVAClassificationApplication" << std::endl; // -------------------------------------------------------------------------------------------------- // Create the Reader object TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" ); // Create a set of variables and declare them to the reader // - the variable names MUST corresponds in name and type to those given in the weight file(s) used Float_t Pp_px, Pp_py, Pp_pz, Pp_e, Pm_px, Pm_py, Pm_pz, Pm_e; Float_t gamma_px, gamma_py, gamma_pz, gamma_e; reader -> AddVariable("if4CPp_px", &Pp_px); reader -> AddVariable("if4CPp_py", &Pp_py); reader -> AddVariable("if4CPp_pz", &Pp_pz); reader -> AddVariable("if4CPp_e", &Pp_e); reader -> AddVariable("if4CPm_px", &Pm_px); reader -> AddVariable("if4CPm_py", &Pm_py); reader -> AddVariable("if4CPm_pz", &Pm_pz); reader -> AddVariable("if4CPm_e", &Pm_e); reader -> AddVariable("if4Cgamma_px", &gamma_px); reader -> AddVariable("if4Cgamma_py", &gamma_py); reader -> AddVariable("if4Cgamma_pz", &gamma_pz); reader -> AddVariable("if4Cgamma_e", &gamma_e); // Book the MVA methods reader->BookMVA( "BDT2", "/home/besuser1/Tommaso/MC/round2/training1_BDT/dataset/weights/TMVAfactory_BDT2.weights.xml" ); int nbin = 100; TH1F *histBDT(0); histBDT = new TH1F( "BDT2","BDT", nbin, -0.8, 0.8 ); // Prepare input tree // TFile *input(0); TString fname = "/home/besuser1/Tommaso/data2/gammapp/inclmc2012gpp.root"; if (!gSystem->AccessPathName( fname )) { input = TFile::Open( fname ); // check if file in local directory exists } if (!input) { std::cout << "ERROR: could not open data file" << std::endl; exit(1); } std::cout << "--- TMVAClassificationApp : Using input file: " << input->GetName() << std::endl; // Event loop // Prepare the event tree // - Here the variable names have to corresponds to your tree // - You can use the same variables as above which is slightly faster, // but of course you can use different ones and copy the values inside the event loop // std::cout << "--- Select signal sample" << std::endl; Double_t dPp_px=Pp_px, dPp_py=Pp_py, dPp_pz=Pp_pz, dPp_e=Pp_e, dPm_px=Pm_px, dPm_py=Pm_py, dPm_pz=Pm_pz, dPm_e=Pm_e; Double_t dgamma_px=gamma_px, dgamma_py=gamma_py, dgamma_pz=gamma_pz, dgamma_e=gamma_e; TTree* theTree = (TTree*)input->Get("ntp1"); theTree->SetBranchAddress( "if4CPp_px", &dPp_px ); theTree->SetBranchAddress( "if4CPp_py", &dPp_py ); theTree->SetBranchAddress( "if4CPp_pz", &dPp_pz ); theTree->SetBranchAddress( "if4CPp_e", &dPp_e ); theTree->SetBranchAddress( "if4CPm_px", &dPm_px ); theTree->SetBranchAddress( "if4CPm_py", &dPm_py ); theTree->SetBranchAddress( "if4CPm_pz", &dPm_pz ); theTree->SetBranchAddress( "if4CPm_e", &dPm_e ); theTree->SetBranchAddress( "if4Cgamma_px", &dgamma_px ); theTree->SetBranchAddress( "if4Cgamma_py", &dgamma_py ); theTree->SetBranchAddress( "if4Cgamma_pz", &dgamma_pz ); theTree->SetBranchAddress( "if4Cgamma_e", &dgamma_e ); std::vector vecVar(4); // vector for EvaluateMVA tests std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl; TStopwatch sw; sw.Start(); for (Long64_t ievt=0; ievtGetEntries();ievt++) { if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl; theTree->GetEntry(ievt); // Return the MVA outputs and fill into histograms histBDT ->Fill( reader->EvaluateMVA( "BDT2") ); } // Get elapsed time sw.Stop(); std::cout << "--- End of event loop: "; sw.Print(); // Write histograms TString outFileName( "Classification1_1.root" ); TFile* outFile = TFile::Open( outFileName, "RECREATE" ); histBDT -> Write(); outFile -> Close(); std::cout << "--- Created root file:" << outFile->GetName() << ", containing the MVA output histograms" << std::endl; delete reader; std::cout << "==> TMVAClassificationApplication is done!" << std::endl << std::endl; }