Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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://tmva.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 #include "TMVA/Configurable.h"
47 #include "TMVA/Types.h"
48 #include "TMVA/DataSetInfo.h"
49 #include "TMVA/DataInputHandler.h"
50 #include "TMVA/DataSetManager.h"
51 
52 #include <vector>
53 #include <map>
54 #include <stdexcept>
55 
56 namespace TMVA {
57 
58  class IMethod;
59  class MethodBase;
60  class DataSetInfo;
61  class MethodCuts;
62 
63  class Reader : public Configurable {
64 
65  public:
66 
67  // without prior specification of variables
68  Reader( const TString& theOption="", Bool_t verbose = 0 );
69 
70  // STL types
71  Reader( std::vector<std::string>& varNames, const TString& theOption = "", Bool_t verbose = 0 );
72  Reader( const std::string& varNames, const TString& theOption, Bool_t verbose = 0 ); // format: "var1:var2:..."
73 
74  // Root types
75  Reader( std::vector<TString>& varNames, const TString& theOption = "", Bool_t verbose = 0 );
76  Reader( const TString& varNames, const TString& theOption, Bool_t verbose = 0 ); // format: "var1:var2:..."
77 
78  virtual ~Reader( void );
79 
80  // book MVA method via weight file
81  IMethod* BookMVA( const TString& methodTag, const TString& weightfile );
82  IMethod* BookMVA( TMVA::Types::EMVA methodType, const char* xmlstr );
83  IMethod* FindMVA( const TString& methodTag );
84  // special function for Cuts to avoid dynamic_casts in ROOT macros,
85  // which are not properly handled by CINT
86  MethodCuts* FindCutsMVA( const TString& methodTag );
87 
88 
89  // returns the MVA response for given event
90  Double_t EvaluateMVA( const std::vector<Float_t> &, const TString& methodTag, Double_t aux = 0 );
91  Double_t EvaluateMVA( const std::vector<Double_t>&, const TString& methodTag, Double_t aux = 0 );
92  Double_t EvaluateMVA( MethodBase* method, Double_t aux = 0 );
93  Double_t EvaluateMVA( const TString& methodTag, Double_t aux = 0 );
94 
95  // returns error on MVA response for given event
96  // NOTE: must be called AFTER "EvaluateMVA(...)" call !
97  Double_t GetMVAError() const { return fMvaEventError; }
98  Double_t GetMVAErrorLower() const { return fMvaEventError; }
99  Double_t GetMVAErrorUpper() const { return fMvaEventErrorUpper; }
100 
101  // regression response
102  const std::vector< Float_t >& EvaluateRegression( const TString& methodTag, Double_t aux = 0 );
103  const std::vector< Float_t >& EvaluateRegression( MethodBase* method, Double_t aux = 0 );
104  Float_t EvaluateRegression( UInt_t tgtNumber, const TString& methodTag, Double_t aux = 0 );
105 
106  // multiclass response
107  const std::vector< Float_t >& EvaluateMulticlass( const TString& methodTag, Double_t aux = 0 );
108  const std::vector< Float_t >& EvaluateMulticlass( MethodBase* method, Double_t aux = 0 );
109  Float_t EvaluateMulticlass( UInt_t clsNumber, const TString& methodTag, Double_t aux = 0 );
110 
111  // probability and rarity accessors (see Users Guide for definition of Rarity)
112  Double_t GetProba ( const TString& methodTag, Double_t ap_sig=0.5, Double_t mvaVal=-9999999 );
113  Double_t GetRarity( const TString& methodTag, Double_t mvaVal=-9999999 );
114 
115  // accessors
116  virtual const char* GetName() const { return "Reader"; }
117  Bool_t Verbose( void ) const { return fVerbose; }
118  void SetVerbose( Bool_t v ) { fVerbose = v; }
119 
120  const DataSetInfo& DataInfo() const { return fDataSetInfo; }
121  DataSetInfo& DataInfo() { return fDataSetInfo; }
122 
123  void AddVariable( const TString& expression, Float_t* );
124  void AddVariable( const TString& expression, Int_t* );
125 
126  void AddSpectator( const TString& expression, Float_t* );
127  void AddSpectator( const TString& expression, Int_t* );
128 
129  private:
130 
131  DataSetManager* fDataSetManager; // DSMTEST
132 
133 
134  TString GetMethodTypeFromFile( const TString& filename );
135 
136  // this booking method is internal
137  IMethod* BookMVA( Types::EMVA method, const TString& weightfile );
138 
139  DataSetInfo fDataSetInfo; // the data set
140 
141  DataInputHandler fDataInputHandler;
142 
143  // Init Reader class
144  void Init( void );
145 
146  // Decode Constructor string (or TString) and fill variable name std::vector
147  void DecodeVarNames( const std::string& varNames );
148  void DecodeVarNames( const TString& varNames );
149 
150  void DeclareOptions();
151 
152  Bool_t fVerbose; // verbosity
153  Bool_t fSilent; // silent mode
154  Bool_t fColor; // color mode
155  Bool_t fCalculateError; // error calculation mode
156 
157  Double_t fMvaEventError; // per-event error returned by MVA
158  Double_t fMvaEventErrorUpper; // per-event error returned by MVA
159 
160  std::map<TString, IMethod*> fMethodMap; // map of methods
161 
162  std::vector<Float_t> fTmpEvalVec; // temporary evaluation vector (if user input is v<double>)
163 
164  mutable MsgLogger* fLogger; // message logger
165  MsgLogger& Log() const { return *fLogger; }
166 
167  ClassDef(Reader,0); // Interpret the trained MVAs in an analysis context
168  };
169 
170 }
171 
172 #endif