ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4GDMLReadDefine.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4GDMLReadDefine.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // class G4GDMLReadDefine Implementation
28 //
29 // Original author: Zoltan Torzsok, November 2007
30 //
31 // --------------------------------------------------------------------
32 
33 #include "G4GDMLReadDefine.hh"
34 #include "G4UnitsTable.hh"
35 
37  : m(0), rows(0), cols(0)
38 {
39 }
40 
41 G4GDMLMatrix::G4GDMLMatrix(size_t rows0, size_t cols0)
42 {
43  if ((rows0==0) || (cols0==0))
44  {
45  G4Exception("G4GDMLMatrix::G4GDMLMatrix(r,c)", "InvalidSetup",
46  FatalException, "Zero indeces as arguments!?");
47  }
48  rows = rows0;
49  cols = cols0;
50  m = new G4double[rows*cols];
51 }
52 
54  : m(0), rows(0), cols(0)
55 {
56  if (rhs.m)
57  {
58  rows = rhs.rows;
59  cols = rhs.cols;
60  m = new G4double[rows*cols];
61  for (size_t i=0; i<rows*cols; i++) { m[i] = rhs.m[i]; }
62  }
63 }
64 
66 {
67  // Check assignment to self
68  //
69  if (this == &rhs) { return *this; }
70 
71  // Copy data
72  //
73  rows = rhs.rows;
74  cols = rhs.cols;
75  if (rhs.m)
76  {
77  m = new G4double[rows*cols];
78  for (size_t i=0; i<rows*cols; i++) { m[i] = rhs.m[i]; }
79  }
80  else
81  {
82  m = 0;
83  }
84 
85  return *this;
86 }
87 
89 {
90  delete [] m;
91 }
92 
93 void G4GDMLMatrix::Set(size_t r,size_t c,G4double a)
94 {
95  if (r>=rows || c>=cols)
96  {
97  G4Exception("G4GDMLMatrix::set()", "InvalidSetup",
98  FatalException, "Index out of range!");
99  }
100  m[cols*r+c] = a;
101 }
102 
103 G4double G4GDMLMatrix::Get(size_t r,size_t c) const
104 {
105  if (r>=rows || c>=cols)
106  {
107  G4Exception("G4GDMLMatrix::get()", "InvalidSetup",
108  FatalException, "Index out of range!");
109  }
110  return m[cols*r+c];
111 }
112 
113 size_t G4GDMLMatrix::GetRows() const
114 {
115  return rows;
116 }
117 
118 size_t G4GDMLMatrix::GetCols() const
119 {
120  return cols;
121 }
122 
124 {
125 }
126 
128 {
129 }
130 
133 {
134  G4RotationMatrix rot;
135 
136  rot.rotateX(angles.x());
137  rot.rotateY(angles.y());
138  rot.rotateZ(angles.z());
139  rot.rectify(); // Rectify matrix from possible roundoff errors
140 
141  return rot;
142 }
143 
144 void
145 G4GDMLReadDefine::ConstantRead(const xercesc::DOMElement* const constantElement)
146 {
147  G4String name = "";
148  G4double value = 0.0;
149 
150  const xercesc::DOMNamedNodeMap* const attributes
151  = constantElement->getAttributes();
152  XMLSize_t attributeCount = attributes->getLength();
153 
154  for (XMLSize_t attribute_index=0;
155  attribute_index<attributeCount; attribute_index++)
156  {
157  xercesc::DOMNode* node = attributes->item(attribute_index);
158 
159  if (node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE) { continue; }
160 
161  const xercesc::DOMAttr* const attribute
162  = dynamic_cast<xercesc::DOMAttr*>(node);
163  if (!attribute)
164  {
165  G4Exception("G4GDMLRead::ConstantRead()", "InvalidRead",
166  FatalException, "No attribute found!");
167  return;
168  }
169  const G4String attName = Transcode(attribute->getName());
170  const G4String attValue = Transcode(attribute->getValue());
171 
172  if (attName=="name") { name = attValue; } else
173  if (attName=="value") { value = eval.Evaluate(attValue); }
174  }
175 
176  eval.DefineConstant(name,value);
177 }
178 
179 void
180 G4GDMLReadDefine::ExpressionRead(const xercesc::DOMElement* const expElement)
181 {
182  G4String name = "";
183  G4double value = 0.0;
184 
185  const xercesc::DOMNamedNodeMap* const attributes
186  = expElement->getAttributes();
187  XMLSize_t attributeCount = attributes->getLength();
188 
189  for (XMLSize_t attribute_index=0;
190  attribute_index<attributeCount; attribute_index++)
191  {
192  xercesc::DOMNode* node = attributes->item(attribute_index);
193 
194  if (node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE) { continue; }
195 
196  const xercesc::DOMAttr* const attribute
197  = dynamic_cast<xercesc::DOMAttr*>(node);
198  if (!attribute)
199  {
200  G4Exception("G4GDMLRead::ExpressionRead()", "InvalidRead",
201  FatalException, "No attribute found!");
202  return;
203  }
204  const G4String attName = Transcode(attribute->getName());
205  const G4String attValue = Transcode(attribute->getValue());
206 
207  if (attName=="name") { name = attValue; }
208  }
209 
210  const G4String expValue = Transcode(expElement->getTextContent());
211  value = eval.Evaluate(expValue);
212  eval.DefineConstant(name,value);
213 }
214 
215 void
216 G4GDMLReadDefine::MatrixRead(const xercesc::DOMElement* const matrixElement)
217 {
218  G4String name = "";
219  G4int coldim = 0;
220  G4String values = "";
221 
222  const xercesc::DOMNamedNodeMap* const attributes
223  = matrixElement->getAttributes();
224  XMLSize_t attributeCount = attributes->getLength();
225 
226  for (XMLSize_t attribute_index=0;
227  attribute_index<attributeCount; attribute_index++)
228  {
229  xercesc::DOMNode* node = attributes->item(attribute_index);
230 
231  if (node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE) { continue; }
232 
233  const xercesc::DOMAttr* const attribute
234  = dynamic_cast<xercesc::DOMAttr*>(node);
235  if (!attribute)
236  {
237  G4Exception("G4GDMLRead::MatrixRead()", "InvalidRead",
238  FatalException, "No attribute found!");
239  return;
240  }
241  const G4String attName = Transcode(attribute->getName());
242  const G4String attValue = Transcode(attribute->getValue());
243 
244  if (attName=="name") { name = GenerateName(attValue); } else
245  if (attName=="coldim") { coldim = eval.EvaluateInteger(attValue); } else
246  if (attName=="values") { values = attValue; }
247  }
248 
249  std::stringstream MatrixValueStream(values);
250  std::vector<G4double> valueList;
251 
252  while (!MatrixValueStream.eof())
253  {
254  G4String MatrixValue;
255  MatrixValueStream >> MatrixValue;
256  valueList.push_back(eval.Evaluate(MatrixValue));
257  }
258 
259  eval.DefineMatrix(name,coldim,valueList);
260 
261  G4GDMLMatrix matrix(valueList.size()/coldim,coldim);
262 
263  for (size_t i=0;i<valueList.size();i++)
264  {
265  matrix.Set(i/coldim,i%coldim,valueList[i]);
266  }
267 
268  matrixMap[name] = matrix;
269 }
270 
271 void
272 G4GDMLReadDefine::PositionRead(const xercesc::DOMElement* const positionElement)
273 {
274  G4String name = "";
275  G4double unit = 1.0;
276  G4ThreeVector position(0.,0.,0.);
277 
278  const xercesc::DOMNamedNodeMap* const attributes
279  = positionElement->getAttributes();
280  XMLSize_t attributeCount = attributes->getLength();
281 
282  for (XMLSize_t attribute_index=0;
283  attribute_index<attributeCount; attribute_index++)
284  {
285  xercesc::DOMNode* node = attributes->item(attribute_index);
286 
287  if (node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE) { continue; }
288 
289  const xercesc::DOMAttr* const attribute
290  = dynamic_cast<xercesc::DOMAttr*>(node);
291  if (!attribute)
292  {
293  G4Exception("G4GDMLRead::PositionRead()", "InvalidRead",
294  FatalException, "No attribute found!");
295  return;
296  }
297  const G4String attName = Transcode(attribute->getName());
298  const G4String attValue = Transcode(attribute->getValue());
299 
300  if (attName=="name") { name = GenerateName(attValue); } else
301  if (attName=="unit") { unit = G4UnitDefinition::GetValueOf(attValue);
302  if (G4UnitDefinition::GetCategory(attValue)!="Length") {
303  G4Exception("G4GDMLReadDefine::PositionRead()", "InvalidRead",
304  FatalException, "Invalid unit for length!"); }
305  } else
306  if (attName=="x") { position.setX(eval.Evaluate(attValue)); } else
307  if (attName=="y") { position.setY(eval.Evaluate(attValue)); } else
308  if (attName=="z") { position.setZ(eval.Evaluate(attValue)); }
309  }
310 
311  positionMap[name] = position*unit;
312 }
313 
314 void
315 G4GDMLReadDefine::RotationRead(const xercesc::DOMElement* const rotationElement)
316 {
317  G4String name = "";
318  G4double unit = 1.0;
319  G4ThreeVector rotation(0.,0.,0.);
320 
321  const xercesc::DOMNamedNodeMap* const attributes
322  = rotationElement->getAttributes();
323  XMLSize_t attributeCount = attributes->getLength();
324 
325  for (XMLSize_t attribute_index=0;
326  attribute_index<attributeCount; attribute_index++)
327  {
328  xercesc::DOMNode* node = attributes->item(attribute_index);
329 
330  if (node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE) { continue; }
331 
332  const xercesc::DOMAttr* const attribute
333  = dynamic_cast<xercesc::DOMAttr*>(node);
334  if (!attribute)
335  {
336  G4Exception("G4GDMLRead::RotationRead()", "InvalidRead",
337  FatalException, "No attribute found!");
338  return;
339  }
340  const G4String attName = Transcode(attribute->getName());
341  const G4String attValue = Transcode(attribute->getValue());
342 
343  if (attName=="name") { name = GenerateName(attValue); } else
344  if (attName=="unit") { unit = G4UnitDefinition::GetValueOf(attValue);
345  if (G4UnitDefinition::GetCategory(attValue)!="Angle") {
346  G4Exception("G4GDMLReadDefine::RotationRead()", "InvalidRead",
347  FatalException, "Invalid unit for angle!"); }
348  } else
349  if (attName=="x") { rotation.setX(eval.Evaluate(attValue)); } else
350  if (attName=="y") { rotation.setY(eval.Evaluate(attValue)); } else
351  if (attName=="z") { rotation.setZ(eval.Evaluate(attValue)); }
352  }
353 
354  rotationMap[name] = rotation*unit;
355 }
356 
357 void G4GDMLReadDefine::ScaleRead(const xercesc::DOMElement* const scaleElement)
358 {
359  G4String name = "";
360  G4ThreeVector scale(1.0,1.0,1.0);
361 
362  const xercesc::DOMNamedNodeMap* const attributes
363  = scaleElement->getAttributes();
364  XMLSize_t attributeCount = attributes->getLength();
365 
366  for (XMLSize_t attribute_index=0;
367  attribute_index<attributeCount; attribute_index++)
368  {
369  xercesc::DOMNode* node = attributes->item(attribute_index);
370 
371  if (node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE) { continue; }
372 
373  const xercesc::DOMAttr* const attribute
374  = dynamic_cast<xercesc::DOMAttr*>(node);
375  if (!attribute)
376  {
377  G4Exception("G4GDMLRead::ScaleRead()", "InvalidRead",
378  FatalException, "No attribute found!");
379  return;
380  }
381  const G4String attName = Transcode(attribute->getName());
382  const G4String attValue = Transcode(attribute->getValue());
383 
384  if (attName=="name") { name = GenerateName(attValue); } else
385  if (attName=="x") { scale.setX(eval.Evaluate(attValue)); } else
386  if (attName=="y") { scale.setY(eval.Evaluate(attValue)); } else
387  if (attName=="z") { scale.setZ(eval.Evaluate(attValue)); }
388  }
389 
390  scaleMap[name] = scale;
391 }
392 
393 void
394 G4GDMLReadDefine::VariableRead(const xercesc::DOMElement* const variableElement)
395 {
396  G4String name = "";
397  G4double value = 0.0;
398 
399  const xercesc::DOMNamedNodeMap* const attributes
400  = variableElement->getAttributes();
401  XMLSize_t attributeCount = attributes->getLength();
402 
403  for (XMLSize_t attribute_index=0;
404  attribute_index<attributeCount; attribute_index++)
405  {
406  xercesc::DOMNode* node = attributes->item(attribute_index);
407 
408  if (node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE) { continue; }
409 
410  const xercesc::DOMAttr* const attribute
411  = dynamic_cast<xercesc::DOMAttr*>(node);
412  if (!attribute)
413  {
414  G4Exception("G4GDMLRead::VariableRead()", "InvalidRead",
415  FatalException, "No attribute found!");
416  return;
417  }
418  const G4String attName = Transcode(attribute->getName());
419  const G4String attValue = Transcode(attribute->getValue());
420 
421  if (attName=="name") { name = attValue; } else
422  if (attName=="value") { value = eval.Evaluate(attValue); }
423  }
424 
425  eval.DefineVariable(name,value);
426 }
427 
428 void G4GDMLReadDefine::QuantityRead(const xercesc::DOMElement* const element)
429 {
430  G4String name = "";
431  G4double unit = 1.0;
432  G4double value = 0.0;
433 
434  const xercesc::DOMNamedNodeMap* const attributes
435  = element->getAttributes();
436  XMLSize_t attributeCount = attributes->getLength();
437 
438  for (XMLSize_t attribute_index=0;
439  attribute_index<attributeCount; attribute_index++)
440  {
441  xercesc::DOMNode* node = attributes->item(attribute_index);
442 
443  if (node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE) { continue; }
444 
445  const xercesc::DOMAttr* const attribute
446  = dynamic_cast<xercesc::DOMAttr*>(node);
447  if (!attribute)
448  {
449  G4Exception("G4GDMLRead::QuantityRead()", "InvalidRead",
450  FatalException, "No attribute found!");
451  return;
452  }
453  const G4String attName = Transcode(attribute->getName());
454  const G4String attValue = Transcode(attribute->getValue());
455 
456  if (attName=="name") { name = attValue; } else
457  if (attName=="value") { value = eval.Evaluate(attValue); } else
458  if (attName=="unit") { unit = G4UnitDefinition::GetValueOf(attValue); }
459  }
460 
461  quantityMap[name] = value*unit;
462  eval.DefineConstant(name,value*unit);
463 }
464 
465 void
466 G4GDMLReadDefine::DefineRead(const xercesc::DOMElement* const defineElement)
467 {
468 #ifdef G4VERBOSE
469  G4cout << "G4GDML: Reading definitions..." << G4endl;
470 #endif
471  for (xercesc::DOMNode* iter = defineElement->getFirstChild();
472  iter != 0;iter = iter->getNextSibling())
473  {
474  if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; }
475 
476  const xercesc::DOMElement* const child
477  = dynamic_cast<xercesc::DOMElement*>(iter);
478  if (!child)
479  {
480  G4Exception("G4GDMLRead::DefineRead()", "InvalidRead",
481  FatalException, "No child found!");
482  return;
483  }
484  const G4String tag = Transcode(child->getTagName());
485 
486  if (tag=="constant") { ConstantRead(child); } else
487  if (tag=="matrix") { MatrixRead(child); } else
488  if (tag=="position") { PositionRead(child); } else
489  if (tag=="rotation") { RotationRead(child); } else
490  if (tag=="scale") { ScaleRead(child); } else
491  if (tag=="variable") { VariableRead(child); } else
492  if (tag=="quantity") { QuantityRead(child); } else
493  if (tag=="expression") { ExpressionRead(child); }
494  else
495  {
496  G4String error_msg = "Unknown tag in define: "+tag;
497  G4Exception("G4GDMLReadDefine::defineRead()", "ReadError",
498  FatalException, error_msg);
499  }
500  }
501 }
502 
503 void
504 G4GDMLReadDefine::VectorRead(const xercesc::DOMElement* const vectorElement,
505  G4ThreeVector& vec)
506 {
507  G4double unit = 1.0;
508 
509  const xercesc::DOMNamedNodeMap* const attributes
510  = vectorElement->getAttributes();
511  XMLSize_t attributeCount = attributes->getLength();
512 
513  for (XMLSize_t attribute_index=0;
514  attribute_index<attributeCount; attribute_index++)
515  {
516  xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
517 
518  if (attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
519  { continue; }
520 
521  const xercesc::DOMAttr* const attribute
522  = dynamic_cast<xercesc::DOMAttr*>(attribute_node);
523  if (!attribute)
524  {
525  G4Exception("G4GDMLRead::VectorRead()", "InvalidRead",
526  FatalException, "No attribute found!");
527  return;
528  }
529  const G4String attName = Transcode(attribute->getName());
530  const G4String attValue = Transcode(attribute->getValue());
531 
532  if (attName=="unit") { unit = G4UnitDefinition::GetValueOf(attValue); } else
533  if (attName=="x") { vec.setX(eval.Evaluate(attValue)); } else
534  if (attName=="y") { vec.setY(eval.Evaluate(attValue)); } else
535  if (attName=="z") { vec.setZ(eval.Evaluate(attValue)); }
536  }
537 
538  vec *= unit;
539 }
540 
541 G4String G4GDMLReadDefine::RefRead(const xercesc::DOMElement* const element)
542 {
543  G4String ref;
544 
545  const xercesc::DOMNamedNodeMap* const attributes = element->getAttributes();
546  XMLSize_t attributeCount = attributes->getLength();
547 
548  for (XMLSize_t attribute_index=0;
549  attribute_index<attributeCount; attribute_index++)
550  {
551  xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
552 
553  if (attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
554  { continue; }
555 
556  const xercesc::DOMAttr* const attribute
557  = dynamic_cast<xercesc::DOMAttr*>(attribute_node);
558  if (!attribute)
559  {
560  G4Exception("G4GDMLRead::Read()", "InvalidRead",
561  FatalException, "No attribute found!");
562  return ref;
563  }
564  const G4String attName = Transcode(attribute->getName());
565  const G4String attValue = Transcode(attribute->getValue());
566 
567  if (attName=="ref") { ref = attValue; }
568  }
569 
570  return ref;
571 }
572 
574 {
575  return eval.IsVariable(ref);
576 }
577 
579 {
580  return eval.GetConstant(ref);
581 }
582 
584 {
585  return eval.GetVariable(ref);
586 }
587 
589 {
590  if (quantityMap.find(ref) == quantityMap.end())
591  {
592  G4String error_msg = "Quantity '"+ref+"' was not found!";
593  G4Exception("G4GDMLReadDefine::getQuantity()", "ReadError",
594  FatalException, error_msg);
595  }
596  return quantityMap[ref];
597 }
598 
600 {
601  if (positionMap.find(ref) == positionMap.end())
602  {
603  G4String error_msg = "Position '"+ref+"' was not found!";
604  G4Exception("G4GDMLReadDefine::getPosition()", "ReadError",
605  FatalException, error_msg);
606  }
607  return positionMap[ref];
608 }
609 
611 {
612  if (rotationMap.find(ref) == rotationMap.end())
613  {
614  G4String error_msg = "Rotation '"+ref+"' was not found!";
615  G4Exception("G4GDMLReadDefine::getRotation()", "ReadError",
616  FatalException, error_msg);
617  }
618  return rotationMap[ref];
619 }
620 
622 {
623  if (scaleMap.find(ref) == scaleMap.end())
624  {
625  G4String error_msg = "Scale '"+ref+"' was not found!";
626  G4Exception("G4GDMLReadDefine::getScale()", "ReadError",
627  FatalException, error_msg);
628  }
629  return scaleMap[ref];
630 }
631 
633 {
634  if (matrixMap.find(ref) == matrixMap.end())
635  {
636  G4String error_msg = "Matrix '"+ref+"' was not found!";
637  G4Exception("G4GDMLReadDefine::getMatrix()", "ReadError",
638  FatalException, error_msg);
639  }
640  return matrixMap[ref];
641 }