Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:09:07

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/io/RootEventReader.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <string>
0010 
0011 #include "corecel/Macros.hh"
0012 #include "celeritas/ext/RootUniquePtr.hh"
0013 #include "celeritas/phys/Primary.hh"
0014 
0015 #include "EventIOInterface.hh"
0016 
0017 namespace celeritas
0018 {
0019 class ParticleParams;
0020 
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Read ROOT file generated by \c RootEventWriter .
0024  *
0025  * Each \c operator() call returns a vector of primaries from a single event.
0026  * \code
0027     RootEventReader read("primaries.root", particle_params);
0028     RootEventReader::result_type event;
0029     while (event = read(), !event.empty())
0030     {
0031         // Do stuff
0032     }
0033  * \endcode
0034  */
0035 class RootEventReader : public EventReaderInterface
0036 {
0037   public:
0038     //!@{
0039     //! \name Type aliases
0040     using SPConstParticles = std::shared_ptr<ParticleParams const>;
0041     using result_type = std::vector<Primary>;
0042     //!@}
0043 
0044     // Construct with ROOT filename
0045     RootEventReader(std::string const& filename, SPConstParticles params);
0046 
0047     //! Prevent copying and moving
0048     CELER_DELETE_COPY_MOVE(RootEventReader);
0049     ~RootEventReader() override = default;
0050 
0051     // Read a user-defined event from the ROOT file
0052     result_type operator()(EventId event_id);
0053 
0054     // Read a single event from the ROOT file
0055     result_type operator()() final;
0056 
0057     //! Get total number of events
0058     size_type num_events() const final { return num_events_; }
0059 
0060   private:
0061     //// DATA ////
0062 
0063     SPConstParticles params_;
0064     UPExtern<TFile> tfile_;
0065     UPExtern<TTree> ttree_;
0066     std::size_t num_entries_;  // Total number of entries in the TTree
0067     size_type num_events_;  // Total number of events
0068     std::size_t entry_count_{0};  // Current TTree entry
0069     EventId expected_event_id_{0};  // Last event ID read
0070     std::vector<std::size_t> event_to_entry_{0};  // Cache event tree entry ids
0071 
0072     //// HELPER FUNCTIONS ////
0073 
0074     // Hardcoded ROOT TTree name defined by RootEventWriter
0075     char const* tree_name() { return "primaries"; }
0076 };
0077 
0078 //---------------------------------------------------------------------------//
0079 #if !CELERITAS_USE_ROOT
0080 inline RootEventReader::RootEventReader(std::string const&, SPConstParticles)
0081 {
0082     CELER_DISCARD(params_);
0083     CELER_DISCARD(tfile_);
0084     CELER_DISCARD(ttree_);
0085     CELER_DISCARD(num_entries_);
0086     CELER_DISCARD(num_events_);
0087     CELER_DISCARD(entry_count_);
0088     CELER_DISCARD(expected_event_id_);
0089     CELER_DISCARD(event_to_entry_);  // NOLINT(bugprone-sizeof-container)
0090     CELER_NOT_CONFIGURED("ROOT");
0091 }
0092 
0093 inline RootEventReader::result_type RootEventReader::operator()()
0094 {
0095     CELER_ASSERT_UNREACHABLE();
0096 }
0097 #endif
0098 
0099 //---------------------------------------------------------------------------//
0100 }  // namespace celeritas