Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:52:22

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/EventWriter.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <string>
0011 
0012 #include "corecel/Config.hh"
0013 
0014 #include "corecel/Assert.hh"
0015 #include "corecel/Macros.hh"
0016 #include "celeritas/Types.hh"
0017 
0018 #include "EventIOInterface.hh"
0019 
0020 namespace HepMC3
0021 {
0022 class Writer;
0023 }
0024 
0025 namespace celeritas
0026 {
0027 //---------------------------------------------------------------------------//
0028 class ParticleParams;
0029 
0030 //---------------------------------------------------------------------------//
0031 /*!
0032  * Write events using HepMC3.
0033  */
0034 class EventWriter : public EventWriterInterface
0035 {
0036   public:
0037     //!@{
0038     //! \name Type aliases
0039     using SPConstParticles = std::shared_ptr<ParticleParams const>;
0040     //!@}
0041 
0042     //! Output format
0043     enum class Format
0044     {
0045         hepevt,
0046         hepmc2,
0047         hepmc3,
0048         size_
0049     };
0050 
0051   public:
0052     // Construct by parsing the extension
0053     EventWriter(std::string const& filename, SPConstParticles params);
0054 
0055     // Construct with a filename, particle data, and output format
0056     EventWriter(std::string const& filename,
0057                 SPConstParticles params,
0058                 Format fmt);
0059 
0060     //! Prevent copying and moving due to file ownership
0061     CELER_DELETE_COPY_MOVE(EventWriter);
0062 
0063     ~EventWriter() override = default;
0064 
0065     // Write all the primaries from a single event
0066     void operator()(VecPrimary const& primaries) final;
0067 
0068   private:
0069     // Shared standard model particle data
0070     SPConstParticles particles_;
0071 
0072     Format fmt_;
0073 
0074     // HepMC3 event record writer
0075     std::shared_ptr<HepMC3::Writer> writer_;
0076 
0077     // Number of events written
0078     EventId::size_type event_count_{0};
0079 
0080     bool warned_mismatched_events_{false};
0081 };
0082 
0083 //---------------------------------------------------------------------------//
0084 // FREE FUNCTIONS
0085 //---------------------------------------------------------------------------//
0086 char const* to_cstring(EventWriter::Format);
0087 
0088 //---------------------------------------------------------------------------//
0089 // INLINE DEFINITIONS
0090 //---------------------------------------------------------------------------//
0091 #if !CELERITAS_USE_HEPMC3
0092 inline EventWriter::EventWriter(std::string const& s, SPConstParticles p)
0093     : EventWriter{s, p, Format::size_}
0094 {
0095 }
0096 inline EventWriter::EventWriter(std::string const&, SPConstParticles, Format)
0097 {
0098     CELER_DISCARD(particles_);
0099     CELER_DISCARD(fmt_);
0100     CELER_DISCARD(writer_);
0101     CELER_DISCARD(event_count_);
0102     CELER_DISCARD(warned_mismatched_events_);
0103     CELER_NOT_CONFIGURED("HepMC3");
0104 }
0105 
0106 inline void EventWriter::operator()(argument_type)
0107 {
0108     CELER_ASSERT_UNREACHABLE();
0109 }
0110 #endif
0111 
0112 //---------------------------------------------------------------------------//
0113 }  // namespace celeritas