ATLAS Offline Software
Loading...
Searching...
No Matches
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
24
25// StoreGate
27
29
31#include "CaloDetDescr/CaloDetDescrElement.h"
34
36
37#include "CLHEP/Units/SystemOfUnits.h"
38
39#include "TFile.h"
40#include <fstream>
41
42using std::abs;
43using std::atan2;
44
46ISF::FastCaloSimV2Tool::FastCaloSimV2Tool( const std::string& type, const std::string& name, const IInterface* parent)
47 : BaseSimulatorTool(type, name, parent)
48{
49}
50
53{
55
57 ATH_MSG_DEBUG( "Successfully retrieve CaloCellMakerTools: " << m_caloCellMakerToolsSetup );
59
60 ATH_CHECK(m_rndmGenSvc.retrieve());
61
62 ATH_CHECK(m_paramSvc.retrieve());
63
64 // m_paramSvc->setLevel(MSG::VERBOSE);
65
67 if (m_doPunchThrough) {
68 ATH_CHECK(m_punchThroughTool.retrieve());
69 }
70
71 ATH_CHECK(m_truthRecordSvc.retrieve());
72
73 // Get FastCaloSimCaloExtrapolation
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);
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
94StatusCode ISF::FastCaloSimV2Tool::setupEvent(const EventContext& ctx)
95{
96 ATH_MSG_DEBUG ("setupEvent");
97
98 m_theContainer = std::make_unique<CaloCellContainer>(SG::VIEW_ELEMENTS);
100 return this->commonSetup(ctx);
101}
102
103StatusCode ISF::FastCaloSimV2Tool::commonSetup(const EventContext& ctx)
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
130StatusCode ISF::FastCaloSimV2Tool::releaseEvent(const EventContext& ctx)
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
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
164StatusCode ISF::FastCaloSimV2Tool::simulate(const EventContext& ctx, ISF::ISFParticle& isfp, ISFParticleContainer& secondaries, McEventCollection*)
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
193 TFCSExtrapolationState extrapol;
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}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
This class provides an interface to generate or decode an identifier for the upper levels of the dete...
Definition of CaloDetDescrManager.
static Double_t sc
A wrapper class for event-slot-local random engines.
Definition RNGWrapper.h:56
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition RNGWrapper.h:169
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
Definition RNGWrapper.h:134
Data object for each calorimeter readout cell.
Definition CaloCell.h:57
void addEnergy(float energy)
add energy
Definition CaloCell.h:449
virtual StatusCode initialize() override
ServiceHandle< IChronoStatSvc > m_chrono
The timing service for general usage.
BaseSimulatorTool(const std::string &type, const std::string &name, const IInterface *parent)
Gaudi::Property< std::string > m_caloCellsOutputName
virtual StatusCode releaseEvent(const EventContext &) override final
Release Event chain - in case of an end-of event action is needed.
CaloCellContainer * m_theContainerPtr
virtual StatusCode setupEventST() override final
Setup Event chain - in case of a begin-of event action is needed (called by ISimulationSvc)
virtual StatusCode simulate(const EventContext &ctx, ISFParticle &isp, ISFParticleContainer &, McEventCollection *mcEventCollection) override final
Simulation Call.
FastCaloSimV2Tool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters.
virtual StatusCode releaseEventST() override final
Release Event chain - in case of an end-of event action is needed (called by ISimulationSvc)
Gaudi::Property< std::string > m_randomEngineName
StatusCode commonSetup(const EventContext &ctx)
PublicToolHandleArray< ICaloCellMakerTool > m_caloCellMakerToolsSetup
SG::WriteHandleKey< CaloCellContainer > m_caloCellKey
virtual StatusCode initialize() override final
Athena algorithm's interface methods.
virtual StatusCode setupEvent(const EventContext &) override final
Setup Event chain - in case of a begin-of event action is needed.
PublicToolHandle< IFastCaloSimCaloExtrapolation > m_FastCaloSimCaloExtrapolation
ServiceHandle< IAthRNGSvc > m_rndmGenSvc
ToolHandle< IPunchThroughTool > m_punchThroughTool
ServiceHandle< IFastCaloSimParamSvc > m_paramSvc
ServiceHandle< ISF::ITruthSvc > m_truthRecordSvc
std::unique_ptr< CaloCellContainer > m_theContainer
PublicToolHandleArray< ICaloCellMakerTool > m_caloCellMakerToolsRelease
The generic ISF particle definition,.
Definition ISFParticle.h:42
double ekin() const
Kinetic energy.
const Amg::Vector3D & momentum() const
The current momentum vector of the ISFParticle.
const Amg::Vector3D & position() const
The current position of the ISFParticle.
int pdgCode() const
PDG value.
double mass() const
mass of the particle
AtlasDetDescr::AtlasRegion nextGeoID() const
next geoID the particle will be simulated in
Interface class for all truth incidents handled by the ISF.
void updateChildParticleProperties()
Update the id and particleLink properties of the child particles (to be called after registerTruthInc...
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
void setAuxInfo(std::uint32_t index, const T &val)
void set_pdgid(int val)
void set_vertex(const TLorentzVector &val)
void set_Ekin_off(double val)
const std::string process
Eigen::Matrix< double, 3, 1 > Vector3D
@ fKillsPrimary
std::list< ISF::ISFParticle * > ISFParticleContainer
generic ISFParticle container (not necessarily a std::list!)
std::vector< ISF::ISFParticle * > ISFParticleVector
ISFParticle vector.
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts