ATLAS Offline Software
ActsPropStepRootWriterSvc.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 "Acts/Propagator/detail/SteppingLogger.hpp"
9 #include "Acts/Geometry/TrackingVolume.hpp"
10 #include "Acts/Geometry/GeometryIdentifier.hpp"
11 
12 #include <vector>
13 #include <deque>
14 #include <mutex>
15 #include <thread>
16 
17 #include "TTree.h"
18 #include "TFile.h"
19 
20 
22 : base_class(name, svc) {
23 }
24 
27 {
28 
29  std::string filePath = m_filePath;
30  m_outputFile = TFile::Open(filePath.c_str(), "RECREATE");
31  if(m_outputFile == nullptr) {
32  ATH_MSG_ERROR("Unable to open output file at " << m_filePath);
33  return StatusCode::FAILURE;
34  }
35  m_outputFile->cd();
36 
37  std::string treeName = m_treeName;
38  m_outputTree = new TTree(treeName.c_str(), "Acts Propagation Steps");
39  if(m_outputTree == nullptr) {
40  ATH_MSG_ERROR("Unable to create TTree");
41  return StatusCode::FAILURE;
42  }
43 
44  m_outputTree->Branch("event_nr", &m_eventNum);
45  m_outputTree->Branch("step_x", &m_s_pX);
46  m_outputTree->Branch("step_y", &m_s_pY);
47  m_outputTree->Branch("step_z", &m_s_pZ);
48  m_outputTree->Branch("step_r", &m_s_pR);
49  m_outputTree->Branch("volume_id", &m_s_volumeID);
50  m_outputTree->Branch("boundary_id", &m_s_boundaryID);
51  m_outputTree->Branch("layer_id", &m_s_layerID);
52  m_outputTree->Branch("approach_id", &m_s_approachID);
53  m_outputTree->Branch("sensitive_id", &m_s_sensitiveID);
54 
55 
56  return StatusCode::SUCCESS;
57 }
58 
61 {
62  end();
63 
64  return StatusCode::SUCCESS;
65 }
66 
67 void
69 {
70 
71  const auto& ctx = Gaudi::Hive::currentContext();
72 
73  std::lock_guard<std::mutex> lock(m_writeMutex);
74 
75  //for(size_t i=0;i<ecells.size();++i) {
76  //m_queue.emplace_back(ctx.eventID().event_number(), std::move(ecells[i]));
77  //}
78  doWrite(steps, ctx.eventID().event_number());
79 }
80 
81 void
83 {
84  using namespace std::chrono_literals;
85  // wait until we have events
86  while(m_queue.empty()) {
87  std::this_thread::sleep_for(2s);
88  if (m_doEnd) return;
89  }
90 
91  while(true) {
92  std::unique_lock<std::mutex> lock(m_writeMutex);
93 
94  if (m_queue.empty()) {
95  lock.unlock();
96  if (!m_doEnd) {
97  std::this_thread::sleep_for(0.5s);
98  continue;
99  } else {
100  ATH_MSG_INFO("Writer thread caught termination signal. Shutting down.");
101  end();
102  return;
103  }
104  }
105 
106  queue_item_t queue_item = std::move(m_queue.front());
107  m_queue.pop_front();
108 
109  lock.unlock();
110 
111  size_t eventNum = queue_item.first;
112  StepVector steps = std::move(queue_item.second);
113 
114  doWrite(steps, eventNum);
115  }
116 
117 }
118 
119 void
121 {
122  m_eventNum = evtNum;
123  m_s_pX.clear();
124  m_s_pY.clear();
125  m_s_pZ.clear();
126  m_s_pR.clear();
127  m_s_volumeID.clear();
128  m_s_boundaryID.clear();
129  m_s_layerID.clear();
130  m_s_approachID.clear();
131  m_s_sensitiveID.clear();
132 
133  for(const auto& step : steps) {
134  Acts::GeometryIdentifier::Value volumeID = 0;
135  Acts::GeometryIdentifier::Value boundaryID = 0;
136  Acts::GeometryIdentifier::Value layerID = 0;
137  Acts::GeometryIdentifier::Value approachID = 0;
138  Acts::GeometryIdentifier::Value sensitiveID = 0;
139  // get the identification from the surface first
140  if (step.surface) {
141  auto geoID = step.surface->geometryId();
142  sensitiveID = geoID.sensitive();
143  approachID = geoID.approach();
144  layerID = geoID.layer();
145  boundaryID = geoID.boundary();
146  volumeID = geoID.volume();
147  }
148  // a current volume overwrites the surface tagged one
149  if (step.geoID != Acts::GeometryIdentifier()) {
150  volumeID = step.geoID.volume();
151  }
152  // now fill
153  m_s_sensitiveID.push_back(sensitiveID);
154  m_s_approachID.push_back(approachID);
155  m_s_layerID.push_back(layerID);
156  m_s_boundaryID.push_back(boundaryID);
157  m_s_volumeID.push_back(volumeID);
158 
159  m_s_pX.push_back(step.position.x());
160  m_s_pY.push_back(step.position.y());
161  m_s_pZ.push_back(step.position.z());
162  m_s_pR.push_back(Acts::VectorHelpers::perp(step.position));
163  }
164 
165  m_outputTree->Fill();
166 }
167 
168 void
170 {
171  m_outputFile->cd();
172  m_outputTree->Write();
173  //m_outputFile->Close();
174 }
ActsPropStepRootWriterSvc::m_s_sensitiveID
std::vector< int > m_s_sensitiveID
sensitive identification
Definition: ActsPropStepRootWriterSvc.h:83
ActsPropStepRootWriterSvc.h
ActsPropStepRootWriterSvc::initialize
virtual StatusCode initialize() override
Definition: ActsPropStepRootWriterSvc.cxx:26
ActsPropStepRootWriterSvc::m_queue
std::deque< queue_item_t > m_queue
Definition: ActsPropStepRootWriterSvc.h:54
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
ActsPropStepRootWriterSvc::m_outputTree
TTree * m_outputTree
the output tree
Definition: ActsPropStepRootWriterSvc.h:73
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
perp
Scalar perp() const
perp method - perpenticular length
Definition: AmgMatrixBasePlugin.h:44
ActsPropStepRootWriterSvc::finalize
virtual StatusCode finalize() override
Definition: ActsPropStepRootWriterSvc.cxx:60
ActsPropStepRootWriterSvc::m_s_layerID
std::vector< int > m_s_layerID
layer identification
Definition: ActsPropStepRootWriterSvc.h:81
ActsPropStepRootWriterSvc::write
void write(const StepVector &steps) override
Definition: ActsPropStepRootWriterSvc.cxx:68
ActsPropStepRootWriterSvc::m_s_pY
std::vector< float > m_s_pY
global position y of the step
Definition: ActsPropStepRootWriterSvc.h:76
ActsPropStepRootWriterSvc::m_s_boundaryID
std::vector< int > m_s_boundaryID
boundary identification
Definition: ActsPropStepRootWriterSvc.h:80
ActsPropStepRootWriterSvc::doWrite
void doWrite(const StepVector &steps, size_t evtNum)
Definition: ActsPropStepRootWriterSvc.cxx:120
ActsPropStepRootWriterSvc::m_writeMutex
std::mutex m_writeMutex
Definition: ActsPropStepRootWriterSvc.h:55
beamspotman.steps
int steps
Definition: beamspotman.py:505
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ActsPropStepRootWriterSvc::ActsPropStepRootWriterSvc
ActsPropStepRootWriterSvc(const std::string &name, ISvcLocator *svc)
Definition: ActsPropStepRootWriterSvc.cxx:21
ActsPropStepRootWriterSvc::m_s_pX
std::vector< float > m_s_pX
global position x of the step
Definition: ActsPropStepRootWriterSvc.h:75
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
dumpFileToPlots.treeName
string treeName
Definition: dumpFileToPlots.py:20
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
ActsPropStepRootWriterSvc::writeThread
void writeThread()
Definition: ActsPropStepRootWriterSvc.cxx:82
ActsPropStepRootWriterSvc::end
void end()
Definition: ActsPropStepRootWriterSvc.cxx:169
hancool.filePath
string filePath
Definition: hancool.py:28
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ActsPropStepRootWriterSvc::StepVector
std::vector< Acts::detail::Step > StepVector
Definition: ActsPropStepRootWriterSvc.h:45
ActsPropStepRootWriterSvc::m_s_approachID
std::vector< int > m_s_approachID
approach identification
Definition: ActsPropStepRootWriterSvc.h:82
ActsPropStepRootWriterSvc::m_treeName
Gaudi::Property< std::string > m_treeName
Definition: ActsPropStepRootWriterSvc.h:65
ActsPropStepRootWriterSvc::m_s_pZ
std::vector< float > m_s_pZ
global position z of the step
Definition: ActsPropStepRootWriterSvc.h:77
ActsPropStepRootWriterSvc::m_s_pR
std::vector< float > m_s_pR
global position z of the step
Definition: ActsPropStepRootWriterSvc.h:78
ActsPropStepRootWriterSvc::queue_item_t
std::pair< size_t, StepVector > queue_item_t
Definition: ActsPropStepRootWriterSvc.h:51
LArCellBinning.step
step
Definition: LArCellBinning.py:158
ActsPropStepRootWriterSvc::m_outputFile
TFile * m_outputFile
the output file
Definition: ActsPropStepRootWriterSvc.h:72
ActsPropStepRootWriterSvc::m_s_volumeID
std::vector< int > m_s_volumeID
volume identification
Definition: ActsPropStepRootWriterSvc.h:79
ActsPropStepRootWriterSvc::m_filePath
Gaudi::Property< std::string > m_filePath
Definition: ActsPropStepRootWriterSvc.h:64
ActsPropStepRootWriterSvc::m_eventNum
int m_eventNum
Definition: ActsPropStepRootWriterSvc.h:74
ActsPropStepRootWriterSvc::m_doEnd
std::atomic< bool > m_doEnd
Definition: ActsPropStepRootWriterSvc.h:57