00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef ROOT_TMVA_Option
00030 #define ROOT_TMVA_Option
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <iomanip>
00041 #include <sstream>
00042 #include <vector>
00043
00044 #ifndef ROOT_TObject
00045 #include "TObject.h"
00046 #endif
00047 #ifndef ROOT_TString
00048 #include "TString.h"
00049 #endif
00050 #ifndef ROOT_TList
00051 #include "TList.h"
00052 #endif
00053 #ifndef ROOT_TMVA_MsgLogger
00054 #include "TMVA/MsgLogger.h"
00055 #endif
00056
00057 namespace TMVA {
00058
00059 class Configurable;
00060
00061 class OptionBase : public TObject {
00062
00063 public:
00064
00065 friend class Configurable;
00066
00067 OptionBase( const TString& name, const TString& desc );
00068 virtual ~OptionBase() {}
00069
00070 virtual const char* GetName() const { return fNameAllLower.Data(); }
00071 virtual const char* TheName() const { return fName.Data(); }
00072 virtual TString GetValue(Int_t i=-1) const = 0;
00073
00074 Bool_t IsSet() const { return fIsSet; }
00075 virtual Bool_t IsArrayOpt() const = 0;
00076 const TString& Description() const { return fDescription; }
00077 virtual Bool_t IsPreDefinedVal(const TString&) const = 0;
00078 virtual Bool_t HasPreDefinedVal() const = 0;
00079 virtual Int_t GetArraySize() const = 0;
00080 virtual Bool_t SetValue( const TString& vs, Int_t i=-1 );
00081
00082 using TObject::Print;
00083 virtual void Print( ostream&, Int_t levelofdetail=0 ) const = 0;
00084
00085 private:
00086
00087 virtual void SetValueLocal(const TString& vs, Int_t i=-1) = 0;
00088
00089 const TString fName;
00090 TString fNameAllLower;
00091 const TString fDescription;
00092 Bool_t fIsSet;
00093
00094 protected:
00095
00096 static MsgLogger* fgLogger;
00097
00098 };
00099
00100
00101
00102 template <class T>
00103
00104 class Option : public OptionBase {
00105
00106 public:
00107
00108 Option( T& ref, const TString& name, const TString& desc ) :
00109 OptionBase(name, desc), fRefPtr(&ref) {}
00110 virtual ~Option() {}
00111
00112
00113 virtual TString GetValue( Int_t i=-1 ) const;
00114 virtual const T& Value ( Int_t i=-1 ) const;
00115 virtual Bool_t HasPreDefinedVal() const { return (fPreDefs.size()!=0); }
00116 virtual Bool_t IsPreDefinedVal( const TString& ) const;
00117 virtual Bool_t IsArrayOpt() const { return kFALSE; }
00118 virtual Int_t GetArraySize() const { return 0; }
00119
00120
00121 virtual void AddPreDefVal(const T&);
00122 using OptionBase::Print;
00123 virtual void Print ( ostream&, Int_t levelofdetail=0 ) const;
00124 virtual void PrintPreDefs( ostream&, Int_t levelofdetail=0 ) const;
00125
00126 protected:
00127
00128 T& Value(Int_t=-1);
00129
00130 virtual void SetValueLocal( const TString& val, Int_t i=-1 );
00131 virtual Bool_t IsPreDefinedValLocal( const T& ) const;
00132
00133 T* fRefPtr;
00134 std::vector<T> fPreDefs;
00135 };
00136
00137 template<typename T>
00138 class Option<T*> : public Option<T> {
00139
00140 public:
00141
00142 Option( T*& ref, Int_t size, const TString& name, const TString& desc ) :
00143 Option<T>(*ref,name, desc), fVRefPtr(&ref), fSize(size) {}
00144 virtual ~Option() {}
00145
00146 TString GetValue( Int_t i ) const {
00147 std::stringstream str;
00148 str << std::scientific << Value(i);
00149 return str.str();
00150 }
00151 const T& Value( Int_t i ) const { return (*fVRefPtr)[i]; }
00152 virtual Bool_t IsArrayOpt() const { return kTRUE; }
00153 virtual Int_t GetArraySize() const { return fSize; }
00154
00155 using Option<T>::Print;
00156 virtual void Print( ostream&, Int_t levelofdetail=0 ) const;
00157
00158 virtual Bool_t SetValue( const TString& val, Int_t i=0 );
00159
00160 T& Value(Int_t i) { return (*fVRefPtr)[i]; }
00161 T ** fVRefPtr;
00162 Int_t fSize;
00163
00164 };
00165
00166 }
00167
00168 namespace TMVA {
00169
00170
00171 template<class T>
00172 inline const T& TMVA::Option<T>::Value( Int_t ) const {
00173 return *fRefPtr;
00174 }
00175
00176 template<class T>
00177 inline T& TMVA::Option<T>::Value( Int_t ) {
00178 return *fRefPtr;
00179 }
00180
00181 template<class T>
00182 inline TString TMVA::Option<T>::GetValue( Int_t ) const {
00183 std::stringstream str;
00184 str << std::scientific << this->Value();
00185 return str.str();
00186 }
00187
00188 template<>
00189 inline TString TMVA::Option<Bool_t>::GetValue( Int_t ) const {
00190 return Value() ? "True" : "False";
00191 }
00192
00193 template<>
00194 inline TString TMVA::Option<Bool_t*>::GetValue( Int_t i ) const {
00195 return Value(i) ? "True" : "False";
00196 }
00197
00198 template<class T>
00199 inline Bool_t TMVA::Option<T>::IsPreDefinedVal( const TString& val ) const
00200 {
00201
00202 T tmpVal;
00203 std::stringstream str(val.Data());
00204 str >> tmpVal;
00205 return IsPreDefinedValLocal(tmpVal);
00206 }
00207
00208 template<class T>
00209 inline Bool_t TMVA::Option<T>::IsPreDefinedValLocal(const T& val) const
00210 {
00211
00212 if (fPreDefs.size()==0) return kTRUE;
00213
00214 typename std::vector<T>::const_iterator predefIt;
00215 predefIt = fPreDefs.begin();
00216 for (;predefIt!=fPreDefs.end(); predefIt++)
00217 if ( (*predefIt)==val ) return kTRUE;
00218
00219 return kFALSE;
00220 }
00221
00222 template<>
00223 inline Bool_t TMVA::Option<TString>::IsPreDefinedValLocal( const TString& val ) const
00224 {
00225
00226 TString tVal(val);
00227 tVal.ToLower();
00228 if (fPreDefs.size()==0) return kFALSE;
00229 Bool_t foundPreDef = kFALSE;
00230 std::vector<TString>::const_iterator predefIt;
00231 predefIt = fPreDefs.begin();
00232 for (;predefIt!=fPreDefs.end(); predefIt++) {
00233 TString s(*predefIt);
00234 s.ToLower();
00235 if (s==tVal) { foundPreDef = kTRUE; break; }
00236 }
00237 return foundPreDef;
00238 }
00239
00240
00241 template<class T>
00242 inline void TMVA::Option<T>::AddPreDefVal( const T& val )
00243 {
00244
00245 fPreDefs.push_back(val);
00246 }
00247
00248 template<>
00249 inline void TMVA::Option<Bool_t>::AddPreDefVal( const Bool_t& )
00250 {
00251
00252 *fgLogger << kFATAL << "<AddPreDefVal> predefined values for Option<Bool_t> don't make sense"
00253 << Endl;
00254 }
00255
00256 template<>
00257 inline void TMVA::Option<Float_t>::AddPreDefVal( const Float_t& )
00258 {
00259
00260 *fgLogger << kFATAL << "<AddPreDefVal> predefined values for Option<Float_t> don't make sense"
00261 << Endl;
00262 }
00263
00264 template<class T>
00265 inline void TMVA::Option<T>::Print( ostream& os, Int_t levelofdetail ) const
00266 {
00267
00268 os << TheName() << ": " << "\"" << GetValue() << "\"" << " [" << Description() << "]";
00269 this->PrintPreDefs(os,levelofdetail);
00270 }
00271
00272 template<class T>
00273 inline void TMVA::Option<T*>::Print( ostream& os, Int_t levelofdetail ) const
00274 {
00275
00276 for (Int_t i=0; i<fSize; i++) {
00277 if (i==0)
00278 os << this->TheName() << "[" << i << "]: " << "\"" << this->GetValue(i) << "\"" << " [" << this->Description() << "]";
00279 else
00280 os << " " << this->TheName() << "[" << i << "]: " << "\"" << this->GetValue(i) << "\"";
00281 if (i!=fSize-1) os << std::endl;
00282 }
00283 this->PrintPreDefs(os,levelofdetail);
00284 }
00285
00286
00287 template<class T>
00288 inline void TMVA::Option<T>::PrintPreDefs( ostream& os, Int_t levelofdetail ) const
00289 {
00290
00291 if (HasPreDefinedVal() && levelofdetail>0) {
00292 os << std::endl << "PreDefined - possible values are:" << std::endl;
00293 typename std::vector<T>::const_iterator predefIt;
00294 predefIt = fPreDefs.begin();
00295 for (;predefIt!=fPreDefs.end(); predefIt++) {
00296 os << " ";
00297 os << " - " << (*predefIt) << std::endl;
00298 }
00299 }
00300 }
00301
00302
00303 template<class T>
00304 inline Bool_t TMVA::Option<T*>::SetValue( const TString& val, Int_t ind )
00305 {
00306
00307 if (ind >= fSize) return kFALSE;
00308 std::stringstream str(val.Data());
00309 if (ind < 0) {
00310 str >> Value(0);
00311 for (Int_t i=1; i<fSize; i++) Value(i) = Value(0);
00312 }
00313 else {
00314 str >> Value(ind);
00315 }
00316 return kTRUE;
00317 }
00318
00319 template<class T>
00320 inline void TMVA::Option<T>::SetValueLocal( const TString& val, Int_t i )
00321 {
00322
00323 std::stringstream str(val.Data());
00324 str >> Value(i);
00325 }
00326
00327 template<>
00328 inline void TMVA::Option<TString>::SetValueLocal( const TString& val, Int_t )
00329 {
00330
00331 TString valToSet(val);
00332 if (fPreDefs.size()!=0) {
00333 TString tVal(val);
00334 tVal.ToLower();
00335 std::vector<TString>::const_iterator predefIt;
00336 predefIt = fPreDefs.begin();
00337 for (;predefIt!=fPreDefs.end(); predefIt++) {
00338 TString s(*predefIt);
00339 s.ToLower();
00340 if (s==tVal) { valToSet = *predefIt; break; }
00341 }
00342 }
00343
00344 std::stringstream str(valToSet.Data());
00345 str >> Value(-1);
00346 }
00347
00348 template<>
00349 inline void TMVA::Option<Bool_t>::SetValueLocal( const TString& val, Int_t )
00350 {
00351
00352 TString valToSet(val);
00353 valToSet.ToLower();
00354 if (valToSet=="1" || valToSet=="true" || valToSet=="ktrue" || valToSet=="t") {
00355 this->Value() = true;
00356 }
00357 else if (valToSet=="0" || valToSet=="false" || valToSet=="kfalse" || valToSet=="f") {
00358 this->Value() = false;
00359 }
00360 else {
00361 *fgLogger << kFATAL << "<SetValueLocal> value \'" << val
00362 << "\' can not be interpreted as boolean" << Endl;
00363 }
00364 }
00365 }
00366 #endif