CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
18 int ClusterizingHistogram::bin( float x) const {
19  if (x < xmin) return -1;
20  else if (x > xmax) return my_nbins;
21  else return int((x-xmin)/binsiz);
22 }
23 int ClusterizingHistogram::bin( double x) const {
24  if (x < xmin) return -1;
25  else if (x > xmax) return my_nbins;
26  else return int((x-xmin)/binsiz);
27 }
28 
30  if (x < xmin) my_underflows++;
31  else if (x > xmax) my_overflows++;
32  else {
33  int bin = int((x-xmin)/binsiz);
34  if ( bin > my_nbins-1) bin = my_nbins-1;
35  ++bin_entries[bin];
36  bin_means[bin] += x;
37  my_entries++;
38  // may be problematic for negative x; check!
39  }
40 }
41 
42 vector<float> ClusterizingHistogram::clusterize( float resol) {
43  vector<float> clust;
44  int nclust = 0;
45  bool inclust = false;
46  float last_pos = xmin - 1000.*resol;
47  int sum = 0;
48  float sumx = 0;
49  for (int i=0; i<my_nbins; i++) {
50  if (bin_entries[i] != 0) {
51  if ( fabs(bin_pos(i)-last_pos) > resol) {
52  inclust = false;
53  if (nclust != 0) clust.push_back( sumx/sum); // create cluster
54  }
55  if (!inclust) {
56  nclust++;
57  sumx = 0.;
58  sum = 0;
59  }
60  sum += bin_entries[i];
61  sumx += bin_means[i];
62  last_pos = bin_pos(i);
63  inclust = true;
64  }
65  }
66  if (nclust != 0) clust.push_back( sumx/sum); // create last cluster
67  return clust;
68 }
69 
70 
72 
73 void ClusterizingHistogram::dump(int i1, int i2) const {
74  cout << "Dumping ClusterizingHistogram contents:" << endl;
75  for (int i=max(i1,0); i<min(i2,my_nbins); i++) {
76  cout << i << " " << bin_entries[i] << " " << bin_pos(i) << endl;
77  }
78  cout << "Underflows: " << my_underflows << endl;
79  cout << "Overflows: " << my_overflows << endl;
80  cout << "Total number of entries: " << my_entries << endl;
81 }
82 
83 void ClusterizingHistogram::dump( float x1, float x2) const { dump( bin(x1), bin(x2));}
84 void ClusterizingHistogram::dump( double x1, double x2) const { dump( bin(x1), bin(x2));}
85 void ClusterizingHistogram::dump( float x1, double x2) const { dump( bin(x1), bin(x2));}
86 void ClusterizingHistogram::dump( double x1, float x2) const { dump( bin(x1), bin(x2));}
87 
89  my_entries = 0;
90  my_underflows = 0;
91  my_overflows = 0;
92  for (int i=0; i<my_nbins; i++) {
93  bin_entries[i] = 0;
94  bin_means[i] = 0.;
95  }
96 }
int i
Definition: DBlmapReader.cc:9
T x() const
Cartesian x coordinate.
std::vector< float > clusterize(float resolution)
T min(T a, T b)
Definition: MathUtil.h:58
float bin_pos(int i) const
tuple cout
Definition: gather_cfg.py:145