21using CLHEP::HepVector;
22using CLHEP::Hep3Vector;
23using CLHEP::HepMatrix;
24using CLHEP::HepSymMatrix;
35static double err_dis_inv(
double x,
double y,
double w,
double a,
double b) {
39 double f =
x * b - y * a;
89 m_wsum_temp = m_wsum + wi;
90 double rri(xi * xi + yi * yi);
91 double wrri(wi * rri);
92 double wsum_inv(1/m_wsum_temp);
93 m_xav = (m_xsum + wi * xi) * wsum_inv;
94 m_yav = (m_ysum + wi * yi) * wsum_inv;
96 double xxav((m_xxsum + wi * xi * xi) * wsum_inv);
97 double yyav((m_yysum + wi * yi * yi) * wsum_inv);
98 double xyav((m_xysum + wi * xi * yi) * wsum_inv);
99 double xrrav((m_xrrsum + xi * wrri) * wsum_inv);
100 double yrrav((m_yrrsum + yi * wrri) * wsum_inv);
101 double rrrrav((m_rrrrsum + wrri * rri) * wsum_inv);
103 calculate_average_n(xxav, yyav, xyav, xrrav, yrrav, rrrrav);
108 if(m_wsum<=0)
return;
109 m_wsum_temp = m_wsum;
110 double wsum_inv(1/m_wsum_temp);
111 m_xav = m_xsum * wsum_inv;
112 m_yav = m_ysum * wsum_inv;
114 double xxav(m_xxsum * wsum_inv);
115 double yyav(m_yysum * wsum_inv);
116 double xyav(m_xysum * wsum_inv);
117 double xrrav(m_xrrsum * wsum_inv);
118 double yrrav(m_yrrsum * wsum_inv);
119 double rrrrav(m_rrrrsum * wsum_inv);
121 calculate_average_n(xxav, yyav, xyav, xrrav, yrrav, rrrrav);
124void Lpav::calculate_average_n(
double xxav,
double yyav,
double xyav,
125 double xrrav,
double yrrav,
double rrrrav) {
126 double xxav_p = xxav - m_xav * m_xav;
127 double yyav_p = yyav - m_yav * m_yav;
128 double xyav_p = xyav - m_xav * m_yav;
129 double rrav_p = xxav_p + yyav_p;
131 double a = std::fabs(xxav_p - yyav_p);
132 double b = 4 * xyav_p * xyav_p;
133 double asqpb = a * a + b;
134 double rasqpb = std::sqrt(asqpb);
135 double splus = 1 + a / rasqpb;
136 double sminus = b / (asqpb*splus);
137 splus = std::sqrt(0.5*splus);
138 sminus = std::sqrt(0.5*sminus);
142 if ( xxav_p <= yyav_p ) {
152 if (xyav_p < 0) m_sinrot = - m_sinrot;
162 if ( m_cosrot*m_xav + m_sinrot*m_yav <= 0 ) {
163 m_cosrot = - m_cosrot;
164 m_sinrot = - m_sinrot;
166 m_rscale = std::sqrt(rrav_p);
167 double cos2 = m_cosrot * m_cosrot;
168 double sin2 = m_sinrot * m_sinrot;
169 double cs2 = 2 * m_sinrot * m_cosrot;
170 double rrav_p_inv(1/rrav_p);
171 m_xxavp = (cos2 * xxav_p + cs2 * xyav_p + sin2 * yyav_p) * rrav_p_inv;
172 m_yyavp = (cos2 * yyav_p - cs2 * xyav_p + sin2 * xxav_p) * rrav_p_inv;
174 double xav2 = m_xav * m_xav;
175 double yav2 = m_yav * m_yav;
176 double xrrav_p = (xrrav - 2 * xxav * m_xav + xav2 * m_xav -
177 2 * xyav * m_yav + m_xav * yav2) - m_xav * rrav_p;
178 double yrrav_p = (yrrav - 2 * yyav * m_yav + yav2 * m_yav -
179 2 * xyav * m_xav + m_yav * xav2) - m_yav * rrav_p;
180 m_xrravp = ( m_cosrot * xrrav_p + m_sinrot * yrrav_p) * rrav_p_inv/m_rscale;
181 m_yrravp = (- m_sinrot * xrrav_p + m_cosrot * yrrav_p) * rrav_p_inv/m_rscale;
183 double rrav = xxav + yyav;
184 double rrrrav_p = rrrrav
185 - 2 * m_yav * yrrav - 2 * m_xav * xrrav
186 + rrav * (xav2 + yav2)
187 - 2 * m_xav * xrrav_p - xav2 * rrav_p
188 - 2 * m_yav * yrrav_p - yav2 * rrav_p;
189 m_rrrravp = rrrrav_p * rrav_p_inv * rrav_p_inv;
194 if(m_wsum<=0)
return;
195 m_wsum_temp = m_wsum + wi;
196 double wsum_inv(1/m_wsum_temp);
197 double rri(xi * xi + yi * yi);
198 m_xav = (m_xsum + wi * xi) * wsum_inv;
199 m_yav = (m_ysum + wi * yi) * wsum_inv;
204 m_xxavp = (m_xxsum + wi * xi * xi) * wsum_inv;
205 m_xyavp = (m_xysum + wi * xi * yi) * wsum_inv;
206 m_yyavp = (m_yysum + wi * yi * yi) * wsum_inv;
207 double wrri(wi * rri);
208 m_xrravp = (m_xrrsum + xi * wrri) * wsum_inv;
209 m_yrravp = (m_yrrsum + yi * wrri) * wsum_inv;
210 m_rrrravp = (m_rrrrsum + rri * wrri) * wsum_inv;
214 if(m_wsum<=0)
return;
215 m_wsum_temp = m_wsum;
216 double wsum_inv(1/m_wsum_temp);
217 m_xav = m_xsum * wsum_inv;
218 m_yav = m_ysum * wsum_inv;
223 m_xxavp = m_xxsum * wsum_inv;
224 m_xyavp = m_xysum * wsum_inv;
225 m_yyavp = m_yysum * wsum_inv;
226 m_xrravp = m_xrrsum * wsum_inv;
227 m_yrravp = m_yrrsum * wsum_inv;
228 m_rrrravp = m_rrrrsum * wsum_inv;
253 o <<
" nc=" << a.m_nc <<
" chisq=" << a.m_chisq <<
" " << (
Lpar&) a;
257double Lpav::solve_lambda(
void) {
258 if (m_rscale<=0)
return -1;
259 double xrrxrr = m_xrravp * m_xrravp;
260 double yrryrr = m_yrravp * m_yrravp;
261 double rrrrm1 = m_rrrravp - 1;
262 double xxyy = m_xxavp * m_yyavp;
264 double c0 = rrrrm1 * xxyy - xrrxrr * m_yyavp - yrryrr * m_xxavp;
265 double c1 = - rrrrm1 + xrrxrr + yrryrr - 4 * xxyy;
266 double c2 = 4 + rrrrm1 - 4 * xxyy;
276 double chiscl = m_wsum_temp * m_rscale * m_rscale;
277 double dlamax = 0.001 / chiscl;
280 double dlambda = dlamax;
281 while ( itry<ntry && std::fabs(dlambda) >= dlamax) {
282 double cpoly = c0 + lambda * (
c1 + lambda *
283 ( c2 + lambda * lambda * c4));
284 double dcpoly =
c1 + lambda * ( c2d + lambda * lambda * c4d);
285 dlambda = - cpoly / dcpoly;
289 lambda = lambda<0 ? 0 : lambda;
293double Lpav::solve_lambda3(
void) {
294 if (m_rscale<=0)
return -1;
295 double xrrxrr = m_xrravp * m_xrravp;
296 double yrryrr = m_yrravp * m_yrravp;
297 double rrrrm1 = m_rrrravp - 1;
298 double xxyy = m_xxavp * m_yyavp;
300 double a = m_rrrravp;
301 double b = xrrxrr + yrryrr - m_rrrravp * (m_xxavp + m_yyavp);
302 double c = m_rrrravp * m_xxavp * m_yyavp
303 - m_yyavp * xrrxrr - m_xxavp * yrryrr
304 + 2 * m_xyavp * m_xrravp * m_yrravp - m_rrrravp * m_xyavp * m_xyavp;
306 return (-b-std::sqrt(b*b-4*a*c))/2/a;
307 }
else if (c>=0 && b>0) {
308 std::cerr <<
" returning " <<-1<<std::endl;
311 return (-b+std::sqrt(b*b-4*a*c))/2/a;
317 double lambda = solve_lambda();
320 if (lambda<0)
return -1;
321 double h11 = m_xxavp - lambda;
322 double h22 = m_yyavp - lambda;
323 if (h11==0.0)
return -1;
324 double h14 = m_xrravp;
325 double h24 = m_yrravp;
326 double h34 = 1 + 2 * lambda;
327 double rootsq = (h14*h14/h11/h11) + 4 * h34;
328 if ( std::fabs(h22) > std::fabs(h24) ) {
329 if(h22==0.0)
return -1;
330 double ratio = h24/h22;
331 rootsq += ratio * ratio ;
332 m_kappa = 1/std::sqrt(rootsq);
333 m_beta = - ratio * m_kappa;
335 if(h24==0.0)
return -1;
336 double ratio = h22 / h24;
337 rootsq = 1 + ratio * ratio * rootsq;
338 m_beta = 1 / std::sqrt(rootsq);
339 m_beta = h24>0 ? -m_beta : m_beta;
340 m_kappa = -ratio * m_beta;
342 m_alpha = - (h14/h11)*m_kappa;
343 m_gamma = - h34 * m_kappa;
359 rotate(m_cosrot, -m_sinrot);
363 move(-m_xav, -m_yav);
364 if (m_yrravp < 0)
neg();
365 if (lambda>=0) m_chisq = lambda * m_wsum_temp * m_rscale * m_rscale;
370 double lambda = solve_lambda3();
373 if (lambda<0)
return -1;
374 double h11 = m_xxavp - lambda;
375 double h22 = m_yyavp - lambda;
376 double h14 = m_xrravp;
377 double h24 = m_yrravp;
379 double h12 = m_xyavp;
380 double det = h11*h22-h12*h12;
382 double r1 = (h14*h22-h24*h12)/(det);
383 double r2 = (h24*h11-h14*h12)/(det);
384 double kinvsq = r1*r1 + r2*r2;
385 m_kappa = std::sqrt(1/kinvsq);
386 if(h11!=0) m_alpha = -m_kappa * r1;
388 if(h22!=0) m_beta = -m_kappa * r2;
392 if (h11!=0 && h22!=0) {
393 m_beta = 1/std::sqrt(1+h12*h12/h11/h11);
394 m_alpha = std::sqrt(1-m_beta*m_beta);
403 if((m_alpha*m_xav + m_beta*m_yav) *
404 (m_beta*m_xav - m_alpha*m_yav)<0)
neg();
408 if (lambda>=0) m_chisq = lambda * m_wsum_temp * m_rscale * m_rscale;
413 if (m_nc<=3)
return -1;
419 if (
q>0) m_chisq =
q * m_wsum_temp * m_rscale * m_rscale;
423 if (
q>0) m_chisq =
q * m_wsum_temp * m_rscale * m_rscale;
429 if (m_nc<=3)
return -1;
435 if (
q>0) m_chisq =
q * m_wsum_temp * m_rscale * m_rscale;
439 if (
q>0) m_chisq =
q * m_wsum_temp * m_rscale * m_rscale;
445#ifdef BELLE_OPTIMIZED_RETURN
450 HepSymMatrix vret(4);
458 vret(4,1) = m_xrrsum;
459 vret(4,2) = m_yrrsum;
460 vret(4,3) = m_xxsum + m_yysum;
461 vret(4,4) = m_rrrrsum;
467 std::cerr <<
"Lpav::cov:could not invert nc=" << m_nc << vret;
477#ifdef BELLE_OPTIMIZED_RETURN
482 HepSymMatrix vret(3);
487 vret = cov(1).similarity(dldc());
491 THROW(Lpav::cov_c1,Singular_c);
499 std::cerr <<
"Lpav::cov_c:could not invert " << vret;
501 THROW(Lpav::cov_c2,Singular_c);
510 if (m_chisq<0)
return -1;
511 if (xy(r,
x, y)!=0)
return -1;
512 phi = std::atan2(y,
x);
526 double l =
cov().similarity(
v);
528 double ls = std::sqrt(l);
543 if (m_nc<=3)
return -1;
548 v(4) =
x *
x + y * y;
553 l =
cov().similarity(
v);
563void Lpav::add(
double xi,
double yi,
double w,
double a,
double b) {
564 register double wi = err_dis_inv(xi, yi, w, a, b);
569 register double wi) {
573 m_xxsum += wi * xi * xi;
574 m_yysum += wi * yi * yi;
575 m_xysum += wi * xi * yi;
576 register double rri = ( xi * xi + yi * yi );
577 register double wrri = wi * rri;
578 m_xrrsum += wrri * xi;
579 m_yrrsum += wrri * yi;
580 m_rrrrsum += wrri * rri;
585 register double wi = w * a;
589 m_xxsum += wi * xi * xi;
590 m_yysum += wi * yi * yi;
591 m_xysum += wi * xi * yi;
592 register double rri = ( xi * xi + yi * yi );
593 register double wrri = wi * rri;
594 m_xrrsum += wrri * xi;
595 m_yrrsum += wrri * yi;
596 m_rrrrsum += wrri * rri;
600void Lpav::sub(
double xi,
double yi,
double w,
double a,
double b) {
601 register double wi = err_dis_inv(xi, yi, w, a, b);
605 m_xxsum -= wi * xi * xi;
606 m_yysum -= wi * yi * yi;
607 m_xysum -= wi * xi * yi;
608 register double rri = ( xi * xi + yi * yi );
609 register double wrri = wi * rri;
610 m_xrrsum -= wrri * xi;
611 m_yrrsum -= wrri * yi;
612 m_rrrrsum -= wrri * rri;
617 m_wsum += la1.m_wsum;
618 m_xsum += la1.m_xsum;
619 m_ysum += la1.m_ysum;
620 m_xxsum += la1.m_xxsum;
621 m_yysum += la1.m_yysum;
622 m_xysum += la1.m_xysum;
623 m_xrrsum += la1.m_xrrsum;
624 m_yrrsum += la1.m_yrrsum;
625 m_rrrrsum += la1.m_rrrrsum;
631#ifdef BELLE_OPTIMIZED_RETURN
638 la.m_wsum = la1.m_wsum + la2.m_wsum;
639 la.m_xsum = la1.m_xsum + la2.m_xsum;
640 la.m_ysum = la1.m_ysum + la2.m_ysum;
641 la.m_xxsum = la1.m_xxsum + la2.m_xxsum;
642 la.m_yysum = la1.m_yysum + la2.m_yysum;
643 la.m_xysum = la1.m_xysum + la2.m_xysum;
644 la.m_xrrsum = la1.m_xrrsum + la2.m_xrrsum;
645 la.m_yrrsum = la1.m_yrrsum + la2.m_yrrsum;
646 la.m_rrrrsum = la1.m_rrrrsum + la2.m_rrrrsum;
647 la.m_nc = la1.m_nc + la2.m_nc;
652 if (m_nc<=3)
return 0;
653 if (m_chisq<0)
return 0;
655 int nci = (int)m_nc - 3;
656 double p = (double)
prob_(&c, &nci);
661 if (m_nc<=3)
return -1;
662 else return m_chisq/(m_nc-3);
669 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
float prob_(float *, int *)
**********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
double d(double x, double y) const
double phi(double r, int dir=0) const
double calculate_lpar3(void)
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)
void add_point(double x, double y, double w=1)
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
Lpav operator+(const Lpav &la1, const Lpav &la2)
std::ostream & operator<<(std::ostream &o, Lpar &s)