source: Sophya/trunk/SophyaLib/HiStats/histos.h@ 3844

Last change on this file since 3844 was 3110, checked in by cmv, 19 years ago

ajout methodes Sum et Sum2 cmv 20/11/2006

File size: 8.9 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2//
3// $Id: histos.h,v 1.26 2006-11-20 13:15:35 cmv Exp $
4//
5
6#ifndef HISTOS_SEEN
7#define HISTOS_SEEN
8
9#include "objfio.h"
10#include <iostream>
11#include <stdio.h>
12#include "peida.h"
13#include "tvector.h"
14#include "ppersist.h"
15#include "anydataobj.h"
16
17namespace SOPHYA {
18
19// Forward class declaration for Fits handler
20template <class T> class FitsHandler;
21
22//! 1 dimension histograms
23class Histo : public AnyDataObj {
24 friend class ObjFileIO<Histo>;
25 friend class FitsHandler<Histo>;
26public:
27
28 // CREATOR / DESTRUCTOR
29 Histo();
30 Histo(r_8 xMin, r_8 xMax, int_4 nBin=100);
31 Histo(r_4 xMin, r_4 xMax, int_4 nBin=100);
32 Histo(const Histo& H);
33 virtual ~Histo();
34
35 // OPTIONS
36 void Errors();
37 void ReCenterBin();
38
39 // UPDATING or SETTING
40 void Zero();
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.);}
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);
53 virtual void UpdateHisto(bool force=false) const;
54
55 // Operators
56 Histo& operator = (const Histo& h);
57 Histo& operator *= (r_8 b);
58 Histo& operator /= (r_8 b);
59 Histo& operator += (r_8 b);
60 Histo& operator -= (r_8 b);
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
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;
71 void PutValue(TVector<r_8>& v, int_4 ierr=0);
72 void PutValueAdd(TVector<r_8> &v, int_4 ierr=0);
73 void PutError2(TVector<r_8>& v);
74 void PutError2Add(TVector<r_8>& v);
75 void PutError(TVector<r_8>& v);
76
77 // INLINES
78 //! Retourne l'abscisse minimum
79 inline r_8 XMin() const {return mMin;}
80 //! Retourne l'abscisse maximum
81 inline r_8 XMax() const {return mMax;}
82 //! Retourne le nombre de bins
83 inline int_4 NBins() const {return mBins;}
84 //! Retourne la largeur du bin
85 inline r_8 BinWidth() const {return binWidth;}
86 //! Retourne le pointeur sur le tableaux des contenus
87 inline r_8* Bins() const {return mData;}
88 //! Retourne le contenu du bin i
89 inline r_8 operator()(int_4 i) const {return mData[i];}
90 //! Remplit le contenu du bin i
91 inline r_8& operator()(int_4 i) {return mData[i];}
92 //! retourne "true" si il y a des erreurs stoquees
93 inline bool HasErrors() const
94 {if(mErr2) return true; else return false;}
95 //! Retourne l'erreur du bin i
96 inline r_8 Error(int_4 i) const
97 {if(mErr2) {if(mErr2[i]>0.) return sqrt(mErr2[i]); else return 0.;}
98 else return 0.;}
99 //! Retourne l'erreur au carre du bin i
100 inline r_8 Error2(int_4 i) const
101 {if(mErr2) return mErr2[i]; else return 0.;}
102 //! Remplit l'erreur au carre du bin i
103 inline r_8& Error2(int_4 i) {return mErr2[i];}
104 //! Retourne la somme ponderee
105 inline r_8 NData() const {return nHist;}
106 //! Retourne le nombre d'entrees
107 inline uint_8 NEntries() const {return nEntries;}
108 //! Retourne le nombre d'overflow
109 inline r_8 NOver() const {return mOver;}
110 //! Retourne le nombre d'underflow
111 inline r_8 NUnder() const {return mUnder;}
112
113 //! Retourne l'abscisse du bord inferieur du bin i
114 inline r_8 BinLowEdge(int_4 i) const {return mMin + i*binWidth;}
115 //! Retourne l'abscisse du centre du bin i
116 inline r_8 BinCenter(int_4 i) const {return mMin + (i+0.5)*binWidth;}
117 //! Retourne l'abscisse du bord superieur du bin i
118 inline r_8 BinHighEdge(int_4 i) const {return mMin + (i+1)*binWidth;}
119 //! Retourne le numero du bin contenant l'abscisse x
120 inline int_4 FindBin(r_8 x) const
121 {return (int_4) floor((x - mMin) / binWidth);}
122
123 // Info, statistique et calculs sur les histogrammes
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 Sum() const;
131 r_8 Sum2() const;
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;
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;
143 void HInteg(r_8 norm = 0.);
144 void HDeriv();
145 virtual void HRebin(int_4 nbinew);
146
147 int_4 MaxiLocal(r_8& maxi,int_4& imax,r_8& maxn,int_4& imaxn) const;
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;
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;
154
155 // Print et Display ASCII
156 virtual void Show(ostream& os) const;
157 inline void Show() const { Show(cout); }
158 void Print(int_4 dyn = 100, r_8 hmin = 1., r_8 hmax = -1.,
159 int_4 pflag = 0, int_4 il = 1, int_4 ih = -1) const;
160
161protected:
162 void CreateOrResize(r_8 xMin, r_8 xMax, int_4 nBin);
163 void Delete(); // Ne pas passer en "virtual"
164
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
170 uint_8 nEntries; //!< nombre d'entrees
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
175};
176
177/*! Prints histogram information on stream \b s (h.Show(s)) */
178inline ostream& operator << (ostream& s, Histo const & h)
179 { h.Show(s); return(s); }
180
181/*! \ingroup HiStats \fn operator<<(POuttPersist&,Histo)
182 \brief Persistance management */
183inline POutPersist& operator << (POutPersist& os, Histo & obj)
184{ ObjFileIO<Histo> fio(&obj); fio.Write(os); return(os); }
185/*! \ingroup HiStats \fn operator<<(POuttPersist&,Histo)
186 \brief Persistance management */
187inline PInPersist& operator >> (PInPersist& is, Histo & obj)
188{ ObjFileIO<Histo> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
189
190// Classe pour la gestion de persistance
191// ObjFileIO<Histo>
192
193/*! \ingroup HiStats \fn operator*(const Histo&,r_8)
194 \brief Operateur H2 = H1 * b */
195inline Histo operator * (const Histo& a, r_8 b)
196{
197 Histo result(a);
198 return (result *= b);
199}
200
201/*! \ingroup HiStats \fn operator*(r_8,const Histo&)
202 \brief Operateur H2 = b * H1 */
203inline Histo operator * (r_8 b, const Histo& a)
204{
205 Histo result(a);
206 return (result *= b);
207}
208
209/*! \ingroup HiStats \fn operator/(const Histo&,r_8)
210 \brief Operateur H2 = H1 / b */
211inline Histo operator / (const Histo& a, r_8 b)
212{
213 Histo result(a);
214 return (result /= b);
215}
216
217/*! \ingroup HiStats \fn operator+(const Histo&,r_8)
218 \brief Operateur H2 = H1 + b */
219inline Histo operator + (const Histo& a, r_8 b)
220{
221 Histo result(a);
222 return (result += b);
223}
224
225/*! \ingroup HiStats \fn operator+(r_8,const Histo&)
226 \brief Operateur H2 = b + H1 */
227inline Histo operator + (r_8 b, const Histo& a)
228{
229 Histo result(a);
230 return (result += b);
231}
232
233/*! \ingroup HiStats \fn operator-(const Histo&,r_8)
234 \brief Operateur H2 = H1 - b */
235inline Histo operator - (const Histo& a, r_8 b)
236{
237 Histo result(a);
238 return (result -= b);
239}
240
241/*! \ingroup HiStats \fn operator-(r_8,const Histo&)
242 \brief Operateur H2 = b - H1 */
243inline Histo operator - (r_8 b, const Histo& a)
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 */
252inline Histo operator + (const Histo& a, const Histo& b)
253{
254if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
255Histo c(a);
256return (c += b);
257}
258
259/*! \ingroup HiStats \fn operator-(const Histo&,const Histo&)
260 \brief Operateur H = H1 - H2 */
261inline Histo operator - (const Histo& a, const Histo& b)
262{
263if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
264Histo c(a);
265return (c -= b);
266}
267
268/*! \ingroup HiStats \fn operator*(const Histo&,const Histo&)
269 \brief Operateur H = H1 * H2 */
270inline Histo operator * (const Histo& a, const Histo& b)
271{
272if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
273Histo c(a);
274return (c *= b);
275}
276
277/*! \ingroup HiStats \fn operator/(const Histo&,const Histo&)
278 \brief Operateur H = H1 / H2 */
279inline Histo operator / (const Histo& a, const Histo& b)
280{
281if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
282Histo c(a);
283return (c /= b);
284}
285
286} // Fin du namespace
287
288#endif // HISTOS_SEEN
Note: See TracBrowser for help on using the repository browser.