Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:22:56

0001 // @(#)root/tmva:$Id$
0002 // Author: Omar Zapata   2016
0003 
0004 /*************************************************************************
0005  * Copyright (C) 2016, Omar Andres Zapata Mesa                           *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 #ifndef ROOT_TMVA_OptionMap
0012 #define ROOT_TMVA_OptionMap
0013 
0014 #include <sstream>
0015 #include <map>
0016 #include <string>
0017 
0018 #include "TMVA/MsgLogger.h"
0019 
0020 #include "TObjString.h"
0021 
0022 #include "TObjArray.h"
0023 
0024 
0025 namespace TMVA {
0026 
0027        /**
0028         * \class TMVA::OptionMap
0029         * \ingroup TMVA
0030         *  class to storage options for the differents methods
0031         */
0032 
0033        class OptionMap
0034        {
0035        protected:
0036            TString fName;
0037            std::map<TString,TString> fOptMap; //
0038            TMVA::MsgLogger fLogger;                 //!
0039            class Binding
0040            {
0041            private:
0042                std::map<TString,TString> &fInternalMap;
0043                TString fInternalKey;
0044            public:
0045                Binding(std::map<TString,TString>  &fmap,TString key):fInternalMap(fmap),fInternalKey(key){}
0046                Binding(const Binding &obj):fInternalMap(obj.fInternalMap)
0047                {
0048                    fInternalKey  = obj.fInternalKey;
0049                }
0050                ~Binding(){}
0051                void SetKey(TString key){fInternalKey=key;}
0052                TString GetKey(){return fInternalKey;}
0053                Binding &operator=(const Binding &obj)
0054                {
0055                    fInternalMap  = obj.fInternalMap;
0056                    fInternalKey  = obj.fInternalKey;
0057                    return *this;
0058                }
0059 
0060                template<class T> Binding& operator=(const T &value)
0061                {
0062                    ParseValue(fInternalMap[fInternalKey],*const_cast<T*>(&value));
0063                    return *this;
0064                }
0065 
0066                template<class T> operator T()
0067                {
0068                    return GetValue<T>();
0069                }
0070                template<class T> T GetValue()
0071                {
0072                    T result;
0073                    ParseValue(fInternalMap[fInternalKey],result,kFALSE);
0074                    return result;
0075                }
0076 
0077                template<class T> void  ParseValue(TString &str,T &value,Bool_t input=kTRUE)
0078                {
0079                    std::stringstream fStringStream;
0080                    if(input)
0081                    {
0082                        fStringStream<<value;
0083                        str=fStringStream.str();
0084                    }else{
0085                        fStringStream<<str.Data();
0086                        fStringStream>>value;
0087                    }
0088 
0089                }
0090 
0091 
0092            };
0093            Binding fBinder;     //!
0094        public:
0095            OptionMap(const TString options="",const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,""){
0096                ParseOption(options);
0097            }
0098 
0099            OptionMap(const Char_t *options,const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,""){
0100                ParseOption(options);
0101            }
0102 
0103            virtual ~OptionMap(){}
0104 
0105            Bool_t IsEmpty(){return fOptMap.empty();}
0106 
0107            Bool_t HasKey(TString key)
0108            {
0109                return fOptMap.count( key )==1;
0110            }
0111 
0112            Binding& operator[](TString key)
0113            {
0114                fBinder.SetKey(key);
0115                return fBinder;
0116            }
0117 
0118            OptionMap& operator=(TString options)
0119            {
0120                ParseOption(options);
0121                return *this;
0122            }
0123 
0124            void Print() const
0125            {
0126                MsgLogger Log(fLogger);
0127                for(auto &item:fOptMap)
0128                {
0129                    Log<<kINFO<<item.first.Data()<<": "<<item.second.Data()<<Endl;
0130                }
0131            }
0132 
0133            template<class T> T GetValue(const TString & key)
0134            {
0135                T result;
0136                fBinder.ParseValue(fOptMap[key],result,kFALSE);
0137                return result;
0138            }
0139 
0140 
0141            template<class T> T GetValue(const TString & key) const
0142            {
0143                T result;
0144                std::stringstream oss;
0145                oss<<fOptMap.at(key);
0146                oss>>result;
0147                return result;
0148            }
0149            void ParseOption(TString options)
0150            {
0151                options.ReplaceAll(" ","");
0152                auto opts=options.Tokenize(":");
0153                for(auto opt:*opts)
0154                {
0155                    TObjString *objstr=(TObjString*)opt;
0156 
0157                    if(objstr->GetString().Contains("="))
0158                    {
0159                       auto pair=objstr->String().Tokenize("=");
0160                       TObjString *key   = (TObjString *)pair->At(0);
0161                       TObjString *value = (TObjString *)pair->At(1);
0162 
0163                       fOptMap[key->GetString()] = value->GetString();
0164                    }else{
0165                       if(objstr->GetString().BeginsWith("!"))
0166                       {
0167                           objstr->String().ReplaceAll("!","");
0168                           fOptMap[objstr->GetString()]=TString("0");
0169                       }else{
0170                           fOptMap[objstr->GetString()]=TString("1");
0171                       }
0172                    }
0173                }
0174 
0175            }
0176            ClassDef(OptionMap,1);
0177        };
0178 
0179 }
0180 
0181 #endif