All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EventCollectionSelector.cpp
Go to the documentation of this file.
1 // Include files
2 #include "GaudiKernel/SmartIF.h"
12 #include "GaudiKernel/NTuple.h"
14 
16 
17 #include <memory>
18 
20 
21 
27 public:
28  typedef std::list<std::string> ListName;
29 private:
32  ListName m_files;
33  std::string m_criteria;
34  ListName::const_iterator m_fileIterator;
35  std::string m_currentInput;
36 
37 public:
41  virtual ~EventCollectionContext();
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  virtual void* identifier() const {
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 
91 }
92 
93 // IService implementation: Db event selector override
95  // Initialize base class
97  MsgStream log(msgSvc(), name());
98  if ( !status.isSuccess() ) {
99  log << MSG::ERROR << "Error initializing base class Service!" << endmsg;
100  return status;
101  }
102  m_pAddrCreator = serviceLocator()->service("EventPersistencySvc");
103  if(!m_pAddrCreator.isValid()) {
104  log << MSG::ERROR << "Unable to locate IAddressCreator interface of " << "EventPersistencySvc" << endmsg;
105  return status;
106  }
108  if( !m_tupleSvc.isValid() ) {
109  log << MSG::ERROR << "Unable to locate INTupleSvc interface of " << m_tupleSvcName << endmsg;
110  return status;
111  }
112  return status;
113 }
114 
115 // Connect collection to selector
117 EventCollectionSelector::connectDataSource(const std::string& db, const std::string& /* typ */ ) const {
120  if ( svc.isValid( ) && db.length() > 0 ) {
121  std::string ident = name() + ' ';
122  ident += "DATAFILE='" + m_database.substr(5,m_database.length()) + "' ";
123  if ( !m_dbSvc.empty() )
124  ident += "SVC='" + m_dbSvc + "' ";
125  else
126  ident += "TYP='" + m_dbType + "' ";
127  ident += "OPT='READ' ";
128  if ( m_authentication.length() > 0 ) {
129  ident += "AUTH='" + m_authentication + "' ";
130  }
131  status = svc->connect(ident);
132  }
133  return status;
134 }
135 
138 EventCollectionSelector::connectTuple(const std::string& nam, const std::string& itName, NTuple::Tuple*& tup, NTuple::Item<IOpaqueAddress*>*& item) const {
139  std::string top = "/NTUPLES/" + name() + '/' + nam;
140  StatusCode status = m_tupleSvc->retrieveObject(top, (DataObject*&)tup);
141  if ( status.isSuccess() ) {
142  item = new NTuple::Item<IOpaqueAddress*>();
143  status = tup->item(itName, *item);
144  if ( status.isSuccess() ) {
145  return status;
146  }
147  else {
148  MsgStream log(msgSvc(), name());
149  log << MSG::ERROR << "Item " << itName << " is not part of the collection:" << top << endmsg;
150  }
151  delete item;
152  item = 0;
153  }
154  else {
155  MsgStream err(msgSvc(), name());
156  err << MSG::ERROR << "Cannot connect to collection:" << top << endmsg;
157  }
158  tup = 0;
159  return status;
160 }
161 
164 EventCollectionSelector::connectStatement(const std::string& typ, const std::string& crit, INTuple* tuple) const {
165  std::string seltyp = typ;
166  if ( seltyp.length() > 0 || crit.length() > 0 ) {
167  if ( crit.length() > 0 && seltyp.length() == 0 ) seltyp = "NTuple::Selector";
168  SmartIF<ISelectStatement> stmt(ObjFactory::create(seltyp, serviceLocator()));
169  if ( stmt.isValid( ) ) {
170  if ( crit.length() > 0 ) stmt->setCriteria(crit);
171  tuple->attachSelector(stmt).ignore();
172  return StatusCode::SUCCESS;
173  }
174  return StatusCode::FAILURE;
175  }
176  return StatusCode::SUCCESS;
177 }
178 
182  if ( 0 != tuple ) {
183  do {
184  status = m_tupleSvc->readRecord(tuple);
185  if ( status.isSuccess() ) {
186  ISelectStatement* statement = tuple->selector();
187  bool use_it = (statement) ? (*statement)(tuple) : true;
188  if ( use_it ) {
189  return status;
190  }
191  }
192  } while ( status.isSuccess() );
193  }
194  return status;
195 }
196 
200  if ( tuple ) {
201  IRegistry* pReg = tuple->registry();
202  if ( pReg ) {
203  IOpaqueAddress* pAddr = pReg->address();
204  if ( pAddr ) {
205  long* ip = (long*)pAddr->ipar();
206  do {
207  if ( ip[1] > 1 ) {
208  ip[1] -= 2;
209  status = m_tupleSvc->readRecord(tuple);
210  if ( status.isSuccess() ) {
211  ISelectStatement* statement = tuple->selector();
212  bool use_it = (statement) ? (*statement)(tuple) : true;
213  if ( use_it ) {
214  return status;
215  }
216  }
217  }
218  else {
219  return StatusCode::FAILURE;
220  }
221  } while ( status.isSuccess() );
222  }
223  }
224  }
225  return StatusCode::FAILURE;
226 }
227 
228 // Connect collection to selector
230 {
231  if ( ctxt ) {
233  if ( status.isSuccess() ) {
234  status = connectTuple(m_cntName, m_itemName, ctxt->tuple, ctxt->item);
235  if ( status.isSuccess() ) {
236  status = connectStatement(m_statement, m_criteria, ctxt->tuple);
237  if ( status.isSuccess() ) {
238  *(ctxt->item) = 0;
239  return status;
240  }
241  }
242  }
243  return status;
244  }
245  return StatusCode::FAILURE;
246 }
247 
248 // Finalize service
250  // release services
251  m_pAddrCreator = 0;
252  m_tupleSvc = 0;
253  return Service::finalize();
254 }
255 
259 {
260  refpCtxt = 0;
261  std::auto_ptr<MyContextType> ctxt(new MyContextType());
262  StatusCode status = connectCollection(ctxt.get());
263  if( !status.isSuccess() ) {
264  MsgStream log(msgSvc(), name());
265  log << MSG::ERROR << "Unable to connect Collection file \"" << m_database << "\"" << endmsg;
266  return status;
267  }
268  refpCtxt = ctxt.release();
269  return StatusCode::SUCCESS;
270 }
271 
275 {
276  return next(refCtxt, 1);
277 }
278 
281 EventCollectionSelector::next(Context& refCtxt,int jump) const
282 {
283  MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
284  if ( ctxt ) {
285  *(ctxt->item) = ctxt->addressBuffer;
287  for ( int i=0; i<jump && sc.isSuccess(); ++i ) {
288  sc = getNextRecord(ctxt->tuple);
289  }
290  return sc;
291  }
292  return StatusCode::FAILURE;
293 }
294 
298 {
299  return previous(refCtxt, 1);
300 }
301 
305 {
306  MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
307  if ( ctxt ) {
308  *(ctxt->item) = ctxt->addressBuffer;
310  for ( int i=0; i<jump && sc.isSuccess(); ++i ) {
311  sc = getPreviousRecord(ctxt->tuple);
312  }
313  return sc;
314  }
315  return StatusCode::FAILURE;
316 }
317 
321 {
322  return StatusCode::FAILURE;
323 }
324 
328 {
329  const MyContextType* ctxt = dynamic_cast<const MyContextType*>(&refCtxt);
330  if ( ctxt ) {
331  IOpaqueAddress* pA = *(ctxt->item);
332  if ( pA ) {
333  IOpaqueAddress* pAddress = 0;
334  StatusCode status = m_pAddrCreator->createAddress(pA->svcType(),
335  pA->clID(),
336  pA->par(),
337  pA->ipar(),
338  pAddress);
339  if ( status.isSuccess() ) {
340  refpAddr = pAddress;
341  return StatusCode::SUCCESS;
342  }
343  else {
344  MsgStream log(msgSvc(), name());
345  log << MSG::ERROR << "Failed to access " << pA->par()[0]
346  << ":" << pA->par()[1]
347  << " SvcTyp:" << long(pA->svcType())
348  << " CLID:" << pA->clID()
349  << endmsg;
350  }
351  }
352  }
353  return StatusCode::FAILURE;
354 }
355 
359 {
360  MyContextType *ctxt = dynamic_cast<MyContextType*>(refCtxt);
361  if ( ctxt ) {
362  delete ctxt;
363  ctxt = 0;
364  return StatusCode::SUCCESS;
365  }
366  return StatusCode::FAILURE;
367 }
368 
372 EventCollectionSelector::resetCriteria(const std::string& cr,Context& refCtxt) const
373 {
374  MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
375  if ( ctxt ) {
376  ctxt->criteria = cr;
377  return StatusCode::SUCCESS;
378  }
379  return StatusCode::FAILURE;
380 }
381 
385 {
386  return StatusCode::FAILURE;
387 }
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
virtual void * identifier() const
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:26
virtual StatusCode getNextRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
A select statement can either contain.
virtual unsigned long addRef()
Add reference to object.
EventCollectionContext(const EventCollectionSelector *pSelector)
Standard constructor.
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
The Event Selector Interface.
Definition: IEvtSelector.h:19
virtual ~EventCollectionContext()
Standard destructor.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:62
virtual StatusCode releaseContext(Context *&refCtxt) const
Release existing event iteration context.
virtual StatusCode connectDataSource(const std::string &db, const std::string &typ) const
Connect collection's data source to selector.
virtual StatusCode resetCriteria(const std::string &cr, Context &c) const
Will set a new criteria for the selection of the next list of events and will change the state of the...
std::string m_criteria
Criteria.
virtual StatusCode initialize()
Service override: Initialize service.
Generic Transient Address.
virtual StatusCode previous(Context &refCtxt) const
Get previous iteration item from the event loop context.
virtual ISelectStatement * selector()=0
Access selector.
virtual long svcType() const =0
Retrieve service type.
virtual StatusCode createContext(Context *&refpCtxt) const
Create a new event loop context.
virtual StatusCode finalize()
Service override: Finalize Service.
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:69
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
NTuple interface class definition.
Definition: INTuple.h:80
std::string m_tupleSvcName
Name of the event collection service name.
std::string m_cntName
Container name.
ListName::const_iterator fileIterator()
virtual unsigned long release()
release reference to object
const std::string & currentInput() const
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
virtual const CLID & clID() const =0
Retrieve class information from link.
virtual StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const
Create new Opaque address corresponding to the current record.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
std::string m_itemName
Item name.
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
virtual StatusCode last(Context &refCtxt) const
Access last item in the iteration.
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
std::string m_dbType
Database type identifier.
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:331
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:367
void setCurrentInput(const std::string &v)
std::string m_dbSvc
Database service (exclusive property with db type)
const EventCollectionSelector * m_pSelector
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:74
#define 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.
tuple item
print s1,s2
Definition: ana.py:146
Templated class to add the standard messaging functionalities.
NTuple::Item< IOpaqueAddress * > * item
virtual ~EventCollectionSelector()
Standard Destructor.
virtual StatusCode next(Context &refCtxt) const
Get next iteration item from the event loop context.
virtual StatusCode rewind(Context &refCtxt) const
Rewind the dataset.
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
std::string m_authentication
Authentication string (if needed)
EventCollectionSelector(const std::string &name, ISvcLocator *svcloc)
Standard Constructor.
Opaque address interface definition.
void ignore() const
Definition: StatusCode.h:94
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:211
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
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
list i
Definition: ana.py:128
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:199
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
void setCriteria(const std::string &crit)
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:336
std::string m_statement
Selector name.
virtual StatusCode connectStatement(const std::string &typ, const std::string &crit, INTuple *tuple) const
Connect selection statement to refine data access.