Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:11:37

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file celeritas/optical/action/DetectorAction.hh
0006 //! \sa DetectorAction.test.cc
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <vector>
0011 
0012 #include "corecel/cont/Span.hh"
0013 #include "corecel/data/AuxInterface.hh"
0014 #include "corecel/data/PinnedAllocator.hh"
0015 #include "celeritas/inp/Scoring.hh"
0016 #include "celeritas/optical/DetectorData.hh"
0017 
0018 #include "ActionInterface.hh"
0019 
0020 namespace celeritas
0021 {
0022 class ActionRegistry;
0023 class AuxParamsRegistry;
0024 
0025 namespace optical
0026 {
0027 //---------------------------------------------------------------------------//
0028 /*!
0029  * Record sensitive detector data for optical photons at the end of every step.
0030  *
0031  * The \c DetectorExecutor is responsible for copying hit data for every photon
0032  * into the state buffer at the end of every step on a kernel level. Even if a
0033  * track was not in a detector, it is still copied into the state buffer with
0034  * an invalid detector ID. All hits are copied into a persistent pinned-memory
0035  * buffer (allocated once as auxiliary state, sized to the number of track
0036  * slots), where invalid hits are pruned by slicing a span over the valid
0037  * prefix. A span of only valid hits is then passed into the user provided
0038  * callback function.
0039  */
0040 class DetectorAction final : public OpticalStepActionInterface,
0041                              public AuxParamsInterface
0042 {
0043   public:
0044     //!@{
0045     //! \name Type aliases
0046     using SPActionRegistry = std::shared_ptr<ActionRegistry>;
0047     using SPAuxParamsRegistry = std::shared_ptr<AuxParamsRegistry>;
0048     using CallbackFunc = inp::OpticalDetector::HitCallbackFunc;
0049     //!@}
0050 
0051   public:
0052     // Create and add to core params
0053     static std::shared_ptr<DetectorAction> make_and_insert(
0054         SPActionRegistry const&,
0055         SPAuxParamsRegistry const&,
0056         CallbackFunc const&);
0057 
0058     // Construct with action ID, aux ID, and callback function
0059     DetectorAction(ActionId, AuxId, CallbackFunc const&);
0060 
0061     // Launch kernel with host data
0062     void step(CoreParams const&, CoreStateHost&) const final;
0063 
0064     // Launch kernel with device data
0065     void step(CoreParams const&, CoreStateDevice&) const final;
0066 
0067     //! Dependency ordering of the action
0068     StepActionOrder order() const final { return StepActionOrder::post; }
0069 
0070     //!@{
0071     //! \name Action interface
0072 
0073     //! ID of the action
0074     ActionId action_id() const final { return sad_.action_id(); }
0075     //! Short name for the action (also satisfies AuxParamsInterface::label)
0076     std::string_view label() const final { return sad_.label(); }
0077     //! Description of the action
0078     std::string_view description() const final { return sad_.description(); }
0079     //!@}
0080 
0081     //!@{
0082     //! \name Aux interface
0083 
0084     //! Index of this class instance in its registry
0085     AuxId aux_id() const final { return aux_id_; }
0086     // Build auxiliary state data for a stream
0087     UPState create_state(MemSpace, StreamId, size_type) const final;
0088     //!@}
0089 
0090   private:
0091     //// DATA ////
0092 
0093     StaticActionData sad_;
0094     AuxId aux_id_;
0095     CallbackFunc callback_;
0096 
0097     //// HELPER FUNCTIONS ////
0098 
0099     // Copy hits from device
0100     Span<DetectorHit const> load_hits_sync(CoreStateDevice const&) const;
0101 
0102     // Send hits to the callback
0103     void callback_hits(Span<DetectorHit const>) const;
0104 };
0105 
0106 //---------------------------------------------------------------------------//
0107 /*!
0108  * Persistent per-stream storage for pruned detector hits on device.
0109  *
0110  * The pinned buffer is allocated and sized once in \c
0111  * DetectorAction::create_state so that no reallocation occurs during
0112  * stepping.
0113  *
0114  * This is \em only done when running on device; in host memory, we operate
0115  * directly on the hit vector.
0116  */
0117 struct DetectorActionState : public AuxStateInterface
0118 {
0119     using VecHit = std::vector<DetectorHit, PinnedAllocator<DetectorHit>>;
0120 
0121     VecHit hits;
0122 };
0123 
0124 //---------------------------------------------------------------------------//
0125 }  // namespace optical
0126 }  // namespace celeritas