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_joSvc("JobOptionsSvc","TEvent"+name),
51  m_evtLoop("AthenaEventLoopMgr/"+name+"_EventLoopMgr","TEvent"+name),
52  m_evtSelect("EventSelectorAthenaPool/"+name+"_EventSelector","TEvent"+name),
53  m_evtStore("StoreGateSvc/"+name,"TEvent"+name),
54  m_inputMetaStore("InputMetaDataStore","TEvent"+name) /*fixme, when reading multiple files at once?*/ {
55 
56  Gaudi::Init();
57 
58  //SEEMS TO CAUSE INITIALIZE LOOP if(name=="StoreGateSvc") m_evtSelect.setName("EventSelector"); //use default selector name if we use default storegate
59 
60  //FIXME: Should protect against attempt to mix POOL with nonPOOL
61  if(mode==kPOOLAccess) {
62  //add AthenaPoolCnvSvc to the EventPersistencySvc (ideally the selector should add this itself!!)
63  ServiceHandle<IService> epSvc("EventPersistencySvc","TEvent"+name);
64  AAH::setProperty( epSvc , "CnvServices" , "['AthenaPoolCnvSvc']" ).ignore(); //FIXME: perhaps should append rather than overwrite
65  //add the AthenaPoolAddressProviderSvc to ProxyProviderSvc
66  ServiceHandle<IService> ppSvc("ProxyProviderSvc","TEvent"+name);
67  AAH::setProperty( ppSvc , "ProviderNames", "['MetaDataSvc', 'AthenaPoolAddressProviderSvc']" ).ignore();
68  } else if(mode==kTreeAccess) {
69  //switch selector type to Athena::RootNtupleEventSelector
70  m_evtSelect.setTypeAndName("Athena::RootNtupleEventSelector/"+m_evtSelect.name());
71  //also default to 'CollectionTree' as the tree to read
72  this->setEvtSelProperty("TupleName","CollectionTree");
73  } else {
74  //switch selector type to xAODEventSelector:
75  m_evtSelect.setTypeAndName("Athena::xAODEventSelector/"+m_evtSelect.name());
76  }
77 
78  //check if a SelectorType has been specified in the joSvc
79  //should retire this code at some point (hangover from basicxAOD.opts)
80  if (m_joSvc->has("TEvent.EventSelectorType")) {
81  m_evtSelect.setTypeAndName(m_joSvc->get("TEvent.EventSelectorType") + "/" + m_evtSelect.name());
82  }
83 
84  AAH::setProperty( m_evtLoop , "ClearStorePolicy", "BeginEvent" ).ignore(); //for interactive use of storegate
85  AAH::setProperty( m_evtLoop , "EvtSel", m_evtSelect.typeAndName() ).ignore(); //connect loop to selector
86  AAH::setProperty( m_evtLoop , "EvtStore", m_evtStore.typeAndName() ).ignore();//connect loop to store
87  AAH::setProperty( m_evtLoop , "EventPrintoutInterval", 0 ).ignore(); //disable printout (speeds up loop)
88 
89  if(m_evtSelect.type()=="Athena::xAODEventSelector") {
90  AAH::setProperty( m_evtSelect , "ReadMetaDataWithPool" , true).ignore(); //uses hybrid xAOD reading by default
91  AAH::setProperty( m_evtSelect , "AccessMode" , int(mode) ).ignore(); //sets the mode
92  AAH::setProperty( m_evtSelect , "EvtStore" , m_evtStore.typeAndName() ).ignore();
93  //FIXME ... cant get dual event stores working :-(
94  //AAH::setProperty( m_evtSelect , "ProxyProviderSvc" , "ProxyProviderSvc/" + name + "_ProxyProviderSvc" );
95  //AAH::setProperty( m_evtStore , "ProxyProviderSvc", "ProxyProviderSvc/" + name + "_ProxyProviderSvc" );
96  }
97 
98  //set outputlevels to WARNING
99  AAH::setProperty( m_evtLoop, "OutputLevel", 4 ).ignore();
100  AAH::setProperty( m_evtSelect, "OutputLevel", 4 ).ignore();
101  //AAH::setProperty( m_evtStore, "OutputLevel", 4 ).ignore();
102 
103  //suppress messages below WARNING too
104  //do this here to stop some pre initialize INFO messages from showing
105  ServiceHandle<IProperty> messageSvc("MessageSvc","");
106  std::vector<std::string> p;
107  Gaudi::Parsers::parse(p,messageSvc->getProperty("setWarning").toString()).ignore();
108  p.push_back( m_evtLoop.name() );
109  p.push_back( m_evtSelect.name() );
110  p.push_back( m_evtStore.name() );
111  messageSvc->setProperty( StringArrayProperty( "setWarning" , p ) ).ignore();
112 
113  //also push this into the joboptionsvc so that if a reinit happens then these settings are remembered
114  //m_joSvc->addPropertyToCatalogue( "MessageSvc", StringArrayProperty( "setWarning", p ) );
115 
116 }
117 
118 
120  m_evtStore->makeCurrent();
121 }
122 
124  if(m_size>=0) return m_size;
125  if(!m_evtLoop.isSet()) { if(m_evtLoop.retrieve().isFailure()) return 0; }
126  if (ICollectionSize* isize = dynamic_cast<ICollectionSize*>(&*m_evtLoop)) {
127  return isize->size();
128  }
129  return 0;
130 }
131 
133  if(file==0 || file->IsZombie()) return StatusCode::FAILURE;
134  return readFrom( file->GetName() );
135 }
136 
138  if(m_curEntry >= 0) {
139  std::cout << "Unable to change file after already reading" << std::endl;
140  return StatusCode::FAILURE;
141  }
142  std::vector<std::string> myFiles;
143  //see if contains wildcard
144  //separate by comma
145  TString sFileIn(file);
146  std::unique_ptr<TObjArray> sFiles(sFileIn.Tokenize(","));
147  std::unique_ptr<TIterator> itr(sFiles->MakeIterator());
148  TObject* obj;
149  while( (obj = itr->Next()) ) {
150  TString sFile(static_cast<TObjString*>(obj)->String());
151  if(sFile.Contains('*')) {
152  std::unique_ptr<TObjArray> theFiles(gSystem->GetFromPipe(("ls " + std::string(file)).c_str()).Tokenize("\n"));
153  for(int i=0;i<theFiles->GetEntries();i++) {
154  //std::cout << "Adding " << dynamic_cast<TObjString*>(theFiles->At(i))->String().Data() << std::endl;
155  if (TObjString* objstr = dynamic_cast<TObjString*>(theFiles->At(i))) {
156  myFiles.push_back(gSystem->ExpandPathName(objstr->String().Data()));
157  }
158  }
159  } else {
160  myFiles.push_back( gSystem->ExpandPathName(sFile.Data()) );
161  }
162  }
163 
164  return AAH::setProperty( m_evtSelect , "InputCollections" , myFiles );
165 }
166 
168 
169  //add all files to the property list by giving a comma separated list of files
170  std::string sfiles;
171  std::unique_ptr<TIterator> itr(files->GetListOfFiles()->MakeIterator());
172  TObject* obj;
173  bool hasFile(false);
174  while( (obj = itr->Next()) ) {
175  if(hasFile) sfiles += ",";
176  sfiles += obj->GetTitle();
177  hasFile=true;
178  }
179  //std::cout << sfiles << std::endl;
180  return readFrom(sfiles.c_str());
181 
182 }
183 
184 int TEvent::getEntry( long entry ) {
185  if(!m_evtLoop.isSet()) { if(m_evtLoop.retrieve().isFailure()) return -1; }
186  if(entry==0 || entry != m_curEntry+1) {
187  //need to seek on first read or if not reading next event
188  if (m_evtLoop->seek(entry).isFailure()) return -1;
189  }
190  //ensure our storegate is the active store
191  setActive();
192  if (m_evtProcessor == nullptr) {
193  m_evtProcessor = dynamic_cast<IEventProcessor*>(&*m_evtLoop);
194  }
195  StatusCode out = m_evtProcessor->nextEvent(entry+1);
196  m_curEntry = entry;
197  return (out.isSuccess()) ? 0 : -1;
198 }
199 
200  //python bindings
201 
203  {
204  return AthenaInternal::retrieveObjectFromStore( &*evtStore() , tp, pykey );
205  }
206 
208  return AthenaInternal::py_sg_getitem( &*evtStore() , pykey );
209  }
211  return AthenaInternal::py_sg_contains( &*evtStore() , tp, pykey );
212  }
214  if(!m_evtLoop.isSet()) { if(m_evtLoop.retrieve().isFailure()) return 0; }
216  }
217  PyObject* TEvent::record (PyObject* obj, PyObject* pykey,bool allowMods,bool resetOnly,bool noHist) {
218  return AthenaInternal::recordObjectToStore( &*evtStore() , obj, pykey,allowMods,resetOnly,noHist );
219  }
220 
221 
222 
223 } //end of POOL namespace
POOL::TEvent::m_joSvc
ServiceHandle< Gaudi::Interfaces::IOptionsSvc > m_joSvc
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:116
POOL::TEvent::retrieveMetaInput
StatusCode retrieveMetaInput(const T *&obj, const std::string &key)
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:89
POOL::TEvent::m_curEntry
long m_curEntry
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:112
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
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:119
Gaudi::Init
IAppMgrUI * Init()
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:22
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:207
POOL::TEvent::kTreeAccess
@ kTreeAccess
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:42
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:210
POOL::TEvent::readFrom
StatusCode readFrom(TFile *file)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:132
POOL::TEvent::EReadMode
EReadMode
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:41
POOL::TEvent::getEntry
int getEntry(long entry)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:184
StdJOSetup.ppSvc
ppSvc
Definition: StdJOSetup.py:37
lumiFormat.i
int i
Definition: lumiFormat.py:92
POOL::TEvent::m_size
long m_size
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:113
POOL::TEvent::getEntries
long getEntries()
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:123
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:119
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:31
Preparation.mode
mode
Definition: Preparation.py:96
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_evtProcessor
IEventProcessor * m_evtProcessor
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:115
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
POOL::TEvent
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:39
TEvent.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
POOL::TEvent::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:59
test_pythinning.out
out
Definition: test_pythinning.py:94
POOL
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:33
POOL::TEvent::setEvtSelProperty
void setEvtSelProperty(const char *name, const T &val)
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:62
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:43
POOL::TEvent::retrieve
StatusCode retrieve(const T *&obj)
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:73
POOL::TEvent::m_evtLoop
ServiceHandle< IEventSeek > m_evtLoop
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:118
POOL::TEvent::record
PyObject * record(PyObject *obj, PyObject *pykey, bool allowMods=true, bool resetOnly=true, bool noHist=false)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:217
POOL::TEvent::inputMetaStore
ServiceHandle< StoreGateSvc > & inputMetaStore()
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:60
Gaudi
=============================================================================
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:273
POOL::TEvent::m_evtStore
ServiceHandle< StoreGateSvc > m_evtStore
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:120
python.PyAthena.obj
obj
Definition: PyAthena.py:135
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