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