Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-14 08:37:52

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Utilities/Logger.hpp"
0012 #include "ActsExamples/Framework/DataHandle.hpp"
0013 #include "ActsExamples/Framework/IWriter.hpp"
0014 
0015 #include <limits>
0016 #include <memory>
0017 #include <string>
0018 
0019 namespace ActsExamples {
0020 
0021 /// NaN values for TTree variables
0022 constexpr double NaNdouble = std::numeric_limits<double>::quiet_NaN();
0023 constexpr float NaNfloat = std::numeric_limits<float>::quiet_NaN();
0024 constexpr float NaNint = std::numeric_limits<int>::quiet_NaN();
0025 
0026 /// A helper class for users to implement framework writers.
0027 ///
0028 /// @note This is not an additional interface class and should not be used as
0029 ///       such, e.g. as a constrained `IWriter` substitute. This class should
0030 ///       only be used as the base class for a concrete writer implementation.
0031 ///
0032 /// @tparam T The object type read from the event store
0033 ///
0034 /// This class can be used when a writer reads a single object from the event
0035 /// store and writes it to file. Reading from the event store and casting
0036 /// to the specified type is done automatically and the user only needs to
0037 /// implement the type-specific write method.
0038 ///
0039 /// Default no-op implementations for `initialize` and `finalize` are provided
0040 /// but can be overridden by the user.
0041 template <typename write_data_t>
0042 class WriterT : public IWriter {
0043  public:
0044   /// @param objectName The object that should be read from the event store
0045   /// @param writerName The name of the writer, e.g. for logging output
0046   /// @param level The internal log level
0047   WriterT(std::string objectName, std::string writerName,
0048           Acts::Logging::Level level);
0049 
0050   /// Provide the name of the writer
0051   std::string name() const override;
0052 
0053   /// Read the object and call the type-specific member function.
0054   ProcessCode write(const AlgorithmContext& context) override;
0055 
0056   /// No-op default implementation.
0057   ProcessCode finalize() override;
0058 
0059  protected:
0060   /// Type-specific write function implementation
0061   /// this method is implemented in the user implementation
0062   /// @param [in] context is the algorithm context that guarantees event
0063   ///        consistency
0064   /// @tparam [in] is the templated collection to be written
0065   virtual ProcessCode writeT(const AlgorithmContext& context,
0066                              const write_data_t& t) = 0;
0067 
0068   const Acts::Logger& logger() const { return *m_logger; }
0069 
0070  private:
0071   std::string m_objectName;
0072   std::string m_writerName;
0073   std::unique_ptr<const Acts::Logger> m_logger;
0074 
0075   ReadDataHandle<write_data_t> m_inputHandle{this, "InputHandle"};
0076 };
0077 
0078 template <typename write_data_t>
0079 WriterT<write_data_t>::WriterT(std::string objectName, std::string writerName,
0080                                Acts::Logging::Level level)
0081     : m_objectName(std::move(objectName)),
0082       m_writerName(std::move(writerName)),
0083       m_logger(Acts::getDefaultLogger(m_writerName, level)) {
0084   if (m_objectName.empty()) {
0085     throw std::invalid_argument("Missing input collection");
0086   } else if (m_writerName.empty()) {
0087     throw std::invalid_argument("Missing writer name");
0088   }
0089 
0090   m_inputHandle.initialize(m_objectName);
0091 }
0092 
0093 template <typename write_data_t>
0094 inline std::string WriterT<write_data_t>::name() const {
0095   return m_writerName;
0096 }
0097 
0098 template <typename write_data_t>
0099 inline ProcessCode WriterT<write_data_t>::finalize() {
0100   return ProcessCode::SUCCESS;
0101 }
0102 
0103 template <typename write_data_t>
0104 inline ProcessCode WriterT<write_data_t>::write(
0105     const AlgorithmContext& context) {
0106   return writeT(context, m_inputHandle(context));
0107 }
0108 
0109 }  // namespace ActsExamples