Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGDMLWrite.h
Go to the documentation of this file.
1 // @(#)root/gdml:$Id$
2 // Author: Anton Pytel 15/9/2011
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2011, 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_TGDMLWRITE
13 #define ROOT_TGDMLWRITE
14 
15 #include "TGeoMatrix.h"
16 #include "TXMLEngine.h"
17 #include "TGeoVolume.h"
18 #include "TGeoParaboloid.h"
19 #include "TGeoSphere.h"
20 #include "TGeoArb8.h"
21 #include "TGeoCone.h"
22 #include "TGeoPara.h"
23 #include "TGeoTrd1.h"
24 #include "TGeoTrd2.h"
25 #include "TGeoTube.h"
26 #include "TGeoPcon.h"
27 #include "TGeoTorus.h"
28 #include "TGeoPgon.h"
29 #include "TGeoXtru.h"
30 #include "TGeoPgon.h"
31 #include "TGeoEltu.h"
32 #include "TGeoHype.h"
33 #include "TGeoBoolNode.h"
34 #include "TGeoCompositeShape.h"
35 #include "TGeoScaledShape.h"
36 #include "TGeoManager.h"
37 #include "TGDMLMatrix.h"
38 
39 #include <map>
40 #include <set>
41 #include <vector>
42 #include <iostream>
43 
44 ////////////////////////////////////////////////////////////////////////////
45 // //
46 // TGDMLWrite - Class for exporting geometries From ROOT's gGeoManager //
47 // (instance of TGeoManager class) To GDML file. More about GDML //
48 // see http://gdml.web.cern.ch. //
49 // //
50 ////////////////////////////////////////////////////////////////////////////
51 
52 class TGeoOpticalSurface;
53 class TGeoSkinSurface;
54 class TGeoBorderSurface;
55 
56 class TGDMLWrite : public TObject {
57 public:
58  TGDMLWrite();
59  virtual ~TGDMLWrite();
60 
61  static void StartGDMLWriting(TGeoManager * geomanager, const char* filename, TString option) {
62  //static function -
63  //options:
64  // g - set by default - geant4 compatibility
65  // f,n - if none of this two is set then naming convention is
66  // with incremental suffix, if "f" then suffix is pointer
67  // if "n" then there is no suffix, but uniqness of names
68  // is not secured.
69  TGDMLWrite *writer = new TGDMLWrite;
70  writer->SetFltPrecision(TGeoManager::GetExportPrecision());
71  writer->WriteGDMLfile(geomanager, filename, option);
72  delete writer;
73  }
74  //wrapper of all main methods for extraction
75  void WriteGDMLfile(TGeoManager * geomanager, const char* filename = "test.gdml", TString option = "");
76  // Wrapper to only selectively write one branch of the volume hierarchy to file
77  void WriteGDMLfile(TGeoManager * geomanager, TGeoNode* top_node, const char* filename = "test.gdml", TString option = "");
78 
79  enum ENamingType {
80  kelegantButSlow = 0,
81  kwithoutSufixNotUniq = 1,
82  kfastButUglySufix = 2
83  };
84  void SetNamingSpeed(ENamingType naming);
85  void SetG4Compatibility(Bool_t G4Compatible) {
86  fgG4Compatibility = G4Compatible;
87  };
88 
89 private:
90  struct Xyz {
91  Double_t x;
92  Double_t y;
93  Double_t z;
94  };
95 
96  typedef std::set<const TGeoOpticalSurface*> SurfaceList;
97  typedef std::set<const TGeoVolume*> VolList;
98  typedef std::set<const TGeoNode*> NodeList;
99  typedef std::map<TString, Bool_t> NameList;
100  typedef std::map<TString, TString> NameListS;
101  typedef std::map<TString, Int_t> NameListI;
102  typedef std::map<TString, Float_t> NameListF;
103  struct StructLst {
104  NameList fLst;
105  }; //to store pointers
106  struct NameLst {
107  NameListS fLst; //to map pointers with names
108  NameListI fLstIter; //to store all the iterators for repeating names
109  };
110 
111  //General lists
112  StructLst *fIsotopeList; //list of isotopes
113  StructLst *fElementList; //list of elements
114  StructLst *fAccPatt; //list of accepted patterns for division
115  StructLst *fRejShape; //list of rejected shapes
116  SurfaceList fSurfaceList; //list of optical surfaces
117  VolList fVolumeList; //list of volumes
118  NodeList fNodeList; //list of placed volumes
119 
120  NameLst *fNameList; //list of names (pointer mapped)
121 
122  //Data members
123  static TGDMLWrite *fgGDMLWrite; //pointer to gdml writer
124  Int_t fgNamingSpeed; //input option for volume and solid naming
125  Bool_t fgG4Compatibility; //input option for Geant4 compatibility
126  XMLDocPointer_t fGdmlFile; //pointer storing xml file
127  TString fTopVolumeName; //name of top volume
128  TXMLEngine *fGdmlE; //xml engine pointer
129 
130  XMLNodePointer_t fDefineNode; //main <define> node...
131  XMLNodePointer_t fMaterialsNode; //main <materials> node...
132  XMLNodePointer_t fSolidsNode; //main <solids> node...
133  XMLNodePointer_t fStructureNode; //main <structure> node...
134  Int_t fVolCnt; //count of volumes
135  Int_t fPhysVolCnt; //count of physical volumes
136  UInt_t fActNameErr; //count of name errors
137  UInt_t fSolCnt; //count of name solids
138  UInt_t fFltPrecision; //! floating point precision when writing
139 
140  static const UInt_t fgkProcBit = BIT(14); //14th bit is set when solid is processed
141  static const UInt_t fgkProcBitVol = BIT(19); //19th bit is set when volume is processed
142  static const UInt_t fgkMaxNameErr = 5; //maximum number of errors for naming
143 
144  //I. Methods processing the gGeoManager geometry object structure
145  //1. Main methods to extract everything from ROOT gGeoManager
146  XMLNodePointer_t ExtractMaterials(TList* materialsLst); //result <materials>...
147  TString ExtractSolid(TGeoShape* volShape); //adds <shape> to <solids>
148  void ExtractVolumes(TGeoNode* topNode); //result <volume> node... + corresp. shape
149  void ExtractMatrices(TObjArray *matrices); //adds <matrix> to <define>
150  void ExtractConstants(TGeoManager *geom); //adds <constant> to <define>
151  void ExtractOpticalSurfaces(TObjArray *surfaces); //adds <opticalsurface> to <solids>
152  void ExtractSkinSurfaces(TObjArray *surfaces); //adds <skinsurface> to <structure>
153  void ExtractBorderSurfaces(TObjArray *surfaces); //adds <bordersurface> to <structure>
154 
155  // Combined implementation to extract GDML information from the geometry tree
156  void WriteGDMLfile(TGeoManager * geomanager, TGeoNode* top_node, TList* materialsLst, const char* filename, TString option);
157 
158  //1.1 Materials sub methods - creating Nodes
159  XMLNodePointer_t CreateAtomN(Double_t atom, const char * unit = "g/mole");
160  XMLNodePointer_t CreateDN(Double_t density, const char * unit = "g/cm3");
161  XMLNodePointer_t CreateFractionN(Double_t percentage, const char * refName);
162  XMLNodePointer_t CreatePropertyN(TNamed const &property);
163 
164  XMLNodePointer_t CreateIsotopN(TGeoIsotope * isotope, const char * name);
165  XMLNodePointer_t CreateElementN(TGeoElement * element, XMLNodePointer_t materials, const char * name);
166  XMLNodePointer_t CreateMixtureN(TGeoMixture * mixture, XMLNodePointer_t materials, TString mname);
167  XMLNodePointer_t CreateMaterialN(TGeoMaterial * material, TString mname);
168 
169 
170  //1.2 Solids sub methods
171  XMLNodePointer_t ChooseObject(TGeoShape *geoShape);
172  XMLNodePointer_t CreateZplaneN(Double_t z, Double_t rmin, Double_t rmax);
173 
174  XMLNodePointer_t CreateBoxN(TGeoBBox * geoShape);
175  XMLNodePointer_t CreateParaboloidN(TGeoParaboloid * geoShape);
176  XMLNodePointer_t CreateSphereN(TGeoSphere * geoShape);
177  XMLNodePointer_t CreateArb8N(TGeoArb8 * geoShape);
178  XMLNodePointer_t CreateConeN(TGeoConeSeg * geoShape);
179  XMLNodePointer_t CreateConeN(TGeoCone * geoShape);
180  XMLNodePointer_t CreateParaN(TGeoPara * geoShape);
181  XMLNodePointer_t CreateTrapN(TGeoTrap * geoShape);
182  XMLNodePointer_t CreateTwistedTrapN(TGeoGtra * geoShape);
183  XMLNodePointer_t CreateTrdN(TGeoTrd1 * geoShape);
184  XMLNodePointer_t CreateTrdN(TGeoTrd2 * geoShape);
185  XMLNodePointer_t CreateTubeN(TGeoTubeSeg * geoShape);
186  XMLNodePointer_t CreateCutTubeN(TGeoCtub * geoShape);
187  XMLNodePointer_t CreateTubeN(TGeoTube * geoShape);
188  XMLNodePointer_t CreatePolyconeN(TGeoPcon * geoShape);
189  XMLNodePointer_t CreateTorusN(TGeoTorus * geoShape);
190  XMLNodePointer_t CreatePolyhedraN(TGeoPgon * geoShape);
191  XMLNodePointer_t CreateEltubeN(TGeoEltu * geoShape);
192  XMLNodePointer_t CreateHypeN(TGeoHype * geoShape);
193  XMLNodePointer_t CreateXtrusionN(TGeoXtru * geoShape);
194  XMLNodePointer_t CreateEllipsoidN(TGeoCompositeShape * geoShape, TString elName);
195  XMLNodePointer_t CreateElConeN(TGeoScaledShape * geoShape);
196  XMLNodePointer_t CreateOpticalSurfaceN(TGeoOpticalSurface * geoSurf);
197  XMLNodePointer_t CreateSkinSurfaceN(TGeoSkinSurface * geoSurf);
198  XMLNodePointer_t CreateBorderSurfaceN(TGeoBorderSurface * geoSurf);
199 
200  XMLNodePointer_t CreateCommonBoolN(TGeoCompositeShape *geoShape);
201 
202  //1.3 Volume sub methods
203  XMLNodePointer_t CreatePhysVolN(const char * name, Int_t copyno, const char * volref, const char * posref, const char * rotref, XMLNodePointer_t scaleN);
204  XMLNodePointer_t CreateDivisionN(Double_t offset, Double_t width, Int_t number, const char * axis, const char * unit, const char * volref);
205 
206  XMLNodePointer_t CreateSetupN(const char * topVolName , const char * name = "default", const char * version = "1.0");
207  XMLNodePointer_t StartVolumeN(const char * name, const char * solid, const char * material);
208  XMLNodePointer_t StartAssemblyN(const char * name);
209 
210 
211  //II. Utility methods
212  Xyz GetXYZangles(const Double_t * rotationMatrix);
213  //nodes to create position, rotation and similar types first-position/rotation...
214  XMLNodePointer_t CreatePositionN(const char * name, Xyz position, const char * type = "position", const char * unit = "cm");
215  XMLNodePointer_t CreateRotationN(const char * name, Xyz rotation, const char * type = "rotation", const char * unit = "deg");
216  XMLNodePointer_t CreateMatrixN(TGDMLMatrix const *matrix);
217  XMLNodePointer_t CreateConstantN(const char *name, Double_t value);
218  TGeoCompositeShape* CreateFakeCtub(TGeoCtub * geoShape); //create fake cut tube as intersection
219 
220  //check name (2nd parameter) whether it is in the list (1st parameter)
221  Bool_t IsInList(NameList list, TString name2check);
222  TString GenName(TString oldname);
223  TString GenName(TString oldname, TString objPointer);
224  Bool_t CanProcess(TObject *pointer);
225  TString GetPattAxis(Int_t divAxis, const char * pattName, TString& unit);
226  Bool_t IsNullParam(Double_t parValue, TString parName, TString objName);
227  void UnsetTemporaryBits(TGeoManager * geoMng);
228  UInt_t GetFltPrecision() const { return fFltPrecision; }
229  void SetFltPrecision(UInt_t prec) { fFltPrecision = prec; }
230 
231  ////////////////////////////////////////////////////////////////////////////////
232  //
233  // Backwards compatibility for old DD4hep version (to be removed in the future)
234  //
235  ////////////////////////////////////////////////////////////////////////////////
236 public:
237  // Backwards compatibility (to be removed in the future): Wrapper to only selectively write one branch
238  void WriteGDMLfile(TGeoManager * geomanager, TGeoVolume* top_vol, const char* filename = "test.gdml", TString option = "");
239 private:
240  // Backwards compatibility (to be removed in the future): Combined implementation to extract GDML information from the geometry tree
241  void WriteGDMLfile(TGeoManager * geomanager, TGeoVolume* top_vol, TList* materialsLst, const char* filename, TString option);
242  void ExtractVolumes(TGeoVolume* topVolume); //result <volume> node... + corresp. shape
243 
244  ClassDef(TGDMLWrite, 0) //imports GDML using DOM and binds it to ROOT
245 };
246 
247 #endif /* ROOT_TGDMLWRITE */