00001
00002 #include "GaudiKernel/Auditor.h"
00003 #include "GaudiKernel/MsgStream.h"
00004 #include "GaudiKernel/Chrono.h"
00005 #include "GaudiKernel/IAlgorithm.h"
00006 #include "GaudiKernel/DeclareFactoryEntries.h"
00007
00008 #include "ntupleWriterSvc/INTupleWriterSvc.h"
00009
00010 #include <map>
00011 #include <algorithm>
00012
00021 class EventAuditor : virtual public Auditor
00022 {
00023 public:
00024
00025 EventAuditor( const std::string &, ISvcLocator * ) ;
00026 virtual StatusCode initialize() ;
00027 virtual StatusCode finalize() ;
00028 virtual ~EventAuditor() ;
00029
00030
00031
00032 virtual StatusCode beforeExecute( IAlgorithm * ) ;
00033 virtual StatusCode afterExecute( IAlgorithm * ) ;
00034
00035
00036
00037 private :
00038
00039 INTupleWriterSvc* m_rootTupleSvc;
00040 StringProperty m_root_tree;
00041 bool m_save_tuple;
00042
00043
00045
00046 std::vector<std::string> m_algoNames ;
00047 int m_nAlgs;
00048
00049
00050 MsgStream * m_log ;
00051 IChronoStatSvc * m_chronoSvc ;
00052
00053
00054 std::map< IAlgorithm *, bool > m_algoStatus ;
00055
00056 std::vector<float> m_timeVals;
00057 } ;
00058
00059
00060
00061
00062
00063 DECLARE_AUDITOR_FACTORY(EventAuditor) ;
00064
00078 EventAuditor::EventAuditor( const std::string & name, ISvcLocator * svcLocator )
00079 : Auditor(name,svcLocator)
00080 {
00081 std::vector<std::string> algoNamesVec ;
00082 algoNamesVec.push_back("Event");
00083 algoNamesVec.push_back("Generation");
00084 algoNamesVec.push_back("Reconstruction");
00085 algoNamesVec.push_back("Tkr");
00086 algoNamesVec.push_back("Cal2");
00087
00088 declareProperty("algoNames",m_algoNames=algoNamesVec);
00089 declareProperty("tree_name", m_root_tree="MeritTuple");
00090 }
00091
00092 StatusCode EventAuditor::initialize()
00093 {
00094 Auditor::initialize() ;
00095 StatusCode sc = StatusCode::SUCCESS;
00096
00097 setProperties();
00098 std::vector<std::string>& algoNames = m_algoNames;
00099
00100
00101
00102 std::vector< std::string >::const_iterator algoName ;
00103 algoName = std::find(algoNames.begin(), algoNames.end(), "Event");
00104 if(algoName==algoNames.end()) {
00105 algoNames.insert(algoNames.begin(),"Event");
00106 }
00107
00108 m_nAlgs = algoNames.size();
00109 m_timeVals.assign(m_nAlgs, -1.0);
00110
00111
00112 IMessageSvc * messageSvc ;
00113 service("MessageSvc",messageSvc,true) ;
00114 m_log = new MsgStream(messageSvc,name()) ;
00115
00116
00117 if (service("ChronoStatSvc",m_chronoSvc,true).isFailure())
00118 {
00119 (*m_log)<<MSG::ERROR<<"Could not find TkrReconSvc"<<endreq ;
00120 return StatusCode::FAILURE ;
00121 }
00122
00123
00124 if( (service("RootTupleSvc", m_rootTupleSvc, true) ). isFailure() ) {
00125 (*m_log) << MSG::ERROR << " RootTupleSvc is not available" << endreq;
00126 m_rootTupleSvc=0;
00127 return StatusCode::FAILURE;
00128 }
00129
00130 if( m_rootTupleSvc==0 ) return sc;
00131
00132 std::string tname = m_root_tree.value();
00133
00134 int i;
00135 for(i=0;i<m_nAlgs;++i) {
00136 m_rootTupleSvc->addItem(tname, "Aud"+m_algoNames[i] , &m_timeVals[i]);
00137 }
00138
00139 (*m_log)<<MSG::DEBUG<<"initialize() "<<endreq ;
00140 return StatusCode::SUCCESS ;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 StatusCode EventAuditor::beforeExecute( IAlgorithm * algo )
00161 {
00162
00163 std::string thisName = algo->name();
00164 if(thisName=="Event") {
00165
00166 m_timeVals.assign(m_nAlgs, -1.0);
00167 }
00168
00169 std::map< IAlgorithm *, bool >::iterator itr ;
00170 itr = m_algoStatus.find(algo) ;
00171 if ( itr == m_algoStatus.end() )
00172 {
00173 const std::vector< std::string > & algoNames = m_algoNames ;
00174 std::vector< std::string >::const_iterator algoName ;
00175
00176 algoName = std::find(algoNames.begin(), algoNames.end(), thisName);
00177 if(algoName==algoNames.end()) {
00178 m_algoStatus[algo] = false ;
00179 return StatusCode::SUCCESS ;
00180 } else {
00181 m_algoStatus[algo] = true ;
00182 }
00183 }
00184
00185 if(m_algoStatus[algo]) {
00186 m_chronoSvc->chronoStart("EA_"+thisName) ;
00187 (*m_log) << MSG::DEBUG << "start " << thisName << std::endl;
00188 }
00189 return StatusCode::SUCCESS ;
00190 }
00191
00192 StatusCode EventAuditor::afterExecute( IAlgorithm * algo )
00193 {
00194
00195 if (m_algoStatus[algo]==false)
00196 { return StatusCode::SUCCESS ; }
00197
00198 std::string thisName = algo->name();
00199
00200
00201 m_chronoSvc->chronoStop("EA_"+thisName) ;
00202
00203
00204
00205 IChronoStatSvc::ChronoTime delta
00206 = m_chronoSvc->chronoDelta("EA_"+thisName,IChronoStatSvc::USER) ;
00207
00208 float fDelta = static_cast<float>(delta)*0.000001;
00209 (*m_log) << MSG::DEBUG << thisName <<" user time: " << fDelta << " sec" << endreq ;
00210
00211 const std::vector< std::string > & algoNames = m_algoNames ;
00212 int i;
00213 for(i=0;i<m_nAlgs;++i) {
00214 if(thisName==algoNames[i]) {
00215 m_timeVals[i] = fDelta;
00216 break;
00217 }
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 return StatusCode::SUCCESS ;
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 StatusCode EventAuditor::finalize()
00253 {
00254 (*m_log) << MSG::DEBUG <<"finalize() "<< endreq ;
00255
00256 return StatusCode::SUCCESS;
00257
00258 }
00259
00260 EventAuditor::~EventAuditor()
00261 {}
00262
00263
00264