CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ESDataFormatterV4.cc
Go to the documentation of this file.
1 #include <vector>
2 #include <map>
3 #include <set>
4 #include <algorithm>
5 
12 
14 
15 using namespace std;
16 using namespace edm;
17 
18 const int ESDataFormatterV4::bDHEAD = 2;
19 const int ESDataFormatterV4::bDH = 6;
20 const int ESDataFormatterV4::bDEL = 24;
21 const int ESDataFormatterV4::bDERR = 8;
22 const int ESDataFormatterV4::bDRUN = 24;
23 const int ESDataFormatterV4::bDRUNTYPE = 32;
24 const int ESDataFormatterV4::bDTRGTYPE = 16;
25 const int ESDataFormatterV4::bDCOMFLAG = 8;
26 const int ESDataFormatterV4::bDORBIT = 32;
27 const int ESDataFormatterV4::bDVMAJOR = 8;
28 const int ESDataFormatterV4::bDVMINOR = 8;
29 const int ESDataFormatterV4::bDCH = 4;
30 const int ESDataFormatterV4::bDOPTO = 8;
31 
32 const int ESDataFormatterV4::sDHEAD = 28;
33 const int ESDataFormatterV4::sDH = 24;
34 const int ESDataFormatterV4::sDEL = 0;
35 const int ESDataFormatterV4::sDERR = bDEL + sDEL;
36 const int ESDataFormatterV4::sDRUN = 0;
37 const int ESDataFormatterV4::sDRUNTYPE = 0;
38 const int ESDataFormatterV4::sDTRGTYPE = 0;
39 const int ESDataFormatterV4::sDCOMFLAG = bDTRGTYPE + sDTRGTYPE;
40 const int ESDataFormatterV4::sDORBIT = 0;
41 const int ESDataFormatterV4::sDVMINOR = 8;
42 const int ESDataFormatterV4::sDVMAJOR = bDVMINOR + sDVMINOR;
43 const int ESDataFormatterV4::sDCH = 0;
44 const int ESDataFormatterV4::sDOPTO = 16;
45 
46 const int ESDataFormatterV4::bKEC = 8; // KCHIP packet event counter
47 const int ESDataFormatterV4::bKFLAG2 = 8;
48 const int ESDataFormatterV4::bKBC = 12; // KCHIP packet bunch counter
49 const int ESDataFormatterV4::bKFLAG1 = 4;
50 const int ESDataFormatterV4::bKET = 1;
51 const int ESDataFormatterV4::bKCRC = 1;
52 const int ESDataFormatterV4::bKCE = 1;
53 const int ESDataFormatterV4::bKID = 16;
54 const int ESDataFormatterV4::bFIBER = 6; // Fiber number
55 const int ESDataFormatterV4::bKHEAD1 = 2;
56 const int ESDataFormatterV4::bKHEAD2 = 2;
57 const int ESDataFormatterV4::bKHEAD = 4;
58 
59 const int ESDataFormatterV4::sKEC = 16;
60 const int ESDataFormatterV4::sKFLAG2 = 16;
61 const int ESDataFormatterV4::sKBC = 0;
62 const int ESDataFormatterV4::sKFLAG1 = 24;
63 const int ESDataFormatterV4::sKET = 0;
64 const int ESDataFormatterV4::sKCRC = bKET + sKET;
65 const int ESDataFormatterV4::sKCE = bKCRC + sKCRC;
66 const int ESDataFormatterV4::sKID = 0;
67 const int ESDataFormatterV4::sFIBER = bKID + sKID + 1;
68 const int ESDataFormatterV4::sKHEAD1 = bFIBER + sFIBER + 2;
69 const int ESDataFormatterV4::sKHEAD2 = bKHEAD1 + sKHEAD1;
70 const int ESDataFormatterV4::sKHEAD = 28;
71 
72 const int ESDataFormatterV4::bADC0 = 16;
73 const int ESDataFormatterV4::bADC1 = 16;
74 const int ESDataFormatterV4::bADC2 = 16;
75 const int ESDataFormatterV4::bPACE = 2;
76 const int ESDataFormatterV4::bSTRIP = 5;
77 const int ESDataFormatterV4::bE0 = 1;
78 const int ESDataFormatterV4::bE1 = 1;
79 const int ESDataFormatterV4::bHEAD = 4;
80 
81 const int ESDataFormatterV4::sADC0 = 0;
82 const int ESDataFormatterV4::sADC1 = bADC0 + sADC0;
83 const int ESDataFormatterV4::sADC2 = 0;
84 const int ESDataFormatterV4::sSTRIP = bADC2 + sADC2;
85 const int ESDataFormatterV4::sPACE = bSTRIP + sSTRIP;
86 const int ESDataFormatterV4::sE0 = bSTRIP + sSTRIP + 1;
87 const int ESDataFormatterV4::sE1 = bE0 + sE0;
88 const int ESDataFormatterV4::sHEAD = 28;
89 
90 const int ESDataFormatterV4::bOEMUTTCEC = 32;
91 const int ESDataFormatterV4::bOEMUTTCBC = 16;
92 const int ESDataFormatterV4::bOEMUKEC = 8;
93 const int ESDataFormatterV4::bOHEAD = 4;
94 
95 const int ESDataFormatterV4::sOEMUTTCEC = 0;
96 const int ESDataFormatterV4::sOEMUTTCBC = 0;
97 const int ESDataFormatterV4::sOEMUKEC = 16;
98 const int ESDataFormatterV4::sOHEAD = 28;
99 
101  lookup_ = ps.getUntrackedParameter<FileInPath>("LookupTable");
102 
103  // initialize look-up table
104  for (int i = 0; i < 2; ++i)
105  for (int j = 0; j < 2; ++j)
106  for (int k = 0; k < 40; ++k)
107  for (int m = 0; m < 40; m++) {
108  fedId_[i][j][k][m] = -1;
109  kchipId_[i][j][k][m] = -1;
110  paceId_[i][j][k][m] = -1;
111  bundleId_[i][j][k][m] = -1;
112  fiberId_[i][j][k][m] = -1;
113  optoId_[i][j][k][m] = -1;
114  }
115 
116  for (int i = 0; i < 56; ++i) {
117  for (int j = 0; j < 3; ++j)
118  fedIdOptoRx_[i][j] = false;
119  }
120 
121  for (int i = 0; i < 56; ++i) {
122  for (int j = 0; j < 3; ++j)
123  for (int k = 0; k < 12; k++)
124  fedIdOptoRxFiber_[i][j][k] = false;
125  }
126 
127  // read in look-up table
128  int nLines, iz, ip, ix, iy, fed, kchip, pace, bundle, fiber, optorx;
129  ifstream file;
130  file.open(lookup_.fullPath().c_str());
131  if (file.is_open()) {
132  file >> nLines;
133 
134  for (int i = 0; i < nLines; ++i) {
135  int fedId = -1;
136  file >> iz >> ip >> ix >> iy >> fed >> kchip >> pace >> bundle >> fiber >> optorx;
137 
138  fedId = fedId_[(3 - iz) / 2 - 1][ip - 1][ix - 1][iy - 1] = fed;
139  kchipId_[(3 - iz) / 2 - 1][ip - 1][ix - 1][iy - 1] = kchip;
140  paceId_[(3 - iz) / 2 - 1][ip - 1][ix - 1][iy - 1] = pace - 1;
141  bundleId_[(3 - iz) / 2 - 1][ip - 1][ix - 1][iy - 1] = bundle;
142  fiberId_[(3 - iz) / 2 - 1][ip - 1][ix - 1][iy - 1] = fiber;
143  optoId_[(3 - iz) / 2 - 1][ip - 1][ix - 1][iy - 1] = optorx;
144 
145  if (fedId < FEDNumbering::MINPreShowerFEDID || fedId > FEDNumbering::MAXPreShowerFEDID) {
146  if (debug_)
147  cout << "ESDataFormatterV4::ESDataFormatterV4 : fedId value : " << fedId
148  << " out of ES range, at lookup table line : " << i << endl;
149  } else if (optorx < 1 || optorx > 3) {
150  if (debug_)
151  cout << "ESDataFormatterV4::ESDataFormatterV4 : optorx value : " << optorx
152  << " out of ES range, at lookup table line : " << i << endl;
153  } else { // all good ..
154  int fedidx = fed - FEDNumbering::MINPreShowerFEDID;
155  fedIdOptoRx_[fedidx][optorx - 1] = true;
156  if (fiber > 0 && fiber < 13) {
157  fedIdOptoRxFiber_[fedidx][optorx - 1][fiber - 1] = true;
158  } else {
159  if (debug_)
160  cout << "ESDataFormatterV4::ESDataFormatterV4 : fiber value : " << fiber
161  << " out of ES range, at lookup table line : " << i << endl;
162  }
163  }
164  }
165 
166  } else {
167  if (debug_)
168  cout << "ESDataFormatterV4::ESDataFormatterV4 : Look up table file can not be found in "
169  << lookup_.fullPath().c_str() << endl;
170  }
171 
172  file.close();
173 }
174 
176 
177 struct ltfiber {
178  bool operator()(const pair<int, int> s1, const pair<int, int> s2) const { return (s1.second < s2.second); }
179 };
180 
182  ESDataFormatterV4::Word64 PACESTRIP_MASK = 0x00ff000000000000ull;
183  ESDataFormatterV4::Word64 PACESTRIP_OFFSET = 48ull;
184 
185  ESDataFormatterV4::Word64 val1 = (s1 & PACESTRIP_MASK) >> PACESTRIP_OFFSET;
186  ESDataFormatterV4::Word64 val2 = (s2 & PACESTRIP_MASK) >> PACESTRIP_OFFSET;
187 
188  return (val1 < val2);
189 }
190 
191 void ESDataFormatterV4::DigiToRaw(int fedId, Digis& digis, FEDRawData& fedRawData, Meta_Data const& meta_data) const {
192  int ts[3] = {0, 0, 0};
193  Word32 word1, word2;
194  Word64 word;
195  int numberOfStrips = 0;
196 
197  int optorx_ch_counts[3][12];
198 
199  int kchip, pace, optorx, fiber;
200  map<int, vector<Word64> > map_data;
201  vector<Word64> words;
202 
203  vector<Word32> testVector;
204 
205  set<pair<int, int>, ltfiber> set_of_kchip_fiber_in_optorx[3];
206 
207  map_data.clear();
208 
209  // clean optorx channel status fields:
210  for (int i = 0; i < 3; ++i)
211  for (int j = 0; j < 12; ++j)
212  optorx_ch_counts[i][j] = 0;
213 
214  const DetDigis& detDigis = digis[fedId];
215 
216  if (debug_) {
217  cout << "ESDataFormatterV4::DigiToRaw : FEDID : " << fedId << " size of detDigis : " << detDigis.size() << endl;
218  }
219 
220  for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); ++it) {
221  const ESDataFrame& dataframe = (*it);
222  const ESDetId& detId = dataframe.id();
223 
224  for (int is = 0; is < dataframe.size(); ++is)
225  ts[is] = dataframe.sample(is).adc();
226 
227  kchip = kchipId_[(3 - detId.zside()) / 2 - 1][detId.plane() - 1][detId.six() - 1][detId.siy() - 1];
228  pace = paceId_[(3 - detId.zside()) / 2 - 1][detId.plane() - 1][detId.six() - 1][detId.siy() - 1];
229 
230  if (debug_)
231  cout << "Si : " << detId.zside() << " " << detId.plane() << " " << detId.six() << " " << detId.siy() << " "
232  << detId.strip() << " (" << kchip << "," << pace << ") " << ts[0] << " " << ts[1] << " " << ts[2] << endl;
233 
234  // convert strip number from detector id to electronics id
235  int siz = detId.zside();
236  int sip = detId.plane();
237  int six = detId.six();
238  int siy = detId.siy();
239  int sistrip = detId.strip();
240  if (siz == 1 && sip == 1 && siy <= 20)
241  sistrip = 33 - sistrip;
242  if (siz == 1 && sip == 2 && six > 20)
243  sistrip = 33 - sistrip;
244  if (siz == -1 && sip == 1 && siy > 20)
245  sistrip = 33 - sistrip;
246  if (siz == -1 && sip == 2 && six <= 20)
247  sistrip = 33 - sistrip;
248 
249  word1 = (ts[1] << sADC1) | (ts[0] << sADC0);
250  word2 = (0xc << sHEAD) | (pace << sPACE) | ((sistrip - 1) << sSTRIP) | (ts[2] << sADC2);
251  word = (Word64(word2) << 32) | Word64(word1);
252 
253  map_data[kchip].push_back(word);
254 
255  optorx = optoId_[(3 - detId.zside()) / 2 - 1][detId.plane() - 1][detId.six() - 1][detId.siy() - 1];
256  fiber = fiberId_[(3 - detId.zside()) / 2 - 1][detId.plane() - 1][detId.six() - 1][detId.siy() - 1];
257 
258  optorx_ch_counts[optorx - 1][fiber - 1]++; // increment number of strip hits on fiber status field ;
259 
260  set<pair<int, int>, ltfiber>& theSet = set_of_kchip_fiber_in_optorx[optorx - 1];
261  theSet.insert(pair<int, int>(kchip, fiber));
262 
263  // mark global strip number in this FED
264  ++numberOfStrips;
265  }
266 
267  for (int iopto = 0; iopto < 3; ++iopto) {
268  if (fedIdOptoRx_[fedId - FEDNumbering::MINPreShowerFEDID][iopto]) {
269  word2 = (0x6 << sOHEAD) | (meta_data.kchip_ec << sOEMUKEC) | (meta_data.kchip_bc << sOEMUTTCBC);
270  word1 = (meta_data.kchip_ec << sOEMUTTCEC);
271  word = (Word64(word2) << 32) | Word64(word1);
272  if (debug_)
273  cout << "OPTORX: " << print(word) << endl;
274  words.push_back(word);
275 
276  set<pair<int, int>, ltfiber>& theSet = set_of_kchip_fiber_in_optorx[iopto];
277 
278  if (debug_) {
279  cout << "ESDataFormatterV4::DigiToRaw : FEDID : " << fedId << " size of set_of_kchip_fiber_in_optorx[" << iopto
280  << "] : " << theSet.size() << endl;
281  }
282 
283  set<pair<int, int>, ltfiber>::const_iterator kit = theSet.begin();
284 
285  int ikchip = 0;
286 
287  while (kit != theSet.end()) {
288  const pair<int, int>& kchip_fiber = (*kit);
289 
290  if (debug_)
291  cout << "KCHIP : " << kchip_fiber.first << " FIBER: " << kchip_fiber.second << endl;
292 
293  if (fedIdOptoRxFiber_[fedId - FEDNumbering::MINPreShowerFEDID][iopto][kchip_fiber.second - 1]) {
294  // Set all PACEs enabled for MC
295  word1 = (0 << sKFLAG1) | (0xf << sKFLAG2) | (((kchip_fiber.first << 2) | 0x02) << sKID);
296  word2 = (0x9 << sKHEAD) | (meta_data.kchip_ec << sKEC) | (meta_data.kchip_bc << sKBC);
297 
298  word = (Word64(word2) << 32) | Word64(word1);
299  if (debug_)
300  cout << "KCHIP : " << print(word) << endl;
301 
302  words.push_back(word);
303 
304  vector<Word64>& data = map_data[kchip_fiber.first];
305 
306  // sort against stripid field, as hardware gives this order to strip data :
307  sort(data.begin(), data.end(), ltstrip);
308 
309  for (unsigned int id = 0; id < data.size(); ++id) {
310  if (debug_)
311  cout << "Data : " << print(data[id]) << endl;
312  words.push_back(data[id]);
313  }
314  }
315  ++kit;
316  ++ikchip;
317  }
318  }
319  }
320 
321  int dataSize = (words.size() + 8) * sizeof(Word64);
322 
323  vector<Word64> DCCwords;
324 
325  word2 = (3 << sDHEAD) | (1 << sDH) | (meta_data.run_number << sDRUN);
326  word1 = (numberOfStrips << sDEL) | (0xff << sDERR);
327  word = (Word64(word2) << 32) | Word64(word1);
328  DCCwords.push_back(word);
329 
330  word2 = (3 << sDHEAD) | (2 << sDH);
331  word1 = 0;
332  word = (Word64(word2) << 32) | Word64(word1);
333  DCCwords.push_back(word);
334 
335  word2 = (3 << sDHEAD) | (3 << sDH) | (4 << sDVMAJOR) | (3 << sDVMINOR);
336  word1 = (meta_data.orbit_number << sDORBIT);
337  word = (Word64(word2) << 32) | Word64(word1);
338  DCCwords.push_back(word);
339 
340  for (int iopto = 0; iopto < 3; ++iopto) {
341  // N optorx module header word:
342  word1 = 0;
343  if (fedIdOptoRx_[fedId - FEDNumbering::MINPreShowerFEDID][iopto]) {
344  word2 = (3 << sDHEAD) | ((iopto + 4) << sDH) | (0x80 << sDOPTO);
345  int ich = 0;
346  for (ich = 0; ich < 4; ++ich) {
347  int chStatus = (optorx_ch_counts[iopto][ich + 8] > 0) ? 0xe : 0xd;
348  chStatus = (fedIdOptoRxFiber_[fedId - FEDNumbering::MINPreShowerFEDID][iopto][ich + 8]) ? chStatus : 0x00;
349  word2 |= (chStatus << (ich * 4)); //
350  }
351 
352  for (ich = 0; ich < 8; ++ich) {
353  int chStatus = (optorx_ch_counts[iopto][ich] > 0) ? 0xe : 0xd;
354  chStatus = (fedIdOptoRxFiber_[fedId - FEDNumbering::MINPreShowerFEDID][iopto][ich]) ? chStatus : 0x00;
355  word1 |= (chStatus << (ich * 4));
356  }
357  } else
358  word2 = (3 << sDHEAD) | ((iopto + 4) << sDH) | (0x00 << sDOPTO);
359 
360  word = (Word64(word2) << 32) | Word64(word1);
361  DCCwords.push_back(word);
362  }
363 
364  // Output (data size in Bytes)
365  // FEDRawData * rawData = new FEDRawData(dataSize);
366  fedRawData.resize(dataSize);
367 
368  Word64* w = reinterpret_cast<Word64*>(fedRawData.data());
369 
370  // header
371  FEDHeader::set(reinterpret_cast<unsigned char*>(w), trgtype_, meta_data.lv1, meta_data.bx, fedId);
372  w++;
373 
374  // ES-DCC
375  for (unsigned int i = 0; i < DCCwords.size(); ++i) {
376  if (debug_)
377  cout << "DCC : " << print(DCCwords[i]) << endl;
378  *w = DCCwords[i];
379  w++;
380  }
381 
382  // event data
383  for (unsigned int i = 0; i < words.size(); ++i) {
384  *w = words[i];
385  w++;
386  }
387 
388  // trailer
389  FEDTrailer::set(reinterpret_cast<unsigned char*>(w),
390  dataSize / sizeof(Word64),
391  evf::compute_crc(fedRawData.data(), dataSize),
392  0,
393  0);
394 }
static const int sOHEAD
int bundleId_[2][2][40][40]
T getUntrackedParameter(std::string const &, T const &) const
int fiberId_[2][2][40][40]
static const int sHEAD
static const int bE0
int strip() const
Definition: ESDetId.h:47
static const int bHEAD
static const int bDHEAD
static const int sSTRIP
static const int sPACE
static const int sDH
static const int bPACE
const double w
Definition: UKUtility.cc:23
uint16_t *__restrict__ id
static const int bKBC
static const int bKHEAD
static const int bKEC
const ESDetId & id() const
Definition: ESDataFrame.h:19
static const int bDVMAJOR
static const int bDRUNTYPE
bool fedIdOptoRxFiber_[56][3][12]
static const int sDVMINOR
static const int bFIBER
static const int sKEC
int six() const
Definition: ESDetId.h:43
static const int bOHEAD
static const int bDOPTO
static const int sKET
static const int bDORBIT
int size() const
Definition: ESDataFrame.h:21
static const int bADC2
std::map< int, DetDigis > Digis
static const int sDTRGTYPE
static const int sDERR
static const int bADC0
uint64_t word
static const int sKFLAG2
static const int sDRUN
int siy() const
Definition: ESDetId.h:45
ESDataFormatter::Word64 Word64
static const int sDRUNTYPE
static const int sOEMUTTCBC
static void set(unsigned char *trailer, uint32_t lenght, uint16_t crc, uint8_t evt_stat, uint8_t tts, bool moreTrailers=false)
Set all fields in the trailer.
Definition: FEDTrailer.cc:31
static const int bDCH
static const int bOEMUKEC
static const int sADC2
void resize(size_t newsize)
Definition: FEDRawData.cc:28
static const int bKID
const int trgtype_
unsigned short compute_crc(unsigned char *buffer, unsigned int bufSize)
Definition: CRC16.h:46
static const int bKHEAD1
edm::FileInPath lookup_
static const int bDVMINOR
static const int bE1
const ESSample & sample(int i) const
Definition: ESDataFrame.h:24
bool operator()(const pair< int, int > s1, const pair< int, int > s2) const
static const int bDH
static const int sKHEAD1
~ESDataFormatterV4() override
static const int sE0
static const int sOEMUKEC
static const int sDCH
int kchipId_[2][2][40][40]
int zside() const
Definition: ESDetId.h:39
bool fedIdOptoRx_[56][3]
static const int sDEL
static const int sKFLAG1
static const int sADC0
static const int sDOPTO
int fedId_[2][2][40][40]
static const int bKCE
static const int sKBC
static const int bKFLAG1
static const int bDTRGTYPE
static const int sKHEAD2
static const int sDORBIT
static const int bKHEAD2
static const int sADC1
static const int bDRUN
static const int sKCE
static const int sDVMAJOR
static const int bADC1
static const int sDCOMFLAG
static const int bKCRC
static const int sKID
static const int sDHEAD
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
static const int bSTRIP
static const int bKFLAG2
const bool debug_
int paceId_[2][2][40][40]
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
int plane() const
Definition: ESDetId.h:41
bool ltstrip(const ESDataFormatterV4::Word64 &s1, const ESDataFormatterV4::Word64 &s2)
std::string print(const Word64 &word) const
std::string fullPath() const
Definition: FileInPath.cc:161
static const int bOEMUTTCEC
tuple cout
Definition: gather_cfg.py:144
int adc() const
get the ADC sample (singed 16 bits)
Definition: ESSample.h:16
ESDataFormatterV4(const edm::ParameterSet &ps)
static const int sE1
static void set(unsigned char *header, uint8_t triggerType, uint32_t lvl1ID, uint16_t bxID, uint16_t sourceID, uint8_t version=0, bool moreHeaders=false)
Set all fields in the header.
Definition: FEDHeader.cc:25
static const int bDERR
static const int sKCRC
static const int bDEL
static const int bDCOMFLAG
static const int bKET
int optoId_[2][2][40][40]
static const int sFIBER
static const int bOEMUTTCBC
std::vector< ESDataFrame > DetDigis
void DigiToRaw(int fedId, Digis &digis, FEDRawData &fedRawData, Meta_Data const &meta_data) const override
static const int sKHEAD
static const int sOEMUTTCEC