TMVARegressionApplication.C

Go to the documentation of this file.
00001 /**********************************************************************************
00002  * Project   : TMVA - a Root-integrated toolkit for multivariate data analysis    *
00003  * Package   : TMVA                                                               *
00004  * Exectuable: TMVARegressionApplication                                          *
00005  *                                                                                *
00006  * This macro provides a simple example on how to use the trained regression MVAs *
00007  * within an analysis module                                                      *
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    // This loads the library
00034    TMVA::Tools::Instance();
00035 
00036    // Default MVA methods to be trained + tested
00037    std::map<std::string,int> Use;
00038 
00039    // --- Mutidimensional likelihood and Nearest-Neighbour methods
00040    Use["PDERS"]           = 0;
00041    Use["PDEFoam"]         = 1; 
00042    Use["KNN"]             = 1;
00043    // 
00044    // --- Linear Discriminant Analysis
00045    Use["LD"]                    = 1;
00046    // 
00047    // --- Function Discriminant analysis
00048    Use["FDA_GA"]          = 1;
00049    Use["FDA_MC"]          = 0;
00050    Use["FDA_MT"]          = 0;
00051    Use["FDA_GAMT"]        = 0;
00052    // 
00053    // --- Neural Network
00054    Use["MLP"]             = 1; 
00055    // 
00056    // --- Support Vector Machine 
00057    Use["SVM"]             = 0;
00058    // 
00059    // --- Boosted Decision Trees
00060    Use["BDT"]             = 0;
00061    Use["BDTG"]            = 1;
00062    // ---------------------------------------------------------------
00063 
00064    std::cout << std::endl;
00065    std::cout << "==> Start TMVARegressionApplication" << std::endl;
00066 
00067    // Select methods (don't look at this code - not of interest)
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    // --- Create the Reader object
00088 
00089    TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    
00090 
00091    // Create a set of variables and declare them to the reader
00092    // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
00093    Float_t var1, var2;
00094    reader->AddVariable( "var1", &var1 );
00095    reader->AddVariable( "var2", &var2 );
00096 
00097    // Spectator variables declared in the training have to be added to the reader, too
00098    Float_t spec1,spec2;
00099    reader->AddSpectator( "spec1:=var1*2",  &spec1 );
00100    reader->AddSpectator( "spec2:=var1*3",  &spec2 );
00101 
00102    // --- Book the MVA methods
00103 
00104    TString dir    = "weights/";
00105    TString prefix = "TMVARegression";
00106 
00107    // Book method(s)
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    // Book output histograms
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    // Prepare input tree (this must be replaced by your data source)
00126    // in this example, there is a toy tree with signal and one with background events
00127    // we'll later on use only the "signal" events for the test in this example.
00128    //   
00129    TFile *input(0);
00130    TString fname = "./tmva_reg_example.root";
00131    if (!gSystem->AccessPathName( fname )) {
00132       input = TFile::Open( fname ); // check if file in local directory exists
00133    } 
00134    else { 
00135       input = TFile::Open( "http://root.cern.ch/files/tmva_reg_example.root" ); // if not: download from ROOT server
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    // --- Event loop
00145 
00146    // Prepare the tree
00147    // - here the variable names have to corresponds to your tree
00148    // - you can use the same variables as above which is slightly faster,
00149    //   but of course you can use different ones and copy the values inside the event loop
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       // Retrieve the MVA target values (regression outputs) and fill into histograms
00168       // NOTE: EvaluateRegression(..) returns a vector for multi-target regression
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    // --- Write histograms
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 }

Generated on Tue Jul 5 15:26:37 2011 for ROOT_528-00b_version by  doxygen 1.5.1