ATLAS Offline Software
Loading...
Searching...
No Matches
sTgcRdoToPrepDataToolMT.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
9
12
13using namespace MuonGM;
14using namespace Trk;
15
16namespace {
17 std::atomic<bool> hitNegativeCharge{false};
18}
19
20
21namespace Muon{
22
23
25 const PrdKey_t& key,
26 const EventContext& ctx):
27 prdWriteHandle{key, ctx}{
28
29 collections.resize(hashMax);
30
31}
32
34 if (!detMgr) {
35 return;
36 }
37 std::sort(coll.begin(), coll.end(),[](const sTgcPrepData*a, const sTgcPrepData* b){
38 return a->identify() < b->identify();
39 });
41 std::size_t idx{0};
42 for (sTgcPrepData* prd : coll) {
43 prd->setHashAndIndex(coll.identifyHash(), idx++);
44 }
45 const IMuonIdHelperSvc* idHelperSvc = detMgr->idHelperSvc();
46
47 const sTgcIdHelper& id_helper = idHelperSvc->stgcIdHelper();
48 for (const sTgcPrepData* prd : coll) {
49 const Identifier prdId = prd->identify();
50 const int gasGap = id_helper.gasGap(prdId);
51 const int channel = id_helper.channel(prdId);
52 const int chType = id_helper.channelType(prdId);
53 xAOD::sTgcMeasurement* outHit{nullptr};
55 if (!pads.hasHandle()) {
56 continue;
57 }
59 lCov(1,1) = prd->localCovariance()(0,0);
61 const MuonGMR4::sTgcReadoutElement* readoutEle = detMgr->getsTgcReadoutElement(prdId);
62 lCov(0,0) = Acts::square(0.5* readoutEle->padHeight(readoutEle->measurementHash(prdId)));
63
64 //skip for now measurements with zero covariance
65 if (lCov.determinant() == 0) {
66 continue;
67 }
68 outHit = pads->push_back(std::make_unique<xAOD::sTgcPadHit>());
69 outHit->setMeasurement<2>(idHelperSvc->detElementHash(prdId),
70 xAOD::toStorage(Eigen::Rotation2D{-M_PI_2}*prd->localPosition()),
71 std::move(lCov));
73 if (!wires.hasHandle()){
74 continue;
75 }
76 outHit = wires->push_back(std::make_unique<xAOD::sTgcWireHit>());
78 if (!strips.hasHandle()) {
79 continue;
80 }
81 auto stripHit = strips->push_back(std::make_unique<xAOD::sTgcStripCluster>());
82 stripHit->setStripCharges(prd->stripCharges());
83 stripHit->setStripNumbers(prd->stripNumbers());
84 stripHit->setStripTimes(prd->stripTimes());
85 outHit = stripHit;
86 }
87 if (!outHit) {
88 continue;
89 }
93 const double locPos = (chType == sTgcIdHelper::sTgcChannelTypes::Wire ? -1. : 1.) * prd->localPosition().x();
96 lCov(0,0) = prd->localCovariance()(0,0);
97 outHit->setMeasurement<1>(idHelperSvc->detElementHash(prdId),
98 std::move(lPos),
99 std::move(lCov));
100
101 }
102 outHit->setChannelNumber(channel);
103 outHit->setGasGap(gasGap);
104 outHit->setAuthor(prd->author());
105 outHit->setTime(prd->time());
106 outHit->setCharge(prd->charge());
107 outHit->setReadoutElement(detMgr->getsTgcReadoutElement(prdId));
108 }
109}
111 if (!isValid) {
112 return;
113 }
114
115 for (std::size_t hash = 0; hash < collections.size(); ++hash) {
116 std::unique_ptr<sTgcPrepDataCollection>& coll = collections[hash];
117 if (!coll) {
118 continue;
119 }
121 if (lock.OnlineAndPresentInAnotherView()){
122 continue;
123 }
124
125 translateAndSort(*coll);
126
127 lock.addOrDelete(std::move(coll)).ignore();
128 }
129}
130//============================================================================
132{
133 ATH_MSG_DEBUG(" in initialize()");
134 ATH_CHECK( m_idHelperSvc.retrieve() );
135 // check if the initialization of the data container is success
137 ATH_CHECK(m_rdoContainerKey.initialize());
138 ATH_CHECK(m_muDetMgrKey.initialize());
139 ATH_CHECK(m_calibTool.retrieve());
140 ATH_CHECK(m_prdContainerCacheKey.initialize(!m_prdContainerCacheKey.key().empty()) );
141
142 if (m_useNewGeo) {
143 ATH_CHECK(detStore()->retrieve(m_detMgrR4));
144 }
145
146 ATH_CHECK(m_xAODStripKey.initialize(!m_xAODStripKey.empty()));
147 ATH_CHECK(m_xAODWireKey.initialize(!m_xAODWireKey.empty()));
148 ATH_CHECK(m_xAODPadKey.initialize(!m_xAODPadKey.empty()));
149 return StatusCode::SUCCESS;
150}
151
152
153//============================================================================
154StatusCode sTgcRdoToPrepDataToolMT::processCollection(const EventContext& ctx,
156 const STGC_RawDataCollection *rdoColl) const {
157
158 const sTgcIdHelper& id_helper = m_idHelperSvc->stgcIdHelper();
159 const IdentifierHash hash = rdoColl->identifyHash();
160
161 ATH_MSG_DEBUG(" ***************** Start of process STGC Collection with hash Id: " << hash);
162
163 // check if the collection already exists, otherwise add it
164 if ( cache.prdWriteHandle->indexFindPtr(hash) != nullptr ) {
165 ATH_MSG_DEBUG("In processCollection: collection already contained in the sTGC PrepData container");
166 return StatusCode::FAILURE;
167
168 }
169
170 // Get write handle for this collection
171 sTgcPrepDataContainer::IDC_WriteHandle lock = cache.prdWriteHandle->getWriteHandle( hash );
172 // Check if collection already exists (via the cache, i.e. in online trigger mode)
173 if( lock.OnlineAndPresentInAnotherView() ) {
174 ATH_MSG_DEBUG("In processCollection: collection already available in the sTgc PrepData container (via cache)");
175 return StatusCode::SUCCESS;
176 }
177
178 // Make the PRD collection (will be added to container later
179 std::unique_ptr<sTgcPrepDataCollection>& prdColl = cache.collections[hash];
180 if (!prdColl) {
181 prdColl = std::make_unique<sTgcPrepDataCollection>(hash);
182 }
183
184 // set the offline identifier of the collection Id
185 IdContext context = id_helper.module_context();
186 Identifier moduleId;
187 int getId = id_helper.get_id(hash, moduleId, &context);
188 if ( getId != 0 ) {
189 ATH_MSG_ERROR("Could not convert the hash Id: " << hash << " to identifier");
190 } else {
191 prdColl->setIdentifier(moduleId);
192 }
193
194 // vectors to hold PRDs decoded for this RDO collection
195 std::vector<sTgcPrepData> sTgcStripPrds;
196 std::vector<sTgcPrepData> sTgcWirePrds;
197 std::vector<sTgcPrepData> sTgcPadPrds;
198 sTgcStripPrds.reserve(rdoColl->size());
199 sTgcPadPrds.reserve(rdoColl->size());
200 sTgcWirePrds.reserve(rdoColl->size());
201
202 // Count hits with negative charge, which indicates bad calibration
203
204
205 // MuonDetectorManager from the conditions store
206 const MuonGM::MuonDetectorManager* muonDetMgr{nullptr};
207 ATH_CHECK(SG::get(muonDetMgr,m_muDetMgrKey,ctx));
208 // convert the RDO collection to a PRD collection
209 for ( const STGC_RawData* rdo : * rdoColl) {
210
211 ATH_MSG_DEBUG("Adding a new sTgc PrepRawData");
212
213 const Identifier rdoId = rdo->identify();
214
215 std::vector<Identifier> rdoList{rdoId};
216
217 // get the local and global positions
218 const MuonGM::sTgcReadoutElement* detEl = muonDetMgr->getsTgcReadoutElement(rdoId);
219 Amg::Vector2D localPos{Amg::Vector2D::Zero()};
220
221 int channelType = id_helper.channelType(rdoId);
222 if (channelType < 0 || channelType > 2) {
223 ATH_MSG_ERROR("Unknown sTGC channel type");
224 return StatusCode::FAILURE;
225 }
226 if (!detEl->stripPosition(rdoId, localPos)) {
227 ATH_MSG_ERROR("Could not get the local strip position for "<<m_idHelperSvc->toString(rdoId));
228 return StatusCode::FAILURE;
229 }
230
231 // get the resolution from strip width
232 // to be fixed: for now do not set the resolution, it will be added in the next update
233 const int gasGap = id_helper.gasGap(rdoId);
234 const int channel = id_helper.channel(rdoId);
235
236 NSWCalib::CalibratedStrip calibStrip;
237 ATH_CHECK (m_calibTool->calibrateStrip(ctx, rdo, calibStrip));
238 int calibratedCharge = static_cast<int>(calibStrip.charge);
239 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.
240 if (!hitNegativeCharge) {
241 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");
242 hitNegativeCharge = true;
243 }
244 continue;
245 }
246
247 double width{0.};
248 if (channelType == sTgcIdHelper::sTgcChannelTypes::Pad) { // Pads
249 const MuonGM::MuonPadDesign* design = detEl->getPadDesign(rdoId);
250 if (!design) {
251 ATH_MSG_WARNING("Failed to get design for sTGC pad" );
252 } else {
253 width = design->channelWidth(localPos, true);
254 }
255 } else { // Strips and wires
256 const MuonGM::MuonChannelDesign* design = detEl->getDesign(rdoId);
257 if (!design) {
258 ATH_MSG_WARNING("Failed to get design for sTGC strip/wire" );
259 } else {
260 width = design->channelWidth();
261 }
262 }
263
264 const double resolution = width/ std::sqrt(12.);
265 auto cov = Amg::MatrixX(1,1);
266 cov.setIdentity();
267 (cov)(0,0) = resolution*resolution;
268
269 ATH_MSG_DEBUG("Adding a new STGC PRD, gasGap: " << gasGap << " channel: " << channel << " type: " << channelType << " resolution " << resolution );
270
271 if(m_merge) {
272 std::vector<sTgcPrepData>& sTgcPrds = channelType == sTgcIdHelper::Pad ? sTgcPadPrds :
273 (channelType == sTgcIdHelper::Strip ? sTgcStripPrds : sTgcWirePrds);
274
275 // check if the same RdoId is already present; keep the one with the smallest time
276 auto it = std::find_if(sTgcPrds.begin(), sTgcPrds.end(), [&rdoId](const sTgcPrepData& prd) {
277 return (prd.identify() == rdoId);
278 });
279 if (it == sTgcPrds.end()) {
280 sTgcPrds.emplace_back(rdoId, hash, std::move(localPos), std::move(rdoList), std::move(cov), detEl, calibratedCharge, calibStrip.time);
281 sTgcPrds.back().setAuthor(sTgcPrepData::Author::RdoToPrdConverter);
282 } else if (it->time() > calibStrip.time) {
283 *it = sTgcPrepData(rdoId, hash, std::move(localPos), std::move(rdoList), std::move(cov), detEl, calibratedCharge, calibStrip.time);
285 }
286
287 // TODO - add merging for xAOD
288
289 } else {
290 // if not merging just add the PRD to the collection
291 prdColl->push_back(std::make_unique<sTgcPrepData>(rdoId,
292 hash,
293 std::move(localPos),
294 std::move(rdoList),
295 std::move(cov),
296 detEl,
297 calibratedCharge,
298 calibStrip.time));
299 }
300 }
301
302 if(m_merge) {
303 // merge strip prds that fire closeby channels (not clusterizing wires and pads)
304 std::vector<std::unique_ptr<sTgcPrepData>> sTgcStripClusters;
305 ATH_CHECK(m_clusterBuilderTool->getClusters(ctx, std::move(sTgcStripPrds), sTgcStripClusters)); // Clusterize strips
306
307 for ( std::unique_ptr<sTgcPrepData>& it : sTgcStripClusters ) {
308 it->setHashAndIndex(prdColl->identifyHash(), prdColl->size());
309 prdColl->push_back(std::move(it));
310 }
311 for ( sTgcPrepData& prd : sTgcWirePrds ) {
312 prd.setHashAndIndex(prdColl->identifyHash(), prdColl->size());
313 prdColl->push_back(std::make_unique<sTgcPrepData>(std::move(prd)));
314 }
315 for (sTgcPrepData& prd : sTgcPadPrds ) {
316 prd.setHashAndIndex(prdColl->identifyHash(), prdColl->size());
317 prdColl->push_back(std::make_unique<sTgcPrepData>(std::move(prd)));
318 }
319 }
320
321 // now add the collection to the container
322 ATH_MSG_DEBUG("PRD hash " << hash << " has been moved to container");
323
324 return StatusCode::SUCCESS;
325}
326
327
328//============================================================================
330{
331 auto rdoContainerHandle = SG::makeHandle(m_rdoContainerKey, ctx);
332 if(rdoContainerHandle.isValid()) {
333 ATH_MSG_DEBUG("STGC_getRdoContainer success");
334 return rdoContainerHandle.cptr();
335 }
336 ATH_MSG_WARNING("Retrieval of STGC_RawDataContainer failed !");
337
338 return nullptr;
339}
340
341
342//============================================================================
345 const std::vector<IdentifierHash>& idsToDecode) const
346{
347 ATH_MSG_DEBUG("In processRDOContainer");
348 const STGC_RawDataContainer* rdoContainer = getRdoContainer(ctx);
349 if (!rdoContainer) return;
350
351 // run in unseeded mode
352 for (const STGC_RawDataCollection* rdoColl : *rdoContainer) {
353 if (rdoColl->empty()) continue;
354 ATH_MSG_DEBUG("New RDO collection with " << rdoColl->size() << "STGC Hits");
355
356 const IdentifierHash hash = rdoColl->identifyHash();
357
358 // check if we actually want to decode this RDO collection
359 if(!idsToDecode.empty() and std::find(idsToDecode.begin(), idsToDecode.end(), hash)==idsToDecode.end()) {
360 ATH_MSG_DEBUG("Hash ID " << hash << " not in input list, ignore");
361 continue;
362 } else ATH_MSG_DEBUG("Going to decode " << hash);
363
364 if(processCollection(ctx, cache, rdoColl).isFailure()) {
365 ATH_MSG_DEBUG("processCsm returns a bad StatusCode - keep going for new data collections in this event");
366 }
367 }
368}
369
370// methods for ROB-based decoding
371//============================================================================
372StatusCode sTgcRdoToPrepDataToolMT::decode(const EventContext& ctx,
373 const std::vector<IdentifierHash>& idVect) const {
374 ATH_MSG_DEBUG("Size of the input hash id vector: " << idVect.size());
375
376 DataCache outCache = setupOutputContainers(ctx);
377 if (!outCache.isValid) return StatusCode::FAILURE;
378
379 processRDOContainer(ctx, outCache, idVect);
380 return StatusCode::SUCCESS;
381}
382
383
384//============================================================================
385StatusCode sTgcRdoToPrepDataToolMT::decode(const EventContext&, const std::vector<uint32_t>& ) const {
386 ATH_MSG_FATAL("ROB based decoding is not supported....");
387 return StatusCode::FAILURE;
388}
389StatusCode sTgcRdoToPrepDataToolMT::provideEmptyContainer(const EventContext& ctx) const {
390 return setupOutputContainers(ctx).isValid ? StatusCode::SUCCESS : StatusCode::FAILURE;
391}
392
393
395 sTgcRdoToPrepDataToolMT::setupOutputContainers(const EventContext& ctx) const {
396
397
398 const std::size_t hashMax = m_idHelperSvc->stgcIdHelper().module_hash_max();
399 DataCache containers{hashMax, m_stgcPrepDataContainerKey, ctx};
400
401 containers.detMgr = m_detMgrR4;
402 if(m_prdContainerCacheKey.key().empty()) {
403 // No external cache, just record the container
404
405 if (!containers.prdWriteHandle.record(std::make_unique<sTgcPrepDataContainer>(hashMax)).isSuccess()){
406 ATH_MSG_FATAL("Faile to record "<<m_stgcPrepDataContainerKey.fullKey());
407 return containers;
408 }
409 } else {
412 if (!update.isValid()) {
413 ATH_MSG_FATAL("Invalid UpdateHandle " << m_prdContainerCacheKey.key());
414 return containers;
415 }
416 if (!containers.prdWriteHandle.record(std::make_unique<sTgcPrepDataContainer>(update.ptr())).isSuccess()) {
417 ATH_MSG_FATAL("Failed to record "<<m_stgcPrepDataContainerKey.fullKey()
418 <<" from "<<m_prdContainerCacheKey.fullKey());
419 return containers;
420 }
421 }
422 containers.isValid = containers.strips.record(m_xAODStripKey, ctx).isSuccess()
423 && containers.wires.record(m_xAODWireKey, ctx).isSuccess()
424 && containers.pads.record(m_xAODPadKey, ctx).isSuccess();
425 return containers;
426}
427}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
static Double_t a
const double width
const_iterator end() const noexcept
const_iterator begin() const noexcept
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
This is a "hash" representation of an Identifier.
IdentifierHash measurementHash(const Identifier &measId) const override final
Constructs the identifier hash from the full measurement Identifier.
double padHeight(const IdentifierHash &measHash) const
Returns the height of all the pads that are not adjacent to the bottom edge of the trapezoid active a...
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
Interface for Helper service that creates muon Identifiers and can be used to print Identifiers.
virtual const sTgcIdHelper & stgcIdHelper() const =0
access to TgcIdHelper
virtual IdentifierHash detElementHash(const Identifier &id) const =0
Returns the detector element hash associated to an Identifier.
virtual IdentifierHash identifyHash() const override final
const IdentifierHash & identifyHash() const
Class to represent sTgc measurements.
StatusCode provideEmptyContainer(const EventContext &ctx) const override
void processRDOContainer(const EventContext &ctx, DataCache &xAODcontainers, const std::vector< IdentifierHash > &idsToDecode) const
ToolHandle< INSWCalibTool > m_calibTool
ToolHandle< ISTgcClusterBuilderTool > m_clusterBuilderTool
SG::ReadHandleKey< STGC_RawDataContainer > m_rdoContainerKey
StatusCode processCollection(const EventContext &ctx, DataCache &xAODcontainers, const STGC_RawDataCollection *rdoColl) const
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
ServiceHandle< IMuonIdHelperSvc > m_idHelperSvc
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...
DataCache setupOutputContainers(const EventContext &ctx) const
SG::WriteHandleKey< sTgcPrepDataContainer > PrdKey_t
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
StatusCode record(const SG::WriteHandleKey< Cont_t > &key, const EventContext &ctx)
Record the container to store gate using the passed write handle key.
void setMeasurement(const DetectorIDHashType idHash, MeasVector< N > locPos, MeasMatrix< N > locCov)
Sets IdentifierHash, local position and local covariance of the measurement.
void setChannelNumber(std::uint16_t channel)
Set the channel number 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 setReadoutElement(const MuonGMR4::sTgcReadoutElement *readoutEle)
set the pointer to the sTgcReadoutElement
void setGasGap(std::uint8_t gap)
Set the associated gas gap of the measurement.
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.
MuonPrepDataCollection< sTgcPrepData > sTgcPrepDataCollection
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.
const IIntersectionCache * cache() const
Retrieve the associated cache block, if it exists.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
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
Data cache to hold the translated prd & xAOD containers.
void translateAndSort(sTgcPrepDataCollection &coll)
std::vector< std::unique_ptr< sTgcPrepDataCollection > > collections
DataCache(const std::size_t hashMax, const PrdKey_t &key, const EventContext &ctx)
const MuonGMR4::MuonDetectorManager * detMgr
SG::WriteHandle< sTgcPrepDataContainer > prdWriteHandle