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

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

Fits IO Histo,HProf,HistErr,Histo2D cmv 11/8/2006

File size: 8.8 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2//
3// $Id: histos.h,v 1.22 2006-08-11 16:10:13 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(void);
37 void ReCenterBin(void);
38
39 // UPDATING or SETTING
40 void Zero(void);
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 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;
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;
141 void HInteg(r_8 norm = 0.);
142 void HDeriv();
143 virtual void HRebin(int_4 nbinew);
144
145 int_4 MaxiLocal(r_8& maxi,int_4& imax,r_8& maxn,int_4& imaxn) const;
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;
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;
152
153 // Print et Display ASCII
154 virtual void Show(ostream& os) const;
155 inline void Show() const { Show(cout); }
156 void Print(int_4 dyn = 100, r_8 hmin = 1., r_8 hmax = -1.,
157 int_4 pflag = 0, int_4 il = 1, int_4 ih = -1) const;
158
159protected:
160 void Delete();
161
162 r_8* mData; //!< donnees
163 r_8* mErr2; //!< erreurs carrees
164 r_8 mUnder; //!< underflow
165 r_8 mOver; //!< overflow
166 r_8 nHist; //!< somme ponderee des entrees
167 uint_8 nEntries; //!< nombre d'entrees
168 int_4 mBins; //!< nombre de bins
169 r_8 mMin; //!< abscisse minimum
170 r_8 mMax; //!< abscisse maximum
171 r_8 binWidth; //!< largeur du bin
172};
173
174/*! Prints histogram information on stream \b s (h.Show(s)) */
175inline ostream& operator << (ostream& s, Histo const & h)
176 { h.Show(s); return(s); }
177
178/*! \ingroup HiStats \fn operator<<(POuttPersist&,Histo)
179 \brief Persistance management */
180inline POutPersist& operator << (POutPersist& os, Histo & obj)
181{ ObjFileIO<Histo> fio(&obj); fio.Write(os); return(os); }
182/*! \ingroup HiStats \fn operator<<(POuttPersist&,Histo)
183 \brief Persistance management */
184inline PInPersist& operator >> (PInPersist& is, Histo & obj)
185{ ObjFileIO<Histo> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
186
187// Classe pour la gestion de persistance
188// ObjFileIO<Histo>
189
190/*! \ingroup HiStats \fn operator*(const Histo&,r_8)
191 \brief Operateur H2 = H1 * b */
192inline Histo operator * (const Histo& a, r_8 b)
193{
194 Histo result(a);
195 return (result *= b);
196}
197
198/*! \ingroup HiStats \fn operator*(r_8,const Histo&)
199 \brief Operateur H2 = b * H1 */
200inline Histo operator * (r_8 b, const Histo& a)
201{
202 Histo result(a);
203 return (result *= b);
204}
205
206/*! \ingroup HiStats \fn operator/(const Histo&,r_8)
207 \brief Operateur H2 = H1 / b */
208inline Histo operator / (const Histo& a, r_8 b)
209{
210 Histo result(a);
211 return (result /= b);
212}
213
214/*! \ingroup HiStats \fn operator+(const Histo&,r_8)
215 \brief Operateur H2 = H1 + b */
216inline Histo operator + (const Histo& a, r_8 b)
217{
218 Histo result(a);
219 return (result += b);
220}
221
222/*! \ingroup HiStats \fn operator+(r_8,const Histo&)
223 \brief Operateur H2 = b + H1 */
224inline Histo operator + (r_8 b, const Histo& a)
225{
226 Histo result(a);
227 return (result += b);
228}
229
230/*! \ingroup HiStats \fn operator-(const Histo&,r_8)
231 \brief Operateur H2 = H1 - b */
232inline Histo operator - (const Histo& a, r_8 b)
233{
234 Histo result(a);
235 return (result -= b);
236}
237
238/*! \ingroup HiStats \fn operator-(r_8,const Histo&)
239 \brief Operateur H2 = b - H1 */
240inline Histo operator - (r_8 b, const Histo& a)
241{
242 Histo result(a);
243 result *= -1.;
244 return (result += b);
245}
246
247/*! \ingroup HiStats \fn operator+(const Histo&,const Histo&)
248 \brief Operateur H = H1 + H2 */
249inline Histo operator + (const Histo& a, const Histo& b)
250{
251if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
252Histo c(a);
253return (c += b);
254}
255
256/*! \ingroup HiStats \fn operator-(const Histo&,const Histo&)
257 \brief Operateur H = H1 - H2 */
258inline Histo operator - (const Histo& a, const Histo& b)
259{
260if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
261Histo c(a);
262return (c -= b);
263}
264
265/*! \ingroup HiStats \fn operator*(const Histo&,const Histo&)
266 \brief Operateur H = H1 * H2 */
267inline Histo operator * (const Histo& a, const Histo& b)
268{
269if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
270Histo c(a);
271return (c *= b);
272}
273
274/*! \ingroup HiStats \fn operator/(const Histo&,const Histo&)
275 \brief Operateur H = H1 / H2 */
276inline Histo operator / (const Histo& a, const Histo& b)
277{
278if (b.NBins()!=a.NBins()) throw SzMismatchError(PExcLongMessage(""));
279Histo c(a);
280return (c /= b);
281}
282
283} // Fin du namespace
284
285#endif // HISTOS_SEEN
Note: See TracBrowser for help on using the repository browser.