Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:11:03

0001 // @(#)root/tmva $Id$
0002 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss
0003 
0004 /**********************************************************************************
0005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
0006  * Package: TMVA                                                                  *
0007  * Class  : Option                                                                *
0008  *                                             *
0009  *                                                                                *
0010  * Description:                                                                   *
0011  *      Option container                                                          *
0012  *                                                                                *
0013  * Authors (alphabetical):                                                        *
0014  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
0015  *      Joerg Stelzer   <Joerg.Stelzer@cern.ch>  - CERN, Switzerland              *
0016  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
0017  *                                                                                *
0018  * Copyright (c) 2005:                                                            *
0019  *      CERN, Switzerland                                                         *
0020  *      U. of Victoria, Canada                                                    *
0021  *      MPI-K Heidelberg, Germany                                                 *
0022  *      LAPP, Annecy, France                                                      *
0023  *                                                                                *
0024  * Redistribution and use in source and binary forms, with or without             *
0025  * modification, are permitted according to the terms listed in LICENSE           *
0026  * (http://mva.sourceforge.net/license.txt)                                       *
0027  **********************************************************************************/
0028 
0029 #ifndef ROOT_TMVA_Option
0030 #define ROOT_TMVA_Option
0031 
0032 //////////////////////////////////////////////////////////////////////////
0033 //                                                                      //
0034 // Option                                                               //
0035 //                                                                      //
0036 // Class for TMVA-option handling                                       //
0037 //                                                                      //
0038 //////////////////////////////////////////////////////////////////////////
0039 
0040 #include <sstream>
0041 #include <vector>
0042 #include <string>
0043 
0044 #include "TObject.h"
0045 #include "TString.h"
0046 #include "TMVA/MsgLogger.h"
0047 
0048 namespace TMVA {
0049 
0050    class Configurable;
0051 
0052    class OptionBase : public TObject {
0053 
0054    public:
0055 
0056       friend class Configurable;
0057 
0058       OptionBase( const TString& name, const TString& desc );
0059       virtual ~OptionBase() {}
0060 
0061       virtual const char* GetName() const { return fNameAllLower.Data(); }
0062       virtual const char* TheName() const { return fName.Data(); }
0063       virtual TString     GetValue(Int_t i=-1) const = 0;
0064 
0065       Bool_t IsSet() const { return fIsSet; }
0066       virtual Bool_t IsArrayOpt() const = 0;
0067       const TString& Description() const { return fDescription; }
0068       virtual Bool_t IsPreDefinedVal(const TString&) const = 0;
0069       virtual Bool_t HasPreDefinedVal() const = 0;
0070       virtual Int_t  GetArraySize() const = 0;
0071       virtual Bool_t SetValue( const TString& vs, Int_t i=-1 );
0072 
0073       using TObject::Print;
0074       virtual void Print( std::ostream&, Int_t levelofdetail=0 ) const = 0;
0075 
0076    private:
0077 
0078       virtual void SetValueLocal(const TString& vs, Int_t i=-1) = 0;
0079 
0080       const TString fName;         // name of variable
0081       TString fNameAllLower;       // name of variable
0082       const TString fDescription;  // its description
0083       Bool_t        fIsSet;        // set by user ?
0084 
0085    protected:
0086 
0087       static MsgLogger& Log();
0088    protected:
0089 
0090        ClassDef(OptionBase,1);
0091    };
0092 
0093    // ---------------------------------------------------------------------------
0094 
0095    template <class T>
0096 
0097       class Option : public OptionBase {
0098 
0099    public:
0100 
0101    Option( T& ref, const TString& name, const TString& desc ) :
0102       OptionBase(name, desc), fRefPtr(&ref) {}
0103       virtual ~Option() {}
0104 
0105       // getters
0106       virtual TString  GetValue( Int_t i=-1 ) const;
0107       virtual const T& Value   ( Int_t i=-1 ) const;
0108       virtual Bool_t HasPreDefinedVal() const { return (fPreDefs.size()!=0); }
0109       virtual Bool_t IsPreDefinedVal( const TString& ) const;
0110       virtual Bool_t IsArrayOpt()   const { return kFALSE; }
0111       virtual Int_t  GetArraySize() const { return 0; }
0112 
0113       // setters
0114       virtual void AddPreDefVal(const T&);
0115       using OptionBase::Print;
0116       virtual void Print       ( std::ostream&, Int_t levelofdetail=0 ) const;
0117       virtual void PrintPreDefs( std::ostream&, Int_t levelofdetail=0 ) const;
0118 
0119    protected:
0120 
0121       T& Value(Int_t=-1);
0122 
0123       virtual void   SetValueLocal( const TString& val, Int_t i=-1 );
0124       virtual Bool_t IsPreDefinedValLocal( const T& ) const;
0125 
0126       T* fRefPtr;
0127       std::vector<T> fPreDefs;  // templated vector
0128    };
0129 
0130    template<typename T>
0131       class Option<T*> : public Option<T> {
0132 
0133    public:
0134 
0135    Option( T*& ref, Int_t size, const TString& name, const TString& desc ) :
0136       Option<T>(*ref,name, desc), fVRefPtr(&ref), fSize(size) {}
0137       virtual ~Option() {}
0138 
0139       TString GetValue( Int_t i ) const {
0140          std::stringstream str;
0141          str << std::scientific << Value(i);
0142          return str.str();
0143       }
0144       const T& Value( Int_t i ) const { return (*fVRefPtr)[i]; }
0145       virtual Bool_t IsArrayOpt()   const { return kTRUE; }
0146       virtual Int_t  GetArraySize() const { return fSize; }
0147 
0148       using Option<T>::Print;
0149       virtual void Print( std::ostream&, Int_t levelofdetail=0 ) const;
0150 
0151       virtual Bool_t SetValue( const TString& val, Int_t i=0 );
0152 
0153       T& Value(Int_t i) { return (*fVRefPtr)[i]; }
0154       T ** fVRefPtr;
0155       Int_t fSize;
0156 
0157    };
0158 
0159 } // namespace
0160 
0161 namespace TMVA {
0162 
0163    //______________________________________________________________________
0164    template<class T>
0165       inline const T& TMVA::Option<T>::Value( Int_t ) const {
0166       return *fRefPtr;
0167    }
0168 
0169    template<class T>
0170       inline T& TMVA::Option<T>::Value( Int_t ) {
0171       return *fRefPtr;
0172    }
0173 
0174    template<class T>
0175       inline TString TMVA::Option<T>::GetValue( Int_t ) const {
0176       std::stringstream str;
0177       str << std::scientific << this->Value();
0178       return str.str();
0179    }
0180 
0181    template<>
0182       inline TString TMVA::Option<Bool_t>::GetValue( Int_t ) const {
0183       return Value() ? "True" : "False";
0184    }
0185 
0186    template<>
0187       inline TString TMVA::Option<Bool_t*>::GetValue( Int_t i ) const {
0188       return Value(i) ? "True" : "False";
0189    }
0190 
0191    template<class T>
0192       inline Bool_t TMVA::Option<T>::IsPreDefinedVal( const TString& val ) const
0193       {
0194          // template
0195          T tmpVal;
0196          std::stringstream str(val.Data());
0197          str >> tmpVal;
0198          return IsPreDefinedValLocal(tmpVal);
0199       }
0200 
0201    template<class T>
0202       inline Bool_t TMVA::Option<T>::IsPreDefinedValLocal(const T& val) const
0203       {
0204          // template
0205          if (fPreDefs.size()==0) return kTRUE; // if nothing pre-defined then allow everything
0206 
0207          typename std::vector<T>::const_iterator predefIt;
0208          predefIt = fPreDefs.begin();
0209          for (;predefIt!=fPreDefs.end(); predefIt++)
0210             if ( (*predefIt)==val ) return kTRUE;
0211 
0212          return kFALSE;
0213       }
0214 
0215    template<>
0216       inline Bool_t TMVA::Option<TString>::IsPreDefinedValLocal( const TString& val ) const
0217       {
0218          // template specialization for Bool_t
0219          TString tVal(val);
0220          tVal.ToLower();
0221          if (fPreDefs.size()==0) return kFALSE; // if nothing pre-defined then allow everything
0222          Bool_t foundPreDef = kFALSE;
0223          std::vector<TString>::const_iterator predefIt;
0224          predefIt = fPreDefs.begin();
0225          for (;predefIt!=fPreDefs.end(); predefIt++) {
0226             TString s(*predefIt);
0227             s.ToLower();
0228             if (s==tVal) { foundPreDef = kTRUE; break; }
0229          }
0230          return foundPreDef;
0231       }
0232 
0233    //______________________________________________________________________
0234    template<class T>
0235       inline void TMVA::Option<T>::AddPreDefVal( const T& val )
0236       {
0237          // template
0238          fPreDefs.push_back(val);
0239       }
0240 
0241    template<>
0242       inline void TMVA::Option<Bool_t>::AddPreDefVal( const Bool_t& )
0243       {
0244          // template specialization for Bool_t
0245          Log() << kFATAL << "<AddPreDefVal> predefined values for Option<Bool_t> don't make sense"
0246                << Endl;
0247       }
0248 
0249    template<>
0250       inline void TMVA::Option<Float_t>::AddPreDefVal( const Float_t& )
0251       {
0252          // template specialization for Float_t
0253          Log() << kFATAL << "<AddPreDefVal> predefined values for Option<Float_t> don't make sense"
0254                << Endl;
0255       }
0256 
0257    template<class T>
0258       inline void TMVA::Option<T>::Print( std::ostream& os, Int_t levelofdetail ) const
0259       {
0260          // template specialization for TString printing
0261          os << TheName() << ": " << "\"" << GetValue() << "\"" << " [" << Description() << "]";
0262          this->PrintPreDefs(os,levelofdetail);
0263       }
0264 
0265    template<class T>
0266       inline void TMVA::Option<T*>::Print( std::ostream& os, Int_t levelofdetail ) const
0267       {
0268          // template specialization for TString printing
0269          for (Int_t i=0; i<fSize; i++) {
0270             if (i==0)
0271                os << this->TheName() << "[" << i << "]: " << "\"" << this->GetValue(i) << "\"" << " [" << this->Description() << "]";
0272             else
0273                os << "    " << this->TheName() << "[" << i << "]: " << "\"" << this->GetValue(i) << "\"";
0274             if (i!=fSize-1) os << std::endl;
0275          }
0276          this->PrintPreDefs(os,levelofdetail);
0277       }
0278 
0279    //______________________________________________________________________
0280    template<class T>
0281       inline void TMVA::Option<T>::PrintPreDefs( std::ostream& os, Int_t levelofdetail ) const
0282       {
0283          // template specialization for TString printing
0284          if (HasPreDefinedVal() && levelofdetail>0) {
0285             os << std::endl << "PreDefined - possible values are:" << std::endl;
0286             typename std::vector<T>::const_iterator predefIt;
0287             predefIt = fPreDefs.begin();
0288             for (;predefIt!=fPreDefs.end(); predefIt++) {
0289                os << "                       ";
0290                os << "  - " << (*predefIt) << std::endl;
0291             }
0292          }
0293       }
0294 
0295    //______________________________________________________________________
0296    template<class T>
0297       inline Bool_t TMVA::Option<T*>::SetValue( const TString& val, Int_t ind )
0298       {
0299          // template
0300          if (ind >= fSize) return kFALSE;
0301          std::stringstream str(val.Data());
0302          if (ind < 0) {
0303             str >> Value(0);
0304             for (Int_t i=1; i<fSize; i++) Value(i) = Value(0);
0305          }
0306          else {
0307             str >> Value(ind);
0308          }
0309          return kTRUE;
0310       }
0311 
0312    template<class T>
0313       inline void TMVA::Option<T>::SetValueLocal( const TString& val, Int_t i )
0314       {
0315          // template
0316          std::stringstream str(val.Data());
0317          str >> Value(i);
0318       }
0319 
0320    template<>
0321       inline void TMVA::Option<TString>::SetValueLocal( const TString& val, Int_t )
0322       {
0323          // set TString value
0324          TString valToSet(val);
0325          if (fPreDefs.size()!=0) {
0326             TString tVal(val);
0327             tVal.ToLower();
0328             std::vector<TString>::const_iterator predefIt;
0329             predefIt = fPreDefs.begin();
0330             for (;predefIt!=fPreDefs.end(); predefIt++) {
0331                TString s(*predefIt);
0332                s.ToLower();
0333                if (s==tVal) { valToSet = *predefIt; break; }
0334             }
0335          }
0336 
0337          std::stringstream str(valToSet.Data());
0338          str >> Value(-1);
0339       }
0340 
0341    template<>
0342       inline void TMVA::Option<Bool_t>::SetValueLocal( const TString& val, Int_t )
0343       {
0344          // set Bool_t value
0345          TString valToSet(val);
0346          valToSet.ToLower();
0347          if (valToSet=="1" || valToSet=="true" || valToSet=="ktrue" || valToSet=="t") {
0348             this->Value() = true;
0349          }
0350          else if (valToSet=="0" || valToSet=="false" || valToSet=="kfalse" || valToSet=="f") {
0351             this->Value() = false;
0352          }
0353          else {
0354             Log() << kFATAL << "<SetValueLocal> value \'" << val
0355                   << "\' can not be interpreted as boolean" << Endl;
0356          }
0357       }
0358 }
0359 #endif