ATLAS Offline Software
Loading...
Searching...
No Matches
ActsExCellWriterSvc.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include "GaudiKernel/IInterface.h"
7
8#include <vector>
9#include <deque>
10#include <mutex>
11#include <thread>
12
13#include "util/RootExCellWriter.h"
14
15ActsExCellWriterSvc::ActsExCellWriterSvc( const std::string& name, ISvcLocator* svc )
16: base_class(name, svc) {
17}
18
19StatusCode
21{
23 reccWriterConfig.filePath = m_filePath;
24 reccWriterConfig.treeName = m_treeName;
25 reccWriterConfig.writeBoundary = m_writeBoundary;
26 reccWriterConfig.writeMaterial = m_writeMaterial;
27 reccWriterConfig.writeSensitive = m_writeSensitive;
28 reccWriterConfig.writePassive = m_writePassive;
30 = std::make_shared<RootExCellWriter<Acts::TrackParameters>>(
31 reccWriterConfig);
32
33 ATH_MSG_INFO("Starting writer thread");
34 m_doEnd = false;
35 m_writeThread = std::thread(&ActsExCellWriterSvc::doWrite, this);
36
37 return StatusCode::SUCCESS;
38}
39
40StatusCode
42{
43 ATH_MSG_INFO("Waiting for writer thread to finish.");
44 m_doEnd = true;
45 m_writeThread.join();
46 ATH_MSG_INFO("Writer thread has terminated.");
47
48 return StatusCode::SUCCESS;
49}
50
51void
53{
54
55 const auto& ctx = Gaudi::Hive::currentContext();
56
57 std::lock_guard<std::mutex> lock(m_chargedMutex);
58
59 for(size_t i=0;i<ecells.size();++i) {
60 m_queue.emplace_back(ctx.eventID().event_number(), std::move(ecells[i]));
61 }
62}
63
64void
66{
67 using namespace std::chrono_literals;
68 // wait until we have events
69 while(m_queue.empty()) {
70 std::this_thread::sleep_for(2s);
71 if (m_doEnd) return;
72 }
73
74 while(true) {
75 std::unique_lock<std::mutex> lock(m_chargedMutex);
76
77 if (m_queue.empty()) {
78 lock.unlock();
79 if (!m_doEnd) {
80 std::this_thread::sleep_for(0.5s);
81 continue;
82 } else {
83 ATH_MSG_INFO("Writer thread caught termination signal. Shutting down.");
84 m_rootEccWriter->endRun();
85 return;
86 }
87 }
88
89 queue_item_t queue_item = std::move(m_queue.front());
90 m_queue.pop_front();
91
92 lock.unlock();
93
94 size_t eventNum = queue_item.first;
95 ExCellCharged ecell = std::move(queue_item.second);
96
97 m_rootEccWriter->write(ecell, eventNum);
98 }
99
100 //std::vector<ExCellCharged> writeBuffer;
101
102 //while(true) {
103 //std::unique_lock<std::mutex> lock(m_chargedMutex);
104 //if (m_exCells.size() < 1000 && !m_doEnd) {
105 //lock.unlock();
106 //std::this_thread::sleep_for(0.2s);
107 //continue;
108 //}
109
111 //writeBuffer.reserve(m_exCells.size());
112 //while(!m_exCells.empty()) {
113 //writeBuffer.push_back(std::move(m_exCells.front()));
114 //m_exCells.pop_front();
115 //}
116
118 //lock.unlock();
119
121 //for(auto &ecell : writeBuffer) {
122 //m_rootEccWriter->write(std::move(ecell));
123 //}
124 //writeBuffer.clear();
125
126 //if (m_doEnd) {
127 //lock.lock();
128 //while(!m_exCells.empty()) {
129 //m_rootEccWriter->write(std::move(m_exCells.front()));
130 //m_exCells.pop_front();
131 //}
132 //lock.unlock();
133 //break;
134 //}
135
136 //}
137
138}
#define ATH_MSG_INFO(x)
std::deque< queue_item_t > m_queue
void store(std::vector< Acts::ExtrapolationCell< Acts::TrackParameters > > &ecells) override
virtual StatusCode initialize() override
Gaudi::Property< bool > m_writeMaterial
Gaudi::Property< bool > m_writeBoundary
Acts::ExtrapolationCell< Acts::TrackParameters > ExCellCharged
std::shared_ptr< RootExCellWriter< Acts::TrackParameters > > m_rootEccWriter
std::atomic< bool > m_doEnd
Gaudi::Property< std::string > m_treeName
Gaudi::Property< bool > m_writeSensitive
ActsExCellWriterSvc(const std::string &name, ISvcLocator *svc)
Gaudi::Property< bool > m_writePassive
Gaudi::Property< std::string > m_filePath
virtual StatusCode finalize() override
std::pair< size_t, ExCellCharged > queue_item_t