TGDMLParse.h

Go to the documentation of this file.
00001 /* @(#)root/gdml:$Id: TGDMLParse.h 36390 2010-10-21 09:35:45Z agheata $ */
00002 // Authors: Ben Lloyd 09/11/06
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TGDMLParse
00013 #define ROOT_TGDMLParse
00014 
00015 #ifndef ROOT_TGeoMatrix
00016 #include "TGeoMatrix.h"
00017 #endif
00018 
00019 #ifndef ROOT_TXMLEngine
00020 #include "TXMLEngine.h"
00021 #endif
00022 
00023 #ifndef ROOT_TGeoVolume
00024 #include "TGeoVolume.h"
00025 #endif
00026 
00027 #include "TFormula.h"
00028 
00029 #include <map>
00030 #include <vector>
00031 #include <iostream>
00032 
00033 /*************************************************************************
00034  * TGDMLRefl - helper class for the import of GDML to ROOT.              * 
00035  *************************************************************************/
00036 
00037 class TGDMLRefl : public TObject {
00038 public:
00039 
00040    TGDMLRefl() {              
00041 
00042       fNameS = ""; 
00043       fSolid = "";
00044       fMatrix = 0;  
00045    }       
00046      
00047    virtual ~TGDMLRefl() {}
00048    
00049    TGDMLRefl(const char* name, const char* solid, TGeoMatrix* matrix);
00050    TGeoMatrix* GetMatrix();  
00051    
00052 private:
00053    
00054    const char*     fNameS;      //!reflected solid name
00055    const char*     fSolid;      //!solid name being reflected
00056    TGeoMatrix     *fMatrix;     //!matrix of reflected solid
00057    
00058    ClassDef(TGDMLRefl, 0)     //helper class used for the storage of reflected solids
00059 };
00060      
00061 /*************************************************************************
00062  * TGDMLParse - base class for the import of GDML to ROOT.               * 
00063  *************************************************************************/
00064 
00065 class TGDMLBaseTGDMMapHelper : public std::map<std::string, const void *>{
00066 };
00067 
00068 //map's [] operator returns reference.
00069 //to avoid ugly UB casts like static_cast<SomeType * &>(voidPtrLValue)
00070 //I have this helper class.
00071 template<typename T>
00072 
00073 class TGDMAssignmentHelper{
00074 private:
00075    TGDMLBaseTGDMMapHelper::iterator fPosInMap;
00076 
00077 public:
00078    TGDMAssignmentHelper(TGDMLBaseTGDMMapHelper &baseMap, const std::string &key) {
00079       baseMap[key];//if we do not have this key-value pair before, insert it now (with zero for pointer).
00080       //find iterator for this key now :)
00081       fPosInMap = baseMap.find(key);
00082    }
00083 
00084    operator T * ()const {
00085       return (T*)fPosInMap->second;//const_cast<T*>(static_cast<const T *>(fPosInMap->second));   
00086    }
00087 
00088    TGDMAssignmentHelper & operator = (const T * ptr) {
00089       fPosInMap->second = ptr;
00090       return *this;
00091    }
00092 };
00093 
00094 template<class T>
00095 class TGDMMapHelper : public TGDMLBaseTGDMMapHelper{
00096 public:
00097    TGDMAssignmentHelper<T> operator [] (const std::string &key)
00098    {
00099       return TGDMAssignmentHelper<T>(*this, key);
00100    }
00101 };
00102 
00103 class TGDMLParse : public TObject {
00104 public:
00105   
00106    const char* fWorldName; //top volume of geometry name
00107    TGeoVolume* fWorld; //top volume of geometry
00108    int fVolID;   //volume ID, incremented as assigned.
00109    int fFILENO; //Holds which level of file the parser is at
00110    TXMLEngine* fFileEngine[20]; //array of dom object pointers
00111    const char* fStartFile; //name of originating file
00112    const char* fCurrentFile; //current file name being parsed
00113 
00114    TGDMLParse() { //constructor
00115 
00116       fVolID = 0;
00117       fFILENO = 0;
00118    }       
00119      
00120    virtual ~TGDMLParse() { //destructor
00121 
00122       for (size_t i=0;i<fformvec.size();i++)
00123          if (fformvec[i] != NULL) delete fformvec[i];
00124    }
00125 
00126    static TGeoVolume* StartGDML(const char* filename){
00127       TGDMLParse* parser = new TGDMLParse;
00128       TGeoVolume* world = parser->GDMLReadFile(filename);
00129       return world;
00130    }
00131    
00132    TGeoVolume*       GDMLReadFile(const char* filename = "test.gdml");
00133     
00134 private:
00135     
00136    const char*       ParseGDML(TXMLEngine* gdml, XMLNodePointer_t node) ;
00137    const char*       GetScale(const char* unit);
00138    double            Evaluate(const char* evalline);
00139    const char*       NameShort(const char* name);
00140     
00141    //'define' section
00142    XMLNodePointer_t  ConProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00143    XMLNodePointer_t  PosProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00144    XMLNodePointer_t  RotProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00145    XMLNodePointer_t  SclProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00146     
00147    //'materials' section
00148    XMLNodePointer_t  IsoProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLNodePointer_t parentn);
00149    XMLNodePointer_t  EleProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLNodePointer_t parentn, Bool_t hasIsotopes);
00150    XMLNodePointer_t  MatProcess(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr, int z);
00151     
00152    //'solids' section
00153    XMLNodePointer_t  BooSolid(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr, int num);
00154    XMLNodePointer_t  Box(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00155    XMLNodePointer_t  Paraboloid(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00156    XMLNodePointer_t  Arb8(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00157    XMLNodePointer_t  Tube(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00158    XMLNodePointer_t  CutTube(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00159    XMLNodePointer_t  Cone(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00160    XMLNodePointer_t  Trap(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00161    XMLNodePointer_t  Trd(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00162    XMLNodePointer_t  Polycone(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00163    XMLNodePointer_t  Polyhedra(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00164    XMLNodePointer_t  Sphere(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00165    XMLNodePointer_t  Torus(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr); 
00166    XMLNodePointer_t  Hype(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr); 
00167    XMLNodePointer_t  Para(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00168    XMLNodePointer_t  TwistTrap(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00169    XMLNodePointer_t  ElTube(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00170    XMLNodePointer_t  Orb(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00171    XMLNodePointer_t  Xtru(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00172    XMLNodePointer_t  Reflection(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
00173    XMLNodePointer_t  Ellipsoid(TXMLEngine* gdml, XMLNodePointer_t node, XMLAttrPointer_t attr); //not really implemented: just approximation to a TGeoBBox
00174  
00175    //'structure' section
00176    XMLNodePointer_t  VolProcess(TXMLEngine* gdml, XMLNodePointer_t node);
00177    XMLNodePointer_t  AssProcess(TXMLEngine* gdml, XMLNodePointer_t node);
00178    Int_t             SetAxis(const char* axisString); //Set Axis for Division
00179 
00180    //'setup' section
00181    XMLNodePointer_t  TopProcess(TXMLEngine* gdml, XMLNodePointer_t node);
00182     
00183    typedef TGDMMapHelper<TGeoTranslation> PosMap;
00184    typedef TGDMMapHelper<TGeoRotation> RotMap;
00185    typedef TGDMMapHelper<TGeoScale> SclMap;
00186    typedef TGDMMapHelper<TGeoElement> EleMap;
00187    typedef TGDMMapHelper<TGeoIsotope> IsoMap;
00188    typedef TGDMMapHelper<TGeoMaterial> MatMap;
00189    typedef TGDMMapHelper<TGeoMedium> MedMap;
00190    typedef TGDMMapHelper<TGeoMixture> MixMap;
00191   
00192    typedef TGDMMapHelper<TGeoShape> SolMap;
00193    typedef TGDMMapHelper<TGeoVolume> VolMap;
00194    typedef TGDMMapHelper<TGDMLRefl> ReflSolidMap;
00195    typedef TGDMMapHelper<const char> FileMap;
00196    typedef std::map<std::string, std::string> ReflectionsMap;
00197    typedef std::map<std::string, std::string> ReflVolMap; 
00198    typedef std::map<std::string, double> FracMap;
00199    typedef std::vector<TFormula*> FormVec;
00200 
00201    PosMap fposmap;                //!Map containing position names and the TGeoTranslation for it
00202    RotMap frotmap;                //!Map containing rotation names and the TGeoRotation for it
00203    SclMap fsclmap;                //!Map containing scale names and the TGeoScale for it
00204    IsoMap fisomap;                //!Map containing isotope names and the TGeoIsotope for it
00205    EleMap felemap;                //!Map containing element names and the TGeoElement for it
00206    MatMap fmatmap;                //!Map containing material names and the TGeoMaterial for it
00207    MedMap fmedmap;                //!Map containing medium names and the TGeoMedium for it
00208    MixMap fmixmap;                //!Map containing mixture names and the TGeoMixture for it
00209    SolMap fsolmap;                //!Map containing solid names and the TGeoShape for it
00210    VolMap fvolmap;                //!Map containing volume names and the TGeoVolume for it
00211    ReflectionsMap freflectmap;    //!Map containing reflection names and the Solid name ir references to
00212    ReflSolidMap freflsolidmap;    //!Map containing reflection names and the TGDMLRefl for it - containing refl matrix
00213    ReflVolMap freflvolmap;        //!Map containing reflected volume names and the solid ref for it
00214    FileMap ffilemap;              //!Map containing files parsed during entire parsing, with their world volume name
00215    FormVec fformvec;              //!Vector containing constant functions for GDML constant definitions
00216 
00217    ClassDef(TGDMLParse, 0)    //imports GDML using DOM and binds it to ROOT
00218 };
00219       
00220 #endif

Generated on Tue Jul 5 14:12:08 2011 for ROOT_528-00b_version by  doxygen 1.5.1