Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:33

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file geocel/g4/VisitGeantVolumes.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <unordered_set>
0011 #include <vector>
0012 #include <G4LogicalVolume.hh>
0013 
0014 #include "corecel/Assert.hh"
0015 #include "corecel/cont/Range.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Do a recursive depth-first listing of Geant4 logical volumes.
0022  *
0023  * This will visit each volume exactly once based on when it's encountered in
0024  * the hierarchy. The visitor function F should have the signature
0025  * \code void(*)(G4LogicalVolume const&) \endcode .
0026  */
0027 template<class F>
0028 void visit_geant_volumes(F&& vis, G4LogicalVolume const& parent_vol)
0029 {
0030     std::unordered_set<G4LogicalVolume const*> visited;
0031     std::vector<G4LogicalVolume const*> stack{&parent_vol};
0032 
0033     while (!stack.empty())
0034     {
0035         G4LogicalVolume const* lv = stack.back();
0036         stack.pop_back();
0037         vis(*lv);
0038         for (auto const i : range(lv->GetNoDaughters()))
0039         {
0040             G4LogicalVolume* daughter = lv->GetDaughter(i)->GetLogicalVolume();
0041             CELER_ASSERT(daughter);
0042             auto&& [iter, inserted] = visited.insert(daughter);
0043             if (inserted)
0044             {
0045                 stack.push_back(daughter);
0046             }
0047         }
0048     }
0049 }
0050 
0051 //---------------------------------------------------------------------------//
0052 }  // namespace celeritas