CMS 3D CMS Logo

CSCComparatorData.cc
Go to the documentation of this file.
5 #include <iostream>
6 #include <cstdio>
7 #include <cstring>
8 
9 #ifdef LOCAL_UNPACK
10 bool CSCComparatorData::debug = false;
11 #else
12 #include <atomic>
13 std::atomic<bool> CSCComparatorData::debug{false};
14 #endif
15 
17  : ncfebs_(tmbHeader->NCFEBs()), ntbins_(tmbHeader->NTBins()) {
18  if (tmbHeader != nullptr)
19  theFirmwareVersion = tmbHeader->FirmwareVersion();
20  else
21  theFirmwareVersion = 2007;
22  size_ = nlines();
23  zero();
24 }
25 
26 CSCComparatorData::CSCComparatorData(int ncfebs, int ntbins, int firmware_version)
27  : ncfebs_(ncfebs), ntbins_(ntbins), theFirmwareVersion(firmware_version) {
28  size_ = nlines();
29  zero();
30 }
31 
32 CSCComparatorData::CSCComparatorData(int ncfebs, int ntbins, const unsigned short* buf, int firmware_version)
33  : ncfebs_(ncfebs), ntbins_(ntbins), theFirmwareVersion(firmware_version) {
34  // add two more for odd ntbins, plus one for the e0c
35  // Oct 2004 Rick: e0c line belongs to CSCTMBTrailer
36  size_ = (nlines() % 2 == 1) ? nlines() + 2 : nlines();
37 
38  memcpy(theData, buf, size_ * 2);
39 }
40 
42  for (int ifeb = 0; ifeb < ncfebs_; ++ifeb) {
43  for (int tbin = 0; tbin < ntbins_; ++tbin) {
44  for (int layer = 1; layer <= 6; ++layer) {
45  dataWord(ifeb, tbin, layer) = CSCComparatorDataWord(ifeb, tbin, 0);
46  }
47  }
48  }
49 }
50 
51 std::vector<CSCComparatorDigi> CSCComparatorData::comparatorDigis(uint32_t idlayer, unsigned cfeb) {
52  static const bool doStripSwapping = true;
53  bool me1a = (CSCDetId::station(idlayer) == 1) && (CSCDetId::ring(idlayer) == 4);
54  bool zplus = (CSCDetId::endcap(idlayer) == 1);
55  bool me1b = (CSCDetId::station(idlayer) == 1) && (CSCDetId::ring(idlayer) == 1);
56  // bool me11 = (CSCDetId::station(idlayer)==1) && ((CSCDetId::ring(idlayer)==1) || (CSCDetId::ring(idlayer)==4));
57  unsigned layer = CSCDetId::layer(idlayer);
58 
59  //looking for comp output on layer
60  std::vector<CSCComparatorDigi> result;
61  assert(layer > 0 && layer <= 6);
62  // this is pretty sparse data, so I wish we could check the
63  // data word by word, not bit by bit, but I don't see how to
64  // do the time sequencing that way.
65  for (int distrip = 0; distrip < 8; ++distrip) {
66  uint16_t tbinbitsS0HS0 = 0;
67  uint16_t tbinbitsS0HS1 = 0;
68  uint16_t tbinbitsS1HS0 = 0;
69  uint16_t tbinbitsS1HS1 = 0;
70  for (int tbin = 0; tbin < ntbins_ - 2; ++tbin) {
71  if (bitValue(cfeb, tbin, layer, distrip)) {
73  CSCComparatorDataWord word = dataWord(cfeb, tbin, layer);
74  assert(word.tbin_ == tbin);
75  assert(word.cfeb_ == cfeb);
76  // we have a hit. The next two time samples
77  // are the other two bits in the triad
78  int bit2 = bitValue(cfeb, tbin + 1, layer, distrip);
79  int bit3 = bitValue(cfeb, tbin + 2, layer, distrip);
80  // should count from zero
81  int chamberDistrip = distrip + cfeb * 8;
82  int HalfStrip = 4 * chamberDistrip + bit2 * 2 + bit3;
83  int output = 4 + bit2 * 2 + bit3;
84  /*
85  * Handles distrip logic; comparator output is for pairs of strips:
86  * hit bin dec
87  * x--- 100 4
88  * -x-- 101 5
89  * --x- 110 6
90  * ---x 111 7
91  *
92  */
93 
94  if (debug)
95  LogTrace("CSCComparatorData|CSCRawToDigi")
96  << "fillComparatorOutputs: layer = " << layer << " timebin = " << tbin << " cfeb = " << cfeb
97  << " distrip = " << chamberDistrip << " HalfStrip = " << HalfStrip << " Output " << output << std::endl;
99 
101  if (output == 4)
102  tbinbitsS0HS0 = tbinbitsS0HS0 + (1 << tbin);
103  if (output == 5)
104  tbinbitsS0HS1 = tbinbitsS0HS1 + (1 << tbin);
105  if (output == 6)
106  tbinbitsS1HS0 = tbinbitsS1HS0 + (1 << tbin);
107  if (output == 7)
108  tbinbitsS1HS1 = tbinbitsS1HS1 + (1 << tbin);
109 
110  tbin += 2;
111  }
112  } //end of loop over time bins
113  //we do not have to check over the last couple of time bins if there are no hits since
114  //comparators take 3 time bins
115 
116  // Store digis each of possible four halfstrips for given distrip:
117  if (tbinbitsS0HS0 || tbinbitsS0HS1 || tbinbitsS1HS0 || tbinbitsS1HS1) {
118  unsigned int cfeb_corr = cfeb;
119  unsigned int distrip_corr = distrip;
120 
121  if (doStripSwapping) {
122  // Fix ordering of strips and CFEBs in ME1/1.
123  // SV, 27/05/08: keep CFEB=4 for ME1/a until CLCT trigger logic
124  // stops combining it with the info from the other 4 CFEBs (ME1/b).
125  //
126  if (theFirmwareVersion >= 2013) {
127  if (me1a && zplus) {
128  distrip_corr = 7 - distrip; // 0-7 -> 7-0
129  cfeb_corr = 10 - cfeb;
130  }
131  if (me1b && !zplus) {
132  distrip_corr = 7 - distrip;
133  cfeb_corr = 3 - cfeb;
134  }
135  } else {
136  // if ( me1a ) { cfeb_corr = 0; } // reset 4 to 0
137  if (me1a && zplus) {
138  distrip_corr = 7 - distrip; // 0-7 -> 7-0
139  }
140  if (me1b && !zplus) {
141  distrip_corr = 7 - distrip;
142  cfeb_corr = 3 - cfeb;
143  }
144  }
145  }
146 
147  int strip = 16 * cfeb_corr + 2 * distrip_corr + 1;
148 
149  if (debug)
150  LogTrace("CSCComparatorData|CSCRawToDigi") << "fillComparatorOutputs: cfeb_corr = " << cfeb_corr
151  << " distrip_corr = " << distrip_corr << " strip = " << strip;
152 
153  if (doStripSwapping && ((me1a && zplus) || (me1b && !zplus))) {
154  // Half-strips need to be flipped too.
155  if (tbinbitsS1HS1)
156  result.push_back(CSCComparatorDigi(strip, 0, tbinbitsS1HS1));
157  if (tbinbitsS1HS0)
158  result.push_back(CSCComparatorDigi(strip, 1, tbinbitsS1HS0));
159  if (tbinbitsS0HS1)
160  result.push_back(CSCComparatorDigi(strip + 1, 0, tbinbitsS0HS1));
161  if (tbinbitsS0HS0)
162  result.push_back(CSCComparatorDigi(strip + 1, 1, tbinbitsS0HS0));
163  } else {
164  if (tbinbitsS0HS0)
165  result.push_back(CSCComparatorDigi(strip, 0, tbinbitsS0HS0));
166  if (tbinbitsS0HS1)
167  result.push_back(CSCComparatorDigi(strip, 1, tbinbitsS0HS1));
168  if (tbinbitsS1HS0)
169  result.push_back(CSCComparatorDigi(strip + 1, 0, tbinbitsS1HS0));
170  if (tbinbitsS1HS1)
171  result.push_back(CSCComparatorDigi(strip + 1, 1, tbinbitsS1HS1));
172  }
173  //uh oh ugly ugly ugly!
174  }
175  } //end of loop over distrips
176  return result;
177 }
178 
179 std::vector<CSCComparatorDigi> CSCComparatorData::comparatorDigis(int layer) {
180  //returns comparators for one layer for all cfebs
181  std::vector<CSCComparatorDigi> result;
182  assert(layer > 0 && layer <= 6);
183 
184  for (int cfeb = 0; cfeb < ncfebs_; ++cfeb) {
185  std::vector<CSCComparatorDigi> oneCfebDigi = comparatorDigis(layer, cfeb);
186  result.insert(result.end(), oneCfebDigi.begin(), oneCfebDigi.end());
187  }
188 
189  return result;
190 }
191 
192 void CSCComparatorData::add(const CSCComparatorDigi& digi, int layer) {
193  //FIXME do flipping
194  int strip = digi.getStrip();
195  int halfStrip = (strip - 1) * 2 + digi.getComparator();
196  int cfeb = (strip - 1) / 16;
197  int distrip = ((strip - 1) % 16) / 2;
198 
199  // assert(distrip < 8 && cfeb < 6 && halfStrip < 161);
201  assert(distrip < 8 && cfeb < 8 && halfStrip < 225);
202 
203  std::vector<int> timeBinsOn = digi.getTimeBinsOn();
204  for (std::vector<int>::const_iterator tbinItr = timeBinsOn.begin(); tbinItr != timeBinsOn.end(); ++tbinItr) {
205  int tbin = *tbinItr;
206  if (tbin >= 0 && tbin < ntbins_ - 2) {
207  // First triad bit indicates the presence of the hit
208  dataWord(cfeb, tbin, layer).set(distrip, true);
209  // Second bit indicates which of the two strips contains the hit
210  if (strip % 2 == 0)
211  dataWord(cfeb, tbin + 1, layer).set(distrip, true);
212  // Third bit indicates whether the hit is located on the left or on the
213  // right side of the strip.
214  if (digi.getComparator())
215  dataWord(cfeb, tbin + 2, layer).set(distrip, true);
216  }
217  }
218 }
219 
220 /***
221  * Comparator packing version with ME11 strips swapping
222  ***/
223 void CSCComparatorData::add(const CSCComparatorDigi& digi, const CSCDetId& cid) {
224  static const bool doStripSwapping = true;
225  bool me1a = (cid.station() == 1) && (cid.ring() == 4);
226  bool zplus = (cid.endcap() == 1);
227  bool me1b = (cid.station() == 1) && (cid.ring() == 1);
228  // bool me11 = (cid.station()==1) && ((cid.ring()==1) || (cid.ring()==4));
229 
230  unsigned layer = cid.layer();
231 
232  int strip = digi.getStrip();
233  int halfstrip = (strip - 1) * 2 + digi.getComparator();
234  int cfeb = (strip - 1) / 16;
235  int distrip = ((strip - 1) % 16) / 2;
236  int bit2 = (strip - 1) % 2;
237  int bit3 = digi.getComparator();
238 
239  // assert(distrip < 8 && cfeb < 6 && halfStrip < 161);
241  if (theFirmwareVersion >= 2013) {
242  assert(distrip < 8 && cfeb < 8 && halfstrip < 225);
243  } else {
244  assert(distrip < 8 && cfeb < 6 && halfstrip < 161);
245  }
246 
247  // Lets try to do ME11 strip flipping
248  if (doStripSwapping) {
249  if (theFirmwareVersion >= 2013) {
250  if ((me1a || (me1b && (cfeb > 3))) && zplus) {
251  distrip = 7 - distrip; // 0-7 -> 7-0
252  cfeb = 10 - cfeb;
253  bit2 = ((31 - (halfstrip % 32)) % 4) / 2;
254  bit3 = ((31 - (halfstrip % 32)) % 4) % 2;
255  }
256  if (me1b && !zplus && (cfeb < 4)) {
257  distrip = 7 - distrip;
258  cfeb = 3 - cfeb;
259  bit2 = ((31 - (halfstrip % 32)) % 4) / 2;
260  bit3 = ((31 - (halfstrip % 32)) % 4) % 2;
261  }
262  } else {
263  // if ( me1a ) { cfeb_corr = 0; } // reset 4 to 0
264  if ((me1a || (me1b && (cfeb > 3))) && zplus) {
265  distrip = 7 - distrip; // 0-7 -> 7-0
266  bit2 = ((31 - (halfstrip % 32)) % 4) / 2;
267  bit3 = ((31 - (halfstrip % 32)) % 4) % 2;
268  }
269  if (me1b && !zplus && (cfeb < 4)) {
270  distrip = 7 - distrip;
271  cfeb = 3 - cfeb;
272  bit2 = ((31 - (halfstrip % 32)) % 4) / 2;
273  bit3 = ((31 - (halfstrip % 32)) % 4) % 2;
274  }
275  }
276  }
277 
278  std::vector<int> timeBinsOn = digi.getTimeBinsOn();
279  for (std::vector<int>::const_iterator tbinItr = timeBinsOn.begin(); tbinItr != timeBinsOn.end(); ++tbinItr) {
280  int tbin = *tbinItr;
281  if (tbin >= 0 && tbin < ntbins_ - 2) {
282  // First triad bit indicates the presence of the hit
283  dataWord(cfeb, tbin, layer).set(distrip, true);
284  // Second bit indicates which of the two strips contains the hit
285  // if (strip%2 == 0)
286  if (bit2)
287  dataWord(cfeb, tbin + 1, layer).set(distrip, true);
288  // Third bit indicates whether the hit is located on the left or on the
289  // right side of the strip.
290  // if (digi.getComparator())
291  if (bit3)
292  dataWord(cfeb, tbin + 2, layer).set(distrip, true);
293  }
294  }
295 }
296 
297 bool CSCComparatorData::check() const {
298  bool result = true;
299  for (int cfeb = 0; cfeb < ncfebs_; ++cfeb) {
300  for (int tbin = 0; tbin < ntbins_; ++tbin) {
301  for (int layer = 1; layer <= 6; ++layer) {
303  const CSCComparatorDataWord& word = dataWord(cfeb, tbin, layer);
304  bool wordIsGood = (word.tbin_ == tbin) && (word.cfeb_ == cfeb);
305  result = result && wordIsGood;
306  if (!wordIsGood && debug) {
307  LogTrace("CSCComparatorData|CSCRawToDigi")
308  << "Bad CLCT data in layer " << layer << " expect CFEB " << cfeb << " tbin " << tbin;
309  LogTrace("CSCComparatorData|CSCRawToDigi") << " See " << word.cfeb_ << " " << word.tbin_;
310  }
311  }
312  }
313  }
314  if (!result)
315  LogTrace("CSCComparatorData|CSCRawToDigi") << "++ Bad CLCT Data ++ ";
316  return result;
317 }
318 
319 void CSCComparatorData::dump() const {
320  for (int i = 0; i < size_; i++) {
321  printf("%04x %04x %04x %04x\n", theData[i + 3], theData[i + 2], theData[i + 1], theData[i]);
322  i += 3;
323  }
324 }
325 
327  CSCComparatorData comparatorData(5, 16);
328  // aim for output 4 in 5th time bin, = 0000000000010000
329  CSCComparatorDigi comparatorDigi1(1, 0, 0x10);
330  // aim for output 5 in 6th time bin, = 0000 0000 0010 0000
331  CSCComparatorDigi comparatorDigi2(39, 1, 0x20);
332  // aim for output 7 in 7th time bin, = 000 0000 0100 0000
333  CSCComparatorDigi comparatorDigi3(80, 1, 0x40);
334 
335  comparatorData.add(comparatorDigi1, 1);
336  comparatorData.add(comparatorDigi2, 4);
337  comparatorData.add(comparatorDigi3, 6);
338 
339  CSCDetId layer1(1, 4, 1, 2, 1);
340  CSCDetId layer4(1, 4, 1, 2, 4);
341  CSCDetId layer6(1, 4, 1, 2, 6);
342 
343  std::vector<CSCComparatorDigi> digis1 = comparatorData.comparatorDigis(1);
344  std::vector<CSCComparatorDigi> digis2 = comparatorData.comparatorDigis(4);
345  std::vector<CSCComparatorDigi> digis3 = comparatorData.comparatorDigis(6);
346 
347  assert(digis1.size() == 1);
348  assert(digis2.size() == 1);
349  assert(digis3.size() == 1);
350 
351  assert(digis1[0].getStrip() == 1);
352  assert(digis1[0].getComparator() == 0);
353  assert(digis1[0].getTimeBin() == 4);
354 
355  assert(digis2[0].getStrip() == 39);
356  assert(digis2[0].getComparator() == 1);
357  assert(digis2[0].getTimeBin() == 5);
358 
359  assert(digis3[0].getStrip() == 80);
360  assert(digis3[0].getComparator() == 1);
361  assert(digis3[0].getTimeBin() == 6);
362 }
mps_fire.i
i
Definition: mps_fire.py:428
MessageLogger.h
CSCTMBHeader::FirmwareVersion
int FirmwareVersion() const
Definition: CSCTMBHeader.h:35
convertSQLitetoXML_cfg.output
output
Definition: convertSQLitetoXML_cfg.py:72
CSCDetId::ring
int ring() const
Definition: CSCDetId.h:68
digitizers_cfi.strip
strip
Definition: digitizers_cfi.py:19
CSCComparatorData::bitValue
bool bitValue(int cfeb, int tbin, int layer, int distrip)
Definition: CSCComparatorData.h:66
cms::cuda::assert
assert(be >=bs)
CSCComparatorDigi::getComparator
int getComparator() const
Get Comparator readings. Can be 0 or 1.
Definition: CSCComparatorDigi.h:35
CSCComparatorData::selfTest
static void selfTest()
Definition: CSCComparatorData.cc:325
CSCDetId.h
word
uint64_t word
Definition: CTPPSTotemDataFormatter.cc:29
CSCComparatorData.h
CSCComparatorData::dump
void dump() const
Definition: CSCComparatorData.cc:318
CSCComparatorData::check
bool check() const
Definition: CSCComparatorData.cc:296
CSCDetId::layer
int layer() const
Definition: CSCDetId.h:56
CSCComparatorDigi
Definition: CSCComparatorDigi.h:16
CSCComparatorDigi::getTimeBinsOn
std::vector< int > getTimeBinsOn() const
Definition: CSCComparatorDigi.cc:72
CSCComparatorData::nlines
int nlines() const
Definition: CSCComparatorData.h:42
CSCComparatorData::theFirmwareVersion
int theFirmwareVersion
Definition: CSCComparatorData.h:91
CSCComparatorData::dataWord
CSCComparatorDataWord & dataWord(int iline) const
Definition: CSCComparatorData.h:49
CSCDetId
Definition: CSCDetId.h:26
CSCComparatorData::size_
int size_
Definition: CSCComparatorData.h:89
visDQMUpload.buf
buf
Definition: visDQMUpload.py:154
CSCComparatorData::ncfebs_
int ncfebs_
Definition: CSCComparatorData.h:87
CSCComparatorData::comparatorDigis
std::vector< CSCComparatorDigi > comparatorDigis(int layer)
layers count from one
Definition: CSCComparatorData.cc:179
CSCComparatorData::theData
unsigned short theData[7 *6 *32]
Definition: CSCComparatorData.h:90
CSCTMBHeader
Definition: CSCTMBHeader.h:26
CSCComparatorData::CSCComparatorData
CSCComparatorData(const CSCTMBHeader *tmbHeader)
Definition: CSCComparatorData.cc:16
CSCComparatorData::add
void add(const CSCComparatorDigi &digi, int layer)
TODO for packing. Doesn't do flipping yet.
Definition: CSCComparatorData.cc:192
CSCDetId::endcap
int endcap() const
Definition: CSCDetId.h:85
SurfaceOrientation::zplus
Definition: Surface.h:19
CSCTMBHeader.h
CSCComparatorData::zero
void zero()
Definition: CSCComparatorData.cc:41
CSCComparatorDigi::getStrip
int getStrip() const
Get the strip number. Counts from 1.
Definition: CSCComparatorDigi.h:32
mps_fire.result
result
Definition: mps_fire.py:311
CSCComparatorDataWord::set
void set(int distrip, bool value)
@ not right! doesn't set zero
Definition: CSCComparatorData.h:16
LogTrace
#define LogTrace(id)
Definition: MessageLogger.h:224
CSCDetId::station
int station() const
Definition: CSCDetId.h:79
CSCComparatorDataWord
Definition: CSCComparatorData.h:12
CSCComparatorData::ntbins_
int ntbins_
Definition: CSCComparatorData.h:88
CSCComparatorData
Definition: CSCComparatorData.h:24
CSCComparatorData::debug
static std::atomic< bool > debug
Definition: CSCComparatorData.h:84