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>;
177 static const SG::AuxElement::ConstAccessor<TruthLink_t> linkToTruth(
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
221
223 descendants(particle, particleMask, encounteredUniqueIDs);
224 encounteredUniqueIDs.clear();
225 }
226
227
235 }
236 int nChildren = 0;
237 if (decayVtx)
238 nChildren = decayVtx->nOutgoingParticles();
239 for (
int i = 0;
i < nChildren; ++
i) {
240 particleMask[decayVtx->outgoingParticle(i)->index()] = true;
241 }
242 }
243 }
244
245
246
247 if (std::find(recoParticleTruthIndices.begin(),
248 recoParticleTruthIndices.end(),
249 i) != recoParticleTruthIndices.end()) {
251
252 ancestors(particle, particleMask, encounteredUniqueIDs);
253 encounteredUniqueIDs.clear();
254 descendants(particle, particleMask, encounteredUniqueIDs);
255 encounteredUniqueIDs.clear();
256 }
257 }
258
259
260
261 if (std::find(egammaTruthIndices.begin(), egammaTruthIndices.end(), i) !=
262 egammaTruthIndices.end()) {
263 descendants(particle, particleMask, encounteredUniqueIDs);
264 encounteredUniqueIDs.clear();
265 }
266
268 particleMask[
i] =
true;
269 }
270 else {
271
272
278
279
280
281 particleMask[
i] =
true;
282 break;
283 }
284 }
285 }
286 }
287 }
288
289
290 for (
int i = 0;
i < nTruthParticles; ++
i) {
291 if (!particleMask[i]) {
295 const auto *prodVertex =
particle->prodVtx();
296 --vertexLinksCounts[prodVertex->index()].second;
297 }
299 const auto *decayVertex =
particle->decayVtx();
300 --vertexLinksCounts[decayVertex->index()].first;
301 }
302 }
303 }
304
305
306
307
308 unsigned int nVerticesThinned = 0;
309 for (
int i = 0;
i < nTruthVertices; ++
i) {
310 if (vertexLinksCounts[i].first != 0 || vertexLinksCounts[i].second != 0) {
311 vertexMask[
i] =
true;
312 } else {
313 ++nVerticesThinned;
314 }
315 }
317
318 truthParticles.
keep(particleMask);
319 truthVertices.keep(vertexMask);
320
321 return StatusCode::SUCCESS;
322}
#define ATH_CHECK
Evaluate an expression and check for errors.
ElementLink< xAOD::TruthParticleContainer > TruthLink_t
bool isValid() const
Check if the element can be found.
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".