00001
00002
00003
00004
00005 #include "GaudiKernel/MsgStream.h"
00006 #include "GaudiKernel/Property.h"
00007 #include "GaudiKernel/ISvcLocator.h"
00008
00009 #include "HltSteering/StepHandler.h"
00010
00011
00012 #include "HltSteerData/Sequence.h"
00013 #include "HltSteerData/Signature.h"
00014
00015
00016
00017
00018
00019 using HltProcessor::Sequence;
00020 using HltProcessor::Signature;
00021 using namespace HltProcessor;
00023
00024 StepHandler::StepHandler(const std::string& name, ISvcLocator* pSvcLocator):
00025 Algorithm(name, pSvcLocator)
00026 {
00027 std::vector<std::string> resultVec;
00028 resultVec.push_back("Error");
00029 resultVec.push_back("Rejected");
00030 resultVec.push_back("Accepted");
00031 m_EFResult = new EFResult(resultVec);
00032
00033 declareProperty("EventTypes", m_tmp_evtType);
00034
00035
00036
00037
00038
00039
00040 }
00041
00042
00043 StepHandler::~StepHandler() {
00044 if (m_EFResult) delete m_EFResult;
00045
00046 }
00047
00049
00050 StatusCode StepHandler::initialize() {
00051
00052 MsgStream log( messageService(), name() );
00053
00054 Algorithm::initialize();
00055
00056 StatusCode sc = service("HltConfigSvc", m_HltConfigSvc);
00057 if( sc.isFailure()) {
00058 log << MSG::FATAL << name() << ": Unable to locate HltConfigSvc" << endreq;
00059 return sc;
00060 }
00061 sc = service("HltStoreSvc", m_HltStoreSvc);
00062 if( sc.isFailure()) {
00063 log << MSG::FATAL << name() << ": Unable to locate HltStoreSvc" << endreq;
00064 return sc;
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 if (m_EFResult)
00099 m_HltStoreSvc->put("EFResult", m_EFResult);
00100 else {
00101 log << MSG::ERROR << "Unvalid m_EFResult" << endreq;
00102 return StatusCode::FAILURE;
00103 }
00104
00105 m_evtType = new EFResult(m_tmp_evtType);
00106 if (m_evtType)
00107 m_HltStoreSvc->put("EventType", m_evtType);
00108 else {
00109 log << MSG::ERROR << "Unvalid m_evtType" << endreq;
00110 return StatusCode::FAILURE;
00111 }
00112 #ifdef TIME_MEASUREMENT
00113 sc = service("BesTimerSvc", m_timersvc);
00114 if (sc.isFailure()) {
00115 log << MSG::WARNING << name() << ": Unable to locate BesTimer Service" << endreq;
00116 return StatusCode::FAILURE;
00117 }
00118 m_timer[0] = m_timersvc->addItem("Step Handler Time");
00119 m_timer[1] = m_timersvc->addItem("1stStep exec Time");
00120 m_timer[2] = m_timersvc->addItem("1stStep deci Time");
00121 m_timer[3] = m_timersvc->addItem("2ndStep exec Time");
00122 m_timer[4] = m_timersvc->addItem("2ndStep deci Time");
00123
00124 NTuplePtr nt(ntupleSvc(),"FILE444/nt");
00125 if ( nt ) {
00126 m_tuple = nt;
00127 m_tuple->addItem("total", g_StepHandlerTime);
00128 m_tuple->addItem("mdc", g_1stStepExecTime);
00129 m_tuple->addItem("mdcd", g_1stStepDeciTime);
00130 m_tuple->addItem("emc", g_2ndStepExecTime);
00131 m_tuple->addItem("emcd", g_2ndStepDeciTime);
00132 } else {
00133 m_tuple = ntupleSvc()->book("FILE444/nt",CLID_ColumnWiseTuple,"StepCnt");
00134 if (m_tuple) {
00135 m_tuple->addItem("total", g_StepHandlerTime);
00136 m_tuple->addItem("mdc", g_1stStepExecTime);
00137 m_tuple->addItem("mdcd", g_1stStepDeciTime);
00138 m_tuple->addItem("emc", g_2ndStepExecTime);
00139 m_tuple->addItem("emcd", g_2ndStepDeciTime);
00140 } else {
00141 log << MSG::ERROR <<"Cannot book N-tuple:" << long(m_tuple) << endmsg;
00142 return StatusCode::FAILURE;
00143 }
00144 }
00145 round = 0;
00146 #endif
00147
00148 log << MSG::INFO << "Initialization " << name() << " completed successfully" << endreq;
00149 return StatusCode::SUCCESS;
00150 }
00151
00153
00154
00155 StatusCode StepHandler::finalize() {
00156 if (m_evtType) delete m_evtType;
00157 return StatusCode::SUCCESS;
00158 }
00159
00160 StatusCode StepHandler::beginRun(){
00161 MsgStream log( messageService(), name() );
00162
00163 Algorithm::beginRun();
00164
00165 Algorithm* p_seqAlg;
00166 Algorithm* p_decAlg;
00167
00168 StatusCode sc = createSubAlgorithm("StepSequencer", "StepSequencer", p_seqAlg);
00169 if( sc.isFailure() ) {
00170 log << MSG::FATAL << "Unable to create StepSequencer SubAlgorithm " << endreq;
00171 return sc;
00172 } else {
00173 sequencerAlg = static_cast<StepSequencer*>(p_seqAlg);
00174 log << MSG::DEBUG << "created SubAlgorithm StepSequencer/StepSequencer" << endreq;
00175 }
00176
00177 sc = createSubAlgorithm("StepDecision", "StepDecision", p_decAlg);
00178 if( sc.isFailure() ) {
00179 log << MSG::FATAL << "Unable to create StepDecision SubAlgorithm " << endreq;
00180 return sc;
00181 } else {
00182 decisionAlg = static_cast<StepDecision*>(p_decAlg);
00183 log << MSG::DEBUG << "created SubAlgorithm StepDecision/StepDecision" << endreq;
00184 }
00185
00186 const std::vector<std::pair<Signature*,Sequence*> >& m_TableVector = m_HltConfigSvc->retrieve();
00187 std::vector<std::pair<Signature*,Sequence*> >::const_iterator It;
00188
00189 for (It = m_TableVector.begin(); It != m_TableVector.end(); ++It) {
00190 sequencerAlg->initSequencer(It->second);
00191 decisionAlg->initDecision(It->first);
00192 m_stepIdList.push_back(It->second->getSeqID());
00193 }
00194
00195 log << MSG::DEBUG << "finish initializing sub-algorithms" << endreq;
00196 return StatusCode::SUCCESS;
00197 }
00198
00200
00201 StatusCode StepHandler::execute() {
00202
00203 MsgStream log( messageService(), name() );
00204 log << MSG::DEBUG << "*** Executing StepHandler *** " << FSMState() << endreq;
00205
00206
00207
00208 #ifdef TIME_MEASUREMENT
00209 m_timer[0]->start();
00210 m_timer[1]->start();
00211 m_timer[2]->start();
00212 m_timer[3]->start();
00213 m_timer[4]->start();
00214 #endif
00215
00216 bool StepSeq = true;
00217 int StepDec = -999;
00218 log << MSG::DEBUG << "point: " << m_EFResult << " " << m_evtType << " " << sequencerAlg << endreq;
00219 m_EFResult->reset();
00220 m_evtType->reset();
00221 if(!sequencerAlg) return StatusCode::FAILURE;
00222 sequencerAlg->reset();
00223
00224 std::string answer, result;
00225 std::string::size_type separator;
00226
00227 std::vector<std::string>::const_iterator id = m_stepIdList.begin();
00228
00229 while (id != m_stepIdList.end()) {
00230 log << MSG::DEBUG << "In step " << (*id) << endreq;
00231
00232 StepSeq = sequencerAlg->execSequencer(*id);
00233 if (!StepSeq) {
00234 log << MSG::DEBUG << " Step Sequencer failed" << endreq;
00235 id++;
00236 continue;
00237 }
00238
00239 log << MSG::DEBUG << "+++++++++++++++++++++++++++++" << endreq;
00240
00241 StepDec = decisionAlg->execDecision(*id);
00242 std::string midAnswer = decisionAlg->getContinue();
00243 if (midAnswer!="") m_evtType->setMidAnswer(midAnswer);
00244
00245
00246 if (StepDec>0) m_EFResult->push_back(StepDec);
00247 else {
00248
00249 m_EFResult->push_back(0);
00250 id++;
00251 continue;
00252 }
00253 answer = decisionAlg->getBehaviour();
00254 log << MSG::DEBUG << " Matched Criteria, Behaviour = " << answer << endreq;
00255
00256 separator = answer.find('_');
00257 if (separator == std::string::npos) {
00258 log << MSG::ERROR << " Unknown decision result: "<< answer << endreq;
00259 return StatusCode::FAILURE;
00260 }
00261 std::string perform = answer.substr(0, separator);
00262
00263
00264
00265
00266 if (perform == "reject") {
00267 result = "Rejected";
00268 break;
00269 }
00270 else if (perform == "accept") {
00271 result = "Accepted";
00272 break;
00273 }
00274 else if (perform == "jump") {
00275 std::string nextId = answer.substr(separator+1);
00276 do {
00277 ++id;
00278 } while ((id != m_stepIdList.end()) && ((*id) != nextId));
00279 if (id == m_stepIdList.end()) {
00280 log << MSG::ERROR << " Unknown jump target: "<< nextId << endreq;
00281 return StatusCode::FAILURE;
00282 }
00283 }
00284 else {
00285 log << MSG::ERROR << " Unknown decision result: "<< answer << endreq;
00286 return StatusCode::FAILURE;
00287 }
00288
00289
00290
00291 }
00292
00293 log << MSG::DEBUG << "==============" <<m_evtType->getAnswerIndex()<< endreq;
00294
00295 if (result == "Accepted") {
00296 std::string evtMark = answer.substr(separator+1);
00297 log << MSG::DEBUG << " Event is Accepted as " << evtMark << endreq;
00298 m_evtType->setAnswer(evtMark);
00299 }
00300 else if (result == "Rejected") {
00301
00302
00303
00304 std::string evtMark = "Beamgas";
00305 m_evtType->setAnswer(evtMark);
00306 log << MSG::DEBUG << " Event is Rejected" << endreq;
00307 }
00308 else {
00309 log << MSG::WARNING << " Unknown Behaviour, force to Reject" << endreq;
00310 result = "Rejected";
00311 }
00312 log << MSG::DEBUG << "==============" <<m_evtType->getAnswerIndex()<< endreq;
00313 m_EFResult->setAnswer(result);
00314
00315 #ifdef TIME_MEASUREMENT
00316 m_timer[0]->stop();
00317 g_StepHandlerTime=m_timer[0]->elapsed();
00318 if(m_tuple){
00319 StatusCode status = m_tuple->write();
00320 if (!status.isSuccess()) {
00321 log << MSG::ERROR << "Can't fill ntuple!" << endreq;
00322 }
00323 }
00324 #endif
00325 return StatusCode::SUCCESS;
00326 }