ATLAS Offline Software
SegmentCnvAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 #include "SegmentCnvAlg.h"
6 
12 
13 namespace MuonR4{
14 
16  ATH_CHECK(m_idHelperSvc.retrieve());
17 
18  ATH_CHECK(m_keyTgc.initialize(!m_keyTgc.empty()));
19  ATH_CHECK(m_keyRpc.initialize(!m_keyRpc.empty()));
20  ATH_CHECK(m_keyMdt.initialize(!m_keyMdt.empty()));
21  ATH_CHECK(m_keysTgc.initialize(!m_keysTgc.empty()));
22  ATH_CHECK(m_keyMM.initialize(!m_keyMM.empty()));
23  ATH_CHECK(m_readKeys.initialize());
24  ATH_CHECK(m_writeKey.initialize());
25  ATH_CHECK(m_mdtCreator.retrieve());
26  ATH_CHECK(m_clusterCreator.retrieve());
27  ATH_CHECK(m_compClusterCreator.retrieve(EnableTool{!m_keyTgc.empty() || !m_keyRpc.empty()}));
29  return StatusCode::SUCCESS;
30  }
31  template <class ContainerType>
32  StatusCode SegmentCnvAlg::retrieveContainer(const EventContext& ctx,
34  const ContainerType*& contToPush) const {
35  contToPush = nullptr;
36  if (key.empty()) {
37  ATH_MSG_VERBOSE("No key has been parsed for object "<< typeid(ContainerType).name());
38  return StatusCode::SUCCESS;
39  }
40  SG::ReadHandle readHandle{key, ctx};
41  ATH_CHECK(readHandle.isPresent());
42  contToPush = readHandle.cptr();
43  return StatusCode::SUCCESS;
44  }
45 
46 
47  StatusCode SegmentCnvAlg::execute(const EventContext& ctx) const {
48 
49  auto translatedSegments = std::make_unique<Trk::SegmentCollection>();
51  const SegmentContainer* translateMe{nullptr};
52  ATH_CHECK(retrieveContainer(ctx, key, translateMe));
53  for (const Segment* segment : *translateMe) {
54  ATH_CHECK(convert(ctx, *segment, *translatedSegments));
55  }
56 
57  }
58  ATH_MSG_VERBOSE("Translated in total "<<translatedSegments->size()<<" segments.");
59 
61  ATH_CHECK(writeHandle.record(std::move(translatedSegments)));
62  return StatusCode::SUCCESS;
63  }
64  template <class PrdType>
65  const PrdType* SegmentCnvAlg::fetchPrd(const Identifier& prdId,
66  const Muon::MuonPrepDataContainerT<PrdType>* prdContainer) const {
67  if (!prdContainer) {
68  ATH_MSG_ERROR("Cannot fetch a prep data object as the container given for "<<
69  m_idHelperSvc->toString(prdId)<<" is a nullptr");
70  return nullptr;
71  }
72  const Muon::MuonPrepDataCollection<PrdType>* coll = prdContainer->indexFindPtr(m_idHelperSvc->moduleHash(prdId));
73  if (!coll) {
74  ATH_MSG_ERROR("No prep data collection where "<<m_idHelperSvc->toString(prdId)<<" can reside in.");
75  return nullptr;
76  }
77  for (const PrdType* prd : *coll) {
78  if (prd->identify() == prdId){
79  return prd;
80  }
81  }
82  ATH_MSG_ERROR("There is no measurement "<<m_idHelperSvc->toString(prdId));
83 
84  return nullptr;
85  }
86  template <class PrdType>
88  const CalibratedSpacePoint& spacePoint,
89  const Muon::MuonPrepDataContainerT<PrdType>* prdContainer,
90  std::vector<std::unique_ptr<Trk::RIO_OnTrack>>& convMeasVec) const {
91  bool added{false};
92 
93  for (const xAOD::UncalibratedMeasurement* uncalib: {spacePoint.spacePoint()->primaryMeasurement(),
94  spacePoint.spacePoint()->secondaryMeasurement()}){
95  if (!uncalib) continue;
96  added = true;
97 
98  const PrdType* prd = fetchPrd(xAOD::identify(uncalib), prdContainer);
99  if (!prd) {
100  ATH_MSG_FATAL("Failed to retrieve segment from "<<m_idHelperSvc->toString(xAOD::identify(uncalib)));
101  return StatusCode::FAILURE;
102  }
103  const Trk::Surface& surf{prd->detectorElement()->surface(prd->identify())};
104  Trk::Intersection isect = surf.straightLineIntersection(segment.position(), segment.direction());
105 
106  std::unique_ptr<Trk::RIO_OnTrack> rot{};
107  if constexpr(std::is_same_v<PrdType, Muon::MdtPrepData>) {
108  rot = std::unique_ptr<Trk::RIO_OnTrack>{m_mdtCreator->createRIO_OnTrack(*prd,
109  isect.position,
110  &segment.direction())};
111  } else {
112  rot = std::unique_ptr<Trk::RIO_OnTrack>{m_clusterCreator->createRIO_OnTrack(*prd,
113  isect.position,
114  segment.direction())};
115  }
116  if (!rot) {
117  ATH_MSG_ERROR("Failed to create rot from "<<m_idHelperSvc->toString(prd->identify()));
118  return StatusCode::FAILURE;
119  }
120  ATH_MSG_VERBOSE("Created ROT "<<m_printer->print(*rot));
121  convMeasVec.push_back(std::move(rot));
122  }
123  if (!added) {
124  ATH_MSG_ERROR("Could not translate space point "<<m_idHelperSvc->toString(spacePoint.spacePoint()->identify()));
125  return StatusCode::FAILURE;
126  }
127  return StatusCode::SUCCESS;
128  }
129 
130  StatusCode SegmentCnvAlg::convert(const EventContext& ctx,
131  const MuonR4::Segment& segment,
132  Trk::SegmentCollection& outContainer) const {
133 
134  const Muon::RpcPrepDataContainer* rpcPrds{nullptr};
135  const Muon::MdtPrepDataContainer* mdtPrds{nullptr};
136  const Muon::TgcPrepDataContainer* tgcPrds{nullptr};
137  const Muon::sTgcPrepDataContainer* stgcPrds{nullptr};
138  const Muon::MMPrepDataContainer* mmPrds{nullptr};
139 
140  ATH_CHECK(retrieveContainer(ctx, m_keyMdt, mdtPrds));
141  ATH_CHECK(retrieveContainer(ctx, m_keyRpc, rpcPrds));
142  ATH_CHECK(retrieveContainer(ctx, m_keyTgc, tgcPrds));
143  ATH_CHECK(retrieveContainer(ctx, m_keysTgc, stgcPrds));
144  ATH_CHECK(retrieveContainer(ctx, m_keyMM, mmPrds));
145 
146  const ActsGeometryContext* gctx{nullptr};
148 
149  std::vector<std::unique_ptr<Trk::RIO_OnTrack>> rots{};
150  unsigned int nPrec{0};
151  for (const Segment::MeasType& spacePoint : segment.measurements()){
152  switch (spacePoint->type()) {
154  ATH_CHECK(convertMeasurement(segment, *spacePoint, mdtPrds, rots));
155  ++nPrec;
156  break;
157  }
159  ATH_CHECK(convertMeasurement(segment,*spacePoint, rpcPrds, rots));
160  break;
161  }
163  ATH_CHECK(convertMeasurement(segment,*spacePoint, tgcPrds, rots));
164  break;
165  }
167  ATH_CHECK(convertMeasurement(segment,*spacePoint, mmPrds, rots));
168  ++nPrec;
169  break;
170  }
172  ATH_CHECK(convertMeasurement(segment,*spacePoint, stgcPrds, rots));
173  ++nPrec;
174  break;
175  }
177  break;
178  default:
179  ATH_MSG_WARNING("Unsupported measurement type ");
180  }
181  }
182 
184  auto makeCompetingROT = [this, &measurements](RotVec& rots) {
185  if (rots.empty()){
186  return;
187  }
188  std::list<const Trk::PrepRawData*> prds{};
189  for (const std::unique_ptr<Trk::RIO_OnTrack>& rot : rots) {
190  prds.push_back(rot->prepRawData());
191  }
192  measurements.push_back(m_compClusterCreator->createBroadCluster(std::move(prds),0.));
193  rots.clear();
194  };
195 
196  RotVec etaPrds{}, phiPrds{};
197  for (std::unique_ptr<Trk::RIO_OnTrack>& rot : rots) {
198  const Trk::PrepRawData* prd = rot->prepRawData();
200  std::vector<std::unique_ptr<Trk::RIO_OnTrack>>& pushMe{m_idHelperSvc->measuresPhi(rot->identify())? phiPrds : etaPrds};
201  if (pushMe.size() && pushMe.back()->detectorElement() != rot->detectorElement()){
202  makeCompetingROT(pushMe);
203  } else {
204  pushMe.push_back(std::move(rot));
205  }
206 
207  } else {
208  makeCompetingROT(etaPrds);
209  makeCompetingROT(phiPrds);
210  measurements.push_back(std::move(rot));
211  }
212  }
213  makeCompetingROT(etaPrds);
214  makeCompetingROT(phiPrds);
215  if (!nPrec) {
216  ATH_MSG_WARNING("No precision hit on "<<std::endl<<m_printer->print(measurements.stdcont())
217  <<". Do not convert segment due to potential puff.");
218  return StatusCode::SUCCESS;
219  }
220  ATH_MSG_DEBUG("Fetched in total "<<measurements.size()<<" measurements. "<<std::endl<<
221  m_printer->print(measurements.stdcont()));
223  const Amg::Transform3D& locToGlob{segment.msSector()->localToGlobalTrans(*gctx)};
224  auto segSurf = std::make_unique<Trk::PlaneSurface>(Amg::getTransformFromRotTransl(locToGlob.linear(), segment.position()));
225  Trk::LocalDirection segDir{};
226  segSurf->globalToLocalDirection(segment.direction(), segDir);
227 
228  auto fitQuality = std::make_unique<Trk::FitQuality>(segment.chi2(),
229  static_cast<double>(segment.nDoF()));
230 
231  Amg::MatrixX covMatrix(4, 4);
232  covMatrix.setIdentity();
233  using namespace MuonR4::SegmentFit;
235  covMatrix(0, 0) = segment.covariance()(toInt(ParamDefs::x0), toInt(ParamDefs::x0));
236  covMatrix(1, 1) = segment.covariance()(toInt(ParamDefs::y0), toInt(ParamDefs::y0));
237 
238  covMatrix(2, 2) = segment.covariance()(toInt(ParamDefs::phi), toInt(ParamDefs::phi));
240 
241  auto legacySeg = std::make_unique<Muon::MuonSegment>(Amg::Vector2D::Zero(),std::move(segDir),
242  std::move(covMatrix), segSurf.release(),
243  std::move(measurements), fitQuality.release());
244 
245 
246  ATH_MSG_VERBOSE(m_printer->print(*legacySeg)<<", pos: "<<Amg::toString(legacySeg->globalPosition())<<" "
247  <<Amg::toString(legacySeg->globalDirection())<<std::endl<<m_printer->print(legacySeg->containedMeasurements()));
248  outContainer.push_back(std::move(legacySeg));
249  return StatusCode::SUCCESS;
250  }
251 }
MuonR4::SegmentCnvAlg::fetchPrd
const PrdType * fetchPrd(const Identifier &prdId, const Muon::MuonPrepDataContainerT< PrdType > *prdContainer) const
Definition: SegmentCnvAlg.cxx:65
Muon::MuonPrepDataContainer
Template for Muon PRD containers (which are basically collections of MuonPrepDataCollections).
Definition: MuonPrepDataContainer.h:42
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
MuonR4::SegmentCnvAlg::m_keysTgc
SG::ReadHandleKey< Muon::sTgcPrepDataContainer > m_keysTgc
Definition: SegmentCnvAlg.h:75
Trk::PrepRawDataType::TgcPrepData
@ TgcPrepData
MuonR4::SegmentCnvAlg::m_compClusterCreator
ToolHandle< Muon::IMuonCompetingClustersOnTrackCreator > m_compClusterCreator
Definition: SegmentCnvAlg.h:90
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
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
MuonR4::SegmentCnvAlg::m_printer
PublicToolHandle< Muon::MuonEDMPrinterTool > m_printer
Definition: SegmentCnvAlg.h:70
CompetingMuonClustersOnTrack.h
MuonR4::SegmentCnvAlg::convertMeasurement
StatusCode convertMeasurement(const MuonR4::Segment &segment, const CalibratedSpacePoint &spacePoint, const Muon::MuonPrepDataContainerT< PrdType > *prdContainer, RotVec &convMeasVec) const
xAOD::identify
Identifier identify(const UncalibratedMeasurement *meas)
Returns the associated identifier.
Definition: MuonSpectrometer/MuonPhaseII/Event/xAOD/xAODMuonPrepData/Root/UtilFunctions.cxx:61
MuonR4::SpacePoint::secondaryMeasurement
const xAOD::UncalibratedMeasurement * secondaryMeasurement() const
Definition: MuonSpectrometer/MuonPhaseII/Event/MuonSpacePoint/src/SpacePoint.cxx:110
MuonR4::SegmentCnvAlg::m_mdtCreator
ToolHandle< Muon::IMdtDriftCircleOnTrackCreator > m_mdtCreator
Definition: SegmentCnvAlg.h:87
Trk::PrepRawData::type
virtual bool type(PrepRawDataType type) const =0
Interface method checking the type.
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::ReadHandleKey< ContainerType >
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
Definition: MuonSpectrometer/MuonPhaseII/Event/MuonPatternEvent/MuonPatternEvent/Segment.h:22
MuonR4::SegmentCnvAlg::m_readKeys
SG::ReadHandleKeyArray< SegmentContainer > m_readKeys
Definition: SegmentCnvAlg.h:82
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
MuonR4::SegmentCnvAlg::m_keyRpc
SG::ReadHandleKey< Muon::RpcPrepDataContainer > m_keyRpc
Definition: SegmentCnvAlg.h:73
MuonR4::SegmentCnvAlg::m_writeKey
SG::WriteHandleKey< Trk::SegmentCollection > m_writeKey
Definition: SegmentCnvAlg.h:84
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
MuonR4::SegmentCnvAlg::RotVec
std::vector< std::unique_ptr< Trk::RIO_OnTrack > > RotVec
Definition: SegmentCnvAlg.h:60
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
xAOD::covMatrix
covMatrix
Definition: TrackMeasurement_v1.cxx:19
Amg::Transform3D
Eigen::Affine3d Transform3D
Definition: GeoPrimitives.h:46
xAOD::Other
@ Other
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonR4::SegmentCnvAlg::m_keyTgc
SG::ReadHandleKey< Muon::TgcPrepDataContainer > m_keyTgc
Prep data container keys.
Definition: SegmentCnvAlg.h:72
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:581
MuonR4::SegmentCnvAlg::m_keyMdt
SG::ReadHandleKey< Muon::MdtPrepDataContainer > m_keyMdt
Definition: SegmentCnvAlg.h:74
Trk::LocalDirection
represents the three-dimensional global direction with respect to a planar surface frame.
Definition: LocalDirection.h:81
ActsGeometryContext
Include the GeoPrimitives which need to be put first.
Definition: ActsGeometryContext.h:27
Trk::PrepRawData
Definition: PrepRawData.h:62
MuonR4::SegmentFit::ParamDefs::y0
@ y0
MuonR4::SegmentCnvAlg::m_geoCtxKey
SG::ReadHandleKey< ActsGeometryContext > m_geoCtxKey
Definition: SegmentCnvAlg.h:79
MuonR4::SegmentCnvAlg::retrieveContainer
StatusCode retrieveContainer(const EventContext &ctx, const SG::ReadHandleKey< ContType > &key, const ContType *&contPtr) const
Loads a container from the StoreGate and returns whether the retrieval is successful.
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
MuonR4::SegmentFit::toInt
constexpr int toInt(const ParamDefs p)
Definition: MuonHoughDefs.h:42
MuonR4::SegmentCnvAlg::initialize
StatusCode initialize() override final
Definition: SegmentCnvAlg.cxx:15
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
MuonR4::SegmentCnvAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: SegmentCnvAlg.h:67
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:845
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
MuonR4::SegmentCnvAlg::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: SegmentCnvAlg.cxx:47
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:292
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
SegmentCnvAlg.h
MuonR4::SegmentCnvAlg::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: SegmentCnvAlg.cxx:130
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
PlaneSurface.h
GeoPrimitivesHelpers.h
MuonR4::CalibratedSpacePoint
The calibrated Space point is created during the calibration process.
Definition: CalibratedSpacePoint.h:15
MuonSegment.h
FitQuality.h
Trk::Surface
Definition: Tracking/TrkDetDescr/TrkSurfaces/TrkSurfaces/Surface.h:75
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
xAOD::UncalibMeasType::RpcStripType
@ RpcStripType
xAOD::UncalibMeasType::MdtDriftCircleType
@ MdtDriftCircleType
MuonR4::SegmentCnvAlg::m_keyMM
SG::ReadHandleKey< Muon::MMPrepDataContainer > m_keyMM
Definition: SegmentCnvAlg.h:76
MuonR4::SpacePoint::identify
const Identifier & identify() const
: Identifier of the primary measurement
Definition: MuonSpectrometer/MuonPhaseII/Event/MuonSpacePoint/src/SpacePoint.cxx:140
MuonR4::SegmentFit::ParamDefs::theta
@ theta
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
MuonR4::SegmentCnvAlg::m_clusterCreator
ToolHandle< Muon::IMuonClusterOnTrackCreator > m_clusterCreator
Definition: SegmentCnvAlg.h:88
Identifier
Definition: IdentifierFieldParser.cxx:14