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