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