CMS 3D CMS Logo

TowerBlockFormatter.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <list>
3 
5 
9 
10 using namespace std;
11 
12 
14 
15 }
16 
17 
19  const EcalElectronicsMapping* TheMapping)
20 
21 {
22 
23  int bx = bx_;
24  int lv1 = lv1_ - 1;
25 
26 
27  int rdsize = rawdata.size() / 8; // size in Word64
28 
29  const EBDetId& ebdetid = dataframe.id();
30 
31  int DCCid = TheMapping -> DCCid(ebdetid);
32  int FEDid = FEDNumbering::MINECALFEDID + DCCid ;
33 
34 
35  int nsamples = dataframe.size();
36  // -- FE number
37  const EcalElectronicsId& elid = TheMapping -> getElectronicsId(ebdetid);
38  int iFE = elid.towerId();
39  if (iFE <= 0 || iFE > 68) throw cms::Exception("InvalidFEid") <<
40  "TowerBlockFormatter::DigiToRaw : Invalid iFE " << iFE << endl;
41 
42 
43  map<int, map<int,int> >::iterator fen = FEDorder.find(FEDid);
44  map<int, map<int,int> >::iterator fed = FEDmap.find(FEDid);
45 
46  if (fen == FEDorder.end()) {
47  if (debug_) cout << "New FED in TowerBlockFormatter " << dec << FEDid << " 0x" << hex << FEDid << endl;
48  map<int,int> FEorder;
49  pair<map<int, map<int,int> >::iterator, bool> t1 = FEDorder.insert(map<int, map<int,int> >::value_type(FEDid,FEorder));
50  map<int,int> FEmap;
51  pair<map<int, map<int,int> >::iterator, bool> t2 = FEDmap.insert(map<int, map<int,int> >::value_type(FEDid,FEmap));
52  fen = t1.first;
53  fed = t2.first;
54  }
55 
56  map<int, int>& FEorder = (*fen).second;
57  map<int, int>& FEmap = (*fed).second;
58 
59  map<int,int>::iterator fe = FEorder.find(iFE);
60  int FE_order;
61  int FE_index;
62  if (fe != FEorder.end()) {
63  FE_order = (*fe).second;
64  map<int,int>::iterator ff = FEmap.find(FE_order);
65  if (ff == FEmap.end()) cout << "Error with maps... " << endl;
66  FE_index = (*ff).second;
67  if (debug_) cout << "FE already there, FE_index = " << dec << FE_index << " FEorder " << FE_order << endl;
68  }
69  else {
70  if (debug_) cout << "New FE in TowerBlockFormatter FE " << dec << iFE << " 0x" << hex << iFE << " in FED id " << dec << FEDid << endl;
71  int inser = rdsize;
72  int number_FEs = FEorder.size() -1;
73  FE_order = number_FEs+1;
74  pair<map<int,int>::iterator, bool> t2 = FEorder.insert(map<int,int>::value_type(iFE,FE_order));
75  if (! t2.second) cout << " FE insertion failed...";
76  pair<map<int,int>::iterator, bool> tt = FEmap.insert(map<int,int>::value_type(FE_order,inser));
77  fe = tt.first;
78  FE_index = (*fe).second;
79  if (debug_) cout << "Build the Tower Block header for FE id " << iFE << " start at line " << rdsize << endl;
80  if (debug_) cout << "This is the Fe number (order) " << number_FEs+1 << endl;
81  rawdata.resize( 8*rdsize + 8);
82  unsigned char* pData = rawdata.data();
83  pData[8*FE_index] = iFE & 0xFF;
84  pData[8*FE_index+1] = (nsamples & 0x7F);
85  pData[8*FE_index+2] = bx & 0xFF;
86  pData[8*FE_index+3] = (bx >>8) & 0x0F;
87  pData[8*FE_index+3] |= 0xa0;
88  pData[8*FE_index+4] = lv1 & 0xFF;
89  pData[8*FE_index+5] = (lv1 >>8) & 0x0F;
90  pData[8*FE_index+6] = 1;
91  pData[8*FE_index+7] = 0xc0;
92  if (debug_) print(rawdata);
93 
94  }
95 
96 
97  // -- Crystal number inside the SM :
98 
99  int istrip = elid.stripId();
100  int ichannel = elid.xtalId();
101 
102  if (debug_) cout << "Now add crystal : strip channel " << dec << istrip << " " << ichannel << endl;
103 
104 
105  unsigned char* pData = rawdata.data();
106 
107  vector<unsigned char> vv(&pData[0],&pData[rawdata.size()]);
108 
109 
110  int n_add = 2 + 2*nsamples; // 2 bytes per sample, plus 2 bytes before sample 0
111  if (n_add % 8 != 0) n_add = n_add/8 +1;
112  else
113  n_add = n_add/8;
114  if (debug_) cout << "will add " << n_add << " lines of 64 bits at line " << (FE_index+1) << endl;
115  rawdata.resize( rawdata.size() + 8*n_add );
116  unsigned char* ppData = rawdata.data();
117 
118  vector<unsigned char>::iterator iter = vv.begin() + 8*(FE_index+1);
119 
120  vector<unsigned char> toadd(n_add*8);
121 
122 
123  int tzs=0;
124  toadd[0] = (istrip & 0x7) + ((ichannel & 0x7)<<4);
125  toadd[1] = (tzs & 0x1) <<12;
126 
127  for (int isample=0; isample < (n_add*8-2)/2; isample++) {
128  if (isample < nsamples) {
129  uint16_t word = (dataframe.sample(isample)).raw(); // 16 bits word corresponding to this sample
130  toadd[2 + isample*2] = word & 0x00FF;
131  toadd[2 + isample*2 +1] = (word & 0xFF00)>>8;
132  }
133  else {
134  toadd[2 + isample*2] = 0;
135  toadd[2 + isample*2 +1] = 0;
136  }
137  if (isample % 2 == 0) toadd[2 + isample*2 +1] |= 0xc0; // need to add the B11 header...
138  }
139 
140  vv.insert(iter,toadd.begin(),toadd.end());
141 
142 
143  // update the pData for this FED :
144  for (int i=0; i < (int)vv.size(); i++) {
145  ppData[i] = vv[i];
146  }
147 
148  if (debug_) {
149  cout << "pData for this FED is now " << endl;
150  print(rawdata);
151  }
152 
153  // and update the FEmap for this FED :
154  for (int i=FE_order+1; i < (int)FEorder.size(); i++) {
155  FEmap[i] += n_add;
156  if (debug_) cout << "FEmap updated for fe number " << dec << i << endl;
157  if (debug_) cout << " FEmap[" << i << "] = " << FEmap[i] << endl;
158  }
159 
160  // update the block length
161  int blocklength = ppData[8*FE_index+6]
162  + ((ppData[8*FE_index+7] & 0x1)<<8);
163  blocklength += n_add;
164  ppData[8*FE_index+6] = blocklength & 0xFF;
165  ppData[8*FE_index+7] |= (blocklength & 0x100)>>8;
166 
167 
168 
169 }
170 
171 
172 
174 
175 // -- Need to reorder the FE's in teh TowerBlock. They come in the right
176 // order when reading the unsuppressed digis, but ganz durcheinander
177 // when reading the SelectiveReadout_Suppressed digis.
178 
179  if (debug_) cout << "enter in TowerBlockFormatter::EndEvent. First reorder the FE's. " << endl;
180 
181  for (int idcc=1; idcc <= 54; idcc++) {
182 
183  // debug_ = (idcc == 34);
184 
185  //if (idcc != 34) continue;
186 
187  int FEDid = FEDNumbering::MINECALFEDID + idcc;
188  // cout << "Process FED " << FEDid << endl;
189  FEDRawData& fedData = productRawData -> FEDData(FEDid);
190  if (fedData.size() <= 16) continue;
191 
192  if (debug_) cout << "This is FEDid = " << FEDid << endl;
193 
194  unsigned char * pData = fedData.data();
195  // Word64* words = reinterpret_cast<Word64*>(const_cast<unsigned char*>(pData));
196  Word64* words = reinterpret_cast<Word64*>(pData);
197 
198  int length = fedData.size() / 8;
199  int iDAQ_header(-1), iDCC_header(-1), iTCCBlock_header(-1),
200  iSRBlock_header(-1), iTowerBlock_header(-1), iDAQ_trailer(-1);
201 
202  for (int i=length-1; i > -1; i--) {
203  if ( ( (words[i] >> 60) & 0xF) == 0x5 ) iDAQ_header=i;
204  if ( ( (words[i] >> 62) & 0x3) == 0x0 ) iDCC_header=i;
205  if ( ( (words[i] >> 61) & 0x7) == 0x3 ) iTCCBlock_header=i;
206  if ( ( (words[i] >> 61) & 0x7) == 0x4 ) iSRBlock_header=i;
207  if ( ( (words[i] >> 62) & 0x3) == 0x3 ) iTowerBlock_header=i;
208  if ( ( (words[i] >> 60) & 0xF) == 0xA ) iDAQ_trailer=i;
209  }
210 
211  if (iTowerBlock_header < 0) iTowerBlock_header = iDAQ_trailer;
212  if (iSRBlock_header < 0) iSRBlock_header = iTowerBlock_header;
213  if (iTCCBlock_header < 0) iTCCBlock_header = iSRBlock_header;
214 
215  if (debug_) {
216  cout << "iDAQ_header = " << iDAQ_header << endl;
217  cout << " iDCC_header = " << iDCC_header << endl;
218  cout << " iTCCBlock_header = " << iTCCBlock_header << endl;
219  cout << " iSRBlock_header = " << iSRBlock_header << endl;
220  cout << " iTowerBlock_header = " << iTowerBlock_header << endl;
221  cout << " iDAQ_trailer = " << iDAQ_trailer << endl;
222  }
223 
224  std::map<int, int> FrontEnd;
225  std::map<int, std::vector<Word64> > Map_xtal_data;
226 
227  int iTowerBlock_header_keep = iTowerBlock_header;
228 
229  while (iTowerBlock_header < iDAQ_trailer) {
230  int fe = words[iTowerBlock_header] & 0xFF;
231  int nlines = (words[iTowerBlock_header] >> 48) & 0x1FF;
232  if (debug_) cout << "This is FE number " << fe << "needs nlines = " << nlines << endl;
233  FrontEnd[fe] = nlines;
234  std::vector<Word64> xtal_data;
235  for (int j=0; j < nlines; j++) {
236  Word64 ww = words[iTowerBlock_header+j];
237  xtal_data.push_back(ww);
238  }
239  Map_xtal_data[fe] = xtal_data;
240  iTowerBlock_header += nlines;
241  }
242 
243  if (debug_) {
244  cout << "vector of FrontEnd : " << FrontEnd.size() << endl;
245  for (std::map<int, int>::const_iterator it=FrontEnd.begin();
246  it != FrontEnd.end(); it++) {
247  int fe = it -> first;
248  int l = it -> second;
249  cout << "FE line " << fe << " " << l << endl;
250  }
251  }
252 
253  iTowerBlock_header = iTowerBlock_header_keep;
254  for (std::map<int, int>::const_iterator it=FrontEnd.begin();
255  it != FrontEnd.end(); it++) {
256  int fe = it -> first;
257  int nlines = it -> second;
258  if (debug_) cout << "iTowerBlock_header = " << iTowerBlock_header << endl;
259  vector<Word64> xtal_data = Map_xtal_data[fe];
260  for (int j=0; j < nlines; j++) {
261  words[iTowerBlock_header+j] = xtal_data[j];
262  if (debug_) cout << "update line " << iTowerBlock_header+j << endl;
263  }
264  if (debug_) {
265  int jFE = pData[8*(iTowerBlock_header)];
266  cout << "Front End on RD : " << jFE << endl;
267  }
268  iTowerBlock_header += nlines;
269  }
270 
271  // -- now the FEs are ordered. STill need to order the xtals within FEs;
272  // need : xtal 1,2,3,4, 5 in strip 1, xtal 1,2,3,4,5 in strip 2 etc..
273  // with possibly missing ones.
274 
275  if (debug_) cout << "now reorder the xtals within the FEs" << endl;
276 
277  iTowerBlock_header = iTowerBlock_header_keep;
278 
279  for (std::map<int, int>::const_iterator it=FrontEnd.begin();
280  it != FrontEnd.end(); it++) {
281 
282  int fe = it -> first;
283  if (fe > 68) cout << "Problem... fe = " << fe << " in FEDid = " << FEDid << endl;
284  if (debug_) cout << " This is for FE = " << fe << endl;
285  int nlines = it -> second;
286  int timesamples = pData[8*iTowerBlock_header+1] & 0x7F;
287  int n4=timesamples-3;
288  int n_lines4 = n4/4;
289  if ( n4 % 4 != 0) n_lines4 ++;
290  if (n_lines4<0) n_lines4=0;
291  int Nxtal_max = (nlines-1)/(1+n_lines4);
292  int Nxtal = 0;
293 
294  map< int, map<int, vector<Word64> > > Strip_Map;
295 
296  while (Nxtal < Nxtal_max) {
297 
298  int i_xtal = iTowerBlock_header+1 + Nxtal*(1+n_lines4);
299  int strip = words[i_xtal] & 0x7;
300  int xtal = ( words[i_xtal] >>4) & 0x7;
301 
302  map< int, map<int, vector<Word64> > >::iterator iit = Strip_Map.find(strip);
303 
304  map<int, vector<Word64> > NewMap;
305  map<int, vector<Word64> > Xtal_Map;
306 
307  if (iit == Strip_Map.end()) { // new strip
308  Xtal_Map = NewMap;
309  }
310  else {
311  Xtal_Map = iit -> second;
312  }
313 
314  std::vector<Word64> xtal_data;
315  for (int j=0; j < n_lines4 +1; j++) {
316  Word64 ww = words[i_xtal +j];
317  xtal_data.push_back(ww);
318  }
319  Xtal_Map[xtal] = xtal_data;
320  Strip_Map[strip] = Xtal_Map;
321 
322  Nxtal ++;
323  }
324 
325  // now, update the xtals for this FE :
326 
327  int idx = 0;
328  for (map< int, map<int, vector<Word64> > >::const_iterator jt = Strip_Map.begin();
329  jt != Strip_Map.end(); jt++) {
330 
331  int strip = jt -> first;
332  if (debug_) cout << " this is strip number " << strip << endl;
333  map<int, vector<Word64> > Xtal_Map = jt -> second;
334 
335  for (map<int, vector<Word64> >::const_iterator kt = Xtal_Map.begin();
336  kt != Xtal_Map.end(); kt++) {
337  int xtal = kt -> first;
338  if (debug_) cout << " this is xtal number " << xtal << endl;
339  vector<Word64> xtal_data = kt -> second;
340 
341  int mlines = (int)xtal_data.size();
342  if (debug_) cout << " mlines = " << mlines << endl;
343  for (int j=0; j < mlines; j++) {
344  int line = iTowerBlock_header+1+idx+j;
345  if (line >= iDAQ_trailer) cout << "smth wrong... line " << line << " trailer " << iDAQ_trailer << endl;
346  words[line] = xtal_data[j] ;
347  if (debug_) cout << " updated line " << iTowerBlock_header+idx+j << endl;
348  }
349  idx += mlines;
350 
351  } // end loop on xtals
352  Xtal_Map.clear();
353 
354  } // end loop on strips
355 
356  Strip_Map.clear();
357 
358  iTowerBlock_header += nlines;
359  } // end loop on FEs
360 
361 
362  if (debug_) cout << " DONE FOR FED " << FEDid << endl;
363  FrontEnd.clear();
364  Map_xtal_data.clear();
365 
366  } // end loop on DCC
367 
368  // cout << " finished reorder, now clean up " << endl;
369 
370 // -- clean up
371 
372  // FEDmap.empty();
373  // FEDorder.empty();
374 
375  // cout << "end of EndEvent " << endl;
376 }
377 
378 
379 
380 
382 
383  // -- now that we have the EcalElectronicsMapping, this method could probably be
384  // merged with DigiToRaw(EBdataframe).
385  // Keep as it is for the while...
386 {
387 
388  // debug_ = false;
389 
390  int bx = bx_;
391  int lv1 = lv1_;
392 
393 
394  int rdsize = rawdata.size() / 8; // size in Word64
395 
396  const EEDetId& eedetid = dataframe.id();
397  EcalElectronicsId elid = TheMapping -> getElectronicsId(eedetid);
398  int DCCid = elid.dccId();
399  int FEDid = FEDNumbering::MINECALFEDID + DCCid ;
400  int iFE = elid.towerId();
401 
402  if (debug_) cout << "enter in TowerBlockFormatter::DigiToRaw DCCid FEDid iFE " <<
403  dec << DCCid << " " << FEDid << " " << iFE << endl;
404 
405  int nsamples = dataframe.size();
406 
407  if (iFE <= 0 || iFE > 68) {
408  cout << "invalid iFE for EndCap DCCid iFE " << DCCid << " " << iFE << endl;
409  return;
410  }
411 
412 
413  map<int, map<int,int> >::iterator fen = FEDorder.find(FEDid);
414  map<int, map<int,int> >::iterator fed = FEDmap.find(FEDid);
415 
416  if (fen == FEDorder.end()) {
417  if (debug_) cout << "New FED in TowerBlockFormatter " << dec << FEDid << " 0x" << hex << FEDid << endl;
418  map<int,int> FEorder;
419  pair<map<int, map<int,int> >::iterator, bool> t1 = FEDorder.insert(map<int, map<int,int> >::value_type(FEDid,FEorder));
420  map<int,int> FEmap;
421  pair<map<int, map<int,int> >::iterator, bool> t2 = FEDmap.insert(map<int, map<int,int> >::value_type(FEDid,FEmap));
422  fen = t1.first;
423  fed = t2.first;
424  }
425 
426  map<int, int>& FEorder = (*fen).second;
427  map<int, int>& FEmap = (*fed).second;
428 
429  map<int,int>::iterator fe = FEorder.find(iFE);
430  int FE_order;
431  int FE_index;
432  if (fe != FEorder.end()) {
433  FE_order = (*fe).second;
434  map<int,int>::iterator ff = FEmap.find(FE_order);
435  if (ff == FEmap.end()) cout << "Error with maps... " << endl;
436  FE_index = (*ff).second;
437  if (debug_) cout << "FE already there, FE_index = " << dec << FE_index << " FEorder " << FE_order << endl;
438  }
439  else {
440  if (debug_) cout << "New FE in TowerBlockFormatter FE " << dec << iFE << " 0x" << hex << iFE << " in FED id " << dec << FEDid << endl;
441  int inser = rdsize;
442  int number_FEs = FEorder.size() -1;
443  FE_order = number_FEs+1;
444  pair<map<int,int>::iterator, bool> t2 = FEorder.insert(map<int,int>::value_type(iFE,FE_order));
445  if (! t2.second) cout << " FE insertion failed...";
446  pair<map<int,int>::iterator, bool> tt = FEmap.insert(map<int,int>::value_type(FE_order,inser));
447  fe = tt.first;
448  FE_index = (*fe).second;
449  if (debug_) cout << "Build the Tower Block header for FE id " << iFE << " start at line " << rdsize << endl;
450  if (debug_) cout << "This is the Fe number (order) " << number_FEs+1 << endl;
451  rawdata.resize( 8*rdsize + 8);
452  unsigned char* pData = rawdata.data();
453 
454  pData[8*FE_index] = iFE & 0xFF;
455  pData[8*FE_index+1] = (nsamples & 0x7F);
456  pData[8*FE_index+2] = bx & 0xFF;
457  pData[8*FE_index+3] = (bx >>8) & 0x0F;
458  pData[8*FE_index+3] |= 0xa0;
459  pData[8*FE_index+4] = lv1 & 0xFF;
460  pData[8*FE_index+5] = (lv1 >>8) & 0x0F;
461  pData[8*FE_index+6] = 1;
462  pData[8*FE_index+7] = 0xc0;
463  if (debug_) print(rawdata);
464 
465  }
466 
467 
468 
469  // -- Crystal number inside the SM :
470  int istrip = elid.stripId();
471  int ichannel = elid.xtalId();
472 
473  if (debug_) cout << "Now add crystal strip channel " << dec << istrip << " " << ichannel << endl;
474 
475  unsigned char* pData = rawdata.data();
476 
477  vector<unsigned char> vv(&pData[0],&pData[rawdata.size()]);
478 
479 
480  int n_add = 2 + 2*nsamples; // 2 bytes per sample, plus 2 bytes before sample 0
481  if (n_add % 8 != 0) n_add = n_add/8 +1;
482  else
483  n_add = n_add/8;
484  if (debug_) cout << "nsamples = " << dec << nsamples << endl;
485  if (debug_) cout << "will add " << n_add << " lines of 64 bits at line " << (FE_index+1) << endl;
486  rawdata.resize( rawdata.size() + 8*n_add );
487  unsigned char* ppData = rawdata.data();
488 
489  vector<unsigned char>::iterator iter = vv.begin() + 8*(FE_index+1);
490 
491  vector<unsigned char> toadd(n_add*8);
492 
493 
494  int tzs=0;
495  toadd[0] = (istrip & 0x7) + ((ichannel & 0x7)<<4);
496  toadd[1] = (tzs & 0x1) <<12;
497 
498  for (int isample=0; isample < (n_add*8-2)/2; isample++) {
499  if (isample < nsamples) {
500  uint16_t word = (dataframe.sample(isample)).raw(); // 16 bits word corresponding to this sample
501  toadd[2 + isample*2] = word & 0x00FF;
502  toadd[2 + isample*2 +1] = (word & 0xFF00)>>8;
503  }
504  else {
505  toadd[2 + isample*2] = 0;
506  toadd[2 + isample*2 +1] = 0;
507  }
508  if (isample % 2 == 0) toadd[2 + isample*2 +1] |= 0xc0; // need to add the B11 header...
509  }
510 
511  vv.insert(iter,toadd.begin(),toadd.end());
512 
513 
514  // update the pData for this FED :
515  for (int i=0; i < (int)vv.size(); i++) {
516  ppData[i] = vv[i];
517  }
518 
519  if (debug_) {
520  cout << "pData for this FED is now " << endl;
521  print(rawdata);
522  }
523 
524  // and update the FEmap for this FED :
525  for (int i=FE_order+1; i < (int)FEorder.size(); i++) {
526  FEmap[i] += n_add;
527  if (debug_) cout << "FEmap updated for fe number " << dec << i << endl;
528  if (debug_) cout << " FEmap[" << i << "] = " << FEmap[i] << endl;
529  }
530 
531  // update the block length
532  int blocklength = ppData[8*FE_index+6]
533  + ((ppData[8*FE_index+7] & 0x1)<<8);
534  blocklength += n_add;
535  ppData[8*FE_index+6] = blocklength & 0xFF;
536  ppData[8*FE_index+7] |= (blocklength & 0x100)>>8;
537 
538 
539 }
540 
541 
542 
543 
544 
545 
546 
547 
548 
549 
key_type id() const
Definition: EBDataFrame.h:31
int xtalId() const
get the channel id
int stripId() const
get the tower id
Ecal readout channel identification [32:20] Unused (so far) [19:13] DCC id [12:6] tower [5:3] strip [...
EcalMGPASample sample(int i) const
Definition: EcalDataFrame.h:29
int towerId() const
get the tower id
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
int size() const
Definition: EcalDataFrame.h:26
void print(FEDRawData &rawdata)
U second(std::pair< T, U > const &p)
void resize(size_t newsize)
Definition: FEDRawData.cc:32
const bool debug_
int dccId() const
get the DCC (Ecal Local DCC value not global one) id
key_type id() const
Definition: EEDataFrame.h:28
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
Definition: Config.py:1
TowerBlockFormatter(BlockFormatter::Config const &, BlockFormatter::Params const &)
void EndEvent(FEDRawDataCollection *productRawData)
void DigiToRaw(const EBDataFrame &dataframe, FEDRawData &rawdata, const EcalElectronicsMapping *TheMapping)
std::map< int, std::map< int, int > > FEDorder
std::map< int, std::map< int, int > > FEDmap