| [763] | 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
|  | 2 | // | 
|---|
| [1413] | 3 | // $Id: histos.h,v 1.15 2001-02-20 18:29:48 ansari Exp $ | 
|---|
| [763] | 4 | // | 
|---|
|  | 5 |  | 
|---|
|  | 6 | #ifndef HISTOS_SEEN | 
|---|
|  | 7 | #define HISTOS_SEEN | 
|---|
|  | 8 |  | 
|---|
|  | 9 | #include "objfio.h" | 
|---|
|  | 10 | #include <iostream.h> | 
|---|
|  | 11 | #include <stdio.h> | 
|---|
|  | 12 | #include "peida.h" | 
|---|
|  | 13 | #include "tvector.h" | 
|---|
|  | 14 | #include "ppersist.h" | 
|---|
|  | 15 | #include "anydataobj.h" | 
|---|
|  | 16 |  | 
|---|
|  | 17 | namespace SOPHYA { | 
|---|
|  | 18 |  | 
|---|
| [926] | 19 | //! 1 dimension histograms | 
|---|
| [763] | 20 | class Histo : public AnyDataObj { | 
|---|
|  | 21 | friend class ObjFileIO<Histo>; | 
|---|
|  | 22 | public: | 
|---|
|  | 23 |  | 
|---|
|  | 24 | // CREATOR / DESTRUCTOR | 
|---|
|  | 25 | Histo(); | 
|---|
| [1092] | 26 | Histo(r_8 xMin, r_8 xMax, int_4 nBin=100); | 
|---|
|  | 27 | Histo(r_4 xMin, r_4 xMax, int_4 nBin=100); | 
|---|
| [763] | 28 | Histo(const Histo& H); | 
|---|
| [1056] | 29 | virtual ~Histo(); | 
|---|
| [763] | 30 |  | 
|---|
|  | 31 | // OPTIONS | 
|---|
| [1056] | 32 | void Errors(); | 
|---|
| [763] | 33 |  | 
|---|
|  | 34 | // UPDATING or SETTING | 
|---|
| [1056] | 35 | void Zero(); | 
|---|
| [1092] | 36 | void Add(r_8 x, r_8 w = 1.); | 
|---|
|  | 37 | void AddBin(int_4 numBin, r_8 w = 1.); | 
|---|
|  | 38 | void SetBin(r_8 x, r_8 w = 1.); | 
|---|
|  | 39 | void SetBin(int_4 numBin, r_8 w = 1.); | 
|---|
|  | 40 | void SetErr2(r_8 x, r_8 e2); | 
|---|
|  | 41 | void SetErr2(int_4 numBin, r_8 e2); | 
|---|
|  | 42 | void SetErr(r_8 x, r_8 e); | 
|---|
|  | 43 | void SetErr(int_4 numBin, r_8 e); | 
|---|
| [1135] | 44 | virtual void UpdateHisto(bool force=false) const; | 
|---|
| [763] | 45 |  | 
|---|
|  | 46 | // Operators | 
|---|
|  | 47 | Histo& operator = (const Histo& h); | 
|---|
| [1092] | 48 | Histo& operator *= (r_8 b); | 
|---|
|  | 49 | Histo& operator /= (r_8 b); | 
|---|
|  | 50 | Histo& operator += (r_8 b); | 
|---|
|  | 51 | Histo& operator -= (r_8 b); | 
|---|
| [763] | 52 | Histo& operator += (const Histo& a); | 
|---|
|  | 53 | Histo& operator -= (const Histo& a); | 
|---|
|  | 54 | Histo& operator *= (const Histo& a); | 
|---|
|  | 55 | Histo& operator /= (const Histo& a); | 
|---|
|  | 56 |  | 
|---|
|  | 57 | // get/put dans/depuis un vector | 
|---|
| [1109] | 58 | void GetAbsc(TVector<r_8>& v) const; | 
|---|
|  | 59 | void GetValue(TVector<r_8>& v) const; | 
|---|
|  | 60 | void GetError2(TVector<r_8>& v) const; | 
|---|
|  | 61 | void GetError(TVector<r_8>& v) const; | 
|---|
| [1092] | 62 | void PutValue(TVector<r_8>& v, int_4 ierr=0); | 
|---|
|  | 63 | void PutValueAdd(TVector<r_8> &v, int_4 ierr=0); | 
|---|
| [943] | 64 | void PutError2(TVector<r_8>& v); | 
|---|
|  | 65 | void PutError2Add(TVector<r_8>& v); | 
|---|
|  | 66 | void PutError(TVector<r_8>& v); | 
|---|
| [763] | 67 |  | 
|---|
|  | 68 | // INLINES | 
|---|
| [914] | 69 | //! Retourne l'abscisse minimum | 
|---|
| [1092] | 70 | inline r_8 XMin() const {return mMin;} | 
|---|
| [914] | 71 | //! Retourne l'abscisse maximum | 
|---|
| [1092] | 72 | inline r_8 XMax() const {return mMax;} | 
|---|
| [914] | 73 | //! Retourne le nombre de bins | 
|---|
| [1092] | 74 | inline int_4 NBins() const {return mBins;} | 
|---|
| [914] | 75 | //! Retourne la largeur du bin | 
|---|
| [1092] | 76 | inline r_8 BinWidth() const {return binWidth;} | 
|---|
| [914] | 77 | //! Retourne le pointeur sur le tableaux des contenus | 
|---|
| [1092] | 78 | inline r_8* Bins() const {return mData;} | 
|---|
| [914] | 79 | //! Retourne le contenu du bin i | 
|---|
| [1092] | 80 | inline r_8 operator()(int_4 i) const  {return mData[i];} | 
|---|
| [914] | 81 | //! Remplit le contenu du bin i | 
|---|
| [1092] | 82 | inline r_8& operator()(int_4 i) {return mData[i];} | 
|---|
| [914] | 83 | //! retourne "true" si il y a des erreurs stoquees | 
|---|
| [1109] | 84 | inline bool HasErrors() const | 
|---|
| [1092] | 85 | {if(mErr2) return true; else return false;} | 
|---|
| [914] | 86 | //! Retourne l'erreur du bin i | 
|---|
| [1092] | 87 | inline r_8 Error(int_4 i) const | 
|---|
|  | 88 | {if(mErr2) {if(mErr2[i]>0.) return sqrt(mErr2[i]); else return 0.;} | 
|---|
| [1056] | 89 | else return 0.;} | 
|---|
| [914] | 90 | //! Retourne l'erreur au carre du bin i | 
|---|
| [1092] | 91 | inline r_8 Error2(int_4 i) const | 
|---|
|  | 92 | {if(mErr2) return mErr2[i]; else return 0.;} | 
|---|
| [914] | 93 | //! Remplit l'erreur au carre du bin i | 
|---|
| [1092] | 94 | inline r_8& Error2(int_4 i) {return mErr2[i];} | 
|---|
| [914] | 95 | //! Retourne la somme ponderee | 
|---|
| [1092] | 96 | inline r_8 NData() const            {return nHist;} | 
|---|
| [914] | 97 | //! Retourne le nombre d'entrees | 
|---|
| [1092] | 98 | inline r_8 NEntries() const         {return nEntries;} | 
|---|
| [914] | 99 | //! Retourne le nombre d'overflow | 
|---|
| [1092] | 100 | inline r_8 NOver() const            {return mOver;} | 
|---|
| [914] | 101 | //! Retourne le nombre d'underflow | 
|---|
| [1092] | 102 | inline r_8 NUnder() const           {return mUnder;} | 
|---|
| [763] | 103 |  | 
|---|
| [914] | 104 | //! Retourne l'abscisse du bord inferieur du bin i | 
|---|
| [1092] | 105 | inline r_8 BinLowEdge(int_4 i)  const {return mMin + i*binWidth;} | 
|---|
| [914] | 106 | //! Retourne l'abscisse du centre du bin i | 
|---|
| [1092] | 107 | inline r_8 BinCenter(int_4 i)   const {return mMin + (i+0.5)*binWidth;} | 
|---|
| [914] | 108 | //! Retourne l'abscisse du bord superieur du bin i | 
|---|
| [1092] | 109 | inline r_8 BinHighEdge(int_4 i) const {return mMin + (i+1)*binWidth;} | 
|---|
| [914] | 110 | //! Retourne le numero du bin contenant l'abscisse x | 
|---|
| [1092] | 111 | inline int_4 FindBin(r_8 x) const | 
|---|
|  | 112 | {return (int_4) floor((x - mMin) / binWidth);} | 
|---|
| [763] | 113 |  | 
|---|
|  | 114 | // Info, statistique et calculs sur les histogrammes | 
|---|
| [1092] | 115 | int_4     BinNonNul() const; | 
|---|
|  | 116 | int_4     ErrNonNul() const; | 
|---|
|  | 117 | int_4     IMax() const; | 
|---|
|  | 118 | int_4     IMin() const; | 
|---|
|  | 119 | r_8       VMax() const; | 
|---|
|  | 120 | r_8       VMin() const; | 
|---|
|  | 121 | r_8       Mean() const; | 
|---|
|  | 122 | r_8       Sigma() const; | 
|---|
|  | 123 | r_8       MeanLH(int_4 il,int_4 ih) const; | 
|---|
|  | 124 | r_8       SigmaLH(int_4 il,int_4 ih) const; | 
|---|
|  | 125 | r_8       Mean(r_8 x0, r_8 dx) const; | 
|---|
|  | 126 | r_8       Sigma(r_8 x0, r_8 dx) const; | 
|---|
|  | 127 | r_8       CleanedMean() const; | 
|---|
|  | 128 | r_8       CleanedMean(r_8& sigma) const; | 
|---|
|  | 129 | int_4     BinPercent(r_8 per) const; | 
|---|
| [1109] | 130 | int_4     BinPercent(r_8 x,r_8 per,int_4& imin,int_4& imax) const; | 
|---|
|  | 131 | int_4     BinPercent(r_8 x,r_8 per,r_8& xmin,r_8& xmax) const; | 
|---|
| [1092] | 132 | void      HInteg(r_8 norm = 0.); | 
|---|
| [1056] | 133 | void      HDeriv(); | 
|---|
| [1092] | 134 | virtual void HRebin(int_4 nbinew); | 
|---|
| [763] | 135 |  | 
|---|
| [1109] | 136 | int_4     MaxiLocal(r_8& maxi,int_4& imax,r_8& maxn,int_4& imaxn) const; | 
|---|
| [1092] | 137 | r_8       FitMax(int_4 degree=2, r_8 frac=0.5f, int_4 debug=0) const; | 
|---|
|  | 138 | r_8       FindWidth(r_8 xmax,r_8 frac=0.5f, int_4 debug=0) const; | 
|---|
|  | 139 | r_8       FindWidth(r_8 frac=0.5f, int_4 debug=0) const; | 
|---|
| [1109] | 140 | int_4     EstimeMax(r_8& xm,int_4 SzPav = 3) const; | 
|---|
|  | 141 | int_4     EstimeMax(int_4& im,r_8& xm,int_4 SzPav = 3) const; | 
|---|
|  | 142 | void      EstimeWidthS(r_8 frac,r_8& widthG,r_8& widthD) const; | 
|---|
| [763] | 143 |  | 
|---|
|  | 144 | // Print et Display ASCII | 
|---|
| [1413] | 145 | virtual void Show(ostream& os) const; | 
|---|
|  | 146 | inline  void Show() const { Show(cout); } | 
|---|
| [1092] | 147 | void Print(int_4 dyn = 100, r_8 hmin = 1., r_8 hmax = -1., | 
|---|
| [1201] | 148 | int_4 pflag = 0, int_4 il = 1, int_4 ih = -1) const; | 
|---|
| [1413] | 149 |  | 
|---|
| [763] | 150 | protected: | 
|---|
|  | 151 | void Delete(); | 
|---|
|  | 152 |  | 
|---|
| [1092] | 153 | r_8*   mData;    //!< donnees | 
|---|
|  | 154 | r_8*   mErr2;    //!< erreurs carrees | 
|---|
|  | 155 | r_8    mUnder;   //!< underflow | 
|---|
|  | 156 | r_8    mOver;    //!< overflow | 
|---|
|  | 157 | r_8    nHist;    //!< somme ponderee des entrees | 
|---|
|  | 158 | int_4  nEntries; //!< nombre d'entrees | 
|---|
|  | 159 | int_4  mBins;    //!< nombre de bins | 
|---|
|  | 160 | r_8    mMin;     //!< abscisse minimum | 
|---|
|  | 161 | r_8    mMax;     //!< abscisse maximum | 
|---|
|  | 162 | r_8    binWidth; //!< largeur du bin | 
|---|
| [763] | 163 | }; | 
|---|
|  | 164 |  | 
|---|
| [1413] | 165 | /*! Prints histogram information on stream \b s (h.Show(s)) */ | 
|---|
|  | 166 | inline ostream& operator << (ostream& s, Histo const & h) | 
|---|
|  | 167 | {  h.Show(s);  return(s);  } | 
|---|
| [763] | 168 |  | 
|---|
| [958] | 169 | /*! \ingroup HiStats \fn operator<<(POuttPersist&,Histo) | 
|---|
|  | 170 | \brief Persistance management */ | 
|---|
| [763] | 171 | inline POutPersist& operator << (POutPersist& os, Histo & obj) | 
|---|
|  | 172 | { ObjFileIO<Histo> fio(&obj);  fio.Write(os);  return(os); } | 
|---|
| [958] | 173 | /*! \ingroup HiStats \fn operator<<(POuttPersist&,Histo) | 
|---|
|  | 174 | \brief Persistance management */ | 
|---|
| [763] | 175 | inline PInPersist& operator >> (PInPersist& is, Histo & obj) | 
|---|
|  | 176 | { ObjFileIO<Histo> fio(&obj);  fio.Read(is);  return(is); } | 
|---|
|  | 177 |  | 
|---|
|  | 178 | // Classe pour la gestion de persistance | 
|---|
|  | 179 | // ObjFileIO<Histo> | 
|---|
|  | 180 |  | 
|---|
| [1092] | 181 | /*! \ingroup HiStats \fn operator*(const Histo&,r_8) | 
|---|
| [1053] | 182 | \brief Operateur H2 = H1 * b */ | 
|---|
| [1092] | 183 | inline Histo operator * (const Histo& a, r_8 b) | 
|---|
| [1053] | 184 | { | 
|---|
|  | 185 | Histo result(a); | 
|---|
|  | 186 | return (result *= b); | 
|---|
|  | 187 | } | 
|---|
| [763] | 188 |  | 
|---|
| [1092] | 189 | /*! \ingroup HiStats \fn operator*(r_8,const Histo&) | 
|---|
| [1053] | 190 | \brief Operateur H2 = b * H1 */ | 
|---|
| [1092] | 191 | inline Histo operator * (r_8 b, const Histo& a) | 
|---|
| [1053] | 192 | { | 
|---|
|  | 193 | Histo result(a); | 
|---|
|  | 194 | return (result *= b); | 
|---|
|  | 195 | } | 
|---|
|  | 196 |  | 
|---|
| [1092] | 197 | /*! \ingroup HiStats \fn operator/(const Histo&,r_8) | 
|---|
| [1053] | 198 | \brief Operateur H2 = H1 / b */ | 
|---|
| [1092] | 199 | inline Histo operator / (const Histo& a, r_8 b) | 
|---|
| [1053] | 200 | { | 
|---|
|  | 201 | Histo result(a); | 
|---|
|  | 202 | return (result /= b); | 
|---|
|  | 203 | } | 
|---|
|  | 204 |  | 
|---|
| [1092] | 205 | /*! \ingroup HiStats \fn operator+(const Histo&,r_8) | 
|---|
| [1053] | 206 | \brief Operateur H2 = H1 + b */ | 
|---|
| [1092] | 207 | inline Histo operator + (const Histo& a, r_8 b) | 
|---|
| [1053] | 208 | { | 
|---|
|  | 209 | Histo result(a); | 
|---|
|  | 210 | return (result += b); | 
|---|
|  | 211 | } | 
|---|
|  | 212 |  | 
|---|
| [1092] | 213 | /*! \ingroup HiStats \fn operator+(r_8,const Histo&) | 
|---|
| [1053] | 214 | \brief Operateur H2 = b + H1 */ | 
|---|
| [1092] | 215 | inline Histo operator + (r_8 b, const Histo& a) | 
|---|
| [1053] | 216 | { | 
|---|
|  | 217 | Histo result(a); | 
|---|
|  | 218 | return (result += b); | 
|---|
|  | 219 | } | 
|---|
|  | 220 |  | 
|---|
| [1092] | 221 | /*! \ingroup HiStats \fn operator-(const Histo&,r_8) | 
|---|
| [1053] | 222 | \brief Operateur H2 = H1 - b */ | 
|---|
| [1092] | 223 | inline Histo operator - (const Histo& a, r_8 b) | 
|---|
| [1053] | 224 | { | 
|---|
|  | 225 | Histo result(a); | 
|---|
|  | 226 | return (result -= b); | 
|---|
|  | 227 | } | 
|---|
|  | 228 |  | 
|---|
| [1092] | 229 | /*! \ingroup HiStats \fn operator-(r_8,const Histo&) | 
|---|
| [1053] | 230 | \brief Operateur H2 = b - H1 */ | 
|---|
| [1092] | 231 | inline Histo operator - (r_8 b, const Histo& a) | 
|---|
| [1053] | 232 | { | 
|---|
|  | 233 | Histo result(a); | 
|---|
|  | 234 | result *= -1.; | 
|---|
|  | 235 | return (result += b); | 
|---|
|  | 236 | } | 
|---|
|  | 237 |  | 
|---|
|  | 238 | /*! \ingroup HiStats \fn operator+(const Histo&,const Histo&) | 
|---|
|  | 239 | \brief Operateur H = H1 + H2 */ | 
|---|
|  | 240 | inline Histo operator + (const Histo& a, const Histo& b) | 
|---|
|  | 241 | { | 
|---|
|  | 242 | if (b.NBins()!=a.NBins()) THROW(sizeMismatchErr); | 
|---|
|  | 243 | Histo c(a); | 
|---|
|  | 244 | return (c += b); | 
|---|
|  | 245 | } | 
|---|
|  | 246 |  | 
|---|
|  | 247 | /*! \ingroup HiStats \fn operator-(const Histo&,const Histo&) | 
|---|
|  | 248 | \brief Operateur H = H1 - H2 */ | 
|---|
|  | 249 | inline Histo operator - (const Histo& a, const Histo& b) | 
|---|
|  | 250 | { | 
|---|
|  | 251 | if (b.NBins()!=a.NBins()) THROW(sizeMismatchErr); | 
|---|
|  | 252 | Histo c(a); | 
|---|
|  | 253 | return (c -= b); | 
|---|
|  | 254 | } | 
|---|
|  | 255 |  | 
|---|
|  | 256 | /*! \ingroup HiStats \fn operator*(const Histo&,const Histo&) | 
|---|
|  | 257 | \brief Operateur H = H1 * H2 */ | 
|---|
|  | 258 | inline Histo operator * (const Histo& a, const Histo& b) | 
|---|
|  | 259 | { | 
|---|
|  | 260 | if (b.NBins()!=a.NBins()) THROW(sizeMismatchErr); | 
|---|
|  | 261 | Histo c(a); | 
|---|
|  | 262 | return (c *= b); | 
|---|
|  | 263 | } | 
|---|
|  | 264 |  | 
|---|
|  | 265 | /*! \ingroup HiStats \fn operator/(const Histo&,const Histo&) | 
|---|
|  | 266 | \brief Operateur H = H1 / H2 */ | 
|---|
|  | 267 | inline Histo operator / (const Histo& a, const Histo& b) | 
|---|
|  | 268 | { | 
|---|
|  | 269 | if (b.NBins()!=a.NBins()) THROW(sizeMismatchErr); | 
|---|
|  | 270 | Histo c(a); | 
|---|
|  | 271 | return (c /= b); | 
|---|
|  | 272 | } | 
|---|
|  | 273 |  | 
|---|
| [763] | 274 | } // Fin du namespace | 
|---|
|  | 275 |  | 
|---|
|  | 276 | #endif // HISTOS_SEEN | 
|---|