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 // Gaudi
0005 #include "Gaudi/Property.h"
0006 #include "GaudiAlg/GaudiAlgorithm.h"
0007 #include "GaudiAlg/GaudiTool.h"
0008 #include "GaudiAlg/Transformer.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 #include "Acts/Geometry/TrackingGeometry.hpp"
0025 #include "Acts/Plugins/DD4hep/DD4hepDetectorElement.hpp"
0026 #include "Acts/Surfaces/Surface.hpp"
0027 
0028 #include "ActsExamples/EventData/GeometryContainers.hpp"
0029 #include "ActsExamples/EventData/Index.hpp"
0030 #include "ActsExamples/EventData/IndexSourceLink.hpp"
0031 #include "ActsExamples/EventData/Measurement.hpp"
0032 #include "ActsExamples/EventData/ProtoTrack.hpp"
0033 
0034 #include "edm4eic/TrackerHitCollection.h"
0035 
0036 namespace Jug::Reco {
0037 
0038 /** Single Track source Linker and proto tracks.
0039  *
0040  * This algorithm assumes only single track events.
0041  *
0042  * \ingroup tracking
0043  */
0044 class SingleTrackSourceLinker : public GaudiAlgorithm {
0045 private:
0046   DataHandle<edm4eic::TrackerHitCollection> m_inputHitCollection{"inputHitCollection", Gaudi::DataHandle::Reader, this};
0047   DataHandle<std::list<ActsExamples::IndexSourceLink>> m_sourceLinkStorage{"sourceLinkStorage", Gaudi::DataHandle::Writer, this};
0048   DataHandle<ActsExamples::IndexSourceLinkContainer> m_outputSourceLinks{"outputSourceLinks", Gaudi::DataHandle::Writer, this};
0049   DataHandle<ActsExamples::MeasurementContainer> m_outputMeasurements{"outputMeasurements", Gaudi::DataHandle::Writer, this};
0050   DataHandle<ActsExamples::ProtoTrackContainer> m_outputProtoTracks{"outputProtoTracks", Gaudi::DataHandle::Writer, this};
0051   /// Pointer to the geometry service
0052   SmartIF<IGeoSvc> m_geoSvc;
0053   SmartIF<IActsGeoSvc> m_actsGeoSvc;
0054   std::shared_ptr<const dd4hep::rec::CellIDPositionConverter> m_converter;
0055 
0056 public:
0057   SingleTrackSourceLinker(const std::string& name, ISvcLocator* svcLoc) : GaudiAlgorithm(name, svcLoc) {
0058     declareProperty("inputHitCollection", m_inputHitCollection, "");
0059     declareProperty("sourceLinkStorage", m_sourceLinkStorage, "");
0060     declareProperty("outputSourceLinks", m_outputSourceLinks, "");
0061     declareProperty("outputMeasurements", m_outputMeasurements, "");
0062     declareProperty("outputProtoTracks", m_outputProtoTracks, "");
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 and SimSvc in the right order 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 and SimSvc in the right order in the configuration." << endmsg;
0079       return StatusCode::FAILURE;
0080     }
0081     m_converter = std::make_shared<const dd4hep::rec::CellIDPositionConverter>(*(m_geoSvc->getDetector()));
0082     return StatusCode::SUCCESS;
0083   }
0084 
0085   StatusCode execute() override {
0086     // input collection
0087     const edm4eic::TrackerHitCollection* hits = m_inputHitCollection.get();
0088     // Create output collections
0089     auto* linkStorage  = m_sourceLinkStorage.createAndPut();
0090     auto* sourceLinks  = m_outputSourceLinks.createAndPut();
0091     auto* measurements = m_outputMeasurements.createAndPut();
0092     auto* protoTracks  = m_outputProtoTracks.createAndPut();
0093     // IndexMultimap<ActsFatras::Barcode> hitParticlesMap;
0094     // IndexMultimap<Index> hitSimHitsMap;
0095     sourceLinks->reserve(hits->size());
0096     measurements->reserve(hits->size());
0097 
0098     // assume single track --> one ProtoTrack
0099     ActsExamples::ProtoTrack track;
0100     track.reserve((*hits).size());
0101 
0102     if (msgLevel(MSG::DEBUG)) {
0103       debug() << (*hits).size() << " hits " << endmsg;
0104     }
0105     int ihit = 0;
0106     for (const auto& ahit : *hits) {
0107 
0108       track.emplace_back(ihit);
0109 
0110       const auto* vol_ctx = m_converter->findContext(ahit.getCellID());
0111       auto vol_id   = vol_ctx->identifier;
0112       const auto is = m_actsGeoSvc->surfaceMap().find(vol_id);
0113       if (is == m_actsGeoSvc->surfaceMap().end()) {
0114         if (msgLevel(MSG::DEBUG)) {
0115           debug() << " vol_id (" << vol_id << ")  not found in m_surfaces!!!!" << endmsg;
0116         }
0117         continue;
0118       }
0119       const Acts::Surface* surface = is->second;
0120 
0121       // NOTE
0122       // Here is where the important hit and tracking geometry is connected.
0123       // A "Measurement" is constructed to for each hit which makes the connection to
0124       // the tracking surface and covariance matrix
0125 
0126       auto acts_pos = surface
0127                           ->globalToLocal(Acts::GeometryContext(),
0128                                           {ahit.getPosition().x, ahit.getPosition().y, ahit.getPosition().z}, {0, 0, 0})
0129                           .value();
0130 
0131       if (msgLevel(MSG::DEBUG)) {
0132         auto volman  = m_geoSvc->getDetector()->volumeManager();
0133         auto detelem = volman.lookupDetElement(vol_id);
0134         auto local_pos =
0135             detelem.nominal().worldToLocal({ahit.getPosition().x, ahit.getPosition().y, ahit.getPosition().z});
0136         debug() << "===== Debugging hit =====" << endmsg;
0137         debug() << "DD4hep global pos (" << ahit.getPosition().x << "," << ahit.getPosition().y << ","
0138                 << ahit.getPosition().z << ")" << endmsg;
0139         debug() << "DD4hep local  pos (" << local_pos.x() << "," << local_pos.y() << "," << local_pos.z() << ")"
0140                 << endmsg;
0141         debug() << "ACTS local position : (" << acts_pos[0] << "," << acts_pos[1] << ")" << endmsg;
0142         debug() << "ACTS surface center : " << surface->center(Acts::GeometryContext()).transpose() << endmsg;
0143         debug() << "DD4hep DetElement center : "
0144                 << detelem.nominal().localToWorld(detelem.placement().position()) / dd4hep::mm << endmsg;
0145       }
0146       // construct the vector of measured parameters (2d getPosition in this case)
0147       Acts::Vector2 pos(acts_pos.x(), acts_pos.y());
0148 
0149       // construct the covariance matrix
0150       Acts::SquareMatrix2 cov = Acts::SquareMatrix2::Zero();
0151       cov(0, 0)            = ahit.getPositionError().xx * Acts::UnitConstants::mm * Acts::UnitConstants::mm;
0152       cov(1, 1)            = ahit.getPositionError().yy * Acts::UnitConstants::mm * Acts::UnitConstants::mm;
0153 
0154       // Above we only consider the two position coordinates the comment below shows how to add time
0155       // which we will probably want to try later.
0156       //
0157       // Acts::SquareMatrix3 cov;
0158       // cov << 0.05, 0., 0., 0., 0.05, 0., 0., 0., 900. * Acts::UnitConstants::ps * Acts::UnitConstants::ps;
0159       // Acts::Vector3 par(localX, localY, simHit.time());
0160 
0161       linkStorage->emplace_back(surface->geometryId(), ihit);
0162       ActsExamples::IndexSourceLink& sourceLink = linkStorage->back();
0163       auto meas =
0164           Acts::makeMeasurement(Acts::SourceLink{sourceLink}, pos, cov, Acts::eBoundLoc0, Acts::eBoundLoc1);
0165 
0166       // add to output containers. since the input is already geometry-order,
0167       // new elements in geometry containers can just be appended at the end.
0168       sourceLinks->emplace_hint(sourceLinks->end(), sourceLink);
0169       measurements->emplace_back(std::move(meas));
0170 
0171       ihit++;
0172     }
0173     // add proto track to the output collection
0174     protoTracks->emplace_back(std::move(track));
0175     return StatusCode::SUCCESS;
0176   }
0177 };
0178 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
0179 DECLARE_COMPONENT(SingleTrackSourceLinker)
0180 
0181 } // namespace Jug::Reco