[3121] | 1 | #include "sopnamsp.h"
|
---|
| 2 | #include "machdefs.h"
|
---|
| 3 | #include <string.h>
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 | #include <math.h>
|
---|
| 6 | #include "perrors.h"
|
---|
| 7 | #include "fioarr.h"
|
---|
| 8 | #include "hist2err.h"
|
---|
| 9 |
|
---|
| 10 | /*!
|
---|
| 11 | \class SOPHYA::Histo2DErr
|
---|
| 12 | \ingroup HiStats
|
---|
| 13 | Classe d'histogrammes 1D avec erreurs donnees par l'utilisateur
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | /********* Methode *********/
|
---|
| 17 | /*! Constructeur par defaut */
|
---|
| 18 | Histo2DErr::Histo2DErr(void)
|
---|
| 19 | : xmin_(1.), xmax_(-1.), nx_(0), dx_(0.)
|
---|
| 20 | , ymin_(1.), ymax_(-1.), ny_(0), dy_(0.)
|
---|
[3147] | 21 | , mMean(0)
|
---|
[3121] | 22 | {
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | /********* Methode *********/
|
---|
| 26 | /*! Constructeur d'un histo */
|
---|
| 27 | Histo2DErr::Histo2DErr(r_8 xmin,r_8 xmax,int_4 nx,r_8 ymin,r_8 ymax,int_4 ny)
|
---|
| 28 | {
|
---|
| 29 | CreateOrResize(xmin,xmax,nx,ymin,ymax,ny);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /********* Methode *********/
|
---|
| 33 | /*! Constructeur par copie */
|
---|
| 34 | Histo2DErr::Histo2DErr(const Histo2DErr& H)
|
---|
[3147] | 35 | : mMean(0)
|
---|
[3121] | 36 | {
|
---|
| 37 | if(H.nx_<=0 || H.ny_<=0) return;
|
---|
| 38 | CreateOrResize(H.xmin_,H.xmax_,H.nx_,H.ymin_,H.ymax_,H.ny_);
|
---|
| 39 | data_ = H.data_;
|
---|
| 40 | err2_ = H.err2_;
|
---|
| 41 | ndata_ = H.ndata_;
|
---|
[3147] | 42 | mMean = H.mMean;
|
---|
[3121] | 43 | }
|
---|
| 44 |
|
---|
| 45 | /********* Methode *********/
|
---|
| 46 | /*! Destructeur */
|
---|
| 47 | Histo2DErr::~Histo2DErr(void)
|
---|
| 48 | {
|
---|
[3147] | 49 | mMean = 0;
|
---|
[3121] | 50 | }
|
---|
| 51 |
|
---|
| 52 | /********* Methode *********/
|
---|
| 53 | /*! Gestion de l'allocation */
|
---|
| 54 | void Histo2DErr::CreateOrResize(r_8 xmin,r_8 xmax,int_4 nx,r_8 ymin,r_8 ymax,int_4 ny)
|
---|
| 55 | {
|
---|
| 56 | xmin_ = xmin; xmax_ = xmax; nx_ = nx; dx_=0.;
|
---|
| 57 | ymin_ = ymin; ymax_ = ymax; ny_ = ny; dy_=0.;
|
---|
| 58 | if(nx_>0 && ny_>0) {
|
---|
| 59 | data_.ReSize(nx_,ny_); data_ = 0.;
|
---|
| 60 | err2_.ReSize(nx_,ny_); err2_ = 0.;
|
---|
| 61 | ndata_.ReSize(nx_,ny_); ndata_ = 0.;
|
---|
| 62 | dx_ = (xmax_-xmin_)/nx_;
|
---|
| 63 | dy_ = (ymax_-ymin_)/ny_;
|
---|
| 64 | }
|
---|
[3147] | 65 | mMean = 0;
|
---|
[3121] | 66 | }
|
---|
| 67 |
|
---|
| 68 | /********* Methode *********/
|
---|
| 69 | /*!
|
---|
| 70 | Remise a zero
|
---|
| 71 | */
|
---|
| 72 | void Histo2DErr::Zero(void)
|
---|
| 73 | {
|
---|
| 74 | if(nx_<=0 || ny_<=0) return;
|
---|
| 75 | data_ = 0.;
|
---|
| 76 | err2_ = 0.;
|
---|
| 77 | ndata_ = 0.;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /********* Methode *********/
|
---|
| 81 | /*!
|
---|
| 82 | Recompute XMin (YMin) and XMax (YMax) so that
|
---|
| 83 | the CENTER of the first bin is exactly XMin (YMin) and
|
---|
| 84 | the CENTER of the last bin is exactly XMax (YMax).
|
---|
| 85 | Remember that otherwise
|
---|
| 86 | XMin (YMin) is the beginning of the first bin
|
---|
| 87 | and XMax (YMax) is the end of the last bin
|
---|
| 88 | */
|
---|
| 89 | void Histo2DErr::ReCenterBinX(void)
|
---|
| 90 | {
|
---|
| 91 | if(nx_<=1) return;
|
---|
| 92 | double dx = (xmax_-xmin_)/(nx_-1);
|
---|
| 93 | xmin_ -= dx/2.;
|
---|
| 94 | xmax_ += dx/2.;
|
---|
| 95 | dx_ = (xmax_-xmin_)/nx_;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void Histo2DErr::ReCenterBinY(void)
|
---|
| 99 | {
|
---|
| 100 | if(ny_<=1) return;
|
---|
| 101 | double dy = (ymax_-ymin_)/(ny_-1);
|
---|
| 102 | ymin_ -= dy/2.;
|
---|
| 103 | ymax_ += dy/2.;
|
---|
| 104 | dy_ = (ymax_-ymin_)/ny_;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | void Histo2DErr::ReCenterBin(void)
|
---|
| 108 | {
|
---|
| 109 | ReCenterBinX();
|
---|
| 110 | ReCenterBinY();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /********* Methode *********/
|
---|
| 114 | /*!
|
---|
[3147] | 115 | Compute the mean histogram.
|
---|
| 116 | Each bin content is divided by the number of entries in the bin.
|
---|
| 117 | Each squared error is divided by the number of entries in the bin.
|
---|
| 118 | The number of entries by bin is NOT set to 1
|
---|
| 119 | (calling ToMean many time will change the histogram !)
|
---|
[3121] | 120 | */
|
---|
[3147] | 121 | void Histo2DErr::ToMean(void)
|
---|
[3121] | 122 | {
|
---|
| 123 | if(nx_<1 || ny_<1) return;
|
---|
[3147] | 124 | mMean++;
|
---|
[3121] | 125 | for(int_4 i=0;i<nx_;i++) {
|
---|
| 126 | for(int_4 j=0;j<ny_;j++) {
|
---|
| 127 | if(ndata_(i,j)<1.) continue;
|
---|
| 128 | data_(i,j) /= ndata_(i,j);
|
---|
| 129 | err2_(i,j) /= ndata_(i,j);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | return;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /********* Methode *********/
|
---|
| 136 | /*!
|
---|
[3147] | 137 | Recompute back the original Histo2DErr after ToMean action
|
---|
[3121] | 138 | */
|
---|
[3147] | 139 | void Histo2DErr::FromMean(void)
|
---|
[3121] | 140 | {
|
---|
| 141 | if(nx_<1 || ny_<1) return;
|
---|
[3147] | 142 | mMean--;
|
---|
[3121] | 143 | for(int_4 i=0;i<nx_;i++) {
|
---|
| 144 | for(int_4 j=0;j<ny_;j++) {
|
---|
| 145 | if(ndata_(i,j)<1.) continue;
|
---|
| 146 | data_(i,j) *= ndata_(i,j);
|
---|
| 147 | err2_(i,j) *= ndata_(i,j);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | return;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /********* Methode *********/
|
---|
| 154 | /*!
|
---|
[3147] | 155 | Compute the mean histogram and replace the "error table" by the variance.
|
---|
| 156 | This should be done if Add(x,w,w) has been used.
|
---|
| 157 | The "value table" is divided by the number of entries to get the mean
|
---|
| 158 | The "error table" is replace by the variance
|
---|
| 159 | The number of entries by bin is NOT set to 1
|
---|
| 160 | (calling ToMean many time will change the histogram !)
|
---|
| 161 | Mixing ToMean and ToVariance leads to unpredictable results
|
---|
| 162 | */
|
---|
| 163 | void Histo2DErr::ToVariance(void)
|
---|
| 164 | {
|
---|
| 165 | if(nx_<1 || ny_<1) return;
|
---|
| 166 | mMean++;
|
---|
| 167 | for(int_4 i=0;i<nx_;i++) {
|
---|
| 168 | for(int_4 j=0;j<ny_;j++) {
|
---|
| 169 | if(ndata_(i,j)<1.) continue;
|
---|
| 170 | data_(i,j) /= ndata_(i,j);
|
---|
| 171 | err2_(i,j) = err2_(i,j)/ndata_(i,j) - data_(i,j)*data_(i,j);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | return;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /********* Methode *********/
|
---|
| 178 | /*!
|
---|
| 179 | Recompute back the original HistoErr after ToVariance action
|
---|
| 180 | Mixing FromMean and FromVariance leads to unpredictable results
|
---|
| 181 | */
|
---|
| 182 | void Histo2DErr::FromVariance(void)
|
---|
| 183 | {
|
---|
| 184 | if(nx_<1 || ny_<1) return;
|
---|
| 185 | mMean--;
|
---|
| 186 | for(int_4 i=0;i<nx_;i++) {
|
---|
| 187 | for(int_4 j=0;j<ny_;j++) {
|
---|
| 188 | if(ndata_(i,j)<1.) continue;
|
---|
| 189 | err2_(i,j) = ndata_(i,j)*(err2_(i,j) + data_(i,j)*data_(i,j));
|
---|
| 190 | data_(i,j) *= ndata_(i,j);
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | return;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /********* Methode *********/
|
---|
| 197 | /*!
|
---|
[3121] | 198 | Fill the histogram with an other histogram
|
---|
| 199 | */
|
---|
| 200 | void Histo2DErr::FillFrHErr(Histo2DErr& hfrom)
|
---|
| 201 | {
|
---|
| 202 | if(nx_<=0 || ny_<=0) return;
|
---|
| 203 | if(hfrom.nx_<=0 || hfrom.ny_<=0) return;
|
---|
| 204 |
|
---|
| 205 | Zero();
|
---|
| 206 |
|
---|
| 207 | for(int_4 i=0;i<hfrom.nx_;i++) {
|
---|
| 208 | for(int_4 j=0;j<hfrom.ny_;j++) {
|
---|
| 209 | r_8 x,y; hfrom.BinCenter(i,j,x,y);
|
---|
| 210 | int ii,jj; FindBin(x,y,ii,jj);
|
---|
| 211 | if(jj<0 || jj>=ny_ || ii<0 || ii>=nx_) continue;
|
---|
| 212 | data_(ii,jj) += hfrom.data_(ii,jj);
|
---|
| 213 | err2_(ii,jj) += hfrom.err2_(ii,jj);
|
---|
| 214 | ndata_(ii,jj) += hfrom.ndata_(ii,jj);
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
[3147] | 217 | mMean = hfrom.mMean;
|
---|
[3121] | 218 |
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /********* Methode *********/
|
---|
| 222 | /*!
|
---|
| 223 | Operateur egal Histo2DErr = Histo2DErr
|
---|
| 224 | */
|
---|
| 225 | Histo2DErr& Histo2DErr::operator = (const Histo2DErr& h)
|
---|
| 226 | {
|
---|
| 227 | if(this==&h) return *this;
|
---|
| 228 | CreateOrResize(h.xmin_,h.xmax_,h.nx_,h.ymin_,h.ymax_,h.ny_);
|
---|
| 229 | data_ = h.data_;
|
---|
| 230 | err2_ = h.err2_;
|
---|
| 231 | ndata_ = h.ndata_;
|
---|
[3147] | 232 | mMean = h.mMean;
|
---|
[3121] | 233 | return *this;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /********* Methode *********/
|
---|
| 237 | /*!
|
---|
[3136] | 238 | Operateur de multiplication par une constante
|
---|
| 239 | */
|
---|
| 240 | Histo2DErr& Histo2DErr::operator *= (r_8 b)
|
---|
| 241 | {
|
---|
| 242 | r_8 b2 = b*b;
|
---|
| 243 | for(int_4 i=0;i<nx_;i++) {
|
---|
| 244 | for(int_4 j=0;j<ny_;j++) {
|
---|
| 245 | data_(i,j) *= b;
|
---|
| 246 | err2_(i,j) *= b2;
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 | return *this;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | /********* Methode *********/
|
---|
| 253 | /*!
|
---|
[3121] | 254 | Print info
|
---|
| 255 | */
|
---|
| 256 | void Histo2DErr::Show(ostream & os) const
|
---|
| 257 | {
|
---|
[3147] | 258 | os <<"Histo2DErr(nmean="<<mMean<<")"<<endl
|
---|
[3121] | 259 | <<" nx="<<nx_<<" ["<<xmin_<<","<<xmax_<<"] dx="<<dx_<<endl
|
---|
| 260 | <<" ny="<<ny_<<" ["<<ymin_<<","<<ymax_<<"] dy="<<dy_<<endl;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[3184] | 263 | /********* Methode *********/
|
---|
| 264 | /*!
|
---|
| 265 | Write to an ASCII file
|
---|
| 266 | */
|
---|
| 267 | int Histo2DErr::WriteASCII(string fname)
|
---|
| 268 | {
|
---|
| 269 | FILE *file = fopen(fname.c_str(),"w");
|
---|
| 270 | if(file==NULL) {
|
---|
| 271 | cout<<"Histo2DErr::WriteASCII_Error: error opening "<<fname<<endl;
|
---|
| 272 | return -1;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | if(NBinX()<=0 || NBinY()<=0) {
|
---|
| 276 | cout<<"Histo2DErr::WriteASCII_Error: wrong number of bins"<<endl;
|
---|
| 277 | return -2;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | fprintf(file,"%ld %.17e %.17e %.17e %ld %.17e %.17e %.17e %d\n"
|
---|
| 281 | ,(long)NBinX(),XMin(),XMax(),WBinX()
|
---|
| 282 | ,(long)NBinY(),YMin(),YMax(),WBinY()
|
---|
| 283 | ,NMean());
|
---|
| 284 | for(long i=0;i<NBinX();i++) for(long j=0;j<NBinY();j++) {
|
---|
| 285 | // ligne = i*NY+j
|
---|
| 286 | fprintf(file,"%d %d %.17e %.17e %.0f\n"
|
---|
| 287 | ,i,j,(*this)(i,j),Error2(i,j),NEntBin(i,j));
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | fclose(file);
|
---|
| 291 | return 0;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | /*!
|
---|
| 295 | Read from an ASCII file
|
---|
| 296 | */
|
---|
| 297 | #define __LENLINE_Histo2DErr_ReadASCII__ 2048
|
---|
| 298 | int Histo2DErr::ReadASCII(string fname)
|
---|
| 299 | {
|
---|
| 300 | FILE *file = fopen(fname.c_str(),"r");
|
---|
| 301 | if(file==NULL) {
|
---|
| 302 | cout<<"Histo2DErr::ReadASCII_Error: error opening "<<fname<<endl;
|
---|
| 303 | return -1;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | char line[__LENLINE_Histo2DErr_ReadASCII__];
|
---|
| 307 | long n=0, nbinx=0, nbiny=0;
|
---|
| 308 |
|
---|
| 309 | while ( fgets(line,__LENLINE_Histo2DErr_ReadASCII__,file) != NULL ) {
|
---|
| 310 |
|
---|
| 311 | if(n==0) {
|
---|
| 312 |
|
---|
| 313 | r_8 xmin,xmax,wx, ymin,ymax,wy; long mnmean=1;
|
---|
| 314 | sscanf(line,"%d %lf %lf %lf %d %lf %lf %lf %d"
|
---|
| 315 | ,&nbinx,&xmin,&xmax,&wx
|
---|
| 316 | ,&nbiny,&ymin,&ymax,&wy
|
---|
| 317 | ,&mnmean);
|
---|
| 318 | if(nbinx<=0 || nbiny<=0) {
|
---|
| 319 | cout<<"Histo2Err::ReadASCII_Error: wrong number of bins"<<endl;
|
---|
| 320 | return -2;
|
---|
| 321 | }
|
---|
| 322 | CreateOrResize(xmin,xmax,nbinx,ymin,ymax,nbiny);
|
---|
| 323 | SetMean(mnmean);
|
---|
| 324 |
|
---|
| 325 | } else {
|
---|
| 326 |
|
---|
| 327 | long i,j; r_8 v,e2,nb;
|
---|
| 328 | sscanf(line,"%d %d %lf %lf %lf",&i,&j,&v,&e2,&nb);
|
---|
| 329 | SetBin(i,j,v);
|
---|
| 330 | SetErr2(i,j,e2);
|
---|
| 331 | SetNentB(i,j,nb);
|
---|
| 332 |
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | n++;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | fclose(file);
|
---|
| 339 | return 0;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[3121] | 342 | ///////////////////////////////////////////////////////////
|
---|
| 343 | // --------------------------------------------------------
|
---|
| 344 | // Les objets delegues pour la gestion de persistance
|
---|
| 345 | // --------------------------------------------------------
|
---|
| 346 | ///////////////////////////////////////////////////////////
|
---|
| 347 |
|
---|
| 348 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 349 | void ObjFileIO<Histo2DErr>::ReadSelf(PInPersist& is)
|
---|
| 350 | {
|
---|
| 351 | string strg;
|
---|
| 352 |
|
---|
| 353 | if(dobj==NULL) dobj = new Histo2DErr;
|
---|
| 354 |
|
---|
| 355 | // Lecture entete
|
---|
| 356 | is.GetStr(strg);
|
---|
| 357 |
|
---|
[3147] | 358 | // Nombre d'appels a ToMean/FromMean
|
---|
| 359 | is.Get(dobj->mMean);
|
---|
[3121] | 360 |
|
---|
| 361 | // Lecture des parametres Histo2DErr
|
---|
| 362 | is.Get(dobj->xmin_);
|
---|
| 363 | is.Get(dobj->xmax_);
|
---|
| 364 | is.Get(dobj->nx_);
|
---|
| 365 | is.Get(dobj->dx_);
|
---|
| 366 | is.Get(dobj->ymin_);
|
---|
| 367 | is.Get(dobj->ymax_);
|
---|
| 368 | is.Get(dobj->ny_);
|
---|
| 369 | is.Get(dobj->dy_);
|
---|
| 370 |
|
---|
| 371 | // Lecture des donnees
|
---|
| 372 | if(dobj->nx_>0 && dobj->ny_>0) {
|
---|
| 373 | is >> dobj->data_;
|
---|
| 374 | is >> dobj->err2_;
|
---|
| 375 | is >> dobj->ndata_;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | return;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 382 | void ObjFileIO<Histo2DErr>::WriteSelf(POutPersist& os) const
|
---|
| 383 | {
|
---|
| 384 | if(dobj == NULL) return;
|
---|
| 385 | string strg;
|
---|
| 386 |
|
---|
| 387 | // Ecriture entete
|
---|
| 388 | strg = "Hist2DErr";
|
---|
| 389 | os.PutStr(strg);
|
---|
| 390 |
|
---|
[3147] | 391 | // Nombre d'appels a ToMean/FromMean
|
---|
| 392 | os.Put(dobj->mMean);
|
---|
[3121] | 393 |
|
---|
| 394 | // Ecriture des parametres Histo2DErr
|
---|
| 395 | os.Put(dobj->xmin_);
|
---|
| 396 | os.Put(dobj->xmax_);
|
---|
| 397 | os.Put(dobj->nx_);
|
---|
| 398 | os.Put(dobj->dx_);
|
---|
| 399 | os.Put(dobj->ymin_);
|
---|
| 400 | os.Put(dobj->ymax_);
|
---|
| 401 | os.Put(dobj->ny_);
|
---|
| 402 | os.Put(dobj->dy_);
|
---|
| 403 |
|
---|
| 404 | // Ecriture des donnees
|
---|
| 405 | if(dobj->nx_>0 && dobj->ny_>0) {
|
---|
| 406 | os << dobj->data_;
|
---|
| 407 | os << dobj->err2_;
|
---|
| 408 | os << dobj->ndata_;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | return;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 415 | #pragma define_template ObjFileIO<Histo2DErr>
|
---|
| 416 | #endif
|
---|
| 417 |
|
---|
| 418 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 419 | template class SOPHYA::ObjFileIO<Histo2DErr>;
|
---|
| 420 | #endif
|
---|