StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
gl3Histo.cxx
1 //:>------------------------------------------------------------------
2 //: FILE: gl3Tracks.h
3 //: HISTORY:
4 //: 6dec1999 version 1.00
5 //:<------------------------------------------------------------------
6 #include <stdio.h>
7 #include <math.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <memory.h>
11 
12 #include "Stl3Util/gl3/gl3Histo.h"
13 
14 
15 
17 //
18 //####################################################################################
19 gl3Histo::gl3Histo (const char iId[10], const char iTitle[100],
20  int iNBins, double iXMin, double iXMax ) {
21  info = 0 ;
22  if ( iNBins < 0 ) {
23  fprintf ( stderr, " %d bins, not valid value \n", (int)iNBins );
24  return ;
25  }
26  header.nBins = iNBins ;
27  if ( iXMin > iXMax ) {
28  fprintf ( stderr, " xMin %e xMax %e, not valid values \n", iXMin, iXMax );
29  return ;
30  }
31  strcpy ( header.id, iId ) ;
32  strcpy ( header.title, iTitle ) ;
33  header.nBins = iNBins ;
34  header.xMin = iXMin ;
35  header.xMax = iXMax ;
36  header.nEntries = 0 ;
37  header.sum = 0 ;
38  header.yMin = header.yMax = 0. ;
39  header.xStep = (header.xMax-header.xMin)/float(header.nBins);
40  header.maxBin = 0;
41 
42  info = new double[header.nBins+2];
43  memset ( info, 0, (header.nBins+2)*sizeof(double) ) ;
44 }
46 //
47 //####################################################################################
49  if ( info ) delete[] info ;
50 }
52 //
53 //####################################################################################
54 int gl3Histo::Fill ( double x, double weight ) {
55  int iBin = (int)((x-header.xMin)/header.xStep)+1 ;
56  if ( iBin < 1 ) iBin = 0 ;
57  else if ( iBin > header.nBins ) iBin = header.nBins + 1 ;
58  info[iBin] += weight ;
59  if ( iBin > 1 && iBin < header.nBins + 1 ) {
60  if ( info[iBin] > header.yMax )
61  {
62  header.yMax = info[iBin] ;
63  header.maxBin= iBin ;
64  }
65  if ( info[iBin] < header.yMin ) header.yMin = info[iBin] ;
66  }
67  header.sum += weight ;
68  header.nEntries++ ;
69  return 0 ;
70 }
72 //
73 //####################################################################################
74 double gl3Histo::GetY ( int iBin ) {
75  if ( iBin < 0 || iBin > header.nBins+2 ) {
76  printf("out of range\n");
77  return 0 ;
78  }
79  return info[iBin] ;
80 }
82 //
83 //####################################################################################
84 int gl3Histo::Print ( short level ) {
85  printf ( "gl3Histo::Print: id %s title %s nBins %d \n",
86  header.id, header.title, (int)header.nBins ) ;
87  return 0 ;
88 }
90 //
91 //#####################################################################
92 int gl3Histo::Read ( char* input ) {
93 
94 // printf("%x / %x / %x\n",
95 // sizeof(gl3HistoHeader),
96 // ((gl3HistoHeader*)input)->nBins,
97 // ( sizeof(gl3HistoHeader) +
98 // sizeof(double) * (((gl3HistoHeader*)input)->nBins+2)));
99 
100  memcpy ( &header, input, sizeof(gl3HistoHeader) ) ;
101 
102  if ( info ) delete info ;
103  info = new double[header.nBins+2];
104 
105  memcpy ( info, input+sizeof(gl3HistoHeader),
106  (header.nBins+2)*sizeof(double) ) ;
107 
108  // printf ( "length 2 %d \n", length_2 ) ;
109 
110 // for (int i=0; i<header.nBins+2; i++) {
111 // printf("%d: %f\n", i, info[i]);
112 // }
113 
114  return sizeof(gl3HistoHeader) + (header.nBins+2)*sizeof(double);
115 }
117 //
118 //##########################################################################
119 int gl3Histo::Reset ( ) {
120  header.nEntries = 0 ;
121  header.sum = 0 ;
122  header.maxBin = 0;
123  header.yMin = 0;
124  header.yMax = 0;
125  memset ( info, 0, (header.nBins+2)*sizeof(double) ) ;
126  return 0 ;
127 }
128 
130 //
131 //#########################################################################
132 int gl3Histo::Write ( int maxBytes, char* output ) {
133 
134  if ( (sizeof(gl3HistoHeader) + (header.nBins+2)*sizeof(double))
135  >= (size_t)maxBytes ) {
136 
137  printf("gl3Histo::Write %d bytes in buffer not enough \n", maxBytes);
138  return 0 ;
139  }
140 
141  memcpy ( output, &header, sizeof(gl3HistoHeader) ) ;
142  memcpy ( output + sizeof(gl3HistoHeader), info,
143  (header.nBins+2)*sizeof(double) );
144 
145 // for (int i=0; i<header.nBins+2; i++) {
146 // printf("%d: %f\n", i, info[i]);
147 // }
148 
149  return sizeof(gl3HistoHeader) + (header.nBins+2)*sizeof(double);
150 }
151 
152 // JB 08/15/2K added some methods
154 //
155 //####################################################################################
156 double gl3Histo::GetMaximum()
157 {
158  return header.yMax;
159 }
160 
162 //
163 //####################################################################################
165 {
166  return header.maxBin;
167 }
168 
170 //
171 //####################################################################################
172 double gl3Histo::GetBinCenter(int Bin)
173 {
174  return(header.xMin+(float(Bin-0.5)*header.xStep)) ;
175 }
176 
178 //
179 //####################################################################################
180 double gl3Histo::Integral(int minBin, int maxBin)
181 {
182  double sum=0;
183  for(int cnt=minBin; cnt<=maxBin; cnt++) sum += GetY(cnt);
184  return sum;
185 }
186 
187 
188 
189 //####################################################################
190 // calculates (fast) the fuckin weighted mean of a gl3Histo
191 //####################################################################
192 double gl3Histo::getWeightedMean(double sigmaWidthBins)
193 {
194  // Weighted mean of Histo
195 
196  // suggestion
197  // sigmaWidthBins = 4;
198  double HistoMax = GetMaximum();
199  int HistoMaxBin = GetMaximumBin();
200  double HistoMaxCenter = GetBinCenter(HistoMaxBin);
201 
202  double SigmaHalfL = Integral(int(HistoMaxBin-sigmaWidthBins),HistoMaxBin-1);
203  double SigmaHalfR = Integral(HistoMaxBin+1,int(HistoMaxBin+sigmaWidthBins));
204  double SigmaHalfLCenter=0;
205 
206  for(int cnt=HistoMaxBin-1; cnt>=(HistoMaxBin-sigmaWidthBins); cnt--) {
207  SigmaHalfLCenter = SigmaHalfLCenter + GetBinCenter(cnt);
208  }
209  SigmaHalfLCenter=SigmaHalfLCenter/sigmaWidthBins;
210 
211  double SigmaHalfRCenter=0;
212  for(int cnt=HistoMaxBin+1; cnt<=(HistoMaxBin+sigmaWidthBins); cnt++) {
213  SigmaHalfRCenter = SigmaHalfRCenter+GetBinCenter(cnt);
214  }
215  SigmaHalfRCenter=SigmaHalfRCenter/sigmaWidthBins;
216 
217  double weightedMean;
218  if((SigmaHalfL+HistoMax+SigmaHalfR)>0) {
219  weightedMean = ( (SigmaHalfL * SigmaHalfLCenter) +
220  (HistoMax * HistoMaxCenter) +
221  (SigmaHalfR * SigmaHalfRCenter) )
222  / (SigmaHalfL+HistoMax+SigmaHalfR);
223  }
224  else weightedMean=0.0; // CAUTION METHOD RETURNS 0.0 IF IT FAILS
225 
226  return(weightedMean);
227 }
double GetMaximum()
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ...
Definition: gl3Histo.cxx:158
int Reset()
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Definition: gl3Histo.cxx:121
double GetBinCenter(int Bin)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ...
Definition: gl3Histo.cxx:174
int Write(unsigned int maxBytes, char *output)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Definition: gl3Histo.cxx:134
double Integral(int minBin, int maxBin)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ...
Definition: gl3Histo.cxx:182
~gl3Histo()
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ...
Definition: gl3Histo.cxx:47
double GetY(int iBin)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ...
Definition: gl3Histo.cxx:75
int Print(short Level=1)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ...
Definition: gl3Histo.cxx:85
gl3Histo(char iId[10]="id", char iTitle[100]="name", int iNBins=100, double iXMin=0., double iXMax=100.)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ...
Definition: gl3Histo.cxx:18
int GetMaximumBin()
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ...
Definition: gl3Histo.cxx:166
int Read(char *input)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Definition: gl3Histo.cxx:94
int Fill(double x, double weight=1)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ...
Definition: gl3Histo.cxx:53