ATLAS Offline Software
TruthOverlay.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 #include "TruthOverlay.h"
5 
8 #include "StoreGate/ReadHandle.h"
11 
12 namespace MuonR4{
13 
15  ATH_CHECK(m_sigKey.initialize());
16  ATH_CHECK(m_bkgKey.initialize());
17  ATH_CHECK(m_writeKey.initialize());
18  ATH_CHECK(m_idHelperSvc.retrieve());
19  if (m_deadTime < m_mergeTime) {
20  ATH_MSG_FATAL("The "<<m_deadTime<<" should be greater than "<<m_mergeTime);
21  return StatusCode::FAILURE;
22  }
23  return StatusCode::SUCCESS;
24  }
25  StatusCode TruthOverlay::execute(const EventContext& ctx) const {
26 
27 
29  std::map<Identifier, std::vector<const xAOD::MuonSimHit*>,
31 
33  auto fillMap = [&overlayed, &ctx, this] (const SG::ReadHandleKey<xAOD::MuonSimHitContainer>& key) -> StatusCode{
34  SG::ReadHandle hits{key, ctx};
35  ATH_CHECK(hits.isPresent());
36  xAOD::ChamberViewer viewer{*hits, m_idHelperSvc.get()};
37  do {
38  for (const xAOD::MuonSimHit* hit : viewer) {
39  overlayed[hit->identify()].push_back(hit);
40  }
41  } while (viewer.next());
42 
43  return StatusCode::SUCCESS;
44  };
45  ATH_CHECK(fillMap(m_sigKey));
46  ATH_CHECK(fillMap(m_bkgKey));
47 
49  SG::WriteHandle writeHandle {m_writeKey, ctx};
50  ATH_CHECK(writeHandle.record(std::make_unique<xAOD::MuonSimHitContainer>(),
51  std::make_unique<xAOD::MuonSimHitAuxContainer>()));
52 
53  for (auto& [mergeId, hits] : overlayed) {
54  ATH_MSG_VERBOSE("Perform truth overlay for "<<m_idHelperSvc->toString(mergeId));
55  std::ranges::sort(hits, [](const xAOD::MuonSimHit* a, const xAOD::MuonSimHit* b){
56  return a->globalTime() < b->globalTime();
57  });
58 
59  for (auto itr = hits.begin(); itr != hits.end(); ++itr){
60  const xAOD::MuonSimHit* primHit{*itr};
61  if (!m_includePileUpTruth && HepMC::ignoreTruthLink(primHit->genParticleLink(), m_vetoPileUpTruthLinks)){
62  ATH_MSG_VERBOSE("Reject background hit");
63  continue;
64  }
65  xAOD::MuonSimHit* merged = writeHandle->push_back(std::make_unique<xAOD::MuonSimHit>());
66  (*merged) = (*primHit);
67  auto mergeItr = itr +1;
68  for ( ; mergeItr !=hits.end(); ++mergeItr) {
69  const xAOD::MuonSimHit* mergeMe{*itr};
70  if (!m_includePileUpTruth && HepMC::ignoreTruthLink(mergeMe->genParticleLink(), m_vetoPileUpTruthLinks)){
71  ATH_MSG_VERBOSE("Don't merge with a background hit.");
72  continue;
73  }
74  if(mergeMe->globalTime() - primHit->globalTime() < m_mergeTime) {
76  if (std::abs(mergeMe->pdgId()) == 13 || (mergeMe->genParticleLink() && !merged->genParticleLink())){
77  const float eDep = merged->energyDeposit();
78  (*merged) = (*mergeMe);
79  merged->setEnergyDeposit(eDep);
80  }
81  merged->setEnergyDeposit(mergeMe->energyDeposit() + primHit->energyDeposit());
82  } else if (mergeMe->globalTime() - primHit->globalTime() < m_deadTime) {
83  continue;
84  } else {
85  break;
86  }
87  }
88  itr = mergeItr - 1;
89  }
90  }
91  return StatusCode::SUCCESS;
92  }
93 }
xAOD::MuonSimHit_v1
Definition: MuonSimHit_v1.h:18
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TRTCalib_Extractor.hits
hits
Definition: TRTCalib_Extractor.py:35
MuonR4::TruthOverlay::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: TruthOverlay.h:25
MuonR4::TruthOverlay::m_mergeTime
Gaudi::Property< double > m_mergeTime
Define a detector merge time.
Definition: TruthOverlay.h:32
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
MuonR4::TruthOverlay::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: TruthOverlay.cxx:25
MuonR4::TruthOverlay::m_bkgKey
SG::ReadHandleKey< xAOD::MuonSimHitContainer > m_bkgKey
Definition: TruthOverlay.h:22
MuonSimHitAuxContainer.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
HepMC::ignoreTruthLink
bool ignoreTruthLink(const T &p, bool vetoPileUp)
Helper function for SDO creation in PileUpTools.
Definition: MagicNumbers.h:332
TruthOverlay.h
MuonR4::TruthOverlay::m_deadTime
Gaudi::Property< double > m_deadTime
Define a detector dead time.
Definition: TruthOverlay.h:29
xAOD::ChamberViewer
Definition: ChamberViewer.h:65
WriteHandle.h
Handle class for recording to StoreGate.
xAOD::MuonSimHit_v1::genParticleLink
const HepMcParticleLink & genParticleLink() const
Returns the link to the HepMC particle producing this hit.
Definition: xAODMuonSimHit_V1.cxx:67
MuonR4::TruthOverlay::m_includePileUpTruth
Gaudi::Property< bool > m_includePileUpTruth
Definition: TruthOverlay.h:34
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
MuonR4::TruthOverlay::initialize
StatusCode initialize() override final
Definition: TruthOverlay.cxx:14
MuonR4::TruthOverlay::m_sigKey
SG::ReadHandleKey< xAOD::MuonSimHitContainer > m_sigKey
Definition: TruthOverlay.h:21
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonR4::TruthOverlay::m_vetoPileUpTruthLinks
Gaudi::Property< int > m_vetoPileUpTruthLinks
Definition: TruthOverlay.h:35
xAOD::MuonSimHit_v1::setEnergyDeposit
void setEnergyDeposit(const float deposit)
Sets the energy deposited by the traversing particle inside the gas volume.
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
ChamberViewer.h
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
MuonR4::TruthOverlay::m_writeKey
SG::WriteHandleKey< xAOD::MuonSimHitContainer > m_writeKey
Definition: TruthOverlay.h:23
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
a
TList * a
Definition: liststreamerinfos.cxx:10
xAOD::MuonSimHit_v1::energyDeposit
float energyDeposit() const
Returns the energy deposited by the traversing particle inside the gas volume.
IdentifierByDetElSorter.h
Muon::IdentifierByDetElSorter
Definition: IdentifierByDetElSorter.h:17
ReadHandle.h
Handle class for reading from StoreGate.
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37