ATLAS Offline Software
InputVariable.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef JETTOOLHELPERS_INPUTVARIABLE_H
6 #define JETTOOLHELPERS_INPUTVARIABLE_H
7 
8 
9 #include "JetToolHelpers/JetContext.h" //used here
10 #include "xAODJet/Jet.h" //used here
11 #include "AthContainers/AuxElement.h" //member variable
12 #include "JetAnalysisInterfaces/IInputVariable.h"//inheritance
13 
14 #include <memory> //std::unique_ptr
15 #include <functional> //std::function
16 #include <string>
17 #include <stdexcept> //std::runtime_error
18 
19 
20 namespace JetHelper {
21 
25 
27 {
28 
29  public:
30 
32  static std::unique_ptr<InputVariable> createVariable(
33  const std::string& name,
34  const std::string& type,
35  const bool isJetVar
36  );
37 
39  InputVariable(const std::string& name): m_name{name}, m_scale{1.} {}
41  const std::string& name,
42  std::function<float(const xAOD::Jet& jet, const JetContext& jc)> func
43  );
44 
46  virtual ~InputVariable() = default;
47 
49  [[nodiscard]] float getValue(const xAOD::Jet& jet, const JetContext& jc) const override {
50  return m_scale * getValue_prot(jet, jc);
51  }
52 
54  std::string getName() const { return m_name; }
56  float getScale() const { return m_scale; }
58  void setScale(const float scale) { m_scale = scale; }
60  void setGeV() { m_scale = 1.e-3; }
62  void setMeV() { m_scale = 1.; }
63 
64  InputVariable(const InputVariable&) = delete;
65 
66  protected:
67  const std::string m_name;
68  float m_scale;
69 
70  std::function<float(const xAOD::Jet& jet, const JetContext& jc)> m_customFunction;
71 
72  [[nodiscard]] virtual float getValue_prot(const xAOD::Jet& jet, const JetContext& jc) const {
73  return (m_customFunction(jet, jc));
74  }
75 };
76 
78 template <typename T> class InputVariableJetContext : public InputVariable {
79  public:
80  InputVariableJetContext(const std::string& name) : InputVariable(name) {}
81  // should be T
82  virtual float getValue(const xAOD::Jet&, const JetContext& event) const {
83  if (event.isAvailable(m_name)) {
84  return event.getValue<T>(m_name);
85  } else {
86  throw std::runtime_error("Value " + m_name + " is not available");
87  }
88  }
89 };
91 template <typename T> class InputVariableAttribute : public InputVariable {
92  public:
94  virtual float getValue(const xAOD::Jet& jet, const JetContext&) const {
95  if (m_acc.isAvailable(jet)) {
96  return m_acc(jet)*m_scale;
97  } else {
98  throw std::runtime_error("Value " + m_name + " is not available");
99  }
100  }
101  private:
103 };
104 } // namespace JetHelper
105 
106 #endif
107 
JetHelper::InputVariableAttribute::getValue
virtual float getValue(const xAOD::Jet &jet, const JetContext &) const
return the value of the variable choose by the user
Definition: InputVariable.h:94
Jet.h
JetHelper::InputVariable::InputVariable
InputVariable(const std::string &name)
Constructors.
Definition: InputVariable.h:39
JetHelper::InputVariable
Class InputVariable This is design to read any kind of xAOD::Jet or JetContext variable e....
Definition: InputVariable.h:27
JetHelper::InputVariable::setGeV
void setGeV()
This function set the scale to GeV, assuming MeV by default.
Definition: InputVariable.h:60
JetHelper::JetContext
Class JetContext Designed to read AOD information related to the event, N vertices,...
Definition: JetContext.h:24
JetHelper::InputVariable::InputVariable
InputVariable(const std::string &name, std::function< float(const xAOD::Jet &jet, const JetContext &jc)> func)
JetHelper::IInputVariable
Definition: IInputVariable.h:21
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
JetHelper
A tool interface class for tools implementing the IInputVariable interface.
Definition: IInputVariable.h:18
yodamerge_tmp.scale
scale
Definition: yodamerge_tmp.py:138
JetHelper::InputVariableJetContext
Template to read JetContext.
Definition: InputVariable.h:78
JetHelper::InputVariable::setScale
void setScale(const float scale)
This function set the scale of the variable.
Definition: InputVariable.h:58
JetHelper::InputVariable::m_customFunction
std::function< float(const xAOD::Jet &jet, const JetContext &jc)> m_customFunction
Definition: InputVariable.h:70
JetHelper::InputVariable::getScale
float getScale() const
This function return the scale of the variable.
Definition: InputVariable.h:56
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
IInputVariable.h
JetHelper::InputVariableJetContext::InputVariableJetContext
InputVariableJetContext(const std::string &name)
Definition: InputVariable.h:80
JetHelper::InputVariable::InputVariable
InputVariable(const InputVariable &)=delete
JetHelper::InputVariableAttribute
Template to read xAOD::Jet variables
Definition: InputVariable.h:91
JetHelper::InputVariableAttribute::InputVariableAttribute
InputVariableAttribute(const std::string &name)
Definition: InputVariable.h:93
JetHelper::InputVariable::m_scale
float m_scale
Definition: InputVariable.h:68
JetHelper::InputVariableAttribute::m_acc
SG::AuxElement::ConstAccessor< T > m_acc
Definition: InputVariable.h:102
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
JetHelper::InputVariable::getValue_prot
virtual float getValue_prot(const xAOD::Jet &jet, const JetContext &jc) const
Definition: InputVariable.h:72
JetHelper::InputVariable::getValue
float getValue(const xAOD::Jet &jet, const JetContext &jc) const override
return the value of the variable choose by the user
Definition: InputVariable.h:49
JetHelper::InputVariable::getName
std::string getName() const
This function return the name of the variable.
Definition: InputVariable.h:54
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
JetHelper::InputVariable::m_name
const std::string m_name
Definition: InputVariable.h:67
JetHelper::InputVariable::createVariable
static std::unique_ptr< InputVariable > createVariable(const std::string &name, const std::string &type, const bool isJetVar)
This function specialize the variable to the one choose by the user.
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
JetContext.h
JetHelper::InputVariable::~InputVariable
virtual ~InputVariable()=default
Default destructor.
readCCLHist.float
float
Definition: readCCLHist.py:83
JetHelper::InputVariable::setMeV
void setMeV()
This function set the scale to MeV, assuming MeV by default (it does nothing)
Definition: InputVariable.h:62
AuxElement.h
Base class for elements of a container that can have aux data.
JetHelper::InputVariableJetContext::getValue
virtual float getValue(const xAOD::Jet &, const JetContext &event) const
return the value of the variable choose by the user
Definition: InputVariable.h:82