ATLAS Offline Software
SGAudSvc.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 // 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
27 #include "StoreGate/StoreGateSvc.h"
28 #include "StoreGate/DataHandle.h"
29 
30 // SGAudSvc includes
31 #include "SGAudSvc.h"
33 // Public methods:
35 
36 /*----------------------------------------------------------------------------*/
37 // Constructors
39 SGAudSvc::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
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 
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;
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 
135 void
136 SGAudSvc::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 
172 void
174  m_nEvents++;
175  return;
176 }
177 
178 /*----------------------------------------------------------------------------*/
179 
180 void
181 SGAudSvc::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 
204 void
205 SGAudSvc::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 /*----------------------------------------------------------------------------*/
225 void
226 SGAudSvc::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) {
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 
300 bool
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() )
313 
314  if (name!=m_currAlg){
316  int index=0;
317  for (i=m_vAlg.begin();i<m_vAlg.end();++i){
318  if (*i==name) {
320  m_currAlg=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 
335 void
336 SGAudSvc::getNobj(const std::string& name){
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;
350 return;
351 }
352 
353 /*----------------------------------------------------------------------------*/
354 
355 void
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 
370 void
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 
385 void
386 SGAudSvc::setFakeCurrentAlg( const std::string& s ) { m_fakeCurrAlg=s; }
387 
388 /*----------------------------------------------------------------------------*/
389 
390 void
392 
393 /*----------------------------------------------------------------------------*/
394 
395 void
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 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
SGAudSvc::addWrite
void addWrite()
Definition: SGAudSvc.cxx:371
SGAudSvc::SGGetCurrentAlg
bool SGGetCurrentAlg()
Gets name of curently running algorithm from AlgContextSvc.
Definition: SGAudSvc.cxx:301
SGAudSvc.h
python.trigbs_prescaleL1.ost
ost
Definition: trigbs_prescaleL1.py:104
SGAudSvc::m_pCID
ServiceHandle< IClassIDSvc > m_pCID
Definition: SGAudSvc.h:120
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SGAudSvc::m_timesWritten
std::map< int, int > m_timesWritten
map counting Writes of each object by each algorithm.
Definition: SGAudSvc.h:129
SGAudSvc::monitor
void monitor()
just counts events. called at EndEvent incident
Definition: SGAudSvc.cxx:173
SGAudSvc::SGAudRECORD
void SGAudRECORD(std::string SGobject)
Definition: SGAudSvc.cxx:205
index
Definition: index.py:1
SGAudSvc::clearFakeCurrentAlg
virtual void clearFakeCurrentAlg() override
For implementing custom increased granularity auditing of for instance tools.
Definition: SGAudSvc.cxx:391
SGAudSvc::m_ofa
std::ofstream m_ofa
Definition: SGAudSvc.h:142
SGAudSvc::m_allFileName
Gaudi::Property< std::string > m_allFileName
Definition: SGAudSvc.h:103
SGAudSvc::getNobj
void getNobj(const std::string &name)
Definition: SGAudSvc.cxx:336
SGAudSvc::m_startEvent
Gaudi::Property< int > m_startEvent
Definition: SGAudSvc.h:112
SGAudSvc::SGAudSvc
SGAudSvc()
Default constructor:
SGAudSvc::m_useCLID
Gaudi::Property< bool > m_useCLID
Definition: SGAudSvc.h:115
SGAudSvc::initialize
virtual StatusCode initialize() override
Gaudi Service Implementation.
Definition: SGAudSvc.cxx:52
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SGAudSvc::m_nCurrObj
int m_nCurrObj
Definition: SGAudSvc.h:134
SGAudSvc::writeJSON
void writeJSON()
Definition: SGAudSvc.cxx:396
DataHandle.h
SGAudSvc::finalize
virtual StatusCode finalize() override
Definition: SGAudSvc.cxx:81
SGAudSvc::handle
virtual void handle(const Incident &incident) override
incident service handle for EndEvent.
Definition: SGAudSvc.cxx:136
SGAudSvc::m_inExec
bool m_inExec
Definition: SGAudSvc.h:143
SGAudSvc::m_write
DataMap m_write
Definition: SGAudSvc.h:140
SGAudSvc::setFakeCurrentAlg
virtual void setFakeCurrentAlg(const std::string &) override
For implementing custom increased granularity auditing of for instance tools.
Definition: SGAudSvc.cxx:386
python.changerun.kk
list kk
Definition: changerun.py:39
SGAudSvc::m_currObj
std::string m_currObj
Definition: SGAudSvc.h:131
SGAudSvc::m_timesRead
std::map< int, int > m_timesRead
map counting Reads of each object by each algorithm.
Definition: SGAudSvc.h:127
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
SGAudSvc::m_currAlg
std::string m_currAlg
Definition: SGAudSvc.h:130
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SGAudSvc::m_outFileName
Gaudi::Property< std::string > m_outFileName
Definition: SGAudSvc.h:100
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
hist_file_dump.f
f
Definition: hist_file_dump.py:140
SGAudSvc::SGAudit
virtual void SGAudit(const std::string &key, const CLID &id, const int &fnc, const int &store_id) override
Definition: SGAudSvc.cxx:226
SGAudSvc::SGAudRETRIEVE
void SGAudRETRIEVE(std::string SGobject)
Definition: SGAudSvc.cxx:181
SGAudSvc::m_vAlg
std::vector< std::string > m_vAlg
Vector of names of algorithms accessing SG.
Definition: SGAudSvc.h:125
SGAudSvc::addRead
void addRead()
Definition: SGAudSvc.cxx:356
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:239
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
SGAudSvc::m_ignoreFakeAlgs
Gaudi::Property< bool > m_ignoreFakeAlgs
Definition: SGAudSvc.h:109
SGAudSvc::m_sumFileName
Gaudi::Property< std::string > m_sumFileName
Definition: SGAudSvc.h:106
SGAudSvc::m_ofs
std::ofstream m_ofs
Definition: SGAudSvc.h:142
SGAudSvc::p_algCtxSvc
ServiceHandle< IAlgContextSvc > p_algCtxSvc
Pointer to the AlgContextScv.
Definition: SGAudSvc.h:119
DeMoScan.index
string index
Definition: DeMoScan.py:362
a
TList * a
Definition: liststreamerinfos.cxx:10
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
extractSporadic.q
list q
Definition: extractSporadic.py:97
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:198
SGAudSvc::m_vObj
std::vector< std::string > m_vObj
Vector of accessed SG objects names.
Definition: SGAudSvc.h:123
SGAudSvc::m_nCurrAlg
int m_nCurrAlg
Definition: SGAudSvc.h:133
SGAudSvc::m_fakeCurrAlg
std::string m_fakeCurrAlg
Definition: SGAudSvc.h:132
StoreGateSvc.h
SGAudSvc::m_read
DataMap m_read
Definition: SGAudSvc.h:139
SGAudSvc::m_nEvents
int m_nEvents
Definition: SGAudSvc.h:135
ServiceHandle< IIncidentSvc >
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37