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