ATLAS Offline Software
Loading...
Searching...
No Matches
Reconstruction/Jet/JetUncertainties/JetUncertainties/Helpers.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef JETUNCERTAINTIES_HELPERS_H
6#define JETUNCERTAINTIES_HELPERS_H
7
8#include <iostream>
9#include <sstream>
10#include <string>
11#include <vector>
12
13#include "TString.h"
14#include "TObjString.h"
15#include "TObjArray.h"
16#include "TFile.h"
17
18
19#include "xAODJet/Jet.h"
21
22#define JESUNC_ERROR_CODE -1234
23#define JESUNC_NO_DEFAULT_CONSTRUCTOR ATH_MSG_FATAL("Default constructor is not supported");
24#define JESUNC_SAFE_DELETE(T) { delete T; T = nullptr; }
25
26class TH1;
27
28namespace jet
29{
38 class JetFourMomAccessor: public xAOD::JetAttributeAccessor::AccessorWrapper<xAOD::JetFourMom_t> {
39 public:
41 xAOD::JetFourMom_t operator()(const xAOD::Jet & jet) const {return const_cast<JetFourMomAccessor*>(this)->getAttribute(jet);}
42 };
43
44namespace utils
45{
46 // Check variable types from strings
47 template<typename T>
48 bool isTypeObjFromString(const std::string& str);
49 template<typename T>
50 bool isTypeObjFromString(const TString& str);
51
52 // Get variables from strings
53 template<typename T>
54 bool getTypeObjFromString(const std::string& str, T& obj);
55 template<typename T>
56 T getTypeObjFromString(const std::string& str);
57 template<typename T>
58 bool getTypeObjFromString(const TString& str, T& obj);
59 template<typename T>
60 T getTypeObjFromString(const TString& str);
61
62 // Specializations of getting variables from strings
63 template <>
64 bool getTypeObjFromString<std::string>(const std::string& str, std::string& obj);
65 template <>
66 bool getTypeObjFromString<TString>(const std::string& str, TString& obj);
67 template <>
68 bool getTypeObjFromString<bool>(const std::string& str, bool& obj);
69 template <>
70 bool getTypeObjFromString<std::string>(const TString& str, std::string& obj);
71 template <>
72 bool getTypeObjFromString<TString>(const TString& str, TString& obj);
73 template <>
74 bool getTypeObjFromString<bool>(const TString& str, bool& obj);
75
76 // Convert strings to vectors of objects
77 template <typename T>
78 bool vectorize(const TString& str, const TString& sep, std::vector<T>& result);
79 template <typename T>
80 std::vector<T> vectorize(const TString& str, const TString& sep);
81
82 // Check if a file exists
83 bool fileExists(const TString& fileName);
84
85 // Find a valid file path
86 TString findFilePath(const TString& fileName, const TString& path = "", const TString& calibArea = "");
87
88 // Open a root file
89 TFile* readRootFile(const TString& fileName, const TString& path = "", const TString& calibArea = "");
90
91 // Make bins easily
92 std::vector<double> getLogBins(const size_t numBins, const double minVal, const double maxVal);
93 std::vector<double> getUniformBins(const size_t numBins, const double minVal, const double maxVal);
94
95 // Scale the axis or axes of a histogram
96 void scaleHistoAxes(TH1* toScale, const double factorX=1, const double factorY=1, const double factorZ=1);
97}
98
99template <typename T>
100bool utils::isTypeObjFromString(const std::string& str)
101{
102 std::istringstream iss(str);
103 T obj;
104 return !(iss >> obj).fail();
105}
106
107template <typename T>
108bool utils::isTypeObjFromString(const TString& str)
109{
110 std::string stdstr = str.Data();
111 return isTypeObjFromString<T>(stdstr);
112}
113
114template <typename T>
115bool utils::getTypeObjFromString(const std::string& str, T& obj)
116{
117 std::istringstream iss(str);
118 return !(iss >> obj).fail();
119}
120template <typename T>
121T utils::getTypeObjFromString(const std::string& str)
122{
123 T toReturn;
124 if (!getTypeObjFromString(str,toReturn))
125 printf("Failed to convert object: %s\n",str.c_str());
126
127 return toReturn;
128}
129template <typename T>
130bool utils::getTypeObjFromString(const TString& str, T& obj)
131{
132 std::string stdstr = str.Data();
133 return getTypeObjFromString(stdstr,obj);
134}
135template <typename T>
137{
138 T toReturn;
139 if (!getTypeObjFromString(str,toReturn))
140 printf("ERROR: Failed to convert object: %s\n",str.Data());
141
142 return toReturn;
143}
144
145template <typename T>
146bool utils::vectorize(const TString& str, const TString& sep, std::vector<T>& result)
147{
148 bool success = true;
149 result.clear();
150
151 TObjArray* tokens = str.Tokenize(sep);
152 TIter istr(tokens);
153 while(TObjString* os=(TObjString*)istr())
154 {
155 T obj;
156 if (!getTypeObjFromString(os->GetString(),obj))
157 {
158 success = false;
159 break;
160 }
161 else
162 result.push_back(obj);
163 }
164 delete tokens;
165
166 return success;
167}
168
169template <typename T>
170std::vector<T> utils::vectorize(const TString& str, const TString& sep)
171{
172 std::vector<T> result;
173 TObjArray* tokens = str.Tokenize(sep);
174 TIter istr(tokens);
175
176 while(TObjString* os=(TObjString*)istr())
177 {
178 T obj;
179 if (!getTypeObjFromString(os->GetString(),obj))
180 printf("ERROR: String \"%s\" is not the requested type\n",os->GetString().Data());
181 result.push_back(obj);
182 }
183 delete tokens;
184
185 return result;
186}
187
188} // end jet namespace
189
190#endif
This header defines wrapper classes around SG::AuxElement::Accessor used internally in the Jet EDM.
JetFourMomAccessor is an extension of JetAttributeAccessor::AccessorWrapper<xAOD::JetFourMom_t> Acces...
xAOD::JetFourMom_t operator()(const xAOD::Jet &jet) const
void getAttribute(const SG::AuxElement &p, xAOD::JetFourMom_t &v) const
bool getTypeObjFromString< TString >(const std::string &str, TString &obj)
void scaleHistoAxes(TH1 *toScale, const double factorX=1, const double factorY=1, const double factorZ=1)
std::vector< double > getLogBins(const size_t numBins, const double minVal, const double maxVal)
bool getTypeObjFromString(const std::string &str, T &obj)
bool getTypeObjFromString< bool >(const std::string &str, bool &obj)
TFile * readRootFile(const TString &fileName, const TString &path="", const TString &calibArea="")
bool vectorize(const TString &str, const TString &sep, std::vector< T > &result)
bool getTypeObjFromString< std::string >(const std::string &str, std::string &obj)
TString findFilePath(const TString &fileName, const TString &path="", const TString &calibArea="")
std::vector< double > getUniformBins(const size_t numBins, const double minVal, const double maxVal)
Jet_v1 Jet
Definition of the current "jet version".
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiM4D< double > > JetFourMom_t
Base 4 Momentum type for Jet.
Definition JetTypes.h:17