The Gaudi Framework  v32r2 (46d42edc)
EventCollectionSelector.cpp
Go to the documentation of this file.
1 // Include files
10 #include "GaudiKernel/NTuple.h"
12 #include "GaudiKernel/SmartIF.h"
13 
15 
16 #include <memory>
17 
19 
20 
26 public:
28 
29 private:
30  GenericAddress* m_pAddressBuffer = nullptr;
31  const EventCollectionSelector* m_pSelector = nullptr;
34  ListName::const_iterator m_fileIterator;
36 
37 public:
41  ~EventCollectionContext() override;
42  const std::string& currentInput() const { return m_currentInput; }
43  void setCurrentInput( const std::string& v ) { m_currentInput = v; }
44  ListName& files() { return m_files; }
45  void* identifier() const override { return (void*)m_pSelector; }
46  void setCriteria( const std::string& crit ) { m_criteria = crit; }
47  ListName::const_iterator fileIterator() { return m_fileIterator; }
48  void setFileIterator( ListName::const_iterator new_iter ) { m_fileIterator = new_iter; }
49 };
50 
52 
53 EventCollectionContext::EventCollectionContext( const EventCollectionSelector* pSelector ) : m_pSelector( pSelector ) {
56 }
57 
58 // IService implementation: Db event selector override
60  // Initialize base class
62  if ( !status.isSuccess() ) {
63  error() << "Error initializing base class Service!" << endmsg;
64  return status;
65  }
66  m_pAddrCreator = serviceLocator()->service( "EventPersistencySvc" );
67  if ( !m_pAddrCreator ) {
68  error() << "Unable to locate IAddressCreator interface of "
69  << "EventPersistencySvc" << endmsg;
70  return status;
71  }
73  if ( !m_tupleSvc ) {
74  error() << "Unable to locate INTupleSvc interface of " << m_tupleSvcName << endmsg;
75  return status;
76  }
77  return status;
78 }
79 
80 // Connect collection to selector
84  if ( svc && !db.empty() ) {
85  std::string ident = name() + ' ';
86  ident += "DATAFILE='" + m_database.value().substr( 5 ) + "' ";
87  if ( !m_dbSvc.empty() )
88  ident += "SVC='" + m_dbSvc + "' ";
89  else
90  ident += "TYP='" + m_dbType + "' ";
91  ident += "OPT='READ' ";
92  if ( m_authentication.length() > 0 ) { ident += "AUTH='" + m_authentication + "' "; }
93  status = svc->connect( ident );
94  }
95  return status;
96 }
97 
100  NTuple::Tuple*& tup, NTuple::Item<IOpaqueAddress*>*& item ) const {
101  std::string top = "/NTUPLES/" + name() + '/' + nam;
102  StatusCode status = m_tupleSvc->retrieveObject( top, (DataObject*&)tup );
103  if ( status.isSuccess() ) {
104  item = new NTuple::Item<IOpaqueAddress*>();
105  status = tup->item( itName, *item );
106  if ( status.isSuccess() ) return status;
107  error() << "Item " << itName << " is not part of the collection:" << top << endmsg;
108  delete item;
109  item = nullptr;
110  } else {
111  error() << "Cannot connect to collection:" << top << endmsg;
112  }
113  tup = nullptr;
114  return status;
115 }
116 
119  INTuple* tuple ) const {
120  std::string seltyp = typ;
121  if ( !seltyp.empty() || !crit.empty() ) {
122  if ( !crit.empty() && seltyp.length() == 0 ) seltyp = "NTuple::Selector";
123  SmartIF<ISelectStatement> stmt( ObjFactory::create( seltyp, serviceLocator() ).release() );
124  if ( stmt ) {
125  if ( !crit.empty() ) stmt->setCriteria( crit );
126  tuple->attachSelector( stmt ).ignore();
127  return StatusCode::SUCCESS;
128  }
129  return StatusCode::FAILURE;
130  }
131  return StatusCode::SUCCESS;
132 }
133 
137  if ( tuple ) {
138  do {
139  status = m_tupleSvc->readRecord( tuple );
140  if ( status.isSuccess() ) {
141  ISelectStatement* statement = tuple->selector();
142  bool use_it = ( statement ) ? ( *statement )( tuple ) : true;
143  if ( use_it ) { return status; }
144  }
145  } while ( status.isSuccess() );
146  }
147  return status;
148 }
149 
153  if ( tuple ) {
154  IRegistry* pReg = tuple->registry();
155  if ( pReg ) {
156  IOpaqueAddress* pAddr = pReg->address();
157  if ( pAddr ) {
158  long* ip = (long*)pAddr->ipar();
159  do {
160  if ( ip[1] > 1 ) {
161  ip[1] -= 2;
162  status = m_tupleSvc->readRecord( tuple );
163  if ( status.isSuccess() ) {
164  ISelectStatement* statement = tuple->selector();
165  bool use_it = ( statement ) ? ( *statement )( tuple ) : true;
166  if ( use_it ) { return status; }
167  }
168  } else {
169  return StatusCode::FAILURE;
170  }
171  } while ( status.isSuccess() );
172  }
173  }
174  }
175  return StatusCode::FAILURE;
176 }
177 
178 // Connect collection to selector
180  if ( ctxt ) {
182  if ( status.isSuccess() ) {
183  status = connectTuple( m_cntName, m_itemName, ctxt->tuple, ctxt->item );
184  if ( status.isSuccess() ) {
185  status = connectStatement( m_statement, m_criteria, ctxt->tuple );
186  if ( status.isSuccess() ) {
187  *( ctxt->item ) = 0;
188  return status;
189  }
190  }
191  }
192  return status;
193  }
194  return StatusCode::FAILURE;
195 }
196 
197 // Finalize service
199  // release services
200  m_pAddrCreator = nullptr;
201  m_tupleSvc = nullptr;
202  return Service::finalize();
203 }
204 
205 // FIXME - method below generates leak errors with sanitizer
206 //
207 // clang-format off
208 // Direct leak of 48 byte(s) in 2 object(s) allocated from:
209 // #0 0x7f36dab12da8 in operator new(unsigned long) /afs/cern.ch/cms/CAF/CMSCOMM/COMM_ECAL/dkonst/GCC/build/contrib/gcc-8.2.0/src/gcc/8.2.0/libsanitizer/lsan/lsan_interceptors.cc:229
210 // #1 0x7f36cca8cf7e in EventCollectionSelector::connectTuple(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, NTuple::Tuple*&, NTuple::Item<IOpaqueAddress*>*&) const ../GaudiCoreSvc/src/EventSelector/EventCollectionSelector.cpp:104
211 // #2 0x7f36cca8c27f in EventCollectionSelector::connectCollection(EventCollectionSelector::MyContextType*) const ../GaudiCoreSvc/src/EventSelector/EventCollectionSelector.cpp:183
212 // #3 0x7f36cca8c808 in EventCollectionSelector::createContext(IEvtSelector::Context*&) const ../GaudiCoreSvc/src/EventSelector/EventCollectionSelector.cpp:209
213 // clang-format on
214 //
215 // These leaks are currently suppressed in Gaudi/job/Gaudi-LSan.supp - remove entry there to reactivate
216 
219  refpCtxt = nullptr;
220  auto ctxt = std::make_unique<MyContextType>();
221  StatusCode status = connectCollection( ctxt.get() );
222  if ( !status.isSuccess() ) {
223  error() << "Unable to connect Collection file \"" << m_database << "\"" << endmsg;
224  return status;
225  }
226  refpCtxt = ctxt.release();
227  return StatusCode::SUCCESS;
228 }
229 
231 StatusCode EventCollectionSelector::next( Context& refCtxt ) const { return next( refCtxt, 1 ); }
232 
234 StatusCode EventCollectionSelector::next( Context& refCtxt, int jump ) const {
235  MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
236  if ( ctxt ) {
237  *( ctxt->item ) = ctxt->addressBuffer;
239  for ( int i = 0; i < jump && sc.isSuccess(); ++i ) { sc = getNextRecord( ctxt->tuple ); }
240  return sc;
241  }
242  return StatusCode::FAILURE;
243 }
244 
246 StatusCode EventCollectionSelector::previous( Context& refCtxt ) const { return previous( refCtxt, 1 ); }
247 
249 StatusCode EventCollectionSelector::previous( Context& refCtxt, int jump ) const {
250  MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
251  if ( ctxt ) {
252  *( ctxt->item ) = ctxt->addressBuffer;
254  for ( int i = 0; i < jump && sc.isSuccess(); ++i ) { sc = getPreviousRecord( ctxt->tuple ); }
255  return sc;
256  }
257  return StatusCode::FAILURE;
258 }
259 
261 StatusCode EventCollectionSelector::rewind( Context& /* refCtxt */ ) const { return StatusCode::FAILURE; }
262 
264 StatusCode EventCollectionSelector::createAddress( const Context& refCtxt, IOpaqueAddress*& refpAddr ) const {
265  const MyContextType* ctxt = dynamic_cast<const MyContextType*>( &refCtxt );
266  if ( ctxt ) {
267  IOpaqueAddress* pA = *( ctxt->item );
268  if ( pA ) {
269  IOpaqueAddress* pAddress = nullptr;
270  StatusCode status = m_pAddrCreator->createAddress( pA->svcType(), pA->clID(), pA->par(), pA->ipar(), pAddress );
271  if ( status.isSuccess() ) {
272  refpAddr = pAddress;
273  return StatusCode::SUCCESS;
274  } else {
275  error() << "Failed to access " << pA->par()[0] << ":" << pA->par()[1] << " SvcTyp:" << long( pA->svcType() )
276  << " CLID:" << pA->clID() << endmsg;
277  }
278  }
279  }
280  return StatusCode::FAILURE;
281 }
282 
285  MyContextType* ctxt = dynamic_cast<MyContextType*>( refCtxt );
286  if ( ctxt ) {
287  delete ctxt;
288  ctxt = nullptr;
289  return StatusCode::SUCCESS;
290  }
291  return StatusCode::FAILURE;
292 }
293 
296 StatusCode EventCollectionSelector::resetCriteria( const std::string& cr, Context& refCtxt ) const {
297  MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
298  if ( ctxt ) {
299  ctxt->criteria = cr;
300  return StatusCode::SUCCESS;
301  }
302  return StatusCode::FAILURE;
303 }
304 
306 StatusCode EventCollectionSelector::last( Context& /* refCtxt */ ) const { return StatusCode::FAILURE; }
SmartIF< INTupleSvc > m_tupleSvc
Reference to Tuple service.
virtual const std::string * par() const =0
Retrieve String parameters.
StatusCode initialize() override
Definition: Service.cpp:60
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:15
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:277
T empty(T... args)
virtual StatusCode connectStatement(const std::string &typ, const std::string &crit, INTuple *tuple) const
Connect selection statement to refine data access.
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
StatusCode finalize() override
Definition: Service.cpp:164
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.
unsigned long release() override
release reference to object
const std::string & currentInput() const
~EventCollectionContext() override
Standard destructor.
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Gaudi::Property< std::string > m_authentication
StatusCode previous(Context &refCtxt) const override
Get previous iteration item from the event loop context.
virtual StatusCode getPreviousRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
virtual StatusCode connectCollection(MyContextType *ctxt) const
Connect collection to selector.
virtual StatusCode getNextRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
#define class
Generic Transient Address.
virtual ISelectStatement * selector()=0
Access selector.
virtual long svcType() const =0
Retrieve service type.
Gaudi::Property< std::string > m_cntName
Gaudi::Property< std::string > m_database
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
Gaudi::Property< std::string > m_tupleSvcName
STL class.
#define DECLARE_COMPONENT(type)
NTuple interface class definition.
Definition: INTuple.h:81
StatusCode finalize() override
Service override: Finalize Service.
virtual StatusCode connect(const std::string &logon, std::string &identifier)=0
Connect data source.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
ListName::const_iterator fileIterator()
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
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:50
void setFileIterator(ListName::const_iterator new_iter)
virtual StatusCode connectDataSource(const std::string &db, const std::string &typ) const
Connect collection's data source to selector.
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...
Gaudi::Property< std::string > m_statement
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
bool isSuccess() const
Definition: StatusCode.h:267
Gaudi::Property< std::string > m_dbType
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
virtual StatusCode connectTuple(const std::string &nam, const std::string &itName, NTuple::Tuple *&tup, NTuple::Item< IOpaqueAddress * > *&item) const
Connect to existing N-tuple.
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:375
Gaudi::Property< std::string > m_criteria
Gaudi::Property< std::string > m_dbSvc
void setCurrentInput(const std::string &v)
T length(T... args)
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
virtual StatusCode retrieveObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
Definition of class EventCollectionSelector.
virtual StatusCode readRecord(NTuple::Tuple *tuple)=0
Read single record from N tuple.
constexpr static const auto FAILURE
Definition: StatusCode.h:86
NTuple::Item< IOpaqueAddress * > * item
StatusCode initialize() override
Service override: Initialize service.
std::list< std::string > ListName
StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const override
Create new Opaque address corresponding to the current record.
Opaque address interface definition.
StatusCode rewind(Context &refCtxt) const override
Rewind the dataset.
SmartIF< IAddressCreator > m_pAddrCreator
virtual StatusCode attachSelector(ISelectStatement *sel)=0
Attach 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
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:192
void setCriteria(const std::string &crit)
Gaudi::Property< std::string > m_itemName
StatusCode createContext(Context *&refpCtxt) const override
Create a new event loop context.
void * identifier() const override