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