Configurable.h

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: Configurable.h 29195 2009-06-24 10:39:49Z brun $   
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  : Configurable                                                          *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      Base class for all classes with option parsing                            *
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  *      MPI-K Heidelberg, Germany                                                 * 
00021  *                                                                                *
00022  * Redistribution and use in source and binary forms, with or without             *
00023  * modification, are permitted according to the terms listed in LICENSE           *
00024  * (http://tmva.sourceforge.net/LICENSE)                                          *
00025  **********************************************************************************/
00026 
00027 #ifndef ROOT_TMVA_Configurable
00028 #define ROOT_TMVA_Configurable
00029 
00030 //////////////////////////////////////////////////////////////////////////
00031 //                                                                      //
00032 // Configurable                                                         //
00033 //                                                                      //
00034 // Base class for all classes with option parsing                       //
00035 //                                                                      //
00036 //////////////////////////////////////////////////////////////////////////
00037 
00038 #ifndef ROOT_TObject
00039 #include "TObject.h"
00040 #endif
00041 #ifndef ROOT_TList
00042 #include "TList.h"
00043 #endif
00044 
00045 #ifndef ROOT_TMVA_Option
00046 #include "TMVA/Option.h"
00047 #endif
00048 
00049 namespace TMVA {
00050 
00051    class Configurable : public TObject {
00052 
00053    public:
00054 
00055       // constructur
00056       Configurable( const TString& theOption = "" );
00057       
00058       // default destructur
00059       virtual ~Configurable();
00060 
00061       // parse the internal option string
00062       virtual void ParseOptions();
00063 
00064       // print list of defined options
00065       void PrintOptions() const;
00066 
00067       virtual const char* GetName()      const { return GetConfigName(); }
00068       const char* GetConfigName()        const { return fConfigName; }
00069       const char* GetConfigDescription() const { return fConfigDescription; }
00070       void SetConfigName       ( const char* n ) { fConfigName        = TString(n); }
00071       void SetConfigDescription( const char* d ) { fConfigDescription = TString(d); }
00072 
00073       // Declare option and bind it to a variable
00074       template<class T> 
00075       OptionBase* DeclareOptionRef( T& ref, const TString& name, const TString& desc = "" );
00076 
00077       template<class T> 
00078       OptionBase* DeclareOptionRef( T*& ref, Int_t size, const TString& name, const TString& desc = "" );
00079 
00080       // Add a predefined value to the last declared option
00081       template<class T>
00082       void AddPreDefVal(const T&);
00083       
00084       void CheckForUnusedOptions() const;
00085 
00086       const TString& GetOptions() const { return fOptions; }
00087       void SetOptions(const TString& s) { fOptions = s; }
00088 
00089       void WriteOptionsToStream ( std::ostream& o, const TString& prefix ) const;
00090       void ReadOptionsFromStream( istream& istr );
00091 
00092       void AddOptionsXMLTo( void* parent ) const;
00093       void ReadOptionsFromXML( void* node );
00094 
00095    protected:
00096       
00097       Bool_t LooseOptionCheckingEnabled() const { return fLooseOptionCheckingEnabled; }
00098       void   EnableLooseOptions( Bool_t b = kTRUE ) { fLooseOptionCheckingEnabled = b; }
00099 
00100       void   WriteOptionsReferenceToFile();
00101 
00102       void   ResetSetFlag();
00103 
00104       const TString& GetReferenceFile() const { return fReferenceFile; }
00105 
00106    private:
00107 
00108       // splits the option string at ':' and fills the list 'loo' with the primitive strings
00109       void SplitOptions(const TString& theOpt, TList& loo) const;
00110 
00111       TString     fOptions;                          //! options string
00112       Bool_t      fLooseOptionCheckingEnabled;       //! checker for option string
00113 
00114       // classes and method related to easy and flexible option parsing
00115       OptionBase* fLastDeclaredOption;  //! last declared option
00116       TList       fListOfOptions;       //! option list
00117 
00118       TString     fConfigName;          // the name of this configurable
00119       TString     fConfigDescription;   // description of this configurable
00120       TString     fReferenceFile;       // reference file for options writing
00121 
00122    protected:
00123 
00124       // the mutable declaration is needed to use the logger in const methods
00125       MsgLogger& Log() const { return *fLogger; }                       
00126 
00127    public:
00128 
00129       // set message type
00130       void SetMsgType( EMsgType t ) { fLogger->SetMinType(t); }
00131 
00132 
00133    private:
00134 
00135       mutable MsgLogger* fLogger;                     //! message logger
00136 
00137       template <class T>
00138       void AssignOpt( const TString& name, T& valAssign ) const;
00139       
00140    public:
00141 
00142       ClassDef(Configurable,0)  // Virtual base class for all TMVA method
00143 
00144    };
00145 } // namespace TMVA
00146 
00147 // Template Declarations go here
00148 
00149 //______________________________________________________________________
00150 template <class T>
00151 TMVA::OptionBase* TMVA::Configurable::DeclareOptionRef( T& ref, const TString& name, const TString& desc) 
00152 {
00153    // set the reference for an option
00154    OptionBase* o = new Option<T>(ref, name, desc);
00155    fListOfOptions.Add(o);
00156    fLastDeclaredOption = o;
00157    return o;
00158 }
00159 
00160 template <class T>
00161 TMVA::OptionBase* TMVA::Configurable::DeclareOptionRef( T*& ref, Int_t size, const TString& name, const TString& desc) 
00162 {
00163    // set the reference for an option
00164    OptionBase* o = new Option<T*>(ref, size, name, desc);
00165    fListOfOptions.Add(o);
00166    fLastDeclaredOption = o;
00167    return o;
00168 }
00169 
00170 //______________________________________________________________________
00171 template<class T>
00172 void TMVA::Configurable::AddPreDefVal(const T& val) 
00173 {
00174    // add predefined option value
00175    Option<T>* oc = dynamic_cast<Option<T>*>(fLastDeclaredOption);
00176    if(oc!=0) oc->AddPreDefVal(val);
00177 }
00178 
00179 //______________________________________________________________________
00180 template <class T>
00181 void TMVA::Configurable::AssignOpt(const TString& name, T& valAssign) const 
00182 {
00183    // assign an option
00184    TObject* opt = fListOfOptions.FindObject(name);
00185    if (opt!=0) valAssign = ((Option<T>*)opt)->Value();
00186    else 
00187       Log() << kFATAL << "Option \"" << name 
00188             << "\" not declared, please check the syntax of your option string" << Endl;
00189 }
00190 
00191 #endif
00192 

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