RootEvtSelector.cpp
Go to the documentation of this file.
1 // $Id: RootEvtSelector.cpp,v 1.11 2010-09-27 15:43:53 frankb Exp $
2 //====================================================================
3 // RootEvtSelector.cpp
4 //--------------------------------------------------------------------
5 //
6 // Package : RootCnv
7 //
8 // Author : M.Frank
9 //====================================================================
10 #ifndef GAUDIROOTCNV_ROOTEVTSELECTORCONTEXT_H
11 #define GAUDIROOTCNV_ROOTEVTSELECTORCONTEXT_H
12 
13 // Include files
14 #include "RootCnv/RootEvtSelector.h"
15 #include <vector>
16 
17 // Forward declarations
18 class TBranch;
19 
20 /*
21 * Gaudi namespace declaration
22 */
23 namespace Gaudi {
24 
34  public:
36  typedef std::vector<std::string> Files;
37  private:
41  Files m_files;
43  Files::const_iterator m_fiter;
45  long m_entry;
47  TBranch* m_branch;
49  std::string m_fid;
50  public:
52  RootEvtSelectorContext(const RootEvtSelector* s) : m_sel(s),m_entry(-1),m_branch(0){}
56  const Files& files() const { return m_files; }
58  void setFiles(const Files& f) {
59  m_files = f;
60  m_fiter = m_files.begin();
61  }
63  virtual void* identifier() const { return (void*)m_sel; }
65  Files::const_iterator fileIterator() const { return m_fiter; }
67  void setFileIterator(Files::const_iterator i) { m_fiter = i; }
69  long entry() const { return m_entry; }
71  void setEntry(long e) { m_entry = e; }
73  void setFID(const std::string& fid) { m_fid = fid; }
75  const std::string& fid() const { return m_fid; }
77  TBranch* branch() const { return m_branch; }
79  void setBranch(TBranch* b) { m_branch = b; }
80  };
81 }
82 #endif // GAUDIROOTCNV_ROOTEVTSELECTORCONTEXT_H
83 
84 // Include files
85 #include "GaudiKernel/ClassID.h"
86 #include "GaudiKernel/MsgStream.h"
87 #include "GaudiKernel/AttribStringParser.h"
88 #include "GaudiKernel/TypeNameString.h"
89 #include "GaudiKernel/IDataManagerSvc.h"
90 #include "GaudiKernel/IPersistencySvc.h"
91 #include "GaudiKernel/ISvcLocator.h"
92 #include "RootCnv/RootCnvSvc.h"
93 #include "RootCnv/RootDataConnection.h"
94 #include "TBranch.h"
95 
96 using namespace Gaudi;
97 using namespace std;
98 
99 // Service Constructor
101 : base_class(name, svcloc), m_rootCLID(CLID_NULL)
102 {
103  m_cnvSvcName = "Gaudi::RootCnvSvc/RootCnvSvc";
104  m_persName = "EventPersistencySvc";
105  declareProperty("EvtPersistencySvc",m_persName="EventPersistencySvc");
106  declareProperty("DbType",m_dummy);
107 }
108 
109 // Helper method to issue error messages
110 StatusCode RootEvtSelector::error(const string& msg) const {
111  MsgStream log(msgSvc(), name());
112  log << MSG::ERROR << msg << endmsg;
113  return StatusCode::FAILURE;
114 }
115 
116 // IService implementation: Db event selector override
118  // Initialize base class
119  StatusCode status = Service::initialize();
120  if ( !status.isSuccess() ) {
121  return error("Error initializing base class Service!");
122  }
123 
125  if( !ipers.isValid() ) {
126  return error("Unable to locate IPersistencySvc interface of "+m_persName);
127  }
128  IConversionSvc *cnvSvc = 0;
130  status = ipers->getService(itm.name(),cnvSvc);
131  if( !status.isSuccess() ) {
132  status = ipers->getService(itm.type(),cnvSvc);
133  if( !status.isSuccess() ) {
134  return error("Unable to locate IConversionSvc interface of database type "+m_cnvSvcName);
135  }
136  }
137  m_dbMgr = dynamic_cast<RootCnvSvc*>(cnvSvc);
138  if( !m_dbMgr ) {
139  cnvSvc->release();
140  return error("Unable to localize service:"+m_cnvSvcName);
141  }
142  m_dbMgr->addRef();
143 
144  // Get DataSvc
145  SmartIF<IDataManagerSvc> eds(serviceLocator()->service("EventDataSvc"));
146  if( !eds.isValid() ) {
147  return error("Unable to localize service EventDataSvc");
148  }
149  m_rootCLID = eds->rootCLID();
150  m_rootName = eds->rootName();
151  MsgStream log(msgSvc(), name());
152  log << MSG::DEBUG << "Selection root:" << m_rootName << " CLID:" << m_rootCLID << endmsg;
153  return status;
154 }
155 
156 // IService implementation: Service finalization
158  // Initialize base class
159  if ( m_dbMgr ) m_dbMgr->release();
160  m_dbMgr = 0; // release
161  return Service::finalize();
162 }
163 
164 // Create a new event loop context
166  refpCtxt = new RootEvtSelectorContext(this);
167  return StatusCode::SUCCESS;
168 }
169 
170 // Access last item in the iteration
171 StatusCode RootEvtSelector::last(Context& /*refContext*/) const {
172  return StatusCode::FAILURE;
173 }
174 
175 // Get next iteration item from the event loop context
177  RootEvtSelectorContext* pCtxt = dynamic_cast<RootEvtSelectorContext*>(&ctxt);
178  if ( pCtxt ) {
179  TBranch* b = pCtxt->branch();
180  if ( !b ) {
181  RootEvtSelectorContext::Files::const_iterator fileit = pCtxt->fileIterator();
182  pCtxt->setBranch(0);
183  pCtxt->setEntry(-1);
184  if ( fileit != pCtxt->files().end() ) {
185  RootDataConnection* con=0;
186  string in = *fileit;
188  if ( sc.isSuccess() ) {
189  string section = m_rootName[0] == '/' ? m_rootName.substr(1) : m_rootName;
190  b = con->getBranch(section,m_rootName);
191  if ( b ) {
192  pCtxt->setFID(con->fid());
193  pCtxt->setBranch(b);
194  return next(ctxt);
195  }
196  }
197  m_dbMgr->disconnect(in).ignore();
198  pCtxt->setFileIterator(++fileit);
199  return next(ctxt);
200  }
201  return StatusCode::FAILURE;
202  }
203  long ent = pCtxt->entry();
204  Long64_t nent = b->GetEntries();
205  if ( nent > (ent+1) ) {
206  pCtxt->setEntry(++ent);
207  return StatusCode::SUCCESS;
208  }
209  RootEvtSelectorContext::Files::const_iterator fit = pCtxt->fileIterator();
210  pCtxt->setFileIterator(++fit);
211  pCtxt->setEntry(-1);
212  pCtxt->setBranch(0);
213  pCtxt->setFID("");
214  return next(ctxt);
215  }
216  return StatusCode::FAILURE;
217 }
218 
219 // Get next iteration item from the event loop context
220 StatusCode RootEvtSelector::next(Context& ctxt,int jump) const {
221  if ( jump > 0 ) {
222  for ( int i = 0; i < jump; ++i ) {
223  StatusCode status = next(ctxt);
224  if ( !status.isSuccess() ) {
225  return status;
226  }
227  }
228  return StatusCode::SUCCESS;
229  }
230  return StatusCode::FAILURE;
231 }
232 
233 // Get previous iteration item from the event loop context
235  return error("EventSelector Iterator, operator -- not supported ");
236 }
237 
238 // Get previous iteration item from the event loop context
240  if ( jump > 0 ) {
241  for ( int i = 0; i < jump; ++i ) {
242  StatusCode status = previous(ctxt);
243  if ( !status.isSuccess() ) {
244  return status;
245  }
246  }
247  return StatusCode::SUCCESS;
248  }
249  return StatusCode::FAILURE;
250 }
251 
252 // Rewind the dataset
254  RootEvtSelectorContext* pCtxt = dynamic_cast<RootEvtSelectorContext*>(&ctxt);
255  if ( pCtxt ) {
256  RootEvtSelectorContext::Files::const_iterator fileit = pCtxt->fileIterator();
257  if ( fileit != pCtxt->files().end() ) {
258  string input = *fileit;
259  m_dbMgr->disconnect(input).ignore();
260  }
261  pCtxt->setFID("");
262  pCtxt->setEntry(-1);
263  pCtxt->setBranch(0);
264  pCtxt->setFileIterator(pCtxt->files().begin());
265  return StatusCode::SUCCESS;
266  }
267  return StatusCode::FAILURE;
268 }
269 
270 // Create new Opaque address corresponding to the current record
273  const RootEvtSelectorContext* pctxt = dynamic_cast<const RootEvtSelectorContext*>(&ctxt);
274  if ( pctxt ) {
275  long ent = pctxt->entry();
276  if ( ent >= 0 ) {
277  RootEvtSelectorContext::Files::const_iterator fileit = pctxt->fileIterator();
278  if ( fileit != pctxt->files().end() ) {
279  const string par[2] = {pctxt->fid(), m_rootName};
280  const unsigned long ipar[2] = {0,(unsigned long)ent};
281  return m_dbMgr->createAddress(m_dbMgr->repSvcType(),m_rootCLID,&par[0],&ipar[0],pAddr);
282  }
283  }
284  }
285  pAddr = 0;
286  return StatusCode::FAILURE;
287 }
288 
289 // Release existing event iteration context
291  RootEvtSelectorContext* pCtxt = dynamic_cast<RootEvtSelectorContext*>(ctxt);
292  if ( pCtxt ) {
293  delete pCtxt;
294  return StatusCode::SUCCESS;
295  }
296  return StatusCode::FAILURE;
297 }
298 
299 // Will set a new criteria for the selection of the next list of events and will change
300 // the state of the context in a way to point to the new list.
302 RootEvtSelector::resetCriteria(const string& criteria, Context& context) const
303 {
304  MsgStream log(msgSvc(), name());
305  RootEvtSelectorContext* ctxt = dynamic_cast<RootEvtSelectorContext*>(&context);
306  string db, typ, item, sel, stmt, aut, addr;
307  if ( ctxt ) {
308  if ( criteria.substr(0,5) == "FILE " ) {
309  // The format for the criteria is:
310  // FILE filename1, filename2 ...
311  db = criteria.substr(5);
312  }
313  else {
314  using Parser = Gaudi::Utils::AttribStringParser;
315  for(auto attrib: Parser(criteria)) {
316  string tmp = attrib.tag.substr(0,3);
317  if(tmp=="DAT") {
318  db = std::move(attrib.value);
319  }
320  else if(tmp=="OPT") {
321  if(attrib.value.substr(0, 3) != "REA") {
322  log << MSG::ERROR << "Option:\"" << attrib.value << "\" not valid" << endmsg;
323  return StatusCode::FAILURE;
324  }
325  }
326  else if (tmp=="TYP") {
327  typ = std::move(attrib.value);
328  }
329  else if(tmp=="ADD") {
330  item = std::move(attrib.value);
331  }
332  else if(tmp=="SEL") {
333  sel = std::move(attrib.value);
334  }
335  else if(tmp=="FUN") {
336  stmt = std::move(attrib.value);
337  }
338  else if(tmp=="AUT") {
339  aut = std::move(attrib.value);
340  }
341  else if(tmp=="COL") {
342  addr = std::move(attrib.value);
343  }
344  }
345  }
346  // It's now time to parse the criteria for the event selection
347  // The format for the criteria is:
348  // FILE filename1, filename2 ...
349  // JOBID number1-number2, number3, ...
351  string rest = db;
352  files.clear();
353  while(true) {
354  int ipos = rest.find_first_not_of(" ,");
355  if (ipos == -1 ) break;
356  rest = rest.substr(ipos,string::npos);// remove blanks before
357  int lpos = rest.find_first_of(" ,"); // locate next blank
358  files.push_back(rest.substr(0,lpos )); // insert in list
359  if (lpos == -1 ) break;
360  rest = rest.substr(lpos,string::npos);// get the rest
361  }
362  ctxt->setFiles(files);
363  ctxt->setFileIterator(ctxt->files().begin());
364  return StatusCode::SUCCESS;
365  }
366  return error("Invalid iteration context.");
367 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
const Files & files() const
Access to the file container.
std::string m_persName
Property; Name of the persistency service to search for conversion service.
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:14
virtual StatusCode rewind(Context &refCtxt) const
Rewind the dataset.
virtual StatusCode initialize()
IService implementation: Db event selector override.
Files::const_iterator fileIterator() const
Access to the file iterator.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
const std::string & fid() const
Access file id.
virtual StatusCode last(Context &refContext) const
Access last item in the iteration.
RootEvtSelectorContext(const RootEvtSelector *s)
Standard constructor with initialization.
virtual void * identifier() const
Context identifier.
tuple itm
Definition: ana.py:57
virtual StatusCode createAddress(long svc_type, const CLID &clid, const std::string *par, const unsigned long *ip, IOpaqueAddress *&refpAddress)
IAddressCreator implementation: Address creation.
Definition: RootCnvSvc.cpp:391
virtual std::string rootName() const =0
Get Name of root Event.
std::vector< std::string > Files
Definition of the file container.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
std::string m_fid
Connection fid.
Description:
Definition: RootCnvSvc.h:54
STL namespace.
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_dummy
Property: dummy to fake backwards compatibility.
virtual StatusCode previous(Context &refCtxt) const
Get previous iteration item from the event loop context.
virtual StatusCode disconnect(const std::string &dbName)
Disconnect from an existing data stream.
virtual StatusCode getService(long service_type, IConversionSvc *&refpSvc)=0
Retrieve conversion service identified by technology.
virtual StatusCode finalize()
IService implementation: Service finalization.
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
virtual long repSvcType() const
Retrieve the class type of the data store the converter uses.
std::string m_cnvSvcName
Property; Name of the concversion service used to create opaque addresses.
virtual CLID rootCLID() const =0
Get class ID of root Event.
ROOT specific event selector context.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
TBranch * getBranch(const std::string &section, const std::string &branch_name)
Access data branch by name: Get existing branch in read only mode.
const RootEvtSelector * m_sel
Reference to the hosting event selector instance.
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:329
void setFiles(const Files &f)
Set the file container.
void setBranch(TBranch *b)
Set the top level branch (typically /Event) used to iterate.
StatusCode connectDatabase(const std::string &dataset, int mode, RootDataConnection **con)
Connect the output file to the service with open mode.
Concrete event selector implementation to access ROOT files.
void setEntry(long e)
Set current event entry number.
virtual unsigned long release()=0
Release Interface instance.
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:72
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
virtual unsigned long release()
Release Interface instance.
Definition: Service.cpp:22
const std::string & type() const
const std::string & fid() const
Access connection fid.
void setFID(const std::string &fid)
Set connection FID.
Files::const_iterator m_fiter
The iterator to the.
string s
Definition: gaudirun.py:244
tuple item
print s1,s2
Definition: ana.py:146
Templated class to add the standard messaging functionalities.
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
RootCnvSvc * m_dbMgr
Reference to the corresponding conversion service.
virtual ~RootEvtSelectorContext()
Standard destructor.
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:143
void setFileIterator(Files::const_iterator i)
Set file iterator.
TBranch * branch() const
Access to the top level branch (typically /Event) used to iterate.
const std::string & name() const
RootEvtSelector(const std::string &name, ISvcLocator *svcloc)
Service Constructor.
Opaque address interface definition.
void ignore() const
Definition: StatusCode.h:107
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:212
Files m_files
The file container managed by this context.
virtual StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&) const
Create new Opaque address corresponding to the current record.
virtual StatusCode releaseContext(Context *&refCtxt) const
Release existing event iteration context.
CLID m_rootCLID
Class id of root node to create opaque address.
list i
Definition: ana.py:128
Concrete implementation of the IDataConnection interface to access ROOT files.
TBranch * m_branch
Reference to the top level branch (typically /Event) used to iterate.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Helper functions to set/get the application return code.
Definition: __init__.py:1
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:197
long entry() const
Access to the current event entry number.
virtual StatusCode next(Context &refCtxt) const
Get next iteration item from the event loop context.
virtual StatusCode createContext(Context *&refpCtxt) const
Create a new event loop context.
std::string m_rootName
Property: Name of the ROOT entry name.
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:334
long m_entry
Current entry of current file.