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