HepMC3 event record library: ReaderMT.h Source File

HepMC3 event record library

ReaderMT.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
5//
6#ifndef HEPMC3_READERMT_H
7#define HEPMC3_READERMT_H
8///
9/// @file ReaderMT.h
10/// @brief Definition of class \b ReaderMT
11///
12/// @class HepMC3::ReaderMT
13/// @brief Multithreader GenEvent I/O parsing
14///
15/// @ingroup IO
16///
17#include <set>
18#include <string>
19#include <fstream>
20#include <istream>
21#include <iterator>
22#include <thread>
23#include "HepMC3/Reader.h"
24#include "HepMC3/GenEvent.h"
25namespace HepMC3 {
26 template <class T, size_t m_number_of_threads> class ReaderMT : public Reader
27{
28private:
29 bool m_go_try_cache; //!< Flag to trigger using the cached event
30 std::vector< std::shared_ptr<T> > m_readers; //!< Vector of all active readers
31 std::vector< std::pair<GenEvent, bool> > m_events; //!< Vector of events
32 std::vector< std::thread > m_threads; //!< Vector of threads
33 /// @brief The reading function
34 static void read_function(std::pair<GenEvent,bool>& e, std::shared_ptr<T> r)
35 {
36 e.second = r->read_event(e.first);
37 r->skip(m_number_of_threads-1);
38 if (r->failed()) r->close();
39 }
40public:
41 /// @brief Constructor
42 ReaderMT(const std::string& filename): m_go_try_cache(true) {
43 m_events.reserve(m_number_of_threads);
44 m_readers.reserve(m_number_of_threads);
45 m_threads.reserve(m_number_of_threads);
46 for (size_t i = 0; i < m_number_of_threads; ++i) {
47 m_readers.push_back(std::make_shared<T>(filename));
48 m_readers.back()->skip(m_number_of_threads-1-i);
49 }
50 }
51 /// @brief Destructor
52 ~ReaderMT() {
53 m_readers.clear();
54 m_events.clear();
55 m_threads.clear();
56 }
57 /// @brief skip
58 bool skip(const int) override {
59 return false;///Not implemented
60 }
61 /// @brief event reading
62 bool read_event(GenEvent& evt) override {
63 if ( !m_events.empty() ) {
64 evt = m_events.back().first;
65 m_events.pop_back();
66 return true;
67 }
68 m_events.clear();
69 m_threads.clear();
70 m_go_try_cache = true;
71 m_threads.reserve(m_number_of_threads);
72 m_events.reserve(m_number_of_threads);
73 for (size_t i = 0; i < m_number_of_threads; ++i) {
74 m_events.push_back(std::pair<GenEvent, bool>(GenEvent(Units::GEV,Units::MM), true));
75 m_threads.push_back(std::thread(read_function, std::ref(m_events.at(i)), m_readers.at(i)));
76 }
77 for (auto& th : m_threads) {
78 th.join();
79 }
80 m_threads.clear();
81
82 m_events.erase(std::remove_if(m_events.begin(), m_events.end(),[](std::pair<GenEvent, bool>& x) {
83 return !x.second;
84 }), m_events.end());
85
86 if (m_events.empty()) {
87 m_go_try_cache = false;
88 return false;
89 }
90 evt = m_events.back().first;
91 m_events.pop_back();
92 return true;
93 }
94 /// @brief failed
95 bool failed() override {
96 for (auto& reader: m_readers) if (reader && !reader->failed()) return false;
97 if ( !m_events.empty() ) return false;
98 if ( m_go_try_cache ) return false;
99 return true;
100 }
101 /// @brief close
102 void close() override {
103 for (auto& reader: m_readers) if (reader) reader->close();
104 }
105};
106}
107#endif
GenEvent.h
Definition of class GenEvent.
Reader.h
Definition of interface Reader.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::ReaderMT::m_events
std::vector< std::pair< GenEvent, bool > > m_events
Vector of events.
Definition ReaderMT.h:31
HepMC3::ReaderMT::m_go_try_cache
bool m_go_try_cache
Flag to trigger using the cached event.
Definition ReaderMT.h:29
HepMC3::ReaderMT::read_event
bool read_event(GenEvent &evt) override
event reading
Definition ReaderMT.h:62
HepMC3::ReaderMT::failed
bool failed() override
failed
Definition ReaderMT.h:95
HepMC3::ReaderMT::skip
bool skip(const int) override
skip
Definition ReaderMT.h:58
HepMC3::ReaderMT::ReaderMT
ReaderMT(const std::string &filename)
Constructor.
Definition ReaderMT.h:42
HepMC3::ReaderMT::close
void close() override
close
Definition ReaderMT.h:102
HepMC3::ReaderMT::~ReaderMT
~ReaderMT()
Destructor.
Definition ReaderMT.h:52
HepMC3::ReaderMT::m_threads
std::vector< std::thread > m_threads
Definition ReaderMT.h:32
HepMC3::ReaderMT::read_function
static void read_function(std::pair< GenEvent, bool > &e, std::shared_ptr< T > r)
The reading function.
Definition ReaderMT.h:34
HepMC3::ReaderMT::m_readers
std::vector< std::shared_ptr< T > > m_readers
Vector of all active readers.
Definition ReaderMT.h:30
HepMC3::Reader::Reader
Reader()
Constructor.
Definition Reader.h:28
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24