00001 #ifndef JETUNCERTAINTIES_HELPERS_H
00002 #define JETUNCERTAINTIES_HELPERS_H
00003
00004 #include <iostream>
00005 #include <sstream>
00006 #include <string>
00007 #include <vector>
00008
00009 #include "TString.h"
00010 #include "TObjString.h"
00011 #include "TObjArray.h"
00012 #include "TFile.h"
00013
00014 #include "AsgTools/MsgStreamMacros.h"
00015
00016 #include "xAODJet/Jet.h"
00017 #include "xAODJet/JetAccessors.h"
00018
00019 #define JESUNC_ERROR_CODE -1234
00020 #define JESUNC_NO_DEFAULT_CONSTRUCTOR ATH_MSG_FATAL("Default constructor is not supported");
00021 #define JESUNC_SAFE_DELETE(T) { if(T) { delete T; T = NULL; } }
00022
00023 class TH1;
00024
00025 namespace jet
00026 {
00035 class JetFourMomAccessor: public xAOD::JetAttributeAccessor::AccessorWrapper<xAOD::JetFourMom_t> {
00036 public:
00037 using xAOD::JetAttributeAccessor::AccessorWrapper<xAOD::JetFourMom_t>::AccessorWrapper;
00038 xAOD::JetFourMom_t operator()(const xAOD::Jet & jet) const {return const_cast<JetFourMomAccessor*>(this)->getAttribute(jet);}
00039 };
00040
00041 namespace utils
00042 {
00043
00044 template<typename T>
00045 bool isTypeObjFromString(const std::string& str);
00046 template<typename T>
00047 bool isTypeObjFromString(const TString& str);
00048
00049
00050 template<typename T>
00051 bool getTypeObjFromString(const std::string& str, T& obj);
00052 template<typename T>
00053 T getTypeObjFromString(const std::string& str);
00054 template<typename T>
00055 bool getTypeObjFromString(const TString& str, T& obj);
00056 template<typename T>
00057 T getTypeObjFromString(const TString& str);
00058
00059
00060 template <>
00061 bool getTypeObjFromString<std::string>(const std::string& str, std::string& obj);
00062 template <>
00063 bool getTypeObjFromString<TString>(const std::string& str, TString& obj);
00064 template <>
00065 bool getTypeObjFromString<bool>(const std::string& str, bool& obj);
00066 template <>
00067 bool getTypeObjFromString<std::string>(const TString& str, std::string& obj);
00068 template <>
00069 bool getTypeObjFromString<TString>(const TString& str, TString& obj);
00070 template <>
00071 bool getTypeObjFromString<bool>(const TString& str, bool& obj);
00072
00073
00074 template <typename T>
00075 bool vectorize(const TString& str, const TString& sep, std::vector<T>& result);
00076 template <typename T>
00077 std::vector<T> vectorize(const TString& str, const TString& sep);
00078
00079
00080 bool fileExists(const TString& fileName);
00081
00082
00083 TString findFilePath(const TString& fileName, const TString& path = "");
00084
00085
00086 TFile* readRootFile(const TString& fileName, const TString& path = "");
00087
00088
00089 std::vector<double> getLogBins(const size_t numBins, const double minVal, const double maxVal);
00090 std::vector<double> getUniformBins(const size_t numBins, const double minVal, const double maxVal);
00091
00092
00093 void scaleHistoAxes(TH1* toScale, const double factorX=1, const double factorY=1, const double factorZ=1);
00094 }
00095
00096 template <typename T>
00097 bool utils::isTypeObjFromString(const std::string& str)
00098 {
00099 std::istringstream iss(str);
00100 T obj;
00101 return !(iss >> obj).fail();
00102 }
00103
00104 template <typename T>
00105 bool utils::isTypeObjFromString(const TString& str)
00106 {
00107 std::string stdstr = str.Data();
00108 return isTypeObjFromString<T>(stdstr);
00109 }
00110
00111 template <typename T>
00112 bool utils::getTypeObjFromString(const std::string& str, T& obj)
00113 {
00114 std::istringstream iss(str);
00115 return !(iss >> obj).fail();
00116 }
00117 template <typename T>
00118 T utils::getTypeObjFromString(const std::string& str)
00119 {
00120 T toReturn;
00121 if (!getTypeObjFromString(str,toReturn))
00122 printf("Failed to convert object: %s\n",str.c_str());
00123
00124 return toReturn;
00125 }
00126 template <typename T>
00127 bool utils::getTypeObjFromString(const TString& str, T& obj)
00128 {
00129 std::string stdstr = str.Data();
00130 return getTypeObjFromString(stdstr,obj);
00131 }
00132 template <typename T>
00133 T utils::getTypeObjFromString(const TString& str)
00134 {
00135 T toReturn;
00136 if (!getTypeObjFromString(str,toReturn))
00137 printf("ERROR: Failed to convert object: %s\n",str.Data());
00138
00139 return toReturn;
00140 }
00141
00142 template <typename T>
00143 bool utils::vectorize(const TString& str, const TString& sep, std::vector<T>& result)
00144 {
00145 bool success = true;
00146 result.clear();
00147
00148 TObjArray* tokens = str.Tokenize(sep);
00149 TIter istr(tokens);
00150 while(TObjString* os=(TObjString*)istr())
00151 {
00152 T obj;
00153 if (!getTypeObjFromString(os->GetString(),obj))
00154 {
00155 success = false;
00156 break;
00157 }
00158 else
00159 result.push_back(obj);
00160 }
00161 delete tokens;
00162
00163 return success;
00164 }
00165
00166 template <typename T>
00167 std::vector<T> utils::vectorize(const TString& str, const TString& sep)
00168 {
00169 std::vector<T> result;
00170 TObjArray* tokens = str.Tokenize(sep);
00171 TIter istr(tokens);
00172
00173 while(TObjString* os=(TObjString*)istr())
00174 {
00175 T obj;
00176 if (!getTypeObjFromString(os->GetString(),obj))
00177 printf("ERROR: String \"%s\" is not the requested type\n",os->GetString().Data());
00178 result.push_back(obj);
00179 }
00180 delete tokens;
00181
00182 return result;
00183 }
00184
00185 }
00186
00187 #endif
00188
00189