GEAR  1.9.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
GearParametersXML.cc
1 #include "gearxml/GearParametersXML.h"
2 
3 #include "gearxml/tinyxml.h"
4 #include "gearimpl/GearParametersImpl.h"
5 #include "gear/GearMgr.h"
6 
7 #include <vector>
8 #include <algorithm>
9 #include <sstream>
10 #include <iomanip>
11 
12 
13 namespace gear {
14 
15 
17  int intVal( const std::string& str ) {
18  return atoi( str.c_str() ) ;
19  }
20 
22  double doubleVal( const std::string& str ) {
23  return (double) atof( str.c_str() ) ;
24  }
25 
28  class Tokenizer{
29 
30  std::vector< std::string >& _tokens ;
31  char _del ;
32  char _last ;
33  public:
34 
35  Tokenizer( std::vector< std::string >& tokens, char del ) : _tokens(tokens) , _del(del), _last(del) {
36  }
37 
38 
39  void operator()(const char& c) {
40 
41  if( c != _del ) {
42 
43  if( _last == _del ) {
44  _tokens.push_back("") ;
45  }
46  _tokens.back() += c ;
47  result() ;
48  }
49  _last = c ;
50 
51  }
52 
53  ~Tokenizer(){
54  }
55 
56  std::vector<std::string> & result() {
57 
58  return _tokens ;
59 
60  }
61  };
62 
63 
64 
65 
67 
68 
69  TiXmlElement det("detector") ;
70 
71  getXMLForParameters( &det , &parameters ) ;
72 
73  return det ;
74 
75  }
76 
77 
78  GearParameters* GearParametersXML::fromXML( const TiXmlElement* xmlElement, GearMgr* gearMgr) const {
79 
80 
81  GearParametersImpl* gearParams = new GearParametersImpl ;
82 
83 
84  setParametersFromXML( xmlElement, gearParams ) ;
85 
86 
87  if( gearMgr != 0 ) {
88 
89  std::string name = getXMLAttribute( xmlElement , "name" ) ;
90 
91  gearMgr->setGearParameters( name , gearParams ) ;
92 
93  }
94 
95 
96  return gearParams ;
97  }
98 
100  const GearParameters* gearParams ){
101 
102  typedef const std::vector< std::string > KeyVec ;
103 
104  if( xmlElement == 0 || gearParams == 0 ){
105  return ;
106  }
107 
108  KeyVec& intKeys = gearParams->getIntKeys() ;
109  for(unsigned int i=0 ; i < intKeys.size() ; ++i ){
110 
111  TiXmlElement param("parameter") ;
112 
113  param.SetAttribute( "name", intKeys[i] ) ;
114  param.SetAttribute( "type", "int" ) ;
115  param.SetAttribute( "value", gearParams->getIntVal( intKeys[i] ) ) ;
116 
117  xmlElement->InsertEndChild( param ) ;
118 
119  }
120 
121  KeyVec& doubleKeys = gearParams->getDoubleKeys() ;
122 
123  for(unsigned int i=0 ; i < doubleKeys.size() ; ++i ){
124 
125  TiXmlElement param("parameter") ;
126 
127  param.SetAttribute( "name", doubleKeys[i] ) ;
128  param.SetAttribute( "type", "double" ) ;
129  param.SetDoubleAttribute( "value", gearParams->getDoubleVal( doubleKeys[i] ) ) ;
130 
131  xmlElement->InsertEndChild( param ) ;
132 
133  }
134 
135  KeyVec& stringKeys = gearParams->getStringKeys() ;
136 
137  for(unsigned int i=0 ; i < stringKeys.size() ; ++i ){
138 
139  TiXmlElement param("parameter") ;
140 
141  param.SetAttribute( "name", stringKeys[i] ) ;
142  param.SetAttribute( "type", "string" ) ;
143  param.SetAttribute( "value", gearParams->getStringVal( stringKeys[i] ) ) ;
144 
145  xmlElement->InsertEndChild( param ) ;
146 
147  }
148 
149  KeyVec& intVecKeys = gearParams->getIntVecKeys() ;
150  for(unsigned int i=0 ; i < intVecKeys.size() ; ++i ){
151 
152  TiXmlElement param("parameter") ;
153 
154  param.SetAttribute( "name", intVecKeys[i] ) ;
155  param.SetAttribute( "type", "IntVec" ) ;
156 
157  IntVec vec = gearParams->getIntVals( intVecKeys[i] ) ;
158  std::stringstream str ;
159  for(unsigned int j=0 ; j < vec.size() ; ++j ){
160  if( j!=0 )
161  str << " " ;
162  str << vec[j] ;
163  }
164 
165  param.SetAttribute( "value", str.str() ) ;
166 
167  xmlElement->InsertEndChild( param ) ;
168 
169  }
170 
171  KeyVec& doubleVecKeys = gearParams->getDoubleVecKeys() ;
172  for(unsigned int i=0 ; i < doubleVecKeys.size() ; ++i ){
173 
174  TiXmlElement param("parameter") ;
175 
176  param.SetAttribute( "name", doubleVecKeys[i] ) ;
177  param.SetAttribute( "type", "DoubleVec" ) ;
178 
179  DoubleVec vec = gearParams->getDoubleVals( doubleVecKeys[i] ) ;
180 
181  std::stringstream str ;
182  // maintain the same level of precision as for single double values
183  str << std::scientific << std::setprecision(9) ;
184  for(unsigned int j=0 ; j < vec.size() ; ++j ){
185  if( j!=0 )
186  str << " " ;
187  str << vec[j] ;
188  }
189 
190  param.SetAttribute( "value", str.str() ) ;
191 
192  xmlElement->InsertEndChild( param ) ;
193 
194  }
195 
196  KeyVec& stringVecKeys = gearParams->getStringVecKeys() ;
197  for(unsigned int i=0 ; i < stringVecKeys.size() ; ++i ){
198 
199  TiXmlElement param("parameter") ;
200 
201  param.SetAttribute( "name", stringVecKeys[i] ) ;
202  param.SetAttribute( "type", "StringVec" ) ;
203 
204  StringVec vec = gearParams->getStringVals( stringVecKeys[i] ) ;
205  std::stringstream str ;
206  for(unsigned int j=0 ; j < vec.size() ; ++j ){
207  if( j!=0 )
208  str << " " ;
209  str << vec[j] ;
210  }
211  param.SetAttribute( "value", str.str() ) ;
212 
213  xmlElement->InsertEndChild( param ) ;
214 
215  }
216 
217  }
218 
220  GearParametersImpl* gearParams ){
221 
222 
223  const TiXmlNode* par = 0 ;
224  while( ( par = xmlElement->IterateChildren( "parameter", par ) ) != 0 ){
225 
226 
227 
228  // get the parameter value from attribute or contents of element
229 
230  std::string value("") ;
231 
232  try{
233 
234  value = getXMLAttribute( par , "value" ) ;
235  }
236  catch( ParseException ) {
237 
238  if( par->FirstChild() )
239  value = par->FirstChild()->Value() ;
240  }
241 
242 
243  std::vector<std::string> stringValues ;
244 
245  Tokenizer t( stringValues ,' ') ;
246 
247  std::for_each( value.begin(), value.end(), t ) ;
248 
249 
250  // fg: protect against empty value strings in the gear file
251  if( stringValues.size() == 0 )
252 
253  continue ;
254 
255 
256 
257  std::string name = getXMLAttribute( par, "name" ) ;
258  std::string type = getXMLAttribute( par, "type" ) ;
259 
260 
261  if( type == "int" ){
262 
263  gearParams->setIntVal( name , intVal( stringValues[0] ) ) ;
264 
265  } else if( type == "double" ){
266 
267  gearParams->setDoubleVal( name , doubleVal( stringValues[0] ) ) ;
268 
269  } else if( type == "string" ){
270 
271  gearParams->setStringVal( name , stringValues[0] ) ;
272 
273  } else if( type == "IntVec" ){
274 
275  IntVec v ;
276  transform( stringValues.begin() , stringValues.end() , back_inserter(v) , intVal ) ;
277  gearParams->setIntVals( name , v ) ;
278 
279 
280  } else if( type == "DoubleVec" ){
281 
282  DoubleVec v ;
283  transform( stringValues.begin() , stringValues.end() , back_inserter(v) , doubleVal ) ;
284  gearParams->setDoubleVals( name , v ) ;
285 
286  } else if( type == "StringVec" ){
287 
288  gearParams->setStringVals( name , stringValues ) ;
289 
290  }
291 
292  }
293 
294 
295  }
296 
297 
298 
299 // std::string GearParametersXML::getAttribute(const TiXmlNode* node , const std::string& name ) const {
300 
301 // const TiXmlElement* el = node->ToElement() ;
302 // if( el == 0 )
303 // throw ParseException("XMLParser::getAttribute not an XMLElement " ) ;
304 
305 // const char* at = el->Attribute( name ) ;
306 
307 // if( at == 0 ){
308 
309 // std::stringstream str ;
310 // str << "XMLParser::getAttribute missing attribute \"" << name
311 // << "\" in element <" << el->Value() << "/> " ;
312 // throw ParseException( str.str() ) ;
313 // }
314 
315 // return std::string( at ) ;
316 
317 // }
318 
319 
320 
321 
322 } // namespace
Implementation of GearParameters - a set off parameters that can be used to describe the geometrical ...
virtual const std::vector< std::string > & getIntVecKeys() const =0
All keys of IntVec variables.
virtual const std::vector< std::string > & getStringVecKeys() const =0
All keys of StringVec variables.
virtual void setIntVals(const std::string &key, const std::vector< int > &vals)
Integer values for key.
virtual void setDoubleVals(const std::string &key, const std::vector< double > &vals)
Double values for key.
virtual int getIntVal(const std::string &key) const =0
Integer value for key.
virtual const std::vector< std::string > & getDoubleKeys() const =0
All keys of double variables.
const char * Value() const
The meaning of &#39;value&#39; changes for the specific type of TiXmlNode.
Definition: tinyxml.h:437
virtual void setStringVals(const std::string &key, const std::vector< std::string > &vals)
String values for key.
static void setParametersFromXML(const TiXmlElement *xmlElement, GearParametersImpl *gearParams)
Static helper function that can be used by other subclass handlers to read parameters.
virtual const std::vector< std::string > & getIntKeys() const =0
All keys of int variables.
virtual const std::vector< double > & getDoubleVals(const std::string &key) const =0
Double values for key.
Abstract interface for a set of parameters that can be used to describe the geometrical properties of...
Helper class for XMLParser.
virtual const std::vector< std::string > & getStringKeys() const =0
All keys of string variables.
virtual void setGearParameters(const std::string &key, GearParameters *gearParameters)=0
Set named parameters for key.
static void getXMLForParameters(TiXmlElement *xmlElement, const GearParameters *gearParams)
Static helper function that can be used by other subclass handlers to create XML for parameters...
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:370
double doubleVal(const std::string &str)
helper method
ParseException used for parse errors, e.g.
Definition: GEAR.h:65
std::string getXMLAttribute(const TiXmlNode *node, const std::string &name)
Helper method used for parsing XML.
virtual GearParameters * fromXML(const TiXmlElement *xmlElement, GearMgr *gearMgr=0) const
Creates the appropriate GearParameters subclass from the given XML element (node) ...
virtual const std::vector< int > & getIntVals(const std::string &key) const =0
Integer values for key.
virtual const std::vector< std::string > & getDoubleVecKeys() const =0
All keys of DoubleVec variables.
The element is a container class.
Definition: tinyxml.h:827
virtual void setIntVal(const std::string &key, int val)
Set Integer value for key.
TiXmlNode * InsertEndChild(const TiXmlNode &addThis)
Add a new node related to this.
Definition: tinyxml.cc:218
virtual TiXmlElement toXML(const GearParameters &parameters) const
Creates an XML node for the given parameters.
void SetDoubleAttribute(const char *name, double value)
Sets an attribute of name to a given value.
Definition: tinyxml.cc:738
virtual const std::string & getStringVal(const std::string &key) const =0
String value for key.
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
Definition: tinyxml.h:466
virtual const std::vector< std::string > & getStringVals(const std::string &key) const =0
String values for key.
Abstract interface for a manager class that returns the Gear classes for the relevant subdetectors...
Definition: GearMgr.h:36
const TiXmlNode * IterateChildren(const TiXmlNode *previous) const
An alternate way to walk the children of a node.
Definition: tinyxml.cc:376
void SetAttribute(const char *name, const char *value)
Sets an attribute of name to a given value.
Definition: tinyxml.cc:746
virtual void setStringVal(const std::string &key, const std::string &val)
String value for key.
int intVal(const std::string &str)
helper method
virtual void setDoubleVal(const std::string &key, double val)
Double value for key.
virtual double getDoubleVal(const std::string &key) const =0
Double value for key.