86{
87
89
90
93 if (!truthParticles.isValid()) {
95 return StatusCode::FAILURE;
96 }
97 if (!truthVertices.isValid()) {
99 return StatusCode::FAILURE;
100 }
101
102
103
104 std::vector<int> recoParticleTruthIndices;
105 std::vector<int> egammaTruthIndices{};
106
107
113 if (truthMuon) {
115 if (truthMuon) {
116 recoParticleTruthIndices.push_back(
truthMuon->index());
117 }
118 }
119 }
120 }
121
122
124
125
128
132 if (truthElectron) {
133 recoParticleTruthIndices.push_back(truthElectron->
index());
134 }
135 }
136
137
140 if (fwdElectrons) {
141
145 if (truthElectron) {
146 recoParticleTruthIndices.push_back(truthElectron->
index());
147 }
148 }
149 }
150
151
157 if (truthPhoton) {
158 recoParticleTruthIndices.push_back(truthPhoton->
index());
159 }
160 }
161
162
165
167
168 static const SG::AuxElement::ConstAccessor<int> accType("truthType");
169
170 if (!accType.isAvailable(*egTruthParticle) ||
173 continue;
174 }
175
176 using TruthLink_t = ElementLink<xAOD::TruthParticleContainer>;
178 "truthParticleLink");
179 if (!linkToTruth.isAvailable(*egTruthParticle)) {
180 continue;
181 }
182
183 const TruthLink_t& truthegamma = linkToTruth(*egTruthParticle);
185 continue;
186 }
187 egammaTruthIndices.push_back((*truthegamma)->index());
188 }
189 }
190
191
192 std::vector<bool> particleMask, vertexMask;
193 int nTruthParticles = truthParticles->size();
194 int nTruthVertices = truthVertices->size();
197 particleMask.assign(nTruthParticles, false);
198 vertexMask.assign(nTruthVertices, false);
199
200
201
202 std::vector<std::pair<int, int>> vertexLinksCounts;
203 for (const auto *vertex : *truthVertices) {
204 std::pair<int, int> tmpPair;
205 tmpPair.first =
vertex->nIncomingParticles();
206 tmpPair.second =
vertex->nOutgoingParticles();
207 vertexLinksCounts.push_back(tmpPair);
208 }
209
210
211 std::unordered_set<int> encounteredUniqueIDs;
212 for (
int i = 0;
i < nTruthParticles; ++
i) {
213 encounteredUniqueIDs.clear();
215
217 descendants(particle, particleMask, encounteredUniqueIDs);
218 encounteredUniqueIDs.clear();
219 }
220
228 }
229 int nChildren = 0;
230 if (decayVtx)
231 nChildren = decayVtx->nOutgoingParticles();
232 for (
int i = 0;
i < nChildren; ++
i) {
233 particleMask[decayVtx->outgoingParticle(i)->index()] = true;
234 }
235 }
236 }
237
238
239
240 if (std::find(recoParticleTruthIndices.begin(),
241 recoParticleTruthIndices.end(),
242 i) != recoParticleTruthIndices.end()) {
244
245 ancestors(particle, particleMask, encounteredUniqueIDs);
246 encounteredUniqueIDs.clear();
247 descendants(particle, particleMask, encounteredUniqueIDs);
248 encounteredUniqueIDs.clear();
249 }
250 }
251
252
253
254 if (std::find(egammaTruthIndices.begin(), egammaTruthIndices.end(), i) !=
255 egammaTruthIndices.end()) {
256 descendants(particle, particleMask, encounteredUniqueIDs);
257 encounteredUniqueIDs.clear();
258 }
259
261 particleMask[
i] =
true;
262 }
263 else {
264
265
271
272
273
274 particleMask[
i] =
true;
275 break;
276 }
277 }
278 }
279 }
280 }
281
282
283 for (
int i = 0;
i < nTruthParticles; ++
i) {
284 if (!particleMask[i]) {
288 const auto *prodVertex =
particle->prodVtx();
289 --vertexLinksCounts[prodVertex->index()].second;
290 }
292 const auto *decayVertex =
particle->decayVtx();
293 --vertexLinksCounts[decayVertex->index()].first;
294 }
295 }
296 }
297
298
299
300
301 unsigned int nVerticesThinned = 0;
302 for (
int i = 0;
i < nTruthVertices; ++
i) {
303 if (vertexLinksCounts[i].first != 0 || vertexLinksCounts[i].second != 0) {
304 vertexMask[
i] =
true;
305 } else {
306 ++nVerticesThinned;
307 }
308 }
310
311 truthParticles.
keep(particleMask);
312 truthVertices.keep(vertexMask);
313
314 return StatusCode::SUCCESS;
315}
#define ATH_CHECK
Evaluate an expression and check for errors.
ElementLink< xAOD::TruthParticleContainer > TruthLink_t
bool isValid() const
Test to see if the link can be dereferenced.
SG::ConstAccessor< T, ALLOC > ConstAccessor
void keep(size_t ndx)
Mark that index ndx in the container should be kept (not thinned away).
SG::ReadHandleKey< xAOD::MuonContainer > m_muonsKey
std::atomic< unsigned long > m_nParticlesThinned
std::atomic< unsigned long > m_nVerticesProcessed
std::atomic< unsigned long > m_nVerticesThinned
SG::ReadHandleKey< xAOD::PhotonContainer > m_photonsKey
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_egammaTruthKey
SG::ThinningHandleKey< xAOD::TruthVertexContainer > m_truthVerticesKey
Gaudi::Property< std::vector< int > > m_longlived
SG::ReadHandleKey< xAOD::ElectronContainer > m_electronsKey
std::atomic< unsigned long > m_nEventsProcessed
Counters.
Gaudi::Property< bool > m_keepMuons
Gaudi::Property< bool > m_keepEGamma
std::atomic< unsigned long > m_nParticlesProcessed
SG::ThinningHandleKey< xAOD::TruthParticleContainer > m_truthParticlesKey
Gaudi::Property< float > m_etaMaxEgTruth
SG::ReadHandleKey< xAOD::ElectronContainer > m_fwdElectronsKey
const TruthParticle_v1 * incomingParticle(size_t index) const
Get one of the incoming particles.
size_t nIncomingParticles() const
Get the number of incoming particles.
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (TODO update to be s...
bool isStable(const T &p)
Identify if the particle is stable, i.e. has not decayed.
bool isDecayed(const T &p)
Identify if the particle decayed.
bool isBSM(const T &p)
APID: graviton and all Higgs extensions are BSM.
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
const xAOD::TruthParticle * getTruthParticle(const xAOD::IParticle &p)
Return the truthParticle associated to the given IParticle (if any)
PhotonContainer_v1 PhotonContainer
Definition of the current "photon container version".
ElectronContainer_v1 ElectronContainer
Definition of the current "electron container version".
TruthParticle_v1 TruthParticle
Typedef to implementation.
Muon_v1 Muon
Reference the current persistent version:
Photon_v1 Photon
Definition of the current "egamma version".
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.
Electron_v1 Electron
Definition of the current "egamma version".