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 // Standard constructor
76  : base_class(name, svcloc)
77 {
78  declareProperty("CnvService", m_tupleSvcName = "EvtTupleSvc");
79  declareProperty("Authentication",m_authentication= "");
80  declareProperty("Container", m_cntName = "B2PiPi");
81  declareProperty("Item", m_itemName = "Address");
82  declareProperty("Criteria", m_criteria = "");
83  declareProperty("DB", m_database = "");
84  declareProperty("DbType", m_dbType = "");
85  declareProperty("DbService", m_dbSvc = "");
86  declareProperty("Function", m_statement= "NTuple::Selector");
87 }
88 
89 // IService implementation: Db event selector override
91  // Initialize base class
93  if ( !status.isSuccess() ) {
94  error() << "Error initializing base class Service!" << endmsg;
95  return status;
96  }
97  m_pAddrCreator = serviceLocator()->service("EventPersistencySvc");
98  if(!m_pAddrCreator) {
99  error() << "Unable to locate IAddressCreator interface of " << "EventPersistencySvc" << endmsg;
100  return status;
101  }
103  if( !m_tupleSvc ) {
104  error() << "Unable to locate INTupleSvc interface of " << m_tupleSvcName << endmsg;
105  return status;
106  }
107  return status;
108 }
109 
110 // Connect collection to selector
115  if ( svc && !db.empty() ) {
116  std::string ident = name() + ' ';
117  ident += "DATAFILE='" + m_database.substr(5) + "' ";
118  if ( !m_dbSvc.empty() )
119  ident += "SVC='" + m_dbSvc + "' ";
120  else
121  ident += "TYP='" + m_dbType + "' ";
122  ident += "OPT='READ' ";
123  if ( m_authentication.length() > 0 ) {
124  ident += "AUTH='" + m_authentication + "' ";
125  }
126  status = svc->connect(ident);
127  }
128  return status;
129 }
130 
134  std::string top = "/NTUPLES/" + name() + '/' + nam;
135  StatusCode status = m_tupleSvc->retrieveObject(top, (DataObject*&)tup);
136  if ( status.isSuccess() ) {
137  item = new NTuple::Item<IOpaqueAddress*>();
138  status = tup->item(itName, *item);
139  if ( status.isSuccess() ) return status;
140  error() << "Item " << itName << " is not part of the collection:" << top << endmsg;
141  delete item;
142  item = nullptr;
143  }
144  else {
145  error() << "Cannot connect to collection:" << top << endmsg;
146  }
147  tup = nullptr;
148  return status;
149 }
150 
154  std::string seltyp = typ;
155  if ( !seltyp.empty() || !crit.empty() ) {
156  if ( !crit.empty() && seltyp.length() == 0 ) seltyp = "NTuple::Selector";
157  SmartIF<ISelectStatement> stmt(ObjFactory::create(seltyp, serviceLocator()));
158  if ( stmt ) {
159  if ( !crit.empty() ) stmt->setCriteria(crit);
160  tuple->attachSelector(stmt).ignore();
161  return StatusCode::SUCCESS;
162  }
163  return StatusCode::FAILURE;
164  }
165  return StatusCode::SUCCESS;
166 }
167 
171  if ( tuple ) {
172  do {
173  status = m_tupleSvc->readRecord(tuple);
174  if ( status.isSuccess() ) {
175  ISelectStatement* statement = tuple->selector();
176  bool use_it = (statement) ? (*statement)(tuple) : true;
177  if ( use_it ) {
178  return status;
179  }
180  }
181  } while ( status.isSuccess() );
182  }
183  return status;
184 }
185 
189  if ( tuple ) {
190  IRegistry* pReg = tuple->registry();
191  if ( pReg ) {
192  IOpaqueAddress* pAddr = pReg->address();
193  if ( pAddr ) {
194  long* ip = (long*)pAddr->ipar();
195  do {
196  if ( ip[1] > 1 ) {
197  ip[1] -= 2;
198  status = m_tupleSvc->readRecord(tuple);
199  if ( status.isSuccess() ) {
200  ISelectStatement* statement = tuple->selector();
201  bool use_it = (statement) ? (*statement)(tuple) : true;
202  if ( use_it ) {
203  return status;
204  }
205  }
206  }
207  else {
208  return StatusCode::FAILURE;
209  }
210  } while ( status.isSuccess() );
211  }
212  }
213  }
214  return StatusCode::FAILURE;
215 }
216 
217 // Connect collection to selector
219 {
220  if ( ctxt ) {
222  if ( status.isSuccess() ) {
223  status = connectTuple(m_cntName, m_itemName, ctxt->tuple, ctxt->item);
224  if ( status.isSuccess() ) {
225  status = connectStatement(m_statement, m_criteria, ctxt->tuple);
226  if ( status.isSuccess() ) {
227  *(ctxt->item) = 0;
228  return status;
229  }
230  }
231  }
232  return status;
233  }
234  return StatusCode::FAILURE;
235 }
236 
237 // Finalize service
239  // release services
240  m_pAddrCreator = nullptr;
241  m_tupleSvc = nullptr;
242  return Service::finalize();
243 }
244 
247 EventCollectionSelector::createContext(Context*& refpCtxt) const
248 {
249  refpCtxt = nullptr;
251  StatusCode status = connectCollection(ctxt.get());
252  if( !status.isSuccess() ) {
253  error() << "Unable to connect Collection file \"" << m_database << "\"" << endmsg;
254  return status;
255  }
256  refpCtxt = ctxt.release();
257  return StatusCode::SUCCESS;
258 }
259 
262 EventCollectionSelector::next(Context& refCtxt) const
263 {
264  return next(refCtxt, 1);
265 }
266 
269 EventCollectionSelector::next(Context& refCtxt,int jump) const
270 {
271  MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
272  if ( ctxt ) {
273  *(ctxt->item) = ctxt->addressBuffer;
275  for ( int i=0; i<jump && sc.isSuccess(); ++i ) {
276  sc = getNextRecord(ctxt->tuple);
277  }
278  return sc;
279  }
280  return StatusCode::FAILURE;
281 }
282 
285 EventCollectionSelector::previous(Context& refCtxt) const
286 {
287  return previous(refCtxt, 1);
288 }
289 
292 EventCollectionSelector::previous(Context& refCtxt,int jump) const
293 {
294  MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
295  if ( ctxt ) {
296  *(ctxt->item) = ctxt->addressBuffer;
298  for ( int i=0; i<jump && sc.isSuccess(); ++i ) {
299  sc = getPreviousRecord(ctxt->tuple);
300  }
301  return sc;
302  }
303  return StatusCode::FAILURE;
304 }
305 
308 EventCollectionSelector::rewind(Context& /* refCtxt */ ) const
309 {
310  return StatusCode::FAILURE;
311 }
312 
315 EventCollectionSelector::createAddress(const Context& refCtxt, IOpaqueAddress*& refpAddr) const
316 {
317  const MyContextType* ctxt = dynamic_cast<const MyContextType*>(&refCtxt);
318  if ( ctxt ) {
319  IOpaqueAddress* pA = *(ctxt->item);
320  if ( pA ) {
321  IOpaqueAddress* pAddress = nullptr;
323  pA->clID(),
324  pA->par(),
325  pA->ipar(),
326  pAddress);
327  if ( status.isSuccess() ) {
328  refpAddr = pAddress;
329  return StatusCode::SUCCESS;
330  }
331  else {
332  error() << "Failed to access " << pA->par()[0]
333  << ":" << pA->par()[1]
334  << " SvcTyp:" << long(pA->svcType())
335  << " CLID:" << pA->clID()
336  << endmsg;
337  }
338  }
339  }
340  return StatusCode::FAILURE;
341 }
342 
346 {
347  MyContextType *ctxt = dynamic_cast<MyContextType*>(refCtxt);
348  if ( ctxt ) {
349  delete ctxt;
350  ctxt = nullptr;
351  return StatusCode::SUCCESS;
352  }
353  return StatusCode::FAILURE;
354 }
355 
359 EventCollectionSelector::resetCriteria(const std::string& cr,Context& refCtxt) const
360 {
361  MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
362  if ( ctxt ) {
363  ctxt->criteria = cr;
364  return StatusCode::SUCCESS;
365  }
366  return StatusCode::FAILURE;
367 }
368 
371 EventCollectionSelector::last(Context& /* refCtxt */ ) const
372 {
373  return StatusCode::FAILURE;
374 }
SmartIF< INTupleSvc > m_tupleSvc
Reference to Tuple service.
virtual const std::string * par() const =0
Retrieve String parameters.
StatusCode initialize() override
Definition: Service.cpp:68
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:14
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:324
T empty(T...args)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
virtual StatusCode getNextRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
StatusCode finalize() override
Definition: Service.cpp:193
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
T release(T...args)
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
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.
std::string m_tupleSvcName
Name of the event collection service name.
std::string m_cntName
Container name.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:319
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
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.
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)
T get(T...args)
std::string m_dbSvc
Database service (exclusive property with db type)
T length(T...args)
#define class
STL class.
std::string m_database
Datafile name.
virtual StatusCode getPreviousRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
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.
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
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
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:215
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.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
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.