| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 {
27 public:
28 typedef std::list<std::string> ListName;
29
30 private:
31 GenericAddress* m_pAddressBuffer = nullptr;
32 const EventCollectionSelector* m_pSelector = nullptr;
33 ListName m_files;
34 std::string m_criteria;
35 ListName::const_iterator m_fileIterator;
36 std::string m_currentInput;
37
38 public:
40 EventCollectionContext( const EventCollectionSelector* pSelector );
42 ~EventCollectionContext() override;
43 const std::string& currentInput() const { return m_currentInput; }
44 void setCurrentInput( const std::string& v ) { m_currentInput = v; }
45 ListName& files() { return m_files; }
46 void* identifier() const override { return (void*)m_pSelector; }
47 void setCriteria( const std::string& crit ) { m_criteria = crit; }
48 ListName::const_iterator fileIterator() { return m_fileIterator; }
49 void setFileIterator( ListName::const_iterator new_iter ) { m_fileIterator = new_iter; }
50 };
51
52 EventCollectionContext::~EventCollectionContext() { m_pAddressBuffer->release(); }
53
54 EventCollectionContext::EventCollectionContext( const EventCollectionSelector* pSelector ) : m_pSelector( pSelector )
55 {
56 m_pAddressBuffer = new GenericAddress();
57 m_pAddressBuffer->addRef();
58 }
59
60 // IService implementation: Db event selector override
61 StatusCode EventCollectionSelector::initialize()
62 {
63 // Initialize base class
64 StatusCode status = Service::initialize();
65 if ( !status.isSuccess() ) {
66 error() << "Error initializing base class Service!" << endmsg;
67 return status;
68 }
69 m_pAddrCreator = serviceLocator()->service( "EventPersistencySvc" );
70 if ( !m_pAddrCreator ) {
71 error() << "Unable to locate IAddressCreator interface of "
72 << "EventPersistencySvc" << endmsg;
73 return status;
74 }
75 m_tupleSvc = serviceLocator()->service( m_tupleSvcName );
76 if ( !m_tupleSvc ) {
77 error() << "Unable to locate INTupleSvc interface of " << m_tupleSvcName << endmsg;
78 return status;
79 }
80 return status;
81 }
82
83 // Connect collection to selector
84 StatusCode EventCollectionSelector::connectDataSource( const std::string& db, const std::string& /* typ */ ) const
85 {
86 StatusCode status = StatusCode::FAILURE;
87 SmartIF<IDataSourceMgr> svc( m_tupleSvc );
88 if ( svc && !db.empty() ) {
89 std::string ident = name() + ' ';
90 ident += "DATAFILE='" + m_database.value().substr( 5 ) + "' ";
91 if ( !m_dbSvc.empty() )
92 ident += "SVC='" + m_dbSvc + "' ";
93 else
94 ident += "TYP='" + m_dbType + "' ";
95 ident += "OPT='READ' ";
96 if ( m_authentication.length() > 0 ) {
97 ident += "AUTH='" + m_authentication + "' ";
98 }
99 status = svc->connect( ident );
100 }
101 return status;
102 }
103
105 StatusCode EventCollectionSelector::connectTuple( const std::string& nam, const std::string& itName,
106 NTuple::Tuple*& tup, NTuple::Item<IOpaqueAddress*>*& item ) const
107 {
108 std::string top = "/NTUPLES/" + name() + '/' + nam;
109 StatusCode status = m_tupleSvc->retrieveObject( top, (DataObject*&)tup );
110 if ( status.isSuccess() ) {
111 item = new NTuple::Item<IOpaqueAddress*>();
112 status = tup->item( itName, *item );
113 if ( status.isSuccess() ) return status;
114 error() << "Item " << itName << " is not part of the collection:" << top << endmsg;
115 delete item;
116 item = nullptr;
117 } else {
118 error() << "Cannot connect to collection:" << top << endmsg;
119 }
120 tup = nullptr;
121 return status;
122 }
123
125 StatusCode EventCollectionSelector::connectStatement( const std::string& typ, const std::string& crit,
126 INTuple* tuple ) const
127 {
128 std::string seltyp = typ;
129 if ( !seltyp.empty() || !crit.empty() ) {
130 if ( !crit.empty() && seltyp.length() == 0 ) seltyp = "NTuple::Selector";
131 SmartIF<ISelectStatement> stmt( ObjFactory::create( seltyp, serviceLocator() ) );
132 if ( stmt ) {
133 if ( !crit.empty() ) stmt->setCriteria( crit );
134 tuple->attachSelector( stmt ).ignore();
135 return StatusCode::SUCCESS;
136 }
137 return StatusCode::FAILURE;
138 }
139 return StatusCode::SUCCESS;
140 }
141
143 StatusCode EventCollectionSelector::getNextRecord( NTuple::Tuple* tuple ) const
144 {
145 StatusCode status = StatusCode::FAILURE;
146 if ( tuple ) {
147 do {
148 status = m_tupleSvc->readRecord( tuple );
149 if ( status.isSuccess() ) {
150 ISelectStatement* statement = tuple->selector();
151 bool use_it = ( statement ) ? ( *statement )( tuple ) : true;
152 if ( use_it ) {
153 return status;
154 }
155 }
156 } while ( status.isSuccess() );
157 }
158 return status;
159 }
160
162 StatusCode EventCollectionSelector::getPreviousRecord( NTuple::Tuple* tuple ) const
163 {
164 StatusCode status = StatusCode::FAILURE;
165 if ( tuple ) {
166 IRegistry* pReg = tuple->registry();
167 if ( pReg ) {
168 IOpaqueAddress* pAddr = pReg->address();
169 if ( pAddr ) {
170 long* ip = (long*)pAddr->ipar();
171 do {
172 if ( ip[1] > 1 ) {
173 ip[1] -= 2;
174 status = m_tupleSvc->readRecord( tuple );
175 if ( status.isSuccess() ) {
176 ISelectStatement* statement = tuple->selector();
177 bool use_it = ( statement ) ? ( *statement )( tuple ) : true;
178 if ( use_it ) {
179 return status;
180 }
181 }
182 } else {
183 return StatusCode::FAILURE;
184 }
185 } while ( status.isSuccess() );
186 }
187 }
188 }
189 return StatusCode::FAILURE;
190 }
191
192 // Connect collection to selector
193 StatusCode EventCollectionSelector::connectCollection( MyContextType* ctxt ) const
194 {
195 if ( ctxt ) {
196 StatusCode status = connectDataSource( m_database, m_dbType );
197 if ( status.isSuccess() ) {
198 status = connectTuple( m_cntName, m_itemName, ctxt->tuple, ctxt->item );
199 if ( status.isSuccess() ) {
200 status = connectStatement( m_statement, m_criteria, ctxt->tuple );
201 if ( status.isSuccess() ) {
202 *( ctxt->item ) = 0;
203 return status;
204 }
205 }
206 }
207 return status;
208 }
209 return StatusCode::FAILURE;
210 }
211
212 // Finalize service
213 StatusCode EventCollectionSelector::finalize()
214 {
215 // release services
216 m_pAddrCreator = nullptr;
217 m_tupleSvc = nullptr;
218 return Service::finalize();
219 }
220
222 StatusCode EventCollectionSelector::createContext( Context*& refpCtxt ) const
223 {
224 refpCtxt = nullptr;
225 std::unique_ptr<MyContextType> ctxt( new MyContextType() );
226 StatusCode status = connectCollection( ctxt.get() );
227 if ( !status.isSuccess() ) {
228 error() << "Unable to connect Collection file \"" << m_database << "\"" << endmsg;
229 return status;
230 }
231 refpCtxt = ctxt.release();
232 return StatusCode::SUCCESS;
233 }
234
236 StatusCode EventCollectionSelector::next( Context& refCtxt ) const { return next( refCtxt, 1 ); }
237
239 StatusCode EventCollectionSelector::next( Context& refCtxt, int jump ) const
240 {
241 MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
242 if ( ctxt ) {
243 *( ctxt->item ) = ctxt->addressBuffer;
244 StatusCode sc = StatusCode::SUCCESS;
245 for ( int i = 0; i < jump && sc.isSuccess(); ++i ) {
246 sc = getNextRecord( ctxt->tuple );
247 }
248 return sc;
249 }
250 return StatusCode::FAILURE;
251 }
252
254 StatusCode EventCollectionSelector::previous( Context& refCtxt ) const { return previous( refCtxt, 1 ); }
255
257 StatusCode EventCollectionSelector::previous( Context& refCtxt, int jump ) const
258 {
259 MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
260 if ( ctxt ) {
261 *( ctxt->item ) = ctxt->addressBuffer;
262 StatusCode sc = StatusCode::SUCCESS;
263 for ( int i = 0; i < jump && sc.isSuccess(); ++i ) {
264 sc = getPreviousRecord( ctxt->tuple );
265 }
266 return sc;
267 }
268 return StatusCode::FAILURE;
269 }
270
272 StatusCode EventCollectionSelector::rewind( Context& /* refCtxt */ ) const { return StatusCode::FAILURE; }
273
275 StatusCode EventCollectionSelector::createAddress( const Context& refCtxt, IOpaqueAddress*& refpAddr ) const
276 {
277 const MyContextType* ctxt = dynamic_cast<const MyContextType*>( &refCtxt );
278 if ( ctxt ) {
279 IOpaqueAddress* pA = *( ctxt->item );
280 if ( pA ) {
281 IOpaqueAddress* pAddress = nullptr;
282 StatusCode status = m_pAddrCreator->createAddress( pA->svcType(), pA->clID(), pA->par(), pA->ipar(), pAddress );
283 if ( status.isSuccess() ) {
284 refpAddr = pAddress;
285 return StatusCode::SUCCESS;
286 } else {
287 error() << "Failed to access " << pA->par()[0] << ":" << pA->par()[1] << " SvcTyp:" << long( pA->svcType() )
288 << " CLID:" << pA->clID() << endmsg;
289 }
290 }
291 }
292 return StatusCode::FAILURE;
293 }
294
296 StatusCode EventCollectionSelector::releaseContext( Context*& refCtxt ) const
297 {
298 MyContextType* ctxt = dynamic_cast<MyContextType*>( refCtxt );
299 if ( ctxt ) {
300 delete ctxt;
301 ctxt = nullptr;
302 return StatusCode::SUCCESS;
303 }
304 return StatusCode::FAILURE;
305 }
306
309 StatusCode EventCollectionSelector::resetCriteria( const std::string& cr, Context& refCtxt ) const
310 {
311 MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
312 if ( ctxt ) {
313 ctxt->criteria = cr;
314 return StatusCode::SUCCESS;
315 }
316 return StatusCode::FAILURE;
317 }
318
320 StatusCode EventCollectionSelector::last( Context& /* refCtxt */ ) const { return StatusCode::FAILURE; }
StatusCode::SUCCESS
Definition: StatusCode.h:29
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:64
SmartIF
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:15
std::string::empty
T empty(T...args)
EventCollectionSelector::createContext
StatusCode createContext(Context *&refpCtxt) const override
Create a new event loop context.
Definition: EventCollectionSelector.cpp:222
IOpaqueAddress::clID
virtual const CLID & clID() const =0
Retrieve class information from link.
EventCollectionSelector::getNextRecord
virtual StatusCode getNextRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
Definition: EventCollectionSelector.cpp:143
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:309
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:174
ISelectStatement
A select statement can either contain.
Definition: ISelectStatement.h:32
EventCollectionContext
Definition: EventCollectionSelector.cpp:25
EventCollectionContext::EventCollectionContext
EventCollectionContext(const EventCollectionSelector *pSelector)
Standard constructor.
Definition: EventCollectionSelector.cpp:54
IEvtSelector
The Event Selector Interface.
Definition: IEvtSelector.h:18
NTuple.h
GenericAddress::addRef
unsigned long addRef() override
Add reference to object.
Definition: GenericAddress.h:63
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:50
GenericAddress::release
unsigned long release() override
release reference to object
Definition: GenericAddress.h:65
EventCollectionSelector::connectDataSource
virtual StatusCode connectDataSource(const std::string &db, const std::string &typ) const
Connect collection&#39;s data source to selector.
Definition: EventCollectionSelector.cpp:84
EventCollectionContext::~EventCollectionContext
~EventCollectionContext() override
Standard destructor.
Definition: EventCollectionSelector.cpp:52
ObjectFactory.h
ISelectStatement.h
class
#define class
Definition: HistogramPersistencySvc.cpp:45
GenericAddress
Generic Transient Address.
Definition: GenericAddress.h:20
INTuple::selector
virtual ISelectStatement * selector()=0
Access selector.
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
EventCollectionSelector.h
EventCollectionContext::m_pAddressBuffer
GenericAddress * m_pAddressBuffer
Definition: EventCollectionSelector.cpp:31
IDataSourceMgr.h
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:73
std::unique_ptr::release
T release(T...args)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
EventCollectionSelector::createAddress
StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const override
Create new Opaque address corresponding to the current record.
Definition: EventCollectionSelector.cpp:275
std::string
STL class.
INTuple
NTuple interface class definition.
Definition: INTuple.h:82
EventCollectionSelector::finalize
StatusCode finalize() override
Service override: Finalize Service.
Definition: EventCollectionSelector.cpp:213
IDataSourceMgr::connect
virtual StatusCode connect(const std::string &logon, std::string &identifier)=0
Connect data source.
EventCollectionContext::fileIterator
ListName::const_iterator fileIterator()
Definition: EventCollectionSelector.cpp:48
EventCollectionSelector::MyContextType::tuple
NTuple::Tuple * tuple
Definition: EventCollectionSelector.h:49
EventCollectionContext::currentInput
const std::string & currentInput() const
Definition: EventCollectionSelector.cpp:43
IOpaqueAddress::ipar
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
EventCollectionSelector::MyContextType::criteria
std::string criteria
Definition: EventCollectionSelector.h:48
EventCollectionContext::setFileIterator
void setFileIterator(ListName::const_iterator new_iter)
Definition: EventCollectionSelector.cpp:49
SmartIF.h
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
EventCollectionSelector::last
StatusCode last(Context &refCtxt) const override
Access last item in the iteration.
Definition: EventCollectionSelector.cpp:320
IOpaqueAddress::svcType
virtual long svcType() const =0
Retrieve service type.
std::list< std::string >
EventCollectionContext::files
ListName & files()
Definition: EventCollectionSelector.cpp:45
NTuple::Tuple
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:412
EventCollectionContext::setCurrentInput
void setCurrentInput(const std::string &v)
Definition: EventCollectionSelector.cpp:44
std::unique_ptr::get
T get(T...args)
std::string::length
T length(T...args)
EventCollectionContext::m_pSelector
const EventCollectionSelector * m_pSelector
Definition: EventCollectionSelector.cpp:32
std::unique_ptr
STL class.
IAddressCreator.h
EventCollectionSelector::getPreviousRecord
virtual StatusCode getPreviousRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
Definition: EventCollectionSelector.cpp:162
EventCollectionSelector
Definition of class EventCollectionSelector.
Definition: EventCollectionSelector.h:42
EventCollectionContext::m_files
ListName m_files
Definition: EventCollectionSelector.cpp:33
EventCollectionSelector::previous
StatusCode previous(Context &refCtxt) const override
Get previous iteration item from the event loop context.
Definition: EventCollectionSelector.cpp:254
EventCollectionSelector::releaseContext
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
Definition: EventCollectionSelector.cpp:296
EventCollectionSelector::MyContextType::item
NTuple::Item< IOpaqueAddress * > * item
Definition: EventCollectionSelector.h:50
EventCollectionSelector::initialize
StatusCode initialize() override
Service override: Initialize service.
Definition: EventCollectionSelector.cpp:61
std::string::substr
T substr(T...args)
EventCollectionSelector::MyContextType::addressBuffer
IOpaqueAddress * addressBuffer
Definition: EventCollectionSelector.h:51
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:105
EventCollectionContext::ListName
std::list< std::string > ListName
Definition: EventCollectionSelector.cpp:28
StatusCode::FAILURE
Definition: StatusCode.h:29
IOpaqueAddress
Opaque address interface definition.
Definition: IOpaqueAddress.h:23
StatusCode::ignore
void ignore() const
Definition: StatusCode.h:84
EventCollectionSelector::rewind
StatusCode rewind(Context &refCtxt) const override
Rewind the dataset.
Definition: EventCollectionSelector.cpp:272
INTuple::attachSelector
virtual StatusCode attachSelector(ISelectStatement *sel)=0
Attach selector.
NTuple::Item< IOpaqueAddress * >
EventCollectionContext::m_currentInput
std::string m_currentInput
Definition: EventCollectionSelector.cpp:36
EventCollectionSelector::connectCollection
virtual StatusCode connectCollection(MyContextType *ctxt) const
Connect collection to selector.
Definition: EventCollectionSelector.cpp:193
EventCollectionContext::m_fileIterator
ListName::const_iterator m_fileIterator
Definition: EventCollectionSelector.cpp:35
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
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
EventCollectionSelector::next
StatusCode next(Context &refCtxt) const override
Get next iteration item from the event loop context.
Definition: EventCollectionSelector.cpp:236
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
EventCollectionContext::m_criteria
std::string m_criteria
Definition: EventCollectionSelector.cpp:34
EventCollectionContext::setCriteria
void setCriteria(const std::string &crit)
Definition: EventCollectionSelector.cpp:47
EventCollectionContext::identifier
void * identifier() const override
Definition: EventCollectionSelector.cpp:46
EventCollectionSelector::MyContextType
Definition: EventCollectionSelector.h:45
INTupleSvc.h
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:125