BOSS 7.1.2
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:
24DECLARE_COMPONENT(StepHandler)
25StepHandler::StepHandler(const std::string& name, ISvcLocator* pSvcLocator):
26 Algorithm(name, pSvcLocator)
27{
28 std::vector<std::string> resultVec;
29 resultVec.push_back("Error");
30 resultVec.push_back("Rejected");
31 resultVec.push_back("Accepted");
32 m_EFResult = new EFResult(resultVec);
33
34 declareProperty("EventTypes", m_tmp_evtType);
35 //evtTypes.push_back("Unknown");
36 //evtTypes.push_back("Hadron");
37 //evtTypes.push_back("Dimuon");
38 //evtTypes.push_back("DiElectron");
39 //evtTypes.push_back("DiPhoton");
40 //m_evtType = new EFResult(m_tmp_evtType);
41}
42
43// DESTRUCTOR:
45 if (m_EFResult) delete m_EFResult;
46 //if (m_evtType) delete m_evtType;
47}
48
49/////////////////////////////////////////////////////////////////
50// INITIALIZE METHOD:
52 // MSGStream object to output messages from your algorithm
53 MsgStream log( messageService(), name() );
54
55 Algorithm::initialize();
56
57 StatusCode sc = service("HltConfigSvc", m_HltConfigSvc);
58 if( sc.isFailure()) {
59 log << MSG::FATAL << name() << ": Unable to locate HltConfigSvc" << endreq;
60 return sc;
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 //CreateSubAlgorithms; StepSequencer and StepDecision
68 /*
69 Algorithm* p_seqAlg;
70 Algorithm* p_decAlg;
71
72 sc = createSubAlgorithm("StepSequencer", "StepSequencer", p_seqAlg);
73 if( sc.isFailure() ) {
74 log << MSG::FATAL << "Unable to create StepSequencer SubAlgorithm " << endreq;
75 return sc;
76 } else {
77 sequencerAlg = static_cast<StepSequencer*>(p_seqAlg);
78 log << MSG::DEBUG << "created SubAlgorithm StepSequencer/StepSequencer" << endreq;
79 }
80
81 sc = createSubAlgorithm("StepDecision", "StepDecision", p_decAlg);
82 if( sc.isFailure() ) {
83 log << MSG::FATAL << "Unable to create StepDecision SubAlgorithm " << endreq;
84 return sc;
85 } else {
86 decisionAlg = static_cast<StepDecision*>(p_decAlg);
87 log << MSG::DEBUG << "created SubAlgorithm StepDecision/StepDecision" << endreq;
88 }
89
90 const std::vector<std::pair<Signature*,Sequence*> >& m_TableVector = m_HltConfigSvc->retrieve();
91 std::vector<std::pair<Signature*,Sequence*> >::const_iterator It;
92
93 for (It = m_TableVector.begin(); It != m_TableVector.end(); ++It) {
94 sequencerAlg->initSequencer(It->second);
95 decisionAlg->initDecision(It->first);
96 m_stepIdList.push_back(It->second->getSeqID());
97 }
98 */
99 if (m_EFResult)
100 m_HltStoreSvc->put("EFResult", m_EFResult);
101 else {
102 log << MSG::ERROR << "Unvalid m_EFResult" << endreq;
103 return StatusCode::FAILURE;
104 }
105
106 m_evtType = new EFResult(m_tmp_evtType);
107 if (m_evtType)
108 m_HltStoreSvc->put("EventType", m_evtType);
109 else {
110 log << MSG::ERROR << "Unvalid m_evtType" << endreq;
111 return StatusCode::FAILURE;
112 }
113#ifdef TIME_MEASUREMENT //// Timer
114 sc = service("BesTimerSvc", m_timersvc);
115 if (sc.isFailure()) {
116 log << MSG::WARNING << name() << ": Unable to locate BesTimer Service" << endreq;
117 return StatusCode::FAILURE;
118 }
119 m_timer[0] = m_timersvc->addItem("Step Handler Time");
120 m_timer[1] = m_timersvc->addItem("1stStep exec Time");
121 m_timer[2] = m_timersvc->addItem("1stStep deci Time");
122 m_timer[3] = m_timersvc->addItem("2ndStep exec Time");
123 m_timer[4] = m_timersvc->addItem("2ndStep deci Time");
124
125 NTuplePtr nt(ntupleSvc(),"FILE444/nt");
126 if ( nt ) {
127 m_tuple = nt;
128 m_tuple->addItem("total", g_StepHandlerTime);
129 m_tuple->addItem("mdc", g_1stStepExecTime);
130 m_tuple->addItem("mdcd", g_1stStepDeciTime);
131 m_tuple->addItem("emc", g_2ndStepExecTime);
132 m_tuple->addItem("emcd", g_2ndStepDeciTime);
133 } else {
134 m_tuple = ntupleSvc()->book("FILE444/nt",CLID_ColumnWiseTuple,"StepCnt");
135 if (m_tuple) {
136 m_tuple->addItem("total", g_StepHandlerTime);
137 m_tuple->addItem("mdc", g_1stStepExecTime);
138 m_tuple->addItem("mdcd", g_1stStepDeciTime);
139 m_tuple->addItem("emc", g_2ndStepExecTime);
140 m_tuple->addItem("emcd", g_2ndStepDeciTime);
141 } else {
142 log << MSG::ERROR <<"Cannot book N-tuple:" << long(m_tuple) << endmsg;
143 return StatusCode::FAILURE;
144 }
145 }
146 round = 0;
147#endif
148
149 log << MSG::INFO << "Initialization " << name() << " completed successfully" << endreq;
150 return StatusCode::SUCCESS;
151}
152
153/////////////////////////////////////////////////////////////////
154// FINALIZE METHOD:
155
157 if (m_evtType) delete m_evtType;
158 return StatusCode::SUCCESS;
159}
160
162 MsgStream log( messageService(), name() );
163
164 Algorithm::beginRun();
165
166 Algorithm* p_seqAlg;
167 Algorithm* p_decAlg;
168
169 StatusCode sc = createSubAlgorithm("StepSequencer", "StepSequencer", p_seqAlg);
170 if( sc.isFailure() ) {
171 log << MSG::FATAL << "Unable to create StepSequencer SubAlgorithm " << endreq;
172 return sc;
173 } else {
174 sequencerAlg = static_cast<StepSequencer*>(p_seqAlg);
175 log << MSG::DEBUG << "created SubAlgorithm StepSequencer/StepSequencer" << endreq;
176 }
177
178 sc = createSubAlgorithm("StepDecision", "StepDecision", p_decAlg);
179 if( sc.isFailure() ) {
180 log << MSG::FATAL << "Unable to create StepDecision SubAlgorithm " << endreq;
181 return sc;
182 } else {
183 decisionAlg = static_cast<StepDecision*>(p_decAlg);
184 log << MSG::DEBUG << "created SubAlgorithm StepDecision/StepDecision" << endreq;
185 }
186
187 const std::vector<std::pair<Signature*,Sequence*> >& m_TableVector = m_HltConfigSvc->retrieve();
188 std::vector<std::pair<Signature*,Sequence*> >::const_iterator It;
189
190 for (It = m_TableVector.begin(); It != m_TableVector.end(); ++It) {
191 sequencerAlg->initSequencer(It->second);
192 decisionAlg->initDecision(It->first);
193 m_stepIdList.push_back(It->second->getSeqID());
194 }
195
196 log << MSG::DEBUG << "finish initializing sub-algorithms" << endreq;
197 return StatusCode::SUCCESS;
198}
199
200/////////////////////////////////////////////////////////////////
201// EXECUTE METHOD:
203 // MSGStream object to output messages from your algorithm
204 MsgStream log( messageService(), name() );
205 log << MSG::DEBUG << "*** Executing StepHandler *** " << FSMState() << endreq;
206
207 //if(EFBesTimer::pBesTimer&&EFBesTimer::pType==2)EFBesTimer::pBesTimer->start();
208
209#ifdef TIME_MEASUREMENT //// Timer
210 m_timer[0]->start();
211 m_timer[1]->start();
212 m_timer[2]->start();
213 m_timer[3]->start();
214 m_timer[4]->start();
215#endif
216
217 bool StepSeq = true;
218 int StepDec = -999;
219 log << MSG::DEBUG << "point: " << m_EFResult << " " << m_evtType << " " << sequencerAlg << endreq;
220 m_EFResult->reset();
221 m_evtType->reset();
222 if(!sequencerAlg) return StatusCode::FAILURE;
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 << "==============" <<m_evtType->getAnswerIndex()<< endreq;
295
296 if (result == "Accepted") {
297 std::string evtMark = answer.substr(separator+1);
298 log << MSG::DEBUG << " Event is Accepted as " << evtMark << endreq;
299 m_evtType->setAnswer(evtMark);
300 }
301 else if (result == "Rejected") {
302 // only beam-gas rejected defaultly
303 // other type event not decided in Event Filter
304 // by Fu Chengdong
305 std::string evtMark = "Beamgas";
306 m_evtType->setAnswer(evtMark);
307 log << MSG::DEBUG << " Event is Rejected" << endreq;
308 }
309 else {
310 log << MSG::WARNING << " Unknown Behaviour, force to Reject" << endreq;
311 result = "Rejected";
312 }
313 log << MSG::DEBUG << "==============" <<m_evtType->getAnswerIndex()<< endreq;
314 m_EFResult->setAnswer(result);
315
316#ifdef TIME_MEASUREMENT //// Timer
317 m_timer[0]->stop();
318 g_StepHandlerTime=m_timer[0]->elapsed();
319 if(m_tuple){
320 StatusCode status = m_tuple->write();
321 if (!status.isSuccess()) {
322 log << MSG::ERROR << "Can't fill ntuple!" << endreq;
323 }
324 }
325#endif
326 return StatusCode::SUCCESS;
327}
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()
StatusCode beginRun()
StatusCode initSequencer(HltProcessor::Sequence *)
bool execSequencer(const std::string &seqID)