ATLAS Offline Software
TPyEvent.cxx
Go to the documentation of this file.
1 // Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 
3 // Local include(s).
5 
7 
8 // Project include(s).
10 
11 // ROOT include(s).
12 #include <TClass.h>
13 #include <TError.h>
14 #include <TPython.h>
15 
16 namespace xAOD {
17 
19 
20 PyObject* TPyEvent::pyRetrieve(const std::string& key) {
21 
22  // Try to find the event format corresponding to this key.
23  const EventFormat* ef = this->inputEventFormat();
24  const EventFormatElement* efe = ((ef != nullptr) ? ef->get(key) : nullptr);
25  if (efe == nullptr) {
26  ef = this->outputEventFormat();
27  efe = ((ef != nullptr) ? ef->get(key) : nullptr);
28  if (efe == nullptr) {
29  // Fail if we can't find the type.
30  Py_INCREF(Py_None);
31  return Py_None;
32  }
33  }
34 
35  // Convert the type to a std::type_info.
36  ::TClass* cl = TClass::GetClass(efe->className().c_str());
37  if (cl == nullptr) {
38  Py_INCREF(Py_None);
39  return Py_None;
40  }
41  const std::type_info* ti = cl->GetTypeInfo();
42  if (ti == nullptr) {
43  Py_INCREF(Py_None);
44  return Py_None;
45  }
46 
47  // Get the object from the store as a void*.
48  static constexpr bool SILENT = true;
49  static constexpr bool METADATA = false;
50  void* obj ATLAS_THREAD_SAFE =
51  const_cast<void*>(this->getInputObject(key, *ti, SILENT, METADATA));
52  if (obj == nullptr) {
53  obj = this->getOutputObject(key, *ti, METADATA);
54  }
55  if (obj == nullptr) {
56  Py_INCREF(Py_None);
57  return Py_None;
58  }
59 
60  // Convert to a PyObject.
61  return TPython::CPPInstance_FromVoidPtr(obj, efe->className().c_str());
62 }
63 
64 bool TPyEvent::pyContains(const std::string& key, const std::string& type) {
65 
66  // Try to access the dictionary of this type.
67  ::TClass* cl = ::TClass::GetClass(type.c_str());
68  if (cl == nullptr) {
69  ::Warning("xAOD::TPyEvent::pyContains", "Type name \"%s\" not known",
70  type.c_str());
71  return false;
72  }
73 
74  // Check if the dictionary can return a type_info.
75  const std::type_info* ti = cl->GetTypeInfo();
76  if (ti == nullptr) {
77  ::Warning("xAOD::TPyEvent::pyContains",
78  "Type \"%s\" doesn't have a proper dictionary", type.c_str());
79  return false;
80  }
81 
82  // Let the base class do the work.
83  static constexpr bool METADATA = false;
84  return TEvent::contains(key, *ti, METADATA);
85 }
86 
87 bool TPyEvent::pyTransientContains(const std::string& key,
88  const std::string& type) const {
89 
90  // Try to access the dictionary of this type.
91  ::TClass* cl = ::TClass::GetClass(type.c_str());
92  if (cl == nullptr) {
93  ::Warning("xAOD::TPyEvent::pyTransientContains",
94  "Type name \"%s\" not known", type.c_str());
95  return false;
96  }
97 
98  // Check if the dictionary can return a type_info:
99  const std::type_info* ti = cl->GetTypeInfo();
100  if (ti == nullptr) {
101  ::Warning("xAOD::TPyEvent::pyTransientContains",
102  "Type \"%s\" doesn't have a proper dictionary", type.c_str());
103  return false;
104  }
105 
106  // Let the base class do the work:
107  return TEvent::transientContains(key, *ti, kFALSE);
108 }
109 
123 StatusCode TPyEvent::pyRecord(void* obj, const std::string& key,
124  const std::string& type) {
125 
126  // Simply forward the call to the base class:
127  static constexpr bool OVERWRITE = false;
128  static constexpr bool METADATA = false;
129  static constexpr bool IS_OWNER = false;
130  RETURN_CHECK("xAOD::TPyEvent::pyRecord",
131  record(obj, type, key, OVERWRITE, METADATA, IS_OWNER));
132  // Return gracefully:
133  return StatusCode::SUCCESS;
134 }
135 
136 } // namespace xAOD
RETURN_CHECK
#define RETURN_CHECK(CONTEXT, EXP)
Helper macro for checking return codes in a compact form in the code.
Definition: ReturnCheck.h:26
xAOD::Event::ATLAS_THREAD_SAFE
SG::SGKeyMap< BranchInfo > m_branches ATLAS_THREAD_SAFE
Map from hashed sgkey to BranchInfo.
Definition: Event.h:358
xAOD::TEvent::record
StatusCode record(void *obj, const std::string &typeName, const std::string &key, bool overwrite, bool metadata, bool isOwner) override
Record an object into a connected output file.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:1638
xAOD::TPyEvent::pyRetrieve
PyObject * pyRetrieve(const std::string &key)
Return the object with a given key as a PyObject.
Definition: TPyEvent.cxx:20
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
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
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
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::TPyEvent::pyRecord
StatusCode pyRecord(void *obj, const std::string &key, const std::string &type)
Add an output object to the event.
Definition: TPyEvent.cxx:123
xAOD::EventFormatElement::className
const std::string & className() const
Get the class name of this branch/key.
Definition: EventFormatElement.cxx:36
Preparation.mode
mode
Definition: Preparation.py:107
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::EventFormat_v1
Event format metadata for xAOD files.
Definition: EventFormat_v1.h:38
xAOD::TPyEvent::TPyEvent
TPyEvent(EAuxMode mode=kClassAccess)
Constructor with an access mode.
Definition: TPyEvent.cxx:18
xAOD::TPyEvent::pyTransientContains
bool pyTransientContains(const std::string &key, const std::string &type) const
Function checking if an object is already in memory.
Definition: TPyEvent.cxx:87
xAOD::Event::contains
bool contains(const std::string &key)
Function checking if an object is available from the store.
xAOD::TEvent::EAuxMode
EAuxMode
Auxiliary store "mode".
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:69
TPyEvent.h
xAOD::TPyEvent::pyContains
bool pyContains(const std::string &key, const std::string &type)
Function checking if an object is available from the store.
Definition: TPyEvent.cxx:64
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
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:57
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37