Logo ROOT   6.07/09
Reference Guide
Reader.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss, Kai Voss, Eckhard von Toerne, Jan Therhaag
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : Reader *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Reader class to be used in the user application to interpret the trained *
12  * MVAs in an analysis context *
13  * *
14  * Authors (alphabetical order): *
15  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
16  * Peter Speckmayer <peter.speckmayer@cern.ch> - CERN, Switzerland *
17  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
18  * Jan Therhaag <Jan.Therhaag@cern.ch> - U of Bonn, Germany *
19  * Eckhard v. Toerne <evt@uni-bonn.de> - U of Bonn, Germany *
20  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
21  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
22  * *
23  * Copyright (c) 2005-2011: *
24  * CERN, Switzerland *
25  * U. of Victoria, Canada *
26  * MPI-K Heidelberg, Germany *
27  * U. of Bonn, Germany *
28  * *
29  * Redistribution and use in source and binary forms, with or without *
30  * modification, are permitted according to the terms listed in LICENSE *
31  * (http://ttmva.sourceforge.net/LICENSE) *
32  **********************************************************************************/
33 
34 #ifndef ROOT_TMVA_Reader
35 #define ROOT_TMVA_Reader
36 
37 //////////////////////////////////////////////////////////////////////////
38 // //
39 // Reader //
40 // //
41 // Reader class to be used in the user application to interpret the //
42 // trained MVAs in an analysis context //
43 // //
44 //////////////////////////////////////////////////////////////////////////
45 
46 #ifndef ROOT_TMVA_Configurable
47 #include "TMVA/Configurable.h"
48 #endif
49 #ifndef ROOT_TMVA_Types
50 #include "TMVA/Types.h"
51 #endif
52 #ifndef ROOT_TMVA_DataSetInfo
53 #include "TMVA/DataSetInfo.h"
54 #endif
55 #ifndef ROOT_TMVA_DataInputHandler
56 #include "TMVA/DataInputHandler.h"
57 #endif
58 #ifndef ROOT_TMVA_DataSetManager
59 #include "TMVA/DataSetManager.h"
60 #endif
61 
62 #include <vector>
63 #include <map>
64 #include <stdexcept>
65 
66 namespace TMVA {
67 
68  class IMethod;
69  class MethodBase;
70  class DataSetInfo;
71  class MethodCuts;
72 
73  class Reader : public Configurable {
74 
75  public:
76 
77  // without prior specification of variables
78  Reader( const TString& theOption="", Bool_t verbose = 0 );
79 
80  // STL types
81  Reader( std::vector<std::string>& varNames, const TString& theOption = "", Bool_t verbose = 0 );
82  Reader( const std::string& varNames, const TString& theOption, Bool_t verbose = 0 ); // format: "var1:var2:..."
83 
84  // Root types
85  Reader( std::vector<TString>& varNames, const TString& theOption = "", Bool_t verbose = 0 );
86  Reader( const TString& varNames, const TString& theOption, Bool_t verbose = 0 ); // format: "var1:var2:..."
87 
88  virtual ~Reader( void );
89 
90  // book MVA method via weight file
91  IMethod* BookMVA( const TString& methodTag, const TString& weightfile );
92  IMethod* BookMVA( TMVA::Types::EMVA methodType, const char* xmlstr );
93  IMethod* FindMVA( const TString& methodTag );
94  // special function for Cuts to avoid dynamic_casts in ROOT macros,
95  // which are not properly handled by CINT
96  MethodCuts* FindCutsMVA( const TString& methodTag );
97 
98 
99  // returns the MVA response for given event
100  Double_t EvaluateMVA( const std::vector<Float_t> &, const TString& methodTag, Double_t aux = 0 );
101  Double_t EvaluateMVA( const std::vector<Double_t>&, const TString& methodTag, Double_t aux = 0 );
102  Double_t EvaluateMVA( MethodBase* method, Double_t aux = 0 );
103  Double_t EvaluateMVA( const TString& methodTag, Double_t aux = 0 );
104 
105  // returns error on MVA response for given event
106  // NOTE: must be called AFTER "EvaluateMVA(...)" call !
107  Double_t GetMVAError() const { return fMvaEventError; }
110 
111  // regression response
112  const std::vector< Float_t >& EvaluateRegression( const TString& methodTag, Double_t aux = 0 );
113  const std::vector< Float_t >& EvaluateRegression( MethodBase* method, Double_t aux = 0 );
114  Float_t EvaluateRegression( UInt_t tgtNumber, const TString& methodTag, Double_t aux = 0 );
115 
116  // multiclass response
117  const std::vector< Float_t >& EvaluateMulticlass( const TString& methodTag, Double_t aux = 0 );
118  const std::vector< Float_t >& EvaluateMulticlass( MethodBase* method, Double_t aux = 0 );
119  Float_t EvaluateMulticlass( UInt_t clsNumber, const TString& methodTag, Double_t aux = 0 );
120 
121  // probability and rarity accessors (see Users Guide for definition of Rarity)
122  Double_t GetProba ( const TString& methodTag, Double_t ap_sig=0.5, Double_t mvaVal=-9999999 );
123  Double_t GetRarity( const TString& methodTag, Double_t mvaVal=-9999999 );
124 
125  // accessors
126  virtual const char* GetName() const { return "Reader"; }
127  Bool_t Verbose( void ) const { return fVerbose; }
128  void SetVerbose( Bool_t v ) { fVerbose = v; }
129 
130  const DataSetInfo& DataInfo() const { return fDataSetInfo; }
132 
133  void AddVariable( const TString& expression, Float_t* );
134  void AddVariable( const TString& expression, Int_t* );
135 
136  void AddSpectator( const TString& expression, Float_t* );
137  void AddSpectator( const TString& expression, Int_t* );
138 
139  private:
140 
142 
143 
144  TString GetMethodTypeFromFile( const TString& filename );
145 
146  // this booking method is internal
147  IMethod* BookMVA( Types::EMVA method, const TString& weightfile );
148 
149  DataSetInfo fDataSetInfo; // the data set
150 
152 
153  // Init Reader class
154  void Init( void );
155 
156  // Decode Constructor string (or TString) and fill variable name std::vector
157  void DecodeVarNames( const std::string& varNames );
158  void DecodeVarNames( const TString& varNames );
159 
160  void DeclareOptions();
161 
162  Bool_t fVerbose; // verbosity
163  Bool_t fSilent; // silent mode
164  Bool_t fColor; // color mode
165  Bool_t fCalculateError; // error calculation mode
166 
167  Double_t fMvaEventError; // per-event error returned by MVA
168  Double_t fMvaEventErrorUpper; // per-event error returned by MVA
169 
170  std::map<TString, IMethod*> fMethodMap; // map of methods
171 
172  std::vector<Float_t> fTmpEvalVec; // temporary evaluation vector (if user input is v<double>)
173 
174  mutable MsgLogger* fLogger; // message logger
175  MsgLogger& Log() const { return *fLogger; }
176 
177  ClassDef(Reader,0); // Interpret the trained MVAs in an analysis context
178  };
179 
180 }
181 
182 #endif
Double_t GetMVAErrorLower() const
Definition: Reader.h:108
void SetVerbose(Bool_t v)
Definition: Reader.h:128
MsgLogger * fLogger
Definition: Reader.h:174
std::map< TString, IMethod * > fMethodMap
Definition: Reader.h:170
const DataSetInfo & DataInfo() const
Definition: Reader.h:130
Double_t GetMVAErrorUpper() const
Definition: Reader.h:109
virtual ~Reader(void)
destructor
Definition: Reader.cxx:283
float Float_t
Definition: RtypesCore.h:53
void AddVariable(const TString &expression, Float_t *)
Add a float variable or expression to the reader.
Definition: Reader.cxx:309
Bool_t fCalculateError
Definition: Reader.h:165
MsgLogger & Log() const
Definition: Reader.h:175
Double_t fMvaEventErrorUpper
Definition: Reader.h:168
const std::vector< Float_t > & EvaluateRegression(const TString &methodTag, Double_t aux=0)
evaluates MVA for given set of input variables
Definition: Reader.cxx:583
Double_t GetRarity(const TString &methodTag, Double_t mvaVal=-9999999)
evaluates the MVA&#39;s rarity
Definition: Reader.cxx:764
Basic string class.
Definition: TString.h:137
DataSetInfo & DataInfo()
Definition: Reader.h:131
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Bool_t fSilent
Definition: Reader.h:163
void DecodeVarNames(const std::string &varNames)
decodes "name1:name2:..." form
Definition: Reader.cxx:799
TString GetMethodTypeFromFile(const TString &filename)
read the method type from the file
Definition: Reader.cxx:343
#define ClassDef(name, id)
Definition: Rtypes.h:254
DataInputHandler fDataInputHandler
Definition: Reader.h:151
void DeclareOptions()
declaration of configuration options
Definition: Reader.cxx:270
virtual const char * GetName() const
Returns name of object.
Definition: Reader.h:126
Double_t GetMVAError() const
Definition: Reader.h:107
IMethod * BookMVA(const TString &methodTag, const TString &weightfile)
read method name from weight file
Definition: Reader.cxx:378
DataSetManager * fDataSetManager
Definition: Reader.h:141
std::vector< Float_t > fTmpEvalVec
Definition: Reader.h:172
SVector< double, 2 > v
Definition: Dict.h:5
unsigned int UInt_t
Definition: RtypesCore.h:42
bool verbose
Double_t fMvaEventError
Definition: Reader.h:167
Reader(const TString &theOption="", Bool_t verbose=0)
constructor
Definition: Reader.cxx:129
Bool_t fVerbose
Definition: Reader.h:162
Bool_t fColor
Definition: Reader.h:164
void AddSpectator(const TString &expression, Float_t *)
Add a float spectator or expression to the reader.
Definition: Reader.cxx:327
double Double_t
Definition: RtypesCore.h:55
DataSetInfo fDataSetInfo
Definition: Reader.h:149
Double_t EvaluateMVA(const std::vector< Float_t > &, const TString &methodTag, Double_t aux=0)
Evaluate a std::vector<float> of input data for a given method The parameter aux is obligatory for th...
Definition: Reader.cxx:486
void Init(void)
default initialisation (no member variables)
Definition: Reader.cxx:298
Bool_t Verbose(void) const
Definition: Reader.h:127
Abstract ClassifierFactory template that handles arbitrary types.
IMethod * FindMVA(const TString &methodTag)
return pointer to method with tag "methodTag"
Definition: Reader.cxx:713
Double_t GetProba(const TString &methodTag, Double_t ap_sig=0.5, Double_t mvaVal=-9999999)
evaluates probability of MVA for given set of input variables
Definition: Reader.cxx:733
const std::vector< Float_t > & EvaluateMulticlass(const TString &methodTag, Double_t aux=0)
evaluates MVA for given set of input variables
Definition: Reader.cxx:648
MethodCuts * FindCutsMVA(const TString &methodTag)
special function for Cuts to avoid dynamic_casts in ROOT macros, which are not properly handled by CI...
Definition: Reader.cxx:725