ATLAS Offline Software
Loading...
Searching...
No Matches
MuonSimHitSortingAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
5
11#include <GaudiKernel/SystemOfUnits.h>
12namespace {
13 constexpr double tolerance = 1. * Gaudi::Units::micrometer;
14}
15
17 if (m_readKeys.empty()) {
18 ATH_MSG_FATAL("Please provide at least one container to sort");
19 return StatusCode::FAILURE;
20 }
21 ATH_CHECK(m_readKeys.initialize());
22 ATH_CHECK(m_writeKey.initialize());
23 ATH_CHECK(m_idHelperSvc.retrieve());
24 return StatusCode::SUCCESS;
25}
26StatusCode MuonSimHitSortingAlg::execute(const EventContext& ctx) const {
29 const xAOD::MuonSimHitContainer* hits{nullptr};
30 ATH_CHECK(SG::get(hits, inKey, ctx));
31
32 std::ranges::copy(*hits, std::back_inserter(allSimHits));
33 }
34 std::stable_sort(allSimHits.begin(), allSimHits.end(),
35 [this](const xAOD::MuonSimHit* a, const xAOD::MuonSimHit* b){
37 const IdentifierHash hashA = m_idHelperSvc->detElementHash(a->identify());
38 const IdentifierHash hashB = m_idHelperSvc->detElementHash(a->identify());
39 if (hashA != hashB) {
40 return hashA < hashB;
41 }
43 if (a->identify() != b->identify()) {
44 return a->identify() < b->identify();
45 }
47 const float dT = a->globalTime() - b->globalTime();
48 if (std::abs(dT) > 0.1 * Gaudi::Units::picosecond) {
49 return dT < 0.;
50 }
52 if (std::abs(a->pdgId()) != std::abs(b->pdgId())){
53 return a->pdgId() > b->pdgId();
54 }
57 return a->genParticleLink().barcode() < b->genParticleLink().barcode();
58 });
59 if (m_removeDuplicates) {
60 std::vector<const xAOD::MuonSimHit*> dupFreeHits{};
61 dupFreeHits.reserve(allSimHits.size());
62 std::ranges::copy_if(allSimHits, std::back_inserter(dupFreeHits),
63 [&dupFreeHits, this] (const xAOD::MuonSimHit* hit) {
64 const int barcode = hit->genParticleLink().barcode();
65 const Identifier hitId = hit->identify();
66 const Amg::Vector3D lPos{xAOD::toEigen(hit->localPosition())};
67 const Amg::Vector3D lDir{xAOD::toEigen(hit->localDirection())};
68 ATH_MSG_VERBOSE("Check sim hit "<<m_idHelperSvc->toString(hitId)<<", pdgId:"<<hit->pdgId()
69 <<", barcode: "<<barcode
70 <<" at "<<Amg::toString(lPos, 2)<<"direction: "<<Amg::toString(lDir, 2));
71 return std::ranges::find_if(dupFreeHits,
72 [&](const xAOD::MuonSimHit* selHit) {
73 if (selHit->identify() != hitId ||
74 barcode != selHit->genParticleLink().barcode()) return false;
75 if (barcode) return true;
76 const Amg::Vector3D dPos = lPos - xAOD::toEigen(selHit->localPosition());
77 const Amg::Vector3D dDir = lDir - xAOD::toEigen(selHit->localDirection());
78 return dPos.mag() < tolerance && dDir.mag() < tolerance;
79 }) == dupFreeHits.end();
80 });
81 allSimHits.clear();
82 std::ranges::copy(dupFreeHits, std::back_inserter(allSimHits));
83 }
84 SG::WriteHandle writeHandle{m_writeKey, ctx};
85 if (m_writeDeepCopy) {
86 ATH_CHECK(writeHandle.record(std::make_unique<xAOD::MuonSimHitContainer>(),
87 std::make_unique<xAOD::MuonSimHitAuxContainer>()));
88 for (const xAOD::MuonSimHit* copy_me : allSimHits) {
89 xAOD::MuonSimHit* newHit = writeHandle->push_back(std::make_unique<xAOD::MuonSimHit>());
90 (*newHit) = (*copy_me);
91 }
92 } else {
93 ATH_CHECK(writeHandle.record(std::make_unique<xAOD::MuonSimHitContainer>(*allSimHits.asDataVector())));
94 }
95 return StatusCode::SUCCESS;
96}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_VERBOSE(x)
DataVector adapter that acts like it holds const pointers.
static Double_t a
if(febId1==febId2)
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
DataVector adapter that acts like it holds const pointers.
iterator begin() noexcept
Return an iterator pointing at the beginning of the collection.
iterator end() noexcept
Return an iterator pointing past the end of the collection.
SG::ReadHandleKeyArray< xAOD::MuonSimHitContainer > m_readKeys
StatusCode execute(const EventContext &ctx) const override
SG::WriteHandleKey< xAOD::MuonSimHitContainer > m_writeKey
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
StatusCode initialize() override
Property holding a SG store/key/clid from which a ReadHandle is made.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ConstVectorMap< 3 > localDirection() const
Returns the local direction of the traversing particle.
int pdgId() const
Returns the pdgID of the traversing particle.
Identifier identify() const
Returns the global ATLAS identifier of the SimHit.
ConstVectorMap< 3 > localPosition() const
Returns the local postion of the traversing particle.
const HepMcParticleLink & genParticleLink() const
Returns the link to the HepMC particle producing this hit.
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Eigen::Matrix< double, 3, 1 > Vector3D
int barcode(const T *p)
Definition Barcode.h:16
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
void stable_sort(DataModel_detail::iterator< DVL > beg, DataModel_detail::iterator< DVL > end)
Specialization of stable_sort for DataVector/List.
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12
MuonSimHitContainer_v1 MuonSimHitContainer
Define the version of the pixel cluster container.