ATLAS Offline Software
Loading...
Searching...
No Matches
ActsMuonAlignCondAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
6
8#include <GeoModelKernel/GeoPerfUtils.h>
9#include <GeoModelKernel/GeoClearAbsPosAction.h>
11
12
13using namespace MuonGMR4;
14
15#define CREATE_READHANDLE(CONT_TYPE, KEY) \
16 SG::ReadCondHandle<CONT_TYPE> readHandle{KEY, ctx}; \
17 if (!readHandle.isValid()) { \
18 ATH_MSG_FATAL(__FILE__<<":"<<__LINE__ \
19 <<" Failed to load "<<KEY.fullKey()); \
20 return StatusCode::FAILURE; \
21 }
22
23ActsMuonAlignCondAlg::ActsMuonAlignCondAlg(const std::string& name, ISvcLocator* pSvcLocator):
24 AthCondAlgorithm{name, pSvcLocator}{}
25
27
33 ATH_CHECK(m_idHelperSvc.retrieve());
34 ATH_CHECK(detStore()->retrieve(m_detMgr));
35 m_techs = m_detMgr->getDetectorTypes();
36 if (m_techs.empty()) {
37 ATH_MSG_FATAL("The detector manager does not contain any elements");
38 return StatusCode::FAILURE;
39 }
40 auto hasDetector = [this](const ActsTrk::DetectorType d) -> bool {
41 return std::find(m_techs.begin(),m_techs.end(), d) != m_techs.end();
42 };
43
45 hasDetector(ActsTrk::DetectorType::Mm) ||
46 hasDetector(ActsTrk::DetectorType::sTgc));
49 hasDetector(ActsTrk::DetectorType::sTgc));
51
52
53
54 for (const ActsTrk::DetectorType det : m_techs) {
55 m_writeKeys.emplace_back(ActsTrk::to_string(det) + m_keyToken);
56 ATH_MSG_INFO("Register new alignment container "<<m_writeKeys.back().fullKey());
57 }
58 ATH_MSG_INFO("Switched options "<<m_fillAlignStoreCache<<", "<<", "<<m_applyALines<<", "
59 <<m_applyBLines<<", "<<m_applyMdtAsBuilt<<", "
61 ATH_CHECK(m_writeKeys.initialize());
62 return StatusCode::SUCCESS;
63}
64
66 if (re->detectorType() == ActsTrk::DetectorType::Mdt ||
67 re->detectorType() == ActsTrk::DetectorType::Tgc) {
68 return m_idHelperSvc->chamberId(re->identify());
69 } else if (re->detectorType() == ActsTrk::DetectorType::Rpc) {
71 if (!m_idHelperSvc->hasMDT() ||
72 (std::abs(re->stationEta()) == 7 && m_idHelperSvc->stationNameString(re->identify()) == "BML")) {
73 return m_idHelperSvc->rpcIdHelper().elementID(re->stationName(), re->stationEta(), re->stationPhi(), 1);
75 } else {
76 return m_idHelperSvc->mdtIdHelper().elementID(re->stationName(), re->stationEta(), re->stationPhi());
77 }
78 }
80 return re->identify();
81}
82StatusCode ActsMuonAlignCondAlg::loadDeltas(const EventContext& ctx,
83 deltaMap& alignDeltas,
84 alignTechMap& techTransforms) const {
85 if (m_readKeyALines.empty()) {
86 ATH_MSG_DEBUG("Loading of the A line parameters deactivated");
87 return StatusCode::SUCCESS;
88 }
89
91 const ALineContainer* aLineContainer{*readHandle};
92 std::vector<const MuonReadoutElement*> readoutEles = m_detMgr->getAllReadoutElements();
93 ATH_MSG_INFO("Load the alignment of "<<readoutEles.size()<<" detector elements");
94 for (const MuonReadoutElement* re : readoutEles) {
95 const GeoAlignableTransform* alignTrans = re->alignableTransform();
96 if (!alignTrans) {
97 ATH_MSG_WARNING("The readout element "<<m_idHelperSvc->toStringDetEl(re->identify())
98 <<" has no alignable transform.");
99 continue;
100 }
101 std::shared_ptr<const Amg::Transform3D>& cached = alignDeltas[alignTrans];
102 if (cached) {
103 ATH_MSG_DEBUG("The alignable transformation for "<<m_idHelperSvc->toStringChamber(re->identify())
104 <<" has been cached before. ");
105 techTransforms[re->detectorType()].insert(alignTrans);
106 continue;
107 }
109 const Identifier stationId = alignmentId(re);
110 ALineContainer::const_iterator aLineItr = aLineContainer->find(stationId);
111 if (aLineItr == aLineContainer->end()) {
112 ATH_MSG_VERBOSE("No Alines were stored for "<<m_idHelperSvc->toString(re->identify())
113 <<". Used "<<m_idHelperSvc->toString(stationId)<<" as station Identifier");
114 continue;
115 }
117 cached = std::make_shared<Amg::Transform3D>(aLineItr->delta());
118 techTransforms[re->detectorType()].insert(alignTrans);
119 }
120 return StatusCode::SUCCESS;
121}
122StatusCode ActsMuonAlignCondAlg::loadMdtDeformPars(const EventContext& ctx,
123 ActsTrk::DetectorAlignStore& store) const {
124
126 return StatusCode::SUCCESS;
127 }
128 std::unique_ptr<MdtAlignmentStore> internAlign = std::make_unique<MdtAlignmentStore>(m_idHelperSvc.get());
129 const MdtAsBuiltContainer* asBuiltCont{nullptr};
130 const BLineContainer* bLines{nullptr};
131 if (m_applyMdtAsBuilt) {
133 asBuiltCont = readHandle.cptr();
134 }
135 if (m_applyBLines) {
137 bLines = readHandle.cptr();
138 }
139 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
140 for (auto itr = idHelper.module_begin(); itr != idHelper.module_end(); ++itr) {
141 const Identifier& stationId{*itr};
142 const BLinePar* bline{nullptr};
143 if (bLines) {
144 BLineContainer::const_iterator itr = bLines->find(stationId);
145 if (itr != bLines->end()) bline = &(*itr);
146 }
147 const MdtAsBuiltPar* asBuilt{nullptr};
148 if (asBuiltCont) {
149 MdtAsBuiltContainer::const_iterator itr = asBuiltCont->find(stationId);
150 if (itr != asBuiltCont->end()) asBuilt = &(*itr);
151 }
152 if (asBuilt || bline) internAlign->storeDistortion(stationId, bline, asBuilt);
153 }
154 // Down cast the alignment pointer
155 store.internalAlignment = std::move(internAlign);
156 return StatusCode::SUCCESS;
157}
158StatusCode ActsMuonAlignCondAlg::loadMmDeformPars(const EventContext& ctx,
159 ActsTrk::DetectorAlignStore& store) const {
161 return StatusCode::SUCCESS;
162 }
163 std::unique_ptr<MmAlignmentStore> internAlign = std::make_unique<MmAlignmentStore>();
166 internAlign->passivation = readHandle.cptr();
167 }
168 if (m_applyNswAsBuilt) {
170 internAlign->asBuiltPars = readHandle->microMegaData;
171 }
172 if (m_applyBLines) {
175 std::vector<const MuonReadoutElement*> reEles = m_detMgr->getAllReadoutElements();
176 for (const MuonReadoutElement* re : reEles){
177 if (re->detectorType() != ActsTrk::DetectorType::Mm) continue;
178 const Identifier stationId = alignmentId(re);
179 BLineContainer::const_iterator itr = readHandle->find(stationId);
180 if (itr != readHandle->end()) internAlign->cacheBLine(re->identify(), *itr);
181 }
182 }
183 store.internalAlignment = std::move(internAlign);
184 return StatusCode::SUCCESS;
185}
186StatusCode ActsMuonAlignCondAlg::loadStgcDeformPars(const EventContext& ctx,
187 ActsTrk::DetectorAlignStore& store) const{
189 return StatusCode::SUCCESS;
190 }
191 std::unique_ptr<sTgcAlignmentStore> internalAlign = std::make_unique<sTgcAlignmentStore>();
192 if (m_applyNswAsBuilt && !m_readsTgcAsBuiltKey.empty()) {
194 internalAlign->asBuiltPars = *readHandle;
195 }
196 if (m_applyBLines) {
199 std::vector<const MuonReadoutElement*> reEles = m_detMgr->getAllReadoutElements();
200 for (const MuonReadoutElement* re : reEles){
201 if (re->detectorType() != ActsTrk::DetectorType::sTgc) continue;
202 const Identifier stationId = alignmentId(re);
203 BLineContainer::const_iterator itr = readHandle->find(stationId);
204 if (itr != readHandle->end()) internalAlign->cacheBLine(re->identify(), *itr);
205 }
206 }
207 store.internalAlignment = std::move(internalAlign);
208 return StatusCode::SUCCESS;
209}
210
211StatusCode ActsMuonAlignCondAlg::declareDependencies(const EventContext& ctx,
212 ActsTrk::DetectorType detType,
215 if (m_applyALines) {
217 writeHandle.addDependency(readHandle);
218 }
219 const bool issTGC = detType == ActsTrk::DetectorType::sTgc;
220 const bool isMm = detType == ActsTrk::DetectorType::Mm;
221 const bool isMdt = detType == ActsTrk::DetectorType::Mdt;
222
223 if (m_applyBLines&& (issTGC || isMm || isMdt)) {
225 writeHandle.addDependency(readHandle);
226 }
227 if (m_applyMdtAsBuilt && isMdt) {
229 writeHandle.addDependency(readHandle);
230 }
233 writeHandle.addDependency(readHandle);
234 }
235 if (m_applyNswAsBuilt && isMm) {
237 writeHandle.addDependency(readHandle);
238 }
239 if(m_applyNswAsBuilt && issTGC && !m_readsTgcAsBuiltKey.empty()){
241 writeHandle.addDependency(readHandle);
242 }
243 return StatusCode::SUCCESS;
244}
245
246StatusCode ActsMuonAlignCondAlg::execute(const EventContext& ctx) const {
247 deltaMap alignDeltas{};
248 alignTechMap techTransforms{};
249 unsigned int memBeforeAlign = GeoPerfUtils::getMem();
250 ATH_CHECK(loadDeltas(ctx, alignDeltas, techTransforms));
251
252 std::vector<const MuonReadoutElement*> readoutEles = m_detMgr->getAllReadoutElements();
254 unsigned int numAligned{0};
255 for (size_t det =0 ; det < m_techs.size(); ++det) {
257 const ActsTrk::DetectorType subDet = m_techs[det];
258
260 if (writeHandle.isValid()) {
261 ATH_MSG_VERBOSE("The alignment constants for "<<ActsTrk::to_string(subDet)
262 <<" are still valid.");
263 continue;
264 }
265 std::unique_ptr<ActsTrk::DetectorAlignStore> writeCdo = std::make_unique<ActsTrk::DetectorAlignStore>(subDet);
266
267 const std::set<const GeoAlignableTransform*>& toStore = techTransforms[subDet];
269 for (const GeoAlignableTransform* alignable : toStore) {
270 const std::shared_ptr<const Amg::Transform3D>& cached = alignDeltas[alignable];
271 if (!cached) continue;
272 writeCdo->geoModelAlignment->setDelta(alignable, alignDeltas[alignable]);
273 }
274 writeCdo->geoModelAlignment->lockDelta();
275 if (subDet == ActsTrk::DetectorType::Mdt) {
276 ATH_CHECK(loadMdtDeformPars(ctx,*writeCdo));
277 } else if (subDet == ActsTrk::DetectorType::Mm) {
278 ATH_CHECK(loadMmDeformPars(ctx, *writeCdo));
279 } else if (subDet == ActsTrk::DetectorType::sTgc) {
280 ATH_CHECK(loadStgcDeformPars(ctx, *writeCdo));
281 }
283 ATH_CHECK(declareDependencies(ctx, subDet, writeHandle));
286 for (const MuonReadoutElement* re : readoutEles) {
287 if (re->detectorType() == subDet) {
288 const Amg::Transform3D& detTrf{re->getMaterialGeom()->getAbsoluteTransform(writeCdo->geoModelAlignment.get())};
289 ATH_MSG_VERBOSE("Detector element "<<m_idHelperSvc->toStringDetEl(re->identify())<<" is located at "
290 <<Amg::toString(detTrf));
291 }
292 }
294 writeCdo->geoModelAlignment->getDeltas()->clear();
295 writeCdo->geoModelAlignment->lockPosCache();
296 } else if (m_fillAlignStoreCache) {
297 for (const MuonReadoutElement* re : readoutEles){
298 numAligned+= re->storeAlignedTransforms(*writeCdo);
299 }
301 writeCdo->geoModelAlignment.reset();
302 }
303 ATH_CHECK(writeHandle.record(std::move(writeCdo)));
304 }
305
306 ATH_MSG_VERBOSE("Only "<<numAligned<<" out of "<<readoutEles.size()<<" were picked up by the alignment cutalg");
307 alignDeltas.clear();
308 techTransforms.clear();
310 GeoClearAbsPosAction whipeTreeTop{};
311 for (unsigned int treeTop = 0 ; treeTop < m_detMgr->getNumTreeTops(); ++treeTop) {
312 m_detMgr->getTreeTop(treeTop)->exec(&whipeTreeTop);
313 }
314
315 unsigned int memAfterAlign = GeoPerfUtils::getMem();
316 ATH_MSG_INFO("Caching of the alignment parameters required "<<(memAfterAlign - memBeforeAlign) / 1024<<" MB of memory");
317 return StatusCode::SUCCESS;
318}
319
320#undef CREATE_READHANDLE
const boost::regex re(r_e)
#define CREATE_READHANDLE(CONT_TYPE, KEY)
#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
StatusCode loadMmDeformPars(const EventContext &ctx, ActsTrk::DetectorAlignStore &store) const
virtual StatusCode initialize() override
Gaudi::Property< bool > m_applyMdtAsBuilt
StatusCode loadStgcDeformPars(const EventContext &ctx, ActsTrk::DetectorAlignStore &store) const
const MuonGMR4::MuonDetectorManager * m_detMgr
SG::ReadCondHandleKey< NswAsBuiltDbData > m_readNswAsBuiltKey
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 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< bool > m_applyMmPassivation
std::unordered_map< const GeoAlignableTransform *, std::shared_ptr< const Amg::Transform3D > > deltaMap
Association map of the GeoAlignableTransforms with the rigid alignment transformations.
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...
ActsMuonAlignCondAlg(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadCondHandleKey< BLineContainer > m_readKeyBLines
Gaudi::Property< bool > m_fillGeoAlignStore
Flag toggling whether the GeoAlignmentStore shall be filled.
Gaudi::Property< bool > m_applyNswAsBuilt
std::map< ActsTrk::DetectorType, std::set< const GeoAlignableTransform * > > alignTechMap
Association map of the GeoAlignable transforms with the detector technologies.
SG::ReadCondHandleKey< ALineContainer > m_readKeyALines
Identifier alignmentId(const MuonGMR4::MuonReadoutElement *reElement) const
Returns the Identifier serving as key to find the alignment parameters connected with the readout ele...
Gaudi::Property< std::string > m_keyToken
virtual StatusCode execute(const EventContext &ctx) const override
Gaudi::Property< bool > m_applyALines
Apply translations and rotations to align the Muon stations.
Gaudi::Property< bool > m_applyBLines
Apply the chamber deformation model (Mdts + Nsw)
SG::ReadCondHandleKey< NswPassivationDbData > m_readNswPassivKey
SG::ReadCondHandleKey< sTGCAsBuiltData > m_readsTgcAsBuiltKey
Gaudi::Property< bool > m_fillAlignStoreCache
Flag toggling whether the alignment store shall be filled with the transforms or not.
SG::WriteCondHandleKeyArray< ActsTrk::DetectorAlignStore > m_writeKeys
std::vector< ActsTrk::DetectorType > m_techs
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
SG::ReadCondHandleKey< MdtAsBuiltContainer > m_readMdtAsBuiltKey
const ServiceHandle< StoreGateSvc > & detStore() const
Base class for conditions algorithms.
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...
The MuonReadoutElement is an abstract class representing the geometry representing the muon detector.
const_id_iterator module_end() const
const_id_iterator module_begin() const
Iterators over full set of ids.
void addDependency(const EventIDRange &range)
StatusCode record(const EventIDRange &range, T *t)
record handle, with explicit range DEPRECATED
Class holding the sTGC as built conditions data and applying it.
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.
Eigen::Affine3d Transform3D
The ReadoutGeomCnvAlg converts the Run4 Readout geometry build from the GeoModelXML into the legacy M...