File indexing completed on 2023-03-17 11:26:45
0001 #include "Utilities/BinningTools/interface/ClusterizingHistogram.h"
0002 #include <iostream>
0003
0004 using namespace std;
0005
0006 ClusterizingHistogram::ClusterizingHistogram(int nb, float xmi, float xma)
0007 : my_nbins(nb), xmin(xmi), xmax(xma), my_entries(0), my_underflows(0), my_overflows(0) {
0008 bin_entries = new int[my_nbins];
0009 bin_means = new float[my_nbins];
0010 binsiz = (xmax - xmin) / my_nbins;
0011 for (int i = 0; i < my_nbins; i++) {
0012 bin_entries[i] = 0;
0013 bin_means[i] = 0.;
0014 }
0015 }
0016 ClusterizingHistogram::~ClusterizingHistogram() {
0017 delete[] bin_entries;
0018 delete[] bin_means;
0019 }
0020
0021 int ClusterizingHistogram::bin(float x) const {
0022 if (x < xmin)
0023 return -1;
0024 else if (x > xmax)
0025 return my_nbins;
0026 else
0027 return int((x - xmin) / binsiz);
0028 }
0029 int ClusterizingHistogram::bin(double x) const {
0030 if (x < xmin)
0031 return -1;
0032 else if (x > xmax)
0033 return my_nbins;
0034 else
0035 return int((x - xmin) / binsiz);
0036 }
0037
0038 void ClusterizingHistogram::fill(float x) {
0039 if (x < xmin)
0040 my_underflows++;
0041 else if (x > xmax)
0042 my_overflows++;
0043 else {
0044 int bin = int((x - xmin) / binsiz);
0045 if (bin > my_nbins - 1)
0046 bin = my_nbins - 1;
0047 ++bin_entries[bin];
0048 bin_means[bin] += x;
0049 my_entries++;
0050
0051 }
0052 }
0053
0054 vector<float> ClusterizingHistogram::clusterize(float resol) {
0055 vector<float> clust;
0056 int nclust = 0;
0057 bool inclust = false;
0058 float last_pos = xmin - 1000. * resol;
0059 int sum = 0;
0060 float sumx = 0;
0061 for (int i = 0; i < my_nbins; i++) {
0062 if (bin_entries[i] != 0) {
0063 if (fabs(bin_pos(i) - last_pos) > resol) {
0064 inclust = false;
0065 if (nclust != 0)
0066 clust.push_back(sumx / sum);
0067 }
0068 if (!inclust) {
0069 nclust++;
0070 sumx = 0.;
0071 sum = 0;
0072 }
0073 sum += bin_entries[i];
0074 sumx += bin_means[i];
0075 last_pos = bin_pos(i);
0076 inclust = true;
0077 }
0078 }
0079 if (nclust != 0)
0080 clust.push_back(sumx / sum);
0081 return clust;
0082 }
0083
0084 void ClusterizingHistogram::dump() const { dump(0, my_nbins); }
0085
0086 void ClusterizingHistogram::dump(int i1, int i2) const {
0087 cout << "Dumping ClusterizingHistogram contents:" << endl;
0088 for (int i = max(i1, 0); i < min(i2, my_nbins); i++) {
0089 cout << i << " " << bin_entries[i] << " " << bin_pos(i) << endl;
0090 }
0091 cout << "Underflows: " << my_underflows << endl;
0092 cout << "Overflows: " << my_overflows << endl;
0093 cout << "Total number of entries: " << my_entries << endl;
0094 }
0095
0096 void ClusterizingHistogram::dump(float x1, float x2) const { dump(bin(x1), bin(x2)); }
0097 void ClusterizingHistogram::dump(double x1, double x2) const { dump(bin(x1), bin(x2)); }
0098 void ClusterizingHistogram::dump(float x1, double x2) const { dump(bin(x1), bin(x2)); }
0099 void ClusterizingHistogram::dump(double x1, float x2) const { dump(bin(x1), bin(x2)); }
0100
0101 void ClusterizingHistogram::reset() {
0102 my_entries = 0;
0103 my_underflows = 0;
0104 my_overflows = 0;
0105 for (int i = 0; i < my_nbins; i++) {
0106 bin_entries[i] = 0;
0107 bin_means[i] = 0.;
0108 }
0109 }