ATLAS Offline Software
Loading...
Searching...
No Matches
Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
8
9#include "TSystem.h"
10#include "TH1.h"
11
12#include <math.h>
13
14namespace jet
15{
16namespace utils
17{
18template <>
19bool getTypeObjFromString<std::string>(const std::string& str, std::string& obj)
20{
21 obj = str;
22 return true;
23}
24
25template <>
26bool getTypeObjFromString<TString>(const std::string& str, TString& obj)
27{
28 obj = str.c_str();
29 return true;
30}
31
32template <>
33bool getTypeObjFromString<bool>(const std::string& str, bool& obj)
34{
35 bool toReturn = false;
36 if (str == "true" || str == "True" || str == "TRUE")
37 {
38 obj = true;
39 toReturn = true;
40 }
41 else if (str == "false" || str == "False" || str == "FALSE")
42 {
43 obj = false;
44 toReturn = true;
45 }
46 else
47 {
48 int value;
49 toReturn = getTypeObjFromString<int>(str,value);
50 if (toReturn)
51 obj = static_cast<bool>(value);
52 }
53 return toReturn;
54}
55
56template <>
57bool getTypeObjFromString<std::string>(const TString& str, std::string& obj)
58{
59 obj = str.Data();
60 return true;
61}
62
63template <>
64bool getTypeObjFromString<TString>(const TString& str, TString& obj)
65{
66 obj = str;
67 return true;
68}
69
70template <>
71bool getTypeObjFromString<bool>(const TString& str, bool& obj)
72{
73 bool toReturn = false;
74 if (str.EqualTo("true",TString::kIgnoreCase))
75 {
76 obj = true;
77 toReturn = true;
78 }
79 else if (str.EqualTo("false",TString::kIgnoreCase))
80 {
81 obj = false;
82 toReturn = true;
83 }
84 else
85 {
86 int value;
87 toReturn = getTypeObjFromString<int>(str,value);
88 if (toReturn)
89 obj = static_cast<bool>(value);
90 }
91 return toReturn;
92}
93
94bool fileExists(const TString& fileName)
95{
96 return !gSystem->AccessPathName(fileName);
97}
98
99TString findFilePath(const TString& fileName, const TString& path, const TString& calibArea)
100{
101 TString pathToGet = "";
102
103 // First, try the raw filename plus path (user-specified), then raw filename (local or full path)
104 if (path != "" && fileExists(path+(path.EndsWith("/")?"":"/")+fileName))
105 pathToGet = path+(path.EndsWith("/")?"":"/")+fileName;
106 else if (fileExists(fileName))
107 pathToGet = fileName;
108
109 // Next, try PathResolver in a few configurations
110 // PathResolver #1: versioned CalibArea (most users should be in this case)
111 //coverity[copy_paste_error]
112 if (pathToGet == "" && calibArea != "")
113 pathToGet = TString(PathResolverFindCalibFile(Form("JetUncertainties/%s/%s",calibArea.Data(),fileName.Data())).c_str());
114 // PathResolver #2: unversioned CalibArea (legacy support)
115 if (pathToGet == "" && calibArea == "")
116 pathToGet = TString(PathResolverFindCalibFile(Form("JetUncertainties/%s",fileName.Data())).c_str());
117 // PathResolver #3: general search in case of files residing in other packages (request from a Higgs group)
118 if (pathToGet == "")
119 pathToGet = TString(PathResolverFindCalibFile(fileName.Data()).c_str());
120
121 // Try backup locations now (legacy support)
122 if (pathToGet == "")
123 {
124 // Try ROOTCOREBIN
125 if (TString(gSystem->Getenv("ROOTCOREBIN")) != "" && fileExists(TString(gSystem->Getenv("ROOTCOREBIN"))+"/data/JetUncertainties/"+fileName))
126 pathToGet = TString(gSystem->Getenv("ROOTCOREBIN"))+"/data/JetUncertainties/"+fileName;
127 // Try ROOTCOREDIR
128 else if (TString(gSystem->Getenv("ROOTCOREDIR")) != "" && fileExists(TString(gSystem->Getenv("ROOTCOREDIR"))+"/data/JetUncertainties/"+fileName))
129 pathToGet = TString(gSystem->Getenv("ROOTCOREDIR"))+"/data/JetUncertainties/"+fileName;
130 // Try standard athena location
131 else if (TString(gSystem->Getenv("TestArea")) != "" && fileExists(TString(gSystem->Getenv("TestArea"))+"/Reconstruction/Jet/JetUncertainties/share/"+fileName))
132 pathToGet = TString(gSystem->Getenv("TestArea"))+"/Reconstruction/Jet/JetUncertainties/share/"+fileName;
133 }
134
135 // Give up, hopefully sucessfully
136 // User must check if return is "" (failed) or an actual path (success)
137 return pathToGet;
138}
139
140TFile* readRootFile(const TString& fileName, const TString& path, const TString& calibArea)
141{
142 TFile* rootFile = nullptr;
143
144 TString pathToGet = findFilePath(fileName,path,calibArea);
145 //std::cout << "Looking for file " << fileName << " in path " << path << std::endl;
146
147 if (pathToGet != "")
148 rootFile = new TFile(pathToGet,"READ");
149 return rootFile;
150}
151
152
153std::vector<double> getLogBins(const size_t numBins, const double minVal, const double maxVal)
154{
155 std::vector<double> bins;
156 bins.resize(numBins+1);
157
158 const double dx = (log(maxVal)-log(minVal))/numBins;
159 for (size_t iBin = 0; iBin <= numBins; ++iBin)
160 bins[iBin] = exp(log(minVal)+iBin*dx);
161
162 return bins;
163}
164
165std::vector<double> getUniformBins(const size_t numBins, const double minVal, const double maxVal)
166{
167 std::vector<double> bins;
168 bins.resize(numBins+1);
169
170 const double dx = (maxVal-minVal)/numBins;
171 for (size_t iBin = 0; iBin <= numBins; ++iBin)
172 bins[iBin] = minVal+iBin*dx;
173
174 return bins;
175}
176
177
178void scaleHistoAxes(TH1* toScale, const double factorX, const double factorY, const double factorZ)
179{
180 const int dim = toScale->GetDimension();
181
182 // Check for simple copies and sanity checks
183 bool returnDuplicate = false;
184 if ( dim == 1 && (factorX < 0 || fabs(factorX-1) < 1.e-4) )
185 returnDuplicate = true;
186 else if ( dim == 2 && (factorX < 0 || fabs(factorX-1) < 1.e-4)
187 && (factorY < 0 || fabs(factorY-1) < 1.e-4) )
188 returnDuplicate = true;
189 else if ( dim == 3 && (factorX < 0 || fabs(factorX-1) < 1.e-4)
190 && (factorY < 0 || fabs(factorY-1) < 1.e-4)
191 && (factorZ < 0 || fabs(factorZ-1) < 1.e-4) )
192 returnDuplicate = true;
193 if (returnDuplicate)
194 {
195 printf("ScaleHistoAxes: Doing nothing, as all scale factors require no changes\n");
196 return;
197 }
198
199 // Let negative numbers mean no change
200 const double facX = factorX < 0 ? 1 : factorX;
201 const double facY = factorY < 0 ? 1 : factorY;
202 const double facZ = factorZ < 0 ? 1 : factorZ;
203
204 // Scale the x axis if necessary
205 if (fabs(facX-1) > 1.e-4)
206 {
207 std::vector<double> binsX;
208 for (int iBin = 1; iBin <= toScale->GetNbinsX()+1; ++iBin)
209 binsX.push_back(toScale->GetXaxis()->GetBinLowEdge(iBin)*facX);
210 toScale->GetXaxis()->Set(binsX.size()-1,&binsX[0]);
211 }
212 // Scale the y axis if necessary
213 if (dim > 1 && fabs(facY-1) > 1.e-4)
214 {
215 std::vector<double> binsY;
216 for (int iBin = 1; iBin <= toScale->GetNbinsY()+1; ++iBin)
217 binsY.push_back(toScale->GetYaxis()->GetBinLowEdge(iBin)*facY);
218 toScale->GetYaxis()->Set(binsY.size()-1,&binsY[0]);
219 }
220 // Scale the z axis if necessary
221 if (dim > 2 && fabs(facZ-1) > 1.e-4)
222 {
223 std::vector<double> binsZ;
224 for (int iBin = 1; iBin <= toScale->GetNbinsZ()+1; ++iBin)
225 binsZ.push_back(toScale->GetZaxis()->GetBinLowEdge(iBin)*facZ);
226 toScale->GetZaxis()->Set(binsZ.size()-1,&binsZ[0]);
227 }
228
229}
230
231
232} // End utils namespace
233} // End jet namespace
static const std::vector< std::string > bins
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
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 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)