ATLAS Offline Software
Loading...
Searching...
No Matches
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
18 formBichselDataFileName(int particleType, unsigned int nCols){
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}
Structs for holding charge calibration parameterisation and data.
std::vector< size_t > vec
static thread_local std::ostringstream errmsg
Definition WaferTree.h:25
std::tuple< double, double, double > parseThreeDoubles(const std::string &line)
std::string formBichselDataFileName(int particleType, unsigned int nCols)
BichselData getBichselDataFromFile(const std::string &fullFilename)
std::pair< int, int > fastSearch(const std::vector< double > &vec, double item)
double randomThreshold(const PixelChargeCalib::Thresholds &t, CLHEP::HepRandomEngine *pEngine)
std::vector< std::vector< double > > logIntegratedCrossSectionsVectorOfVector
Definition BichselData.h:18
void addEntry(double logBetaGamma, double logCollisionEnergy, double logIntegratedCrossSection)
std::vector< double > logHighestCrossSectionsVector
Definition BichselData.h:19
MsgStream & msg
Definition testRead.cxx:32