ATLAS Offline Software
PixelDigitizationUtilities.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
5 #include "PixelConditionsData/ChargeCalibParameters.h" //Thresholds, LegacyFitParameters etc
6 #include "BichselData.h"
7 #include "CLHEP/Random/RandGaussZiggurat.h"
8 #include <fstream>
9 #include <exception>
10 #include <regex>
11 #include <algorithm> //std::lower_bound
12 #include <iterator> //std::distance
13 #include <limits>
14 
15 
17  std::string
19  const std::string prefix{"PixelDigitization/Bichsel_"};
20  const std::string suffix{".dat"};
21  const std::string particleTypeString = std::to_string(particleType);
22  const std::string nColString= (nCols == 1) ? "" : "_" + std::to_string(nCols) + "steps";
23  return prefix + particleTypeString + nColString + suffix;
24  }
25 
26 
28  getBichselDataFromFile(const std::string & fullFilename){
29  std::ifstream inputFile(fullFilename);
30  if (not inputFile){
31  std::string errmsg = "getBichselDataFromFile: File " + fullFilename +" could not be opened.";
32  throw std::runtime_error(errmsg);
33  }
34  BichselData iData;
35  std::string thisLine;
36  thisLine.reserve(60); //expecting maximum line length to be 53, allow some margin
37  while (getline(inputFile, thisLine)) {
38  const auto & [logBetaGamma, logCollisionEnergy, logCrossSection] = parseThreeDoubles(thisLine);
39  iData.addEntry(logBetaGamma, logCollisionEnergy, logCrossSection);
40  }
42  return iData;
43  }
44 
45 
46  std::tuple<double, double, double>
47  parseThreeDoubles(const std::string & line){
48  std::tuple<double, double, double> result{0., 0.,0.};
49  //in local testing, making the following 'static const' reduced the test time (one parse)
50  //from 1 637us to 455us. A total file parse test (209 lines of data) was reduced from
51  //54 156us to 3 657us
52  static const std::regex threeDoublesRx("^([-+.0-9eE]+)\\s+([-+.0-9eE]+)\\s+([-+.0-9eE]+)$");
53  std::smatch x3;
54  bool foundDoubles=std::regex_match(line, x3, threeDoublesRx);
55  if (foundDoubles){
56  try{
57  //std::stod might throw a out_of_range or an invalid_argument error
58  //both of which are subclasses of logic_error
59  result = {std::stod(x3[1]), std::stod(x3[2]), std::stod(x3[3])};
60  } catch (std::logic_error & e){
61  const std::string msg("parseThreeDoubles: error in parsing a number in "+ line);
62  throw(std::runtime_error(msg));
63  }
64  } else {
65  const std::string msg("parseThreeDoubles: error in parsing the line " + line);
66  throw(std::runtime_error(msg));
67  }
68  return result;
69  }
70 
71 
72  std::pair<int, int>
73  fastSearch(const std::vector<double> & vec, double item){
74  std::pair<int, int> output{-1, -1};
75  if (vec.empty()) return output;
76  int index_low = 0;
77  int index_up = vec.size() - 1;
78  if ((item < vec[index_low]) || (item > vec[index_up])) {
79  return output;
80  } else if (item == vec[index_low]) {
81  output.first = index_low;
82  output.second = index_low;
83  return output;
84  } else if (item == vec[index_up]) {
85  output.first = index_up;
86  output.second = index_up;
87  return output;
88  }
89  while ((index_up - index_low) != 1) {
90  int index_middle = int(1.0 * (index_up + index_low) / 2.);
91  if (item < vec[index_middle]) index_up = index_middle;
92  else if (item > vec[index_middle]) index_low = index_middle;
93  else { // accurate hit. Though this is nearly impossible ...
94  output.first = index_middle;
95  output.second = index_middle;
96  return output;
97  }
98  }
99  output.first = index_low;
100  output.second = index_up;
101  return output;
102  }
103 
104  double
105  randomThreshold(const PixelChargeCalib::Thresholds & t, CLHEP::HepRandomEngine* pEngine){
106  const double & thrand1 = CLHEP::RandGaussZiggurat::shoot(pEngine);
107  const double & thrand2 = CLHEP::RandGaussZiggurat::shoot(pEngine);
108  return t.value + t.sigma * thrand1 + t.noise * thrand2;
109  }
110 }
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
checkFileSG.line
line
Definition: checkFileSG.py:75
PixelDigitization::parseThreeDoubles
std::tuple< double, double, double > parseThreeDoubles(const std::string &line)
Definition: PixelDigitizationUtilities.cxx:47
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
ChargeCalibParameters.h
Structs for holding charge calibration parameterisation and data.
BichselData
Definition: BichselData.h:14
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
PixelDigitization
Definition: PixelDigitizationUtilities.cxx:16
particleType
Definition: particleType.h:29
PixelDigitization::randomThreshold
double randomThreshold(const PixelChargeCalib::Thresholds &t, CLHEP::HepRandomEngine *pEngine)
Definition: PixelDigitizationUtilities.cxx:105
PixelChargeCalib::Thresholds
Definition: ChargeCalibParameters.h:73
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:28
RPDUtils::nCols
unsigned constexpr int nCols
Definition: RPDUtils.h:25
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
PixelDigitization::formBichselDataFileName
std::string formBichselDataFileName(int particleType, unsigned int nCols)
Definition: PixelDigitizationUtilities.cxx:18
PixelDigitization::fastSearch
std::pair< int, int > fastSearch(const std::vector< double > &vec, double item)
Definition: PixelDigitizationUtilities.cxx:73
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
PixelDigitizationUtilities.h
BichselData::logIntegratedCrossSectionsVectorOfVector
std::vector< std::vector< double > > logIntegratedCrossSectionsVectorOfVector
Definition: BichselData.h:18