[2615] | 1 | #include "sopnamsp.h"
|
---|
[2603] | 2 | #include "machdefs.h"
|
---|
| 3 | #include <string.h>
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 | #include <math.h>
|
---|
[3123] | 6 | #include "perrors.h"
|
---|
| 7 | #include "fioarr.h"
|
---|
[2603] | 8 | #include "histerr.h"
|
---|
| 9 |
|
---|
| 10 | /*!
|
---|
| 11 | \class SOPHYA::HistoErr
|
---|
| 12 | \ingroup HiStats
|
---|
| 13 | Classe d'histogrammes 1D avec erreurs donnees par l'utilisateur
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | /********* Methode *********/
|
---|
| 17 | /*! Constructeur par defaut */
|
---|
| 18 | HistoErr::HistoErr(void)
|
---|
[3123] | 19 | : xmin_(1.), xmax_(-1.), nx_(0), dx_(0.)
|
---|
[3147] | 20 | , 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)
|
---|
[3147] | 27 | : mMean(0)
|
---|
[2603] | 28 | {
|
---|
[3123] | 29 | CreateOrResize(xmin,xmax,nx);
|
---|
[2603] | 30 | }
|
---|
| 31 |
|
---|
| 32 | /********* Methode *********/
|
---|
| 33 | /*! Constructeur par copie */
|
---|
| 34 | HistoErr::HistoErr(const HistoErr& H)
|
---|
[3147] | 35 | : mMean(H.mMean)
|
---|
[2603] | 36 | {
|
---|
[3123] | 37 | if(H.nx_<=0) return;
|
---|
| 38 | CreateOrResize(H.xmin_,H.xmax_,H.nx_);
|
---|
| 39 | data_ = H.data_;
|
---|
| 40 | err2_ = H.err2_;
|
---|
| 41 | ndata_ = H.ndata_;
|
---|
[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.;
|
---|
[2603] | 75 | }
|
---|
| 76 |
|
---|
| 77 | /********* Methode *********/
|
---|
| 78 | /*!
|
---|
[3123] | 79 | Recompute XMin (and XMax so that
|
---|
| 80 | the CENTER of the first bin is exactly XMin and
|
---|
| 81 | the CENTER of the last bin is exactly XMax.
|
---|
| 82 | Remember that otherwise
|
---|
| 83 | XMin is the beginning of the first bin
|
---|
| 84 | and XMax is the end of the last bin
|
---|
[2604] | 85 | */
|
---|
[3123] | 86 | void HistoErr::ReCenterBin(void)
|
---|
[2604] | 87 | {
|
---|
[3123] | 88 | if(nx_<=1) return;
|
---|
| 89 | double dx = (xmax_-xmin_)/(nx_-1);
|
---|
| 90 | xmin_ -= dx/2.;
|
---|
| 91 | xmax_ += dx/2.;
|
---|
| 92 | dx_ = (xmax_-xmin_)/nx_;
|
---|
[2604] | 93 | }
|
---|
| 94 |
|
---|
| 95 | /********* Methode *********/
|
---|
| 96 | /*!
|
---|
[3147] | 97 | Compute the mean histogram.
|
---|
| 98 | Each bin content is divided by the number of entries in the bin.
|
---|
| 99 | Each squared error is divided by the number of entries in the bin.
|
---|
| 100 | The number of entries by bin is NOT set to 1
|
---|
| 101 | (calling ToMean many time will change the histogram !)
|
---|
[2604] | 102 | */
|
---|
[3147] | 103 | void HistoErr::ToMean(void)
|
---|
[2604] | 104 | {
|
---|
[3123] | 105 | if(nx_<1) return;
|
---|
[3147] | 106 | mMean++;
|
---|
[3123] | 107 | for(int_4 i=0;i<nx_;i++) {
|
---|
| 108 | if(ndata_(i)<1.) continue;
|
---|
| 109 | data_(i) /= ndata_(i);
|
---|
| 110 | err2_(i) /= ndata_(i);
|
---|
[2604] | 111 | }
|
---|
| 112 | return;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[2619] | 115 | /********* Methode *********/
|
---|
| 116 | /*!
|
---|
[3147] | 117 | Recompute back the original HistoErr after ToMean action
|
---|
[2819] | 118 | */
|
---|
[3147] | 119 | void HistoErr::FromMean(void)
|
---|
[2819] | 120 | {
|
---|
[3123] | 121 | if(nx_<1) return;
|
---|
[3147] | 122 | mMean--;
|
---|
[3123] | 123 | for(int_4 i=0;i<nx_;i++) {
|
---|
| 124 | if(ndata_(i)<1.) continue;
|
---|
| 125 | data_(i) *= ndata_(i);
|
---|
| 126 | err2_(i) *= ndata_(i);
|
---|
[2819] | 127 | }
|
---|
| 128 | return;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /********* Methode *********/
|
---|
| 132 | /*!
|
---|
[3147] | 133 | Compute the mean histogram and replace the "error table" by the variance.
|
---|
| 134 | This should be done if Add(x,w,w) has been used.
|
---|
| 135 | The "value table" is divided by the number of entries to get the mean
|
---|
| 136 | The "error table" is replace by the variance
|
---|
| 137 | The number of entries by bin is NOT set to 1
|
---|
| 138 | (calling ToMean many time will change the histogram !)
|
---|
| 139 | Mixing ToMean and ToVariance leads to unpredictable results
|
---|
| 140 | */
|
---|
| 141 | void HistoErr::ToVariance(void)
|
---|
| 142 | {
|
---|
| 143 | if(nx_<1) return;
|
---|
| 144 | mMean++;
|
---|
| 145 | for(int_4 i=0;i<nx_;i++) {
|
---|
| 146 | if(ndata_(i)<1.) continue;
|
---|
| 147 | data_(i) /= ndata_(i);
|
---|
| 148 | err2_(i) = err2_(i)/ndata_(i) - data_(i)*data_(i);
|
---|
| 149 | }
|
---|
| 150 | return;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /********* Methode *********/
|
---|
| 154 | /*!
|
---|
| 155 | Recompute back the original HistoErr after ToVariance action
|
---|
| 156 | Mixing FromMean and FromVariance leads to unpredictable results
|
---|
| 157 | */
|
---|
| 158 | void HistoErr::FromVariance(void)
|
---|
| 159 | {
|
---|
| 160 | if(nx_<1) return;
|
---|
| 161 | mMean--;
|
---|
| 162 | for(int_4 i=0;i<nx_;i++) {
|
---|
| 163 | if(ndata_(i)<1.) continue;
|
---|
| 164 | err2_(i) = ndata_(i)*(err2_(i) + data_(i)*data_(i));
|
---|
| 165 | data_(i) *= ndata_(i);
|
---|
| 166 | }
|
---|
| 167 | return;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /********* Methode *********/
|
---|
| 171 | /*!
|
---|
[2628] | 172 | Fill the histogram with an other histogram
|
---|
| 173 | */
|
---|
| 174 | void HistoErr::FillFrHErr(HistoErr& hfrom)
|
---|
| 175 | {
|
---|
[3123] | 176 | if(nx_<=0) return;
|
---|
| 177 | if(hfrom.nx_<=0) return;
|
---|
[2628] | 178 |
|
---|
| 179 | Zero();
|
---|
| 180 |
|
---|
[3123] | 181 | for(int_4 i=0;i<hfrom.nx_;i++) {
|
---|
| 182 | r_8 x = hfrom.BinCenter(i);
|
---|
| 183 | int ii = FindBin(x);
|
---|
| 184 | if(ii<0 || ii>=nx_) continue;
|
---|
| 185 | data_(ii) += hfrom.data_(ii);
|
---|
| 186 | err2_(ii) += hfrom.err2_(ii);
|
---|
| 187 | ndata_(ii) += hfrom.ndata_(ii);
|
---|
[2628] | 188 | }
|
---|
[3147] | 189 | mMean = hfrom.mMean;
|
---|
[2628] | 190 |
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /********* Methode *********/
|
---|
| 194 | /*!
|
---|
[2619] | 195 | Operateur egal HistoErr = HistoErr
|
---|
| 196 | */
|
---|
| 197 | HistoErr& HistoErr::operator = (const HistoErr& h)
|
---|
| 198 | {
|
---|
| 199 | if(this==&h) return *this;
|
---|
[3123] | 200 | CreateOrResize(h.xmin_,h.xmax_,h.nx_);
|
---|
| 201 | data_ = h.data_;
|
---|
| 202 | err2_ = h.err2_;
|
---|
| 203 | ndata_ = h.ndata_;
|
---|
[3147] | 204 | mMean = h.mMean;
|
---|
[2619] | 205 | return *this;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[3053] | 208 | /********* Methode *********/
|
---|
| 209 | /*!
|
---|
[3136] | 210 | Operateur de multiplication par une constante
|
---|
| 211 | */
|
---|
| 212 | HistoErr& HistoErr::operator *= (r_8 b)
|
---|
| 213 | {
|
---|
| 214 | r_8 b2 = b*b;
|
---|
| 215 | for(int_4 i=0;i<nx_;i++) {
|
---|
| 216 | data_(i) *= b;
|
---|
| 217 | err2_(i) *= b2;
|
---|
| 218 | }
|
---|
| 219 | return *this;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | /********* Methode *********/
|
---|
| 223 | /*!
|
---|
[3053] | 224 | Print info
|
---|
| 225 | */
|
---|
| 226 | void HistoErr::Show(ostream & os) const
|
---|
| 227 | {
|
---|
[3147] | 228 | os <<"HistoErr(nmean="<<mMean<<")"<<endl
|
---|
[3123] | 229 | <<" nx="<<nx_<<" ["<<xmin_<<","<<xmax_<<"] dx="<<dx_<<endl;
|
---|
[3053] | 230 | }
|
---|
| 231 |
|
---|
[2603] | 232 | ///////////////////////////////////////////////////////////
|
---|
| 233 | // --------------------------------------------------------
|
---|
| 234 | // Les objets delegues pour la gestion de persistance
|
---|
| 235 | // --------------------------------------------------------
|
---|
| 236 | ///////////////////////////////////////////////////////////
|
---|
| 237 |
|
---|
| 238 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 239 | void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
|
---|
| 240 | {
|
---|
[2608] | 241 | string strg;
|
---|
[2603] | 242 |
|
---|
[3123] | 243 | if(dobj==NULL) dobj = new HistoErr;
|
---|
[2603] | 244 |
|
---|
| 245 | // Lecture entete
|
---|
[2608] | 246 | is.GetStr(strg);
|
---|
[2603] | 247 |
|
---|
[3147] | 248 | // Nombre d'appels a ToMean/FromMean
|
---|
| 249 | is.Get(dobj->mMean);
|
---|
[3049] | 250 |
|
---|
[3123] | 251 | // Lecture des parametres HistoErr
|
---|
| 252 | is.Get(dobj->xmin_);
|
---|
| 253 | is.Get(dobj->xmax_);
|
---|
| 254 | is.Get(dobj->nx_);
|
---|
| 255 | is.Get(dobj->dx_);
|
---|
[2603] | 256 |
|
---|
[3123] | 257 | // Lecture des donnees
|
---|
| 258 | if(dobj->nx_>0) {
|
---|
| 259 | is >> dobj->data_;
|
---|
| 260 | is >> dobj->err2_;
|
---|
| 261 | is >> dobj->ndata_;
|
---|
| 262 | }
|
---|
[2603] | 263 |
|
---|
| 264 | return;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 268 | void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
|
---|
| 269 | {
|
---|
| 270 | if(dobj == NULL) return;
|
---|
[2608] | 271 | string strg;
|
---|
[2603] | 272 |
|
---|
[3049] | 273 | // Ecriture entete
|
---|
[2608] | 274 | strg = "HistErr";
|
---|
| 275 | os.PutStr(strg);
|
---|
[2603] | 276 |
|
---|
[3147] | 277 | // Nombre d'appels a ToMean/FromMean
|
---|
| 278 | os.Put(dobj->mMean);
|
---|
[3049] | 279 |
|
---|
[3123] | 280 | // Ecriture des parametres HistoErr
|
---|
| 281 | os.Put(dobj->xmin_);
|
---|
| 282 | os.Put(dobj->xmax_);
|
---|
| 283 | os.Put(dobj->nx_);
|
---|
| 284 | os.Put(dobj->dx_);
|
---|
[2603] | 285 |
|
---|
[3123] | 286 | // Ecriture des donnees
|
---|
| 287 | if(dobj->nx_>0) {
|
---|
| 288 | os << dobj->data_;
|
---|
| 289 | os << dobj->err2_;
|
---|
| 290 | os << dobj->ndata_;
|
---|
| 291 | }
|
---|
[2603] | 292 |
|
---|
| 293 | return;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 297 | #pragma define_template ObjFileIO<HistoErr>
|
---|
| 298 | #endif
|
---|
| 299 |
|
---|
| 300 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
[2868] | 301 | template class SOPHYA::ObjFileIO<HistoErr>;
|
---|
[2603] | 302 | #endif
|
---|