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
12
13#include "Acts/Utilities/Helpers.hpp"
14using namespace MuonGMR4;
15
16namespace MuonR4{
18
24 ATH_CHECK(m_idHelperSvc.retrieve());
25 ATH_CHECK(detStore()->retrieve(m_detMgr));
26 m_techs = m_detMgr->getDetectorTypes();
27 if (m_techs.empty()) {
28 ATH_MSG_FATAL("The detector manager does not contain any elements");
29 return StatusCode::FAILURE;
30 }
31 auto hasDetector = [this](const ActsTrk::DetectorType d) -> bool {
32 return Acts::rangeContainsValue(m_techs, d);
33 };
35 hasDetector(ActsTrk::DetectorType::Mm) ||
36 hasDetector(ActsTrk::DetectorType::sTgc));
39 hasDetector(ActsTrk::DetectorType::sTgc));
41
42
43
44 for (const ActsTrk::DetectorType det : m_techs) {
45 m_writeKeys.emplace_back(ActsTrk::to_string(det) + m_keyToken);
46 ATH_MSG_INFO("Register new alignment container "<<m_writeKeys.back().fullKey());
47 }
48 ATH_MSG_INFO("Switched options "<<m_fillAlignStoreCache<<", "<<", "<<m_applyALines<<", "
49 <<m_applyBLines<<", "<<m_applyMdtAsBuilt<<", "
51 ATH_CHECK(m_writeKeys.initialize());
52 return StatusCode::SUCCESS;
53}
54
56 if (re->detectorType() == ActsTrk::DetectorType::Mdt ||
57 re->detectorType() == ActsTrk::DetectorType::Tgc) {
58 return m_idHelperSvc->chamberId(re->identify());
59 } else if (re->detectorType() == ActsTrk::DetectorType::Rpc) {
61 if (!m_idHelperSvc->hasMDT() ||
62 (std::abs(re->stationEta()) == 7 && m_idHelperSvc->stationNameString(re->identify()) == "BML")) {
63 return m_idHelperSvc->rpcIdHelper().elementID(re->stationName(), re->stationEta(), re->stationPhi(), 1);
65 } else {
66 return m_idHelperSvc->mdtIdHelper().elementID(re->stationName(), re->stationEta(), re->stationPhi());
67 }
68 }
70 return re->identify();
71}
72StatusCode GeomAlignCondAlg::loadDeltas(const EventContext& ctx,
73 deltaMap& alignDeltas,
74 alignTechMap& techTransforms) const {
75 if (m_readKeyALines.empty()) {
76 ATH_MSG_DEBUG("Loading of the A line parameters deactivated");
77 return StatusCode::SUCCESS;
78 }
79 const ALineContainer* aLineContainer{};
80 ATH_CHECK(SG::get(aLineContainer, m_readKeyALines, ctx));
81 std::vector<const MuonReadoutElement*> readoutEles = m_detMgr->getAllReadoutElements();
82 ATH_MSG_INFO("Load the alignment of "<<readoutEles.size()<<" detector elements");
83 for (const MuonReadoutElement* re : readoutEles) {
84 const GeoAlignableTransform* alignTrans = re->alignableTransform();
85 if (!alignTrans) {
86 ATH_MSG_WARNING("The readout element "<<m_idHelperSvc->toStringDetEl(re->identify())
87 <<" has no alignable transform.");
88 continue;
89 }
90 std::shared_ptr<const Amg::Transform3D>& cached = alignDeltas[alignTrans];
91 if (cached) {
92 ATH_MSG_DEBUG("The alignable transformation for "<<m_idHelperSvc->toStringChamber(re->identify())
93 <<" has been cached before. ");
94 techTransforms[re->detectorType()].insert(alignTrans);
95 continue;
96 }
98 const Identifier stationId = alignmentId(re);
99 ALineContainer::const_iterator aLineItr = aLineContainer->find(stationId);
100 if (aLineItr == aLineContainer->end()) {
101 ATH_MSG_VERBOSE("No Alines were stored for "<<m_idHelperSvc->toString(re->identify())
102 <<". Used "<<m_idHelperSvc->toString(stationId)<<" as station Identifier");
103 continue;
104 }
106 cached = std::make_shared<Amg::Transform3D>(aLineItr->delta());
107 techTransforms[re->detectorType()].insert(alignTrans);
108 }
109 return StatusCode::SUCCESS;
110}
111StatusCode GeomAlignCondAlg::loadMdtDeformPars(const EventContext& ctx,
112 ActsTrk::DetectorAlignStore& store) const {
113
115 return StatusCode::SUCCESS;
116 }
117 auto internAlign = std::make_unique<MdtAlignmentStore>(m_idHelperSvc.get());
118 const MdtAsBuiltContainer* asBuiltCont{nullptr};
119 const BLineContainer* bLines{nullptr};
120
121 ATH_CHECK(SG::get(asBuiltCont, m_readMdtAsBuiltKey, ctx));
122 ATH_CHECK(SG::get(bLines, m_readKeyBLines, ctx));
123
124 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
125 for (auto itr = idHelper.module_begin(); itr != idHelper.module_end(); ++itr) {
126 const Identifier& stationId{*itr};
127 const BLinePar* bline{nullptr};
128 if (bLines) {
129 BLineContainer::const_iterator itr = bLines->find(stationId);
130 if (itr != bLines->end()) bline = &(*itr);
131 }
132 const MdtAsBuiltPar* asBuilt{nullptr};
133 if (asBuiltCont) {
134 MdtAsBuiltContainer::const_iterator itr = asBuiltCont->find(stationId);
135 if (itr != asBuiltCont->end()) asBuilt = &(*itr);
136 }
137 if (asBuilt || bline) {
138 internAlign->storeDistortion(stationId, bline, asBuilt);
139 }
140 }
141 // Down cast the alignment pointer
142 store.internalAlignment = std::move(internAlign);
143 return StatusCode::SUCCESS;
144}
145StatusCode GeomAlignCondAlg::loadMmDeformPars(const EventContext& ctx,
146 ActsTrk::DetectorAlignStore& store) const {
148 return StatusCode::SUCCESS;
149 }
150 auto internAlign = std::make_unique<MmAlignmentStore>();
151 const NswAsBuiltDbData* asBuiltPars{};
152 ATH_CHECK(SG::get(internAlign->passivation, m_readNswPassivKey, ctx));
153 ATH_CHECK(SG::get(asBuiltPars, m_readNswAsBuiltKey, ctx));
154 if (asBuiltPars) {
155 internAlign->asBuiltPars = asBuiltPars->microMegaData;
156 }
157 const BLineContainer* bLines{};
158 ATH_CHECK(SG::get(bLines, m_readKeyBLines, ctx));
159 if (bLines) {
160 for (const MmReadoutElement* re : m_detMgr->getAllMmReadoutElements()){
161 const Identifier stationId = alignmentId(re);
162 BLineContainer::const_iterator itr = bLines->find(stationId);
163 if (itr != bLines->end()) {
164 internAlign->cacheBLine(re->identify(), *itr);
165 }
166 }
167 }
168 store.internalAlignment = std::move(internAlign);
169 return StatusCode::SUCCESS;
170}
171StatusCode GeomAlignCondAlg::loadStgcDeformPars(const EventContext& ctx,
172 ActsTrk::DetectorAlignStore& store) const{
174 return StatusCode::SUCCESS;
175 }
176 auto internAlign = std::make_unique<sTgcAlignmentStore>();
177 ATH_CHECK(SG::get(internAlign->asBuiltPars, m_readsTgcAsBuiltKey, ctx));
178 const BLineContainer* bLines{};
179 ATH_CHECK(SG::get(bLines, m_readKeyBLines, ctx));
180 if (bLines) {
181 for (const sTgcReadoutElement* re : m_detMgr->getAllsTgcReadoutElements()) {
182 const Identifier stationId = alignmentId(re);
183 BLineContainer::const_iterator itr = bLines->find(stationId);
184 if (itr != bLines->end()) {
185 internAlign->cacheBLine(re->identify(), *itr);
186 }
187 }
188 }
189 store.internalAlignment = std::move(internAlign);
190 return StatusCode::SUCCESS;
191}
192
193StatusCode GeomAlignCondAlg::declareDependencies(const EventContext& ctx,
194 ActsTrk::DetectorType detType,
197 if (m_applyALines) {
198 SG::ReadCondHandle depHandle{m_readKeyALines, ctx};
199 ATH_CHECK(depHandle.isValid());
200 writeHandle.addDependency(depHandle);
201 }
202 const bool issTGC = detType == ActsTrk::DetectorType::sTgc;
203 const bool isMm = detType == ActsTrk::DetectorType::Mm;
204 const bool isMdt = detType == ActsTrk::DetectorType::Mdt;
205
206 if (m_applyBLines&& (issTGC || isMm || isMdt)) {
207 SG::ReadCondHandle depHandle{m_readKeyBLines, ctx};
208 ATH_CHECK(depHandle.isValid());
209 writeHandle.addDependency(depHandle);
210 }
211 if (m_applyMdtAsBuilt && isMdt) {
213 ATH_CHECK(depHandle.isValid());
214 writeHandle.addDependency(depHandle);
215 }
218 ATH_CHECK(depHandle.isValid());
219 writeHandle.addDependency(depHandle);
220 }
221 if (m_applyNswAsBuilt && isMm) {
223 ATH_CHECK(depHandle.isValid());
224 writeHandle.addDependency(depHandle);
225 }
226 if(m_applyNswAsBuilt && issTGC && !m_readsTgcAsBuiltKey.empty()){
228 ATH_CHECK(depHandle.isValid());
229 writeHandle.addDependency(depHandle);
230 }
231 return StatusCode::SUCCESS;
232}
233
234StatusCode GeomAlignCondAlg::execute(const EventContext& ctx) const {
235 deltaMap alignDeltas{};
236 alignTechMap techTransforms{};
237 const unsigned memBeforeAlign = GeoPerfUtils::getMem();
238 ATH_CHECK(loadDeltas(ctx, alignDeltas, techTransforms));
239
241 for (std::size_t det =0 ; det < m_techs.size(); ++det) {
243 const ActsTrk::DetectorType subDet = m_techs[det];
244
246 if (writeHandle.isValid()) {
247 ATH_MSG_VERBOSE("The alignment constants for "<<ActsTrk::to_string(subDet)
248 <<" are still valid.");
249 continue;
250 }
251 auto writeCdo = std::make_unique<ActsTrk::DetectorAlignStore>(subDet);
252
253 const std::set<const GeoAlignableTransform*>& toStore = techTransforms[subDet];
255 for (const GeoAlignableTransform* alignable : toStore) {
256 const std::shared_ptr<const Amg::Transform3D>& cached = alignDeltas[alignable];
257 if (!cached) continue;
258 writeCdo->geoModelAlignment->setDelta(alignable, alignDeltas[alignable]);
259 }
260 writeCdo->geoModelAlignment->lockDelta();
261 if (subDet == ActsTrk::DetectorType::Mdt) {
262 ATH_CHECK(loadMdtDeformPars(ctx,*writeCdo));
263 } else if (subDet == ActsTrk::DetectorType::Mm) {
264 ATH_CHECK(loadMmDeformPars(ctx, *writeCdo));
265 } else if (subDet == ActsTrk::DetectorType::sTgc) {
266 ATH_CHECK(loadStgcDeformPars(ctx, *writeCdo));
267 }
269 ATH_CHECK(declareDependencies(ctx, subDet, writeHandle));
272 unsigned numAligned{0};
273 std::ranges::for_each(m_detMgr->getAllReadoutElements(subDet),
274 [&](const MuonGMR4::MuonReadoutElement* re) {
275 numAligned += re->storeAlignedTransforms(*writeCdo);
276 });
278 writeCdo->geoModelAlignment.reset();
279 ATH_MSG_DEBUG("Populated the alignment store "<<to_string(subDet)<<" with "<<numAligned<<" transforms");
280 } else if (m_fillGeoAlignStore) {
282 std::ranges::for_each(m_detMgr->getAllReadoutElements(subDet),
283 [&](const MuonReadoutElement* re){
284 const Amg::Transform3D& detTrf{re->getMaterialGeom()->getAbsoluteTransform(writeCdo->geoModelAlignment.get())};
285 ATH_MSG_VERBOSE("Detector element "<<m_idHelperSvc->toStringDetEl(re->identify())<<" is located at "
286 <<Amg::toString(detTrf));
287 });
289 writeCdo->geoModelAlignment->getDeltas()->clear();
290 writeCdo->geoModelAlignment->lockPosCache();
291 }
292 ATH_CHECK(writeHandle.record(std::move(writeCdo)));
293 }
294
295 alignDeltas.clear();
296 techTransforms.clear();
298 GeoClearAbsPosAction whipeTreeTop{};
299 for (unsigned treeTop = 0 ; treeTop < m_detMgr->getNumTreeTops(); ++treeTop) {
300 m_detMgr->getTreeTop(treeTop)->exec(&whipeTreeTop);
301 }
302
303 const unsigned memAfterAlign = GeoPerfUtils::getMem();
304 ATH_MSG_INFO("Caching of the alignment parameters required "<<(memAfterAlign - memBeforeAlign) / 1024<<" MB of memory");
305 return StatusCode::SUCCESS;
306}
307}
const boost::regex re(r_e)
#define ATH_CHECK
Evaluate an expression and check for errors.
#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
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...
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)
std::string to_string(const DetectorType &type)
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.
std::string to_string(const SectorProjector proj)
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.