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  }
75  m_barrelParam[m_nTIB + layer - 1] = params;
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 }
SiStripApvSimulationParameters::m_endcapParam_xInt
std::vector< PhysicsTools::Calibration::HistogramF2D > m_endcapParam_xInt
Definition: SiStripApvSimulationParameters.h:69
SiStripApvSimulationParameters::m_nTOB
layerid m_nTOB
Definition: SiStripApvSimulationParameters.h:65
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
mps_fire.i
i
Definition: mps_fire.py:355
MessageLogger.h
SiStripApvSimulationParameters::m_nTEC
layerid m_nTEC
Definition: SiStripApvSimulationParameters.h:65
detailsBasic3DVector::z
float float float z
Definition: extBasic3DVector.h:14
CalibrationSummaryClient_cfi.params
params
Definition: CalibrationSummaryClient_cfi.py:14
pos
Definition: PixelAliasList.h:18
SiStripApvSimulationParameters::m_nTIB
layerid m_nTIB
Definition: SiStripApvSimulationParameters.h:65
compare.hist
hist
Definition: compare.py:376
SiStripApvSimulationParameters::m_barrelParam
std::vector< PhysicsTools::Calibration::HistogramF3D > m_barrelParam
Definition: SiStripApvSimulationParameters.h:66
SiStripApvSimulationParameters::putTEC
bool putTEC(layerid wheel, const LayerParameters &params)
Definition: SiStripApvSimulationParameters.h:43
DDAxes::z
dqmdumpme.k
k
Definition: dqmdumpme.py:60
PhysicsTools::Calibration::HistogramF2D
Histogram2D< float > HistogramF2D
Definition: Histogram2D.h:138
edm::LogError
Definition: MessageLogger.h:183
SiStripApvSimulationParameters::putTIB
bool putTIB(layerid layer, const LayerParameters &params)
Definition: SiStripApvSimulationParameters.h:34
makeMuonMisalignmentScenario.wheel
wheel
Definition: makeMuonMisalignmentScenario.py:319
SiStripApvSimulationParameters::m_endcapParam
std::vector< PhysicsTools::Calibration::HistogramF3D > m_endcapParam
Definition: SiStripApvSimulationParameters.h:68
PhysicsTools::Calibration::Histogram2D
Definition: Histogram2D.h:20
SiStripApvSimulationParameters::putTOB
bool putTOB(layerid layer, const LayerParameters &params)
Definition: SiStripApvSimulationParameters.h:37
SiStripApvSimulationParameters::putTID
bool putTID(layerid wheel, const LayerParameters &params)
Definition: SiStripApvSimulationParameters.h:40
alignCSCRings.r
r
Definition: alignCSCRings.py:93
SiStripApvSimulationParameters::layerid
unsigned int layerid
Definition: SiStripApvSimulationParameters.h:19
SiStripApvSimulationParameters::sampleBarrel
float sampleBarrel(layerid layerIdx, float z, float pu, CLHEP::HepRandomEngine *engine) const
Definition: SiStripApvSimulationParameters.cc:104
heppy_batch.val
val
Definition: heppy_batch.py:351
SiStripApvSimulationParameters::calculateIntegrals
void calculateIntegrals()
Definition: SiStripApvSimulationParameters.cc:41
Exception
Definition: hltDiff.cc:246
SiStripApvSimulationParameters::sampleEndcap
float sampleEndcap(layerid wheelIdx, float r, float pu, CLHEP::HepRandomEngine *engine) const
Definition: SiStripApvSimulationParameters.cc:132
SiStripApvSimulationParameters.h
PhysicsTools::Calibration::Histogram3D
Definition: Histogram3D.h:24
muons2muons_cfi.pu
pu
Definition: muons2muons_cfi.py:31
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
SiStripApvSimulationParameters::m_nTID
layerid m_nTID
Definition: SiStripApvSimulationParameters.h:65
SiStripApvSimulationParameters::m_barrelParam_xInt
std::vector< PhysicsTools::Calibration::HistogramF2D > m_barrelParam_xInt
Definition: SiStripApvSimulationParameters.h:67