ATLAS Offline Software
Loading...
Searching...
No Matches
SGAudSvc.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7// SGAudSvc.cxx
8// Implementation file for class SGAudSvc
9// Author: Ilija Vukotic<ivukotic@cern.ch>
11
12
13// STL includes
14#include <sstream>
15#include <fstream>
16#include <vector>
17#include <set>
18#include <utility>
19
20// FrameWork includes
21#include "Gaudi/Property.h"
22#include "GaudiKernel/IIncidentSvc.h"
23#include "GaudiKernel/Incident.h"
24#include "GaudiKernel/IAlgContextSvc.h"
25
26// StoreGate includes
28
29// SGAudSvc includes
30#include "SGAudSvc.h"
32// Public methods:
34
35/*----------------------------------------------------------------------------*/
36// Constructors
38SGAudSvc::SGAudSvc( const std::string& name, ISvcLocator* pSvcLocator ) :
39 base_class ( name, pSvcLocator ),
40 p_algCtxSvc("AlgContextSvc", name),
41 m_pCID("ClassIDSvc", name)
42{
43}
44
45
46/*----------------------------------------------------------------------------*/
47
48// Athena Algorithm's Hooks
50StatusCode
52
53 ATH_MSG_INFO("Initializing " << name() << "..." );
54
55 ATH_CHECK( p_algCtxSvc.retrieve() );
56 ATH_CHECK( m_pCID.retrieve() );
57
58 if (m_allFileName != "") {
59 m_ofa.open(m_allFileName.value().c_str());
60 }
61
62 if (m_sumFileName != "") {
63 m_ofs.open(m_sumFileName.value().c_str());
64 }
65
66 // Set to be listener for end-of-event
67 ServiceHandle<IIncidentSvc> incSvc( "IncidentSvc", this->name() );
68 ATH_CHECK( incSvc.retrieve() );
69
70 incSvc->addListener( this, IncidentType::BeginRun );
71 incSvc->addListener( this, IncidentType::BeginEvent );
72 incSvc->addListener( this, IncidentType::EndEvent );
73
74 return StatusCode::SUCCESS;
75}
76
77/*----------------------------------------------------------------------------*/
78
79StatusCode
81
82 if (m_vAlg.size()==0) {
83 ATH_MSG_WARNING("No data gathered. This might be because you did not run over at least 3 events.");
84 return StatusCode::SUCCESS;
85 }
86
87 ATH_MSG_INFO("Writing output to: "<<m_outFileName);
88 std::ofstream f( m_outFileName.value().c_str() );
89
90 f << "Algs: " << m_vAlg.size() << std::endl;
91 std::vector<std::string>::iterator i;
92 for (i=m_vAlg.begin();i<m_vAlg.end();++i) {
93 f << (*i) << std::endl;
94 }
95
96 f << "Obj: "<< m_vObj.size()<<std::endl;
97 for (i=m_vObj.begin();i<m_vObj.end();++i) {
98 f << (*i) << std::endl;
99 }
100
101
102 for (unsigned int w=0;w<m_vAlg.size();w++){
103 for(unsigned int q=0;q<m_vObj.size();q++){
104 int oaHash=q*1000 + w;
105 if (m_timesRead.find(oaHash)==m_timesRead.end())
106 f << "0:";
107 else
108 f << m_timesRead.find(oaHash)->second << ":";
109 if (m_timesWritten.find(oaHash)==m_timesWritten.end())
110 f << "0\t";
111 else
112 f << m_timesWritten.find(oaHash)->second << "\t";
113 }
114 f << std::endl;
115 }
116
117 f.close();
118
119 if (m_ofa.is_open()) m_ofa.close();
120
121 if (m_ofs.is_open()) {
122
123 writeJSON();
124
125 m_ofs.close();
126 }
127
128
129 return StatusCode::SUCCESS;
130}
131
132/*----------------------------------------------------------------------------*/
133
134void
135SGAudSvc::handle( const Incident& inc )
136{
137 ATH_MSG_VERBOSE("Entering handle(). Incidence type: " << inc.type() <<
138 " from: " << inc.source());
139
140 if (inc.type() == IncidentType::BeginEvent) {
141 if (m_ofa.is_open())
142 m_ofa << "---- BEGIN EVENT " << m_nEvents << " ----" << std::endl;
143 m_inExec = true;
144
145 }
146
147 // Performing performance-monitoring for EndEvent incident
148 if ( inc.type() == IncidentType::EndEvent ) {
149 monitor();
150
151 if (m_ofa.is_open())
152 m_ofa << "---- END EVENT " << m_nEvents << " ----" << std::endl;
153 m_inExec = false;
154
155 }
156
157 // Performing performance-monitoring for BeginRun incident
158 // at this point everybody has been initialized, we can harvest performance
159 // data for the initialize step.
160
161 if ( inc.type() == IncidentType::BeginRun ) {
162 monitor();
163 }
164
165 return;
166}
167
168
169/*----------------------------------------------------------------------------*/
170
171void
173 m_nEvents++;
174 return;
175}
176
177/*----------------------------------------------------------------------------*/
178
179void
180SGAudSvc::SGAudRETRIEVE(std::string SGobject){
181 // if (m_nEvents<3) return;
182 if (SGobject=="") SGobject="noKey";
183 bool isAnumb=true;
184 for (unsigned int i = 0; i < SGobject.length(); i++)
185 if (!std::isdigit(SGobject[i])){
186 isAnumb=false;
187 break;
188 }
189
190 if (!isAnumb){
191 if (m_currObj==SGobject)
192 addRead();
193 else {
194 getNobj(SGobject);
195 addRead();
196 }
197 }
198 return;
199}
200
201/*----------------------------------------------------------------------------*/
202
203void
204SGAudSvc::SGAudRECORD(std::string SGobject){
205 // if (m_nEvents<3) return;
206 if (SGobject=="") SGobject="noKey";
207 for (unsigned int i = 0; i < SGobject.length(); i++)
208 if (!std::isdigit(SGobject[i])){
209 //isAnumb=false;
210 break;
211 }
212
213 if (m_currObj==SGobject)
214 addWrite();
215 else {
216 getNobj(SGobject);
217 addWrite();
218 }
219
220 return;
221}
222
223/*----------------------------------------------------------------------------*/
224void
225SGAudSvc::SGAudit(const std::string& key, const CLID& id,
226 const int& typ, const int& store_id) {
227
228 // we can sometimes get here really early, before initialization.
229 if (m_pCID == 0) { return; }
230
231 bool a = SGGetCurrentAlg();
232
233 if (m_nEvents >= m_startEvent && store_id == 0 && a) {
234 if (typ == 0) {
235 SGAudRETRIEVE(key);
236 } else {
237 SGAudRECORD(key);
238 }
239 }
240
241 std::string idname;
242
243 if (m_ofa.is_open() || ( m_ofs.is_open() && ! m_useCLID ) ) {
244 if( ! m_pCID->getTypeNameOfID(id,idname).isSuccess()) {
245 std::ostringstream ost;
246 ost << id;
247 idname = ost.str();
248 }
249 }
250
251 if (m_ofa.is_open()) {
252 m_ofa << ( (typ == 1) ? "RECORD" : "RETRIEVE" ) << " clid: " << id
253 << " | ";
254
255 m_ofa << idname;
256
257 m_ofa << " key: " << key << " alg: " << m_currAlg
258 << " store: " << store_id
259 << std:: endl;
260 }
261
262 // store stuff for the summary
263
264 if (!m_ofs.is_open()) return;
265
266 if (m_nEvents < m_startEvent || !m_inExec) return;
267
268 if (m_currAlg == "----") return;
269
270 std::string kk;
271 if (m_useCLID) {
272 std::ostringstream ost;
273 ost << id << "/" << key;
274 kk = ost.str();
275 } else {
276 kk = idname + "/" + key;
277 }
278
279 DataMap::iterator itr;
280 if (typ == 0) {
281 itr = m_read.find(m_currAlg);
282 if (itr != m_read.end()) {
283 itr->second.insert(std::move(kk));
284 } else {
285 m_read[m_currAlg] = std::set<std::string> ( {std::move(kk)} );
286 }
287 } else {
288 itr = m_write.find(m_currAlg);
289 if (itr != m_write.end()) {
290 itr->second.insert(std::move(kk));
291 } else {
292 m_write[m_currAlg] = std::set<std::string> ( {std::move(kk)} );
293 }
294 }
295}
296
297/*----------------------------------------------------------------------------*/
298
299bool
301
302 IAlgorithm *asdf = p_algCtxSvc->currentAlg();
303 if (!asdf || m_nEvents==0) { // to skip before first event
304 m_currAlg="----";
305 m_nCurrAlg=-1;
306 return false;
307 }
308 std::string name = asdf->name();
309
310 if ( !m_ignoreFakeAlgs && !m_fakeCurrAlg.empty() )
311 name=m_fakeCurrAlg;
312
313 if (name!=m_currAlg){
314 std::vector<std::string>::iterator i;
315 int index=0;
316 for (i=m_vAlg.begin();i<m_vAlg.end();++i){
317 if (*i==name) {
319 m_currAlg=std::move(name);
320 return true;
321 }
322 index++;
323 }
324 m_vAlg.push_back(name);
326 m_currAlg=std::move(name);
327 return true;
328 }
329 return true;
330}
331
332/*----------------------------------------------------------------------------*/
333
334void
335SGAudSvc::getNobj(const std::string& name){
336 std::vector<std::string>::iterator i;
337 int index=0;
338 for (i=m_vObj.begin();i<m_vObj.end();++i){
339 if (*i==name) {
341 m_currObj=name;
342 return;
343 }
344 index++;
345 }
346 m_vObj.push_back(name);
348 m_currObj=name;
349return;
350}
351
352/*----------------------------------------------------------------------------*/
353
354void
356 int oaHash=m_nCurrObj*1000 + m_nCurrAlg;
357 if (m_timesRead.end()!=m_timesRead.find(oaHash)){
358 m_timesRead.find(oaHash)->second++;
359 }
360 else{
361 std::pair<int,int> p(oaHash,1);
362 m_timesRead.insert(p);
363 }
364 return;
365}
366
367/*----------------------------------------------------------------------------*/
368
369void
371 int oaHash=m_nCurrObj*1000 + m_nCurrAlg;
372 if (m_timesWritten.end()!=m_timesWritten.find(oaHash)){
373 m_timesWritten.find(oaHash)->second++;
374 }
375 else{
376 std::pair<int,int> p(oaHash,1);
377 m_timesWritten.insert(p);
378 }
379 return;
380}
381
382/*----------------------------------------------------------------------------*/
383
384void
385SGAudSvc::setFakeCurrentAlg( const std::string& s ) { m_fakeCurrAlg=s; }
386
387/*----------------------------------------------------------------------------*/
388
389void
391
392/*----------------------------------------------------------------------------*/
393
394void
396
397 DataMap::const_iterator itr;
398 std::set<std::string>::const_iterator it2;
399 std::vector<std::string>::const_iterator ia;
400
401 m_ofs << "{ \"algorithms\" : [" << std::endl;
402 for (ia = m_vAlg.begin(); ia != m_vAlg.end(); ++ia) {
403 m_ofs << " {" << std::endl;
404 m_ofs << " \"name\" : \"" << *ia << "\"," << std::endl;
405 m_ofs << " \"inputs\" : [";
406
407 itr = m_read.find(*ia);
408 if (itr != m_read.end()) {
409 for (it2 = itr->second.begin(); it2 != itr->second.end(); ++it2) {
410 if (it2 != itr->second.begin()) m_ofs << ",";
411 m_ofs << "\"" << *it2 << "\"";
412 }
413 }
414 m_ofs << "]," << std::endl;
415
416 m_ofs << " \"outputs\" : [";
417
418 itr = m_write.find(*ia);
419 if (itr != m_write.end()) {
420 for (it2 = itr->second.begin(); it2 != itr->second.end(); ++it2) {
421 if (it2 != itr->second.begin()) m_ofs << ",";
422 m_ofs << "\"" << *it2 << "\"";
423 }
424 }
425 m_ofs << "]," << std::endl;
426 m_ofs << " }," << std::endl;
427 }
428 m_ofs << " ]" << std::endl << "}" << std::endl;
429
430}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
uint32_t CLID
The Class ID type.
static Double_t a
Gaudi::Property< std::string > m_outFileName
Definition SGAudSvc.h:100
bool m_inExec
Definition SGAudSvc.h:143
void addRead()
Definition SGAudSvc.cxx:355
std::string m_fakeCurrAlg
Definition SGAudSvc.h:132
std::string m_currObj
Definition SGAudSvc.h:131
virtual void setFakeCurrentAlg(const std::string &) override
For implementing custom increased granularity auditing of for instance tools.
Definition SGAudSvc.cxx:385
std::string m_currAlg
Definition SGAudSvc.h:130
std::map< int, int > m_timesWritten
map counting Writes of each object by each algorithm.
Definition SGAudSvc.h:129
void SGAudRECORD(std::string SGobject)
Definition SGAudSvc.cxx:204
DataMap m_write
Definition SGAudSvc.h:140
std::map< int, int > m_timesRead
map counting Reads of each object by each algorithm.
Definition SGAudSvc.h:127
virtual void SGAudit(const std::string &key, const CLID &id, const int &fnc, const int &store_id) override
Definition SGAudSvc.cxx:225
void monitor()
just counts events. called at EndEvent incident
Definition SGAudSvc.cxx:172
bool SGGetCurrentAlg()
Gets name of curently running algorithm from AlgContextSvc.
Definition SGAudSvc.cxx:300
void SGAudRETRIEVE(std::string SGobject)
Definition SGAudSvc.cxx:180
Gaudi::Property< bool > m_useCLID
Definition SGAudSvc.h:115
std::ofstream m_ofs
Definition SGAudSvc.h:142
virtual StatusCode initialize() override
Gaudi Service Implementation.
Definition SGAudSvc.cxx:51
int m_nCurrObj
Definition SGAudSvc.h:134
int m_nEvents
Definition SGAudSvc.h:135
SGAudSvc()
Default constructor:
virtual void clearFakeCurrentAlg() override
For implementing custom increased granularity auditing of for instance tools.
Definition SGAudSvc.cxx:390
ServiceHandle< IClassIDSvc > m_pCID
Definition SGAudSvc.h:120
std::vector< std::string > m_vAlg
Vector of names of algorithms accessing SG.
Definition SGAudSvc.h:125
int m_nCurrAlg
Definition SGAudSvc.h:133
Gaudi::Property< std::string > m_sumFileName
Definition SGAudSvc.h:106
virtual void handle(const Incident &incident) override
incident service handle for EndEvent.
Definition SGAudSvc.cxx:135
DataMap m_read
Definition SGAudSvc.h:139
void writeJSON()
Definition SGAudSvc.cxx:395
Gaudi::Property< int > m_startEvent
Definition SGAudSvc.h:112
void getNobj(const std::string &name)
Definition SGAudSvc.cxx:335
virtual StatusCode finalize() override
Definition SGAudSvc.cxx:80
Gaudi::Property< bool > m_ignoreFakeAlgs
Definition SGAudSvc.h:109
void addWrite()
Definition SGAudSvc.cxx:370
std::vector< std::string > m_vObj
Vector of accessed SG objects names.
Definition SGAudSvc.h:123
std::ofstream m_ofa
Definition SGAudSvc.h:142
Gaudi::Property< std::string > m_allFileName
Definition SGAudSvc.h:103
ServiceHandle< IAlgContextSvc > p_algCtxSvc
Pointer to the AlgContextScv.
Definition SGAudSvc.h:119
Definition index.py:1