CMS 3D CMS Logo

RPCSimAsymmetricCls.cc
Go to the documentation of this file.
5 
10 
11 #include <cmath>
12 
17 
24 
25 #include <cstring>
26 #include <iostream>
27 #include <fstream>
28 #include <string>
29 #include <vector>
30 #include <cstdlib>
31 #include <utility>
32 #include <map>
33 
34 #include "CLHEP/Random/RandFlat.h"
35 #include "CLHEP/Random/RandPoissonQ.h"
36 
37 using namespace std;
38 
40  aveEff = config.getParameter<double>("averageEfficiency");
41  aveCls = config.getParameter<double>("averageClusterSize");
42  resRPC = config.getParameter<double>("timeResolution");
43  timOff = config.getParameter<double>("timingRPCOffset");
44  dtimCs = config.getParameter<double>("deltatimeAdjacentStrip");
45  resEle = config.getParameter<double>("timeJitter");
46  sspeed = config.getParameter<double>("signalPropagationSpeed");
47  lbGate = config.getParameter<double>("linkGateWidth");
48  rpcdigiprint = config.getParameter<bool>("printOutDigitizer");
49  eledig = config.getParameter<bool>("digitizeElectrons");
50 
51  rate = config.getParameter<double>("Rate");
52  nbxing = config.getParameter<int>("Nbxing");
53  gate = config.getParameter<double>("Gate");
54  frate = config.getParameter<double>("Frate");
55 
56  if (rpcdigiprint) {
57  std::cout << "Average Efficiency = " << aveEff << std::endl;
58  std::cout << "Average Cluster Size = " << aveCls << " strips" << std::endl;
59  std::cout << "RPC Time Resolution = " << resRPC << " ns" << std::endl;
60  std::cout << "RPC Signal formation time = " << timOff << " ns" << std::endl;
61  std::cout << "RPC adjacent strip delay = " << dtimCs << " ns" << std::endl;
62  std::cout << "Electronic Jitter = " << resEle << " ns" << std::endl;
63  std::cout << "Signal propagation time = " << sspeed << " x c" << std::endl;
64  std::cout << "Link Board Gate Width = " << lbGate << " ns" << std::endl;
65  }
66 
68 }
69 
71 
72 int RPCSimAsymmetricCls::getClSize(uint32_t id, float posX, CLHEP::HepRandomEngine* engine) {
73  std::vector<double> clsForDetId = getRPCSimSetUp()->getAsymmetricClsDistribution(id, slice(posX));
74 
75  int cnt = 1;
76  int min = 1;
77 
78  double rr_cl = CLHEP::RandFlat::shoot(engine);
79  LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::getClSize] Fired RandFlat :: " << rr_cl;
80  for (unsigned int i = 0; i < clsForDetId.size(); i++) {
81  cnt++;
82  if (rr_cl > clsForDetId[i]) {
83  min = cnt;
84  } else if (rr_cl < clsForDetId[i]) {
85  break;
86  }
87  }
88  return min;
89 }
90 
91 int RPCSimAsymmetricCls::getClSize(float posX, CLHEP::HepRandomEngine* engine) {
92  std::map<int, std::vector<double> > clsMap = getRPCSimSetUp()->getClsMap();
93 
94  int cnt = 1;
95  int min = 1;
96  double func = 0.0;
97  std::vector<double> sum_clsize;
98 
99  double rr_cl = CLHEP::RandFlat::shoot(engine);
100  LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::getClSize] Fired RandFlat :: " << rr_cl;
101 
102  if (0.0 <= posX && posX < 0.2) {
103  func = (clsMap[1])[(clsMap[1]).size() - 1] * (rr_cl);
104  sum_clsize = clsMap[1];
105  }
106  if (0.2 <= posX && posX < 0.4) {
107  func = (clsMap[2])[(clsMap[2]).size() - 1] * (rr_cl);
108  sum_clsize = clsMap[2];
109  }
110  if (0.4 <= posX && posX < 0.6) {
111  func = (clsMap[3])[(clsMap[3]).size() - 1] * (rr_cl);
112  sum_clsize = clsMap[3];
113  }
114  if (0.6 <= posX && posX < 0.8) {
115  func = (clsMap[4])[(clsMap[4]).size() - 1] * (rr_cl);
116  sum_clsize = clsMap[4];
117  }
118  if (0.8 <= posX && posX < 1.0) {
119  func = (clsMap[5])[(clsMap[5]).size() - 1] * (rr_cl);
120  sum_clsize = clsMap[5];
121  }
122 
123  for (vector<double>::iterator iter = sum_clsize.begin(); iter != sum_clsize.end(); ++iter) {
124  cnt++;
125  if (func > (*iter)) {
126  min = cnt;
127  } else if (func < (*iter)) {
128  break;
129  }
130  }
131  return min;
132 }
133 
135  const edm::PSimHitContainer& rpcHits,
136  CLHEP::HepRandomEngine* engine) {
139  theDetectorHitMap.clear();
141 
142  RPCDetId rpcId = roll->id();
143  RPCGeomServ RPCname(rpcId);
144  std::string nameRoll = RPCname.name();
145 
146  const Topology& topology = roll->specs()->topology();
147  for (edm::PSimHitContainer::const_iterator _hit = rpcHits.begin(); _hit != rpcHits.end(); ++_hit) {
148  if (!eledig && _hit->particleType() == 11)
149  continue;
150  // Here I hould check if the RPC are up side down;
151  const LocalPoint& entr = _hit->entryPoint();
152 
153  int time_hit = _rpcSync->getSimHitBx(&(*_hit), engine);
154  float posX = roll->strip(_hit->localPosition()) - static_cast<int>(roll->strip(_hit->localPosition()));
155 
156  std::vector<float> veff = (getRPCSimSetUp())->getEff(rpcId.rawId());
157 
158 #ifdef EDM_ML_DEBUG
159  std::stringstream veffstream;
160  veffstream << "[";
161  for (std::vector<float>::iterator veffIt = veff.begin(); veffIt != veff.end(); ++veffIt) {
162  veffstream << (*veffIt) << ",";
163  }
164  veffstream << "]";
165  std::string veffstr = veffstream.str();
166  LogDebug("RPCSimAsymmetricCls") << "Get Eff from RPCSimSetup for detId = " << rpcId.rawId() << " :: " << veffstr;
167 #endif
168 
169  // Efficiency
170  int centralStrip = topology.channel(entr) + 1;
171  float fire = CLHEP::RandFlat::shoot(engine);
172  LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulate] Fired RandFlat :: " << fire << " --> < "
173  << veff[centralStrip - 1] << " ? --> " << ((fire < veff[centralStrip - 1]) ? 1 : 0);
174 
175  if (fire < veff[centralStrip - 1]) {
176  LogDebug("RPCSimAsymmetricCls") << "Detector is Efficient for this simhit";
177 
178  int fstrip = centralStrip;
179  int lstrip = centralStrip;
180 
181  // Compute the cluster size
182 
183  //double w = CLHEP::RandFlat::shoot(engine);
184  //LogDebug ("RPCSimAsymmetricCls")<<"[RPCSimAsymmetricCls::simulate] Fired RandFlat :: "<<w<<" (w is not used)";
185  //if (w < 1.e-10) w=1.e-10;
186 
187  int clsize = this->getClSize(rpcId.rawId(), posX, engine); // This is for cluster size chamber by chamber
188  LogDebug("RPCSimAsymmetricCls") << "Clustersize = " << clsize;
189 
190  std::vector<int> cls;
191 
192  cls.push_back(centralStrip);
193  if (clsize > 1) {
194  for (int cl = 0; cl < (clsize - 1) / 2; cl++) {
195  if (centralStrip - cl - 1 >= 1) {
196  fstrip = centralStrip - cl - 1;
197  cls.push_back(fstrip);
198  }
199  if (centralStrip + cl + 1 <= roll->nstrips()) {
200  lstrip = centralStrip + cl + 1;
201  cls.push_back(lstrip);
202  }
203  }
204  if (clsize % 2 == 0) { //even cluster size is a special case
205  if (clsize > 5) {
206  // insert the last strip according to the
207  // simhit position in the central strip
208  // needed for cls > 5, because higher cluster size has no asymmetry
209  // and thus is treated like in the old parametrization
210  double deltaw = roll->centreOfStrip(centralStrip).x() - entr.x();
211  if (deltaw < 0.) {
212  if (lstrip < roll->nstrips()) {
213  lstrip++;
214  cls.push_back(lstrip);
215  }
216  } else {
217  if (fstrip > 1) {
218  fstrip--;
219  cls.push_back(fstrip);
220  }
221  }
222  } else {
223  // needed for correct initial position for even cluster size
224  // in case of asymmetric cluster size
225  if (lstrip < roll->nstrips()) {
226  lstrip++;
227  cls.push_back(lstrip);
228  }
229  }
230  }
231  }
232 
233  //Now calculate the shift according to the distribution
234  float fire1 = CLHEP::RandFlat::shoot(engine);
235  LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulate] Fired RandFlat :: " << fire1
236  << " (fire1 is used for a shift of the cluster)";
237 
238  int strip_shift = 0;
239 
240  int offset;
241 
242  if (clsize % 2 == 0) {
243  offset = 2;
244  } else {
245  offset = 1;
246  }
247 
248  //No shift (asymmetry) for higher cluster size.
249  if (clsize > 5) {
250  strip_shift = 0;
251  } else {
252  std::vector<double> TMPclsAsymmForDetId = getRPCSimSetUp()->getAsymmetryForCls(rpcId, slice(posX), clsize);
253 
254  for (unsigned int i = 0; i < TMPclsAsymmForDetId.size(); i++) {
255  if (fire1 < TMPclsAsymmForDetId[i]) {
256  strip_shift = i - offset;
257  break;
258  }
259  }
260  }
261 
262  vector<int> shifted_cls; // vector to hold shifted strips
263  shifted_cls.clear();
264 
265  int min_strip = 100;
266  int max_strip = 0;
267 
268  //correction for the edges
269  for (std::vector<int>::iterator i = cls.begin(); i != cls.end(); i++) {
270  if (*i + strip_shift < min_strip) {
271  min_strip = *i + strip_shift;
272  }
273  if (*i + strip_shift > max_strip) {
274  max_strip = *i + strip_shift;
275  }
276  }
277 
278  if (min_strip < 1 || max_strip - roll->nstrips() > 0) {
279  strip_shift = 0;
280  }
281 
282  //Now shift the cluster
283  for (std::vector<int>::iterator i = cls.begin(); i != cls.end(); i++) {
284  shifted_cls.push_back(*i + strip_shift);
285  }
286  for (std::vector<int>::iterator i = shifted_cls.begin(); i != shifted_cls.end(); i++) {
287  // Check the timing of the adjacent strip
288  if (*i != centralStrip) {
289  double fire2 = CLHEP::RandFlat::shoot(engine);
290  LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulate] Fired RandFlat :: " << fire2
291  << " (check whether adjacent strips are efficient)";
292  if (fire2 < veff[*i - 1]) {
293  std::pair<int, int> digi(*i, time_hit);
294  strips.insert(digi);
295  LogDebug("RPCSimAsymmetricCls")
296  << "RPC Digi inserted :: Signl :: DetId :: " << rpcId << " = " << rpcId.rawId() << " ==> digi <"
297  << digi.first << "," << digi.second << ">";
298 
299  theDetectorHitMap.insert(DetectorHitMap::value_type(digi, &(*_hit)));
300  }
301  } else {
302  std::pair<int, int> digi(*i, time_hit);
303  theDetectorHitMap.insert(DetectorHitMap::value_type(digi, &(*_hit)));
304 
305  strips.insert(digi);
306  LogDebug("RPCSimAsymmetricCls") << "RPC Digi inserted :: Signl :: DetId :: " << rpcId << " = "
307  << rpcId.rawId() << " ==> digi <" << digi.first << "," << digi.second << ">";
308  }
309  }
310  }
311  }
312 }
313 
314 void RPCSimAsymmetricCls::simulateNoise(const RPCRoll* roll, CLHEP::HepRandomEngine* engine) {
315  RPCDetId rpcId = roll->id();
316 
317  RPCGeomServ RPCname(rpcId);
318  std::string nameRoll = RPCname.name();
319 
320  std::vector<float> vnoise = (getRPCSimSetUp())->getNoise(rpcId.rawId());
321  std::vector<float> veff = (getRPCSimSetUp())->getEff(rpcId.rawId());
322 
323  LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulateNoise] Treating DetId :: " << rpcId << " = "
324  << rpcId.rawId() << " which has " << roll->nstrips() << " strips";
325 
326 #ifdef EDM_ML_DEBUG
327  std::stringstream vnoisestream;
328  vnoisestream << "[";
329  for (std::vector<float>::iterator vnoiseIt = vnoise.begin(); vnoiseIt != vnoise.end(); ++vnoiseIt) {
330  vnoisestream << (*vnoiseIt) << ",";
331  }
332  vnoisestream << "]";
333  std::string vnoisestr = vnoisestream.str();
334  LogDebug("RPCSimAsymmetricCls") << "Get Noise from RPCSimSetup for detId = " << rpcId.rawId() << " :: vector with "
335  << vnoise.size() << "entries :: " << vnoisestr;
336 #endif
337 
338  unsigned int nstrips = roll->nstrips();
339  double area = 0.0;
340 
341  if (rpcId.region() == 0) {
342  const RectangularStripTopology* top_ = dynamic_cast<const RectangularStripTopology*>(&(roll->topology()));
343  float xmin = (top_->localPosition(0.)).x();
344  float xmax = (top_->localPosition((float)roll->nstrips())).x();
345  float striplength = (top_->stripLength());
346  area = striplength * (xmax - xmin);
347  } else {
348  const TrapezoidalStripTopology* top_ = dynamic_cast<const TrapezoidalStripTopology*>(&(roll->topology()));
349  float xmin = (top_->localPosition(0.)).x();
350  float xmax = (top_->localPosition((float)roll->nstrips())).x();
351  float striplength = (top_->stripLength());
352  area = striplength * (xmax - xmin);
353  }
354 
355  LogDebug("RPCSimAsymmetricCls") << "Noise :: vnoise.size() = " << vnoise.size();
356 
357  for (unsigned int j = 0; j < vnoise.size(); ++j) {
358  if (j >= nstrips)
359  break;
360 
361  // The efficiency of 0% does not imply on the noise rate.
362  // If the strip is masked the noise rate should be 0 Hz/cm^2
363  // if(veff[j] == 0) continue;
364 
365  // double ave = vnoise[j]*nbxing*gate*area*1.0e-9*frate;
366  // The vnoise is the noise rate per strip, so we shout multiply not
367  // by the chamber area,
368  // but the strip area which is area/((float)roll->nstrips()));
369  double ave = vnoise[j] * nbxing * gate * area * 1.0e-9 * frate / ((float)roll->nstrips());
370  LogDebug("RPCSimAsymmetricCls") << "Noise :: strip " << j << " Average = " << ave
371  << " = vnoise[j]*nbxing*gate*area*1.0e-9*frate/((float)roll->nstrips()) = "
372  << vnoise[j] << "*" << nbxing << "*" << gate << "*" << area << "*" << 1.0e-9 << "*"
373  << frate << "/" << ((float)roll->nstrips());
374 
375  CLHEP::RandPoissonQ randPoissonQ(*engine, ave);
376  N_hits = randPoissonQ.fire();
377  LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulateNoise] Fired RandPoissonQ :: " << N_hits;
378  LogDebug("RPCSimAsymmetricCls") << "Noise :: Amount of Noise Hits for DetId :: " << rpcId << " = " << rpcId.rawId()
379  << " = N_hits = randPoissonQ.fire() = " << N_hits;
380 
381  for (int i = 0; i < N_hits; i++) {
382  double time2 = CLHEP::RandFlat::shoot((nbxing * gate) / gate);
383  LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulateNoise] Fired RandFlat :: " << time2;
384  int time_hit = (static_cast<int>(time2) - nbxing / 2);
385  std::pair<int, int> digi(j + 1, time_hit);
386  strips.insert(digi);
387  LogDebug("RPCSimAsymmetricCls") << "RPC Digi inserted :: Noise :: DetId :: " << rpcId << " = " << rpcId.rawId()
388  << " ==> digi <" << digi.first << "," << digi.second << ">";
389  }
390  }
391 }
392 
393 unsigned int RPCSimAsymmetricCls::slice(float posX) {
394  if (0.0 <= posX && posX < 0.2) {
395  return 0;
396  } else if (0.2 <= posX && posX < 0.4) {
397  return 1;
398  } else if (0.4 <= posX && posX < 0.6) {
399  return 2;
400  } else if (0.6 <= posX && posX < 0.8) {
401  return 3;
402  } else if (0.8 <= posX && posX < 1.0) {
403  return 4;
404  } else
405  return 2;
406 }
void simulateNoise(const RPCRoll *, CLHEP::HepRandomEngine *) override
LocalPoint localPosition(float strip) const override
DetectorHitMap theDetectorHitMap
Definition: RPCSim.h:68
void setRPCSimSetUp(RPCSimSetUp *simsetup)
const std::vector< double > & getAsymmetricClsDistribution(uint32_t id, uint32_t slice)
Definition: RPCSimSetUp.cc:476
RPCSimSetUp * getRPCSimSetUp()
Definition: RPCSim.h:45
Definition: config.py:1
int nstrips() const
Definition: RPCRoll.cc:24
const RPCRollSpecs * specs() const
Definition: RPCRoll.cc:14
int getSimHitBx(const PSimHit *, CLHEP::HepRandomEngine *)
float stripLength() const override
T x() const
Definition: PV3DBase.h:59
std::vector< double > clsForDetId
edm::DetSet< RPCDigiSimLink > RPCDigiSimLinks
Definition: RPCSim.h:33
std::map< int, std::vector< double > > clsMap
virtual std::string name()
Definition: RPCGeomServ.cc:11
std::set< std::pair< int, int > > strips
Definition: RPCSim.h:55
std::vector< double > sum_clsize
const std::map< int, std::vector< double > > & getClsMap()
Definition: RPCSimSetUp.cc:449
virtual int channel(const LocalPoint &p) const =0
Definition: RPCSim.h:30
const Topology & topology() const override
Definition: RPCRollSpecs.cc:36
void simulate(const RPCRoll *roll, const edm::PSimHitContainer &rpcHits, CLHEP::HepRandomEngine *) override
const std::vector< double > & getAsymmetryForCls(uint32_t id, uint32_t slice, uint32_t cls)
Definition: RPCSimSetUp.cc:540
int getClSize(float posX, CLHEP::HepRandomEngine *)
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
RPCSynchronizer * _rpcSync
int region() const
Region id: 0 for Barrel, +/-1 For +/- Endcap.
Definition: RPCDetId.h:53
RPCSimAsymmetricCls(const edm::ParameterSet &config)
const Topology & topology() const override
Definition: RPCRoll.cc:18
float strip(const LocalPoint &lp) const
Definition: RPCRoll.cc:35
RPCDetId id() const
Definition: RPCRoll.cc:16
void clear()
Definition: DetSet.h:71
std::vector< PSimHit > PSimHitContainer
float stripLength() const override
det heigth (strip length in the middle)
RPCDigiSimLinks theRpcDigiSimLinks
Definition: RPCSim.h:70
LocalPoint localPosition(float strip) const override
unsigned int slice(float posX)
#define LogDebug(id)
LocalPoint centreOfStrip(int strip) const
Definition: RPCRoll.cc:26