CMS 3D CMS Logo

ClusterizingHistogram.cc
Go to the documentation of this file.
2 #include <iostream>
3 
4 using namespace std;
5 
6 ClusterizingHistogram::ClusterizingHistogram(int nb, float xmi, float xma)
7  : my_nbins(nb), xmin(xmi), xmax(xma), my_entries(0), my_underflows(0), my_overflows(0) {
8  bin_entries = new int[my_nbins];
9  bin_means = new float[my_nbins];
10  binsiz = (xmax - xmin) / my_nbins;
11  for (int i = 0; i < my_nbins; i++) {
12  bin_entries[i] = 0;
13  bin_means[i] = 0.;
14  }
15 }
17  delete[] bin_entries;
18  delete[] bin_means;
19 }
20 
21 int ClusterizingHistogram::bin(float x) const {
22  if (x < xmin)
23  return -1;
24  else if (x > xmax)
25  return my_nbins;
26  else
27  return int((x - xmin) / binsiz);
28 }
29 int ClusterizingHistogram::bin(double x) const {
30  if (x < xmin)
31  return -1;
32  else if (x > xmax)
33  return my_nbins;
34  else
35  return int((x - xmin) / binsiz);
36 }
37 
39  if (x < xmin)
40  my_underflows++;
41  else if (x > xmax)
42  my_overflows++;
43  else {
44  int bin = int((x - xmin) / binsiz);
45  if (bin > my_nbins - 1)
46  bin = my_nbins - 1;
47  ++bin_entries[bin];
48  bin_means[bin] += x;
49  my_entries++;
50  // may be problematic for negative x; check!
51  }
52 }
53 
54 vector<float> ClusterizingHistogram::clusterize(float resol) {
55  vector<float> clust;
56  int nclust = 0;
57  bool inclust = false;
58  float last_pos = xmin - 1000. * resol;
59  int sum = 0;
60  float sumx = 0;
61  for (int i = 0; i < my_nbins; i++) {
62  if (bin_entries[i] != 0) {
63  if (fabs(bin_pos(i) - last_pos) > resol) {
64  inclust = false;
65  if (nclust != 0)
66  clust.push_back(sumx / sum); // create cluster
67  }
68  if (!inclust) {
69  nclust++;
70  sumx = 0.;
71  sum = 0;
72  }
73  sum += bin_entries[i];
74  sumx += bin_means[i];
75  last_pos = bin_pos(i);
76  inclust = true;
77  }
78  }
79  if (nclust != 0)
80  clust.push_back(sumx / sum); // create last cluster
81  return clust;
82 }
83 
85 
86 void ClusterizingHistogram::dump(int i1, int i2) const {
87  cout << "Dumping ClusterizingHistogram contents:" << endl;
88  for (int i = max(i1, 0); i < min(i2, my_nbins); i++) {
89  cout << i << " " << bin_entries[i] << " " << bin_pos(i) << endl;
90  }
91  cout << "Underflows: " << my_underflows << endl;
92  cout << "Overflows: " << my_overflows << endl;
93  cout << "Total number of entries: " << my_entries << endl;
94 }
95 
96 void ClusterizingHistogram::dump(float x1, float x2) const { dump(bin(x1), bin(x2)); }
97 void ClusterizingHistogram::dump(double x1, double x2) const { dump(bin(x1), bin(x2)); }
98 void ClusterizingHistogram::dump(float x1, double x2) const { dump(bin(x1), bin(x2)); }
99 void ClusterizingHistogram::dump(double x1, float x2) const { dump(bin(x1), bin(x2)); }
100 
102  my_entries = 0;
103  my_underflows = 0;
104  my_overflows = 0;
105  for (int i = 0; i < my_nbins; i++) {
106  bin_entries[i] = 0;
107  bin_means[i] = 0.;
108  }
109 }
std::vector< float > clusterize(float resolution)
T min(T a, T b)
Definition: MathUtil.h:58
float bin_pos(int i) const