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