REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestGeant4GeometryInfo.h
1 
2 #ifndef REST_TRESTGEANT4GEOMETRYINFO_H
3 #define REST_TRESTGEANT4GEOMETRYINFO_H
4 
5 #include <TString.h>
6 #include <TVector3.h>
7 
8 #include <map>
9 #include <set>
10 #include <vector>
11 
12 class G4VPhysicalVolume;
13 
15  ClassDef(TRestGeant4GeometryInfo, 3);
16 
17  private:
18  bool fIsAssembly = false;
19 
20  std::map<Int_t, TString> fVolumeNameMap = {};
21  std::map<TString, Int_t> fVolumeNameReverseMap = {};
22 
23  void PopulateFromGeant4World(const G4VPhysicalVolume*);
24 
25  inline void InitializeOnDetectorConstruction(const TString& gdmlFilename,
26  const G4VPhysicalVolume* world) {
27  PopulateFromGdml(gdmlFilename);
28  PopulateFromGeant4World(world);
29  }
30 
31  public:
32  // Insertion order is important for GDML containers. These containers are filled from GDML only not Geant4
33  std::vector<TString> fGdmlNewPhysicalNames;
34  std::vector<TString> fGdmlLogicalNames;
35 
36  std::map<TString, TString>
37  fGeant4PhysicalNameToNewPhysicalNameMap; /*
38  * only makes sense when using assembly
39  */
40 
41  std::map<TString, TString> fPhysicalToLogicalVolumeMap;
42  std::map<TString, std::vector<TString> > fLogicalToPhysicalMap;
43  // many physical volumes can point to one single logical
44  std::map<TString, TString> fLogicalToMaterialMap;
45  std::map<TString, TVector3> fPhysicalToPositionInWorldMap;
46 
47  public:
48  inline TRestGeant4GeometryInfo() = default;
49 
50  void PopulateFromGdml(const TString&);
51 
52  TString GetAlternativeNameFromGeant4PhysicalName(const TString&) const;
53  TString GetGeant4PhysicalNameFromAlternativeName(const TString&) const;
54 
55  std::vector<TString> GetAllLogicalVolumes() const;
56  std::vector<TString> GetAllPhysicalVolumes() const;
57  std::vector<TString> GetAllAlternativePhysicalVolumes() const;
58 
59  std::vector<TString> GetAllLogicalVolumesMatchingExpression(const TString&) const;
60  std::vector<TString> GetAllPhysicalVolumesMatchingExpression(const TString&) const;
61 
62  inline bool IsValidGdmlName(const TString& volume) const {
63  for (const auto& name : fGdmlNewPhysicalNames) {
64  if (name == volume) {
65  return true;
66  }
67  }
68  return false;
69  }
70 
71  inline bool IsValidPhysicalVolume(const TString& volume) const {
72  return fPhysicalToLogicalVolumeMap.count(volume) > 0;
73  }
74  inline bool IsValidLogicalVolume(const TString& volume) const {
75  return fLogicalToPhysicalMap.count(volume) > 0;
76  }
77  inline std::vector<TString> GetAllPhysicalVolumesFromLogical(const TString& logicalVolume) const {
78  if (IsValidLogicalVolume(logicalVolume)) {
79  return fLogicalToPhysicalMap.at(logicalVolume);
80  }
81  return {};
82  }
83 
84  inline TVector3 GetPosition(const TString& volume) const {
85  return fPhysicalToPositionInWorldMap.at(volume);
86  }
87 
88  inline bool IsAssembly() const { return fIsAssembly; }
89 
90  void InsertVolumeName(Int_t id, const TString& volumeName);
91 
92  TString GetVolumeFromID(Int_t id) const;
93  Int_t GetIDFromVolume(const TString& volumeName) const;
94 
95  void Print() const;
96 
97  friend class DetectorConstruction;
98 };
99 
100 #endif // REST_TRESTGEANT4GEOMETRYINFO_H