CMS 3D CMS Logo

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