ATLAS Offline Software
StripCalibratorImpl.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 
4 */
5 
6 #ifndef STRIPCALIBRATORIMPL_ICC
7 #define STRIPCALIBRATORIMPL_ICC
8 
9 #include "InDetReadoutGeometry/SiDetectorElement.h"
10 #include "SCT_ReadoutGeometry/StripStereoAnnulusDesign.h"
11 #include <stdexcept>
12 
13 namespace ActsTrk::detail{
14 
15  template <typename traj_t>
16  StripCalibratorImpl<traj_t>::StripCalibratorImpl(const std::string& type,
17  const std::string& name,
18  const IInterface* parent)
19  : base_class(type, name, parent)
20  {}
21 
22  template <typename traj_t>
23  StatusCode StripCalibratorImpl<traj_t>::initialize()
24  {
25  ATH_MSG_DEBUG("Initializing " << AthAlgTool::name() << " ...");
26  ATH_CHECK(m_stripDetEleCollKey.initialize());
27 
28  ATH_MSG_DEBUG(AthAlgTool::name() << " successfully initialized");
29 
30  ATH_MSG_INFO( m_correctCovariance );
31  ATH_MSG_INFO( m_postCalibration );
32  ATH_MSG_INFO( m_errorStrategy );
33 
34  return StatusCode::SUCCESS;
35 
36  }
37 
38  template <typename traj_t>
39  const InDetDD::SiDetectorElement& StripCalibratorImpl<traj_t>::getDetectorElement(xAOD::DetectorIDHashType id) const
40  {
41  const InDetDD::SiDetectorElement* element=nullptr;
42  SG::ReadCondHandle<InDetDD::SiDetectorElementCollection> stripDetEleHandle(
43  m_stripDetEleCollKey,
44  Gaudi::Hive::currentContext());
45 
46  const InDetDD::SiDetectorElementCollection* detElements(*stripDetEleHandle);
47 
48  if (!stripDetEleHandle.isValid() or detElements == nullptr) {
49  ATH_MSG_ERROR(m_stripDetEleCollKey.fullKey() << " is not available.");
50  }
51  else {
52  element = detElements->getDetectorElement(id);
53  }
54  if (element == nullptr) {
55  ATH_MSG_ERROR("No element corresponding to hash " << id << " for " << m_stripDetEleCollKey.fullKey());
56  throw std::runtime_error("SiDetectorElement is NULL");
57  }
58 
59  return *element;
60  }
61 
62  template <typename traj_t>
63  std::optional<float> StripCalibratorImpl<traj_t>::getCorrectedError(const xAOD::StripCluster& cluster) const
64  {
65  std::optional<float> errX {std::nullopt};
66  if (not m_correctCovariance) {
67  return errX;
68  }
69 
70  Cov cov = cluster.template localCovariance<1>();
71  int nrows = cluster.channelsInPhi();
72 
73  if (m_errorStrategy == 0 ) {
74 
75  errX = std::sqrt(cov(0,0));
76 
77  } else if (m_errorStrategy == 1 ) {
78 
79  errX = std::sqrt(cov(0,0)) / nrows;
80 
81  }
82 
83 
84  return errX;
85  }
86 
87  template <typename traj_t>
88  std::pair<typename StripCalibratorImpl<traj_t>::Pos,
89  typename StripCalibratorImpl<traj_t>::Cov>
90  StripCalibratorImpl<traj_t>::calibrate(
91  const Acts::GeometryContext& gctx,
92  const Acts::CalibrationContext& cctx,
93  const xAOD::StripCluster& cluster,
94  const TrackStateProxy& /*state*/) const
95  {
96  const InDetDD::SiDetectorElement &detElement = getDetectorElement(cluster.identifierHash());
97  return calibrate(gctx, cctx, cluster, detElement);
98  }
99 
100  template <typename traj_t>
101  std::pair<typename StripCalibratorImpl<traj_t>::Pos,
102  typename StripCalibratorImpl<traj_t>::Cov>
103  StripCalibratorImpl<traj_t>::calibrate(
104  const Acts::GeometryContext& gctx,
105  const Acts::CalibrationContext& cctx,
106  const xAOD::StripCluster& cluster,
107  const Acts::BoundTrackParameters& /*bound_parameters*/) const
108  {
109  const InDetDD::SiDetectorElement &detElement = getDetectorElement(cluster.identifierHash());
110  return calibrate(gctx, cctx, cluster, detElement);
111  }
112 
113  template <typename traj_t>
114  std::pair<typename StripCalibratorImpl<traj_t>::Pos,
115  typename StripCalibratorImpl<traj_t>::Cov>
116  StripCalibratorImpl<traj_t>::calibrate(
117  const Acts::GeometryContext& /*gctx*/,
118  const Acts::CalibrationContext& /*cctx*/,
119  const xAOD::StripCluster& cluster,
120  const InDetDD::SiDetectorElement& /*detElement*/) const
121  {
122  Pos pos = cluster.template localPosition<1>();
123  Cov cov = cluster.template localCovariance<1>();
124 
125  auto errX = getCorrectedError(cluster);
126  if (errX.has_value()) {
127  double newErr = *errX;
128  cov(0, 0) = newErr * newErr;
129  }
130 
131  return std::make_pair(pos, cov);
132  }
133 
134  template <typename traj_t>
135  void StripCalibratorImpl<traj_t>::connect(OnTrackCalibrator<traj_t>& calibrator) const
136  {
137  using CalibFuncPtr_t =
138  std::pair<typename StripCalibratorImpl<traj_t>::Pos, typename StripCalibratorImpl<traj_t>::Cov>
139  (StripCalibratorImpl<traj_t>:: *)(const Acts::GeometryContext&,
140  const Acts::CalibrationContext&,
141  const xAOD::StripCluster&,
142  const TrackStateProxy&) const;
143 
144  calibrator.stripCalibrator. template connect<static_cast<CalibFuncPtr_t>(&StripCalibratorImpl<traj_t>::calibrate)>(this);
145 }
146 
147 template <typename traj_t>
148 void StripCalibratorImpl<traj_t>::connectStripCalibrator(IOnBoundStateCalibratorTool::StripCalibrator& stripCalibrator) const
149 {
150  using CalibFuncPtr_t =
151  std::pair<typename StripCalibratorImpl<traj_t>::Pos,
152  typename StripCalibratorImpl<traj_t>::Cov>
153  (StripCalibratorImpl<traj_t>:: *)(const Acts::GeometryContext&,
154  const Acts::CalibrationContext&,
155  const xAOD::StripCluster&,
156  const Acts::BoundTrackParameters&) const;
157 
158  stripCalibrator.template connect<static_cast<CalibFuncPtr_t>(&StripCalibratorImpl<traj_t>::calibrate)>(this);
159 }
160 
161 template <typename traj_t>
162 bool StripCalibratorImpl<traj_t>::calibrateAfterMeasurementSelection() const
163 { return m_postCalibration.value(); }
164 
165 
166 
167 } // namespace ActsTrk::detail
168 
169 #endif