BOSS 7.0.8
BESIII Offline Software System
Loading...
Searching...
No Matches
McEventSelector.cxx
Go to the documentation of this file.
1//====================================================================
2// EventSelector.cpp
3//--------------------------------------------------------------------
4//
5// Package : McEventSelector
6//
7// Description: The EventSelector component is able
8// to produce a list of event references given a set of "selection
9// criteria".
10//
11//
12//
13//====================================================================
14
15// Include files
16#include <vector>
17#include "GaudiKernel/ISvcLocator.h"
18#include "GaudiKernel/GenericAddress.h"
19#include "GaudiKernel/StatusCode.h"
20#include "GaudiKernel/SvcFactory.h"
21#include "GaudiKernel/MsgStream.h"
22#include "GaudiKernel/SmartIF.h"
23#include "GaudiKernel/Property.h"
24#include "GaudiKernel/Bootstrap.h"
32//#include "McEventSelector/McIterator.h"
33//#include "EventInfo/EventInfo.h"
34
35using namespace EventModel;
36extern const CLID& CLID_Event;
37
38// Instantiation of a static factory class used by clients to create
39// instances of this service
40//static const SvcFactory<McEventSelector> s_factory;
41//const ISvcFactory& McEventSelectorFactory = s_factory;
42
43
44class McContext : public IEvtSelector::Context {
45private:
46 const McEventSelector* m_pSelector;
47 std::string m_criteria;
48 std::vector<int> m_runList;
49 std::vector<int> m_evtNoList;
50
51 RealizationSvc* m_RealizationSvc;
52 IProperty* m_appMgrProperty;
53
54 int m_initRunNo;
55 unsigned int m_initEventNo;
56 unsigned int m_eventsP;
57
58 int m_runNo;
59 unsigned int m_eventNo;
60 bool m_firstEvent;
61 int m_lumiBlockNo;
62
63
64public:
65 /// Standard constructor
66 McContext(const McEventSelector* pSelector);
67 McContext(const McEventSelector* pSelector,
68 const int &initRunNo, const unsigned int &initEventNo,
69 const unsigned int &evPR);
70
71 /// Standard destructor
72 virtual ~McContext();
73
74 virtual void* identifier() const {
75 return (void*)m_pSelector;
76 }
77 void setCriteria(const std::string& crit) {
78 m_criteria = crit;
79 }
80
81 void rewind() {
82
83 // Question: should this rewind to before the first event, or to
84 // it? ie, will next() be called right after rewind()?
85 // if so, then should set m_firstEvent to true;
86
87 m_eventNo = m_initEventNo;
88 m_runNo = m_initRunNo;
89 }
90
91 void next() {
92 if (m_firstEvent) {
93 m_firstEvent = false;
94
95 m_eventNo = m_initEventNo;
96 m_runNo = m_initRunNo;
97
98 return;
99 }
100 m_eventNo++;
101 if(m_RealizationSvc->UseDBFlag() == false) return;
102 if(m_eventNo>=(m_initEventNo + m_evtNoList[m_lumiBlockNo])){
103 m_lumiBlockNo++;
104 m_runNo = -std::abs(m_runList[m_lumiBlockNo]);
105 m_RealizationSvc->setRunId(m_runNo);
106 m_RealizationSvc->setRunEvtNum(m_evtNoList[m_lumiBlockNo]);
107 m_eventNo= m_initEventNo;
108 }
109 }
110
111 void previous() {
112 m_eventNo--;
113 }
114
115 int runNumber() const { return m_runNo; }
116 unsigned int eventNumber() const { return m_eventNo; }
117
118 void setRunNumber( int runNo ) { m_runNo = runNo; }
119 void setEventNumber( unsigned int eventNo ) { m_eventNo = eventNo ; }
120
121};
122
124 m_pSelector(pSelector), m_firstEvent(true), m_lumiBlockNo(0), m_initEventNo(0) {
125 // Obtain the IProperty of the ApplicationMgr
126 //SmartIF<IProperty> prpMgr(IProperty::interfaceID(), Gaudi::svcLocator());
127 SmartIF<IProperty> prpMgr(Gaudi::svcLocator());
128 if ( ! prpMgr.isValid() ) {
129 std::cout << "IProperty interface not found in ApplicationMgr." << std::endl;
130 }
131 else {
132 m_appMgrProperty = prpMgr;
133 }
134 //get IRealizationSvc
135 ISvcLocator* svcLocator = Gaudi::svcLocator();
136 IRealizationSvc *tmpReal;
137 StatusCode status = svcLocator->service("RealizationSvc", tmpReal);
138 if (!status.isSuccess())
139 {
140 std::cout << " Could not initialize Realization Service" << std::endl;
141 } else {
142 m_RealizationSvc=dynamic_cast<RealizationSvc*>(tmpReal);
143 }
144 //get jobSvc
145 IDataInfoSvc* tmpInfoSvc;
146 DataInfoSvc* jobInfoSvc;
147 status = svcLocator->service("DataInfoSvc",tmpInfoSvc);
148 if (status.isSuccess()) {
149 std::cout << "get the DataInfoSvc" << std::endl;
150 jobInfoSvc=dynamic_cast<DataInfoSvc *>(tmpInfoSvc);
151 }else {
152 std::cout << "could not get the DataInfoSvc." << std::endl;
153 }
154
155 //set initial event ID
156 m_initEventNo = m_RealizationSvc->getInitEvtID();
157
158 std::string sMax;
159 status = m_appMgrProperty->getProperty("EvtMax", sMax);
160 int EvtMax = std::atoi(sMax.c_str());
161 //for realization
162 if(m_RealizationSvc->UseDBFlag() == true) {
163 std::vector<int> totEvtNo;
164 totEvtNo.clear();
165 std::vector<float> lumi;
166 std::vector<int> tmp_runList = m_RealizationSvc->getRunList();
167 float totLumi = 0;
168 for(unsigned int i = 0; i < tmp_runList.size(); i++)
169 {
170 float lumi_value = m_RealizationSvc->getLuminosity(tmp_runList[i]);
171 lumi.push_back(lumi_value);
172 totLumi += lumi_value;
173 }
174
175 m_runList.clear();
176
177 int totSimEvt = 0;
178 int evtSubNo = 0;
179 if(totLumi != 0) {
180 for(unsigned int i = 0; i < lumi.size(); i++) {
181 //if(i == 0) m_evtNoList.push_back(0);
182 double ratio = lumi[i]/totLumi*EvtMax;
183 evtSubNo = int (ratio);
184 if((ratio-evtSubNo) >= 0.5) evtSubNo = evtSubNo + 1;
185 totSimEvt += evtSubNo;
186 if(evtSubNo == 0) {
187 std::cout << "The run " <<tmp_runList[i]<<" is not simulated, due to the luminosity is too small!" << std::endl;
188 }
189 else {
190 //m_evtNoList.push_back(totSimEvt);
191 m_evtNoList.push_back(evtSubNo);
192 m_runList.push_back(tmp_runList[i]);
193 totEvtNo.push_back(tmp_runList[i]);
194 totEvtNo.push_back(evtSubNo);
195 }
196 std::cout <<"Total "<< evtSubNo <<" events need to be simulated in run " <<tmp_runList[i]<< std::endl;
197 }
198
199 if((EvtMax - totSimEvt) != 0) {
200 unsigned int effRunSize = m_evtNoList.size();
201 if(effRunSize > 0) {
202 m_evtNoList[effRunSize - 1] = m_evtNoList[effRunSize - 1] + (EvtMax - totSimEvt);
203 effRunSize = totEvtNo.size();
204 totEvtNo[effRunSize - 1] = totEvtNo[effRunSize - 1] + (EvtMax - totSimEvt);
205 std::cout <<"Additional "<< EvtMax - totSimEvt <<" events need to be simulated in run " << m_runList[m_runList.size() - 1]<< std::endl;
206 }
207 else {
208 bool found = false;
209 for(unsigned int i = 0; i < lumi.size(); i++) {
210 if(lumi[i] > 0) {
211 m_evtNoList.push_back(EvtMax - totSimEvt);
212 m_runList.push_back(tmp_runList[i]);
213 totEvtNo.push_back(tmp_runList[i]);
214 totEvtNo.push_back(EvtMax - totSimEvt);
215 found = true;
216 std::cout <<"The max event number maybe too small, all "<< evtSubNo <<" events need to be simulated in run " <<tmp_runList[i]<< std::endl;
217 break;
218 }
219 }
220 if(!found) {
221 std::cerr << "ERORR: " << "Total luminosity is ZERO, please check your run list. " << std::endl;
222 std::exit(0);
223 }
224 }
225 }
226 }
227 else {
228 std::cerr << "ERORR: " << "Total luminosity is ZERO!!! Please check your run list. " << std::endl;
229 std::exit(0);
230 }
231 m_RealizationSvc->setRunId(-std::abs(m_runList[0]));
232 m_RealizationSvc->setRunEvtNum(m_evtNoList[0]);
233 m_initRunNo = -std::abs(m_runList[0]);
234 jobInfoSvc->setTotEvtNo(totEvtNo);
235 }
236 else {
237 m_initRunNo = -std::abs((m_RealizationSvc->getRunList())[0]);
238 }
239}
240
242 const int &initRunNo,
243 const unsigned int &initEventNo,
244 const unsigned int &evPR):
245 m_pSelector(pSelector),
246 m_initRunNo(initRunNo), m_initEventNo(initEventNo), m_eventsP(evPR),
247 m_firstEvent(true), m_lumiBlockNo(0) {
248}
249
250
252}
253
254
255// IInterface::queryInterface
256StatusCode McEventSelector::queryInterface(const InterfaceID& riid,
257 void** ppvIf) {
258 if ( riid == IEvtSelector::interfaceID() ) {
259 *ppvIf = (IEvtSelector*)this;
260 addRef();
261 return SUCCESS;
262 }
263 return Service::queryInterface( riid, ppvIf );
264}
265
266
267
268McEventSelector::McEventSelector( const std::string& name, ISvcLocator* svcloc ) :
269 Service( name, svcloc)
270{
271
272 //declareProperty( "RunNumber", m_runNo = 5040 );
273 //declareProperty( "EventsPerRun", m_eventsPerRun = 100000000 );
274 //declareProperty( "FirstEvent", m_firstEventNo = 0 );
275 m_runNo.verifier().setLower( 0 );
276 m_eventsPerRun.verifier().setLower( 0 );
277 m_firstEventNo.verifier().setLower( 0 );
278
279}
280
282 // FIXME: who deletes the Context?
283}
284
285StatusCode
286McEventSelector::createContext(Context*& refpCtxt) const
287{
288 McContext* ctx = new McContext(this);//,
289 //(unsigned int) m_runNo.value( ),
290 //(unsigned int) m_firstEventNo.value( ),
291 //(unsigned int) m_eventsPerRun.value( )
292 //);
293 refpCtxt = ctx;
294
295 return StatusCode::SUCCESS;
296}
297
299 MsgStream log(messageService(), name());
300 log << MSG::INFO << " Enter McEventSelector Initialization " << endreq;
301 StatusCode sc = Service::initialize();
302 if( sc.isSuccess() ) {
303 } else {
304 log << MSG::ERROR << "Unable to initialize service " << endreq;
305 return sc;
306 }
307
308 log << MSG::INFO << " McEventSelector Initialized Properly ... " << endreq;
309 return sc;
310}
311
313 MsgStream log(messageService(), name());
314 log << MSG::INFO << "finalize" << endreq;
315
316 return StatusCode::SUCCESS;
317}
318
319// IEvtSelector::next
320StatusCode
321McEventSelector::next(Context& ctxt) const {
322 MsgStream log(messageService(), name());
323 log << MSG::DEBUG << "............. Next Event ............." << endreq;
324
325 McContext* ct = dynamic_cast<McContext*>(&ctxt);
326
327 if (ct != 0 ) {
328 ct->next();
329 return StatusCode::SUCCESS;
330 } else {
331 MsgStream log(messageService(), name());
332 log << "Could not dcast to McContext" << endreq;
333 return StatusCode::FAILURE;
334 }
335}
336StatusCode
337McEventSelector::next(Context& ctxt,int jump) const
338{
339 if ( jump > 0 ) {
340 for ( int i = 0; i < jump; ++i ) {
341 StatusCode status = next(ctxt);
342 if ( !status.isSuccess() ) {
343 return status;
344 }
345 }
346 return StatusCode::SUCCESS;
347 }
348 return StatusCode::FAILURE;
349}
350
351
352// IEvtSelector::previous
353StatusCode
354McEventSelector::previous(Context& ctxt) const {
355 McContext* ct = dynamic_cast<McContext*>(&ctxt);
356
357 if (ct != 0 ) {
358 ct->previous();
359 return StatusCode::SUCCESS;
360 } else {
361 MsgStream log(messageService(), name());
362 log << "Could not dcast to McContext" << endreq;
363 return StatusCode::FAILURE;
364 }
365
366}
367
368StatusCode
369McEventSelector::previous(Context& ctxt, int jump) const
370{
371 if ( jump > 0 ) {
372 for ( int i = 0; i < jump; ++i ) {
373 StatusCode status = previous(ctxt);
374 if ( !status.isSuccess() ) {
375 return status;
376 }
377 }
378 return StatusCode::SUCCESS;
379 }
380 return StatusCode::FAILURE;
381}
382
383StatusCode
384McEventSelector::last(Context& /*ctxt*/) const {
385 MsgStream log(messageService(), name());
386 log << MSG::ERROR
387 << "............. Last Event Not Implemented ............."
388 << endreq;
389 return StatusCode::FAILURE;
390}
391
392
393StatusCode
394McEventSelector::rewind(Context& ctxt) const
395{
396
397 McContext* ct = dynamic_cast<McContext*>(&ctxt);
398
399 if (ct != 0 ) {
400 ct->rewind();
401 return StatusCode::SUCCESS;
402 } else {
403 MsgStream log(messageService(), name());
404 log << "Could not dcast to McContext" << endreq;
405 return StatusCode::FAILURE;
406 }
407
408}
409
410
411StatusCode
412McEventSelector::createAddress(const Context& refCtxt,
413 IOpaqueAddress*& addr) const {
414
415 const McContext* ctx = dynamic_cast<const McContext*>( &refCtxt );
416
417 if (ctx != 0) {
418 McAddress* mcAddr = new McAddress(CLID_Event, "Event", "");
419 mcAddr-> setRunEvt(ctx->runNumber(), ctx->eventNumber());
420 addr = mcAddr;
421 } else {
422 MsgStream log(messageService(), name());
423 log << MSG::ERROR << "casting to a McContext" << endreq;
424 return StatusCode::FAILURE;
425 }
426
427 return StatusCode::SUCCESS;
428
429}
430
431StatusCode
432McEventSelector::releaseContext(Context*& /*refCtxt*/) const {
433
434 MsgStream log(messageService(), name());
435 log << MSG::ERROR
436 << "............. releaseContext Not Implemented ............."
437 << endreq;
438
439 return StatusCode::FAILURE;
440
441}
442
443StatusCode
444McEventSelector::resetCriteria(const std::string&, Context& ) const {
445
446 MsgStream log(messageService(), name());
447 log << MSG::ERROR
448 << "............. resetCriteria Not Implemented ............."
449 << endreq;
450
451 return StatusCode::FAILURE;
452
453}
454
int runNo
Definition: DQA_TO_DB.cxx:12
const CLID & CLID_Event
Definition: EventModel.cxx:193
int eventNo
const CLID & CLID_Event
Definition: EventModel.cxx:193
void setTotEvtNo(std::vector< int > i)
Definition: DataInfoSvc.h:29
void setCriteria(const std::string &crit)
virtual ~McContext()
Standard destructor.
virtual void * identifier() const
unsigned int eventNumber() const
void setRunNumber(int runNo)
void setEventNumber(unsigned int eventNo)
int runNumber() const
McContext(const McEventSelector *pSelector)
Standard constructor.
virtual StatusCode initialize()
virtual StatusCode previous(Context &refCtxt) const
virtual StatusCode resetCriteria(const std::string &cr, Context &c) const
virtual StatusCode rewind(Context &refCtxt) const
virtual StatusCode releaseContext(Context *&refCtxt) const
McEventSelector(const std::string &name, ISvcLocator *svcloc)
virtual StatusCode finalize()
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvInterface)
virtual StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&) const
virtual StatusCode createContext(Context *&refpCtxt) const
virtual StatusCode last(Context &refContext) const
virtual StatusCode next(Context &refCtxt) const
std::vector< int > getRunList()
void setRunEvtNum(int i)
void setRunId(int i)
float getLuminosity()