Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:32:32

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