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

Last change on this file since 4034 was 3950, checked in by cmv, 15 years ago

ajout d'une methode ReSize publique, cmv 18/02/2011

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