ATLAS Offline Software
StripClusteringTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "StripClusteringTool.h"
6 
7 
8 #include <Acts/Clusterization/Clusterization.hpp>
9 
13 #include <TrkSurfaces/Surface.h>
14 
15 #include <algorithm>
16 #include <stdexcept>
17 
18 
19 namespace ActsTrk {
20 constexpr double ONE_TWELFTH = 1./12.;
21 
22  // Required by ACTS clusterization
23 static
24 int getCellColumn(const StripClusteringTool::Cell& cell)
25 {
26  return cell.index;
27 }
28 
29 // Required by ACTS clusterization
30 static
31 int& getCellLabel(StripClusteringTool::Cell& cell)
32 {
33  return cell.label;
34 }
35 
36 // Required by ACTS clusterization
37 static
38 void clusterAddCell(StripClusteringTool::Cluster& cl, const StripClusteringTool::Cell& cell)
39 {
40  cl.ids.push_back(cell.id.get_compact());
41  if (cl.ids.size() < (sizeof(cl.hitsInThirdTimeBin) * 8)) {
42  cl.hitsInThirdTimeBin |= cell.timeBits.test(0) << cl.ids.size();
43  }
44 }
45 
47  const std::string& type, const std::string& name, const IInterface* parent)
48  : base_class(type,name,parent)
49 {
50 }
51 
53 {
54  ATH_MSG_DEBUG("Initializing " << name() << "...");
55 
56  ATH_CHECK(m_conditionsTool.retrieve(DisableTool{!m_stripDetElStatus.empty()} ));
57  ATH_CHECK(m_lorentzAngleTool.retrieve());
59 
62 
63  ATH_CHECK( detStore()->retrieve(m_stripID, "SCT_ID") );
64 
65  return StatusCode::SUCCESS;
66 }
67 
69 {
70  for (size_t i = 0; i < m_timeBinStr.size(); i++) {
71  if (i >= 3) {
72  ATH_MSG_WARNING("Time bin string has excess characters");
73  break;
74  }
75  switch (std::toupper(m_timeBinStr[i])) {
76  case 'X': m_timeBinBits[i] = -1; break;
77  case '0': m_timeBinBits[i] = 0; break;
78  case '1': m_timeBinBits[i] = 1; break;
79  default:
80  ATH_MSG_FATAL("Invalid time bin string: " << m_timeBinStr);
81  return StatusCode::FAILURE;
82  }
83  }
84  return StatusCode::SUCCESS;
85 }
86 
88 StripClusteringTool::clusterize(const EventContext& ctx,
89  const RawDataCollection& RDOs,
90  const InDet::SiDetectorElementStatus& stripDetElStatus,
91  const InDetDD::SiDetectorElement& element,
92  std::vector<typename IStripClusteringTool::ClusterCollection>& collection) const
93 {
94  IdentifierHash idHash = RDOs.identifyHash();
95 
96  bool goodModule = true;
97  if (m_checkBadModules.value()) {
98  goodModule = stripDetElStatus.isGood(idHash);
99  }
100 
103  stripDetElStatus.isGood(idHash), m_conditionsTool->isGood(idHash));
104 
105  if (!goodModule) {
106  ATH_MSG_DEBUG("Strip module failed status check");
107  return StatusCode::SUCCESS;
108  }
109 
110  // If more than a certain number of RDOs set module to bad
111  // in this case we skip clusterization
112  if (m_maxFiredStrips != 0u) {
113  unsigned int nFiredStrips = 0u;
114  for (const SCT_RDORawData* rdo : RDOs) {
115  nFiredStrips += rdo->getGroupSize();
116  }
117  if (nFiredStrips > m_maxFiredStrips)
118  return StatusCode::SUCCESS;
119  }
120 
121  std::optional<std::pair<typename IStripClusteringTool::CellCollection,bool>> unpckd
122  = unpackRDOs(ctx, RDOs, stripDetElStatus, element);
123 
124  if (not unpckd.has_value()) {
125  ATH_MSG_FATAL("Error encountered while unpacking strip RDOs!");
126  return StatusCode::FAILURE;
127  }
128 
129  auto& [cells, badStripOnModule] = *unpckd;
130  // Bad strips on a module invalidates the hitsInThirdTimeBin word.
131  // Therefore set it to 0 if that's the case.
132  // We are currently not using this, but keeping it here should we need it in the future
133 
134  ClusterCollection clusters =
135  Acts::Ccl::createClusters<CellCollection, typename IStripClusteringTool::ClusterCollection, 1>(cells);
136  collection.push_back( std::move(clusters) );
137 
138  return StatusCode::SUCCESS;
139 }
140 
141 
143 StripClusteringTool::makeClusters(const EventContext& ctx,
145  const InDetDD::SiDetectorElement& element,
146  typename ClusterContainer::iterator itrContainer) const
147 {
148  const IdentifierHash idHash = element.identifyHash();
149  double lorentzShift = m_lorentzAngleTool->getLorentzShift(idHash, ctx);
150 
151  const InDetDD::SiDetectorDesign& design = element.design();
152  // get the pitch, this will be the local covariance for the cluster
153  float pitch = element.isBarrel()
154  ? design.phiPitch()
155  : dynamic_cast<const InDetDD::StripStereoAnnulusDesign&>(element.design()).phiPitchPhi();
156  Eigen::Matrix<float,1,1> localCov(pitch * pitch * ONE_TWELFTH);
157 
158  for (typename IStripClusteringTool::Cluster& cl : clusters) {
159  try {
160  xAOD::StripCluster *xaodCluster = *itrContainer;
162  lorentzShift,
163  localCov,
164  *m_stripID,
165  element,
166  design,
167  *xaodCluster));
168  ++itrContainer;
169  } catch (const std::exception& e) {
170  ATH_MSG_FATAL("Exception thrown while creating xAOD::StripCluster:"
171  << e.what());
172  ATH_MSG_FATAL("Detector Element identifier: " << element.identify());
173  ATH_MSG_FATAL("Strip Identifiers in cluster:");
174  for (const auto& id : cl.ids)
175  ATH_MSG_FATAL(" " << id);
176  return StatusCode::FAILURE;
177  }
178  }
179 
180  return StatusCode::SUCCESS;
181 }
182 
183 static
184 std::pair<
185  Eigen::Matrix<float,1,1>,
186  Eigen::Matrix<float,3,1>>
187 computePosition(const StripClusteringTool::Cluster& cluster,
188  std::size_t size,
189  double lorentzShift,
190  const IStripClusteringTool::IDHelper& stripID,
191  const InDetDD::SiDetectorElement& element,
192  const InDetDD::SiDetectorDesign& design)
193 {
194 
195  Identifier ids_front(cluster.ids.front());
196  Identifier ids_back(cluster.ids.back());
197  InDetDD::SiCellId frontId = stripID.strip(ids_front);
199  if (size > 1) {
200  InDetDD::SiCellId backId = stripID.strip(ids_back);
201  InDetDD::SiLocalPosition backPos =
202  design.localPositionOfCell(backId);
203  pos = 0.5 * (pos + backPos);
204  }
205 
206  // update the xPhi position
207  pos.xPhi( pos.xPhi() + lorentzShift );
208  Eigen::Matrix<float,3,1> posG(element.surface().localToGlobal(pos).cast<float>());
209 
210  if (!element.isBarrel()) {
211  const InDetDD::StripStereoAnnulusDesign& annulusDesign =
212  dynamic_cast<const InDetDD::StripStereoAnnulusDesign&>
213  (design);
214  pos = annulusDesign.localPositionOfCellPC(element.cellIdOfPosition(pos));
215  }
216 
217  return std::make_pair(Eigen::Matrix<float,1,1>(pos.xPhi()),
218  std::move(posG));
219 }
220 
221 
222 // N.B. the cluster is added to the container
225  double lorentzShift,
226  Eigen::Matrix<float,1,1>& localCov,
227  const StripID& stripID,
228  const InDetDD::SiDetectorElement& element,
229  const InDetDD::SiDetectorDesign& design,
230  xAOD::StripCluster& cl) const
231 {
232  std::size_t size = cluster.ids.size();
233 
234  auto [localPos, globalPos]
235  = computePosition(cluster, size, lorentzShift, stripID, element, design);
236 
237  // For Strip Clusters the identifier is taken from the front rod list object
238  // This is the same strategy used in Athena:
239  // Since clusterId is arbitary (it only needs to be unique) just use ID of first strip
240  // For strip Cluster it has been found that "identifierOfPosition" does not produces unique values
241  cl.setMeasurement<1>(element.identifyHash(), localPos, localCov);
242  cl.setIdentifier( cluster.ids.front() );
243 
244  // Do I really need the global position in fast tracking?
245  cl.globalPosition() = globalPos;
246 
247  cl.setChannelsInPhi(size);
248  cl.setRDOlist(std::move(cluster.ids));
249 
250  return StatusCode::SUCCESS;
251 }
252 
253 
254 bool StripClusteringTool::passTiming(const std::bitset<3>& timePattern) const {
255  // Convert the given timebin to a bit set and test each bit
256  // if bit is -1 (i.e. X) it always passes, other wise require exact match of 0/1
257  // N.B bitset has opposite order to the bit pattern we define
258  if (m_timeBinBits[0] != -1 and timePattern.test(2) != static_cast<bool>(m_timeBinBits[0])) return false;
259  if (m_timeBinBits[1] != -1 and timePattern.test(1) != static_cast<bool>(m_timeBinBits[1])) return false;
260  if (m_timeBinBits[2] != -1 and timePattern.test(0) != static_cast<bool>(m_timeBinBits[2])) return false;
261  return true;
262 }
263 
264 
265 bool StripClusteringTool::isBadStrip(const EventContext& ctx,
266  const InDet::SiDetectorElementStatus *stripDetElStatus,
267  const StripID& stripID,
268  IdentifierHash waferHash,
269  Identifier stripId) const
270 {
271  if (stripDetElStatus) {
272  const int strip_i{stripID.strip(stripId)};
274  stripDetElStatus,
275  stripDetElStatus->isCellGood(waferHash.value(), strip_i),
276  m_conditionsTool->isGood(stripId, InDetConditions::SCT_STRIP));
277  return not stripDetElStatus->isCellGood(waferHash.value(), strip_i) ;
278  }
279  return not m_conditionsTool->isGood(stripId, InDetConditions::SCT_STRIP, ctx);
280 }
281 
282 
283 std::optional<std::pair<typename IStripClusteringTool::CellCollection, bool>>
284 StripClusteringTool::unpackRDOs(const EventContext& ctx,
285  const RawDataCollection& RDOs,
286  const InDet::SiDetectorElementStatus& stripDetElStatus,
287  const InDetDD::SiDetectorElement& element) const
288 {
289  const InDetDD::SiDetectorDesign& design = element.design();
290 
291  CellCollection cells;
292  // reserve memory. number evaluated on ttbar pu200
293  cells.reserve(60);
294  bool badStripOnModule{false};
295 
296  std::size_t ncells = static_cast<size_t>(dynamic_cast<const InDetDD::SCT_ModuleSideDesign&>(design).cells());
297 
298  // Simple single-entry cache
299  Identifier::value_type waferId_compact_cache = 0;
300  IdentifierHash waferHash_cache(0);
301  bool cache_valid = false;
302 
303  for (const StripRDORawData * raw : RDOs) {
304  const SCT3_RawData* raw3 = dynamic_cast<const SCT3_RawData*>(raw);
305  if (!raw3) {
306  ATH_MSG_ERROR("Casting into SCT3_RawData failed");
307  return {};
308  }
309 
310  std::bitset<3> timePattern(raw3->getTimeBin());
311  if (!passTiming(timePattern)) {
312  ATH_MSG_DEBUG("Strip failed timing check");
313  continue;
314  }
315 
316  Identifier firstStripId = raw->identify();
317  Identifier waferId = m_stripID->wafer_id(firstStripId);
318 
319  Identifier::value_type waferId_compact = waferId.get_compact();
320 
321 
322  // Check cache - will be invalid when switching wafer groups
323  if (!cache_valid || waferId_compact != waferId_compact_cache) {
324  waferId_compact_cache = waferId_compact;
325  waferHash_cache = m_stripID->wafer_hash(waferId);
326  cache_valid = true;
327  }
328 
329  std::size_t iFirstStrip = static_cast<size_t>(m_stripID->strip(firstStripId));
330 
331  std::size_t iMaxStrip = std::min(
332  iFirstStrip + raw->getGroupSize(),
333  ncells
334  );
335 
336  for (size_t i = iFirstStrip; i < iMaxStrip; i++) {
337  Identifier stripIdent = m_stripID->strip_id(waferId, i);
338  if (isBadStrip(ctx, &stripDetElStatus, *m_stripID, waferHash_cache, stripIdent)) {
339  // Bad strip, throw it out to minimize useless work.
340  ATH_MSG_DEBUG("Bad strip encountered:" << stripIdent
341  << ", wafer is: " << waferId);
342  badStripOnModule = true;
343  } else {
344  // Good strip!
345  cells.emplace_back(i, stripIdent, std::move(timePattern));
346  }
347  }
348  }
349 
350  return std::make_pair(std::move(cells), badStripOnModule);
351 }
352 
353 } // namespace ActsTrk
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
RunTileCalibRec.cells
cells
Definition: RunTileCalibRec.py:280
ActsTrk::StripClusteringTool::unpackRDOs
std::optional< std::pair< typename IStripClusteringTool::CellCollection, bool > > unpackRDOs(const EventContext &ctx, const RawDataCollection &RDOs, const InDet::SiDetectorElementStatus &stripDetElStatus, const InDetDD::SiDetectorElement &element) const
Definition: StripClusteringTool.cxx:284
ActsTrk::StripClusteringTool::makeClusters
virtual StatusCode makeClusters(const EventContext &ctx, typename IStripClusteringTool::ClusterCollection &cluster, const InDetDD::SiDetectorElement &element, typename ClusterContainer::iterator itrContainer) const override
Definition: StripClusteringTool.cxx:143
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
InDetDD::SolidStateDetectorElementBase::cellIdOfPosition
SiCellId cellIdOfPosition(const Amg::Vector2D &localPos) const
As in previous method but returns SiCellId.
Definition: SolidStateDetectorElementBase.cxx:224
ReadCellNoiseFromCool.cell
cell
Definition: ReadCellNoiseFromCool.py:53
ActsTrk::StripClusteringTool::isBadStrip
bool isBadStrip(const EventContext &ctx, const InDet::SiDetectorElementStatus *sctDetElStatus, const StripID &idHelper, IdentifierHash waferHash, Identifier stripId) const
Definition: StripClusteringTool.cxx:265
ActsTrk::StripClusteringTool::m_stripID
const StripID * m_stripID
Definition: StripClusteringTool.h:101
ActsTrk::StripClusteringTool::m_lorentzAngleTool
ToolHandle< ISiLorentzAngleTool > m_lorentzAngleTool
Definition: StripClusteringTool.h:78
Surface.h
SCT_ModuleSideDesign.h
InDetDD::SCT_ModuleSideDesign
Definition: SCT_ModuleSideDesign.h:40
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
InDetDD::DetectorDesign::localPositionOfCell
virtual SiLocalPosition localPositionOfCell(const SiCellId &cellId) const =0
readout or diode id -> position.
ActsTrk::IStripClusteringTool::Cluster
Definition: IStripClusteringTool.h:42
StripClusteringTool.h
InDetDD::SolidStateDetectorElementBase::surface
Trk::Surface & surface()
Element Surface.
Identifier::get_compact
value_type get_compact() const
Get the compact id.
SCT_RDORawData
Definition: SCT_RDORawData.h:24
InDet::SiDetectorElementStatus::isCellGood
bool isCellGood(IdentifierHash hash, unsigned short cell_i) const
Definition: SiDetectorElementStatus.h:107
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
ActsTrk::StripClusteringTool::m_checkBadModules
Gaudi::Property< bool > m_checkBadModules
Definition: StripClusteringTool.h:89
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
InDetDD::SolidStateDetectorElementBase::identifyHash
virtual IdentifierHash identifyHash() const override final
identifier hash (inline)
ActsTrk::StripClusteringTool::makeCluster
StatusCode makeCluster(StripClusteringTool::Cluster &cluster, double LorentzShift, Eigen::Matrix< float, 1, 1 > &localCov, const StripID &stripID, const InDetDD::SiDetectorElement &element, const InDetDD::SiDetectorDesign &design, xAOD::StripCluster &container) const
Definition: StripClusteringTool.cxx:224
InDetDD::SiLocalPosition
Definition: SiLocalPosition.h:31
ActsTrk::StripClusteringTool::m_timeBinBits
int m_timeBinBits[3]
Definition: StripClusteringTool.h:98
ActsTrk::StripClusteringTool::clusterize
virtual StatusCode clusterize(const EventContext &ctx, const InDetRawDataCollection< StripRDORawData > &RDOs, const InDet::SiDetectorElementStatus &stripDetElStatus, const InDetDD::SiDetectorElement &element, std::vector< typename IStripClusteringTool::ClusterCollection > &collection) const override
Definition: StripClusteringTool.cxx:88
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SCT3_RawData.h
VALIDATE_STATUS_ARRAY
#define VALIDATE_STATUS_ARRAY(use_info, info_val, summary_val)
Definition: SiDetectorElementStatus.h:51
lumiFormat.i
int i
Definition: lumiFormat.py:85
InDet::SiDetectorElementStatus
Definition: SiDetectorElementStatus.h:62
SCT3_RawData
Definition: SCT3_RawData.h:24
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
InDetDD::StripStereoAnnulusDesign
Definition: StripStereoAnnulusDesign.h:50
calibdata.exception
exception
Definition: calibdata.py:496
test_pyathena.parent
parent
Definition: test_pyathena.py:15
xAOD::StripCluster_v1
Definition: StripCluster_v1.h:17
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SCT3_RawData::getTimeBin
int getTimeBin() const
Definition: SCT3_RawData.h:92
SCT_ID::wafer_hash
IdentifierHash wafer_hash(const Identifier &wafer_id) const
wafer hash from id - optimized
Definition: SCT_ID.h:492
ActsTrk::StripClusteringTool::initialize
virtual StatusCode initialize() override
Definition: StripClusteringTool.cxx:52
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
ActsTrk::StripClusteringTool::m_maxFiredStrips
Gaudi::Property< unsigned int > m_maxFiredStrips
Definition: StripClusteringTool.h:92
ActsTrk::StripClusteringTool::decodeTimeBins
StatusCode decodeTimeBins()
Definition: StripClusteringTool.cxx:68
python.PyKernel.detStore
detStore
Definition: PyKernel.py:41
InDet::SiDetectorElementStatus::isGood
bool isGood(IdentifierHash hash) const
Definition: SiDetectorElementStatus.h:97
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
ActsTrk::StripClusteringTool::m_timeBinStr
StringProperty m_timeBinStr
Definition: StripClusteringTool.h:76
ActsTrk::StripClusteringTool::m_stripDetElStatus
SG::ReadHandleKey< InDet::SiDetectorElementStatus > m_stripDetElStatus
Definition: StripClusteringTool.h:83
InDetDD::SiDetectorElement
Definition: SiDetectorElement.h:109
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
InDetDD::SiDetectorElement::isBarrel
bool isBarrel() const
IdentifierHash::value
value_type value() const
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
ActsTrk::StripClusteringTool::passTiming
bool passTiming(const std::bitset< 3 > &timePattern) const
Definition: StripClusteringTool.cxx:254
InDetDD::SiCellId
Definition: SiCellId.h:29
StripStereoAnnulusDesign.h
ActsTrk::StripClusteringTool::m_stripDetEleCollKey
SG::ReadCondHandleKey< InDetDD::SiDetectorElementCollection > m_stripDetEleCollKey
Definition: StripClusteringTool.h:95
SCT_ID
Definition: SCT_ID.h:68
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
SCT_ID::strip
int strip(const Identifier &id) const
Definition: SCT_ID.h:764
RunTileMonitoring.clusters
clusters
Definition: RunTileMonitoring.py:133
Identifier::value_type
unsigned long long value_type
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:27
ActsTrk::ONE_TWELFTH
constexpr double ONE_TWELFTH
Definition: StripClusteringTool.cxx:20
ActsTrk
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
Definition: MSTrackingVolumeBuilder.cxx:24
SCT_ID::wafer_id
Identifier wafer_id(int barrel_ec, int layer_disk, int phi_module, int eta_module, int side) const
For a single side of module.
Definition: SCT_ID.h:464
IdentifierHash
This is a "hash" representation of an Identifier. This encodes a 32 bit index which can be used to lo...
Definition: IdentifierHash.h:25
InDetDD::SiDetectorDesign
Definition: SiDetectorDesign.h:50
InDetConditions::SCT_STRIP
@ SCT_STRIP
Definition: InDetHierarchy.h:14
ActsTrk::StripClusteringTool::m_conditionsTool
ToolHandle< IInDetConditionsTool > m_conditionsTool
Definition: StripClusteringTool.h:86
InDetDD::SiDetectorElement::design
virtual const SiDetectorDesign & design() const override final
access to the local description (inline):
InDetDD::StripStereoAnnulusDesign::localPositionOfCellPC
SiLocalPosition localPositionOfCellPC(const SiCellId &cellId) const
This is for debugging only.
Definition: StripStereoAnnulusDesign.cxx:428
dq_make_web_display.cl
cl
print [x.__class__ for x in toList(dqregion.getSubRegions()) ]
Definition: dq_make_web_display.py:26
InDetDD::DetectorDesign::phiPitch
virtual double phiPitch() const =0
Pitch in phi direction.
InDetDD::SolidStateDetectorElementBase::identify
virtual Identifier identify() const override final
identifier of this detector element (inline)
ActsTrk::StripClusteringTool::StripClusteringTool
StripClusteringTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: StripClusteringTool.cxx:46
Trk::Surface::localToGlobal
virtual void localToGlobal(const Amg::Vector2D &locp, const Amg::Vector3D &mom, Amg::Vector3D &glob) const =0
Specified by each surface type: LocalToGlobal method without dynamic memory allocation.
ActsTrk::IStripClusteringTool::ClusterCollection
std::vector< Cluster > ClusterCollection
Definition: IStripClusteringTool.h:46
SCT_ID::strip_id
Identifier strip_id(int barrel_ec, int layer_disk, int phi_module, int eta_module, int side, int strip) const
For an individual strip.
Definition: SCT_ID.h:535
Identifier
Definition: IdentifierFieldParser.cxx:14