Logo ROOT   6.16/01
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 }
105 {
106 fName = obj.fName;
107 fLogger = obj.fLogger;
108 fOptMap = obj.fOptMap;
109 }
110// OptionMap(const Char_t *options,const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,"")
111// {
112// ParseOption(options);
113// }
114
115 virtual ~OptionMap(){}
116
117 Bool_t IsEmpty(){return fOptMap.empty();}
118
120 {
121 return fOptMap.count( key )==1;
122 }
123
125 {
126 fBinder.SetKey(key);
127 return fBinder;
128 }
129
131 {
132 ParseOption(options);
133 return *this;
134 }
135
136 void Print() const
137 {
139 for(auto &item:fOptMap)
140 {
141 Log<<kINFO<<item.first.Data()<<": "<<item.second.Data()<<Endl;
142 }
143 }
144
145 template<class T> T GetValue(const TString & key)
146 {
147 T result;
148 fBinder.ParseValue(fOptMap[key],result,kFALSE);
149 return result;
150 }
151
152
153 template<class T> T GetValue(const TString & key) const
154 {
155 T result;
156 std::stringstream oss;
157 oss<<fOptMap.at(key);
158 oss>>result;
159 return result;
160 }
161 void ParseOption(TString options)
162 {
163 options.ReplaceAll(" ","");
164 auto opts=options.Tokenize(":");
165 for(auto opt:*opts)
166 {
167 TObjString *objstr=(TObjString*)opt;
168
169 if(objstr->GetString().Contains("="))
170 {
171 auto pair=objstr->String().Tokenize("=");
172 TObjString *key = (TObjString *)pair->At(0);
173 TObjString *value = (TObjString *)pair->At(1);
174
175 fOptMap[key->GetString()] = value->GetString();
176 }else{
177 if(objstr->GetString().BeginsWith("!"))
178 {
179 objstr->String().ReplaceAll("!","");
180 fOptMap[objstr->GetString()]=TString("0");
181 }else{
182 fOptMap[objstr->GetString()]=TString("1");
183 }
184 }
185 }
186
187 }
189 };
190
191}
192
193#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:324
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:153
OptionMap(const TString options="", const TString name="Option")
Definition: OptionMap.h:97
Bool_t IsEmpty()
Definition: OptionMap.h:117
Binding fBinder
Definition: OptionMap.h:95
std::map< TString, TString > fOptMap
Definition: OptionMap.h:39
virtual ~OptionMap()
Definition: OptionMap.h:115
OptionMap & operator=(TString options)
Definition: OptionMap.h:130
Binding & operator[](TString key)
Definition: OptionMap.h:124
T GetValue(const TString &key)
Definition: OptionMap.h:145
Bool_t HasKey(TString key)
Definition: OptionMap.h:119
TMVA::MsgLogger fLogger
Definition: OptionMap.h:40
TString fName
Definition: OptionMap.h:38
void ParseOption(TString options)
Definition: OptionMap.h:161
OptionMap(const OptionMap &obj)
Definition: OptionMap.h:104
void Print() const
Definition: OptionMap.h:136
Collectable string class.
Definition: TObjString.h:28
const TString & GetString() const
Definition: TObjString.h:47
TString & String()
Definition: TObjString.h:49
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:2172
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
Abstract ClassifierFactory template that handles arbitrary types.
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
Double_t Log(Double_t x)
Definition: TMath.h:748