test
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 
194  //double w = CLHEP::RandFlat::shoot(engine);
195  //LogDebug ("RPCSimAsymmetricCls")<<"[RPCSimAsymmetricCls::simulate] Fired RandFlat :: "<<w<<" (w is not used)";
196  //if (w < 1.e-10) w=1.e-10;
197 
198  int clsize = this->getClSize(rpcId.rawId(),posX, engine); // This is for cluster size chamber by chamber
199  LogDebug ("RPCSimAsymmetricCls")<<"Clustersize = "<<clsize;
200 
201  std::vector<int> cls;
202 
203  cls.push_back(centralStrip);
204  if (clsize > 1){
205  for (int cl = 0; cl < (clsize-1)/2; cl++){
206  if (centralStrip - cl -1 >= 1 ){
207  fstrip = centralStrip-cl-1;
208  cls.push_back(fstrip);
209  }
210  if (centralStrip + cl + 1 <= roll->nstrips() ){
211  lstrip = centralStrip+cl+1;
212  cls.push_back(lstrip);
213  }
214  }
215  if (clsize%2 == 0){ //even cluster size is a special case
216  if(clsize>5){
217  // insert the last strip according to the
218  // simhit position in the central strip
219  // needed for cls > 5, because higher cluster size has no asymmetry
220  // and thus is treated like in the old parametrization
221  double deltaw=roll->centreOfStrip(centralStrip).x()-entr.x();
222  if (deltaw<0.) {
223  if (lstrip < roll->nstrips() ){
224  lstrip++;
225  cls.push_back(lstrip);
226  }
227  }else{
228  if (fstrip > 1 ){
229  fstrip--;
230  cls.push_back(fstrip);
231  }
232  }
233  }
234  else {
235  // needed for correct initial position for even cluster size
236  // in case of asymmetric cluster size
237  if (lstrip < roll->nstrips() ){
238  lstrip++;
239  cls.push_back(lstrip);
240  }
241  }
242  }
243  }
244 
245  //Now calculate the shift according to the distribution
246  float fire1 = CLHEP::RandFlat::shoot(engine);
247  LogDebug ("RPCSimAsymmetricCls")<<"[RPCSimAsymmetricCls::simulate] Fired RandFlat :: "<<fire1<<" (fire1 is used for a shift of the cluster)";
248 
249  int strip_shift=0;
250 
251  int offset;
252 
253  if(clsize%2==0){
254  offset = 2;
255  }
256  else {
257  offset = 1;
258  }
259 
260  //No shift (asymmetry) for higher cluster size.
261  if(clsize>5){
262  strip_shift = 0;
263  }
264  else {
265  std::vector<double> TMPclsAsymmForDetId
266  = getRPCSimSetUp()->getAsymmetryForCls(rpcId,slice(posX),clsize);
267 
268  for(unsigned int i = 0; i < TMPclsAsymmForDetId.size(); i ++){
269  if(fire1 < TMPclsAsymmForDetId[i]){
270  strip_shift = i - offset;
271  break;
272  }
273  }
274  }
275 
276  vector<int> shifted_cls; // vector to hold shifted strips
277  shifted_cls.clear();
278 
279  int min_strip=100;
280  int max_strip=0;
281 
282  //correction for the edges
283  for (std::vector<int>::iterator i=cls.begin(); i!=cls.end();i++){
284  if(*i+strip_shift < min_strip){
285  min_strip = *i+strip_shift;
286  }
287  if(*i+strip_shift > max_strip){
288  max_strip = *i+strip_shift;
289  }
290  }
291 
292  if(min_strip<1 || max_strip-roll->nstrips()>0){
293  strip_shift = 0;
294  }
295 
296  //Now shift the cluster
297  for (std::vector<int>::iterator i=cls.begin(); i!=cls.end();i++){
298  shifted_cls.push_back(*i+strip_shift);
299  }
300  for (std::vector<int>::iterator i=shifted_cls.begin();
301  i!=shifted_cls.end();i++){
302  // Check the timing of the adjacent strip
303  if(*i != centralStrip){
304  double fire2 = CLHEP::RandFlat::shoot(engine);
305  LogDebug ("RPCSimAsymmetricCls")<<"[RPCSimAsymmetricCls::simulate] Fired RandFlat :: "<<fire2<<" (check whether adjacent strips are efficient)";
306  if(fire2 < veff[*i-1]){
307  std::pair<int, int> digi(*i,time_hit);
308  strips.insert(digi);
309  LogDebug ("RPCSimAsymmetricCls")<<"RPC Digi inserted :: Signl :: DetId :: "<<rpcId<<" = "<<rpcId.rawId()<<" ==> digi <"<<digi.first<<","<<digi.second<<">";
310 
311  theDetectorHitMap.insert(DetectorHitMap::value_type(digi,&(*_hit)));
312  }
313  }
314  else {
315  std::pair<int, int> digi(*i,time_hit);
316  theDetectorHitMap.insert(DetectorHitMap::value_type(digi,&(*_hit)));
317 
318  strips.insert(digi);
319  LogDebug ("RPCSimAsymmetricCls")<<"RPC Digi inserted :: Signl :: DetId :: "<<rpcId<<" = "<<rpcId.rawId()<<" ==> digi <"<<digi.first<<","<<digi.second<<">";
320  }
321  }
322  }
323  }
324 }
325 
327  CLHEP::HepRandomEngine* engine)
328 {
329 
330  RPCDetId rpcId = roll->id();
331 
332  RPCGeomServ RPCname(rpcId);
333  std::string nameRoll = RPCname.name();
334 
335  std::vector<float> vnoise = (getRPCSimSetUp())->getNoise(rpcId.rawId());
336  std::vector<float> veff = (getRPCSimSetUp())->getEff(rpcId.rawId());
337 
338  LogDebug ("RPCSimAsymmetricCls")<<"[RPCSimAsymmetricCls::simulateNoise] Treating DetId :: "<<rpcId<<" = "<<rpcId.rawId()<<" which has "<<roll->nstrips()<<" strips";
339 
340  std::stringstream vnoisestream; vnoisestream<<"[";
341  for(std::vector<float>::iterator vnoiseIt = vnoise.begin(); vnoiseIt != vnoise.end(); ++vnoiseIt) { vnoisestream<<(*vnoiseIt)<<","; }
342  vnoisestream<<"]";
343  std::string vnoisestr = vnoisestream.str();
344  LogDebug("RPCSimAsymmetricCls")<<"Get Noise from RPCSimSetup for detId = "<<rpcId.rawId()<<" :: vector with "<<vnoise.size()<<"entries :: "<<vnoisestr;
345 
346 
347  unsigned int nstrips = roll->nstrips();
348  double area = 0.0;
349 
350  if ( rpcId.region() == 0 )
351  {
352  const RectangularStripTopology* top_ = dynamic_cast<const
353  RectangularStripTopology*>(&(roll->topology()));
354  float xmin = (top_->localPosition(0.)).x();
355  float xmax = (top_->localPosition((float)roll->nstrips())).x();
356  float striplength = (top_->stripLength());
357  area = striplength*(xmax-xmin);
358  }
359  else
360  {
361  const TrapezoidalStripTopology* top_=dynamic_cast<const TrapezoidalStripTopology*>(&(roll->topology()));
362  float xmin = (top_->localPosition(0.)).x();
363  float xmax = (top_->localPosition((float)roll->nstrips())).x();
364  float striplength = (top_->stripLength());
365  area = striplength*(xmax-xmin);
366  }
367 
368  LogDebug ("RPCSimAsymmetricCls")<<"Noise :: vnoise.size() = "<<vnoise.size();
369 
370  for(unsigned int j = 0; j < vnoise.size(); ++j){
371 
372  if(j >= nstrips) break;
373 
374  // The efficiency of 0% does not imply on the noise rate.
375  // If the strip is masked the noise rate should be 0 Hz/cm^2
376  // if(veff[j] == 0) continue;
377 
378  // double ave = vnoise[j]*nbxing*gate*area*1.0e-9*frate;
379  // The vnoise is the noise rate per strip, so we shout multiply not
380  // by the chamber area,
381  // but the strip area which is area/((float)roll->nstrips()));
382  double ave = vnoise[j]*nbxing*gate*area*1.0e-9*frate/((float)roll->nstrips());
383  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());
384 
385  CLHEP::RandPoissonQ randPoissonQ(*engine, ave);
386  N_hits = randPoissonQ.fire();
387  LogDebug ("RPCSimAsymmetricCls")<<"[RPCSimAsymmetricCls::simulateNoise] Fired RandPoissonQ :: "<<N_hits;
388  LogDebug ("RPCSimAsymmetricCls")<<"Noise :: Amount of Noise Hits for DetId :: "<<rpcId<<" = "<<rpcId.rawId()<<" = N_hits = randPoissonQ.fire() = "<<N_hits;
389 
390  for (int i = 0; i < N_hits; i++ ){
391  double time2 = CLHEP::RandFlat::shoot((nbxing*gate)/gate);
392  LogDebug ("RPCSimAsymmetricCls")<<"[RPCSimAsymmetricCls::simulateNoise] Fired RandFlat :: "<<time2;
393  int time_hit = (static_cast<int>(time2) - nbxing/2);
394  std::pair<int, int> digi(j+1,time_hit);
395  strips.insert(digi);
396  LogDebug ("RPCSimAsymmetricCls")<<"RPC Digi inserted :: Noise :: DetId :: "<<rpcId<<" = "<<rpcId.rawId()<<" ==> digi <"<<digi.first<<","<<digi.second<<">";
397  }
398  }
399 }
400 
401 unsigned int RPCSimAsymmetricCls::slice(float posX){
402 
403  if(0.0 <= posX && posX < 0.2) {
404  return 0;
405  }
406  else if(0.2 <= posX && posX < 0.4) {
407  return 1;
408  }
409  else if(0.4 <= posX && posX < 0.6) {
410  return 2;
411  }
412  else if(0.6 <= posX && posX < 0.8) {
413  return 3;
414  }
415  else if(0.8 <= posX && posX < 1.0) {
416  return 4;
417  }
418  else return 2;
419 }
#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
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
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:145
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