CMS 3D CMS Logo

ESUnpacker.cc
Go to the documentation of this file.
7 
8 #include <fstream>
9 
11  : pset_(ps), fedId_(0), run_number_(0), orbit_number_(0), bx_(0), lv1_(0), trgtype_(0) {
12  debug_ = pset_.getUntrackedParameter<bool>("debugMode", false);
13  lookup_ = ps.getParameter<edm::FileInPath>("LookupTable");
14 
15  m1 = ~(~Word64(0) << 1);
16  m2 = ~(~Word64(0) << 2);
17  m4 = ~(~Word64(0) << 4);
18  m5 = ~(~Word64(0) << 5);
19  m6 = ~(~Word64(0) << 6);
20  m8 = ~(~Word64(0) << 8);
21  m12 = ~(~Word64(0) << 12);
22  m16 = ~(~Word64(0) << 16);
23  m32 = ~(~Word64(0) << 32);
24 
25  // read in look-up table
26  int nLines, iz, ip, ix, iy, fed, kchip, pace, bundle, fiber, optorx;
27  std::ifstream file;
28  file.open(lookup_.fullPath().c_str());
29  if (file.is_open()) {
30  file >> nLines;
31 
32  for (int i = 0; i < nLines; ++i) {
33  file >> iz >> ip >> ix >> iy >> fed >> kchip >> pace >> bundle >> fiber >> optorx;
34 
35  zside_[kchip - 1][pace - 1] = iz;
36  pl_[kchip - 1][pace - 1] = ip;
37  x_[kchip - 1][pace - 1] = ix;
38  y_[kchip - 1][pace - 1] = iy;
39  }
40 
41  } else {
42  edm::LogWarning("Invalid Data") << "ESUnpacker::ESUnpacker : Look up table file can not be found in "
43  << lookup_.fullPath().c_str();
44  }
45 }
46 
48 
50  const FEDRawData& rawData,
51  ESRawDataCollection& dccs,
53  ESDigiCollection& digis) {
54  unsigned int nWords = rawData.size() / sizeof(Word64);
55  if (nWords == 0)
56  return;
57  int dccWords = 6;
58  int head, kPACE[4], kFlag1, kFlag2, kBC, kEC, optoBC, optoEC;
59  int kid = -1;
60 
61  ESDCCHeaderBlock ESDCCHeader;
62  ESDCCHeader.setFedId(fedId);
63 
64  // Event header
65  const Word64* header = reinterpret_cast<const Word64*>(rawData.data());
66  --header;
67  bool moreHeaders = true;
68  while (moreHeaders) {
69  ++header;
70  FEDHeader ESHeader(reinterpret_cast<const unsigned char*>(header));
71  if (!ESHeader.check()) {
72  if (debug_)
73  edm::LogWarning("Invalid Data") << "ES : Failed header check !";
74  return;
75  }
76 
77  fedId_ = ESHeader.sourceID();
78  lv1_ = ESHeader.lvl1ID();
79  bx_ = ESHeader.bxID();
80 
81  if (debug_) {
82  LogDebug("ESUnpacker") << "[ESUnpacker]: FED Header candidate. Is header? " << ESHeader.check();
83  if (ESHeader.check())
84  LogDebug("ESUnpacker") << ". BXID: " << bx_ << " SourceID : " << fedId_ << " L1ID: " << lv1_;
85  else
86  LogDebug("ESUnpacker") << " WARNING!, this is not a ES Header";
87  }
88 
89  moreHeaders = ESHeader.moreHeaders();
90  }
91  if (fedId != fedId_) {
92  if (debug_)
93  edm::LogWarning("Invalid Data") << "Invalid ES data with source id " << fedId_;
94  ESDCCHeader.setDCCErrors(1);
95  dccs.push_back(ESDCCHeader);
96  return;
97  }
98  ESDCCHeader.setLV1(lv1_);
99  ESDCCHeader.setBX(bx_);
100 
101  // Event trailer
102  int slinkCRC = 1;
103  const Word64* trailer = reinterpret_cast<const Word64*>(rawData.data()) + (nWords - 1);
104  ++trailer;
105  bool moreTrailers = true;
106  while (moreTrailers) {
107  --trailer;
108  FEDTrailer ESTrailer(reinterpret_cast<const unsigned char*>(trailer));
109  if (!ESTrailer.check()) {
110  ++trailer;
111  if (debug_)
112  edm::LogWarning("Invalid Data") << "ES : Failed trailer check !";
113  return;
114  }
115  if (ESTrailer.fragmentLength() != nWords) {
116  if (debug_)
117  edm::LogWarning("Invalid Data") << "Invalid ES data : the length is not correct !";
118  ESDCCHeader.setDCCErrors(2);
119  dccs.push_back(ESDCCHeader);
120  return;
121  }
122  if (ESTrailer.fragmentLength() < 8) {
123  if (debug_)
124  edm::LogWarning("Invalid Data") << "Invalid ES data : the length is not correct !";
125  ESDCCHeader.setDCCErrors(3);
126  dccs.push_back(ESDCCHeader);
127  return;
128  }
129  slinkCRC = (*trailer >> 2) & 0x1;
130  if (debug_) {
131  LogDebug("ESUnpacker") << "[ESUnpacker]: FED Trailer candidate. Is trailer? " << ESTrailer.check();
132  if (ESTrailer.check())
133  LogDebug("ESUnpacker") << ". Length of the ES event: " << ESTrailer.fragmentLength();
134  else
135  LogDebug("ESUnpacker") << " WARNING!, this is not a ES Trailer";
136  }
137 
138  moreTrailers = ESTrailer.moreTrailers();
139  }
140 
141  if (slinkCRC != 0) {
142  ESDCCHeader.setDCCErrors(101);
143  dccs.push_back(ESDCCHeader);
144  return;
145  }
146 
147  // DCC data
148  std::vector<int> FEch_status;
149  int dccHeaderCount = 0;
150  int dccLineCount = 0;
151  int dccHead, dccLine;
152  int dccCRC1_ = 0;
153  int dccCRC2_ = 0;
154  int dccCRC3_ = 0;
155  for (const Word64* word = (header + 1); word != (header + dccWords + 1); ++word) {
156  if (debug_)
157  LogDebug("ESUnpacker") << "DCC : " << print(*word);
158  dccHead = (*word >> 60) & m4;
159  if (dccHead == 3)
160  dccHeaderCount++;
161  dccLine = (*word >> 56) & m4;
162  dccLineCount++;
163  if (dccLine != dccLineCount) {
164  if (debug_)
165  edm::LogWarning("Invalid Data") << "Invalid ES data : DCC header order is not correct !";
166  ESDCCHeader.setDCCErrors(4);
167  dccs.push_back(ESDCCHeader);
168  return;
169  }
170  if (dccLineCount == 1) {
171  dccCRC1_ = (*word >> 24) & m1;
172  dccCRC2_ = (*word >> 25) & m1;
173  dccCRC3_ = (*word >> 26) & m1;
174  } else if (dccLineCount == 2) {
175  runtype_ = (*word >> 0) & m4;
176  seqtype_ = (*word >> 4) & m4;
177  dac_ = (*word >> 8) & m12;
178  gain_ = (*word >> 20) & m1;
179  precision_ = (*word >> 21) & m1;
180  trgtype_ = (*word >> 34) & m6;
181 
182  ESDCCHeader.setRunType(runtype_);
183  ESDCCHeader.setSeqType(seqtype_);
184  ESDCCHeader.setTriggerType(trgtype_);
185  ESDCCHeader.setDAC(dac_);
186  ESDCCHeader.setGain(gain_);
187  ESDCCHeader.setPrecision(precision_);
188  }
189  if (dccLineCount == 3) {
190  orbit_number_ = (*word >> 0) & m32;
191  vminor_ = (*word >> 40) & m8;
192  vmajor_ = (*word >> 48) & m8;
193 
194  ESDCCHeader.setOrbitNumber(orbit_number_);
195  ESDCCHeader.setMajorVersion(vmajor_);
196  ESDCCHeader.setMinorVersion(vminor_);
197  }
198  if (dccLineCount == 4)
199  optoRX0_ = (*word >> 48) & m8;
200  if (dccLineCount == 5)
201  optoRX1_ = (*word >> 48) & m8;
202  if (dccLineCount == 6)
203  optoRX2_ = (*word >> 48) & m8;
204  if (dccLineCount >= 4) {
205  for (unsigned int j = 0; j < 12; ++j) {
206  FEch_[(dccLineCount - 4) * 12 + j] = (*word >> (j * 4)) & m4;
207  FEch_status.push_back(FEch_[(dccLineCount - 4) * 12 + j]);
208  }
209  }
210  }
211  if (vmajor_ < 4) {
212  if (debug_)
213  edm::LogWarning("Invalid Data") << "Invalid ES data format : " << vmajor_ << " " << vminor_;
214  return;
215  }
216  if (dccHeaderCount != 6) {
217  edm::LogWarning("Invalid Data") << "Invalid ES data : DCC header lines are " << dccHeaderCount;
218  ESDCCHeader.setDCCErrors(5);
219  dccs.push_back(ESDCCHeader);
220  return;
221  }
222  ESDCCHeader.setOptoRX0(optoRX0_ + dccCRC1_);
223  ESDCCHeader.setOptoRX1(optoRX1_ + dccCRC2_);
224  ESDCCHeader.setOptoRX2(optoRX2_ + dccCRC3_);
225  ESDCCHeader.setFEChannelStatus(FEch_status);
226  int enableOptoRX[3] = {-1, -1, -1};
227  int NenableOptoRX = 0;
228  if (optoRX0_ == 128) {
229  enableOptoRX[NenableOptoRX] = 0;
230  NenableOptoRX++;
231  }
232  if (optoRX1_ == 128) {
233  enableOptoRX[NenableOptoRX] = 1;
234  NenableOptoRX++;
235  }
236  if (optoRX2_ == 128) {
237  enableOptoRX[NenableOptoRX] = 2;
238  }
239 
240  // Event data
241  int iopto = 0;
242  int opto = -1;
243  for (const Word64* word = (header + dccWords + 1); word != trailer; ++word) {
244  if (debug_)
245  LogDebug("ESUnpacker") << "Event : " << print(*word);
246 
247  head = (*word >> 60) & m4;
248 
249  if (head == 12) {
250  if ((opto == 0 && ESDCCHeader.getOptoRX0() == 129) || (opto == 1 && ESDCCHeader.getOptoRX1() == 129) ||
251  (opto == 2 && ESDCCHeader.getOptoRX2() == 129))
252  word2digi(kid, kPACE, *word, digis);
253  } else if (head == 9) {
254  kid = (*word >> 2) & 0x07ff;
255  kPACE[0] = (*word >> 16) & m1;
256  kPACE[1] = (*word >> 17) & m1;
257  kPACE[2] = (*word >> 18) & m1;
258  kPACE[3] = (*word >> 19) & m1;
259  kFlag2 = (*word >> 20) & m4;
260  kFlag1 = (*word >> 24) & m8;
261  kBC = (*word >> 32) & m16;
262  kEC = (*word >> 48) & m8;
263 
264  ESKCHIPBlock ESKCHIP;
265  ESKCHIP.setId(kid);
266  ESKCHIP.setBC(kBC);
267  ESKCHIP.setEC(kEC);
268  ESKCHIP.setOptoBC(optoBC);
269  ESKCHIP.setOptoEC(optoEC);
270  ESKCHIP.setFlag1(kFlag1);
271  ESKCHIP.setFlag2(kFlag2);
272  kchips.push_back(ESKCHIP);
273  } else if (head == 6) {
274  optoBC = (*word >> 32) & m16;
275  optoEC = (*word >> 48) & m8;
276 
277  opto = enableOptoRX[iopto];
278  if (opto == 0)
279  ESDCCHeader.setOptoBC0(optoBC);
280  else if (opto == 1)
281  ESDCCHeader.setOptoBC1(optoBC);
282  else if (opto == 2)
283  ESDCCHeader.setOptoBC2(optoBC);
284  if (iopto < 2)
285  ++iopto;
286  }
287  }
288 
289  dccs.push_back(ESDCCHeader);
290 }
291 
292 void ESUnpacker::word2digi(int kid, int kPACE[4], const Word64& word, ESDigiCollection& digis) {
293  int pace = (word >> 53) & m2;
294  if (kPACE[pace] == 0)
295  return;
296  if (kid > 1511 || kid < 1)
297  return;
298 
299  int adc[3];
300  adc[0] = (word >> 0) & m16;
301  adc[1] = (word >> 16) & m16;
302  adc[2] = (word >> 32) & m16;
303  int strip = (word >> 48) & m5;
304 
305  if (debug_)
306  LogDebug("ESUnpacker") << kid << " " << strip << " " << pace << " " << adc[0] << " " << adc[1] << " " << adc[2];
307 
308  int zside, plane, ix, iy;
309  zside = zside_[kid - 1][pace];
310  plane = pl_[kid - 1][pace];
311  ix = x_[kid - 1][pace];
312  iy = y_[kid - 1][pace];
313 
314  // convert strip number from electronics id to detector id
315  if (vmajor_ == 4 && (vminor_ == 2 || vminor_ == 3)) {
316  if (zside == 1 && plane == 1 && iy <= 20)
317  strip = 31 - strip;
318  if (zside == 1 && plane == 2 && ix > 20)
319  strip = 31 - strip;
320  if (zside == -1 && plane == 1 && iy > 20)
321  strip = 31 - strip;
322  if (zside == -1 && plane == 2 && ix <= 20)
323  strip = 31 - strip;
324  }
325 
326  if (debug_)
327  LogDebug("ESUnpacker") << "DetId : " << zside << " " << plane << " " << ix << " " << iy << " " << strip + 1;
328 
329  if (ESDetId::validDetId(strip + 1, ix, iy, plane, zside)) {
330  ESDetId detId(strip + 1, ix, iy, plane, zside);
332  df.setSize(3);
333 
334  for (int i = 0; i < 3; i++)
335  df.setSample(i, adc[i]);
336 
337  digis.push_back(df);
338 
339  if (debug_)
340  LogDebug("ESUnpacker") << "Si : " << detId.zside() << " " << detId.plane() << " " << detId.six() << " "
341  << detId.siy() << " " << detId.strip() << " (" << kid << "," << pace << ") "
342  << df.sample(0).adc() << " " << df.sample(1).adc() << " " << df.sample(2).adc();
343  }
344 }
345 
347  std::ostringstream str;
348  str << "Word64: " << reinterpret_cast<const std::bitset<64>&>(word);
349  return str.str();
350 }
void setOptoBC2(const int &optoBC2)
int runtype_
Definition: ESUnpacker.h:54
void setOrbitNumber(const int &orbit)
int vmajor_
Definition: ESUnpacker.h:58
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
bool debug_
Definition: ESUnpacker.h:64
void setGain(const int &gain)
edm::FileInPath lookup_
Definition: ESUnpacker.h:65
void setOptoBC(const int &OptoBC)
Definition: ESKCHIPBlock.h:27
void setFEChannelStatus(const std::vector< int > &FEch)
void setFedId(const int &fedId)
void setId(const int &kId)
Definition: ESKCHIPBlock.h:14
int fedId_
Definition: ESUnpacker.h:46
int optoRX0_
Definition: ESUnpacker.h:59
std::string fullPath() const
Definition: FileInPath.cc:161
int y_[4288][4]
Definition: ESUnpacker.h:72
int getOptoRX1() const
bool check() const
Check that the header is OK.
Definition: FEDHeader.cc:44
uint16_t sourceID() const
Identifier of the FED.
Definition: FEDHeader.cc:19
void setOptoRX1(const int &optoRX1)
void push_back(T const &t)
Word64 m5
Definition: ESUnpacker.h:70
int x_[4288][4]
Definition: ESUnpacker.h:72
int zside(DetId const &)
Word64 m12
Definition: ESUnpacker.h:70
void setPrecision(const int &precision)
void setOptoBC0(const int &optoBC0)
void setDCCErrors(const int &dccErrs)
uint32_t fragmentLength() const
The length of the event fragment counted in 64-bit words including header and trailer.
Definition: FEDTrailer.cc:13
T getUntrackedParameter(std::string const &, T const &) const
ESUnpacker(const edm::ParameterSet &ps)
Definition: ESUnpacker.cc:10
void setEC(const int &EC)
Definition: ESKCHIPBlock.h:26
int zside_[4288][4]
Definition: ESUnpacker.h:72
void setBX(const int &BX)
uint64_t word
void setFlag1(const int &flag1)
Definition: ESKCHIPBlock.h:29
int getOptoRX2() const
int seqtype_
Definition: ESUnpacker.h:55
int optoRX2_
Definition: ESUnpacker.h:61
void setMajorVersion(const int &vmajor)
void setSeqType(const int &seqType)
Word64 m1
Definition: ESUnpacker.h:70
int vminor_
Definition: ESUnpacker.h:57
void setLV1(const int &LV1)
Word64 m32
Definition: ESUnpacker.h:70
void setOptoBC1(const int &optoBC1)
bool moreHeaders() const
Definition: FEDHeader.cc:23
Word64 m2
Definition: ESUnpacker.h:70
int optoRX1_
Definition: ESUnpacker.h:60
void interpretRawData(int fedId, const FEDRawData &rawData, ESRawDataCollection &dccs, ESLocalRawDataCollection &kchips, ESDigiCollection &digis)
Definition: ESUnpacker.cc:49
void setOptoRX0(const int &optoRX0)
void setMinorVersion(const int &vminor)
void setOptoEC(const int &OptoEC)
Definition: ESKCHIPBlock.h:28
uint16_t bxID() const
The bunch crossing number.
Definition: FEDHeader.cc:17
void word2digi(int kchip, int kPACE[4], const Word64 &word, ESDigiCollection &digis)
Definition: ESUnpacker.cc:292
void setOptoRX2(const int &optoRX2)
int precision_
Definition: ESUnpacker.h:53
void setFlag2(const int &flag2)
Definition: ESKCHIPBlock.h:30
int pl_[4288][4]
Definition: ESUnpacker.h:72
static bool validDetId(int istrip, int ixs, int iys, int iplane, int iz)
check if a valid index combination
Definition: ESDetId.cc:15
void push_back(unsigned int i)
Word64 m8
Definition: ESUnpacker.h:70
int FEch_[36]
Definition: ESUnpacker.h:62
void setBC(const int &BC)
Definition: ESKCHIPBlock.h:25
int getOptoRX0() const
void setTriggerType(const int &trgType)
void setRunType(const int &runType)
Word64 m6
Definition: ESUnpacker.h:70
const edm::ParameterSet pset_
Definition: ESUnpacker.h:41
uint32_t lvl1ID() const
Level-1 event number generated by the TTC system.
Definition: FEDHeader.cc:15
Log< level::Warning, false > LogWarning
Word64 m16
Definition: ESUnpacker.h:70
unsigned long long Word64
Definition: ESUnpacker.h:25
#define str(s)
int orbit_number_
Definition: ESUnpacker.h:48
void setDAC(const int &dac)
bool check() const
Check that the trailer is OK.
Definition: FEDTrailer.cc:45
int trgtype_
Definition: ESUnpacker.h:56
bool moreTrailers() const
Definition: FEDTrailer.cc:21
uint16_t *__restrict__ uint16_t const *__restrict__ adc
#define LogDebug(id)
Word64 m4
Definition: ESUnpacker.h:70
std::string print(const Word64 &word) const
Definition: ESUnpacker.cc:346