Logo ROOT  
Reference Guide
Option.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 : Option *
8 * Web : http://tmva.sourceforge.net *
9 * *
10 * Description: *
11 * Option container *
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 * U. of Victoria, Canada *
21 * MPI-K Heidelberg, Germany *
22 * LAPP, Annecy, France *
23 * *
24 * Redistribution and use in source and binary forms, with or without *
25 * modification, are permitted according to the terms listed in LICENSE *
26 * (http://mva.sourceforge.net/license.txt) *
27 **********************************************************************************/
28
29#ifndef ROOT_TMVA_Option
30#define ROOT_TMVA_Option
31
32//////////////////////////////////////////////////////////////////////////
33// //
34// Option //
35// //
36// Class for TMVA-option handling //
37// //
38//////////////////////////////////////////////////////////////////////////
39
40#include <sstream>
41#include <vector>
42#include <string>
43
44#include "TObject.h"
45#include "TString.h"
46#include "TMVA/MsgLogger.h"
47
48namespace TMVA {
49
50 class Configurable;
51
52 class OptionBase : public TObject {
53
54 public:
55
56 friend class Configurable;
57
58 OptionBase( const TString& name, const TString& desc );
59 virtual ~OptionBase() {}
60
61 virtual const char* GetName() const { return fNameAllLower.Data(); }
62 virtual const char* TheName() const { return fName.Data(); }
63 virtual TString GetValue(Int_t i=-1) const = 0;
64
65 Bool_t IsSet() const { return fIsSet; }
66 virtual Bool_t IsArrayOpt() const = 0;
67 const TString& Description() const { return fDescription; }
68 virtual Bool_t IsPreDefinedVal(const TString&) const = 0;
69 virtual Bool_t HasPreDefinedVal() const = 0;
70 virtual Int_t GetArraySize() const = 0;
71 virtual Bool_t SetValue( const TString& vs, Int_t i=-1 );
72
73 using TObject::Print;
74 virtual void Print( std::ostream&, Int_t levelofdetail=0 ) const = 0;
75
76 private:
77
78 virtual void SetValueLocal(const TString& vs, Int_t i=-1) = 0;
79
80 const TString fName; // name of variable
81 TString fNameAllLower; // name of variable
82 const TString fDescription; // its description
83 Bool_t fIsSet; // set by user ?
84
85 protected:
86
87 static MsgLogger& Log();
88 protected:
89
91 };
92
93 // ---------------------------------------------------------------------------
94
95 template <class T>
96
97 class Option : public OptionBase {
98
99 public:
100
101 Option( T& ref, const TString& name, const TString& desc ) :
102 OptionBase(name, desc), fRefPtr(&ref) {}
103 virtual ~Option() {}
104
105 // getters
106 virtual TString GetValue( Int_t i=-1 ) const;
107 virtual const T& Value ( Int_t i=-1 ) const;
108 virtual Bool_t HasPreDefinedVal() const { return (fPreDefs.size()!=0); }
109 virtual Bool_t IsPreDefinedVal( const TString& ) const;
110 virtual Bool_t IsArrayOpt() const { return kFALSE; }
111 virtual Int_t GetArraySize() const { return 0; }
112
113 // setters
114 virtual void AddPreDefVal(const T&);
115 using OptionBase::Print;
116 virtual void Print ( std::ostream&, Int_t levelofdetail=0 ) const;
117 virtual void PrintPreDefs( std::ostream&, Int_t levelofdetail=0 ) const;
118
119 protected:
120
121 T& Value(Int_t=-1);
122
123 virtual void SetValueLocal( const TString& val, Int_t i=-1 );
124 virtual Bool_t IsPreDefinedValLocal( const T& ) const;
125
127 std::vector<T> fPreDefs; // templated vector
128 };
129
130 template<typename T>
131 class Option<T*> : public Option<T> {
132
133 public:
134
135 Option( T*& ref, Int_t size, const TString& name, const TString& desc ) :
136 Option<T>(*ref,name, desc), fVRefPtr(&ref), fSize(size) {}
137 virtual ~Option() {}
138
139 TString GetValue( Int_t i ) const {
140 std::stringstream str;
141 str << std::scientific << Value(i);
142 return str.str();
143 }
144 const T& Value( Int_t i ) const { return (*fVRefPtr)[i]; }
145 virtual Bool_t IsArrayOpt() const { return kTRUE; }
146 virtual Int_t GetArraySize() const { return fSize; }
147
148 using Option<T>::Print;
149 virtual void Print( std::ostream&, Int_t levelofdetail=0 ) const;
150
151 virtual Bool_t SetValue( const TString& val, Int_t i=0 );
152
153 T& Value(Int_t i) { return (*fVRefPtr)[i]; }
156
157 };
158
159} // namespace
160
161namespace TMVA {
162
163 //______________________________________________________________________
164 template<class T>
165 inline const T& TMVA::Option<T>::Value( Int_t ) const {
166 return *fRefPtr;
167 }
168
169 template<class T>
171 return *fRefPtr;
172 }
173
174 template<class T>
176 std::stringstream str;
177 str << std::scientific << this->Value();
178 return str.str();
179 }
180
181 template<>
183 return Value() ? "True" : "False";
184 }
185
186 template<>
188 return Value(i) ? "True" : "False";
189 }
190
191 template<class T>
193 {
194 // template
195 T tmpVal;
196 std::stringstream str(val.Data());
197 str >> tmpVal;
198 return IsPreDefinedValLocal(tmpVal);
199 }
200
201 template<class T>
203 {
204 // template
205 if (fPreDefs.size()==0) return kTRUE; // if nothing pre-defined then allow everything
206
207 typename std::vector<T>::const_iterator predefIt;
208 predefIt = fPreDefs.begin();
209 for (;predefIt!=fPreDefs.end(); predefIt++)
210 if ( (*predefIt)==val ) return kTRUE;
211
212 return kFALSE;
213 }
214
215 template<>
217 {
218 // template specialization for Bool_t
219 TString tVal(val);
220 tVal.ToLower();
221 if (fPreDefs.size()==0) return kFALSE; // if nothing pre-defined then allow everything
222 Bool_t foundPreDef = kFALSE;
223 std::vector<TString>::const_iterator predefIt;
224 predefIt = fPreDefs.begin();
225 for (;predefIt!=fPreDefs.end(); predefIt++) {
226 TString s(*predefIt);
227 s.ToLower();
228 if (s==tVal) { foundPreDef = kTRUE; break; }
229 }
230 return foundPreDef;
231 }
232
233 //______________________________________________________________________
234 template<class T>
235 inline void TMVA::Option<T>::AddPreDefVal( const T& val )
236 {
237 // template
238 fPreDefs.push_back(val);
239 }
240
241 template<>
243 {
244 // template specialization for Bool_t
245 Log() << kFATAL << "<AddPreDefVal> predefined values for Option<Bool_t> don't make sense"
246 << Endl;
247 }
248
249 template<>
251 {
252 // template specialization for Float_t
253 Log() << kFATAL << "<AddPreDefVal> predefined values for Option<Float_t> don't make sense"
254 << Endl;
255 }
256
257 template<class T>
258 inline void TMVA::Option<T>::Print( std::ostream& os, Int_t levelofdetail ) const
259 {
260 // template specialization for TString printing
261 os << TheName() << ": " << "\"" << GetValue() << "\"" << " [" << Description() << "]";
262 this->PrintPreDefs(os,levelofdetail);
263 }
264
265 template<class T>
266 inline void TMVA::Option<T*>::Print( std::ostream& os, Int_t levelofdetail ) const
267 {
268 // template specialization for TString printing
269 for (Int_t i=0; i<fSize; i++) {
270 if (i==0)
271 os << this->TheName() << "[" << i << "]: " << "\"" << this->GetValue(i) << "\"" << " [" << this->Description() << "]";
272 else
273 os << " " << this->TheName() << "[" << i << "]: " << "\"" << this->GetValue(i) << "\"";
274 if (i!=fSize-1) os << std::endl;
275 }
276 this->PrintPreDefs(os,levelofdetail);
277 }
278
279 //______________________________________________________________________
280 template<class T>
281 inline void TMVA::Option<T>::PrintPreDefs( std::ostream& os, Int_t levelofdetail ) const
282 {
283 // template specialization for TString printing
284 if (HasPreDefinedVal() && levelofdetail>0) {
285 os << std::endl << "PreDefined - possible values are:" << std::endl;
286 typename std::vector<T>::const_iterator predefIt;
287 predefIt = fPreDefs.begin();
288 for (;predefIt!=fPreDefs.end(); predefIt++) {
289 os << " ";
290 os << " - " << (*predefIt) << std::endl;
291 }
292 }
293 }
294
295 //______________________________________________________________________
296 template<class T>
298 {
299 // template
300 if (ind >= fSize) return kFALSE;
301 std::stringstream str(val.Data());
302 if (ind < 0) {
303 str >> Value(0);
304 for (Int_t i=1; i<fSize; i++) Value(i) = Value(0);
305 }
306 else {
307 str >> Value(ind);
308 }
309 return kTRUE;
310 }
311
312 template<class T>
313 inline void TMVA::Option<T>::SetValueLocal( const TString& val, Int_t i )
314 {
315 // template
316 std::stringstream str(val.Data());
317 str >> Value(i);
318 }
319
320 template<>
322 {
323 // set TString value
324 TString valToSet(val);
325 if (fPreDefs.size()!=0) {
326 TString tVal(val);
327 tVal.ToLower();
328 std::vector<TString>::const_iterator predefIt;
329 predefIt = fPreDefs.begin();
330 for (;predefIt!=fPreDefs.end(); predefIt++) {
331 TString s(*predefIt);
332 s.ToLower();
333 if (s==tVal) { valToSet = *predefIt; break; }
334 }
335 }
336
337 std::stringstream str(valToSet.Data());
338 str >> Value(-1);
339 }
340
341 template<>
343 {
344 // set Bool_t value
345 TString valToSet(val);
346 valToSet.ToLower();
347 if (valToSet=="1" || valToSet=="true" || valToSet=="ktrue" || valToSet=="t") {
348 this->Value() = true;
349 }
350 else if (valToSet=="0" || valToSet=="false" || valToSet=="kfalse" || valToSet=="f") {
351 this->Value() = false;
352 }
353 else {
354 Log() << kFATAL << "<SetValueLocal> value \'" << val
355 << "\' can not be interpreted as boolean" << Endl;
356 }
357 }
358}
359#endif
size_t fSize
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Definition: RtypesCore.h:45
const Bool_t kFALSE
Definition: RtypesCore.h:101
bool Bool_t
Definition: RtypesCore.h:63
float Float_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassDef(name, id)
Definition: Rtypes.h:325
char name[80]
Definition: TGX11.cxx:110
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:57
Class for TMVA-option handling.
Definition: Option.h:52
virtual const char * TheName() const
Definition: Option.h:62
OptionBase(const TString &name, const TString &desc)
constructor
Definition: Option.cxx:46
virtual Bool_t SetValue(const TString &vs, Int_t i=-1)
set value for option
Definition: Option.cxx:59
virtual Bool_t IsPreDefinedVal(const TString &) const =0
virtual Bool_t IsArrayOpt() const =0
const TString fDescription
Definition: Option.h:82
virtual ~OptionBase()
Definition: Option.h:59
virtual void SetValueLocal(const TString &vs, Int_t i=-1)=0
Bool_t IsSet() const
Definition: Option.h:65
virtual Bool_t HasPreDefinedVal() const =0
Bool_t fIsSet
Definition: Option.h:83
const TString fName
Definition: Option.h:80
const TString & Description() const
Definition: Option.h:67
virtual const char * GetName() const
Returns name of object.
Definition: Option.h:61
virtual Int_t GetArraySize() const =0
static MsgLogger & Log()
Definition: Option.cxx:66
virtual void Print(std::ostream &, Int_t levelofdetail=0) const =0
virtual TString GetValue(Int_t i=-1) const =0
TString fNameAllLower
Definition: Option.h:81
virtual ~Option()
Definition: Option.h:137
T & Value(Int_t i)
Definition: Option.h:153
Option(T *&ref, Int_t size, const TString &name, const TString &desc)
Definition: Option.h:135
TString GetValue(Int_t i) const
Definition: Option.h:139
virtual Int_t GetArraySize() const
Definition: Option.h:146
const T & Value(Int_t i) const
Definition: Option.h:144
virtual Bool_t IsArrayOpt() const
Definition: Option.h:145
virtual void PrintPreDefs(std::ostream &, Int_t levelofdetail=0) const
Definition: Option.h:281
virtual Bool_t HasPreDefinedVal() const
Definition: Option.h:108
virtual void SetValueLocal(const TString &val, Int_t i=-1)
Definition: Option.h:313
virtual Bool_t IsPreDefinedVal(const TString &) const
Definition: Option.h:192
virtual TString GetValue(Int_t i=-1) const
Definition: Option.h:175
virtual ~Option()
Definition: Option.h:103
std::vector< T > fPreDefs
Definition: Option.h:127
virtual void AddPreDefVal(const T &)
Definition: Option.h:235
T * fRefPtr
Definition: Option.h:126
virtual void Print(std::ostream &, Int_t levelofdetail=0) const
Definition: Option.h:258
Option(T &ref, const TString &name, const TString &desc)
Definition: Option.h:101
virtual Int_t GetArraySize() const
Definition: Option.h:111
virtual Bool_t IsArrayOpt() const
Definition: Option.h:110
virtual const T & Value(Int_t i=-1) const
Definition: Option.h:165
virtual Bool_t IsPreDefinedValLocal(const T &) const
Definition: Option.h:202
@ kFATAL
Definition: Types.h:61
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:552
Basic string class.
Definition: TString.h:136
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1150
const char * Data() const
Definition: TString.h:369
double T(double x)
Definition: ChebyshevPol.h:34
void Print(std::ostream &os, const OptionType &opt)
static constexpr double s
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:148
Double_t Log(Double_t x)
Definition: TMath.h:710
const char * Value
Definition: TXMLSetup.cxx:73