CMS 3D CMS Logo

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