ClassApplication.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: ClassApplication                                                   *
00005  *                                                                                *
00006  * Test suit for comparison of Reader and standalone class outputs                *
00007  **********************************************************************************/
00008 
00009 #include <vector>
00010 
00011 void ClassApplication( TString myMethodList = "Fisher" ) 
00012 {
00013    cout << endl;
00014    cout << "==> start ClassApplication" << endl;
00015    const int Nmvas = 16;
00016 
00017    const char* bulkname[Nmvas] = { "MLP","MLPBFGS","Fisher","FisherG","Likelihood","LikelihoodD","LikelihoodPCA","LD","HMatrix","FDA_MT","FDA_MC","FDA_GA","BDT","BDTD","BDTG","BDTB"};
00018 
00019    bool iuse[Nmvas] = { Nmvas*kFALSE };
00020 
00021    // interpret input list
00022    if (myMethodList != "") {
00023       TList* mlist = TMVA::gTools().ParseFormatLine( myMethodList, " :," );
00024       for (int imva=0; imva<Nmvas; imva++) if (mlist->FindObject( bulkname[imva] )) iuse[imva] = kTRUE;
00025       delete mlist;
00026    }
00027 
00028    // create a set of variables and declare them to the reader
00029    // - the variable names must corresponds in name and type to 
00030    // those given in the weight file(s) that you use
00031    std::vector<std::string> inputVars;
00032    inputVars.push_back( "var1+var2" );
00033    inputVars.push_back( "var1-var2" );
00034    inputVars.push_back( "var3" );
00035    inputVars.push_back( "var4" );
00036       
00037    // preload standalone class(es)
00038    string dir    = "weights/";
00039    string prefix = "TMVAClassification";
00040 
00041    for (int imva=0; imva<Nmvas; imva++) {
00042       if (iuse[imva]) {
00043          TString cfile = dir + prefix + "_" + bulkname[imva] + ".class.C++";
00044 
00045          cout << "=== Macro        : Loading class  file: " << cfile << endl;         
00046 
00047          // load the classifier's standalone class
00048          gROOT->LoadMacro( cfile );
00049       }
00050    }
00051    cout << "=== Macro        : Classifier class loading successfully terminated" << endl;
00052 
00053    // define classes
00054    IClassifierReader* classReader[Nmvas] = { Nmvas*0 };
00055 
00056    // ... and create them (and some histograms for the output)
00057    int nbin = 100;
00058    TH1* hist[Nmvas];
00059 
00060    for (int imva=0; imva<Nmvas; imva++) {
00061       if (iuse[imva]) {
00062          cout << "=== Macro        : Testing " << bulkname[imva] << endl;
00063          if (bulkname[imva] == "Likelihood"   ) {
00064             classReader[imva] = new ReadLikelihood   ( inputVars );            
00065             hist[imva] = new TH1F( "MVA_Likelihood",    "MVA_Likelihood",    nbin,  0, 1 );
00066          }
00067          if (bulkname[imva] == "LikelihoodD"  ) {
00068             classReader[imva] = new ReadLikelihoodD  ( inputVars );
00069             hist[imva] = new TH1F( "MVA_LikelihoodD",   "MVA_LikelihoodD",   nbin,  0, 1 );
00070          }
00071          if (bulkname[imva] == "LikelihoodPCA") {
00072             classReader[imva] = new ReadLikelihoodPCA( inputVars );
00073             hist[imva] = new TH1F( "MVA_LikelihoodPCA", "MVA_LikelihoodPCA", nbin,  0, 1 );
00074          }
00075          if (bulkname[imva] == "LikelihoodMIX") {
00076             classReader[imva] = new ReadLikelihoodMIX( inputVars );
00077             hist[imva] = new TH1F( "MVA_LikelihoodMIX", "MVA_LikelihoodMIX", nbin,  0, 1 );
00078          }
00079          if (bulkname[imva] == "HMatrix"      ) {
00080             classReader[imva] = new ReadHMatrix      ( inputVars );
00081             hist[imva] = new TH1F( "MVA_HMatrix",       "MVA_HMatrix",       nbin, -0.95, 1.55 );
00082          }
00083          if (bulkname[imva] == "Fisher"       ) {
00084             classReader[imva] = new ReadFisher       ( inputVars );
00085             hist[imva] = new TH1F( "MVA_Fisher",        "MVA_Fisher",        nbin, -4, 4 );
00086          }
00087          if (bulkname[imva] == "FisherG"       ) {
00088             classReader[imva] = new ReadFisherG       ( inputVars );
00089             hist[imva] = new TH1F( "MVA_FisherG",        "MVA_FisherG",        nbin, -4, 4 );
00090          }
00091          if (bulkname[imva] == "LD"   ) {
00092             classReader[imva] = new ReadLD   ( inputVars );            
00093             hist[imva] = new TH1F( "MVA_LD",    "MVA_LD",    nbin,  -1., 1 );
00094          }       
00095          if (bulkname[imva] == "FDA_MT"       ) {
00096             classReader[imva] = new ReadFDA_MT       ( inputVars );
00097             hist[imva] = new TH1F( "MVA_FDA_MT",        "MVA_FDA_MT",        nbin, -2.0, 3.0 );
00098          }
00099          if (bulkname[imva] == "FDA_MC"       ) {
00100             classReader[imva] = new ReadFDA_MC       ( inputVars );
00101             hist[imva] = new TH1F( "MVA_FDA_MC",        "MVA_FDA_MC",        nbin, -2.0, 3.0 );
00102          }
00103          if (bulkname[imva] == "FDA_GA"       ) {
00104             classReader[imva] = new ReadFDA_GA       ( inputVars );
00105             hist[imva] = new TH1F( "MVA_FDA_GA",        "MVA_FDA_GA",        nbin, -2.0, 3.0 );
00106          }
00107          if (bulkname[imva] == "MLP"          ) {
00108             classReader[imva] = new ReadMLP          ( inputVars );
00109             hist[imva] = new TH1F( "MVA_MLP",           "MVA_MLP",           nbin, -1.2, 1.2 );
00110          }
00111          if (bulkname[imva] == "MLPBFGS"          ) {
00112             classReader[imva] = new ReadMLPBFGS          ( inputVars );
00113             hist[imva] = new TH1F( "MVA_MLPBFGS",           "MVA_MLPBFGS",           nbin, -1.5, 1.5 );
00114          }
00115          if (bulkname[imva] == "BDT"          ) {
00116             classReader[imva] = new ReadBDT          ( inputVars );
00117             hist[imva] = new TH1F( "MVA_BDT",           "MVA_BDT",           nbin, -1, 1 );
00118          }
00119          if (bulkname[imva] == "BDTD"          ) {
00120             classReader[imva] = new ReadBDTD          ( inputVars );
00121             hist[imva] = new TH1F( "MVA_BDTD",           "MVA_BDTD",         nbin, -1, 1 );
00122          }
00123          if (bulkname[imva] == "BDTG"          ) {
00124             classReader[imva] = new ReadBDTG          ( inputVars );
00125             hist[imva] = new TH1F( "MVA_BDTG",           "MVA_BDTG",         nbin, -1, 1 );
00126          }
00127          if (bulkname[imva] == "BDTB"          ) {
00128             classReader[imva] = new ReadBDTB          ( inputVars );
00129             hist[imva] = new TH1F( "MVA_BDTB",           "MVA_BDTB",         nbin, -1, 1 );
00130          }
00131       }
00132    }
00133    cout << "=== Macro        : Class creation was successful" << endl;
00134 
00135    // Prepare input tree (this must be replaced by your data source)
00136    // in this example, there is a toy tree with signal and one with background events
00137    // we'll later on use only the "signal" events for the test in this example.
00138    //   
00139    TFile *input(0);
00140    if (!gSystem->AccessPathName("./tmva_example.root")) {
00141       // first we try to find tmva_example.root in the local directory
00142       cout << "=== Macro        : Accessing ./tmva_example.root" << endl;
00143       input = TFile::Open("tmva_example.root");
00144    } 
00145    
00146    if (!input) {
00147       cout << "ERROR: could not open data file" << endl;
00148       exit(1);
00149    }
00150 
00151    //
00152    // prepare the tree
00153    // - here the variable names have to corresponds to your tree
00154    // - you can use the same variables as above which is slightly faster,
00155    //   but of course you can use different ones and copy the values inside the event loop
00156    //
00157    TTree* theTree = (TTree*)input->Get("TreeS");
00158    cout << "=== Macro        : Loop over signal sample" << endl;
00159 
00160    // the references to the variables
00161    float var1, var2, var3, var4;
00162    float userVar1, userVar2;
00163    theTree->SetBranchAddress( "var1", &userVar1 );
00164    theTree->SetBranchAddress( "var2", &userVar2 );
00165    theTree->SetBranchAddress( "var3", &var3 );
00166    theTree->SetBranchAddress( "var4", &var4 );
00167 
00168    cout << "=== Macro        : Processing total of " << theTree->GetEntries() << " events ... " << endl;
00169 
00170    std::vector<double>* inputVec = new std::vector<double>( 4 );
00171    for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
00172 
00173       if (ievt%1000 == 0) cout << "=== Macro        : ... processing event: " << ievt << endl;
00174 
00175       theTree->GetEntry(ievt);
00176 
00177       var1 = userVar1 + userVar2;
00178       var2 = userVar1 - userVar2;
00179 
00180       (*inputVec)[0] = var1;
00181       (*inputVec)[1] = var2;
00182       (*inputVec)[2] = var3;
00183       (*inputVec)[3] = var4;
00184       
00185       // loop over all booked classifiers
00186       for (int imva=0; imva<Nmvas; imva++) {
00187 
00188          if (iuse[imva]) {
00189             
00190             // retrive the classifier responses            
00191             double retval = classReader[imva]->GetMvaValue( *inputVec );
00192             hist[imva]->Fill( retval, 1.0 );
00193          }
00194       }
00195    }
00196    
00197    cout << "=== Macro        : Event loop done! " << endl;
00198 
00199    TFile *target  = new TFile( "ClassApp.root","RECREATE" );
00200    for (int imva=0; imva<Nmvas; imva++) {
00201       if (iuse[imva]) {
00202          hist[imva]->Write();
00203       }
00204    }
00205    cout << "=== Macro        : Created target file: " << target->GetName() << endl;
00206    target->Close();
00207 
00208    delete target;
00209    delete inputVec;
00210     
00211    cout << "==> ClassApplication is done!" << endl << endl;
00212 } 

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