CMS 3D CMS Logo

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