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 <iomanip>
41#include <sstream>
42#include <vector>
43
44#include "TObject.h"
45#include "TString.h"
46#include "TList.h"
47#include "TMVA/MsgLogger.h"
48
49namespace TMVA {
50
51 class Configurable;
52
53 class OptionBase : public TObject {
54
55 public:
56
57 friend class Configurable;
58
59 OptionBase( const TString& name, const TString& desc );
60 virtual ~OptionBase() {}
61
62 virtual const char* GetName() const { return fNameAllLower.Data(); }
63 virtual const char* TheName() const { return fName.Data(); }
64 virtual TString GetValue(Int_t i=-1) const = 0;
65
66 Bool_t IsSet() const { return fIsSet; }
67 virtual Bool_t IsArrayOpt() const = 0;
68 const TString& Description() const { return fDescription; }
69 virtual Bool_t IsPreDefinedVal(const TString&) const = 0;
70 virtual Bool_t HasPreDefinedVal() const = 0;
71 virtual Int_t GetArraySize() const = 0;
72 virtual Bool_t SetValue( const TString& vs, Int_t i=-1 );
73
74 using TObject::Print;
75 virtual void Print( std::ostream&, Int_t levelofdetail=0 ) const = 0;
76
77 private:
78
79 virtual void SetValueLocal(const TString& vs, Int_t i=-1) = 0;
80
81 const TString fName; // name of variable
82 TString fNameAllLower; // name of variable
83 const TString fDescription; // its description
84 Bool_t fIsSet; // set by user ?
85
86 protected:
87
88 static MsgLogger& Log();
89 protected:
90
92 };
93
94 // ---------------------------------------------------------------------------
95
96 template <class T>
97
98 class Option : public OptionBase {
99
100 public:
101
102 Option( T& ref, const TString& name, const TString& desc ) :
103 OptionBase(name, desc), fRefPtr(&ref) {}
104 virtual ~Option() {}
105
106 // getters
107 virtual TString GetValue( Int_t i=-1 ) const;
108 virtual const T& Value ( Int_t i=-1 ) const;
109 virtual Bool_t HasPreDefinedVal() const { return (fPreDefs.size()!=0); }
110 virtual Bool_t IsPreDefinedVal( const TString& ) const;
111 virtual Bool_t IsArrayOpt() const { return kFALSE; }
112 virtual Int_t GetArraySize() const { return 0; }
113
114 // setters
115 virtual void AddPreDefVal(const T&);
116 using OptionBase::Print;
117 virtual void Print ( std::ostream&, Int_t levelofdetail=0 ) const;
118 virtual void PrintPreDefs( std::ostream&, Int_t levelofdetail=0 ) const;
119
120 protected:
121
122 T& Value(Int_t=-1);
123
124 virtual void SetValueLocal( const TString& val, Int_t i=-1 );
125 virtual Bool_t IsPreDefinedValLocal( const T& ) const;
126
128 std::vector<T> fPreDefs; // templated vector
129 };
130
131 template<typename T>
132 class Option<T*> : public Option<T> {
133
134 public:
135
136 Option( T*& ref, Int_t size, const TString& name, const TString& desc ) :
137 Option<T>(*ref,name, desc), fVRefPtr(&ref), fSize(size) {}
138 virtual ~Option() {}
139
140 TString GetValue( Int_t i ) const {
141 std::stringstream str;
142 str << std::scientific << Value(i);
143 return str.str();
144 }
145 const T& Value( Int_t i ) const { return (*fVRefPtr)[i]; }
146 virtual Bool_t IsArrayOpt() const { return kTRUE; }
147 virtual Int_t GetArraySize() const { return fSize; }
148
149 using Option<T>::Print;
150 virtual void Print( std::ostream&, Int_t levelofdetail=0 ) const;
151
152 virtual Bool_t SetValue( const TString& val, Int_t i=0 );
153
154 T& Value(Int_t i) { return (*fVRefPtr)[i]; }
157
158 };
159
160} // namespace
161
162namespace TMVA {
163
164 //______________________________________________________________________
165 template<class T>
166 inline const T& TMVA::Option<T>::Value( Int_t ) const {
167 return *fRefPtr;
168 }
169
170 template<class T>
172 return *fRefPtr;
173 }
174
175 template<class T>
177 std::stringstream str;
178 str << std::scientific << this->Value();
179 return str.str();
180 }
181
182 template<>
184 return Value() ? "True" : "False";
185 }
186
187 template<>
189 return Value(i) ? "True" : "False";
190 }
191
192 template<class T>
194 {
195 // template
196 T tmpVal;
197 std::stringstream str(val.Data());
198 str >> tmpVal;
199 return IsPreDefinedValLocal(tmpVal);
200 }
201
202 template<class T>
204 {
205 // template
206 if (fPreDefs.size()==0) return kTRUE; // if nothing pre-defined then allow everything
207
208 typename std::vector<T>::const_iterator predefIt;
209 predefIt = fPreDefs.begin();
210 for (;predefIt!=fPreDefs.end(); predefIt++)
211 if ( (*predefIt)==val ) return kTRUE;
212
213 return kFALSE;
214 }
215
216 template<>
218 {
219 // template specialization for Bool_t
220 TString tVal(val);
221 tVal.ToLower();
222 if (fPreDefs.size()==0) return kFALSE; // if nothing pre-defined then allow everything
223 Bool_t foundPreDef = kFALSE;
224 std::vector<TString>::const_iterator predefIt;
225 predefIt = fPreDefs.begin();
226 for (;predefIt!=fPreDefs.end(); predefIt++) {
227 TString s(*predefIt);
228 s.ToLower();
229 if (s==tVal) { foundPreDef = kTRUE; break; }
230 }
231 return foundPreDef;
232 }
233
234 //______________________________________________________________________
235 template<class T>
236 inline void TMVA::Option<T>::AddPreDefVal( const T& val )
237 {
238 // template
239 fPreDefs.push_back(val);
240 }
241
242 template<>
244 {
245 // template specialization for Bool_t
246 Log() << kFATAL << "<AddPreDefVal> predefined values for Option<Bool_t> don't make sense"
247 << Endl;
248 }
249
250 template<>
252 {
253 // template specialization for Float_t
254 Log() << kFATAL << "<AddPreDefVal> predefined values for Option<Float_t> don't make sense"
255 << Endl;
256 }
257
258 template<class T>
259 inline void TMVA::Option<T>::Print( std::ostream& os, Int_t levelofdetail ) const
260 {
261 // template specialization for TString printing
262 os << TheName() << ": " << "\"" << GetValue() << "\"" << " [" << Description() << "]";
263 this->PrintPreDefs(os,levelofdetail);
264 }
265
266 template<class T>
267 inline void TMVA::Option<T*>::Print( std::ostream& os, Int_t levelofdetail ) const
268 {
269 // template specialization for TString printing
270 for (Int_t i=0; i<fSize; i++) {
271 if (i==0)
272 os << this->TheName() << "[" << i << "]: " << "\"" << this->GetValue(i) << "\"" << " [" << this->Description() << "]";
273 else
274 os << " " << this->TheName() << "[" << i << "]: " << "\"" << this->GetValue(i) << "\"";
275 if (i!=fSize-1) os << std::endl;
276 }
277 this->PrintPreDefs(os,levelofdetail);
278 }
279
280 //______________________________________________________________________
281 template<class T>
282 inline void TMVA::Option<T>::PrintPreDefs( std::ostream& os, Int_t levelofdetail ) const
283 {
284 // template specialization for TString printing
285 if (HasPreDefinedVal() && levelofdetail>0) {
286 os << std::endl << "PreDefined - possible values are:" << std::endl;
287 typename std::vector<T>::const_iterator predefIt;
288 predefIt = fPreDefs.begin();
289 for (;predefIt!=fPreDefs.end(); predefIt++) {
290 os << " ";
291 os << " - " << (*predefIt) << std::endl;
292 }
293 }
294 }
295
296 //______________________________________________________________________
297 template<class T>
299 {
300 // template
301 if (ind >= fSize) return kFALSE;
302 std::stringstream str(val.Data());
303 if (ind < 0) {
304 str >> Value(0);
305 for (Int_t i=1; i<fSize; i++) Value(i) = Value(0);
306 }
307 else {
308 str >> Value(ind);
309 }
310 return kTRUE;
311 }
312
313 template<class T>
314 inline void TMVA::Option<T>::SetValueLocal( const TString& val, Int_t i )
315 {
316 // template
317 std::stringstream str(val.Data());
318 str >> Value(i);
319 }
320
321 template<>
323 {
324 // set TString value
325 TString valToSet(val);
326 if (fPreDefs.size()!=0) {
327 TString tVal(val);
328 tVal.ToLower();
329 std::vector<TString>::const_iterator predefIt;
330 predefIt = fPreDefs.begin();
331 for (;predefIt!=fPreDefs.end(); predefIt++) {
332 TString s(*predefIt);
333 s.ToLower();
334 if (s==tVal) { valToSet = *predefIt; break; }
335 }
336 }
337
338 std::stringstream str(valToSet.Data());
339 str >> Value(-1);
340 }
341
342 template<>
344 {
345 // set Bool_t value
346 TString valToSet(val);
347 valToSet.ToLower();
348 if (valToSet=="1" || valToSet=="true" || valToSet=="ktrue" || valToSet=="t") {
349 this->Value() = true;
350 }
351 else if (valToSet=="0" || valToSet=="false" || valToSet=="kfalse" || valToSet=="f") {
352 this->Value() = false;
353 }
354 else {
355 Log() << kFATAL << "<SetValueLocal> value \'" << val
356 << "\' can not be interpreted as boolean" << Endl;
357 }
358 }
359}
360#endif
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
float Float_t
Definition: RtypesCore.h:53
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassDef(name, id)
Definition: Rtypes.h:326
char name[80]
Definition: TGX11.cxx:109
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
Class for TMVA-option handling.
Definition: Option.h:53
virtual const char * TheName() const
Definition: Option.h:63
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:83
virtual ~OptionBase()
Definition: Option.h:60
virtual void SetValueLocal(const TString &vs, Int_t i=-1)=0
Bool_t IsSet() const
Definition: Option.h:66
virtual Bool_t HasPreDefinedVal() const =0
Bool_t fIsSet
Definition: Option.h:84
const TString fName
Definition: Option.h:81
const TString & Description() const
Definition: Option.h:68
virtual const char * GetName() const
Returns name of object.
Definition: Option.h:62
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:82
virtual ~Option()
Definition: Option.h:138
T & Value(Int_t i)
Definition: Option.h:154
Option(T *&ref, Int_t size, const TString &name, const TString &desc)
Definition: Option.h:136
TString GetValue(Int_t i) const
Definition: Option.h:140
virtual Int_t GetArraySize() const
Definition: Option.h:147
const T & Value(Int_t i) const
Definition: Option.h:145
virtual Bool_t IsArrayOpt() const
Definition: Option.h:146
virtual void PrintPreDefs(std::ostream &, Int_t levelofdetail=0) const
Definition: Option.h:282
virtual Bool_t HasPreDefinedVal() const
Definition: Option.h:109
virtual void SetValueLocal(const TString &val, Int_t i=-1)
Definition: Option.h:314
virtual Bool_t IsPreDefinedVal(const TString &) const
Definition: Option.h:193
virtual TString GetValue(Int_t i=-1) const
Definition: Option.h:176
virtual ~Option()
Definition: Option.h:104
std::vector< T > fPreDefs
Definition: Option.h:128
virtual void AddPreDefVal(const T &)
Definition: Option.h:236
T * fRefPtr
Definition: Option.h:127
virtual void Print(std::ostream &, Int_t levelofdetail=0) const
Definition: Option.h:259
Option(T &ref, const TString &name, const TString &desc)
Definition: Option.h:102
virtual Int_t GetArraySize() const
Definition: Option.h:112
virtual Bool_t IsArrayOpt() const
Definition: Option.h:111
virtual const T & Value(Int_t i=-1) const
Definition: Option.h:166
virtual Bool_t IsPreDefinedValLocal(const T &) const
Definition: Option.h:203
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:550
Basic string class.
Definition: TString.h:131
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1125
const char * Data() const
Definition: TString.h:364
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:158
Double_t Log(Double_t x)
Definition: TMath.h:750
const char * Value
Definition: TXMLSetup.cxx:72