| The Gaudi Framework v32r1 (f65d50dc)

EventCollectionSelector.cpp
Go to the documentation of this file.
1 // Include files
2 #include "GaudiKernel/EventSelectorDataStream.h"
3 #include "GaudiKernel/GenericAddress.h"
4 #include "GaudiKernel/IAddressCreator.h"
5 #include "GaudiKernel/IDataSourceMgr.h"
6 #include "GaudiKernel/INTupleSvc.h"
7 #include "GaudiKernel/IRegistry.h"
8 #include "GaudiKernel/ISelectStatement.h"
9 #include "GaudiKernel/ISvcLocator.h"
10 #include "GaudiKernel/NTuple.h"
11 #include "GaudiKernel/ObjectFactory.h"
12 #include "GaudiKernel/SmartIF.h"
13
14 #include "EventCollectionSelector.h"
15
16 #include <memory>
17
18 DECLARE_COMPONENT( EventCollectionSelector )
19
20
25 class EventCollectionContext : public IEvtSelector::Context {
26 public:
27 typedef std::list<std::string> ListName;
28
29 private:
30 GenericAddress* m_pAddressBuffer = nullptr;
31 const EventCollectionSelector* m_pSelector = nullptr;
32 ListName m_files;
33 std::string m_criteria;
34 ListName::const_iterator m_fileIterator;
35 std::string m_currentInput;
36
37 public:
39 EventCollectionContext( const EventCollectionSelector* pSelector );
41 ~EventCollectionContext() override;
42 const std::string& currentInput() const { return m_currentInput; }
43 void setCurrentInput( const std::string& v ) { m_currentInput = v; }
44 ListName& files() { return m_files; }
45 void* identifier() const override { return (void*)m_pSelector; }
46 void setCriteria( const std::string& crit ) { m_criteria = crit; }
47 ListName::const_iterator fileIterator() { return m_fileIterator; }
48 void setFileIterator( ListName::const_iterator new_iter ) { m_fileIterator = new_iter; }
49 };
50
51 EventCollectionContext::~EventCollectionContext() { m_pAddressBuffer->release(); }
52
53 EventCollectionContext::EventCollectionContext( const EventCollectionSelector* pSelector ) : m_pSelector( pSelector ) {
54 m_pAddressBuffer = new GenericAddress();
55 m_pAddressBuffer->addRef();
56 }
57
58 // IService implementation: Db event selector override
59 StatusCode EventCollectionSelector::initialize() {
60 // Initialize base class
61 StatusCode status = Service::initialize();
62 if ( !status.isSuccess() ) {
63 error() << "Error initializing base class Service!" << endmsg;
64 return status;
65 }
66 m_pAddrCreator = serviceLocator()->service( "EventPersistencySvc" );
67 if ( !m_pAddrCreator ) {
68 error() << "Unable to locate IAddressCreator interface of "
69 << "EventPersistencySvc" << endmsg;
70 return status;
71 }
72 m_tupleSvc = serviceLocator()->service( m_tupleSvcName );
73 if ( !m_tupleSvc ) {
74 error() << "Unable to locate INTupleSvc interface of " << m_tupleSvcName << endmsg;
75 return status;
76 }
77 return status;
78 }
79
80 // Connect collection to selector
81 StatusCode EventCollectionSelector::connectDataSource( const std::string& db, const std::string& /* typ */ ) const {
82 StatusCode status = StatusCode::FAILURE;
83 SmartIF<IDataSourceMgr> svc( m_tupleSvc );
84 if ( svc && !db.empty() ) {
85 std::string ident = name() + ' ';
86 ident += "DATAFILE='" + m_database.value().substr( 5 ) + "' ";
87 if ( !m_dbSvc.empty() )
88 ident += "SVC='" + m_dbSvc + "' ";
89 else
90 ident += "TYP='" + m_dbType + "' ";
91 ident += "OPT='READ' ";
92 if ( m_authentication.length() > 0 ) { ident += "AUTH='" + m_authentication + "' "; }
93 status = svc->connect( ident );
94 }
95 return status;
96 }
97
99 StatusCode EventCollectionSelector::connectTuple( const std::string& nam, const std::string& itName,
100 NTuple::Tuple*& tup, NTuple::Item<IOpaqueAddress*>*& item ) const {
101 std::string top = "/NTUPLES/" + name() + '/' + nam;
102 StatusCode status = m_tupleSvc->retrieveObject( top, (DataObject*&)tup );
103 if ( status.isSuccess() ) {
104 item = new NTuple::Item<IOpaqueAddress*>();
105 status = tup->item( itName, *item );
106 if ( status.isSuccess() ) return status;
107 error() << "Item " << itName << " is not part of the collection:" << top << endmsg;
108 delete item;
109 item = nullptr;
110 } else {
111 error() << "Cannot connect to collection:" << top << endmsg;
112 }
113 tup = nullptr;
114 return status;
115 }
116
118 StatusCode EventCollectionSelector::connectStatement( const std::string& typ, const std::string& crit,
119 INTuple* tuple ) const {
120 std::string seltyp = typ;
121 if ( !seltyp.empty() || !crit.empty() ) {
122 if ( !crit.empty() && seltyp.length() == 0 ) seltyp = "NTuple::Selector";
123 SmartIF<ISelectStatement> stmt( ObjFactory::create( seltyp, serviceLocator() ).release() );
124 if ( stmt ) {
125 if ( !crit.empty() ) stmt->setCriteria( crit );
126 tuple->attachSelector( stmt ).ignore();
127 return StatusCode::SUCCESS;
128 }
129 return StatusCode::FAILURE;
130 }
131 return StatusCode::SUCCESS;
132 }
133
135 StatusCode EventCollectionSelector::getNextRecord( NTuple::Tuple* tuple ) const {
136 StatusCode status = StatusCode::FAILURE;
137 if ( tuple ) {
138 do {
139 status = m_tupleSvc->readRecord( tuple );
140 if ( status.isSuccess() ) {
141 ISelectStatement* statement = tuple->selector();
142 bool use_it = ( statement ) ? ( *statement )( tuple ) : true;
143 if ( use_it ) { return status; }
144 }
145 } while ( status.isSuccess() );
146 }
147 return status;
148 }
149
151 StatusCode EventCollectionSelector::getPreviousRecord( NTuple::Tuple* tuple ) const {
152 StatusCode status = StatusCode::FAILURE;
153 if ( tuple ) {
154 IRegistry* pReg = tuple->registry();
155 if ( pReg ) {
156 IOpaqueAddress* pAddr = pReg->address();
157 if ( pAddr ) {
158 long* ip = (long*)pAddr->ipar();
159 do {
160 if ( ip[1] > 1 ) {
161 ip[1] -= 2;
162 status = m_tupleSvc->readRecord( tuple );
163 if ( status.isSuccess() ) {
164 ISelectStatement* statement = tuple->selector();
165 bool use_it = ( statement ) ? ( *statement )( tuple ) : true;
166 if ( use_it ) { return status; }
167 }
168 } else {
169 return StatusCode::FAILURE;
170 }
171 } while ( status.isSuccess() );
172 }
173 }
174 }
175 return StatusCode::FAILURE;
176 }
177
178 // Connect collection to selector
179 StatusCode EventCollectionSelector::connectCollection( MyContextType* ctxt ) const {
180 if ( ctxt ) {
181 StatusCode status = connectDataSource( m_database, m_dbType );
182 if ( status.isSuccess() ) {
183 status = connectTuple( m_cntName, m_itemName, ctxt->tuple, ctxt->item );
184 if ( status.isSuccess() ) {
185 status = connectStatement( m_statement, m_criteria, ctxt->tuple );
186 if ( status.isSuccess() ) {
187 *( ctxt->item ) = 0;
188 return status;
189 }
190 }
191 }
192 return status;
193 }
194 return StatusCode::FAILURE;
195 }
196
197 // Finalize service
198 StatusCode EventCollectionSelector::finalize() {
199 // release services
200 m_pAddrCreator = nullptr;
201 m_tupleSvc = nullptr;
202 return Service::finalize();
203 }
204
206 StatusCode EventCollectionSelector::createContext( Context*& refpCtxt ) const {
207 refpCtxt = nullptr;
208 auto ctxt = std::make_unique<MyContextType>();
209 StatusCode status = connectCollection( ctxt.get() );
210 if ( !status.isSuccess() ) {
211 error() << "Unable to connect Collection file \"" << m_database << "\"" << endmsg;
212 return status;
213 }
214 refpCtxt = ctxt.release();
215 return StatusCode::SUCCESS;
216 }
217
219 StatusCode EventCollectionSelector::next( Context& refCtxt ) const { return next( refCtxt, 1 ); }
220
222 StatusCode EventCollectionSelector::next( Context& refCtxt, int jump ) const {
223 MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
224 if ( ctxt ) {
225 *( ctxt->item ) = ctxt->addressBuffer;
226 StatusCode sc = StatusCode::SUCCESS;
227 for ( int i = 0; i < jump && sc.isSuccess(); ++i ) { sc = getNextRecord( ctxt->tuple ); }
228 return sc;
229 }
230 return StatusCode::FAILURE;
231 }
232
234 StatusCode EventCollectionSelector::previous( Context& refCtxt ) const { return previous( refCtxt, 1 ); }
235
237 StatusCode EventCollectionSelector::previous( Context& refCtxt, int jump ) const {
238 MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
239 if ( ctxt ) {
240 *( ctxt->item ) = ctxt->addressBuffer;
241 StatusCode sc = StatusCode::SUCCESS;
242 for ( int i = 0; i < jump && sc.isSuccess(); ++i ) { sc = getPreviousRecord( ctxt->tuple ); }
243 return sc;
244 }
245 return StatusCode::FAILURE;
246 }
247
249 StatusCode EventCollectionSelector::rewind( Context& /* refCtxt */ ) const { return StatusCode::FAILURE; }
250
252 StatusCode EventCollectionSelector::createAddress( const Context& refCtxt, IOpaqueAddress*& refpAddr ) const {
253 const MyContextType* ctxt = dynamic_cast<const MyContextType*>( &refCtxt );
254 if ( ctxt ) {
255 IOpaqueAddress* pA = *( ctxt->item );
256 if ( pA ) {
257 IOpaqueAddress* pAddress = nullptr;
258 StatusCode status = m_pAddrCreator->createAddress( pA->svcType(), pA->clID(), pA->par(), pA->ipar(), pAddress );
259 if ( status.isSuccess() ) {
260 refpAddr = pAddress;
261 return StatusCode::SUCCESS;
262 } else {
263 error() << "Failed to access " << pA->par()[0] << ":" << pA->par()[1] << " SvcTyp:" << long( pA->svcType() )
264 << " CLID:" << pA->clID() << endmsg;
265 }
266 }
267 }
268 return StatusCode::FAILURE;
269 }
270
272 StatusCode EventCollectionSelector::releaseContext( Context*& refCtxt ) const {
273 MyContextType* ctxt = dynamic_cast<MyContextType*>( refCtxt );
274 if ( ctxt ) {
275 delete ctxt;
276 ctxt = nullptr;
277 return StatusCode::SUCCESS;
278 }
279 return StatusCode::FAILURE;
280 }
281
284 StatusCode EventCollectionSelector::resetCriteria( const std::string& cr, Context& refCtxt ) const {
285 MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
286 if ( ctxt ) {
287 ctxt->criteria = cr;
288 return StatusCode::SUCCESS;
289 }
290 return StatusCode::FAILURE;
291 }
292
294 StatusCode EventCollectionSelector::last( Context& /* refCtxt */ ) const { return StatusCode::FAILURE; }
EventCollectionSelector::m_tupleSvc
SmartIF< INTupleSvc > m_tupleSvc
Reference to Tuple service.
Definition: EventCollectionSelector.h:75
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:60
SmartIF
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:15
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:277
std::string::empty
T empty(T... args)
EventCollectionSelector::connectStatement
virtual StatusCode connectStatement(const std::string &typ, const std::string &crit, INTuple *tuple) const
Connect selection statement to refine data access.
Definition: EventCollectionSelector.cpp:118
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:164
ISelectStatement
A select statement can either contain.
Definition: ISelectStatement.h:32
EventCollectionContext
Definition: EventCollectionSelector.cpp:25
IAddressCreator::createAddress
virtual StatusCode createAddress(long svc_type, const CLID &clid, const std::string *par, const unsigned long *ipar, IOpaqueAddress *&refpAddress)=0
Create a Generic address using explicit arguments to identify a single object.
EventCollectionContext::EventCollectionContext
EventCollectionContext(const EventCollectionSelector *pSelector)
Standard constructor.
Definition: EventCollectionSelector.cpp:53
IEvtSelector
The Event Selector Interface.
Definition: IEvtSelector.h:18
NTuple.h
GenericAddress::addRef
unsigned long addRef() override
Add reference to object.
Definition: GenericAddress.h:57
GenericAddress::release
unsigned long release() override
release reference to object
Definition: GenericAddress.h:59
EventCollectionContext::currentInput
const std::string & currentInput() const
Definition: EventCollectionSelector.cpp:42
EventCollectionContext::~EventCollectionContext
~EventCollectionContext() override
Standard destructor.
Definition: EventCollectionSelector.cpp:51
ObjectFactory.h
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
ISelectStatement.h
EventCollectionSelector::m_authentication
Gaudi::Property< std::string > m_authentication
Definition: EventCollectionSelector.h:81
EventCollectionSelector::previous
StatusCode previous(Context &refCtxt) const override
Get previous iteration item from the event loop context.
Definition: EventCollectionSelector.cpp:234
EventCollectionSelector::getPreviousRecord
virtual StatusCode getPreviousRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
Definition: EventCollectionSelector.cpp:151
EventCollectionSelector::connectCollection
virtual StatusCode connectCollection(MyContextType *ctxt) const
Connect collection to selector.
Definition: EventCollectionSelector.cpp:179
EventCollectionSelector::getNextRecord
virtual StatusCode getNextRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
Definition: EventCollectionSelector.cpp:135
class
#define class
Definition: HistogramPersistencySvc.cpp:45
GenericAddress
Generic Transient Address.
Definition: GenericAddress.h:20
INTuple::selector
virtual ISelectStatement * selector()=0
Access selector.
IOpaqueAddress::svcType
virtual long svcType() const =0
Retrieve service type.
EventCollectionSelector.h
EventCollectionContext::m_pAddressBuffer
GenericAddress * m_pAddressBuffer
Definition: EventCollectionSelector.cpp:30
EventCollectionSelector::m_cntName
Gaudi::Property< std::string > m_cntName
Definition: EventCollectionSelector.h:82
IDataSourceMgr.h
EventCollectionSelector::m_database
Gaudi::Property< std::string > m_database
Definition: EventCollectionSelector.h:85
EventCollectionSelector::releaseContext
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
Definition: EventCollectionSelector.cpp:272
EventCollectionSelector::m_tupleSvcName
Gaudi::Property< std::string > m_tupleSvcName
Definition: EventCollectionSelector.h:79
std::string
STL class.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
INTuple
NTuple interface class definition.
Definition: INTuple.h:81
EventCollectionSelector::finalize
StatusCode finalize() override
Service override: Finalize Service.
Definition: EventCollectionSelector.cpp:198
IDataSourceMgr::connect
virtual StatusCode connect(const std::string &logon, std::string &identifier)=0
Connect data source.
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
Service::name
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
EventCollectionContext::fileIterator
ListName::const_iterator fileIterator()
Definition: EventCollectionSelector.cpp:47
EventCollectionSelector::MyContextType::tuple
NTuple::Tuple * tuple
Definition: EventCollectionSelector.h:47
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:98
EventCollectionSelector::last
StatusCode last(Context &refCtxt) const override
Access last item in the iteration.
Definition: EventCollectionSelector.cpp:294
EventCollectionSelector::next
StatusCode next(Context &refCtxt) const override
Get next iteration item from the event loop context.
Definition: EventCollectionSelector.cpp:219
IOpaqueAddress::clID
virtual const CLID & clID() const =0
Retrieve class information from link.
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
EventCollectionSelector::MyContextType::criteria
std::string criteria
Definition: EventCollectionSelector.h:46
EventCollectionContext::setFileIterator
void setFileIterator(ListName::const_iterator new_iter)
Definition: EventCollectionSelector.cpp:48
SmartIF.h
EventCollectionSelector::connectDataSource
virtual StatusCode connectDataSource(const std::string &db, const std::string &typ) const
Connect collection's data source to selector.
Definition: EventCollectionSelector.cpp:81
EventCollectionSelector::resetCriteria
StatusCode resetCriteria(const std::string &cr, Context &c) const override
Will set a new criteria for the selection of the next list of events and will change the state of the...
Definition: EventCollectionSelector.cpp:284
EventCollectionSelector::m_statement
Gaudi::Property< std::string > m_statement
Definition: EventCollectionSelector.h:88
EventSelectorDataStream.h
IRegistry.h
IRegistry
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:267
EventCollectionSelector::m_dbType
Gaudi::Property< std::string > m_dbType
Definition: EventCollectionSelector.h:86
std::list< std::string >
EventCollectionContext::files
ListName & files()
Definition: EventCollectionSelector.cpp:44
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
EventCollectionSelector::connectTuple
virtual StatusCode connectTuple(const std::string &nam, const std::string &itName, NTuple::Tuple *&tup, NTuple::Item< IOpaqueAddress * > *&item) const
Connect to existing N-tuple.
Definition: EventCollectionSelector.cpp:99
NTuple::Tuple
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:375
EventCollectionSelector::m_criteria
Gaudi::Property< std::string > m_criteria
Definition: EventCollectionSelector.h:84
EventCollectionSelector::m_dbSvc
Gaudi::Property< std::string > m_dbSvc
Definition: EventCollectionSelector.h:87
EventCollectionContext::setCurrentInput
void setCurrentInput(const std::string &v)
Definition: EventCollectionSelector.cpp:43
std::string::length
T length(T... args)
StatusCode::ignore
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
IDataProviderSvc::retrieveObject
virtual StatusCode retrieveObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
IAddressCreator.h
EventCollectionSelector
Definition of class EventCollectionSelector.
Definition: EventCollectionSelector.h:42
EventCollectionContext::m_files
ListName m_files
Definition: EventCollectionSelector.cpp:32
INTupleSvc::readRecord
virtual StatusCode readRecord(NTuple::Tuple *tuple)=0
Read single record from N tuple.
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
EventCollectionSelector::MyContextType::item
NTuple::Item< IOpaqueAddress * > * item
Definition: EventCollectionSelector.h:48
EventCollectionSelector::initialize
StatusCode initialize() override
Service override: Initialize service.
Definition: EventCollectionSelector.cpp:59
EventCollectionSelector::MyContextType::addressBuffer
IOpaqueAddress * addressBuffer
Definition: EventCollectionSelector.h:49
EventCollectionContext::ListName
std::list< std::string > ListName
Definition: EventCollectionSelector.cpp:27
EventCollectionSelector::createAddress
StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const override
Create new Opaque address corresponding to the current record.
Definition: EventCollectionSelector.cpp:252
IOpaqueAddress
Opaque address interface definition.
Definition: IOpaqueAddress.h:23
EventCollectionSelector::rewind
StatusCode rewind(Context &refCtxt) const override
Rewind the dataset.
Definition: EventCollectionSelector.cpp:249
EventCollectionSelector::m_pAddrCreator
SmartIF< IAddressCreator > m_pAddrCreator
Definition: EventCollectionSelector.h:76
INTuple::attachSelector
virtual StatusCode attachSelector(ISelectStatement *sel)=0
Attach selector.
NTuple::Item< IOpaqueAddress * >
EventCollectionContext::m_currentInput
std::string m_currentInput
Definition: EventCollectionSelector.cpp:35
EventCollectionContext::m_fileIterator
ListName::const_iterator m_fileIterator
Definition: EventCollectionSelector.cpp:34
ISelectStatement::setCriteria
virtual void setCriteria(const std::string &crit)=0
Set the type.
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
GenericAddress.h
ISvcLocator.h
IOpaqueAddress::ipar
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
EventCollectionContext::m_criteria
std::string m_criteria
Definition: EventCollectionSelector.cpp:33
EventCollectionContext::setCriteria
void setCriteria(const std::string &crit)
Definition: EventCollectionSelector.cpp:46
EventCollectionSelector::m_itemName
Gaudi::Property< std::string > m_itemName
Definition: EventCollectionSelector.h:83
EventCollectionSelector::MyContextType
Definition: EventCollectionSelector.h:44
INTupleSvc.h
EventCollectionSelector::createContext
StatusCode createContext(Context *&refpCtxt) const override
Create a new event loop context.
Definition: EventCollectionSelector.cpp:206
EventCollectionContext::identifier
void * identifier() const override
Definition: EventCollectionSelector.cpp:45