Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:24

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file celeritas/io/RootEventReader.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <string>
0011 
0012 #include "corecel/Macros.hh"
0013 #include "celeritas/ext/RootUniquePtr.hh"
0014 #include "celeritas/phys/Primary.hh"
0015 
0016 #include "EventIOInterface.hh"
0017 
0018 namespace celeritas
0019 {
0020 class ParticleParams;
0021 
0022 //---------------------------------------------------------------------------//
0023 /*!
0024  * Read ROOT file generated by \c RootEventWriter .
0025  *
0026  * Each \c operator() call returns a vector of primaries from a single event.
0027  * \code
0028     RootEventReader read("primaries.root", particle_params);
0029     RootEventReader::result_type event;
0030     while (event = read(), !event.empty())
0031     {
0032         // Do stuff
0033     }
0034  * \endcode
0035  */
0036 class RootEventReader : public EventReaderInterface
0037 {
0038   public:
0039     //!@{
0040     //! \name Type aliases
0041     using SPConstParticles = std::shared_ptr<ParticleParams const>;
0042     using result_type = std::vector<Primary>;
0043     //!@}
0044 
0045     // Construct with ROOT filename
0046     RootEventReader(std::string const& filename, SPConstParticles params);
0047 
0048     //! Prevent copying and moving
0049     CELER_DELETE_COPY_MOVE(RootEventReader);
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_);
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