TMVATest.cxx

Go to the documentation of this file.
00001 /**********************************************************************************
00002  * Project   : TMVA - a Root-integrated toolkit for multivariate data analysis    *
00003  * Package   : TMVA                                                               *
00004  * Exectuable: TMVRead                                                            *
00005  *                                                                                *
00006  * This exectutable provides an example on JUST testing of several                   *
00007  * Multivariate Analyser (MVA) methods using previously written weight files      *
00008  * on a given test tree  (set of test events), which must of course contain the   *
00009  * variables named as in the weight file                                          *
00010  *                                                                                   *
00011  *                                                                                  *
00012  * The methods to be used can be switched on and off by means of the boolians          *
00013  * below                                                                          *
00014  *                                                                                  *
00015  * The output file "TMVA.root" can be analysed with the use of dedicated          *
00016  * macros (simply say: root -l <macro.C>) :                                          *
00017  *                                                                                  *
00018  * ../macros/variables.C  ==> shows the MVA input variables for signal and backgr  *
00019  * ../macros/correlations.C  ==> shows the correlations between the MVA input vars *
00020  * ../macros/mvas.C          ==> shows the trained MVAs for the test events          *
00021  * ../macros/efficiencies.C  ==> shows the background rejections versus signal effs*
00022  *                         for all MVAs used                                          *
00023  *                                                                                  *
00024  *                                                                                *
00025  **********************************************************************************/
00026 
00027 #include <iostream> // Stream declarations
00028 #include "TMVA/Factory.h"
00029 #include "TMVA/MethodBDT.h"
00030 #include "TMVA/MethodCuts.h"
00031 #include "TMVA/MethodLikelihood.h"
00032 #include "TMVA/MethodCFMlpANN.h"
00033 #include "TMVA/MethodFisher.h"
00034 #include "TMVA/MethodPDERS.h"
00035 #include "TMVA/MethodHMatrix.h"
00036 #include "TMVA/MethodTMlpANN.h"
00037 #include "TFile.h"
00038 #include "TTree.h"
00039 #include "TString.h"
00040 #include "TObjString.h"
00041 #include "TMVA/Tools.h"
00042 
00043 using namespace std;
00044 
00045 // ---------------------------------------------------------------
00046 // choose MVA methods to be tested
00047 Bool_t  Use_Cuts             = 0;
00048 TString weightFileCuts       = "weights/TMVAnalysis_Cuts.weights";
00049 Bool_t  Use_Likelihood       = 0;
00050 TString weightFileLikelihood = "weights/TMVAnalysis_Likelihood.weights";
00051 Bool_t  Use_LikelihoodD      = 0;
00052 TString weightFileLikelihoodD= "weights/TMVAnalysis_LikelihoodD.weights";
00053 Bool_t  Use_PDERS            = 0;
00054 TString weightFilePDERS      = "weights/TMVAnalysis_PDERS.weights.root";
00055 Bool_t  Use_HMatrix          = 0;
00056 TString weightFileHMatrix    = "weights/TMVAnalysis_HMatrix.weights";
00057 Bool_t  Use_Fisher           = 0;
00058 TString weightFileFisher     = "weights/TMVAnalysis_Fisher.weights";
00059 Bool_t  Use_CFMlpANN         = 0;
00060 TString weightFileCFMlpANN   = "weights/TMVAnalysis_CFMlpANN.weights";
00061 Bool_t  Use_TMlpANN          = 0;
00062 TString weightFileTMlpANN    = "weights/TMVAnalysis_TMlpANN.weights";
00063 Bool_t  Use_BDTGini          = 1; 
00064 TString weightFileBDTGini    = "weights/TMVAnalysis_BDTGini.weights";
00065 Bool_t  Use_BDTCD            = 0; 
00066 TString weightFileBDTCE      = "weights/TMVAnalysis_BDTCE.weights";
00067 Bool_t  Use_BDTStatSig       = 0; 
00068 TString weightFileBDTStatSig = "weights/TMVAnalysis_BDTStatSig.weights";
00069 Bool_t  Use_BDTMisCL         = 0; 
00070 TString weightFileBDTMisCl   = "weights/TMVAnalysis_BDTMisCl.weights";
00071 // ---------------------------------------------------------------
00072 Bool_t EvaluateVariables  = 0; // perform evaluation for each input variable
00073 // ---------------------------------------------------------------
00074 
00075 int main( int argc, char** argv ) 
00076 {
00077   cout << endl;
00078   cout << "==> start TMVRead" << endl;
00079 
00080   // ---- define the root output file
00081   char *outFile = new char[160];
00082   char *inFile = new char[160];
00083   outFile="TMVATest.root";
00084   inFile="dummy.root";
00085 
00086   if (argc<2) {
00087     cout << "ERROR!!!!!!"<<endl;
00088     cout << "    You need to provide the input file (with the test tree) and optionally "<<endl;
00089     cout << "    the name of the output file (defaut TMVRead.root) " << endl;
00090     cout << "ERROR!!!!!!"<<endl;
00091     exit(1);
00092   }
00093   else{
00094     inFile = argv[1];
00095   }
00096 
00097   if (argc>2) outFile = argv[2];
00098   // you want the TestTree in the output file... as it seems IMPOSSIBLE to 
00099   // copy (clone) a tree without knowing what is inside, I simply copy the 
00100   // whole file
00101   char syscall[160];
00102   sprintf (syscall,"cp %s %s ",inFile,outFile);
00103   system(syscall);
00104   TFile* target = TFile::Open( outFile, "UPDATE" );
00105 
00106   //attention, the first STRING given here defined the TAG of the
00107   //weight files, that will be looked at. Have a look at in the directory
00108   // weights/ and you'll understand what I mean.
00109   TMVA::Factory *factory = new TMVA::Factory( target ) ;
00110 
00111   vector<TString>* inputVars = new vector<TString>;
00112   inputVars->push_back("var1");
00113   inputVars->push_back("var2");
00114   inputVars->push_back("var3");
00115   inputVars->push_back("var4");
00116 
00117   if (Use_BDTGini)
00118     factory->BookMethod(new TMVA_MethodBDT(inputVars, weightFileBDTGini));
00119 
00120   TFile *testTreeFile = new TFile(inFile);
00121   TTree *testTree = (TTree*) testTreeFile->Get("TestTree");
00122 
00123 
00124 
00125   factory->SetTestTree(testTree);
00126 
00127 
00128   // test MVAs
00129   factory->TestAllMethods();
00130 
00131   // evaluate MVAs
00132   factory->EvaluateAllMethods();    
00133 
00134   // ---- terminate job
00135 
00136   // close output file
00137   target->Close();
00138 
00139   // clean up
00140   delete factory;
00141 
00142   cout << "==> wrote root file " <<   TString(outFile) <<  endl;
00143   cout << "==> TMVTest is done!" << endl;      
00144 }

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