ATLAS Offline Software
Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 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 
14 namespace jet
15 {
16 namespace utils
17 {
18 template <>
19 bool getTypeObjFromString<std::string>(const std::string& str, std::string& obj)
20 {
21  obj = str;
22  return true;
23 }
24 
25 template <>
26 bool getTypeObjFromString<TString>(const std::string& str, TString& obj)
27 {
28  obj = str.c_str();
29  return true;
30 }
31 
32 template <>
33 bool 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 
56 template <>
57 bool getTypeObjFromString<std::string>(const TString& str, std::string& obj)
58 {
59  obj = str.Data();
60  return true;
61 }
62 
63 template <>
64 bool getTypeObjFromString<TString>(const TString& str, TString& obj)
65 {
66  obj = str;
67  return true;
68 }
69 
70 template <>
71 bool 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 
94 bool fileExists(const TString& fileName)
95 {
96  return !gSystem->AccessPathName(fileName);
97 }
98 
99 TString 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  if (pathToGet == "" && calibArea != "")
112  pathToGet = TString(PathResolverFindCalibFile(Form("JetUncertainties/%s/%s",calibArea.Data(),fileName.Data())).c_str());
113  // PathResolver #2: unversioned CalibArea (legacy support)
114  if (pathToGet == "" && calibArea == "")
115  pathToGet = TString(PathResolverFindCalibFile(Form("JetUncertainties/%s",fileName.Data())).c_str());
116  // PathResolver #3: general search in case of files residing in other packages (request from a Higgs group)
117  if (pathToGet == "")
118  pathToGet = TString(PathResolverFindCalibFile(fileName.Data()).c_str());
119 
120  // Try backup locations now (legacy support)
121  if (pathToGet == "")
122  {
123  // Try ROOTCOREBIN
124  if (TString(gSystem->Getenv("ROOTCOREBIN")) != "" && fileExists(TString(gSystem->Getenv("ROOTCOREBIN"))+"/data/JetUncertainties/"+fileName))
125  pathToGet = TString(gSystem->Getenv("ROOTCOREBIN"))+"/data/JetUncertainties/"+fileName;
126  // Try ROOTCOREDIR
127  else if (TString(gSystem->Getenv("ROOTCOREDIR")) != "" && fileExists(TString(gSystem->Getenv("ROOTCOREDIR"))+"/data/JetUncertainties/"+fileName))
128  pathToGet = TString(gSystem->Getenv("ROOTCOREDIR"))+"/data/JetUncertainties/"+fileName;
129  // Try standard athena location
130  else if (TString(gSystem->Getenv("TestArea")) != "" && fileExists(TString(gSystem->Getenv("TestArea"))+"/Reconstruction/Jet/JetUncertainties/share/"+fileName))
131  pathToGet = TString(gSystem->Getenv("TestArea"))+"/Reconstruction/Jet/JetUncertainties/share/"+fileName;
132  }
133 
134  // Give up, hopefully sucessfully
135  // User must check if return is "" (failed) or an actual path (success)
136  return pathToGet;
137 }
138 
139 TFile* readRootFile(const TString& fileName, const TString& path, const TString& calibArea)
140 {
141  TFile* rootFile = nullptr;
142 
143  TString pathToGet = findFilePath(fileName,path,calibArea);
144  //std::cout << "Looking for file " << fileName << " in path " << path << std::endl;
145 
146  if (pathToGet != "")
147  rootFile = new TFile(pathToGet,"READ");
148  return rootFile;
149 }
150 
151 
152 std::vector<double> getLogBins(const size_t numBins, const double minVal, const double maxVal)
153 {
154  std::vector<double> bins;
155  bins.resize(numBins+1);
156 
157  const double dx = (log(maxVal)-log(minVal))/numBins;
158  for (size_t iBin = 0; iBin <= numBins; ++iBin)
159  bins[iBin] = exp(log(minVal)+iBin*dx);
160 
161  return bins;
162 }
163 
164 std::vector<double> getUniformBins(const size_t numBins, const double minVal, const double maxVal)
165 {
166  std::vector<double> bins;
167  bins.resize(numBins+1);
168 
169  const double dx = (maxVal-minVal)/numBins;
170  for (size_t iBin = 0; iBin <= numBins; ++iBin)
171  bins[iBin] = minVal+iBin*dx;
172 
173  return bins;
174 }
175 
176 
177 void scaleHistoAxes(TH1* toScale, const double factorX, const double factorY, const double factorZ)
178 {
179  const int dim = toScale->GetDimension();
180 
181  // Check for simple copies and sanity checks
182  bool returnDuplicate = false;
183  if ( dim == 1 && (factorX < 0 || fabs(factorX-1) < 1.e-4) )
184  returnDuplicate = true;
185  else if ( dim == 2 && (factorX < 0 || fabs(factorX-1) < 1.e-4)
186  && (factorY < 0 || fabs(factorY-1) < 1.e-4) )
187  returnDuplicate = true;
188  else if ( dim == 3 && (factorX < 0 || fabs(factorX-1) < 1.e-4)
189  && (factorY < 0 || fabs(factorY-1) < 1.e-4)
190  && (factorZ < 0 || fabs(factorZ-1) < 1.e-4) )
191  returnDuplicate = true;
192  if (returnDuplicate)
193  {
194  printf("ScaleHistoAxes: Doing nothing, as all scale factors require no changes\n");
195  return;
196  }
197 
198  // Let negative numbers mean no change
199  const double facX = factorX < 0 ? 1 : factorX;
200  const double facY = factorY < 0 ? 1 : factorY;
201  const double facZ = factorZ < 0 ? 1 : factorZ;
202 
203  // Scale the x axis if necessary
204  if (fabs(facX-1) > 1.e-4)
205  {
206  std::vector<double> binsX;
207  for (int iBin = 1; iBin <= toScale->GetNbinsX()+1; ++iBin)
208  binsX.push_back(toScale->GetXaxis()->GetBinLowEdge(iBin)*facX);
209  toScale->GetXaxis()->Set(binsX.size()-1,&binsX[0]);
210  }
211  // Scale the y axis if necessary
212  if (dim > 1 && fabs(facY-1) > 1.e-4)
213  {
214  std::vector<double> binsY;
215  for (int iBin = 1; iBin <= toScale->GetNbinsY()+1; ++iBin)
216  binsY.push_back(toScale->GetYaxis()->GetBinLowEdge(iBin)*facY);
217  toScale->GetYaxis()->Set(binsY.size()-1,&binsY[0]);
218  }
219  // Scale the z axis if necessary
220  if (dim > 2 && fabs(facZ-1) > 1.e-4)
221  {
222  std::vector<double> binsZ;
223  for (int iBin = 1; iBin <= toScale->GetNbinsZ()+1; ++iBin)
224  binsZ.push_back(toScale->GetZaxis()->GetBinLowEdge(iBin)*facZ);
225  toScale->GetZaxis()->Set(binsZ.size()-1,&binsZ[0]);
226  }
227 
228 }
229 
230 
231 } // End utils namespace
232 } // End jet namespace
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
yodamerge_tmp.dim
dim
Definition: yodamerge_tmp.py:239
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
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
ChangeHistoRange.binsZ
list binsZ
Definition: ChangeHistoRange.py:63
athena.value
value
Definition: athena.py:124
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
Helpers.h
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ChangeHistoRange.binsY
list binsY
Definition: ChangeHistoRange.py:59
makeComparison.rootFile
rootFile
Definition: makeComparison.py:27
plotting.yearwise_luminosity_vs_mu.bins
bins
Definition: yearwise_luminosity_vs_mu.py:30
PathResolver.h
jet::utils::fileExists
bool fileExists(const TString &fileName)
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:94
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
jet::utils::findFilePath
TString findFilePath(const TString &fileName, const TString &path="", const TString &calibArea="")
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:99
python.CaloScaleNoiseConfig.str
str
Definition: CaloScaleNoiseConfig.py:78
utils
Definition: Trigger/TrigMonitoring/TrigMinBiasMonitoring/python/utils.py:1
makeTRTBarrelCans.dx
tuple dx
Definition: makeTRTBarrelCans.py:20
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
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
ChangeHistoRange.binsX
list binsX
Definition: ChangeHistoRange.py:56
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
python.PyAthena.obj
obj
Definition: PyAthena.py:132
jet::utils::getTypeObjFromString< bool >
bool getTypeObjFromString< bool >(const std::string &str, bool &obj)
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:33