ATLAS Offline Software
SGAudSvc.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2024 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  m_msg ( msgSvc(), name ),
42  p_algCtxSvc("AlgContextSvc", name),
43  m_pCID("ClassIDSvc", name),
44  m_useCLID(true),
45  m_nCurrAlg(0),
46  m_nCurrObj(0),
47  m_nEvents(0), m_startEvent(3), m_inExec(false)
48 {
49  //
50  // Property declaration
51  //
52  declareProperty( "OutFileName", m_outFileName = "SGAudSvc.out",
53  "Name of the output file to hold SGAudSvc data" );
54 
55  declareProperty( "FullFileName", m_allFileName = "",
56  "Name of the output file to hold the full SG aud data");
57 
58  declareProperty( "SummaryFileName", m_sumFileName = "",
59  "Name of the output file to hold the summary output in json format");
60 
61  declareProperty( "IgnoreFakeAlgs", m_ignoreFakeAlgs = false,
62  "Set to ignore any attempts to override current-alg" );
63 
64  declareProperty( "StartEvent", m_startEvent = 3,
65  "Event number to start recording data" );
66 
67  declareProperty( "UseCLID", m_useCLID = true,
68  "Use CLID or DataObj name in Summary File" );
69 }
70 
71 /*----------------------------------------------------------------------------*/
72 // Destructor
75 {
76  m_msg << MSG::DEBUG << "Calling destructor" << endmsg;
77 }
78 
79 /*----------------------------------------------------------------------------*/
80 
81 // Athena Algorithm's Hooks
85 
86  // initialize MsgStream
87  m_msg.setLevel( m_outputLevel.value() );
88 
89  m_msg << MSG::INFO << "Initializing " << name() << "..." << endmsg;
90 
91  if ( AthService::initialize().isFailure() ) {
92  m_msg << MSG::ERROR << "Could not intialize base class !!" << endmsg;
93  return StatusCode::FAILURE;
94  }
95 
96  ATH_CHECK( p_algCtxSvc.retrieve() );
97  ATH_CHECK( m_pCID.retrieve() );
98 
99  if (m_allFileName != "") {
100  m_ofa.open(m_allFileName.c_str());
101  }
102 
103  if (m_sumFileName != "") {
104  m_ofs.open(m_sumFileName.c_str());
105  }
106 
107  // Set to be listener for end-of-event
108  ServiceHandle<IIncidentSvc> incSvc( "IncidentSvc", this->name() );
109  ATH_CHECK( incSvc.retrieve() );
110 
111  incSvc->addListener( this, IncidentType::BeginRun );
112  incSvc->addListener( this, IncidentType::BeginEvent );
113  incSvc->addListener( this, IncidentType::EndEvent );
114 
115  return StatusCode::SUCCESS;
116 }
117 
118 /*----------------------------------------------------------------------------*/
119 
120 StatusCode
122 
123  m_msg << MSG::INFO << "Finalizing " << name() <<"..."<< endmsg;
124 
125  if (m_vAlg.size()==0) {
126  m_msg << MSG::WARNING<<"No data gathered. This might be because you did not run over at least 3 events."<<endmsg;
127  return StatusCode::SUCCESS;
128  }
129 
130  m_msg << MSG::INFO<<"Writing output to: "<<m_outFileName<<endmsg;
131  std::ofstream f( m_outFileName.c_str() );
132 
133  f << "Algs: " << m_vAlg.size() << std::endl;
135  for (i=m_vAlg.begin();i<m_vAlg.end();++i) {
136  f << (*i) << std::endl;
137  }
138 
139  f << "Obj: "<< m_vObj.size()<<std::endl;
140  for (i=m_vObj.begin();i<m_vObj.end();++i) {
141  f << (*i) << std::endl;
142  }
143 
144 
145  for (unsigned int w=0;w<m_vAlg.size();w++){
146  for(unsigned int q=0;q<m_vObj.size();q++){
147  int oaHash=q*1000 + w;
148  if (m_timesRead.find(oaHash)==m_timesRead.end())
149  f << "0:";
150  else
151  f << m_timesRead.find(oaHash)->second << ":";
152  if (m_timesWritten.find(oaHash)==m_timesWritten.end())
153  f << "0\t";
154  else
155  f << m_timesWritten.find(oaHash)->second << "\t";
156  }
157  f << std::endl;
158  }
159 
160  f.close();
161 
162  if (m_ofa.is_open()) m_ofa.close();
163 
164  if (m_ofs.is_open()) {
165 
166  writeJSON();
167 
168  m_ofs.close();
169  }
170 
171 
172  return StatusCode::SUCCESS;
173 }
174 
175 /*----------------------------------------------------------------------------*/
176 
177 void
178 SGAudSvc::handle( const Incident& inc )
179 {
180  if ( m_msg.level() <= MSG::VERBOSE ) {
181  m_msg << MSG::VERBOSE << "Entering handle(): " << endmsg
182  << " Incidence type: " << inc.type() << endmsg
183  << " from: " << inc.source() << endmsg;
184  }
185 
186  if (inc.type() == IncidentType::BeginEvent) {
187  if (m_ofa.is_open())
188  m_ofa << "---- BEGIN EVENT " << m_nEvents << " ----" << std::endl;
189  m_inExec = true;
190 
191  }
192 
193  // Performing performance-monitoring for EndEvent incident
194  if ( inc.type() == IncidentType::EndEvent ) {
195  monitor();
196 
197  if (m_ofa.is_open())
198  m_ofa << "---- END EVENT " << m_nEvents << " ----" << std::endl;
199  m_inExec = false;
200 
201  }
202 
203  // Performing performance-monitoring for BeginRun incident
204  // at this point everybody has been initialized, we can harvest performance
205  // data for the initialize step.
206 
207  if ( inc.type() == IncidentType::BeginRun ) {
208  monitor();
209  }
210 
211  return;
212 }
213 
215 // Protected methods:
217 
219 // Const methods:
221 
223 // Non-const methods:
225 
226 /*----------------------------------------------------------------------------*/
227 
228 void
230  m_nEvents++;
231  return;
232 }
233 
234 /*----------------------------------------------------------------------------*/
235 
236 void
237 SGAudSvc::SGAudRETRIEVE(std::string SGobject){
238  // if (m_nEvents<3) return;
239  if (SGobject=="") SGobject="noKey";
240  bool isAnumb=true;
241  for (unsigned int i = 0; i < SGobject.length(); i++)
242  if (!std::isdigit(SGobject[i])){
243  isAnumb=false;
244  break;
245  }
246 
247  if (!isAnumb){
248  if (m_currObj==SGobject)
249  addRead();
250  else {
251  getNobj(SGobject);
252  addRead();
253  }
254  }
255  return;
256 }
257 
258 /*----------------------------------------------------------------------------*/
259 
260 void
261 SGAudSvc::SGAudRECORD(std::string SGobject){
262  // if (m_nEvents<3) return;
263  if (SGobject=="") SGobject="noKey";
264  for (unsigned int i = 0; i < SGobject.length(); i++)
265  if (!std::isdigit(SGobject[i])){
266  //isAnumb=false;
267  break;
268  }
269 
270  if (m_currObj==SGobject)
271  addWrite();
272  else {
273  getNobj(SGobject);
274  addWrite();
275  }
276 
277  return;
278 }
279 
280 /*----------------------------------------------------------------------------*/
281 void
282 SGAudSvc::SGAudit(const std::string& key, const CLID& id,
283  const int& typ, const int& store_id) {
284 
285  // we can sometimes get here really early, before initialization.
286  if (m_pCID == 0) { return; }
287 
288  bool a = SGGetCurrentAlg();
289 
290  if (m_nEvents >= m_startEvent && store_id == 0 && a) {
291  if (typ == 0) {
293  } else {
294  SGAudRECORD(key);
295  }
296  }
297 
298  std::string idname;
299 
300  if (m_ofa.is_open() || ( m_ofs.is_open() && ! m_useCLID ) ) {
301  if( ! m_pCID->getTypeNameOfID(id,idname).isSuccess()) {
302  std::ostringstream ost;
303  ost << id;
304  idname = ost.str();
305  }
306  }
307 
308  if (m_ofa.is_open()) {
309  m_ofa << ( (typ == 1) ? "RECORD" : "RETRIEVE" ) << " clid: " << id
310  << " | ";
311 
312  m_ofa << idname;
313 
314  m_ofa << " key: " << key << " alg: " << m_currAlg
315  << " store: " << store_id
316  << std:: endl;
317  }
318 
319  // store stuff for the summary
320 
321  if (!m_ofs.is_open()) return;
322 
323  if (m_nEvents < m_startEvent || !m_inExec) return;
324 
325  if (m_currAlg == "----") return;
326 
327  std::string kk;
328  if (m_useCLID) {
329  std::ostringstream ost;
330  ost << id << "/" << key;
331  kk = ost.str();
332  } else {
333  kk = idname + "/" + key;
334  }
335 
336  DataMap::iterator itr;
337  if (typ == 0) {
338  itr = m_read.find(m_currAlg);
339  if (itr != m_read.end()) {
340  itr->second.insert(kk);
341  } else {
342  m_read[m_currAlg] = std::set<std::string> ( {kk} );
343  }
344  } else {
345  itr = m_write.find(m_currAlg);
346  if (itr != m_write.end()) {
347  itr->second.insert(kk);
348  } else {
349  m_write[m_currAlg] = std::set<std::string> ( {kk} );
350  }
351  }
352 }
353 
354 /*----------------------------------------------------------------------------*/
355 
356 bool
358 
359  IAlgorithm *asdf = p_algCtxSvc->currentAlg();
360  if (!asdf || m_nEvents==0) { // to skip before first event
361  m_currAlg="----";
362  m_nCurrAlg=-1;
363  return false;
364  }
365  std::string name = asdf->name();
366 
367  if ( !m_ignoreFakeAlgs && !m_fakeCurrAlg.empty() )
369 
370  if (name!=m_currAlg){
372  int index=0;
373  for (i=m_vAlg.begin();i<m_vAlg.end();++i){
374  if (*i==name) {
376  m_currAlg=name;
377  return true;
378  }
379  index++;
380  }
381  m_vAlg.push_back(name);
383  m_currAlg=name;
384  return true;
385  }
386  return true;
387 }
388 
389 /*----------------------------------------------------------------------------*/
390 
391 void
392 SGAudSvc::getNobj(std::string name){
394  int index=0;
395  for (i=m_vObj.begin();i<m_vObj.end();++i){
396  if (*i==name) {
398  m_currObj=name;
399  return;
400  }
401  index++;
402  }
403  m_vObj.push_back(name);
405  m_currObj=name;
406 return;
407 }
408 
409 /*----------------------------------------------------------------------------*/
410 
411 void
413  int oaHash=m_nCurrObj*1000 + m_nCurrAlg;
414  if (m_timesRead.end()!=m_timesRead.find(oaHash)){
415  m_timesRead.find(oaHash)->second++;
416  }
417  else{
418  std::pair<int,int> p(oaHash,1);
419  m_timesRead.insert(p);
420  }
421  return;
422 }
423 
424 /*----------------------------------------------------------------------------*/
425 
426 void
428  int oaHash=m_nCurrObj*1000 + m_nCurrAlg;
429  if (m_timesWritten.end()!=m_timesWritten.find(oaHash)){
430  m_timesWritten.find(oaHash)->second++;
431  }
432  else{
433  std::pair<int,int> p(oaHash,1);
434  m_timesWritten.insert(p);
435  }
436  return;
437 }
438 
439 /*----------------------------------------------------------------------------*/
440 
441 void
442 SGAudSvc::setFakeCurrentAlg( const std::string& s ) { m_fakeCurrAlg=s; }
443 
444 /*----------------------------------------------------------------------------*/
445 
446 void
448 
449 /*----------------------------------------------------------------------------*/
450 
451 void
453 
454  DataMap::const_iterator itr;
455  std::set<std::string>::const_iterator it2;
456  std::vector<std::string>::const_iterator ia;
457 
458  m_ofs << "{ \"algorithms\" : [" << std::endl;
459  for (ia = m_vAlg.begin(); ia != m_vAlg.end(); ++ia) {
460  m_ofs << " {" << std::endl;
461  m_ofs << " \"name\" : \"" << *ia << "\"," << std::endl;
462  m_ofs << " \"inputs\" : [";
463 
464  itr = m_read.find(*ia);
465  if (itr != m_read.end()) {
466  for (it2 = itr->second.begin(); it2 != itr->second.end(); ++it2) {
467  if (it2 != itr->second.begin()) m_ofs << ",";
468  m_ofs << "\"" << *it2 << "\"";
469  }
470  }
471  m_ofs << "]," << std::endl;
472 
473  m_ofs << " \"outputs\" : [";
474 
475  itr = m_write.find(*ia);
476  if (itr != m_write.end()) {
477  for (it2 = itr->second.begin(); it2 != itr->second.end(); ++it2) {
478  if (it2 != itr->second.begin()) m_ofs << ",";
479  m_ofs << "\"" << *it2 << "\"";
480  }
481  }
482  m_ofs << "]," << std::endl;
483  m_ofs << " }," << std::endl;
484  }
485  m_ofs << " ]" << std::endl << "}" << std::endl;
486 
487 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
SGAudSvc::addWrite
void addWrite()
Definition: SGAudSvc.cxx:427
SGAudSvc::SGGetCurrentAlg
bool SGGetCurrentAlg()
Gets name of curently running algorithm from AlgContextSvc.
Definition: SGAudSvc.cxx:357
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
SGAudSvc.h
python.trigbs_prescaleL1.ost
ost
Definition: trigbs_prescaleL1.py:104
SGAudSvc::m_pCID
ServiceHandle< IClassIDSvc > m_pCID
Definition: SGAudSvc.h:105
SGAudSvc::m_timesWritten
std::map< int, int > m_timesWritten
map counting Writes of each object by each algorithm.
Definition: SGAudSvc.h:123
SGAudSvc::m_sumFileName
std::string m_sumFileName
Definition: SGAudSvc.h:108
SGAudSvc::monitor
void monitor()
just counts events. called at EndEvent incident
Definition: SGAudSvc.cxx:229
SGAudSvc::SGAudRECORD
void SGAudRECORD(std::string SGobject)
Definition: SGAudSvc.cxx:261
index
Definition: index.py:1
SGAudSvc::clearFakeCurrentAlg
virtual void clearFakeCurrentAlg() override
For implementing custom increased granularity auditing of for instance tools.
Definition: SGAudSvc.cxx:447
SGAudSvc::m_ofa
std::ofstream m_ofa
Definition: SGAudSvc.h:137
initialize
void initialize()
Definition: run_EoverP.cxx:894
SGAudSvc::SGAudSvc
SGAudSvc()
Default constructor:
SGAudSvc::initialize
virtual StatusCode initialize() override
Gaudi Service Implementation.
Definition: SGAudSvc.cxx:84
SGAudSvc::getNobj
void getNobj(std::string name)
Definition: SGAudSvc.cxx:392
SGAudSvc::m_useCLID
bool m_useCLID
Whether to use CLID or Data Obj Name in JSON output file.
Definition: SGAudSvc.h:114
SGAudSvc::m_nCurrObj
int m_nCurrObj
Definition: SGAudSvc.h:128
SGAudSvc::writeJSON
void writeJSON()
Definition: SGAudSvc.cxx:452
DataHandle.h
SGAudSvc::finalize
virtual StatusCode finalize() override
Definition: SGAudSvc.cxx:121
SGAudSvc::handle
virtual void handle(const Incident &incident) override
incident service handle for EndEvent.
Definition: SGAudSvc.cxx:178
SGAudSvc::m_inExec
bool m_inExec
Definition: SGAudSvc.h:138
SGAudSvc::m_write
DataMap m_write
Definition: SGAudSvc.h:135
SGAudSvc::m_ignoreFakeAlgs
bool m_ignoreFakeAlgs
Whether to ignore fake current algs.
Definition: SGAudSvc.h:111
SGAudSvc::setFakeCurrentAlg
virtual void setFakeCurrentAlg(const std::string &) override
For implementing custom increased granularity auditing of for instance tools.
Definition: SGAudSvc.cxx:442
python.changerun.kk
list kk
Definition: changerun.py:41
SGAudSvc::m_currObj
std::string m_currObj
Definition: SGAudSvc.h:125
SGAudSvc::m_timesRead
std::map< int, int > m_timesRead
map counting Reads of each object by each algorithm.
Definition: SGAudSvc.h:121
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
SGAudSvc::m_currAlg
std::string m_currAlg
Definition: SGAudSvc.h:124
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:36
SGAudSvc::m_outFileName
std::string m_outFileName
Name of the output file.
Definition: SGAudSvc.h:108
lumiFormat.i
int i
Definition: lumiFormat.py:85
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SGAudSvc::m_startEvent
int m_startEvent
Definition: SGAudSvc.h:130
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
hist_file_dump.f
f
Definition: hist_file_dump.py:135
SGAudSvc::SGAudit
virtual void SGAudit(const std::string &key, const CLID &id, const int &fnc, const int &store_id) override
Definition: SGAudSvc.cxx:282
SGAudSvc::SGAudRETRIEVE
void SGAudRETRIEVE(std::string SGobject)
Definition: SGAudSvc.cxx:237
SGAudSvc::m_vAlg
std::vector< std::string > m_vAlg
Vector of names of algorithms accessing SG.
Definition: SGAudSvc.h:119
SGAudSvc::addRead
void addRead()
Definition: SGAudSvc.cxx:412
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:220
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
SGAudSvc::~SGAudSvc
virtual ~SGAudSvc()
Destructor:
Definition: SGAudSvc.cxx:74
SGAudSvc::m_allFileName
std::string m_allFileName
Definition: SGAudSvc.h:108
SGAudSvc::m_ofs
std::ofstream m_ofs
Definition: SGAudSvc.h:137
SGAudSvc::m_msg
MsgStream m_msg
MsgStream for talking with the outside world.
Definition: SGAudSvc.h:101
SGAudSvc::p_algCtxSvc
ServiceHandle< IAlgContextSvc > p_algCtxSvc
Pointer to the AlgContextScv.
Definition: SGAudSvc.h:104
DeMoScan.index
string index
Definition: DeMoScan.py:364
a
TList * a
Definition: liststreamerinfos.cxx:10
DEBUG
#define DEBUG
Definition: page_access.h:11
extractSporadic.q
list q
Definition: extractSporadic.py:98
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
SGAudSvc::m_vObj
std::vector< std::string > m_vObj
Vector of accessed SG objects names.
Definition: SGAudSvc.h:117
SGAudSvc::m_nCurrAlg
int m_nCurrAlg
Definition: SGAudSvc.h:127
SGAudSvc::m_fakeCurrAlg
std::string m_fakeCurrAlg
Definition: SGAudSvc.h:126
StoreGateSvc.h
SGAudSvc::m_read
DataMap m_read
Definition: SGAudSvc.h:134
SGAudSvc::m_nEvents
int m_nEvents
Definition: SGAudSvc.h:129
ServiceHandle< IIncidentSvc >
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37