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/RootEventWriter.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <array>
0010 
0011 #include "corecel/Macros.hh"
0012 #include "celeritas/ext/RootFileManager.hh"
0013 #include "celeritas/phys/Primary.hh"
0014 
0015 #include "EventIOInterface.hh"
0016 
0017 namespace celeritas
0018 {
0019 class ParticleParams;
0020 
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Export primary data to ROOT.
0024  *
0025  * One TTree entry represents one primary.
0026  */
0027 class RootEventWriter : public EventWriterInterface
0028 {
0029   public:
0030     //!@{
0031     //! \name Type aliases
0032     using SPConstParticles = std::shared_ptr<ParticleParams const>;
0033     using SPRootFileManager = std::shared_ptr<RootFileManager>;
0034     //!@}
0035 
0036     // Construct with ROOT output filename
0037     RootEventWriter(SPRootFileManager root_file_manager,
0038                     SPConstParticles params);
0039 
0040     //! Prevent copying and moving
0041     CELER_DELETE_COPY_MOVE(RootEventWriter);
0042     ~RootEventWriter() override = default;
0043 
0044     // Export primaries to ROOT
0045     void operator()(VecPrimary const& primaries) override;
0046 
0047   private:
0048     //// DATA ////
0049 
0050     // Basic data types stored to ROOT to avoid the need of a dictionary
0051     struct RootOffloadPrimary
0052     {
0053         std::size_t event_id;
0054         std::size_t track_id;
0055         int particle;
0056         double energy;
0057         double time;
0058         std::array<double, 3> pos;
0059         std::array<double, 3> dir;
0060     };
0061 
0062     SPRootFileManager tfile_mgr_;
0063     SPConstParticles params_;
0064     size_type event_id_;  // Contiguous event id
0065     UPRootTreeWritable ttree_;
0066     RootOffloadPrimary primary_;  // Temporary object stored to the ROOT TTree
0067     bool warned_mismatched_events_{false};
0068 
0069     //// HELPER FUNCTIONS ////
0070 
0071     // Hardcoded TTree name and title
0072     char const* tree_name() { return "primaries"; }
0073 };
0074 
0075 //---------------------------------------------------------------------------//
0076 #if !CELERITAS_USE_ROOT
0077 inline RootEventWriter::RootEventWriter(SPRootFileManager, SPConstParticles)
0078 {
0079     CELER_DISCARD(tfile_mgr_);
0080     CELER_DISCARD(params_);
0081     CELER_DISCARD(event_id_);
0082     CELER_DISCARD(ttree_);
0083     CELER_DISCARD(primary_);
0084     CELER_DISCARD(warned_mismatched_events_);
0085     CELER_NOT_CONFIGURED("ROOT");
0086 }
0087 
0088 inline void RootEventWriter::operator()(VecPrimary const&)
0089 {
0090     CELER_ASSERT_UNREACHABLE();
0091 }
0092 #endif
0093 
0094 //---------------------------------------------------------------------------//
0095 }  // namespace celeritas