source: Sophya/trunk/SophyaLib/HiStats/histerr.h@ 3819

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

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

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