Logo ROOT   6.08/07
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_TNamed
39 #include "TNamed.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 TNamed {
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  const char* GetConfigName() const { return GetName(); }
68  const char* GetConfigDescription() const { return fConfigDescription; }
69  void SetConfigName ( const char* n ) { SetName(n); }
70  void SetConfigDescription( const char* d ) { fConfigDescription = TString(d); }
71 
72  // Declare option and bind it to a variable
73  template<class T>
74  OptionBase* DeclareOptionRef( T& ref, const TString& name, const TString& desc = "" );
75 
76  template<class T>
77  OptionBase* DeclareOptionRef( T*& ref, Int_t size, const TString& name, const TString& desc = "" );
78 
79  // Add a predefined value to the last declared option
80  template<class T>
81  void AddPreDefVal(const T&);
82 
83  // Add a predefined value to the option named optname
84  template<class T>
85  void AddPreDefVal(const TString&optname ,const T&);
86 
87 
88  void CheckForUnusedOptions() const;
89 
90  const TString& GetOptions() const { return fOptions; }
91  void SetOptions(const TString& s) { fOptions = s; }
92 
93  void WriteOptionsToStream ( std::ostream& o, const TString& prefix ) const;
94  void ReadOptionsFromStream( std::istream& istr );
95 
96  void AddOptionsXMLTo( void* parent ) const;
97  void ReadOptionsFromXML( void* node );
98 
99  protected:
100 
103 
105 
106  void ResetSetFlag();
107 
108  const TString& GetReferenceFile() const { return fReferenceFile; }
109 
110  private:
111 
112  // splits the option string at ':' and fills the list 'loo' with the primitive strings
113  void SplitOptions(const TString& theOpt, TList& loo) const;
114 
115  TString fOptions; // options string
116  Bool_t fLooseOptionCheckingEnabled; // checker for option string
117 
118  // classes and method related to easy and flexible option parsing
119  OptionBase* fLastDeclaredOption; //! last declared option
120  TList fListOfOptions; // option list
121 
122  TString fConfigDescription; // description of this configurable
123  TString fReferenceFile; // reference file for options writing
124 
125  public:
126 
127  // the mutable declaration is needed to use the logger in const methods
128  MsgLogger& Log() const { return *fLogger; }
129 
130  // set message type
132 
133  protected:
134  mutable MsgLogger* fLogger; //! message logger
135 
136  private:
137 
138 
139  template <class T>
140  void AssignOpt( const TString& name, T& valAssign ) const;
141 
142  public:
143 
144  ClassDef(Configurable,1); // Virtual base class for all TMVA method
145 
146  };
147 } // namespace TMVA
148 
149 // Template Declarations go here
150 
151 //______________________________________________________________________
152 template <class T>
154 {
155  // set the reference for an option
156  OptionBase* o = new Option<T>(ref, name, desc);
157  fListOfOptions.Add(o);
159  return o;
160 }
161 
162 template <class T>
164 {
165  // set the reference for an option
166  OptionBase* o = new Option<T*>(ref, size, name, desc);
167  fListOfOptions.Add(o);
169  return o;
170 }
171 
172 //______________________________________________________________________
173 template<class T>
175 {
176  // add predefined option value to the last declared option
177  Option<T>* oc = dynamic_cast<Option<T>*>(fLastDeclaredOption);
178  if(oc!=0) oc->AddPreDefVal(val);
179 }
180 
181 //______________________________________________________________________
182 template<class T>
183 void TMVA::Configurable::AddPreDefVal(const TString &optname, const T& val)
184 {
185  // add predefined option value to the option named optname
186 
187  TListIter optIt( &fListOfOptions );
188  while (OptionBase * op = (OptionBase *) optIt()) {
189  if (optname == TString(op->TheName())){
190  Option<T>* oc = dynamic_cast<Option<T>*>(op);
191  if(oc!=0){
192  oc->AddPreDefVal(val);
193  return;
194  }
195  else{
196  Log() << kFATAL << "Option \"" << optname
197  << "\" was found, but somehow I could not convert the pointer propperly.. please check the syntax of your option declaration" << Endl;
198  return;
199  }
200 
201  }
202  }
203  Log() << kFATAL << "Option \"" << optname
204  << "\" is not declared, hence cannot add predefined value, please check the syntax of your option declaration" << Endl;
205 
206 }
207 
208 //______________________________________________________________________
209 template <class T>
210 void TMVA::Configurable::AssignOpt(const TString& name, T& valAssign) const
211 {
212  // assign an option
213  TObject* opt = fListOfOptions.FindObject(name);
214  if (opt!=0) valAssign = ((Option<T>*)opt)->Value();
215  else
216  Log() << kFATAL << "Option \"" << name
217  << "\" not declared, please check the syntax of your option string" << Endl;
218 }
219 
220 #endif
221 
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
void SetMsgType(EMsgType t)
Definition: Configurable.h:131
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
void ReadOptionsFromXML(void *node)
TString fConfigDescription
Definition: Configurable.h:122
double T(double x)
Definition: ChebyshevPol.h:34
void ReadOptionsFromStream(std::istream &istr)
read option back from the weight file
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:131
void AssignOpt(const TString &name, T &valAssign) const
message logger
Definition: Configurable.h:210
MsgLogger & Log() const
Definition: Configurable.h:128
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
virtual ~Configurable()
default destructur
const TString & GetReferenceFile() const
Definition: Configurable.h:108
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:102
virtual void AddPreDefVal(const T &)
Definition: Option.h:244
MsgLogger * fLogger
Definition: Configurable.h:134
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:497
#define ClassDef(name, id)
Definition: Rtypes.h:254
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
const char * GetConfigName() const
Definition: Configurable.h:67
virtual void ParseOptions()
options parser
void SetOptions(const TString &s)
Definition: Configurable.h:91
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
Definition: Configurable.h:116
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:174
Bool_t LooseOptionCheckingEnabled() const
Definition: Configurable.h:101
const TString & GetOptions() const
Definition: Configurable.h:90
Mother of all ROOT objects.
Definition: TObject.h:37
void SetConfigName(const char *n)
Definition: Configurable.h:69
Abstract ClassifierFactory template that handles arbitrary types.
const char * GetConfigDescription() const
Definition: Configurable.h:68
virtual void Add(TObject *obj)
Definition: TList.h:81
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
TList fListOfOptions
last declared option
Definition: Configurable.h:120
void AddOptionsXMLTo(void *parent) const
write options to XML file
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
Definition: Configurable.h:119
char name[80]
Definition: TGX11.cxx:109
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:70