ATLAS Offline Software
Loading...
Searching...
No Matches
PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 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
21namespace Gaudi {
22 IAppMgrUI* Init() {
23 return AAH::initGaudi("POOLRootAccess/basic.opts");
24 }
25}
26
27namespace POOL {
28
29IAppMgrUI* 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
46TEvent::TEvent(const std::string& name ) : TEvent( kPOOLAccess , name ) { }
47
48TEvent::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
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 AAH::setProperty(m_evtStore, "PruneIncidents", true).ignore();
91
92 if(m_evtSelect.type()=="Athena::xAODEventSelector") {
93 AAH::setProperty( m_evtSelect , "ReadMetaDataWithPool" , true).ignore(); //uses hybrid xAOD reading by default
94 AAH::setProperty( m_evtSelect , "AccessMode" , int(mode) ).ignore(); //sets the mode
95 AAH::setProperty( m_evtSelect , "EvtStore" , m_evtStore.typeAndName() ).ignore();
96 //FIXME ... cant get dual event stores working :-(
97 //AAH::setProperty( m_evtSelect , "ProxyProviderSvc" , "ProxyProviderSvc/" + name + "_ProxyProviderSvc" );
98 //AAH::setProperty( m_evtStore , "ProxyProviderSvc", "ProxyProviderSvc/" + name + "_ProxyProviderSvc" );
99 }
100
101 //set outputlevels to WARNING
102 AAH::setProperty( m_evtLoop, "OutputLevel", 4 ).ignore();
103 AAH::setProperty( m_evtSelect, "OutputLevel", 4 ).ignore();
104 //AAH::setProperty( m_evtStore, "OutputLevel", 4 ).ignore();
105
106 //suppress messages below WARNING too
107 //do this here to stop some pre initialize INFO messages from showing
108 ServiceHandle<IProperty> messageSvc("MessageSvc","");
109 std::vector<std::string> p;
110 Gaudi::Parsers::parse(p,messageSvc->getProperty("setWarning").toString()).ignore();
111 p.push_back( m_evtLoop.name() );
112 p.push_back( m_evtSelect.name() );
113 p.push_back( m_evtStore.name() );
114 messageSvc->setProperty( StringArrayProperty( "setWarning" , p ) ).ignore();
115
116 //also push this into the joboptionsvc so that if a reinit happens then these settings are remembered
117 //m_joSvc->addPropertyToCatalogue( "MessageSvc", StringArrayProperty( "setWarning", p ) );
118
119}
120
121
123 m_evtStore->makeCurrent();
124}
125
127 if(m_size>=0) return m_size;
128 if(!m_evtLoop.isSet()) { if(m_evtLoop.retrieve().isFailure()) return 0; }
129 if (ICollectionSize* isize = dynamic_cast<ICollectionSize*>(&*m_evtLoop)) {
130 return isize->size();
131 }
132 return 0;
133}
134
135StatusCode TEvent::readFrom( TFile* file ) {
136 if(file==0 || file->IsZombie()) return StatusCode::FAILURE;
137 return readFrom( file->GetName() );
138}
139
140StatusCode TEvent::readFrom( const char* file ) {
141 if(m_curEntry >= 0) {
142 std::cout << "Unable to change file after already reading" << std::endl;
143 return StatusCode::FAILURE;
144 }
145 std::vector<std::string> myFiles;
146 //see if contains wildcard
147 //separate by comma
148 TString sFileIn(file);
149 std::unique_ptr<TObjArray> sFiles(sFileIn.Tokenize(","));
150 std::unique_ptr<TIterator> itr(sFiles->MakeIterator());
151 TObject* obj;
152 while( (obj = itr->Next()) ) {
153 TString sFile(static_cast<TObjString*>(obj)->String());
154 if(sFile.Contains('*')) {
155 std::unique_ptr<TObjArray> theFiles(gSystem->GetFromPipe(("ls " + std::string(file)).c_str()).Tokenize("\n"));
156 for(int i=0;i<theFiles->GetEntries();i++) {
157 //std::cout << "Adding " << dynamic_cast<TObjString*>(theFiles->At(i))->String().Data() << std::endl;
158 if (TObjString* objstr = dynamic_cast<TObjString*>(theFiles->At(i))) {
159 myFiles.push_back(gSystem->ExpandPathName(objstr->String().Data()));
160 }
161 }
162 } else {
163 myFiles.push_back( gSystem->ExpandPathName(sFile.Data()) );
164 }
165 }
166
167 return AAH::setProperty( m_evtSelect , "InputCollections" , myFiles );
168}
169
170StatusCode TEvent::readFrom(TChain* files) {
171
172 //add all files to the property list by giving a comma separated list of files
173 std::string sfiles;
174 std::unique_ptr<TIterator> itr(files->GetListOfFiles()->MakeIterator());
175 TObject* obj;
176 bool hasFile(false);
177 while( (obj = itr->Next()) ) {
178 if(hasFile) sfiles += ",";
179 sfiles += obj->GetTitle();
180 hasFile=true;
181 }
182 //std::cout << sfiles << std::endl;
183 return readFrom(sfiles.c_str());
184
185}
186
187int TEvent::getEntry( long entry ) {
188 if(!m_evtLoop.isSet()) { if(m_evtLoop.retrieve().isFailure()) return -1; }
189 if(entry==0 || entry != m_curEntry+1) {
190 //need to seek on first read or if not reading next event
191 if (m_evtLoop->seek(entry).isFailure()) return -1;
192 }
193 //ensure our storegate is the active store
194 setActive();
195 if (m_evtProcessor == nullptr) {
196 m_evtProcessor = dynamic_cast<IEventProcessor*>(&*m_evtLoop);
197 }
198 if (!m_evtProcessor){
199 std::cout << "m_evtProcessor is still nullptr" << std::endl;
200 return -1;
201 }
202 StatusCode out = m_evtProcessor->nextEvent(entry+1);
203 m_curEntry = entry;
204
205 if (m_listenerState == UNCHECKED) {
206 std::vector<IIncidentListener*> l;
207 m_incSvc->getListeners (l, "BeginEvent");
209 }
210
212 m_incSvc->fireIncident(Incident("BeginEvent",IncidentType::BeginEvent));
213 }
214 return (out.isSuccess()) ? 0 : -1;
215}
216
217 //python bindings
218
220 {
221 return AthenaInternal::retrieveObjectFromStore( &*evtStore() , tp, pykey );
222 }
223
225 return AthenaInternal::py_sg_getitem( &*evtStore() , pykey );
226 }
228 return AthenaInternal::py_sg_contains( &*evtStore() , tp, pykey );
229 }
231 if(!m_evtLoop.isSet()) { if(m_evtLoop.retrieve().isFailure()) return 0; }
233 }
234 PyObject* TEvent::record (PyObject* obj, PyObject* pykey,bool allowMods,bool resetOnly,bool noHist) {
235 return AthenaInternal::recordObjectToStore( &*evtStore() , obj, pykey,allowMods,resetOnly,noHist );
236 }
237
238
239
240} //end of POOL namespace
Abstract interface for finding the size of an event collection.
_object PyObject
static StatusCode setProperty(const GaudiHandleBase &toolHandle, const std::string &property, const int &value, bool override=true)
static IAppMgrUI * initGaudi(const char *options="")
initGaudi method starts the gaudi ApplicationMgr ready for working with all the components
Abstract interface for finding the size of an event collection.
TEvent(const std::string &name="StoreGateSvc")
PyObject * contains(PyObject *tp, PyObject *pykey)
ServiceHandle< IEvtSelectorSeek > m_evtSelect
PyObject * record(PyObject *obj, PyObject *pykey, bool allowMods=true, bool resetOnly=true, bool noHist=false)
ServiceHandle< Gaudi::Interfaces::IOptionsSvc > m_joSvc
ServiceHandle< StoreGateSvc > & evtStore()
StatusCode retrieveMetaInput(const T *&obj, const std::string &key)
void setActive()
ensure we are the active store, for the purpose of element links etc
ServiceHandle< StoreGateSvc > & inputMetaStore()
void setEvtSelProperty(const char *name, const T &val)
std::vector< std::string > files
file names and file pointers
Definition hcg.cxx:52
PyObject * retrieveObjectFromStore(PyObject *storeGateSvc, PyObject *tp, PyObject *key)
PyObject * py_sg_getitem(PyObject *storeGateSvc, PyObject *key)
PyObject * py_sg_contains(PyObject *storeGateSvc, PyObject *tp, PyObject *key)
PyObject * recordObjectToStore(StoreGateSvc *, PyObject *, PyObject *, bool, bool, bool)
record object to storegate
StatusCode parse(std::tuple< Tup... > &tup, const Gaudi::Parsers::InputData &input)
=============================================================================
IAppMgrUI * Init(const char *options="POOLRootAccess/basic.opts")
Bootstraps (creates and configures) the Gaudi Application with the provided options file.
TFile * file