qupid 0.2
Quasi-Signal Generation Model for Pixelized Detector
Loading...
Searching...
No Matches
QPMath.cpp
1#include "QPMath.hpp"
2
3// #include "QPMath.h"
4#include "QPVector.hpp"
5#include "QPMatrix.hpp"
6
7#include <cmath>
8#include <iostream>
9
11 double integral,
12 double mean,
13 double sigma){
14 if(sigma <= 0) return 0.;
15 return QPMath::Gaus(x, mean, sigma, true) * integral;
16}
17
19 double y,
20 double integral,
21 double meanx,
22 double meany,
23 double sigma){
24 if(sigma <= 0) return 0.;
25 double chi = QPMath::Sqrt( QPMath::Power(x-meanx,2) + QPMath::Power(y-meany,2) ) / sigma;
26 return QPMath::Gaussian::Simple1D(chi, integral*QPMath::InvSqrt2Pi/(sigma*sigma));
27}
28
30 double y,
31 double integral,
32 double meanx,
33 double meany,
34 double sigmax,
35 double sigmay){
36 if(sigmax<=0 || sigmay<=0) return 0.;
37 double chi = QPMath::Sqrt(QPMath::Power(x-meanx,2)/QPMath::Power(sigmax,2) + QPMath::Power(y-meany,2)/QPMath::Power(sigmay,2));
38 return QPMath::Gaussian::Simple1D(chi, integral*QPMath::InvSqrt2Pi/(sigmax*sigmay));
39}
40
42 double y ,
43 double integral ,
44 double meanx ,
45 double meany ,
46 double sigmax ,
47 double sigmay ,
48 double rotation ){
49 if(sigmax<=0 || sigmay<=0) return 0.;
50 double xp = QPMath::Cos(rotation)*(x-meanx) + QPMath::Sin(rotation)*(y-meany);
51 double yp = QPMath::Cos(rotation)*(y-meany) - QPMath::Sin(rotation)*(x-meanx);
52 double chi = QPMath::Sqrt(QPMath::Power(xp,2)/QPMath::Power(sigmax,2) + QPMath::Power(yp,2)/QPMath::Power(sigmay,2));
53 return QPMath::Gaussian::Simple1D(chi, integral*QPMath::InvSqrt2Pi/(sigmax*sigmay));
54}
55
57 QPVector vx = x - x0;
58 QPMatrix X = QPMatrix(vx,2);
59
60 // std::cout<<X.Print(true)<<std::endl;
61 // std::cout<<X.Transpose().Print(true)<<std::endl;
62 // std::cout<<S.Print(true)<<std::endl;
63 // std::cout<<S.Inverse().Print(true)<<std::endl;
64
65 double expo = (X.Transpose() * (S.Inverse()) * X)[0];
66 return QPMath::Exp(-0.5 * expo) * 0.5* (QPMath::InvPi) / QPMath::Sqrt(S.Det());
67}
68
69// double QPMath::Gaussian::UniCylinder3D(double x,
70// double y,
71// double z,
72// double dirx ,
73// double diry ,
74// double dirz ,
75// double rotcx ,
76// double rotcy ,
77// double rotcx ,
78// double stx ,
79// double sty ,
80// double stz ,
81// double integral ,
82// double sigmax ,
83// double sigmay ,
84// double rotation ,
85// double length ,
86// double dispersion ){
87
88// AXVector pos = AXVector(x,y,z);
89// AXVector dir = AXVector(dirx,diry,dirz).Unit();
90// AXVector stt = AXVector(stx,sty,stz);
91
92// double pdist = ((stt-pos).Cross(dir)).Norm();
93// double ldist = QPMath::Sqrt((stt-pos).Norm2() - QPMath::Power(pdist,2));
94
95
96
97// return 0;
98// }
99
101 double y,
102 double integral ,
103 double meanx ,
104 double meany ,
105 double sigmax ,
106 double sigmay ,
107 double rotation ,
108 double length ,
109 double tilt ,
110 double dispersion ){
111
112 double xp = x-meanx;
113 double yp = y-meany;
114
115 // if( tilt > 0 && -1* length / QPMath::Sin(tilt) > xp) return 0;
116
117 double z = 0;
118 double xp3 = xp*QPMath::Cos(tilt) + z*QPMath::Sin(tilt);
119 double yp3 = yp;
120 double zp3 = -1*xp*QPMath::Sin(tilt) + z*(tilt) - length;
121
122 double sigmapx = -1* dispersion * zp3 + sigmax;
123 double sigmapy = -1* dispersion * zp3 + sigmay;
124
125 if(sigmapx<=0 || sigmapy<=0 ) return 0;
126
127 return QPMath::Gaussian::RotAxis2D(xp3, yp3, integral, 0, 0, sigmapx, sigmapy, rotation);
128}
129
130// double QPMath::Gaussian::AF::UniCylinder3D(double* x, double* par){
131// return QPMath::Gaussian::UniCylinder3D(x[0], x[1], x[2], par[0], par[1], par[2], par[3], par[4], par[5], par[6], par[7], par[8], par[9], par[10], par[11]);
132// }
133
134double QPMath::WoodSaxon::Simple1D(double x, double maximum, double mean, double a, double radius){
135 double xp = x-mean;
136 xp = x > 0 ? x : -1*x;
137 return maximum / ( 1+QPMath::Exp((xp-radius)/a));
138}
139
140double QPMath::WoodSaxon::Simple2D(double x, double y,
141 double maximum, double meanx, double meany, double a, double radius){
142 double dist = QPMath::Sqrt(QPMath::Power(x-meanx,2)+QPMath::Power(y-meany,2));
143 return QPMath::WoodSaxon::Simple1D(dist, maximum, 0, a, radius);
144}
145
146double QPMath::WoodSaxon::UniAxis2D(double x, double y,
147 double maximum, double meanx, double meany, double ax, double ay, double radiusX, double radiusY){
148 double dist = QPMath::Sqrt(QPMath::Power(x-meanx,2)+QPMath::Power(y-meany,2));
149 double a = QPMath::GeometryCalc::EllipseUniAxis(x-meanx, y-meany, ax, ay);
150 double radius = QPMath::GeometryCalc::EllipseUniAxis(x-meanx, y-meany, radiusX, radiusY);
151 return QPMath::WoodSaxon::Simple1D(dist, maximum, 0, a, radius);
152}
153
154double QPMath::WoodSaxon::RotAxis2D(double x, double y,
155 double maximum, double meanx, double meany,
156 double ax, double ay, double radiusX, double radiusY, double rotation){
157 double dist = QPMath::Sqrt(QPMath::Power(x-meanx,2)+QPMath::Power(y-meany,2));
158 double a = QPMath::GeometryCalc::EllipseRotAxis(x-meanx, y-meany, ax, ay, rotation);
159 double radius = QPMath::GeometryCalc::EllipseRotAxis(x-meanx, y-meany, radiusX, radiusY, rotation);
160 return QPMath::WoodSaxon::Simple1D(dist, maximum, 0, a, radius);
161}
162
163double QPMath::WoodSaxon::RotAxis2DUnsync(double x, double y,
164 double maximum, double meanx, double meany,
165 double ax, double ay, double radiusX, double radiusY, double rotationA, double rotationR){
166 double dist = QPMath::Sqrt(QPMath::Power(x-meanx,2)+QPMath::Power(y-meany,2));
167 double a = QPMath::GeometryCalc::EllipseRotAxis(x-meanx, y-meany, ax, ay, rotationA);
168 double radius = QPMath::GeometryCalc::EllipseRotAxis(x-meanx, y-meany, radiusX, radiusY, rotationR);
169 return QPMath::WoodSaxon::Simple1D(dist, maximum, 0, a, radius);
170}
171
172double QPMath::GeometryCalc::EllipseUniAxis(double x, double y, double lx, double ly){
173 return QPMath::GeometryCalc::EllipseRotAxis(x, y, lx, ly, 0);
174}
175
176double QPMath::GeometryCalc::EllipseRotAxis(double x, double y, double lx, double ly, double theta){
177 double angle = QPMath::GeometryCalc::Angle(x,y) - theta;
178 double xp = lx * QPMath::Cos(angle);
179 double yp = ly * QPMath::Sin(angle);
180
181 return QPMath::Sqrt(QPMath::Power(xp,2) + QPMath::Power(yp,2));
182}
183
184double QPMath::GeometryCalc::Angle(double x, double y, double centx, double centy){
185 double xp = x - centx;
186 double yp = y - centy;
187
188 return QPMath::ATan2(yp,xp);
189
190}
191
192double QPMath::Sqrt(const double& x){
193 return sqrt(x);
194}
195
196double QPMath::Power(const double& x, const double& exponent){
197 return pow(x,exponent);
198}
199
200double QPMath::Sin(const double& x){
201 return sin(x);
202}
203
204double QPMath::Cos(const double& x){
205 return cos(x);
206}
207
208double QPMath::ATan(const double& x){
209 return atan(x);
210}
211
212double QPMath::ATan2(const double& y, const double& x){
213 return atan2(x, y);
214}
215
216double QPMath::Exp(const double& x){
217 return exp(x);
218}
219
220double QPMath::Gaus(const double& x, const double& mean, const double& sigma, const bool& norm)
221{
222 if (sigma == 0) return 1.e30;
223 double arg = (x-mean)/sigma;
224 // for |arg| > 39 result is zero in double precision
225 if (arg < -39.0 || arg > 39.0) return 0.0;
226 double res = QPMath::Exp(-0.5*arg*arg);
227 if (!norm) return res;
228 return res/(2.50662827463100024*sigma); //sqrt(2*Pi)=2.50662827463100024
229}
230
231
233 QPMatrix ans = QPMatrix(2,2,4,QPMath::Cos(theta), -QPMath::Sin(theta), QPMath::Sin(theta), QPMath::Cos(theta));
234 return ans;
235}
Dense m×n matrix of double with hand-rolled linear algebra.
Definition QPMatrix.hpp:15
QPMatrix Transpose()
Definition QPMatrix.cpp:227
QPMatrix Inverse()
Inverse by the adjugate formula .
Definition QPMatrix.cpp:200
double Det()
Determinant by recursive Laplace expansion along the first row.
Definition QPMatrix.cpp:146
3-component double vector with hand-rolled operations.
Definition QPVector.hpp:13
double MatrixForm2D(QPVector &x, QPVector &x0, QPMatrix &S)
2D Gaussian in covariance-matrix form,
Definition QPMath.cpp:56
double Simple2D(double x, double y, double integral=1., double meanx=0, double meany=0, double sigma=1.)
Isotropic 2D Gaussian .
Definition QPMath.cpp:18
double RotAxis2D(double x, double y, double integral=1., double meanx=0., double meany=0., double sigmax=1., double sigmay=1., double rotation=0.)
Elliptic 2D Gaussian with principal axes rotated by rotation: the offsets are first turned into the e...
Definition QPMath.cpp:41
double Assymetry2D(double x, double y, double integral=1., double meanx=0., double meany=0., double sigmax=1., double sigmay=1., double rotation=0., double length=0., double tilt=0., double dispersion=1.)
Gaussian of an inclined track with depth-dependent widening.
Definition QPMath.cpp:100
double Simple1D(double x, double integral=1., double mean=0., double sigma=1.)
1D Gaussian .
Definition QPMath.cpp:10
double UniAxis2D(double x, double y, double integral=1., double meanx=0., double meany=0., double sigmax=1., double sigmay=1.)
Axis-aligned elliptic 2D Gaussian .
Definition QPMath.cpp:29
double Angle(double x, double y, double centx=0., double centy=0.)
Quadrant-aware angle of the point (x, y) about (centx, centy).
Definition QPMath.cpp:184
double EllipseRotAxis(double x, double y, double lx, double ly, double theta)
Radius of the ellipse with semi-axes rotated by , evaluated in the direction of (x,...
Definition QPMath.cpp:176
double EllipseUniAxis(double x, double y, double lx, double ly)
EllipseRotAxis with : radius of the axis-aligned ellipse with semi-axes in the direction of (x,...
Definition QPMath.cpp:172
double Simple1D(double x, double maximum=1.0, double mean=0.0, double a=1.0, double radius=1.0)
1D Woods-Saxon profile .
Definition QPMath.cpp:134
double Simple2D(double x, double y, double maximum=1.0, double meanx=0.0, double meany=0.0, double a=1.0, double radius=1.0)
Radial 2D Woods-Saxon profile with .
Definition QPMath.cpp:140
double RotAxis2D(double x, double y, double maximum=1.0, double meanx=0.0, double meany=0.0, double ax=1.0, double ay=1.0, double radiusX=1.0, double radiusY=1.0, double rotation=0.0)
Elliptic 2D Woods-Saxon profile with both ellipses rotated by rotation (via GeometryCalc::EllipseRotA...
Definition QPMath.cpp:154
double RotAxis2DUnsync(double x, double y, double maximum=1.0, double meanx=0.0, double meany=0.0, double ax=1.0, double ay=1.0, double radiusX=1.0, double radiusY=1.0, double rotationA=0.0, double rotationR=0.0)
As RotAxis2D, but the diffuseness ellipse and the radius ellipse rotate independently.
Definition QPMath.cpp:163
double UniAxis2D(double x, double y, double maximum=1.0, double meanx=0.0, double meany=0.0, double ax=1.0, double ay=1.0, double radiusX=1.0, double radiusY=1.0)
Elliptic 2D Woods-Saxon profile with axis-aligned axes: where is the distance to the center and bot...
Definition QPMath.cpp:146
double Power(const double &x, const double &exponent)
std::pow wrapper.
Definition QPMath.cpp:196
double Exp(const double &x)
std::exp wrapper.
Definition QPMath.cpp:216
double Cos(const double &x)
std::cos wrapper.
Definition QPMath.cpp:204
double Sin(const double &x)
std::sin wrapper.
Definition QPMath.cpp:200
double ATan(const double &x)
std::atan wrapper.
Definition QPMath.cpp:208
double Sqrt(const double &x)
std::sqrt wrapper.
Definition QPMath.cpp:192
double Gaus(const double &x, const double &mean, const double &sigma, const bool &norm)
Gaussian kernel , divided by when norm is true.
Definition QPMath.cpp:220
double ATan2(const double &y, const double &x)
Quadrant-aware arctangent with swapped argument forwarding.
Definition QPMath.cpp:212
const double InvSqrt2Pi
Definition QPMath.hpp:14
const double InvPi
Definition QPMath.hpp:12
QPMatrix RotationMatrix(double theta)
Counterclockwise 2D rotation matrix.
Definition QPMath.cpp:232