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 #include <atomic>
9 
10 std::atomic<bool> CSCCLCTData::debug{false};
11 
12 
14 : ncfebs_(tmbHeader->NCFEBs()), ntbins_(tmbHeader->NTBins())
15 {
16  size_ = nlines();
17  zero();
18 }
19 
20 
21 CSCCLCTData::CSCCLCTData(int ncfebs, int ntbins)
22 : ncfebs_(ncfebs), ntbins_(ntbins)
23 {
24  size_ = nlines();
25  zero();
26 }
27 
28 
29 
30 
31 CSCCLCTData::CSCCLCTData(int ncfebs, int ntbins, const unsigned short * buf)
32 : ncfebs_(ncfebs), ntbins_(ntbins)
33 {
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 }
41 
42 
44 {
45  for(int ifeb = 0; ifeb < ncfebs_; ++ifeb)
46  {
47  for(int tbin = 0; tbin < ntbins_; ++tbin)
48  {
49  for(int layer = 1; layer <= 6; ++layer)
50  {
51  dataWord(ifeb, tbin, layer) = CSCCLCTDataWord(ifeb, tbin, 0);
52  }
53  }
54  }
55 
56 }
57 
58 
59 std::vector<CSCComparatorDigi> CSCCLCTData::comparatorDigis(uint32_t idlayer, unsigned cfeb)
60 {
61  static const bool doStripSwapping = true;
62  bool me1a = (CSCDetId::station(idlayer)==1) && (CSCDetId::ring(idlayer)==4);
63  bool zplus = (CSCDetId::endcap(idlayer) == 1);
64  bool me1b = (CSCDetId::station(idlayer)==1) && (CSCDetId::ring(idlayer)==1);
65  unsigned layer = CSCDetId::layer(idlayer);
66 
67  //looking for comp output on layer
68  std::vector<CSCComparatorDigi> result;
69  assert(layer>0 && layer<= 6);
70  // this is pretty sparse data, so I wish we could check the
71  // data word by word, not bit by bit, but I don't see how to
72  // do the time sequencing that way.
73  for(int distrip = 0; distrip < 8; ++distrip)
74  {
75  uint16_t tbinbitsS0HS0=0;
76  uint16_t tbinbitsS0HS1=0;
77  uint16_t tbinbitsS1HS0=0;
78  uint16_t tbinbitsS1HS1=0;
79  for(int tbin = 0; tbin < ntbins_-2; ++tbin)
80  {
81  if(bitValue(cfeb, tbin, layer, distrip))
82  {
84  CSCCLCTDataWord word = dataWord(cfeb, tbin, layer);
85  assert(word.tbin_ == tbin);
86  assert(word.cfeb_ == cfeb);
87  // we have a hit. The next two time samples
88  // are the other two bits in the triad
89  int bit2 = bitValue(cfeb, tbin+1, layer, distrip);
90  int bit3 = bitValue(cfeb, tbin+2, layer, distrip);
91  // should count from zero
92  int chamberDistrip = distrip + cfeb*8;
93  int HalfStrip = 4*chamberDistrip + bit2*2 + bit3;
94  int output = 4 + bit2*2 + bit3;
95  /*
96  * Handles distrip logic; comparator output is for pairs of strips:
97  * hit bin dec
98  * x--- 100 4
99  * -x-- 101 5
100  * --x- 110 6
101  * ---x 111 7
102  *
103  */
104 
105  if (debug)
106  LogTrace ("CSCCLCTData|CSCRawToDigi")
107  << "fillComparatorOutputs: layer = "
108  << layer << " timebin = " << tbin
109  << " cfeb = " << cfeb << " distrip = " << chamberDistrip
110  << " HalfStrip = " << HalfStrip
111  << " Output " << output << std::endl;
113 
115  if (output==4) tbinbitsS0HS0=tbinbitsS0HS0+(1<<tbin);
116  if (output==5) tbinbitsS0HS1=tbinbitsS0HS1+(1<<tbin);
117  if (output==6) tbinbitsS1HS0=tbinbitsS1HS0+(1<<tbin);
118  if (output==7) tbinbitsS1HS1=tbinbitsS1HS1+(1<<tbin);
119 
120  tbin += 2;
121  }
122  }//end of loop over time bins
123  //we do not have to check over the last couple of time bins if there are no hits since
124  //comparators take 3 time bins
125 
126  // Store digis each of possible four halfstrips for given distrip:
127  if (tbinbitsS0HS0 || tbinbitsS0HS1 || tbinbitsS1HS0 || tbinbitsS1HS1) {
128  unsigned int cfeb_corr = cfeb;
129  unsigned int distrip_corr = distrip;
130 
131  if (doStripSwapping) {
132  // Fix ordering of strips and CFEBs in ME1/1.
133  // SV, 27/05/08: keep CFEB=4 for ME1/a until CLCT trigger logic
134  // stops combining it with the info from the other 4 CFEBs (ME1/b).
135  // if ( me1a ) { cfeb_corr = 0; } // reset 4 to 0
136  if ( me1a && zplus ) {distrip_corr = 7-distrip;} // 0-7 -> 7-0
137  if ( me1b && !zplus ) {distrip_corr = 7-distrip; cfeb_corr = 3-cfeb;}
138  }
139 
140  int strip = 16*cfeb_corr + 2*distrip_corr + 1;
141 
142  if (debug)
143  LogTrace ("CSCCLCTData|CSCRawToDigi")
144  << "fillComparatorOutputs: cfeb_corr = " << cfeb_corr
145  << " distrip_corr = " << distrip_corr << " strip = " << strip;
146 
147  if (doStripSwapping && (( me1a && zplus ) || ( me1b && !zplus ))) {
148  // Half-strips need to be flipped too.
149  if (tbinbitsS1HS1) result.push_back(CSCComparatorDigi(strip, 0, tbinbitsS1HS1));
150  if (tbinbitsS1HS0) result.push_back(CSCComparatorDigi(strip, 1, tbinbitsS1HS0));
151  if (tbinbitsS0HS1) result.push_back(CSCComparatorDigi(strip+1, 0, tbinbitsS0HS1));
152  if (tbinbitsS0HS0) result.push_back(CSCComparatorDigi(strip+1, 1, tbinbitsS0HS0));
153  }
154  else {
155  if (tbinbitsS0HS0) result.push_back(CSCComparatorDigi(strip, 0, tbinbitsS0HS0));
156  if (tbinbitsS0HS1) result.push_back(CSCComparatorDigi(strip, 1, tbinbitsS0HS1));
157  if (tbinbitsS1HS0) result.push_back(CSCComparatorDigi(strip+1, 0, tbinbitsS1HS0));
158  if (tbinbitsS1HS1) result.push_back(CSCComparatorDigi(strip+1, 1, tbinbitsS1HS1));
159  }
160  //uh oh ugly ugly ugly!
161  }
162  }//end of loop over distrips
163  return result;
164 }
165 
166 
167 
168 std::vector<CSCComparatorDigi> CSCCLCTData::comparatorDigis(int layer)
169 {
170  //returns comparators for one layer for all cfebs
171  std::vector<CSCComparatorDigi> result;
172  assert(layer>0 && layer<= 6);
173 
174  for(int cfeb = 0; cfeb < ncfebs_; ++cfeb)
175  {
176  std::vector<CSCComparatorDigi> oneCfebDigi = comparatorDigis(layer,cfeb);
177  result.insert(result.end(), oneCfebDigi.begin(), oneCfebDigi.end());
178  }
179 
180  return result;
181 }
182 
183 
184 void CSCCLCTData::add(const CSCComparatorDigi & digi, int layer)
185 {
186  //FIXME do flipping
187  int strip = digi.getStrip();
188  int halfStrip = (strip-1)*2 + digi.getComparator();
189  int cfeb = (strip-1)/16;
190  int distrip = ((strip-1)%16) / 2;
191  assert(distrip < 8 && cfeb < 6 && halfStrip < 161);
192 
193  std::vector<int> timeBinsOn = digi.getTimeBinsOn();
194  for(std::vector<int>::const_iterator tbinItr = timeBinsOn.begin();
195  tbinItr != timeBinsOn.end(); ++tbinItr)
196  {
197  int tbin = *tbinItr;
198  if(tbin >= 0 && tbin < ntbins_-2) {
199  // First triad bit indicates the presence of the hit
200  dataWord(cfeb, tbin, layer).set(distrip, true);
201  // Second bit indicates which of the two strips contains the hit
202  if (strip%2 == 0)
203  dataWord(cfeb, tbin+1, layer).set(distrip, true);
204  // Third bit indicates whether the hit is located on the left or on the
205  // right side of the strip.
206  if (digi.getComparator())
207  dataWord(cfeb, tbin+2, layer).set(distrip, true);
208  }
209  }
210 }
211 
212 
213 bool CSCCLCTData::check() const
214 {
215  bool result = true;
216  for(int cfeb = 0; cfeb < ncfebs_; ++cfeb)
217  {
218  for(int tbin = 0; tbin < ntbins_; ++tbin)
219  {
220  for(int layer = 1; layer <= 6; ++layer)
221  {
223  const CSCCLCTDataWord & word = dataWord(cfeb, tbin, layer);
224  bool wordIsGood = (word.tbin_ == tbin) && (word.cfeb_ == cfeb);
225  result = result && wordIsGood;
226  if(!wordIsGood && debug)
227  {
228  LogTrace("CSCCLCTData|CSCRawToDigi") << "Bad CLCT data in layer " << layer
229  << " expect CFEB " << cfeb << " tbin " << tbin;
230  LogTrace("CSCCLCTData|CSCRawToDigi") << " See " << word.cfeb_ << " "
231  << word.tbin_;
232  }
233  }
234  }
235  }
236  if(!result) LogTrace("CSCCLCTData|CSCRawToDigi") << "++ Bad CLCT Data ++ ";
237  return result;
238 }
239 
240 
241 void CSCCLCTData::dump() const {
242  for (int i=0;i<size_;i++) {
243  printf("%04x %04x %04x %04x\n", theData[i+3], theData[i+2], theData[i+1], theData[i]);
244  i+=3;
245  }
246 }
247 
248 
250 {
251  CSCCLCTData clctData(5, 16);
252  // aim for output 4 in 5th time bin, = 0000000000010000
253  CSCComparatorDigi comparatorDigi1(1, 0, 0x10);
254  // aim for output 5 in 6th time bin, = 0000 0000 0010 0000
255  CSCComparatorDigi comparatorDigi2(39, 1, 0x20);
256  // aim for output 7 in 7th time bin, = 000 0000 0100 0000
257  CSCComparatorDigi comparatorDigi3(80, 1, 0x40);
258 
259  clctData.add(comparatorDigi1,1);
260  clctData.add(comparatorDigi2,4);
261  clctData.add(comparatorDigi3,6);
262 
263  CSCDetId layer1(1,4,1,2,1);
264  CSCDetId layer4(1,4,1,2,4);
265  CSCDetId layer6(1,4,1,2,6);
266 
267  std::vector<CSCComparatorDigi> digis1 = clctData.comparatorDigis(1);
268  std::vector<CSCComparatorDigi> digis2 = clctData.comparatorDigis(4);
269  std::vector<CSCComparatorDigi> digis3 = clctData.comparatorDigis(6);
270 
271  assert(digis1.size() == 1);
272  assert(digis2.size() == 1);
273  assert(digis3.size() == 1);
274 
275  assert(digis1[0].getStrip() == 1);
276  assert(digis1[0].getComparator() == 0);
277  assert(digis1[0].getTimeBin() == 4);
278 
279  assert(digis2[0].getStrip() == 39);
280  assert(digis2[0].getComparator() == 1);
281  assert(digis2[0].getTimeBin() == 5);
282 
283  assert(digis3[0].getStrip() == 80);
284  assert(digis3[0].getComparator() == 1);
285  assert(digis3[0].getTimeBin() == 6);
286 }
287 
int i
Definition: DBlmapReader.cc:9
int nlines() const
Definition: CSCCLCTData.h:42
int getStrip() const
Get the strip number.
static void selfTest()
Definition: CSCCLCTData.cc:249
void set(int distrip, bool value)
@ not right! doesn&#39;t set zero
Definition: CSCCLCTData.h:13
CSCCLCTDataWord & dataWord(int iline) const
Definition: CSCCLCTData.h:47
unsigned short cfeb_
Definition: CSCCLCTData.h:16
int getComparator() const
Get Comparator readings.
int layer() const
Definition: CSCDetId.h:74
int endcap() const
Definition: CSCDetId.h:106
unsigned short theData[5 *6 *32]
Definition: CSCCLCTData.h:84
void dump() const
Definition: CSCCLCTData.cc:241
bool bitValue(int cfeb, int tbin, int layer, int distrip)
Definition: CSCCLCTData.h:61
tuple result
Definition: query.py:137
std::vector< int > getTimeBinsOn() const
static std::atomic< bool > debug
Definition: CSCCLCTData.h:80
CSCCLCTData(const CSCTMBHeader *tmbHeader)
Definition: CSCCLCTData.cc:13
#define LogTrace(id)
std::vector< CSCComparatorDigi > comparatorDigis(int layer)
layers count from one
Definition: CSCCLCTData.cc:168
int ring() const
Definition: CSCDetId.h:88
unsigned short tbin_
Definition: CSCCLCTData.h:15
void add(const CSCComparatorDigi &digi, int layer)
TODO for packing. Doesn&#39;t do flipping yet.
Definition: CSCCLCTData.cc:184
bool check() const
Definition: CSCCLCTData.cc:213
int station() const
Definition: CSCDetId.h:99
void zero()
Definition: CSCCLCTData.cc:43