ATLAS Offline Software
FastCaloSimV2Tool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // class header include
6 #include "FastCaloSimV2Tool.h"
7 
8 
9 // FastCaloSim includes
15 
17 
22 #include "CaloIdentifier/TileID.h"
24 
25 // StoreGate
26 #include "StoreGate/StoreGateSvc.h"
27 
29 
31 #include "CaloDetDescr/CaloDetDescrElement.h"
34 
36 
37 #include "CLHEP/Units/SystemOfUnits.h"
38 
39 #include "TFile.h"
40 #include <fstream>
41 
42 using std::abs;
43 using std::atan2;
44 
46 ISF::FastCaloSimV2Tool::FastCaloSimV2Tool( const std::string& type, const std::string& name, const IInterface* parent)
48 {
49 }
50 
53 {
55 
56  ATH_CHECK( m_caloCellMakerToolsSetup.retrieve() );
57  ATH_MSG_DEBUG( "Successfully retrieve CaloCellMakerTools: " << m_caloCellMakerToolsSetup );
58  ATH_CHECK( m_caloCellMakerToolsRelease.retrieve() );
59 
60  ATH_CHECK(m_rndmGenSvc.retrieve());
61 
62  ATH_CHECK(m_paramSvc.retrieve());
63 
64  // m_paramSvc->setLevel(MSG::VERBOSE);
65 
66  m_doPunchThrough = not m_punchThroughTool.empty();
67  if (m_doPunchThrough) {
68  ATH_CHECK(m_punchThroughTool.retrieve());
69  }
70 
71  ATH_CHECK(m_truthRecordSvc.retrieve());
72 
73  // Get FastCaloSimCaloExtrapolation
74  ATH_CHECK(m_FastCaloSimCaloExtrapolation.retrieve());
75 
76  // Output data handle
77  ATH_CHECK( m_caloCellKey.initialize() );
78 
79  return StatusCode::SUCCESS;
80 }
81 
83 {
84  ATH_MSG_DEBUG ("setupEventST");
85 
86  m_theContainer = std::make_unique<CaloCellContainer>(SG::VIEW_ELEMENTS);
87  m_theContainerPtr = m_theContainer.get();
88  ATH_CHECK(evtStore()->record(std::move(m_theContainer), m_caloCellsOutputName));
89  // NB: m_theContainer is now nullptr
90  const EventContext& ctx = Gaudi::Hive::currentContext();
91  return this->commonSetup(ctx);
92 }
93 
95 {
96  ATH_MSG_DEBUG ("setupEvent");
97 
98  m_theContainer = std::make_unique<CaloCellContainer>(SG::VIEW_ELEMENTS);
99  m_theContainerPtr = m_theContainer.get();
100  return this->commonSetup(ctx);
101 }
102 
104 {
105  // Set the RNGs to use for this event. We need to reset it for MT jobs
106  // because of the mismatch between Gaudi slot-local and G4 thread-local RNG.
107  ATHRNG::RNGWrapper* rngWrapper = m_rndmGenSvc->getEngine(this, m_randomEngineName);
108  rngWrapper->setSeed( m_randomEngineName, ctx );
109 
110  for (const ToolHandle<ICaloCellMakerTool>& tool : m_caloCellMakerToolsSetup)
111  {
112  std::string chronoName=this->name()+"_"+ tool.name();
113  if (m_chrono) m_chrono->chronoStart(chronoName);
114  StatusCode sc = tool->process(m_theContainerPtr, ctx);
115  if (m_chrono) {
116  m_chrono->chronoStop(chronoName);
117  ATH_MSG_DEBUG( "Chrono stop : delta " << m_chrono->chronoDelta (chronoName,IChronoStatSvc::USER) * CLHEP::microsecond / CLHEP::second << " second " );
118  }
119 
120  if (sc.isFailure())
121  {
122  ATH_MSG_ERROR( "Error executing tool " << tool.name() );
123  return StatusCode::FAILURE;
124  }
125  }
126 
127  return StatusCode::SUCCESS;
128 }
129 
131 {
132  ATH_MSG_VERBOSE( "FastCaloSimV2Tool " << name() << " releaseEvent() " );
133  // Run the version of releaseEvent that returns the output collection
134  // Run the normal method
135  ATH_CHECK(this->releaseEventST());
136  if ( m_theContainer ) {
137 
138  // Record with WriteHandle
139  SG::WriteHandle< CaloCellContainer > caloCellHandle( m_caloCellKey, ctx );
140  ATH_CHECK( caloCellHandle.record( std::move( m_theContainer ) ) );
141  return StatusCode::SUCCESS;
142  }
143  return StatusCode::FAILURE;
144 }
145 
147 {
148  ATH_MSG_VERBOSE("release Event");
149  const EventContext& ctx = Gaudi::Hive::currentContext();
150 
151  //run release tools in a loop
152  for (const ToolHandle<ICaloCellMakerTool>& tool : m_caloCellMakerToolsRelease)
153  {
154  ATH_MSG_VERBOSE( "Calling tool " << tool.name() );
155 
156  ATH_CHECK(tool->process(m_theContainerPtr, ctx));
157  }
158 
159  return StatusCode::SUCCESS;
160 
161 }
162 
165 {
166 
167  ATH_MSG_VERBOSE("NEW PARTICLE! FastCaloSimV2Tool called with ISFParticle: " << isfp);
168 
169  Amg::Vector3D particle_position = isfp.position();
170  Amg::Vector3D particle_direction(isfp.momentum().x(),isfp.momentum().y(),isfp.momentum().z());
171 
172  ATHRNG::RNGWrapper* rngWrapper = m_rndmGenSvc->getEngine(this, m_randomEngineName);
173 
174  //Don't simulate particles with total energy below 10 MeV
175  if(isfp.ekin() < 10) {
176  ATH_MSG_VERBOSE("Skipping particle with Ekin: " << isfp.ekin() <<" MeV. Below the 10 MeV threshold.");
177  return StatusCode::SUCCESS;
178  }
179 
180  TFCSTruthState truth;
181  truth.SetPtEtaPhiM(particle_direction.perp(), particle_direction.eta(), particle_direction.phi(), isfp.mass());
182  truth.set_pdgid(isfp.pdgCode());
183  truth.set_vertex(particle_position[Amg::x], particle_position[Amg::y], particle_position[Amg::z]);
184 
188  if(isfp.pdgCode() == -2212 || isfp.pdgCode() == -2112) {
189  truth.set_Ekin_off(2*isfp.mass());
190  ATH_MSG_VERBOSE("Found anti-proton/neutron, setting Ekin offset in TFCSTruthState.");
191  }
192 
194  m_FastCaloSimCaloExtrapolation->extrapolate(extrapol, &truth);
195 
196  ATH_MSG_DEBUG(" particle: " << isfp.pdgCode() << " Ekin: " << isfp.ekin() << " position eta: " << particle_position.eta() << " direction eta: " << particle_direction.eta() << " position phi: " << particle_position.phi() << " direction phi: " << particle_direction.phi());
197 
198  //only simulate if extrapolation to calo surface succeeded
199  if(extrapol.IDCaloBoundary_eta() != -999){
200  CLHEP::HepRandomEngine *rndmEngine = rngWrapper->getEngine(ctx);
201  TFCSSimulationState simulstate(rndmEngine);
202 
203  // ATH_MSG_WARNING("Event number for this event: " << ctx.evt());
204  simulstate.setAuxInfo<int>("EventNr"_FCShash, ctx.evt() );
205 
206  ATH_CHECK(m_paramSvc->simulate(simulstate, &truth, &extrapol));
207 
208  ATH_MSG_DEBUG("Energy returned: " << simulstate.E());
209  ATH_MSG_VERBOSE("Energy fraction for layer: ");
210  for (int s = 0; s < CaloCell_ID_FCS::MaxSample; s++)
211  ATH_MSG_VERBOSE(" Sampling " << s << " energy " << simulstate.E(s));
212 
213  //Now deposit all cell energies into the CaloCellContainer
214  for(const auto& iter : simulstate.cells()) {
215  CaloCell* theCell = (CaloCell*)m_theContainerPtr->findCell(iter.first->calo_hash());
216  theCell->addEnergy(iter.second);
217  }
218 
219  //now perform punch through
220  if (m_doPunchThrough) {
221  int process = 201;
222  // call punch-through simulation
223  const ISF::ISFParticleVector *someSecondaries = m_punchThroughTool->computePunchThroughParticles(isfp, simulstate, rndmEngine);
224 
225  if (someSecondaries && !someSecondaries->empty()) {
226  //Record truth incident for created punch through particles
227  ISF::ISFTruthIncident truth( const_cast<ISF::ISFParticle&>(isfp),
228  *someSecondaries,
229  process,
230  isfp.nextGeoID(), // inherits from the parent
232 
233  m_truthRecordSvc->registerTruthIncident( truth, true );
234  // At this point we need to update the properties of the
235  // ISFParticles produced in the interaction
237 
238  for (auto *secondary : *someSecondaries) {
239  if (secondary->getTruthBinding()) {
240  secondaries.push_back(secondary);
241  }
242  else {
243  ATH_MSG_WARNING("Secondary particle created by PunchThroughTool not written out to truth.\n Parent (" << isfp << ")\n Secondary (" << *secondary <<")");
244  delete secondary;
245  }
246  }
247  delete someSecondaries;
248  }
249  }
250  simulstate.DoAuxInfoCleanup();
251  }
252  else ATH_MSG_DEBUG("Skipping simulation as extrapolation to ID-Calo boundary failed.");
253 
254 
255  return StatusCode::SUCCESS;
256 
257 }
ISF::ISFParticleContainer
std::list< ISF::ISFParticle * > ISFParticleContainer
generic ISFParticle container (not necessarily a std::list!)
Definition: ISFParticleContainer.h:23
ISF::ISFParticle::ekin
double ekin() const
Kinetic energy.
ATHRNG::RNGWrapper::setSeed
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition: RNGWrapper.h:169
ISF::ISFTruthIncident::updateChildParticleProperties
void updateChildParticleProperties()
Update the id and particleLink properties of the child particles (to be called after registerTruthInc...
Definition: ISFTruthIncident.cxx:239
createLinkingScheme.iter
iter
Definition: createLinkingScheme.py:62
LArEM_ID.h
TFCSSimulationState::DoAuxInfoCleanup
void DoAuxInfoCleanup()
Definition: TFCSSimulationState.cxx:79
ISF::FastCaloSimV2Tool::setupEventST
virtual StatusCode setupEventST() override final
Setup Event chain - in case of a begin-of event action is needed (called by ISimulationSvc)
Definition: FastCaloSimV2Tool.cxx:82
TFCSTruthState::set_Ekin_off
void set_Ekin_off(double val)
Definition: TFCSTruthState.h:23
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
TFCSSimulationState::cells
Cellmap_t & cells()
Definition: TFCSSimulationState.h:72
ISF::FastCaloSimV2Tool::commonSetup
StatusCode commonSetup(const EventContext &ctx)
Definition: FastCaloSimV2Tool.cxx:103
FCALDetectorManager.h
Amg::y
@ y
Definition: GeoPrimitives.h:35
python.SystemOfUnits.second
float second
Definition: SystemOfUnits.py:135
ISF::ISFParticle
Definition: ISFParticle.h:42
ISF::FastCaloSimV2Tool::releaseEventST
virtual StatusCode releaseEventST() override final
Release Event chain - in case of an end-of event action is needed (called by ISimulationSvc)
Definition: FastCaloSimV2Tool.cxx:146
ISFTruthIncident.h
ISF::ISFParticle::pdgCode
int pdgCode() const
PDG value.
TFCSExtrapolationState
Definition: TFCSExtrapolationState.h:13
RunActsMaterialValidation.extrapol
extrapol
Definition: RunActsMaterialValidation.py:91
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SUSY_SimplifiedModel_PostInclude.process
string process
Definition: SUSY_SimplifiedModel_PostInclude.py:43
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
CaloDetDescrManager.h
Definition of CaloDetDescrManager.
TileID.h
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
ISF::ISFParticle::position
const Amg::Vector3D & position() const
The current position of the ISFParticle.
TFCSParametrizationBase.h
ISF::FastCaloSimV2Tool::simulate
virtual StatusCode simulate(const EventContext &ctx, ISFParticle &isp, ISFParticleContainer &, McEventCollection *mcEventCollection) override final
Simulation Call.
Definition: FastCaloSimV2Tool.cxx:164
AtlasDetectorID.h
This class provides an interface to generate or decode an identifier for the upper levels of the dete...
CaloGeometryFromCaloDDM.h
Amg::z
@ z
Definition: GeoPrimitives.h:36
CaloCell_ID_FCS::MaxSample
@ MaxSample
Definition: FastCaloSim_CaloCell_ID.h:47
IdDictParser.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ISF::ISFParticle::nextGeoID
AtlasDetDescr::AtlasRegion nextGeoID() const
next geoID the particle will be simulated in
ISF::ISFTruthIncident
Definition: ISFTruthIncident.h:35
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
Amg::x
@ x
Definition: GeoPrimitives.h:34
ISF::ISFParticleVector
std::vector< ISF::ISFParticle * > ISFParticleVector
ISFParticle vector.
Definition: ISFParticleContainer.h:26
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:32
ISF::FastCaloSimV2Tool::releaseEvent
virtual StatusCode releaseEvent(const EventContext &) override final
Release Event chain - in case of an end-of event action is needed.
Definition: FastCaloSimV2Tool.cxx:130
FastCaloSimV2Tool.h
PathResolver.h
ISF::BaseSimulatorTool
Definition: BaseSimulatorTool.h:36
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
ATHRNG::RNGWrapper
A wrapper class for event-slot-local random engines.
Definition: RNGWrapper.h:56
ISF::ISFParticle::momentum
const Amg::Vector3D & momentum() const
The current momentum vector of the ISFParticle.
TFCSSimulationState::setAuxInfo
void setAuxInfo(std::uint32_t index, const T &val)
Definition: TFCSSimulationState.h:168
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:452
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
CaloCellContainer.h
ATHRNG::RNGWrapper::getEngine
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
Definition: RNGWrapper.h:134
RNGWrapper.h
ISF::BaseSimulatorTool::initialize
virtual StatusCode initialize() override
Definition: BaseSimulatorTool.h:57
SG::WriteHandle< CaloCellContainer >
ISF::FastCaloSimV2Tool::setupEvent
virtual StatusCode setupEvent(const EventContext &) override final
Setup Event chain - in case of a begin-of event action is needed.
Definition: FastCaloSimV2Tool.cxx:94
TFCSTruthState.h
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
CaloCell
Data object for each calorimeter readout cell.
Definition: CaloCell.h:57
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TFCSExtrapolationState.h
TFCSTruthState::set_vertex
void set_vertex(const TLorentzVector &val)
Definition: TFCSTruthState.h:19
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
TFCSSimulationState::E
double E() const
Definition: TFCSSimulationState.h:42
ISF::FastCaloSimV2Tool::FastCaloSimV2Tool
FastCaloSimV2Tool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters.
Definition: FastCaloSimV2Tool.cxx:46
TFCSSimulationState.h
CaloCell::addEnergy
void addEnergy(float energy)
add energy
Definition: CaloCell.h:449
ISF::FastCaloSimV2Tool::initialize
virtual StatusCode initialize() override final
Athena algorithm's interface methods.
Definition: FastCaloSimV2Tool.cxx:52
python.SystemOfUnits.microsecond
float microsecond
Definition: SystemOfUnits.py:137
TFCSTruthState
Definition: TFCSTruthState.h:13
StoreGateSvc.h
TFCSSimulationState
Definition: TFCSSimulationState.h:32
TFCSTruthState::set_pdgid
void set_pdgid(int val)
Definition: TFCSTruthState.h:18
ISF::fKillsPrimary
@ fKillsPrimary
Definition: ISFTruthIncident.h:25
ISF::ISFParticle::mass
double mass() const
mass of the particle