Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-02 08:28:36

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 //------------------------------------------------
0027 // The Geant4 Virtual Monte Carlo package
0028 // Copyright (C) 2007 - 2015 Ivana Hrivnacova
0029 // All rights reserved.
0030 //
0031 // For the licensing terms see geant4_vmc/LICENSE.
0032 // Contact: root-vmc@cern.ch
0033 //-------------------------------------------------
0034 
0035 /// \file G4FieldBuilder.h
0036 /// \brief Definition of the G4FieldBuilder class
0037 ///
0038 /// \author I. Hrivnacova; IJCLab, Orsay
0039 
0040 #ifndef G4FIELDBUILDER_HH
0041 #define G4FIELDBUILDER_HH
0042 
0043 #include "G4Cache.hh"
0044 #include "G4FieldParameters.hh"
0045 #include "globals.hh"
0046 
0047 #include <vector>
0048 
0049 class G4FieldBuilderMessenger;
0050 class G4FieldSetup;
0051 class G4LogicalVolume;
0052 class G4EquationOfMotion;
0053 class G4MagIntegratorStepper;
0054 
0055 /// \brief The manger class for building magnetic or other field
0056 /// using the configuration in field parameters.
0057 ///
0058 /// Purpose: Provide a single 'place' to configure field & integration
0059 ///
0060 /// - It can configure a global field, and field(s) local to a (logical) volume
0061 /// - The parameter values can be configured by the user (else use a default)
0062 /// - They can be set/changed via a messenger provided or in the code
0063 ///      of the user detector construciton
0064 /// - It retains ownership of the following object(s):
0065 ///      field parameters and field setups, field
0066 ///
0067 ///  Note MT: an object of the builder class should be created on master only
0068 ///      (in DetectorConstruction constructor)
0069 ///      The functions SetGlobal/LocalField and ConstructFieldSetup should
0070 ///      be called on workers (in DetectorConstruction::ConstructSDandField )
0071 ///
0072 ///  This design/implementation covers the most common use cases.
0073 ///  It cannot be used to create some complex setups such as
0074 ///    - equations templated on the field type,
0075 ///    - steppers/drivers templated on the equation and field types.
0076 ///
0077 /// \author I. Hrivnacova; IJCLab, Orsay
0078 
0079 class G4FieldBuilder
0080 {
0081  public:
0082   /// Destructor
0083   ~G4FieldBuilder();
0084 
0085   // Static access method
0086   //
0087 
0088   /// Create the class instance, if it does not exist,
0089   /// and return it on the next calls.
0090   static G4FieldBuilder* Instance();
0091 
0092   /// Return the information if an instance exists
0093   static G4bool IsInstance();
0094 
0095   // Functions for constructing field setup
0096   //
0097 
0098   /// Create local magnetic field parameters (configuration) which can be then
0099   /// configured by the user via UI commands.
0100   /// The parameters are used in geometry only if a local magnetic field is
0101   /// associated with the volumes with the given name
0102   G4FieldParameters* CreateFieldParameters(const G4String& fieldVolName);
0103 
0104   /// Construct setups for all registered fields.
0105   void ConstructFieldSetup();
0106 
0107   /// Update magnetic field.
0108   /// This function must be called if the field parameters were changed
0109   /// in other than PreInit> phase.
0110   void UpdateField();
0111 
0112   /// Reinitialize if geometry has been modified.
0113   /// This function is called by G4RunManager during ReinitializeGeometry()
0114   void Reinitialize();
0115 
0116   // Set methods
0117   //
0118 
0119   /// Default field type is set to kMagnetic;
0120   /// this function should be called for other than magnetic field
0121   /// in order to update the default equation and stepper types.
0122   void SetFieldType(G4FieldType fieldType);
0123 
0124   // Set or reset the global field.
0125   // Update field objects, if the field was already constructed.
0126   // If warn, issue a warning if the previous field is deleted.
0127   void SetGlobalField(G4Field* field, G4bool warn = false);
0128 
0129   /// Register the local field in the map.
0130   /// Update field objects, if the field was already constructed.
0131   /// If warn, issue a warning if the previous field is deleted.
0132   /// The field is propagated to all volume daughters regardless
0133   /// if they have already assigned a field manager or not.
0134   /// When multiple local fields are defined (by calling this function
0135   /// multiple times), they will be applied in the order they were set.
0136   void SetLocalField(G4Field* field, G4LogicalVolume* lv, G4bool warn = false);
0137 
0138   /// Set user equation of motion
0139   void SetUserEquationOfMotion(
0140     G4EquationOfMotion* equation, G4String volumeName = "");
0141 
0142   /// Set user stepper
0143   void SetUserStepper(
0144     G4MagIntegratorStepper* stepper, G4String volumeName = "");
0145 
0146   /// Set verbose level
0147   void SetVerboseLevel(G4int value);
0148 
0149   // Get methods
0150   //
0151 
0152   /// Get field parameters with the given volumeName.
0153   /// Return global field parameters, if volume name is empty.
0154   G4FieldParameters* GetFieldParameters(const G4String& volumeName = "") const;
0155 
0156  private:
0157   /// Default constructor
0158   G4FieldBuilder();
0159   /// Not implemented
0160   G4FieldBuilder(const G4FieldBuilder& right) = delete;
0161   /// Not implemented
0162   G4FieldBuilder& operator=(const G4FieldBuilder& right) = delete;
0163 
0164   // Methods
0165 
0166   /// Get field parameters with the given volumeName or create them if they
0167   /// do not exist yet
0168   G4FieldParameters* GetOrCreateFieldParameters(const G4String& volumeName);
0169 
0170   /// Get field setup with the given logical volume
0171   G4FieldSetup* GetFieldSetup(G4LogicalVolume* lv);
0172 
0173   /// Create magnetic, electromagnetic or gravity field setup
0174   void CreateFieldSetup(G4Field* field,
0175     G4FieldParameters* fieldParameters, G4LogicalVolume* lv);
0176 
0177   /// Construct Geant4 global magnetic field setup
0178   void ConstructGlobalField();
0179 
0180   /// Construct Geant4 local magnetic field setups from the local fields map
0181   void ConstructLocalFields();
0182 
0183   /// Update all field setups
0184   void UpdateFieldSetups();
0185 
0186   // helper methods
0187   std::vector<G4FieldSetup*>& GetFieldSetups();
0188   std::vector<std::pair<G4LogicalVolume*, G4Field*>>& GetLocalFields();
0189 
0190   // Data members
0191 
0192   /// Information if an instance exists
0193   inline static G4ThreadLocal G4bool fgIsInstance { false };
0194 
0195   /// Messenger for this class
0196   G4FieldBuilderMessenger* fMessenger = nullptr;
0197 
0198   /// Field parameters
0199   std::vector<G4FieldParameters*> fFieldParameters;
0200 
0201   /// Field setups
0202   G4Cache<std::vector<G4FieldSetup*>*> fFieldSetups;
0203 
0204   /// Registered global field
0205   static G4ThreadLocal G4Field* fGlobalField;
0206 
0207   /// Registered local fields
0208   G4Cache<std::vector<std::pair<G4LogicalVolume*, G4Field*>>*> fLocalFields;
0209 
0210   /// info if field objects were constructed
0211   static G4ThreadLocal G4bool fIsConstructed;
0212 
0213   /// verbose level
0214   G4int fVerboseLevel = 1;
0215 };
0216 
0217 // inline methods
0218 
0219 inline G4bool G4FieldBuilder::IsInstance()
0220 {
0221   // Return the information if an instance exists
0222   return fgIsInstance;
0223 }
0224 
0225 inline void G4FieldBuilder::SetVerboseLevel(G4int value)
0226 {
0227   // Set verbose level
0228   fVerboseLevel = value;
0229 }
0230 
0231 inline std::vector<G4FieldSetup*>& G4FieldBuilder::GetFieldSetups()
0232 {
0233   // Return reference to field setups from G4Cache
0234   return *fFieldSetups.Get();
0235 }
0236 
0237 inline std::vector<std::pair<G4LogicalVolume*, G4Field*>>& G4FieldBuilder::GetLocalFields()
0238 {
0239   // Return reference to local fields map from G4Cache
0240   return *fLocalFields.Get();
0241 }
0242 
0243 #endif // G4FIELDBUILDER_HH