HighLAND
GeometryManager.hxx
1 #ifndef GeometryManager_hxx_seen
2 #define GeometryManager_hxx_seen
3 
4 #include <vector>
5 #include <map>
6 #include <stdexcept>
7 
8 //------ ROOT includes ---------
9 
10 #include <TGeoVolume.h>
11 #include <TGeoManager.h>
12 #include <TGeoNode.h>
13 #include <TGeoMatrix.h>
14 #include "BasicUtils.hxx"
15 
16 
17 /// This exception is thrown when fail to open a file with geometry
18 class BadRootFileException: public std::runtime_error {
19  public:
21  std::runtime_error("Error reading file with ROOT geometry") {
22  }
23 };
24 
25 /// This exception is thrown when no geometry is found in a file
26 class NoGeometryManagerException: public std::runtime_error {
27  public:
29  std::runtime_error("No ROOT geometry found in file") {
30  }
31 };
32 
33 /// This exception is thrown when the geometry is not a proper one (not with t2k volume)
34 class BadGeometryManagerException: public std::runtime_error {
35  public:
37  std::runtime_error("No t2k volume found in the geometry") {
38  }
39 };
40 
41 
43 public:
44  virtual ~GeometryManager() {}
45 
46  /// Get a pointer to the singleton instance of the geometry information.
47  static GeometryManager& Get();
48 
49  // Returns the ROOT TGeoManager
50  TGeoManager* GeoManager(){
51 
52  TGeoManager* man = NULL;
53 
54  if (_currentGeomID>=0 && _currentGeomID<(Int_t)(_GeoManagers.size()))
55  man = _GeoManagers[_currentGeomID];
56  else{
57  //geometry is not loaded yet, load default one
58  LoadGeometry();
59  if (_currentGeomID>=0 && _currentGeomID<(Int_t)(_GeoManagers.size()))
60  man = _GeoManagers[_currentGeomID];
61  }
62 
63  if (man) return man;
64 
65  std::cout << "ERROR. GeometryManager::GeoManager() failed to retrieve valid manager" << std::endl;
66  exit(1);
67  }
68 
69  /// General function to initialize the geometry
70  /// For the moment used to set appropriate ECal volume definitions depending on Data/MC status
71  void InitializeGeometry(bool IsMC = true) const;
72 
73  /// Overrride the values in DetectorDefinion with the ones extracted from the ROOT geometry
74  void InitializeDetectorDefinitionFromROOTGeometry() const;
75 
76  /// Fill FGD info
77  void FillFGDInfoFromROOTGeometry() const;
78 
79  /// Get the volume position and size from the ROOT geometry
80  void GetVolumeProperties(const std::string& name, Double_t* pos, Double_t* size) const;
81 
82  /// Overrride the values in DetectorDefinion with the ones extracted from the ROOT geometry
83  void SetDetectorBoundaries(const std::string& det_name, Float_t* det_min, Float_t* det_max,
84  const std::string& name1, const std::string& name2="") const;
85 
86 
87  /// Load the TGeoManager from the input root file. Returns true when the new geometry was loaded
88  bool LoadGeometry(const std::string& file="",Int_t geomID=-1, const std::string& geomDir="");
89 
90  /// Set detector boundaries
91  void SetDetectorBoundaries(Float_t* detMin, Float_t* detMax, const Float_t* min, const Float_t* max) const{
92  anaUtils::CopyArray(min, detMin, 3);
93  anaUtils::CopyArray(max, detMax, 3);
94  }
95 
96  /// Retrieve current geometry ID
97  Int_t GetCurrentGeomID() const{
98  return _currentGeomID;
99  }
100 
101 private:
102  GeometryManager();
103  GeometryManager(const GeometryManager& man);
104  GeometryManager& operator=(const GeometryManager& man);
105 
106  /// The static pointer to the singleton instance.
107  static GeometryManager* fGeometryManager;
108 
109  /// The geometry manager that is currently described in this class.
110  mutable std::vector<TGeoManager*> _GeoManagers;
111 
112  /// default geometry file name
113  std::string _defaultFileName;
114 
115  /// Use the default geometry file under psycheUtils/data
116  bool _useDefaultGeometry;
117 
118  /// The current geometry ID
119  Int_t _currentGeomID;
120 
121  /// Loop through the ROOT geometry and retrieve the info for detetor volume envelopes
122  void FillVolumesWithGeometryLoop() const;
123 
124 
125  /// Process GeoNode including it into particular detector volume
126  /// returns boolean which defines whether to go deeper into the ROOT volume hierarchy: saves time
127  bool VisitNode(const std::string& name, const TGeoNode* node) const;
128 
129  /// Update particular volume bound: if the provided low/high values exceed the current limits
130  void UpdateVolumeBound(Float_t* detMin, Float_t* detMax, const Float_t* low, const Float_t* high) const;
131 
132  /// Navigate geometry processing the nodes
133  void RecurseGeometry(std::string name) const;
134 
135  /// Reset volume definitions
136  void ResetVolumeDefinitions() const;
137 
138 };
139 
140 namespace ND{
141  GeometryManager& hgman();
142 }
143 
144 
145 #endif
This exception is thrown when the geometry is not a proper one (not with t2k volume) ...
This exception is thrown when fail to open a file with geometry.
Int_t GetCurrentGeomID() const
Retrieve current geometry ID.
void SetDetectorBoundaries(Float_t *detMin, Float_t *detMax, const Float_t *min, const Float_t *max) const
Set detector boundaries.
This exception is thrown when no geometry is found in a file.