ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MethodCompositeBase.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss,Or Cohen
3 
4 /*****************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : MethodCompositeBase *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Virtual base class for all MVA method *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - MSU, USA *
16  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
17  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
18  * Or Cohen <orcohenor@gmail.com> - Weizmann Inst., Israel *
19  * *
20  * Copyright (c) 2005: *
21  * CERN, Switzerland *
22  * U. of Victoria, Canada *
23  * MPI-K Heidelberg, Germany *
24  * LAPP, Annecy, France *
25  * *
26  * Redistribution and use in source and binary forms, with or without *
27  * modification, are permitted according to the terms listed in LICENSE *
28  * (http://tmva.sourceforge.net/LICENSE) *
29  *****************************************************************************/
30 
31 //_______________________________________________________________________
32 //
33 // This class is virtual class meant to combine more than one classifier//
34 // together. The training of the classifiers is done by classes that are//
35 // derived from this one, while the saving and loading of weights file //
36 // and the evaluation is done here. //
37 //_______________________________________________________________________
38 
40 
41 #include <algorithm>
42 #include <iomanip>
43 #include <vector>
44 
45 #include "Riostream.h"
46 #include "TRandom3.h"
47 #include "TMath.h"
48 #include "TObjString.h"
49 
50 #include "TMVA/ClassifierFactory.h"
51 #include "TMVA/Factory.h"
52 #include "TMVA/IMethod.h"
53 #include "TMVA/MethodBase.h"
54 #include "TMVA/MethodBoost.h"
55 #include "TMVA/MsgLogger.h"
56 #include "TMVA/Tools.h"
57 #include "TMVA/Types.h"
58 
59 using std::vector;
60 
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 
65 TMVA::MethodCompositeBase::MethodCompositeBase( const TString& jobName,
66  Types::EMVA methodType,
67  const TString& methodTitle,
68  DataSetInfo& theData,
69  const TString& theOption,
70  TDirectory* theTargetDir )
71  : TMVA::MethodBase( jobName, methodType, methodTitle, theData, theOption, theTargetDir ),
72  fCurrentMethodIdx(0), fCurrentMethod(0)
73 {}
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 
78  DataSetInfo& dsi,
79  const TString& weightFile,
80  TDirectory* theTargetDir )
81  : TMVA::MethodBase( methodType, dsi, weightFile, theTargetDir ),
82  fCurrentMethodIdx(0), fCurrentMethod(0)
83 {}
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// returns pointer to MVA that corresponds to given method title
87 
89 {
90  std::vector<IMethod*>::const_iterator itrMethod = fMethods.begin();
91  std::vector<IMethod*>::const_iterator itrMethodEnd = fMethods.end();
92 
93  for (; itrMethod != itrMethodEnd; itrMethod++) {
94  MethodBase* mva = dynamic_cast<MethodBase*>(*itrMethod);
95  if ( (mva->GetMethodName())==methodTitle ) return mva;
96  }
97  return 0;
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// returns pointer to MVA that corresponds to given method index
102 
104 {
105  std::vector<IMethod*>::const_iterator itrMethod = fMethods.begin()+index;
106  if (itrMethod<fMethods.end()) return *itrMethod;
107  else return 0;
108 }
109 
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 
114 {
115  void* wght = gTools().AddChild(parent, "Weights");
116  gTools().AddAttr( wght, "NMethods", fMethods.size() );
117  for (UInt_t i=0; i< fMethods.size(); i++)
118  {
119  void* methxml = gTools().AddChild( wght, "Method" );
120  MethodBase* method = dynamic_cast<MethodBase*>(fMethods[i]);
121  gTools().AddAttr(methxml,"Index", i );
122  gTools().AddAttr(methxml,"Weight", fMethodWeight[i]);
123  gTools().AddAttr(methxml,"MethodSigCut", method->GetSignalReferenceCut());
124  gTools().AddAttr(methxml,"MethodSigCutOrientation", method->GetSignalReferenceCutOrientation());
125  gTools().AddAttr(methxml,"MethodTypeName", method->GetMethodTypeName());
126  gTools().AddAttr(methxml,"MethodName", method->GetMethodName() );
127  gTools().AddAttr(methxml,"JobName", method->GetJobName());
128  gTools().AddAttr(methxml,"Options", method->GetOptions());
129  if (method->fTransformationPointer)
130  gTools().AddAttr(methxml,"UseMainMethodTransformation", TString("true"));
131  else
132  gTools().AddAttr(methxml,"UseMainMethodTransformation", TString("false"));
133  method->AddWeightsXMLTo(methxml);
134  }
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// delete methods
139 
141 {
142  std::vector<IMethod*>::iterator itrMethod = fMethods.begin();
143  for (; itrMethod != fMethods.end(); itrMethod++) {
144  Log() << kVERBOSE << "Delete method: " << (*itrMethod)->GetName() << Endl;
145  delete (*itrMethod);
146  }
147  fMethods.clear();
148 }
149 
150 ////////////////////////////////////////////////////////////////////////////////
151 /// XML streamer
152 
154 {
155  UInt_t nMethods;
156  TString methodName, methodTypeName, jobName, optionString;
157 
158  for (UInt_t i=0;i<fMethods.size();i++) delete fMethods[i];
159  fMethods.clear();
160  fMethodWeight.clear();
161  gTools().ReadAttr( wghtnode, "NMethods", nMethods );
162  void* ch = gTools().GetChild(wghtnode);
163  for (UInt_t i=0; i< nMethods; i++) {
164  Double_t methodWeight, methodSigCut, methodSigCutOrientation;
165  gTools().ReadAttr( ch, "Weight", methodWeight );
166  gTools().ReadAttr( ch, "MethodSigCut", methodSigCut);
167  gTools().ReadAttr( ch, "MethodSigCutOrientation", methodSigCutOrientation);
168  gTools().ReadAttr( ch, "MethodTypeName", methodTypeName );
169  gTools().ReadAttr( ch, "MethodName", methodName );
170  gTools().ReadAttr( ch, "JobName", jobName );
171  gTools().ReadAttr( ch, "Options", optionString );
172 
173  // Bool_t rerouteTransformation = kFALSE;
174  if (gTools().HasAttr( ch, "UseMainMethodTransformation")) {
175  TString rerouteString("");
176  gTools().ReadAttr( ch, "UseMainMethodTransformation", rerouteString );
177  rerouteString.ToLower();
178  // if (rerouteString=="true")
179  // rerouteTransformation=kTRUE;
180  }
181 
182  //remove trailing "~" to signal that options have to be reused
183  optionString.ReplaceAll("~","");
184  //ignore meta-options for method Boost
185  optionString.ReplaceAll("Boost_","~Boost_");
186  optionString.ReplaceAll("!~","~!");
187 
188  if (i==0){
189  // the cast on MethodBoost is ugly, but a similar line is also in ReadWeightsFromFile --> needs to be fixed later
190  ((TMVA::MethodBoost*)this)->BookMethod( Types::Instance().GetMethodType( methodTypeName), methodName, optionString );
191  }
192  fMethods.push_back(ClassifierFactory::Instance().Create(
193  std::string(methodTypeName),jobName, methodName,DataInfo(),optionString));
194 
195  fMethodWeight.push_back(methodWeight);
196  MethodBase* meth = dynamic_cast<MethodBase*>(fMethods.back());
197 
198  if(meth==0)
199  Log() << kFATAL << "Could not read method from XML" << Endl;
200 
201  void* methXML = gTools().GetChild(ch);
202  meth->SetupMethod();
203  meth->SetMsgType(kWARNING);
204  meth->ParseOptions();
205  meth->ProcessSetup();
206  meth->CheckSetup();
207  meth->ReadWeightsFromXML(methXML);
208  meth->SetSignalReferenceCut(methodSigCut);
209  meth->SetSignalReferenceCutOrientation(methodSigCutOrientation);
210 
211  meth->RerouteTransformationHandler (&(this->GetTransformationHandler()));
212 
213  ch = gTools().GetNextChild(ch);
214  }
215  //Log() << kINFO << "Reading methods from XML done " << Endl;
216 }
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 /// text streamer
220 
222 {
223  TString var, dummy;
224  TString methodName, methodTitle=GetMethodName(),
225  jobName=GetJobName(),optionString=GetOptions();
226  UInt_t methodNum; Double_t methodWeight;
227  // and read the Weights (BDT coefficients)
228  // coverity[tainted_data_argument]
229  istr >> dummy >> methodNum;
230  Log() << kINFO << "Read " << methodNum << " Classifiers" << Endl;
231  for (UInt_t i=0;i<fMethods.size();i++) delete fMethods[i];
232  fMethods.clear();
233  fMethodWeight.clear();
234  for (UInt_t i=0; i<methodNum; i++) {
235  istr >> dummy >> methodName >> dummy >> fCurrentMethodIdx >> dummy >> methodWeight;
236  if ((UInt_t)fCurrentMethodIdx != i) {
237  Log() << kFATAL << "Error while reading weight file; mismatch MethodIndex="
238  << fCurrentMethodIdx << " i=" << i
239  << " MethodName " << methodName
240  << " dummy " << dummy
241  << " MethodWeight= " << methodWeight
242  << Endl;
243  }
244  if (GetMethodType() != Types::kBoost || i==0) {
245  istr >> dummy >> jobName;
246  istr >> dummy >> methodTitle;
247  istr >> dummy >> optionString;
248  if (GetMethodType() == Types::kBoost)
249  ((TMVA::MethodBoost*)this)->BookMethod( Types::Instance().GetMethodType( methodName), methodTitle, optionString );
250  }
251  else methodTitle=Form("%s (%04i)",GetMethodName().Data(),fCurrentMethodIdx);
252  fMethods.push_back(ClassifierFactory::Instance().Create( std::string(methodName), jobName,
253  methodTitle,DataInfo(), optionString) );
254  fMethodWeight.push_back( methodWeight );
255  if(MethodBase* m = dynamic_cast<MethodBase*>(fMethods.back()) )
256  m->ReadWeightsFromStream(istr);
257  }
258 }
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// return composite MVA response
262 
264 {
265  Double_t mvaValue = 0;
266  for (UInt_t i=0;i< fMethods.size(); i++) mvaValue+=fMethods[i]->GetMvaValue()*fMethodWeight[i];
267 
268  // cannot determine error
269  NoErrorCalc(err, errUpper);
270 
271  return mvaValue;
272 }
static ClassifierFactory & Instance()
access to the ClassifierFactory singleton creates the instance if needed
void SetMsgType(EMsgType t)
Definition: Configurable.h:135
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
void ReadWeightsFromXML(void *wghtnode)
XML streamer.
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
TransformationHandler * fTransformationPointer
Definition: MethodBase.h:587
void SetSignalReferenceCutOrientation(Double_t cutOrientation)
Definition: MethodBase.h:330
Basic string class.
Definition: TString.h:137
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1075
int Int_t
Definition: RtypesCore.h:41
void ReadWeightsFromStream(std::istream &istr)
text streamer
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
Definition: Tools.h:308
void AddWeightsXMLTo(void *parent) const
void * AddChild(void *parent, const char *childname, const char *content=0, bool isRootNode=false)
add child node
Definition: Tools.cxx:1134
const TString & GetMethodName() const
Definition: MethodBase.h:296
MethodCompositeBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &theData, const TString &theOption="", TDirectory *theTargetDir=NULL)
static Types & Instance()
the the single instance of "Types" if existin already, or create it (Signleton)
Definition: Types.cxx:64
ClassImp(TMVA::MethodCompositeBase) TMVA
Tools & gTools()
Definition: Tools.cxx:79
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1158
virtual ~MethodCompositeBase(void)
delete methods
std::vector< std::vector< double > > Data
virtual void ParseOptions()
options parser
void SetupMethod()
setup of methods
Definition: MethodBase.cxx:302
virtual void AddWeightsXMLTo(void *parent) const =0
virtual void ReadWeightsFromXML(void *wghtnode)=0
std::string GetMethodName(TCppMethod_t)
Definition: Cppyy.cxx:706
IMethod * GetMethod(const TString &title) const
returns pointer to MVA that corresponds to given method title
unsigned int UInt_t
Definition: RtypesCore.h:42
TMarker * m
Definition: textangle.C:8
char * Form(const char *fmt,...)
void ReadAttr(void *node, const char *, T &value)
Definition: Tools.h:295
virtual void CheckSetup()
check may be overridden by derived class (sometimes, eg, fitters are used which can only be implement...
Definition: MethodBase.cxx:327
void RerouteTransformationHandler(TransformationHandler *fTargetTransformation)
Definition: MethodBase.h:357
double Double_t
Definition: RtypesCore.h:55
Describe directory structure in memory.
Definition: TDirectory.h:44
static RooMathCoreReg dummy
void * GetNextChild(void *prevchild, const char *childname=0)
XML helpers.
Definition: Tools.cxx:1170
Double_t GetSignalReferenceCutOrientation() const
Definition: MethodBase.h:326
void ProcessSetup()
process all options the "CheckForUnusedOptions" is done in an independent call, since it may be overr...
Definition: MethodBase.cxx:317
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
return composite MVA response
const TString & GetJobName() const
Definition: MethodBase.h:295
const TString & GetOptions() const
Definition: Configurable.h:91
Double_t GetSignalReferenceCut() const
Definition: MethodBase.h:325
TString GetMethodTypeName() const
Definition: MethodBase.h:297
Definition: math.cpp:60
void SetSignalReferenceCut(Double_t cut)
Definition: MethodBase.h:329