ATLAS Offline Software
RPyEvent.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
3  */
14 #include "TPython.h"
15 
16 
17 namespace xAOD::Experimental {
18 
19 
21 PyObject* RPyEvent::pyRetrieve(const std::string& key) {
22 
23  // Try to find the event format corresponding to this key.
24  const EventFormat* ef = this->inputEventFormat();
25  const EventFormatElement* efe = ((ef != nullptr) ? ef->get(key) : nullptr);
26  if (efe == nullptr) {
27  ef = this->outputEventFormat();
28  efe = ((ef != nullptr) ? ef->get(key) : nullptr);
29  if (efe == nullptr) {
30  // Fail if we can't find the type.
31  Py_INCREF(Py_None);
32  return Py_None;
33  }
34  }
35 
36  // Convert the type to a std::type_info.
37  ::TClass* cl = TClass::GetClass(efe->className().c_str());
38  if (cl == nullptr) {
39  Py_INCREF(Py_None);
40  return Py_None;
41  }
42  const std::type_info* ti = cl->GetTypeInfo();
43  if (ti == nullptr) {
44  Py_INCREF(Py_None);
45  return Py_None;
46  }
47 
48  // Get the object from the store as a void*.
49  static constexpr bool SILENT = true;
50  static constexpr bool METADATA = false;
51  void* obj ATLAS_THREAD_SAFE =
52  const_cast<void*>(this->getInputObject(key, *ti, SILENT, METADATA));
53  if (obj == nullptr) {
54  obj = this->getOutputObject(key, *ti, METADATA);
55  }
56  if (obj == nullptr) {
57  Py_INCREF(Py_None);
58  return Py_None;
59  }
60 
61  // Convert to a PyObject.
62  return TPython::CPPInstance_FromVoidPtr(obj, efe->className().c_str());
63 }
64 
66 bool RPyEvent::pyContains(const std::string& key, const std::string& type) {
67 
68  // Try to access the dictionary of this type.
69  ::TClass* cl = ::TClass::GetClass(type.c_str());
70  if (cl == nullptr) {
71  ::Warning("xAOD::TPyEvent::pyContains", "Type name \"%s\" not known",
72  type.c_str());
73  return false;
74  }
75 
76  // Check if the dictionary can return a type_info.
77  const std::type_info* ti = cl->GetTypeInfo();
78  if (ti == nullptr) {
79  ::Warning("xAOD::TPyEvent::pyContains",
80  "Type \"%s\" doesn't have a proper dictionary", type.c_str());
81  return false;
82  }
83 
84  // Let the base class do the work.
85  static constexpr bool METADATA = false;
86  return REvent::contains(key, *ti, METADATA);
87 }
88 
90 bool RPyEvent::pyTransientContains(const std::string& key,
91  const std::string& type) const {
92 
93  // Try to access the dictionary of this type.
94  ::TClass* cl = ::TClass::GetClass(type.c_str());
95  if (cl == nullptr) {
96  ::Warning("xAOD::TPyEvent::pyTransientContains",
97  "Type name \"%s\" not known", type.c_str());
98  return false;
99  }
100 
101  // Check if the dictionary can return a type_info:
102  const std::type_info* ti = cl->GetTypeInfo();
103  if (ti == nullptr) {
104  ::Warning("xAOD::TPyEvent::pyTransientContains",
105  "Type \"%s\" doesn't have a proper dictionary", type.c_str());
106  return false;
107  }
108 
109  // Let the base class do the work:
110  return REvent::transientContains(key, *ti, kFALSE);
111 }
112 
113 
114 } // namespace xAOD::Experimental
xAOD::Event::ATLAS_THREAD_SAFE
SG::SGKeyMap< BranchInfo > m_branches ATLAS_THREAD_SAFE
Map from hashed sgkey to BranchInfo.
Definition: Event.h:358
xAOD::Experimental::RPyEvent::pyTransientContains
bool pyTransientContains(const std::string &key, const std::string &type) const
Function checking if an object is already in memory.
Definition: RPyEvent.cxx:90
xAOD::Experimental
Definition: REvent.cxx:67
xAOD::EventFormatElement
Class describing one branch of the ROOT file.
Definition: EventFormatElement.h:35
ReturnCheck.h
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
xAOD::Event::inputEventFormat
const EventFormat * inputEventFormat() const
Get information about the input objects.
Definition: EventCore.cxx:228
xAOD::Event::getOutputObject
void * getOutputObject(SG::sgkey_t key, const std::type_info &ti) override
Function for retrieving an output object in a non-template way.
Definition: EventTVirtualEvent.cxx:138
xAOD::EventFormatElement::className
const std::string & className() const
Get the class name of this branch/key.
Definition: EventFormatElement.cxx:36
xAOD::Event::getInputObject
const void * getInputObject(SG::sgkey_t key, const std::type_info &ti, bool silent) override
Function for retrieving an input object in a non-template way.
Definition: EventTVirtualEvent.cxx:162
xAOD::Experimental::RPyEvent::pyRetrieve
PyObject * pyRetrieve(const std::string &key)
Return the object with a given key as a PyObject.
Definition: RPyEvent.cxx:21
xAOD::EventFormat_v1
Event format metadata for xAOD files.
Definition: EventFormat_v1.h:38
xAOD::Event::contains
bool contains(const std::string &key)
Function checking if an object is available from the store.
xAOD::Experimental::RPyEvent::pyContains
bool pyContains(const std::string &key, const std::string &type)
Function checking if an object is available from the store.
Definition: RPyEvent.cxx:66
RPyEvent.h
Python interface to xAOD::REvent.
checker_macros.h
Define macros for attributes used to control the static checker.
python.PyAthena.obj
obj
Definition: PyAthena.py:132
PyObject
_object PyObject
Definition: IPyComponent.h:26
dq_make_web_display.cl
cl
print [x.__class__ for x in toList(dqregion.getSubRegions()) ]
Definition: dq_make_web_display.py:25
xAOD::Event::outputEventFormat
const EventFormat * outputEventFormat() const
Get information about the output objects.
Definition: EventCore.cxx:236
xAOD::Event::transientContains
bool transientContains(const std::string &key) const
Function checking if an object is already in memory.
LheEventFiller_Common.ef
ef
Definition: SFGen_i/share/common/LheEventFiller_Common.py:7
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37