21#include "TrackUtil/Lpav.h"
32static double err_dis_inv(
double x,
double y,
double w,
double a,
double b) {
36 double f =
x * b - y * a;
86 m_wsum_temp = m_wsum + wi;
87 double rri(xi * xi + yi * yi);
88 double wrri(wi * rri);
89 double wsum_inv(1/m_wsum_temp);
90 m_xav = (m_xsum + wi * xi) * wsum_inv;
91 m_yav = (m_ysum + wi * yi) * wsum_inv;
93 double xxav((m_xxsum + wi * xi * xi) * wsum_inv);
94 double yyav((m_yysum + wi * yi * yi) * wsum_inv);
95 double xyav((m_xysum + wi * xi * yi) * wsum_inv);
96 double xrrav((m_xrrsum + xi * wrri) * wsum_inv);
97 double yrrav((m_yrrsum + yi * wrri) * wsum_inv);
98 double rrrrav((m_rrrrsum + wrri * rri) * wsum_inv);
100 calculate_average_n(xxav, yyav, xyav, xrrav, yrrav, rrrrav);
105 if(m_wsum<=0)
return;
106 m_wsum_temp = m_wsum;
107 double wsum_inv(1/m_wsum_temp);
108 m_xav = m_xsum * wsum_inv;
109 m_yav = m_ysum * wsum_inv;
111 double xxav(m_xxsum * wsum_inv);
112 double yyav(m_yysum * wsum_inv);
113 double xyav(m_xysum * wsum_inv);
114 double xrrav(m_xrrsum * wsum_inv);
115 double yrrav(m_yrrsum * wsum_inv);
116 double rrrrav(m_rrrrsum * wsum_inv);
118 calculate_average_n(xxav, yyav, xyav, xrrav, yrrav, rrrrav);
121void Lpav::calculate_average_n(
double xxav,
double yyav,
double xyav,
122 double xrrav,
double yrrav,
double rrrrav) {
123 double xxav_p = xxav - m_xav * m_xav;
124 double yyav_p = yyav - m_yav * m_yav;
125 double xyav_p = xyav - m_xav * m_yav;
126 double rrav_p = xxav_p + yyav_p;
128 double a = std::fabs(xxav_p - yyav_p);
129 double b = 4 * xyav_p * xyav_p;
130 double asqpb = a * a + b;
131 double rasqpb = std::sqrt(asqpb);
132 double splus = 1 + a / rasqpb;
133 double sminus = b / (asqpb*splus);
134 splus = std::sqrt(0.5*splus);
135 sminus = std::sqrt(0.5*sminus);
139 if ( xxav_p <= yyav_p ) {
149 if (xyav_p < 0) m_sinrot = - m_sinrot;
159 if ( m_cosrot*m_xav + m_sinrot*m_yav <= 0 ) {
160 m_cosrot = - m_cosrot;
161 m_sinrot = - m_sinrot;
163 m_rscale = std::sqrt(rrav_p);
164 double cos2 = m_cosrot * m_cosrot;
165 double sin2 = m_sinrot * m_sinrot;
166 double cs2 = 2 * m_sinrot * m_cosrot;
167 double rrav_p_inv(1/rrav_p);
168 m_xxavp = (cos2 * xxav_p + cs2 * xyav_p + sin2 * yyav_p) * rrav_p_inv;
169 m_yyavp = (cos2 * yyav_p - cs2 * xyav_p + sin2 * xxav_p) * rrav_p_inv;
171 double xav2 = m_xav * m_xav;
172 double yav2 = m_yav * m_yav;
173 double xrrav_p = (xrrav - 2 * xxav * m_xav + xav2 * m_xav -
174 2 * xyav * m_yav + m_xav * yav2) - m_xav * rrav_p;
175 double yrrav_p = (yrrav - 2 * yyav * m_yav + yav2 * m_yav -
176 2 * xyav * m_xav + m_yav * xav2) - m_yav * rrav_p;
177 m_xrravp = ( m_cosrot * xrrav_p + m_sinrot * yrrav_p) * rrav_p_inv/m_rscale;
178 m_yrravp = (- m_sinrot * xrrav_p + m_cosrot * yrrav_p) * rrav_p_inv/m_rscale;
180 double rrav = xxav + yyav;
181 double rrrrav_p = rrrrav
182 - 2 * m_yav * yrrav - 2 * m_xav * xrrav
183 + rrav * (xav2 + yav2)
184 - 2 * m_xav * xrrav_p - xav2 * rrav_p
185 - 2 * m_yav * yrrav_p - yav2 * rrav_p;
186 m_rrrravp = rrrrav_p * rrav_p_inv * rrav_p_inv;
191 if(m_wsum<=0)
return;
192 m_wsum_temp = m_wsum + wi;
193 double wsum_inv(1/m_wsum_temp);
194 double rri(xi * xi + yi * yi);
195 m_xav = (m_xsum + wi * xi) * wsum_inv;
196 m_yav = (m_ysum + wi * yi) * wsum_inv;
201 m_xxavp = (m_xxsum + wi * xi * xi) * wsum_inv;
202 m_xyavp = (m_xysum + wi * xi * yi) * wsum_inv;
203 m_yyavp = (m_yysum + wi * yi * yi) * wsum_inv;
204 double wrri(wi * rri);
205 m_xrravp = (m_xrrsum + xi * wrri) * wsum_inv;
206 m_yrravp = (m_yrrsum + yi * wrri) * wsum_inv;
207 m_rrrravp = (m_rrrrsum + rri * wrri) * wsum_inv;
211 if(m_wsum<=0)
return;
212 m_wsum_temp = m_wsum;
213 double wsum_inv(1/m_wsum_temp);
214 m_xav = m_xsum * wsum_inv;
215 m_yav = m_ysum * wsum_inv;
220 m_xxavp = m_xxsum * wsum_inv;
221 m_xyavp = m_xysum * wsum_inv;
222 m_yyavp = m_yysum * wsum_inv;
223 m_xrravp = m_xrrsum * wsum_inv;
224 m_yrravp = m_yrrsum * wsum_inv;
225 m_rrrravp = m_rrrrsum * wsum_inv;
250 o <<
" nc=" << a.m_nc <<
" chisq=" << a.m_chisq <<
" " << (
Lpar&) a;
254double Lpav::solve_lambda(
void) {
255 if (m_rscale<=0)
return -1;
256 double xrrxrr = m_xrravp * m_xrravp;
257 double yrryrr = m_yrravp * m_yrravp;
258 double rrrrm1 = m_rrrravp - 1;
259 double xxyy = m_xxavp * m_yyavp;
261 double c0 = rrrrm1 * xxyy - xrrxrr * m_yyavp - yrryrr * m_xxavp;
262 double c1 = - rrrrm1 + xrrxrr + yrryrr - 4 * xxyy;
263 double c2 = 4 + rrrrm1 - 4 * xxyy;
273 double chiscl = m_wsum_temp * m_rscale * m_rscale;
274 double dlamax = 0.001 / chiscl;
277 double dlambda = dlamax;
278 while ( itry<ntry && std::fabs(dlambda) >= dlamax) {
279 double cpoly = c0 + lambda * ( c1 + lambda *
280 ( c2 + lambda * lambda * c4));
281 double dcpoly = c1 + lambda * ( c2d + lambda * lambda * c4d);
282 dlambda = - cpoly / dcpoly;
286 lambda = lambda<0 ? 0 : lambda;
290double Lpav::solve_lambda3(
void) {
291 if (m_rscale<=0)
return -1;
292 double xrrxrr = m_xrravp * m_xrravp;
293 double yrryrr = m_yrravp * m_yrravp;
294 double rrrrm1 = m_rrrravp - 1;
295 double xxyy = m_xxavp * m_yyavp;
297 double a = m_rrrravp;
298 double b = xrrxrr + yrryrr - m_rrrravp * (m_xxavp + m_yyavp);
299 double c = m_rrrravp * m_xxavp * m_yyavp
300 - m_yyavp * xrrxrr - m_xxavp * yrryrr
301 + 2 * m_xyavp * m_xrravp * m_yrravp - m_rrrravp * m_xyavp * m_xyavp;
303 return (-b-std::sqrt(b*b-4*a*c))/2/a;
304 }
else if (c>=0 && b>0) {
305 std::cerr <<
" returning " <<-1<<std::endl;
308 return (-b+std::sqrt(b*b-4*a*c))/2/a;
314 double lambda = solve_lambda();
317 if (lambda<0)
return -1;
318 double h11 = m_xxavp - lambda;
319 double h22 = m_yyavp - lambda;
320 if (h11==0.0)
return -1;
321 double h14 = m_xrravp;
322 double h24 = m_yrravp;
323 double h34 = 1 + 2 * lambda;
324 double rootsq = (h14*h14/h11/h11) + 4 * h34;
325 if ( std::fabs(h22) > std::fabs(h24) ) {
326 if(h22==0.0)
return -1;
327 double ratio = h24/h22;
328 rootsq += ratio * ratio ;
329 m_kappa = 1/std::sqrt(rootsq);
330 m_beta = - ratio * m_kappa;
332 if(h24==0.0)
return -1;
333 double ratio = h22 / h24;
334 rootsq = 1 + ratio * ratio * rootsq;
335 m_beta = 1 / std::sqrt(rootsq);
336 m_beta = h24>0 ? -m_beta : m_beta;
337 m_kappa = -ratio * m_beta;
339 m_alpha = - (h14/h11)*m_kappa;
340 m_gamma = - h34 * m_kappa;
356 rotate(m_cosrot, -m_sinrot);
360 move(-m_xav, -m_yav);
361 if (m_yrravp < 0)
neg();
362 if (lambda>=0) m_chisq = lambda * m_wsum_temp * m_rscale * m_rscale;
367 double lambda = solve_lambda3();
370 if (lambda<0)
return -1;
371 double h11 = m_xxavp - lambda;
372 double h22 = m_yyavp - lambda;
373 double h14 = m_xrravp;
374 double h24 = m_yrravp;
376 double h12 = m_xyavp;
377 double det = h11*h22-h12*h12;
379 double r1 = (h14*h22-h24*h12)/(det);
380 double r2 = (h24*h11-h14*h12)/(det);
381 double kinvsq = r1*r1 + r2*r2;
382 m_kappa = std::sqrt(1/kinvsq);
383 if(h11!=0) m_alpha = -m_kappa * r1;
385 if(h22!=0) m_beta = -m_kappa * r2;
389 if (h11!=0 && h22!=0) {
390 m_beta = 1/std::sqrt(1+h12*h12/h11/h11);
391 m_alpha = std::sqrt(1-m_beta*m_beta);
400 if((m_alpha*m_xav + m_beta*m_yav) *
401 (m_beta*m_xav - m_alpha*m_yav)<0)
neg();
405 if (lambda>=0) m_chisq = lambda * m_wsum_temp * m_rscale * m_rscale;
409double Lpav::fit(
double x,
double y,
double w) {
410 if (m_nc<=3)
return -1;
416 if (
q>0) m_chisq =
q * m_wsum_temp * m_rscale * m_rscale;
420 if (
q>0) m_chisq =
q * m_wsum_temp * m_rscale * m_rscale;
426 if (m_nc<=3)
return -1;
432 if (
q>0) m_chisq =
q * m_wsum_temp * m_rscale * m_rscale;
436 if (
q>0) m_chisq =
q * m_wsum_temp * m_rscale * m_rscale;
442#ifdef BELLE_OPTIMIZED_RETURN
447 HepSymMatrix vret(4);
455 vret(4,1) = m_xrrsum;
456 vret(4,2) = m_yrrsum;
457 vret(4,3) = m_xxsum + m_yysum;
458 vret(4,4) = m_rrrrsum;
464 std::cerr <<
"Lpav::cov:could not invert nc=" << m_nc << vret;
474#ifdef BELLE_OPTIMIZED_RETURN
479 HepSymMatrix vret(3);
484 vret = cov(1).similarity(dldc());
488 THROW(Lpav::cov_c1,Singular_c);
496 std::cerr <<
"Lpav::cov_c:could not invert " << vret;
498 THROW(Lpav::cov_c2,Singular_c);
507 if (m_chisq<0)
return -1;
508 if (xy(r, x, y)!=0)
return -1;
509 phi = std::atan2(y,x);
523 double l =
cov().similarity(
v);
525 double ls = std::sqrt(l);
540 if (m_nc<=3)
return -1;
545 v(4) =
x *
x + y * y;
550 l =
cov().similarity(
v);
560void Lpav::add(
double xi,
double yi,
double w,
double a,
double b) {
561 register double wi = err_dis_inv(xi, yi,
w, a, b);
566 register double wi) {
570 m_xxsum += wi * xi * xi;
571 m_yysum += wi * yi * yi;
572 m_xysum += wi * xi * yi;
573 register double rri = ( xi * xi + yi * yi );
574 register double wrri = wi * rri;
575 m_xrrsum += wrri * xi;
576 m_yrrsum += wrri * yi;
577 m_rrrrsum += wrri * rri;
582 register double wi =
w * a;
586 m_xxsum += wi * xi * xi;
587 m_yysum += wi * yi * yi;
588 m_xysum += wi * xi * yi;
589 register double rri = ( xi * xi + yi * yi );
590 register double wrri = wi * rri;
591 m_xrrsum += wrri * xi;
592 m_yrrsum += wrri * yi;
593 m_rrrrsum += wrri * rri;
597void Lpav::sub(
double xi,
double yi,
double w,
double a,
double b) {
598 register double wi = err_dis_inv(xi, yi,
w, a, b);
602 m_xxsum -= wi * xi * xi;
603 m_yysum -= wi * yi * yi;
604 m_xysum -= wi * xi * yi;
605 register double rri = ( xi * xi + yi * yi );
606 register double wrri = wi * rri;
607 m_xrrsum -= wrri * xi;
608 m_yrrsum -= wrri * yi;
609 m_rrrrsum -= wrri * rri;
614 m_wsum += la1.m_wsum;
615 m_xsum += la1.m_xsum;
616 m_ysum += la1.m_ysum;
617 m_xxsum += la1.m_xxsum;
618 m_yysum += la1.m_yysum;
619 m_xysum += la1.m_xysum;
620 m_xrrsum += la1.m_xrrsum;
621 m_yrrsum += la1.m_yrrsum;
622 m_rrrrsum += la1.m_rrrrsum;
628#ifdef BELLE_OPTIMIZED_RETURN
635 la.m_wsum = la1.m_wsum + la2.m_wsum;
636 la.m_xsum = la1.m_xsum + la2.m_xsum;
637 la.m_ysum = la1.m_ysum + la2.m_ysum;
638 la.m_xxsum = la1.m_xxsum + la2.m_xxsum;
639 la.m_yysum = la1.m_yysum + la2.m_yysum;
640 la.m_xysum = la1.m_xysum + la2.m_xysum;
641 la.m_xrrsum = la1.m_xrrsum + la2.m_xrrsum;
642 la.m_yrrsum = la1.m_yrrsum + la2.m_yrrsum;
643 la.m_rrrrsum = la1.m_rrrrsum + la2.m_rrrrsum;
644 la.m_nc = la1.m_nc + la2.m_nc;
649 if (m_nc<=3)
return 0;
650 if (m_chisq<0)
return 0;
652 int nci = (int)m_nc - 3;
653 double p = (double)
prob_(&c, &nci);
658 if (m_nc<=3)
return -1;
659 else return m_chisq/(m_nc-3);
666 double delta = std::sqrt(
d) *
w / (1 + sim *
w);
float prob_(float *, int *)
****INTEGER imax DOUBLE PRECISION m_pi *DOUBLE PRECISION m_amfin DOUBLE PRECISION m_Chfin DOUBLE PRECISION m_Xenph DOUBLE PRECISION m_sinw2 DOUBLE PRECISION m_GFermi DOUBLE PRECISION m_MfinMin DOUBLE PRECISION m_ta2 INTEGER m_out INTEGER m_KeyFSR INTEGER m_KeyQCD *COMMON c_Semalib $ !copy of input $ !CMS energy $ !beam mass $ !final mass $ !beam charge $ !final charge $ !smallest final mass $ !Z mass $ !Z width $ !EW mixing angle $ !Gmu Fermi $ alphaQED at q
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
std::ostream & operator<<(std::ostream &o, const Lpav &a)
Lpav operator+(const Lpav &la1, const Lpav &la2)
float prob_(float *, int *)
double d(double x, double y) const
double phi(double r, int dir=0) const
double calculate_lpar3(void)
void add_point(double x, double y, double w=1)
HepSymMatrix cov_c(int=0) const
void calculate_average3(void)
void calculate_average(void)
double calculate_lpar(void)
void add_point_frac(double x, double y, double w, double f)
double delta_chisq(double x, double y, double w=1) const
int extrapolate(double, double &, double &) const
const Lpav & operator+=(const Lpav &)
double similarity(double, double) const
HepSymMatrix cov(int=0) const