ATLAS Offline Software
Loading...
Searching...
No Matches
sTgcRdoToPrepDataToolMT.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
14
15using namespace MuonGM;
16using namespace Trk;
17using namespace Muon;
18
19namespace {
20 std::atomic<bool> hitNegativeCharge{false};
21}
22
23//============================================================================
25{
26 ATH_MSG_DEBUG(" in initialize()");
27 ATH_CHECK( m_idHelperSvc.retrieve() );
28 // check if the initialization of the data container is success
30 ATH_CHECK(m_rdoContainerKey.initialize());
31 ATH_CHECK(m_muDetMgrKey.initialize());
32 ATH_CHECK(m_calibTool.retrieve());
33 ATH_CHECK(m_prdContainerCacheKey.initialize(!m_prdContainerCacheKey.key().empty()) );
34
35 if (m_useNewGeo) {
36 ATH_CHECK(detStore()->retrieve(m_detMgrR4));
37 }
38
39 ATH_CHECK(m_xAODStripKey.initialize(!m_xAODStripKey.empty()));
40 ATH_CHECK(m_xAODWireKey.initialize(!m_xAODWireKey.empty()));
41 ATH_CHECK(m_xAODPadKey.initialize(!m_xAODPadKey.empty()));
42 return StatusCode::SUCCESS;
43}
44
45
46//============================================================================
47StatusCode Muon::sTgcRdoToPrepDataToolMT::processCollection(const EventContext& ctx,
48 outputCache& xAODcontainers,
49 const STGC_RawDataCollection *rdoColl) const {
50
51 const sTgcIdHelper& id_helper = m_idHelperSvc->stgcIdHelper();
52 const IdentifierHash hash = rdoColl->identifyHash();
53
54 ATH_MSG_DEBUG(" ***************** Start of process STGC Collection with hash Id: " << hash);
55
56 auto stgcPrepDataContainer = xAODcontainers.prd;
57 // check if the collection already exists, otherwise add it
58 if ( stgcPrepDataContainer->indexFindPtr(hash) != nullptr ) {
59 ATH_MSG_DEBUG("In processCollection: collection already contained in the sTGC PrepData container");
60 return StatusCode::FAILURE;
61
62 }
63
64 // Get write handle for this collection
65 sTgcPrepDataContainer::IDC_WriteHandle lock = stgcPrepDataContainer->getWriteHandle( hash );
66 // Check if collection already exists (via the cache, i.e. in online trigger mode)
68 ATH_MSG_DEBUG("In processCollection: collection already available in the sTgc PrepData container (via cache)");
69 return StatusCode::SUCCESS;
70 }
71
72 // Make the PRD collection (will be added to container later
73 std::unique_ptr<sTgcPrepDataCollection> prdColl = std::make_unique<sTgcPrepDataCollection>(hash);
74
75 // set the offline identifier of the collection Id
76 IdContext context = id_helper.module_context();
77 Identifier moduleId;
78 int getId = id_helper.get_id(hash, moduleId, &context);
79 if ( getId != 0 ) {
80 ATH_MSG_ERROR("Could not convert the hash Id: " << hash << " to identifier");
81 } else {
82 prdColl->setIdentifier(moduleId);
83 }
84
85 // vectors to hold PRDs decoded for this RDO collection
86 std::vector<sTgcPrepData> sTgcStripPrds;
87 std::vector<sTgcPrepData> sTgcWirePrds;
88 std::vector<sTgcPrepData> sTgcPadPrds;
89 sTgcStripPrds.reserve(rdoColl->size());
90 sTgcPadPrds.reserve(rdoColl->size());
91 sTgcWirePrds.reserve(rdoColl->size());
92
93 // Count hits with negative charge, which indicates bad calibration
94
95
96 // MuonDetectorManager from the conditions store
97 const MuonGM::MuonDetectorManager* muonDetMgr{nullptr};
98 ATH_CHECK(SG::get(muonDetMgr,m_muDetMgrKey,ctx));
99 // convert the RDO collection to a PRD collection
100 for ( const STGC_RawData* rdo : * rdoColl) {
101
102 ATH_MSG_DEBUG("Adding a new sTgc PrepRawData");
103
104 const Identifier rdoId = rdo->identify();
105
106 std::vector<Identifier> rdoList{rdoId};
107
108 // get the local and global positions
109 const MuonGM::sTgcReadoutElement* detEl = muonDetMgr->getsTgcReadoutElement(rdoId);
110 Amg::Vector2D localPos{Amg::Vector2D::Zero()};
111
112 int channelType = id_helper.channelType(rdoId);
113 if (channelType < 0 || channelType > 2) {
114 ATH_MSG_ERROR("Unknown sTGC channel type");
115 return StatusCode::FAILURE;
116 }
117 if (!detEl->stripPosition(rdoId, localPos)) {
118 ATH_MSG_ERROR("Could not get the local strip position for "<<m_idHelperSvc->toString(rdoId));
119 return StatusCode::FAILURE;
120 }
121
122 // get the resolution from strip width
123 // to be fixed: for now do not set the resolution, it will be added in the next update
124 const int gasGap = id_helper.gasGap(rdoId);
125 const int channel = id_helper.channel(rdoId);
126
127 NSWCalib::CalibratedStrip calibStrip;
128 ATH_CHECK (m_calibTool->calibrateStrip(ctx, rdo, calibStrip));
129 int calibratedCharge = static_cast<int>(calibStrip.charge);
130 if (calibratedCharge < 0 && channelType == 1) { // we only want to protect against negatively charged strips and we should not lose wire or pad hits because of bad calibrations since charge does not matter for them in reco.
131 if (!hitNegativeCharge) {
132 ATH_MSG_DEBUG("One sTGC RDO or more, such as one with pdo = "<<rdo->charge() << " counts, corresponds to a negative charge (" << calibratedCharge << "). Skipping these RDOs");
133 hitNegativeCharge = true;
134 }
135 continue;
136 }
137
138 double width{0.};
139 if (channelType == sTgcIdHelper::sTgcChannelTypes::Pad) { // Pads
140 const MuonGM::MuonPadDesign* design = detEl->getPadDesign(rdoId);
141 if (!design) {
142 ATH_MSG_WARNING("Failed to get design for sTGC pad" );
143 } else {
144 width = design->channelWidth(localPos, true);
145 }
146 } else { // Strips and wires
147 const MuonGM::MuonChannelDesign* design = detEl->getDesign(rdoId);
148 if (!design) {
149 ATH_MSG_WARNING("Failed to get design for sTGC strip/wire" );
150 } else {
151 width = design->channelWidth();
152 }
153 }
154
155 const double resolution = width/ std::sqrt(12.);
156 auto cov = Amg::MatrixX(1,1);
157 cov.setIdentity();
158 (cov)(0,0) = resolution*resolution;
159
160 ATH_MSG_DEBUG("Adding a new STGC PRD, gasGap: " << gasGap << " channel: " << channel << " type: " << channelType << " resolution " << resolution );
161
162 if(m_merge) {
163 std::vector<sTgcPrepData>& sTgcPrds = channelType == sTgcIdHelper::Pad ? sTgcPadPrds :
164 (channelType == sTgcIdHelper::Strip ? sTgcStripPrds : sTgcWirePrds);
165
166 // check if the same RdoId is already present; keep the one with the smallest time
167 auto it = std::find_if(sTgcPrds.begin(), sTgcPrds.end(), [&rdoId](const sTgcPrepData& prd) {
168 return (prd.identify() == rdoId);
169 });
170 if (it == sTgcPrds.end()) {
171 sTgcPrds.emplace_back(rdoId, hash, std::move(localPos), std::move(rdoList), std::move(cov), detEl, calibratedCharge, calibStrip.time);
172 sTgcPrds.back().setAuthor(sTgcPrepData::Author::RdoToPrdConverter);
173 } else if (it->time() > calibStrip.time) {
174 *it = sTgcPrepData(rdoId, hash, std::move(localPos), std::move(rdoList), std::move(cov), detEl, calibratedCharge, calibStrip.time);
176 }
177
178 // TODO - add merging for xAOD
179
180 } else {
181 // if not merging just add the PRD to the collection
182 prdColl->push_back(std::make_unique<sTgcPrepData>(rdoId,
183 hash,
184 std::move(localPos),
185 std::move(rdoList),
186 std::move(cov),
187 detEl,
188 calibratedCharge,
189 calibStrip.time));
190 }
191 }
192
193 if(m_merge) {
194 // merge strip prds that fire closeby channels (not clusterizing wires and pads)
195 std::vector<std::unique_ptr<Muon::sTgcPrepData>> sTgcStripClusters;
196 ATH_CHECK(m_clusterBuilderTool->getClusters(ctx, std::move(sTgcStripPrds), sTgcStripClusters)); // Clusterize strips
197
198 for ( std::unique_ptr<Muon::sTgcPrepData>& it : sTgcStripClusters ) {
199 it->setHashAndIndex(prdColl->identifyHash(), prdColl->size());
200 prdColl->push_back(std::move(it));
201 }
202 for ( Muon::sTgcPrepData& prd : sTgcWirePrds ) {
203 prd.setHashAndIndex(prdColl->identifyHash(), prdColl->size());
204 prdColl->push_back(std::make_unique<sTgcPrepData>(std::move(prd)));
205 }
206 for (Muon::sTgcPrepData& prd : sTgcPadPrds ) {
207 prd.setHashAndIndex(prdColl->identifyHash(), prdColl->size());
208 prdColl->push_back(std::make_unique<sTgcPrepData>(std::move(prd)));
209 }
210 }
211 const bool convertXAOD = !m_xAODPadKey.empty() || !m_xAODStripKey.empty() ||
212 !m_xAODWireKey.empty();
213
214 if (convertXAOD) {
215 for (const Muon::sTgcPrepData* prd : *prdColl) {
216 const Identifier prdId = prd->identify();
217 const int gasGap = id_helper.gasGap(prdId);
218 const int channel = id_helper.channel(prdId);
219 const int chType = id_helper.channelType(prdId);
220 xAOD::sTgcMeasurement* outHit{nullptr};
221 ATH_MSG_VERBOSE("Convert "
222 <<m_idHelperSvc->toString(prdId)<<". "<<Amg::toString(prd->localPosition())
223 <<", cov: "<<prd->localCovariance()(0,0)
224 <<" global pos: "<<Amg::toString(prd->globalPosition()));
226 outHit = xAODcontainers.pad->push_back(std::make_unique<xAOD::sTgcPadHit>());
228 lCov(0,0) = prd->localCovariance()(0,0);
230 // lCov(1,1) = prd->localCovariance()(1,1);
231 outHit->setMeasurement<2>(m_idHelperSvc->detElementHash(prdId),
232 xAOD::toStorage(prd->localPosition()),
233 std::move(lCov));
235 outHit = xAODcontainers.wire->push_back(std::make_unique<xAOD::sTgcWireHit>());
237 auto stripHit = xAODcontainers.strip->push_back(std::make_unique<xAOD::sTgcStripCluster>());
238 stripHit->setStripCharges(prd->stripCharges());
239 stripHit->setStripNumbers(prd->stripNumbers());
240 stripHit->setStripTimes(prd->stripTimes());
241 outHit = stripHit;
242 }
243 if (!outHit) {
244 continue;
245 }
247 xAOD::MeasVector<1> lPos = prd->localPosition().x() * xAOD::MeasVector<1>::UnitX();
248 xAOD::MeasMatrix<1> lCov{};
249 lCov(0,0) = prd->localCovariance()(0,0);
250 outHit->setMeasurement<1>(m_idHelperSvc->detElementHash(prdId),
251 std::move(lPos),
252 std::move(lCov));
253
254 }
255 outHit->setChannelNumber(channel);
256 outHit->setGasGap(gasGap);
257 outHit->setAuthor(prd->author());
258 outHit->setTime(prd->time());
259 outHit->setCharge(prd->charge());
260 outHit->setIdentifier(prdId.get_compact());
261 if (m_detMgrR4) {
262 outHit->setReadoutElement(m_detMgrR4->getsTgcReadoutElement(prdId));
263 }
264 }
265 }
266
267
268 // now add the collection to the container
269 ATH_CHECK( lock.addOrDelete(std::move( prdColl ) ) );
270 ATH_MSG_DEBUG("PRD hash " << hash << " has been moved to container");
271
272 return StatusCode::SUCCESS;
273}
274
275
276//============================================================================
278{
279 auto rdoContainerHandle = SG::makeHandle(m_rdoContainerKey, ctx);
280 if(rdoContainerHandle.isValid()) {
281 ATH_MSG_DEBUG("STGC_getRdoContainer success");
282 return rdoContainerHandle.cptr();
283 }
284 ATH_MSG_WARNING("Retrieval of STGC_RawDataContainer failed !");
285
286 return nullptr;
287}
288
289
290//============================================================================
292 outputCache& xAODcontainers,
293 const std::vector<IdentifierHash>& idsToDecode) const
294{
295 ATH_MSG_DEBUG("In processRDOContainer");
296 const STGC_RawDataContainer* rdoContainer = getRdoContainer(ctx);
297 if (!rdoContainer) return;
298
299 // run in unseeded mode
300 for (const STGC_RawDataCollection* rdoColl : *rdoContainer) {
301 if (rdoColl->empty()) continue;
302 ATH_MSG_DEBUG("New RDO collection with " << rdoColl->size() << "STGC Hits");
303
304 const IdentifierHash hash = rdoColl->identifyHash();
305
306 // check if we actually want to decode this RDO collection
307 if(!idsToDecode.empty() and std::find(idsToDecode.begin(), idsToDecode.end(), hash)==idsToDecode.end()) {
308 ATH_MSG_DEBUG("Hash ID " << hash << " not in input list, ignore");
309 continue;
310 } else ATH_MSG_DEBUG("Going to decode " << hash);
311
312 if(processCollection(ctx, xAODcontainers, rdoColl).isFailure()) {
313 ATH_MSG_DEBUG("processCsm returns a bad StatusCode - keep going for new data collections in this event");
314 }
315 }
316}
317
318// methods for ROB-based decoding
319//============================================================================
320StatusCode Muon::sTgcRdoToPrepDataToolMT::decode(const EventContext& ctx,
321 const std::vector<IdentifierHash>& idVect) const {
322 ATH_MSG_DEBUG("Size of the input hash id vector: " << idVect.size());
323
324 outputCache outCache = setupOutputContainers(ctx);
325 if (!outCache.isValid) return StatusCode::FAILURE;
326
327 processRDOContainer(ctx, outCache, idVect);
328 return StatusCode::SUCCESS;
329}
330
331
332//============================================================================
333StatusCode Muon::sTgcRdoToPrepDataToolMT::decode(const EventContext&, const std::vector<uint32_t>& ) const {
334 ATH_MSG_FATAL("ROB based decoding is not supported....");
335 return StatusCode::FAILURE;
336}
337StatusCode Muon::sTgcRdoToPrepDataToolMT::provideEmptyContainer(const EventContext& ctx) const {
338 return setupOutputContainers(ctx).isValid ? StatusCode::SUCCESS : StatusCode::FAILURE;
339}
340
341
343 sTgcRdoToPrepDataToolMT::setupOutputContainers(const EventContext& ctx) const {
344 outputCache containers;
345 if (!m_xAODStripKey.empty()) {
347 if (!containers.strip.record(std::make_unique<xAOD::sTgcStripContainer>(),
348 std::make_unique<xAOD::sTgcStripAuxContainer>()).isSuccess()){
349 ATH_MSG_FATAL("Failed to record "<<m_xAODStripKey.fullKey());
350 return containers;
351 }
352 }
353 if (!m_xAODPadKey.empty()) {
355 if (!containers.pad.record(std::make_unique<xAOD::sTgcPadContainer>(),
356 std::make_unique<xAOD::sTgcPadAuxContainer>()).isSuccess()){
357 ATH_MSG_FATAL("Failed to record "<<m_xAODPadKey.fullKey());
358 return containers;
359 }
360 }
361 if (!m_xAODWireKey.empty()) {
363 if (!containers.wire.record(std::make_unique<xAOD::sTgcWireContainer>(),
364 std::make_unique<xAOD::sTgcWireAuxContainer>()).isSuccess()){
365 ATH_MSG_FATAL("Failed to record "<<m_xAODWireKey.fullKey());
366 return containers;
367 }
368 }
369
371 if(m_prdContainerCacheKey.key().empty()) {
372 // No external cache, just record the container
373 const int hashMax = m_idHelperSvc->stgcIdHelper().module_hash_max();
374 if (!containers.prd.record(std::make_unique<Muon::sTgcPrepDataContainer>(hashMax)).isSuccess()){
375 ATH_MSG_FATAL("Faile to record "<<m_stgcPrepDataContainerKey.fullKey());
376 return containers;
377 }
378 } else {
381 if (!update.isValid()) {
382 ATH_MSG_FATAL("Invalid UpdateHandle " << m_prdContainerCacheKey.key());
383 return containers;
384 }
385 if (!containers.prd.record(std::make_unique<Muon::sTgcPrepDataContainer>(update.ptr())).isSuccess()) {
386 ATH_MSG_FATAL("Failed to record "<<m_stgcPrepDataContainerKey.fullKey()
387 <<" from "<<m_prdContainerCacheKey.fullKey());
388 return containers;
389 }
390 }
391 containers.isValid = true;
392 return containers;
393}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
const double width
size_type size() const noexcept
Returns the number of elements in the collection.
This class saves the "context" of an expanded identifier (ExpandedIdentifier) for compact or hash ver...
Definition IdContext.h:26
bool OnlineAndPresentInAnotherView()
This method is to avoid calling an expensive operation in the offline case.
StatusCode addOrDelete(std::unique_ptr< T > ptr)
This is a "hash" representation of an Identifier.
value_type get_compact() const
Get the compact id.
The MuonDetectorManager stores the transient representation of the Muon Spectrometer geometry and pro...
const sTgcReadoutElement * getsTgcReadoutElement(const Identifier &id) const
access via extended identifier (requires unpacking)
An sTgcReadoutElement corresponds to a single STGC module; therefore typicaly a barrel muon station c...
const MuonPadDesign * getPadDesign(const Identifier &id) const
returns the MuonChannelDesign class for the given identifier
virtual bool stripPosition(const Identifier &id, Amg::Vector2D &pos) const override final
strip position - should be renamed to channel position If the strip number is outside the range of va...
const MuonChannelDesign * getDesign(const Identifier &id) const
returns the MuonChannelDesign class for the given identifier
virtual int get_id(const IdentifierHash &hash_id, Identifier &id, const IdContext *context=0) const override
Create compact id from hash id (return == 0 for OK)
IdContext module_context() const
id for module
const IdentifierHash & identifyHash() const
Class to represent sTgc measurements.
StatusCode provideEmptyContainer(const EventContext &ctx) const override
StatusCode processCollection(const EventContext &ctx, outputCache &xAODcontainers, const STGC_RawDataCollection *rdoColl) const
ToolHandle< INSWCalibTool > m_calibTool
ToolHandle< ISTgcClusterBuilderTool > m_clusterBuilderTool
SG::WriteHandleKey< sTgcPrepDataContainer > m_stgcPrepDataContainerKey
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
SG::ReadHandleKey< STGC_RawDataContainer > m_rdoContainerKey
SG::WriteHandleKey< xAOD::sTgcWireContainer > m_xAODWireKey
SG::UpdateHandleKey< sTgcPrepDataCollection_Cache > m_prdContainerCacheKey
This is the key for the cache for the sTGC PRD containers, can be empty.
const MuonGMR4::MuonDetectorManager * m_detMgrR4
outputCache setupOutputContainers(const EventContext &ctx) const
SG::WriteHandleKey< xAOD::sTgcStripContainer > m_xAODStripKey
SG::WriteHandleKey< xAOD::sTgcPadContainer > m_xAODPadKey
StatusCode decode(const EventContext &ctx, const std::vector< IdentifierHash > &idVect) const override
Decode RDO to PRD A vector of IdentifierHash are passed in, and the data corresponding to this list...
void processRDOContainer(const EventContext &ctx, outputCache &xAODcontainers, const std::vector< IdentifierHash > &idsToDecode) const
const STGC_RawDataContainer * getRdoContainer(const EventContext &ctx) const
virtual StatusCode initialize() override
Standard AthAlgTool initialize method.
SG::ReadCondHandleKey< MuonGM::MuonDetectorManager > m_muDetMgrKey
int channelType(const Identifier &id) const
int channel(const Identifier &id) const override
int gasGap(const Identifier &id) const override
get the hashes
void setMeasurement(const DetectorIDHashType idHash, MeasVector< N > locPos, MeasMatrix< N > locCov)
Sets IdentifierHash, local position and local covariance of the measurement.
void setIdentifier(const DetectorIdentType measId)
Sets the full Identifier of the measurement.
void setTime(short int t)
: Set the calibrated time of the wire measurement
void setAuthor(Author a)
Set the author of the producing algorithm.
void setCharge(int q)
: Set the collected charge on the wire
void setChannelNumber(uint16_t channel)
Set the channel number of the measurement.
void setReadoutElement(const MuonGMR4::sTgcReadoutElement *readoutEle)
set the pointer to the sTgcReadoutElement
void setGasGap(uint8_t gap)
Set the associated gas gap of the measurement.
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Dynamic Matrix - dynamic allocation.
Eigen::Matrix< double, 2, 1 > Vector2D
Ensure that the Athena extensions are properly loaded.
Definition GeoMuonHits.h:27
NRpcCablingAlg reads raw condition data and writes derived condition data to the condition store.
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Ensure that the ATLAS eigen extensions are properly loaded.
Eigen::Matrix< float, N, N > MeasMatrix
Eigen::Matrix< float, N, 1 > MeasVector
Abrivation of the Matrix & Covariance definitions.
MeasVector< N > toStorage(const AmgVector(N)&amgVec)
Converts the double precision of the AmgVector into the floating point storage precision of the MeasV...
sTgcMeasurement_v1 sTgcMeasurement
double channelWidth() const
calculate local channel width
Parameters defining the design of the readout sTGC pads.
double channelWidth(const Amg::Vector2D &pos, bool measPhi, bool preciseMeas=false) const
calculate local channel width
SG::WriteHandle< xAOD::sTgcWireContainer > wire
SG::WriteHandle< Muon::sTgcPrepDataContainer > prd
SG::WriteHandle< xAOD::sTgcStripContainer > strip
SG::WriteHandle< xAOD::sTgcPadContainer > pad