Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:14:07

0001 // This file is part of VecGeom and is distributed under the
0002 // conditions in the file LICENSE.txt in the top directory.
0003 // For the full list of authors see CONTRIBUTORS.txt and `git log`.
0004 
0005 /// \brief Declaration of the logical volume.
0006 /// \file LogicalVolume.h
0007 /// \author created by Johannes de Fine Licht, Sandro Wenzel (CERN)
0008 
0009 #ifndef VECGEOM_VOLUMES_LOGICALVOLUME_H_
0010 #define VECGEOM_VOLUMES_LOGICALVOLUME_H_
0011 
0012 #include "VecGeom/base/Cuda.h"
0013 #include "VecGeom/base/Global.h"
0014 #include "VecGeom/base/Vector.h"
0015 #include "VecGeom/volumes/UnplacedVolume.h"
0016 
0017 #include <string>
0018 #include <list>
0019 #include <set>
0020 #include <typeindex>
0021 #include <typeinfo>
0022 
0023 class TGeoShape;
0024 
0025 namespace vecgeom {
0026 
0027 VECGEOM_DEVICE_FORWARD_DECLARE(class LogicalVolume;);
0028 VECGEOM_DEVICE_FORWARD_DECLARE(class VPlacedVolume;);
0029 VECGEOM_DEVICE_FORWARD_DECLARE(class VLevelLocator;);
0030 VECGEOM_DEVICE_FORWARD_DECLARE(class VSafetyEstimator;);
0031 VECGEOM_DEVICE_FORWARD_DECLARE(class VNavigator;);
0032 
0033 VECGEOM_DEVICE_DECLARE_CONV(class, LogicalVolume);
0034 
0035 inline namespace VECGEOM_IMPL_NAMESPACE {
0036 
0037 class Region; // forward declaration for a class which
0038               // a user needs to implement in his code
0039 
0040 class VLevelLocator;
0041 class VSafetyEstimator;
0042 class VNavigator;
0043 typedef VPlacedVolume const *Daughter;
0044 class GeoManager;
0045 
0046 /**
0047  * \brief Class responsible for storing the list of daughter volumes
0048  *        of an unplaced volume and various associated properties.
0049  *
0050  *  A logical volume adds various properties to unplaced volumes, notably
0051  *  the list of daughter volumes it contains. As such a logical volume
0052  *  describes an un-positioned sub-tree of the geometry hierarchy.
0053  *  The class also holds various pointers to structures associated with
0054  *  unplaced volumes, such as those used by simulation engines for tracking.
0055  *  This class follows largely the class existing in Geant4: G4LogicalVolume.
0056  */
0057 class LogicalVolume {
0058   friend class GeoManager;
0059 
0060 private:
0061   /// Pointer to concrete unplaced volume/shape
0062   VUnplacedVolume const *fUnplacedVolume;
0063 
0064   unsigned int fId; ///< global id of logical volume object
0065 
0066   std::string *fLabel; ///< name of logical volume
0067 
0068   static int gIdCount; ///< a static class counter
0069 
0070   //--- The following pointers are used mainly by simulation packages ---
0071   // Consider enabling them only when needed
0072 
0073   /// A pointer member to register arbitrary objects with logical volumes.
0074   /// Included for the moment to model UserExtension like in a TGeoVolume.
0075   void *fUserExtensionPtr;
0076 
0077   void *fMaterialPtr; ///< Pointer to some user material class (used by Geant-V)
0078 
0079   void *fMaterialCutsPtr; ///< Pointer to some user cuts (used by Geant-V)
0080 
0081   void *fBasketManagerPtr; ///< A specific pointer used by Geant-V
0082 
0083   Region *fRegion = nullptr; ///< Pointer to a region object (following the Geant4 Region concept)
0084 
0085   //--- The following pointers are used by VecGeom itself ---
0086 
0087   VLevelLocator const *fLevelLocator;       ///< A locator class for this logical volume.
0088   VSafetyEstimator const *fSafetyEstimator; ///< A safety estimator class for this logical volume.
0089   VNavigator const *fNavigator;             ///< Pointer to attached VecGeom navigator.
0090 
0091   /// The container of daughter (placed) volumes which are placed inside this logical volume
0092   Vector<Daughter> *fDaughters;
0093 
0094   using CudaDaughter_t = cuda::VPlacedVolume const *;
0095   friend class CudaManager;
0096 
0097   // possibility to change pointer of daughter volumes ( can be used by GeoManager )
0098   //  void SetDaughter(unsigned int i, VPlacedVolume const *pvol);
0099 
0100 public:
0101 #ifndef VECCORE_CUDA
0102   /// Standard constructor taking a name and an unplaced volume
0103   LogicalVolume(char const *const label, VUnplacedVolume const *const unplaced_vol);
0104 
0105   /// Standard constructor taking an unplaced volume
0106   LogicalVolume(VUnplacedVolume const *const unplaced_vol) : LogicalVolume("", unplaced_vol) {}
0107 
0108   /// copy operators deleted
0109   LogicalVolume(LogicalVolume const &other) = delete;
0110   LogicalVolume *operator=(LogicalVolume const &other) = delete;
0111 
0112 #else
0113   VECCORE_ATT_DEVICE
0114   LogicalVolume(VUnplacedVolume const *const unplaced_vol,
0115                 unsigned int id, Vector<Daughter> *GetDaughter);
0116 #endif
0117 
0118   ~LogicalVolume();
0119 
0120   /// Returns the unplaced volume
0121   VECCORE_ATT_HOST_DEVICE
0122   VECGEOM_FORCE_INLINE
0123   VUnplacedVolume const *GetUnplacedVolume() const { return fUnplacedVolume; }
0124 
0125   // will be deprecated in favour of better encapsulation of internal storage
0126   /// Returns the list of daughter volumes
0127   VECCORE_ATT_HOST_DEVICE
0128   VECGEOM_FORCE_INLINE
0129   Vector<Daughter> const &GetDaughters() const { return *fDaughters; }
0130 
0131   // will be deprecated in favour of better encapsulation of internal storage
0132   /// Returns pointer to the list of daughter volumes
0133   VECCORE_ATT_HOST_DEVICE
0134   VECGEOM_FORCE_INLINE
0135   Vector<Daughter> const *GetDaughtersp() const { return fDaughters; }
0136 
0137   /// Returns pointer to the list of daughter volumes
0138   VECCORE_ATT_HOST_DEVICE
0139   VECGEOM_FORCE_INLINE
0140   Vector<Daughter> *GetDaughtersp() { return fDaughters; }
0141 
0142   /// Returns user request for caching transformations
0143   VECCORE_ATT_HOST_DEVICE
0144   VECGEOM_FORCE_INLINE
0145   bool IsReqCaching() const { return false; }
0146 
0147   //  VECCORE_ATT_HOST_DEVICE
0148   //  VECGEOM_FORCE_INLINE
0149   //  VPlacedVolume const* GetDaughter(unsigned int i) const { return daughters_->operator[](i); }
0150   //
0151   //  VECCORE_ATT_HOST_DEVICE
0152   //  VECGEOM_FORCE_INLINE
0153   //  unsigned int GetNDaughters() const { return daughters_->size(); }
0154 
0155   /// Returns the total number of placeds volume contained in this logical volume AND below.
0156   size_t GetNTotal() const;
0157 
0158   /// Returns value of static instance counter
0159   static unsigned int GetIdCount() { return (unsigned int)gIdCount; }
0160 
0161   /// Returns the user extension pointer.
0162   VECGEOM_FORCE_INLINE
0163   void *GetUserExtensionPtr() const { return fUserExtensionPtr; }
0164 
0165   /// Returns the material extension pointer.
0166   VECCORE_ATT_HOST_DEVICE
0167   VECGEOM_FORCE_INLINE
0168   void *GetMaterialPtr() const { return fMaterialPtr; }
0169 
0170   /// Returns the cuts extension pointer.
0171   VECCORE_ATT_HOST_DEVICE
0172   VECGEOM_FORCE_INLINE
0173   void *GetMaterialCutsPtr() const { return fMaterialCutsPtr; }
0174 
0175   /// Returns the basked manager pointer (Geant-V specific).
0176   VECCORE_ATT_HOST_DEVICE
0177   VECGEOM_FORCE_INLINE
0178   void *GetBasketManagerPtr() const { return fBasketManagerPtr; }
0179 
0180   /// Returns the level locator (used by VecGeom navigation).
0181   VECCORE_ATT_HOST_DEVICE
0182   VECGEOM_FORCE_INLINE
0183   VLevelLocator const *GetLevelLocator() const { return fLevelLocator; }
0184 
0185   /// Sets the level locator (used by VecGeom navigation).
0186   VECCORE_ATT_HOST_DEVICE
0187   VECGEOM_FORCE_INLINE
0188   void SetLevelLocator(VLevelLocator const *locator) { fLevelLocator = locator; }
0189 
0190   /// Returns the safety estimator (used by VecGeom navigation).
0191   VECCORE_ATT_HOST_DEVICE
0192   VECGEOM_FORCE_INLINE
0193   VSafetyEstimator const *GetSafetyEstimator() const { return fSafetyEstimator; }
0194 
0195   /// Sets the safety estimator (used by VecGeom navigation).
0196   VECCORE_ATT_HOST_DEVICE
0197   VECGEOM_FORCE_INLINE
0198   void SetSafetyEstimator(VSafetyEstimator const *est) { fSafetyEstimator = est; }
0199 
0200   /// Returns the navigator for this logical volume (used by VecGeom navigation).
0201   VECCORE_ATT_HOST_DEVICE
0202   VECGEOM_FORCE_INLINE
0203   VNavigator const *GetNavigator() const { return fNavigator; }
0204 
0205   /// Sets the navigator for this logical volume (used by VecGeom navigation).
0206   VECCORE_ATT_HOST_DEVICE
0207   VECGEOM_FORCE_INLINE
0208   void SetNavigator(VNavigator const *n) { fNavigator = n; }
0209 
0210   /// Returns the id of this logical volume.
0211   VECCORE_ATT_HOST_DEVICE
0212   VECGEOM_FORCE_INLINE
0213   unsigned int id() const { return fId; }
0214 
0215   /// Returns the name of this logical volume.
0216   VECCORE_ATT_HOST_DEVICE
0217   const char *GetName() const
0218   {
0219     #ifndef VECCORE_CUDA
0220       return fLabel->c_str();
0221     #else
0222       return "- Names unavailable on GPU -";
0223     #endif
0224   }
0225   /// Returns the name of this logical volume as string.
0226   std::string const &GetLabel() const { return *fLabel; }
0227 
0228   /// Sets the name/label of this logical volume.
0229   void SetLabel(char const *const label)
0230   {
0231     if (fLabel) delete fLabel;
0232     fLabel = new std::string(label);
0233   }
0234 
0235   /// Set user extension pointer of this logical volume.
0236   VECGEOM_FORCE_INLINE
0237   void SetUserExtensionPtr(void *userpointer) { fUserExtensionPtr = userpointer; }
0238 
0239   /// Set the material pointer of this logical volume.
0240   VECGEOM_FORCE_INLINE
0241   void SetMaterialPtr(void *matpointer) { fMaterialPtr = matpointer; }
0242 
0243   /// Set the cuts pointer of this logical volume.
0244   VECGEOM_FORCE_INLINE
0245   void SetMaterialCutsPtr(void *matcutpointer) { fMaterialCutsPtr = matcutpointer; }
0246 
0247   /// Set the basket manager pointer of this logical volume (Geant-V specific).
0248   VECGEOM_FORCE_INLINE
0249   VECCORE_ATT_HOST_DEVICE
0250   void SetBasketManagerPtr(void *basketpointer) { fBasketManagerPtr = basketpointer; }
0251 
0252   /// Print the daughter information of this logical volume with a given indentation.
0253   VECCORE_ATT_HOST_DEVICE
0254   void Print(const int indent = 0) const;
0255 
0256   /// Print the daughter information of this logical volume and of all containing daughters
0257   /// up to a certain depth
0258   VECCORE_ATT_HOST_DEVICE
0259   void PrintContent(const int depth = 0) const;
0260 
0261   /// Produce a labeled placed volume out of this logical volume by using a given transformation.
0262   VPlacedVolume *Place(char const *const label, Transformation3D const *const transformation) const;
0263 
0264   /// Produce a placed volume out of this logical volume by using a given transformation.
0265   VPlacedVolume *Place(Transformation3D const *const transformation) const;
0266 
0267   /// Produce a labeled placed volume out of this logical volume with identity transformation.
0268   VPlacedVolume *Place(char const *const label) const;
0269 
0270   /// Produce a trivially placed volume out of this logical volume with identity transformation.
0271   VPlacedVolume *Place() const;
0272 
0273   /// Adds/places a named daughter volume inside this logical volume.
0274   /// @param label Name of placed daughter volume.
0275   /// @param volume The logical volume to be added to the list of daughter volumes.
0276   /// @param transformation The transformation used to place the daughter.
0277   VPlacedVolume const *PlaceDaughter(char const *const label, LogicalVolume *const volume,
0278                                      Transformation3D const *const transformation);
0279 
0280   /// Adds/places a daughter volume inside this logical volume.
0281   /// @param volume The logical volume to be added to the list of daughter volumes.
0282   /// @param transformation The transformation used to place the daughter.
0283   VPlacedVolume const *PlaceDaughter(LogicalVolume *const volume, Transformation3D const *const transformation);
0284 
0285   /// Adds/places an already existing placed daughter volume
0286   void PlaceDaughter(VPlacedVolume *const placed);
0287 
0288   /// Returns true of at least one of the daughters is an assembly.
0289   bool ContainsAssembly() const;
0290 
0291   friend std::ostream &operator<<(std::ostream &os, LogicalVolume const &vol);
0292 
0293   /// Returns the region object associated to this logical volume.
0294   Region *GetRegion() { return fRegion; }
0295 
0296   /** Sets the Region pointer for this logical Volume.
0297    * @param region The Region object that  will be associated to this logical volume.
0298    * @param pushdown If pushdown=true this region will be applied to all logical volumes below (in the geom hierarchy)
0299    *
0300    * Set region will override any existing region. The user has to make sure to call in the right order.
0301    */
0302   void SetRegion(Region *region, bool pushdown = true);
0303 
0304 #ifdef VECGEOM_CUDA_INTERFACE
0305   DevicePtr<cuda::LogicalVolume> CopyToGpu(DevicePtr<cuda::VUnplacedVolume> const unplaced_vol, int id,
0306                                            DevicePtr<cuda::Vector<CudaDaughter_t>> GetDaughter) const;
0307   DevicePtr<cuda::LogicalVolume> CopyToGpu(DevicePtr<cuda::VUnplacedVolume> const unplaced_vol, int id,
0308                                            DevicePtr<cuda::Vector<CudaDaughter_t>> GetDaughter,
0309                                            DevicePtr<cuda::LogicalVolume> const gpu_ptr) const;
0310 #endif
0311 
0312 private:
0313   std::set<LogicalVolume *> GetSetOfDaughterLogicalVolumes() const;
0314 
0315 }; // End class
0316 
0317 inline void LogicalVolume::SetRegion(Region *region, bool pushdown)
0318 {
0319   fRegion = region;
0320   if (pushdown) {
0321     for (auto &lv : GetSetOfDaughterLogicalVolumes()) {
0322       lv->SetRegion(region, true);
0323     }
0324   }
0325 }
0326 
0327 } // namespace VECGEOM_IMPL_NAMESPACE
0328 } // namespace vecgeom
0329 
0330 #endif // VECGEOM_VOLUMES_LOGICALVOLUME_H_