LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
ROOTGeometryNavigator.h
Go to the documentation of this file.
1 
8 #ifndef LARCOREALG_GEOMETRY_ROOTGEOMETRYNAVIGATOR_H
9 #define LARCOREALG_GEOMETRY_ROOTGEOMETRYNAVIGATOR_H
10 
11 // LArSoft libraries
12 #include "larcorealg/CoreUtils/counter.h" // geo::...::makeFromCoords()
14 
15 // ROOT libraries
16 #include "TGeoManager.h"
17 #include "TGeoNode.h"
18 #include "TGeoVolume.h"
19 
20 // C++ standard library
21 #include <cassert>
22 #include <utility> // std::forward()
23 
24 namespace geo {
25 
26  class ROOTGeometryNavigator;
27 
28 } // namespace geo
29 
30 //------------------------------------------------------------------------------
60 
61  TGeoNode const* fTopNode = nullptr;
62 
63 public:
65  ROOTGeometryNavigator(TGeoManager const& manager) : fTopNode(manager.GetTopNode()) {}
66 
82  template <typename Op>
83  bool apply(GeoNodePath& path, Op&& op) const;
84 
96  template <typename Op>
97  bool apply(TGeoNode const* node, Op&& op) const;
98 
108  template <typename Op>
109  bool apply(Op&& op) const;
110 
111 }; // geo::ROOTGeometryNavigator
112 
113 //------------------------------------------------------------------------------
114 //--- template implementation
115 //------------------------------------------------------------------------------
116 template <typename Op>
118 {
119  if (!op(path)) return false;
120 
121  TGeoNode const* node = path.current();
122  TGeoVolume const* pVolume = node->GetVolume();
123  if (pVolume) { // is it even possible not to?
124  int const nDaughters = pVolume->GetNdaughters();
125  for (int iDaughter : util::counter<int>(nDaughters)) {
126  TGeoNode const* pDaughter = pVolume->GetNode(iDaughter);
127  if (!pDaughter) continue; // fishy...
128 
129  path.append(pDaughter);
130  if (!apply(path, std::forward<Op>(op))) return false;
131  path.pop();
132  } // for
133  } // if we have a volume
134 
135  return true;
136 }
137 
138 //------------------------------------------------------------------------------
139 template <typename Op>
140 bool geo::ROOTGeometryNavigator::apply(TGeoNode const* node, Op&& op) const
141 {
142  GeoNodePath path{node};
143  return apply(path, std::forward<Op>(op));
144 }
145 
146 //------------------------------------------------------------------------------
147 template <typename Op>
149 {
150  assert(fTopNode);
151  return apply(fTopNode, std::forward<Op>(op));
152 }
153 
154 //------------------------------------------------------------------------------
155 
156 #endif // LARCOREALG_GEOMETRY_ROOTGEOMETRYNAVIGATOR_H
TGeoNode const * current() const
Returns the current node. Undefined if the path is empty.
Definition: GeoNodePath.cxx:51
Executes an operation on all the nodes of the ROOT geometry.
bool apply(GeoNodePath &path, Op &&op) const
Applies the specified operation to all nodes under the path.
Class representing a path in ROOT geometry.
void pop()
Removes the current node from the path, moving the current one up.
Definition: GeoNodePath.cxx:73
Test of util::counter and support utilities.
void append(TGeoNode const *node)
Adds a node to the current path.
Definition: GeoNodePath.cxx:67
Representation of a node and its ancestry.
Definition: GeoNodePath.h:34
ROOTGeometryNavigator(TGeoManager const &manager)
Constructor: picks the manager.
ROOT libraries.