/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Generator/McEventSelector/McEventSelector-01-00-13/src/McEventSelector.cxx

Go to the documentation of this file.
00001 //====================================================================
00002 //      EventSelector.cpp
00003 //--------------------------------------------------------------------
00004 //
00005 //      Package    : McEventSelector
00006 //
00007 //  Description: The EventSelector component is able
00008 //              to produce a list of event references given a set of "selection
00009 //               criteria".
00010 //
00011 //
00012 //
00013 //====================================================================
00014 
00015 // Include files
00016 #include <vector>
00017 #include "GaudiKernel/ISvcLocator.h"
00018 #include "GaudiKernel/GenericAddress.h"
00019 #include "GaudiKernel/StatusCode.h"
00020 #include "GaudiKernel/SvcFactory.h"
00021 #include "GaudiKernel/MsgStream.h"
00022 #include "GaudiKernel/SmartIF.h"
00023 #include "GaudiKernel/Property.h"
00024 #include "GaudiKernel/Bootstrap.h"
00025 #include "McEventSelector/McEventSelector.h"
00026 #include "McEventSelector/McAddress.h"
00027 #include "EventModel/EventModel.h"
00028 #include "RealizationSvc/RealizationSvc.h"
00029 #include "RealizationSvc/IRealizationSvc.h"
00030 #include "DataInfoSvc/DataInfoSvc.h"
00031 #include "DataInfoSvc/IDataInfoSvc.h"
00032 //#include "McEventSelector/McIterator.h"
00033 //#include "EventInfo/EventInfo.h" 
00034 
00035 using namespace EventModel;
00036 extern const CLID& CLID_Event;
00037 
00038 // Instantiation of a static factory class used by clients to create
00039 // instances of this service
00040 //static const SvcFactory<McEventSelector> s_factory;
00041 //const ISvcFactory& McEventSelectorFactory = s_factory;
00042 
00043 
00044 class McContext : public IEvtSelector::Context {
00045 private:
00046   const McEventSelector*        m_pSelector;
00047   std::string                   m_criteria;
00048   std::vector<int> m_runList;
00049   std::vector<int> m_evtNoList;
00050 
00051   RealizationSvc* m_RealizationSvc;
00052   IProperty*        m_appMgrProperty;
00053 
00054   int m_initRunNo;
00055   unsigned int m_initEventNo;
00056   unsigned int m_eventsP;
00057 
00058   int m_runNo;
00059   unsigned int m_eventNo;
00060   bool m_firstEvent;
00061   int m_lumiBlockNo;
00062   
00063 
00064 public:
00066   McContext(const McEventSelector* pSelector);
00067   McContext(const McEventSelector* pSelector, 
00068             const int &initRunNo, const unsigned int &initEventNo, 
00069             const unsigned int &evPR);
00070 
00072   virtual ~McContext();
00073 
00074   virtual void* identifier() const {
00075     return (void*)m_pSelector;
00076   }
00077   void setCriteria(const std::string& crit) {
00078     m_criteria = crit;
00079   }
00080 
00081   void rewind() {
00082 
00083     // Question: should this rewind to before the first event, or to
00084     // it? ie, will next() be called right after rewind()?
00085     // if so, then should set m_firstEvent to true;
00086 
00087       m_eventNo = m_initEventNo;
00088       m_runNo = m_initRunNo;
00089   }    
00090   
00091   void next() {
00092     if (m_firstEvent) {
00093       m_firstEvent = false;
00094 
00095       m_eventNo = m_initEventNo;
00096       m_runNo = m_initRunNo;
00097 
00098       return;
00099     }
00100     m_eventNo++;
00101     if(m_RealizationSvc->UseDBFlag() == false) return;
00102     if(m_eventNo>=(m_initEventNo + m_evtNoList[m_lumiBlockNo])){
00103       m_lumiBlockNo++;
00104       m_runNo = -std::abs(m_runList[m_lumiBlockNo]);
00105       m_RealizationSvc->setRunId(m_runNo);
00106       m_RealizationSvc->setRunEvtNum(m_evtNoList[m_lumiBlockNo]);
00107       m_eventNo= m_initEventNo;
00108     }
00109   }
00110 
00111   void previous() {
00112     m_eventNo--;
00113   }
00114 
00115   int runNumber() const { return m_runNo; }
00116   unsigned int eventNumber() const { return m_eventNo; }
00117 
00118   void setRunNumber( int runNo ) { m_runNo = runNo; }
00119   void setEventNumber( unsigned int eventNo ) { m_eventNo = eventNo ; }
00120 
00121 };
00122 
00123 McContext::McContext(const McEventSelector* pSelector):
00124   m_pSelector(pSelector), m_firstEvent(true), m_lumiBlockNo(0), m_initEventNo(0) {
00125   // Obtain the IProperty of the ApplicationMgr
00126   //SmartIF<IProperty> prpMgr(IProperty::interfaceID(), Gaudi::svcLocator());
00127   SmartIF<IProperty> prpMgr(Gaudi::svcLocator());
00128     if ( ! prpMgr.isValid() )   {
00129     std::cout << "IProperty interface not found in ApplicationMgr." << std::endl;
00130   }
00131   else {
00132     m_appMgrProperty = prpMgr;
00133   }
00134   //get IRealizationSvc
00135   ISvcLocator* svcLocator = Gaudi::svcLocator();
00136   IRealizationSvc *tmpReal;
00137   StatusCode status = svcLocator->service("RealizationSvc", tmpReal);
00138   if (!status.isSuccess())
00139   {
00140      std::cout << " Could not initialize Realization Service" << std::endl;
00141   } else {
00142     m_RealizationSvc=dynamic_cast<RealizationSvc*>(tmpReal);
00143   }
00144   //get jobSvc
00145   IDataInfoSvc* tmpInfoSvc;
00146   DataInfoSvc* jobInfoSvc;
00147   status = svcLocator->service("DataInfoSvc",tmpInfoSvc);
00148   if (status.isSuccess()) {
00149     std::cout << "get the DataInfoSvc" << std::endl;
00150     jobInfoSvc=dynamic_cast<DataInfoSvc *>(tmpInfoSvc);
00151   }else {
00152     std::cout << "could not get the DataInfoSvc." << std::endl;
00153   }
00154 
00155   //set initial event ID
00156   m_initEventNo = m_RealizationSvc->getInitEvtID();
00157 
00158   std::string sMax;
00159   status = m_appMgrProperty->getProperty("EvtMax", sMax);
00160   int EvtMax = std::atoi(sMax.c_str());
00161   //for realization
00162   if(m_RealizationSvc->UseDBFlag() == true) {
00163     std::vector<int> totEvtNo;
00164     totEvtNo.clear();
00165     std::vector<float> lumi;
00166     std::vector<int> tmp_runList = m_RealizationSvc->getRunList();
00167     float totLumi = 0;
00168     for(unsigned int i = 0; i < tmp_runList.size(); i++)
00169     {
00170       float lumi_value = m_RealizationSvc->getLuminosity(tmp_runList[i]);
00171       lumi.push_back(lumi_value); 
00172       totLumi += lumi_value;      
00173     }                             
00174                                   
00175     m_runList.clear();
00176 
00177     int totSimEvt = 0;
00178     int evtSubNo = 0;
00179     if(totLumi != 0) {
00180       for(unsigned int i = 0; i < lumi.size(); i++) {
00181         //if(i == 0) m_evtNoList.push_back(0);
00182         double ratio = lumi[i]/totLumi*EvtMax;
00183         evtSubNo = int (ratio);
00184         if((ratio-evtSubNo) >= 0.5) evtSubNo = evtSubNo + 1;
00185         totSimEvt += evtSubNo;
00186         if(evtSubNo == 0) {
00187           std::cout << "The run " <<tmp_runList[i]<<" is not simulated, due to the luminosity is too small!" << std::endl;
00188         }
00189         else {
00190           //m_evtNoList.push_back(totSimEvt);
00191           m_evtNoList.push_back(evtSubNo);
00192           m_runList.push_back(tmp_runList[i]);
00193           totEvtNo.push_back(tmp_runList[i]);
00194           totEvtNo.push_back(evtSubNo);
00195         }
00196         std::cout <<"Total "<< evtSubNo <<" events need to be simulated in run " <<tmp_runList[i]<< std::endl;
00197       }
00198 
00199       if((EvtMax - totSimEvt) != 0) {
00200         unsigned int effRunSize = m_evtNoList.size();
00201         if(effRunSize > 0) {
00202           m_evtNoList[effRunSize - 1] = m_evtNoList[effRunSize - 1] + (EvtMax - totSimEvt);
00203           effRunSize = totEvtNo.size();
00204           totEvtNo[effRunSize - 1] = totEvtNo[effRunSize - 1] + (EvtMax - totSimEvt);
00205           std::cout <<"Additional "<< EvtMax - totSimEvt  <<" events need to be simulated in run " << m_runList[m_runList.size() - 1]<< std::endl;
00206         }
00207         else {
00208           bool found = false;
00209           for(unsigned int i = 0; i < lumi.size(); i++) {
00210             if(lumi[i] > 0) {
00211               m_evtNoList.push_back(EvtMax - totSimEvt);
00212               m_runList.push_back(tmp_runList[i]);
00213               totEvtNo.push_back(tmp_runList[i]);
00214               totEvtNo.push_back(EvtMax - totSimEvt);
00215               found = true;
00216               std::cout <<"The max event number maybe too small, all "<< evtSubNo <<" events need to be simulated in run " <<tmp_runList[i]<< std::endl;
00217               break;
00218             }
00219           }
00220           if(!found) {
00221             std::cerr << "ERORR: " << "Total luminosity is ZERO, please check your run list. " << std::endl;
00222             std::exit(0);
00223           }
00224         }
00225       }
00226     }
00227     else {
00228       std::cerr << "ERORR: " << "Total luminosity is ZERO!!! Please check your run list. " << std::endl;
00229       std::exit(0);
00230     }
00231     m_RealizationSvc->setRunId(-std::abs(m_runList[0]));
00232     m_RealizationSvc->setRunEvtNum(m_evtNoList[0]);
00233     m_initRunNo = -std::abs(m_runList[0]);
00234     jobInfoSvc->setTotEvtNo(totEvtNo);
00235   }
00236   else {
00237     m_initRunNo = -std::abs((m_RealizationSvc->getRunList())[0]);
00238   }
00239 }
00240 
00241 McContext::McContext(const McEventSelector* pSelector, 
00242                      const int &initRunNo, 
00243                      const unsigned int &initEventNo, 
00244                      const unsigned int &evPR):
00245   m_pSelector(pSelector),
00246   m_initRunNo(initRunNo), m_initEventNo(initEventNo), m_eventsP(evPR),
00247   m_firstEvent(true), m_lumiBlockNo(0) {
00248 }
00249   
00250 
00251 McContext::~McContext() {
00252 }
00253 
00254 
00255 // IInterface::queryInterface
00256 StatusCode McEventSelector::queryInterface(const InterfaceID& riid, 
00257                                            void** ppvIf) {
00258   if ( riid == IEvtSelector::interfaceID() )  {
00259     *ppvIf = (IEvtSelector*)this;
00260     addRef();
00261     return SUCCESS;
00262   }
00263   return Service::queryInterface( riid, ppvIf );
00264 }
00265 
00266 
00267 
00268 McEventSelector::McEventSelector( const std::string& name, ISvcLocator* svcloc ) :
00269   Service( name, svcloc)
00270 {
00271 
00272   //declareProperty( "RunNumber",           m_runNo = 5040 );
00273   //declareProperty( "EventsPerRun",        m_eventsPerRun = 100000000 );
00274   //declareProperty( "FirstEvent",          m_firstEventNo = 0 );
00275   m_runNo.verifier().setLower( 0 );
00276   m_eventsPerRun.verifier().setLower( 0 );
00277   m_firstEventNo.verifier().setLower( 0 );   
00278  
00279 }
00280 
00281 McEventSelector::~McEventSelector() {
00282   // FIXME: who deletes the Context?
00283 }
00284 
00285 StatusCode
00286 McEventSelector::createContext(Context*& refpCtxt) const 
00287 {
00288   McContext* ctx =  new McContext(this);//,
00289                                   //(unsigned int) m_runNo.value( ),
00290                                   //(unsigned int) m_firstEventNo.value( ),
00291                                   //(unsigned int) m_eventsPerRun.value( )
00292                                   //);
00293   refpCtxt = ctx;
00294 
00295   return StatusCode::SUCCESS;
00296 }
00297 
00298 StatusCode McEventSelector::initialize()     {
00299   MsgStream log(messageService(), name());
00300   log << MSG::INFO << " Enter McEventSelector Initialization " << endreq;
00301   StatusCode sc = Service::initialize();
00302   if( sc.isSuccess() ) {
00303   } else {
00304     log << MSG::ERROR << "Unable to initialize service " << endreq;
00305     return sc;
00306   }
00307 
00308   log << MSG::INFO  << " McEventSelector Initialized Properly ... " << endreq;
00309   return sc;
00310 }
00311 
00312 StatusCode McEventSelector::finalize()     {
00313   MsgStream log(messageService(), name());
00314   log << MSG::INFO << "finalize" << endreq;
00315 
00316   return StatusCode::SUCCESS;
00317 }
00318 
00319 // IEvtSelector::next
00320 StatusCode
00321 McEventSelector::next(Context& ctxt) const {
00322   MsgStream log(messageService(), name());
00323   log << MSG::DEBUG << "............. Next Event ............." << endreq;
00324 
00325   McContext* ct = dynamic_cast<McContext*>(&ctxt);
00326 
00327   if (ct != 0 ) {
00328     ct->next();
00329     return StatusCode::SUCCESS;
00330   } else {
00331     MsgStream log(messageService(), name());
00332     log << "Could not dcast to McContext" << endreq;
00333     return StatusCode::FAILURE;
00334   }
00335 }
00336 StatusCode 
00337 McEventSelector::next(Context& ctxt,int jump) const 
00338 {
00339   if ( jump > 0 ) {
00340     for ( int i = 0; i < jump; ++i ) {
00341       StatusCode status = next(ctxt);
00342       if ( !status.isSuccess() ) {
00343         return status;
00344       }
00345     }
00346     return StatusCode::SUCCESS;
00347   }
00348   return StatusCode::FAILURE;
00349 }
00350 
00351 
00352 // IEvtSelector::previous
00353 StatusCode
00354 McEventSelector::previous(Context& ctxt) const {
00355   McContext* ct = dynamic_cast<McContext*>(&ctxt);
00356 
00357   if (ct != 0 ) {
00358     ct->previous();
00359     return StatusCode::SUCCESS;
00360   } else {
00361     MsgStream log(messageService(), name());
00362     log << "Could not dcast to McContext" << endreq;
00363     return StatusCode::FAILURE;
00364   }
00365 
00366 }
00367 
00368 StatusCode 
00369 McEventSelector::previous(Context& ctxt, int jump) const 
00370 {
00371   if ( jump > 0 ) {
00372     for ( int i = 0; i < jump; ++i ) {
00373       StatusCode status = previous(ctxt);
00374       if ( !status.isSuccess() ) {
00375         return status;
00376       }
00377     }
00378     return StatusCode::SUCCESS;
00379   }
00380   return StatusCode::FAILURE;
00381 }
00382 
00383 StatusCode
00384 McEventSelector::last(Context& /*ctxt*/) const {
00385   MsgStream log(messageService(), name());
00386   log << MSG::ERROR 
00387       << "............. Last Event Not Implemented ............." 
00388       << endreq;
00389   return StatusCode::FAILURE;
00390 }
00391 
00392 
00393 StatusCode 
00394 McEventSelector::rewind(Context& ctxt) const 
00395 {
00396 
00397   McContext* ct = dynamic_cast<McContext*>(&ctxt);
00398 
00399   if (ct != 0 ) {
00400     ct->rewind();
00401     return StatusCode::SUCCESS;
00402   } else {
00403     MsgStream log(messageService(), name());
00404     log << "Could not dcast to McContext" << endreq;
00405     return StatusCode::FAILURE;
00406   }
00407 
00408 }
00409 
00410 
00411 StatusCode
00412 McEventSelector::createAddress(const Context& refCtxt, 
00413                                IOpaqueAddress*& addr) const {
00414 
00415   const McContext* ctx = dynamic_cast<const McContext*>( &refCtxt );
00416 
00417   if (ctx != 0) {
00418     McAddress* mcAddr = new McAddress(CLID_Event, "Event", "");
00419     mcAddr-> setRunEvt(ctx->runNumber(), ctx->eventNumber());
00420     addr = mcAddr;
00421   } else {
00422     MsgStream log(messageService(), name());
00423     log << MSG::ERROR << "casting to a McContext" << endreq;
00424     return StatusCode::FAILURE;
00425   }
00426 
00427   return StatusCode::SUCCESS;
00428 
00429 }
00430 
00431 StatusCode
00432 McEventSelector::releaseContext(Context*& /*refCtxt*/) const {
00433 
00434   MsgStream log(messageService(), name());
00435   log << MSG::ERROR 
00436       << "............. releaseContext Not Implemented ............." 
00437       << endreq;
00438 
00439   return StatusCode::FAILURE;
00440 
00441 }
00442 
00443 StatusCode
00444 McEventSelector::resetCriteria(const std::string&, Context& ) const {
00445 
00446   MsgStream log(messageService(), name());
00447   log << MSG::ERROR 
00448       << "............. resetCriteria Not Implemented ............." 
00449       << endreq;
00450 
00451   return StatusCode::FAILURE;
00452   
00453 }
00454 

Generated on Tue Nov 29 23:12:41 2016 for BOSS_7.0.2 by  doxygen 1.4.7