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