CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/Utilities/BinningTools/src/ClusterizingHistogram.cc

Go to the documentation of this file.
00001 #include "Utilities/BinningTools/interface/ClusterizingHistogram.h"
00002 #include <iostream>
00003 
00004 using namespace std;
00005 
00006 ClusterizingHistogram::ClusterizingHistogram(int nb, float xmi, float xma) : 
00007   my_nbins(nb), xmin(xmi), xmax(xma), my_entries(0), my_underflows(0), my_overflows(0) {
00008     bin_entries = new int[my_nbins];
00009     bin_means = new float[my_nbins];
00010     binsiz = (xmax-xmin) / my_nbins;
00011     for (int i=0; i<my_nbins; i++) {
00012       bin_entries[i] = 0;
00013       bin_means[i] = 0.;
00014     }
00015 }
00016 ClusterizingHistogram::~ClusterizingHistogram(){ delete[] bin_entries; delete [] bin_means;}
00017 
00018 int ClusterizingHistogram::bin( float x) const {
00019   if      (x < xmin) return -1;
00020   else if (x > xmax) return my_nbins;
00021   else return int((x-xmin)/binsiz);
00022 }
00023 int ClusterizingHistogram::bin( double x) const {
00024   if      (x < xmin) return -1;
00025   else if (x > xmax) return my_nbins;
00026   else return int((x-xmin)/binsiz);
00027 }
00028 
00029 void ClusterizingHistogram::fill( float x) {
00030   if      (x < xmin) my_underflows++;
00031   else if (x > xmax) my_overflows++;
00032   else {
00033     int bin = int((x-xmin)/binsiz);
00034     if ( bin > my_nbins-1) bin = my_nbins-1;
00035     ++bin_entries[bin];
00036     bin_means[bin] += x;
00037     my_entries++;
00038     // may be problematic for negative x; check!
00039   }
00040 }
00041 
00042 vector<float> ClusterizingHistogram::clusterize( float resol) {
00043   vector<float> clust;
00044   int nclust = 0;
00045   bool inclust = false;
00046   float last_pos = xmin - 1000.*resol;
00047   int sum = 0;
00048   float sumx = 0;
00049   for (int i=0; i<my_nbins; i++) {
00050     if (bin_entries[i] != 0) {
00051       if ( fabs(bin_pos(i)-last_pos) > resol) {
00052         inclust = false;
00053         if (nclust != 0) clust.push_back( sumx/sum);  // create cluster
00054       }
00055       if (!inclust) {
00056         nclust++;
00057         sumx = 0.;
00058         sum = 0;
00059       }
00060       sum += bin_entries[i];
00061       sumx += bin_means[i];
00062       last_pos = bin_pos(i);
00063       inclust = true;
00064     }
00065   }
00066   if (nclust != 0) clust.push_back( sumx/sum);  // create last cluster
00067   return clust;
00068 }
00069 
00070 
00071 void ClusterizingHistogram::dump() const { dump(0, my_nbins);}
00072 
00073 void ClusterizingHistogram::dump(int i1, int i2) const {
00074   cout << "Dumping ClusterizingHistogram contents:" << endl;
00075   for (int i=max(i1,0); i<min(i2,my_nbins); i++) {
00076     cout << i << "  " << bin_entries[i] << "   " << bin_pos(i) << endl;
00077   }
00078   cout << "Underflows: " << my_underflows << endl;
00079   cout << "Overflows:  " << my_overflows << endl;
00080   cout << "Total number of entries: " << my_entries << endl;
00081 }
00082 
00083 void ClusterizingHistogram::dump( float x1, float x2) const { dump( bin(x1), bin(x2));}
00084 void ClusterizingHistogram::dump( double x1, double x2) const { dump( bin(x1), bin(x2));}
00085 void ClusterizingHistogram::dump( float x1, double x2) const { dump( bin(x1), bin(x2));}
00086 void ClusterizingHistogram::dump( double x1, float x2) const { dump( bin(x1), bin(x2));}
00087 
00088 void ClusterizingHistogram::reset() {
00089   my_entries = 0;
00090   my_underflows = 0;
00091   my_overflows = 0;
00092   for (int i=0; i<my_nbins; i++) {
00093     bin_entries[i] = 0;
00094     bin_means[i] = 0.;
00095   }
00096 }