ATLAS Offline Software
PixelDigitizationUtilities.cxx
Go to the documentation of this file.
1 /*
2 
3  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
4 
5 */
6 
7 
8 
10 
11 #include "BichselData.h"
12 
13 #include <fstream>
14 
15 #include <exception>
16 
17 #include <regex>
18 
19 #include <algorithm> //std::lower_bound
20 
21 #include <iterator> //std::distance
22 
23 #include <limits>
24 
25 
26 
27 
28 
30 
31  std::string
32 
33  formBichselDataFileName(int particleType, unsigned int nCols){
34 
35  const std::string prefix{"PixelDigitization/Bichsel_"};
36 
37  const std::string suffix{".dat"};
38 
39  const std::string particleTypeString = std::to_string(particleType);
40 
41  const std::string nColString= (nCols == 1) ? "" : "_" + std::to_string(nCols) + "steps";
42 
43  return prefix + particleTypeString + nColString + suffix;
44 
45  }
46 
47 
48 
50 
51  getBichselDataFromFile(const std::string & fullFilename){
52 
53  std::ifstream inputFile(fullFilename);
54 
55  if (not inputFile){
56 
57  std::string errmsg = "getBichselDataFromFile: File " + fullFilename +" could not be opened.";
58 
59  throw std::runtime_error(errmsg);
60 
61  }
62 
63  BichselData iData;
64 
65  std::string thisLine;
66 
67  thisLine.reserve(60); //expecting maximum line length to be 53, allow some margin
68 
69  while (getline(inputFile, thisLine)) {
70 
71  const auto & [logBetaGamma, logCollisionEnergy, logCrossSection] = parseThreeDoubles(thisLine);
72 
73  iData.addEntry(logBetaGamma, logCollisionEnergy, logCrossSection);
74 
75  }
76 
78 
79  return iData;
80 
81  }
82 
83 
84 
85  std::tuple<double, double, double>
86 
87  parseThreeDoubles(const std::string & line){
88 
89  std::tuple<double, double, double> result{0., 0.,0.};
90  //in local testing, making the following 'static const' reduced the test time (one parse)
91  //from 1 637us to 455us. A total file parse test (209 lines of data) was reduced from
92  //54 156us to 3 657us
93  static const std::regex threeDoublesRx("^([-+.0-9eE]+)\\s+([-+.0-9eE]+)\\s+([-+.0-9eE]+)$");
94 
95  std::smatch x3;
96 
97  bool foundDoubles=std::regex_match(line, x3, threeDoublesRx);
98 
99  if (foundDoubles){
100 
101  try{
102 
103  //std::stod might throw a out_of_range or an invalid_argument error
104 
105  //both of which are subclasses of logic_error
106 
107  result = {std::stod(x3[1]), std::stod(x3[2]), std::stod(x3[3])};
108 
109  } catch (std::logic_error & e){
110 
111  const std::string msg("parseThreeDoubles: error in parsing a number in "+ line);
112 
113  throw(std::runtime_error(msg));
114 
115  }
116 
117  } else {
118 
119  const std::string msg("parseThreeDoubles: error in parsing the line " + line);
120 
121  throw(std::runtime_error(msg));
122 
123  }
124 
125  return result;
126 
127  }
128 
129 
130 
131  std::pair<int, int>
132 
133  fastSearch(const std::vector<double> & vec, double item){
134 
135  std::pair<int, int> output{-1, -1};
136 
137  if (vec.empty()) return output;
138 
139  int index_low = 0;
140 
141  int index_up = vec.size() - 1;
142 
143 
144 
145  if ((item < vec[index_low]) || (item > vec[index_up])) {
146 
147  return output;
148 
149  } else if (item == vec[index_low]) {
150 
151  output.first = index_low;
152 
153  output.second = index_low;
154 
155  return output;
156 
157  } else if (item == vec[index_up]) {
158 
159  output.first = index_up;
160 
161  output.second = index_up;
162 
163  return output;
164 
165  }
166 
167 
168 
169  while ((index_up - index_low) != 1) {
170 
171  int index_middle = int(1.0 * (index_up + index_low) / 2.);
172 
173  if (item < vec[index_middle]) index_up = index_middle;
174 
175  else if (item > vec[index_middle]) index_low = index_middle;
176 
177  else { // accurate hit. Though this is nearly impossible ...
178 
179  output.first = index_middle;
180 
181  output.second = index_middle;
182 
183  return output;
184 
185  }
186 
187  }
188 
189 
190 
191  output.first = index_low;
192 
193  output.second = index_up;
194 
195  return output;
196 
197  }
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 }
checkFileSG.line
line
Definition: checkFileSG.py:75
PixelDigitization::parseThreeDoubles
std::tuple< double, double, double > parseThreeDoubles(const std::string &line)
Definition: PixelDigitizationUtilities.cxx:87
get_generator_info.result
result
Definition: get_generator_info.py:21
hotSpotInTAG.suffix
string suffix
Definition: hotSpotInTAG.py:186
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
BichselData
Definition: BichselData.h:14
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
PixelDigitization
Definition: PixelDigitizationUtilities.cxx:29
particleType
Definition: particleType.h:29
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
BichselData::addEntry
void addEntry(double logBetaGamma, double logCollisionEnergy, double logIntegratedCrossSection)
Definition: BichselData.cxx:33
CaloCondBlobAlgs_fillNoiseFromASCII.inputFile
string inputFile
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:17
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
PixelDigitization::getBichselDataFromFile
BichselData getBichselDataFromFile(const std::string &fullFilename)
Definition: PixelDigitizationUtilities.cxx:51
merge.output
output
Definition: merge.py:17
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
BichselData.h
BichselData::logHighestCrossSectionsVector
std::vector< double > logHighestCrossSectionsVector
Definition: BichselData.h:19
item
Definition: ItemListSvc.h:43
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
PixelDigitization::formBichselDataFileName
std::string formBichselDataFileName(int particleType, unsigned int nCols)
Definition: PixelDigitizationUtilities.cxx:33
PixelDigitization::fastSearch
std::pair< int, int > fastSearch(const std::vector< double > &vec, double item)
Definition: PixelDigitizationUtilities.cxx:133
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
PixelDigitizationUtilities.h
BichselData::logIntegratedCrossSectionsVectorOfVector
std::vector< std::vector< double > > logIntegratedCrossSectionsVectorOfVector
Definition: BichselData.h:18