Logo ROOT  
Reference Guide
OptionMap.h
Go to the documentation of this file.
1// @(#)root/tmva:$Id$
2// Author: Omar Zapata 2016
3
4/*************************************************************************
5 * Copyright (C) 2016, Omar Andres Zapata Mesa *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11#ifndef ROOT_TMVA_OptionMap
12#define ROOT_TMVA_OptionMap
13
14#include <sstream>
15#include<iostream>
16#include<map>
17
18#include <TNamed.h>
19
20#include "TMVA/MsgLogger.h"
21
22#include "TObjString.h"
23
24#include "TObjArray.h"
25
26
27namespace TMVA {
28
29 /**
30 * \class TMVA::OptionMap
31 * \ingroup TMVA
32 * class to storage options for the differents methods
33 */
34
36 {
37 protected:
39 std::map<TString,TString> fOptMap; //
41 class Binding
42 {
43 private:
44 std::map<TString,TString> &fInternalMap;
46 public:
47 Binding(std::map<TString,TString> &fmap,TString key):fInternalMap(fmap),fInternalKey(key){}
49 {
51 }
53 void SetKey(TString key){fInternalKey=key;}
56 {
59 return *this;
60 }
61
62 template<class T> Binding& operator=(const T &value)
63 {
64 ParseValue(fInternalMap[fInternalKey],*const_cast<T*>(&value));
65 return *this;
66 }
67
68 template<class T> operator T()
69 {
70 return GetValue<T>();
71 }
72 template<class T> T GetValue()
73 {
74 T result;
76 return result;
77 }
78
79 template<class T> void ParseValue(TString &str,T &value,Bool_t input=kTRUE)
80 {
81 std::stringstream fStringStream;
82 if(input)
83 {
84 fStringStream<<value;
85 str=fStringStream.str();
86 }else{
87 fStringStream<<str.Data();
88 fStringStream>>value;
89 }
90
91 }
92
93
94 };
96 public:
97 OptionMap(const TString options="",const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,""){
98 ParseOption(options);
99 }
100
101 OptionMap(const Char_t *options,const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,""){
102 ParseOption(options);
103 }
104
105 virtual ~OptionMap(){}
106
107 Bool_t IsEmpty(){return fOptMap.empty();}
108
110 {
111 return fOptMap.count( key )==1;
112 }
113
115 {
116 fBinder.SetKey(key);
117 return fBinder;
118 }
119
121 {
122 ParseOption(options);
123 return *this;
124 }
125
126 void Print() const
127 {
129 for(auto &item:fOptMap)
130 {
131 Log<<kINFO<<item.first.Data()<<": "<<item.second.Data()<<Endl;
132 }
133 }
134
135 template<class T> T GetValue(const TString & key)
136 {
137 T result;
138 fBinder.ParseValue(fOptMap[key],result,kFALSE);
139 return result;
140 }
141
142
143 template<class T> T GetValue(const TString & key) const
144 {
145 T result;
146 std::stringstream oss;
147 oss<<fOptMap.at(key);
148 oss>>result;
149 return result;
150 }
151 void ParseOption(TString options)
152 {
153 options.ReplaceAll(" ","");
154 auto opts=options.Tokenize(":");
155 for(auto opt:*opts)
156 {
157 TObjString *objstr=(TObjString*)opt;
158
159 if(objstr->GetString().Contains("="))
160 {
161 auto pair=objstr->String().Tokenize("=");
162 TObjString *key = (TObjString *)pair->At(0);
163 TObjString *value = (TObjString *)pair->At(1);
164
165 fOptMap[key->GetString()] = value->GetString();
166 }else{
167 if(objstr->GetString().BeginsWith("!"))
168 {
169 objstr->String().ReplaceAll("!","");
170 fOptMap[objstr->GetString()]=TString("0");
171 }else{
172 fOptMap[objstr->GetString()]=TString("1");
173 }
174 }
175 }
176
177 }
179 };
180
181}
182
183#endif
char Char_t
Definition: RtypesCore.h:29
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassDef(name, id)
Definition: Rtypes.h:326
char name[80]
Definition: TGX11.cxx:109
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
Binding(const Binding &obj)
Definition: OptionMap.h:48
std::map< TString, TString > & fInternalMap
Definition: OptionMap.h:44
Binding(std::map< TString, TString > &fmap, TString key)
Definition: OptionMap.h:47
void SetKey(TString key)
Definition: OptionMap.h:53
Binding & operator=(const T &value)
Definition: OptionMap.h:62
void ParseValue(TString &str, T &value, Bool_t input=kTRUE)
Definition: OptionMap.h:79
Binding & operator=(const Binding &obj)
Definition: OptionMap.h:55
class to storage options for the differents methods
Definition: OptionMap.h:36
OptionMap(const Char_t *options, const TString name="Option")
Definition: OptionMap.h:101
T GetValue(const TString &key) const
Definition: OptionMap.h:143
OptionMap(const TString options="", const TString name="Option")
Definition: OptionMap.h:97
Bool_t IsEmpty()
Definition: OptionMap.h:107
Binding fBinder
Definition: OptionMap.h:95
std::map< TString, TString > fOptMap
Definition: OptionMap.h:39
virtual ~OptionMap()
Definition: OptionMap.h:105
OptionMap & operator=(TString options)
Definition: OptionMap.h:120
Binding & operator[](TString key)
Definition: OptionMap.h:114
T GetValue(const TString &key)
Definition: OptionMap.h:135
Bool_t HasKey(TString key)
Definition: OptionMap.h:109
TMVA::MsgLogger fLogger
Definition: OptionMap.h:40
TString fName
Definition: OptionMap.h:38
void ParseOption(TString options)
Definition: OptionMap.h:151
void Print() const
Definition: OptionMap.h:126
Collectable string class.
Definition: TObjString.h:28
const TString & GetString() const
Definition: TObjString.h:46
TString & String()
Definition: TObjString.h:48
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2197
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:610
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
double T(double x)
Definition: ChebyshevPol.h:34
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
Double_t Log(Double_t x)
Definition: TMath.h:750