CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CSCTMBData.cc
Go to the documentation of this file.
1 
10 
11 #include <iomanip> // dump for JK
12 #include <iostream>
13 #include <cstdio>
16 
17 bool CSCTMBData::debug =false;
18 
20  : theOriginalBuffer(0),
21  theB0CLine( 0 ),
22  theE0FLine( 0 ),
23  theTMBHeader(2007, 0x50c3),
24  theCLCTData(&theTMBHeader),
25  theTMBScopeIsPresent(false),
26  theTMBScope(0),
27  theTMBMiniScopeIsPresent(false),
28  theTMBMiniScope(0),
29  theBlockedCFEBIsPresent(false),
30  theTMBBlockedCFEB(0),
31  theTMBTrailer(theTMBHeader.sizeInWords()+theCLCTData.sizeInWords(), 2007),
32  size_( 0 ),
33  cWordCnt( 0 ),
34  theRPCDataIsPresent(false)
35 {
36 
37 
38 }
39 
40 
41 CSCTMBData::CSCTMBData(unsigned short *buf)
42  : theOriginalBuffer(buf),
43  theTMBHeader(2007, 0x50c3),
44  theCLCTData(&theTMBHeader),
45  theTMBScopeIsPresent(false),
46  theTMBScope(0),
47  theTMBMiniScopeIsPresent(false),
48  theTMBMiniScope(0),
49  theBlockedCFEBIsPresent(false),
50  theTMBBlockedCFEB(0),
51  theTMBTrailer(theTMBHeader.sizeInWords()+theCLCTData.sizeInWords(), 2007),
52  theRPCDataIsPresent(false){
53  size_ = UnpackTMB(buf);
54 }
55 
56 // Explicitly-defined copy constructor is needed when the scope data is
57 // present, to prevent the same pointer from being deleted twice. -SV.
59  theOriginalBuffer(data.theOriginalBuffer),
60  theB0CLine(data.theB0CLine), theE0FLine(data.theE0FLine),
61  theTMBHeader(data.theTMBHeader),
62  theCLCTData(data.theCLCTData), theRPCData(data.theRPCData),
63  theTMBScopeIsPresent(data.theTMBScopeIsPresent),
64  theTMBMiniScopeIsPresent(data.theTMBMiniScopeIsPresent),
65  theBlockedCFEBIsPresent(data.theBlockedCFEBIsPresent),
66  theTMBTrailer(data.theTMBTrailer),
67  size_(data.size_), cWordCnt(data.cWordCnt),
68  theRPCDataIsPresent(data.theRPCDataIsPresent)
69 {
71  theTMBScope = new CSCTMBScope(*(data.theTMBScope));
72  }
73  else {
74  theTMBScope = 0;
75  }
76 
79  }
80  else {
81  theTMBMiniScope = 0;
82  }
83 
86  }
87  else {
89  }
90 
91 }
92 
95  delete theTMBScope;
96  theTMBScopeIsPresent = false;
97  }
98 
100  delete theTMBMiniScope;
101  theTMBMiniScopeIsPresent = false;
102  }
103 
105  delete theTMBBlockedCFEB;
106  theBlockedCFEBIsPresent = false;
107  }
108 }
109 
112 int findLine(unsigned short *buf, unsigned short marker,int first, int maxToDo) {
113  for(int i = first; i < maxToDo; ++i) {
114  if(buf[i] == marker) {
115  return i;
116  }
117  }
118  return -1;
119 }
120 
122  std::vector<std::bitset<16> > theTotalTMBData(theE0FLine+1-theB0CLine);
123  unsigned i = 0;
124  for (unsigned int line=theB0CLine; line<theE0FLine+1;++line) {
125  theTotalTMBData[i] = std::bitset<16>(theOriginalBuffer[line]);
126  ++i;
127  }
128  if ( theTotalTMBData.size() > 0 ) {
129  std::bitset<22> CRC=calCRC22(theTotalTMBData);
130  LogTrace("CSCTMBData|CSCRawToDigi") << " Test here " << CRC.to_ulong();
131  return CRC.to_ulong();
132  }
133  else {
134  LogTrace("CSCTMBData|CSCRawToDigi") << "theTotalTMBData doesn't exist";
135  return 0;
136  }
137 }
138 
139 int CSCTMBData::UnpackTMB(unsigned short *buf) {
141  unsigned short int firmwareVersion=0;
142  int Ntbins = 0 ;
143  int NHeaderFrames = 0; //WARNING in 5_0_X
144  int NRPCtbins = 0; // =VB= number of RPC tbins
145 
146  int b0cLine=0;
147 
150  NHeaderFrames++; NHeaderFrames--;
151 
152  if (buf[b0cLine]==0xdb0c) {
153  firmwareVersion=2007;
154  Ntbins = buf[b0cLine+19]&0xF8;
155  NRPCtbins = (buf[b0cLine+36]>>5)&0x1F; // =VB= get RPC tbins
156  NHeaderFrames = buf[b0cLine+5]&0x3F; //WARNING in 5_0_X
157  }
158  else if (buf[b0cLine]==0x6b0c) {
159  firmwareVersion=2006;
160  Ntbins = buf[b0cLine+1]&0x1f ;
161  NRPCtbins = Ntbins;
162  NHeaderFrames = buf[b0cLine+4]&0x1f; //WARNING in 5_0_X
163  }
164  else {
165  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ Can't find b0C flag";
166  }
167 
168  if ((firmwareVersion==2007)&&(!(((buf[b0cLine]&0xFFFF)==0xDB0C)&&((buf[b0cLine+1]&0xf000)==0xD000)
169  &&((buf[b0cLine+2]&0xf000)==0xD000)&&((buf[b0cLine+3]&0xf000)==0xD000)))){
170  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ CSCTMBData warning: error in header in 2007 format!";
171  }
172 
173  int MaxSizeRPC = 1+NRPCtbins*2*4+1;
174  //int MaxSizeScope = 5;
175  int e0bLine =-1;
176  switch (firmwareVersion) {
177  case 2007:
178  e0bLine = 42; //last word of header2007
179  break;
180  case 2006:
181  e0bLine = 26; //last word of header in 2006 format
182  break;
183  default:
184  edm::LogError("CSCTMBData|CSCRawToDigi") << "+++ undetermined firmware format - cant find e0bLine";
185  }
186 
188 
189  if(!theTMBHeader.check()) {
190  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ CSCTMBData warning: Bad TMB header e0bLine=" << std::hex << buf[e0bLine];
191  return 0;
192  }
193 
194  int currentPosition = theTMBHeader.sizeInWords();
195 
197 
198  if(!theCLCTData.check()) {
199  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ CSCTMBData warning: Bad CLCT data";
200  }
201  else {
202  currentPosition+=theCLCTData.sizeInWords();
203  }
204 
205  //int i = currentPosition-1;
206  //printf ( "%04x %04x %04x %04x\n",buf[i+3],buf[i+2],buf[i+1],buf[i] ) ;
207 
208 
209  // look for RPC
210  int b04Line = currentPosition;
211 
212  if(buf[b04Line]==0x6b04) {
213  // we need an e04 line to calculate the size
214  int e04Line = findLine(buf, 0x6e04, currentPosition, currentPosition+MaxSizeRPC);
215  if(e04Line != -1) {
216  theRPCDataIsPresent = true;
217  theRPCData = CSCRPCData(buf+b04Line, e04Line-b04Line+1);
218  currentPosition+=theRPCData.sizeInWords();
219  }
220  else {
221  LogTrace("CSCTMBData|CSCRawToDigi") << "CSCTMBData::corrupt RPC data! Failed to find end! ";
222  return 0;
223  }
224  }
225 
226  int TotTMBReadout=0;
227  switch (firmwareVersion) {
228  case 2007:
229  TotTMBReadout= 43+Ntbins*6*5+1+NRPCtbins*2*4+2+8*256+8;
230  break;
231  case 2006:
232  TotTMBReadout= 27+Ntbins*6*5+1+NRPCtbins*2*4+2+8*256+8; //see tmb2004 manual (version v2p06) page54.
233  break;
234  default:
235  edm::LogError("CSCTMBData|CSCRawToDigi") << "can't find TotTMBReadout - unknown firmware version!";
236  break;
237  }
238 
239 //std::cout << " !!!TMB Scope!!! " << std::endl;
240  if (buf[currentPosition]==0x6b05) {
241  int b05Line = currentPosition;
242  LogTrace("CSCTMBData|CSCRawToDigi") << "found scope!";
243  int e05Line = findLine(buf, 0x6e05, currentPosition, TotTMBReadout-currentPosition);
244  if(e05Line != -1){
245  theTMBScopeIsPresent = true;
246  theTMBScope = new CSCTMBScope(buf,b05Line, e05Line);
247  // The size of the TMB scope data can vary, and I see no good reasons
248  // not to determine it dynamically. -SV, 5 Nov 2008.
249  //currentPosition+=theTMBScope->sizeInWords();
250  currentPosition+=(e05Line-b05Line+1);
251  }
252  else {
253  LogTrace("CSCTMBData|CSCRawToDigi")
254  << "+++ CSCTMBData warning: found 0x6b05 line, but not 0x6e05! +++";
255  }
256  }
257 
259  if (buf[currentPosition]==0x6b07){
260  int Line6b07 = currentPosition;
261  LogTrace("CSCTMBData") << " TMBData ---> Begin of MiniScope found " ;
262  int Line6E07 = findLine(buf, 0x6E07, currentPosition, TotTMBReadout-currentPosition);
263  if(Line6E07 !=-1){
264  LogTrace("CSCTMBData") << " TMBData --> End of MiniScope found " << Line6E07-Line6b07+1 << " words ";
266  theTMBMiniScope = new CSCTMBMiniScope(buf, Line6b07, Line6E07);
267  currentPosition += (Line6E07-Line6b07+1);
268  }
269  else {
270  LogTrace("CSCTMBData")
271  << "+++ CSCTMBData warning MiniScope!: found 0x6b07 line, but not 0x6e07! +++";
272  }
273  }
275 
277  if (buf[currentPosition]==0x6BCB){
278  int Line6BCB = currentPosition;
279  LogTrace("CSCTMBData") << " TMBData ---> Begin of Blocked CFEB found " ;
280  int Line6ECB = findLine(buf, 0x6ECB, currentPosition, TotTMBReadout-currentPosition);
281  if(Line6ECB !=-1){
282  LogTrace("CSCTMBData") << " TMBData --> End of Blocked CFEB found " << Line6ECB-Line6BCB+1 << " words ";
284  theTMBBlockedCFEB = new CSCTMBBlockedCFEB(buf, Line6BCB, Line6ECB);
285  currentPosition += (Line6ECB-Line6BCB+1);
286  }
287  else {
288  LogTrace("CSCTMBData")
289  << "+++ CSCTMBData warning Blocked CFEB!: found 0x6BCB line, but not 0x6ECB! +++";
290  }
291  }
293 
294  int maxLine = findLine(buf, 0xde0f, currentPosition, TotTMBReadout-currentPosition);
295  if(maxLine == -1)
296  {
297  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ CSCTMBData warning: No e0f line!";
298  return 0;
299  }
300 
301  //Now for CRC check put this information into bitset
302 
303  theB0CLine = b0cLine;
304  theE0FLine = maxLine;
305 
306  // finally, the trailer
307  int e0cLine = findLine(buf, 0x6e0c, currentPosition, maxLine);
308  if (e0cLine == -1)
309  {
310  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ CSCTMBData warning: No e0c line!";
311  }
312  else
313  {
314  theTMBTrailer = CSCTMBTrailer(buf+e0cLine, firmwareVersion);
315  LogTrace("CSCTMBData|CSCRawToDigi")
316  << "TMB trailer size: " << theTMBTrailer.sizeInWords();
317  }
318 
319  checkSize();
320 
321  // Dump of TMB; format proposed by JK.
322 #ifdef TMBDUMP
323  LogTrace("CSCTMBData") << "Dump of TMB data:";
324  for (int line = b0cLine; line <= maxLine+3; line++) {
325  LogTrace("CSCTMBData")
326  << "Adr= " << std::setw(4) << line
327  << " Data= " << std::setfill('0') << std::setw(5)
328  << std::uppercase << std::hex << buf[line] << std::dec << std::endl;
329  }
330 #endif
331 
332  // size, since we count from 0 and have one more trailer word
333  // there are sometimes multiple "de0f" lines in trailer, so key on "6e0c"
334  return e0cLine-b0cLine+theTMBTrailer.sizeInWords();
335 } //UnpackTMB
336 
338 {
339  // sum up all the components and see if they have the size indicated in the TMBTrailer
340  return true;
341 }
342 
343 std::bitset<22> CSCTMBData::calCRC22(const std::vector< std::bitset<16> >& datain)
344 {
345  std::bitset<22> CRC;
346  CRC.reset();
347  for(unsigned int i=0;i<datain.size()-3;++i)
348  {
349  CRC=nextCRC22_D16(datain[i],CRC);
350  }
351  return CRC;
352 }
353 
355 {
356  if (!theTMBScopeIsPresent) throw("No TMBScope in this chamber");
357  return * theTMBScope;
358 }
359 
361 {
362  if (!theTMBMiniScopeIsPresent) throw("No TMBScope in this chamber");
363  return * theTMBMiniScope;
364 }
365 
366 
368 {
369  if (!theBlockedCFEBIsPresent) throw("No TMB Blocked CFEB in this chamber");
370  return * theTMBBlockedCFEB;
371 }
372 
373 
374 std::bitset<22> CSCTMBData::nextCRC22_D16(const std::bitset<16>& D,
375  const std::bitset<22>& C)
376 {
377  std::bitset<22> NewCRC;
378 
379  NewCRC[ 0] = D[ 0] ^ C[ 6];
380  NewCRC[ 1] = D[ 1] ^ D[ 0] ^ C[ 6] ^ C[ 7];
381  NewCRC[ 2] = D[ 2] ^ D[ 1] ^ C[ 7] ^ C[ 8];
382  NewCRC[ 3] = D[ 3] ^ D[ 2] ^ C[ 8] ^ C[ 9];
383  NewCRC[ 4] = D[ 4] ^ D[ 3] ^ C[ 9] ^ C[10];
384  NewCRC[ 5] = D[ 5] ^ D[ 4] ^ C[10] ^ C[11];
385  NewCRC[ 6] = D[ 6] ^ D[ 5] ^ C[11] ^ C[12];
386  NewCRC[ 7] = D[ 7] ^ D[ 6] ^ C[12] ^ C[13];
387  NewCRC[ 8] = D[ 8] ^ D[ 7] ^ C[13] ^ C[14];
388  NewCRC[ 9] = D[ 9] ^ D[ 8] ^ C[14] ^ C[15];
389  NewCRC[10] = D[10] ^ D[ 9] ^ C[15] ^ C[16];
390  NewCRC[11] = D[11] ^ D[10] ^ C[16] ^ C[17];
391  NewCRC[12] = D[12] ^ D[11] ^ C[17] ^ C[18];
392  NewCRC[13] = D[13] ^ D[12] ^ C[18] ^ C[19];
393  NewCRC[14] = D[14] ^ D[13] ^ C[19] ^ C[20];
394  NewCRC[15] = D[15] ^ D[14] ^ C[20] ^ C[21];
395  NewCRC[16] = D[15] ^ C[ 0] ^ C[21];
396  NewCRC[17] = C[ 1];
397  NewCRC[18] = C[ 2];
398  NewCRC[19] = C[ 3];
399  NewCRC[20] = C[ 4];
400  NewCRC[21] = C[ 5];
401 
402  return NewCRC;
403 }
404 
405 
406 boost::dynamic_bitset<> CSCTMBData::pack()
407 {
408  boost::dynamic_bitset<> result = bitset_utilities::ushortToBitset(theTMBHeader.sizeInWords()*16,
409  theTMBHeader.data());
410  boost::dynamic_bitset<> clctData = bitset_utilities::ushortToBitset(theCLCTData.sizeInWords()*16,
411  theCLCTData.data());
412  result = bitset_utilities::append(result,clctData);
413  boost::dynamic_bitset<> newResult = result;
414 // theTMBTrailer.setCRC(TMBCRCcalc());
415 
417  theTMBTrailer.data());
418  result = bitset_utilities::append(result,tmbTrailer);
419 
420  // now convert to a vector<bitset<16>>, so we can calculate the crc
421  std::vector<std::bitset<16> > wordVector;
422  // try to tune so it stops before the e0f line
423  for(unsigned pos = 0; pos < result.size()-16; pos += 16)
424  {
425  std::bitset<16> word;
426  for(int i = 0; i < 16; ++i)
427  {
428  word[i] = result[pos+i];
429  }
430  wordVector.push_back(word);
431  }
432  theTMBTrailer.setCRC(calCRC22(wordVector).to_ulong());
434  theTMBTrailer.data());
435  newResult = bitset_utilities::append(newResult, tmbTrailer);
436 
437  return newResult;
438 }
439 
440 
442 {
443  CSCTMBData tmbData;
444  cscClassPackerCompare(tmbData);
445 }
446 
int sizeInWords() const
in 16-bit words
Definition: CSCCLCTData.h:40
CSCTMBTrailer * tmbTrailer()
Definition: CSCTMBData.h:49
unsigned theE0FLine
Definition: CSCTMBData.h:75
CSCCLCTData * clctData()
Definition: CSCTMBData.h:39
int i
Definition: DBlmapReader.cc:9
bool theRPCDataIsPresent
Definition: CSCTMBData.h:96
uint16_t NTBins() const
Definition: CSCTMBHeader.h:71
CSCTMBTrailer theTMBTrailer
Definition: CSCTMBData.h:92
static void selfTest()
tests packing
Definition: CSCTMBData.cc:441
int TMBCRCcalc()
Definition: CSCTMBData.cc:121
unsigned short * theOriginalBuffer
Definition: CSCTMBData.h:71
boost::dynamic_bitset pack()
not const because it sets size int TMBTrailer
Definition: CSCTMBData.cc:406
bool theBlockedCFEBIsPresent
Definition: CSCTMBData.h:89
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
std::bitset< 22 > nextCRC22_D16(const std::bitset< 16 > &D, const std::bitset< 22 > &C)
Definition: CSCTMBData.cc:374
unsigned short size_
Definition: CSCTMBData.h:94
bool theTMBMiniScopeIsPresent
Definition: CSCTMBData.h:86
int sizeInWords() const
in 16-bit frames
Definition: CSCTMBTrailer.h:47
unsigned theB0CLine
Definition: CSCTMBData.h:74
CSCRPCData theRPCData
Definition: CSCTMBData.h:79
CSCTMBBlockedCFEB & tmbBlockedCFEB() const
Definition: CSCTMBData.cc:367
unsigned short * data()
Definition: CSCTMBTrailer.h:48
bool cscClassPackerCompare(T &t)
uint16_t NCFEBs() const
Definition: CSCTMBHeader.h:74
std::bitset< 22 > calCRC22(const std::vector< std::bitset< 16 > > &datain)
Definition: CSCTMBData.cc:343
static bool debug
Definition: CSCTMBData.h:93
CSCTMBHeader theTMBHeader
Definition: CSCTMBData.h:77
CSCTMBBlockedCFEB * theTMBBlockedCFEB
Definition: CSCTMBData.h:90
unsigned short * data()
Definition: CSCCLCTData.h:38
tuple result
Definition: query.py:137
unsigned short * data()
Definition: CSCTMBHeader.h:99
CSCTMBScope & tmbScope() const
Definition: CSCTMBData.cc:354
void setCRC(int crc)
int sizeInWords()
Definition: CSCRPCData.h:19
bool first
Definition: L1TdeRCT.cc:94
#define LogTrace(id)
unsigned short int sizeInWords() const
Definition: CSCTMBHeader.h:93
CSCTMBMiniScope * theTMBMiniScope
Definition: CSCTMBData.h:87
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:150
bool theTMBScopeIsPresent
The TMB scope is not present in most of data hence its dynamic.
Definition: CSCTMBData.h:81
bool check() const
Definition: CSCCLCTData.cc:212
bool check() const
Definition: CSCTMBHeader.h:106
CSCTMBScope * theTMBScope
Definition: CSCTMBData.h:82
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
size_(0)
Definition: OwnArray.h:181
bool checkSize() const
sees if the size adds up to the word count
Definition: CSCTMBData.cc:337
boost::dynamic_bitset ushortToBitset(const unsigned int numberOfBits, unsigned short *buf)
this method takes numberOfBits bits from unsigned short * array and returns them in the bitset obj...
CSCCLCTData theCLCTData
Definition: CSCTMBData.h:78
int UnpackTMB(unsigned short *buf)
Definition: CSCTMBData.cc:139
CSCTMBMiniScope & tmbMiniScope() const
Definition: CSCTMBData.cc:360
int findLine(unsigned short *buf, unsigned short marker, int first, int maxToDo)
Definition: CSCTMBData.cc:112