DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4FastSimShowerModel.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
16 #include <DDG4/Geant4Mapping.h>
17 #include <DDG4/Geant4Kernel.h>
18 
19 // Geant4 include files
20 #include <G4FastSimulationManager.hh>
21 #include <G4VFastSimulationModel.hh>
22 #include <G4TouchableHandle.hh>
23 #include <G4ParticleTable.hh>
24 #include <G4FastStep.hh>
25 
26 // C/C++ include files
27 #include <sstream>
28 
29 using namespace dd4hep::sim;
30 
32 namespace dd4hep {
33 
35  namespace sim {
36 
39 
41 
48  class Geant4ShowerModelWrapper : public G4VFastSimulationModel {
50  Geant4FastSimShowerModel* m_model { nullptr };
51 
52  public:
56  virtual ~Geant4ShowerModelWrapper() = default;
58  virtual G4bool IsApplicable(const G4ParticleDefinition& particle) override;
60  virtual G4bool ModelTrigger(const G4FastTrack& track) override;
62  virtual void DoIt(const G4FastTrack& track, G4FastStep& step) override;
63  };
64 
65  }
66 }
67 
70  : G4VFastSimulationModel(model->name()), m_model(model)
71 {
72 }
73 
75 G4bool Geant4ShowerModelWrapper::IsApplicable(const G4ParticleDefinition& particle) {
76  return this->m_model->check_applicability(particle);
77 }
78 
80 G4bool Geant4ShowerModelWrapper::ModelTrigger(const G4FastTrack& track) {
81  return this->m_model->check_trigger(track);
82 }
83 
85 void Geant4ShowerModelWrapper::DoIt(const G4FastTrack& track, G4FastStep& step) {
86  this->m_model->modelShower(track, step);
87 }
88 
91  : Geant4DetectorConstruction(ctxt, nam)
92 {
93  this->declareProperty("RegionName", this->m_regionName);
94  this->declareProperty("ApplicableParticles", this->m_applicablePartNames);
95  this->declareProperty("Enable", this->m_enable);
96  this->declareProperty("StepLength", this->m_stepX0);
97  this->declareProperty("Emin", this->m_eMin);
98  this->declareProperty("Emax", this->m_eMax);
99  this->declareProperty("Ekill", this->m_eKill);
100  this->declareProperty("Etrigger", this->m_eTriggerNames);
101  this->m_wrapper= new Geant4ShowerModelWrapper(this);
102 }
103 
106  detail::deletePtr(m_model);
107  detail::deletePtr(m_wrapper);
108 }
109 
111 G4ParticleDefinition* Geant4FastSimShowerModel::getParticleDefinition(const std::string& particle) const {
112  G4ParticleTable* pt = G4ParticleTable::GetParticleTable();
113  G4ParticleDefinition* def = pt->FindParticle(particle);
114  if ( def ) return def;
115  except("Failed to access Geant4 particle definition: %s", particle.c_str());
116  return nullptr;
117 }
118 
119 G4Material* Geant4FastSimShowerModel::getMaterial(const std::string& mat_name) const {
120  auto& kernel = this->context()->kernel();
122  Material material = kernel.detectorDescription().material(mat_name);
123  if ( material.isValid() ) {
124  auto mat_iter = mapping.g4Materials.find(material);
125  if ( mat_iter != mapping.g4Materials.end() ) {
126  return (*mat_iter).second;
127  }
128  }
129  except("Failed to access shower parametrization material: %s", mat_name.c_str());
130  return nullptr;
131 }
132 
134 G4Region* Geant4FastSimShowerModel::getRegion(const std::string& nam) const {
135  auto& kernel = this->context()->kernel();
137  Region rg = kernel.detectorDescription().region(nam);
138  if ( !rg.isValid() ) {
139  except("Failed to access the region %s from the detector description.", nam.c_str());
140  }
141  auto region_iter = mapping.g4Regions.find(rg);
142  if ( region_iter == mapping.g4Regions.end() ) {
143  except("Failed to locate G4Region: %s from the Geant4 mapping.", nam.c_str());
144  }
145  G4Region* region = (*region_iter).second;
146  if ( region ) {
147  }
148  return region;
149 }
150 
153  if ( !region ) {
154  except("Geant4FastSimShowerModel::addShowerModel: Invalid G4Region reference!");
155  }
156  // Retrieves the Fast Simulation Manage and add the model
157  G4FastSimulationManager* fastsimManager = region->GetFastSimulationManager();
158  if ( !fastsimManager ) {
159  fastsimManager = new G4FastSimulationManager(region, true);
160  }
161  // add this model to the Fast Simulation Manager.
162  if ( m_model )
163  fastsimManager->AddFastSimulationModel(m_model);
164  else if ( m_wrapper )
165  fastsimManager->AddFastSimulationModel(m_wrapper);
166  else
167  except("Geant4FastSimShowerModel::addShowerModel: Invalid shower model reference!");
168 }
169 
172  this->m_applicableParticles.clear();
173  for(const std::string& p : m_applicablePartNames) {
174  G4ParticleDefinition* def = (p=="*") ? nullptr : this->getParticleDefinition(p);
175  this->m_applicableParticles.emplace(def);
176  }
177 }
178 
181 }
182 
185  G4Region* region = this->getRegion(this->m_regionName);
186  for(const auto& prop : this->m_eTriggerNames) {
187  G4ParticleDefinition* def = this->getParticleDefinition(prop.first);
188  double val = dd4hep::_toDouble(prop.second) ; // allready in G4units
189  this->m_eTriggerCut.emplace(def, val);
190  this->info("Set Energy(ModelTrigger) [%-16s] = %8.4f GeV", prop.first.c_str(), val);
191  }
192  this->m_model = nullptr;
193  this->addShowerModel(region);
194 }
195 
197 void Geant4FastSimShowerModel::killParticle(G4FastStep& step, double deposit, double step_length) {
198  step.KillPrimaryTrack();
199  step.ProposePrimaryTrackPathLength(step_length);
200  step.ProposeTotalEnergyDeposited(deposit);
201 }
202 
204 bool Geant4FastSimShowerModel::check_applicability(const G4ParticleDefinition& particle) {
205  return
206  this->m_enable &&
207  this->m_applicableParticles.find(&particle) != this->m_applicableParticles.end();
208 }
209 
211 bool Geant4FastSimShowerModel::check_trigger(const G4FastTrack& track) {
212  auto* prim = track.GetPrimaryTrack();
213  auto* def = prim->GetParticleDefinition();
214  auto iter = this->m_eTriggerCut.find(def);
215  if ( iter != this->m_eTriggerCut.end() ) {
216  return (*iter).second < prim->GetKineticEnergy();
217  }
218  iter = this->m_eTriggerCut.find(nullptr);
219  if ( iter != this->m_eTriggerCut.end() ) {
220  return (*iter).second < prim->GetKineticEnergy();
221  }
222  return false;
223 }
224 
226 void Geant4FastSimShowerModel::modelShower(const G4FastTrack& /* track */, G4FastStep& /* step */) {
227  except("Method %s::modelShower(const G4FastTrack& track, G4FastStep& step) "
228  "is not implemented. User implementation mandatory.", this->name().c_str());
229 }
dd4hep::sim::Geant4FastSimShowerModel::modelShower
virtual void modelShower(const G4FastTrack &track, G4FastStep &step)
User callback to model the particle/energy shower.
Definition: Geant4FastSimShowerModel.cpp:226
dd4hep::sim::Geant4FastSimShowerModel::m_eMin
ParticleConfig m_eMin
Property: Set minimum kinetic energy to trigger parametrisation.
Definition: Geant4FastSimShowerModel.h:66
dd4hep::sim::Geant4FastSimShowerModel::m_applicablePartNames
std::vector< std::string > m_applicablePartNames
Property: Particle names for which this parametrization is applicable.
Definition: Geant4FastSimShowerModel.h:60
dd4hep::sim::Geant4FastSimShowerModel::m_eTriggerNames
ParticleConfig m_eTriggerNames
Property: Set minimal kinetic energy for particles to trigger the model.
Definition: Geant4FastSimShowerModel.h:72
Geant4Mapping.h
dd4hep::sim::Geant4ShowerModelWrapper::ModelTrigger
virtual G4bool ModelTrigger(const G4FastTrack &track) override
User callback to determine if the shower creation should be triggered.
Definition: Geant4FastSimShowerModel.cpp:80
dd4hep::sim::Geant4FastSimShowerModel::m_eTriggerCut
ParticleCut m_eTriggerCut
Particle cut to trigger model simulation indexed by G4ParticleDefinition.
Definition: Geant4FastSimShowerModel.h:77
dd4hep::sim::Geant4Mapping::data
Geant4GeometryInfo & data() const
Access to the data pointer.
Definition: Geant4Mapping.h:58
dd4hep::sim::Geant4FastSimShowerModel::m_stepX0
double m_stepX0
Property: Defines step length.
Definition: Geant4FastSimShowerModel.h:64
dd4hep::sim::Geant4FastSimShowerModel::m_wrapper
Wrapper m_wrapper
Reference to the shower model.
Definition: Geant4FastSimShowerModel.h:81
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
dd4hep::sim::Geant4Mapping::instance
static Geant4Mapping & instance()
Possibility to define a singleton instance.
Definition: Geant4Mapping.cpp:35
dd4hep::sim::Geant4ShowerModelWrapper::~Geant4ShowerModelWrapper
virtual ~Geant4ShowerModelWrapper()=default
Default destructor.
dd4hep::sim::Geant4FastSimShowerModel::constructSensitives
virtual void constructSensitives(Geant4DetectorConstructionContext *ctxt) override
Sensitive detector construction callback. Called at "ConstructSDandField()".
Definition: Geant4FastSimShowerModel.cpp:184
dd4hep::sim::Geant4Action::info
void info(const char *fmt,...) const
Support of info messages.
Definition: Geant4Action.cpp:215
dd4hep::Material
Handle class describing a material.
Definition: Objects.h:272
dd4hep::sim::Geant4Action::except
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
Definition: Geant4Action.cpp:256
dd4hep::sim::Geant4DetectorConstructionContext
Geant4 detector construction context definition.
Definition: Geant4DetectorConstruction.h:61
dd4hep::sim::Geant4FastSimShowerModel::constructGeo
virtual void constructGeo(Geant4DetectorConstructionContext *ctxt) override
Geometry construction callback. Called at "Construct()".
Definition: Geant4FastSimShowerModel.cpp:171
dd4hep::sim::Geant4FastSimShowerModel::Geant4FastSimShowerModel
Geant4FastSimShowerModel(Geant4Context *context, const std::string &nam)
Standard constructor.
Definition: Geant4FastSimShowerModel.cpp:90
dd4hep::sim::Geant4FastSimShowerModel::m_applicableParticles
std::set< const G4ParticleDefinition * > m_applicableParticles
Particle definitions for which this parametrization is applicable.
Definition: Geant4FastSimShowerModel.h:75
dd4hep::sim::Geant4ShowerModelWrapper::DoIt
virtual void DoIt(const G4FastTrack &track, G4FastStep &step) override
User callback to model the particle/energy shower.
Definition: Geant4FastSimShowerModel.cpp:85
dd4hep::sim::Geant4Action::declareProperty
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:366
dd4hep::sim::Geant4FastSimShowerModel::m_regionName
std::string m_regionName
Property: Region name to which this parametrization should be applied.
Definition: Geant4FastSimShowerModel.h:58
dd4hep::sim::Geant4FastSimShowerModel::check_trigger
virtual bool check_trigger(const G4FastTrack &track)
User callback to determine if the shower creation should be triggered.
Definition: Geant4FastSimShowerModel.cpp:211
dd4hep::sim::Geant4FastSimShowerModel::~Geant4FastSimShowerModel
virtual ~Geant4FastSimShowerModel()
Default destructor.
Definition: Geant4FastSimShowerModel.cpp:105
dd4hep::Region
Handle class describing a region as used in simulation.
Definition: Objects.h:462
dd4hep::sim::Geant4FastSimShowerModel::constructField
virtual void constructField(Geant4DetectorConstructionContext *ctxt) override
Electromagnetic field construction callback. Called at "ConstructSDandField()".
Definition: Geant4FastSimShowerModel.cpp:180
dd4hep::sim::Geant4FastSimShowerModel::check_applicability
virtual bool check_applicability(const G4ParticleDefinition &particle)
User callback to determine if the model is applicable for the particle type.
Definition: Geant4FastSimShowerModel.cpp:204
dd4hep::sim::Geant4Action::name
const std::string & name() const
Access name of the action.
Definition: Geant4Action.h:280
dd4hep::sim::Geant4FastSimShowerModel::m_eMax
ParticleConfig m_eMax
Property: Set maximum kinetic energy to trigger parametrisation.
Definition: Geant4FastSimShowerModel.h:68
dd4hep::sim::Geant4FastSimShowerModel::getParticleDefinition
G4ParticleDefinition * getParticleDefinition(const std::string &name) const
Access particle definition from string.
Definition: Geant4FastSimShowerModel.cpp:111
dd4hep::sim::Geant4ShowerModelWrapper::IsApplicable
virtual G4bool IsApplicable(const G4ParticleDefinition &particle) override
User callback to determine if the model is applicable for the particle type.
Definition: Geant4FastSimShowerModel.cpp:75
dd4hep::sim::Geant4FastSimShowerModel::getRegion
G4Region * getRegion(const std::string &nam) const
Access the region from the detector description by name.
Definition: Geant4FastSimShowerModel.cpp:134
dd4hep::_toDouble
double _toDouble(const std::string &value)
String conversions: string to double value.
Definition: Handle.cpp:116
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::Geant4Context::kernel
Geant4Kernel & kernel() const
Access to the kernel object.
Definition: Geant4Context.h:233
dd4hep::sim::Geant4ShowerModelWrapper::Geant4ShowerModelWrapper
Geant4ShowerModelWrapper(Geant4FastSimShowerModel *model)
Initializing constructor.
Definition: Geant4FastSimShowerModel.cpp:69
dd4hep::sim::Geant4GeometryInfo
Concreate class holding the relation information between geant4 objects and dd4hep objects.
Definition: Geant4GeometryInfo.h:93
dd4hep::sim::Geant4ShowerModelWrapper
Geant4 wrapper for the Geant4 fast simulation shower model.
Definition: Geant4FastSimShowerModel.cpp:48
dd4hep::sim::Geant4FastSimShowerModel::addShowerModel
void addShowerModel(G4Region *region)
Add shower model to region's fast simulation manager.
Definition: Geant4FastSimShowerModel.cpp:152
mapping
ConditionsMap & mapping
Definition: AlignmentsCalculator.cpp:82
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
Geant4FastSimShowerModel.h
dd4hep::sim::Geant4FastSimShowerModel::getMaterial
G4Material * getMaterial(const std::string &name) const
Get parametrization material.
Definition: Geant4FastSimShowerModel.cpp:119
Geant4Kernel.h
dd4hep::sim::Geant4FastSimShowerModel::m_model
G4VFastSimulationModel * m_model
Reference to the G4 fast simulation model.
Definition: Geant4FastSimShowerModel.h:79
dd4hep::sim::Geant4DetectorConstruction
Basic implementation of the Geant4 detector construction action.
Definition: Geant4DetectorConstruction.h:102
dd4hep::sim::Geant4ShowerModelWrapper::m_model
Geant4FastSimShowerModel * m_model
Reference to the model wrapper.
Definition: Geant4FastSimShowerModel.cpp:50
dd4hep::sim::Geant4FastSimShowerModel::killParticle
void killParticle(G4FastStep &step, double deposit, double step_length=0e0)
Kill primary particle when creating the shower.
Definition: Geant4FastSimShowerModel.cpp:197
dd4hep::sim::Geant4FastSimShowerModel::m_eKill
ParticleConfig m_eKill
Property: Set maximum kinetic energy for particles to be killed.
Definition: Geant4FastSimShowerModel.h:70
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270
dd4hep::sim::Geant4FastSimShowerModel
Geant4 wrapper for the Geant4 fast simulation shower model.
Definition: Geant4FastSimShowerModel.h:50
dd4hep::sim::Geant4FastSimShowerModel::m_enable
bool m_enable
Property: Parametrisation control: Enable/disable fast simulation.
Definition: Geant4FastSimShowerModel.h:62