ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  };
98 
99  // ---------------------------------------------------------------------------
100 
101  template <class T>
102 
103  class Option : public OptionBase {
104 
105  public:
106 
107  Option( T& ref, const TString& name, const TString& desc ) :
108  OptionBase(name, desc), fRefPtr(&ref) {}
109  virtual ~Option() {}
110 
111  // getters
112  virtual TString GetValue( Int_t i=-1 ) const;
113  virtual const T& Value ( Int_t i=-1 ) const;
114  virtual Bool_t HasPreDefinedVal() const { return (fPreDefs.size()!=0); }
115  virtual Bool_t IsPreDefinedVal( const TString& ) const;
116  virtual Bool_t IsArrayOpt() const { return kFALSE; }
117  virtual Int_t GetArraySize() const { return 0; }
118 
119  // setters
120  virtual void AddPreDefVal(const T&);
121  using OptionBase::Print;
122  virtual void Print ( std::ostream&, Int_t levelofdetail=0 ) const;
123  virtual void PrintPreDefs( std::ostream&, Int_t levelofdetail=0 ) const;
124 
125  protected:
126 
127  T& Value(Int_t=-1);
128 
129  virtual void SetValueLocal( const TString& val, Int_t i=-1 );
130  virtual Bool_t IsPreDefinedValLocal( const T& ) const;
131 
133  std::vector<T> fPreDefs; // templated vector
134  };
135 
136  template<typename T>
137  class Option<T*> : public Option<T> {
138 
139  public:
140 
141  Option( T*& ref, Int_t size, const TString& name, const TString& desc ) :
142  Option<T>(*ref,name, desc), fVRefPtr(&ref), fSize(size) {}
143  virtual ~Option() {}
144 
145  TString GetValue( Int_t i ) const {
146  std::stringstream str;
147  str << std::scientific << Value(i);
148  return str.str();
149  }
150  const T& Value( Int_t i ) const { return (*fVRefPtr)[i]; }
151  virtual Bool_t IsArrayOpt() const { return kTRUE; }
152  virtual Int_t GetArraySize() const { return fSize; }
153 
154  using Option<T>::Print;
155  virtual void Print( std::ostream&, Int_t levelofdetail=0 ) const;
156 
157  virtual Bool_t SetValue( const TString& val, Int_t i=0 );
158 
159  T& Value(Int_t i) { return (*fVRefPtr)[i]; }
162 
163  };
164 
165 } // namespace
166 
167 namespace TMVA {
168 
169  //______________________________________________________________________
170  template<class T>
171  inline const T& TMVA::Option<T>::Value( Int_t ) const {
172  return *fRefPtr;
173  }
174 
175  template<class T>
177  return *fRefPtr;
178  }
179 
180  template<class T>
182  std::stringstream str;
183  str << std::scientific << this->Value();
184  return str.str();
185  }
186 
187  template<>
189  return Value() ? "True" : "False";
190  }
191 
192  template<>
194  return Value(i) ? "True" : "False";
195  }
196 
197  template<class T>
198  inline Bool_t TMVA::Option<T>::IsPreDefinedVal( const TString& val ) const
199  {
200  // template
201  T tmpVal;
202  std::stringstream str(val.Data());
203  str >> tmpVal;
204  return IsPreDefinedValLocal(tmpVal);
205  }
206 
207  template<class T>
209  {
210  // template
211  if (fPreDefs.size()==0) return kTRUE; // if nothing pre-defined then allow everything
212 
213  typename std::vector<T>::const_iterator predefIt;
214  predefIt = fPreDefs.begin();
215  for (;predefIt!=fPreDefs.end(); predefIt++)
216  if ( (*predefIt)==val ) return kTRUE;
217 
218  return kFALSE;
219  }
220 
221  template<>
223  {
224  // template specialization for Bool_t
225  TString tVal(val);
226  tVal.ToLower();
227  if (fPreDefs.size()==0) return kFALSE; // if nothing pre-defined then allow everything
228  Bool_t foundPreDef = kFALSE;
229  std::vector<TString>::const_iterator predefIt;
230  predefIt = fPreDefs.begin();
231  for (;predefIt!=fPreDefs.end(); predefIt++) {
232  TString s(*predefIt);
233  s.ToLower();
234  if (s==tVal) { foundPreDef = kTRUE; break; }
235  }
236  return foundPreDef;
237  }
238 
239  //______________________________________________________________________
240  template<class T>
241  inline void TMVA::Option<T>::AddPreDefVal( const T& val )
242  {
243  // template
244  fPreDefs.push_back(val);
245  }
246 
247  template<>
249  {
250  // template specialization for Bool_t
251  Log() << kFATAL << "<AddPreDefVal> predefined values for Option<Bool_t> don't make sense"
252  << Endl;
253  }
254 
255  template<>
257  {
258  // template specialization for Float_t
259  Log() << kFATAL << "<AddPreDefVal> predefined values for Option<Float_t> don't make sense"
260  << Endl;
261  }
262 
263  template<class T>
264  inline void TMVA::Option<T>::Print( std::ostream& os, Int_t levelofdetail ) const
265  {
266  // template specialization for TString printing
267  os << TheName() << ": " << "\"" << GetValue() << "\"" << " [" << Description() << "]";
268  this->PrintPreDefs(os,levelofdetail);
269  }
270 
271  template<class T>
272  inline void TMVA::Option<T*>::Print( std::ostream& os, Int_t levelofdetail ) const
273  {
274  // template specialization for TString printing
275  for (Int_t i=0; i<fSize; i++) {
276  if (i==0)
277  os << this->TheName() << "[" << i << "]: " << "\"" << this->GetValue(i) << "\"" << " [" << this->Description() << "]";
278  else
279  os << " " << this->TheName() << "[" << i << "]: " << "\"" << this->GetValue(i) << "\"";
280  if (i!=fSize-1) os << std::endl;
281  }
282  this->PrintPreDefs(os,levelofdetail);
283  }
284 
285  //______________________________________________________________________
286  template<class T>
287  inline void TMVA::Option<T>::PrintPreDefs( std::ostream& os, Int_t levelofdetail ) const
288  {
289  // template specialization for TString printing
290  if (HasPreDefinedVal() && levelofdetail>0) {
291  os << std::endl << "PreDefined - possible values are:" << std::endl;
292  typename std::vector<T>::const_iterator predefIt;
293  predefIt = fPreDefs.begin();
294  for (;predefIt!=fPreDefs.end(); predefIt++) {
295  os << " ";
296  os << " - " << (*predefIt) << std::endl;
297  }
298  }
299  }
300 
301  //______________________________________________________________________
302  template<class T>
303  inline Bool_t TMVA::Option<T*>::SetValue( const TString& val, Int_t ind )
304  {
305  // template
306  if (ind >= fSize) return kFALSE;
307  std::stringstream str(val.Data());
308  if (ind < 0) {
309  str >> Value(0);
310  for (Int_t i=1; i<fSize; i++) Value(i) = Value(0);
311  }
312  else {
313  str >> Value(ind);
314  }
315  return kTRUE;
316  }
317 
318  template<class T>
319  inline void TMVA::Option<T>::SetValueLocal( const TString& val, Int_t i )
320  {
321  // template
322  std::stringstream str(val.Data());
323  str >> Value(i);
324  }
325 
326  template<>
328  {
329  // set TString value
330  TString valToSet(val);
331  if (fPreDefs.size()!=0) {
332  TString tVal(val);
333  tVal.ToLower();
334  std::vector<TString>::const_iterator predefIt;
335  predefIt = fPreDefs.begin();
336  for (;predefIt!=fPreDefs.end(); predefIt++) {
337  TString s(*predefIt);
338  s.ToLower();
339  if (s==tVal) { valToSet = *predefIt; break; }
340  }
341  }
342 
343  std::stringstream str(valToSet.Data());
344  str >> Value(-1);
345  }
346 
347  template<>
349  {
350  // set Bool_t value
351  TString valToSet(val);
352  valToSet.ToLower();
353  if (valToSet=="1" || valToSet=="true" || valToSet=="ktrue" || valToSet=="t") {
354  this->Value() = true;
355  }
356  else if (valToSet=="0" || valToSet=="false" || valToSet=="kfalse" || valToSet=="f") {
357  this->Value() = false;
358  }
359  else {
360  Log() << kFATAL << "<SetValueLocal> value \'" << val
361  << "\' can not be interpreted as boolean" << Endl;
362  }
363  }
364 }
365 #endif
virtual Int_t GetArraySize() const
Definition: Option.h:152
virtual Bool_t IsArrayOpt() const
Definition: Option.h:116
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
virtual ~OptionBase()
Definition: Option.h:68
static MsgLogger & Log()
Definition: Option.cxx:59
float Float_t
Definition: RtypesCore.h:53
virtual void Print(std::ostream &, Int_t levelofdetail=0) const =0
const TString & Description() const
Definition: Option.h:76
virtual Bool_t IsArrayOpt() const
Definition: Option.h:151
Option(T *&ref, Int_t size, const TString &name, const TString &desc)
Definition: Option.h:141
Basic string class.
Definition: TString.h:137
virtual Bool_t IsArrayOpt() const =0
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1075
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual Bool_t IsPreDefinedVal(const TString &) const
Definition: Option.h:198
virtual TString GetValue(Int_t i=-1) const =0
virtual void AddPreDefVal(const T &)
Definition: Option.h:241
virtual ~Option()
Definition: Option.h:109
TTree * T
const char * Data() const
Definition: TString.h:349
OptionBase(const TString &name, const TString &desc)
constructor
Definition: Option.cxx:39
Bool_t IsSet() const
Definition: Option.h:74
virtual Int_t GetArraySize() const
Definition: Option.h:117
virtual void SetValueLocal(const TString &val, Int_t i=-1)
Definition: Option.h:319
virtual Bool_t SetValue(const TString &vs, Int_t i=-1)
set value for option
Definition: Option.cxx:52
const TString fName
Definition: Option.h:89
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:594
TString fNameAllLower
Definition: Option.h:90
TString GetValue(Int_t i) const
Definition: Option.h:145
virtual void Print(std::ostream &, Int_t levelofdetail=0) const
Definition: Option.h:264
virtual const char * TheName() const
Definition: Option.h:71
virtual Bool_t HasPreDefinedVal() const =0
virtual Int_t GetArraySize() const =0
T & Value(Int_t i)
Definition: Option.h:159
virtual Bool_t HasPreDefinedVal() const
Definition: Option.h:114
Option(T &ref, const TString &name, const TString &desc)
Definition: Option.h:107
virtual Bool_t IsPreDefinedValLocal(const T &) const
Definition: Option.h:208
const TString fDescription
Definition: Option.h:91
void Print(std::ostream &os, const OptionType &opt)
virtual const T & Value(Int_t i=-1) const
Definition: Option.h:171
virtual Bool_t IsPreDefinedVal(const TString &) const =0
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
virtual ~Option()
Definition: Option.h:143
virtual void PrintPreDefs(std::ostream &, Int_t levelofdetail=0) const
Definition: Option.h:287
Bool_t fIsSet
Definition: Option.h:92
virtual TString GetValue(Int_t i=-1) const
Definition: Option.h:181
const T & Value(Int_t i) const
Definition: Option.h:150
virtual void SetValueLocal(const TString &vs, Int_t i=-1)=0
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual const char * GetName() const
Returns name of object.
Definition: Option.h:70
std::vector< T > fPreDefs
Definition: Option.h:133
Definition: math.cpp:60
const char * Value
Definition: TXMLSetup.cxx:73
T * fRefPtr
Definition: Option.h:132