Logo ROOT  
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 
Option.h
n
const Int_t n
Definition: legend1.C:16
TMVA::Configurable::fLooseOptionCheckingEnabled
Bool_t fLooseOptionCheckingEnabled
Definition: Configurable.h:110
TMVA::Configurable::PrintOptions
void PrintOptions() const
prints out the options set in the options string and the defaults
Definition: Configurable.cxx:299
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TMVA::Configurable::Log
MsgLogger & Log() const
Definition: Configurable.h:122
TMVA::Configurable::ReadOptionsFromStream
void ReadOptionsFromStream(std::istream &istr)
read option back from the weight file
Definition: Configurable.cxx:435
TMVA::Configurable
Definition: Configurable.h:45
TNamed::SetName
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
TMVA::Configurable::WriteOptionsReferenceToFile
void WriteOptionsReferenceToFile()
write complete options to output stream
Definition: Configurable.cxx:409
TMVA::Configurable::GetReferenceFile
const TString & GetReferenceFile() const
Definition: Configurable.h:102
TMVA::Configurable::DeclareOptionRef
OptionBase * DeclareOptionRef(T *&ref, Int_t size, const TString &name, const TString &desc="")
TMVA::Configurable::AddPreDefVal
void AddPreDefVal(const T &)
Definition: Configurable.h:168
TMVA::Configurable::GetConfigDescription
const char * GetConfigDescription() const
Definition: Configurable.h:62
TMVA::Option::AddPreDefVal
virtual void AddPreDefVal(const T &)
Definition: Option.h:235
TNamed.h
TMath::Log
Double_t Log(Double_t x)
Definition: TMath.h:760
TMVA::Configurable::WriteOptionsToStream
void WriteOptionsToStream(std::ostream &o, const TString &prefix) const
write options to output stream (e.g. in writing the MVA weight files
Definition: Configurable.cxx:333
TMVA::Configurable::AddOptionsXMLTo
void AddOptionsXMLTo(void *parent) const
write options to XML file
Definition: Configurable.cxx:349
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
TList.h
TMVA::Configurable::SplitOptions
void SplitOptions(const TString &theOpt, TList &loo) const
splits the option string at ':' and fills the list 'loo' with the primitive strings
Definition: Configurable.cxx:92
TString
Basic string class.
Definition: TString.h:136
b
#define b(i)
Definition: RSha256.hxx:100
TMVA::Configurable::AssignOpt
void AssignOpt(const TString &name, T &valAssign) const
message logger
Definition: Configurable.h:204
bool
TListIter
Iterator of linked list.
Definition: TList.h:200
TMVA::Configurable::fConfigDescription
TString fConfigDescription
Definition: Configurable.h:116
TMVA::Configurable::LooseOptionCheckingEnabled
Bool_t LooseOptionCheckingEnabled() const
Definition: Configurable.h:95
TMVA::Configurable::EnableLooseOptions
void EnableLooseOptions(Bool_t b=kTRUE)
Definition: Configurable.h:96
TMVA::Configurable::ReadOptionsFromXML
void ReadOptionsFromXML(void *node)
Definition: Configurable.cxx:378
TMVA::Configurable::CheckForUnusedOptions
void CheckForUnusedOptions() const
checks for unused options in option string
Definition: Configurable.cxx:270
TMVA::Option
Definition: Option.h:97
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TObject::FindObject
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition: TObject.cxx:323
TMVA::MsgLogger::SetMinType
void SetMinType(EMsgType minType)
Definition: MsgLogger.h:72
TMVA::Configurable::ResetSetFlag
void ResetSetFlag()
resets the IsSet flag for all declare options to be called before options are read from stream
Definition: Configurable.cxx:113
TMVA::Configurable::fOptions
TString fOptions
Definition: Configurable.h:109
TMVA::Endl
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
TMVA::Configurable::GetOptions
const TString & GetOptions() const
Definition: Configurable.h:84
TMVA::Configurable::SetMsgType
void SetMsgType(EMsgType t)
Definition: Configurable.h:125
TMVA::Configurable::SetOptions
void SetOptions(const TString &s)
Definition: Configurable.h:85
TMVA::Configurable::GetConfigName
const char * GetConfigName() const
Definition: Configurable.h:61
TMVA::MsgLogger
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
TMVA::Option< T * >
Definition: Option.h:131
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
TMVA::Configurable::fLogger
MsgLogger * fLogger
Definition: Configurable.h:128
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TMVA::Configurable::SetConfigDescription
void SetConfigDescription(const char *d)
Definition: Configurable.h:64
name
char name[80]
Definition: TGX11.cxx:110
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
d
#define d(i)
Definition: RSha256.hxx:102
TMVA::Configurable::Configurable
Configurable(const TString &theOption="")
constructor
Definition: Configurable.cxx:66
TMVA::Configurable::fLastDeclaredOption
OptionBase * fLastDeclaredOption
Definition: Configurable.h:113
TMVA::Configurable::fReferenceFile
TString fReferenceFile
Definition: Configurable.h:117
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TMVA::Configurable::~Configurable
virtual ~Configurable()
default destructor
Definition: Configurable.cxx:84
TMVA::Configurable::DeclareOptionRef
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
TMVA::OptionBase
Class for TMVA-option handling.
Definition: Option.h:52
TMVA::Configurable::ParseOptions
virtual void ParseOptions()
options parser
Definition: Configurable.cxx:124
TMVA::Configurable::fListOfOptions
TList fListOfOptions
last declared option
Definition: Configurable.h:114
TList
A doubly linked list.
Definition: TList.h:44
TMVA::Configurable::SetConfigName
void SetConfigName(const char *n)
Definition: Configurable.h:63
TMVA
create variable transformations
Definition: GeneticMinimizer.h:22
int