GEAR  1.9.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
TPCParametersXML.cc
1 #include "gearxml/TPCParametersXML.h"
2 #include "gearimpl/TPCParametersImpl.h"
3 
4 #include "gearxml/XMLHandler.h"
5 #include "gearxml/GearParametersXML.h"
6 #include "gearxml/PadRowLayout2DXML.h"
7 
8 #include "gearxml/tinyxml.h"
9 #include "gear/GearMgr.h"
10 //modulesElement
11 #include "gear/PadRowLayout2D.h"
12 
13 #include <sstream>
14 
15 namespace gear {
16 
17 
19 
20  const TPCParameters *mTPC = dynamic_cast<const TPCParameters *>(&modularTPC);
21  if (!mTPC)
22  throw ParseException ("XML TPCParametersXML::toXML given parameter not of correct type. "
23  "Needs to be a gear::TPCParameter");
24 
25 
26  // append data to PadRowLayout2D
27  TiXmlElement modularTPCXML("detector");
28  modularTPCXML.SetAttribute("geartype","TPCParameters");
29 
30  TiXmlElement maxDriftLengthElement("maxDriftLength");
31  maxDriftLengthElement.SetDoubleAttribute( "value", mTPC->getMaxDriftLength() );
32  modularTPCXML.InsertEndChild(maxDriftLengthElement);
33 
34  TiXmlElement driftVelocityElement("driftVelocity");
35  driftVelocityElement.SetDoubleAttribute("value", mTPC->getDriftVelocity());
36  modularTPCXML.InsertEndChild(driftVelocityElement);
37 
38  TiXmlElement coordinateTypeElement("coordinateType");
39  switch( mTPC->getCoordinateType() )
40  {
41  case PadRowLayout2D::CARTESIAN :
42  coordinateTypeElement.SetAttribute( "value", "cartesian" ); break;
43  case PadRowLayout2D::POLAR :
44  coordinateTypeElement.SetAttribute( "value", "polar" ); break;
45  default:
46  throw ParseException("TPCParametersXML::toXML : Unknown coordinateType !");
47  }
48  modularTPCXML.InsertEndChild(coordinateTypeElement);
49 
50 // TiXmlElement nModulesElement("numberOfModules");
51 // maxDriftLengthElement.SetDoubleAttribute( "value", mTPC->getNModules() );
52 // modularTPCXML.InsertEndChild(nModulesElement);
53 
54  TiXmlElement modulesElement("modules");
55 
56  const std::vector<TPCModule *> modules = mTPC->getModules();
57  for ( std::vector<TPCModule *>::const_iterator moduleIter = modules.begin();
58  moduleIter < modules.end(); moduleIter++)
59  {
60  modulesElement.InsertEndChild( (_tpcModuleXML.toXML( *moduleIter ) ) );
61  }
62 
63  modularTPCXML.InsertEndChild( modulesElement );
64 
65  // Write all other parameters to detecotor as attributes
66  GearParametersXML::getXMLForParameters( &modularTPCXML , mTPC ) ;
67 
68  return modularTPCXML ;
69  }
70 
71 
73  GearMgr* gearMgr ) const
74  {
75  // debug information
76  //std::cout << "GEARDEBUG: " << "This is TPCParametersXML::fromXML" << std::endl;
77 
78  // a flag whether to use old or new syntax
79  bool oldsyntax=false;
80 
81  double maxDriftLength = atof( getChildElementValue( xmlElement , "maxDriftLength" ) .c_str() ) ;
82  int coordinateType;
83  std::string deaultString;
84  std::string typeString = getOptionalChildElementValue( xmlElement , "coordinateType" , deaultString);
85  if ( typeString.empty() )
86  {
87 // std::cout << "TPCParametersXML::fromXML : "
88 // << "No coordinate type given for TPCParameters,"
89 // << " switching to old, non-modular syntax (deprecated)" << std::endl;
90  oldsyntax = true;
91  coordinateType = PadRowLayout2D::CARTESIAN;
92  }
93  else
94  if ( typeString==std::string("cartesian") )
95  coordinateType = PadRowLayout2D::CARTESIAN;
96  else
97  if ( typeString==std::string("polar") )
98  coordinateType = PadRowLayout2D::POLAR;
99  else
100  throw ParseException("TPCParametersXML::fromXML : Unknown coordinateType !");
101 
102 
103  TPCParametersImpl* modularTPC = 0;
104 
105  // check for old syntax
106  if (oldsyntax)
107  {
108  // get the pad layout
109  const TiXmlElement* layout = xmlElement->FirstChildElement( "PadRowLayout2D" ) ;
110 
111  if( layout == 0 ) {
112 
113  std::stringstream str ;
114  str << "XMLParser::getChildElementValue missing element \"PadRowLayout2D\" "
115  << " in element <" << xmlElement->Value() << "/> " ;
116 
117  throw ParseException( str.str() ) ;
118  }
119 
120  std::string layoutType = getXMLAttribute( layout , "type" ) ;
121 
122  PadRowLayout2DXML* layoutXML = PadRowLayout2DXML::getHandler( layoutType ) ;
123 
124  if( layoutXML == 0 ) {
125 
126  throw ParseException( "TPCParametersXML::fromXML: no handler for " + layoutType + " found !" ) ;
127  }
128 
129  PadRowLayout2D* dLayout = layoutXML->fromXML( layout ) ;
130 
131  // we can only create the module here because now we know the type of coordinate system
132  modularTPC = new TPCParametersImpl( maxDriftLength , dLayout->getCoordinateType() ) ;
133 
134  modularTPC->setPadLayout( dLayout ) ;
135 
136  // the module has to be set first, now the frequency can be set
137  modularTPC->setDriftVelocity( atof( getChildElementValue( xmlElement, "driftVelocity" ).c_str() )) ;
138  modularTPC->setReadoutFrequency( atof( getChildElementValue( xmlElement, "readoutFrequency" ).c_str() )) ;
139 
140  }
141  else // new syntax with modules
142  {
143  // create the modular TPC
144  modularTPC = new TPCParametersImpl( maxDriftLength ,coordinateType ) ;
145 
146  // try to find a default module
147 // const TiXmlElement* defaultModuleElement = xmlElement->FirstChildElement("default");
148 // // no need to check whether the default module was found, the module parser can run without
149 // std::cout << "GEARDEBUG: " << "defaultModuleElement = "<<defaultModuleElement<< std::endl;
150 
151  // loop the modules section (there might be mor than one)
152  const TiXmlElement* modulesElement=0;
153  while( ( modulesElement =dynamic_cast<const TiXmlElement*>(
154  xmlElement->IterateChildren( "modules", modulesElement ) ) ) != 0 )
155  {
156  //std::cout << "GEARDEBUG: " << "TPCParametersXML::fromXML : found modules section" << std::endl;
157 
158  int moduleIDOffset=0;
159  int moduleIDStartCount = atoi( getOptionalXMLAttribute( modulesElement,
160  "moduleIDStartCount" ,
161  "0" ) .c_str() ) ;
162  //std::cout << "GEARDEBUG: " << "TPCParametersXML::fromXML : moduleIDStartCount = "
163  // <<moduleIDStartCount<< std::endl;
164 
165  // try to find a default module for this section
166  const TiXmlElement* defaultModuleElement = modulesElement->FirstChildElement("default");
167  // no need to check whether the default module was found, the module parser can run without
168  //std::cout << "GEARDEBUG: " << "defaultModuleElement = "<<defaultModuleElement<< std::endl;
169 
170  // Now loop the content of the modules section. It might be comments, "module" or
171  // "default" elements.
172  const TiXmlNode * moduleNode = 0;
173 
174  while( ( moduleNode = modulesElement->IterateChildren( moduleNode ) ) != 0 )
175  {
176  //std::cout << "GEARDEBUG: " << "looping modules "<< std::endl;
177 
178  // modulesElement (with s ) is for a block of (all) modules
179  // moduleElement (without s ) is for a single module
180  const TiXmlElement* moduleElement = dynamic_cast<const TiXmlElement*>(moduleNode);
181 
182  // Check if the dynamic cast succeeded. This is not the case for comments.
183  // In this case just continue with the next node.
184  if (moduleElement==0) continue;
185 
186  // Check if it is a new default module
187  if( std::string(moduleElement->Value()) == std::string("default") )
188  {
189  defaultModuleElement = moduleElement;
190  continue;
191  }
192 
193  // Now the element should be a module. If not give a warning and ignore it.
194  if ( std::string(moduleElement->Value()) != std::string("module") )
195  {
196  std::cout << "GEAR::TPCParametersXML: WARNING "
197  << " Unknown tag " << moduleNode->Value() << " in <modules> section."
198  << std::endl;
199  continue;
200  }
201 
202  // finaly create the module from the xml element and add it to the TPC
203  modularTPC->addModule (_tpcModuleXML.fromXML(moduleElement,
204  defaultModuleElement,
205  coordinateType,
206  moduleIDStartCount + moduleIDOffset++ ) );
207  }
208  }
209 
210  } // else (oldsyntax)
211 
212  // now read the generic parameters
213  GearParametersXML::setParametersFromXML( xmlElement, modularTPC ) ;
214 
215  if( gearMgr != 0 )
216  {
217  gearMgr->setTPCParameters( modularTPC ) ;
218  }
219 
220  //std::cout << "GEARDEBUG: " << "TPCParametersXML: vector size is "<< modularTPC->getModules().size() << std::endl;
221  return modularTPC ;
222  }// TPCParametersXML::fromXML
223 
224 }//namespace gear
225 
226 
virtual PadRowLayout2D * fromXML(const TiXmlElement *xmlElement) const =0
Creates the appropriate PadRowLayout2D subclass from the given XML element (node) ...
virtual int getCoordinateType() const =0
Returns coordinate type as an int (see PadRowLayout2D::CARTESIAN, PadRowLayout2D::POLAR) ...
virtual void setDriftVelocity(double driftVelocity)
Abstract description of a planar subdetector with pads (cells) that are positioned in rows (circular ...
std::string getOptionalXMLAttribute(const TiXmlNode *node, const std::string &name, const std::string &defaultValue)
Helper method used for parsing XML.
Proposal for an abstract interface that defines the geometry properties of a TPC like detector needed...
Definition: TPCParameters.h:24
virtual void setTPCParameters(TPCParameters *tpcParameters)=0
Set the TPCParameters.
virtual double getDriftVelocity() const =0
The electron drift velocity in the TPC in mm/s.
const char * Value() const
The meaning of &#39;value&#39; changes for the specific type of TiXmlNode.
Definition: tinyxml.h:437
std::string getOptionalChildElementValue(const TiXmlNode *node, const std::string &name, const std::string &defaultValue)
Helper method used for parsing XML - returns the attribute &#39;value&#39; of the optional named child elemen...
virtual const std::vector< TPCModule * > & getModules() const =0
Returns vector of all modules in this TPC (endplate).
virtual TiXmlElement toXML(const TPCModule *layout) const
Creates an XML node for the given TPCModule.
Definition: TPCModuleXML.cc:19
virtual GearParameters * fromXML(const TiXmlElement *xmlElement, GearMgr *gearMgr=0) const
Creates the appropriate TPCParameters class from the given XML element (node)
static void setParametersFromXML(const TiXmlElement *xmlElement, GearParametersImpl *gearParams)
Static helper function that can be used by other subclass handlers to read parameters.
Abstract interface for a set of parameters that can be used to describe the geometrical properties of...
virtual void setPadLayout(PadRowLayout2D *padLayout)
virtual int getCoordinateType() const =0
The type of the row layouts coordinate system: PadRowLayout2D.CARTESIAN or PadRowLayout2D.POLAR.
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
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.
static PadRowLayout2DXML * getHandler(const std::string &typeName)
Get handler for given type.
The element is a container class.
Definition: tinyxml.h:827
TiXmlNode * InsertEndChild(const TiXmlNode &addThis)
Add a new node related to this.
Definition: tinyxml.cc:218
virtual void setReadoutFrequency(double readoutFrequency)
A Container for TPCModules which describe the geometry properties of a given TPC. ...
void SetDoubleAttribute(const char *name, double value)
Sets an attribute of name to a given value.
Definition: tinyxml.cc:738
virtual void addModule(TPCModule *TPCModule)
Adds a Module to the vector of modules, or throws an exception.
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 double getMaxDriftLength() const =0
The maximum drift length in the TPC in mm.
virtual TPCModule * fromXML(const TiXmlElement *moduleElement, const TiXmlElement *defaultModuleElement, int tpcCoordinateType, int moduleID) const
Creates the appropriate TPCModule class from the given XML element (node)
Definition: TPCModuleXML.cc:73
std::string getChildElementValue(const TiXmlNode *node, const std::string &name)
Helper method used for parsing XML - returns the attribute &#39;value&#39; of the named child element as doub...
virtual TiXmlElement toXML(const GearParameters &modularTPC) const
Creates an XML node for the given TPCParameters.
const TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cc:482
Abstract XML handler for PadRowLayout2DXML.