Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-13 09:11:05

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // G4VPhysicalVolume
0027 //
0028 // Class description:
0029 //
0030 // This is an abstract base class for the representation of a positioned volume.
0031 // The volume is placed within a mother volume, relative to its coordinate 
0032 // system. Either a single positioned volume or many positioned volumes can 
0033 // be represented by a particular G4VPhysicalVolume.
0034 
0035 // Author: Paul Kent (CERN), 24.07.1995 - First non-stub version
0036 // --------------------------------------------------------------------
0037 #ifndef G4VPHYSICALVOLUME_HH
0038 #define G4VPHYSICALVOLUME_HH
0039 
0040 #include "G4Types.hh"
0041 #include "G4String.hh"
0042 
0043 #include "geomdefs.hh"
0044 
0045 #include "G4RotationMatrix.hh"
0046 #include "G4ThreeVector.hh"
0047 #include "G4GeomSplitter.hh"
0048 
0049 class G4LogicalVolume;
0050 class G4VPVParameterisation;
0051 
0052 /**
0053  * @brief G4PVData encapsulates the fields associated to G4VPhysicalVolume
0054  * that are not read-only - they will change during simulation and must have
0055  * a per-thread state.
0056  */
0057 
0058 class G4PVData
0059 {
0060   public:
0061 
0062     G4PVData() = default;
0063 
0064     void initialize()
0065     {
0066       frot = nullptr;
0067       tx = 0.; ty = 0.; tz = 0.;
0068     }
0069 
0070     G4RotationMatrix* frot = nullptr;
0071     G4double tx = 0., ty = 0., tz = 0.;
0072 };
0073 
0074 /** Type defined for use of G4PVData objects. */
0075 using G4PVManager = G4GeomSplitter<G4PVData>;
0076 
0077 /**
0078  * @brief G4VPhysicalVolume is an abstract base class for the representation
0079  * of a positioned volume. The volume is placed within a mother volume,
0080  * relative to its coordinate system. Either a single positioned volume or
0081  * many positioned volumes can be represented by a particular G4VPhysicalVolume.
0082  */
0083 
0084 class G4VPhysicalVolume
0085 {
0086   public:
0087 
0088     /**
0089      * Constructor for G4VPhysicalVolume; it initialises a volume, positioned
0090      * in a frame which is rotated by 'pRot', relative to the coordinate system
0091      * of the mother volume 'pMother'. The center of the object is then placed
0092      * at 'tlate' in the new coordinates. If 'pRot' is null, the volume is
0093      * unrotated with respect to its mother. The physical volume is added to
0094      * the mother's logical volume.
0095      * The constructor must be called by all subclasses; 'pMother' must point
0096      * to a valid parent volume, except in the case of the world/top volume,
0097      * when it can be a null pointer. The constructor also registers the volume
0098      * within the physical volumes store.
0099      *  @param[in] pRot The pointer to the rotation matrix.
0100      *  @param[in] tlate The translation vector coordinates.
0101      *  @param[in] pName The name of the volume.
0102      *  @param[in] pLogical The pointer to its logical volume.
0103      *  @param[in] pMother The pointer to the mother's physical volume.
0104      */
0105     G4VPhysicalVolume(G4RotationMatrix* pRot,
0106                 const G4ThreeVector& tlate,
0107                 const G4String& pName,
0108                       G4LogicalVolume* pLogical,
0109                       G4VPhysicalVolume* pMother);
0110 
0111     /**
0112      * Destructor, will be subclassed. Removes volume from the volume store.
0113      */
0114     virtual ~G4VPhysicalVolume();
0115 
0116     /**
0117      * Copy constructor and assignement operator not allowed.
0118      */
0119     G4VPhysicalVolume(const G4VPhysicalVolume&) = delete;
0120     G4VPhysicalVolume& operator=(const G4VPhysicalVolume&) = delete;
0121 
0122     /**
0123      * Equality defined by equal addresses only..
0124      */
0125     inline G4bool operator == (const G4VPhysicalVolume& p) const;
0126 
0127     // Accessors. They make a distinction between whether the rotation or
0128     // translation is being made for the frame or the object/volume that is
0129     // being placed (they are the inverse of each other).
0130 
0131     /**
0132      * Accessors returning the rotation/translation of the *object* relative
0133      * to the mother.
0134      */
0135     G4RotationMatrix* GetObjectRotation() const;       //  Obsolete 
0136     G4RotationMatrix GetObjectRotationValue() const;   //  Replacement
0137     G4ThreeVector GetObjectTranslation() const;
0138 
0139     /**
0140      * Accessors returning the rotation/translation of the *frame* used to
0141      * position this volume in its mother volume (opposite of object rot/trans).
0142      */
0143     const G4RotationMatrix* GetFrameRotation() const;
0144     G4ThreeVector GetFrameTranslation() const;
0145 
0146     /**
0147      * Old access functions, that do not distinguish between frame/object!
0148      * They simply return the translation/rotation of the volume.
0149      */
0150     const G4ThreeVector GetTranslation() const;
0151     const G4RotationMatrix* GetRotation() const;
0152     G4RotationMatrix* GetRotation();
0153 
0154     // Modifiers for translation and rotation
0155 
0156     /**
0157      * Sets the translation vector.
0158      */
0159     void SetTranslation(const G4ThreeVector& v);
0160 
0161     /**
0162      * Sets the rotation matrix. NOT INTENDED FOR GENERAL USE.
0163      * Non constant version, used to change transformation for the
0164      * replication/parameterisation mechanism.
0165      */
0166     void SetRotation(G4RotationMatrix*);
0167 
0168     /**
0169      * Returns the associated logical volume pointer.
0170      */
0171     inline G4LogicalVolume* GetLogicalVolume() const;
0172 
0173     /**
0174      * Sets the logical volume pointer. Must not be called when geometry
0175      * is closed.
0176      */
0177     inline void SetLogicalVolume(G4LogicalVolume* pLogical);
0178 
0179     inline G4LogicalVolume* GetMotherLogical() const;
0180       // Return the current mother logical volume pointer.
0181     inline void SetMotherLogical(G4LogicalVolume* pMother);
0182       // Set the mother logical volume. Must not be called when geometry closed.
0183 
0184     /**
0185      * Getter/setter for the volume's name.
0186      */
0187     inline const G4String& GetName() const;
0188     void SetName(const G4String& pName);
0189 
0190     /**
0191      * Returns the number of object entities (1 for normal placements,
0192      * n for replicas or parameterised).
0193      */
0194     virtual G4int GetMultiplicity() const;
0195 
0196     // Functions required of subclasses
0197 
0198     /**
0199      * Characterises the type of volume - normal/replicated/parameterised.
0200      */
0201     virtual EVolume VolumeType() const = 0;
0202 
0203     /**
0204      * NOT implemented. Should return true if the volume is MANY type.
0205      */
0206     virtual G4bool IsMany() const = 0;
0207  
0208     /**
0209      * Accessor/modifier for optional handling of the volume copy-number.
0210      */
0211     virtual G4int GetCopyNo() const = 0;
0212     virtual void SetCopyNo(G4int CopyNo) = 0;
0213 
0214     /**
0215      * Returns true if the volume is replicated (single object instance
0216      * represents many real volumes), else false.
0217      */
0218     virtual G4bool IsReplicated() const = 0;
0219 
0220     /**
0221      * Returns true if the volume is parameterised (single object instance
0222      * represents many real parameterised volumes), else false.
0223      */
0224     virtual G4bool IsParameterised() const = 0;
0225 
0226     /**
0227      * Returns a pointer to the replicas parameterisation object/algorithm
0228      * (able to compute dimensions and transformations of replicas), or a
0229      * null pointer if not applicable.
0230      */
0231     virtual G4VPVParameterisation* GetParameterisation() const = 0;
0232 
0233     /**
0234      * Returns the replication information. No-op for non replicated volumes.
0235      *  @param[in,out] axis The axis of replication/parameterisation.
0236      *  @param[in,out] nReplicas The number of replicated/parameterised objects.
0237      *  @param[in,out] width The width of replicated object.
0238      *  @param[in,out] offset The optional offset distance from mother's border.
0239      *  @param[in,out] consuming Flag of replica characterisation (always true
0240      *                 for pure replicas).
0241      */
0242     virtual void GetReplicationData(EAxis& axis,
0243                                     G4int& nReplicas,
0244                                     G4double& width,
0245                                     G4double& offset,
0246                                     G4bool& consuming) const = 0;
0247 
0248     /**
0249      * Returns true if the underlying volume structure is regular.
0250      */
0251     virtual G4bool IsRegularStructure() const = 0;
0252 
0253     /**
0254      * Returns non-zero code in case the underlying volume structure is regular,
0255      * voxel-like. The value is an identifier for the structure type.
0256      * If non-zero the volume is a candidate for specialised navigation such
0257      * as 'nearest neighbour' directly on volumes.
0258      */
0259     virtual G4int GetRegularStructureId() const = 0;
0260 
0261     /**
0262      * Verifies if the placed volume is overlapping with the existing
0263      * daughters or with the mother volume. It provides a default resolution
0264      * for the number of points to be generated and verified. A concrete
0265      * implementation is done and required only for placed and parameterised
0266      * volumes. Returns true if the volume is overlapping.
0267      *  @param[in] res The number of points to generate on volume's surface.
0268      *  @param[in] tol The precision tolerance for the overlap check, below
0269      *             which to ignore overlaps (default is maximim precision).
0270      *  @param[in] verbose Verbosity mode (default is true).
0271      *  @param[in] errMax Maximum of overlaps errors to report (default is 1).
0272      *  @returns True if an overlap occurs.
0273      */
0274     virtual G4bool CheckOverlaps(G4int res=1000, G4double tol=0.,
0275                                  G4bool verbose=true, G4int errMax=1);
0276 
0277     /**
0278      * Fake default constructor for usage restricted to direct object
0279      * persistency for clients requiring preallocation of memory for
0280      * persistifiable objects.
0281      */
0282     G4VPhysicalVolume(__void__&);
0283 
0284     /**
0285      * Returns the instance ID for multi-threading.
0286      */
0287     inline G4int GetInstanceID() const;
0288 
0289     /**
0290      * Returns the private data instance manager for multi-threading.
0291      */
0292     static const G4PVManager& GetSubInstanceManager();
0293 
0294     /**
0295      * Clears the memory allocated by the MT sub-instance manager.
0296      */
0297     static void Clean();
0298 
0299     /**
0300      * Old VolumeType() method, replaced by virtual method, kept for checking.
0301      */
0302     inline EVolume DeduceVolumeType() const;
0303       
0304   protected:
0305 
0306     /**
0307      * This method is similar to the constructor. It is used by each worker
0308      * thread to achieve the partial effect as that of the master thread.
0309      */
0310     void InitialiseWorker(G4VPhysicalVolume* pMasterObject,
0311                           G4RotationMatrix* pRot, const G4ThreeVector& tlate);
0312 
0313     /**
0314      * This method is similar to the destructor. It is used by each worker
0315      * thread to achieve the partial effect as that of the master thread.
0316      */
0317     void TerminateWorker(G4VPhysicalVolume* pMasterObject);
0318 
0319   protected:
0320 
0321     /** For use in implementing the per-thread data.
0322         It is equivalent to a pointer to a G4PVData object. */
0323     G4int instanceID;
0324 
0325     /** Needed to use G4PVManager for the G4PVData per-thread objects. */
0326     G4GEOM_DLL static G4PVManager subInstanceManager;
0327 
0328   private:
0329 
0330     /** The logical volume representing the attributes of the volume. */
0331     G4LogicalVolume* flogical = nullptr;
0332 
0333     /** The name of the volume. */
0334     G4String fname;
0335 
0336     /** The current mother logical volume. */
0337     G4LogicalVolume* flmother = nullptr;
0338 
0339     /** Shadow pointer for use of object persistency. */
0340     G4PVData* pvdata = nullptr;
0341 };
0342 
0343 // NOTE: 
0344 // The type G4PVManager is introduced to encapsulate the methods used by
0345 // both the master thread and worker threads to allocate memory space for
0346 // the fields encapsulated by the class G4PVData. When each thread
0347 // initializes the value for these fields, it refers to them using a macro
0348 // definition defined below. For every G4VPhysicalVolume instance, there is
0349 // a corresponding G4PVData instance. All G4PVData instances are organized
0350 // by the class G4PVManager as an array.
0351 // The field "int instanceID" is added to the class G4VPhysicalVolume.
0352 // The value of this field in each G4VPhysicalVolume instance is the subscript
0353 // of the corresponding G4PVData instance.
0354 // In order to use the class G4PVManager, we add a static member in the class
0355 // G4VPhysicalVolume as follows: "static G4PVManager subInstanceManager;".
0356 // For the master thread, the array for G4PVData instances grows dynamically
0357 // along with G4VPhysicalVolume instances are created. For each worker thread,
0358 // it copies the array of G4PVData instances from the master thread.           
0359 // In addition, it invokes a method similiar to the constructor explicitly
0360 // to achieve the partial effect for each instance in the array.
0361 
0362 #include "G4VPhysicalVolume.icc"
0363 
0364 #endif