CMS 3D CMS Logo

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