ATLAS Offline Software
Loading...
Searching...
No Matches
McAodWriterTool.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// McAodWriterTool.cxx
7// Implementation file for class McAodWriterTool
8// Author: S.Binet<binet@cern.ch>
10
11
12// STL includes
13#include <algorithm>
14#include <cctype>
15#include <iomanip>
16
17// FrameWork includes
18
19// HepMC includes
21
22// McParticleEvent includes
24
25// McParticleTools includes
26#include "McAodWriterTool.h"
27
28static const char * const s_protocolSep = ":";
29
30struct ToLower
31{
32 char operator() (char c) const { return std::tolower(c); }
33};
34
38
42 const std::string& name,
43 const IInterface* parent ) :
44 AthAlgTool( type, name, parent ),
45 m_ioBackend( nullptr )
46{
47 //
48 // Property declaration
49 //
50
51 declareProperty( "Output",
52 m_ioBackendURL = "ascii:mcaod.txt",
53 "Name of the back-end we'll use to write out the "
54 "TruthParticleContainer.\nEx: ascii:mcaod.txt" );
55 m_ioBackendURL.declareUpdateHandler( &McAodWriterTool::setupBackend,
56 this );
57
58 declareProperty( "TruthParticles",
59 m_truthParticlesName = "SpclMC",
60 "Input location of the TruthParticleContainer to write out" );
61
62 declareInterface<IIOMcAodTool>(this);
63}
64
68{
69 ATH_MSG_DEBUG("Calling destructor");
70
71 if ( m_ioBackend ) {
72 delete m_ioBackend;
73 m_ioBackend = nullptr;
74 }
75}
76
77// Athena Algorithm's Hooks
80{
81 ATH_MSG_INFO("Initializing " << name() << "...");
82 // Get pointer to StoreGateSvc and cache it :
83 if ( !evtStore().retrieve().isSuccess() ) {
84 ATH_MSG_ERROR("Unable to retrieve pointer to StoreGateSvc");
85 return StatusCode::FAILURE;
86 }
87
88 // setup backend
89 if ( nullptr == m_ioBackend ) {
91 }
92
93 return StatusCode::SUCCESS;
94}
95
97{
98 ATH_MSG_INFO("Finalizing " << name() << "...");
99 ATH_MSG_DEBUG("Closing backend...");
100 m_ioBackend->close();
101
102 return StatusCode::SUCCESS;
103}
104
106{
107 // retrieve the TruthParticles
108 const TruthParticleContainer * mcParts = nullptr;
109 if ( evtStore()->retrieve( mcParts, m_truthParticlesName ).isFailure() ||
110 nullptr == mcParts ) {
111 ATH_MSG_WARNING("Could not retrieve a TruthParticleContainer at ["
112 << m_truthParticlesName << "] !!");
113 return StatusCode::RECOVERABLE;
114 }
115
116 return write(mcParts);
117}
118
122
124{
125 if ( nullptr == mcParts ) {
126 ATH_MSG_WARNING("NULL pointer to TruthParticleContainer !!");
127 return StatusCode::RECOVERABLE;
128 }
129
130 std::ofstream& out = *m_ioBackend;
131 out << std::fixed;
132
133 out << "---" << std::setw(5) << mcParts->size() << std::endl;
134 for ( unsigned int i = 0; i != mcParts->size(); ++i ) {
135 const TruthParticle* mc = (*mcParts)[i];
136 out << "P " << std::setprecision(3) << std::setw(10) << mc->px()
137 << " " << std::setprecision(3) << std::setw(10) << mc->py()
138 << " " << std::setprecision(3) << std::setw(10) << mc->pz()
139 << " " << std::setprecision(3) << std::setw(10) << mc->e()
140 << " " << std::setprecision(3) << std::setw(10) << mc->pdgId()
141 << " " << std::setprecision(3) << std::setw(10) << mc->charge()
142 << " " << std::setprecision(3) << std::setw(10) << HepMC::barcode(mc->genParticle())
143 << std::endl;
144 out << "EtIsol "
145 << std::setw(3) << TruthParticleParameters::NbrOfCones
146 << " : ";
147 for ( unsigned int iCone = 0;
149 ++iCone ) {
150 out << " " << std::setprecision(3) << std::setw(10)
151 << mc->etIsol(static_cast<TruthParticleParameters::ConeSize>(iCone));
152 }
153 out << std::endl;
154 }
155
156 return StatusCode::SUCCESS;
157}
158
159void McAodWriterTool::setupBackend( Gaudi::Details::PropertyBase& /*prop*/ )
160{
161 // defaults
162 std::string protocol = "ascii";
163 std::string fileName = "mcaod.txt";
164
165 // reset internal state
166 if ( m_ioBackend ) {
167 delete m_ioBackend;
168 m_ioBackend = nullptr;
169 }
170
171 // caching URL
172 const std::string& url = m_ioBackendURL.value();
173
174 std::string::size_type protocolPos = url.find(s_protocolSep);
175
176 if ( std::string::npos != protocolPos ) {
177 protocol = url.substr( 0, protocolPos );
178 fileName = url.substr( protocolPos+1, std::string::npos );
179 } else {
180 //protocol = "ascii";
181 fileName = url;
182 }
183
184 // get the protocol name in lower cases
185 std::transform( protocol.begin(), protocol.end(),
186 protocol.begin(),
187 ToLower() );
188
189 if ( "ascii" == protocol ) {
190 m_ioBackend = new std::ofstream( fileName.c_str(),
191 std::ios::out | std::ios::trunc );
192
193 } else {
194 ATH_MSG_WARNING("UNKNOWN protocol [" << protocol << "] !!" << endmsg
195 << "Will use [ascii] instead...");
196 protocol = "ascii";
197 m_ioBackend = new std::ofstream( fileName.c_str(),
198 std::ios::out | std::ios::trunc );
199 }
200
201 ATH_MSG_DEBUG("Using protocol [" << protocol << "] and write to ["
202 << fileName << "]");
203}
#define endmsg
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
static const char *const s_protocolSep
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
ServiceHandle< StoreGateSvc > & evtStore()
size_type size() const noexcept
Returns the number of elements in the collection.
std::ofstream * m_ioBackend
Abstract base class for the back-end.
StatusCode finalize()
StatusCode initialize()
StatusCode write(const TruthParticleContainer *mcParts)
Process the TruthParticleContainer through the I/O backend.
void setupBackend(Gaudi::Details::PropertyBase &ioBackendURL)
Method to configure the back-end to write out the TruthParticleContainer.
virtual ~McAodWriterTool()
Destructor:
StringProperty m_ioBackendURL
URL of the I/O back-end (only "ASCII" for now...) glued with the name of the output file name.
McAodWriterTool()
Default constructor:
StringProperty m_truthParticlesName
Location of the TruthParticleContainer to be written out.
int barcode(const T *p)
Definition Barcode.h:16
ConeSize
Enum for Cone size indexes (for isolation)