ROOT  6.06/08
Reference Guide
Configurable.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : Configurable *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Base class for all classes with option parsing *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
16  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
17  * *
18  * Copyright (c) 2005: *
19  * CERN, Switzerland *
20  * MPI-K Heidelberg, Germany *
21  * *
22  * Redistribution and use in source and binary forms, with or without *
23  * modification, are permitted according to the terms listed in LICENSE *
24  * (http://tmva.sourceforge.net/LICENSE) *
25  **********************************************************************************/
26 
27 #ifndef ROOT_TMVA_Configurable
28 #define ROOT_TMVA_Configurable
29 
30 //////////////////////////////////////////////////////////////////////////
31 // //
32 // Configurable //
33 // //
34 // Base class for all classes with option parsing //
35 // //
36 //////////////////////////////////////////////////////////////////////////
37 
38 #ifndef ROOT_TObject
39 #include "TObject.h"
40 #endif
41 #ifndef ROOT_TList
42 #include "TList.h"
43 #endif
44 
45 #ifndef ROOT_TMVA_Option
46 #include "TMVA/Option.h"
47 #endif
48 
49 namespace TMVA {
50 
51  class Configurable : public TObject {
52 
53  public:
54 
55  // constructur
56  Configurable( const TString& theOption = "" );
57 
58  // default destructur
59  virtual ~Configurable();
60 
61  // parse the internal option string
62  virtual void ParseOptions();
63 
64  // print list of defined options
65  void PrintOptions() const;
66 
67  virtual const char* GetName() const { return GetConfigName(); }
68  const char* GetConfigName() const { return fConfigName; }
69  const char* GetConfigDescription() const { return fConfigDescription; }
70  void SetConfigName ( const char* n ) { fConfigName = TString(n); }
71  void SetConfigDescription( const char* d ) { fConfigDescription = TString(d); }
72 
73  // Declare option and bind it to a variable
74  template<class T>
75  OptionBase* DeclareOptionRef( T& ref, const TString& name, const TString& desc = "" );
76 
77  template<class T>
78  OptionBase* DeclareOptionRef( T*& ref, Int_t size, const TString& name, const TString& desc = "" );
79 
80  // Add a predefined value to the last declared option
81  template<class T>
82  void AddPreDefVal(const T&);
83 
84  // Add a predefined value to the option named optname
85  template<class T>
86  void AddPreDefVal(const TString&optname ,const T&);
87 
88 
89  void CheckForUnusedOptions() const;
90 
91  const TString& GetOptions() const { return fOptions; }
92  void SetOptions(const TString& s) { fOptions = s; }
93 
94  void WriteOptionsToStream ( std::ostream& o, const TString& prefix ) const;
95  void ReadOptionsFromStream( std::istream& istr );
96 
97  void AddOptionsXMLTo( void* parent ) const;
98  void ReadOptionsFromXML( void* node );
99 
100  protected:
101 
104 
106 
107  void ResetSetFlag();
108 
109  const TString& GetReferenceFile() const { return fReferenceFile; }
110 
111  private:
112 
113  // splits the option string at ':' and fills the list 'loo' with the primitive strings
114  void SplitOptions(const TString& theOpt, TList& loo) const;
115 
116  TString fOptions; //! options string
117  Bool_t fLooseOptionCheckingEnabled; //! checker for option string
118 
119  // classes and method related to easy and flexible option parsing
120  OptionBase* fLastDeclaredOption; //! last declared option
121  TList fListOfOptions; //! option list
122 
123  TString fConfigName; // the name of this configurable
124  TString fConfigDescription; // description of this configurable
125  TString fReferenceFile; // reference file for options writing
126 
127  protected:
128 
129  // the mutable declaration is needed to use the logger in const methods
130  MsgLogger& Log() const { return *fLogger; }
131 
132  public:
133 
134  // set message type
136 
137 
138  private:
139 
140  mutable MsgLogger* fLogger; //! message logger
141 
142  template <class T>
143  void AssignOpt( const TString& name, T& valAssign ) const;
144 
145  public:
146 
147  ClassDef(Configurable,0) // Virtual base class for all TMVA method
148 
149  };
150 } // namespace TMVA
151 
152 // Template Declarations go here
153 
154 //______________________________________________________________________
155 template <class T>
157 {
158  // set the reference for an option
159  OptionBase* o = new Option<T>(ref, name, desc);
160  fListOfOptions.Add(o);
162  return o;
163 }
164 
165 template <class T>
167 {
168  // set the reference for an option
169  OptionBase* o = new Option<T*>(ref, size, name, desc);
170  fListOfOptions.Add(o);
172  return o;
173 }
174 
175 //______________________________________________________________________
176 template<class T>
178 {
179  // add predefined option value to the last declared option
180  Option<T>* oc = dynamic_cast<Option<T>*>(fLastDeclaredOption);
181  if(oc!=0) oc->AddPreDefVal(val);
182 }
183 
184 //______________________________________________________________________
185 template<class T>
186 void TMVA::Configurable::AddPreDefVal(const TString &optname, const T& val)
187 {
188  // add predefined option value to the option named optname
189 
190  TListIter optIt( &fListOfOptions );
191  while (OptionBase * op = (OptionBase *) optIt()) {
192  if (optname == TString(op->TheName())){
193  Option<T>* oc = dynamic_cast<Option<T>*>(op);
194  if(oc!=0){
195  oc->AddPreDefVal(val);
196  return;
197  }
198  else{
199  Log() << kFATAL << "Option \"" << optname
200  << "\" was found, but somehow I could not convert the pointer propperly.. please check the syntax of your option declaration" << Endl;
201  return;
202  }
203 
204  }
205  }
206  Log() << kFATAL << "Option \"" << optname
207  << "\" is not declared, hence cannot add predefined value, please check the syntax of your option declaration" << Endl;
208 
209 }
210 
211 //______________________________________________________________________
212 template <class T>
213 void TMVA::Configurable::AssignOpt(const TString& name, T& valAssign) const
214 {
215  // assign an option
216  TObject* opt = fListOfOptions.FindObject(name);
217  if (opt!=0) valAssign = ((Option<T>*)opt)->Value();
218  else
219  Log() << kFATAL << "Option \"" << name
220  << "\" not declared, please check the syntax of your option string" << Endl;
221 }
222 
223 #endif
224 
void SetMsgType(EMsgType t)
Definition: Configurable.h:135
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
void ReadOptionsFromXML(void *node)
TString fConfigDescription
Definition: Configurable.h:124
double T(double x)
Definition: ChebyshevPol.h:34
void ReadOptionsFromStream(std::istream &istr)
read option back from the weight file
void AssignOpt(const TString &name, T &valAssign) const
message logger
Definition: Configurable.h:213
MsgLogger & Log() const
Definition: Configurable.h:130
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
TString fConfigName
option list
Definition: Configurable.h:123
virtual ~Configurable()
default destructur
const TString & GetReferenceFile() const
Definition: Configurable.h:109
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void EnableLooseOptions(Bool_t b=kTRUE)
Definition: Configurable.h:103
virtual void AddPreDefVal(const T &)
Definition: Option.h:241
MsgLogger * fLogger
Definition: Configurable.h:140
Iterator of linked list.
Definition: TList.h:187
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:496
#define ClassDef(name, id)
Definition: Rtypes.h:254
const char * GetConfigName() const
Definition: Configurable.h:68
virtual void ParseOptions()
options parser
void SetOptions(const TString &s)
Definition: Configurable.h:92
void SetMinType(EMsgType minType)
Definition: MsgLogger.h:76
A doubly linked list.
Definition: TList.h:47
void SplitOptions(const TString &theOpt, TList &loo) const
splits the option string at &#39;:&#39; and fills the list &#39;loo&#39; with the primitive strings ...
EMsgType
Definition: Types.h:61
void WriteOptionsReferenceToFile()
write complete options to output stream
Bool_t fLooseOptionCheckingEnabled
options string
Definition: Configurable.h:117
Configurable(const TString &theOption="")
constructor
void PrintOptions() const
prints out the options set in the options string and the defaults
void ResetSetFlag()
resets the IsSet falg for all declare options to be called before options are read from stream ...
void AddPreDefVal(const T &)
Definition: Configurable.h:177
Bool_t LooseOptionCheckingEnabled() const
Definition: Configurable.h:102
#define name(a, b)
Definition: linkTestLib0.cpp:5
const TString & GetOptions() const
Definition: Configurable.h:91
Mother of all ROOT objects.
Definition: TObject.h:58
void SetConfigName(const char *n)
Definition: Configurable.h:70
Abstract ClassifierFactory template that handles arbitrary types.
const char * GetConfigDescription() const
Definition: Configurable.h:69
virtual void Add(TObject *obj)
Definition: TList.h:81
TList fListOfOptions
last declared option
Definition: Configurable.h:121
void AddOptionsXMLTo(void *parent) const
write options to XML file
virtual const char * GetName() const
Returns name of object.
Definition: Configurable.h:67
const Bool_t kTRUE
Definition: Rtypes.h:91
void CheckForUnusedOptions() const
checks for unused options in option string
const Int_t n
Definition: legend1.C:16
OptionBase * fLastDeclaredOption
checker for option string
Definition: Configurable.h:120
void WriteOptionsToStream(std::ostream &o, const TString &prefix) const
write options to output stream (e.g. in writing the MVA weight files
void SetConfigDescription(const char *d)
Definition: Configurable.h:71