| # Gaudi Framework, version v22r4 |

Home |  | Generated: Fri Sep 2 2011

# EventCollectionSelector.cpp

Go to the documentation of this file.

    00001 //$Id: EventCollectionSelector.cpp,v 1.16 2007/01/09 16:54:47 hmd Exp $
    00002
    00003 // Include files
    00004 #include "GaudiKernel/SmartIF.h"
    00005 #include "GaudiKernel/SvcFactory.h"
    00006 #include "GaudiKernel/ObjectFactory.h"
    00007 #include "GaudiKernel/GenericAddress.h"
    00008 #include "GaudiKernel/MsgStream.h"
    00009 #include "GaudiKernel/IRegistry.h"
    00010 #include "GaudiKernel/INTupleSvc.h"
    00011 #include "GaudiKernel/ISvcLocator.h"
    00012 #include "GaudiKernel/IDataSourceMgr.h"
    00013 #include "GaudiKernel/IAddressCreator.h"
    00014 #include "GaudiKernel/ISelectStatement.h"
    00015 #include "GaudiKernel/NTuple.h"
    00016 #include "GaudiKernel/EventSelectorDataStream.h"
    00017
    00018 #include "EventCollectionSelector.h"
    00019
    00020 #include <memory>
    00021
    00022 DECLARE_SERVICE_FACTORY(EventCollectionSelector)
    00023
    00024
    00029 class EventCollectionContext : public IEvtSelector::Context {
    00030 public:
    00031   typedef std::list<std::string> ListName;
    00032 private:
    00033   GenericAddress*                m_pAddressBuffer;
    00034   const EventCollectionSelector* m_pSelector;
    00035   ListName                      m_files;
    00036   std::string                   m_criteria;
    00037   ListName::const_iterator      m_fileIterator;
    00038   std::string                   m_currentInput;
    00039
    00040 public:
    00042   EventCollectionContext(const EventCollectionSelector* pSelector);
    00044   virtual ~EventCollectionContext();
    00045   const std::string& currentInput() const {
    00046     return m_currentInput;
    00047   }
    00048   void setCurrentInput(const std::string& v) {
    00049     m_currentInput = v;
    00050   }
    00051   ListName& files() {
    00052     return m_files;
    00053   }
    00054   virtual void* identifier() const {
    00055     return (void*)m_pSelector;
    00056   }
    00057   void setCriteria(const std::string& crit) {
    00058     m_criteria = crit;
    00059   }
    00060   ListName::const_iterator fileIterator()        {
    00061     return m_fileIterator;
    00062   }
    00063   void setFileIterator(ListName::const_iterator new_iter)
    00064   {  m_fileIterator = new_iter;   }
    00065 };
    00066
    00067 EventCollectionContext::~EventCollectionContext() {
    00068   m_pAddressBuffer->release();
    00069 }
    00070
    00071 EventCollectionContext::EventCollectionContext(const EventCollectionSelector* pSelector)
    00072   : m_pSelector(pSelector)
    00073 {
    00074   m_pAddressBuffer = new GenericAddress();
    00075   m_pAddressBuffer->addRef();
    00076 }
    00077
    00078 // Standard constructor
    00079 EventCollectionSelector::EventCollectionSelector(const std::string& name, ISvcLocator* svcloc )
    00080   : base_class(name, svcloc)
    00081 {
    00082   declareProperty("CnvService",    m_tupleSvcName = "EvtTupleSvc");
    00083   declareProperty("Authentication",m_authentication= "");
    00084   declareProperty("Container",     m_cntName  = "B2PiPi");
    00085   declareProperty("Item",          m_itemName = "Address");
    00086   declareProperty("Criteria",      m_criteria = "");
    00087   declareProperty("DB",            m_database = "");
    00088   declareProperty("DbType",        m_dbType   = "");
    00089   declareProperty("DbService",     m_dbSvc    = "");
    00090   declareProperty("Function",      m_statement= "NTuple::Selector");
    00091 }
    00092
    00093 EventCollectionSelector::~EventCollectionSelector()   {
    00094 }
    00095
    00096 // IService implementation: Db event selector override
    00097 StatusCode EventCollectionSelector::initialize()    {
    00098   // Initialize base class
    00099   StatusCode status = Service::initialize();
    00100   MsgStream log(msgSvc(), name());
    00101   if ( !status.isSuccess() )    {
    00102     log << MSG::ERROR << "Error initializing base class Service!" << endmsg;
    00103     return status;
    00104   }
    00105   m_pAddrCreator = serviceLocator()->service("EventPersistencySvc");
    00106   if(!m_pAddrCreator.isValid()) {
    00107     log << MSG::ERROR << "Unable to locate IAddressCreator interface of " << "EventPersistencySvc" << endmsg;
    00108     return status;
    00109   }
    00110   m_tupleSvc = serviceLocator()->service(m_tupleSvcName);
    00111   if( !m_tupleSvc.isValid() )   {
    00112     log << MSG::ERROR << "Unable to locate INTupleSvc interface of " << m_tupleSvcName << endmsg;
    00113     return status;
    00114   }
    00115   return status;
    00116 }
    00117
    00118 // Connect collection to selector
    00119 StatusCode
    00120 EventCollectionSelector::connectDataSource(const std::string& db, const std::string& /* typ */ )  const {
    00121   StatusCode status = StatusCode::FAILURE;
    00122   SmartIF<IDataSourceMgr> svc(m_tupleSvc);
    00123   if ( svc.isValid( ) && db.length() > 0 )   {
    00124     std::string ident = name() + ' ';
    00125     ident += "DATAFILE='" + m_database.substr(5,m_database.length()) + "' ";
    00126     if ( !m_dbSvc.empty() )
    00127       ident += "SVC='" + m_dbSvc + "' ";
    00128     else
    00129       ident += "TYP='" + m_dbType + "' ";
    00130     ident += "OPT='READ' ";
    00131     if ( m_authentication.length() > 0 )    {
    00132       ident += "AUTH='" + m_authentication + "' ";
    00133     }
    00134     status = svc->connect(ident);
    00135   }
    00136   return status;
    00137 }
    00138
    00140 StatusCode
    00141 EventCollectionSelector::connectTuple(const std::string& nam, const std::string& itName, NTuple::Tuple*& tup, NTuple::Item<IOpaqueAddress*>*& item)  const {
    00142   std::string top = "/NTUPLES/" + name() + '/' + nam;
    00143   StatusCode status = m_tupleSvc->retrieveObject(top, (DataObject*&)tup);
    00144   if ( status.isSuccess() )    {
    00145     item = new NTuple::Item<IOpaqueAddress*>();
    00146     status = tup->item(itName, *item);
    00147     if ( status.isSuccess() )   {
    00148       return status;
    00149     }
    00150     else    {
    00151       MsgStream log(msgSvc(), name());
    00152       log << MSG::ERROR << "Item " << itName << " is not part of the collection:" << top << endmsg;
    00153     }
    00154     delete item;
    00155     item = 0;
    00156   }
    00157   else  {
    00158     MsgStream err(msgSvc(), name());
    00159     err << MSG::ERROR << "Cannot connect to collection:" << top << endmsg;
    00160   }
    00161   tup = 0;
    00162   return status;
    00163 }
    00164
    00166 StatusCode
    00167 EventCollectionSelector::connectStatement(const std::string& typ, const std::string& crit, INTuple* tuple)  const {
    00168   std::string seltyp = typ;
    00169   if ( seltyp.length() > 0 || crit.length() > 0 )   {
    00170     if ( crit.length() > 0 && seltyp.length() == 0 ) seltyp = "NTuple::Selector";
    00171     SmartIF<ISelectStatement> stmt(ROOT::Reflex::PluginService::Create<IInterface*>(seltyp, (IInterface*)serviceLocator()));
    00172     if ( stmt.isValid( ) )    {
    00173       if ( crit.length() > 0 ) stmt->setCriteria(crit);
    00174       tuple->attachSelector(stmt).ignore();
    00175       return StatusCode::SUCCESS;
    00176     }
    00177     return StatusCode::FAILURE;
    00178   }
    00179   return StatusCode::SUCCESS;
    00180 }
    00181
    00183 StatusCode EventCollectionSelector::getNextRecord(NTuple::Tuple* tuple)   const {
    00184   StatusCode status = StatusCode::FAILURE;
    00185   if ( 0 != tuple )   {
    00186     do {
    00187       status = m_tupleSvc->readRecord(tuple);
    00188       if ( status.isSuccess() )   {
    00189         ISelectStatement* statement = tuple->selector();
    00190         bool use_it = (statement) ? (*statement)(tuple) : true;
    00191         if ( use_it )   {
    00192           return status;
    00193         }
    00194       }
    00195     } while ( status.isSuccess() );
    00196   }
    00197   return status;
    00198 }
    00199
    00201 StatusCode EventCollectionSelector::getPreviousRecord(NTuple::Tuple* tuple )  const  {
    00202   StatusCode status = StatusCode::FAILURE;
    00203   if ( tuple )  {
    00204     IRegistry* pReg = tuple->registry();
    00205     if ( pReg )  {
    00206       IOpaqueAddress* pAddr = pReg->address();
    00207       if ( pAddr )  {
    00208         long* ip = (long*)pAddr->ipar();
    00209         do {
    00210           if ( ip[1] > 1 )  {
    00211             ip[1] -= 2;
    00212             status = m_tupleSvc->readRecord(tuple);
    00213             if ( status.isSuccess() )   {
    00214               ISelectStatement* statement = tuple->selector();
    00215               bool use_it = (statement) ? (*statement)(tuple) : true;
    00216               if ( use_it )   {
    00217                 return status;
    00218               }
    00219             }
    00220           }
    00221           else  {
    00222             return StatusCode::FAILURE;
    00223           }
    00224         } while ( status.isSuccess() );
    00225       }
    00226     }
    00227   }
    00228   return StatusCode::FAILURE;
    00229 }
    00230
    00231 // Connect collection to selector
    00232 StatusCode EventCollectionSelector::connectCollection(MyContextType* ctxt) const
    00233 {
    00234   if ( ctxt )  {
    00235     StatusCode status = connectDataSource(m_database, m_dbType);
    00236     if ( status.isSuccess() )   {
    00237       status = connectTuple(m_cntName, m_itemName, ctxt->tuple, ctxt->item);
    00238       if ( status.isSuccess() )   {
    00239         status = connectStatement(m_statement, m_criteria, ctxt->tuple);
    00240         if ( status.isSuccess() )   {
    00241           *(ctxt->item) = 0;
    00242           return status;
    00243         }
    00244       }
    00245     }
    00246     return status;
    00247   }
    00248   return StatusCode::FAILURE;
    00249 }
    00250
    00251 // Finalize service
    00252 StatusCode EventCollectionSelector::finalize()    {
    00253   // release services
    00254   m_pAddrCreator = 0;
    00255   m_tupleSvc = 0;
    00256   return Service::finalize();
    00257 }
    00258
    00260 StatusCode
    00261 EventCollectionSelector::createContext(Context*& refpCtxt) const
    00262 {
    00263   refpCtxt = 0;
    00264   std::auto_ptr<MyContextType> ctxt(new MyContextType());
    00265   StatusCode status = connectCollection(ctxt.get());
    00266   if( !status.isSuccess() )  {
    00267     MsgStream log(msgSvc(), name());
    00268     log << MSG::ERROR << "Unable to connect Collection file \"" << m_database << "\"" << endmsg;
    00269     return status;
    00270   }
    00271   refpCtxt = ctxt.release();
    00272   return StatusCode::SUCCESS;
    00273 }
    00274
    00276 StatusCode
    00277 EventCollectionSelector::next(Context& refCtxt) const
    00278 {
    00279   return next(refCtxt, 1);
    00280 }
    00281
    00283 StatusCode
    00284 EventCollectionSelector::next(Context& refCtxt,int jump) const
    00285 {
    00286   MyContextType *ctxt  = dynamic_cast<MyContextType*>(&refCtxt);
    00287   if ( ctxt )    {
    00288     *(ctxt->item) = ctxt->addressBuffer;
    00289     StatusCode sc = StatusCode::SUCCESS;
    00290     for ( int i=0; i<jump && sc.isSuccess(); ++i ) {
    00291       sc = getNextRecord(ctxt->tuple);
    00292     }
    00293     return sc;
    00294   }
    00295   return StatusCode::FAILURE;
    00296 }
    00297
    00299 StatusCode
    00300 EventCollectionSelector::previous(Context& refCtxt) const
    00301 {
    00302   return previous(refCtxt, 1);
    00303 }
    00304
    00306 StatusCode
    00307 EventCollectionSelector::previous(Context& refCtxt,int jump) const
    00308 {
    00309   MyContextType *ctxt  = dynamic_cast<MyContextType*>(&refCtxt);
    00310   if ( ctxt )    {
    00311     *(ctxt->item) = ctxt->addressBuffer;
    00312     StatusCode sc = StatusCode::SUCCESS;
    00313     for ( int i=0; i<jump && sc.isSuccess(); ++i ) {
    00314       sc = getPreviousRecord(ctxt->tuple);
    00315     }
    00316     return sc;
    00317   }
    00318   return StatusCode::FAILURE;
    00319 }
    00320
    00322 StatusCode
    00323 EventCollectionSelector::rewind(Context& /* refCtxt */ ) const
    00324 {
    00325   return StatusCode::FAILURE;
    00326 }
    00327
    00329 StatusCode
    00330 EventCollectionSelector::createAddress(const Context& refCtxt, IOpaqueAddress*& refpAddr) const
    00331 {
    00332   const MyContextType* ctxt = dynamic_cast<const MyContextType*>(&refCtxt);
    00333   if ( ctxt )   {
    00334     IOpaqueAddress* pA = *(ctxt->item);
    00335     if ( pA )  {
    00336       IOpaqueAddress* pAddress = 0;
    00337       StatusCode status = m_pAddrCreator->createAddress(pA->svcType(),
    00338                                                         pA->clID(),
    00339                                                         pA->par(),
    00340                                                         pA->ipar(),
    00341                                                         pAddress);
    00342       if ( status.isSuccess() )   {
    00343         refpAddr = pAddress;
    00344         return StatusCode::SUCCESS;
    00345       }
    00346       else    {
    00347         MsgStream log(msgSvc(), name());
    00348         log << MSG::ERROR << "Failed to access " << pA->par()[0]
    00349             << ":" << pA->par()[1]
    00350             << " SvcTyp:" << long(pA->svcType())
    00351             << " CLID:"   << pA->clID()
    00352             << endmsg;
    00353       }
    00354     }
    00355   }
    00356   return StatusCode::FAILURE;
    00357 }
    00358
    00360 StatusCode
    00361 EventCollectionSelector::releaseContext(Context*& refCtxt) const
    00362 {
    00363   MyContextType *ctxt  = dynamic_cast<MyContextType*>(refCtxt);
    00364   if ( ctxt )   {
    00365     delete ctxt;
    00366     ctxt = 0;
    00367     return StatusCode::SUCCESS;
    00368   }
    00369   return StatusCode::FAILURE;
    00370 }
    00371
    00374 StatusCode
    00375 EventCollectionSelector::resetCriteria(const std::string& cr,Context& refCtxt) const
    00376 {
    00377   MyContextType *ctxt  = dynamic_cast<MyContextType*>(&refCtxt);
    00378   if ( ctxt )   {
    00379     ctxt->criteria = cr;
    00380     return StatusCode::SUCCESS;
    00381   }
    00382   return StatusCode::FAILURE;
    00383 }
    00384
    00386 StatusCode
    00387 EventCollectionSelector::last(Context& /* refCtxt */ ) const
    00388 {
    00389   return StatusCode::FAILURE;
    00390 }

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines