Option.h

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: Option.h 34301 2010-07-02 11:32:29Z stelzer $   
00002 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss
00003 
00004 /**********************************************************************************
00005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
00006  * Package: TMVA                                                                  *
00007  * Class  : Option                                                                *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      Option container                                                          *
00012  *                                                                                *
00013  * Authors (alphabetical):                                                        *
00014  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
00015  *      Joerg Stelzer   <Joerg.Stelzer@cern.ch>  - CERN, Switzerland              *
00016  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
00017  *                                                                                *
00018  * Copyright (c) 2005:                                                            *
00019  *      CERN, Switzerland                                                         * 
00020  *      U. of Victoria, Canada                                                    * 
00021  *      MPI-K Heidelberg, Germany                                                 * 
00022  *      LAPP, Annecy, France                                                      *
00023  *                                                                                *
00024  * Redistribution and use in source and binary forms, with or without             *
00025  * modification, are permitted according to the terms listed in LICENSE           *
00026  * (http://mva.sourceforge.net/license.txt)                                       *
00027  **********************************************************************************/
00028 
00029 #ifndef ROOT_TMVA_Option
00030 #define ROOT_TMVA_Option
00031 
00032 //////////////////////////////////////////////////////////////////////////
00033 //                                                                      //
00034 // Option                                                               //
00035 //                                                                      //
00036 // Class for MVA-option handling                                        //
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;         // name of variable 
00090       TString fNameAllLower;       // name of variable 
00091       const TString fDescription;  // its description
00092       Bool_t        fIsSet;        // set by user ?
00093 
00094    protected:
00095 
00096       static MsgLogger* fgLogger;  // message logger
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       // getters
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       // setters
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;  // templated vector
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 } // namespace
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       // template 
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       // template
00212       if (fPreDefs.size()==0) return kTRUE; // if nothing pre-defined then allow everything
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       // template specialization for Bool_t 
00226       TString tVal(val);
00227       tVal.ToLower();
00228       if (fPreDefs.size()==0) return kFALSE; // if nothing pre-defined then allow everything
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       // template 
00245       fPreDefs.push_back(val);
00246    }
00247 
00248    template<>
00249    inline void TMVA::Option<Bool_t>::AddPreDefVal( const Bool_t& ) 
00250    {
00251       // template specialization for Bool_t 
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       // template specialization for Float_t 
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       // template specialization for TString printing
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       // template specialization for TString printing
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       // template specialization for TString printing
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       // template 
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       // template 
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       // set TString value
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       // set Bool_t value
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

Generated on Tue Jul 5 14:27:33 2011 for ROOT_528-00b_version by  doxygen 1.5.1