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>
21#include <span>
22
23namespace columnar
24{
37
39 {
42
43 public:
44
46 PythonToolHandle() : asg::AsgMessaging("PythonToolHandle") {}
47
49 void setTypeAndName (const std::string& typeAndName)
50 {
51 m_config.setTypeAndName (typeAndName);
52 }
53
55 template<typename T>
56 void setProperty (const std::string& key, T&& value)
57 {
58 if (m_config.setProperty (key, std::forward<T> (value)).isFailure())
59 throw std::runtime_error ("failed to set property: " + key);
60 }
61
63 [[nodiscard]] const asg::AsgToolConfig& getConfig () const
64 {
65 return m_config;
66 }
67
70 {
71 ANA_MSG_DEBUG("preinitializing with " << m_toolHandle << " and cleanup " << m_cleanup);
72 if (!m_config.makeTool (m_toolHandle, m_cleanup).isSuccess())
73 throw std::runtime_error ("failed to create tool");
74
75 ANA_MSG_DEBUG("m_config created tool" << m_toolHandle);
76
77 m_tool = dynamic_cast<IColumnarTool*> (&*m_toolHandle);
78
79 ANA_MSG_DEBUG("attempting to dynamically cast to IColumnarTool* gives " << m_tool);
80 if (m_tool == nullptr)
81 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.");
82 m_systTool = dynamic_cast<CP::ISystematicsTool*> (m_tool);
83 }
84
86 void renameContainers (const std::vector<std::pair<std::string,std::string>>& renames)
87 {
88 if (m_tool == nullptr)
90 {
91 auto columnInfo = m_tool->getColumnInfo ();
92 for (auto& [from, to] : renames)
93 {
94 for (auto& column : columnInfo)
95 {
96 if (column.name.starts_with (from) && (column.name.size() == from.size() || column.name[from.size()] == '.'))
97 {
98 std::string newName = to + column.name.substr (from.size());
99 m_tool->renameColumn (column.name, newName);
100 }
101 }
102 }
103 }
104 if (m_toolWrapper)
105 {
106 auto columnHeader = std::make_shared<ColumnVectorHeader> ();
107 m_columnHeader = columnHeader;
108 m_toolWrapper = std::make_shared<ToolColumnVectorMap> (*columnHeader, *m_tool);
109 m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
110 }
111 }
112
115 {
116 if (m_tool == nullptr)
117 preinitialize ();
118
119 auto columnHeader = std::make_shared<ColumnVectorHeader> ();
120 m_columnHeader = columnHeader;
121 m_toolWrapper = std::make_shared<ToolColumnVectorMap> (*columnHeader, *m_tool);
122 m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
123 }
124
126 void applySystematicVariation (const std::string& sysName)
127 {
128 // by convention setting a systematic on a non-systematics tool
129 // will do nothing
130 if (m_systTool == nullptr)
131 return;
132 if (!m_systTool->applySystematicVariation (CP::SystematicSet (sysName)).isSuccess())
133 throw std::runtime_error ("failed to apply systematic variation");
134 }
135
137 template<typename CT>
138 void setColumn (const std::string& key, std::size_t size, CT* dataPtr)
139 {
140 if (!m_columns)
141 throw std::runtime_error ("tool not initialized");
142 m_columns->setColumn (m_toolWrapper->getColumnIndex (key), size, dataPtr);
143 }
144
146 void setColumnVoid (const std::string& name, std::size_t size, const void *dataPtr, const std::type_info& type, bool isConst) {
147 if (!m_columns)
148 throw std::runtime_error ("tool not initialized");
149 m_columns->setColumnVoid (m_toolWrapper->getColumnIndex (name), size, dataPtr, type, isConst);
150 }
151
153 void call ()
154 {
155 if (!m_columns)
156 throw std::runtime_error ("no columns set");
157 m_columns->checkData ();
158 m_columns->callNoCheck (*m_tool);
159 m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
160 }
161
163 [[nodiscard]] std::vector<ColumnInfo> getColumnInfo () const
164 {
165 if (!m_toolWrapper)
166 throw std::runtime_error ("tool not initialized");
167 return m_tool->getColumnInfo ();
168 }
169
171 std::vector<std::string> getColumnNames () const
172 {
173 if (!m_toolWrapper)
174 throw std::runtime_error ("tool not initialized");
175 return m_toolWrapper->getColumnNames ();
176 }
177
179 std::vector<std::string> getRecommendedSystematics () const
180 {
181 if (!m_systTool)
182 return {""};
183 std::vector<std::string> result;
184 for (auto& sys : CP::make_systematics_vector (m_systTool->recommendedSystematics()))
185 result.push_back (sys.name());
186 return result;
187 }
188
189
190
193
194 private:
195
197 ToolHandle<asg::AsgTool> m_toolHandle;
198 std::shared_ptr<void> m_cleanup;
199
202
203 std::shared_ptr<const ColumnVectorHeader> m_columnHeader;
204 std::shared_ptr<const ToolColumnVectorMap> m_toolWrapper;
205 std::unique_ptr<ColumnVectorData> m_columns;
206 };
207}
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