CMS 3D CMS Logo

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  : ESDataFormatter(ps) {
102 
103  lookup_ = ps.getUntrackedParameter<FileInPath>("LookupTable");
104 
105  // initialize look-up table
106  for (int i=0; i<2; ++i)
107  for (int j=0; j<2; ++j)
108  for (int k=0 ;k<40; ++k)
109  for (int m=0; m<40; m++) {
110  fedId_[i][j][k][m] = -1;
111  kchipId_[i][j][k][m] = -1;
112  paceId_[i][j][k][m] = -1;
113  bundleId_[i][j][k][m] = -1;
114  fiberId_[i][j][k][m] = -1;
115  optoId_[i][j][k][m] = -1;
116  }
117 
118  for(int i=0;i<56;++i) {
119  for(int j=0;j<3;++j) fedIdOptoRx_[i][j] = false ;
120  }
121 
122  for(int i=0;i<56;++i) {
123  for(int j=0;j<3;++j)
124  for(int k=0;k<12;k++)
125  fedIdOptoRxFiber_[i][j][k] = false ;
126  }
127 
128  // read in look-up table
129  int nLines, iz, ip, ix, iy, fed, kchip, pace, bundle, fiber, optorx;
130  ifstream file;
131  file.open(lookup_.fullPath().c_str());
132  if( file.is_open() ) {
133 
134  file >> nLines;
135 
136  for (int i=0; i<nLines; ++i) {
137  int fedId = -1;
138  file >> iz >> ip >> ix >> iy >> fed >> kchip >> pace >> bundle >> fiber >> optorx;
139 
140  fedId = fedId_[(3-iz)/2-1][ip-1][ix-1][iy-1] = fed;
141  kchipId_[(3-iz)/2-1][ip-1][ix-1][iy-1] = kchip;
142  paceId_[(3-iz)/2-1][ip-1][ix-1][iy-1] = pace - 1;
143  bundleId_[(3-iz)/2-1][ip-1][ix-1][iy-1] = bundle;
144  fiberId_[(3-iz)/2-1][ip-1][ix-1][iy-1] = fiber;
145  optoId_[(3-iz)/2-1][ip-1][ix-1][iy-1] = optorx;
146 
147  if (fedId<FEDNumbering::MINPreShowerFEDID|| fedId>FEDNumbering::MAXPreShowerFEDID) {
148  if (debug_) cout << "ESDataFormatterV4::ESDataFormatterV4 : fedId value : " << fedId
149  << " out of ES range, at lookup table line : " << i << endl;
150  } else if (optorx < 1 || optorx > 3) {
151  if (debug_) 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_) cout << "ESDataFormatterV4::ESDataFormatterV4 : fiber value : " << fiber
160  << " out of ES range, at lookup table line : " << i << endl;
161  }
162  }
163 
164  }
165 
166  } else {
167  if (debug_) cout<<"ESDataFormatterV4::ESDataFormatterV4 : Look up table file can not be found in "<<lookup_.fullPath().c_str()<<endl;
168  }
169 
170  file.close();
171 
172 }
173 
175 }
176 
177 struct ltfiber
178 {
179  bool operator()(const pair<int,int> s1, const pair<int,int> s2) const
180  {
181  return (s1.second < s2.second);
182  }
183 };
184 
185 
187 {
188  ESDataFormatterV4::Word64 PACESTRIP_MASK = 0x00ff000000000000ull;
189  ESDataFormatterV4::Word64 PACESTRIP_OFFSET = 48ull;
190 
191  ESDataFormatterV4::Word64 val1 = (s1 & PACESTRIP_MASK) >> PACESTRIP_OFFSET ;
192  ESDataFormatterV4::Word64 val2 = (s2 & PACESTRIP_MASK) >> PACESTRIP_OFFSET ;
193 
194  return (val1 < val2);
195 }
196 
197 
198 
199 void ESDataFormatterV4::DigiToRaw(int fedId, Digis & digis, FEDRawData& fedRawData, Meta_Data const &meta_data) const {
200 
201  int ts[3] = {0, 0, 0};
202  Word32 word1, word2;
203  Word64 word;
204  int numberOfStrips = 0 ;
205 
206  int optorx_ch_counts[3][12];
207 
208  int kchip, pace, optorx, fiber ;
209  map<int, vector<Word64> > map_data;
210  vector<Word64> words;
211 
212  vector<Word32> testVector ;
213 
214  set<pair<int,int>, ltfiber> set_of_kchip_fiber_in_optorx[3];
215 
216  map_data.clear();
217 
218  // clean optorx channel status fields:
219  for(int i=0;i<3;++i)
220  for(int j=0;j<12;++j)
221  optorx_ch_counts[i][j] = 0 ;
222 
223  const DetDigis & detDigis = digis[fedId] ;
224 
225  if (debug_) {
226  cout << "ESDataFormatterV4::DigiToRaw : FEDID : " << fedId << " size of detDigis : "
227  << detDigis.size() << endl ;
228  }
229 
230  for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); ++it) {
231 
232  const ESDataFrame& dataframe = (*it);
233  const ESDetId& detId = dataframe.id();
234 
235  for (int is=0; is<dataframe.size(); ++is) ts[is] = dataframe.sample(is).adc();
236 
237  kchip = kchipId_[(3-detId.zside())/2-1][detId.plane()-1][detId.six()-1][detId.siy()-1];
238  pace = paceId_[(3-detId.zside())/2-1][detId.plane()-1][detId.six()-1][detId.siy()-1];
239 
240  if (debug_) cout <<"Si : "<<detId.zside()<<" "<<detId.plane()<<" "<<detId.six()<<" "<<detId.siy()
241  <<" "<<detId.strip()<<" ("<<kchip<<","<<pace<<") "<<ts[0]<<" "<<ts[1]<<" "<<ts[2]<<endl;
242 
243  // convert strip number from detector id to electronics id
244  int siz = detId.zside();
245  int sip = detId.plane();
246  int six = detId.six();
247  int siy = detId.siy();
248  int sistrip = detId.strip();
249  if (siz == 1 && sip == 1 && siy <= 20) sistrip = 33 - sistrip;
250  if (siz == 1 && sip == 2 && six > 20) sistrip = 33 - sistrip;
251  if (siz == -1 && sip == 1 && siy > 20) sistrip = 33 - sistrip;
252  if (siz == -1 && sip == 2 && six <= 20) sistrip = 33 - sistrip;
253 
254  word1 = (ts[1] << sADC1) | (ts[0] << sADC0);
255  word2 = (0xc << sHEAD) | (pace << sPACE) | ((sistrip-1) << sSTRIP) | (ts[2] << sADC2);
256  word = (Word64(word2) << 32 ) | Word64(word1);
257 
258  map_data[kchip].push_back(word);
259 
260  optorx = optoId_[(3-detId.zside())/2-1][detId.plane()-1][detId.six()-1][detId.siy()-1];
261  fiber = fiberId_[(3-detId.zside())/2-1][detId.plane()-1][detId.six()-1][detId.siy()-1];
262 
263  optorx_ch_counts[optorx-1][fiber-1]++; // increment number of strip hits on fiber status field ;
264 
265  set<pair<int,int>, ltfiber> & theSet = set_of_kchip_fiber_in_optorx[optorx-1];
266  theSet.insert(pair<int,int>(kchip,fiber));
267 
268  // mark global strip number in this FED
269  ++numberOfStrips;
270  }
271 
272  for(int iopto=0; iopto<3; ++iopto) {
273 
274  if (fedIdOptoRx_[fedId-FEDNumbering::MINPreShowerFEDID][iopto]) {
275 
276  word2 = (0x6 << sOHEAD) | (meta_data.kchip_ec << sOEMUKEC) | (meta_data.kchip_bc << sOEMUTTCBC) ;
277  word1 = (meta_data.kchip_ec << sOEMUTTCEC) ;
278  word = (Word64(word2) << 32 ) | Word64(word1);
279  if (debug_) cout<<"OPTORX: "<<print(word)<<endl;
280  words.push_back(word);
281 
282  set<pair<int,int>, ltfiber> & theSet = set_of_kchip_fiber_in_optorx[iopto];
283 
284  if (debug_) {
285  cout << "ESDataFormatterV4::DigiToRaw : FEDID : " << fedId << " size of set_of_kchip_fiber_in_optorx["
286  << iopto << "] : "
287  << theSet.size() << endl ;
288  }
289 
290  set<pair<int,int>, ltfiber>::const_iterator kit = theSet.begin();
291 
292  int ikchip = 0;
293 
294  while (kit != theSet.end()) {
295 
296  const pair<int,int>& kchip_fiber = (*kit);
297 
298  if (debug_) cout<<"KCHIP : "<<kchip_fiber.first << " FIBER: " << kchip_fiber.second << endl;
299 
300  if (fedIdOptoRxFiber_[fedId-FEDNumbering::MINPreShowerFEDID][iopto][kchip_fiber.second-1]) {
301 
302  // Set all PACEs enabled for MC
303  word1 = (0 << sKFLAG1) | (0xf << sKFLAG2) | (((kchip_fiber.first<<2) | 0x02) << sKID);
304  word2 = (0x9 << sKHEAD) | (meta_data.kchip_ec << sKEC) | (meta_data.kchip_bc << sKBC);
305 
306  word = (Word64(word2) << 32 ) | Word64(word1);
307  if (debug_) cout<<"KCHIP : "<<print(word)<<endl;
308 
309  words.push_back(word);
310 
311  vector<Word64> & data = map_data[kchip_fiber.first];
312 
313  // sort against stripid field, as hardware gives this order to strip data :
314  sort(data.begin(), data.end(), ltstrip);
315 
316  for (unsigned int id=0; id<data.size(); ++id) {
317  if (debug_) cout<<"Data : "<<print(data[id])<<endl;
318  words.push_back(data[id]);
319  }
320  }
321  ++kit ; ++ikchip;
322 
323  }
324  }
325 
326  }
327 
328  int dataSize = (words.size() + 8) * sizeof(Word64);
329 
330  vector<Word64> DCCwords;
331 
332  word2 = (3 << sDHEAD) | (1 <<sDH) | (meta_data.run_number << sDRUN);
333  word1 = (numberOfStrips << sDEL) | (0xff << sDERR) ;
334  word = (Word64(word2) << 32 ) | Word64(word1);
335  DCCwords.push_back(word);
336 
337  word2 = (3 << sDHEAD) | (2 <<sDH);
338  word1 = 0;
339  word = (Word64(word2) << 32 ) | Word64(word1);
340  DCCwords.push_back(word);
341 
342  word2 = (3 << sDHEAD) | (3 <<sDH) | (4 << sDVMAJOR) | (3 << sDVMINOR);
343  word1 = (meta_data.orbit_number << sDORBIT);
344  word = (Word64(word2) << 32 ) | Word64(word1);
345  DCCwords.push_back(word);
346 
347  for(int iopto=0; iopto < 3; ++iopto ) {
348  // N optorx module header word:
349  word1 = 0;
350  if (fedIdOptoRx_[fedId-FEDNumbering::MINPreShowerFEDID][iopto]) {
351  word2 = (3 << sDHEAD) | ((iopto+4) <<sDH) | (0x80 << sDOPTO) ;
352  int ich = 0;
353  for(ich=0;ich<4;++ich) {
354  int chStatus = (optorx_ch_counts[iopto][ich+8]>0) ? 0xe : 0xd ;
355  chStatus = (fedIdOptoRxFiber_[fedId-FEDNumbering::MINPreShowerFEDID][iopto][ich+8]) ? chStatus : 0x00 ;
356  word2 |= (chStatus << (ich*4)); //
357  }
358 
359  for(ich=0;ich<8;++ich) {
360  int chStatus = (optorx_ch_counts[iopto][ich]>0) ? 0xe : 0xd ;
361  chStatus = (fedIdOptoRxFiber_[fedId-FEDNumbering::MINPreShowerFEDID][iopto][ich]) ? chStatus : 0x00 ;
362  word1 |= (chStatus << (ich*4));
363  }
364  } else
365  word2 = (3 << sDHEAD) | ((iopto+4) <<sDH) | (0x00 << sDOPTO) ;
366 
367  word = (Word64(word2) << 32 ) | Word64(word1);
368  DCCwords.push_back(word);
369 
370  }
371 
372  // Output (data size in Bytes)
373  // FEDRawData * rawData = new FEDRawData(dataSize);
374  fedRawData.resize(dataSize);
375 
376  Word64 * w = reinterpret_cast<Word64* >(fedRawData.data());
377 
378  // header
379  FEDHeader::set( reinterpret_cast<unsigned char*>(w), trgtype_, meta_data.lv1, meta_data.bx, fedId);
380  w++;
381 
382  // ES-DCC
383  for (unsigned int i=0; i<DCCwords.size(); ++i) {
384  if (debug_) cout<<"DCC : "<<print(DCCwords[i])<<endl;
385  *w = DCCwords[i];
386  w++;
387  }
388 
389  // event data
390  for (unsigned int i=0; i<words.size(); ++i) {
391  *w = words[i];
392  w++;
393  }
394 
395  // trailer
396  FEDTrailer::set( reinterpret_cast<unsigned char*>(w), dataSize/sizeof(Word64),
397  evf::compute_crc(fedRawData.data(), dataSize),
398  0, 0);
399 
400 }
401 
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
static const int bKBC
static const int bKHEAD
static const int bKEC
const ESDetId & id() const
Definition: ESDataFrame.h:21
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:23
static const int bADC2
std::map< int, DetDigis > Digis
sistrip classes
static const int sDTRGTYPE
static const int sDERR
static const int bADC0
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:60
static const int bDCH
static const int bOEMUKEC
static const int sADC2
void resize(size_t newsize)
Definition: FEDRawData.cc:32
static const int bKID
const int trgtype_
void DigiToRaw(int fedId, Digis &digis, FEDRawData &fedRawData, Meta_Data const &meta_data) const override
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:26
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
int k[5][pyjets_maxn]
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
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
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:28
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:163
static const int bOEMUTTCEC
int adc() const
get the ADC sample (singed 16 bits)
Definition: ESSample.h:18
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:47
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
static const int sKHEAD
static const int sOEMUTTCEC