%%cpp -d #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" using namespace TMVA; TString myMethodList = ""; TMVA::Tools::Instance(); std::map Use; Use["PDERS"] = 0; Use["PDEFoam"] = 1; Use["KNN"] = 1; Use["LD"] = 1; Use["FDA_GA"] = 0; Use["FDA_MC"] = 0; Use["FDA_MT"] = 0; Use["FDA_GAMT"] = 0; Use["MLP"] = 0; #ifdef R__HAS_TMVAGPU Use["DNN_GPU"] = 1; Use["DNN_CPU"] = 0; #else Use["DNN_GPU"] = 0; #ifdef R__HAS_TMVACPU Use["DNN_CPU"] = 1; #else Use["DNN_CPU"] = 0; #endif #endif Use["SVM"] = 0; Use["BDT"] = 0; Use["BDTG"] = 1; std::cout << std::endl; std::cout << "==> Start TMVARegressionApplication" << std::endl; if (myMethodList != "") { for (std::map::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0; std::vector mlist = gTools().SplitString( myMethodList, ',' ); for (UInt_t i=0; i::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " "; std::cout << std::endl; return; } Use[regMethod] = 1; } } TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" ); Float_t var1, var2; reader->AddVariable( "var1", &var1 ); reader->AddVariable( "var2", &var2 ); Float_t spec1,spec2; reader->AddSpectator( "spec1:=var1*2", &spec1 ); reader->AddSpectator( "spec2:=var1*3", &spec2 ); TString dir = "datasetreg/weights/"; TString prefix = "TMVARegression"; for (std::map::iterator it = Use.begin(); it != Use.end(); it++) { if (it->second) { TString methodName = it->first + " method"; TString weightfile = dir + prefix + "_" + TString(it->first) + ".weights.xml"; reader->BookMVA( methodName, weightfile ); } } TH1* hists[100]; Int_t nhists = -1; for (std::map::iterator it = Use.begin(); it != Use.end(); it++) { TH1* h = new TH1F( it->first.c_str(), TString(it->first) + " method", 100, -100, 600 ); if (it->second) hists[++nhists] = h; } nhists++; TFile *input(0); TString fname = "./tmva_reg_example.root"; if (!gSystem->AccessPathName( fname )) { input = TFile::Open( fname ); // check if file in local directory exists } else { TFile::SetCacheFileDir("."); input = TFile::Open("http://root.cern/files/tmva_reg_example.root", "CACHEREAD"); // if not: download from ROOT server } if (!input) { std::cout << "ERROR: could not open data file" << std::endl; exit(1); } std::cout << "--- TMVARegressionApp : Using input file: " << input->GetName() << std::endl; TTree* theTree = (TTree*)input->Get("TreeR"); std::cout << "--- Select signal sample" << std::endl; theTree->SetBranchAddress( "var1", &var1 ); theTree->SetBranchAddress( "var2", &var2 ); 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); // Retrieve the MVA target values (regression outputs) and fill into histograms // NOTE: EvaluateRegression(..) returns a vector for multi-target regression for (Int_t ih=0; ihGetTitle(); Float_t val = (reader->EvaluateRegression( title ))[0]; hists[ih]->Fill( val ); } } sw.Stop(); std::cout << "--- End of event loop: "; sw.Print(); TFile *target = new TFile( "TMVARegApp.root","RECREATE" ); for (Int_t ih=0; ihWrite(); target->Close(); std::cout << "--- Created root file: \"" << target->GetName() << "\" containing the MVA output histograms" << std::endl; delete reader; std::cout << "==> TMVARegressionApplication is done!" << std::endl << std::endl;