| 1 | // polfitclip.cc
|
|---|
| 2 | // Eric Aubourg CEA/DAPNIA/SPP novembre 1999
|
|---|
| 3 |
|
|---|
| 4 | #include <iostream.h>
|
|---|
| 5 |
|
|---|
| 6 | #ifndef ANSI
|
|---|
| 7 | #define ANSI
|
|---|
| 8 | #endif
|
|---|
| 9 |
|
|---|
| 10 | #include <math.h>
|
|---|
| 11 | #include <string>
|
|---|
| 12 | #include <algorithm>
|
|---|
| 13 |
|
|---|
| 14 | extern "C" {
|
|---|
| 15 | #include "nrutil.h"
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | #include "polfitclip.h"
|
|---|
| 19 |
|
|---|
| 20 | PolFitClip::PolFitClip(long ndatamax, long degpol) {
|
|---|
| 21 | this->ndata = 0;
|
|---|
| 22 | this->ndatamax = ndatamax;
|
|---|
| 23 | this->nparm = degpol+1;
|
|---|
| 24 |
|
|---|
| 25 | x = ::dvector(1, ndatamax);
|
|---|
| 26 | y = ::dvector(1, ndatamax);
|
|---|
| 27 | ySort= ::dvector(1, ndatamax);
|
|---|
| 28 | sig = ::dvector(1, ndatamax);
|
|---|
| 29 | a = ::dvector(1, nparm);
|
|---|
| 30 | ia = ::ivector(1, nparm);
|
|---|
| 31 | cov = ::matrix(1, nparm, 1, nparm);
|
|---|
| 32 |
|
|---|
| 33 | for (int i=1; i<=ndatamax; i++) sig[i] = 1;
|
|---|
| 34 |
|
|---|
| 35 | for (int i=1; i<=nparm; i++) ia[i] = 1;
|
|---|
| 36 |
|
|---|
| 37 | frc = 0;
|
|---|
| 38 | nsig = 3;
|
|---|
| 39 | npass = 3;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | PolFitClip::~PolFitClip() {
|
|---|
| 43 | free_vector(x, 1, ndatamax);
|
|---|
| 44 | free_vector(y, 1, ndatamax);
|
|---|
| 45 | free_vector(ySort, 1, ndatamax);
|
|---|
| 46 | free_vector(sig, 1, ndatamax);
|
|---|
| 47 | free_vector(a, 1, nparm);
|
|---|
| 48 | free_ivector(ia, 1, ndatamax);
|
|---|
| 49 | free_matrix(cov, 1, nparm, 1, nparm);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | void PolFitClip::setData(double const* xx, double const* yy, long n) {
|
|---|
| 53 | if (n>ndatamax) {
|
|---|
| 54 | cerr << "PolFitClip::setData : n > ndatamax " << n << " " << ndatamax << endl;
|
|---|
| 55 | exit(-1);
|
|---|
| 56 | }
|
|---|
| 57 | ndata = n;
|
|---|
| 58 | for (int i=1; i<=n; i++) {
|
|---|
| 59 | x[i] = xx[i-1];
|
|---|
| 60 | y[i] = yy[i-1];
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | void PolFitClip::addData(double xx, double yy) {
|
|---|
| 65 | ndata++;
|
|---|
| 66 | if (ndata>ndatamax) {
|
|---|
| 67 | cerr << "PolFitClip::addData : n > ndatamax " << ndata << " " << ndatamax << endl;
|
|---|
| 68 | exit(-1);
|
|---|
| 69 | }
|
|---|
| 70 | x[ndata] = xx;
|
|---|
| 71 | y[ndata] = yy;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | void PolFitClip::clear() {
|
|---|
| 75 | this->ndata = 0;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | void PolFitClip::setClip(double frc, double nsig, int npass) {
|
|---|
| 79 | this->frc = frc;
|
|---|
| 80 | this->nsig = nsig;
|
|---|
| 81 | this->npass = npass;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | extern "C" void PolFitClip_polfunc(double x, double afunc[], int ma);
|
|---|
| 85 |
|
|---|
| 86 | void PolFitClip_polfunc(double x, double afunc[], int ma) {
|
|---|
| 87 | afunc[1] = 1;
|
|---|
| 88 | for (int i=2; i<=ma; i++)
|
|---|
| 89 | afunc[i] = afunc[i-1]*x;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | extern "C"
|
|---|
| 93 | void lfit(double x[], double y[], double sig[], int ndat, double a[], int ia[],
|
|---|
| 94 | int ma, double **covar, double *chisq, void (*funcs)(double, double [], int));
|
|---|
| 95 |
|
|---|
| 96 | int PolFitClip::doFit1() {
|
|---|
| 97 | if (ndata < nparm) return -1;
|
|---|
| 98 |
|
|---|
| 99 | try {
|
|---|
| 100 | lfit(x, y, sig, ndata, a, ia, nparm, cov, &chi2, PolFitClip_polfunc);
|
|---|
| 101 | } catch (string st) {
|
|---|
| 102 | return -2;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | return 0;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | int PolFitClip::doFit(double* coef) {
|
|---|
| 109 | if (ndata < nparm) return -1;
|
|---|
| 110 |
|
|---|
| 111 | memcpy(ySort, y, (ndata+1)*sizeof(double));
|
|---|
| 112 | sort(ySort+1, ySort+ndata+1);
|
|---|
| 113 | int i = frc*ndata;
|
|---|
| 114 | double cutMin = ySort[i+1];
|
|---|
| 115 | i = ndata-i;
|
|---|
| 116 | double cutMax = ySort[i];
|
|---|
| 117 |
|
|---|
| 118 | sigma = 0;
|
|---|
| 119 |
|
|---|
| 120 | for (int ipass = 0; ipass<npass; ipass++) {
|
|---|
| 121 | for (int i=1; i<=ndata; i++) {
|
|---|
| 122 | if (y[i] >= cutMin && y[i] <= cutMax)
|
|---|
| 123 | sig[i]=1;
|
|---|
| 124 | else
|
|---|
| 125 | sig[i]=9.e99;
|
|---|
| 126 | }
|
|---|
| 127 | double s=0, s2=0;
|
|---|
| 128 | int n=0;
|
|---|
| 129 | for (int i=1; i<=ndata; i++) {
|
|---|
| 130 | if (sig[i]>=10) continue;
|
|---|
| 131 | double yy = ipass ? value(x[i]) : 0;
|
|---|
| 132 | if (ipass>0) {
|
|---|
| 133 | if (fabs(yy-y[i]) > sigma*nsig) {
|
|---|
| 134 | sig[i]=9.e99;
|
|---|
| 135 | continue;
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | s += yy-y[i];
|
|---|
| 139 | s2 += (yy-y[i])*(yy-y[i]);
|
|---|
| 140 | n++;
|
|---|
| 141 | }
|
|---|
| 142 | if (n<1) return -3;
|
|---|
| 143 | s/=n;
|
|---|
| 144 | s2/=n;
|
|---|
| 145 | sigma = s2 - s*s;
|
|---|
| 146 | sigma = (sigma>0) ? sqrt(sigma) : 0;
|
|---|
| 147 |
|
|---|
| 148 | int rc = doFit1();
|
|---|
| 149 | if (rc) return rc;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | if (coef)
|
|---|
| 153 | for (int i=1; i<=nparm; i++) {
|
|---|
| 154 | coef[i-1] = a[i];
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | return 0;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | double PolFitClip::value(double x) {
|
|---|
| 162 | double r = a[nparm];
|
|---|
| 163 | for (int i=nparm-1; i>0; i--) {
|
|---|
| 164 | r = r*x+a[i];
|
|---|
| 165 | }
|
|---|
| 166 | return r;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 | long PolFitClip::getNData() {
|
|---|
| 171 | return ndata;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | double PolFitClip::getXMin() {
|
|---|
| 175 | double xm = 9.e99;
|
|---|
| 176 | for (int i=1; i<=ndata; i++)
|
|---|
| 177 | if (x[i]<xm) xm=x[i];
|
|---|
| 178 | return xm;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | double PolFitClip::getXMax() {
|
|---|
| 182 | double xm = -9.e99;
|
|---|
| 183 | for (int i=1; i<=ndata; i++)
|
|---|
| 184 | if (x[i]>xm) xm=x[i];
|
|---|
| 185 | return xm;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | double PolFitClip::getChi2() {
|
|---|
| 189 | return chi2;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | double PolFitClip::getSigma() {
|
|---|
| 193 | return sigma;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | double PolFitClip::getCov(int i, int j) {
|
|---|
| 197 | return cov[i+1][j+1];
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 | /************************************************/
|
|---|
| 202 |
|
|---|
| 203 | PolFitClip2::PolFitClip2(long ndatamax, long degpol) {
|
|---|
| 204 | this->ndata = 0;
|
|---|
| 205 | this->ndatamax = ndatamax;
|
|---|
| 206 | this->nparm = degpol+1;
|
|---|
| 207 |
|
|---|
| 208 | x = ::dvector(1, ndatamax);
|
|---|
| 209 | y = ::dvector(1, ndatamax);
|
|---|
| 210 | ySort= ::dvector(1, ndatamax);
|
|---|
| 211 | z = ::dvector(1, ndatamax);
|
|---|
| 212 | zSort= ::dvector(1, ndatamax);
|
|---|
| 213 | sig = ::dvector(1, ndatamax);
|
|---|
| 214 | aY = ::dvector(1, nparm);
|
|---|
| 215 | aZ = ::dvector(1, nparm);
|
|---|
| 216 | ia = ::ivector(1, nparm);
|
|---|
| 217 | covY = ::matrix(1, nparm, 1, nparm);
|
|---|
| 218 | covZ = ::matrix(1, nparm, 1, nparm);
|
|---|
| 219 |
|
|---|
| 220 | for (int i=1; i<=ndatamax; i++) sig[i] = 1;
|
|---|
| 221 |
|
|---|
| 222 | for (int i=1; i<=nparm; i++) ia[i] = 1;
|
|---|
| 223 |
|
|---|
| 224 | frcY = 0;
|
|---|
| 225 | frcZ = 0;
|
|---|
| 226 | nsig = 3;
|
|---|
| 227 | npass = 3;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | PolFitClip2::~PolFitClip2() {
|
|---|
| 231 | free_vector(x, 1, ndatamax);
|
|---|
| 232 | free_vector(y, 1, ndatamax);
|
|---|
| 233 | free_vector(ySort, 1, ndatamax);
|
|---|
| 234 | free_vector(z, 1, ndatamax);
|
|---|
| 235 | free_vector(zSort, 1, ndatamax);
|
|---|
| 236 | free_vector(sig, 1, ndatamax);
|
|---|
| 237 | free_vector(aY, 1, nparm);
|
|---|
| 238 | free_vector(aZ, 1, nparm);
|
|---|
| 239 | free_ivector(ia, 1, ndatamax);
|
|---|
| 240 | free_matrix(covY, 1, nparm, 1, nparm);
|
|---|
| 241 | free_matrix(covZ, 1, nparm, 1, nparm);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | void PolFitClip2::setData(double const* xx, double const* yy, double const* zz, long n) {
|
|---|
| 245 | if (n>ndatamax) {
|
|---|
| 246 | cerr << "PolFitClip2::setData : n > ndatamax " << n << " " << ndatamax << endl;
|
|---|
| 247 | exit(-1);
|
|---|
| 248 | }
|
|---|
| 249 | ndata = n;
|
|---|
| 250 | for (int i=1; i<=n; i++) {
|
|---|
| 251 | x[i] = xx[i-1];
|
|---|
| 252 | y[i] = yy[i-1];
|
|---|
| 253 | z[i] = zz[i-1];
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | void PolFitClip2::addData(double xx, double yy, double zz) {
|
|---|
| 258 | ndata++;
|
|---|
| 259 | if (ndata>ndatamax) {
|
|---|
| 260 | cerr << "PolFitClip2::addData : n > ndatamax " << ndata << " " << ndatamax << endl;
|
|---|
| 261 | exit(-1);
|
|---|
| 262 | }
|
|---|
| 263 | x[ndata] = xx;
|
|---|
| 264 | y[ndata] = yy;
|
|---|
| 265 | z[ndata] = zz;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | void PolFitClip2::clear() {
|
|---|
| 269 | this->ndata = 0;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | void PolFitClip2::setClip(double frcY, double frcZ, double nsig, int npass) {
|
|---|
| 273 | this->frcY = frcY;
|
|---|
| 274 | this->frcZ = frcZ;
|
|---|
| 275 | this->nsig = nsig;
|
|---|
| 276 | this->npass = npass;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 | int PolFitClip2::doFit1() {
|
|---|
| 281 | if (ndata < nparm) return -1;
|
|---|
| 282 |
|
|---|
| 283 | try {
|
|---|
| 284 | lfit(x, y, sig, ndata, aY, ia, nparm, covY, &chi2Y, PolFitClip_polfunc);
|
|---|
| 285 | lfit(x, z, sig, ndata, aZ, ia, nparm, covZ, &chi2Z, PolFitClip_polfunc);
|
|---|
| 286 | } catch (string st) {
|
|---|
| 287 | return -2;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | return 0;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | int PolFitClip2::doFit(double* coefY, double* coefZ) {
|
|---|
| 294 | if (ndata < nparm) return -1;
|
|---|
| 295 |
|
|---|
| 296 | memcpy(ySort, y, (ndata+1)*sizeof(double));
|
|---|
| 297 | sort(ySort+1, ySort+ndata+1);
|
|---|
| 298 | memcpy(zSort, z, (ndata+1)*sizeof(double));
|
|---|
| 299 | sort(zSort+1, zSort+ndata+1);
|
|---|
| 300 |
|
|---|
| 301 | int i = frcY*ndata;
|
|---|
| 302 | double cutMinY = ySort[i+1];
|
|---|
| 303 | i = ndata-i;
|
|---|
| 304 | double cutMaxY = ySort[i];
|
|---|
| 305 |
|
|---|
| 306 | i = frcZ*ndata;
|
|---|
| 307 | double cutMinZ = zSort[i+1];
|
|---|
| 308 | i = ndata-i;
|
|---|
| 309 | double cutMaxZ = zSort[i];
|
|---|
| 310 |
|
|---|
| 311 | sigmaY = 0;
|
|---|
| 312 | sigmaZ = 0;
|
|---|
| 313 |
|
|---|
| 314 | for (int ipass = 0; ipass<=npass; ipass++) {
|
|---|
| 315 | for (int i=1; i<=ndata; i++) {
|
|---|
| 316 | if (y[i] >= cutMinY && y[i] <= cutMaxY &&
|
|---|
| 317 | z[i] >= cutMinZ && z[i] <= cutMaxZ)
|
|---|
| 318 | sig[i]=1;
|
|---|
| 319 | else
|
|---|
| 320 | sig[i]=9.e99;
|
|---|
| 321 | }
|
|---|
| 322 | double sy=0, sy2=0, sz=0, sz2=0;
|
|---|
| 323 | ndataused=0;
|
|---|
| 324 | for (int i=1; i<=ndata; i++) {
|
|---|
| 325 | if (sig[i]>=10) continue;
|
|---|
| 326 | double yy = ipass ? valueY(x[i]) : 0;
|
|---|
| 327 | double zz = ipass ? valueZ(x[i]) : 0;
|
|---|
| 328 | if (ipass>0 && ipass < npass) {
|
|---|
| 329 | if (fabs(yy-y[i]) > sigmaY*nsig ||
|
|---|
| 330 | fabs(zz-z[i]) > sigmaZ*nsig) {
|
|---|
| 331 | sig[i]=9.e99;
|
|---|
| 332 | continue;
|
|---|
| 333 | }
|
|---|
| 334 | }
|
|---|
| 335 | sy += yy-y[i];
|
|---|
| 336 | sy2 += (yy-y[i])*(yy-y[i]);
|
|---|
| 337 | sz += zz-z[i];
|
|---|
| 338 | sz2 += (zz-z[i])*(zz-z[i]);
|
|---|
| 339 | ndataused++;
|
|---|
| 340 | }
|
|---|
| 341 | if (ndataused<nparm) return -3;
|
|---|
| 342 | sy/=ndataused;
|
|---|
| 343 | sy2/=ndataused;
|
|---|
| 344 | sigmaY = sy2 - sy*sy;
|
|---|
| 345 | sigmaY = (sigmaY>0) ? sqrt(sigmaY) : 0;
|
|---|
| 346 | sz/=ndataused;
|
|---|
| 347 | sz2/=ndataused;
|
|---|
| 348 | sigmaZ = sz2 - sz*sz;
|
|---|
| 349 | sigmaZ = (sigmaZ>0) ? sqrt(sigmaZ) : 0;
|
|---|
| 350 | if (ipass == npass) break;
|
|---|
| 351 |
|
|---|
| 352 | int rc = doFit1();
|
|---|
| 353 | if (rc) return rc;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | if (coefY)
|
|---|
| 357 | for (int i=1; i<=nparm; i++) {
|
|---|
| 358 | coefY[i-1] = aY[i];
|
|---|
| 359 | }
|
|---|
| 360 | if (coefZ)
|
|---|
| 361 | for (int i=1; i<=nparm; i++) {
|
|---|
| 362 | coefZ[i-1] = aZ[i];
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | return 0;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 | double PolFitClip2::valueY(double x) {
|
|---|
| 370 | double r = aY[nparm];
|
|---|
| 371 | for (int i=nparm-1; i>0; i--) {
|
|---|
| 372 | r = r*x+aY[i];
|
|---|
| 373 | }
|
|---|
| 374 | return r;
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | double PolFitClip2::valueZ(double x) {
|
|---|
| 378 | double r = aZ[nparm];
|
|---|
| 379 | for (int i=nparm-1; i>0; i--) {
|
|---|
| 380 | r = r*x+aZ[i];
|
|---|
| 381 | }
|
|---|
| 382 | return r;
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 | long PolFitClip2::getNData() {
|
|---|
| 387 | return ndata;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | long PolFitClip2::getNDataUsed() {
|
|---|
| 391 | return ndataused;
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | double PolFitClip2::getXMin() {
|
|---|
| 395 | double xm = 9.e99;
|
|---|
| 396 | for (int i=1; i<=ndata; i++)
|
|---|
| 397 | if (x[i]<xm) xm=x[i];
|
|---|
| 398 | return xm;
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | double PolFitClip2::getXMax() {
|
|---|
| 402 | double xm = -9.e99;
|
|---|
| 403 | for (int i=1; i<=ndata; i++)
|
|---|
| 404 | if (x[i]>xm) xm=x[i];
|
|---|
| 405 | return xm;
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | double PolFitClip2::getChi2Y() {
|
|---|
| 409 | return chi2Y;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | double PolFitClip2::getChi2Z() {
|
|---|
| 413 | return chi2Z;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | double PolFitClip2::getSigmaY() {
|
|---|
| 417 | return sigmaY;
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | double PolFitClip2::getSigmaZ() {
|
|---|
| 421 | return sigmaZ;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | double PolFitClip2::getCovY(int i, int j) {
|
|---|
| 425 | return covY[i+1][j+1];
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | double PolFitClip2::getCovZ(int i, int j) {
|
|---|
| 429 | return covZ[i+1][j+1];
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|