ATLAS Offline Software
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 
15 ActsExCellWriterSvc::ActsExCellWriterSvc( const std::string& name, ISvcLocator* svc )
16 : base_class(name, svc) {
17 }
18 
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 
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 
51 void
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 
64 void
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 }
ActsExCellWriterSvc::m_writeThread
std::thread m_writeThread
Definition: ActsExCellWriterSvc.h:53
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
ActsExCellWriterSvc::m_writePassive
Gaudi::Property< bool > m_writePassive
Definition: ActsExCellWriterSvc.h:64
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ActsExCellWriterSvc::initialize
virtual StatusCode initialize() override
Definition: ActsExCellWriterSvc.cxx:20
ActsExCellWriterSvc::queue_item_t
std::pair< size_t, ExCellCharged > queue_item_t
Definition: ActsExCellWriterSvc.h:48
ActsExCellWriterSvc::m_writeSensitive
Gaudi::Property< bool > m_writeSensitive
Definition: ActsExCellWriterSvc.h:63
RootExCellWriter.h
ActsExCellWriterSvc::m_writeMaterial
Gaudi::Property< bool > m_writeMaterial
Definition: ActsExCellWriterSvc.h:62
ActsExCellWriterSvc::store
void store(std::vector< Acts::ExtrapolationCell< Acts::TrackParameters >> &ecells) override
Definition: ActsExCellWriterSvc.cxx:52
ActsExCellWriterSvc::finalize
virtual StatusCode finalize() override
Definition: ActsExCellWriterSvc.cxx:41
Acts::ExtrapolationCell
Definition: ActsExCellWriterSvc.h:29
ActsExCellWriterSvc.h
ActsExCellWriterSvc::doWrite
void doWrite()
Definition: ActsExCellWriterSvc.cxx:65
lumiFormat.i
int i
Definition: lumiFormat.py:85
ActsExCellWriterSvc::m_doEnd
std::atomic< bool > m_doEnd
Definition: ActsExCellWriterSvc.h:54
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ActsExCellWriterSvc::m_queue
std::deque< queue_item_t > m_queue
Definition: ActsExCellWriterSvc.h:51
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
ActsExCellWriterSvc::m_rootEccWriter
std::shared_ptr< RootExCellWriter< Acts::TrackParameters > > m_rootEccWriter
Definition: ActsExCellWriterSvc.h:50
ActsExCellWriterSvc::m_filePath
Gaudi::Property< std::string > m_filePath
Definition: ActsExCellWriterSvc.h:59
ActsExCellWriterSvc::ActsExCellWriterSvc
ActsExCellWriterSvc(const std::string &name, ISvcLocator *svc)
Definition: ActsExCellWriterSvc.cxx:15
ActsExCellWriterSvc::m_writeBoundary
Gaudi::Property< bool > m_writeBoundary
Definition: ActsExCellWriterSvc.h:61
RootExCellWriter
Definition: ActsExCellWriterSvc.h:23
ActsExCellWriterSvc::m_chargedMutex
std::mutex m_chargedMutex
Definition: ActsExCellWriterSvc.h:52
ActsExCellWriterSvc::m_treeName
Gaudi::Property< std::string > m_treeName
Definition: ActsExCellWriterSvc.h:60