33 void TMVARegressionApplication( TString myMethodList =
"" )
37 TMVA::Tools::Instance();
40 std::map<std::string,int> Use;
73 std::cout << std::endl;
74 std::cout <<
"==> Start TMVARegressionApplication" << std::endl;
77 if (myMethodList !=
"") {
78 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
80 std::vector<TString> mlist = gTools().SplitString( myMethodList,
',' );
81 for (UInt_t i=0; i<mlist.size(); i++) {
82 std::string regMethod(mlist[i]);
84 if (Use.find(regMethod) == Use.end()) {
85 std::cout <<
"Method \"" << regMethod <<
"\" not known in TMVA under this name. Choose among the following:" << std::endl;
86 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first <<
" ";
87 std::cout << std::endl;
98 TMVA::Reader *reader =
new TMVA::Reader(
"!Color:!Silent" );
103 reader->AddVariable(
"var1", &var1 );
104 reader->AddVariable(
"var2", &var2 );
108 reader->AddSpectator(
"spec1:=var1*2", &spec1 );
109 reader->AddSpectator(
"spec2:=var1*3", &spec2 );
113 TString dir =
"dataset/weights/";
114 TString prefix =
"TMVARegression";
117 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
119 TString methodName = it->first +
" method";
120 TString weightfile = dir + prefix +
"_" + TString(it->first) +
".weights.xml";
121 reader->BookMVA( methodName, weightfile );
128 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
129 TH1* h =
new TH1F( it->first.c_str(), TString(it->first) +
" method", 100, -100, 600 );
130 if (it->second) hists[++nhists] = h;
139 TString fname =
"./tmva_reg_example.root";
140 if (!gSystem->AccessPathName( fname )) {
141 input = TFile::Open( fname );
144 TFile::SetCacheFileDir(
".");
145 input = TFile::Open(
"http://root.cern.ch/files/tmva_reg_example.root",
"CACHEREAD");
148 std::cout <<
"ERROR: could not open data file" << std::endl;
151 std::cout <<
"--- TMVARegressionApp : Using input file: " << input->GetName() << std::endl;
160 TTree* theTree = (TTree*)input->Get(
"TreeR");
161 std::cout <<
"--- Select signal sample" << std::endl;
162 theTree->SetBranchAddress(
"var1", &var1 );
163 theTree->SetBranchAddress(
"var2", &var2 );
165 std::cout <<
"--- Processing: " << theTree->GetEntries() <<
" events" << std::endl;
168 for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
170 if (ievt%1000 == 0) {
171 std::cout <<
"--- ... Processing event: " << ievt << std::endl;
174 theTree->GetEntry(ievt);
179 for (Int_t ih=0; ih<nhists; ih++) {
180 TString title = hists[ih]->GetTitle();
181 Float_t val = (reader->EvaluateRegression( title ))[0];
182 hists[ih]->Fill( val );
186 std::cout <<
"--- End of event loop: "; sw.Print();
190 TFile *target =
new TFile(
"TMVARegApp.root",
"RECREATE" );
191 for (Int_t ih=0; ih<nhists; ih++) hists[ih]->Write();
194 std::cout <<
"--- Created root file: \"" << target->GetName()
195 <<
"\" containing the MVA output histograms" << std::endl;
199 std::cout <<
"==> TMVARegressionApplication is done!" << std::endl << std::endl;
202 int main(
int argc,
char** argv )
206 for (
int i=1; i<argc; i++) {
207 TString regMethod(argv[i]);
208 if(regMethod==
"-b" || regMethod==
"--batch")
continue;
209 if (!methodList.IsNull()) methodList += TString(
",");
210 methodList += regMethod;
212 TMVARegressionApplication(methodList);