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-2017 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// #include "AsgMessaging/MsgStreamMacros.h
19
20#include "xAODJet/Jet.h"
22
23#define JESUNC_ERROR_CODE -1234
24#define JESUNC_NO_DEFAULT_CONSTRUCTOR ATH_MSG_FATAL("Default constructor is not supported");
25#define JESUNC_SAFE_DELETE(T) { if(T) { delete T; T = NULL; } }
26
27class TH1;
28
29namespace jet
30{
39 class JetFourMomAccessor: public xAOD::JetAttributeAccessor::AccessorWrapper<xAOD::JetFourMom_t> {
40 public:
42 xAOD::JetFourMom_t operator()(const xAOD::Jet & jet) const {return const_cast<JetFourMomAccessor*>(this)->getAttribute(jet);}
43 };
44
45namespace utils
46{
47 // Check variable types from strings
48 template<typename T>
49 bool isTypeObjFromString(const std::string& str);
50 template<typename T>
51 bool isTypeObjFromString(const TString& str);
52
53 // Get variables from strings
54 template<typename T>
55 bool getTypeObjFromString(const std::string& str, T& obj);
56 template<typename T>
57 T getTypeObjFromString(const std::string& str);
58 template<typename T>
59 bool getTypeObjFromString(const TString& str, T& obj);
60 template<typename T>
61 T getTypeObjFromString(const TString& str);
62
63 // Specializations of getting variables from strings
64 template <>
65 bool getTypeObjFromString<std::string>(const std::string& str, std::string& obj);
66 template <>
67 bool getTypeObjFromString<TString>(const std::string& str, TString& obj);
68 template <>
69 bool getTypeObjFromString<bool>(const std::string& str, bool& obj);
70 template <>
71 bool getTypeObjFromString<std::string>(const TString& str, std::string& obj);
72 template <>
73 bool getTypeObjFromString<TString>(const TString& str, TString& obj);
74 template <>
75 bool getTypeObjFromString<bool>(const TString& str, bool& obj);
76
77 // Convert strings to vectors of objects
78 template <typename T>
79 bool vectorize(const TString& str, const TString& sep, std::vector<T>& result);
80 template <typename T>
81 std::vector<T> vectorize(const TString& str, const TString& sep);
82
83 // Check if a file exists
84 bool fileExists(const TString& fileName);
85
86 // Find a valid file path
87 TString findFilePath(const TString& fileName, const TString& path = "", const TString& calibArea = "");
88
89 // Open a root file
90 TFile* readRootFile(const TString& fileName, const TString& path = "", const TString& calibArea = "");
91
92 // Make bins easily
93 std::vector<double> getLogBins(const size_t numBins, const double minVal, const double maxVal);
94 std::vector<double> getUniformBins(const size_t numBins, const double minVal, const double maxVal);
95
96 // Scale the axis or axes of a histogram
97 void scaleHistoAxes(TH1* toScale, const double factorX=1, const double factorY=1, const double factorZ=1);
98}
99
100template <typename T>
101bool utils::isTypeObjFromString(const std::string& str)
102{
103 std::istringstream iss(str);
104 T obj;
105 return !(iss >> obj).fail();
106}
107
108template <typename T>
109bool utils::isTypeObjFromString(const TString& str)
110{
111 std::string stdstr = str.Data();
112 return isTypeObjFromString<T>(stdstr);
113}
114
115template <typename T>
116bool utils::getTypeObjFromString(const std::string& str, T& obj)
117{
118 std::istringstream iss(str);
119 return !(iss >> obj).fail();
120}
121template <typename T>
122T utils::getTypeObjFromString(const std::string& str)
123{
124 T toReturn;
125 if (!getTypeObjFromString(str,toReturn))
126 printf("Failed to convert object: %s\n",str.c_str());
127
128 return toReturn;
129}
130template <typename T>
131bool utils::getTypeObjFromString(const TString& str, T& obj)
132{
133 std::string stdstr = str.Data();
134 return getTypeObjFromString(stdstr,obj);
135}
136template <typename T>
138{
139 T toReturn;
140 if (!getTypeObjFromString(str,toReturn))
141 printf("ERROR: Failed to convert object: %s\n",str.Data());
142
143 return toReturn;
144}
145
146template <typename T>
147bool utils::vectorize(const TString& str, const TString& sep, std::vector<T>& result)
148{
149 bool success = true;
150 result.clear();
151
152 TObjArray* tokens = str.Tokenize(sep);
153 TIter istr(tokens);
154 while(TObjString* os=(TObjString*)istr())
155 {
156 T obj;
157 if (!getTypeObjFromString(os->GetString(),obj))
158 {
159 success = false;
160 break;
161 }
162 else
163 result.push_back(obj);
164 }
165 delete tokens;
166
167 return success;
168}
169
170template <typename T>
171std::vector<T> utils::vectorize(const TString& str, const TString& sep)
172{
173 std::vector<T> result;
174 TObjArray* tokens = str.Tokenize(sep);
175 TIter istr(tokens);
176
177 while(TObjString* os=(TObjString*)istr())
178 {
179 T obj;
180 if (!getTypeObjFromString(os->GetString(),obj))
181 printf("ERROR: String \"%s\" is not the requested type\n",os->GetString().Data());
182 result.push_back(obj);
183 }
184 delete tokens;
185
186 return result;
187}
188
189} // end jet namespace
190
191#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