ATLAS Offline Software
Loading...
Searching...
No Matches
RootAsciiDumperAlgHandle.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// 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
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
88{
89 ATH_MSG_DEBUG ("Executing " << name() << "...");
90
91 const EventContext& ctx = Gaudi::Hive::currentContext();
92
93 uint64_t nevts = m_nentries;
94 m_nentries += 1;
95
98
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
#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.
uint64_t m_nentries
number of entries processed so-far
int m_ofd
file handle to the ASCII output file
SG::ReadHandleKey< int32_t > m_el_n
number of electrons
StringProperty m_ofname
ASCII output file name.
SG::ReadHandleKey< std::vector< std::vector< float > > > m_el_jetcone_dr
jetcone dR
SG::ReadHandleKey< std::vector< float > > m_el_eta
eta of electrons
SG::ReadHandleKey< uint32_t > m_runnbr
run number
SG::ReadHandleKey< uint32_t > m_evtnbr
event number
SG::ReadHandleKey< xAOD::EventInfo > m_eiKey
Helper class to provide constant type-safe access to aux data.
Some weak symbol referencing magic... These are declared in AthenaKernel/getMessageSvc....
static char buf[SIGNAL_MESSAGE_BUFSIZE]
Dump application state information on a fatal signal.