ATLAS Offline Software
Loading...
Searching...
No Matches
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
21ActsPropStepRootWriterSvc::ActsPropStepRootWriterSvc( const std::string& name, ISvcLocator* svc )
22: base_class(name, svc) {
23}
24
25StatusCode
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
59StatusCode
61{
62 end();
63
64 return StatusCode::SUCCESS;
65}
66
67void
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
81void
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
119void
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
168void
170{
171 m_outputFile->cd();
172 m_outputTree->Write();
173 //m_outputFile->Close();
174}
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
std::vector< int > m_s_boundaryID
boundary identification
ActsPropStepRootWriterSvc(const std::string &name, ISvcLocator *svc)
std::vector< float > m_s_pR
global position z of the step
TTree * m_outputTree
the output tree
Gaudi::Property< std::string > m_filePath
void doWrite(const StepVector &steps, size_t evtNum)
std::deque< queue_item_t > m_queue
std::vector< int > m_s_volumeID
volume identification
std::vector< float > m_s_pZ
global position z of the step
void write(const StepVector &steps) override
virtual StatusCode initialize() override
std::vector< Acts::detail::Step > StepVector
virtual StatusCode finalize() override
std::pair< size_t, StepVector > queue_item_t
Gaudi::Property< std::string > m_treeName
std::vector< int > m_s_approachID
approach identification
std::vector< float > m_s_pX
global position x of the step
std::vector< int > m_s_layerID
layer identification
std::vector< int > m_s_sensitiveID
sensitive identification
TFile * m_outputFile
the output file
std::vector< float > m_s_pY
global position y of the step