ATLAS Offline Software
Loading...
Searching...
No Matches
PythonToolHandle.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
9
12#include <AsgTools/AsgTool.h>
14#include <AsgTools/ToolHandle.h>
22#include <span>
23
24namespace columnar
25{
38
40 {
43
44 public:
45
47 PythonToolHandle() : asg::AsgMessaging("PythonToolHandle") {}
48
50 void setTypeAndName (const std::string& typeAndName)
51 {
52 m_config.setTypeAndName (typeAndName);
53 }
54
56 template<typename T>
57 void setProperty (const std::string& key, T&& value)
58 {
59 if (m_config.setProperty (key, std::forward<T> (value)).isFailure())
60 throw std::runtime_error ("failed to set property: " + key);
61 }
62
64 [[nodiscard]] const asg::AsgToolConfig& getConfig () const
65 {
66 return m_config;
67 }
68
71 {
72 ANA_MSG_DEBUG("preinitializing with " << m_toolHandle << " and cleanup " << m_cleanup);
73 if (!m_config.makeTool (m_toolHandle, m_cleanup).isSuccess())
74 throw std::runtime_error ("failed to create tool");
75
76 ANA_MSG_DEBUG("m_config created tool" << m_toolHandle);
77
78 m_tool = dynamic_cast<IColumnarTool*> (&*m_toolHandle);
79
80 ANA_MSG_DEBUG("attempting to dynamically cast to IColumnarTool* gives " << m_tool);
81 if (m_tool == nullptr)
82 throw std::runtime_error ("The tool does not implement IColumnarTool. First, check to make sure you're in the ColumnarAnalysis release. Then, check to see if the tool inherits from ColumnarTool.");
83 m_systTool = dynamic_cast<CP::ISystematicsTool*> (m_tool);
84 }
85
87 void renameContainers (const std::vector<std::pair<std::string,std::string>>& renames)
88 {
89 if (m_tool == nullptr)
91 {
93 }
94 if (m_toolWrapper)
95 {
96 auto columnHeader = std::make_shared<ColumnVectorHeader> ();
97 m_columnHeader = columnHeader;
98 m_toolWrapper = std::make_shared<ToolColumnVectorMap> (*columnHeader, *m_tool);
99 m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
100 }
101 }
102
105 {
106 if (m_tool == nullptr)
107 preinitialize ();
108
109 auto columnHeader = std::make_shared<ColumnVectorHeader> ();
110 m_columnHeader = columnHeader;
111 m_toolWrapper = std::make_shared<ToolColumnVectorMap> (*columnHeader, *m_tool);
112 m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
113 }
114
116 void applySystematicVariation (const std::string& sysName)
117 {
118 // by convention setting a systematic on a non-systematics tool
119 // will do nothing
120 if (m_systTool == nullptr)
121 return;
122 if (!m_systTool->applySystematicVariation (CP::SystematicSet (sysName)).isSuccess())
123 throw std::runtime_error ("failed to apply systematic variation");
124 }
125
127 template<typename CT>
128 void setColumn (const std::string& key, std::size_t size, CT* dataPtr)
129 {
130 if (!m_columns)
131 throw std::runtime_error ("tool not initialized");
132 m_columns->setColumn (m_toolWrapper->getColumnIndex (key), size, dataPtr);
133 }
134
136 void setColumnVoid (const std::string& name, std::size_t size, const void *dataPtr, const std::type_info& type, bool isConst) {
137 if (!m_columns)
138 throw std::runtime_error ("tool not initialized");
139 m_columns->setColumnVoid (m_toolWrapper->getColumnIndex (name), size, dataPtr, type, isConst);
140 }
141
143 void call ()
144 {
145 if (!m_columns)
146 throw std::runtime_error ("no columns set");
147 m_columns->checkData ();
148 m_columns->callNoCheck (*m_tool);
149 m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
150 }
151
153 [[nodiscard]] std::vector<ColumnInfo> getColumnInfo () const
154 {
155 if (!m_toolWrapper)
156 throw std::runtime_error ("tool not initialized");
157 return m_tool->getColumnInfo ();
158 }
159
161 std::vector<std::string> getColumnNames () const
162 {
163 if (!m_toolWrapper)
164 throw std::runtime_error ("tool not initialized");
165 return m_toolWrapper->getColumnNames ();
166 }
167
169 std::vector<std::string> getRecommendedSystematics () const
170 {
171 if (!m_systTool)
172 return {""};
173 std::vector<std::string> result;
174 for (auto& sys : CP::make_systematics_vector (m_systTool->recommendedSystematics()))
175 result.push_back (sys.name());
176 return result;
177 }
178
179
180
183
184 private:
185
187 ToolHandle<asg::AsgTool> m_toolHandle;
188 std::shared_ptr<void> m_cleanup;
189
192
193 std::shared_ptr<const ColumnVectorHeader> m_columnHeader;
194 std::shared_ptr<const ToolColumnVectorMap> m_toolWrapper;
195 std::unique_ptr<ColumnVectorData> m_columns;
196 };
197}
macros for messaging and checking status codes
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Interface for all CP tools supporting systematic variations.
Class to wrap a set of SystematicVariations.
Class mimicking the AthMessaging class from the offline software.
AsgMessaging(const std::string &name)
Constructor with a name.
an object that can create a AsgTool
an interface for tools that operate on columnar data
std::shared_ptr< void > m_cleanup
void setColumn(const std::string &key, std::size_t size, CT *dataPtr)
set a column pointer (raw pointer version)
PythonToolHandle()
standard constructor
void renameContainers(const std::vector< std::pair< std::string, std::string > > &renames)
rename the columns the tool uses
void preinitialize()
preinitialize the tool
void applySystematicVariation(const std::string &sysName)
set the tool to apply the given systematic variation
std::shared_ptr< const ColumnVectorHeader > m_columnHeader
void setTypeAndName(const std::string &typeAndName)
set the type and name for the tool
std::unique_ptr< ColumnVectorData > m_columns
void setColumnVoid(const std::string &name, std::size_t size, const void *dataPtr, const std::type_info &type, bool isConst)
set a column pointer
std::shared_ptr< const ToolColumnVectorMap > m_toolWrapper
CP::ISystematicsTool * m_systTool
std::vector< std::string > getRecommendedSystematics() const
get the recommended systematics
ToolHandle< asg::AsgTool > m_toolHandle
const asg::AsgToolConfig & getConfig() const
get the AsgToolConfig
std::vector< ColumnInfo > getColumnInfo() const
get the expected column info
std::vector< std::string > getColumnNames() const
get the expected column names
void setProperty(const std::string &key, T &&value)
set a property on the tool
void call()
call the tool and reset the columns
void initialize()
initialize the tool
std::vector< CP::SystematicSet > make_systematics_vector(const SystematicSet &systematics)
utility functions for working with systematics
void renameContainers(IColumnarTool &tool, const std::vector< std::pair< std::string, std::string > > &renames)
rename containers in the columnar tool