Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 08:51:55

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2022 Whitney Armstrong, Sylvester Joosten, Wouter Deconinck
0003 
0004 #include "ActsExamples/EventData/GeometryContainers.hpp"
0005 
0006 // Gaudi
0007 #include "Gaudi/Property.h"
0008 #include "Gaudi/Algorithm.h"
0009 #include "GaudiKernel/RndmGenerators.h"
0010 #include "GaudiKernel/ToolHandle.h"
0011 
0012 #include <k4FWCore/DataHandle.h>
0013 #include <k4Interface/IGeoSvc.h>
0014 #include "JugTrack/IActsGeoSvc.h"
0015 
0016 #include "DD4hep/DD4hepUnits.h"
0017 #include "DD4hep/Volumes.h"
0018 #include "DDRec/CellIDPositionConverter.h"
0019 #include "DDRec/Surface.h"
0020 #include "DDRec/SurfaceManager.h"
0021 
0022 #include "Acts/Definitions/Common.hpp"
0023 #include "Acts/Definitions/Units.hpp"
0024 #if Acts_VERSION_MAJOR < 36
0025 #include "Acts/EventData/Measurement.hpp"
0026 #endif
0027 #include "Acts/EventData/MeasurementHelpers.hpp"
0028 #include "Acts/Geometry/TrackingGeometry.hpp"
0029 #include "Acts/Plugins/DD4hep/DD4hepDetectorElement.hpp"
0030 #include "Acts/Surfaces/Surface.hpp"
0031 
0032 #include "ActsExamples/EventData/Index.hpp"
0033 #include "ActsExamples/EventData/IndexSourceLink.hpp"
0034 #include "ActsExamples/EventData/Measurement.hpp"
0035 
0036 #include "edm4eic/TrackerHitCollection.h"
0037 
0038 namespace Jug::Reco {
0039 
0040 /** Source source Linker.
0041  *
0042  * The source linker creates "source links" which map the hit to the tracking surface.
0043  * It also creates "measurements" which take the hit information and creates a corresponding
0044  * "measurement" which contains the covariance matrix and other geometry related hit information.
0045  *
0046  * \ingroup tracking
0047  */
0048 class TrackerSourceLinker : public Gaudi::Algorithm {
0049 private:
0050   mutable DataHandle<edm4eic::TrackerHitCollection> m_inputHitCollection{"inputHitCollection", Gaudi::DataHandle::Reader, this};
0051   mutable DataHandle<std::list<ActsExamples::IndexSourceLink>> m_sourceLinkStorage{"sourceLinkStorage", Gaudi::DataHandle::Writer, this};
0052 #if Acts_VERSION_MAJOR < 37 || (Acts_VERSION_MAJOR == 37 && Acts_VERSION_MINOR == 0)
0053   mutable DataHandle<ActsExamples::IndexSourceLinkContainer> m_outputSourceLinks{"outputSourceLinks", Gaudi::DataHandle::Writer, this};
0054 #endif
0055   mutable DataHandle<ActsExamples::MeasurementContainer> m_outputMeasurements{"outputMeasurements", Gaudi::DataHandle::Writer, this};
0056   /// Pointer to the geometry services
0057   SmartIF<IGeoSvc> m_geoSvc;
0058   SmartIF<IActsGeoSvc> m_actsGeoSvc;
0059   std::shared_ptr<const dd4hep::rec::CellIDPositionConverter> m_converter;
0060 
0061 public:
0062   TrackerSourceLinker(const std::string& name, ISvcLocator* svcLoc) : Gaudi::Algorithm(name, svcLoc) {
0063     declareProperty("inputHitCollection", m_inputHitCollection, "");
0064     declareProperty("sourceLinkStorage", m_sourceLinkStorage, "");
0065 #if Acts_VERSION_MAJOR < 37 || (Acts_VERSION_MAJOR == 37 && Acts_VERSION_MINOR == 0)
0066     declareProperty("outputSourceLinks", m_outputSourceLinks, "");
0067 #endif
0068     declareProperty("outputMeasurements", m_outputMeasurements, "");
0069   }
0070 
0071   StatusCode initialize() override {
0072     if (Gaudi::Algorithm::initialize().isFailure()) {
0073       return StatusCode::FAILURE;
0074     }
0075     m_geoSvc = service("GeoSvc");
0076     if (!m_geoSvc) {
0077       error() << "Unable to locate Geometry Service. "
0078               << "Make sure you have GeoSvc in the right place in the configuration." << endmsg;
0079       return StatusCode::FAILURE;
0080     }
0081     m_actsGeoSvc = service("ActsGeoSvc");
0082     if (!m_actsGeoSvc) {
0083       error() << "Unable to locate ACTS Geometry Service. "
0084               << "Make sure you have ActsGeoSvc in the right place in the configuration." << endmsg;
0085       return StatusCode::FAILURE;
0086     }
0087     m_converter = std::make_shared<const dd4hep::rec::CellIDPositionConverter>(*(m_geoSvc->getDetector()));
0088 
0089     return StatusCode::SUCCESS;
0090   }
0091 
0092   StatusCode execute(const EventContext&) const override {
0093     constexpr double mm_acts = Acts::UnitConstants::mm;
0094     constexpr double mm_conv = mm_acts / dd4hep::mm; // = 1/0.1
0095 
0096     // input collection
0097     const edm4eic::TrackerHitCollection* hits = m_inputHitCollection.get();
0098     // Create output collections
0099     auto* linkStorage  = m_sourceLinkStorage.createAndPut();
0100 #if Acts_VERSION_MAJOR < 37 || (Acts_VERSION_MAJOR == 37 && Acts_VERSION_MINOR == 0)
0101     auto* sourceLinks  = m_outputSourceLinks.createAndPut();
0102     sourceLinks->reserve(hits->size());
0103 #endif
0104     auto* measurements = m_outputMeasurements.createAndPut();
0105     measurements->reserve(hits->size());
0106 
0107     if (msgLevel(MSG::DEBUG)) {
0108       debug() << (*hits).size() << " hits " << endmsg;
0109     }
0110     int ihit = 0;
0111     for (const auto& ahit : *hits) {
0112 
0113       Acts::SquareMatrix2 cov = Acts::SquareMatrix2::Zero();
0114       cov(0, 0)            = ahit.getPositionError().xx * mm_acts * mm_acts; // note mm = 1 (Acts)
0115       cov(1, 1)            = ahit.getPositionError().yy * mm_acts * mm_acts;
0116       if (msgLevel(MSG::DEBUG)) {
0117         debug() << "cov matrix:\n" << cov << endmsg;
0118       }
0119 
0120       const auto* vol_ctx = m_converter->findContext(ahit.getCellID());
0121       auto vol_id = vol_ctx->identifier;
0122 
0123       const auto is = m_actsGeoSvc->surfaceMap().find(vol_id);
0124       if (is == m_actsGeoSvc->surfaceMap().end()) {
0125         error() << " vol_id (" << vol_id << ")  not found in m_surfaces." << endmsg;
0126         continue;
0127       }
0128       const Acts::Surface* surface = is->second;
0129       // variable surf_center not used anywhere;
0130       // auto surf_center = surface->center(Acts::GeometryContext());
0131 
0132       // transform global position into local coordinates
0133       // geometry context contains nothing here
0134       Acts::Vector2 pos =
0135           surface
0136               ->globalToLocal(Acts::GeometryContext(),
0137                               {ahit.getPosition().x, ahit.getPosition().y, ahit.getPosition().z}, {0, 0, 0})
0138               .value();
0139 
0140       Acts::Vector2 loc     = Acts::Vector2::Zero();
0141       loc[Acts::eBoundLoc0] = pos[0];
0142       loc[Acts::eBoundLoc1] = pos[1];
0143 
0144       if (msgLevel(MSG::DEBUG)) {
0145         auto volman         = m_geoSvc->getDetector()->volumeManager();
0146         auto alignment      = volman.lookupDetElement(vol_id).nominal();
0147         auto local_position = (alignment.worldToLocal({ahit.getPosition().x / mm_conv, ahit.getPosition().y / mm_conv,
0148                                                        ahit.getPosition().z / mm_conv})) *
0149                               mm_conv;
0150         debug() << " hit position     : " << ahit.getPosition().x << " " << ahit.getPosition().y << " "
0151                 << ahit.getPosition().z << endmsg;
0152         debug() << " dd4hep loc pos   : " << local_position.x() << " " << local_position.y() << " "
0153                 << local_position.z() << endmsg;
0154         debug() << " surface center   :" << surface->center(Acts::GeometryContext()).transpose() << endmsg;
0155         debug() << " acts local center:" << pos.transpose() << endmsg;
0156         debug() << " acts loc pos     : " << loc[Acts::eBoundLoc0] << ", " << loc[Acts::eBoundLoc1] << endmsg;
0157       }
0158 
0159       // the measurement container is unordered and the index under which the
0160       // measurement will be stored is known before adding it.
0161       //
0162       // variable hitIdx not used anywhere
0163       // Index hitIdx = measurements->size();
0164 
0165       auto geoId = surface->geometryId();
0166 
0167       linkStorage->emplace_back(geoId, ihit);
0168 #if Acts_VERSION_MAJOR < 37 || (Acts_VERSION_MAJOR == 37 && Acts_VERSION_MINOR == 0)
0169       ActsExamples::IndexSourceLink& sourceLink = linkStorage->back();
0170       sourceLinks->emplace_hint(sourceLinks->end(), sourceLink);
0171 #endif
0172 
0173 #if Acts_VERSION_MAJOR > 37 || (Acts_VERSION_MAJOR == 37 && Acts_VERSION_MINOR >= 1)
0174       std::array<Acts::BoundIndices, 2> indices = {Acts::eBoundLoc0, Acts::eBoundLoc1};
0175       Acts::visit_measurement(
0176         indices.size(), [&](auto dim) -> ActsExamples::VariableBoundMeasurementProxy {
0177           if constexpr (dim == indices.size()) {
0178             return ActsExamples::VariableBoundMeasurementProxy{
0179               measurements->emplaceMeasurement<dim>(geoId, indices, loc, cov)
0180             };
0181           } else {
0182             throw std::runtime_error("Dimension not supported in measurement creation");
0183           }
0184         }
0185       );
0186 #elif Acts_VERSION_MAJOR == 37 && Acts_VERSION_MINOR == 0
0187       std::array<Acts::BoundIndices, 2> indices = {Acts::eBoundLoc0, Acts::eBoundLoc1};
0188       Acts::visit_measurement(
0189         indices.size(), [&](auto dim) -> ActsExamples::VariableBoundMeasurementProxy {
0190           if constexpr (dim == indices.size()) {
0191             return ActsExamples::VariableBoundMeasurementProxy{
0192               measurements->emplaceMeasurement<dim>(Acts::SourceLink{sourceLink}, indices, loc, cov)
0193             };
0194           } else {
0195             throw std::runtime_error("Dimension not supported in measurement creation");
0196           }
0197         }
0198       );
0199 #elif Acts_VERSION_MAJOR == 36 && Acts_VERSION_MINOR >= 1
0200       auto measurement = ActsExamples::makeVariableSizeMeasurement(
0201         Acts::SourceLink{sourceLink}, loc, cov, Acts::eBoundLoc0, Acts::eBoundLoc1);
0202       measurements->emplace_back(std::move(measurement));
0203 #elif Acts_VERSION_MAJOR == 36 && Acts_VERSION_MINOR == 0
0204       auto measurement = ActsExamples::makeFixedSizeMeasurement(
0205         Acts::SourceLink{sourceLink}, loc, cov, Acts::eBoundLoc0, Acts::eBoundLoc1);
0206       measurements->emplace_back(std::move(measurement));
0207 #else
0208       auto measurement = Acts::makeMeasurement(
0209         Acts::SourceLink{sourceLink}, loc, cov, Acts::eBoundLoc0, Acts::eBoundLoc1);
0210       measurements->emplace_back(std::move(measurement));
0211 #endif
0212 
0213       ihit++;
0214     }
0215     return StatusCode::SUCCESS;
0216   }
0217 };
0218 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
0219 DECLARE_COMPONENT(TrackerSourceLinker)
0220 
0221 } // namespace Jug::Reco