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