CMS 3D CMS Logo

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