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