ATLAS Offline Software
RootAsciiDumperAlgHandle.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // RootAsciiDumperAlgHandle.cxx
8 // Implementation file for class RootAsciiDumperAlgHandle
9 // Author: S.Binet<binet@cern.ch>
11 
12 // AthenaRootComps includes
14 
15 // STL includes
16 #include <sstream>
17 #include <stdio.h>
18 #include <stdexcept>
19 
20 #include <inttypes.h>
21 
22 // linux i/o includes
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <format>
27 
28 // FrameWork includes
29 #include "Gaudi/Property.h"
31 
32 // SGTools
33 #include "SGTools/BuiltinsClids.h" // to put ints,... in evtstore
34 #include "SGTools/StlVectorClids.h" // to put std::vectors... in evtstore
35 
36 namespace Athena {
37 
39 // Public methods:
41 
42 // Athena Algorithm's Hooks
45 {
46  ATH_MSG_INFO ("Initializing " << name() << "...");
53 
54  ATH_MSG_INFO("dumping data into file ["
55  << m_ofname << "]...");
56  if (m_ofname.empty()) {
57  ATH_MSG_ERROR("cannot dump data into an empty file name!");
58  return StatusCode::FAILURE;
59  }
60  m_ofd = open(m_ofname.value().c_str(),
61  O_WRONLY | O_CREAT | O_TRUNC,
62  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
63 
64  if (m_ofd < 0) {
65  ATH_MSG_ERROR("problem opening file [" << m_ofname << "] with "
66  "write permissions.");
67  return StatusCode::FAILURE;
68  }
69 
70  return StatusCode::SUCCESS;
71 }
72 
74 {
75  ATH_MSG_INFO ("Finalizing " << name() << "...");
76 
77  if (m_ofd > 0) {
78  fflush(NULL);
79  if (close(m_ofd) < 0) {
80  ATH_MSG_WARNING("problem while closing [" << m_ofname << "]");
81  }
82  }
83 
84  return StatusCode::SUCCESS;
85 }
86 
88 {
89  ATH_MSG_DEBUG ("Executing " << name() << "...");
90 
91  const EventContext& ctx = Gaudi::Hive::currentContext();
92 
94  m_nentries += 1;
95 
96  SG::ReadHandle<uint32_t> runnbr (m_runnbr, ctx);
97  SG::ReadHandle<uint32_t> evtnbr (m_evtnbr, ctx);
98 
99  SG::ReadHandle<int32_t> el_n (m_el_n, ctx);
100 
102  static const SG::ConstAccessor<std::string> tupleName ("tupleName");
103  static const SG::ConstAccessor<std::string> collectionName ("collectionName");
104  std::string collName = collectionName(*ei);
105  std::string::size_type pos = collName.rfind ("/");
106  if (pos != std::string::npos) {
107  collName.erase (0, pos+1);
108  }
109 
110  std::string buf = std::format(
111  "{:03}.{} = {}\n"
112  "{:03}.{} = {}\n"
113  "{:03}.{} = {}\n"
114  "{:03}.{} = {}\n"
115  "{:03}.{} = {}\n",
116  nevts, "collectionName", collName,
117  nevts, "tupleName", tupleName(*ei),
118  nevts, "RunNumber", *runnbr,
119  nevts, "EventNumber", *evtnbr,
120  nevts, "el_n", *el_n
121  );
122 
123  if (buf.empty()) {
124  throw std::runtime_error("Empty buffer in RootAsciiDumperAlgHandle::execute");
125  }
126 
127  if (write(m_ofd, buf.data(), buf.size()) == -1) {
128  throw std::runtime_error("Failed to write buffer to file descriptor");
129  }
130 
131 
132  if (*el_n > 0) {
135 
136  std::ostringstream bufv;
137  for (int32_t ii = 0; ii < *el_n; ++ii) {
138  bufv << (*el_eta)[ii];
139  if (ii != (*el_n) - 1) {
140  bufv << ", ";
141  }
142  }
143 
144  buf = std::format(
145  "{:03}.{} = [{}]\n",
146  nevts,
147  "el_eta",
148  bufv.str()
149  );
150 
151  if (buf.empty()) {
152  throw std::runtime_error("Empty buffer in RootAsciiDumperAlgHandle::execute");
153  }
154 
155  if (write(m_ofd, buf.data(), buf.size()) == -1) {
156  throw std::runtime_error("Failed to write buffer to file descriptor");
157  }
158  bufv.str("");//clear
159  for (int32_t ii = 0; ii < *el_n; ++ii) {
160  bufv << "[";
161  for (std::size_t jj = 0; jj < (*el_jetcone_dr)[ii].size(); ++jj) {
162  bufv << (*el_jetcone_dr)[ii][jj];
163  if (jj + 1 < (*el_jetcone_dr)[ii].size()) {
164  bufv << ", ";
165  }
166  }
167  bufv << "]";
168  if (ii + 1 < *el_n) {
169  bufv << ", ";
170  }
171  }
172 
173  buf = std::format(
174  "{:03}.{} = [{}]\n",
175  nevts,
176  "el_jetcone_dr",
177  bufv.str()
178  );
179 
180  if (write(m_ofd, buf.data(), buf.size()) == -1) {
181  throw std::runtime_error("Failed to write buffer to file descriptor");
182  }
183  }
184 
185  return StatusCode::SUCCESS;
186 }
187 
188 } //> end namespace Athena
Athena::RootAsciiDumperAlgHandle::m_eiKey
SG::ReadHandleKey< xAOD::EventInfo > m_eiKey
Definition: RootAsciiDumperAlgHandle.h:98
vtune_athena.format
format
Definition: vtune_athena.py:14
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Athena::RootAsciiDumperAlgHandle::m_ofd
int m_ofd
file handle to the ASCII output file
Definition: RootAsciiDumperAlgHandle.h:72
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
Athena::RootAsciiDumperAlgHandle::finalize
virtual StatusCode finalize()
Definition: RootAsciiDumperAlgHandle.cxx:73
BuiltinsClids.h
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
Athena::RootAsciiDumperAlgHandle::m_el_eta
SG::ReadHandleKey< std::vector< float > > m_el_eta
eta of electrons
Definition: RootAsciiDumperAlgHandle.h:91
Athena::RootAsciiDumperAlgHandle::m_ofname
StringProperty m_ofname
ASCII output file name.
Definition: RootAsciiDumperAlgHandle.h:67
Athena::RootAsciiDumperAlgHandle::m_evtnbr
SG::ReadHandleKey< uint32_t > m_evtnbr
event number
Definition: RootAsciiDumperAlgHandle.h:83
Athena::RootAsciiDumperAlgHandle::m_runnbr
SG::ReadHandleKey< uint32_t > m_runnbr
run number
Definition: RootAsciiDumperAlgHandle.h:79
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
StlVectorClids.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Athena::RootAsciiDumperAlgHandle::m_el_n
SG::ReadHandleKey< int32_t > m_el_n
number of electrons
Definition: RootAsciiDumperAlgHandle.h:87
Athena
Some weak symbol referencing magic...
Definition: AthLegacySequence.h:21
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
python.ByteStreamConfig.write
def write
Definition: Event/ByteStreamCnvSvc/python/ByteStreamConfig.py:248
Athena::RootAsciiDumperAlgHandle::initialize
virtual StatusCode initialize()
Definition: RootAsciiDumperAlgHandle.cxx:44
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
sg-dump.nevts
nevts
Definition: sg-dump.py:145
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
Athena::RootAsciiDumperAlgHandle::m_nentries
uint64_t m_nentries
number of entries processed so-far
Definition: RootAsciiDumperAlgHandle.h:75
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:16
Trk::open
@ open
Definition: BinningType.h:40
Athena::RootAsciiDumperAlgHandle::execute
virtual StatusCode execute()
Definition: RootAsciiDumperAlgHandle.cxx:87
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
plotIsoValidation.el_eta
el_eta
Definition: plotIsoValidation.py:164
ConstAccessor.h
Helper class to provide constant type-safe access to aux data.
RootAsciiDumperAlgHandle.h
Athena::RootAsciiDumperAlgHandle::m_el_jetcone_dr
SG::ReadHandleKey< std::vector< std::vector< float > > > m_el_jetcone_dr
jetcone dR
Definition: RootAsciiDumperAlgHandle.h:95