ATLAS Offline Software
Loading...
Searching...
No Matches
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
16namespace xAOD {
17
19
20PyObject* 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
64bool 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
87bool 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
123StatusCode 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
_object PyObject
#define RETURN_CHECK(CONTEXT, EXP)
Helper macro for checking return codes in a compact form in the code.
Definition ReturnCheck.h:26
Define macros for attributes used to control the static checker.
Class describing one branch of the ROOT file.
const std::string & className() const
Get the class name of this branch/key.
bool transientContains(const std::string &key) const
Function checking if an object is already in memory.
const EventFormat * outputEventFormat() const
Get information about the output objects.
const EventFormat * inputEventFormat() const
Get information about the input objects.
bool contains(const std::string &key)
Function checking if an object is available from the store.
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.
void * getOutputObject(SG::sgkey_t key, const std::type_info &ti) override
Function for retrieving an output object in a non-template way.
SG::SGKeyMap< BranchInfo > m_branches ATLAS_THREAD_SAFE
Map from hashed sgkey to BranchInfo.
Definition Event.h:358
TEvent(EAuxMode mode=kClassAccess)
Default constructor.
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.
bool pyContains(const std::string &key, const std::string &type)
Function checking if an object is available from the store.
Definition TPyEvent.cxx:64
StatusCode pyRecord(void *obj, const std::string &key, const std::string &type)
Add an output object to the event.
Definition TPyEvent.cxx:123
bool pyTransientContains(const std::string &key, const std::string &type) const
Function checking if an object is already in memory.
Definition TPyEvent.cxx:87
PyObject * pyRetrieve(const std::string &key)
Return the object with a given key as a PyObject.
Definition TPyEvent.cxx:20
TPyEvent(EAuxMode mode=kClassAccess)
Constructor with an access mode.
Definition TPyEvent.cxx:18
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
EventFormat_v1 EventFormat
Definition of the current event format version.
Definition EventFormat.h:16