All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
EventCollectionSelector.cpp
Go to the documentation of this file.
1 // Include files
2 #include "GaudiKernel/SmartIF.h"
11 #include "GaudiKernel/NTuple.h"
13 
15 
16 #include <memory>
17 
19 
20 
26 public:
28 private:
29  GenericAddress* m_pAddressBuffer = nullptr;
30  const EventCollectionSelector* m_pSelector = nullptr;
31  ListName m_files;
33  ListName::const_iterator m_fileIterator;
35 
36 public:
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 
65 }
66 
68  : m_pSelector(pSelector)
69 {
72 }
73 
74 // IService implementation: Db event selector override
76  // Initialize base class
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
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 
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 
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 
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 
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
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
224  // release services
225  m_pAddrCreator = nullptr;
226  m_tupleSvc = nullptr;
227  return Service::finalize();
228 }
229 
232 EventCollectionSelector::createContext(Context*& refpCtxt) const
233 {
234  refpCtxt = nullptr;
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 
247 EventCollectionSelector::next(Context& refCtxt) const
248 {
249  return next(refCtxt, 1);
250 }
251 
254 EventCollectionSelector::next(Context& refCtxt,int jump) const
255 {
256  MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
257  if ( ctxt ) {
258  *(ctxt->item) = ctxt->addressBuffer;
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 
270 EventCollectionSelector::previous(Context& refCtxt) const
271 {
272  return previous(refCtxt, 1);
273 }
274 
277 EventCollectionSelector::previous(Context& refCtxt,int jump) const
278 {
279  MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
280  if ( ctxt ) {
281  *(ctxt->item) = ctxt->addressBuffer;
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 
293 EventCollectionSelector::rewind(Context& /* refCtxt */ ) const
294 {
295  return StatusCode::FAILURE;
296 }
297 
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 
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 
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 
356 EventCollectionSelector::last(Context& /* refCtxt */ ) const
357 {
358  return StatusCode::FAILURE;
359 }
StatusCode initialize() override
Definition: Service.cpp:64
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:14
T empty(T...args)
StatusCode createContext(Context *&refpCtxt) const override
Create a new event loop context.
virtual const CLID & clID() const =0
Retrieve class information from link.
virtual StatusCode getNextRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
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...
StatusCode finalize() override
Definition: Service.cpp:174
A select statement can either contain.
EventCollectionContext(const EventCollectionSelector *pSelector)
Standard constructor.
The Event Selector Interface.
Definition: IEvtSelector.h:18
unsigned long addRef() override
Add reference to object.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
unsigned long release() override
release reference to object
virtual StatusCode connectDataSource(const std::string &db, const std::string &typ) const
Connect collection&#39;s data source to selector.
~EventCollectionContext() override
Standard destructor.
#define class
Generic Transient Address.
virtual ISelectStatement * selector()=0
Access selector.
virtual const std::string * par() const =0
Retrieve String parameters.
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:74
T release(T...args)
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const override
Create new Opaque address corresponding to the current record.
STL class.
NTuple interface class definition.
Definition: INTuple.h:79
StatusCode finalize() override
Service override: Finalize Service.
virtual StatusCode connect(const std::string &logon, std::string &identifier)=0
Connect data source.
ListName::const_iterator fileIterator()
const std::string & currentInput() const
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
void setFileIterator(ListName::const_iterator new_iter)
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
StatusCode last(Context &refCtxt) const override
Access last item in the iteration.
virtual long svcType() const =0
Retrieve service type.
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:370
void setCurrentInput(const std::string &v)
T get(T...args)
T length(T...args)
const EventCollectionSelector * m_pSelector
STL class.
virtual StatusCode getPreviousRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
Definition of class EventCollectionSelector.
StatusCode previous(Context &refCtxt) const override
Get previous iteration item from the event loop context.
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
NTuple::Item< IOpaqueAddress * > * item
StatusCode initialize() override
Service override: Initialize service.
T substr(T...args)
virtual StatusCode connectTuple(const std::string &nam, const std::string &itName, NTuple::Tuple *&tup, NTuple::Item< IOpaqueAddress * > *&item) const
Connect to existing N-tuple.
std::list< std::string > ListName
Opaque address interface definition.
void ignore() const
Definition: StatusCode.h:106
StatusCode rewind(Context &refCtxt) const override
Rewind the dataset.
virtual StatusCode attachSelector(ISelectStatement *sel)=0
Attach selector.
virtual StatusCode connectCollection(MyContextType *ctxt) const
Connect collection to selector.
ListName::const_iterator m_fileIterator
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
virtual void setCriteria(const std::string &crit)=0
Set the type.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
StatusCode next(Context &refCtxt) const override
Get next iteration item from the event loop context.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
void setCriteria(const std::string &crit)
void * identifier() const override
virtual StatusCode connectStatement(const std::string &typ, const std::string &crit, INTuple *tuple) const
Connect selection statement to refine data access.