CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
XHistogram.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <cmath>
3 #include <vector>
4 
5 #include "XHistogram.h"
6 
7 std::vector<XHistogram::position> XHistogram::splitSegment( Range rangeX, Range rangeY ) const
8 {
9  double deltaX = rangeX.second - rangeX.first;
10  double deltaY = rangeY.second - rangeY.first;
11  double length = hypot(deltaX, deltaY);
12  double stepX = (m_xRange.second - m_xRange.first) / m_xBins;
13  double stepY = (m_yRange.second - m_yRange.first) / m_yBins;
14 
15  int min_i, max_i, min_j, max_j;
16  if (rangeX.first < rangeX.second) {
17  min_i = (int) ceil(rangeX.first / stepX); // included
18  max_i = (int) floor(rangeX.second / stepX) + 1; // excluded
19  } else {
20  min_i = (int) ceil(rangeX.second / stepX);
21  max_i = (int) floor(rangeX.first / stepX) + 1;
22  }
23  if (rangeY.first < rangeY.second) {
24  min_j = (int) ceil(rangeY.first / stepY);
25  max_j = (int) floor(rangeY.second / stepY) + 1;
26  } else {
27  min_j = (int) ceil(rangeY.second / stepY);
28  max_j = (int) floor(rangeY.first / stepY) + 1;
29  }
30 
31  int steps = max_i-min_i + max_j-min_j + 2;
32  std::vector<position> v;
33  v.clear();
34  v.reserve(steps);
35 
36  v.push_back( position(0., rangeX.first, rangeY.first) );
37  double x, y, f;
38  for (int i = min_i; i < max_i; ++i) {
39  x = i * stepX;
40  y = rangeY.first + (x - rangeX.first) * deltaY / deltaX;
41  f = std::fabs( (x - rangeX.first) / deltaX );
42  v.push_back( position(f, x, y) );
43  }
44  for (int i = min_j; i < max_j; ++i) {
45  y = i * stepY;
46  x = rangeX.first + (y - rangeY.first) * deltaX / deltaY;
47  f = std::fabs( (y - rangeY.first) / deltaY );
48  v.push_back( position(f, x, y) );
49  }
50  v.push_back( position(1., rangeX.second, rangeY.second) );
51 
52  // sort by distance from the start of the segment
53  std::sort(v.begin(), v.end());
54 
55  // filter away the fragments shorter than m_minDl, and save the center of each fragment along with its fractionary length
56  std::vector<position> result;
57  result.push_back( v.front() );
58  for (int i = 1, s = v.size(); i < s; ++i) {
59  double mx = (v[i].x + v[i-1].x) / 2.;
60  double my = (v[i].y + v[i-1].y) / 2.;
61  double df = (v[i].f - v[i-1].f);
62  if (df * length < m_minDl)
63  continue;
64  result.push_back( position( df, mx, my ) );
65  }
66 
67  return result;
68 }
69 
71 void XHistogram::fill( double x, double y, const std::vector<double> & weight, double norm )
72 {
73  check_weight( weight );
74 
75  for (size_t h = 0; h < m_size; ++h)
76  m_histograms[h]->Fill( x, y, weight[h] );
77  m_normalization->Fill( x, y, norm );
78 }
79 
81 void XHistogram::fill( double x, double y, const std::vector<double> & weight, double norm, unsigned int colour )
82 {
83  check_weight( weight );
84 
85  for (size_t h = 0; h < m_size; ++h)
86  m_histograms[h]->Fill( x, y, weight[h] );
87  m_normalization->Fill( x, y, norm );
88  m_colormap->SetBinContent( m_colormap->FindBin(x, y), (float) colour );
89 }
90 
92 void XHistogram::fill( const Range& x, const Range& y, const std::vector<double> & weight, double norm )
93 {
94  check_weight( weight );
95 
96  std::vector<position> v = splitSegment( x, y );
97  for (size_t i = 0, s = v.size(); i < s; ++i) {
98  for (size_t h = 0; h < m_size; ++h)
99  m_histograms[h]->Fill( v[i].x, v[i].y, v[i].f * weight[h] );
100  m_normalization->Fill( v[i].x, v[i].y, v[i].f * norm );
101  }
102 }
103 
105 void XHistogram::fill( const Range& x, const Range& y, const std::vector<double> & weight, double norm, unsigned int colour )
106 {
107  check_weight( weight );
108 
109  std::vector<position> v = splitSegment( x, y );
110  for (size_t i = 0, s = v.size(); i < s; ++i) {
111  for (size_t h = 0; h < m_size; ++h)
112  m_histograms[h]->Fill( v[i].x, v[i].y, v[i].f * weight[h] );
113  m_normalization->Fill( v[i].x, v[i].y, v[i].f * norm );
114  m_colormap->SetBinContent( m_colormap->FindBin(v[i].x, v[i].y), (float) colour );
115  }
116 }
117 
120 {
121  for (int i = 0; i < m_normalization->GetSize(); ++i) {
122  if ((*m_normalization)[i] > 0.) {
123  for (size_t h = 0; h < m_size; ++h)
124  (*m_histograms[h])[i] /= (*m_normalization)[i];
125  (*m_normalization)[i] = 1.;
126  }
127  }
128 }
int i
Definition: DBlmapReader.cc:9
double m_minDl
Definition: XHistogram.h:21
Range m_xRange
Definition: XHistogram.h:22
size_t m_size
Definition: XHistogram.h:26
static int position[TOTALCHAMBERS][3]
Definition: ReadPGInfo.cc:509
boost::shared_ptr< ColorMap > m_colormap
Definition: XHistogram.h:30
std::vector< position > splitSegment(Range x, Range y) const
split a segment into a vector of points
Definition: XHistogram.cc:7
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
void check_weight(const std::vector< double > &weight)
check the weights passed as an std::vector have the correct size
Definition: XHistogram.h:144
tuple result
Definition: query.py:137
double f[11][100]
std::vector< boost::shared_ptr< Histogram > > m_histograms
Definition: XHistogram.h:28
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
boost::shared_ptr< Histogram > m_normalization
Definition: XHistogram.h:29
size_t m_xBins
Definition: XHistogram.h:24
size_t m_yBins
Definition: XHistogram.h:25
void fill(double x, double y, const std::vector< double > &weight, double norm)
fill one point
Definition: XHistogram.cc:71
void normalize(void)
normalize the histograms
Definition: XHistogram.cc:119
Range m_yRange
Definition: XHistogram.h:23
Definition: DDAxes.h:10
int weight
Definition: histoStyle.py:50
std::pair< double, double > Range
Definition: XHistogram.h:18