Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGDMLParse.h
Go to the documentation of this file.
1 /* @(#)root/gdml:$Id$ */
2 // Authors: Ben Lloyd 09/11/06
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TGDMLParse
13 #define ROOT_TGDMLParse
14 
15 #include "TFormula.h"
16 #include "TXMLEngine.h"
17 #include "TGeoMatrix.h"
18 #include "TGeoVolume.h"
19 
20 #include <map>
21 #include <vector>
22 #include <iostream>
23 
24 class TGDMLMatrix;
25 
26 /*************************************************************************
27  * TGDMLRefl - helper class for the import of GDML to ROOT. *
28  *************************************************************************/
29 
30 class TGDMLRefl : public TObject {
31 public:
32 
33  TGDMLRefl() {
34 
35  fNameS = "";
36  fSolid = "";
37  fMatrix = 0;
38  }
39 
40  virtual ~TGDMLRefl() {}
41 
42  TGDMLRefl(const char* name, const char* solid, TGeoMatrix* matrix);
43  TGeoMatrix* GetMatrix();
44 
45 private:
46 
47  const char* fNameS; //!reflected solid name
48  const char* fSolid; //!solid name being reflected
49  TGeoMatrix *fMatrix; //!matrix of reflected solid
50 
51  ClassDef(TGDMLRefl, 0) //helper class used for the storage of reflected solids
52 };
53 
54 /*************************************************************************
55  * TGDMLParse - base class for the import of GDML to ROOT. *
56  *************************************************************************/
57 
58 class TGDMLBaseTGDMMapHelper : public std::map<std::string, const void *> {
59 };
60 
61 //map's [] operator returns reference.
62 //to avoid ugly UB casts like static_cast<SomeType * &>(voidPtrLValue)
63 //I have this helper class.
64 template<typename T>
65 
66 class TGDMAssignmentHelper {
67 private:
68  TGDMLBaseTGDMMapHelper::iterator fPosInMap;
69 
70 public:
71  TGDMAssignmentHelper(TGDMLBaseTGDMMapHelper &baseMap, const std::string &key) {
72  baseMap[key];//if we do not have this key-value pair before, insert it now (with zero for pointer).
73  //find iterator for this key now :)
74  fPosInMap = baseMap.find(key);
75  }
76 
77  operator T * ()const {
78  return (T*)fPosInMap->second;//const_cast<T*>(static_cast<const T *>(fPosInMap->second));
79  }
80 
81  TGDMAssignmentHelper & operator = (const T * ptr) {
82  fPosInMap->second = ptr;
83  return *this;
84  }
85 };
86 
87 template<class T>
88 class TGDMMapHelper : public TGDMLBaseTGDMMapHelper {
89 public:
90  TGDMAssignmentHelper<T> operator [](const std::string &key) {
91  return TGDMAssignmentHelper<T>(*this, key);
92  }
93 };
94 
95 class TGDMLParse : public TObject {
96 public:
97 
98  TString fWorldName;
99  TGeoVolume* fWorld; //top volume of geometry
100  int fVolID; //volume ID, incremented as assigned.
101  int fFILENO; //Holds which level of file the parser is at
102  TXMLEngine* fFileEngine[20]; //array of dom object pointers
103  const char* fStartFile; //name of originating file
104  const char* fCurrentFile; //current file name being parsed
105  std::string fDefault_lunit = "mm";
106  std::string fDefault_aunit = "rad";
107 
108  TGDMLParse();
109  virtual ~TGDMLParse() {}
110 
111  static TGeoVolume* StartGDML(const char* filename) {
112  TGDMLParse* parser = new TGDMLParse;
113  TGeoVolume* world = parser->GDMLReadFile(filename);
114  return world;
115  }
116 
117  TGeoVolume* GDMLReadFile(const char* filename = "test.gdml");
118 
119 private:
120 
121  const char* ParseGDML(TXMLEngine* gdml, XMLNodePointer_t node) ;
122  TString GetScale(const char* unit);
123  double GetScaleVal(const char* unit);
124  double Evaluate(const char* evalline);
125  const char* NameShort(const char* name);
126  double Value(const char *svalue) const;
127  void DefineConstants();
128 
129  //'define' section
130  XMLNodePointer_t ConProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
131  XMLNodePointer_t PosProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
132  XMLNodePointer_t QuantityProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
133  XMLNodePointer_t RotProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
134  XMLNodePointer_t SclProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
135  XMLNodePointer_t MatrixProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
136 
137  //'materials' section
138  XMLNodePointer_t IsoProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLNodePointer_t parentn);
139  XMLNodePointer_t EleProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLNodePointer_t parentn, Bool_t hasIsotopes, Bool_t hasIsotopesExtended);
140  XMLNodePointer_t MatProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr, int z);
141 
142  //'solids' section
143  XMLNodePointer_t OpticalSurfaceProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
144  XMLNodePointer_t BooSolid(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr, int num);
145  XMLNodePointer_t Box(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
146  XMLNodePointer_t Paraboloid(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
147  XMLNodePointer_t Arb8(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
148  XMLNodePointer_t Tube(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
149  XMLNodePointer_t CutTube(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
150  XMLNodePointer_t Cone(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
151  XMLNodePointer_t ElCone(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
152  XMLNodePointer_t Trap(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
153  XMLNodePointer_t Trd(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
154  XMLNodePointer_t Polycone(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
155  XMLNodePointer_t Polyhedra(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
156  XMLNodePointer_t Sphere(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
157  XMLNodePointer_t Torus(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
158  XMLNodePointer_t Hype(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
159  XMLNodePointer_t Para(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
160  XMLNodePointer_t TwistTrap(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
161  XMLNodePointer_t ElTube(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
162  XMLNodePointer_t Orb(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
163  XMLNodePointer_t Xtru(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
164  XMLNodePointer_t Reflection(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
165  XMLNodePointer_t Ellipsoid(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr); //not really implemented: just approximation to a TGeoBBox
166 
167  //'structure' section
168  XMLNodePointer_t VolProcess(TXMLEngine* gdml, XMLNodePointer_t node);
169  XMLNodePointer_t AssProcess(TXMLEngine* gdml, XMLNodePointer_t node);
170  XMLNodePointer_t UsrProcess(TXMLEngine* gdml, XMLNodePointer_t node);
171  XMLNodePointer_t SkinSurfaceProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
172  XMLNodePointer_t BorderSurfaceProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
173  Int_t SetAxis(const char* axisString); //Set Axis for Division
174 
175  //'setup' section
176  XMLNodePointer_t TopProcess(TXMLEngine* gdml, XMLNodePointer_t node);
177 
178  typedef TGDMMapHelper<TGeoTranslation> PosMap;
179  typedef TGDMMapHelper<TGeoRotation> RotMap;
180  typedef TGDMMapHelper<TGeoScale> SclMap;
181  typedef TGDMMapHelper<TGeoElement> EleMap;
182  typedef TGDMMapHelper<TGeoIsotope> IsoMap;
183  typedef TGDMMapHelper<TGeoMaterial> MatMap;
184  typedef TGDMMapHelper<TGeoMedium> MedMap;
185  typedef TGDMMapHelper<TGeoMixture> MixMap;
186 
187  typedef TGDMMapHelper<TGeoShape> SolMap;
188  typedef TGDMMapHelper<TGeoVolume> VolMap;
189  typedef TGDMMapHelper<TGeoNode> PvolMap;
190  typedef TGDMMapHelper<TGDMLMatrix> MatrixMap;
191  typedef TGDMMapHelper<TGDMLRefl> ReflSolidMap;
192  typedef TGDMMapHelper<const char> FileMap;
193  typedef std::map<std::string, std::string> ReflectionsMap;
194  typedef std::map<std::string, std::string> ReflVolMap;
195  typedef std::map<std::string, double> FracMap;
196  typedef std::map<std::string, double> ConstMap;
197 
198  PosMap fposmap; //!Map containing position names and the TGeoTranslation for it
199  RotMap frotmap; //!Map containing rotation names and the TGeoRotation for it
200  SclMap fsclmap; //!Map containing scale names and the TGeoScale for it
201  IsoMap fisomap; //!Map containing isotope names and the TGeoIsotope for it
202  EleMap felemap; //!Map containing element names and the TGeoElement for it
203  MatMap fmatmap; //!Map containing material names and the TGeoMaterial for it
204  MedMap fmedmap; //!Map containing medium names and the TGeoMedium for it
205  MixMap fmixmap; //!Map containing mixture names and the TGeoMixture for it
206  SolMap fsolmap; //!Map containing solid names and the TGeoShape for it
207  VolMap fvolmap; //!Map containing volume names and the TGeoVolume for it
208  PvolMap fpvolmap; //!Map containing placed volume names and the TGeoNode for it
209  ReflectionsMap freflectmap; //!Map containing reflection names and the Solid name ir references to
210  ReflSolidMap freflsolidmap; //!Map containing reflection names and the TGDMLRefl for it - containing refl matrix
211  ReflVolMap freflvolmap; //!Map containing reflected volume names and the solid ref for it
212  FileMap ffilemap; //!Map containing files parsed during entire parsing, with their world volume name
213  ConstMap fconsts; //!Map containing values of constants declared in the file
214  MatrixMap fmatrices; //!Map containing matrices defined in the GDML file
215 
216  ClassDef(TGDMLParse, 0) //imports GDML using DOM and binds it to ROOT
217 };
218 
219 #endif