EDepSimRootGeometryManager.hh
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////
2 // $Id: EDepSim::RootGeometryManager.hh,v 1.13 2011/01/17 02:45:46 mcgrew Exp $
3 //
4 #ifndef EDepSim_RootGeometryManager_hh_Seen
5 #define EDepSim_RootGeometryManager_hh_Seen
6 
7 #include "EDepSimVolumeId.hh"
8 
9 #include <vector>
10 #include <map>
11 #include <set>
12 
13 #include <G4String.hh>
14 #include <G4ThreeVector.hh>
15 #include <G4Color.hh>
16 
17 #include <Rtypes.h>
18 
19 class TGeoManager;
20 class TGeoVolume;
21 class TGeoShape;
22 class TGeoMatrix;
23 class TGeoMedium;
24 class TGeoElement;
25 class TGeoNode;
26 
27 class G4Material;
28 class G4VPhysicalVolume;
29 
30 /// Provide a root output for the geant 4 events.
31 namespace EDepSim {class RootGeometryManager;}
33  typedef struct {
34  int color;
35  int fill;
36  float alpha;
38  typedef std::map<G4String,DrawAttributes> AttributeMap;
39 
40 public:
41  virtual ~RootGeometryManager();
42 
43  /// If a persistency manager has not been created, create one.
44  static EDepSim::RootGeometryManager* Get(void);
45 
46  /// Export the geometry to a file.
47  void Export(const char *file);
48 
49  /// Update the root geometry to match the g4 geometry
50  void Update(const G4VPhysicalVolume* aWorld, bool validate);
51 
52  /// Set the root geometry from a gdml file.
53  void Update(std::string gdmlFile, bool validate);
54 
55  /// Make sure that the current geometry passes a bunch of tests.
56  void Validate();
57 
58  /// Get a volume ID base on the volume position.
59  int GetNodeId(const G4ThreeVector& pos);
60 
61  /// Set material color
62  void SetDrawAtt(G4Material* material, int color, double opacity=1.0);
63 
64  /// Get a color for a material. This can be used to create the
65  /// G4VisAttributes object.
66  G4Color GetG4Color(G4Material* material);
67 
68  /// A method to request that a volume mass should be printed;
70  fPrintMass.push_back(name);
71  }
72 
73 protected:
74  /// use Get() instead
76 
77  /// Find the right color for a physical volume. This won't necessarily be
78  /// the same color as set in G4, but should be a reasonable choice for a
79  /// detector picture. This will return a negative number if the volume
80  /// should be invisible.
81  int GetColor(const G4VPhysicalVolume* vol);
82 
83  /// Get the opacity of the physical volume. This will return an opacity of
84  /// zero (i.e. transparent) if the volume should be invisible.
85  double GetOpacity(const G4VPhysicalVolume* vol);
86 
87 private:
88  /// The pointer to the instantiation of this object.
90 
91  /// A map between G4 material names and Root Material definitions.
92  std::map<G4String,TGeoMedium*> fMedium;
93 
94  /// A map between G4 isotope names and Root Element definitions.
95  std::map<G4String,TGeoElement*> fIsotope;
96 
97  /// A map between G4 volume names and count of volumes with that name.
98  std::map<G4String,int> fNodeCount;
99 
100  /// A map between a material name and a color.
101  AttributeMap fColorMap;
102 
103  /// A vector of volume names to print
104  std::vector<G4String> fPrintMass;
105 
106  /// A map of which masses have been printed.
107  std::set<G4String> fPrintedMass;
108 
109  /// A stack of volume names that have been seen. These are the "short"
110  /// volume names of all of the parents to the current volue.
111  std::vector<G4String> fNameStack;
112 
113  /// A map between the G4 logical volumes and the root volumes. This keeps
114  /// track of which volumes have already been created. It needs to be
115  /// cleared before starting to build a new root geometry.
116  std::map<G4LogicalVolume*,TGeoVolume*> fKnownVolumes;
117 
118  /// A flag whether to brute force the geometry or not. If this is true,
119  /// then a new volume is created for every object in the geometry. This
120  /// means that replicated volumes, and duplicated volume trees occur
121  /// multiple times. This is good for small detectors since it means that
122  /// each volume in the detector will have a unique TGeoNode associated
123  /// with it. It is bad for large detectors since the ROOT geometry will
124  /// become impractially large. The decision is made based on traversing
125  /// the geant geometry and counting the number of unique volumes.
127 
128  /// Create a new ROOT shape object based on the G4 solid. This might be
129  /// called recursively if a G4 boolean solid is found.
130  TGeoShape* CreateShape(const G4VSolid* theSolid, TGeoMatrix **mat = NULL);
131 
132  /// Create a new ROOT volume object.
133  TGeoVolume* CreateVolume(const G4VSolid* theSolid,
134  std::string theName,
135  TGeoMedium* theMedium);
136 
137  /// Save the detector envelope. This is called recursively to fill in the
138  /// entire detector. The G4 physical volume, theVol, is used to create a
139  /// new root TGeoVolume which is added to the existing root TGeoVolume,
140  /// theMother, as a daughter.
141  ///
142  /// RETURN VALUE: This returns true if one of the immediate daughter
143  /// volumes was ignored. This tells the mother volume that it will need
144  /// to adjust it's mass and material to account for the missing detail.
145  bool CreateEnvelope(const G4VPhysicalVolume* theVol,
146  TGeoManager* theEnvelope,
147  TGeoVolume* theMother);
148 
149  // Method counts how many nodes there are in mother volume with the
150  // same name as daughter node.
151  int HowManySimilarNodesInVolume(TGeoVolume* theMother,
152  std::string theName);
153 
154  /// Create the materials needed for the detector. This called recursively
155  /// to find all of the materials in the detector.
156  void CreateMaterials(const G4VPhysicalVolume * theVol);
157 
158  /// A method to prune the geometry that gets exported from G4 into root.
159  /// If this returns true, the volume and all of it's children will not be
160  /// exported to ROOT. This allows the internal structure of low level
161  /// objects (i.e. the internal "structure" of scintillating bars) to be
162  /// hidden.
163  virtual bool IgnoreVolume(const G4VPhysicalVolume* theVol);
164 
165  /// A method to flag that a volume mass should be printed.
166  virtual bool PrintMass(const G4VPhysicalVolume* theVol);
167 
168  /// A method to translate from the local volume material to the right
169  /// material for the root geometry. This is required for volumes where
170  /// the inner structure is not saved, and the material of the outer volume
171  /// does not provide a good base name.
172  virtual std::string MaterialName(const G4VPhysicalVolume *theVol);
173 
174  /// Create a medium that describes the average composition for all the
175  /// materials in the logical volume. This is used when the geometry is
176  /// simplified by not including sub-volume (i.e. when the internal
177  /// structure of the scintillating bars is skipped). The averaged
178  /// material name is based on the name provided in materialBase;
179  virtual TGeoMedium* AverageMaterial(const G4VPhysicalVolume* theVol);
180 };
181 #endif
static QCString name
Definition: declinfo.cpp:673
double GetOpacity(const G4VPhysicalVolume *vol)
virtual void ShouldPrintMass(std::string name)
A method to request that a volume mass should be printed;.
std::map< G4String, int > fNodeCount
A map between G4 volume names and count of volumes with that name.
std::map< G4String, TGeoMedium * > fMedium
A map between G4 material names and Root Material definitions.
G4Color GetG4Color(G4Material *material)
void Update(const G4VPhysicalVolume *aWorld, bool validate)
Update the root geometry to match the g4 geometry.
AttributeMap fColorMap
A map between a material name and a color.
std::string string
Definition: nybbler.cc:12
virtual TGeoMedium * AverageMaterial(const G4VPhysicalVolume *theVol)
virtual bool IgnoreVolume(const G4VPhysicalVolume *theVol)
virtual std::string MaterialName(const G4VPhysicalVolume *theVol)
void Validate()
Make sure that the current geometry passes a bunch of tests.
std::vector< G4String > fPrintMass
A vector of volume names to print.
void Export(const char *file)
Export the geometry to a file.
TGeoVolume * CreateVolume(const G4VSolid *theSolid, std::string theName, TGeoMedium *theMedium)
Create a new ROOT volume object.
std::map< G4String, DrawAttributes > AttributeMap
void CreateMaterials(const G4VPhysicalVolume *theVol)
static EDepSim::RootGeometryManager * fThis
The pointer to the instantiation of this object.
int GetColor(const G4VPhysicalVolume *vol)
Construct a module from components.
Definition: TG4HitSegment.h:10
virtual bool PrintMass(const G4VPhysicalVolume *theVol)
A method to flag that a volume mass should be printed.
def validate(nxgraph, desc)
Definition: graph.py:45
void SetDrawAtt(G4Material *material, int color, double opacity=1.0)
Set material color.
std::size_t color(std::string const &procname)
int HowManySimilarNodesInVolume(TGeoVolume *theMother, std::string theName)
TGeoShape * CreateShape(const G4VSolid *theSolid, TGeoMatrix **mat=NULL)
bool CreateEnvelope(const G4VPhysicalVolume *theVol, TGeoManager *theEnvelope, TGeoVolume *theMother)
std::map< G4LogicalVolume *, TGeoVolume * > fKnownVolumes
std::map< G4String, TGeoElement * > fIsotope
A map between G4 isotope names and Root Element definitions.
int GetNodeId(const G4ThreeVector &pos)
Get a volume ID base on the volume position.
std::set< G4String > fPrintedMass
A map of which masses have been printed.
static EDepSim::RootGeometryManager * Get(void)
If a persistency manager has not been created, create one.