Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files 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 #include "TObject.h"
45 #include "TString.h"
46 #include "TList.h"
47 #include "TMVA/MsgLogger.h"
48 
49 namespace 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 
91  ClassDef(OptionBase,1);
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 
127  T* fRefPtr;
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]; }
155  T ** fVRefPtr;
156  Int_t fSize;
157 
158  };
159 
160 } // namespace
161 
162 namespace 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>
171  inline T& TMVA::Option<T>::Value( Int_t ) {
172  return *fRefPtr;
173  }
174 
175  template<class T>
176  inline TString TMVA::Option<T>::GetValue( Int_t ) const {
177  std::stringstream str;
178  str << std::scientific << this->Value();
179  return str.str();
180  }
181 
182  template<>
183  inline TString TMVA::Option<Bool_t>::GetValue( Int_t ) const {
184  return Value() ? "True" : "False";
185  }
186 
187  template<>
188  inline TString TMVA::Option<Bool_t*>::GetValue( Int_t i ) const {
189  return Value(i) ? "True" : "False";
190  }
191 
192  template<class T>
193  inline Bool_t TMVA::Option<T>::IsPreDefinedVal( const TString& val ) const
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>
203  inline Bool_t TMVA::Option<T>::IsPreDefinedValLocal(const T& val) const
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<>
217  inline Bool_t TMVA::Option<TString>::IsPreDefinedValLocal( const TString& val ) const
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<>
243  inline void TMVA::Option<Bool_t>::AddPreDefVal( const Bool_t& )
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<>
251  inline void TMVA::Option<Float_t>::AddPreDefVal( const Float_t& )
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>
298  inline Bool_t TMVA::Option<T*>::SetValue( const TString& val, Int_t ind )
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<>
322  inline void TMVA::Option<TString>::SetValueLocal( const TString& val, Int_t )
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<>
343  inline void TMVA::Option<Bool_t>::SetValueLocal( const TString& val, Int_t )
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