00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <cstdlib>
00011 #include <vector>
00012 #include <iostream>
00013 #include <map>
00014 #include <string>
00015
00016 #include "TFile.h"
00017 #include "TTree.h"
00018 #include "TString.h"
00019 #include "TSystem.h"
00020 #include "TROOT.h"
00021 #include "TStopwatch.h"
00022
00023 #if not defined(__CINT__) || defined(__MAKECINT__)
00024 #include "TMVA/Tools.h"
00025 #include "TMVA/Reader.h"
00026 #endif
00027
00028 using namespace TMVA;
00029
00030 void TMVARegressionApplication( TString myMethodList = "" )
00031 {
00032
00033
00034 TMVA::Tools::Instance();
00035
00036
00037 std::map<std::string,int> Use;
00038
00039
00040 Use["PDERS"] = 0;
00041 Use["PDEFoam"] = 1;
00042 Use["KNN"] = 1;
00043
00044
00045 Use["LD"] = 1;
00046
00047
00048 Use["FDA_GA"] = 1;
00049 Use["FDA_MC"] = 0;
00050 Use["FDA_MT"] = 0;
00051 Use["FDA_GAMT"] = 0;
00052
00053
00054 Use["MLP"] = 1;
00055
00056
00057 Use["SVM"] = 0;
00058
00059
00060 Use["BDT"] = 0;
00061 Use["BDTG"] = 1;
00062
00063
00064 std::cout << std::endl;
00065 std::cout << "==> Start TMVARegressionApplication" << std::endl;
00066
00067
00068 if (myMethodList != "") {
00069 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
00070
00071 std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
00072 for (UInt_t i=0; i<mlist.size(); i++) {
00073 std::string regMethod(mlist[i]);
00074
00075 if (Use.find(regMethod) == Use.end()) {
00076 std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
00077 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
00078 std::cout << std::endl;
00079 return;
00080 }
00081 Use[regMethod] = 1;
00082 }
00083 }
00084
00085
00086
00087
00088
00089 TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );
00090
00091
00092
00093 Float_t var1, var2;
00094 reader->AddVariable( "var1", &var1 );
00095 reader->AddVariable( "var2", &var2 );
00096
00097
00098 Float_t spec1,spec2;
00099 reader->AddSpectator( "spec1:=var1*2", &spec1 );
00100 reader->AddSpectator( "spec2:=var1*3", &spec2 );
00101
00102
00103
00104 TString dir = "weights/";
00105 TString prefix = "TMVARegression";
00106
00107
00108 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
00109 if (it->second) {
00110 TString methodName = it->first + " method";
00111 TString weightfile = dir + prefix + "_" + TString(it->first) + ".weights.xml";
00112 reader->BookMVA( methodName, weightfile );
00113 }
00114 }
00115
00116
00117 TH1* hists[100];
00118 Int_t nhists = -1;
00119 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
00120 TH1* h = new TH1F( it->first.c_str(), TString(it->first) + " method", 100, -100, 600 );
00121 if (it->second) hists[++nhists] = h;
00122 }
00123 nhists++;
00124
00125
00126
00127
00128
00129 TFile *input(0);
00130 TString fname = "./tmva_reg_example.root";
00131 if (!gSystem->AccessPathName( fname )) {
00132 input = TFile::Open( fname );
00133 }
00134 else {
00135 input = TFile::Open( "http://root.cern.ch/files/tmva_reg_example.root" );
00136 }
00137
00138 if (!input) {
00139 std::cout << "ERROR: could not open data file" << std::endl;
00140 exit(1);
00141 }
00142 std::cout << "--- TMVARegressionApp : Using input file: " << input->GetName() << std::endl;
00143
00144
00145
00146
00147
00148
00149
00150
00151 TTree* theTree = (TTree*)input->Get("TreeR");
00152 std::cout << "--- Select signal sample" << std::endl;
00153 theTree->SetBranchAddress( "var1", &var1 );
00154 theTree->SetBranchAddress( "var2", &var2 );
00155
00156 std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
00157 TStopwatch sw;
00158 sw.Start();
00159 for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
00160
00161 if (ievt%1000 == 0) {
00162 std::cout << "--- ... Processing event: " << ievt << std::endl;
00163 }
00164
00165 theTree->GetEntry(ievt);
00166
00167
00168
00169
00170 for (Int_t ih=0; ih<nhists; ih++) {
00171 TString title = hists[ih]->GetTitle();
00172 Float_t val = (reader->EvaluateRegression( title ))[0];
00173 hists[ih]->Fill( val );
00174 }
00175 }
00176 sw.Stop();
00177 std::cout << "--- End of event loop: "; sw.Print();
00178
00179
00180
00181 TFile *target = new TFile( "TMVARegApp.root","RECREATE" );
00182 for (Int_t ih=0; ih<nhists; ih++) hists[ih]->Write();
00183 target->Close();
00184
00185 std::cout << "--- Created root file: \"" << target->GetName()
00186 << "\" containing the MVA output histograms" << std::endl;
00187
00188 delete reader;
00189
00190 std::cout << "==> TMVARegressionApplication is done!" << std::endl << std::endl;
00191 }