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"
3 #include "GaudiKernel/ObjectFactory.h"
4 #include "GaudiKernel/GenericAddress.h"
5 #include "GaudiKernel/MsgStream.h"
6 #include "GaudiKernel/IRegistry.h"
7 #include "GaudiKernel/INTupleSvc.h"
8 #include "GaudiKernel/ISvcLocator.h"
9 #include "GaudiKernel/IDataSourceMgr.h"
10 #include "GaudiKernel/IAddressCreator.h"
11 #include "GaudiKernel/ISelectStatement.h"
12 #include "GaudiKernel/NTuple.h"
13 #include "GaudiKernel/EventSelectorDataStream.h"
14 
16 
17 #include <memory>
18 
20 
21 
27 public:
28  typedef std::list<std::string> ListName;
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:
41  ~EventCollectionContext() override;
42  const std::string& currentInput() const {
43  return m_currentInput;
44  }
45  void setCurrentInput(const std::string& v) {
46  m_currentInput = v;
47  }
48  ListName& files() {
49  return m_files;
50  }
51  void* identifier() const override {
52  return (void*)m_pSelector;
53  }
54  void setCriteria(const std::string& crit) {
55  m_criteria = crit;
56  }
57  ListName::const_iterator fileIterator() {
58  return m_fileIterator;
59  }
60  void setFileIterator(ListName::const_iterator new_iter)
61  { m_fileIterator = new_iter; }
62 };
63 
66 }
67 
69  : m_pSelector(pSelector)
70 {
73 }
74 
75 // Standard constructor
77  : base_class(name, svcloc)
78 {
79  declareProperty("CnvService", m_tupleSvcName = "EvtTupleSvc");
80  declareProperty("Authentication",m_authentication= "");
81  declareProperty("Container", m_cntName = "B2PiPi");
82  declareProperty("Item", m_itemName = "Address");
83  declareProperty("Criteria", m_criteria = "");
84  declareProperty("DB", m_database = "");
85  declareProperty("DbType", m_dbType = "");
86  declareProperty("DbService", m_dbSvc = "");
87  declareProperty("Function", m_statement= "NTuple::Selector");
88 }
89 
90 // IService implementation: Db event selector override
92  // Initialize base class
94  MsgStream log(msgSvc(), name());
95  if ( !status.isSuccess() ) {
96  log << MSG::ERROR << "Error initializing base class Service!" << endmsg;
97  return status;
98  }
99  m_pAddrCreator = serviceLocator()->service("EventPersistencySvc");
100  if(!m_pAddrCreator) {
101  log << MSG::ERROR << "Unable to locate IAddressCreator interface of " << "EventPersistencySvc" << endmsg;
102  return status;
103  }
104  m_tupleSvc = serviceLocator()->service(m_tupleSvcName);
105  if( !m_tupleSvc ) {
106  log << MSG::ERROR << "Unable to locate INTupleSvc interface of " << m_tupleSvcName << endmsg;
107  return status;
108  }
109  return status;
110 }
111 
112 // Connect collection to selector
114 EventCollectionSelector::connectDataSource(const std::string& db, const std::string& /* typ */ ) const {
117  if ( svc && !db.empty() ) {
118  std::string ident = name() + ' ';
119  ident += "DATAFILE='" + m_database.substr(5) + "' ";
120  if ( !m_dbSvc.empty() )
121  ident += "SVC='" + m_dbSvc + "' ";
122  else
123  ident += "TYP='" + m_dbType + "' ";
124  ident += "OPT='READ' ";
125  if ( m_authentication.length() > 0 ) {
126  ident += "AUTH='" + m_authentication + "' ";
127  }
128  status = svc->connect(ident);
129  }
130  return status;
131 }
132 
135 EventCollectionSelector::connectTuple(const std::string& nam, const std::string& itName, NTuple::Tuple*& tup, NTuple::Item<IOpaqueAddress*>*& item) const {
136  std::string top = "/NTUPLES/" + name() + '/' + nam;
137  StatusCode status = m_tupleSvc->retrieveObject(top, (DataObject*&)tup);
138  if ( status.isSuccess() ) {
139  item = new NTuple::Item<IOpaqueAddress*>();
140  status = tup->item(itName, *item);
141  if ( status.isSuccess() ) return status;
142  MsgStream log(msgSvc(), name());
143  log << MSG::ERROR << "Item " << itName << " is not part of the collection:" << top << endmsg;
144  delete item;
145  item = nullptr;
146  }
147  else {
148  MsgStream err(msgSvc(), name());
149  err << MSG::ERROR << "Cannot connect to collection:" << top << endmsg;
150  }
151  tup = nullptr;
152  return status;
153 }
154 
157 EventCollectionSelector::connectStatement(const std::string& typ, const std::string& crit, INTuple* tuple) const {
158  std::string seltyp = typ;
159  if ( !seltyp.empty() || !crit.empty() ) {
160  if ( !crit.empty() && seltyp.length() == 0 ) seltyp = "NTuple::Selector";
161  SmartIF<ISelectStatement> stmt(ObjFactory::create(seltyp, serviceLocator()));
162  if ( stmt ) {
163  if ( !crit.empty() ) stmt->setCriteria(crit);
164  tuple->attachSelector(stmt).ignore();
165  return StatusCode::SUCCESS;
166  }
167  return StatusCode::FAILURE;
168  }
169  return StatusCode::SUCCESS;
170 }
171 
175  if ( tuple ) {
176  do {
177  status = m_tupleSvc->readRecord(tuple);
178  if ( status.isSuccess() ) {
179  ISelectStatement* statement = tuple->selector();
180  bool use_it = (statement) ? (*statement)(tuple) : true;
181  if ( use_it ) {
182  return status;
183  }
184  }
185  } while ( status.isSuccess() );
186  }
187  return status;
188 }
189 
193  if ( tuple ) {
194  IRegistry* pReg = tuple->registry();
195  if ( pReg ) {
196  IOpaqueAddress* pAddr = pReg->address();
197  if ( pAddr ) {
198  long* ip = (long*)pAddr->ipar();
199  do {
200  if ( ip[1] > 1 ) {
201  ip[1] -= 2;
202  status = m_tupleSvc->readRecord(tuple);
203  if ( status.isSuccess() ) {
204  ISelectStatement* statement = tuple->selector();
205  bool use_it = (statement) ? (*statement)(tuple) : true;
206  if ( use_it ) {
207  return status;
208  }
209  }
210  }
211  else {
212  return StatusCode::FAILURE;
213  }
214  } while ( status.isSuccess() );
215  }
216  }
217  }
218  return StatusCode::FAILURE;
219 }
220 
221 // Connect collection to selector
223 {
224  if ( ctxt ) {
226  if ( status.isSuccess() ) {
227  status = connectTuple(m_cntName, m_itemName, ctxt->tuple, ctxt->item);
228  if ( status.isSuccess() ) {
229  status = connectStatement(m_statement, m_criteria, ctxt->tuple);
230  if ( status.isSuccess() ) {
231  *(ctxt->item) = 0;
232  return status;
233  }
234  }
235  }
236  return status;
237  }
238  return StatusCode::FAILURE;
239 }
240 
241 // Finalize service
243  // release services
244  m_pAddrCreator = nullptr;
245  m_tupleSvc = nullptr;
246  return Service::finalize();
247 }
248 
251 EventCollectionSelector::createContext(Context*& refpCtxt) const
252 {
253  refpCtxt = nullptr;
254  std::unique_ptr<MyContextType> ctxt(new MyContextType());
255  StatusCode status = connectCollection(ctxt.get());
256  if( !status.isSuccess() ) {
257  MsgStream log(msgSvc(), name());
258  log << MSG::ERROR << "Unable to connect Collection file \"" << m_database << "\"" << endmsg;
259  return status;
260  }
261  refpCtxt = ctxt.release();
262  return StatusCode::SUCCESS;
263 }
264 
267 EventCollectionSelector::next(Context& refCtxt) const
268 {
269  return next(refCtxt, 1);
270 }
271 
274 EventCollectionSelector::next(Context& refCtxt,int jump) const
275 {
276  MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
277  if ( ctxt ) {
278  *(ctxt->item) = ctxt->addressBuffer;
280  for ( int i=0; i<jump && sc.isSuccess(); ++i ) {
281  sc = getNextRecord(ctxt->tuple);
282  }
283  return sc;
284  }
285  return StatusCode::FAILURE;
286 }
287 
290 EventCollectionSelector::previous(Context& refCtxt) const
291 {
292  return previous(refCtxt, 1);
293 }
294 
297 EventCollectionSelector::previous(Context& refCtxt,int jump) const
298 {
299  MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
300  if ( ctxt ) {
301  *(ctxt->item) = ctxt->addressBuffer;
303  for ( int i=0; i<jump && sc.isSuccess(); ++i ) {
304  sc = getPreviousRecord(ctxt->tuple);
305  }
306  return sc;
307  }
308  return StatusCode::FAILURE;
309 }
310 
313 EventCollectionSelector::rewind(Context& /* refCtxt */ ) const
314 {
315  return StatusCode::FAILURE;
316 }
317 
320 EventCollectionSelector::createAddress(const Context& refCtxt, IOpaqueAddress*& refpAddr) const
321 {
322  const MyContextType* ctxt = dynamic_cast<const MyContextType*>(&refCtxt);
323  if ( ctxt ) {
324  IOpaqueAddress* pA = *(ctxt->item);
325  if ( pA ) {
326  IOpaqueAddress* pAddress = nullptr;
328  pA->clID(),
329  pA->par(),
330  pA->ipar(),
331  pAddress);
332  if ( status.isSuccess() ) {
333  refpAddr = pAddress;
334  return StatusCode::SUCCESS;
335  }
336  else {
337  MsgStream log(msgSvc(), name());
338  log << MSG::ERROR << "Failed to access " << pA->par()[0]
339  << ":" << pA->par()[1]
340  << " SvcTyp:" << long(pA->svcType())
341  << " CLID:" << pA->clID()
342  << endmsg;
343  }
344  }
345  }
346  return StatusCode::FAILURE;
347 }
348 
352 {
353  MyContextType *ctxt = dynamic_cast<MyContextType*>(refCtxt);
354  if ( ctxt ) {
355  delete ctxt;
356  ctxt = nullptr;
357  return StatusCode::SUCCESS;
358  }
359  return StatusCode::FAILURE;
360 }
361 
365 EventCollectionSelector::resetCriteria(const std::string& cr,Context& refCtxt) const
366 {
367  MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
368  if ( ctxt ) {
369  ctxt->criteria = cr;
370  return StatusCode::SUCCESS;
371  }
372  return StatusCode::FAILURE;
373 }
374 
377 EventCollectionSelector::last(Context& /* refCtxt */ ) const
378 {
379  return StatusCode::FAILURE;
380 }
SmartIF< INTupleSvc > m_tupleSvc
Reference to Tuple service.
virtual const std::string * par() const =0
Retrieve String parameters.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:63
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:14
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
virtual StatusCode getNextRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
StatusCode finalize() override
Definition: Service.cpp:188
A select statement can either contain.
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(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:76
unsigned long release() override
release reference to object
virtual StatusCode connectDataSource(const std::string &db, const std::string &typ) const
Connect collection's data source to selector.
~EventCollectionContext() override
Standard destructor.
std::string m_criteria
Criteria.
StatusCode previous(Context &refCtxt) const override
Get previous iteration item from the event loop context.
Generic Transient Address.
virtual ISelectStatement * selector()=0
Access selector.
virtual long svcType() const =0
Retrieve service type.
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:74
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
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.
std::string m_tupleSvcName
Name of the event collection service name.
std::string m_cntName
Container name.
ListName::const_iterator fileIterator()
const std::string & currentInput() const
StatusCode last(Context &refCtxt) const override
Access last item in the iteration.
StatusCode next(Context &refCtxt) const override
Get next iteration item from the event loop context.
virtual const CLID & clID() const =0
Retrieve class information from link.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::string m_itemName
Item name.
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
void setFileIterator(ListName::const_iterator new_iter)
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...
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
std::string m_dbType
Database type identifier.
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)
std::string m_dbSvc
Database service (exclusive property with db type)
#define class
std::string m_database
Datafile name.
virtual StatusCode getPreviousRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
Definition of class EventCollectionSelector.
virtual StatusCode readRecord(NTuple::Tuple *tuple)=0
Read single record from N tuple.
tuple item
print s1,s2
Definition: ana.py:146
NTuple::Item< IOpaqueAddress * > * item
StatusCode initialize() override
Service override: Initialize service.
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
StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const override
Create new Opaque address corresponding to the current record.
std::string m_authentication
Authentication string (if needed)
EventCollectionSelector(const std::string &name, ISvcLocator *svcloc)
Standard Constructor.
Opaque address interface definition.
StatusCode rewind(Context &refCtxt) const override
Rewind the dataset.
void ignore() const
Definition: StatusCode.h:108
SmartIF< IAddressCreator > m_pAddrCreator
virtual StatusCode attachSelector(ISelectStatement *sel)=0
Attach selector.
virtual StatusCode connectCollection(MyContextType *ctxt) const
Connect collection to selector.
ListName::const_iterator m_fileIterator
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
list i
Definition: ana.py:128
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
void setCriteria(const std::string &crit)
std::string m_statement
Selector name.
StatusCode createContext(Context *&refpCtxt) const override
Create a new event loop context.
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.
virtual StatusCode retrieveObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.