ATLAS Offline Software
PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
8 
9 #include "GaudiKernel/IEventProcessor.h"
10 
11 #include "TString.h"
12 #include "TObjArray.h"
13 #include "TObjString.h"
14 #include "TSystem.h"
15 #include "TFile.h"
16 #include "TChain.h"
17 
20 
21 namespace Gaudi {
22  IAppMgrUI* Init() {
23  return AAH::initGaudi("POOLRootAccess/basic.opts");
24  }
25 }
26 
27 namespace POOL {
28 
29 IAppMgrUI* Init( const char* options ) {
30  return AAH::initGaudi(options); //see AthAnalysisHelper
31 }
32 
34  //need to destroy my storegate, selector, and loop
35  if(m_evtLoop.isSet()) {
36  m_evtLoop.release().ignore();
37  }
38  if(m_evtSelect.isSet()) {
39  m_evtSelect.release().ignore();
40  }
41  if(m_evtStore.isSet()) {
42  m_evtStore.release().ignore();
43  }
44 }
45 
46 TEvent::TEvent(const std::string& name ) : TEvent( kPOOLAccess , name ) { }
47 
48 TEvent::TEvent(EReadMode mode, const std::string& name) :
49  m_evtProcessor (nullptr),
50  m_incSvc("IncidentSvc","TEvent"+name),
51  m_joSvc("JobOptionsSvc","TEvent"+name),
52  m_evtLoop("AthenaEventLoopMgr/"+name+"_EventLoopMgr","TEvent"+name),
53  m_evtSelect("EventSelectorAthenaPool/"+name+"_EventSelector","TEvent"+name),
54  m_evtStore("StoreGateSvc/"+name,"TEvent"+name),
55  m_inputMetaStore("InputMetaDataStore","TEvent"+name) /*fixme, when reading multiple files at once?*/ {
56 
57  Gaudi::Init();
58 
59  //SEEMS TO CAUSE INITIALIZE LOOP if(name=="StoreGateSvc") m_evtSelect.setName("EventSelector"); //use default selector name if we use default storegate
60 
61  //FIXME: Should protect against attempt to mix POOL with nonPOOL
62  if(mode==kPOOLAccess) {
63  //add AthenaPoolCnvSvc to the EventPersistencySvc (ideally the selector should add this itself!!)
64  ServiceHandle<IService> epSvc("EventPersistencySvc","TEvent"+name);
65  AAH::setProperty( epSvc , "CnvServices" , "['AthenaPoolCnvSvc']" ).ignore(); //FIXME: perhaps should append rather than overwrite
66  //add the AthenaPoolAddressProviderSvc to ProxyProviderSvc
67  ServiceHandle<IService> ppSvc("ProxyProviderSvc","TEvent"+name);
68  AAH::setProperty( ppSvc , "ProviderNames", "['MetaDataSvc', 'AthenaPoolAddressProviderSvc']" ).ignore();
69  } else if(mode==kTreeAccess) {
70  //switch selector type to Athena::RootNtupleEventSelector
71  m_evtSelect.setTypeAndName("Athena::RootNtupleEventSelector/"+m_evtSelect.name());
72  //also default to 'CollectionTree' as the tree to read
73  this->setEvtSelProperty("TupleName","CollectionTree");
74  } else {
75  //switch selector type to xAODEventSelector:
76  m_evtSelect.setTypeAndName("Athena::xAODEventSelector/"+m_evtSelect.name());
77  }
78 
79  //check if a SelectorType has been specified in the joSvc
80  //should retire this code at some point (hangover from basicxAOD.opts)
81  if (m_joSvc->has("TEvent.EventSelectorType")) {
82  m_evtSelect.setTypeAndName(m_joSvc->get("TEvent.EventSelectorType") + "/" + m_evtSelect.name());
83  }
84 
85  AAH::setProperty( m_evtLoop , "ClearStorePolicy", "BeginEvent" ).ignore(); //for interactive use of storegate
86  AAH::setProperty( m_evtLoop , "EvtSel", m_evtSelect.typeAndName() ).ignore(); //connect loop to selector
87  AAH::setProperty( m_evtLoop , "EvtStore", m_evtStore.typeAndName() ).ignore();//connect loop to store
88  AAH::setProperty( m_evtLoop , "EventPrintoutInterval", 0 ).ignore(); //disable printout (speeds up loop)
89 
90  if(m_evtSelect.type()=="Athena::xAODEventSelector") {
91  AAH::setProperty( m_evtSelect , "ReadMetaDataWithPool" , true).ignore(); //uses hybrid xAOD reading by default
92  AAH::setProperty( m_evtSelect , "AccessMode" , int(mode) ).ignore(); //sets the mode
93  AAH::setProperty( m_evtSelect , "EvtStore" , m_evtStore.typeAndName() ).ignore();
94  //FIXME ... cant get dual event stores working :-(
95  //AAH::setProperty( m_evtSelect , "ProxyProviderSvc" , "ProxyProviderSvc/" + name + "_ProxyProviderSvc" );
96  //AAH::setProperty( m_evtStore , "ProxyProviderSvc", "ProxyProviderSvc/" + name + "_ProxyProviderSvc" );
97  }
98 
99  //set outputlevels to WARNING
100  AAH::setProperty( m_evtLoop, "OutputLevel", 4 ).ignore();
101  AAH::setProperty( m_evtSelect, "OutputLevel", 4 ).ignore();
102  //AAH::setProperty( m_evtStore, "OutputLevel", 4 ).ignore();
103 
104  //suppress messages below WARNING too
105  //do this here to stop some pre initialize INFO messages from showing
106  ServiceHandle<IProperty> messageSvc("MessageSvc","");
107  std::vector<std::string> p;
108  Gaudi::Parsers::parse(p,messageSvc->getProperty("setWarning").toString()).ignore();
109  p.push_back( m_evtLoop.name() );
110  p.push_back( m_evtSelect.name() );
111  p.push_back( m_evtStore.name() );
112  messageSvc->setProperty( StringArrayProperty( "setWarning" , p ) ).ignore();
113 
114  //also push this into the joboptionsvc so that if a reinit happens then these settings are remembered
115  //m_joSvc->addPropertyToCatalogue( "MessageSvc", StringArrayProperty( "setWarning", p ) );
116 
117 }
118 
119 
121  m_evtStore->makeCurrent();
122 }
123 
125  if(m_size>=0) return m_size;
126  if(!m_evtLoop.isSet()) { if(m_evtLoop.retrieve().isFailure()) return 0; }
127  if (ICollectionSize* isize = dynamic_cast<ICollectionSize*>(&*m_evtLoop)) {
128  return isize->size();
129  }
130  return 0;
131 }
132 
134  if(file==0 || file->IsZombie()) return StatusCode::FAILURE;
135  return readFrom( file->GetName() );
136 }
137 
139  if(m_curEntry >= 0) {
140  std::cout << "Unable to change file after already reading" << std::endl;
141  return StatusCode::FAILURE;
142  }
143  std::vector<std::string> myFiles;
144  //see if contains wildcard
145  //separate by comma
146  TString sFileIn(file);
147  std::unique_ptr<TObjArray> sFiles(sFileIn.Tokenize(","));
148  std::unique_ptr<TIterator> itr(sFiles->MakeIterator());
149  TObject* obj;
150  while( (obj = itr->Next()) ) {
151  TString sFile(static_cast<TObjString*>(obj)->String());
152  if(sFile.Contains('*')) {
153  std::unique_ptr<TObjArray> theFiles(gSystem->GetFromPipe(("ls " + std::string(file)).c_str()).Tokenize("\n"));
154  for(int i=0;i<theFiles->GetEntries();i++) {
155  //std::cout << "Adding " << dynamic_cast<TObjString*>(theFiles->At(i))->String().Data() << std::endl;
156  if (TObjString* objstr = dynamic_cast<TObjString*>(theFiles->At(i))) {
157  myFiles.push_back(gSystem->ExpandPathName(objstr->String().Data()));
158  }
159  }
160  } else {
161  myFiles.push_back( gSystem->ExpandPathName(sFile.Data()) );
162  }
163  }
164 
165  return AAH::setProperty( m_evtSelect , "InputCollections" , myFiles );
166 }
167 
169 
170  //add all files to the property list by giving a comma separated list of files
171  std::string sfiles;
172  std::unique_ptr<TIterator> itr(files->GetListOfFiles()->MakeIterator());
173  TObject* obj;
174  bool hasFile(false);
175  while( (obj = itr->Next()) ) {
176  if(hasFile) sfiles += ",";
177  sfiles += obj->GetTitle();
178  hasFile=true;
179  }
180  //std::cout << sfiles << std::endl;
181  return readFrom(sfiles.c_str());
182 
183 }
184 
185 int TEvent::getEntry( long entry ) {
186  if(!m_evtLoop.isSet()) { if(m_evtLoop.retrieve().isFailure()) return -1; }
187  if(entry==0 || entry != m_curEntry+1) {
188  //need to seek on first read or if not reading next event
189  if (m_evtLoop->seek(entry).isFailure()) return -1;
190  }
191  //ensure our storegate is the active store
192  setActive();
193  if (m_evtProcessor == nullptr) {
194  m_evtProcessor = dynamic_cast<IEventProcessor*>(&*m_evtLoop);
195  }
196  StatusCode out = m_evtProcessor->nextEvent(entry+1);
197  m_curEntry = entry;
198  m_incSvc->fireIncident(Incident("BeginEvent",IncidentType::BeginEvent));
199  return (out.isSuccess()) ? 0 : -1;
200 }
201 
202  //python bindings
203 
205  {
206  return AthenaInternal::retrieveObjectFromStore( &*evtStore() , tp, pykey );
207  }
208 
210  return AthenaInternal::py_sg_getitem( &*evtStore() , pykey );
211  }
213  return AthenaInternal::py_sg_contains( &*evtStore() , tp, pykey );
214  }
216  if(!m_evtLoop.isSet()) { if(m_evtLoop.retrieve().isFailure()) return 0; }
218  }
219  PyObject* TEvent::record (PyObject* obj, PyObject* pykey,bool allowMods,bool resetOnly,bool noHist) {
220  return AthenaInternal::recordObjectToStore( &*evtStore() , obj, pykey,allowMods,resetOnly,noHist );
221  }
222 
223 
224 
225 } //end of POOL namespace
POOL::TEvent::m_joSvc
ServiceHandle< Gaudi::Interfaces::IOptionsSvc > m_joSvc
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:119
POOL::TEvent::retrieveMetaInput
StatusCode retrieveMetaInput(const T *&obj, const std::string &key)
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:90
POOL::TEvent::m_curEntry
long m_curEntry
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:113
Gaudi::Parsers::parse
StatusCode parse(std::tuple< Tup... > &tup, const Gaudi::Parsers::InputData &input)
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:284
AthAnalysisHelper::setProperty
static StatusCode setProperty(const std::string &name, const std::string &property, const W &value, bool override=true)
Definition: AthAnalysisHelper.h:79
StoreGatePyExt.h
POOL::TEvent::setActive
void setActive()
ensure we are the active store, for the purpose of element links etc
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:120
Gaudi::Init
IAppMgrUI * Init()
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:22
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
ParticleTest.tp
tp
Definition: ParticleTest.py:25
ICollectionSize.h
Abstract interface for finding the size of an event collection.
POOL::TEvent::get_item
PyObject * get_item(PyObject *pykey)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:209
POOL::TEvent::kTreeAccess
@ kTreeAccess
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:43
POOL::Init
IAppMgrUI * Init(const char *options="POOLRootAccess/basic.opts")
Bootstraps (creates and configures) the Gaudi Application with the provided options file.
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:29
AthenaInternal::py_sg_contains
PyObject * py_sg_contains(PyObject *storeGateSvc, PyObject *tp, PyObject *key)
Definition: StoreGatePyExt.cxx:346
POOL::TEvent::contains
PyObject * contains(PyObject *tp, PyObject *pykey)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:212
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
POOL::TEvent::readFrom
StatusCode readFrom(TFile *file)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:133
POOL::TEvent::EReadMode
EReadMode
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:42
POOL::TEvent::getEntry
int getEntry(long entry)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:185
StdJOSetup.ppSvc
ppSvc
Definition: StdJOSetup.py:38
lumiFormat.i
int i
Definition: lumiFormat.py:85
POOL::TEvent::m_size
long m_size
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:114
POOL::TEvent::getEntries
long getEntries()
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:124
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
POOL::TEvent::m_evtSelect
ServiceHandle< IEvtSelectorSeek > m_evtSelect
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:122
generateReferenceFile.files
files
Definition: generateReferenceFile.py:12
file
TFile * file
Definition: tile_monitor.h:29
ICollectionSize
Abstract interface for finding the size of an event collection.
Definition: ICollectionSize.h:26
Preparation.mode
mode
Definition: Preparation.py:94
python.AtlRunQueryLib.options
options
Definition: AtlRunQueryLib.py:379
AthenaInternal::py_sg_getitem
PyObject * py_sg_getitem(PyObject *storeGateSvc, PyObject *key)
Definition: StoreGatePyExt.cxx:414
POOL::TEvent::m_incSvc
ServiceHandle< IIncidentSvc > m_incSvc
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:118
POOL::TEvent::m_evtProcessor
IEventProcessor * m_evtProcessor
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:116
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
POOL::TEvent
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:40
TEvent.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
POOL::TEvent::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:60
POOL
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:34
POOL::TEvent::setEvtSelProperty
void setEvtSelProperty(const char *name, const T &val)
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:63
AthAnalysisHelper.h
AthAnalysisHelper::initGaudi
static IAppMgrUI * initGaudi(const char *options="")
initGaudi method starts the gaudi ApplicationMgr ready for working with all the components
Definition: AthAnalysisHelper.cxx:21
POOL::TEvent::~TEvent
~TEvent()
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:33
POOL::TEvent::kPOOLAccess
@ kPOOLAccess
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:44
POOL::TEvent::retrieve
StatusCode retrieve(const T *&obj)
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:74
POOL::TEvent::m_evtLoop
ServiceHandle< IEventSeek > m_evtLoop
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:121
POOL::TEvent::record
PyObject * record(PyObject *obj, PyObject *pykey, bool allowMods=true, bool resetOnly=true, bool noHist=false)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:219
POOL::TEvent::inputMetaStore
ServiceHandle< StoreGateSvc > & inputMetaStore()
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:61
Gaudi
=============================================================================
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:273
POOL::TEvent::m_evtStore
ServiceHandle< StoreGateSvc > m_evtStore
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:123
python.PyAthena.obj
obj
Definition: PyAthena.py:132
PyObject
_object PyObject
Definition: IPyComponent.h:26
AthenaInternal::retrieveObjectFromStore
PyObject * retrieveObjectFromStore(PyObject *storeGateSvc, PyObject *tp, PyObject *key)
Definition: StoreGatePyExt.cxx:101
POOL::TEvent::TEvent
TEvent(const std::string &name="StoreGateSvc")
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:46
AthenaInternal::recordObjectToStore
PyObject * recordObjectToStore(StoreGateSvc *, PyObject *, PyObject *, bool, bool, bool)
record object to storegate
Definition: StoreGatePyExt.cxx:476
ServiceHandle
Definition: ClusterMakerTool.h:37