Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:58

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  : Configurable                                                          *
0008  *                                             *
0009  *                                                                                *
0010  * Description:                                                                   *
0011  *      Base class for all classes with option parsing                            *
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  *      MPI-K Heidelberg, Germany                                                 *
0021  *                                                                                *
0022  * Redistribution and use in source and binary forms, with or without             *
0023  * modification, are permitted according to the terms listed in LICENSE           *
0024  * (see tmva/doc/LICENSE)                                          *
0025  **********************************************************************************/
0026 
0027 #ifndef ROOT_TMVA_Configurable
0028 #define ROOT_TMVA_Configurable
0029 
0030 //////////////////////////////////////////////////////////////////////////
0031 //                                                                      //
0032 // Configurable                                                         //
0033 //                                                                      //
0034 // Base class for all classes with option parsing                       //
0035 //                                                                      //
0036 //////////////////////////////////////////////////////////////////////////
0037 
0038 #include "TNamed.h"
0039 #include "TList.h"
0040 
0041 #include "TMVA/Option.h"
0042 
0043 namespace TMVA {
0044 
0045    class Configurable : public TNamed {
0046 
0047    public:
0048 
0049       // constructor
0050       Configurable( const TString& theOption = "" );
0051 
0052       // default destructor
0053       virtual ~Configurable();
0054 
0055       // parse the internal option string
0056       virtual void ParseOptions();
0057 
0058       // print list of defined options
0059       void PrintOptions() const;
0060 
0061       const char* GetConfigName()        const { return GetName(); }
0062       const char* GetConfigDescription() const { return fConfigDescription; }
0063       void SetConfigName       ( const char* n ) { SetName(n); }
0064       void SetConfigDescription( const char* d ) { fConfigDescription = TString(d); }
0065 
0066       // Declare option and bind it to a variable
0067       template<class T>
0068          OptionBase* DeclareOptionRef( T& ref, const TString& name, const TString& desc = "" );
0069 
0070       template<class T>
0071          OptionBase* DeclareOptionRef( T*& ref, Int_t size, const TString& name, const TString& desc = "" );
0072 
0073       // Add a predefined value to the last declared option
0074       template<class T>
0075          void AddPreDefVal(const T&);
0076 
0077       // Add a predefined value to the option named optname
0078       template<class T>
0079          void AddPreDefVal(const TString&optname ,const T&);
0080 
0081 
0082       void CheckForUnusedOptions() const;
0083 
0084       const TString& GetOptions() const { return fOptions; }
0085       void SetOptions(const TString& s) { fOptions = s; }
0086 
0087       void WriteOptionsToStream ( std::ostream& o, const TString& prefix ) const;
0088       void ReadOptionsFromStream( std::istream& istr );
0089 
0090       void AddOptionsXMLTo( void* parent ) const;
0091       void ReadOptionsFromXML( void* node );
0092 
0093    protected:
0094 
0095       Bool_t LooseOptionCheckingEnabled() const { return fLooseOptionCheckingEnabled; }
0096       void   EnableLooseOptions( Bool_t b = kTRUE ) { fLooseOptionCheckingEnabled = b; }
0097 
0098       void   WriteOptionsReferenceToFile();
0099 
0100       void   ResetSetFlag();
0101 
0102       const TString& GetReferenceFile() const { return fReferenceFile; }
0103 
0104    private:
0105 
0106       // splits the option string at ':' and fills the list 'loo' with the primitive strings
0107       void SplitOptions(const TString& theOpt, TList& loo) const;
0108 
0109       TString     fOptions;                          ///< options string
0110       Bool_t      fLooseOptionCheckingEnabled;       ///< checker for option string
0111 
0112       // classes and method related to easy and flexible option parsing
0113       OptionBase* fLastDeclaredOption;  ///<! last declared option
0114       TList       fListOfOptions;       ///< option list
0115 
0116       TString     fConfigDescription;   ///< description of this configurable
0117       TString     fReferenceFile;       ///< reference file for options writing
0118 
0119    public:
0120 
0121       // the mutable declaration is needed to use the logger in const methods
0122       MsgLogger& Log() const { return *fLogger; }
0123 
0124       // set message type
0125       void SetMsgType( EMsgType t ) { fLogger->SetMinType(t); }
0126 
0127    protected:
0128       mutable MsgLogger* fLogger;                     ///<! message logger
0129 
0130    private:
0131 
0132 
0133       template <class T>
0134          void AssignOpt( const TString& name, T& valAssign ) const;
0135 
0136    public:
0137 
0138       ClassDef(Configurable,1);  // Virtual base class for all TMVA method
0139 
0140    };
0141 } // namespace TMVA
0142 
0143 // Template Declarations go here
0144 
0145 //______________________________________________________________________
0146 template <class T>
0147 TMVA::OptionBase* TMVA::Configurable::DeclareOptionRef( T& ref, const TString& name, const TString& desc)
0148 {
0149    // set the reference for an option
0150    OptionBase* o = new Option<T>(ref, name, desc);
0151    fListOfOptions.Add(o);
0152    fLastDeclaredOption = o;
0153    return o;
0154 }
0155 
0156 template <class T>
0157 TMVA::OptionBase* TMVA::Configurable::DeclareOptionRef( T*& ref, Int_t size, const TString& name, const TString& desc)
0158 {
0159    // set the reference for an option
0160    OptionBase* o = new Option<T*>(ref, size, name, desc);
0161    fListOfOptions.Add(o);
0162    fLastDeclaredOption = o;
0163    return o;
0164 }
0165 
0166 //______________________________________________________________________
0167 template<class T>
0168 void TMVA::Configurable::AddPreDefVal(const T& val)
0169 {
0170    // add predefined option value to the last declared option
0171    Option<T>* oc = dynamic_cast<Option<T>*>(fLastDeclaredOption);
0172    if(oc) oc->AddPreDefVal(val);
0173 }
0174 
0175 //______________________________________________________________________
0176 template<class T>
0177 void TMVA::Configurable::AddPreDefVal(const TString &optname, const T& val)
0178 {
0179    // add predefined option value to the option named optname
0180 
0181    TListIter optIt( &fListOfOptions );
0182    while (OptionBase * op = (OptionBase *) optIt()) {
0183       if (optname == TString(op->TheName())){
0184          Option<T>* oc = dynamic_cast<Option<T>*>(op);
0185          if(oc){
0186             oc->AddPreDefVal(val);
0187             return;
0188          }
0189          else{
0190             Log() << kFATAL << "Option \"" << optname
0191                   << "\" was found, but somehow I could not convert the pointer properly.. please check the syntax of your option declaration" << Endl;
0192             return;
0193          }
0194 
0195       }
0196    }
0197    Log() << kFATAL << "Option \"" << optname
0198          << "\" is not declared, hence cannot add predefined value, please check the syntax of your option declaration" << Endl;
0199 
0200 }
0201 
0202 //______________________________________________________________________
0203 template <class T>
0204 void TMVA::Configurable::AssignOpt(const TString& name, T& valAssign) const
0205 {
0206    // assign an option
0207    TObject* opt = fListOfOptions.FindObject(name);
0208    if (opt!=0) valAssign = ((Option<T>*)opt)->Value();
0209    else
0210       Log() << kFATAL << "Option \"" << name
0211             << "\" not declared, please check the syntax of your option string" << Endl;
0212 }
0213 
0214 #endif
0215