| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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"
3 #include "GaudiKernel/ObjectFactory.h"
4 #include "GaudiKernel/GenericAddress.h"
5 #include "GaudiKernel/MsgStream.h"
6 #include "GaudiKernel/IRegistry.h"
7 #include "GaudiKernel/INTupleSvc.h"
8 #include "GaudiKernel/ISvcLocator.h"
9 #include "GaudiKernel/IDataSourceMgr.h"
10 #include "GaudiKernel/IAddressCreator.h"
11 #include "GaudiKernel/ISelectStatement.h"
12 #include "GaudiKernel/NTuple.h"
13 #include "GaudiKernel/EventSelectorDataStream.h"
14
15 #include "EventCollectionSelector.h"
16
17 #include <memory>
18
19 DECLARE_COMPONENT(EventCollectionSelector)
20
21
26 class EventCollectionContext : public IEvtSelector::Context {
27 public:
28 typedef std::list<std::string> ListName;
29 private:
30 GenericAddress* m_pAddressBuffer;
31 const EventCollectionSelector* m_pSelector;
32 ListName m_files;
33 std::string m_criteria;
34 ListName::const_iterator m_fileIterator;
35 std::string m_currentInput;
36
37 public:
39 EventCollectionContext(const EventCollectionSelector* pSelector);
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
64 EventCollectionContext::~EventCollectionContext() {
65 m_pAddressBuffer->release();
66 }
67
68 EventCollectionContext::EventCollectionContext(const EventCollectionSelector* pSelector)
69 : m_pSelector(pSelector)
70 {
71 m_pAddressBuffer = new GenericAddress();
72 m_pAddressBuffer->addRef();
73 }
74
75 // Standard constructor
76 EventCollectionSelector::EventCollectionSelector(const std::string& name, ISvcLocator* svcloc )
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
90 EventCollectionSelector::~EventCollectionSelector() {
91 }
92
93 // IService implementation: Db event selector override
94 StatusCode EventCollectionSelector::initialize() {
95 // Initialize base class
96 StatusCode status = Service::initialize();
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 }
107 m_tupleSvc = serviceLocator()->service(m_tupleSvcName);
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
116 StatusCode
117 EventCollectionSelector::connectDataSource(const std::string& db, const std::string& /* typ */ ) const {
118 StatusCode status = StatusCode::FAILURE;
119 SmartIF<IDataSourceMgr> svc(m_tupleSvc);
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
137 StatusCode
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
163 StatusCode
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
180 StatusCode EventCollectionSelector::getNextRecord(NTuple::Tuple* tuple) const {
181 StatusCode status = StatusCode::FAILURE;
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
198 StatusCode EventCollectionSelector::getPreviousRecord(NTuple::Tuple* tuple ) const {
199 StatusCode status = StatusCode::FAILURE;
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
229 StatusCode EventCollectionSelector::connectCollection(MyContextType* ctxt) const
230 {
231 if ( ctxt ) {
232 StatusCode status = connectDataSource(m_database, m_dbType);
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
249 StatusCode EventCollectionSelector::finalize() {
250 // release services
251 m_pAddrCreator = 0;
252 m_tupleSvc = 0;
253 return Service::finalize();
254 }
255
257 StatusCode
258 EventCollectionSelector::createContext(Context*& refpCtxt) const
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
273 StatusCode
274 EventCollectionSelector::next(Context& refCtxt) const
275 {
276 return next(refCtxt, 1);
277 }
278
280 StatusCode
281 EventCollectionSelector::next(Context& refCtxt,int jump) const
282 {
283 MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
284 if ( ctxt ) {
285 *(ctxt->item) = ctxt->addressBuffer;
286 StatusCode sc = StatusCode::SUCCESS;
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
296 StatusCode
297 EventCollectionSelector::previous(Context& refCtxt) const
298 {
299 return previous(refCtxt, 1);
300 }
301
303 StatusCode
304 EventCollectionSelector::previous(Context& refCtxt,int jump) const
305 {
306 MyContextType *ctxt = dynamic_cast<MyContextType*>(&refCtxt);
307 if ( ctxt ) {
308 *(ctxt->item) = ctxt->addressBuffer;
309 StatusCode sc = StatusCode::SUCCESS;
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
319 StatusCode
320 EventCollectionSelector::rewind(Context& /* refCtxt */ ) const
321 {
322 return StatusCode::FAILURE;
323 }
324
326 StatusCode
327 EventCollectionSelector::createAddress(const Context& refCtxt, IOpaqueAddress*& refpAddr) const
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
357 StatusCode
358 EventCollectionSelector::releaseContext(Context*& refCtxt) const
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
371 StatusCode
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
383 StatusCode
384 EventCollectionSelector::last(Context& /* refCtxt */ ) const
385 {
386 return StatusCode::FAILURE;
387 }