ATLAS Offline Software
Loading...
Searching...
No Matches
RootAsciiDumperAlg.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
5*/
6
7// RootAsciiDumperAlg.cxx
8// Implementation file for class RootAsciiDumperAlg
9// Author: S.Binet<binet@cern.ch>
11
12// AthenaRootComps includes
13#include "RootAsciiDumperAlg.h"
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
36namespace Athena {
37
39// Public methods:
41
42// Athena Algorithm's Hooks
45{
46 ATH_MSG_INFO ("Initializing " << name() << "...");
47 ATH_CHECK( m_eiKey.initialize() );
48 ATH_CHECK( m_el_jetcone_dr.initialize() );
49 ATH_CHECK( m_el_n.initialize() );
50 ATH_CHECK( m_el_eta.initialize() );
51 ATH_CHECK( m_evtnbr.initialize() );
52 ATH_CHECK( m_runnbr.initialize() );
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
87StatusCode RootAsciiDumperAlg::execute(const EventContext& ctx)
88{
89 ATH_MSG_DEBUG ("Executing " << name() << "...");
90
91
92 uint64_t nevts = m_nentries;
93 m_nentries += 1;
94
97
99
101 static const SG::ConstAccessor<std::string> tupleName ("tupleName");
102 static const SG::ConstAccessor<std::string> collectionName ("collectionName");
103 std::string collName = collectionName(*ei);
104 std::string::size_type pos = collName.rfind ("/");
105 if (pos != std::string::npos) {
106 collName.erase (0, pos+1);
107 }
108
109 std::string buf = std::format(
110 "{:03}.{} = {}\n"
111 "{:03}.{} = {}\n"
112 "{:03}.{} = {}\n"
113 "{:03}.{} = {}\n"
114 "{:03}.{} = {}\n",
115 nevts, "collectionName", collName,
116 nevts, "tupleName", tupleName(*ei),
117 nevts, "RunNumber", *runnbr,
118 nevts, "EventNumber", *evtnbr,
119 nevts, "el_n", *el_n
120 );
121
122 if (buf.empty()) {
123 throw std::runtime_error("Empty buffer in RootAsciiDumperAlg::execute");
124 }
125
126 if (write(m_ofd, buf.data(), buf.size()) == -1) {
127 throw std::runtime_error("Failed to write buffer to file descriptor");
128 }
129
130
131 if (*el_n > 0) {
134
135 std::ostringstream bufv;
136 for (int32_t ii = 0; ii < *el_n; ++ii) {
137 bufv << (*el_eta)[ii];
138 if (ii != (*el_n) - 1) {
139 bufv << ", ";
140 }
141 }
142
143 buf = std::format(
144 "{:03}.{} = [{}]\n",
145 nevts,
146 "el_eta",
147 bufv.str()
148 );
149
150 if (buf.empty()) {
151 throw std::runtime_error("Empty buffer in RootAsciiDumperAlg::execute");
152 }
153
154 if (write(m_ofd, buf.data(), buf.size()) == -1) {
155 throw std::runtime_error("Failed to write buffer to file descriptor");
156 }
157 bufv.str("");//clear
158 for (int32_t ii = 0; ii < *el_n; ++ii) {
159 bufv << "[";
160 for (std::size_t jj = 0; jj < (*el_jetcone_dr)[ii].size(); ++jj) {
161 bufv << (*el_jetcone_dr)[ii][jj];
162 if (jj + 1 < (*el_jetcone_dr)[ii].size()) {
163 bufv << ", ";
164 }
165 }
166 bufv << "]";
167 if (ii + 1 < *el_n) {
168 bufv << ", ";
169 }
170 }
171
172 buf = std::format(
173 "{:03}.{} = [{}]\n",
174 nevts,
175 "el_jetcone_dr",
176 bufv.str()
177 );
178
179 if (write(m_ofd, buf.data(), buf.size()) == -1) {
180 throw std::runtime_error("Failed to write buffer to file descriptor");
181 }
182 }
183
184 return StatusCode::SUCCESS;
185}
186
187} //> end namespace Athena
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Helper class to provide constant type-safe access to aux data.
size_t size() const
Number of registered mappings.
StringProperty m_ofname
ASCII output file name.
SG::ReadHandleKey< uint32_t > m_evtnbr
event number
int m_ofd
file handle to the ASCII output file
SG::ReadHandleKey< std::vector< std::vector< float > > > m_el_jetcone_dr
jetcone dR
SG::ReadHandleKey< xAOD::EventInfo > m_eiKey
SG::ReadHandleKey< uint32_t > m_runnbr
run number
uint64_t m_nentries
number of entries processed so-far
SG::ReadHandleKey< int32_t > m_el_n
number of electrons
virtual StatusCode execute(const EventContext &ctx)
Execute method.
SG::ReadHandleKey< std::vector< float > > m_el_eta
eta of electrons
virtual StatusCode initialize()
Helper class to provide constant type-safe access to aux data.
Some weak symbol referencing magic... These are declared in AthenaKernel/getMessageSvc....
Definition AthDsoUtils.h:10
static char buf[SIGNAL_MESSAGE_BUFSIZE]
Dump application state information on a fatal signal.