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