The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
EventCollectionSelector.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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\***********************************************************************************/
20#include <GaudiKernel/NTuple.h>
22#include <GaudiKernel/SmartIF.h>
23#include <memory>
24
26
27
33public:
34 typedef std::list<std::string> ListName;
35
36private:
40 std::string m_criteria;
41 ListName::const_iterator m_fileIterator;
42 std::string m_currentInput;
43
44public:
48 ~EventCollectionContext() override;
52
53 const std::string& currentInput() const { return m_currentInput; }
54 void setCurrentInput( const std::string& v ) { m_currentInput = v; }
55 ListName& files() { return m_files; }
56 void* identifier() const override { return (void*)m_pSelector; }
57 void setCriteria( const std::string& crit ) { m_criteria = crit; }
58 ListName::const_iterator fileIterator() { return m_fileIterator; }
59 void setFileIterator( ListName::const_iterator new_iter ) { m_fileIterator = new_iter; }
60};
61
63
68
69// IService implementation: Db event selector override
71 // Initialize base class
73 if ( !status.isSuccess() ) {
74 error() << "Error initializing base class Service!" << endmsg;
75 return status;
76 }
77 m_pAddrCreator = serviceLocator()->service( "EventPersistencySvc" );
78 if ( !m_pAddrCreator ) {
79 error() << "Unable to locate IAddressCreator interface of "
80 << "EventPersistencySvc" << endmsg;
81 return status;
82 }
84 if ( !m_tupleSvc ) {
85 error() << "Unable to locate INTupleSvc interface of " << m_tupleSvcName << endmsg;
86 return status;
87 }
88 return status;
89}
90
91// Connect collection to selector
92StatusCode EventCollectionSelector::connectDataSource( const std::string& db, const std::string& /* typ */ ) const {
95 if ( svc && !db.empty() ) {
96 std::string ident = name() + ' ';
97 ident += "DATAFILE='" + m_database.value().substr( 5 ) + "' ";
98 if ( !m_dbSvc.empty() )
99 ident += "SVC='" + m_dbSvc + "' ";
100 else
101 ident += "TYP='" + m_dbType + "' ";
102 ident += "OPT='READ' ";
103 if ( m_authentication.length() > 0 ) { ident += "AUTH='" + m_authentication + "' "; }
104 status = svc->connect( ident );
105 }
106 return status;
107}
108
110StatusCode EventCollectionSelector::connectTuple( const std::string& nam, const std::string& itName,
111 NTuple::Tuple*& tup,
112 std::optional<NTuple::Item<IOpaqueAddress*>>& item ) const {
113 std::string top = "/NTUPLES/" + name() + '/' + nam;
114 StatusCode status = m_tupleSvc->retrieveObject( top, (DataObject*&)tup );
115 if ( status.isSuccess() ) {
116 item.emplace();
117 status = tup->item( itName, *item );
118 if ( status.isSuccess() ) return status;
119 error() << "Item " << itName << " is not part of the collection:" << top << endmsg;
120 item.reset();
121 } else {
122 error() << "Cannot connect to collection:" << top << endmsg;
123 }
124 tup = nullptr;
125 return status;
126}
127
129StatusCode EventCollectionSelector::connectStatement( const std::string& typ, const std::string& crit,
130 INTuple* tuple ) const {
131 std::string seltyp = typ;
132 if ( !seltyp.empty() || !crit.empty() ) {
133 if ( !crit.empty() && seltyp.length() == 0 ) seltyp = "NTuple::Selector";
134 SmartIF<ISelectStatement> stmt( ObjFactory::create( seltyp, serviceLocator() ).release() );
135 if ( stmt ) {
136 if ( !crit.empty() ) stmt->setCriteria( crit );
137 tuple->attachSelector( stmt ).ignore();
138 return StatusCode::SUCCESS;
139 }
140 return StatusCode::FAILURE;
141 }
142 return StatusCode::SUCCESS;
143}
144
148 if ( tuple ) {
149 do {
150 status = m_tupleSvc->readRecord( tuple );
151 if ( status.isSuccess() ) {
152 ISelectStatement* statement = tuple->selector();
153 bool use_it = ( statement ) ? ( *statement )( tuple ) : true;
154 if ( use_it ) { return status; }
155 }
156 } while ( status.isSuccess() );
157 }
158 return status;
159}
160
164 if ( tuple ) {
165 IRegistry* pReg = tuple->registry();
166 if ( pReg ) {
167 IOpaqueAddress* pAddr = pReg->address();
168 if ( pAddr ) {
169 long* ip = (long*)pAddr->ipar();
170 do {
171 if ( ip[1] > 1 ) {
172 ip[1] -= 2;
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 ) { return status; }
178 }
179 } else {
180 return StatusCode::FAILURE;
181 }
182 } while ( status.isSuccess() );
183 }
184 }
185 }
186 return StatusCode::FAILURE;
187}
188
189// Connect collection to selector
191 if ( ctxt ) {
193 if ( status.isSuccess() ) {
194 status = connectTuple( m_cntName, m_itemName, ctxt->tuple, ctxt->item );
195 if ( status.isSuccess() ) {
196 status = connectStatement( m_statement, m_criteria, ctxt->tuple );
197 if ( status.isSuccess() ) {
198 *( ctxt->item ) = 0;
199 return status;
200 }
201 }
202 }
203 return status;
204 }
205 return StatusCode::FAILURE;
206}
207
208// Finalize service
210 // release services
211 m_pAddrCreator = nullptr;
212 m_tupleSvc = nullptr;
213 return Service::finalize();
214}
215
218 refpCtxt = nullptr;
219 auto ctxt = std::make_unique<MyContextType>();
220 StatusCode status = connectCollection( ctxt.get() );
221 if ( !status.isSuccess() ) {
222 error() << "Unable to connect Collection file \"" << m_database << "\"" << endmsg;
223 return status;
224 }
225 refpCtxt = ctxt.release();
226 return StatusCode::SUCCESS;
227}
228
230StatusCode EventCollectionSelector::next( Context& refCtxt ) const { return next( refCtxt, 1 ); }
231
233StatusCode EventCollectionSelector::next( Context& refCtxt, int jump ) const {
234 MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
235 if ( ctxt ) {
236 *( ctxt->item ) = ctxt->addressBuffer;
238 for ( int i = 0; i < jump && sc.isSuccess(); ++i ) { sc = getNextRecord( ctxt->tuple ); }
239 return sc;
240 }
241 return StatusCode::FAILURE;
242}
243
245StatusCode EventCollectionSelector::previous( Context& refCtxt ) const { return previous( refCtxt, 1 ); }
246
248StatusCode EventCollectionSelector::previous( 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 = getPreviousRecord( ctxt->tuple ); }
254 return sc;
255 }
256 return StatusCode::FAILURE;
257}
258
260StatusCode EventCollectionSelector::rewind( Context& /* refCtxt */ ) const { return StatusCode::FAILURE; }
261
263StatusCode EventCollectionSelector::createAddress( const Context& refCtxt, IOpaqueAddress*& refpAddr ) const {
264 const MyContextType* ctxt = dynamic_cast<const MyContextType*>( &refCtxt );
265 if ( ctxt ) {
266 IOpaqueAddress* pA = *( ctxt->item );
267 if ( pA ) {
268 IOpaqueAddress* pAddress = nullptr;
269 StatusCode status = m_pAddrCreator->createAddress( pA->svcType(), pA->clID(), pA->par(), pA->ipar(), pAddress );
270 if ( status.isSuccess() ) {
271 refpAddr = pAddress;
272 return StatusCode::SUCCESS;
273 } else {
274 error() << "Failed to access " << pA->par()[0] << ":" << pA->par()[1] << " SvcTyp:" << long( pA->svcType() )
275 << " CLID:" << pA->clID() << endmsg;
276 }
277 }
278 }
279 return StatusCode::FAILURE;
280}
281
284 MyContextType* ctxt = dynamic_cast<MyContextType*>( refCtxt );
285 if ( ctxt ) {
286 delete ctxt;
287 ctxt = nullptr;
288 return StatusCode::SUCCESS;
289 }
290 return StatusCode::FAILURE;
291}
292
295StatusCode EventCollectionSelector::resetCriteria( const std::string& cr, Context& refCtxt ) const {
296 MyContextType* ctxt = dynamic_cast<MyContextType*>( &refCtxt );
297 if ( ctxt ) {
298 ctxt->criteria = cr;
299 return StatusCode::SUCCESS;
300 }
301 return StatusCode::FAILURE;
302}
303
305StatusCode EventCollectionSelector::last( Context& /* refCtxt */ ) const { return StatusCode::FAILURE; }
#define class
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
IRegistry * registry() const
Get pointer to Registry.
Definition DataObject.h:79
void setCriteria(const std::string &crit)
EventCollectionContext(const EventCollectionSelector *pSelector)
Standard constructor.
EventCollectionContext(const EventCollectionContext &)=delete
No copy.
void * identifier() const override
~EventCollectionContext() override
Standard destructor.
void setCurrentInput(const std::string &v)
void setFileIterator(ListName::const_iterator new_iter)
std::list< std::string > ListName
const std::string & currentInput() const
EventCollectionContext & operator=(const EventCollectionContext &)=delete
const EventCollectionSelector * m_pSelector
ListName::const_iterator fileIterator()
ListName::const_iterator m_fileIterator
std::optional< NTuple::Item< IOpaqueAddress * > > item
Definition of class EventCollectionSelector.
StatusCode rewind(Context &refCtxt) const override
Rewind the dataset.
StatusCode previous(Context &refCtxt) const override
Get previous iteration item from the event loop context.
virtual StatusCode connectCollection(MyContextType *ctxt) const
Connect collection to selector.
virtual StatusCode connectDataSource(const std::string &db, const std::string &typ) const
Connect collection's data source to selector.
StatusCode createContext(Context *&refpCtxt) const override
Create a new event loop context.
Gaudi::Property< std::string > m_authentication
StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const override
Create new Opaque address corresponding to the current record.
virtual StatusCode getPreviousRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
SmartIF< IAddressCreator > m_pAddrCreator
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
StatusCode last(Context &refCtxt) const override
Access last item in the iteration.
Gaudi::Property< std::string > m_database
StatusCode initialize() override
Service override: Initialize service.
virtual StatusCode connectTuple(const std::string &nam, const std::string &itName, NTuple::Tuple *&tup, std::optional< NTuple::Item< IOpaqueAddress * > > &item) const
Connect to existing N-tuple.
Gaudi::Property< std::string > m_tupleSvcName
Gaudi::Property< std::string > m_itemName
Gaudi::Property< std::string > m_dbSvc
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_cntName
virtual StatusCode getNextRecord(NTuple::Tuple *tuple) const
Read next record of the N-tuple.
SmartIF< INTupleSvc > m_tupleSvc
Reference to Tuple service.
virtual StatusCode connectStatement(const std::string &typ, const std::string &crit, INTuple *tuple) const
Connect selection statement to refine data access.
Gaudi::Property< std::string > m_dbType
Gaudi::Property< std::string > m_statement
StatusCode finalize() override
Service override: Finalize Service.
Gaudi::Property< std::string > m_criteria
StatusCode next(Context &refCtxt) const override
Get next iteration item from the event loop context.
Generic Transient Address.
The Event Selector Interface.
NTuple interface class definition.
Definition INTuple.h:86
virtual StatusCode attachSelector(ISelectStatement *sel)=0
Attach selector.
virtual ISelectStatement * selector()=0
Access selector.
Opaque address interface definition.
virtual long svcType() const =0
Retrieve service type.
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
virtual const CLID & clID() const =0
Retrieve class information from link.
virtual const std::string * par() const =0
Retrieve String parameters.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition IRegistry.h:29
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
A select statement can either contain.
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
Class acting as a smart pointer holding a N tuple _Item.
Definition NTuple.h:251
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition NTuple.h:380
StatusCode item(const std::string &name, Item< TYPE > &result)
Locate a scalar Item of data to the N tuple type safe.
Definition NTuple.h:464
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
StatusCode finalize() override
Definition Service.cpp:223
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
StatusCode initialize() override
Definition Service.cpp:118
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition StatusCode.h:139
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100