CGEM BOSS 6.6.5.h
BESIII Offline Software System
Loading...
Searching...
No Matches
StepHandler.cxx
Go to the documentation of this file.
1/********************************************************************
2NAME: StepHandler.cxx
3********************************************************************/
4// INCLUDE GAUDI HEADER FILES:
5#include "GaudiKernel/MsgStream.h"
6#include "GaudiKernel/Property.h"
7#include "GaudiKernel/ISvcLocator.h"
8
10//#include "HltSteering/StepSequencer.h"
11//#include "HltSteering/StepDecision.h"
14
15//#ifdef TIME_MEASUREMENT //// Timer
16//#include "HltDataTypes/EFBesTimer.h"
17//#endif
18
21using namespace HltProcessor;
22////////////////////////////////////////////////////////////////
23// CONSTRUCTOR:
24StepHandler::StepHandler(const std::string& name, ISvcLocator* pSvcLocator):
25 Algorithm(name, pSvcLocator)
26{
27 std::vector<std::string> resultVec;
28 resultVec.push_back("Error");
29 resultVec.push_back("Rejected");
30 resultVec.push_back("Accepted");
31 m_EFResult = new EFResult(resultVec);
32
33 declareProperty("EventTypes", m_tmp_evtType);
34 //evtTypes.push_back("Unknown");
35 //evtTypes.push_back("Hadron");
36 //evtTypes.push_back("Dimuon");
37 //evtTypes.push_back("DiElectron");
38 //evtTypes.push_back("DiPhoton");
39 //m_evtType = new EFResult(m_tmp_evtType);
40}
41
42// DESTRUCTOR:
44 if (m_EFResult) delete m_EFResult;
45 //if (m_evtType) delete m_evtType;
46}
47
48/////////////////////////////////////////////////////////////////
49// INITIALIZE METHOD:
51 // MSGStream object to output messages from your algorithm
52 MsgStream log( messageService(), name() );
53
54 Algorithm::initialize();
55
56 StatusCode sc = service("HltConfigSvc", m_HltConfigSvc);
57 if( sc.isFailure()) {
58 log << MSG::FATAL << name() << ": Unable to locate HltConfigSvc" << endreq;
59 return sc;
60 }
61 sc = service("HltStoreSvc", m_HltStoreSvc);
62 if( sc.isFailure()) {
63 log << MSG::FATAL << name() << ": Unable to locate HltStoreSvc" << endreq;
64 return sc;
65 }
66 //CreateSubAlgorithms; StepSequencer and StepDecision
67 /*
68 Algorithm* p_seqAlg;
69 Algorithm* p_decAlg;
70
71 sc = createSubAlgorithm("StepSequencer", "StepSequencer", p_seqAlg);
72 if( sc.isFailure() ) {
73 log << MSG::FATAL << "Unable to create StepSequencer SubAlgorithm " << endreq;
74 return sc;
75 } else {
76 sequencerAlg = static_cast<StepSequencer*>(p_seqAlg);
77 log << MSG::DEBUG << "created SubAlgorithm StepSequencer/StepSequencer" << endreq;
78 }
79
80 sc = createSubAlgorithm("StepDecision", "StepDecision", p_decAlg);
81 if( sc.isFailure() ) {
82 log << MSG::FATAL << "Unable to create StepDecision SubAlgorithm " << endreq;
83 return sc;
84 } else {
85 decisionAlg = static_cast<StepDecision*>(p_decAlg);
86 log << MSG::DEBUG << "created SubAlgorithm StepDecision/StepDecision" << endreq;
87 }
88
89 const std::vector<std::pair<Signature*,Sequence*> >& m_TableVector = m_HltConfigSvc->retrieve();
90 std::vector<std::pair<Signature*,Sequence*> >::const_iterator It;
91
92 for (It = m_TableVector.begin(); It != m_TableVector.end(); ++It) {
93 sequencerAlg->initSequencer(It->second);
94 decisionAlg->initDecision(It->first);
95 m_stepIdList.push_back(It->second->getSeqID());
96 }
97 */
98 if (m_EFResult)
99 m_HltStoreSvc->put("EFResult", m_EFResult);
100 else {
101 log << MSG::ERROR << "Unvalid m_EFResult" << endreq;
102 return StatusCode::FAILURE;
103 }
104
105 m_evtType = new EFResult(m_tmp_evtType);
106 if (m_evtType)
107 m_HltStoreSvc->put("EventType", m_evtType);
108 else {
109 log << MSG::ERROR << "Unvalid m_evtType" << endreq;
110 return StatusCode::FAILURE;
111 }
112#ifdef TIME_MEASUREMENT //// Timer
113 sc = service("BesTimerSvc", m_timersvc);
114 if (sc.isFailure()) {
115 log << MSG::WARNING << name() << ": Unable to locate BesTimer Service" << endreq;
116 return StatusCode::FAILURE;
117 }
118 m_timer[0] = m_timersvc->addItem("Step Handler Time");
119 m_timer[1] = m_timersvc->addItem("1stStep exec Time");
120 m_timer[2] = m_timersvc->addItem("1stStep deci Time");
121 m_timer[3] = m_timersvc->addItem("2ndStep exec Time");
122 m_timer[4] = m_timersvc->addItem("2ndStep deci Time");
123
124 NTuplePtr nt(ntupleSvc(),"FILE444/nt");
125 if ( nt ) {
126 m_tuple = nt;
127 m_tuple->addItem("total", g_StepHandlerTime);
128 m_tuple->addItem("mdc", g_1stStepExecTime);
129 m_tuple->addItem("mdcd", g_1stStepDeciTime);
130 m_tuple->addItem("emc", g_2ndStepExecTime);
131 m_tuple->addItem("emcd", g_2ndStepDeciTime);
132 } else {
133 m_tuple = ntupleSvc()->book("FILE444/nt",CLID_ColumnWiseTuple,"StepCnt");
134 if (m_tuple) {
135 m_tuple->addItem("total", g_StepHandlerTime);
136 m_tuple->addItem("mdc", g_1stStepExecTime);
137 m_tuple->addItem("mdcd", g_1stStepDeciTime);
138 m_tuple->addItem("emc", g_2ndStepExecTime);
139 m_tuple->addItem("emcd", g_2ndStepDeciTime);
140 } else {
141 log << MSG::ERROR <<"Cannot book N-tuple:" << long(m_tuple) << endmsg;
142 return StatusCode::FAILURE;
143 }
144 }
145 round = 0;
146#endif
147
148 log << MSG::INFO << "Initialization " << name() << " completed successfully" << endreq;
149 return StatusCode::SUCCESS;
150}
151
152/////////////////////////////////////////////////////////////////
153// FINALIZE METHOD:
154
156 if (m_evtType) delete m_evtType;
157 return StatusCode::SUCCESS;
158}
159
161 MsgStream log( messageService(), name() );
162
163 Algorithm::beginRun();
164
165 Algorithm* p_seqAlg;
166 Algorithm* p_decAlg;
167
168 StatusCode sc = createSubAlgorithm("StepSequencer", "StepSequencer", p_seqAlg);
169 if( sc.isFailure() ) {
170 log << MSG::FATAL << "Unable to create StepSequencer SubAlgorithm " << endreq;
171 return sc;
172 } else {
173 sequencerAlg = static_cast<StepSequencer*>(p_seqAlg);
174 log << MSG::DEBUG << "created SubAlgorithm StepSequencer/StepSequencer" << endreq;
175 }
176
177 sc = createSubAlgorithm("StepDecision", "StepDecision", p_decAlg);
178 if( sc.isFailure() ) {
179 log << MSG::FATAL << "Unable to create StepDecision SubAlgorithm " << endreq;
180 return sc;
181 } else {
182 decisionAlg = static_cast<StepDecision*>(p_decAlg);
183 log << MSG::DEBUG << "created SubAlgorithm StepDecision/StepDecision" << endreq;
184 }
185
186 const std::vector<std::pair<Signature*,Sequence*> >& m_TableVector = m_HltConfigSvc->retrieve();
187 std::vector<std::pair<Signature*,Sequence*> >::const_iterator It;
188
189 for (It = m_TableVector.begin(); It != m_TableVector.end(); ++It) {
190 sequencerAlg->initSequencer(It->second);
191 decisionAlg->initDecision(It->first);
192 m_stepIdList.push_back(It->second->getSeqID());
193 }
194
195 log << MSG::DEBUG << "finish initializing sub-algorithms" << endreq;
196 return StatusCode::SUCCESS;
197}
198
199/////////////////////////////////////////////////////////////////
200// EXECUTE METHOD:
202 // MSGStream object to output messages from your algorithm
203 MsgStream log( messageService(), name() );
204 log << MSG::DEBUG << "*** Executing StepHandler *** " << FSMState() << endreq;
205
206 //if(EFBesTimer::pBesTimer&&EFBesTimer::pType==2)EFBesTimer::pBesTimer->start();
207
208#ifdef TIME_MEASUREMENT //// Timer
209 m_timer[0]->start();
210 m_timer[1]->start();
211 m_timer[2]->start();
212 m_timer[3]->start();
213 m_timer[4]->start();
214#endif
215
216 bool StepSeq = true;
217 int StepDec = -999;
218 log << MSG::DEBUG << "point: " << m_EFResult << " " << m_evtType << " " << sequencerAlg << endreq;
219 m_EFResult->reset();
220 m_evtType->reset();
221 if(!sequencerAlg) return StatusCode::FAILURE;
222 sequencerAlg->reset();
223
224 std::string answer, result;
225 std::string::size_type separator;
226
227 std::vector<std::string>::const_iterator id = m_stepIdList.begin();
228
229 while (id != m_stepIdList.end()) {
230 log << MSG::DEBUG << "In step " << (*id) << endreq;
231
232 StepSeq = sequencerAlg->execSequencer(*id);
233 if (!StepSeq) {
234 log << MSG::DEBUG << " Step Sequencer failed" << endreq;
235 id++;
236 continue;
237 }
238
239 log << MSG::DEBUG << "+++++++++++++++++++++++++++++" << endreq;
240
241 StepDec = decisionAlg->execDecision(*id);
242 std::string midAnswer = decisionAlg->getContinue();
243 if (midAnswer!="") m_evtType->setMidAnswer(midAnswer);
244 //cout <<midAnswer<< endl;
245
246 if (StepDec>0) m_EFResult->push_back(StepDec);
247 else {
248 //log << MSG::DEBUG << " Execute next step" << endreq;
249 m_EFResult->push_back(0);
250 id++;
251 continue;
252 }
253 answer = decisionAlg->getBehaviour();
254 log << MSG::DEBUG << " Matched Criteria, Behaviour = " << answer << endreq;
255
256 separator = answer.find('_');
257 if (separator == std::string::npos) {
258 log << MSG::ERROR << " Unknown decision result: "<< answer << endreq;
259 return StatusCode::FAILURE;
260 }
261 std::string perform = answer.substr(0, separator);
262
263 //std::string midAnswer = decisionAlg->getContinue();
264 //if(midAnswer!="") m_evtType->setMidAnswer(midAnswer);
265 //cout <<answer<<" "<<midAnswer<< endl;
266 if (perform == "reject") {
267 result = "Rejected";
268 break;
269 }
270 else if (perform == "accept") {
271 result = "Accepted";
272 break;
273 }
274 else if (perform == "jump") {
275 std::string nextId = answer.substr(separator+1);
276 do {
277 ++id;
278 } while ((id != m_stepIdList.end()) && ((*id) != nextId));
279 if (id == m_stepIdList.end()) {
280 log << MSG::ERROR << " Unknown jump target: "<< nextId << endreq;
281 return StatusCode::FAILURE;
282 }
283 }
284 else {
285 log << MSG::ERROR << " Unknown decision result: "<< answer << endreq;
286 return StatusCode::FAILURE;
287 }
288 //std::string evtMark = tmp.substr(tmp.find('_')+1);
289 //if(evtMark!="") m_evtType->setMidAnswer(evtMark);
290 //cout <<answer<<endl;
291 }
292
293 log << MSG::DEBUG << "==============" <<m_evtType->getAnswerIndex()<< endreq;
294
295 if (result == "Accepted") {
296 std::string evtMark = answer.substr(separator+1);
297 log << MSG::DEBUG << " Event is Accepted as " << evtMark << endreq;
298 m_evtType->setAnswer(evtMark);
299 }
300 else if (result == "Rejected") {
301 // only beam-gas rejected defaultly
302 // other type event not decided in Event Filter
303 // by Fu Chengdong
304 std::string evtMark = "Beamgas";
305 m_evtType->setAnswer(evtMark);
306 log << MSG::DEBUG << " Event is Rejected" << endreq;
307 }
308 else {
309 log << MSG::WARNING << " Unknown Behaviour, force to Reject" << endreq;
310 result = "Rejected";
311 }
312 log << MSG::DEBUG << "==============" <<m_evtType->getAnswerIndex()<< endreq;
313 m_EFResult->setAnswer(result);
314
315#ifdef TIME_MEASUREMENT //// Timer
316 m_timer[0]->stop();
317 g_StepHandlerTime=m_timer[0]->elapsed();
318 if(m_tuple){
319 StatusCode status = m_tuple->write();
320 if (!status.isSuccess()) {
321 log << MSG::ERROR << "Can't fill ntuple!" << endreq;
322 }
323 }
324#endif
325 return StatusCode::SUCCESS;
326}
INTupleSvc * ntupleSvc()
void setAnswer(std::string &answer)
Definition EFResult.cxx:115
const int getAnswerIndex() const
Definition EFResult.cxx:168
void setMidAnswer(std::string &answer)
Definition EFResult.cxx:133
void reset()
Definition EFResult.cxx:153
bool push_back(uint32_t val)
Definition EFResult.cxx:61
const std::vector< std::pair< Signature *, Sequence * > > & retrieve()
bool put(const std::string &name, const T &value)
Definition HltStoreSvc.h:60
int execDecision(const std::string &sigID)
StatusCode initDecision(HltProcessor::Signature *)
const std::string & getBehaviour() const
const std::string & getContinue() const
StatusCode execute()
StatusCode initialize()
StatusCode finalize()
StepHandler(const std::string &name, ISvcLocator *pSvcLocator)
StatusCode beginRun()
StatusCode initSequencer(HltProcessor::Sequence *)
bool execSequencer(const std::string &seqID)