CMS 3D CMS Logo

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