Logo ROOT   master
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 #include "TNamed.h"
39 #include "TList.h"
40 
41 #include "TMVA/Option.h"
42 
43 namespace TMVA {
44 
45  class Configurable : public TNamed {
46 
47  public:
48 
49  // constructur
50  Configurable( const TString& theOption = "" );
51 
52  // default destructur
53  virtual ~Configurable();
54 
55  // parse the internal option string
56  virtual void ParseOptions();
57 
58  // print list of defined options
59  void PrintOptions() const;
60 
61  const char* GetConfigName() const { return GetName(); }
62  const char* GetConfigDescription() const { return fConfigDescription; }
63  void SetConfigName ( const char* n ) { SetName(n); }
64  void SetConfigDescription( const char* d ) { fConfigDescription = TString(d); }
65 
66  // Declare option and bind it to a variable
67  template<class T>
68  OptionBase* DeclareOptionRef( T& ref, const TString& name, const TString& desc = "" );
69 
70  template<class T>
71  OptionBase* DeclareOptionRef( T*& ref, Int_t size, const TString& name, const TString& desc = "" );
72 
73  // Add a predefined value to the last declared option
74  template<class T>
75  void AddPreDefVal(const T&);
76 
77  // Add a predefined value to the option named optname
78  template<class T>
79  void AddPreDefVal(const TString&optname ,const T&);
80 
81 
82  void CheckForUnusedOptions() const;
83 
84  const TString& GetOptions() const { return fOptions; }
85  void SetOptions(const TString& s) { fOptions = s; }
86 
87  void WriteOptionsToStream ( std::ostream& o, const TString& prefix ) const;
88  void ReadOptionsFromStream( std::istream& istr );
89 
90  void AddOptionsXMLTo( void* parent ) const;
91  void ReadOptionsFromXML( void* node );
92 
93  protected:
94 
97 
99 
100  void ResetSetFlag();
101 
102  const TString& GetReferenceFile() const { return fReferenceFile; }
103 
104  private:
105 
106  // splits the option string at ':' and fills the list 'loo' with the primitive strings
107  void SplitOptions(const TString& theOpt, TList& loo) const;
108 
109  TString fOptions; // options string
110  Bool_t fLooseOptionCheckingEnabled; // checker for option string
111 
112  // classes and method related to easy and flexible option parsing
113  OptionBase* fLastDeclaredOption; //! last declared option
114  TList fListOfOptions; // option list
115 
116  TString fConfigDescription; // description of this configurable
117  TString fReferenceFile; // reference file for options writing
118 
119  public:
120 
121  // the mutable declaration is needed to use the logger in const methods
122  MsgLogger& Log() const { return *fLogger; }
123 
124  // set message type
125  void SetMsgType( EMsgType t ) { fLogger->SetMinType(t); }
126 
127  protected:
128  mutable MsgLogger* fLogger; //! message logger
129 
130  private:
131 
132 
133  template <class T>
134  void AssignOpt( const TString& name, T& valAssign ) const;
135 
136  public:
137 
138  ClassDef(Configurable,1); // Virtual base class for all TMVA method
139 
140  };
141 } // namespace TMVA
142 
143 // Template Declarations go here
144 
145 //______________________________________________________________________
146 template <class T>
148 {
149  // set the reference for an option
150  OptionBase* o = new Option<T>(ref, name, desc);
151  fListOfOptions.Add(o);
153  return o;
154 }
155 
156 template <class T>
158 {
159  // set the reference for an option
160  OptionBase* o = new Option<T*>(ref, size, name, desc);
161  fListOfOptions.Add(o);
162  fLastDeclaredOption = o;
163  return o;
164 }
165 
166 //______________________________________________________________________
167 template<class T>
169 {
170  // add predefined option value to the last declared option
171  Option<T>* oc = dynamic_cast<Option<T>*>(fLastDeclaredOption);
172  if(oc!=0) oc->AddPreDefVal(val);
173 }
174 
175 //______________________________________________________________________
176 template<class T>
177 void TMVA::Configurable::AddPreDefVal(const TString &optname, const T& val)
178 {
179  // add predefined option value to the option named optname
180 
181  TListIter optIt( &fListOfOptions );
182  while (OptionBase * op = (OptionBase *) optIt()) {
183  if (optname == TString(op->TheName())){
184  Option<T>* oc = dynamic_cast<Option<T>*>(op);
185  if(oc!=0){
186  oc->AddPreDefVal(val);
187  return;
188  }
189  else{
190  Log() << kFATAL << "Option \"" << optname
191  << "\" was found, but somehow I could not convert the pointer propperly.. please check the syntax of your option declaration" << Endl;
192  return;
193  }
194 
195  }
196  }
197  Log() << kFATAL << "Option \"" << optname
198  << "\" is not declared, hence cannot add predefined value, please check the syntax of your option declaration" << Endl;
199 
200 }
201 
202 //______________________________________________________________________
203 template <class T>
204 void TMVA::Configurable::AssignOpt(const TString& name, T& valAssign) const
205 {
206  // assign an option
207  TObject* opt = fListOfOptions.FindObject(name);
208  if (opt!=0) valAssign = ((Option<T>*)opt)->Value();
209  else
210  Log() << kFATAL << "Option \"" << name
211  << "\" not declared, please check the syntax of your option string" << Endl;
212 }
213 
214 #endif
215 
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
void SetMsgType(EMsgType t)
Definition: Configurable.h:125
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
void ReadOptionsFromXML(void *node)
TString fConfigDescription
Definition: Configurable.h:116
Double_t Log(Double_t x)
Definition: TMath.h:750
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:140
void AssignOpt(const TString &name, T &valAssign) const
message logger
Definition: Configurable.h:204
MsgLogger & Log() const
Definition: Configurable.h:122
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
virtual ~Configurable()
default destructor
const TString & GetReferenceFile() const
Definition: Configurable.h:102
Basic string class.
Definition: TString.h:131
void EnableLooseOptions(Bool_t b=kTRUE)
Definition: Configurable.h:96
virtual void AddPreDefVal(const T &)
Definition: Option.h:235
MsgLogger * fLogger
Definition: Configurable.h:128
Iterator of linked list.
Definition: TList.h:197
#define ClassDef(name, id)
Definition: Rtypes.h:322
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
const char * GetConfigName() const
Definition: Configurable.h:61
virtual void ParseOptions()
options parser
void SetOptions(const TString &s)
Definition: Configurable.h:85
void SetMinType(EMsgType minType)
Definition: MsgLogger.h:72
static constexpr double s
A doubly linked list.
Definition: TList.h:44
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition: TObject.cxx:321
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 ...
Class for TMVA-option handling.
Definition: Option.h:52
void WriteOptionsReferenceToFile()
write complete options to output stream
Bool_t fLooseOptionCheckingEnabled
Definition: Configurable.h:110
Configurable(const TString &theOption="")
constructor
void PrintOptions() const
prints out the options set in the options string and the defaults
#define d(i)
Definition: RSha256.hxx:102
void ResetSetFlag()
resets the IsSet flag for all declare options to be called before options are read from stream ...
void AddPreDefVal(const T &)
Definition: Configurable.h:168
Bool_t LooseOptionCheckingEnabled() const
Definition: Configurable.h:95
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
const TString & GetOptions() const
Definition: Configurable.h:84
Mother of all ROOT objects.
Definition: TObject.h:37
void SetConfigName(const char *n)
Definition: Configurable.h:63
create variable transformations
const char * GetConfigDescription() const
Definition: Configurable.h:62
virtual void Add(TObject *obj)
Definition: TList.h:87
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:114
void AddOptionsXMLTo(void *parent) const
write options to XML file
const Bool_t kTRUE
Definition: RtypesCore.h:89
void CheckForUnusedOptions() const
checks for unused options in option string
const Int_t n
Definition: legend1.C:16
OptionBase * fLastDeclaredOption
Definition: Configurable.h:113
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:64