ATLAS Offline Software
Loading...
Searching...
No Matches
MaterialTrackWriter.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
7#include "GaudiKernel/IInterface.h"
8
9#include "TTree.h"
10#include "TFile.h"
11
12
13ActsTrk::MaterialTrackWriter::MaterialTrackWriter(const std::string& name, ISvcLocator* pSvcLocator) :
14 AthReentrantAlgorithm(name, pSvcLocator),
16{}
17
23
25{
26 // Check the input collection key
28
29 // Setup ROOT I/O
30 m_outputFile = TFile::Open(m_fileName.value().c_str(), "RECREATE");
31 if (m_outputFile == nullptr) {
32 ATH_MSG_ERROR("Could not open '" + m_fileName + "'");
33 return StatusCode::FAILURE;
34 }
35
36 m_outputFile->cd();
37 m_outputTree =
38 new TTree(m_treeName.value().c_str(), "TTree from MaterialTrackWriter");
39 if (m_outputTree == nullptr) {
40 ATH_MSG_ERROR("Could not create TTree '" + m_treeName + "'");
41 return StatusCode::FAILURE;
42 }
43
44 // Connect the branches
45 m_accessor.connectForWrite(*m_outputTree);
46
47 return StatusCode::SUCCESS;
48}
49
51{
52 // write the tree and close the file
53 ATH_MSG_INFO("Writing ROOT output File : " << m_outputFile);
54
55 m_outputFile->cd();
56 m_outputTree->Write();
57 m_outputFile->Close();
58
59 return StatusCode::SUCCESS;
60}
61
62StatusCode
63ActsTrk::MaterialTrackWriter::execute (const EventContext& ctx) const
64{
65 // Get the collection from storegate
67 if (!materialTracks.isValid()) {
68 ATH_MSG_ERROR("Failed to read " << m_materialTrackCollectionKey.key());
69 return StatusCode::FAILURE;
70 }
71
72 const ActsTrk::GeometryContext& geoContext{m_trackingGeometrySvc->getNominalContext()};
73
74 std::lock_guard<std::mutex> lock(m_writeMutex);
75
76 // Loop over the material tracks and write them out
77 for (const auto& materialTrack : *materialTracks) {
78 // write & fill
79 m_accessor.write(geoContext.context(), ctx.evt(), materialTrack);
80 m_outputTree->Fill();
81 }
82
83 // return success
84 return StatusCode::SUCCESS;
85}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
Acts::GeometryContext context() const
virtual StatusCode initialize() override
Gaudi::Property< std::string > m_treeName
The output file name.
Gaudi::Property< bool > m_recalculateTotals
Re-calculate total values from individual steps (for cross-checks)
TFile * m_outputFile
The output file and tree.
SG::ReadHandleKey< RecordedMaterialTrackCollection > m_materialTrackCollectionKey
The RecordedMaterialTrackCollection to read.
Gaudi::Property< bool > m_storeVolume
Write the volume to which the material step correpond.
virtual StatusCode finalize() override
Gaudi::Property< bool > m_storeSurface
Write the surface to which the material step correpond.
std::mutex m_writeMutex
mutex used to protect multi-threaded writes
ServiceHandle< ActsTrk::ITrackingGeometrySvc > m_trackingGeometrySvc
The tracking geometry service to retrive the geometry context.
MaterialTrackWriter(const std::string &name, ISvcLocator *pSvcLocator)
virtual StatusCode execute(const EventContext &ctx) const override
Gaudi::Property< std::string > m_fileName
The output file name.
Gaudi::Property< bool > m_prePostStep
Write out pre and post step (for G4), otherwise central step position.
An algorithm that can be simultaneously executed in multiple threads.
virtual bool isValid() override final
Can the handle be successfully dereferenced?