ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
PhysicsAnalysis
TruthParticleID
McParticleTools
src
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
20
#include "
AtlasHepMC/GenParticle.h
"
21
22
// McParticleEvent includes
23
#include "
McParticleEvent/TruthParticleContainer.h
"
24
25
// McParticleTools includes
26
#include "
McAodWriterTool.h
"
27
28
static
const
char
*
const
s_protocolSep
=
":"
;
29
30
struct
ToLower
31
{
32
char
operator()
(
char
c)
const
{
return
std::tolower(c); }
33
};
34
38
41
McAodWriterTool::McAodWriterTool
(
const
std::string&
type
,
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
67
McAodWriterTool::~McAodWriterTool
()
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
79
StatusCode
McAodWriterTool::initialize
()
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
) {
90
setupBackend
(
m_ioBackendURL
);
91
}
92
93
return
StatusCode::SUCCESS;
94
}
95
96
StatusCode
McAodWriterTool::finalize
()
97
{
98
ATH_MSG_INFO
(
"Finalizing "
<< name() <<
"..."
);
99
ATH_MSG_DEBUG
(
"Closing backend..."
);
100
m_ioBackend
->close();
101
102
return
StatusCode::SUCCESS;
103
}
104
105
StatusCode
McAodWriterTool::execute
()
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
123
StatusCode
McAodWriterTool::write
(
const
TruthParticleContainer
* mcParts )
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
() <<
'\n'
;
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
<<
'\n'
;
144
out <<
"EtIsol "
145
<< std::setw(3) <<
TruthParticleParameters::NbrOfCones
146
<<
" : "
;
147
for
(
unsigned
int
iCone = 0;
148
iCone !=
TruthParticleParameters::NbrOfCones
;
149
++iCone ) {
150
out <<
" "
<< std::setprecision(3) << std::setw(10)
151
<<
mc
->etIsol(
static_cast<
TruthParticleParameters::ConeSize
>
(iCone));
152
}
153
out <<
'\n'
;
154
}
155
156
return
StatusCode::SUCCESS;
157
}
158
159
void
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
}
endmsg
#define endmsg
Definition
AnalysisConfig_Ntuple.cxx:54
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition
AthMsgStreamMacros.h:33
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition
AthMsgStreamMacros.h:31
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition
AthMsgStreamMacros.h:32
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition
AthMsgStreamMacros.h:29
GenParticle.h
s_protocolSep
static const char *const s_protocolSep
Definition
HepMcFloatWriterTool.cxx:27
McAodWriterTool.h
TruthParticleContainer.h
AthAlgTool::AthAlgTool
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Definition
AthAlgTool.cxx:16
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Definition
AthCommonDataStore.h:145
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
Definition
AthCommonDataStore.h:85
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
McAodWriterTool::m_ioBackend
std::ofstream * m_ioBackend
Abstract base class for the back-end.
Definition
McAodWriterTool.h:96
McAodWriterTool::finalize
StatusCode finalize()
Definition
McAodWriterTool.cxx:96
McAodWriterTool::initialize
StatusCode initialize()
Definition
McAodWriterTool.cxx:79
McAodWriterTool::write
StatusCode write(const TruthParticleContainer *mcParts)
Process the TruthParticleContainer through the I/O backend.
Definition
McAodWriterTool.cxx:123
McAodWriterTool::setupBackend
void setupBackend(Gaudi::Details::PropertyBase &ioBackendURL)
Method to configure the back-end to write out the TruthParticleContainer.
Definition
McAodWriterTool.cxx:159
McAodWriterTool::~McAodWriterTool
virtual ~McAodWriterTool()
Destructor:
Definition
McAodWriterTool.cxx:67
McAodWriterTool::m_ioBackendURL
StringProperty m_ioBackendURL
URL of the I/O back-end (only "ASCII" for now...) glued with the name of the output file name.
Definition
McAodWriterTool.h:88
McAodWriterTool::McAodWriterTool
McAodWriterTool()
Default constructor:
McAodWriterTool::execute
StatusCode execute()
Definition
McAodWriterTool.cxx:105
McAodWriterTool::m_truthParticlesName
StringProperty m_truthParticlesName
Location of the TruthParticleContainer to be written out.
Definition
McAodWriterTool.h:92
TruthParticleContainer
Definition
PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:42
TruthParticle
(HepMC) Monte Carlo particle.
Definition
PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:58
HepMC::barcode
int barcode(const T *p)
Definition
Barcode.h:15
TruthParticleParameters::ConeSize
ConeSize
Enum for Cone size indexes (for isolation).
Definition
TruthParticleParamDefs.h:20
TruthParticleParameters::NbrOfCones
@ NbrOfCones
Definition
TruthParticleParamDefs.h:31
mc
Definition
mc.Ep4_simple_OO.py:1
type
ToLower
Definition
McAodWriterTool.cxx:31
ToLower::operator()
char operator()(char c) const
Definition
McAodWriterTool.cxx:32
Generated on
for ATLAS Offline Software by
1.17.0