Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:11:48

0001 /* @(#)root/gdml:$Id$ */
0002 // Authors: Ben Lloyd 09/11/06
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TGDMLParse
0013 #define ROOT_TGDMLParse
0014 
0015 #include "TFormula.h"
0016 #include "TXMLEngine.h"
0017 #include "TGeoMatrix.h"
0018 #include "TGeoVolume.h"
0019 #include "TGeoElement.h"
0020 
0021 #include <map>
0022 #include <iostream>
0023 
0024 class TGDMLMatrix;
0025 
0026 /*************************************************************************
0027  * TGDMLRefl - helper class for the import of GDML to ROOT.              *
0028  *************************************************************************/
0029 
0030 class TGDMLRefl : public TObject {
0031 public:
0032    TGDMLRefl()
0033    {
0034 
0035       fNameS = "";
0036       fSolid = "";
0037       fMatrix = nullptr;
0038    }
0039 
0040    ~TGDMLRefl() override {}
0041 
0042    TGDMLRefl(const char *name, const char *solid, TGeoMatrix *matrix);
0043    TGeoMatrix *GetMatrix();
0044 
0045 private:
0046    const char *fNameS;  //! reflected solid name
0047    const char *fSolid;  //! solid name being reflected
0048    TGeoMatrix *fMatrix; //! matrix of reflected solid
0049 
0050    ClassDefOverride(TGDMLRefl, 0) // helper class used for the storage of reflected solids
0051 };
0052 
0053 /*************************************************************************
0054  * TGDMLParse - base class for the import of GDML to ROOT.               *
0055  *************************************************************************/
0056 
0057 class TGDMLBaseTGDMMapHelper : public std::map<std::string, const void *> {
0058 };
0059 
0060 // map's [] operator returns reference.
0061 // to avoid ugly UB casts like static_cast<SomeType * &>(voidPtrLValue)
0062 // I have this helper class.
0063 template <typename T>
0064 
0065 class TGDMAssignmentHelper {
0066 private:
0067    TGDMLBaseTGDMMapHelper::iterator fPosInMap;
0068 
0069 public:
0070    TGDMAssignmentHelper(TGDMLBaseTGDMMapHelper &baseMap, const std::string &key)
0071    {
0072       baseMap[key]; // if we do not have this key-value pair before, insert it now (with zero for pointer).
0073       // find iterator for this key now :)
0074       fPosInMap = baseMap.find(key);
0075    }
0076 
0077    operator T *() const
0078    {
0079       return (T *)fPosInMap->second; // const_cast<T*>(static_cast<const T *>(fPosInMap->second));
0080    }
0081 
0082    TGDMAssignmentHelper &operator=(const T *ptr)
0083    {
0084       fPosInMap->second = ptr;
0085       return *this;
0086    }
0087 };
0088 
0089 template <class T>
0090 class TGDMMapHelper : public TGDMLBaseTGDMMapHelper {
0091 public:
0092    TGDMAssignmentHelper<T> operator[](const std::string &key) { return TGDMAssignmentHelper<T>(*this, key); }
0093 };
0094 
0095 class TGDMLParse : public TObject {
0096 public:
0097    TString fWorldName;
0098    TGeoVolume *fWorld;          // top volume of geometry
0099    int fVolID;                  // volume ID, incremented as assigned.
0100    int fFILENO;                 // Holds which level of file the parser is at
0101    int fNunitless{0};           // Number of entities defined unitless
0102    TXMLEngine *fFileEngine[20]; // array of dom object pointers
0103    const char *fStartFile;      // name of originating file
0104    const char *fCurrentFile;    // current file name being parsed
0105    std::string fDefault_lunit = "mm";
0106    std::string fDefault_aunit = "rad";
0107 
0108    TGDMLParse();
0109    ~TGDMLParse() override {}
0110 
0111    static TGeoVolume *StartGDML(const char *filename)
0112    {
0113       TGDMLParse *parser = new TGDMLParse;
0114       TGeoVolume *world = parser->GDMLReadFile(filename);
0115       return world;
0116    }
0117 
0118    TGeoVolume *GDMLReadFile(const char *filename = "test.gdml");
0119 
0120 private:
0121    const char *ParseGDML(TXMLEngine *gdml, XMLNodePointer_t node);
0122    TString GetScale(const char *unit);
0123    double GetScaleVal(const char *unit);
0124    double Evaluate(const char *evalline);
0125    const char *NameShort(const char *name);
0126    double Value(const char *svalue) const;
0127    void DefineConstants();
0128 
0129    //'define' section
0130    XMLNodePointer_t ConProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0131    XMLNodePointer_t PosProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0132    XMLNodePointer_t QuantityProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0133    XMLNodePointer_t RotProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0134    XMLNodePointer_t SclProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0135    XMLNodePointer_t MatrixProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0136 
0137    //'materials' section
0138    XMLNodePointer_t IsoProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLNodePointer_t parentn);
0139    XMLNodePointer_t EleProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLNodePointer_t parentn, Bool_t hasIsotopes,
0140                                Bool_t hasIsotopesExtended);
0141    XMLNodePointer_t MatProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr, int z);
0142 
0143    //'solids' section
0144    XMLNodePointer_t OpticalSurfaceProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0145    XMLNodePointer_t BooSolid(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr, int num);
0146    XMLNodePointer_t Box(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0147    XMLNodePointer_t Paraboloid(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0148    XMLNodePointer_t Arb8(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0149    XMLNodePointer_t Tube(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0150    XMLNodePointer_t CutTube(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0151    XMLNodePointer_t Cone(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0152    XMLNodePointer_t ElCone(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0153    XMLNodePointer_t Trap(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0154    XMLNodePointer_t Trd(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0155    XMLNodePointer_t Polycone(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0156    XMLNodePointer_t Polyhedra(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0157    XMLNodePointer_t Sphere(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0158    XMLNodePointer_t Torus(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0159    XMLNodePointer_t Hype(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0160    XMLNodePointer_t Para(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0161    XMLNodePointer_t TwistTrap(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0162    XMLNodePointer_t ElTube(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0163    XMLNodePointer_t Orb(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0164    XMLNodePointer_t Xtru(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0165    XMLNodePointer_t Reflection(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0166    XMLNodePointer_t Ellipsoid(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0167    XMLNodePointer_t Tessellated(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0168    XMLNodePointer_t ScaledSolid(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0169 
0170    //'structure' section
0171    XMLNodePointer_t VolProcess(TXMLEngine *gdml, XMLNodePointer_t node);
0172    XMLNodePointer_t AssProcess(TXMLEngine *gdml, XMLNodePointer_t node);
0173    XMLNodePointer_t UsrProcess(TXMLEngine *gdml, XMLNodePointer_t node);
0174    XMLNodePointer_t SkinSurfaceProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0175    XMLNodePointer_t BorderSurfaceProcess(TXMLEngine *gdml, XMLNodePointer_t node, XMLAttrPointer_t attr);
0176    Int_t SetAxis(const char *axisString); // Set Axis for Division
0177 
0178    //'setup' section
0179    XMLNodePointer_t TopProcess(TXMLEngine *gdml, XMLNodePointer_t node);
0180 
0181    // Find defined objects by name
0182    TGeoTranslation *GetPosition(const char *name);
0183    TGeoRotation *GetRotation(const char *name);
0184    TGeoScale *GetScaleObj(const char *name);
0185    TGeoShape *GetSolid(const char *name);
0186    TGeoVolume *GetVolume(const char *name);
0187 
0188    typedef TGDMMapHelper<TGeoTranslation> PosMap;
0189    typedef TGDMMapHelper<TGeoRotation> RotMap;
0190    typedef TGDMMapHelper<TGeoScale> SclMap;
0191    typedef TGDMMapHelper<TGeoElement> EleMap;
0192    typedef TGDMMapHelper<TGeoIsotope> IsoMap;
0193    typedef TGDMMapHelper<TGeoMaterial> MatMap;
0194    typedef TGDMMapHelper<TGeoMedium> MedMap;
0195    typedef TGDMMapHelper<TGeoMixture> MixMap;
0196 
0197    typedef TGDMMapHelper<TGeoShape> SolMap;
0198    typedef TGDMMapHelper<TGeoVolume> VolMap;
0199    typedef TGDMMapHelper<TGeoNode> PvolMap;
0200    typedef TGDMMapHelper<TGDMLMatrix> MatrixMap;
0201    typedef TGDMMapHelper<TGDMLRefl> ReflSolidMap;
0202    typedef TGDMMapHelper<const char> FileMap;
0203    typedef std::map<std::string, std::string> ReflectionsMap;
0204    typedef std::map<std::string, std::string> ReflVolMap;
0205    typedef std::map<std::string, double> FracMap;
0206    typedef std::map<std::string, double> ConstMap;
0207 
0208    PosMap fposmap;             //! Map containing position names and the TGeoTranslation for it
0209    RotMap frotmap;             //! Map containing rotation names and the TGeoRotation for it
0210    SclMap fsclmap;             //! Map containing scale names and the TGeoScale for it
0211    IsoMap fisomap;             //! Map containing isotope names and the TGeoIsotope for it
0212    EleMap felemap;             //! Map containing element names and the TGeoElement for it
0213    MatMap fmatmap;             //! Map containing material names and the TGeoMaterial for it
0214    MedMap fmedmap;             //! Map containing medium names and the TGeoMedium for it
0215    MixMap fmixmap;             //! Map containing mixture names and the TGeoMixture for it
0216    SolMap fsolmap;             //! Map containing solid names and the TGeoShape for it
0217    VolMap fvolmap;             //! Map containing volume names and the TGeoVolume for it
0218    PvolMap fpvolmap;           //! Map containing placed volume names and the TGeoNode for it
0219    ReflectionsMap freflectmap; //! Map containing reflection names and the Solid name ir references to
0220    ReflSolidMap freflsolidmap; //! Map containing reflection names and the TGDMLRefl for it - containing refl matrix
0221    ReflVolMap freflvolmap;     //! Map containing reflected volume names and the solid ref for it
0222    FileMap ffilemap;           //! Map containing files parsed during entire parsing, with their world volume name
0223    ConstMap fconsts;           //! Map containing values of constants declared in the file
0224    MatrixMap fmatrices;        //! Map containing matrices defined in the GDML file
0225 
0226    ClassDefOverride(TGDMLParse, 0) // imports GDML using DOM and binds it to ROOT
0227 };
0228 
0229 #endif