ATLAS Offline Software
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
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 
42  const std::string& name,
43  const IInterface* 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 
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 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
TruthParticleParameters::NbrOfCones
@ NbrOfCones
Definition: TruthParticleParamDefs.h:31
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TruthParticleContainer.h
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
McAodWriterTool::m_ioBackend
std::ofstream * m_ioBackend
Abstract base class for the back-end.
Definition: McAodWriterTool.h:96
TruthParticle
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:58
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
TruthParticleParameters::ConeSize
ConeSize
Enum for Cone size indexes (for isolation)
Definition: TruthParticleParamDefs.h:20
McAodWriterTool::initialize
StatusCode initialize()
Definition: McAodWriterTool.cxx:79
TruthParticleContainer
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:42
McAodWriterTool::setupBackend
void setupBackend(Gaudi::Details::PropertyBase &ioBackendURL)
Method to configure the back-end to write out the TruthParticleContainer.
Definition: McAodWriterTool.cxx:159
physics_parameters.url
string url
Definition: physics_parameters.py:27
GenParticle.h
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
mc
Definition: mc.PG_single_nu_valid.py:1
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:92
McAodWriterTool::~McAodWriterTool
virtual ~McAodWriterTool()
Destructor:
Definition: McAodWriterTool.cxx:67
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
tolower
void tolower(std::string &s)
Definition: AthenaSummarySvc.cxx:113
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
test_pyathena.parent
parent
Definition: test_pyathena.py:15
McAodWriterTool::write
StatusCode write(const TruthParticleContainer *mcParts)
Process the TruthParticleContainer through the I/O backend.
Definition: McAodWriterTool.cxx:123
McAodWriterTool::finalize
StatusCode finalize()
Definition: McAodWriterTool.cxx:96
McAodWriterTool.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
McAodWriterTool::m_truthParticlesName
StringProperty m_truthParticlesName
Location of the TruthParticleContainer to be written out.
Definition: McAodWriterTool.h:92
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
McAodWriterTool::McAodWriterTool
McAodWriterTool()
Default constructor:
AthAlgTool
Definition: AthAlgTool.h:26
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
python.compressB64.c
def c
Definition: compressB64.py:93
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
McAodWriterTool::execute
StatusCode execute()
Definition: McAodWriterTool.cxx:105