GEAR  1.9.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
TPCModuleXML.cc
1 #include "gearxml/TPCModuleXML.h"
2 #include "gearimpl/TPCModuleImpl.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 
11 #include "gear/PadRowLayout2D.h"
12 
13 #include <sstream>
14 #include <typeinfo>
15 
16 namespace gear {
17 
18 
20 {
21  TiXmlElement tpcModuleXML( "module" );
22 
23  TiXmlElement moduleIDElement("moduleID");
24  moduleIDElement.SetAttribute( "value", module->getModuleID() );
25  tpcModuleXML.InsertEndChild(moduleIDElement);
26 
27  TiXmlElement readoutFrequencyElement("readoutFrequency");
28  readoutFrequencyElement.SetDoubleAttribute( "value", module->getReadoutFrequency() );
29  tpcModuleXML.InsertEndChild(readoutFrequencyElement);
30 
31  // cast to TPCModuleImpl so we can directly access the pad layout
32  // TPCModuleImpl is a friend of TPCModule so we can access the private variables
34  if( layoutXML == 0 )
35  {
36  throw ParseException( "TPCModuleXML::toXML: no handler for " + std::string(typeid(& module->getLocalPadLayout()).name()) + " found !" ) ;
37  }
38  tpcModuleXML.InsertEndChild( layoutXML->toXML( & module->getLocalPadLayout() ) );
39 
40 
41  TiXmlElement offsetElement("offset");
42  offsetElement.SetDoubleAttribute( "x_r", module->getOffset()[0] );
43  offsetElement.SetDoubleAttribute( "y_phi", module->getOffset()[1] );
44  tpcModuleXML.InsertEndChild(offsetElement);
45 
46  TiXmlElement zPositionElement("zPosition");
47  try
48  {
49  zPositionElement.SetDoubleAttribute( "value", module->getZPosition() );
50  tpcModuleXML.InsertEndChild(zPositionElement);
51  }
53  {
54  // debug output
55  // std::cout << "TPCModuleXML::toXML: Not writing z position because it was not set." << std::endl;
56  }
57 
58  TiXmlElement angleElement("angle");
59  angleElement.SetDoubleAttribute( "value", module->getAngle() );
60  tpcModuleXML.InsertEndChild(angleElement);
61 
62  TiXmlElement borderWidthElement("enlargeActiveAreaBy");
63  borderWidthElement.SetDoubleAttribute( "value", module->getBorderWidth() );
64  tpcModuleXML.InsertEndChild(borderWidthElement);
65 
66  // Write all other parameters to detecotor as attributes
67  GearParametersXML::getXMLForParameters( &tpcModuleXML , module ) ;
68 
69  return tpcModuleXML ;
70 }
71 
72 
74  const TiXmlElement* defaultModuleElement,
75  int tpcCoordinateType, int moduleID) const
76 {
77  // the values which have to be there: moduleID, readoutFrequency and the PadRowLayout2D
78 
79  const TiXmlElement* moduleIDElement = moduleElement->FirstChildElement( "moduleID" );
80  if ( ( moduleIDElement == 0 ) && defaultModuleElement) // tag has not been found
81  { //try to find it in the defaultModuleElement
82  moduleIDElement = defaultModuleElement->FirstChildElement( "moduleID" );
83  }
84  if ( moduleIDElement != 0 ) // tag has been found
85  {
86  moduleID = atoi( getXMLAttribute( moduleIDElement , "value" ).c_str() );
87  }
88  // if the module index has not been found at all it's ok, there is a default value
89 
90 
91  // the readout frequency
92  const TiXmlElement* readoutFrequencyElement = moduleElement->FirstChildElement( "readoutFrequency" );
93  if ( ( readoutFrequencyElement == 0 ) && defaultModuleElement) // tag has not been found
94  {
95  //try to find it in the defaultModuleElement
96  readoutFrequencyElement = defaultModuleElement->FirstChildElement( "readoutFrequency" );
97  }
98  if ( readoutFrequencyElement == 0 ) // tag still has not been found
99  {
100  throw ParseException("TPCModuleXML: No readoutFrequency in TPCModule" ) ;
101  }
102  double readoutFrequency = atoi( getXMLAttribute( readoutFrequencyElement , "value" ).c_str() );
103 
104  // get the pad layout
105  const TiXmlElement* padLayoutElement = moduleElement->FirstChildElement( "PadRowLayout2D" );
106  if( ( padLayoutElement == 0 ) && defaultModuleElement) // tag has not been found
107  {
108  //try to find it in the defaultModuleElement
109  padLayoutElement = defaultModuleElement->FirstChildElement( "PadRowLayout2D" );
110  }
111  if( padLayoutElement == 0 ) // tag still has not been found
112  {
113  throw ParseException( "TPCModuleXML: No PadRowLayout2D in TPCModule" );
114  }
115 
116  std::string layoutType = getXMLAttribute( padLayoutElement , "type" ) ;
117 
118  PadRowLayout2DXML* padLayoutXML = PadRowLayout2DXML::getHandler( layoutType ) ;
119  if( padLayoutXML == 0 )
120  {
121  throw ParseException( "TPCParametersXML::fromXML: no handler for " + layoutType + " found !" ) ;
122  }
123 
124  PadRowLayout2D* padLayout = padLayoutXML->fromXML( padLayoutElement ) ;
125 
126  // now everything is there to create the module instance
127 
128  TPCModuleImpl *tpcModule = new TPCModuleImpl( moduleID,
129  padLayout,
130  tpcCoordinateType,
131  readoutFrequency);
132 
133  // now search for the additional parameters: offset, angle, enlargeActiveAreaBy
134 
135  // offset
136  const TiXmlElement* offsetElement = moduleElement->FirstChildElement( "offset" );
137  if ( ( offsetElement == 0 ) && defaultModuleElement) // tag has not been found
138  { //try to find it in the defaultModuleElement
139  offsetElement = defaultModuleElement->FirstChildElement( "offset" );
140  }
141  if ( offsetElement != 0 ) // tag has been found
142  {
143  tpcModule->setOffset ( atof( getXMLAttribute( offsetElement , "x_r" ).c_str() ),
144  atof( getXMLAttribute( offsetElement , "y_phi" ).c_str() ) );
145  }
146 
147  // the z position
148  const TiXmlElement* zPositionElement = moduleElement->FirstChildElement( "zPosition" );
149  if ( ( zPositionElement == 0 ) && defaultModuleElement) // tag has not been found
150  {
151  //try to find it in the defaultModuleElement
152  zPositionElement = defaultModuleElement->FirstChildElement( "zPosition" );
153  }
154  if ( zPositionElement != 0 ) // tag has been found
155  {
156  tpcModule->setZPosition( atof( getXMLAttribute( zPositionElement , "value" ).c_str() ) );
157  }
158  // angle
159  const TiXmlElement* angleElement = moduleElement->FirstChildElement( "angle" );
160  if ( ( angleElement == 0 ) && defaultModuleElement )// tag has not been found
161  { //try to find it in the defaultModuleElement
162  angleElement = defaultModuleElement->FirstChildElement( "angle" );
163  }
164  if ( angleElement != 0 ) // tag has been found
165  {
166  tpcModule->setAngle ( atof( getXMLAttribute( angleElement , "value" ).c_str() ) );
167  }
168 
169  // enlargeActiveAreaBy
170  const TiXmlElement* borderWidthElement = moduleElement->FirstChildElement( "enlargeActiveAreaBy" );
171  if ( ( borderWidthElement == 0 ) && defaultModuleElement) // tag has not been found
172  { //try to find it in the defaultModuleElement
173  borderWidthElement = defaultModuleElement->FirstChildElement( "enlargeActiveAreaBy" );
174  }
175  if ( borderWidthElement != 0 ) // tag has been found
176  {
177  tpcModule->setBorderWidth ( atof( getXMLAttribute( borderWidthElement , "value" ).c_str() ) );
178  }
179 
180  // now read the generic parameters
181  GearParametersXML::setParametersFromXML( moduleElement, tpcModule ) ;
182 
183  return tpcModule;
184 }// TPCModuleXML::fromXML
185 
186 }//namespace gear
187 
virtual PadRowLayout2D * fromXML(const TiXmlElement *xmlElement) const =0
Creates the appropriate PadRowLayout2D subclass from the given XML element (node) ...
virtual TiXmlElement toXML(const PadRowLayout2D *layout) const =0
Creates an XML node for the given PadRowLayout2D.
void setOffset(double x_r, double y_phi)
Set the offset of the local pad plane wrt.
virtual double getZPosition() const =0
Returns the z position of the module.
Abstract description of a planar subdetector with pads (cells) that are positioned in rows (circular ...
virtual const Vector2D & getOffset() const =0
Returns the offest of Modules origin from the global origin A vector from Global (0,0) to module (0,0)
virtual TiXmlElement toXML(const TPCModule *layout) const
Creates an XML node for the given TPCModule.
Definition: TPCModuleXML.cc:19
static void setParametersFromXML(const TiXmlElement *xmlElement, GearParametersImpl *gearParams)
Static helper function that can be used by other subclass handlers to read parameters.
virtual const PadRowLayout2D & getLocalPadLayout() const =0
Returns a reference to the instance of the underlaying pad layout.
virtual double getReadoutFrequency() const =0
The readout frequency in Hz.
static void getXMLForParameters(TiXmlElement *xmlElement, const GearParameters *gearParams)
Static helper function that can be used by other subclass handlers to create XML for parameters...
void setZPosition(double z)
Set the z position of the module.
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 int getModuleID() const =0
Returns module ID.
void SetDoubleAttribute(const char *name, double value)
Sets an attribute of name to a given value.
Definition: tinyxml.cc:738
virtual double getAngle() const =0
Returns the rotation of the module, in Rads, with respect to the modules internal origin...
virtual double getBorderWidth() const =0
Returns the amount by which the pad plane has been extended to produce the module plane...
void SetAttribute(const char *name, const char *value)
Sets an attribute of name to a given value.
Definition: tinyxml.cc:746
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
An exception that is special for the TPCModule.
Definition: TPCModule.h:48
const TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cc:482
A wrapper Class for PadRowLayout2D which converts between the actual pad layouts local coodinate syst...
Definition: TPCModule.h:41
A wrapper Class for PadRowLayout2D, allowing which converts between local and global coordinate syste...
Definition: TPCModuleImpl.h:23
Abstract XML handler for PadRowLayout2DXML.