ATLAS Offline Software
Loading...
Searching...
No Matches
GeomAlignCondAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "GeomAlignCondAlg.h"
6
8#include <GeoModelKernel/GeoPerfUtils.h>
9#include <GeoModelKernel/GeoClearAbsPosAction.h>
11
13
14
15#include "Acts/Utilities/Helpers.hpp"
16using namespace MuonGMR4;
17
18namespace MuonR4{
20
26 ATH_CHECK(m_idHelperSvc.retrieve());
27 ATH_CHECK(detStore()->retrieve(m_detMgr));
28 m_techs = m_detMgr->getDetectorTypes();
29 if (m_techs.empty()) {
30 ATH_MSG_FATAL("The detector manager does not contain any elements");
31 return StatusCode::FAILURE;
32 }
33 auto hasDetector = [this](const ActsTrk::DetectorType d) -> bool {
34 return Acts::rangeContainsValue(m_techs, d);
35 };
37 hasDetector(ActsTrk::DetectorType::Mm) ||
38 hasDetector(ActsTrk::DetectorType::sTgc));
41 hasDetector(ActsTrk::DetectorType::sTgc));
43
44
45
46 for (const ActsTrk::DetectorType det : m_techs) {
47 m_writeKeys.emplace_back(std::format("{:}{:}", det, m_keyToken.value()));
48 ATH_MSG_INFO("Register new alignment container "<<m_writeKeys.back().fullKey());
49 }
50 ATH_MSG_INFO("Switched options "<<m_fillAlignStoreCache<<", "<<", "<<m_applyALines<<", "
51 <<m_applyBLines<<", "<<m_applyMdtAsBuilt<<", "
53 ATH_CHECK(m_writeKeys.initialize());
54 return StatusCode::SUCCESS;
55}
56
58 if (re->detectorType() == ActsTrk::DetectorType::Mdt ||
59 re->detectorType() == ActsTrk::DetectorType::Tgc) {
60 return m_idHelperSvc->chamberId(re->identify());
61 } else if (re->detectorType() == ActsTrk::DetectorType::Rpc) {
63 if (!m_idHelperSvc->hasMDT() ||
64 (std::abs(re->stationEta()) == 7 && m_idHelperSvc->stationNameString(re->identify()) == "BML")) {
65 return m_idHelperSvc->rpcIdHelper().elementID(re->stationName(), re->stationEta(), re->stationPhi(), 1);
67 } else {
68 return m_idHelperSvc->mdtIdHelper().elementID(re->stationName(), re->stationEta(), re->stationPhi());
69 }
70 }
72 return re->identify();
73}
74StatusCode GeomAlignCondAlg::loadDeltas(const EventContext& ctx,
75 deltaMap& alignDeltas,
76 alignTechMap& techTransforms) const {
77 if (m_readKeyALines.empty()) {
78 ATH_MSG_DEBUG("Loading of the A line parameters deactivated");
79 return StatusCode::SUCCESS;
80 }
81 const ALineContainer* aLineContainer{};
82 ATH_CHECK(SG::get(aLineContainer, m_readKeyALines, ctx));
83 std::vector<const MuonReadoutElement*> readoutEles = m_detMgr->getAllReadoutElements();
84 ATH_MSG_INFO("Load the alignment of "<<readoutEles.size()<<" detector elements");
85 for (const MuonReadoutElement* re : readoutEles) {
86 const GeoAlignableTransform* alignTrans = re->alignableTransform();
87 if (!alignTrans) {
88 ATH_MSG_WARNING("The readout element "<<m_idHelperSvc->toStringDetEl(re->identify())
89 <<" has no alignable transform.");
90 continue;
91 }
92 std::shared_ptr<const Amg::Transform3D>& cached = alignDeltas[alignTrans];
93 if (cached) {
94 ATH_MSG_DEBUG("The alignable transformation for "<<m_idHelperSvc->toStringChamber(re->identify())
95 <<" has been cached before. ");
96 techTransforms[re->detectorType()].insert(alignTrans);
97 continue;
98 }
100 const Identifier stationId = alignmentId(re);
101 ALineContainer::const_iterator aLineItr = aLineContainer->find(stationId);
102 if (aLineItr == aLineContainer->end()) {
103 ATH_MSG_VERBOSE("No Alines were stored for "<<m_idHelperSvc->toString(re->identify())
104 <<". Used "<<m_idHelperSvc->toString(stationId)<<" as station Identifier");
105 continue;
106 }
108 cached = std::make_shared<Amg::Transform3D>(aLineItr->delta());
109 techTransforms[re->detectorType()].insert(alignTrans);
110 }
111 return StatusCode::SUCCESS;
112}
113StatusCode GeomAlignCondAlg::loadMdtDeformPars(const EventContext& ctx,
114 ActsTrk::DetectorAlignStore& store) const {
115
117 return StatusCode::SUCCESS;
118 }
119 auto internAlign = std::make_unique<MdtAlignmentStore>(m_idHelperSvc.get());
120 const MdtAsBuiltContainer* asBuiltCont{nullptr};
121 const BLineContainer* bLines{nullptr};
122
123 ATH_CHECK(SG::get(asBuiltCont, m_readMdtAsBuiltKey, ctx));
124 ATH_CHECK(SG::get(bLines, m_readKeyBLines, ctx));
125
126 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
127 for (auto itr = idHelper.module_begin(); itr != idHelper.module_end(); ++itr) {
128 const Identifier& stationId{*itr};
129 const BLinePar* bline{nullptr};
130 if (bLines) {
131 BLineContainer::const_iterator itr = bLines->find(stationId);
132 if (itr != bLines->end()) bline = &(*itr);
133 }
134 const MdtAsBuiltPar* asBuilt{nullptr};
135 if (asBuiltCont) {
136 MdtAsBuiltContainer::const_iterator itr = asBuiltCont->find(stationId);
137 if (itr != asBuiltCont->end()) asBuilt = &(*itr);
138 }
139 if (asBuilt || bline) {
140 internAlign->storeDistortion(stationId, bline, asBuilt);
141 }
142 }
143 // Down cast the alignment pointer
144 store.internalAlignment = std::move(internAlign);
145 return StatusCode::SUCCESS;
146}
147StatusCode GeomAlignCondAlg::loadMmDeformPars(const EventContext& ctx,
148 ActsTrk::DetectorAlignStore& store) const {
150 return StatusCode::SUCCESS;
151 }
152 auto internAlign = std::make_unique<MmAlignmentStore>();
153 const NswAsBuiltDbData* asBuiltPars{};
154 ATH_CHECK(SG::get(internAlign->passivation, m_readNswPassivKey, ctx));
155 ATH_CHECK(SG::get(asBuiltPars, m_readNswAsBuiltKey, ctx));
156 if (asBuiltPars) {
157 internAlign->asBuiltPars = asBuiltPars->microMegaData;
158 }
159 const BLineContainer* bLines{};
160 ATH_CHECK(SG::get(bLines, m_readKeyBLines, ctx));
161 if (bLines) {
162 for (const MmReadoutElement* re : m_detMgr->getAllMmReadoutElements()){
163 const Identifier stationId = alignmentId(re);
164 BLineContainer::const_iterator itr = bLines->find(stationId);
165 if (itr != bLines->end()) {
166 internAlign->cacheBLine(re->identify(), *itr);
167 }
168 }
169 }
170 store.internalAlignment = std::move(internAlign);
171 return StatusCode::SUCCESS;
172}
173StatusCode GeomAlignCondAlg::loadStgcDeformPars(const EventContext& ctx,
174 ActsTrk::DetectorAlignStore& store) const{
176 return StatusCode::SUCCESS;
177 }
178 auto internAlign = std::make_unique<sTgcAlignmentStore>();
179 ATH_CHECK(SG::get(internAlign->asBuiltPars, m_readsTgcAsBuiltKey, ctx));
180 const BLineContainer* bLines{};
181 ATH_CHECK(SG::get(bLines, m_readKeyBLines, ctx));
182 if (bLines) {
183 for (const sTgcReadoutElement* re : m_detMgr->getAllsTgcReadoutElements()) {
184 const Identifier stationId = alignmentId(re);
185 BLineContainer::const_iterator itr = bLines->find(stationId);
186 if (itr != bLines->end()) {
187 internAlign->cacheBLine(re->identify(), *itr);
188 }
189 }
190 }
191 store.internalAlignment = std::move(internAlign);
192 return StatusCode::SUCCESS;
193}
194
195StatusCode GeomAlignCondAlg::declareDependencies(const EventContext& ctx,
196 ActsTrk::DetectorType detType,
199 if (m_applyALines) {
200 SG::ReadCondHandle depHandle{m_readKeyALines, ctx};
201 ATH_CHECK(depHandle.isValid());
202 writeHandle.addDependency(depHandle);
203 }
204 const bool issTGC = detType == ActsTrk::DetectorType::sTgc;
205 const bool isMm = detType == ActsTrk::DetectorType::Mm;
206 const bool isMdt = detType == ActsTrk::DetectorType::Mdt;
207
208 if (m_applyBLines&& (issTGC || isMm || isMdt)) {
209 SG::ReadCondHandle depHandle{m_readKeyBLines, ctx};
210 ATH_CHECK(depHandle.isValid());
211 writeHandle.addDependency(depHandle);
212 }
213 if (m_applyMdtAsBuilt && isMdt) {
215 ATH_CHECK(depHandle.isValid());
216 writeHandle.addDependency(depHandle);
217 }
220 ATH_CHECK(depHandle.isValid());
221 writeHandle.addDependency(depHandle);
222 }
223 if (m_applyNswAsBuilt && isMm) {
225 ATH_CHECK(depHandle.isValid());
226 writeHandle.addDependency(depHandle);
227 }
228 if(m_applyNswAsBuilt && issTGC && !m_readsTgcAsBuiltKey.empty()){
230 ATH_CHECK(depHandle.isValid());
231 writeHandle.addDependency(depHandle);
232 }
233 return StatusCode::SUCCESS;
234}
235
236StatusCode GeomAlignCondAlg::execute(const EventContext& ctx) const {
237 deltaMap alignDeltas{};
238 alignTechMap techTransforms{};
239 const unsigned memBeforeAlign = GeoPerfUtils::getMem();
240 ATH_CHECK(loadDeltas(ctx, alignDeltas, techTransforms));
241
243 for (std::size_t det =0 ; det < m_techs.size(); ++det) {
245 const ActsTrk::DetectorType subDet = m_techs[det];
246
248 ActsTrk::DetectorAlignStore::Mode::Block :
249 ActsTrk::DetectorAlignStore::Mode::LazyFill};
250 SG::WriteCondHandle writeHandle{key, ctx};
251 if (writeHandle.isValid()) {
252 ATH_MSG_DEBUG("The alignment constants for "<<subDet <<" are still valid.");
253 continue;
254 }
255 auto writeCdo = std::make_unique<ActsTrk::DetectorAlignStore>(subDet, mode);
256
257 const std::set<const GeoAlignableTransform*>& toStore = techTransforms[subDet];
259 for (const GeoAlignableTransform* alignable : toStore) {
260 const std::shared_ptr<const Amg::Transform3D>& cached = alignDeltas[alignable];
261 if (!cached) continue;
262 writeCdo->geoModelAlignment->setDelta(alignable, alignDeltas[alignable]);
263 }
264 writeCdo->geoModelAlignment->lockDelta();
265 if (subDet == ActsTrk::DetectorType::Mdt) {
266 ATH_CHECK(loadMdtDeformPars(ctx,*writeCdo));
267 } else if (subDet == ActsTrk::DetectorType::Mm) {
268 ATH_CHECK(loadMmDeformPars(ctx, *writeCdo));
269 } else if (subDet == ActsTrk::DetectorType::sTgc) {
270 ATH_CHECK(loadStgcDeformPars(ctx, *writeCdo));
271 }
273 ATH_CHECK(declareDependencies(ctx, subDet, writeHandle));
276 unsigned numAligned{0};
277 std::ranges::for_each(m_detMgr->getAllReadoutElements(subDet),
278 [&](const MuonGMR4::MuonReadoutElement* re) {
279 numAligned += re->storeAlignedTransforms(*writeCdo);
280 });
281 std::ranges::for_each(m_detMgr->getAllChambers(),
282 [&](const MuonGMR4::Chamber* chamber){
283 const auto& placement = chamber->parameters().placement;
284 if (placement) {
285 numAligned+= placement->storeAlignedTransforms(*writeCdo);
286 }
287 });
289 writeCdo->geoModelAlignment.reset();
290 ATH_MSG_DEBUG("Populated the alignment store "<<subDet<<" with "<<numAligned<<" transforms");
291 if (writeCdo->trackingAlignment->filled() != writeCdo->trackingAlignment->size()) {
292 ATH_MSG_ERROR("The number of aligned transforms "<<(*writeCdo->trackingAlignment)
293 <<" does not match what's reported from the readout geometry "
294 <<writeCdo->trackingAlignment->filled());
295 return StatusCode::FAILURE;
296 }
297 } else if (m_fillGeoAlignStore) {
299 std::ranges::for_each(m_detMgr->getAllReadoutElements(subDet),
300 [&](const MuonReadoutElement* re){
301 const Amg::Transform3D& detTrf{re->getMaterialGeom()->getAbsoluteTransform(writeCdo->geoModelAlignment.get())};
302 ATH_MSG_VERBOSE("Detector element "<<m_idHelperSvc->toStringDetEl(re->identify())<<" is located at "
303 <<Amg::toString(detTrf));
304 });
306 writeCdo->geoModelAlignment->getDeltas()->clear();
307 writeCdo->geoModelAlignment->lockPosCache();
308 }
309 ATH_CHECK(writeHandle.record(std::move(writeCdo)));
310 }
311
312 alignDeltas.clear();
313 techTransforms.clear();
315 GeoClearAbsPosAction whipeTreeTop{};
316 for (unsigned treeTop = 0 ; treeTop < m_detMgr->getNumTreeTops(); ++treeTop) {
317 m_detMgr->getTreeTop(treeTop)->exec(&whipeTreeTop);
318 }
319
320 const unsigned memAfterAlign = GeoPerfUtils::getMem();
321 ATH_MSG_INFO("Caching of the alignment parameters required "<<(memAfterAlign - memBeforeAlign) / 1024<<" MB of memory");
322 return StatusCode::SUCCESS;
323}
324}
const std::regex re(r_e)
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::set< ALinePar, std::less<> > ALineContainer
std::set< MdtAsBuiltPar, std::less<> > MdtAsBuiltContainer
std::set< BLinePar, std::less<> > BLineContainer
detail::TransformStore::Mode Mode
const ServiceHandle< StoreGateSvc > & detStore() const
static EventIDRange infiniteTime()
Produces an EventIDRange that is inifinite in Time and invalid in RunLumi.
Container classifier the MDT as-built parameters See parameter description in http://atlas-muon-align...
Chamber represent the volume enclosing a muon station.
Definition Chamber.h:29
MuonReadoutElement is an abstract class representing the geometry of a muon detector.
const_id_iterator module_end() const
const_id_iterator module_begin() const
Iterators over full set of ids.
std::unordered_map< const GeoAlignableTransform *, std::shared_ptr< const Amg::Transform3D > > deltaMap
Association map of the GeoAlignableTransforms with the rigid alignment transformations.
StatusCode loadMmDeformPars(const EventContext &ctx, ActsTrk::DetectorAlignStore &store) const
Gaudi::Property< bool > m_applyALines
Apply translations and rotations to align the Muon stations.
const MuonGMR4::MuonDetectorManager * m_detMgr
SG::ReadCondHandleKey< MdtAsBuiltContainer > m_readMdtAsBuiltKey
StatusCode loadMdtDeformPars(const EventContext &ctx, ActsTrk::DetectorAlignStore &store) const
Loads the BLine container and the Mdt-as built parameters from the Conditions store and stores them i...
SG::ReadCondHandleKey< sTGCAsBuiltData > m_readsTgcAsBuiltKey
SG::ReadCondHandleKey< BLineContainer > m_readKeyBLines
Gaudi::Property< bool > m_applyMmPassivation
Gaudi::Property< bool > m_applyMdtAsBuilt
virtual StatusCode initialize() override
std::vector< ActsTrk::DetectorType > m_techs
std::map< ActsTrk::DetectorType, std::set< const GeoAlignableTransform * > > alignTechMap
Association map of the GeoAlignable transforms with the detector technologies.
Gaudi::Property< bool > m_applyBLines
Apply the chamber deformation model (Mdts + Nsw).
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
SG::ReadCondHandleKey< NswPassivationDbData > m_readNswPassivKey
Identifier alignmentId(const MuonGMR4::MuonReadoutElement *reElement) const
Returns the Identifier serving as key to find the alignment parameters connected with the readout ele...
SG::ReadCondHandleKey< NswAsBuiltDbData > m_readNswAsBuiltKey
SG::WriteCondHandleKeyArray< ActsTrk::DetectorAlignStore > m_writeKeys
StatusCode loadDeltas(const EventContext &ctx, deltaMap &alignDeltas, alignTechMap &techTransforms) const
Loads the ALineContainer from the conditions store and fills the deltaMap with the A-Line delta trans...
Gaudi::Property< std::string > m_keyToken
Gaudi::Property< bool > m_applyNswAsBuilt
SG::ReadCondHandleKey< ALineContainer > m_readKeyALines
virtual StatusCode execute(const EventContext &ctx) const override
Gaudi::Property< bool > m_fillAlignStoreCache
Flag toggling whether the alignment store shall be filled with the transforms or not.
StatusCode declareDependencies(const EventContext &ctx, ActsTrk::DetectorType detType, SG::WriteCondHandle< ActsTrk::DetectorAlignStore > &writeHandle) const
Loads the corresponding ReadCondHandles from the Conditions store and adds their IOVs to the dependen...
StatusCode loadStgcDeformPars(const EventContext &ctx, ActsTrk::DetectorAlignStore &store) const
Gaudi::Property< bool > m_fillGeoAlignStore
Flag toggling whether the GeoAlignmentStore shall be filled.
MmAsBuiltPtr microMegaData
void addDependency(const EventIDRange &range)
DetectorType
Simple enum to Identify the Type of the ACTS sub detector.
@ Mm
Maybe not needed in the migration.
@ Tgc
Resitive Plate Chambers.
@ sTgc
Micromegas (NSW).
@ Rpc
Monitored Drift Tubes.
@ Mdt
MuonSpectrometer.
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
The ReadoutGeomCnvAlg converts the Run4 Readout geometry build from the GeoModelXML into the legacy M...
This header ties the generic definitions in this package.
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.