CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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  : 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 
186 struct ltstrip : public binary_function<ESDataFormatterV4::Word64&, ESDataFormatterV4::Word64&,bool> {
188  {
189  ESDataFormatterV4::Word64 PACESTRIP_MASK = 0x00ff000000000000ull;
190  ESDataFormatterV4::Word64 PACESTRIP_OFFSET = 48ull;
191 
192  ESDataFormatterV4::Word64 val1 = (s1 & PACESTRIP_MASK) >> PACESTRIP_OFFSET ;
193  ESDataFormatterV4::Word64 val2 = (s2 & PACESTRIP_MASK) >> PACESTRIP_OFFSET ;
194 
195  return (val1 < val2);
196  }
197 };
198 
199 
200 
201 void ESDataFormatterV4::DigiToRaw(int fedId, Digis & digis, FEDRawData& fedRawData) {
202 
203  int ts[3] = {0, 0, 0};
204  Word32 word1, word2;
205  Word64 word;
206  int numberOfStrips = 0 ;
207 
208  int optorx_ch_counts[3][12];
209 
210  int kchip, pace, optorx, fiber ;
211  map<int, vector<Word64> > map_data;
212  vector<Word64> words;
213 
214  vector<Word32> testVector ;
215 
216  set<pair<int,int>, ltfiber> set_of_kchip_fiber_in_optorx[3];
217 
218  map_data.clear();
219 
220  // clean optorx channel status fields:
221  for(int i=0;i<3;++i)
222  for(int j=0;j<12;++j)
223  optorx_ch_counts[i][j] = 0 ;
224 
225  const DetDigis & detDigis = digis[fedId] ;
226 
227  if (debug_) {
228  cout << "ESDataFormatterV4::DigiToRaw : FEDID : " << fedId << " size of detDigis : "
229  << detDigis.size() << endl ;
230  }
231 
232  for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); ++it) {
233 
234  const ESDataFrame& dataframe = (*it);
235  const ESDetId& detId = dataframe.id();
236 
237  for (int is=0; is<dataframe.size(); ++is) ts[is] = dataframe.sample(is).adc();
238 
239  kchip = kchipId_[(3-detId.zside())/2-1][detId.plane()-1][detId.six()-1][detId.siy()-1];
240  pace = paceId_[(3-detId.zside())/2-1][detId.plane()-1][detId.six()-1][detId.siy()-1];
241 
242  if (debug_) cout <<"Si : "<<detId.zside()<<" "<<detId.plane()<<" "<<detId.six()<<" "<<detId.siy()
243  <<" "<<detId.strip()<<" ("<<kchip<<","<<pace<<") "<<ts[0]<<" "<<ts[1]<<" "<<ts[2]<<endl;
244 
245  // convert strip number from detector id to electronics id
246  int siz = detId.zside();
247  int sip = detId.plane();
248  int six = detId.six();
249  int siy = detId.siy();
250  int sistrip = detId.strip();
251  if (siz == 1 && sip == 1 && siy <= 20) sistrip = 33 - sistrip;
252  if (siz == 1 && sip == 2 && six > 20) sistrip = 33 - sistrip;
253  if (siz == -1 && sip == 1 && siy > 20) sistrip = 33 - sistrip;
254  if (siz == -1 && sip == 2 && six <= 20) sistrip = 33 - sistrip;
255 
256  word1 = (ts[1] << sADC1) | (ts[0] << sADC0);
257  word2 = (0xc << sHEAD) | (pace << sPACE) | ((sistrip-1) << sSTRIP) | (ts[2] << sADC2);
258  word = (Word64(word2) << 32 ) | Word64(word1);
259 
260  map_data[kchip].push_back(word);
261 
262  optorx = optoId_[(3-detId.zside())/2-1][detId.plane()-1][detId.six()-1][detId.siy()-1];
263  fiber = fiberId_[(3-detId.zside())/2-1][detId.plane()-1][detId.six()-1][detId.siy()-1];
264 
265  optorx_ch_counts[optorx-1][fiber-1]++; // increment number of strip hits on fiber status field ;
266 
267  set<pair<int,int>, ltfiber> & theSet = set_of_kchip_fiber_in_optorx[optorx-1];
268  theSet.insert(pair<int,int>(kchip,fiber));
269 
270  // mark global strip number in this FED
271  ++numberOfStrips;
272  }
273 
274  for(int iopto=0; iopto<3; ++iopto) {
275 
276  if (fedIdOptoRx_[fedId-FEDNumbering::MINPreShowerFEDID][iopto]) {
277 
278  word2 = (0x6 << sOHEAD) | (kchip_ec_ << sOEMUKEC) | (kchip_bc_ << sOEMUTTCBC) ;
279  word1 = (kchip_ec_ << sOEMUTTCEC) ;
280  word = (Word64(word2) << 32 ) | Word64(word1);
281  if (debug_) cout<<"OPTORX: "<<print(word)<<endl;
282  words.push_back(word);
283 
284  set<pair<int,int>, ltfiber> & theSet = set_of_kchip_fiber_in_optorx[iopto];
285 
286  if (debug_) {
287  cout << "ESDataFormatterV4::DigiToRaw : FEDID : " << fedId << " size of set_of_kchip_fiber_in_optorx["
288  << iopto << "] : "
289  << theSet.size() << endl ;
290  }
291 
292  set<pair<int,int>, ltfiber>::const_iterator kit = theSet.begin();
293 
294  int ikchip = 0;
295 
296  while (kit != theSet.end()) {
297 
298  const pair<int,int>& kchip_fiber = (*kit);
299 
300  if (debug_) cout<<"KCHIP : "<<kchip_fiber.first << " FIBER: " << kchip_fiber.second << endl;
301 
302  if (fedIdOptoRxFiber_[fedId-FEDNumbering::MINPreShowerFEDID][iopto][kchip_fiber.second-1]) {
303 
304  // Set all PACEs enabled for MC
305  word1 = (0 << sKFLAG1) | (0xf << sKFLAG2) | (((kchip_fiber.first<<2) | 0x02) << sKID);
306  word2 = (0x9 << sKHEAD) | (kchip_ec_ << sKEC) | (kchip_bc_ << sKBC);
307 
308  word = (Word64(word2) << 32 ) | Word64(word1);
309  if (debug_) cout<<"KCHIP : "<<print(word)<<endl;
310 
311  words.push_back(word);
312 
313  vector<Word64> & data = map_data[kchip_fiber.first];
314 
315  // sort against stripid field, as hardware gives this order to strip data :
316  sort(data.begin(), data.end(), ltstrip());
317 
318  for (unsigned int id=0; id<data.size(); ++id) {
319  if (debug_) cout<<"Data : "<<print(data[id])<<endl;
320  words.push_back(data[id]);
321  }
322  }
323  ++kit ; ++ikchip;
324 
325  }
326  }
327 
328  }
329 
330  int dataSize = (words.size() + 8) * sizeof(Word64);
331 
332  vector<Word64> DCCwords;
333 
334  word2 = (3 << sDHEAD) | (1 <<sDH) | (run_number_ << sDRUN);
335  word1 = (numberOfStrips << sDEL) | (0xff << sDERR) ;
336  word = (Word64(word2) << 32 ) | Word64(word1);
337  DCCwords.push_back(word);
338 
339  word2 = (3 << sDHEAD) | (2 <<sDH);
340  word1 = 0;
341  word = (Word64(word2) << 32 ) | Word64(word1);
342  DCCwords.push_back(word);
343 
344  word2 = (3 << sDHEAD) | (3 <<sDH) | (4 << sDVMAJOR) | (3 << sDVMINOR);
345  word1 = (orbit_number_ << sDORBIT);
346  word = (Word64(word2) << 32 ) | Word64(word1);
347  DCCwords.push_back(word);
348 
349  for(int iopto=0; iopto < 3; ++iopto ) {
350  // N optorx module header word:
351  word1 = 0;
352  if (fedIdOptoRx_[fedId-FEDNumbering::MINPreShowerFEDID][iopto]) {
353  word2 = (3 << sDHEAD) | ((iopto+4) <<sDH) | (0x80 << sDOPTO) ;
354  int ich = 0;
355  for(ich=0;ich<4;++ich) {
356  int chStatus = (optorx_ch_counts[iopto][ich+8]>0) ? 0xe : 0xd ;
357  chStatus = (fedIdOptoRxFiber_[fedId-FEDNumbering::MINPreShowerFEDID][iopto][ich+8]) ? chStatus : 0x00 ;
358  word2 |= (chStatus << (ich*4)); //
359  }
360 
361  for(ich=0;ich<8;++ich) {
362  int chStatus = (optorx_ch_counts[iopto][ich]>0) ? 0xe : 0xd ;
363  chStatus = (fedIdOptoRxFiber_[fedId-FEDNumbering::MINPreShowerFEDID][iopto][ich]) ? chStatus : 0x00 ;
364  word1 |= (chStatus << (ich*4));
365  }
366  } else
367  word2 = (3 << sDHEAD) | ((iopto+4) <<sDH) | (0x00 << sDOPTO) ;
368 
369  word = (Word64(word2) << 32 ) | Word64(word1);
370  DCCwords.push_back(word);
371 
372  }
373 
374  // Output (data size in Bytes)
375  // FEDRawData * rawData = new FEDRawData(dataSize);
376  fedRawData.resize(dataSize);
377 
378  Word64 * w = reinterpret_cast<Word64* >(fedRawData.data());
379 
380  // header
381  FEDHeader::set( reinterpret_cast<unsigned char*>(w), trgtype_, lv1_, bx_, fedId);
382  w++;
383 
384  // ES-DCC
385  for (unsigned int i=0; i<DCCwords.size(); ++i) {
386  if (debug_) cout<<"DCC : "<<print(DCCwords[i])<<endl;
387  *w = DCCwords[i];
388  w++;
389  }
390 
391  // event data
392  for (unsigned int i=0; i<words.size(); ++i) {
393  *w = words[i];
394  w++;
395  }
396 
397  // trailer
398  FEDTrailer::set( reinterpret_cast<unsigned char*>(w), dataSize/sizeof(Word64),
399  evf::compute_crc(fedRawData.data(), dataSize),
400  0, 0);
401 
402 }
403 
static const int sOHEAD
int bundleId_[2][2][40][40]
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
int fiberId_[2][2][40][40]
static const int sHEAD
static const int bE0
int strip() const
Definition: ESDetId.h:52
static const int bHEAD
static const int bDHEAD
static const int sSTRIP
void DigiToRaw(int fedId, Digis &digis, FEDRawData &fedRawData)
static const int sPACE
static const int sDH
static const int bPACE
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:48
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
static void set(unsigned char *trailer, int evt_lgth, int crc, int evt_stat, int tts, bool T=false)
Set all fields in the trailer.
Definition: FEDTrailer.cc:42
std::map< int, DetDigis > Digis
tuple s2
Definition: indexGen.py:106
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:50
ESDataFormatter::Word64 Word64
static const int sDRUNTYPE
static const int sOEMUTTCBC
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
static void set(unsigned char *header, int evt_ty, int lvl1_ID, int bx_ID, int source_ID, int version=0, bool H=false)
Set all fields in the header.
Definition: FEDHeader.cc:40
unsigned short compute_crc(unsigned char *buffer, unsigned int bufSize)
Definition: CRC16.h:67
static const int bKHEAD1
edm::FileInPath lookup_
static const int bDVMINOR
bool operator()(const ESDataFormatterV4::Word64 &s1, const ESDataFormatterV4::Word64 &s2)
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
int j
Definition: DBlmapReader.cc:9
static const int sKHEAD1
static const int sE0
static const int sOEMUKEC
static const int sDCH
int kchipId_[2][2][40][40]
int zside() const
Definition: ESDetId.h:44
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
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
static const int bSTRIP
static const int bKFLAG2
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:46
std::string print(const Word64 &word) const
static const int bOEMUTTCEC
tuple cout
Definition: gather_cfg.py:121
int adc() const
get the ADC sample (singed 16 bits)
Definition: ESSample.h:18
ESDataFormatterV4(const edm::ParameterSet &ps)
static const int sE1
T w() const
std::string fullPath() const
Definition: FileInPath.cc:165
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