| The Gaudi Framework v28r2-patches (bc4ad17a)

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