ATLAS Offline Software
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"
21 #include "xAODJet/JetAccessors.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 
27 class TH1;
28 
29 namespace 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 
45 namespace 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 
100 template <typename T>
101 bool utils::isTypeObjFromString(const std::string& str)
102 {
103  std::istringstream iss(str);
104  T obj;
105  return !(iss >> obj).fail();
106 }
107 
108 template <typename T>
109 bool utils::isTypeObjFromString(const TString& str)
110 {
111  std::string stdstr = str.Data();
112  return isTypeObjFromString<T>(stdstr);
113 }
114 
115 template <typename T>
116 bool utils::getTypeObjFromString(const std::string& str, T& obj)
117 {
118  std::istringstream iss(str);
119  return !(iss >> obj).fail();
120 }
121 template <typename T>
122 T 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 }
130 template <typename T>
131 bool utils::getTypeObjFromString(const TString& str, T& obj)
132 {
133  std::string stdstr = str.Data();
134  return getTypeObjFromString(stdstr,obj);
135 }
136 template <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 
146 template <typename T>
147 bool 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 
170 template <typename T>
171 std::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
Jet.h
get_generator_info.result
result
Definition: get_generator_info.py:21
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:126
jet::utils::getUniformBins
std::vector< double > getUniformBins(const size_t numBins, const double minVal, const double maxVal)
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:164
jet::utils::getLogBins
std::vector< double > getLogBins(const size_t numBins, const double minVal, const double maxVal)
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:152
jet::utils::getTypeObjFromString
T getTypeObjFromString(const TString &str)
Definition: Reconstruction/Jet/JetUncertainties/JetUncertainties/Helpers.h:137
xAOD::JetAttributeAccessor::AccessorWrapper< xAOD::JetFourMom_t >::getAttribute
void getAttribute(const SG::AuxElement &p, xAOD::JetFourMom_t &v) const
Definition: JetAccessors.h:58
beamspotman.tokens
tokens
Definition: beamspotman.py:1284
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
jet::JetFourMomAccessor::operator()
xAOD::JetFourMom_t operator()(const xAOD::Jet &jet) const
Definition: Reconstruction/Jet/JetUncertainties/JetUncertainties/Helpers.h:42
jet::JetFourMomAccessor
JetFourMomAccessor is an extension of JetAttributeAccessor::AccessorWrapper<xAOD::JetFourMom_t> Acces...
Definition: JetCalibTools_PlotJESFactors.cxx:32
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
jet::utils::vectorize
bool vectorize(const TString &str, const TString &sep, std::vector< T > &result)
Definition: Reconstruction/Jet/JetUncertainties/JetUncertainties/Helpers.h:147
xAOD::JetFourMom_t
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiM4D< double > > JetFourMom_t
Base 4 Momentum type for Jet.
Definition: JetTypes.h:17
grepfile.sep
sep
Definition: grepfile.py:38
jet::utils::isTypeObjFromString
bool isTypeObjFromString(const std::string &str)
Definition: Reconstruction/Jet/JetUncertainties/JetUncertainties/Helpers.h:101
jet::utils::fileExists
bool fileExists(const TString &fileName)
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:94
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
jet::utils::findFilePath
TString findFilePath(const TString &fileName, const TString &path="", const TString &calibArea="")
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:99
xAOD::JetAttributeAccessor::AccessorWrapper
Definition: JetAccessors.h:49
TH1
Definition: rootspy.cxx:268
utils
Definition: Trigger/TrigMonitoring/TrigMinBiasMonitoring/python/utils.py:1
jet::utils::scaleHistoAxes
void scaleHistoAxes(TH1 *toScale, const double factorX=1, const double factorY=1, const double factorZ=1)
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:177
jet::utils::getTypeObjFromString< TString >
bool getTypeObjFromString< TString >(const std::string &str, TString &obj)
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:26
jet::utils::readRootFile
TFile * readRootFile(const TString &fileName, const TString &path="", const TString &calibArea="")
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:139
str
Definition: BTagTrackIpAccessor.cxx:11
jet::utils::getTypeObjFromString
bool getTypeObjFromString(const std::string &str, T &obj)
Definition: Reconstruction/Jet/JetUncertainties/JetUncertainties/Helpers.h:116
JetAccessors.h
This header defines wrapper classes around SG::AuxElement::Accessor used internally in the Jet EDM.
python.PyAthena.obj
obj
Definition: PyAthena.py:135
beamspotman.fail
def fail(message)
Definition: beamspotman.py:201
jet::utils::getTypeObjFromString< bool >
bool getTypeObjFromString< bool >(const std::string &str, bool &obj)
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:33