CMS 3D CMS Logo

ESDigitizer.cc
Go to the documentation of this file.
5 
6 #include "CLHEP/Random/RandGeneral.h"
7 #include "CLHEP/Random/RandPoissonQ.h"
8 #include "CLHEP/Random/RandFlat.h"
9 
10 #include <gsl/gsl_sf_erf.h>
11 #include <gsl/gsl_sf_result.h>
12 
13 ESDigitizer::ESDigitizer(EcalHitResponse* hitResponse, ESElectronicsSimFast* electronicsSim, bool addNoise)
14  : EcalTDigitizer<ESDigitizerTraits>(hitResponse, electronicsSim, addNoise),
15  m_detIds(nullptr),
16  m_ranGeneral(nullptr),
17  m_ESGain(0),
18  m_histoBin(0),
19  m_histoInf(0),
20  m_histoWid(0),
21  m_meanNoisy(0),
22  m_trip() {
23  m_trip.reserve(2500);
24 }
25 
27 
29 void ESDigitizer::setDetIds(const std::vector<DetId>& detIds) {
30  assert(nullptr == m_detIds || &detIds == m_detIds); // sanity check; don't allow to change midstream
31  m_detIds = &detIds;
32 }
33 
34 void ESDigitizer::setGain(const int gain) {
35  if (0 != m_ESGain) {
36  assert(gain == m_ESGain); // only allow one value
37  } else {
38  assert(nullptr != m_detIds && !m_detIds->empty()); // detIds must already be set as we need size
39 
40  assert(1 == gain || 2 == gain); // legal values
41 
42  m_ESGain = gain;
43 
44  if (addNoise()) {
45  double zsThresh(0.);
46  std::string refFile;
47 
48  if (1 == m_ESGain) {
49  zsThresh = 3;
50  refFile = "SimCalorimetry/EcalSimProducers/data/esRefHistosFile_LG.txt";
51  } else {
52  zsThresh = 4;
53  refFile = "SimCalorimetry/EcalSimProducers/data/esRefHistosFile_HG.txt";
54  }
55 
56  gsl_sf_result result;
57  int status = gsl_sf_erf_Q_e(zsThresh, &result);
58  if (status != 0)
59  std::cerr << "ESDigitizer::could not compute gaussian tail probability for the threshold chosen" << std::endl;
60 
61  const double probabilityLeft(result.val);
62  m_meanNoisy = probabilityLeft * m_detIds->size();
63 
64  std::ifstream histofile(edm::FileInPath(refFile).fullPath().c_str());
65  if (!histofile.good()) {
66  throw edm::Exception(edm::errors::InvalidReference, "NullPointer") << "Reference histos file not opened";
67  } else {
68  // number of bins
69  char buffer[200];
70  int thisLine = 0;
71  while (0 == thisLine) {
72  histofile.getline(buffer, 200);
73  if (!strstr(buffer, "#") && !(strspn(buffer, " ") == strlen(buffer))) {
74  float histoBin;
75  sscanf(buffer, "%f", &histoBin);
76  m_histoBin = (double)histoBin;
77  ++thisLine;
78  }
79  }
80  const uint32_t histoBin1((int)m_histoBin);
81  const uint32_t histoBin2(histoBin1 * histoBin1);
82 
83  double t_histoSup(0);
84 
85  std::vector<double> t_refHistos;
86  t_refHistos.reserve(2500);
87 
88  int thisBin = -2;
89  while (!(histofile.eof())) {
90  histofile.getline(buffer, 200);
91  if (!strstr(buffer, "#") && !(strspn(buffer, " ") == strlen(buffer))) {
92  if (-2 == thisBin) {
93  float histoInf;
94  sscanf(buffer, "%f", &histoInf);
95  m_histoInf = (double)histoInf;
96  }
97  if (-1 == thisBin) {
98  float histoSup;
99  sscanf(buffer, "%f", &histoSup);
100  t_histoSup = (double)histoSup;
101  }
102  if (0 <= thisBin) {
103  float refBin;
104  sscanf(buffer, "%f", &refBin);
105  if (0.5 < refBin) {
106  t_refHistos.push_back((double)refBin);
107  const uint32_t i2(thisBin / histoBin2);
108  const uint32_t off(i2 * histoBin2);
109  const uint32_t i1((thisBin - off) / histoBin1);
110  const uint32_t i0(thisBin - off - i1 * histoBin1);
111  m_trip.emplace_back(i0, i1, i2);
112  }
113  }
114  ++thisBin;
115  }
116  }
117  m_histoWid = (t_histoSup - m_histoInf) / m_histoBin;
118 
119  m_histoInf -= 1000.;
120 
121  // creating the reference distribution to extract random numbers
122  m_ranGeneral = new CLHEP::RandGeneral(nullptr, &t_refHistos.front(), t_refHistos.size(), 0);
123  histofile.close();
124  }
125  }
126  }
127 }
128 
130 void ESDigitizer::run(ESDigiCollection& output, CLHEP::HepRandomEngine* engine) {
131  assert(nullptr != m_detIds && !m_detIds->empty() && (!addNoise() || nullptr != m_ranGeneral)); // sanity check
132 
133  // reserve space for how many digis we expect, with some cushion
134  output.reserve(2 * ((int)m_meanNoisy) + hitResponse()->samplesSize());
135 
137 
138  // random generation of channel above threshold
139  std::vector<DetId> abThreshCh;
140  if (addNoise())
141  createNoisyList(abThreshCh, engine);
142 
143  // first make a raw digi for every cell where we have noise
144  for (std::vector<DetId>::const_iterator idItr(abThreshCh.begin()); idItr != abThreshCh.end(); ++idItr) {
145  if (hitResponse()->findDetId(*idItr)->zero()) // only if no true hit!
146  {
147  ESHitResponse::ESSamples analogSignal(*idItr, 3); // space for the noise hit
148  uint32_t myBin((uint32_t)m_trip.size() * m_ranGeneral->shoot(engine));
149  if (myBin == m_trip.size())
150  --myBin; // guard against roundup
151  assert(myBin < m_trip.size());
152  const Triplet& trip(m_trip[myBin]);
153  analogSignal[0] = m_histoInf + m_histoWid * trip.first;
154  analogSignal[1] = m_histoInf + m_histoWid * trip.second;
155  analogSignal[2] = m_histoInf + m_histoWid * trip.third;
156  ESDataFrame digi(*idItr);
157  const_cast<ESElectronicsSimFast*>(elecSim())->analogToDigital(engine, analogSignal, digi, true);
158  output.push_back(digi);
159  }
160  }
161 }
162 
163 // preparing the list of channels where the noise has to be generated
164 void ESDigitizer::createNoisyList(std::vector<DetId>& abThreshCh, CLHEP::HepRandomEngine* engine) {
165  CLHEP::RandPoissonQ randPoissonQ(*engine, m_meanNoisy);
166  const unsigned int nChan(randPoissonQ.fire());
167  abThreshCh.reserve(nChan);
168 
169  for (unsigned int i(0); i != nChan; ++i) {
170  std::vector<DetId>::const_iterator idItr(abThreshCh.end());
171  uint32_t iChan(0);
172  DetId id;
173  do {
174  iChan = (uint32_t)CLHEP::RandFlat::shoot(engine, m_detIds->size());
175  if (iChan == m_detIds->size())
176  --iChan; //protect against roundup at end
177  assert(m_detIds->size() > iChan); // sanity check
178  id = (*m_detIds)[iChan];
179  idItr = find(abThreshCh.begin(), abThreshCh.end(), id);
180  } while (idItr != abThreshCh.end());
181 
182  abThreshCh.push_back(id);
183  }
184 }
testProducerWithPsetDescEmpty_cfi.i2
i2
Definition: testProducerWithPsetDescEmpty_cfi.py:46
mps_fire.i
i
Definition: mps_fire.py:428
EcalHitResponse::findDetId
const EcalSamples * findDetId(const DetId &detId) const
Definition: EcalHitResponse.cc:224
edm::errors::InvalidReference
Definition: EDMException.h:39
convertSQLitetoXML_cfg.output
output
Definition: convertSQLitetoXML_cfg.py:72
mps_update.status
status
Definition: mps_update.py:69
contentValuesFiles.fullPath
fullPath
Definition: contentValuesFiles.py:64
EcalTDigitizer< ESDigitizerTraits >::addNoise
bool addNoise() const
testProducerWithPsetDescEmpty_cfi.i1
i1
Definition: testProducerWithPsetDescEmpty_cfi.py:45
cms::cuda::assert
assert(be >=bs)
ESDigitizer::setDetIds
void setDetIds(const std::vector< DetId > &detIds)
tell the digitizer which cells exist; cannot change during a run
Definition: ESDigitizer.cc:29
CaloTSamplesBase::zero
bool zero() const
ESDigitizer.h
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
ESDataFrame
Definition: ESDataFrame.h:10
edmScanValgrind.buffer
buffer
Definition: edmScanValgrind.py:171
DetId
Definition: DetId.h:17
edm::FileInPath
Definition: FileInPath.h:64
ESDigitizer::m_trip
std::vector< Triplet > m_trip
Definition: ESDigitizer.h:49
CaloTSamples
Definition: CaloTSamples.h:14
ESDigitizer::Triplet
Definition: ESDigitizer.h:39
ESDigitizer::m_meanNoisy
double m_meanNoisy
Definition: ESDigitizer.h:37
ESDigitizer::m_ESGain
int m_ESGain
Definition: ESDigitizer.h:33
ESDigiCollection
Definition: EcalDigiCollections.h:82
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
ESDigitizer::createNoisyList
void createNoisyList(std::vector< DetId > &abThreshCh, CLHEP::HepRandomEngine *)
Definition: ESDigitizer.cc:164
ESDigitizer::m_ranGeneral
CLHEP::RandGeneral * m_ranGeneral
Definition: ESDigitizer.h:32
EcalTDigitizer< ESDigitizerTraits >::elecSim
const ElectronicsSim * elecSim() const
ESHitResponse.h
PCaloHit.h
EcalTDigitizer::run
virtual void run(DigiCollection &output, CLHEP::HepRandomEngine *)
EcalHitResponse
Definition: EcalHitResponse.h:29
ESElectronicsSimFast
Definition: ESElectronicsSimFast.h:16
ESDigitizer::setGain
void setGain(const int gain)
Definition: ESDigitizer.cc:34
ESDigitizer::m_histoInf
double m_histoInf
Definition: ESDigitizer.h:35
ESDigitizer::m_histoBin
double m_histoBin
Definition: ESDigitizer.h:34
ESDigitizerTraits
Definition: EcalDigitizerTraits.h:45
EcalTDigitizer
Definition: EcalTDigitizer.h:23
PedestalClient_cfi.gain
gain
Definition: PedestalClient_cfi.py:37
jetmet_cfg.histofile
histofile
Definition: jetmet_cfg.py:4
ESDigitizer::~ESDigitizer
~ESDigitizer() override
Definition: ESDigitizer.cc:26
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:31
Exception
Definition: hltDiff.cc:246
ESDigitizer::m_detIds
const std::vector< DetId > * m_detIds
Definition: ESDigitizer.h:31
mps_fire.result
result
Definition: mps_fire.py:311
ESDigitizer::run
void run(ESDigiCollection &output, CLHEP::HepRandomEngine *) override
turns hits into digis
Definition: ESDigitizer.cc:130
ESElectronicsSimFast.h
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
ESDigitizer::m_histoWid
double m_histoWid
Definition: ESDigitizer.h:36
EcalTDigitizer< ESDigitizerTraits >::hitResponse
const EcalHitResponse * hitResponse() const
ESDigitizer::ESDigitizer
ESDigitizer(EcalHitResponse *hitResponse, ElectronicsSim *electronicsSim, bool addNoise)
Definition: ESDigitizer.cc:13