The Gaudi Framework  master (37c0b60a)
EventCollectionSelector.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // Include files
16 #include <GaudiKernel/INTupleSvc.h>
17 #include <GaudiKernel/IRegistry.h>
20 #include <GaudiKernel/NTuple.h>
22 #include <GaudiKernel/SmartIF.h>
23 
25 
26 #include <memory>
27 
29 
30 
36 public:
38 
39 private:
40  GenericAddress* m_pAddressBuffer = nullptr;
41  const EventCollectionSelector* m_pSelector = nullptr;
44  ListName::const_iterator m_fileIterator;
46 
47 public:
51  ~EventCollectionContext() override;
55 
56  const std::string& currentInput() const { return m_currentInput; }
57  void setCurrentInput( const std::string& v ) { m_currentInput = v; }
58  ListName& files() { return m_files; }
59  void* identifier() const override { return (void*)m_pSelector; }
60  void setCriteria( const std::string& crit ) { m_criteria = crit; }
61  ListName::const_iterator fileIterator() { return m_fileIterator; }
62  void setFileIterator( ListName::const_iterator new_iter ) { m_fileIterator = new_iter; }
63 };
64 
66 
67 EventCollectionContext::EventCollectionContext( const EventCollectionSelector* pSelector ) : m_pSelector( pSelector ) {
70 }
71 
72 // IService implementation: Db event selector override
74  // Initialize base class
76  if ( !status.isSuccess() ) {
77  error() << "Error initializing base class Service!" << endmsg;
78  return status;
79  }
80  m_pAddrCreator = serviceLocator()->service( "EventPersistencySvc" );
81  if ( !m_pAddrCreator ) {
82  error() << "Unable to locate IAddressCreator interface of "
83  << "EventPersistencySvc" << endmsg;
84  return status;
85  }
87  if ( !m_tupleSvc ) {
88  error() << "Unable to locate INTupleSvc interface of " << m_tupleSvcName << endmsg;
89  return status;
90  }
91  return status;
92 }
93 
94 // Connect collection to selector
98  if ( svc && !db.empty() ) {
99  std::string ident = name() + ' ';
100  ident += "DATAFILE='" + m_database.value().substr( 5 ) + "' ";
101  if ( !m_dbSvc.empty() )
102  ident += "SVC='" + m_dbSvc + "' ";
103  else
104  ident += "TYP='" + m_dbType + "' ";
105  ident += "OPT='READ' ";
106  if ( m_authentication.length() > 0 ) { ident += "AUTH='" + m_authentication + "' "; }
107  status = svc->connect( ident );
108  }
109  return status;
110 }
111 
114  NTuple::Tuple*& tup, NTuple::Item<IOpaqueAddress*>*& item ) const {
115  std::string top = "/NTUPLES/" + name() + '/' + nam;
116  StatusCode status = m_tupleSvc->retrieveObject( top, (DataObject*&)tup );
117  if ( status.isSuccess() ) {
118  item = new NTuple::Item<IOpaqueAddress*>();
119  status = tup->item( itName, *item );
120  if ( status.isSuccess() ) return status;
121  error() << "Item " << itName << " is not part of the collection:" << top << endmsg;
122  delete item;
123  item = nullptr;
124  } else {
125  error() << "Cannot connect to collection:" << top << endmsg;
126  }
127  tup = nullptr;
128  return status;
129 }
130 
133  INTuple* tuple ) const {
134  std::string seltyp = typ;
135  if ( !seltyp.empty() || !crit.empty() ) {
136  if ( !crit.empty() && seltyp.length() == 0 ) seltyp = "NTuple::Selector";
137  SmartIF<ISelectStatement> stmt( ObjFactory::create( seltyp, serviceLocator() ).release() );
138  if ( stmt ) {
139  if ( !crit.empty() ) stmt->setCriteria( crit );
140  tuple->attachSelector( stmt ).ignore();
141  return StatusCode::SUCCESS;
142  }
143  return StatusCode::FAILURE;
144  }
145  return StatusCode::SUCCESS;
146 }
147 
151  if ( tuple ) {
152  do {
153  status = m_tupleSvc->readRecord( tuple );
154  if ( status.isSuccess() ) {
155  ISelectStatement* statement = tuple->selector();
156  bool use_it = ( statement ) ? ( *statement )( tuple ) : true;
157  if ( use_it ) { return status; }
158  }
159  } while ( status.isSuccess() );
160  }
161  return status;
162 }
163 
167  if ( tuple ) {
168  IRegistry* pReg = tuple->registry();
169  if ( pReg ) {
170  IOpaqueAddress* pAddr = pReg->address();
171  if ( pAddr ) {
172  long* ip = (long*)pAddr->ipar();
173  do {
174  if ( ip[1] > 1 ) {
175  ip[1] -= 2;
176  status = m_tupleSvc->readRecord( tuple );
177  if ( status.isSuccess() ) {
178  ISelectStatement* statement = tuple->selector();
179  bool use_it = ( statement ) ? ( *statement )( tuple ) : true;
180  if ( use_it ) { return status; }
181  }
182  } else {
183  return StatusCode::FAILURE;
184  }
185  } while ( status.isSuccess() );
186  }
187  }
188  }
189  return StatusCode::FAILURE;
190 }
191 
192 // Connect collection to selector
194  if ( ctxt ) {
196  if ( status.isSuccess() ) {
197  status = connectTuple( m_cntName, m_itemName, ctxt->tuple, ctxt->item );
198  if ( status.isSuccess() ) {
199  status = connectStatement( m_statement, m_criteria, ctxt->tuple );
200  if ( status.isSuccess() ) {
201  *( ctxt->item ) = 0;
202  return status;
203  }
204  }
205  }
206  return status;
207  }
208  return StatusCode::FAILURE;
209 }
210 
211 // Finalize service
213  // release services
214  m_pAddrCreator = nullptr;
215  m_tupleSvc = nullptr;
216  return Service::finalize();
217 }
218 
219 // FIXME - method below generates leak errors with sanitizer
220 //
221 // clang-format off
222 // Direct leak of 48 byte(s) in 2 object(s) allocated from:
223 // #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
224 // #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
225 // #2 0x7f36cca8c27f in EventCollectionSelector::connectCollection(EventCollectionSelector::MyContextType*) const ../GaudiCoreSvc/src/EventSelector/EventCollectionSelector.cpp:183
226 // #3 0x7f36cca8c808 in EventCollectionSelector::createContext(IEvtSelector::Context*&) const ../GaudiCoreSvc/src/EventSelector/EventCollectionSelector.cpp:209
227 // clang-format on
228 //
229 // These leaks are currently suppressed in Gaudi/job/Gaudi-LSan.supp - remove entry there to reactivate
230 
233  refpCtxt = nullptr;
234  auto ctxt = std::make_unique<MyContextType>();
235  StatusCode status = connectCollection( ctxt.get() );
236  if ( !status.isSuccess() ) {
237  error() << "Unable to connect Collection file \"" << m_database << "\"" << endmsg;
238  return status;
239  }
240  refpCtxt = ctxt.release();
241  return StatusCode::SUCCESS;
242 }
243 
245 StatusCode EventCollectionSelector::next( Context& refCtxt ) const { return next( refCtxt, 1 ); }
246 
248 StatusCode EventCollectionSelector::next( Context& refCtxt, int jump ) const {
249  MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
250  if ( ctxt ) {
251  *( ctxt->item ) = ctxt->addressBuffer;
253  for ( int i = 0; i < jump && sc.isSuccess(); ++i ) { sc = getNextRecord( ctxt->tuple ); }
254  return sc;
255  }
256  return StatusCode::FAILURE;
257 }
258 
260 StatusCode EventCollectionSelector::previous( Context& refCtxt ) const { return previous( refCtxt, 1 ); }
261 
263 StatusCode EventCollectionSelector::previous( Context& refCtxt, int jump ) const {
264  MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
265  if ( ctxt ) {
266  *( ctxt->item ) = ctxt->addressBuffer;
268  for ( int i = 0; i < jump && sc.isSuccess(); ++i ) { sc = getPreviousRecord( ctxt->tuple ); }
269  return sc;
270  }
271  return StatusCode::FAILURE;
272 }
273 
275 StatusCode EventCollectionSelector::rewind( Context& /* refCtxt */ ) const { return StatusCode::FAILURE; }
276 
278 StatusCode EventCollectionSelector::createAddress( const Context& refCtxt, IOpaqueAddress*& refpAddr ) const {
279  const MyContextType* ctxt = dynamic_cast<const MyContextType*>( &refCtxt );
280  if ( ctxt ) {
281  IOpaqueAddress* pA = *( ctxt->item );
282  if ( pA ) {
283  IOpaqueAddress* pAddress = nullptr;
284  StatusCode status = m_pAddrCreator->createAddress( pA->svcType(), pA->clID(), pA->par(), pA->ipar(), pAddress );
285  if ( status.isSuccess() ) {
286  refpAddr = pAddress;
287  return StatusCode::SUCCESS;
288  } else {
289  error() << "Failed to access " << pA->par()[0] << ":" << pA->par()[1] << " SvcTyp:" << long( pA->svcType() )
290  << " CLID:" << pA->clID() << endmsg;
291  }
292  }
293  }
294  return StatusCode::FAILURE;
295 }
296 
299  MyContextType* ctxt = dynamic_cast<MyContextType*>( refCtxt );
300  if ( ctxt ) {
301  delete ctxt;
302  ctxt = nullptr;
303  return StatusCode::SUCCESS;
304  }
305  return StatusCode::FAILURE;
306 }
307 
310 StatusCode EventCollectionSelector::resetCriteria( const std::string& cr, Context& refCtxt ) const {
311  MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
312  if ( ctxt ) {
313  ctxt->criteria = cr;
314  return StatusCode::SUCCESS;
315  }
316  return StatusCode::FAILURE;
317 }
318 
320 StatusCode EventCollectionSelector::last( Context& /* refCtxt */ ) const { return StatusCode::FAILURE; }
EventCollectionSelector::resetCriteria
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...
Definition: EventCollectionSelector.cpp:310
EventCollectionSelector::finalize
StatusCode finalize() override
Service override: Finalize Service.
Definition: EventCollectionSelector.cpp:212
EventCollectionSelector::m_dbType
Gaudi::Property< std::string > m_dbType
Definition: EventCollectionSelector.h:98
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
std::string
STL class.
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
EventCollectionSelector::connectDataSource
virtual StatusCode connectDataSource(const std::string &db, const std::string &typ) const
Connect collection's data source to selector.
Definition: EventCollectionSelector.cpp:95
EventCollectionContext::m_currentInput
std::string m_currentInput
Definition: EventCollectionSelector.cpp:45
EventSelectorDataStream.h
std::list< std::string >
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
EventCollectionSelector::MyContextType::criteria
std::string criteria
Definition: EventCollectionSelector.h:56
EventCollectionSelector::m_tupleSvc
SmartIF< INTupleSvc > m_tupleSvc
Reference to Tuple service.
Definition: EventCollectionSelector.h:87
EventCollectionSelector::MyContextType::addressBuffer
IOpaqueAddress * addressBuffer
Definition: EventCollectionSelector.h:59
EventCollectionSelector::MyContextType
Definition: EventCollectionSelector.h:54
EventCollectionSelector.h
IOpaqueAddress
Definition: IOpaqueAddress.h:33
EventCollectionContext::m_criteria
std::string m_criteria
Definition: EventCollectionSelector.cpp:43
std::string::length
T length(T... args)
IOpaqueAddress::svcType
virtual long svcType() const =0
Retrieve service type.
IAddressCreator.h
IEvtSelector
Definition: IEvtSelector.h:28
EventCollectionContext::EventCollectionContext
EventCollectionContext(const EventCollectionSelector *pSelector)
Standard constructor.
Definition: EventCollectionSelector.cpp:67
EventCollectionSelector::m_database
Gaudi::Property< std::string > m_database
Definition: EventCollectionSelector.h:97
EventCollectionContext::m_pAddressBuffer
GenericAddress * m_pAddressBuffer
Definition: EventCollectionSelector.cpp:40
ISelectStatement
Definition: ISelectStatement.h:42
conf.release
string release
Definition: conf.py:27
EventCollectionSelector::m_itemName
Gaudi::Property< std::string > m_itemName
Definition: EventCollectionSelector.h:95
IRegistry
Definition: IRegistry.h:32
class
#define class
Definition: HistogramPersistencySvc.cpp:54
EventCollectionContext::EventCollectionContext
EventCollectionContext(const EventCollectionContext &)=delete
No copy.
ObjectFactory.h
EventCollectionSelector::MyContextType::tuple
NTuple::Tuple * tuple
Definition: EventCollectionSelector.h:57
EventCollectionSelector::next
StatusCode next(Context &refCtxt) const override
Get next iteration item from the event loop context.
Definition: EventCollectionSelector.cpp:245
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:222
EventCollectionSelector::createAddress
StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const override
Create new Opaque address corresponding to the current record.
Definition: EventCollectionSelector.cpp:278
EventCollectionSelector::initialize
StatusCode initialize() override
Service override: Initialize service.
Definition: EventCollectionSelector.cpp:73
EventCollectionContext::m_files
ListName m_files
Definition: EventCollectionSelector.cpp:42
GenericAddress.h
IDataSourceMgr.h
SmartIF.h
GenericAddress
Definition: GenericAddress.h:30
EventCollectionSelector::rewind
StatusCode rewind(Context &refCtxt) const override
Rewind the dataset.
Definition: EventCollectionSelector.cpp:275
EventCollectionSelector::m_pAddrCreator
SmartIF< IAddressCreator > m_pAddrCreator
Definition: EventCollectionSelector.h:88
INTuple::attachSelector
virtual StatusCode attachSelector(ISelectStatement *sel)=0
Attach selector.
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:332
StatusCode
Definition: StatusCode.h:65
IOpaqueAddress::clID
virtual const CLID & clID() const =0
Retrieve class information from link.
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
NTuple.h
EventCollectionSelector::MyContextType::item
NTuple::Item< IOpaqueAddress * > * item
Definition: EventCollectionSelector.h:58
EventCollectionSelector::previous
StatusCode previous(Context &refCtxt) const override
Get previous iteration item from the event loop context.
Definition: EventCollectionSelector.cpp:260
INTuple
Definition: INTuple.h:91
ISelectStatement.h
SmartIF
Definition: IConverter.h:25
EventCollectionSelector::getNextRecord
virtual StatusCode getNextRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
Definition: EventCollectionSelector.cpp:149
EventCollectionSelector::createContext
StatusCode createContext(Context *&refpCtxt) const override
Create a new event loop context.
Definition: EventCollectionSelector.cpp:232
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
EventCollectionContext::fileIterator
ListName::const_iterator fileIterator()
Definition: EventCollectionSelector.cpp:61
EventCollectionContext::~EventCollectionContext
~EventCollectionContext() override
Standard destructor.
Definition: EventCollectionSelector.cpp:65
EventCollectionContext::identifier
void * identifier() const override
Definition: EventCollectionSelector.cpp:59
IRegistry.h
EventCollectionSelector::connectStatement
virtual StatusCode connectStatement(const std::string &typ, const std::string &crit, INTuple *tuple) const
Connect selection statement to refine data access.
Definition: EventCollectionSelector.cpp:132
EventCollectionSelector::connectCollection
virtual StatusCode connectCollection(MyContextType *ctxt) const
Connect collection to selector.
Definition: EventCollectionSelector.cpp:193
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
INTuple::selector
virtual ISelectStatement * selector()=0
Access selector.
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
EventCollectionContext::setCurrentInput
void setCurrentInput(const std::string &v)
Definition: EventCollectionSelector.cpp:57
EventCollectionSelector::getPreviousRecord
virtual StatusCode getPreviousRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
Definition: EventCollectionSelector.cpp:165
GenericAddress::release
unsigned long release() override
release reference to object
Definition: GenericAddress.h:71
NTuple::Tuple
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:388
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
EventCollectionSelector::m_cntName
Gaudi::Property< std::string > m_cntName
Definition: EventCollectionSelector.h:94
EventCollectionSelector
Definition of class EventCollectionSelector.
Definition: EventCollectionSelector.h:52
EventCollectionSelector::m_dbSvc
Gaudi::Property< std::string > m_dbSvc
Definition: EventCollectionSelector.h:99
DataObject
Definition: DataObject.h:36
EventCollectionContext
Definition: EventCollectionSelector.cpp:35
EventCollectionContext::setCriteria
void setCriteria(const std::string &crit)
Definition: EventCollectionSelector.cpp:60
std::string::empty
T empty(T... args)
EventCollectionContext::setFileIterator
void setFileIterator(ListName::const_iterator new_iter)
Definition: EventCollectionSelector.cpp:62
Properties.v
v
Definition: Properties.py:122
GenericAddress::addRef
unsigned long addRef() override
Add reference to object.
Definition: GenericAddress.h:69
GaudiConfig2.semantics.ident
ident
Definition: semantics.py:198
EventCollectionSelector::m_tupleSvcName
Gaudi::Property< std::string > m_tupleSvcName
Definition: EventCollectionSelector.h:91
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
EventCollectionSelector::connectTuple
virtual StatusCode connectTuple(const std::string &nam, const std::string &itName, NTuple::Tuple *&tup, NTuple::Item< IOpaqueAddress * > *&item) const
Connect to existing N-tuple.
Definition: EventCollectionSelector.cpp:113
EventCollectionSelector::m_criteria
Gaudi::Property< std::string > m_criteria
Definition: EventCollectionSelector.h:96
ISvcLocator.h
AlgSequencer.top
top
Definition: AlgSequencer.py:37
EventCollectionSelector::releaseContext
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
Definition: EventCollectionSelector.cpp:298
EventCollectionContext::files
ListName & files()
Definition: EventCollectionSelector.cpp:58
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:78
EventCollectionSelector::last
StatusCode last(Context &refCtxt) const override
Access last item in the iteration.
Definition: EventCollectionSelector.cpp:320
EventCollectionContext::ListName
std::list< std::string > ListName
Definition: EventCollectionSelector.cpp:37
EventCollectionSelector::m_statement
Gaudi::Property< std::string > m_statement
Definition: EventCollectionSelector.h:100
INTupleSvc.h
IOpaqueAddress::ipar
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:335
EventCollectionSelector::m_authentication
Gaudi::Property< std::string > m_authentication
Definition: EventCollectionSelector.h:93
NTuple::Item< IOpaqueAddress * >
EventCollectionContext::currentInput
const std::string & currentInput() const
Definition: EventCollectionSelector.cpp:56
EventCollectionContext::operator=
EventCollectionContext & operator=(const EventCollectionContext &)=delete
EventCollectionContext::m_fileIterator
ListName::const_iterator m_fileIterator
Definition: EventCollectionSelector.cpp:44