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