CMS 3D CMS Logo

VFATFrame.cc
Go to the documentation of this file.
1 /****************************************************************************
2 *
3 * This is a part of the TOTEM testbeam/monitoring software.
4 * This is a part of the TOTEM offline software.
5 * Authors:
6 * Jan Kašpar (jan.kaspar@gmail.com)
7 * Leszek Grzanka
8 *
9 ****************************************************************************/
10 
12 
13 #include <cstdio>
14 #include <cstring>
15 
17  : presenceFlags(15), // by default BC, EC, ID and CRC are present
18  daqErrorFlags(0), // by default, no DAQ error
19  numberOfClusters(0) // no clusters by default
20 {
21  if (_data)
22  setData(_data);
23  else
24  memset(data, 0, 12 * sizeof(word));
25 }
26 
27 void VFATFrame::setData(const VFATFrame::word *_data) { memcpy(data, _data, 24); }
28 
29 std::vector<unsigned char> VFATFrame::getActiveChannels() const {
30  std::vector<unsigned char> channels;
31 
32  for (int i = 0; i < 8; i++) {
33  // quick check
34  if (!data[1 + i])
35  continue;
36 
37  // go throug bits
38  word mask;
39  char offset;
40  for (mask = 1 << 15, offset = 15; mask; mask >>= 1, offset--) {
41  if (data[1 + i] & mask)
42  channels.push_back(i * 16 + offset);
43  }
44  }
45 
46  return channels;
47 }
48 
50  if (isIDPresent() && (data[9] & 0xF000) != 0xE000)
51  return false;
52 
53  if (isECPresent() && (data[10] & 0xF000) != 0xC000)
54  return false;
55 
56  if (isBCPresent() && (data[11] & 0xF000) != 0xA000)
57  return false;
58 
59  return true;
60 }
61 
63  if (isIDPresent() && (data[2] & 0xF000) != 0xE000)
64  return false;
65 
66  if (isECPresent() && (data[1] & 0xF000) != 0xC000)
67  return false;
68 
69  if (isBCPresent() && (data[0] & 0xF000) != 0xA000)
70  return false;
71 
72  return true;
73 }
74 
75 bool VFATFrame::checkCRC() const {
76  // check DAQ error flags
77  if (daqErrorFlags != 0)
78  return false;
79 
80  // return true if CRC not present
81  if (!isCRCPresent())
82  return true;
83 
84  // compare CRC
85  word crc_fin = 0xffff;
86 
87  for (int i = 11; i >= 1; i--)
88  crc_fin = calculateCRC(crc_fin, data[i]);
89 
90  return (crc_fin == data[0]);
91 }
92 
93 bool VFATFrame::checkCRCT2() const {
94  // return true if CRC not present
95  if (!isCRCPresent())
96  return true;
97 
98  // compare CRC
99  word crc_fin = 0xffff;
100 
101  for (int i = 0; i < 11; i++)
102  crc_fin = calculateCRC(crc_fin, data[i]);
103 
104  return (crc_fin == data[11]);
105 }
106 
108  word v = 0x0001;
109  word mask = 0x0001;
110  bool d = false;
111  word crc_temp = crc_in;
112  unsigned char datalen = 16;
113 
114  for (int i = 0; i < datalen; i++) {
115  if (dato & v)
116  d = true;
117  else
118  d = false;
119 
120  if ((crc_temp & mask) ^ d)
121  crc_temp = crc_temp >> 1 ^ 0x8408;
122  else
123  crc_temp = crc_temp >> 1;
124 
125  v <<= 1;
126  }
127 
128  return crc_temp;
129 }
130 
131 void VFATFrame::Print(bool binary) const {
132  if (binary) {
133  for (int i = 0; i < 12; i++) {
134  const word &w = data[11 - i];
135  word mask = (1 << 15);
136  for (int j = 0; j < 16; j++) {
137  if (w & mask)
138  printf("1");
139  else
140  printf("0");
141  mask = (mask >> 1);
142  if ((j + 1) % 4 == 0)
143  printf("|");
144  }
145  printf("\n");
146  }
147  } else {
148  printf("ID = %03x, BC = %04u, EC = %03u, flags = %2u, CRC = %04x ",
149  getChipID(),
150  getBC(),
151  getEC(),
152  getFlags(),
153  getCRC());
154 
155  if (checkCRC())
156  printf("( OK), footprint ");
157  else
158  printf("(FAIL), footprint ");
159 
160  if (checkFootprint())
161  printf(" OK");
162  else
163  printf("FAIL");
164 
165  printf(", frame = %04x|%04x|%04x|", data[11], data[10], data[9]);
166  for (int i = 8; i > 0; i--)
167  printf("%04x", data[i]);
168  printf("|%04x", data[0]);
169 
170  printf(", presFl=%x", presenceFlags);
171  printf(", daqErrFl=%x", daqErrorFlags);
172 
173  printf("\n");
174  }
175 }
176 
177 void VFATFrame::PrintT2(bool binary) const {
178  if (binary) {
179  for (int i = 0; i < 12; i++) {
180  const word &w = data[i];
181  word mask = (1 << 15);
182  for (int j = 0; j < 16; j++) {
183  if (w & mask)
184  printf("1");
185  else
186  printf("0");
187  mask = (mask >> 1);
188  if ((j + 1) % 4 == 0)
189  printf("|");
190  }
191  printf("\n");
192  }
193  } else {
194  // print right CRC
195  word crc_fin = 0xffff;
196 
197  for (int i = 0; i < 11; i++)
198  crc_fin = calculateCRC(crc_fin, data[i]);
199 
200  printf("CRC = %04x ", getCRCT2());
201 
202  if (checkCRCT2())
203  printf("( OK), footprint ");
204  else
205  printf("(FAIL, right = %04x), footprint ", crc_fin);
206 
207  if (checkFootprintT2())
208  printf(" OK");
209  else
210  printf("FAIL");
211 
212  printf("Frame = %04x|%04x|%04x|", data[0], data[1], data[2]);
213  for (int i = 3; i < 11; i++)
214  printf("%04x", data[i]);
215  printf("|%04x", data[11]);
216 
217  printf("\n");
218  }
219 }
virtual bool checkCRC() const
Definition: VFATFrame.cc:75
word data[12]
Definition: VFATFrame.h:137
void Print(bool binary=false) const
Definition: VFATFrame.cc:131
VFATFrame::word getEC() const
Returns Event Counter (EV<7:0>).
Definition: VFATFrame.h:46
T w() const
bool checkFootprint() const
Definition: VFATFrame.cc:49
VFATFrame::word getCRC() const
Returns the CRC.
Definition: VFATFrame.h:55
uint16_t word
Definition: VFATFrame.h:21
VFATFrame::word getFlags() const
Returns flags.
Definition: VFATFrame.h:49
virtual std::vector< unsigned char > getActiveChannels() const
Definition: VFATFrame.cc:29
bool isBCPresent() const
Returns true if the BC word is present in the frame.
Definition: VFATFrame.h:64
uint8_t daqErrorFlags
Error flag as given by certain versions of DAQ.
Definition: VFATFrame.h:149
uint8_t presenceFlags
Definition: VFATFrame.h:146
virtual bool checkCRCT2() const
Definition: VFATFrame.cc:93
VFATFrame::word getChipID() const
Returns ChipID (ChipID<11:0>).
Definition: VFATFrame.h:52
d
Definition: ztail.py:151
VFATFrame::word getCRCT2() const
Returns the CRC, for non-reversed TOTEM T2.
Definition: VFATFrame.h:58
void PrintT2(bool binary=false) const
Definition: VFATFrame.cc:177
bool checkFootprintT2() const
Definition: VFATFrame.cc:62
bool isCRCPresent() const
Returns true if the CRC word is present in the frame.
Definition: VFATFrame.h:73
static word calculateCRC(word crc_in, word dato)
internaly used to check CRC
Definition: VFATFrame.cc:107
bool isECPresent() const
Returns true if the EC word is present in the frame.
Definition: VFATFrame.h:67
bool isIDPresent() const
Returns true if the ID word is present in the frame.
Definition: VFATFrame.h:70
VFATFrame::word getBC() const
Returns Bunch Crossing number (BC<11:0>).
Definition: VFATFrame.h:43
void setData(const word *_data)
Copies a memory block to data buffer.
Definition: VFATFrame.cc:27
VFATFrame(const word *_data=nullptr)
Definition: VFATFrame.cc:16