GEAR  1.9.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
CalorimeterParametersXML.cc
1 #include "gearxml/CalorimeterParametersXML.h"
2 
3 #include "gearxml/XMLHandlerMgr.h"
4 #include "gearxml/GearParametersXML.h"
5 
6 #include "gearxml/tinyxml.h"
7 #include "gearimpl/CalorimeterParametersImpl.h"
8 // #include "gear/LayerLayout.h"
9 #include "gear/GearMgr.h"
10 
11 #include "gearimpl/FixedPadSizeDiskLayout.h"
12 
13 #include <vector>
14 #include <algorithm>
15 #include <sstream>
16 #include <math.h>
17 #include <string>
18 
19 #define _EPSILON 0.0001
20 
21 
22 namespace gear {
23 
24 
26 
27  //check wheter parameter is valid CalorimeterParameter
28  const CalorimeterParameters* param = dynamic_cast<const CalorimeterParameters*> ( &parameters );
29 
30  if( param == 0 ) {
31 
32  throw Exception( "CaloriemterParametersXML::toXML given parmeters not of correct type. "
33  "needs to be gear::CaloriemeterParameters." );
34  }
35 
36  //Set up Caloriemter-Detector as Element
37  TiXmlElement det("detector");
38 
39  //Dimension
40  const std::vector<double> vecDim=param->getExtent();
41  TiXmlElement dimensions("dimensions");
42 
43  //Layout
44  TiXmlElement layout("layout");
45 
46  switch( param->getLayoutType() ) {
47 
48  case CalorimeterParameters::BARREL :
49 
50  layout.SetAttribute("type","Barrel");
51  dimensions.SetDoubleAttribute("inner_r",vecDim[0]);
52  dimensions.SetDoubleAttribute("outer_z",vecDim[3]);
53  break;
54 
55  case CalorimeterParameters::ENDCAP :
56 
57  layout.SetAttribute("type","Endcap");
58  dimensions.SetDoubleAttribute("inner_r",vecDim[0]);
59  dimensions.SetDoubleAttribute("outer_r",vecDim[1]);
60  dimensions.SetDoubleAttribute("inner_z",vecDim[2]);
61  break;
62 
63  default:
64 
65  layout.SetAttribute("type","Unknown");
66  dimensions.SetDoubleAttribute("inner_r",vecDim[0]);
67  dimensions.SetDoubleAttribute("outer_r",vecDim[1]);
68  dimensions.SetDoubleAttribute("inner_z",vecDim[2]);
69  dimensions.SetDoubleAttribute("outer_z",vecDim[3]);
70  break;
71  }
72 
73  layout.SetAttribute("symmetry",param->getSymmetryOrder());
74  layout.SetDoubleAttribute("phi0",param->getPhi0());
75 
76 
77  //Assemble first parts
78  det.InsertEndChild(layout);
79  det.InsertEndChild(dimensions);
80 
81  //LayerLayout
82  const LayerLayout& layerLayout = param->getLayerLayout();
83 
84  //<------Go through layers to find out how to repeat
85  // prepare currentValues for each repeater
86  double curThickness = 0.0 ;
87  double curAbsorber = 0.0 ;
88  double curCell0 = 0.0 ;
89  double curCell1 = 0.0 ;
90 
91  int repN = 0 ;
92 
93  for(int i=0; i<layerLayout.getNLayers();i++) {
94 
95  // check if anything has changed
96 
97  bool layerChanged =
98  ( ! isEqual( curThickness, layerLayout.getThickness(i) ) ) ||
99  ( ! isEqual( curAbsorber, layerLayout.getAbsorberThickness(i) ) ) ||
100  ( ! isEqual( curCell0, layerLayout.getCellSize0(i) ) ) ||
101  ( ! isEqual( curCell1, layerLayout.getCellSize1(i) ) ) ;
102 
103  if( layerChanged && repN > 0 ) {
104 
105  //Write parameters
106 
107  TiXmlElement layer("layer");
108 
109  layer.SetAttribute("repeat",repN);
110 
111  layer.SetDoubleAttribute("thickness",curThickness);
112  layer.SetDoubleAttribute("absorberThickness",curAbsorber);
113  layer.SetDoubleAttribute("cellSize0",curCell0);
114  layer.SetDoubleAttribute("cellSize1",curCell1);
115 
116  det.InsertEndChild(layer);
117 
118  repN = 1;
119 
120  } else {
121 
122  repN += 1;
123  }
124 
125  // take on new values
126  curThickness = layerLayout.getThickness(i);
127  curAbsorber = layerLayout.getAbsorberThickness(i);
128  curCell0 = layerLayout.getCellSize0(i);
129  curCell1 = layerLayout.getCellSize1(i);
130  }
131 
132 
133 
134  //Assemble last Repeater
135  if ( repN > 0 ) {
136 
137  //Write parameters
138  TiXmlElement layer("layer");
139  layer.SetAttribute("repeat",repN);
140  layer.SetDoubleAttribute("thickness",curThickness);
141  layer.SetDoubleAttribute("absorberThickness",curAbsorber);
142  layer.SetDoubleAttribute("cellSize0",curCell0);
143  layer.SetDoubleAttribute("cellSize1",curCell1);
144  det.InsertEndChild(layer);
145  }
146  //Assemble Detector
147 
148  GearParametersXML::getXMLForParameters( &det , &parameters ) ;
149 
150  return det ;
151  }
152 
153 
155 
156 
157  // ---- layout -----
158 
159  const TiXmlElement* layout = xmlElement->FirstChildElement( "layout" ) ;
160 
161  if( layout == 0 ) {
162 
163  std::stringstream str ;
164  str << "XMLParser::getChildElementValue missing element \"layout\" "
165  << " in element <" << xmlElement->Value() << "/> " ;
166 
167  throw ParseException( str.str() ) ;
168  }
169 
170  std::string layoutType = getXMLAttribute( layout , "type" ) ;
171 
172  std::transform( layoutType.begin() , layoutType.end() , layoutType.begin(), tolower ) ;
173 
174  if( layoutType != "barrel" && layoutType != "endcap" ) {
175 
176  throw ParseException( "CalorimeterParametersXML::toXML: currently only barrel and endcap type layouts implemented ! " ) ;
177  }
178 
179 
180  // ---- dimensions -----
181  const TiXmlElement* dimensions = xmlElement->FirstChildElement( "dimensions" ) ;
182 
183  if( dimensions == 0 ) {
184 
185  std::stringstream str ;
186  str << "XMLParser::getChildElementValue missing element \"dimensions\" "
187  << " in element <" << xmlElement->Value() << "/> " ;
188 
189  throw ParseException( str.str() ) ;
190  }
191 
192  int symmetry = atoi( getXMLAttribute( layout , "symmetry" ) .c_str() ) ;
193  double phi0 = atof( getXMLAttribute( layout , "phi0" ) .c_str() ) ;
194 
195  CalorimeterParametersImpl* caloParams = 0 ;
196 
197  double firstLayerDistance = 0. ;
198  bool isBarrelCalorimeter = false ;
199 
200  if( layoutType == "barrel" ) {
201 
202  isBarrelCalorimeter = true ;
203 
204  // barrel dimensions defined through inner_r and outer_z
205  double rMin = atof( getXMLAttribute( dimensions , "inner_r" ) .c_str() ) ;
206  double zMax = atof( getXMLAttribute( dimensions , "outer_z" ) .c_str() ) ;
207 
208  caloParams = new CalorimeterParametersImpl( rMin, zMax, symmetry , phi0 ) ;
209 
210  firstLayerDistance = rMin ;
211 
212  } else if( layoutType == "endcap" ) {
213 
214  // endcap dimensions defined through inner_r, outer_r and inner_z
215  double rMin = atof( getXMLAttribute( dimensions , "inner_r" ) .c_str() ) ;
216  double rMax = atof( getXMLAttribute( dimensions , "outer_r" ) .c_str() ) ;
217  double zMin = atof( getXMLAttribute( dimensions , "inner_z" ) .c_str() ) ;
218 
219  caloParams = new CalorimeterParametersImpl( rMin, rMax, zMin, symmetry , phi0 ) ;
220 
221  firstLayerDistance = zMin ;
222 
223  }
224 
225  // now we can read the generic parameters
226  GearParametersXML::setParametersFromXML( xmlElement, caloParams ) ;
227 
228 
229  // ----- layers ------------
230  int layerId = 0 ;
231 
232  const TiXmlNode* layer = 0 ;
233  while( ( layer = xmlElement->IterateChildren( "layer", layer ) ) != 0 ){
234 
235  int nLayer = atoi( getXMLAttribute( layer , "repeat" ) .c_str() ) ;
236  double thickness = atof( getXMLAttribute( layer , "thickness" ) .c_str() ) ;
237  double absorberThickness = atof( getXMLAttribute( layer , "absorberThickness" ) .c_str() ) ;
238  double cellSize0 = atof( getXMLAttribute( layer , "cellSize0" ) .c_str() ) ;
239  double cellSize1 = atof( getXMLAttribute( layer , "cellSize1" ) .c_str() ) ;
240 
241  for(int i=0 ; i < nLayer ; ++i ) {
242 
243  if( layerId == 0 )
244  caloParams->layerLayout().positionLayer( firstLayerDistance , thickness,
245  cellSize0, cellSize1, absorberThickness ) ;
246  else
247  caloParams->layerLayout().addLayer( thickness,
248  cellSize0, cellSize1, absorberThickness ) ;
249 
250  ++ layerId ;
251  }
252  }
253 
254 
255 
256  //--------- add to proper section in GearMgr ----
257  if( gearMgr != 0 ) {
258 
259  std::string caloName = getXMLAttribute( xmlElement , "name" ) ;
260 
261 // bool isEcal = ( caloName[0] == 'E' || caloName[0] == 'e' ) ;
262 
263  std::transform( caloName.begin() , caloName.end() ,
264  caloName.begin(), tolower ) ;
265 
266 
267  if( caloName.find("ecal") != std::string::npos ){
268 
269  if( caloName.find("plug" ) != std::string::npos ){
270 
271  gearMgr->setEcalPlugParameters( caloParams ) ;
272  }
273  else if( isBarrelCalorimeter )
274  gearMgr->setEcalBarrelParameters( caloParams ) ;
275  else
276  gearMgr->setEcalEndcapParameters( caloParams ) ;
277 
278  } else if( caloName.find("yoke") != std::string::npos ){
279 
280  if( caloName.find("plug" ) != std::string::npos ){
281 
282  gearMgr->setYokePlugParameters( caloParams ) ;
283  }
284  else if( isBarrelCalorimeter )
285  gearMgr->setYokeBarrelParameters( caloParams ) ;
286  else
287  gearMgr->setYokeEndcapParameters( caloParams ) ;
288 
289  } else if( caloName.find("lcal" ) != std::string::npos ) {
290 
291  gearMgr->setLcalParameters( caloParams ) ;
292 
293  } else if( caloName.find("lhcal" ) != std::string::npos ) {
294 
295  gearMgr->setLHcalParameters( caloParams ) ;
296 
297  } else if( caloName.find("beamcal" ) != std::string::npos ) {
298 
299  gearMgr->setBeamCalParameters( caloParams ) ;
300 
301  } else if( caloName.find("hcal" ) != std::string::npos ) {
302 
303  // the additional hcal ring might be endcap or barrel like..
304  if( caloName.find("ring" ) != std::string::npos ){
305 
306  gearMgr->setHcalRingParameters( caloParams ) ;
307  }
308  else if( isBarrelCalorimeter )
309  gearMgr->setHcalBarrelParameters( caloParams ) ;
310  else
311  gearMgr->setHcalEndcapParameters( caloParams ) ;
312 
313  }
314  }
315 
316  return caloParams ;
317  }
318 
319 
320  bool CalorimeterParametersXML::isEqual( const double valueOne, const double valueTwo ) const {
321 
322  // save calculating time if equal
323  if ( valueOne == valueTwo ) return true ;
324 
325  // get avg value and calculate maximum allowed delta
326  double maxDelta = fabs( ( valueOne + valueTwo ) / 2 * _EPSILON ) ;
327 
328  // check if values differ by less than maximal delta
329  bool differsLess = ( fabs( valueOne - valueTwo ) < maxDelta ) ;
330 
331  // return
332  return differsLess ;
333  }
334 
335 } // namespace
virtual int getLayoutType() const =0
The type of the calorimeter layout: CalorimeterParameters.BARREL or CalorimeterParameters.ENDCAP.
virtual void setEcalEndcapParameters(CalorimeterParameters *ecalEndcapParameters)=0
Set the EcalEndcapParameters.
Base exception class for GEAR - all other exceptions extend this.
Definition: GEAR.h:41
const char * Value() const
The meaning of &#39;value&#39; changes for the specific type of TiXmlNode.
Definition: tinyxml.h:437
Proposal for an abstract interface that defines geometry properties of a typical sampling calorimeter...
virtual double getAbsorberThickness(int layerIndex) const =0
The thickness of the absorber part of the layer layerIndex - layer indexing starts at 0 for the layer...
Abstract description of a layered layout detector - typically a a sampling calorimeter.
Definition: LayerLayout.h:15
virtual double getCellSize0(int layerIndex) const =0
The cell size along the first axis where first is either along the beam (barrel type) or up (endcap) ...
virtual void setEcalPlugParameters(CalorimeterParameters *ecalPlugParameters)=0
Set the EcalPlugParameters.
virtual const std::vector< double > & getExtent() const =0
Extent of the calorimeter in the r-z-plane [ rmin, rmax, zmin, zmax ] in mm.
virtual void setHcalBarrelParameters(CalorimeterParameters *hcalBarrelParameters)=0
Set the HcalBarrelParameters.
virtual void setYokeBarrelParameters(CalorimeterParameters *ecalBarrelParameters)=0
Set the YokeBarrelParameters.
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 double getThickness(int layerIndex) const =0
The thickness of the layer layerIndex - layer indexing starts at 0 for the layer closest to the origi...
virtual void positionLayer(double distance, double thickness, double cellSize0, double cellSize1, double absorberThickness=0.)
Position new layer at the given distance (after the last layer) - typically this will be used for the...
virtual int getNLayers() const =0
The total number of layers.
static void getXMLForParameters(TiXmlElement *xmlElement, const GearParameters *gearParams)
Static helper function that can be used by other subclass handlers to create XML for parameters...
virtual void setBeamCalParameters(CalorimeterParameters *beamcalParameters)=0
Set the BeamCalParameters.
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:370
virtual void setHcalEndcapParameters(CalorimeterParameters *hcalEndcapParameters)=0
Set the HcalEndcapParameters.
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 void setHcalRingParameters(CalorimeterParameters *hcalRingParameters)=0
Set the HcalRingParameters.
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 setLHcalParameters(CalorimeterParameters *lhcalParameters)=0
Set the LHcalParameters.
virtual TiXmlElement toXML(const GearParameters &parameters) const
Creates an XML node for the given parameters.
virtual const LayerLayout & getLayerLayout() const =0
The layer layout in the calorimeter.
virtual void setYokePlugParameters(CalorimeterParameters *ecalPlugParameters)=0
Set the YokePlugParameters.
virtual void addLayer(double thickness, double cellSize0, double cellSize1, double absorberThickness=0.)
Add a new layer right after the last layer - at distance 0.
void SetDoubleAttribute(const char *name, double value)
Sets an attribute of name to a given value.
Definition: tinyxml.cc:738
virtual void setYokeEndcapParameters(CalorimeterParameters *ecalEndcapParameters)=0
Set the YokeEndcapParameters.
virtual int getSymmetryOrder() const =0
The order of the rotational symmetry 8 for an octagonal barrel calorimeter 2 for an endcap calorim...
virtual GearParameters * fromXML(const TiXmlElement *xmlElement, GearMgr *gearMgr=0) const
Creates the appropriate GearParameters subclass from the given XML element (node) ...
virtual double getCellSize1(int layerIndex) const =0
The second cell size, perpendicular to the first direction (getCellSize0()) and the depth of the laye...
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
virtual void setEcalBarrelParameters(CalorimeterParameters *ecalBarrelParameters)=0
Set the EcalBarrelParameters.
void SetAttribute(const char *name, const char *value)
Sets an attribute of name to a given value.
Definition: tinyxml.cc:746
const TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cc:482
virtual void setLcalParameters(CalorimeterParameters *lcalParameters)=0
Set the LcalParameters.
virtual double getPhi0() const =0
The azimuthal angle of the first module if symmetryOrder &gt; 0.