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

Last change on this file since 4051 was 3307, checked in by cmv, 18 years ago

methode ReCenterBinW binwidth kept, number of bin incresed cmv 22/08/2007

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