00001
00002
00003
00004
00005
00006 #include <stdio.h>
00007 #include <math.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include <memory.h>
00011 #include "gl3Histo.h"
00012
00013
00014
00016
00017
00018 gl3Histo::gl3Histo ( char iId[10], char iTitle[100],
00019 int iNBins, double iXMin, double iXMax ) {
00020 info = 0 ;
00021 if ( iNBins < 0 ) {
00022 fprintf ( stderr, " %d bins, not valid value \n", (int)iNBins );
00023 return ;
00024 }
00025 header.nBins = iNBins ;
00026 if ( iXMin > iXMax ) {
00027 fprintf ( stderr, " xMin %e xMax %e, not valid values \n", iXMin, iXMax );
00028 return ;
00029 }
00030 strcpy ( header.id, iId ) ;
00031 strcpy ( header.title, iTitle ) ;
00032 header.nBins = iNBins ;
00033 header.xMin = iXMin ;
00034 header.xMax = iXMax ;
00035 header.nEntries = 0 ;
00036 header.sum = 0 ;
00037 header.yMin = header.yMax = 0. ;
00038 header.xStep = (header.xMax-header.xMin)/float(header.nBins);
00039 header.maxBin = 0;
00040
00041 info = new double[header.nBins+2];
00042 memset ( info, 0, (header.nBins+2)*sizeof(double) ) ;
00043 }
00045
00046
00047 gl3Histo::~gl3Histo ( ) {
00048 if ( info ) delete[] info ;
00049 }
00051
00052
00053 int gl3Histo::Fill ( double x, double weight ) {
00054 int iBin = (int)((x-header.xMin)/header.xStep)+1 ;
00055 if ( iBin < 1 ) iBin = 0 ;
00056 else if ( iBin > header.nBins ) iBin = header.nBins + 1 ;
00057
00058 info[iBin] += weight ;
00059 if ( iBin > 1 && iBin < header.nBins + 1 ) {
00060 if ( info[iBin] > header.yMax ) {
00061 header.yMax = info[iBin] ;
00062 header.maxBin= iBin ;
00063 }
00064 if ( info[iBin] < header.yMin ) header.yMin = info[iBin] ;
00065 }
00066 header.sum += weight ;
00067 header.nEntries++ ;
00068 return 0 ;
00069 }
00070
00071
00073
00074
00075 double gl3Histo::GetY ( int iBin ) {
00076 if ( iBin < 0 || iBin > header.nBins+2 ) {
00077 printf("out of range\n");
00078 return 0 ;
00079 }
00080 return info[iBin] ;
00081 }
00083
00084
00085 int gl3Histo::Print ( short level ) {
00086 printf ( "gl3Histo::Print: id \"%s\" title \"%s\" nBins %d nEntries %d\n",
00087 header.id, header.title,
00088 (int)header.nBins, (int)header.nEntries ) ;
00089 return 0 ;
00090 }
00092
00093
00094 int gl3Histo::Read ( char* input ) {
00095
00096
00097
00098
00099
00100
00101
00102 memcpy ( &header, input, sizeof(gl3HistoHeader) ) ;
00103
00104 if ( info ) delete info ;
00105 info = new double[header.nBins+2];
00106
00107 memcpy ( info, input+sizeof(gl3HistoHeader),
00108 (header.nBins+2)*sizeof(double) ) ;
00109
00110
00111
00112
00113
00114
00115
00116 return sizeof(gl3HistoHeader) + (header.nBins+2)*sizeof(double);
00117 }
00119
00120
00121 int gl3Histo::Reset ( ) {
00122 header.nEntries = 0 ;
00123 header.sum = 0 ;
00124 header.maxBin = 0;
00125 header.yMin = 0;
00126 header.yMax = 0;
00127 memset ( info, 0, (header.nBins+2)*sizeof(double) ) ;
00128 return 0 ;
00129 }
00130
00132
00133
00134 int gl3Histo::Write ( unsigned int maxBytes, char* output ) {
00135
00136 if ( (sizeof(gl3HistoHeader) + (header.nBins+2)*sizeof(double))
00137 >= maxBytes ) {
00138
00139 printf("gl3Histo::Write %d bytes in buffer not enough \n", maxBytes);
00140 return 0 ;
00141 }
00142
00143 memcpy ( output, &header, sizeof(gl3HistoHeader) ) ;
00144 memcpy ( output + sizeof(gl3HistoHeader), info,
00145 (header.nBins+2)*sizeof(double) );
00146
00147
00148
00149
00150
00151 return sizeof(gl3HistoHeader) + (header.nBins+2)*sizeof(double);
00152 }
00153
00154
00156
00157
00158 double gl3Histo::GetMaximum()
00159 {
00160 return header.yMax;
00161 }
00162
00164
00165
00166 int gl3Histo::GetMaximumBin()
00167 {
00168 return header.maxBin;
00169 }
00170
00172
00173
00174 double gl3Histo::GetBinCenter(int Bin)
00175 {
00176 return(header.xMin+(float(Bin-0.5)*header.xStep)) ;
00177 }
00178
00180
00181
00182 double gl3Histo::Integral(int minBin, int maxBin)
00183 {
00184 double sum=0;
00185
00186
00187
00188
00189 if(minBin>=0 && maxBin<header.nBins) {
00190 for(int cnt=minBin; cnt<=maxBin; cnt++) sum += GetY(cnt);
00191 return sum;
00192 }
00193 else return 0;
00194 }
00195
00196
00197
00198
00199
00200
00201 double gl3Histo::getWeightedMean(double sigmaWidthBins)
00202 {
00203
00204
00205
00206
00207 double HistoMax = GetMaximum();
00208 int HistoMaxBin = GetMaximumBin();
00209 double HistoMaxCenter = GetBinCenter(HistoMaxBin);
00210
00211 double SigmaHalfL = Integral((int)(HistoMaxBin-sigmaWidthBins),HistoMaxBin-1);
00212 double SigmaHalfR = Integral(HistoMaxBin+1,(int)(HistoMaxBin+sigmaWidthBins));
00213 double SigmaHalfLCenter=0;
00214
00215 for(int cnt=HistoMaxBin-1; cnt>=(HistoMaxBin-sigmaWidthBins); cnt--) {
00216 SigmaHalfLCenter = SigmaHalfLCenter + GetBinCenter(cnt);
00217 }
00218 SigmaHalfLCenter=SigmaHalfLCenter/sigmaWidthBins;
00219
00220 double SigmaHalfRCenter=0;
00221 for(int cnt=HistoMaxBin+1; cnt<=(HistoMaxBin+sigmaWidthBins); cnt++) {
00222 SigmaHalfRCenter = SigmaHalfRCenter+GetBinCenter(cnt);
00223 }
00224 SigmaHalfRCenter=SigmaHalfRCenter/sigmaWidthBins;
00225
00226 double weightedMean;
00227 if((SigmaHalfL+HistoMax+SigmaHalfR)>0) {
00228 weightedMean = ( (SigmaHalfL * SigmaHalfLCenter) +
00229 (HistoMax * HistoMaxCenter) +
00230 (SigmaHalfR * SigmaHalfRCenter) )
00231 / (SigmaHalfL+HistoMax+SigmaHalfR);
00232 }
00233 else weightedMean=0.0;
00234
00235 return(weightedMean);
00236 }