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