1 #ifndef FWD_TRACKER_CONFIG_H
2 #define FWD_TRACKER_CONFIG_H
4 #include "St_base/StMessMgr.h"
6 #include "TXMLEngine.h"
20 static const std::string valDNE;
21 static const std::string pathDelim;
22 static const std::string attrDelim;
24 bool mErrorParsing =
false;
26 std::map<std::string, std::string> mNodes;
27 static std::stringstream sstr;
38 std::string p = path + TString::Format(
"[%zu]", index ).Data();
39 while ( mNodes.count( p ) ){
41 p = path + TString::Format(
"[%zu]", index ).Data();
54 void mapFile(TXMLEngine &xml, XMLNodePointer_t node, Int_t level, std::string path =
"") {
57 if ( !path.empty() ) path += FwdTrackerConfig::pathDelim;
61 path += xml.GetNodeName(node);
64 const string node_name = xml.GetNodeName(node);
65 const string node_content = xml.GetNodeContent(node) !=
nullptr ? xml.GetNodeContent(node) : FwdTrackerConfig::valDNE;
68 if ( mNodes.count( path ) == 0 ) {
69 mNodes[ path ] = node_content;
72 path += TString::Format(
"[%zu]", index ).Data();
73 mNodes[ path ] = node_content;
77 XMLAttrPointer_t attr = xml.GetFirstAttr(node);
81 const string attr_name = xml.GetAttrName(attr);
82 const string attr_val = xml.GetAttrValue(attr) !=
nullptr ? xml.GetAttrValue(attr) : FwdTrackerConfig::valDNE;
85 mNodes[ (path + FwdTrackerConfig::attrDelim + attr_name) ] = attr_val;
86 attr = xml.GetNextAttr(attr);
90 XMLNodePointer_t child = xml.GetChild(node);
92 mapFile(xml, child, level + 1, path);
93 child = xml.GetNext(child);
105 path.erase(std::remove_if(path.begin(), path.end(),
static_cast<int(*) (
int)
>(std::isspace)), path.end());
108 size_t pos = path.find(
"[0]" );
111 size_t len = (pos != std::string::npos) ? 3 : 0;
112 pos = (pos != std::string::npos) ? pos : 0;
113 path.erase( pos, len );
124 FwdTrackerConfig::sstr.str(
"");
125 FwdTrackerConfig::sstr.clear();
126 for (
auto kv : mNodes ){
127 FwdTrackerConfig::sstr <<
"[" << kv.first <<
"] = " << kv.second << endl;
129 return FwdTrackerConfig::sstr.str();
142 if ( 0 == mNodes.count( path ) )
155 template <
typename T>
158 FwdTrackerConfig::sstr.str(
"");
159 FwdTrackerConfig::sstr.clear();
160 FwdTrackerConfig::sstr << s;
161 FwdTrackerConfig::sstr >> rv;
172 template <
typename T>
174 FwdTrackerConfig::sstr.str(
"");
175 FwdTrackerConfig::sstr.clear();
176 FwdTrackerConfig::sstr << v;
177 return FwdTrackerConfig::sstr.str();
189 template <
typename T>
190 T
get( std::string path, T dv )
const {
198 return convert<T>( mNodes.at( path ) );
208 template <
typename T>
209 void set( std::string path, T v ) {
212 mNodes[ path ] = convertTo<T>( v );
223 template <
typename T>
224 std::vector<T>
getVector( std::string path, std::vector<T> dv )
const {
229 std::string val = mNodes.at( path );
231 val.erase(std::remove_if(val.begin(), val.end(),
static_cast<int(*) (
int)
>(std::isspace) ), val.end());
232 std::vector<std::string> elems;
236 std::stringstream ss(val);
238 while (std::getline(ss, str,
',')) {
239 elems.push_back(str);
244 std::vector<T> result;
245 for (
auto sv : elems ){
246 result.push_back( convert<T>( sv ) );
257 std::vector<std::string>
childrenOf( std::string path )
const {
259 vector<string> result;
264 auto is_attribute = [&](
string str){
265 return ( str.find( FwdTrackerConfig::attrDelim ) != string::npos );
268 for (
auto kv : mNodes ){
270 string parent = (kv.first).substr( 0, path.length() );
273 if ( parent == kv.first )
continue;
276 if ( parent == path && !is_attribute( kv.first )){
277 result.push_back( kv.first );
305 void load( std::string filename,
bool asString =
false ) {
315 XMLDocPointer_t xmldoc;
317 xmldoc = xml.ParseString(filename.c_str());
319 xmldoc = xml.ParseFile(filename.c_str());
322 mErrorParsing =
true;
327 XMLNodePointer_t root_node = xml.DocGetRootElement(xmldoc);
FwdTrackerConfig(std::string filename)
Construct a new Fwd Tracker Config object and load a file.
std::string convertTo(T v) const
Generic conversion of type T to a string.
std::vector< std::string > childrenOf(std::string path) const
list the paths of children nodes for a given node
FwdTrackerConfig()
Constructor is noop, use load(...)
bool exists(std::string path) const
returns whether or not a path exist Either node or attribute - used to determine if default value is ...
T convert(std::string s) const
Generic conversion of type T from string override this for special conversions.
std::string dump() const
dump config to a basic string representation - mostly for debugging
std::vector< T > getVector(std::string path, std::vector< T > dv) const
Get a Vector object from config.
void load(std::string filename, bool asString=false)
Main setup routine Loads the given XML file (or string) and maps it.
void set(std::string path, T v)
Writes a value of type T to the map Uses convertTo<T> to convert type T to a string rep...
size_t pathCount(const std::string path)
get lowest non-existing path index assumes bare path and adds [i] until DNE starts at 1 since 0 is ch...
void mapFile(TXMLEngine &xml, XMLNodePointer_t node, Int_t level, std::string path="")
Reads an xml document and writes it into map.
T get(std::string path, T dv) const
template function for getting any type that can be converted from string via stringstream ...
static void canonize(std::string &path)
Returns a path in its cannonical form.