Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 * *
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 * (see tmva/doc/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
43namespace TMVA {
44
45 class Configurable : public TNamed {
46
47 public:
48
49 // constructor
50 Configurable( const TString& theOption = "" );
51
52 // default destructor
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); }
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//______________________________________________________________________
146template <class T>
148{
149 // set the reference for an option
150 OptionBase* o = new Option<T>(ref, name, desc);
153 return o;
154}
155
156template <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//______________________________________________________________________
167template<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) oc->AddPreDefVal(val);
173}
174
175//______________________________________________________________________
176template<class T>
177void 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){
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 properly.. 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//______________________________________________________________________
203template <class T>
204void 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
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
#define ClassDef(name, id)
Definition Rtypes.h:342
char name[80]
Definition TGX11.cxx:110
Iterator of linked list.
Definition TList.h:193
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:83
void SetConfigDescription(const char *d)
const TString & GetReferenceFile() const
void ReadOptionsFromXML(void *node)
Bool_t LooseOptionCheckingEnabled() const
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
TString fReferenceFile
reference file for options writing
void AddPreDefVal(const T &)
OptionBase * fLastDeclaredOption
! last declared option
void AssignOpt(const TString &name, T &valAssign) const
void SetConfigName(const char *n)
void ResetSetFlag()
resets the IsSet flag for all declare options to be called before options are read from stream
const char * GetConfigName() const
const char * GetConfigDescription() const
virtual ~Configurable()
default destructor
OptionBase * DeclareOptionRef(T *&ref, Int_t size, const TString &name, const TString &desc="")
void WriteOptionsReferenceToFile()
write complete options to output stream
void WriteOptionsToStream(std::ostream &o, const TString &prefix) const
write options to output stream (e.g. in writing the MVA weight files
TString fConfigDescription
description of this configurable
TList fListOfOptions
option list
virtual void ParseOptions()
options parser
const TString & GetOptions() const
MsgLogger & Log() const
MsgLogger * fLogger
! message logger
TString fOptions
options string
void SetMsgType(EMsgType t)
void PrintOptions() const
prints out the options set in the options string and the defaults
void CheckForUnusedOptions() const
checks for unused options in option string
void SplitOptions(const TString &theOpt, TList &loo) const
splits the option string at ':' and fills the list 'loo' with the primitive strings
Bool_t fLooseOptionCheckingEnabled
checker for option string
void ReadOptionsFromStream(std::istream &istr)
read option back from the weight file
void EnableLooseOptions(Bool_t b=kTRUE)
void AddOptionsXMLTo(void *parent) const
write options to XML file
void SetOptions(const TString &s)
ostringstream derivative to redirect and format output
Definition MsgLogger.h:57
void SetMinType(EMsgType minType)
Definition MsgLogger.h:70
Class for TMVA-option handling.
Definition Option.h:52
virtual void AddPreDefVal(const T &)
Definition Option.h:235
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
Mother of all ROOT objects.
Definition TObject.h:41
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:408
Basic string class.
Definition TString.h:139
const Int_t n
Definition legend1.C:16
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148