CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RPCStripsRing.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: RPCConeBuilder
4 // Class : RPCStripsRing
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Tomasz Fruboes
10 // Created: Tue Feb 26 15:13:10 CET 2008
11 //
12 
13 // system include files
14 
15 // user include files
16 //#include "L1TriggerConfig/RPCConeBuilder/interface/RPCStripsRing.h"
22 
24  : m_hwPlane(-1),
25  m_etaPartition(99),
26  m_region(-2),
27  m_isReferenceRing(false),
28  m_didVirtuals(false),
29  m_didFiltering(false) {}
30 
31 RPCStripsRing::RPCStripsRing(const RPCRoll* roll, std::shared_ptr<L1RPCConeBuilder::TConMap> cmap)
32  : m_didVirtuals(false), m_didFiltering(false), m_connectionsMap(cmap) {
33  RPCDetId detId = roll->id();
34  RPCGeomServ grs(detId);
35 
38 
39  m_isReferenceRing = false;
40 
41  m_region = detId.region();
42 
43  int ring = detId.ring();
44 
45  if (m_region == 0 && std::abs(ring) < 2 && m_hwPlane == 2) // for barell wheel -1,0,1 refplane is hwPlane=2
46  m_isReferenceRing = true;
47  else if (m_region == 0 && std::abs(ring) == 2 && m_hwPlane == 6) // for barell wheel -2,2 refplane is hwPlane=6
48  m_isReferenceRing = true;
49  else if (m_region != 0 && m_hwPlane == 2) // for endcaps
50  m_isReferenceRing = true;
51 
52  if (getRingId() == 2008 || getRingId() == 2108) //exception: endcaps;hwplane 2;farest roll from beam
53  m_isReferenceRing = false;
54 
55  addRoll(roll);
56 }
57 
58 void RPCStripsRing::addRoll(const RPCRoll* roll) {
59  // RPCDetId detId = roll->id();
60 
61  if (getRingId() != getRingId(roll)) {
62  throw cms::Exception("RPCInternal") << "RPCStripsRing::addRoll ringsIds dont match \n";
63  }
64 
65  //iterate over the strips of this roll
66  for (int i = 1; i <= roll->nstrips(); i++) {
67  LocalPoint lStripCentre = roll->centreOfStrip(i);
68  GlobalPoint gStripCentre = roll->toGlobal(lStripCentre);
69  float phiRaw = gStripCentre.phi();
70 
71  TStrip newStrip(roll->id().rawId(), i);
72  (*this)[phiRaw] = newStrip;
73  }
74 }
75 
76 int RPCStripsRing::getRingId(int etaPart, int hwPlane) {
77  int sign = 1; // positive
78  if (etaPart < 0) {
79  sign = 0;
80  }
81 
82  return 1000 * (hwPlane) + //1...6
83  100 * (sign) + //
84  1 * (std::abs(etaPart)); //-17...17
85 }
86 
88 
90  RPCDetId detId = roll->id();
91  RPCGeomServ grs(detId);
92  int etaPartition = grs.eta_partition();
93  int hwPlane = calculateHwPlane(roll);
94 
95  return getRingId(etaPartition, hwPlane);
96 }
97 
98 // hwPlane is station number for endcaps
99 // for barrell numbering goes 1 5 2 6 3 4 (first number means plane closest to the beam)
101  int hwPlane = -1;
102  RPCDetId detId = roll->id();
103  int station = detId.station();
104  int layer = detId.layer();
105  int region = detId.region();
106 
107  if (region != 0) { // endcaps
108  hwPlane = station;
109  }
110  // Now comes the barell
111  else if (station > 2) {
112  hwPlane = station;
113  } else if (station == 1 && layer == 1) {
114  hwPlane = 1;
115  } else if (station == 1 && layer == 2) {
116  hwPlane = 5;
117  } else if (station == 2 && layer == 1) {
118  hwPlane = 2;
119  } else if (station == 2 && layer == 2) {
120  hwPlane = 6;
121  }
122 
123  /*if (hwPlane < 1)
124  std::cout << "prb: " << hwPlane << " "
125  << region << " "
126  << station << " "
127  << layer << std::endl;*/
128  if (hwPlane < 0) {
129  throw cms::Exception("RPCInternal") << "Calculated negative hwplane \n";
130  }
131 
132  return hwPlane;
133 }
134 
136  if (m_didFiltering)
137  return;
138  m_didFiltering = true;
139 
140  if (m_region != 0 || m_hwPlane != 4)
141  return;
142 
143  typedef std::map<uint32_t, int> TDetId2StripNo;
144  TDetId2StripNo det2stripNo;
145 
146  // Note: we begin in middle of first chamber (ch1), we have to handle that
147  int ch1BegStrips = 0; // no of strips on the begining of the map (first=last chamber of map)
148  int ch1EndStrips = 0; // no of strips on the end of the map (first=last chamber of map)
149 
150  // How many strips has each chamber?
151  RPCStripsRing::iterator it = this->begin();
152  uint32_t ch1Det = it->second.m_detRawId;
153  for (; it != this->end(); ++it) {
154  if (det2stripNo.find(it->second.m_detRawId) == det2stripNo.end()) {
155  det2stripNo[it->second.m_detRawId] = 1; // Add new chamber to a map, set strip cnt to 1
156  } else {
157  ++det2stripNo[it->second.m_detRawId]; // Increase strip count of a chamber
158  }
159 
160  if (det2stripNo.size() == 1 && ch1Det == it->second.m_detRawId) {
161  ++ch1BegStrips;
162  } else if (ch1Det == it->second.m_detRawId) {
163  ++ch1EndStrips;
164  }
165  }
166 
167  det2stripNo[ch1Det] -= ch1EndStrips;
168 
169  // std::cout << ch1BegStrips << " " << ch1EndStrips << std::endl;
170 
171  //TDetId2StripNo::iterator itIds = det2stripNo.begin();
172  //for(;itIds!=det2stripNo.end();++itIds){
173  // std::cout << itIds->first << " " << itIds->second << std::endl;
174  // }
175 
176  it = this->begin();
177  uint32_t lastDet = it->second.m_detRawId;
178  while (it != this->end()) {
179  if (det2stripNo[it->second.m_detRawId] < 0) {
180  throw cms::Exception("RPCInternal") << " RPCStripsRing::filterOverlapingChambers() - no strips left \n";
181  }
182  if (it->second.m_detRawId == lastDet) {
183  --det2stripNo[lastDet];
184  ++it;
185  } else if (det2stripNo[lastDet] == 0) { // no more strips left in lastDet, proceed to new det
186 
187  if (lastDet == ch1Det) {
188  det2stripNo[ch1Det] += ch1EndStrips;
189  }
190 
191  lastDet = it->second.m_detRawId;
192  --det2stripNo[lastDet];
193  ++it;
194  } else { // there are still strips in last det, delete current strip
195  --det2stripNo[it->second.m_detRawId];
196  RPCStripsRing::iterator itErase = it;
197  ++it;
198  //std::cout << "Removing strip " << it->second.m_detRawId << " " << (int)it->second.m_strip << std::endl;
199  this->erase(itErase);
200  }
201  }
202 }
203 
205  if (m_didVirtuals)
206  return;
207  m_didVirtuals = true;
208 
209  const float pi = 3.141592654;
210  double dphi = 2.0 * pi / 1152; // defines angular granulation of strips.
211 
212  RPCStripsRing stripsToInsert;
213 
214  float delta = 0;
215  int stripsToAdd = 0;
216 
217  RPCStripsRing::iterator it = this->begin();
218  RPCStripsRing::iterator itLast = this->begin();
219  for (; it != this->end(); ++it) {
220  /*std::cout << it->first << " "
221  << it->second.m_detRawId << " "
222  << (int)it->second.m_strip << std::endl;
223  */
224 
225  delta = it->first - itLast->first;
226  if (it == itLast || // skip first loop iteration
227  itLast->second.m_detRawId == it->second.m_detRawId || // insert strips between two chambers only
228  delta < 0) {
229  itLast = it;
230  continue;
231  }
232 
233  stripsToAdd = (int)std::floor(delta / dphi) - 1;
234  //std::cout << delta << " " << stripsToAdd << std::endl;
235 
236  if (isReferenceRing() && m_hwPlane == 6)
237  ++stripsToAdd;
238 
239  for (int i = 0; i < stripsToAdd; ++i) {
240  stripsToInsert[itLast->first + dphi * (i + 1)] = TStrip();
241  }
242 
243  itLast = it;
244  }
245  // TODO: check delta between first and last strip in map
246 
247  this->insert(stripsToInsert.begin(), stripsToInsert.end());
248 }
249 void RPCStripsRing::createRefConnections(TOtherConnStructVec& otherRings, int logplane, int logplaneSize) {
250  //*
251  /*std::cout << "RefCon for " << getRingId()
252  << " (" << getEtaPartition()<< ")"
253  << " tower: " << getTowerForRefRing()
254  << " ; connected: "
255  << otherRings.size()
256  << std::endl
257  << std::endl;
258  //*/
259 
260  // XXX - TODO: warning on wrong logplaneSize
261 
262  if (!this->isReferenceRing()) {
263  throw cms::Exception("RPCInternal") << " RPCStripsRing::createRefConnections "
264  << " called for non-reference ring \n";
265  }
266 
267  /*
268  if (logplaneSize!=8) {
269  throw cms::Exception("RPCInternal") << " RPCStripsRing::createRefConnections "
270  << " called for lpSize " << logplaneSize << " \n";
271 
272  }*/
273  const float pi = 3.141592654;
274  const float offset = (5. / 360.) * 2 * pi; // XXX
275 
276  //find first reference strip of first PAC (the strip with phi ~= 5deg)
277  RPCStripsRing::iterator starEndIt = this->begin();
278  while ((++starEndIt)->first < offset)
279  ;
280 
281  RPCStripsRing::iterator it = starEndIt;
282  //--starEndIt;
283 
284  float angle = 0;
285  int curPACno = -1;
286  int curStripNo = 0;
287  int curBegStripNo = 0;
288 
289  bool firstIter = true;
290 
291  while (it != starEndIt || firstIter) { // iterate over strips
292 
293  firstIter = false;
294  // New PAC
295  if (curStripNo % logplaneSize == 0) {
296  ++curPACno;
297  curBegStripNo = curStripNo;
298  RPCStripsRing::iterator plus8 = it;
299  bool skipOccured = false;
300  for (int i = 0; i < 7; ++i) {
301  ++plus8;
302  if (plus8 == this->end()) {
303  plus8 = this->begin();
304  skipOccured = true;
305  }
306  }
307 
308  // calculate angle
309  float phi = it->first;
310  float phiP8 = plus8->first;
311  if (skipOccured) {
312  // phiP8 is negative
313  // phi is positive
314  // xcheck
315  if (phi * phiP8 > 0) {
316  throw cms::Exception("RPCInternal") << " RPCStripsRing::createRefConnections phi/phi8 error \n";
317  }
318  angle = (2 * pi + phiP8 + phi) / 2;
319  if (angle > pi) { // should land on positive side
320  angle -= 2 * pi;
321  }
322 
323  if (std::abs(angle) > pi) {
324  throw cms::Exception("RPCInternal") << " RPCStripsRing::createRefConnections "
325  << " problem with angle calc \n";
326  }
327  } else {
328  angle = (phiP8 + phi) / 2;
329  }
330  //std::cout << curPACno << " " << phiP8 << " " << phi << " " << angle << std::endl;
331 
332  TOtherConnStructVec::iterator itOt = otherRings.begin();
333  for (; itOt != otherRings.end(); ++itOt) {
334  itOt->m_it->second.createOtherConnections(
335  getTowerForRefRing(), curPACno, itOt->m_logplane, itOt->m_logplaneSize, angle);
336  }
337  }
338 
339  if (!it->second.isVirtual()) {
341  newCon.m_tower = getTowerForRefRing();
342  newCon.m_PAC = curPACno;
343  newCon.m_logplane = logplane;
344  newCon.m_logstrip = curStripNo - curBegStripNo;
345  //std::cout << " Adding con for " << it->second.m_detRawId << std::endl;
346  (*m_connectionsMap)[it->second.m_detRawId][it->second.m_strip].push_back(newCon);
347  //std::cout << " Adding ref connection " << std::endl;
348  }
349  ++curStripNo;
350  ++it;
351  if (it == this->end()) {
352  it = this->begin();
353  }
354 
355  } // iteration over strips ends
356 
357  //std::cout << " refcon: " << curPACno << " PACs" << std::endl;
358  //std::cout << "After refCon: " << m_connectionsMap.size() << std::endl;
359 }
360 
361 void RPCStripsRing::createOtherConnections(int tower, int PACno, int logplane, int logplaneSize, float angle) {
362  //std::cout << " OtherCon for " << getRingId() << std::endl;
363 
364  if (this->isReferenceRing()) {
365  throw cms::Exception("RPCInternal") << " RPCStripsRing::createOtherConnections "
366  << " called for reference ring \n";
367  }
368 
369  RPCStripsRing::const_iterator it = this->lower_bound(angle);
370 
371  if (it == this->end())
372  it = this->begin();
373 
374  for (int i = 0; i < logplaneSize / 2; i++) {
375  if (it == this->begin())
376  it = this->end(); // (m_stripPhiMap.end()--) is ok.
377 
378  --it;
379  }
380 
381  for (int i = 0; i < logplaneSize; i++) {
382  if (!it->second.isVirtual()) {
384  newCon.m_tower = tower;
385  newCon.m_PAC = PACno;
386  newCon.m_logplane = logplane;
387  newCon.m_logstrip = i;
388  (*m_connectionsMap)[it->second.m_detRawId][it->second.m_strip].push_back(newCon);
389  //std::cout << " Adding other connection " << std::endl;
390  }
391 
392  ++it;
393  if (it == this->end())
394  it = this->begin();
395  }
396 }
397 
398 // Defines to which tower this ring (only ref ring) belongs
400  int ret = 0;
401 
402  if (!this->isReferenceRing()) {
403  throw cms::Exception("RPCInternal") << " RPCStripsRing::getTowerForRefRing() "
404  << " called for non reference ring \n";
405  }
406 
407  int etaAbs = std::abs(getEtaPartition());
408  if (etaAbs < 8) {
409  ret = getEtaPartition();
410  } else if (etaAbs > 8) {
411  int sign = (getEtaPartition() > 0 ? 1 : -1);
412  ret = getEtaPartition() - sign;
413  } else {
414  throw cms::Exception("RPCInternal") << " RPCStripsRing::getTowerForRefRing() "
415  << " called for etaPartition 8 \n";
416  }
417 
418  return ret;
419 }
420 /*
421  struct TStripCon{
422  signed char m_tower;
423  unsigned char m_PAC;
424  unsigned char m_logplane;
425  unsigned char m_logstrip;
426  };
427  typedef std::vector<TStripCon> TStripConVec;
428  typedef std::map<unsigned char, TStripConVec> TStrip2ConVec;
429  typedef std::map<uint32_t, TStrip2ConVec> TConMap;
430 
431  // compressed connections
432  struct TCompressedCon{
433  signed char m_tower;
434  unsigned char m_PAC;
435  signed char m_offset;
436  signed char m_mul;
437  };
438  typedef std::vector<TCompressedCon> TCompressedConVec;
439  typedef std::map<uint32_t, TCompressedConVec> TCompressedConMap;
440 
441 */
442 
444  L1RPCConeBuilder::TConMap::iterator itChamber = m_connectionsMap->begin();
445 
446  auto uncompressedConsLeft = std::make_shared<L1RPCConeBuilder::TConMap>();
447 
448  m_compressedConnectionMap = std::make_shared<L1RPCConeBuilder::TCompressedConMap>();
449 
450  int compressedCons = 0, uncompressedConsBefore = 0, uncompressedConsAfter = 0;
451 
452  // int offsetMin =0, offsetMax =0;
453 
454  for (; itChamber != m_connectionsMap->end(); ++itChamber) {
455  uint32_t detId = itChamber->first;
456 
457  for (L1RPCConeBuilder::TStrip2ConVec::iterator itStrip = itChamber->second.begin();
458  itStrip != itChamber->second.end();
459  ++itStrip) {
460  // Iterate over strip Connections
461  for (L1RPCConeBuilder::TStripConVec::iterator itConn = itStrip->second.begin(); itConn != itStrip->second.end();
462  ++itConn) {
463  // Check if this connection isn't allready present in the compressed map
464  ++uncompressedConsBefore;
465  bool alreadyDone = false;
466  if (m_compressedConnectionMap->find(detId) != m_compressedConnectionMap->end()) {
467  // iterate over the vec, check element by element
468  for (L1RPCConeBuilder::TCompressedConVec::iterator itCompConn = (*m_compressedConnectionMap)[detId].begin();
469  itCompConn != (*m_compressedConnectionMap)[detId].end();
470  ++itCompConn) {
471  if (itCompConn->m_tower == itConn->m_tower && itCompConn->m_PAC == itConn->m_PAC &&
472  itCompConn->m_logplane == itConn->m_logplane) // connection allready compressed
473  {
474  alreadyDone = true;
475 
476  int logStrip = itCompConn->m_mul * itStrip->first + itCompConn->m_offset;
477  if (logStrip != itConn->m_logstrip) {
478  //copy the problematic connection to the "safe" map
479  (*uncompressedConsLeft)[detId][itStrip->first].push_back(*itConn);
480  ++uncompressedConsAfter;
481  edm::LogWarning("RPCTriggerConfig")
482  << " Compression failed for det " << detId << " strip " << (int)itStrip->first << " . Got "
483  << (int)logStrip << " expected " << (int)itConn->m_logstrip << std::endl;
484  } else {
485  itCompConn->addStrip(itStrip->first);
486  }
487  }
488  } // compressed connection iteration end
489  }
490  //if (detId==637569977) std::cout << " Buld cons for strip " << (int)itStrip->first << std::endl;
491 
492  if (!alreadyDone) {
493  // find another strip contributing to the same PAC,tower,logplane
494  L1RPCConeBuilder::TStrip2ConVec::iterator itStripOther = itStrip;
495  ++itStripOther;
496  bool otherStripFound = false;
497  signed char mul = 1;
498  for (; itStripOther != itChamber->second.end() && !otherStripFound; ++itStripOther) {
499  for (L1RPCConeBuilder::TStripConVec::iterator itConnOther = itStripOther->second.begin();
500  itConnOther != itStripOther->second.end();
501  ++itConnOther) {
502  if (itConnOther->m_tower == itConn->m_tower && itConnOther->m_PAC == itConn->m_PAC &&
503  itConnOther->m_logplane == itConn->m_logplane) // connection to same PAC,logplane
504  {
505  otherStripFound = true;
506  if ((itStripOther->first - itStrip->first) * (itConnOther->m_logstrip - itConn->m_logstrip) < 0) {
507  mul = -1;
508  }
509  break;
510  }
511  } // otherConnections iter ends
512  } // otherStrip iter ends
513 
514  /*
515  if (itConn->m_tower==3 && itConn->m_PAC==73 && itConn->m_logplane==4 && detId==637569977){
516  std::cout << " Buld cons for strip " << (int)itStrip->first;
517  if (otherStripFound)
518  std::cout << " other strip " << itStrip->first;
519  else
520  std::cout << " no other strip ";
521 
522  std::cout << std::endl;
523 
524  }*/
525 
527  nCompConn.m_tower = itConn->m_tower;
528  nCompConn.m_PAC = itConn->m_PAC;
529  nCompConn.m_logplane = itConn->m_logplane;
530  nCompConn.m_mul = mul;
531  nCompConn.m_offset = itConn->m_logstrip - mul * (signed short)(itStrip->first);
532  nCompConn.addStrip(itStrip->first);
533 
534  if (otherStripFound) {
535  } else {
536  // uncompressedConsLeft[detId][itStrip->first].push_back(*itConn);
537  // ++uncompressedConsAfter;
538  }
539  (*m_compressedConnectionMap)[detId].push_back(nCompConn);
540  ++compressedCons;
541 
542  } // if(!allreadyDone)
543  } // iterate on connections
544  } // iterate on strips
545  } // iterate on chambers
546 
547  // 159 -87
548  //std::cout << offsetMax << " TT " << offsetMin << std::endl;
549 
550  edm::LogInfo("RPCTriggerConfig") << " Compressed: " << compressedCons << " "
552  << " Uncompressed before: " << uncompressedConsBefore << " "
553  << sizeof(L1RPCConeBuilder::TStripCon)
554  << " Uncompressed after: " << uncompressedConsAfter << " "
555  << sizeof(L1RPCConeBuilder::TStripCon);
556  m_connectionsMap = uncompressedConsLeft;
557 }
std::shared_ptr< L1RPCConeBuilder::TConMap > m_connectionsMap
Definition: RPCStripsRing.h:92
void compressConnections()
tuple ret
prodAgent to be discontinued
LocalPoint centreOfStrip(int strip) const
Definition: RPCRoll.cc:26
int getTowerForRefRing()
void fillWithVirtualStrips()
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
double sign(double x)
int nstrips() const
Definition: RPCRoll.cc:24
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
void addStrip(unsigned char strip)
virtual int eta_partition()
Definition: RPCGeomServ.cc:230
constexpr std::array< uint8_t, layerIndexSize > layer
const Double_t pi
void createOtherConnections(int tower, int PACno, int logplane, int logplanesize, float angle)
bool isReferenceRing()
Definition: RPCStripsRing.h:73
RPCDetId id() const
Definition: RPCRoll.cc:16
void filterOverlapingChambers()
int ring() const
Definition: RPCDetId.h:59
std::vector< TOtherConnStruct > TOtherConnStructVec
Definition: RPCStripsRing.h:48
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:50
int getEtaPartition()
Definition: RPCStripsRing.h:72
Log< level::Info, false > LogInfo
int layer() const
Definition: RPCDetId.h:85
bool m_isReferenceRing
Definition: RPCStripsRing.h:88
void addRoll(const RPCRoll *roll)
std::shared_ptr< L1RPCConeBuilder::TCompressedConMap > m_compressedConnectionMap
Definition: RPCStripsRing.h:93
__host__ __device__ constexpr RandomIt lower_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
string end
Definition: dataset.py:937
static int calculateHwPlane(const RPCRoll *roll)
Calculate ringId for any given RPCRoll.
void createRefConnections(TOtherConnStructVec &otherRings, int logplane, int logplaneSize)
Log< level::Warning, false > LogWarning
T angle(T x1, T y1, T z1, T x2, T y2, T z2)
Definition: angle.h:11
int region() const
Region id: 0 for Barrel, +/-1 For +/- Endcap.
Definition: RPCDetId.h:53
int station() const
Definition: RPCDetId.h:78