29 #ifndef ROOT_TMVA_Option
30 #define ROOT_TMVA_Option
53 class OptionBase :
public TObject {
57 friend class Configurable;
59 OptionBase(
const TString& name,
const TString& desc );
60 virtual ~OptionBase() {}
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;
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 );
75 virtual void Print( std::ostream&, Int_t levelofdetail=0 )
const = 0;
79 virtual void SetValueLocal(
const TString& vs, Int_t i=-1) = 0;
82 TString fNameAllLower;
83 const TString fDescription;
88 static MsgLogger& Log();
91 ClassDef(OptionBase,1);
98 class Option :
public OptionBase {
102 Option( T& ref,
const TString& name,
const TString& desc ) :
103 OptionBase(name, desc), fRefPtr(&ref) {}
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; }
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;
124 virtual void SetValueLocal(
const TString& val, Int_t i=-1 );
125 virtual Bool_t IsPreDefinedValLocal(
const T& )
const;
128 std::vector<T> fPreDefs;
132 class Option<T*> :
public Option<T> {
136 Option( T*& ref, Int_t size,
const TString& name,
const TString& desc ) :
137 Option<T>(*ref,name, desc), fVRefPtr(&ref), fSize(size) {}
140 TString GetValue( Int_t i )
const {
141 std::stringstream str;
142 str << std::scientific << Value(i);
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; }
149 using Option<T>::Print;
150 virtual void Print( std::ostream&, Int_t levelofdetail=0 )
const;
152 virtual Bool_t SetValue(
const TString& val, Int_t i=0 );
154 T& Value(Int_t i) {
return (*fVRefPtr)[i]; }
166 inline const T& TMVA::Option<T>::Value( Int_t )
const {
171 inline T& TMVA::Option<T>::Value( Int_t ) {
176 inline TString TMVA::Option<T>::GetValue( Int_t )
const {
177 std::stringstream str;
178 str << std::scientific << this->Value();
183 inline TString TMVA::Option<Bool_t>::GetValue( Int_t )
const {
184 return Value() ?
"True" :
"False";
188 inline TString TMVA::Option<Bool_t*>::GetValue( Int_t i )
const {
189 return Value(i) ?
"True" :
"False";
193 inline Bool_t TMVA::Option<T>::IsPreDefinedVal(
const TString& val )
const
197 std::stringstream str(val.Data());
199 return IsPreDefinedValLocal(tmpVal);
203 inline Bool_t TMVA::Option<T>::IsPreDefinedValLocal(
const T& val)
const
206 if (fPreDefs.size()==0)
return kTRUE;
208 typename std::vector<T>::const_iterator predefIt;
209 predefIt = fPreDefs.begin();
210 for (;predefIt!=fPreDefs.end(); predefIt++)
211 if ( (*predefIt)==val )
return kTRUE;
217 inline Bool_t TMVA::Option<TString>::IsPreDefinedValLocal(
const TString& val )
const
222 if (fPreDefs.size()==0)
return kFALSE;
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);
229 if (s==tVal) { foundPreDef = kTRUE;
break; }
236 inline void TMVA::Option<T>::AddPreDefVal(
const T& val )
239 fPreDefs.push_back(val);
243 inline void TMVA::Option<Bool_t>::AddPreDefVal(
const Bool_t& )
246 Log() << kFATAL <<
"<AddPreDefVal> predefined values for Option<Bool_t> don't make sense"
251 inline void TMVA::Option<Float_t>::AddPreDefVal(
const Float_t& )
254 Log() << kFATAL <<
"<AddPreDefVal> predefined values for Option<Float_t> don't make sense"
259 inline void TMVA::Option<T>::Print( std::ostream& os, Int_t levelofdetail )
const
262 os << TheName() <<
": " <<
"\"" << GetValue() <<
"\"" <<
" [" << Description() <<
"]";
263 this->PrintPreDefs(os,levelofdetail);
267 inline void TMVA::Option<T*>::Print( std::ostream& os, Int_t levelofdetail )
const
270 for (Int_t i=0; i<fSize; i++) {
272 os << this->TheName() <<
"[" << i <<
"]: " <<
"\"" << this->GetValue(i) <<
"\"" <<
" [" << this->Description() <<
"]";
274 os <<
" " << this->TheName() <<
"[" << i <<
"]: " <<
"\"" << this->GetValue(i) <<
"\"";
275 if (i!=fSize-1) os << std::endl;
277 this->PrintPreDefs(os,levelofdetail);
282 inline void TMVA::Option<T>::PrintPreDefs( std::ostream& os, Int_t levelofdetail )
const
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++) {
291 os <<
" - " << (*predefIt) << std::endl;
298 inline Bool_t TMVA::Option<T*>::SetValue(
const TString& val, Int_t ind )
301 if (ind >= fSize)
return kFALSE;
302 std::stringstream str(val.Data());
305 for (Int_t i=1; i<fSize; i++) Value(i) = Value(0);
314 inline void TMVA::Option<T>::SetValueLocal(
const TString& val, Int_t i )
317 std::stringstream str(val.Data());
322 inline void TMVA::Option<TString>::SetValueLocal(
const TString& val, Int_t )
325 TString valToSet(val);
326 if (fPreDefs.size()!=0) {
329 std::vector<TString>::const_iterator predefIt;
330 predefIt = fPreDefs.begin();
331 for (;predefIt!=fPreDefs.end(); predefIt++) {
332 TString s(*predefIt);
334 if (s==tVal) { valToSet = *predefIt;
break; }
338 std::stringstream str(valToSet.Data());
343 inline void TMVA::Option<Bool_t>::SetValueLocal(
const TString& val, Int_t )
346 TString valToSet(val);
348 if (valToSet==
"1" || valToSet==
"true" || valToSet==
"ktrue" || valToSet==
"t") {
349 this->Value() =
true;
351 else if (valToSet==
"0" || valToSet==
"false" || valToSet==
"kfalse" || valToSet==
"f") {
352 this->Value() =
false;
355 Log() << kFATAL <<
"<SetValueLocal> value \'" << val
356 <<
"\' can not be interpreted as boolean" << Endl;