source: Sophya/trunk/SophyaLib/HiStats/hist2err.h@ 3147

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

correct bug, intro ToVariance et changement nom ToCorrel->ToMean cmv 18/01/2007

File size: 5.5 KB
Line 
1#ifndef HIST2ERR_SEEN
2#define HIST2ERR_SEEN
3
4#include "objfio.h"
5#include <iostream>
6#include <stdio.h>
7#include "tmatrix.h"
8
9namespace SOPHYA {
10
11// Forward class declaration for Fits handler
12template <class T> class FitsHandler;
13
14//! 2 dimensions histograms with errors given by user
15class Histo2DErr : public AnyDataObj {
16 friend class ObjFileIO<Histo2DErr>;
17 friend class FitsHandler<Histo2DErr>;
18public:
19
20 //! Create or destroy
21 Histo2DErr(void);
22 Histo2DErr(r_8 xmin,r_8 xmax,int_4 nx,r_8 ymin,r_8 ymax,int_4 ny);
23 Histo2DErr(const Histo2DErr& H);
24 virtual ~Histo2DErr(void);
25
26 //! Updating or Setting
27 void ReCenterBinX(void);
28 void ReCenterBinY(void);
29 void ReCenterBin(void);
30 void Zero(void);
31
32 //! Getting Info
33 int_4 NBinX(void) {return nx_;}
34 int_4 NBinY(void) {return ny_;}
35 r_8 XMin(void) {return xmin_;}
36 r_8 YMin(void) {return ymin_;}
37 r_8 XMax(void) {return xmax_;}
38 r_8 YMax(void) {return ymax_;}
39 r_8 WBinX(void) {return dx_;}
40 r_8 WBinY(void) {return dy_;}
41
42 //! Retourne le contenu du bin
43 inline r_8 operator()(int_4 i,int_4 j) const
44 {
45 if(i<0 || i>=nx_ || j<0 || j>=ny_) return 0.;
46 return data_(i,j);
47 }
48 inline r_8& operator()(int_4 i,int_4 j)
49 {
50 return data_(i,j);
51 }
52
53 //! Retourne le nombre l'erreur au carre dans le bin
54 inline r_8 Error2(int_4 i,int_4 j) const
55 {
56 if(i<0 || i>=nx_ || j<0 || j>=ny_) return 0.;
57 return err2_(i,j);
58 }
59 inline r_8 Error(int_4 i,int_4 j) const
60 {
61 if(i<0 || i>=nx_ || j<0 || j>=ny_) return 0.;
62 if(err2_(i,j)>0.) return sqrt(err2_(i,j)); else return 0.;
63 }
64
65 //! Retourne le nombre d'entree dans le bin
66 inline r_8 NEntBin(int_4 i,int_4 j) const
67 {
68 if(i<0 || i>=nx_ || j<0 || j>=ny_) return 0.;
69 return ndata_(i,j);
70 }
71
72 //! Retourne l'abscisse et l'ordonnee du coin inferieur du bin i,j.
73 inline void BinLowEdge(int_4 i,int_4 j,r_8& x,r_8& y) const
74 {x = xmin_ + i*dx_; y = ymin_ + j*dy_;}
75 //! Retourne l'abscisse et l'ordonnee du centre du bin i,j.
76 inline void BinCenter(int_4 i,int_4 j,r_8& x,r_8& y) const
77 {x = xmin_ + (i+0.5)*dx_; y = ymin_ + (j+0.5)*dy_;}
78 //! Retourne l'abscisse et l'ordonnee du coin superieur du bin i,j.
79 inline void BinHighEdge(int_4 i,int_4 j,r_8& x,r_8& y) const
80 {x = xmin_ + (i+1)*dx_; y = ymin_ + (j+1)*dy_;}
81 //! Retourne les numeros du bin contenant l'abscisse et l'ordonnee x,y.
82 inline void FindBin(r_8 x,r_8 y,int_4& i,int_4& j) const
83 {i=(int_4) floor((x-xmin_)/dx_); j=(int_4) floor((y-ymin_)/dy_);}
84
85 //! Addition du contenu de l'histo pour abscisse x poids w et l'erreur e2
86 inline void Add(r_8 x, r_8 y, r_8 w, r_8 e)
87 {
88 int_4 i,j; FindBin(x,y,i,j);
89 if(i<0 || i>=nx_ || j<0 || j>=ny_) return;
90 data_(i,j) += w; ndata_(i,j) += 1.; err2_(i,j) += e*e;
91 }
92 inline void Add(r_8 x,r_8 y, r_8 w) {Add(x,y,w,w);}
93
94 //! Addition du contenu de l'histo pour le bin numBin poids w et l'erreur e
95 inline void AddBin(int_4 i,int_4 j, r_8 w, r_8 e)
96 {
97 if(i<0 || i>=nx_ || j<0 || j>=ny_) return;
98 data_(i,j) += w; ndata_(i,j) += 1.; err2_(i,j) += e*e;
99 }
100 inline void AddBin(int_4 i,int_4 j, r_8 w) {AddBin(i,j,w,w);}
101
102 //! remplissage contenu de l'histo pour le bin i poids w et l'erreur e
103 inline void SetBin(int_4 i,int_4 j, r_8 w, r_8 e, r_8 nb)
104 {
105 if(i<0 || i>=nx_ || j<0 || j>=ny_) return;
106 data_(i,j) = w;
107 err2_(i,j) = e*e;
108 ndata_(i,j) = nb;
109 }
110 //! remplissage de la valeur pour le bin i
111 inline void SetBin(int_4 i,int_4 j, r_8 w)
112 {
113 if(i<0 || i>=nx_ || j<0 || j>=ny_) return;
114 data_(i,j) = w;
115 }
116 //! remplissage de l'erreur carree pour le bin i
117 void SetErr2(int_4 i,int_4 j, r_8 e2)
118 {
119 if(i<0 || i>=nx_) return;
120 err2_(i,j) = e2;
121 }
122 //! remplissage nombre d'entrees pour le bin i
123 void SetNentB(int_4 i,int_4 j, r_8 nb)
124 {
125 if(i<0 || i>=nx_ || j<0 || j>=ny_) return;
126 ndata_(i,j) = nb;
127 }
128
129 //! Compute the Mean histogram
130 void ToMean(void);
131 void FromMean(void);
132 int_4 NMean(void) {return mMean;}
133 void SetMean(int_4 nmean) {mMean = nmean;}
134
135 //! Replace the errors by the variance
136 void ToVariance(void);
137 void FromVariance(void);
138
139 //! Fill an histogram with an histogram
140 void FillFrHErr(Histo2DErr& hfrom);
141
142 //! Recuperation des matrices elementaires
143 void GetData(TMatrix<r_8>& data) {data = data_;}
144 void GetError2(TMatrix<r_8>& err2) {err2 = err2_;}
145 void GetNData(TMatrix<r_8>& ndata) {ndata = ndata_;}
146
147 // Operators
148 Histo2DErr& operator = (const Histo2DErr& h);
149 Histo2DErr& operator *= (r_8 b);
150
151 // Print
152 void Show(ostream& os) const;
153 void Show() const { Show(cout); }
154
155protected:
156 void CreateOrResize(r_8 xmin,r_8 xmax,int_4 nx,r_8 ymin,r_8 ymax,int_4 ny);
157
158 r_4 xmin_,xmax_,dx_,ymin_,ymax_,dy_;
159 int_4 nx_,ny_;
160 TMatrix<r_8> data_;
161 TMatrix<r_8> err2_;
162 TMatrix<r_8> ndata_;
163 int_4 mMean; //!< Nombre d'appels a ToMean/Variance(+1) ou FromMean/Variance(-1)
164};
165
166/*! Prints histogram information on stream \b s (h.Show(s)) */
167inline ostream& operator << (ostream& s, Histo2DErr const & h)
168 { h.Show(s); return(s); }
169
170/*! \ingroup HiStats \fn operator<<(POuttPersist&,Histo2DErr)
171 \brief Persistance management */
172inline POutPersist& operator << (POutPersist& os, Histo2DErr & obj)
173{ ObjFileIO<Histo2DErr> fio(&obj); fio.Write(os); return(os); }
174/*! \ingroup HiStats \fn operator<<(POuttPersist&,Histo2DErr)
175 \brief Persistance management */
176inline PInPersist& operator >> (PInPersist& is, Histo2DErr & obj)
177{ ObjFileIO<Histo2DErr> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
178
179} // Fin du namespace
180
181#endif // HIST2ERR_SEEN
Note: See TracBrowser for help on using the repository browser.