ATLAS Offline Software
Loading...
Searching...
No Matches
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
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 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
133StatusCode TEvent::readFrom( TFile* file ) {
134 if(file==0 || file->IsZombie()) return StatusCode::FAILURE;
135 return readFrom( file->GetName() );
136}
137
138StatusCode TEvent::readFrom( const char* file ) {
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
168StatusCode TEvent::readFrom(TChain* files) {
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
185int 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
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:50
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