ATLAS Offline Software
Loading...
Searching...
No Matches
AlignmentErrorTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
9
10#include "AlignmentErrorTool.h"
11
12#include <boost/functional/hash.hpp>
13#include <fstream>
14#include <sstream>
15#include <unordered_map>
16
24#include "TrkTrack/Track.h"
25
26namespace MuonAlign {
27
28AlignmentErrorTool::AlignmentErrorTool(const std::string& t, const std::string& n, const IInterface* p) : AthAlgTool(t, n, p) {
29 declareInterface<Trk::ITrkAlignmentDeviationTool>(this);
30}
31
33 ATH_MSG_INFO("*****************************************");
34 ATH_MSG_INFO("AlignmentErrorTool::initialize()");
35
36 ATH_CHECK(m_idTool.retrieve());
37 ATH_CHECK(m_idHelperSvc.retrieve());
38 ATH_CHECK(m_readKey.initialize());
39
40 return StatusCode::SUCCESS;
41}
42
43void AlignmentErrorTool::makeAlignmentDeviations(const Trk::Track& track, std::vector<Trk::AlignmentDeviation*>& deviations) const {
44 ATH_MSG_DEBUG("AlignmentErrorTool::makeAlignmentDeviations()");
45
47 const MuonAlignmentErrorData* readCdo{*readHandle};
48 if (!readCdo) {
49 ATH_MSG_ERROR("nullptr to the read conditions object");
50 return;
51 }
52 const auto& deviationVec = readCdo->getAlignmentErrorRules();
53 std::vector<deviationSummary_t> devSumVec;
54 devSumVec.reserve(deviationVec.size());
55 deviationSummary_t aDevSumm;
56 for (const auto & i : deviationVec) {
57 aDevSumm.translation = i.translation;
58 aDevSumm.rotation = i.rotation;
59 aDevSumm.stationName = i.stationName;
60 aDevSumm.multilayer = i.multilayer;
61 devSumVec.emplace_back(std::move(aDevSumm));
62 }
63
64 const auto& MuonAlignmentErrorRuleCacheVec = readCdo->getMuonAlignmentErrorRuleCache();
65 const auto& map_struct = MuonAlignmentErrorRuleCacheVec[0];
66
67 typedef Trk::TrackStates tsosc_t;
68 const tsosc_t* tsosc = track.trackStateOnSurfaces();
69
70 // LOOP ON HITS ON TRACK //
71 unsigned int nPrecisionHits = 0;
72 for (const auto *tsos : *tsosc) {
74 continue;
75 }
76 const Trk::MeasurementBase* meas = tsos->measurementOnTrack();
77 const auto* rot = dynamic_cast<const Trk::RIO_OnTrack*>(meas);
78
79 if (!rot) {
80 const auto* crot = dynamic_cast<const Trk::CompetingRIOsOnTrack*>(meas);
81 if (crot) {
82 unsigned int index = crot->indexOfMaxAssignProb();
83 rot = &(crot->rioOnTrack(index));
84 }
85 }
86 if (!rot) continue;
87
88 Identifier channelId = rot->identify();
89 if (!m_idHelperSvc->isMuon(channelId)) {
90 // the RIO_OnTrack Identifiers could also come from ID or Calo, but this tool is only interested in MS hits
91 ATH_MSG_VERBOSE("Given Identifier " << channelId.get_compact() << " is not a muon Identifier, continuing");
92 continue;
93 }
94
95 // Keep only the precision coordinate hits
96 if (m_idHelperSvc->isRpc(channelId)
97 || m_idHelperSvc->isTgc(channelId)
98 || (m_idHelperSvc->isCsc(channelId) && m_idHelperSvc->cscIdHelper().measuresPhi(channelId) == 1)
99 || (m_idHelperSvc->issTgc(channelId) && m_idHelperSvc->stgcIdHelper().channelType(channelId) != sTgcIdHelper::sTgcChannelTypes::Strip)) {
100 continue;
101 }
102
103 // To maintain backward compatibility with the old error CLOBs, activate
104 // the NSW hits only if specified in the CLOB, else disccard them
105 if (!readCdo->hasNswHits()
106 && (m_idHelperSvc->issTgc(channelId) || m_idHelperSvc->isMM(channelId))) {
107 continue;
108 }
109
110 MuonCalib::MuonFixedLongId calibId = m_idTool->idToFixedLongId(channelId);
111 if (!calibId.isValid()) continue;
112
113 ATH_MSG_DEBUG("Hit is in station " << calibId << " (MuonFixedLongId)");
114 ++nPrecisionHits;
115
116 // Compute deviationSummary_t building blocks
117 const Trk::PrepRawData* prd = rot->prepRawData();
118 const Trk::Surface& sur = prd->detectorElement()->surface(prd->identify());
119
120 double w2 = 1.0 / (rot->localCovariance()(Trk::loc1, Trk::loc1));
121 Amg::Vector3D hitP = tsos->trackParameters()->position();
122 Amg::Vector3D hitU = tsos->trackParameters()->momentum().unit();
123
124 // Wire direction for MDT, strip direction for MM or sTGC
125 int icol = (calibId.is_mdt()||calibId.is_csc()) ? 2 : 1;
126 Amg::Vector3D hitV = sur.transform().rotation().col(icol);
127
128 // Enforce orientation of the V vectors
129 static const Amg::Vector3D zATLAS(0., 0., 1.);
130 if (hitP.cross(zATLAS).dot(hitV)<0.0) {
131 hitV *= -1.0;
132 }
133
134 // FOR CROSS-CHECK
135 bool is_matched = false;
136
137 // Construct detector element identifier from channelId, remaining only chamber name and multilayer information
138 int multilayer = 1;
139 if (calibId.is_mdt()) {
140 multilayer = calibId.mdtMultilayer();
141 } else if (calibId.is_mmg()) {
142 multilayer = calibId.mmgMultilayer();
143 } else if (calibId.is_stg()) {
144 multilayer = calibId.stgMultilayer();
145 }
146
147 Identifier key_id{};
148 if (m_idHelperSvc->isMdt(channelId)){
149 key_id = m_idHelperSvc->mdtIdHelper().channelID(channelId, multilayer, 1, 1);
150 }
151 if (m_idHelperSvc->isRpc(channelId)){
152 key_id = m_idHelperSvc->rpcIdHelper().elementID(channelId);
153 }
154 if (m_idHelperSvc->isMM(channelId)){
155 key_id = m_idHelperSvc->mmIdHelper().channelID(channelId, multilayer, 1, 1);
156 }
157 if (m_idHelperSvc->issTgc(channelId)){
158 key_id = m_idHelperSvc->stgcIdHelper().channelID(channelId, multilayer, 1, 0, 1);
159 }
160 if (m_idHelperSvc->isCsc(channelId)){
161 key_id = m_idHelperSvc->cscIdHelper().elementID(channelId);
162 }
163 if (m_idHelperSvc->isTgc(channelId)){
164 key_id = m_idHelperSvc->tgcIdHelper().elementID(channelId);
165 }
166
167 // Find deviation (pointer) for current detector element
168 auto range = map_struct.id_rule_map.equal_range(key_id);
169 for (auto i = range.first; i != range.second; ++i){
171
172 // ASSOCIATE EACH NUISANCE TO A LIST OF HITS
173 devSumVec[rule_idx].hits.push_back(rot);
174
175 devSumVec[rule_idx].sumW2 += w2;
176 devSumVec[rule_idx].sumP += w2 * hitP;
177 devSumVec[rule_idx].sumU += w2 * hitU;
178 devSumVec[rule_idx].sumV += w2 * hitV;
179
180 // FOR CROSS-CHECK
181 is_matched = true;
182
183
184 } // LOOP ON DEVIATIONS
185
186 if (!is_matched) {
187 ATH_MSG_WARNING("The hits in the station " << calibId << " (MuonFixedLongId)"
188 << " couldn't be matched to any deviation regexp in the list.");
189 }
190
191 } // LOOP ON TSOS
192
193 // Nuisance parameters covering the complete track are not wanted. (MS/ID
194 // error treated differently for now). Removing the deviations covering the
195 // full track in further processing.
196 for (auto& dev : devSumVec) {
197 if (dev.hits.size() == nPrecisionHits) {
198 dev.hits.clear();
199 }
200 }
201
202 // GET RID OF PERFECT OVERLAPS BY COMBINING ERRORS
203 std::vector<const Trk::RIO_OnTrack*> v1, v2;
204 for (auto iti = devSumVec.begin(); iti != devSumVec.end(); ++iti) {
205
206 v1 = iti->hits;
207 if (v1.empty()) {
208 continue;
209 }
210
211 std::stable_sort(v1.begin(), v1.end());
212
213 for (auto itj = iti+1; itj != devSumVec.end(); ++itj) {
214
215 if (iti->hits.size() != itj->hits.size()) {
216 continue;
217 }
218
219 v2 = itj->hits;
220 std::stable_sort(v2.begin(), v2.end());
221
222 if (v1 == v2) {
223 auto iDev = std::distance(devSumVec.begin(), iti);
224 auto jDev = std::distance(devSumVec.begin(), itj);
225 ATH_MSG_DEBUG("Found deviations " << iDev << " and " << jDev << " related to the same list of hits. Merging...");
226 ATH_MSG_DEBUG("old (translation, rotation) systematic uncertainties for "
227 << iDev << ": " << iti->translation << ", " << iti->rotation);
228 ATH_MSG_DEBUG("old (translation, rotation) systematic uncertainties for "
229 << jDev << ": " << itj->translation << ", " << itj->rotation);
230
231 // MERGE THE TWO DEVIATIONS ASSOCIATED TO THE SAME LIST OF HITS //
232 double new_translation = std::hypot(iti->translation, itj->translation);
233 double new_rotation = std::hypot(iti->rotation, itj->rotation);
234
235 // NOW PREPARE TO ERASE ONE OF THE TWO COPIES //
236 itj->hits.clear();
237
238 // ASSIGN NEW TRASLATION/ROTATION TO THE REMAINING COPY //
239 iti->translation = new_translation;
240 iti->rotation = new_rotation;
241 ATH_MSG_DEBUG("New combined (translation, rotation) systematic uncertainties: " << new_translation << ", " << new_rotation);
242
243 } // FIND AN OVERLAP IN THE HITS LISTS
244 } // SECOND LOOP ON DEVIATIONS
245 } // FIRST LOOP ON DEVIATIONS
246
247
248 // NOW BUILD THE DEVIATIONS
249 deviations.clear();
250 ATH_MSG_DEBUG("************************************");
251 ATH_MSG_DEBUG("FINAL LIST OF DEVIATIONS");
252 for (const auto & iDev : devSumVec) {
253 if (iDev.hits.empty()) {
254 continue;
255 }
256
257 double rotation = iDev.rotation;
258 double translation = iDev.translation;
259
260 Amg::Vector3D sumP = iDev.sumP;
261 Amg::Vector3D sumU = iDev.sumU;
262 Amg::Vector3D sumV = iDev.sumV;
263 double sumW2 = iDev.sumW2;
264
265 sumP *= (1. / sumW2);
266 sumU *= (1. / sumW2);
267 sumV *= (1. / sumW2);
268
269 if (translation >= 0.001 * Gaudi::Units::mm) {
270 std::size_t hitshash = 0;
271 for (const auto *it : iDev.hits) {
272 boost::hash_combine(hitshash, (it->identify()).get_compact());
273 }
274 deviations.push_back(
275 new AlignmentTranslationDeviation(sumU.cross(sumV), translation * Gaudi::Units::mm, iDev.hits));
276 deviations.back()->setHashOfHits(hitshash);
277
278 ATH_MSG_DEBUG("A translation along ("
279 << sumU.x() << ", " << sumU.y() << ", " << sumU.z() << ") with sigma=" << translation * Gaudi::Units::mm
280 << " mm was applied to " << iDev.hits.size()
281 << " hits matching the station: " << iDev.stationName.str() << " and the multilayer "
282 << iDev.multilayer.str());
283 }
284 if (rotation >= 0.000001 * Gaudi::Units::rad) {
285 std::size_t hitshash = 0;
286 for (const auto *it : iDev.hits) {
287 boost::hash_combine(hitshash, (it->identify()).get_compact());
288 }
289 deviations.push_back(new AlignmentRotationDeviation(sumP, sumV, rotation * Gaudi::Units::rad, iDev.hits));
290 deviations.back()->setHashOfHits(hitshash);
291
292 ATH_MSG_DEBUG("A rotation around the center = (" << sumP.x() << ", " << sumP.y() << ", " << sumP.z() << ") and axis = ("
293 << sumV.x() << ", " << sumV.y() << ", " << sumV.z()
294 << ") with sigma=" << rotation / Gaudi::Units::mrad << " mrad was applied to "
295 << iDev.hits.size() << " hits matching the station "
296 << iDev.stationName.str() << " and the multilayer "
297 << iDev.multilayer.str());
298 }
299
300 } // LOOP ON NUISANCES
301
302 ATH_MSG_DEBUG("******************************");
303 ATH_MSG_DEBUG("FINAL CHECKUP");
304 ATH_MSG_DEBUG("Found " << deviations.size() << " nuisances after duplicates merging");
305 ATH_MSG_DEBUG("******************************");
306}
307
308} // namespace MuonAlign
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
void makeAlignmentDeviations(const Trk::Track &track, std::vector< Trk::AlignmentDeviation * > &deviations) const override
Compute alignment deviations, given a track as input.
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
ToolHandle< MuonCalib::IIdToFixedIdTool > m_idTool
AlignmentErrorTool(const std::string &, const std::string &, const IInterface *)
SG::ReadCondHandleKey< MuonAlignmentErrorData > m_readKey
MuonAlignmentErrorData is condition data which is derived and recorded by MuonAlignmentErrorDbAlg.
const std::vector< MuonAlignmentErrorRuleCache > & getMuonAlignmentErrorRuleCache() const
const std::vector< MuonAlignmentErrorRule > & getAlignmentErrorRules() const
int stgMultilayer() const
Stg specific:
int mdtMultilayer() const
Mdt specific:
int mmgMultilayer() const
Mmg specific:
bool isValid() const
check validity of the identifier.
Base class for all CompetingRIOsOnTack implementations, extends the common MeasurementBase.
This class is the pure abstract base class for all fittable tracking measurements.
virtual const TrkDetElementBase * detectorElement() const =0
return the detector element corresponding to this PRD The pointer will be zero if the det el is not d...
Identifier identify() const
return the identifier
Class to handle RIO On Tracks ROT) for InDet and Muons, it inherits from the common MeasurementBase.
Definition RIO_OnTrack.h:70
Abstract Base Class for tracking surfaces.
const Amg::Transform3D & transform() const
Returns HepGeom::Transform3D by reference.
@ Measurement
This is a measurement, and will at least contain a Trk::MeasurementBase.
virtual const Surface & surface() const =0
Return surface associated with this detector element.
Eigen::Matrix< double, 3, 1 > Vector3D
DataVector< const Trk::TrackStateOnSurface > TrackStates
@ loc1
Definition ParamDefs.h:34
Definition index.py:1
void stable_sort(DataModel_detail::iterator< DVL > beg, DataModel_detail::iterator< DVL > end)
Specialization of stable_sort for DataVector/List.