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

Last change on this file since 3263 was 3198, checked in by cmv, 18 years ago

add methods Sum(),Sum2(),SumN() in HistoErr cmv 03/04/2007

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