CMS 3D CMS Logo

SiStripApvSimulationParameters.cc
Go to the documentation of this file.
3 #include "CLHEP/Random/RandFlat.h"
4 
5 namespace {
7  auto hXInt = (params.hasEquidistantBinsY()
8  ? (params.hasEquidistantBinsZ()
10  params.numberOfBinsY(), params.rangeY(), params.numberOfBinsZ(), params.rangeZ())
12  params.numberOfBinsY(), params.rangeY(), params.upperLimitsZ()))
13  : (params.hasEquidistantBinsZ()
15  params.upperLimitsY(), params.numberOfBinsZ(), params.rangeZ())
16  : PhysicsTools::Calibration::HistogramF2D(params.upperLimitsY(), params.upperLimitsZ())));
17  for (int i{0}; i != params.numberOfBinsY() + 2; ++i) {
18  for (int j{0}; j != params.numberOfBinsZ() + 2; ++j) {
19  float xInt = 0.;
20  for (int k{0}; k != params.numberOfBinsX() + 2; ++k) {
21  xInt += params.binContent(k, i, j);
22  }
23  hXInt.setBinContent(i, j, xInt);
24  }
25  }
26  return hXInt;
27  }
28 
29  float xBinPos(const SiStripApvSimulationParameters::LayerParameters& hist, int iBin, float pos = 0.5) {
30  // NOTE: does not work for under- and overflow bins (iBin = 0 and iBIn == hist.numberOfBinsX()+1)
31  if (hist.hasEquidistantBinsX()) {
32  const auto range = hist.rangeX();
33  const auto binWidth = (range.max - range.min) / hist.numberOfBinsX();
34  return range.min + (iBin - 1 + pos) * binWidth;
35  } else {
36  return (1. - pos) * hist.upperLimitsX()[iBin - 1] + pos * hist.upperLimitsX()[iBin];
37  }
38  }
39 } // namespace
40 
42  if (m_barrelParam.size() != m_barrelParam_xInt.size()) {
43  m_barrelParam_xInt.resize(m_barrelParam.size());
44  for (unsigned int i{0}; i != m_barrelParam.size(); ++i) {
45  m_barrelParam_xInt[i] = calculateXInt(m_barrelParam[i]);
46  }
47  }
48  if (m_endcapParam.size() != m_endcapParam_xInt.size()) {
49  m_endcapParam_xInt.resize(m_endcapParam.size());
50  for (unsigned int i{0}; i != m_endcapParam.size(); ++i) {
51  m_endcapParam_xInt[i] = calculateXInt(m_endcapParam[i]);
52  }
53  }
54 }
55 
58  if ((layer > m_nTIB) || (layer < 1)) {
59  edm::LogError("SiStripApvSimulationParameters")
60  << "[" << __PRETTY_FUNCTION__ << "] layer index " << layer << " out of range [1," << m_nTIB << "]";
61  return false;
62  }
63  m_barrelParam[layer - 1] = params;
64  m_barrelParam_xInt[layer - 1] = calculateXInt(params);
65  return true;
66 }
67 
70  if ((layer > m_nTOB) || (layer < 1)) {
71  edm::LogError("SiStripApvSimulationParameters")
72  << "[" << __PRETTY_FUNCTION__ << "] layer index " << layer << " out of range [1," << m_nTOB << ")";
73  return false;
74  }
76  m_barrelParam_xInt[m_nTIB + layer - 1] = calculateXInt(params);
77  return true;
78 }
79 
82  if ((wheel > m_nTID) || (wheel < 1)) {
83  edm::LogError("SiStripApvSimulationParameters")
84  << "[" << __PRETTY_FUNCTION__ << "] wheel index " << wheel << " out of range [1," << m_nTID << "]";
85  return false;
86  }
87  m_endcapParam[wheel - 1] = params;
88  m_endcapParam_xInt[wheel - 1] = calculateXInt(params);
89  return true;
90 }
91 
94  if ((wheel > m_nTEC) || (wheel < 1)) {
95  edm::LogError("SiStripApvSimulationParameters")
96  << "[" << __PRETTY_FUNCTION__ << "] wheel index " << wheel << " out of range [1," << m_nTEC << ")";
97  return false;
98  }
100  m_endcapParam_xInt[m_nTID + wheel - 1] = calculateXInt(params);
101  return true;
102 }
103 
105  float z,
106  float pu,
107  CLHEP::HepRandomEngine* engine) const {
108  if (m_barrelParam.size() != m_barrelParam_xInt.size()) {
109  throw cms::Exception("LogicError") << "x-integrals of 3D histograms have not been calculated";
110  }
111  const auto layerParam = m_barrelParam[layerIdx];
112  const int ip = layerParam.findBinY(pu);
113  const int iz = layerParam.findBinZ(z);
114  const float norm = m_barrelParam_xInt[layerIdx].binContent(ip, iz);
115  const auto val = CLHEP::RandFlat::shoot(engine) * norm;
116  if (val < layerParam.binContent(0, ip, iz)) { // underflow
117  return layerParam.rangeX().min;
118  } else if (norm - val < layerParam.binContent(layerParam.numberOfBinsX() + 1, ip, iz)) { // overflow
119  return layerParam.rangeX().max;
120  } else { // loop over bins, return center of our bin
121  float sum = layerParam.binContent(0, ip, iz);
122  for (int i{1}; i != layerParam.numberOfBinsX() + 1; ++i) {
123  sum += layerParam.binContent(i, ip, iz);
124  if (sum > val) {
125  return xBinPos(layerParam, i, (sum - val) / layerParam.binContent(i, ip, iz));
126  }
127  }
128  }
129  throw cms::Exception("LogicError") << "Problem drawing a random number from the distribution";
130 }
131 
133  float r,
134  float pu,
135  CLHEP::HepRandomEngine* engine) const {
136  if (m_endcapParam.size() != m_endcapParam_xInt.size()) {
137  throw cms::Exception("LogicError") << "x-integrals of 3D histograms have not been calculated";
138  }
139  const auto layerParam = m_endcapParam[wheelIdx];
140  const int ip = layerParam.findBinY(pu);
141  const int ir = layerParam.findBinZ(r);
142  const float norm = m_endcapParam_xInt[wheelIdx].binContent(ip, ir);
143  const auto val = CLHEP::RandFlat::shoot(engine) * norm;
144  if (val < layerParam.binContent(0, ip, ir)) { // underflow
145  return layerParam.rangeX().min;
146  } else if (norm - val < layerParam.binContent(layerParam.numberOfBinsX() + 1, ip, ir)) { // overflow
147  return layerParam.rangeX().max;
148  } else { // loop over bins, return center of our bin
149  float sum = layerParam.binContent(0, ip, ir);
150  for (int i{1}; i != layerParam.numberOfBinsX() + 1; ++i) {
151  sum += layerParam.binContent(i, ip, ir);
152  if (sum > val) {
153  return xBinPos(layerParam, i, (sum - val) / layerParam.binContent(i, ip, ir));
154  }
155  }
156  }
157  throw cms::Exception("LogicError") << "Problem drawing a random number from the distribution";
158 }
bool putTIB(layerid layer, const LayerParameters &params)
Log< level::Error, false > LogError
Histogram2D< float > HistogramF2D
Definition: Histogram2D.h:138
bool putTID(layerid wheel, const LayerParameters &params)
std::vector< PhysicsTools::Calibration::HistogramF2D > m_endcapParam_xInt
std::vector< PhysicsTools::Calibration::HistogramF3D > m_endcapParam
float sampleEndcap(layerid wheelIdx, float r, float pu, CLHEP::HepRandomEngine *engine) const
std::vector< PhysicsTools::Calibration::HistogramF3D > m_barrelParam
bool putTEC(layerid wheel, const LayerParameters &params)
std::vector< PhysicsTools::Calibration::HistogramF2D > m_barrelParam_xInt
bool putTOB(layerid layer, const LayerParameters &params)
float sampleBarrel(layerid layerIdx, float z, float pu, CLHEP::HepRandomEngine *engine) const