|
||||
File indexing completed on 2025-01-30 10:11:48
0001 // $Id$ 0002 // 0003 // File: JGeometry.h 0004 // Created: Fri Jul 6 16:24:24 EDT 2007 0005 // Creator: davidl (on Darwin fwing-dhcp61.jlab.org 8.10.1 i386) 0006 // 0007 0008 #pragma once 0009 #include "jerror.h" 0010 0011 #include <map> 0012 #include <string> 0013 #include <sstream> 0014 #include <vector> 0015 using std::map; 0016 using std::string; 0017 using std::stringstream; 0018 using std::vector; 0019 0020 0021 /// JGeometry is a virtual base class used to define the interface by 0022 /// which geometry information can be obtained in JANA. 0023 /// Implementing this base class allows the JANA end user to be 0024 /// agnostic as to the details of how the geometry info is stored. 0025 /// The geometry can be stored in a database or any number of file 0026 /// formats. The files can be stored locally, or on the network 0027 /// somewhere. 0028 /// 0029 /// The primary advantage here is that it allows one to work with 0030 /// local files, but then easily switch to a remote source method 0031 /// when appropriate without requiring modifications to the end 0032 /// user code. 0033 /// 0034 /// On the user side they will call one of the Get(...) methods which all get 0035 /// translated into a call of one of the two vitural methods: 0036 /// 0037 /// virtual bool Get(string namepath, string &sval, map<string, string> &where)=0; 0038 /// virtual bool Get(string namepath, map<string, string> &svals, map<string, string> &where)=0; 0039 /// 0040 /// These two virtual methods along with one to get a list of the available 0041 /// namepaths are the only things that need to be implemented 0042 /// in a concrete subclass of JGeometry. 0043 /// 0044 /// A geometry element is specified by its <i>namepath</i> and an optional 0045 /// set of qualifiers (the <i>where</i> argument). The <i>namepath</i> 0046 /// is a hierarchal list of elements separated by forward slashes(/) 0047 /// analogous to a path on a unix filesystem. This path is always assumed 0048 /// to be relative to the <i>url</i> specified in the constructor. So, 0049 /// for instance, suppose one kept the geometry in a set XML files on the 0050 /// local filesystem and wished to access information from the file 0051 /// 0052 /// <tt>/home/joe/calib/geom_Oct10_2017.xml</tt> 0053 /// 0054 /// One would specify the <i>url</i> as: 0055 /// 0056 /// file:///home/joe/calib/geom_Oct10_2017.xml 0057 /// 0058 /// and then the namepath could be specified as the string: 0059 /// 0060 /// "TOF/bar/X_Y_Z" 0061 /// 0062 /// which would indicate the attribute <i>"X_Y_Z"</i> of the 0063 /// subtag <i>"bar"</i> of the tag <i>"TOF"</i> in the file 0064 /// <i>"/home/joe/calib/geom_Oct10_2017.xml"</i> 0065 0066 class JGeometry{ 0067 public: 0068 JGeometry(string url, int run, string context="default"){ 0069 this->url = url; 0070 this->run_requested = run; 0071 this->context = context; 0072 } 0073 virtual ~JGeometry(){} 0074 virtual const char* className(void){return static_className();} 0075 static const char* static_className(void){return "JGeometry";} 0076 0077 typedef enum{ 0078 // Used to specify which (if any) attributes should be included in 0079 // the values obtained through GetXPaths(). 0080 attr_level_none = 0, // Don't include any attributes. Only node names. 0081 attr_level_last = 1, // Include the attributes for the last node only. 0082 attr_level_all = 2 // Include attributes for all nodes. 0083 }ATTR_LEVEL_t; 0084 0085 void SetVerbose(int newval){ verbose = newval; } 0086 0087 // Virtual methods called through base class 0088 virtual bool Get(string xpath, string &sval)=0; 0089 virtual bool Get(string xpath, map<string, string> &svals)=0; 0090 virtual bool GetMultiple(string xpath, vector<string> &vsval)=0; 0091 virtual bool GetMultiple(string xpath, vector<map<string, string> >&vsvals)=0; 0092 virtual void GetXPaths(vector<string> &xpaths, ATTR_LEVEL_t level=attr_level_last, const string &filter="")=0; 0093 virtual string GetChecksum(void) const {return string("not supported");} 0094 0095 // Templated methods that can return more useful forms 0096 template<class T> bool Get(string xpath, T &val); 0097 template<class T> bool Get(string xpath, vector<T> &vals, string delimiter=" "); 0098 template<class T> bool Get(string xpath, map<string,T> &vals); 0099 template<class T> bool GetMultiple(string xpath, vector<T> &vval); 0100 template<class T> bool GetMultiple(string xpath, vector<vector<T> > &vvals, string delimiter=" "); 0101 template<class T> bool GetMultiple(string xpath, vector<map<string,T> > &vvals); 0102 0103 const int& GetRunRequested(void) const {return run_requested;} 0104 const int& GetRunFound(void) const {return run_found;} 0105 const int& GetRunMin(void) const {return run_min;} 0106 const int& GetRunMax(void) const {return run_max;} 0107 const string& GetContext(void) const {return context;} 0108 const string& GetURL(void) const {return url;} 0109 0110 protected: 0111 int run_min; 0112 int run_max; 0113 int run_found; 0114 0115 int verbose=1; 0116 0117 private: 0118 JGeometry(){} // Don't allow trivial constructor 0119 0120 int run_requested; 0121 string context; 0122 string url; 0123 map<string,string> anywhere; // an empty map means no constraints 0124 0125 }; 0126 0127 0128 //------------- 0129 // Get (single version) 0130 //------------- 0131 template<class T> 0132 bool JGeometry::Get(string xpath, T &val) 0133 { 0134 /// Templated method used to get a single geometry element. 0135 /// 0136 /// This method will get the specified geometry element in the form of 0137 /// a string using the virtual (non-templated) Get(...) method. It will 0138 /// then convert the string into the data type on which <i>val</i> is 0139 /// based. It does this using the stringstream 0140 /// class so T is restricted to the types stringstream understands (int, float, 0141 /// double, string, ...). 0142 /// 0143 /// If no element of the specified name is found, a value 0144 /// of boolean "false" is returned. A value of "true" is 0145 /// returned upon success. 0146 0147 // Get values in the form of a string 0148 string sval; 0149 bool res = Get(xpath, sval); 0150 if(!res)return res; 0151 0152 // Convert the string to type "T" and copy it into val. 0153 // Use stringstream to convert from a string to type "T" 0154 stringstream ss(sval); 0155 ss >> val; 0156 0157 return res; 0158 } 0159 0160 //------------- 0161 // Get (vector version) 0162 //------------- 0163 template<class T> 0164 bool JGeometry::Get(string xpath, vector<T> &vals, string delimiter) 0165 { 0166 /// Templated method used to get a set of values from a geometry attribute. 0167 /// 0168 /// This method can be used to get a list of values (possibly only one) 0169 /// from a single geometry attribute specified by xpath. The attribute 0170 /// is obtained as a string using the non-templated Get(...) method 0171 /// and the string broken into tokens separated by the delimiter 0172 /// (which defaults to a single white space). Each 0173 /// token is then converted into type T using the stringstream class 0174 /// so T is restricted to the types stringstream understands (int, float, 0175 /// double, string, ...). 0176 /// 0177 /// If no element of the specified name is found, a value 0178 /// of boolean "false" is returned. A value of "true" is 0179 /// returned upon success. 0180 0181 // Get values in the form of strings 0182 vals.clear(); 0183 string svals; 0184 bool res = Get(xpath, svals); 0185 if(!res)return res; 0186 0187 string::size_type pos_start = svals.find_first_not_of(delimiter,0); 0188 while(pos_start != string::npos){ 0189 string::size_type pos_end = svals.find_first_of(delimiter, pos_start); 0190 if(pos_end==string::npos)pos_end=svals.size(); 0191 0192 T v; 0193 string val = svals.substr(pos_start, pos_end-pos_start); 0194 stringstream ss(val); 0195 ss >> v; 0196 vals.push_back(v); 0197 0198 pos_start = svals.find_first_not_of(delimiter, pos_end); 0199 } 0200 0201 return res; 0202 } 0203 0204 //------------- 0205 // Get (map version) 0206 //------------- 0207 template<class T> 0208 bool JGeometry::Get(string xpath, map<string,T> &vals) 0209 { 0210 /// Templated method used to get a set of geometry attributes. 0211 /// 0212 /// This method can be used to get a list of all attributes for 0213 /// a given xpath. The attributes are copied into the <i>vals</i> 0214 /// map with the attribute name as the key and the attribute 0215 /// value as the value. This relies on the non-templated, virtual 0216 /// Get(string, map<string,string>&) method to first get the values 0217 /// in the form of strings. It converts them using the stringstream 0218 /// class so T is restricted to the types it understands (int, float, 0219 /// double, string, ...). 0220 /// 0221 /// If no element of the specified name is found, a value 0222 /// of boolean "false" is returned. A value of "true" is 0223 /// returned upon success. 0224 0225 // Get values in the form of strings 0226 map<string, string> svals; 0227 bool res = Get(xpath, svals); 0228 0229 // Loop over values, converting the strings to type "T" and 0230 // copying them into the vals map. 0231 vals.clear(); 0232 map<string,string>::const_iterator iter; 0233 for(iter=svals.begin(); iter!=svals.end(); ++iter){ 0234 // Use stringstream to convert from a string to type "T" 0235 T v; 0236 stringstream ss(iter->second); 0237 ss >> v; 0238 vals[iter->first] = v; 0239 } 0240 0241 return res; 0242 } 0243 0244 0245 //------------- 0246 // GetMultiple (single version) 0247 //------------- 0248 template<class T> 0249 bool JGeometry::GetMultiple(string xpath, vector<T> &vval) 0250 { 0251 /// Templated method used to get multiple entries satisfying a single xpath. 0252 /// 0253 /// This method will get the specified geometry element in the form of 0254 /// a string using the virtual (non-templated) Get(...) method. It will 0255 /// then convert the string into the data type on which <i>val</i> is 0256 /// based. It does this using the stringstream 0257 /// class so T is restricted to the types stringstream understands (int, float, 0258 /// double, string, ...). 0259 /// 0260 /// This differs from the similar Get() method in that the geometry tree 0261 /// will be searched for all nodes satisfying the given xpath and all 0262 /// all values will be copied into the container provided. In Get(), only 0263 /// the first node encountered that satisfies the xpath will be copied. 0264 /// 0265 /// If no element of the specified name is found, a value 0266 /// of boolean "false" is returned. A value of "true" is 0267 /// returned upon success. 0268 0269 // Get values in the form of a string 0270 vector<string> vsval; 0271 bool res = GetMultiple(xpath, vsval); 0272 if(!res)return res; 0273 0274 // Convert the string to type "T" and copy it into val. 0275 // Use stringstream to convert from a string to type "T" 0276 for(unsigned int i=0; i<vsval.size(); i++){ 0277 stringstream ss(vsval[i]); 0278 T val; 0279 ss >> val; 0280 vval.push_back(val); 0281 } 0282 0283 return res; 0284 } 0285 0286 //------------- 0287 // GetMultiple (vector version) 0288 //------------- 0289 template<class T> 0290 bool JGeometry::GetMultiple(string xpath, vector<vector<T> > &vvals, string delimiter) 0291 { 0292 /// Templated method used to get a set of values from a geometry attribute. 0293 /// 0294 /// This method can be used to get a list of values (possibly only one) 0295 /// from a single geometry attribute specified by xpath. The attribute 0296 /// is obtained as a string using the non-templated Get(...) method 0297 /// and the string broken into tokens separated by the delimiter 0298 /// (which defaults to a single white space). Each 0299 /// token is then converted into type T using the stringstream class 0300 /// so T is restricted to the types stringstream understands (int, float, 0301 /// double, string, ...). 0302 /// 0303 /// If no element of the specified name is found, a value 0304 /// of boolean "false" is returned. A value of "true" is 0305 /// returned upon success. 0306 0307 // Get values in the form of strings 0308 vvals.clear(); 0309 vector<string> vsvals; 0310 bool res = GetMultiple(xpath, vsvals); 0311 if(!res)return res; 0312 0313 for(unsigned int i=0; i<vsvals.size(); i++){ 0314 string &svals = vsvals[i]; 0315 0316 string::size_type pos_start = svals.find_first_not_of(delimiter,0); 0317 vector<T> vals; 0318 while(pos_start != string::npos){ 0319 string::size_type pos_end = svals.find_first_of(delimiter, pos_start); 0320 if(pos_end==string::npos)pos_end=svals.size(); 0321 0322 T v; 0323 string val = svals.substr(pos_start, pos_end-pos_start); 0324 stringstream ss(val); 0325 ss >> v; 0326 vals.push_back(v); 0327 0328 pos_start = svals.find_first_not_of(delimiter, pos_end); 0329 } 0330 0331 vvals.push_back(vals); 0332 } 0333 0334 return res; 0335 } 0336 0337 //------------- 0338 // GetMultiple (map version) 0339 //------------- 0340 template<class T> 0341 bool JGeometry::GetMultiple(string xpath, vector<map<string,T> > &vvals) 0342 { 0343 /// Templated method used to get a set of geometry attributes. 0344 /// 0345 /// This method can be used to get a list of all attributes for 0346 /// a given xpath. The attributes are copied into the <i>vals</i> 0347 /// map with the attribute name as the key and the attribute 0348 /// value as the value. This relies on the non-templated, virtual 0349 /// Get(string, map<string,string>&) method to first get the values 0350 /// in the form of strings. It converts them using the stringstream 0351 /// class so T is restricted to the types it understands (int, float, 0352 /// double, string, ...). 0353 /// 0354 /// If no element of the specified name is found, a value 0355 /// of boolean "false" is returned. A value of "true" is 0356 /// returned upon success. 0357 0358 // Get values in the form of strings 0359 vector<map<string, string> > vsvals; 0360 bool res = GetMultiple(xpath, vsvals); 0361 0362 // Loop over values, converting the strings to type "T" and 0363 // copying them into the vals map. 0364 vvals.clear(); 0365 0366 for(unsigned int i=0; i<vsvals.size(); i++){ 0367 map<string,string> &svals = vsvals[i]; 0368 0369 map<string,T> vals; 0370 map<string,string>::const_iterator iter; 0371 for(iter=svals.begin(); iter!=svals.end(); ++iter){ 0372 // Use stringstream to convert from a string to type "T" 0373 T v; 0374 stringstream ss(iter->second); 0375 ss >> v; 0376 vals[iter->first] = v; 0377 } 0378 0379 vvals.push_back(vals); 0380 } 0381 0382 return res; 0383 } 0384
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |