ROOT logo
/* @(#)root/gdml:$Id: TGDMLParse.h 36390 2010-10-21 09:35:45Z agheata $ */
// Authors: Ben Lloyd 09/11/06

/*************************************************************************
 * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TGDMLParse
#define ROOT_TGDMLParse

#ifndef ROOT_TGeoMatrix
#include "TGeoMatrix.h"
#endif

#ifndef ROOT_TXMLEngine
#include "TXMLEngine.h"
#endif

#ifndef ROOT_TGeoVolume
#include "TGeoVolume.h"
#endif

#include "TFormula.h"

#include <map>
#include <vector>
#include <iostream>

/*************************************************************************
 * TGDMLRefl - helper class for the import of GDML to ROOT.              * 
 *************************************************************************/

class TGDMLRefl : public TObject {
public:

   TGDMLRefl() {              

      fNameS = ""; 
      fSolid = "";
      fMatrix = 0;  
   }       
     
   virtual ~TGDMLRefl() {}
   
   TGDMLRefl(const char* name, const char* solid, TGeoMatrix* matrix);
   TGeoMatrix* GetMatrix();  
   
private:
   
   const char*     fNameS;      //!reflected solid name
   const char*     fSolid;      //!solid name being reflected
   TGeoMatrix     *fMatrix;     //!matrix of reflected solid
   
   ClassDef(TGDMLRefl, 0)     //helper class used for the storage of reflected solids
};
     
/*************************************************************************
 * TGDMLParse - base class for the import of GDML to ROOT.               * 
 *************************************************************************/

class TGDMLBaseTGDMMapHelper : public std::map<std::string, const void *>{
};

//map's [] operator returns reference.
//to avoid ugly UB casts like static_cast<SomeType * &>(voidPtrLValue)
//I have this helper class.
template<typename T>

class TGDMAssignmentHelper{
private:
   TGDMLBaseTGDMMapHelper::iterator fPosInMap;

public:
   TGDMAssignmentHelper(TGDMLBaseTGDMMapHelper &baseMap, const std::string &key) {
      baseMap[key];//if we do not have this key-value pair before, insert it now (with zero for pointer).
      //find iterator for this key now :)
      fPosInMap = baseMap.find(key);
   }

   operator T * ()const {
      return (T*)fPosInMap->second;//const_cast<T*>(static_cast<const T *>(fPosInMap->second));   
   }

   TGDMAssignmentHelper & operator = (const T * ptr) {
      fPosInMap->second = ptr;
      return *this;
   }
};

template<class T>
class TGDMMapHelper : public TGDMLBaseTGDMMapHelper{
public:
   TGDMAssignmentHelper<T> operator [] (const std::string &key)
   {
      return TGDMAssignmentHelper<T>(*this, key);
   }
};

class TGDMLParse : public TObject {
public:
  
   const char* fWorldName; //top volume of geometry name
   TGeoVolume* fWorld; //top volume of geometry
   int fVolID;   //volume ID, incremented as assigned.
   int fFILENO; //Holds which level of file the parser is at
   TXMLEngine* fFileEngine[20]; //array of dom object pointers
   const char* fStartFile; //name of originating file
   const char* fCurrentFile; //current file name being parsed

   TGDMLParse() { //constructor

      fVolID = 0;
      fFILENO = 0;
   }       
     
   virtual ~TGDMLParse() { //destructor

      for (size_t i=0;i<fformvec.size();i++)
         if (fformvec[i] != NULL) delete fformvec[i];
   }

   static TGeoVolume* StartGDML(const char* filename){
      TGDMLParse* parser = new TGDMLParse;
      TGeoVolume* world = parser->GDMLReadFile(filename);
      return world;
   }
   
   TGeoVolume*       GDMLReadFile(const char* filename = "test.gdml");
    
private:
    
   const char*       ParseGDML(TXMLEngine* gdml, XMLNodePointer_t node) ;
   const char*       GetScale(const char* unit);
   double            Evaluate(const char* evalline);
   const char*       NameShort(const char* name);
    
   //'define' section
   XMLNodePointer_t  ConProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  PosProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  RotProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  SclProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
    
   //'materials' section
   XMLNodePointer_t  IsoProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLNodePointer_t parentn);
   XMLNodePointer_t  EleProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLNodePointer_t parentn, Bool_t hasIsotopes);
   XMLNodePointer_t  MatProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr, int z);
    
   //'solids' section
   XMLNodePointer_t  BooSolid(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr, int num);
   XMLNodePointer_t  Box(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Paraboloid(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Arb8(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Tube(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  CutTube(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Cone(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Trap(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Trd(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Polycone(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Polyhedra(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Sphere(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Torus(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr); 
   XMLNodePointer_t  Hype(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr); 
   XMLNodePointer_t  Para(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  TwistTrap(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  ElTube(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Orb(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Xtru(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Reflection(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
   XMLNodePointer_t  Ellipsoid(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr); //not really implemented: just approximation to a TGeoBBox
 
   //'structure' section
   XMLNodePointer_t  VolProcess(TXMLEngine* gdml, XMLNodePointer_t node);
   XMLNodePointer_t  AssProcess(TXMLEngine* gdml, XMLNodePointer_t node);
   Int_t             SetAxis(const char* axisString); //Set Axis for Division

   //'setup' section
   XMLNodePointer_t  TopProcess(TXMLEngine* gdml, XMLNodePointer_t node);
    
   typedef TGDMMapHelper<TGeoTranslation> PosMap;
   typedef TGDMMapHelper<TGeoRotation> RotMap;
   typedef TGDMMapHelper<TGeoScale> SclMap;
   typedef TGDMMapHelper<TGeoElement> EleMap;
   typedef TGDMMapHelper<TGeoIsotope> IsoMap;
   typedef TGDMMapHelper<TGeoMaterial> MatMap;
   typedef TGDMMapHelper<TGeoMedium> MedMap;
   typedef TGDMMapHelper<TGeoMixture> MixMap;
  
   typedef TGDMMapHelper<TGeoShape> SolMap;
   typedef TGDMMapHelper<TGeoVolume> VolMap;
   typedef TGDMMapHelper<TGDMLRefl> ReflSolidMap;
   typedef TGDMMapHelper<const char> FileMap;
   typedef std::map<std::string, std::string> ReflectionsMap;
   typedef std::map<std::string, std::string> ReflVolMap; 
   typedef std::map<std::string, double> FracMap;
   typedef std::vector<TFormula*> FormVec;

   PosMap fposmap;                //!Map containing position names and the TGeoTranslation for it
   RotMap frotmap;                //!Map containing rotation names and the TGeoRotation for it
   SclMap fsclmap;                //!Map containing scale names and the TGeoScale for it
   IsoMap fisomap;                //!Map containing isotope names and the TGeoIsotope for it
   EleMap felemap;                //!Map containing element names and the TGeoElement for it
   MatMap fmatmap;                //!Map containing material names and the TGeoMaterial for it
   MedMap fmedmap;                //!Map containing medium names and the TGeoMedium for it
   MixMap fmixmap;                //!Map containing mixture names and the TGeoMixture for it
   SolMap fsolmap;                //!Map containing solid names and the TGeoShape for it
   VolMap fvolmap;                //!Map containing volume names and the TGeoVolume for it
   ReflectionsMap freflectmap;    //!Map containing reflection names and the Solid name ir references to
   ReflSolidMap freflsolidmap;    //!Map containing reflection names and the TGDMLRefl for it - containing refl matrix
   ReflVolMap freflvolmap;        //!Map containing reflected volume names and the solid ref for it
   FileMap ffilemap;              //!Map containing files parsed during entire parsing, with their world volume name
   FormVec fformvec;              //!Vector containing constant functions for GDML constant definitions

   ClassDef(TGDMLParse, 0)    //imports GDML using DOM and binds it to ROOT
};
      
#endif
 TGDMLParse.h:1
 TGDMLParse.h:2
 TGDMLParse.h:3
 TGDMLParse.h:4
 TGDMLParse.h:5
 TGDMLParse.h:6
 TGDMLParse.h:7
 TGDMLParse.h:8
 TGDMLParse.h:9
 TGDMLParse.h:10
 TGDMLParse.h:11
 TGDMLParse.h:12
 TGDMLParse.h:13
 TGDMLParse.h:14
 TGDMLParse.h:15
 TGDMLParse.h:16
 TGDMLParse.h:17
 TGDMLParse.h:18
 TGDMLParse.h:19
 TGDMLParse.h:20
 TGDMLParse.h:21
 TGDMLParse.h:22
 TGDMLParse.h:23
 TGDMLParse.h:24
 TGDMLParse.h:25
 TGDMLParse.h:26
 TGDMLParse.h:27
 TGDMLParse.h:28
 TGDMLParse.h:29
 TGDMLParse.h:30
 TGDMLParse.h:31
 TGDMLParse.h:32
 TGDMLParse.h:33
 TGDMLParse.h:34
 TGDMLParse.h:35
 TGDMLParse.h:36
 TGDMLParse.h:37
 TGDMLParse.h:38
 TGDMLParse.h:39
 TGDMLParse.h:40
 TGDMLParse.h:41
 TGDMLParse.h:42
 TGDMLParse.h:43
 TGDMLParse.h:44
 TGDMLParse.h:45
 TGDMLParse.h:46
 TGDMLParse.h:47
 TGDMLParse.h:48
 TGDMLParse.h:49
 TGDMLParse.h:50
 TGDMLParse.h:51
 TGDMLParse.h:52
 TGDMLParse.h:53
 TGDMLParse.h:54
 TGDMLParse.h:55
 TGDMLParse.h:56
 TGDMLParse.h:57
 TGDMLParse.h:58
 TGDMLParse.h:59
 TGDMLParse.h:60
 TGDMLParse.h:61
 TGDMLParse.h:62
 TGDMLParse.h:63
 TGDMLParse.h:64
 TGDMLParse.h:65
 TGDMLParse.h:66
 TGDMLParse.h:67
 TGDMLParse.h:68
 TGDMLParse.h:69
 TGDMLParse.h:70
 TGDMLParse.h:71
 TGDMLParse.h:72
 TGDMLParse.h:73
 TGDMLParse.h:74
 TGDMLParse.h:75
 TGDMLParse.h:76
 TGDMLParse.h:77
 TGDMLParse.h:78
 TGDMLParse.h:79
 TGDMLParse.h:80
 TGDMLParse.h:81
 TGDMLParse.h:82
 TGDMLParse.h:83
 TGDMLParse.h:84
 TGDMLParse.h:85
 TGDMLParse.h:86
 TGDMLParse.h:87
 TGDMLParse.h:88
 TGDMLParse.h:89
 TGDMLParse.h:90
 TGDMLParse.h:91
 TGDMLParse.h:92
 TGDMLParse.h:93
 TGDMLParse.h:94
 TGDMLParse.h:95
 TGDMLParse.h:96
 TGDMLParse.h:97
 TGDMLParse.h:98
 TGDMLParse.h:99
 TGDMLParse.h:100
 TGDMLParse.h:101
 TGDMLParse.h:102
 TGDMLParse.h:103
 TGDMLParse.h:104
 TGDMLParse.h:105
 TGDMLParse.h:106
 TGDMLParse.h:107
 TGDMLParse.h:108
 TGDMLParse.h:109
 TGDMLParse.h:110
 TGDMLParse.h:111
 TGDMLParse.h:112
 TGDMLParse.h:113
 TGDMLParse.h:114
 TGDMLParse.h:115
 TGDMLParse.h:116
 TGDMLParse.h:117
 TGDMLParse.h:118
 TGDMLParse.h:119
 TGDMLParse.h:120
 TGDMLParse.h:121
 TGDMLParse.h:122
 TGDMLParse.h:123
 TGDMLParse.h:124
 TGDMLParse.h:125
 TGDMLParse.h:126
 TGDMLParse.h:127
 TGDMLParse.h:128
 TGDMLParse.h:129
 TGDMLParse.h:130
 TGDMLParse.h:131
 TGDMLParse.h:132
 TGDMLParse.h:133
 TGDMLParse.h:134
 TGDMLParse.h:135
 TGDMLParse.h:136
 TGDMLParse.h:137
 TGDMLParse.h:138
 TGDMLParse.h:139
 TGDMLParse.h:140
 TGDMLParse.h:141
 TGDMLParse.h:142
 TGDMLParse.h:143
 TGDMLParse.h:144
 TGDMLParse.h:145
 TGDMLParse.h:146
 TGDMLParse.h:147
 TGDMLParse.h:148
 TGDMLParse.h:149
 TGDMLParse.h:150
 TGDMLParse.h:151
 TGDMLParse.h:152
 TGDMLParse.h:153
 TGDMLParse.h:154
 TGDMLParse.h:155
 TGDMLParse.h:156
 TGDMLParse.h:157
 TGDMLParse.h:158
 TGDMLParse.h:159
 TGDMLParse.h:160
 TGDMLParse.h:161
 TGDMLParse.h:162
 TGDMLParse.h:163
 TGDMLParse.h:164
 TGDMLParse.h:165
 TGDMLParse.h:166
 TGDMLParse.h:167
 TGDMLParse.h:168
 TGDMLParse.h:169
 TGDMLParse.h:170
 TGDMLParse.h:171
 TGDMLParse.h:172
 TGDMLParse.h:173
 TGDMLParse.h:174
 TGDMLParse.h:175
 TGDMLParse.h:176
 TGDMLParse.h:177
 TGDMLParse.h:178
 TGDMLParse.h:179
 TGDMLParse.h:180
 TGDMLParse.h:181
 TGDMLParse.h:182
 TGDMLParse.h:183
 TGDMLParse.h:184
 TGDMLParse.h:185
 TGDMLParse.h:186
 TGDMLParse.h:187
 TGDMLParse.h:188
 TGDMLParse.h:189
 TGDMLParse.h:190
 TGDMLParse.h:191
 TGDMLParse.h:192
 TGDMLParse.h:193
 TGDMLParse.h:194
 TGDMLParse.h:195
 TGDMLParse.h:196
 TGDMLParse.h:197
 TGDMLParse.h:198
 TGDMLParse.h:199
 TGDMLParse.h:200
 TGDMLParse.h:201
 TGDMLParse.h:202
 TGDMLParse.h:203
 TGDMLParse.h:204
 TGDMLParse.h:205
 TGDMLParse.h:206
 TGDMLParse.h:207
 TGDMLParse.h:208
 TGDMLParse.h:209
 TGDMLParse.h:210
 TGDMLParse.h:211
 TGDMLParse.h:212
 TGDMLParse.h:213
 TGDMLParse.h:214
 TGDMLParse.h:215
 TGDMLParse.h:216
 TGDMLParse.h:217
 TGDMLParse.h:218
 TGDMLParse.h:219
 TGDMLParse.h:220