ATLAS Offline Software
MuonPRD_MultiTruthMaker.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Algorithm producing truth info for PrepRawData, keeping all MC particles contributed to a PRD.
6 // A. Gaponenko, 2006
7 
9 
10 #include <iterator>
11 #include <typeinfo>
12 
13 //================================================================
14 MuonPRD_MultiTruthMaker::MuonPRD_MultiTruthMaker(const std::string& name, ISvcLocator* pSvcLocator) : AthAlgorithm(name, pSvcLocator) {
15  // Input
16  declareProperty("MDT_PrepRawDataContainer", m_MDT_ContainerName = "MDT_DriftCircles");
17  declareProperty("CSC_PrepRawDataContainer", m_CSC_ContainerName = "CSC_Clusters");
18  declareProperty("RPC_PrepRawDataContainer", m_RPC_ContainerName = "RPC_Measurements");
19  declareProperty("TGC_PrepRawDataContainer", m_TGC_ContainerName = "TGC_Measurements");
20  declareProperty("STGC_PrepRawDataContainer", m_STGC_ContainerName = "STGC_Measurements");
21  declareProperty("MM_PrepRawDataContainer", m_MM_ContainerName = "MM_Measurements");
22 
23  declareProperty("MDT_SDO_Container", m_MDT_SimDataMapName = "MDT_SDO");
24  declareProperty("CSC_SDO_Container", m_CSC_SimDataMapName = "CSC_SDO");
25  declareProperty("RPC_SDO_Container", m_RPC_SimDataMapName = "RPC_SDO");
26  declareProperty("TGC_SDO_Container", m_TGC_SimDataMapName = "TGC_SDO");
27  declareProperty("STGC_SDO_Container", m_STGC_SimDataMapName = "sTGC_SDO");
28  declareProperty("MM_SDO_Container", m_MM_SimDataMapName = "MM_SDO");
29 
30  // Output
31  declareProperty("MDT_PRD_TruthContainer", m_MDT_PRD_TruthName = "MDT_TruthMap");
32  declareProperty("CSC_PRD_TruthContainer", m_CSC_PRD_TruthName = "CSC_TruthMap");
33  declareProperty("RPC_PRD_TruthContainer", m_RPC_PRD_TruthName = "RPC_TruthMap");
34  declareProperty("TGC_PRD_TruthContainer", m_TGC_PRD_TruthName = "TGC_TruthMap");
35  declareProperty("STGC_PRD_TruthContainer", m_STGC_PRD_TruthName = "STGC_TruthMap");
36  declareProperty("MM_PRD_TruthContainer", m_MM_PRD_TruthName = "MM_TruthMap");
37 }
38 
39 //================================================================
41  ATH_MSG_DEBUG("MuonPRD_MultiTruthMaker::initialize()");
42  ATH_CHECK(m_idHelperSvc.retrieve());
43  ATH_CHECK(m_MDT_ContainerName.initialize());
44  ATH_CHECK(m_CSC_ContainerName.initialize(m_idHelperSvc->hasCSC()));
45  ATH_CHECK(m_RPC_ContainerName.initialize());
46  ATH_CHECK(m_TGC_ContainerName.initialize());
47  ATH_CHECK(m_STGC_ContainerName.initialize(m_idHelperSvc->hasSTGC()));
48  ATH_CHECK(m_MM_ContainerName.initialize(m_idHelperSvc->hasMM()));
61  return StatusCode::SUCCESS;
62 }
63 
64 //================================================================
66  ATH_MSG_DEBUG("MuonPRD_MultiTruthMaker::execute()");
67 
68  std::vector<StatusCode> retvals;
69  retvals.push_back(
70  buildPRD_Truth<Muon::MdtPrepDataContainer, MuonSimDataCollection>(m_MDT_ContainerName, m_MDT_SimDataMapName, m_MDT_PRD_TruthName));
71  if (m_idHelperSvc->hasCSC())
72  retvals.push_back(buildPRD_Truth<Muon::CscPrepDataContainer, CscSimDataCollection>(m_CSC_ContainerName, m_CSC_SimDataMapName,
74  retvals.push_back(
75  buildPRD_Truth<Muon::RpcPrepDataContainer, MuonSimDataCollection>(m_RPC_ContainerName, m_RPC_SimDataMapName, m_RPC_PRD_TruthName));
76  retvals.push_back(
77  buildPRD_Truth<Muon::TgcPrepDataContainer, MuonSimDataCollection>(m_TGC_ContainerName, m_TGC_SimDataMapName, m_TGC_PRD_TruthName));
78  if (m_idHelperSvc->hasSTGC())
79  retvals.push_back(buildPRD_Truth<Muon::sTgcPrepDataContainer, MuonSimDataCollection>(m_STGC_ContainerName, m_STGC_SimDataMapName,
81  if (m_idHelperSvc->hasMM())
82  retvals.push_back(
83  buildPRD_Truth<Muon::MMPrepDataContainer, MuonSimDataCollection>(m_MM_ContainerName, m_MM_SimDataMapName, m_MM_PRD_TruthName));
84 
85  bool ok = true;
86  for (std::vector<StatusCode>::const_iterator i = retvals.begin(); i != retvals.end(); ++i) {
87  if (i->isFailure()) ok = false;
88  }
89  if (ok) return StatusCode::SUCCESS;
90  return StatusCode::FAILURE;
91 }
92 
93 //================================================================
94 template <class PrepDataContainer, class SIMDATACOLLECTION>
98  SG::ReadHandle<SIMDATACOLLECTION> simDataMap(sdoKey);
99  if (!simDataMap.isPresent()) {
100  ATH_MSG_DEBUG("SimDataCollection for key=" << sdoKey.key() << " not in storegate, not adding it ");
101  return StatusCode::SUCCESS;
102  }
103 
104  if (!simDataMap.isValid()) {
105  ATH_MSG_ERROR("Could not read " << sdoKey.key());
106  return StatusCode::FAILURE;
107  }
108 
109  SG::ReadHandle<PrepDataContainer> prdContainer(prepDataKey);
110  if (!prdContainer.isValid()) {
111  ATH_MSG_ERROR("Could not read " << prepDataKey.key());
112  return StatusCode::FAILURE;
113  }
114 
115  // Create and fill the PRD truth structure
116  ATH_MSG_DEBUG("make PRD truth for " << outputKey.key());
118  ATH_CHECK(output.record(std::make_unique<PRD_MultiTruthCollection>()));
119  addPRDCollections(output, prdContainer->begin(), prdContainer->end(), simDataMap);
120 
121  return StatusCode::SUCCESS;
122 }
123 
124 //================================================================
125 template <class PRD_Container_Iterator, class SIMDATACOLLECTION>
127  PRD_Container_Iterator collections_begin, PRD_Container_Iterator collections_end,
129  for (PRD_Container_Iterator colNext = collections_begin; colNext != collections_end; ++colNext) {
130  addPRDRange(prdTruth, (*colNext)->begin(), (*colNext)->end(), simDataMap);
131  }
132 }
133 
134 //================================================================
135 // Adds PRDs in the range to prdTruth.
136 template <class PRD_Collection_Iterator, class SIMDATACOLLECTION>
137 void MuonPRD_MultiTruthMaker::addPRDRange(SG::WriteHandle<PRD_MultiTruthCollection>& prdTruth, PRD_Collection_Iterator range_begin,
138  PRD_Collection_Iterator range_end, SG::ReadHandle<SIMDATACOLLECTION> simDataMap) {
139  for (PRD_Collection_Iterator nextDatum = range_begin; nextDatum != range_end; nextDatum++) {
140  addPrepRawDatum(prdTruth, *nextDatum, simDataMap);
141  }
142 }
143 
144 //================================================================
145 template <class SIMDATACOLLECTION>
148  ATH_MSG_VERBOSE("addPrepRawDatum(): new PRD " << prd << ", id=" << prd->identify() << ", number of RDOs: " << prd->rdoList().size());
149 
150  bool gotSDO = false;
151  bool gotValidParticle = false;
152 
153  // loop over RDOs
154  for (const auto& nextRDO : prd->rdoList()) {
155  typename SIMDATACOLLECTION::const_iterator iter(simDataMap->find(nextRDO));
156 
157  if (iter != simDataMap->end()) {
158  gotSDO = true;
159  // Got an SDO. Try to associate the PRD to MC particles we have info about.
160  typedef typename SIMDATACOLLECTION::mapped_type SIMDATA;
161  const SIMDATA& sdo = iter->second;
162  const std::vector<typename SIMDATA::Deposit>& deposits = sdo.getdeposits();
163  if (deposits.empty()) { continue; }
164  for (const auto& [particleLink, mcData] : deposits) {
165  ATH_MSG_VERBOSE("addPrepRawDatum(): particleLink.isValid() " << particleLink.isValid());
166  ATH_MSG_VERBOSE("addPrepRawDatum(): Barcode " << particleLink.barcode() << " evt " << particleLink.eventIndex());
167  if (particleLink.isValid()) {
168  gotValidParticle = true;
169  // Associate the particle to the PRD. But don't add duplicates.
170  // Note: it may be more efficient to filter out duplicates among particles for the current PRD, then check-and-add the
171  // reduced set to the large multimap. But may be not for the typically small RDO/PRD ratio.
172  typedef PRD_MultiTruthCollection::iterator truthiter;
173  std::pair<truthiter, truthiter> r = prdTruth->equal_range(prd->identify());
174  const auto& pl = particleLink; // Work around problem with clang 6.0.1
175  if (r.second == std::find_if(r.first, r.second, [pl](const PRD_MultiTruthCollection::value_type& prd_to_truth) {
176  return prd_to_truth.second == pl;
177  })) {
178  prdTruth->insert(std::make_pair(prd->identify(), particleLink));
179  }
180  }
181  }
182  }
183  }
184 
185  if (gotSDO && !gotValidParticle) {
186  // Looked at all the deposits from all the SDOs, but did not find any valid particle link.
187  // prdTruth->insert(std::make_pair(prd, particleLinkUnknown));
188  ATH_MSG_DEBUG("addPrepRawDatum(): got SDO but no particles");
189  }
190 }
191 
192 //================================================================
193 // EOF
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
beamspotman.r
def r
Definition: beamspotman.py:676
MuonPRD_MultiTruthMaker.h
MuonPRD_MultiTruthMaker::m_MDT_SimDataMapName
SG::ReadHandleKey< MuonSimDataCollection > m_MDT_SimDataMapName
Definition: MuonPRD_MultiTruthMaker.h:44
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
MuonPRD_MultiTruthMaker::m_MM_ContainerName
SG::ReadHandleKey< Muon::MMPrepDataContainer > m_MM_ContainerName
Definition: MuonPRD_MultiTruthMaker.h:42
MuonPRD_MultiTruthMaker::m_MM_SimDataMapName
SG::ReadHandleKey< MuonSimDataCollection > m_MM_SimDataMapName
Definition: MuonPRD_MultiTruthMaker.h:49
Trk::PrepRawData::rdoList
const std::vector< Identifier > & rdoList() const
return the List of rdo identifiers (pointers)
MuonPRD_MultiTruthMaker::MuonPRD_MultiTruthMaker
MuonPRD_MultiTruthMaker(const std::string &name, ISvcLocator *pSvcLocator)
Definition: MuonPRD_MultiTruthMaker.cxx:14
MuonPRD_MultiTruthMaker::m_CSC_PRD_TruthName
SG::WriteHandleKey< PRD_MultiTruthCollection > m_CSC_PRD_TruthName
Definition: MuonPRD_MultiTruthMaker.h:52
MuonPRD_MultiTruthMaker::m_MM_PRD_TruthName
SG::WriteHandleKey< PRD_MultiTruthCollection > m_MM_PRD_TruthName
Definition: MuonPRD_MultiTruthMaker.h:56
MuonPRD_MultiTruthMaker::addPRDRange
void addPRDRange(SG::WriteHandle< PRD_MultiTruthCollection > &prdTruth, PRD_Collection_Iterator range_begin, PRD_Collection_Iterator range_end, SG::ReadHandle< SIMDATACOLLECTION > simDataMap)
Definition: MuonPRD_MultiTruthMaker.cxx:137
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
MuonPRD_MultiTruthMaker::m_TGC_SimDataMapName
SG::ReadHandleKey< MuonSimDataCollection > m_TGC_SimDataMapName
Definition: MuonPRD_MultiTruthMaker.h:47
MuonPRD_MultiTruthMaker::m_STGC_PRD_TruthName
SG::WriteHandleKey< PRD_MultiTruthCollection > m_STGC_PRD_TruthName
Definition: MuonPRD_MultiTruthMaker.h:55
MuonPRD_MultiTruthMaker::m_TGC_ContainerName
SG::ReadHandleKey< Muon::TgcPrepDataContainer > m_TGC_ContainerName
Definition: MuonPRD_MultiTruthMaker.h:40
MuonPRD_MultiTruthMaker::addPRDCollections
void addPRDCollections(SG::WriteHandle< PRD_MultiTruthCollection > &prdTruth, PRD_Container_Iterator collections_begin, PRD_Container_Iterator collections_end, SG::ReadHandle< SIMDATACOLLECTION > simDataMap)
Definition: MuonPRD_MultiTruthMaker.cxx:126
MuonPRD_MultiTruthMaker::m_TGC_PRD_TruthName
SG::WriteHandleKey< PRD_MultiTruthCollection > m_TGC_PRD_TruthName
Definition: MuonPRD_MultiTruthMaker.h:54
TileDigitizationConfig.outputKey
outputKey
Definition: TileDigitizationConfig.py:104
MuonPRD_MultiTruthMaker::m_STGC_SimDataMapName
SG::ReadHandleKey< MuonSimDataCollection > m_STGC_SimDataMapName
Definition: MuonPRD_MultiTruthMaker.h:48
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::WriteHandleKey< PRD_MultiTruthCollection >
lumiFormat.i
int i
Definition: lumiFormat.py:92
MuonPRD_MultiTruthMaker::execute
virtual StatusCode execute()
Definition: MuonPRD_MultiTruthMaker.cxx:65
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
MuonPRD_MultiTruthMaker::m_STGC_ContainerName
SG::ReadHandleKey< Muon::sTgcPrepDataContainer > m_STGC_ContainerName
Definition: MuonPRD_MultiTruthMaker.h:41
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
AthAlgorithm
Definition: AthAlgorithm.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
MuonPRD_MultiTruthMaker::m_CSC_SimDataMapName
SG::ReadHandleKey< CscSimDataCollection > m_CSC_SimDataMapName
Definition: MuonPRD_MultiTruthMaker.h:45
merge.output
output
Definition: merge.py:17
Trk::PrepRawData
Definition: PrepRawData.h:62
Trk::PrepRawData::identify
Identifier identify() const
return the identifier
MuonPRD_MultiTruthMaker::m_RPC_SimDataMapName
SG::ReadHandleKey< MuonSimDataCollection > m_RPC_SimDataMapName
Definition: MuonPRD_MultiTruthMaker.h:46
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
MuonPRD_MultiTruthMaker::buildPRD_Truth
StatusCode buildPRD_Truth(SG::ReadHandleKey< PrepDataContainer > prepDataKey, SG::ReadHandleKey< SIMDATACOLLECTION > sdoKey, const SG::WriteHandleKey< PRD_MultiTruthCollection > &outputKey)
Definition: MuonPRD_MultiTruthMaker.cxx:95
MuonPRD_MultiTruthMaker::m_MDT_PRD_TruthName
SG::WriteHandleKey< PRD_MultiTruthCollection > m_MDT_PRD_TruthName
Definition: MuonPRD_MultiTruthMaker.h:51
MuonPRD_MultiTruthMaker::m_RPC_PRD_TruthName
SG::WriteHandleKey< PRD_MultiTruthCollection > m_RPC_PRD_TruthName
Definition: MuonPRD_MultiTruthMaker.h:53
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
MuonPRD_MultiTruthMaker::addPrepRawDatum
void addPrepRawDatum(SG::WriteHandle< PRD_MultiTruthCollection > &prdTruth, const Trk::PrepRawData *prd, SG::ReadHandle< SIMDATACOLLECTION > simDataMap)
Definition: MuonPRD_MultiTruthMaker.cxx:146
MuonPRD_MultiTruthMaker::m_MDT_ContainerName
SG::ReadHandleKey< Muon::MdtPrepDataContainer > m_MDT_ContainerName
Definition: MuonPRD_MultiTruthMaker.h:37
MuonPRD_MultiTruthMaker::initialize
virtual StatusCode initialize()
Definition: MuonPRD_MultiTruthMaker.cxx:40
MuonPRD_MultiTruthMaker::m_CSC_ContainerName
SG::ReadHandleKey< Muon::CscPrepDataContainer > m_CSC_ContainerName
Definition: MuonPRD_MultiTruthMaker.h:38
MuonPRD_MultiTruthMaker::m_RPC_ContainerName
SG::ReadHandleKey< Muon::RpcPrepDataContainer > m_RPC_ContainerName
Definition: MuonPRD_MultiTruthMaker.h:39
MuonPRD_MultiTruthMaker::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MuonPRD_MultiTruthMaker.h:35
SG::VarHandleBase::isPresent
bool isPresent() const
Is the referenced object present in SG?
Definition: StoreGate/src/VarHandleBase.cxx:397