ATLAS Offline Software
AANTEventSelector.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 //====================================================================
6 // AANTEventSelector.cxx
7 //====================================================================
8 //
9 // Include files.
10 
14 
15 #include "GaudiKernel/ISvcLocator.h"
16 #include "GaudiKernel/StatusCode.h"
17 #include "GaudiKernel/MsgStream.h"
18 #include "GaudiKernel/ClassID.h"
19 
20 #include "EventInfo/EventInfo.h"
21 #include "EventInfo/EventType.h"
22 #include "EventInfo/EventID.h"
23 #include "StoreGate/StoreGateSvc.h"
24 
25 #include "TChain.h"
26 
27 TChain *AANTTreeGate::m_tree = 0;
28 
29 // Constructor.
30 AANTEventSelector::AANTEventSelector(const std::string& name, ISvcLocator* svcloc)
31  : AthService(name, svcloc),
32  m_storeGate(0),
33  m_skipEvents(0), m_numEvents(0), m_totalNEvents(0),
34  m_tree(0),
35  m_runNumber(0),
36  m_eventNumber(0),
37  m_convFunc(0), m_selectionFunc(0)
38 {
39  declareProperty("InputCollections", m_inputCollectionsProp);
40  declareProperty("SkipEvents", m_skipEvents = 0);
41  declareProperty("Converter", m_strConverter = "");
42  declareProperty("Selection", m_strSelection = "");
43 }
44 
45 
46 // Destructor.
48 {
49  if (m_tree)
50  delete m_tree;
51 }
52 
53 
54 // AANTEventSelector::initialize().
56 {
57  // Initialize the Service base class.
59 
60  // Create a message stream.
61  MsgStream log(msgSvc(), name());
62 
63  log << MSG::DEBUG << "initialize()" << endmsg;
64 
65  if (sc.isFailure())
66  {
67  log << MSG::ERROR << "Unable to initialize Service base class" << endmsg;
68  return sc;
69  }
70 
71  sc = service("StoreGateSvc", m_storeGate);
72  if (sc.isFailure())
73  {
74  log << MSG::ERROR << "Unable to retrieve pointer to StoreGateSvc" << endmsg;
75  return sc;
76  }
77 
78 
79  // parse jobO
80  if (m_inputCollectionsProp.value().size() == 0)
81  {
82  log << MSG::ERROR << "Use the property:"
83  << " EventSelector.InputCollections = [ \"<collectionName>\" ] (list of collections)"
84  << endmsg;
85  return StatusCode::FAILURE;
86  }
87  // create TChain
88  m_tree = new TChain("CollectionTree");
89  std::vector<std::string> inputColl = m_inputCollectionsProp.value();
90  std::vector<std::string>::iterator it = inputColl.begin();
91  std::vector<std::string>::iterator itE = inputColl.end();
92  for (; it!=itE; ++it)
93  {
94  log << MSG::DEBUG << "Add : " << *it << endmsg;
95  m_tree->Add(it->c_str());
96  }
97 
98  // get total number of events
99  m_totalNEvents = m_tree->GetEntries();
100 
101  log << MSG::DEBUG << "Total Events : " << m_totalNEvents << endmsg;
102 
103  // RunNumber and EventNumber
104  m_tree->SetBranchAddress("EventNumber",&m_eventNumber);
105  m_tree->SetBranchAddress("RunNumber", &m_runNumber);
106 
107  // set TTree to AANTTreeGate
109 
110  // selection criteria
111  log << MSG::DEBUG << "Load Sel: " << m_strSelection << " from __main__" << endmsg;
112  char smain[] = "__main__";
113 #if PY_MAJOR_VERSION < 3
114  m_selectionFunc = PyObject_GetAttr(PyImport_AddModule(smain),PyString_FromString(m_strSelection.c_str()));
115 #else
116  m_selectionFunc = PyObject_GetAttr(PyImport_AddModule(smain),PyUnicode_FromString(m_strSelection.c_str()));
117 #endif
118  if (m_selectionFunc == NULL)
119  {
120  log << MSG::ERROR << "Could not load sel : " << m_strSelection << endmsg;
121  return StatusCode::FAILURE;
122  }
123 
124  log << MSG::DEBUG << "Load Cnv: " << m_strConverter << " from __main__" << endmsg;
125 #if PY_MAJOR_VERSION < 3
126  m_convFunc = PyObject_GetAttr(PyImport_AddModule(smain),PyString_FromString(m_strConverter.c_str()));
127 #else
128  m_convFunc = PyObject_GetAttr(PyImport_AddModule(smain),PyUnicode_FromString(m_strConverter.c_str()));
129 #endif
130  if (m_convFunc == NULL)
131  {
132  log << MSG::ERROR << "Could not load conv : " << m_strConverter << endmsg;
133  return StatusCode::FAILURE;
134  }
135 
136  return StatusCode::SUCCESS;
137 }
138 
139 
140 // createContext
141 StatusCode AANTEventSelector::createContext(IEvtSelector::Context*& it) const
142 {
143  it = new AANTEventContext(this);
144  return StatusCode::SUCCESS;
145 }
146 
147 
148 // Implementation of IEvtSelector::next().
149 StatusCode AANTEventSelector::next(IEvtSelector::Context& it)const
150 {
151  return this->next(it,0);
152 }
153 
154 
155 // jump
156 StatusCode AANTEventSelector::next(IEvtSelector::Context& it, int jump) const
157 {
158  MsgStream log(msgSvc(), name());
159  log << MSG::DEBUG << "next(" << jump << ") : iEvt " << m_numEvents << endmsg;
160 
161  // get EventContext
162  AANTEventContext* ct = dynamic_cast<AANTEventContext*>(&it);
163  if (ct == 0)
164  {
165  log << MSG::ERROR << "Could not dcast to AANTEventContext" << endmsg;
166  return StatusCode::FAILURE;
167  }
168  // jump
169  if (((m_numEvents + jump) >= 0) and ((m_numEvents + jump) < m_totalNEvents))
170  {
171  // move pointer
172  m_numEvents += jump;
173  // get entry
174  m_tree->GetEntry(m_numEvents);
175  // increment pointer
176  ++m_numEvents;
177  // convert C++ obj to Python obj
179  PyObject *pyObj = PyObject_CallObject(m_convFunc,NULL);
180  // execute Python code fragment
181  PyObject *tup = Py_BuildValue((char*)"(O)",pyObj);
182  PyObject *ret = PyObject_CallObject(m_selectionFunc,tup);
183  // decrement reference counters
184  Py_DECREF(pyObj);
185  Py_DECREF(tup);
186  if (ret != NULL and PyObject_IsTrue(ret))
187  {
188  // decrement reference counters
189  Py_DECREF(ret);
190  // EventInfo
191  EventType *peT = new EventType();
192  EventInfo *evtInfo = new EventInfo( new EventID(m_runNumber,m_eventNumber,0),peT);
193  StatusCode sc = m_storeGate->record(evtInfo, "AANTEventInfo");
194  if (sc.isFailure())
195  {
196  log << MSG::ERROR << "Could not record AANTEventInfo" << endmsg;
197  return sc;
198  }
199  // return
200  return StatusCode::SUCCESS;
201  }
202  else
203  {
204  // decrement reference counters
205  Py_XDECREF(ret);
206  // next
207  return this->next(it,0);
208  }
209  }
210 
211  // EOF
212  //ct = new AANTEventContext(0);
213  return StatusCode::FAILURE;
214 }
215 
216 
217 // previous
218 StatusCode AANTEventSelector::previous(IEvtSelector::Context& it) const
219 {
220  return this->next(it,-1);
221 }
222 
223 
224 // previous jump
225 StatusCode AANTEventSelector::previous(IEvtSelector::Context& it, int jump) const
226 {
227  return this->next(it,-jump);
228 }
229 
230 
231 // last
232 StatusCode AANTEventSelector::last(IEvtSelector::Context& /*it*/) const
233 {
234  MsgStream log(msgSvc(), name());
235  log << MSG::ERROR << "AANTEventSelector::last() not implemented" << endmsg;
236  return StatusCode::FAILURE;
237 }
238 
239 
240 // resetCriteria
241 StatusCode AANTEventSelector::resetCriteria(const std::string& /*criteria*/, IEvtSelector::Context& /*ctxt*/) const
242 {
243  MsgStream log(msgSvc(), name());
244  log << MSG::ERROR << "AANTEventSelector::resetCriteria() not implemented" << endmsg;
245  return StatusCode::FAILURE;
246 }
247 
248 
249 // rewind
250 StatusCode AANTEventSelector::rewind(IEvtSelector::Context& /*it*/) const
251 {
252  MsgStream log(msgSvc(), name());
253  log << MSG::ERROR << "AANTEventSelector::rewind() not implemented" << endmsg;
254  return StatusCode::FAILURE;
255 }
256 
257 
258 // createAddress
259 StatusCode AANTEventSelector::createAddress(const IEvtSelector::Context& /*it*/,
260  IOpaqueAddress*& /*iop*/) const
261 {
262  return StatusCode::SUCCESS;
263 }
264 
265 
266 // releaseContext
267 StatusCode AANTEventSelector::releaseContext(IEvtSelector::Context*& /*it*/) const
268 {
269  MsgStream log(msgSvc(), name());
270  log << MSG::ERROR << "AANTEventSelector::releaseContext() not implemented" << endmsg;
271  return StatusCode::FAILURE;
272 }
273 
274 
275 // Implementation of IInterface::queryInterface.
276 StatusCode AANTEventSelector::queryInterface(const InterfaceID& riid, void** ppvInterface)
277 {
278  if (riid == IEvtSelector::interfaceID())
279  {
280  *ppvInterface = (IEvtSelector*)this;
281  }
282  else if (riid == IProperty::interfaceID())
283  {
284  *ppvInterface = (IProperty*)this;
285  }
286  else
287  {
288  return Service::queryInterface(riid, ppvInterface);
289  }
290 
291  addRef();
292 
293  return StatusCode::SUCCESS;
294 }
295 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
StoreGateSvc::record
StatusCode record(T *p2BRegistered, const TKEY &key)
Record an object with a key.
AANTEventSelector::rewind
virtual StatusCode rewind(Context &it) const
Definition: AANTEventSelector.cxx:250
AANTEventSelector::m_tree
TChain * m_tree
Definition: AANTEventSelector.h:89
AANTEventContext
Definition: AANTEventContext.h:13
AANTEventSelector::m_convFunc
PyObject * m_convFunc
Definition: AANTEventSelector.h:95
AANTTreeGate::setTree
static void setTree(TChain *chain)
Definition: AANTEventSelector.h:103
AANTEventSelector::m_strConverter
std::string m_strConverter
Definition: AANTEventSelector.h:83
EventType
This class represents the "type of event" where the type is given by one or more "characteristics".
Definition: EventType.h:92
initialize
void initialize()
Definition: run_EoverP.cxx:894
AANTEventSelector::AANTEventSelector
AANTEventSelector(const std::string &name, ISvcLocator *svcloc)
Definition: AANTEventSelector.cxx:30
EventInfo
EventInfo
Definition: EventTPCnv.cxx:47
skel.it
it
Definition: skel.GENtoEVGEN.py:423
AANTEventContext.h
AANTEventSelector::initialize
virtual StatusCode initialize()
Definition: AANTEventSelector.cxx:55
AANTEventSelector.h
AANTEventSelector::previous
virtual StatusCode previous(Context &it) const
Definition: AANTEventSelector.cxx:218
AANTEventSelector::m_storeGate
StoreGateSvc * m_storeGate
Definition: AANTEventSelector.h:68
EventType.h
This class provides general information about an event. It extends EventInfo with a list of sub-evts ...
AANTEventSelector::m_skipEvents
int m_skipEvents
Definition: AANTEventSelector.h:74
AANTEventSelector::m_inputCollectionsProp
StringArrayProperty m_inputCollectionsProp
Definition: AANTEventSelector.h:71
AANTEventSelector::last
virtual StatusCode last(Context &it) const
Definition: AANTEventSelector.cxx:232
TruthTest.itE
itE
Definition: TruthTest.py:25
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
AANTEventSelector::resetCriteria
virtual StatusCode resetCriteria(const std::string &criteria, Context &context) const
Definition: AANTEventSelector.cxx:241
AANTEventSelector::m_runNumber
Long64_t m_runNumber
Definition: AANTEventSelector.h:91
RootUtils::PyGILStateEnsure
Definition: PyAthenaGILStateEnsure.h:20
EventID.h
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:36
ret
T ret(T t)
Definition: rootspy.cxx:260
AANTTreeGate::m_tree
static TChain * m_tree
Definition: AANTEventSelector.h:106
AANTEventSelector::queryInterface
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvInterface)
Definition: AANTEventSelector.cxx:276
AANTEventSelector::m_selectionFunc
PyObject * m_selectionFunc
Definition: AANTEventSelector.h:96
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
AthService
Definition: AthService.h:32
AANTEventSelector::~AANTEventSelector
~AANTEventSelector()
Definition: AANTEventSelector.cxx:47
AANTEventSelector::m_numEvents
long m_numEvents
Definition: AANTEventSelector.h:77
AANTEventSelector::createContext
virtual StatusCode createContext(Context *&it) const
Definition: AANTEventSelector.cxx:141
AANTEventSelector::m_strSelection
std::string m_strSelection
Definition: AANTEventSelector.h:86
AANTEventSelector::m_eventNumber
Long64_t m_eventNumber
Definition: AANTEventSelector.h:92
calibdata.ct
ct
Definition: calibdata.py:418
AANTEventSelector::m_totalNEvents
long m_totalNEvents
Definition: AANTEventSelector.h:80
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
EventInfo
This class provides general information about an event. Event information is provided by the accessor...
Definition: EventInfo/EventInfo/EventInfo.h:42
AANTEventSelector::releaseContext
virtual StatusCode releaseContext(Context *&it) const
Definition: AANTEventSelector.cxx:267
DEBUG
#define DEBUG
Definition: page_access.h:11
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
EventID
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition: EventID.h:35
PyAthenaGILStateEnsure.h
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
AANTEventSelector::next
virtual StatusCode next(Context &it) const
Definition: AANTEventSelector.cxx:149
AANTEventSelector::createAddress
virtual StatusCode createAddress(const Context &it, IOpaqueAddress *&iop) const
Definition: AANTEventSelector.cxx:259
PyObject
_object PyObject
Definition: IPyComponent.h:26
StoreGateSvc.h