ATLAS Offline Software
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>
13 #include <AsgTools/AsgToolConfig.h>
14 #include <AsgTools/ToolHandle.h>
21 #include <span>
22 
23 namespace 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 
69  void preinitialize ()
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)
89  preinitialize ();
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());
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 
114  void initialize ()
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;
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;
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 
200  IColumnarTool* m_tool = nullptr;
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 }
columnar::PythonToolHandle::m_toolHandle
ToolHandle< asg::AsgTool > m_toolHandle
Definition: PythonToolHandle.h:197
columnar::PythonToolHandle::getConfig
const asg::AsgToolConfig & getConfig() const
get the AsgToolConfig
Definition: PythonToolHandle.h:63
CaloCondBlobAlgs_fillNoiseFromASCII.sysName
sysName
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:92
columnar::PythonToolHandle::setProperty
void setProperty(const std::string &key, T &&value)
set a property on the tool
Definition: PythonToolHandle.h:56
columnar::PythonToolHandle::getRecommendedSystematics
std::vector< std::string > getRecommendedSystematics() const
get the recommended systematics
Definition: PythonToolHandle.h:179
columnar::PythonToolHandle::m_cleanup
std::shared_ptr< void > m_cleanup
Definition: PythonToolHandle.h:198
columnar::IColumnarTool
an interface for tools that operate on columnar data
Definition: IColumnarTool.h:214
get_generator_info.result
result
Definition: get_generator_info.py:21
columnar::PythonToolHandle::getColumnNames
std::vector< std::string > getColumnNames() const
get the expected column names
Definition: PythonToolHandle.h:171
CP::make_systematics_vector
std::vector< CP::SystematicSet > make_systematics_vector(const SystematicSet &systematics)
utility functions for working with systematics
Definition: SystematicsUtil.cxx:25
columnar::PythonToolHandle::m_toolWrapper
std::shared_ptr< const ToolColumnVectorMap > m_toolWrapper
Definition: PythonToolHandle.h:204
SystematicSet.h
asg
Definition: DataHandleTestTool.h:28
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
DeMoUpdate.column
dictionary column
Definition: DeMoUpdate.py:1110
columnar::PythonToolHandle::setColumn
void setColumn(const std::string &key, std::size_t size, CT *dataPtr)
set a column pointer (raw pointer version)
Definition: PythonToolHandle.h:138
columnar::PythonToolHandle::m_tool
IColumnarTool * m_tool
Definition: PythonToolHandle.h:200
columnar::PythonToolHandle::setTypeAndName
void setTypeAndName(const std::string &typeAndName)
set the type and name for the tool
Definition: PythonToolHandle.h:49
asg::AsgMessaging::AsgMessaging
AsgMessaging(const std::string &name)
Constructor with a name.
Definition: AsgMessaging.cxx:17
athena.value
value
Definition: athena.py:124
columnar::PythonToolHandle::m_systTool
CP::ISystematicsTool * m_systTool
Definition: PythonToolHandle.h:201
columnar::PythonToolHandle::initialize
void initialize()
initialize the tool
Definition: PythonToolHandle.h:114
columnar::PythonToolHandle::m_columnHeader
std::shared_ptr< const ColumnVectorHeader > m_columnHeader
Definition: PythonToolHandle.h:203
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
columnar::PythonToolHandle::setColumnVoid
void setColumnVoid(const std::string &name, std::size_t size, const void *dataPtr, const std::type_info &type, bool isConst)
set a column pointer
Definition: PythonToolHandle.h:146
ISystematicsTool.h
columnar::PythonToolHandle
a handle to a python tool for use via nanobind
Definition: PythonToolHandle.h:39
AsgMessaging.h
asg::AsgToolConfig
an object that can create a AsgTool
Definition: AsgToolConfig.h:22
IColumnarTool.h
columnar::IColumnarTool::getColumnInfo
virtual std::vector< ColumnInfo > getColumnInfo() const =0
the meta-information for the columns
AsgToolConfig.h
MessageCheck.h
macros for messaging and checking status codes
columnar::PythonToolHandle::m_config
asg::AsgToolConfig m_config
Definition: PythonToolHandle.h:196
columnar::PythonToolHandle::call
void call()
call the tool and reset the columns
Definition: PythonToolHandle.h:153
columnar::size
std::size_t size() const noexcept
Definition: ObjectRange.h:166
columnar::PythonToolHandle::PythonToolHandle
PythonToolHandle()
standard constructor
Definition: PythonToolHandle.h:46
columnar::IColumnarTool::renameColumn
virtual void renameColumn(const std::string &from, const std::string &to)=0
rename one of the columns the tool uses
ToolColumnVectorMap.h
columnar::PythonToolHandle::renameContainers
void renameContainers(const std::vector< std::pair< std::string, std::string >> &renames)
rename the columns the tool uses
Definition: PythonToolHandle.h:86
columnar::final
CM final
Definition: ColumnAccessor.h:106
CP::IReentrantSystematicsTool::recommendedSystematics
virtual SystematicSet recommendedSystematics() const =0
the list of all systematics this tool recommends to use
asg::AsgComponentConfig::setTypeAndName
void setTypeAndName(const std::string &val_typeAndName)
set type and name at the same time
Definition: AsgComponentConfig.cxx:116
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
asg::AsgMessaging
Class mimicking the AthMessaging class from the offline software.
Definition: AsgMessaging.h:40
MakeNewFileFromOldAndSubstitution.newName
dictionary newName
Definition: ICHEP2016/MakeNewFileFromOldAndSubstitution.py:95
columnar::PythonToolHandle::preinitialize
void preinitialize()
preinitialize the tool
Definition: PythonToolHandle.h:69
CxxUtils::to
CONT to(RANGE &&r)
Definition: ranges.h:39
CP::ISystematicsTool
Interface for all CP tools supporting systematic variations.
Definition: ISystematicsTool.h:32
columnar::CT
CT
Definition: ColumnAccessor.h:160
columnar
Definition: ClusterDef.h:16
asg::AsgComponentConfig::setProperty
StatusCode setProperty(const std::string &name, const T &value)
set the given property
columnar::PythonToolHandle::getColumnInfo
std::vector< ColumnInfo > getColumnInfo() const
get the expected column info
Definition: PythonToolHandle.h:163
columnar::PythonToolHandle::m_columns
std::unique_ptr< ColumnVectorData > m_columns
Definition: PythonToolHandle.h:205
ToolHandle.h
columnar::PythonToolHandle::applySystematicVariation
void applySystematicVariation(const std::string &sysName)
set the tool to apply the given systematic variation
Definition: PythonToolHandle.h:126
ColumnInfo.h
asg::AsgToolConfig::makeTool
::StatusCode makeTool(ToolHandle< T > &toolHandle, std::shared_ptr< void > &cleanup, bool allowNestedName=false) const
make a tool with the given configuration
AsgTool.h
CP::ISystematicsTool::applySystematicVariation
virtual StatusCode applySystematicVariation(const SystematicSet &systConfig)=0
effects: configure this tool for the given list of systematic variations.
python.difftuple.renames
dictionary renames
Definition: difftuple.py:22
SystematicsUtil.h
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288