| [763] | 1 | // This may look like C code, but it is really -*- C++ -*-
 | 
|---|
 | 2 | //
 | 
|---|
| [3110] | 3 | // $Id: histos.h,v 1.26 2006-11-20 13:15:35 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
 | 
|---|
| [3110] | 36 |   void Errors();
 | 
|---|
 | 37 |   void ReCenterBin();
 | 
|---|
| [763] | 38 | 
 | 
|---|
 | 39 |   // UPDATING or SETTING
 | 
|---|
| [3110] | 40 |   void Zero();
 | 
|---|
| [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;
 | 
|---|
| [3110] | 130 |   r_8       Sum() const;
 | 
|---|
 | 131 |   r_8       Sum2() const;
 | 
|---|
| [1092] | 132 |   r_8       Mean() const;
 | 
|---|
 | 133 |   r_8       Sigma() const;
 | 
|---|
 | 134 |   r_8       MeanLH(int_4 il,int_4 ih) const;
 | 
|---|
 | 135 |   r_8       SigmaLH(int_4 il,int_4 ih) const;
 | 
|---|
 | 136 |   r_8       Mean(r_8 x0, r_8 dx) const;
 | 
|---|
 | 137 |   r_8       Sigma(r_8 x0, r_8 dx) const;
 | 
|---|
 | 138 |   r_8       CleanedMean() const;
 | 
|---|
 | 139 |   r_8       CleanedMean(r_8& sigma) const;
 | 
|---|
 | 140 |   int_4     BinPercent(r_8 per) const;
 | 
|---|
| [1109] | 141 |   int_4     BinPercent(r_8 x,r_8 per,int_4& imin,int_4& imax) const;
 | 
|---|
 | 142 |   int_4     BinPercent(r_8 x,r_8 per,r_8& xmin,r_8& xmax) const;
 | 
|---|
| [1092] | 143 |   void      HInteg(r_8 norm = 0.);
 | 
|---|
| [1056] | 144 |   void      HDeriv();
 | 
|---|
| [1092] | 145 |   virtual void HRebin(int_4 nbinew);
 | 
|---|
| [763] | 146 | 
 | 
|---|
| [1109] | 147 |   int_4     MaxiLocal(r_8& maxi,int_4& imax,r_8& maxn,int_4& imaxn) const;
 | 
|---|
| [1092] | 148 |   r_8       FitMax(int_4 degree=2, r_8 frac=0.5f, int_4 debug=0) const;
 | 
|---|
 | 149 |   r_8       FindWidth(r_8 xmax,r_8 frac=0.5f, int_4 debug=0) const;
 | 
|---|
 | 150 |   r_8       FindWidth(r_8 frac=0.5f, int_4 debug=0) const;
 | 
|---|
| [1109] | 151 |   int_4     EstimeMax(r_8& xm,int_4 SzPav = 3) const;
 | 
|---|
 | 152 |   int_4     EstimeMax(int_4& im,r_8& xm,int_4 SzPav = 3) const;
 | 
|---|
 | 153 |   void      EstimeWidthS(r_8 frac,r_8& widthG,r_8& widthD) const;
 | 
|---|
| [763] | 154 | 
 | 
|---|
 | 155 |   // Print et Display ASCII
 | 
|---|
| [1413] | 156 |   virtual void Show(ostream& os) const;
 | 
|---|
 | 157 |   inline  void Show() const { Show(cout); }
 | 
|---|
| [1092] | 158 |   void Print(int_4 dyn = 100, r_8 hmin = 1., r_8 hmax = -1.,
 | 
|---|
| [1201] | 159 |              int_4 pflag = 0, int_4 il = 1, int_4 ih = -1) const;
 | 
|---|
| [1413] | 160 |   
 | 
|---|
| [763] | 161 | protected:
 | 
|---|
| [3053] | 162 |   void CreateOrResize(r_8 xMin, r_8 xMax, int_4 nBin);
 | 
|---|
| [3110] | 163 |   void Delete(); // Ne pas passer en "virtual"
 | 
|---|
| [763] | 164 | 
 | 
|---|
| [1092] | 165 |   r_8*   mData;    //!< donnees
 | 
|---|
 | 166 |   r_8*   mErr2;    //!< erreurs carrees
 | 
|---|
 | 167 |   r_8    mUnder;   //!< underflow
 | 
|---|
 | 168 |   r_8    mOver;    //!< overflow
 | 
|---|
 | 169 |   r_8    nHist;    //!< somme ponderee des entrees
 | 
|---|
| [2628] | 170 |   uint_8 nEntries; //!< nombre d'entrees
 | 
|---|
| [1092] | 171 |   int_4  mBins;    //!< nombre de bins
 | 
|---|
 | 172 |   r_8    mMin;     //!< abscisse minimum
 | 
|---|
 | 173 |   r_8    mMax;     //!< abscisse maximum
 | 
|---|
 | 174 |   r_8    binWidth; //!< largeur du bin
 | 
|---|
| [763] | 175 | };
 | 
|---|
 | 176 | 
 | 
|---|
| [1413] | 177 | /*! Prints histogram information on stream \b s (h.Show(s)) */
 | 
|---|
 | 178 | inline ostream& operator << (ostream& s, Histo const & h)
 | 
|---|
 | 179 |   {  h.Show(s);  return(s);  }
 | 
|---|
| [763] | 180 | 
 | 
|---|
| [958] | 181 | /*! \ingroup HiStats \fn operator<<(POuttPersist&,Histo)
 | 
|---|
 | 182 |   \brief Persistance management */
 | 
|---|
| [763] | 183 | inline POutPersist& operator << (POutPersist& os, Histo & obj)
 | 
|---|
 | 184 | { ObjFileIO<Histo> fio(&obj);  fio.Write(os);  return(os); }
 | 
|---|
| [958] | 185 | /*! \ingroup HiStats \fn operator<<(POuttPersist&,Histo)
 | 
|---|
 | 186 |   \brief Persistance management */
 | 
|---|
| [763] | 187 | inline PInPersist& operator >> (PInPersist& is, Histo & obj)
 | 
|---|
| [2479] | 188 | { ObjFileIO<Histo> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
 | 
|---|
| [763] | 189 | 
 | 
|---|
 | 190 | // Classe pour la gestion de persistance
 | 
|---|
 | 191 | // ObjFileIO<Histo>
 | 
|---|
 | 192 | 
 | 
|---|
| [1092] | 193 | /*! \ingroup HiStats \fn operator*(const Histo&,r_8)
 | 
|---|
| [1053] | 194 |   \brief Operateur H2 = H1 * b */
 | 
|---|
| [1092] | 195 | inline Histo operator * (const Histo& a, r_8 b)
 | 
|---|
| [1053] | 196 | {
 | 
|---|
 | 197 |   Histo result(a);
 | 
|---|
 | 198 |   return (result *= b);
 | 
|---|
 | 199 | }
 | 
|---|
| [763] | 200 | 
 | 
|---|
| [1092] | 201 | /*! \ingroup HiStats \fn operator*(r_8,const Histo&)
 | 
|---|
| [1053] | 202 |   \brief Operateur H2 = b * H1 */
 | 
|---|
| [1092] | 203 | inline Histo operator * (r_8 b, const Histo& a)
 | 
|---|
| [1053] | 204 | {
 | 
|---|
 | 205 |   Histo result(a);
 | 
|---|
 | 206 |   return (result *= b);
 | 
|---|
 | 207 | }
 | 
|---|
 | 208 | 
 | 
|---|
| [1092] | 209 | /*! \ingroup HiStats \fn operator/(const Histo&,r_8)
 | 
|---|
| [1053] | 210 |   \brief Operateur H2 = H1 / b */
 | 
|---|
| [1092] | 211 | inline Histo operator / (const Histo& a, r_8 b)
 | 
|---|
| [1053] | 212 | {
 | 
|---|
 | 213 |   Histo result(a);
 | 
|---|
 | 214 |   return (result /= b);
 | 
|---|
 | 215 | }
 | 
|---|
 | 216 | 
 | 
|---|
| [1092] | 217 | /*! \ingroup HiStats \fn operator+(const Histo&,r_8)
 | 
|---|
| [1053] | 218 |   \brief Operateur H2 = H1 + b */
 | 
|---|
| [1092] | 219 | inline Histo operator + (const Histo& a, r_8 b)
 | 
|---|
| [1053] | 220 | {
 | 
|---|
 | 221 |   Histo result(a);
 | 
|---|
 | 222 |   return (result += b);
 | 
|---|
 | 223 | }
 | 
|---|
 | 224 | 
 | 
|---|
| [1092] | 225 | /*! \ingroup HiStats \fn operator+(r_8,const Histo&)
 | 
|---|
| [1053] | 226 |   \brief Operateur H2 = b + H1 */
 | 
|---|
| [1092] | 227 | inline Histo operator + (r_8 b, const Histo& a)
 | 
|---|
| [1053] | 228 | {
 | 
|---|
 | 229 |   Histo result(a);
 | 
|---|
 | 230 |   return (result += b);
 | 
|---|
 | 231 | }
 | 
|---|
 | 232 | 
 | 
|---|
| [1092] | 233 | /*! \ingroup HiStats \fn operator-(const Histo&,r_8)
 | 
|---|
| [1053] | 234 |   \brief Operateur H2 = H1 - b */
 | 
|---|
| [1092] | 235 | inline Histo operator - (const Histo& a, r_8 b)
 | 
|---|
| [1053] | 236 | {
 | 
|---|
 | 237 |   Histo result(a);
 | 
|---|
 | 238 |   return (result -= b);
 | 
|---|
 | 239 | }
 | 
|---|
 | 240 | 
 | 
|---|
| [1092] | 241 | /*! \ingroup HiStats \fn operator-(r_8,const Histo&)
 | 
|---|
| [1053] | 242 |   \brief Operateur H2 = b - H1 */
 | 
|---|
| [1092] | 243 | inline Histo operator - (r_8 b, const Histo& a)
 | 
|---|
| [1053] | 244 | {
 | 
|---|
 | 245 |   Histo result(a);
 | 
|---|
 | 246 |   result *= -1.;
 | 
|---|
 | 247 |   return (result += b);
 | 
|---|
 | 248 | }
 | 
|---|
 | 249 | 
 | 
|---|
 | 250 | /*! \ingroup HiStats \fn operator+(const Histo&,const Histo&)
 | 
|---|
 | 251 |   \brief Operateur H = H1 + H2 */
 | 
|---|
 | 252 | inline Histo operator + (const Histo& a, const Histo& b)
 | 
|---|
 | 253 | {
 | 
|---|
| [2507] | 254 | if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
 | 
|---|
| [1053] | 255 | Histo c(a);
 | 
|---|
 | 256 | return (c += b);
 | 
|---|
 | 257 | }
 | 
|---|
 | 258 | 
 | 
|---|
 | 259 | /*! \ingroup HiStats \fn operator-(const Histo&,const Histo&)
 | 
|---|
 | 260 |   \brief Operateur H = H1 - H2 */
 | 
|---|
 | 261 | inline Histo operator - (const Histo& a, const Histo& b)
 | 
|---|
 | 262 | {
 | 
|---|
| [2507] | 263 | if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
 | 
|---|
| [1053] | 264 | Histo c(a);
 | 
|---|
 | 265 | return (c -= b);
 | 
|---|
 | 266 | }
 | 
|---|
 | 267 | 
 | 
|---|
 | 268 | /*! \ingroup HiStats \fn operator*(const Histo&,const Histo&)
 | 
|---|
 | 269 |   \brief Operateur H = H1 * H2 */
 | 
|---|
 | 270 | inline Histo operator * (const Histo& a, const Histo& b)
 | 
|---|
 | 271 | {
 | 
|---|
| [2507] | 272 | if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
 | 
|---|
| [1053] | 273 | Histo c(a);
 | 
|---|
 | 274 | return (c *= b);
 | 
|---|
 | 275 | }
 | 
|---|
 | 276 | 
 | 
|---|
 | 277 | /*! \ingroup HiStats \fn operator/(const Histo&,const Histo&)
 | 
|---|
 | 278 |   \brief Operateur H = H1 / H2 */
 | 
|---|
 | 279 | inline Histo operator / (const Histo& a, const Histo& b)
 | 
|---|
 | 280 | {
 | 
|---|
| [2507] | 281 | if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
 | 
|---|
| [1053] | 282 | Histo c(a);
 | 
|---|
 | 283 | return (c /= b);
 | 
|---|
 | 284 | }
 | 
|---|
 | 285 | 
 | 
|---|
| [763] | 286 | } // Fin du namespace
 | 
|---|
 | 287 | 
 | 
|---|
 | 288 | #endif // HISTOS_SEEN
 | 
|---|