ATLAS Offline Software
TrkSegmentCnvAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 #include "TrkSegmentCnvAlg.h"
6 
13 
14 
15 namespace MuonR4{
16 
18  ATH_CHECK(m_idHelperSvc.retrieve());
19 
20  ATH_CHECK(m_keyTgc.initialize(!m_keyTgc.empty()));
21  ATH_CHECK(m_keyRpc.initialize(!m_keyRpc.empty()));
22  ATH_CHECK(m_keyMdt.initialize(!m_keyMdt.empty()));
23  ATH_CHECK(m_keysTgc.initialize(!m_keysTgc.empty()));
24  ATH_CHECK(m_keyMM.initialize(!m_keyMM.empty()));
25  ATH_CHECK(m_readKeys.initialize());
26  ATH_CHECK(m_writeKey.initialize());
27  ATH_CHECK(m_mdtCreator.retrieve());
28  ATH_CHECK(m_clusterCreator.retrieve());
29  ATH_CHECK(m_compClusterCreator.retrieve(EnableTool{!m_keyTgc.empty() || !m_keyRpc.empty()}));
31  return StatusCode::SUCCESS;
32  }
33 
34  StatusCode TrkSegmentCnvAlg::execute(const EventContext& ctx) const {
35 
36  auto translatedSegments = std::make_unique<Trk::SegmentCollection>();
38  const SegmentContainer* translateMe{nullptr};
39  ATH_CHECK(SG::get(translateMe, key, ctx));
40  for (const Segment* segment : *translateMe) {
41  ATH_CHECK(convert(ctx, *segment, *translatedSegments));
42  }
43 
44  }
45  ATH_MSG_VERBOSE("Translated in total "<<translatedSegments->size()<<" segments.");
46 
47  SG::WriteHandle writeHandle{m_writeKey, ctx};
48  ATH_CHECK(writeHandle.record(std::move(translatedSegments)));
49  return StatusCode::SUCCESS;
50  }
51  template <class PrdType>
52  const PrdType* TrkSegmentCnvAlg::fetchPrd(const Identifier& prdId,
53  const Muon::MuonPrepDataContainerT<PrdType>* prdContainer) const {
54  if (!prdContainer) {
55  ATH_MSG_ERROR("Cannot fetch a prep data object as the container given for "<<
56  m_idHelperSvc->toString(prdId)<<" is a nullptr");
57  return nullptr;
58  }
59  const Muon::MuonPrepDataCollection<PrdType>* coll = prdContainer->indexFindPtr(m_idHelperSvc->moduleHash(prdId));
60  if (!coll) {
61  ATH_MSG_ERROR("No prep data collection where "<<m_idHelperSvc->toString(prdId)<<" can reside in.");
62  return nullptr;
63  }
64  for (const PrdType* prd : *coll) {
65  if (prd->identify() == prdId){
66  return prd;
67  }
68  }
69  ATH_MSG_ERROR("There is no measurement "<<m_idHelperSvc->toString(prdId));
70 
71  return nullptr;
72  }
73  template <class PrdType>
75  const CalibratedSpacePoint& spacePoint,
76  const Muon::MuonPrepDataContainerT<PrdType>* prdContainer,
77  std::vector<std::unique_ptr<Trk::RIO_OnTrack>>& convMeasVec) const {
78  bool added{false};
79 
80  for (const xAOD::UncalibratedMeasurement* uncalib: {spacePoint.spacePoint()->primaryMeasurement(),
81  spacePoint.spacePoint()->secondaryMeasurement()}){
82  if (!uncalib) continue;
83  added = true;
84 
85  const PrdType* prd = fetchPrd(xAOD::identify(uncalib), prdContainer);
86  if (!prd) {
87  ATH_MSG_FATAL("Failed to retrieve segment from "<<m_idHelperSvc->toString(xAOD::identify(uncalib)));
88  return StatusCode::FAILURE;
89  }
90  const Trk::Surface& surf{prd->detectorElement()->surface(prd->identify())};
91  Trk::Intersection isect = surf.straightLineIntersection(segment.position(), segment.direction());
92 
93  std::unique_ptr<Trk::RIO_OnTrack> rot{};
94  if constexpr(std::is_same_v<PrdType, Muon::MdtPrepData>) {
95  rot = std::unique_ptr<Trk::RIO_OnTrack>{m_mdtCreator->createRIO_OnTrack(*prd,
96  isect.position,
97  &segment.direction())};
98  } else {
99  rot = std::unique_ptr<Trk::RIO_OnTrack>{m_clusterCreator->createRIO_OnTrack(*prd,
100  isect.position,
101  segment.direction())};
102  }
103  if (!rot) {
104  ATH_MSG_ERROR("Failed to create rot from "<<m_idHelperSvc->toString(prd->identify()));
105  return StatusCode::FAILURE;
106  }
107  ATH_MSG_VERBOSE("Created ROT "<<m_printer->print(*rot));
108  convMeasVec.push_back(std::move(rot));
109  }
110  if (!added) {
111  ATH_MSG_ERROR("Could not translate space point "<<m_idHelperSvc->toString(spacePoint.spacePoint()->identify()));
112  return StatusCode::FAILURE;
113  }
114  return StatusCode::SUCCESS;
115  }
116 
117  StatusCode TrkSegmentCnvAlg::convert(const EventContext& ctx,
118  const MuonR4::Segment& segment,
119  Trk::SegmentCollection& outContainer) const {
120 
121  const Muon::RpcPrepDataContainer* rpcPrds{nullptr};
122  const Muon::MdtPrepDataContainer* mdtPrds{nullptr};
123  const Muon::TgcPrepDataContainer* tgcPrds{nullptr};
124  const Muon::sTgcPrepDataContainer* stgcPrds{nullptr};
125  const Muon::MMPrepDataContainer* mmPrds{nullptr};
126 
127  ATH_CHECK(SG::get(mdtPrds, m_keyMdt, ctx));
128  ATH_CHECK(SG::get(rpcPrds, m_keyRpc, ctx));
129  ATH_CHECK(SG::get(tgcPrds, m_keyTgc, ctx));
130  ATH_CHECK(SG::get(stgcPrds, m_keysTgc, ctx));
131  ATH_CHECK(SG::get(mmPrds, m_keyMM, ctx));
132 
133  const ActsGeometryContext* gctx{nullptr};
134  ATH_CHECK(SG::get(gctx, m_geoCtxKey, ctx));
135 
136  std::vector<std::unique_ptr<Trk::RIO_OnTrack>> rots{};
137  unsigned int nPrec{0};
138  for (const Segment::MeasType& spacePoint : segment.measurements()){
139  if (spacePoint->fitState() != CalibratedSpacePoint::State::Valid) {
140  ATH_MSG_VERBOSE("'Reject invalid measurement'");
141  continue;
142  }
143  switch (spacePoint->type()) {
145  ATH_CHECK(convertMeasurement(segment, *spacePoint, mdtPrds, rots));
146  ++nPrec;
147  break;
148  }
150  ATH_CHECK(convertMeasurement(segment,*spacePoint, rpcPrds, rots));
151  break;
152  }
154  ATH_CHECK(convertMeasurement(segment,*spacePoint, tgcPrds, rots));
155  break;
156  }
158  ATH_CHECK(convertMeasurement(segment,*spacePoint, mmPrds, rots));
159  ++nPrec;
160  break;
161  }
163  ATH_CHECK(convertMeasurement(segment,*spacePoint, stgcPrds, rots));
164  ++nPrec;
165  break;
166  }
168  break;
169  default:
170  ATH_MSG_WARNING("Unsupported measurement type ");
171  }
172  }
173 
175  auto makeCompetingROT = [this, &measurements](RotVec& rots) {
176  if (rots.empty()){
177  return;
178  }
179  std::list<const Trk::PrepRawData*> prds{};
180  for (const std::unique_ptr<Trk::RIO_OnTrack>& rot : rots) {
181  prds.push_back(rot->prepRawData());
182  }
183  measurements.push_back(m_compClusterCreator->createBroadCluster(std::move(prds),0.));
184  rots.clear();
185  };
186 
187  RotVec etaPrds{}, phiPrds{};
188  for (std::unique_ptr<Trk::RIO_OnTrack>& rot : rots) {
189  const Trk::PrepRawData* prd = rot->prepRawData();
191  std::vector<std::unique_ptr<Trk::RIO_OnTrack>>& pushMe{m_idHelperSvc->measuresPhi(rot->identify())? phiPrds : etaPrds};
192  if (pushMe.size() && pushMe.back()->detectorElement() != rot->detectorElement()){
193  makeCompetingROT(pushMe);
194  } else {
195  pushMe.push_back(std::move(rot));
196  }
197 
198  } else {
199  makeCompetingROT(etaPrds);
200  makeCompetingROT(phiPrds);
201  measurements.push_back(std::move(rot));
202  }
203  }
204  makeCompetingROT(etaPrds);
205  makeCompetingROT(phiPrds);
206  if (!nPrec) {
207  ATH_MSG_WARNING("No precision hit on "<<std::endl<<m_printer->print(measurements.stdcont())
208  <<". Do not convert segment due to potential puff.");
209  return StatusCode::SUCCESS;
210  }
211  ATH_MSG_DEBUG("Fetched in total "<<measurements.size()<<" measurements. "<<std::endl<<
212  m_printer->print(measurements.stdcont()));
214  const Amg::Transform3D& locToGlob{segment.msSector()->localToGlobalTrans(*gctx)};
215  auto segSurf = std::make_unique<Trk::PlaneSurface>(Amg::getTransformFromRotTransl(locToGlob.linear(), segment.position()));
216  Trk::LocalDirection segDir{};
217  segSurf->globalToLocalDirection(segment.direction(), segDir);
218  std::vector<Identifier> holes{};
219  auto fitQuality = std::make_unique<Muon::MuonSegmentQuality>(segment.chi2(),
220  static_cast<double>(segment.nDoF()),
221  std::move(holes));
222 
223  Amg::MatrixX covMatrix(4, 4);
224  covMatrix.setIdentity();
225  using namespace MuonR4::SegmentFit;
228 
231 
232  auto legacySeg = std::make_unique<Muon::MuonSegment>(Amg::Vector2D::Zero(), std::move(segDir),
233  std::move(covMatrix), segSurf.release(),
234  std::move(measurements), fitQuality.release());
235 
236  ATH_MSG_VERBOSE(m_printer->print(*legacySeg)<<", pos: "<<Amg::toString(legacySeg->globalPosition())<<" "
237  <<Amg::toString(legacySeg->globalDirection())<<std::endl<<m_printer->print(legacySeg->containedMeasurements()));
238  outContainer.push_back(std::move(legacySeg));
239  return StatusCode::SUCCESS;
240  }
241 }
Muon::MuonPrepDataContainer
Template for Muon PRD containers (which are basically collections of MuonPrepDataCollections).
Definition: MuonPrepDataContainer.h:42
xAOD::identify
const Identifier & identify(const UncalibratedMeasurement *meas)
Returns the associated identifier from the muon measurement.
Definition: MuonSpectrometer/MuonPhaseII/Event/xAOD/xAODMuonPrepData/Root/UtilFunctions.cxx:82
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
MuonR4::SegmentFit
Definition: MuonHoughDefs.h:28
Amg::MatrixX
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Dynamic Matrix - dynamic allocation.
Definition: EventPrimitives.h:27
Trk::Intersection
Definition: Intersection.h:24
TrackParameters.h
Trk::PrepRawDataType::TgcPrepData
@ TgcPrepData
Trk::locX
@ locX
Definition: ParamDefs.h:37
Trk::locY
@ locY
local cartesian
Definition: ParamDefs.h:38
MuonR4::TrkSegmentCnvAlg::m_clusterCreator
ToolHandle< Muon::IMuonClusterOnTrackCreator > m_clusterCreator
Definition: TrkSegmentCnvAlg.h:77
InDetDD::holes
@ holes
Definition: InDetDD_Defs.h:17
MuonR4::TrkSegmentCnvAlg::m_keyMM
SG::ReadHandleKey< Muon::MMPrepDataContainer > m_keyMM
Definition: TrkSegmentCnvAlg.h:65
MuonR4::Segment
Placeholder for what will later be the muon segment EDM representation.
Definition: MuonSpectrometer/MuonPhaseII/Event/MuonPatternEvent/MuonPatternEvent/Segment.h:19
xAOD::UncalibMeasType::MMClusterType
@ MMClusterType
CompetingMuonClustersOnTrack.h
MuonR4::TrkSegmentCnvAlg::m_writeKey
SG::WriteHandleKey< Trk::SegmentCollection > m_writeKey
Definition: TrkSegmentCnvAlg.h:73
MuonR4::SpacePoint::secondaryMeasurement
const xAOD::UncalibratedMeasurement * secondaryMeasurement() const
Definition: MuonSpectrometer/MuonPhaseII/Event/MuonSpacePoint/src/SpacePoint.cxx:50
MuonR4::TrkSegmentCnvAlg::m_printer
PublicToolHandle< Muon::MuonEDMPrinterTool > m_printer
Definition: TrkSegmentCnvAlg.h:59
MuonSegmentQuality.h
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
xAOD::UncalibMeasType::sTgcStripType
@ sTgcStripType
Trk::PrepRawDataType::RpcPrepData
@ RpcPrepData
MuonR4::SegmentFit::ParamDefs::phi
@ phi
xAOD::UncalibMeasType::TgcStripType
@ TgcStripType
Amg::getTransformFromRotTransl
Amg::Transform3D getTransformFromRotTransl(Amg::RotationMatrix3D rot, Amg::Vector3D transl_vec)
Definition: GeoPrimitivesHelpers.h:172
MuonR4::Segment::MeasType
std::unique_ptr< CalibratedSpacePoint > MeasType
Calibrated space point type.
Definition: MuonSpectrometer/MuonPhaseII/Event/MuonPatternEvent/MuonPatternEvent/Segment.h:23
xAOD::UncalibratedMeasurement_v1
Definition: UncalibratedMeasurement_v1.h:13
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:287
Trk::theta
@ theta
Definition: ParamDefs.h:66
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
MuonR4::TrkSegmentCnvAlg::fetchPrd
const PrdType * fetchPrd(const Identifier &prdId, const Muon::MuonPrepDataContainerT< PrdType > *prdContainer) const
Definition: TrkSegmentCnvAlg.cxx:52
xAOD::covMatrix
covMatrix
Definition: TrackMeasurement_v1.cxx:19
Trk::PrepRawData::type
virtual bool type(PrepRawDataType type) const
Interface method checking the type.
Definition: PrepRawData.h:133
MuonR4::TrkSegmentCnvAlg::initialize
StatusCode initialize() override final
Definition: TrkSegmentCnvAlg.cxx:17
Amg::Transform3D
Eigen::Affine3d Transform3D
Definition: GeoPrimitives.h:46
MuonR4::TrkSegmentCnvAlg::m_keysTgc
SG::ReadHandleKey< Muon::sTgcPrepDataContainer > m_keysTgc
Definition: TrkSegmentCnvAlg.h:64
MuonR4::TrkSegmentCnvAlg::RotVec
std::vector< std::unique_ptr< Trk::RIO_OnTrack > > RotVec
Definition: TrkSegmentCnvAlg.h:49
MuonR4::TrkSegmentCnvAlg::m_geoCtxKey
SG::ReadHandleKey< ActsGeometryContext > m_geoCtxKey
Definition: TrkSegmentCnvAlg.h:68
xAOD::Other
@ Other
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
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
MuonR4::SegmentFit::ParamDefs::x0
@ x0
Trk::Intersection::position
Amg::Vector3D position
Definition: Intersection.h:25
Muon::MuonPrepDataCollection
Template to hold collections of MuonPrepRawData objects.
Definition: MuonPrepDataCollection.h:46
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
Trk::LocalDirection
represents the three-dimensional global direction with respect to a planar surface frame.
Definition: LocalDirection.h:81
MuonR4::TrkSegmentCnvAlg::m_keyTgc
SG::ReadHandleKey< Muon::TgcPrepDataContainer > m_keyTgc
Prep data container keys.
Definition: TrkSegmentCnvAlg.h:61
ActsGeometryContext
Include the GeoPrimitives which need to be put first.
Definition: ActsGeometryContext.h:27
MuonR4::CalibratedSpacePoint::State::Valid
@ Valid
Trk::PrepRawData
Definition: PrepRawData.h:62
MuonR4::SegmentFit::ParamDefs::y0
@ y0
MuonR4::TrkSegmentCnvAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: TrkSegmentCnvAlg.h:56
MuonR4::SegmentFit::toInt
constexpr int toInt(const ParamDefs p)
Definition: MuonHoughDefs.h:42
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
MuonR4::TrkSegmentCnvAlg::convert
StatusCode convert(const EventContext &ctx, const MuonR4::Segment &segment, Trk::SegmentCollection &outContainer) const
Convert the R4 segment and fill the converted segment into the SegmentCollection.
Definition: TrkSegmentCnvAlg.cxx:117
Trk::GsfMeasurementUpdator::fitQuality
FitQualityOnSurface fitQuality(const MultiComponentState &, const MeasurementBase &)
Method for determining the chi2 of the multi-component state and the number of degrees of freedom.
Definition: GsfMeasurementUpdator.cxx:842
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
IdentifiableContainerMT::indexFindPtr
virtual const T * indexFindPtr(IdentifierHash hashId) const override final
return pointer on the found entry or null if out of range using hashed index - fast version,...
Definition: IdentifiableContainerMT.h:289
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
MuonR4::TrkSegmentCnvAlg::m_readKeys
SG::ReadHandleKeyArray< SegmentContainer > m_readKeys
Definition: TrkSegmentCnvAlg.h:71
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TrkSegmentCnvAlg.h
PlaneSurface.h
GeoPrimitivesHelpers.h
MuonR4::CalibratedSpacePoint
The calibrated Space point is created during the calibration process.
Definition: CalibratedSpacePoint.h:15
MuonR4::TrkSegmentCnvAlg::m_keyMdt
SG::ReadHandleKey< Muon::MdtPrepDataContainer > m_keyMdt
Definition: TrkSegmentCnvAlg.h:63
MuonSegment.h
MuonR4::TrkSegmentCnvAlg::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: TrkSegmentCnvAlg.cxx:34
MuonR4::TrkSegmentCnvAlg::m_mdtCreator
ToolHandle< Muon::IMdtDriftCircleOnTrackCreator > m_mdtCreator
Definition: TrkSegmentCnvAlg.h:76
FitQuality.h
Trk::Surface
Definition: Tracking/TrkDetDescr/TrkSurfaces/TrkSurfaces/Surface.h:79
MuonR4::CalibratedSpacePoint::spacePoint
const SpacePoint * spacePoint() const
The pointer to the space point out of which this space point has been built.
Definition: CalibratedSpacePoint.cxx:18
MuonR4::TrkSegmentCnvAlg::m_keyRpc
SG::ReadHandleKey< Muon::RpcPrepDataContainer > m_keyRpc
Definition: TrkSegmentCnvAlg.h:62
xAOD::UncalibMeasType::RpcStripType
@ RpcStripType
xAOD::UncalibMeasType::MdtDriftCircleType
@ MdtDriftCircleType
MuonR4::TrkSegmentCnvAlg::convertMeasurement
StatusCode convertMeasurement(const MuonR4::Segment &segment, const CalibratedSpacePoint &spacePoint, const Muon::MuonPrepDataContainerT< PrdType > *prdContainer, RotVec &convMeasVec) const
Trk::phi0
@ phi0
Definition: ParamDefs.h:65
MuonR4::SpacePoint::identify
const Identifier & identify() const
: Identifier of the primary measurement
Definition: MuonSpectrometer/MuonPhaseII/Event/MuonSpacePoint/src/SpacePoint.cxx:80
MuonR4::SegmentFit::ParamDefs::theta
@ theta
MuonR4::TrkSegmentCnvAlg::m_compClusterCreator
ToolHandle< Muon::IMuonCompetingClustersOnTrackCreator > m_compClusterCreator
Definition: TrkSegmentCnvAlg.h:79
generate::Zero
void Zero(TH1D *hin)
Definition: generate.cxx:32
NSWL1::PadTriggerAdapter::segment
Muon::NSW_PadTriggerSegment segment(const NSWL1::PadTrigger &data)
Definition: PadTriggerAdapter.cxx:5
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
Identifier
Definition: IdentifierFieldParser.cxx:14