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