Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TGDMLWrite.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/gdml:$Id$
0002 // Author: Anton Pytel 15/9/2011
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2011, 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_TGDMLWRITE
0013 #define ROOT_TGDMLWRITE
0014 
0015 #include "TGeoMatrix.h"
0016 #include "TGeoElement.h"
0017 #include "TXMLEngine.h"
0018 #include "TGeoVolume.h"
0019 #include "TGeoParaboloid.h"
0020 #include "TGeoSphere.h"
0021 #include "TGeoArb8.h"
0022 #include "TGeoCone.h"
0023 #include "TGeoPara.h"
0024 #include "TGeoTrd1.h"
0025 #include "TGeoTrd2.h"
0026 #include "TGeoTube.h"
0027 #include "TGeoPcon.h"
0028 #include "TGeoTorus.h"
0029 #include "TGeoPgon.h"
0030 #include "TGeoXtru.h"
0031 #include "TGeoEltu.h"
0032 #include "TGeoHype.h"
0033 #include "TGeoBoolNode.h"
0034 #include "TGeoCompositeShape.h"
0035 #include "TGeoScaledShape.h"
0036 #include "TGeoTessellated.h"
0037 #include "TGeoManager.h"
0038 #include "TGDMLMatrix.h"
0039 
0040 #include <map>
0041 #include <set>
0042 #include <iostream>
0043 
0044 ////////////////////////////////////////////////////////////////////////////
0045 //                                                                        //
0046 // TGDMLWrite - Class for exporting geometries From ROOT's gGeoManager    //
0047 //    (instance of TGeoManager class) To GDML file. More about GDML       //
0048 //    see http://gdml.web.cern.ch.                                        //
0049 //                                                                        //
0050 ////////////////////////////////////////////////////////////////////////////
0051 
0052 class TGeoOpticalSurface;
0053 class TGeoSkinSurface;
0054 class TGeoBorderSurface;
0055 
0056 class TGDMLWrite : public TObject {
0057 public:
0058    TGDMLWrite();
0059    ~TGDMLWrite() override;
0060 
0061    static void StartGDMLWriting(TGeoManager *geomanager, const char *filename, TString option)
0062    {
0063       // static function -
0064       // options:
0065       //  g - set by default - geant4 compatibility
0066       //  f,n - if none of this two is set then naming convention is
0067       //        with incremental suffix, if "f" then suffix is pointer
0068       //        if "n" then there is no suffix, but uniqness of names
0069       //        is not secured.
0070       TGDMLWrite *writer = new TGDMLWrite;
0071       writer->WriteGDMLfile(geomanager, filename, option);
0072       delete writer;
0073    }
0074    // wrapper of all main methods for extraction
0075    void WriteGDMLfile(TGeoManager *geomanager, const char *filename = "test.gdml", TString option = "");
0076    // Wrapper to only selectively write one branch of the volume hierarchy to file
0077    void
0078    WriteGDMLfile(TGeoManager *geomanager, TGeoNode *top_node, const char *filename = "test.gdml", TString option = "");
0079 
0080    enum ENamingType { kelegantButSlow = 0, kwithoutSufixNotUniq = 1, kfastButUglySufix = 2 };
0081    void SetNamingSpeed(ENamingType naming);
0082    // Ignore dummy material instance, which causes trouble reading GDML in Geant4
0083    void SetIgnoreDummyMaterial(bool value);
0084    void SetG4Compatibility(Bool_t G4Compatible) { fgG4Compatibility = G4Compatible; };
0085 
0086 private:
0087    struct Xyz {
0088       Double_t x;
0089       Double_t y;
0090       Double_t z;
0091    };
0092 
0093    typedef std::set<const TGeoOpticalSurface *> SurfaceList;
0094    typedef std::set<const TGeoVolume *> VolList;
0095    typedef std::set<const TGeoNode *> NodeList;
0096    typedef std::map<TString, Bool_t> NameList;
0097    typedef std::map<TString, TString> NameListS;
0098    typedef std::map<TString, Int_t> NameListI;
0099    typedef std::map<TString, Float_t> NameListF;
0100    struct StructLst {
0101       NameList fLst;
0102    }; // to store pointers
0103    struct NameLst {
0104       NameListS fLst;     // to map pointers with names
0105       NameListI fLstIter; // to store all the iterators for repeating names
0106    };
0107 
0108    // General lists
0109    StructLst *fIsotopeList;  // list of isotopes
0110    StructLst *fElementList;  // list of elements
0111    StructLst *fAccPatt;      // list of accepted patterns for division
0112    StructLst *fRejShape;     // list of rejected shapes
0113    SurfaceList fSurfaceList; // list of optical surfaces
0114    VolList fVolumeList;      // list of volumes
0115    NodeList fNodeList;       // list of placed volumes
0116 
0117    NameLst *fNameList; // list of names (pointer mapped)
0118 
0119    // Data members
0120    static TGDMLWrite *fgGDMLWrite;   // pointer to gdml writer
0121    Int_t fgNamingSpeed;              // input option for volume and solid naming
0122    Int_t fIgnoreDummyMaterial;       // Flag to ignore TGeo's dummy material
0123    Bool_t fgG4Compatibility;         // input option for Geant4 compatibility
0124    XMLDocPointer_t fGdmlFile;        // pointer storing xml file
0125    TString fDefault_lunit;           // Default unit of length (depends on ROOT unit system)
0126    TString fTopVolumeName;           // name of top volume
0127    TGeoVolume *fTopVolume = nullptr; // top volume of the tree being written
0128    TXMLEngine *fGdmlE;               // xml engine pointer
0129 
0130    XMLNodePointer_t fDefineNode;    // main <define> node...
0131    XMLNodePointer_t fMaterialsNode; // main <materials> node...
0132    XMLNodePointer_t fSolidsNode;    // main <solids> node...
0133    XMLNodePointer_t fStructureNode; // main <structure> node...
0134    Int_t fVolCnt;                   // count of volumes
0135    Int_t fPhysVolCnt;               // count of physical volumes
0136    UInt_t fActNameErr;              // count of name errors
0137    UInt_t fSolCnt;                  // count of name solids
0138    UInt_t fFltPrecision;            //! floating point precision when writing
0139 
0140    static const UInt_t fgkProcBit = BIT(14);    // 14th bit is set when solid is processed
0141    static const UInt_t fgkProcBitVol = BIT(19); // 19th bit is set when volume is processed
0142    static const UInt_t fgkMaxNameErr = 5;       // maximum number of errors for naming
0143 
0144    // I. Methods processing the gGeoManager geometry object structure
0145    // 1. Main methods to extract everything from ROOT gGeoManager
0146    XMLNodePointer_t ExtractMaterials(TList *materialsLst); // result <materials>...
0147    TString ExtractSolid(TGeoShape *volShape);              // adds <shape> to <solids>
0148    void ExtractVolumes(TGeoNode *topNode);                 // result <volume> node...  + corresp. shape
0149    void ExtractMatrices(TObjArray *matrices);              // adds <matrix> to <define>
0150    void ExtractConstants(TGeoManager *geom);               // adds <constant> to <define>
0151    void ExtractOpticalSurfaces(TObjArray *surfaces);       // adds <opticalsurface> to <solids>
0152    void ExtractSkinSurfaces(TObjArray *surfaces);          // adds <skinsurface> to <structure>
0153    void ExtractBorderSurfaces(TObjArray *surfaces);        // adds <bordersurface> to <structure>
0154 
0155    // Combined implementation to extract GDML information from the geometry tree
0156    void WriteGDMLfile(TGeoManager *geomanager, TGeoNode *top_node, TList *materialsLst, const char *filename,
0157                       TString option);
0158 
0159    // 1.1 Materials sub methods - creating Nodes
0160    XMLNodePointer_t CreateAtomN(Double_t atom, const char *unit = "g/mole");
0161    XMLNodePointer_t CreateDN(Double_t density, const char *unit = "g/cm3");
0162    XMLNodePointer_t CreateFractionN(Double_t percentage, const char *refName);
0163    XMLNodePointer_t CreatePropertyN(TNamed const &property);
0164 
0165    XMLNodePointer_t CreateIsotopN(TGeoIsotope *isotope, const char *name);
0166    XMLNodePointer_t CreateElementN(TGeoElement *element, XMLNodePointer_t materials, const char *name);
0167    XMLNodePointer_t CreateMixtureN(TGeoMixture *mixture, XMLNodePointer_t materials, TString mname);
0168    XMLNodePointer_t CreateMaterialN(TGeoMaterial *material, TString mname);
0169 
0170    // 1.2 Solids sub methods
0171    XMLNodePointer_t ChooseObject(TGeoShape *geoShape);
0172    XMLNodePointer_t CreateZplaneN(Double_t z, Double_t rmin, Double_t rmax);
0173 
0174    XMLNodePointer_t CreateBoxN(TGeoBBox *geoShape);
0175    XMLNodePointer_t CreateParaboloidN(TGeoParaboloid *geoShape);
0176    XMLNodePointer_t CreateSphereN(TGeoSphere *geoShape);
0177    XMLNodePointer_t CreateArb8N(TGeoArb8 *geoShape);
0178    XMLNodePointer_t CreateConeN(TGeoConeSeg *geoShape);
0179    XMLNodePointer_t CreateConeN(TGeoCone *geoShape);
0180    XMLNodePointer_t CreateParaN(TGeoPara *geoShape);
0181    XMLNodePointer_t CreateTrapN(TGeoTrap *geoShape);
0182    XMLNodePointer_t CreateTwistedTrapN(TGeoGtra *geoShape);
0183    XMLNodePointer_t CreateTrdN(TGeoTrd1 *geoShape);
0184    XMLNodePointer_t CreateTrdN(TGeoTrd2 *geoShape);
0185    XMLNodePointer_t CreateTubeN(TGeoTubeSeg *geoShape);
0186    XMLNodePointer_t CreateCutTubeN(TGeoCtub *geoShape);
0187    XMLNodePointer_t CreateTubeN(TGeoTube *geoShape);
0188    XMLNodePointer_t CreatePolyconeN(TGeoPcon *geoShape);
0189    XMLNodePointer_t CreateTorusN(TGeoTorus *geoShape);
0190    XMLNodePointer_t CreatePolyhedraN(TGeoPgon *geoShape);
0191    XMLNodePointer_t CreateEltubeN(TGeoEltu *geoShape);
0192    XMLNodePointer_t CreateHypeN(TGeoHype *geoShape);
0193    XMLNodePointer_t CreateXtrusionN(TGeoXtru *geoShape);
0194    XMLNodePointer_t CreateEllipsoidN(TGeoCompositeShape *geoShape, TString elName);
0195    XMLNodePointer_t CreateElConeN(TGeoScaledShape *geoShape);
0196    XMLNodePointer_t CreateScaledN(TGeoScaledShape *geoShape);
0197    XMLNodePointer_t CreateTessellatedN(TGeoTessellated *geoShape);
0198    XMLNodePointer_t CreateOpticalSurfaceN(TGeoOpticalSurface *geoSurf);
0199    XMLNodePointer_t CreateSkinSurfaceN(TGeoSkinSurface *geoSurf);
0200    XMLNodePointer_t CreateBorderSurfaceN(TGeoBorderSurface *geoSurf);
0201 
0202    XMLNodePointer_t CreateCommonBoolN(TGeoCompositeShape *geoShape);
0203 
0204    // 1.3 Volume sub methods
0205    XMLNodePointer_t CreatePhysVolN(const char *name, Int_t copyno, const char *volref, const char *posref,
0206                                    const char *rotref, XMLNodePointer_t scaleN);
0207    XMLNodePointer_t CreateDivisionN(Double_t offset, Double_t width, Int_t number, const char *axis, const char *unit,
0208                                     const char *volref);
0209 
0210    XMLNodePointer_t CreateSetupN(const char *topVolName, const char *name = "default", const char *version = "1.0");
0211    XMLNodePointer_t StartVolumeN(const char *name, const char *solid, const char *material);
0212    XMLNodePointer_t StartAssemblyN(const char *name);
0213 
0214    // II. Utility methods
0215    Xyz GetXYZangles(const Double_t *rotationMatrix);
0216    // nodes to create position, rotation and similar types first-position/rotation...
0217    XMLNodePointer_t CreatePositionN(const char *name, Xyz position, const char *type, const char *unit);
0218    XMLNodePointer_t
0219    CreateRotationN(const char *name, Xyz rotation, const char *type = "rotation", const char *unit = "deg");
0220    XMLNodePointer_t CreateMatrixN(TGDMLMatrix const *matrix);
0221    XMLNodePointer_t CreateConstantN(const char *name, Double_t value);
0222    TGeoCompositeShape *CreateFakeCtub(TGeoCtub *geoShape); // create fake cut tube as intersection
0223 
0224    // check name (2nd parameter) whether it is in the list (1st parameter)
0225    Bool_t IsInList(NameList list, TString name2check);
0226    TString GenName(TString oldname);
0227    TString GenName(TString oldname, TString objPointer);
0228    Bool_t CanProcess(TObject *pointer);
0229    TString GetPattAxis(Int_t divAxis, const char *pattName, TString &unit);
0230    Bool_t IsNullParam(Double_t parValue, TString parName, TString objName);
0231    void UnsetTemporaryBits(TGeoManager *geoMng);
0232    UInt_t GetFltPrecision() const { return fFltPrecision; }
0233    void SetFltPrecision(UInt_t prec) { fFltPrecision = prec; }
0234 
0235    ////////////////////////////////////////////////////////////////////////////////
0236    //
0237    // Backwards compatibility for old DD4hep version (to be removed in the future)
0238    //
0239    ////////////////////////////////////////////////////////////////////////////////
0240 public:
0241    // Backwards compatibility (to be removed in the future): Wrapper to only selectively write one branch
0242    void
0243    WriteGDMLfile(TGeoManager *geomanager, TGeoVolume *top_vol, const char *filename = "test.gdml", TString option = "");
0244 
0245 private:
0246    // Backwards compatibility (to be removed in the future): Combined implementation to extract GDML information from
0247    // the geometry tree
0248    void WriteGDMLfile(TGeoManager *geomanager, TGeoVolume *top_vol, TList *materialsLst, const char *filename,
0249                       TString option);
0250    void ExtractVolumes(TGeoVolume *topVolume); // result <volume> node...  + corresp. shape
0251 
0252    ClassDefOverride(TGDMLWrite, 0) // imports GDML using DOM and binds it to ROOT
0253 };
0254 
0255 #endif /* ROOT_TGDMLWRITE */