ATLAS Offline Software
MuonSimHitSortingAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 #include "MuonSimHitSortingAlg.h"
5 
6 
8 #include <StoreGate/ReadHandle.h>
13 #include <GaudiKernel/SystemOfUnits.h>
14 namespace {
15  constexpr double tolerance = 10. * Gaudi::Units::micrometer;
16 }
17 
18 MuonSimHitSortingAlg::MuonSimHitSortingAlg(const std::string& name, ISvcLocator* pSvcLocator):
19  AthReentrantAlgorithm{name, pSvcLocator} {}
20 
22  if (m_readKeys.empty()) {
23  ATH_MSG_FATAL("Please provide at least one container to sort");
24  return StatusCode::FAILURE;
25  }
26  ATH_CHECK(m_readKeys.initialize());
27  ATH_CHECK(m_writeKey.initialize());
28  ATH_CHECK(m_idHelperSvc.retrieve());
29  return StatusCode::SUCCESS;
30 }
31 StatusCode MuonSimHitSortingAlg::execute(const EventContext& ctx) const {
34  SG::ReadHandle<xAOD::MuonSimHitContainer> readHandle{inKey, ctx};
35  if(!readHandle.isValid()) {
36  ATH_MSG_FATAL("Failed to retrieve "<<inKey.fullKey());
37  return StatusCode::FAILURE;
38  }
39  std::copy(readHandle->begin(), readHandle->end(), std::back_inserter(allSimHits));
40  }
41  std::stable_sort(allSimHits.begin(), allSimHits.end(),
42  [this](const xAOD::MuonSimHit* a, const xAOD::MuonSimHit* b){
44  const IdentifierHash hashA = m_idHelperSvc->detElementHash(a->identify());
45  const IdentifierHash hashB = m_idHelperSvc->detElementHash(a->identify());
46  if (hashA != hashB) {
47  return hashA < hashB;
48  }
50  if (a->identify() != b->identify()) {
51  return a->identify() < b->identify();
52  }
54  if (std::abs(a->pdgId()) != std::abs(b->pdgId())){
55  return a->pdgId() > b->pdgId();
56  }
59  if (a->genParticleLink().barcode() != b->genParticleLink().barcode()) {
60  return a->genParticleLink().barcode() < b->genParticleLink().barcode();
61  }
63  return a->globalTime() < b->globalTime();
64  });
65  if (m_removeDuplicates) {
66  std::vector<const xAOD::MuonSimHit*> dupFreeHits{};
67  std::copy_if(allSimHits.begin(), allSimHits.end(), std::back_inserter(dupFreeHits),
68  [&dupFreeHits, this] (const xAOD::MuonSimHit* hit) {
69  const int barcode = hit->genParticleLink().barcode();
70  const Identifier hitId = hit->identify();
71  const Amg::Vector3D lPos{xAOD::toEigen(hit->localPosition())};
72  const Amg::Vector3D lDir{xAOD::toEigen(hit->localDirection())};
73  ATH_MSG_VERBOSE("Check sim hit "<<m_idHelperSvc->toString(hitId)<<", pdgId:"<<hit->pdgId()
74  <<", barcode: "<<barcode
75  <<" at "<<Amg::toString(lPos, 2)<<"direction: "<<Amg::toString(lDir, 2));
76  return std::find_if(dupFreeHits.begin(), dupFreeHits.end(),
77  [&](const xAOD::MuonSimHit* selHit) {
78  if (selHit->identify() != hitId ||
79  barcode != selHit->genParticleLink().barcode()) return false;
80  if (barcode) return true;
81  const Amg::Vector3D dPos = lPos - xAOD::toEigen(selHit->localPosition());
82  const Amg::Vector3D dDir = lDir - xAOD::toEigen(selHit->localDirection());
83  return dPos.mag() < tolerance && dDir.mag() < tolerance;
84  }) == dupFreeHits.end();
85  });
86  allSimHits.clear();
87  std::copy(dupFreeHits.begin(), dupFreeHits.end(), std::back_inserter(allSimHits));
88  }
89  SG::WriteHandle<xAOD::MuonSimHitContainer> writeHandle{m_writeKey, ctx};
90  if (m_writeDeepCopy) {
91  ATH_CHECK(writeHandle.record(std::make_unique<xAOD::MuonSimHitContainer>(),
92  std::make_unique<xAOD::MuonSimHitAuxContainer>()));
93  for (const xAOD::MuonSimHit* copy_me : allSimHits) {
94  xAOD::MuonSimHit* newHit = new xAOD::MuonSimHit();
95  writeHandle->push_back(newHit);
96  (*newHit) = (*copy_me);
97  }
98  } else {
99  ATH_CHECK(writeHandle.record(std::make_unique<xAOD::MuonSimHitContainer>(*allSimHits.asDataVector())));
100  }
101  return StatusCode::SUCCESS;
102 }
MuonSimHitSortingAlg::m_readKeys
SG::ReadHandleKeyArray< xAOD::MuonSimHitContainer > m_readKeys
Definition: MuonSimHitSortingAlg.h:22
xAOD::MuonSimHit_v1
Definition: MuonSimHit_v1.h:18
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
ConstDataVector.h
DataVector adapter that acts like it holds const pointers.
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
MuonSimHitAuxContainer.h
MuonSimHitSortingAlg.h
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
xAOD::MuonSimHit_v1::pdgId
int pdgId() const
Returns the pdgID of the traversing particle.
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
WriteHandle.h
Handle class for recording to StoreGate.
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
MuonSimHitSortingAlg::m_writeKey
SG::WriteHandleKey< xAOD::MuonSimHitContainer > m_writeKey
Definition: MuonSimHitSortingAlg.h:23
MuonSimHitSortingAlg::initialize
StatusCode initialize() override
Definition: MuonSimHitSortingAlg.cxx:21
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
AnalysisUtils::copy_if
Out copy_if(In first, const In &last, Out res, const Pred &p)
Definition: IFilterUtils.h:30
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonSimHitSortingAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MuonSimHitSortingAlg.h:25
python.SystemOfUnits.micrometer
int micrometer
Definition: SystemOfUnits.py:71
AthReentrantAlgorithm.h
tolerance
Definition: suep_shower.h:17
MuonSimHitSortingAlg::MuonSimHitSortingAlg
MuonSimHitSortingAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: MuonSimHitSortingAlg.cxx:18
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
MuonSimHitSortingAlg::execute
StatusCode execute(const EventContext &ctx) const override
Definition: MuonSimHitSortingAlg.cxx:31
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
a
TList * a
Definition: liststreamerinfos.cxx:10
ConstDataVector
DataVector adapter that acts like it holds const pointers.
Definition: ConstDataVector.h:76
if
if(febId1==febId2)
Definition: LArRodBlockPhysicsV0.cxx:569
MuonDetectorDefs.h
calibdata.copy
bool copy
Definition: calibdata.py:27
ReadHandle.h
Handle class for reading from StoreGate.
xAOD::MuonSimHit
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition: MuonSimHit.h:12