File indexing completed on 2024-04-06 12:31:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 #include "TopQuarkAnalysis/TopHitFit/interface/Resolution.h"
0034 #include "CLHEP/Random/RandGauss.h"
0035 #include <cmath>
0036 #include <iostream>
0037 #include <cctype>
0038 #include <cstdlib>
0039
0040 using std::isdigit;
0041 using std::isspace;
0042 using std::ostream;
0043 using std::sqrt;
0044 using std::string;
0045 #ifndef __GNUC__
0046 using std::atof;
0047 #endif
0048
0049 namespace {
0050
0051 bool get_field(string s, string::size_type i, double& x)
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 {
0068 string::size_type j = i;
0069 while (j < s.size() && s[j] != ',' && !isdigit(s[j]) && s[j] != '.')
0070 ++j;
0071 if (j < s.size() && (isdigit(s[j]) || s[j] == '.')) {
0072 x = atof(s.c_str() + j);
0073 return true;
0074 }
0075 return false;
0076 }
0077
0078 }
0079
0080 namespace hitfit {
0081
0082 Resolution::Resolution(std::string s )
0083
0084
0085
0086
0087
0088
0089
0090 : _resolution_exponent(0) {
0091 _inverse = false;
0092 _constant_sigma = 0;
0093 _resolution_sigma = 0;
0094 _noise_sigma = 0;
0095
0096
0097 double x;
0098 string::size_type i = 0;
0099 while (i < s.size() && isspace(s[i]))
0100 ++i;
0101
0102
0103 if (s[i] == '-') {
0104 _inverse = true;
0105 ++i;
0106 } else if (s[i] == '+') {
0107 ++i;
0108 }
0109
0110
0111 if (get_field(s, i, x))
0112 _constant_sigma = x;
0113 i = s.find(',', i);
0114
0115
0116 if (i != string::npos) {
0117 ++i;
0118 if (get_field(s, i, x))
0119 _resolution_sigma = x;
0120
0121
0122 i = s.find(',', i);
0123 if (i != string::npos) {
0124 if (get_field(s, i + 1, x))
0125 _noise_sigma = x;
0126 }
0127 }
0128 }
0129
0130 Resolution::Resolution(double C, double R, double m, double N, bool inverse )
0131 : _constant_sigma(C), _resolution_sigma(R), _resolution_exponent(m), _noise_sigma(N), _inverse(inverse) {}
0132
0133 Resolution::Resolution(double res, bool inverse )
0134
0135
0136
0137
0138
0139
0140
0141
0142 : _constant_sigma(0), _resolution_sigma(0), _resolution_exponent(0), _noise_sigma(res), _inverse(inverse) {}
0143
0144 bool Resolution::inverse() const
0145
0146
0147
0148
0149
0150
0151 {
0152 return _inverse;
0153 }
0154
0155 double Resolution::C() const { return _constant_sigma; }
0156
0157 double Resolution::R() const { return _resolution_sigma; }
0158
0159 double Resolution::m() const { return _resolution_exponent; }
0160
0161 double Resolution::N() const { return _noise_sigma; }
0162
0163 double Resolution::sigma(double p) const
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173 {
0174 p = std::fabs(p);
0175 if (_inverse)
0176 p = 1 / p;
0177
0178 return sqrt((_constant_sigma * _constant_sigma * p + _resolution_sigma * _resolution_sigma) * p +
0179 _noise_sigma * _noise_sigma);
0180 }
0181
0182 double Resolution::pick(double x, double p, CLHEP::HepRandomEngine& engine) const
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196 {
0197 CLHEP::RandGauss gen(engine);
0198 if (_inverse)
0199 return 1 / gen.fire(1 / x, sigma(p));
0200 else
0201 return gen.fire(x, sigma(p));
0202 }
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212 std::ostream& operator<<(std::ostream& s, const Resolution& r)
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223 {
0224 if (r._inverse)
0225 s << "-";
0226 s << r._constant_sigma << "," << r._resolution_sigma << "," << r._noise_sigma;
0227 return s;
0228 }
0229
0230 }