Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-09-28 07:03:50

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 "GaudiAlg/GaudiAlgorithm.h"
0009 #include "GaudiAlg/GaudiTool.h"
0010 #include "GaudiAlg/Transformer.h"
0011 #include "GaudiKernel/RndmGenerators.h"
0012 #include "GaudiKernel/ToolHandle.h"
0013 
0014 #include <k4FWCore/DataHandle.h>
0015 #include <k4Interface/IGeoSvc.h>
0016 #include "JugTrack/IActsGeoSvc.h"
0017 
0018 #include "DD4hep/DD4hepUnits.h"
0019 #include "DD4hep/Volumes.h"
0020 #include "DDRec/CellIDPositionConverter.h"
0021 #include "DDRec/Surface.h"
0022 #include "DDRec/SurfaceManager.h"
0023 
0024 #include "Acts/Definitions/Common.hpp"
0025 #include "Acts/Definitions/Units.hpp"
0026 #include "Acts/Geometry/TrackingGeometry.hpp"
0027 #include "Acts/Plugins/DD4hep/DD4hepDetectorElement.hpp"
0028 #include "Acts/Surfaces/Surface.hpp"
0029 
0030 #include "ActsExamples/EventData/Index.hpp"
0031 #include "ActsExamples/EventData/IndexSourceLink.hpp"
0032 #include "ActsExamples/EventData/Measurement.hpp"
0033 
0034 #include "edm4eic/TrackerHitCollection.h"
0035 
0036 namespace Jug::Reco {
0037 
0038 /** Source source Linker.
0039  *
0040  * The source linker creates "source links" which map the hit to the tracking surface.
0041  * It also creates "measurements" which take the hit information and creates a corresponding
0042  * "measurement" which contains the covariance matrix and other geometry related hit information.
0043  *
0044  * \ingroup tracking
0045  */
0046 class TrackerSourceLinker : public GaudiAlgorithm {
0047 private:
0048   DataHandle<edm4eic::TrackerHitCollection> m_inputHitCollection{"inputHitCollection", Gaudi::DataHandle::Reader, this};
0049   DataHandle<std::list<ActsExamples::IndexSourceLink>> m_sourceLinkStorage{"sourceLinkStorage", Gaudi::DataHandle::Writer, this};
0050   DataHandle<ActsExamples::IndexSourceLinkContainer> m_outputSourceLinks{"outputSourceLinks", Gaudi::DataHandle::Writer, this};
0051   DataHandle<ActsExamples::MeasurementContainer> m_outputMeasurements{"outputMeasurements", Gaudi::DataHandle::Writer, this};
0052   /// Pointer to the geometry services
0053   SmartIF<IGeoSvc> m_geoSvc;
0054   SmartIF<IActsGeoSvc> m_actsGeoSvc;
0055   std::shared_ptr<const dd4hep::rec::CellIDPositionConverter> m_converter;
0056 
0057 public:
0058   TrackerSourceLinker(const std::string& name, ISvcLocator* svcLoc) : GaudiAlgorithm(name, svcLoc) {
0059     declareProperty("inputHitCollection", m_inputHitCollection, "");
0060     declareProperty("sourceLinkStorage", m_sourceLinkStorage, "");
0061     declareProperty("outputSourceLinks", m_outputSourceLinks, "");
0062     declareProperty("outputMeasurements", m_outputMeasurements, "");
0063   }
0064 
0065   StatusCode initialize() override {
0066     if (GaudiAlgorithm::initialize().isFailure()) {
0067       return StatusCode::FAILURE;
0068     }
0069     m_geoSvc = service("GeoSvc");
0070     if (!m_geoSvc) {
0071       error() << "Unable to locate Geometry Service. "
0072               << "Make sure you have GeoSvc in the right place in the configuration." << endmsg;
0073       return StatusCode::FAILURE;
0074     }
0075     m_actsGeoSvc = service("ActsGeoSvc");
0076     if (!m_actsGeoSvc) {
0077       error() << "Unable to locate ACTS Geometry Service. "
0078               << "Make sure you have ActsGeoSvc in the right place in the configuration." << endmsg;
0079       return StatusCode::FAILURE;
0080     }
0081     m_converter = std::make_shared<const dd4hep::rec::CellIDPositionConverter>(*(m_geoSvc->getDetector()));
0082 
0083     return StatusCode::SUCCESS;
0084   }
0085 
0086   StatusCode execute() override {
0087     constexpr double mm_acts = Acts::UnitConstants::mm;
0088     constexpr double mm_conv = mm_acts / dd4hep::mm; // = 1/0.1
0089 
0090     // input collection
0091     const edm4eic::TrackerHitCollection* hits = m_inputHitCollection.get();
0092     // Create output collections
0093     auto* linkStorage  = m_sourceLinkStorage.createAndPut();
0094     auto* sourceLinks  = m_outputSourceLinks.createAndPut();
0095     auto* measurements = m_outputMeasurements.createAndPut();
0096     sourceLinks->reserve(hits->size());
0097     measurements->reserve(hits->size());
0098 
0099     if (msgLevel(MSG::DEBUG)) {
0100       debug() << (*hits).size() << " hits " << endmsg;
0101     }
0102     int ihit = 0;
0103     for (const auto& ahit : *hits) {
0104 
0105       Acts::SquareMatrix2 cov = Acts::SquareMatrix2::Zero();
0106       cov(0, 0)            = ahit.getPositionError().xx * mm_acts * mm_acts; // note mm = 1 (Acts)
0107       cov(1, 1)            = ahit.getPositionError().yy * mm_acts * mm_acts;
0108       if (msgLevel(MSG::DEBUG)) {
0109         debug() << "cov matrix:\n" << cov << endmsg;
0110       }
0111 
0112       const auto* vol_ctx = m_converter->findContext(ahit.getCellID());
0113       auto vol_id = vol_ctx->identifier;
0114 
0115       const auto is = m_actsGeoSvc->surfaceMap().find(vol_id);
0116       if (is == m_actsGeoSvc->surfaceMap().end()) {
0117         error() << " vol_id (" << vol_id << ")  not found in m_surfaces." << endmsg;
0118         continue;
0119       }
0120       const Acts::Surface* surface = is->second;
0121       // variable surf_center not used anywhere;
0122       // auto surf_center = surface->center(Acts::GeometryContext());
0123 
0124       // transform global position into local coordinates
0125       // geometry context contains nothing here
0126       Acts::Vector2 pos =
0127           surface
0128               ->globalToLocal(Acts::GeometryContext(),
0129                               {ahit.getPosition().x, ahit.getPosition().y, ahit.getPosition().z}, {0, 0, 0})
0130               .value();
0131 
0132       Acts::Vector2 loc     = Acts::Vector2::Zero();
0133       loc[Acts::eBoundLoc0] = pos[0];
0134       loc[Acts::eBoundLoc1] = pos[1];
0135 
0136       if (msgLevel(MSG::DEBUG)) {
0137         auto volman         = m_geoSvc->getDetector()->volumeManager();
0138         auto alignment      = volman.lookupDetElement(vol_id).nominal();
0139         auto local_position = (alignment.worldToLocal({ahit.getPosition().x / mm_conv, ahit.getPosition().y / mm_conv,
0140                                                        ahit.getPosition().z / mm_conv})) *
0141                               mm_conv;
0142         debug() << " hit position     : " << ahit.getPosition().x << " " << ahit.getPosition().y << " "
0143                 << ahit.getPosition().z << endmsg;
0144         debug() << " dd4hep loc pos   : " << local_position.x() << " " << local_position.y() << " "
0145                 << local_position.z() << endmsg;
0146         debug() << " surface center   :" << surface->center(Acts::GeometryContext()).transpose() << endmsg;
0147         debug() << " acts local center:" << pos.transpose() << endmsg;
0148         debug() << " acts loc pos     : " << loc[Acts::eBoundLoc0] << ", " << loc[Acts::eBoundLoc1] << endmsg;
0149       }
0150 
0151       // the measurement container is unordered and the index under which the
0152       // measurement will be stored is known before adding it.
0153       //
0154       // variable hitIdx not used anywhere
0155       // Index hitIdx = measurements->size();
0156       linkStorage->emplace_back(surface->geometryId(), ihit);
0157       ActsExamples::IndexSourceLink& sourceLink = linkStorage->back();
0158       auto meas = Acts::makeMeasurement(Acts::SourceLink{sourceLink}, loc, cov, Acts::eBoundLoc0, Acts::eBoundLoc1);
0159 
0160       // add to output containers. since the input is already geometry-order,
0161       // new elements in geometry containers can just be appended at the end.
0162       sourceLinks->emplace_hint(sourceLinks->end(), sourceLink);
0163       measurements->emplace_back(std::move(meas));
0164 
0165       ihit++;
0166     }
0167     return StatusCode::SUCCESS;
0168   }
0169 };
0170 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
0171 DECLARE_COMPONENT(TrackerSourceLinker)
0172 
0173 } // namespace Jug::Reco