00001 #include "EventFilter/ESDigiToRaw/interface/ESDataFormatter.h"
00002 #include "EventFilter/FEDInterface/interface/FEDHeader.h"
00003 #include "EventFilter/FEDInterface/interface/FEDTrailer.h"
00004 #include "FWCore/Utilities/interface/CRC16.h"
00005
00006 const int ESDataFormatter::bDHEAD = 2;
00007 const int ESDataFormatter::bDH = 6;
00008 const int ESDataFormatter::bDEL = 24;
00009 const int ESDataFormatter::bDERR = 8;
00010 const int ESDataFormatter::bDRUN = 24;
00011 const int ESDataFormatter::bDRUNTYPE = 32;
00012 const int ESDataFormatter::bDTRGTYPE = 16;
00013 const int ESDataFormatter::bDCOMFLAG = 8;
00014 const int ESDataFormatter::bDORBIT = 32;
00015 const int ESDataFormatter::bDVMAJOR = 8;
00016 const int ESDataFormatter::bDVMINOR = 8;
00017 const int ESDataFormatter::bDCH = 4;
00018 const int ESDataFormatter::bDOPTO = 8;
00019
00020 const int ESDataFormatter::sDHEAD = 26;
00021 const int ESDataFormatter::sDH = 24;
00022 const int ESDataFormatter::sDEL = 0;
00023 const int ESDataFormatter::sDERR = bDEL + sDEL;
00024 const int ESDataFormatter::sDRUN = 0;
00025 const int ESDataFormatter::sDRUNTYPE = 0;
00026 const int ESDataFormatter::sDTRGTYPE = 0;
00027 const int ESDataFormatter::sDCOMFLAG = bDTRGTYPE + sDTRGTYPE;
00028 const int ESDataFormatter::sDORBIT = 0;
00029 const int ESDataFormatter::sDVMINOR = 8;
00030 const int ESDataFormatter::sDVMAJOR = bDVMINOR + sDVMINOR;
00031 const int ESDataFormatter::sDCH = 0;
00032 const int ESDataFormatter::sDOPTO = 16;
00033
00034 const int ESDataFormatter::bKEC = 8;
00035 const int ESDataFormatter::bKFLAG2 = 8;
00036 const int ESDataFormatter::bKBC = 12;
00037 const int ESDataFormatter::bKFLAG1 = 4;
00038 const int ESDataFormatter::bKET = 1;
00039 const int ESDataFormatter::bKCRC = 1;
00040 const int ESDataFormatter::bKCE = 1;
00041 const int ESDataFormatter::bKID = 11;
00042 const int ESDataFormatter::bFIBER = 6;
00043 const int ESDataFormatter::bKHEAD1 = 2;
00044 const int ESDataFormatter::bKHEAD2 = 2;
00045
00046 const int ESDataFormatter::sKEC = 0;
00047 const int ESDataFormatter::sKFLAG2 = bKEC + sKEC;
00048 const int ESDataFormatter::sKBC = bKFLAG2 + sKFLAG2;
00049 const int ESDataFormatter::sKFLAG1 = bKBC + sKBC;
00050 const int ESDataFormatter::sKET = 0;
00051 const int ESDataFormatter::sKCRC = bKET + sKET;
00052 const int ESDataFormatter::sKCE = bKCRC + sKCRC;
00053 const int ESDataFormatter::sKID = bKCE + sKCE + 5;
00054 const int ESDataFormatter::sFIBER = bKID + sKID + 1;
00055 const int ESDataFormatter::sKHEAD1 = bFIBER + sFIBER + 2;
00056 const int ESDataFormatter::sKHEAD2 = bKHEAD1 + sKHEAD1;
00057
00058 const int ESDataFormatter::bADC0 = 16;
00059 const int ESDataFormatter::bADC1 = 16;
00060 const int ESDataFormatter::bADC2 = 16;
00061 const int ESDataFormatter::bPACE = 2;
00062 const int ESDataFormatter::bSTRIP = 5;
00063 const int ESDataFormatter::bE0 = 1;
00064 const int ESDataFormatter::bE1 = 1;
00065 const int ESDataFormatter::bHEAD = 2;
00066
00067 const int ESDataFormatter::sADC0 = 0;
00068 const int ESDataFormatter::sADC1 = bADC0 + sADC0;
00069 const int ESDataFormatter::sADC2 = 0;
00070 const int ESDataFormatter::sPACE = bADC2 + sADC2;
00071 const int ESDataFormatter::sSTRIP = bPACE + sPACE;
00072 const int ESDataFormatter::sE0 = bSTRIP + sSTRIP + 1;
00073 const int ESDataFormatter::sE1 = bE0 + sE0;
00074 const int ESDataFormatter::sHEAD = bE1 + sE1 + 4;
00075
00076 ESDataFormatter::ESDataFormatter(const ParameterSet& ps)
00077 : pset_(ps), run_number_(0), orbit_number_(0), bx_(0), lv1_(0), trgtype_(0)
00078 {
00079
00080 debug_ = pset_.getUntrackedParameter<bool>("debugMode", false);
00081
00082 }
00083
00084 ESDataFormatter::~ESDataFormatter() {
00085 }
00086
00087 FEDRawData * ESDataFormatter::DigiToRaw(int fedId, const Digis & digis) {
00088
00089 map<int, vector<Word64> > map_data;
00090 map_data.clear();
00091
00092 int ts[3] = {0, 0, 0};
00093 Word32 word1, word2;
00094 Word64 word;
00095 vector<Word64> words;
00096
00097 for (Digis::const_iterator itr = digis.begin(); itr != digis.end(); ++itr) {
00098
00099 if (itr->first != fedId) continue;
00100 const DetDigis & detDigis = itr->second;
00101
00102 for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); ++it) {
00103
00104 const ESDataFrame& dataframe = (*it);
00105 const ESDetId& detId = dataframe.id();
00106
00107 for (int is=0; is<dataframe.size(); ++is) ts[is] = dataframe.sample(is).adc();
00108
00109
00110 int kchip = -1;
00111 int pace = -1;
00112 int ix = -1;
00113 int iy = -1;
00114
00115 ix = detId.six() % 2;
00116 iy = detId.siy() % 2;
00117 if (ix == 1 && iy == 1)
00118 pace = 0;
00119 else if (ix == 0 && iy == 1)
00120 pace = 1;
00121 else if (ix == 1 && iy == 0)
00122 pace = 2;
00123 else if (ix == 0 && iy == 0)
00124 pace = 3;
00125
00126 ix = (1 + detId.six()) / 2;
00127 iy = (1 + detId.siy()) / 2;
00128 if (detId.zside() == 1 && detId.plane() == 1)
00129 kchip = ix + (iy-1)*20 - 1;
00130 else if (detId.zside() == 1 && detId.plane() == 2)
00131 kchip = ix + (iy-1)*20 + 399;
00132 else if (detId.zside() == -1 && detId.plane() == 1)
00133 kchip = ix + (iy-1)*20 + 799;
00134 else if (detId.zside() == -1 && detId.plane() == 2)
00135 kchip = ix + (iy-1)*20 + 1199;
00136
00137 if (debug_) cout<<"Si : "<<detId.zside()<<" "<<detId.plane()<<" "<<detId.six()<<" "<<detId.siy()<<" "<<detId.strip()<<" ("<<kchip<<","<<pace<<") "<<ts[2]<<" "<<ts[1]<<" "<<ts[0]<<endl;
00138
00139 word1 = (ts[1] << sADC1) | (ts[0] << sADC0);
00140 word2 = (2 << sHEAD) | (0 << sE1) | (0 << sE0) | ((detId.strip()-1) << sSTRIP) | (pace << sPACE) | (ts[2] << sADC2);
00141 word = (Word64(word2) << 32 ) | Word64(word1);
00142
00143 map_data[kchip].push_back(word);
00144 }
00145 }
00146
00147 map<int, vector<Word64> >::const_iterator kit;
00148 for (kit=map_data.begin(); kit!=map_data.end(); ++kit) {
00149
00150 if (debug_) cout<<"KCHIP : "<<kit->first<<endl;
00151
00152 word1 = (0 << sKFLAG1) | (0 << sKBC) | (0 << sKFLAG2) | (0 << sKEC);
00153 word2 = (1 << sKHEAD2) | (0 << sKHEAD1) | (0 << sFIBER) | (kit->first << sKID) | (0 << sKCE) | (0 << sKCRC) | (0 << sKET);
00154 word = (Word64(word2) << 32 ) | Word64(word1);
00155
00156 if (debug_) cout<<"KCHIP : "<<print(word)<<endl;
00157 words.push_back(word);
00158
00159 const vector<Word64> & data = kit->second;
00160 for (unsigned int id=0; id<data.size(); ++id) {
00161 if (debug_) cout<<"Data : "<<print(data[id])<<endl;
00162 words.push_back(data[id]);
00163 }
00164
00165 }
00166
00167 int dataSize = (words.size() + 8) * sizeof(Word64);
00168
00169
00170 vector<Word64> DCCwords;
00171 word2 = (0 << sDHEAD) | (1 <<sDH) | (run_number_ << sDRUN);
00172 word1 = (dataSize << sDEL);
00173 word = (Word64(word2) << 32 ) | Word64(word1);
00174 DCCwords.push_back(word);
00175 word2 = (0 << sDHEAD) | (2 <<sDH);
00176 word1 = 0;
00177 word = (Word64(word2) << 32 ) | Word64(word1);
00178 DCCwords.push_back(word);
00179 word2 = (0 << sDHEAD) | (3 <<sDH) | (1 << sDVMAJOR) | (1 << sDVMINOR);
00180 word1 = (orbit_number_ << sDORBIT);
00181 word = (Word64(word2) << 32 ) | Word64(word1);
00182 DCCwords.push_back(word);
00183 word2 = (0 << sDHEAD) | (4 <<sDH);
00184 word1 = 0;
00185 word = (Word64(word2) << 32 ) | Word64(word1);
00186 DCCwords.push_back(word);
00187 word2 = (0 << sDHEAD) | (5 <<sDH);
00188 word1 = 0;
00189 word = (Word64(word2) << 32 ) | Word64(word1);
00190 DCCwords.push_back(word);
00191 word2 = (0 << sDHEAD) | (6 <<sDH);
00192 word1 = 0;
00193 word = (Word64(word2) << 32 ) | Word64(word1);
00194 DCCwords.push_back(word);
00195
00196
00197 FEDRawData * rawData = new FEDRawData(dataSize);
00198
00199 Word64 * w = reinterpret_cast<Word64* >(rawData->data());
00200
00201
00202 FEDHeader::set( reinterpret_cast<unsigned char*>(w), trgtype_, lv1_, bx_, fedId);
00203 w++;
00204
00205
00206 for (unsigned int i=0; i<DCCwords.size(); ++i) {
00207 if (debug_) cout<<"DCC : "<<print(DCCwords[i])<<endl;
00208 *w = DCCwords[i];
00209 w++;
00210 }
00211
00212
00213 for (unsigned int i=0; i<words.size(); ++i) {
00214 *w = words[i];
00215 w++;
00216 }
00217
00218
00219 FEDTrailer::set( reinterpret_cast<unsigned char*>(w), dataSize/sizeof(Word64),
00220 evf::compute_crc(rawData->data(), dataSize),
00221 0, 0);
00222 w++;
00223
00224
00225
00226 return rawData;
00227
00228 }
00229
00230 FEDRawData * ESDataFormatter::DigiToRawTB(int fedId, const Digis & digis) {
00231
00232 Word8 word8;
00233 Word16 word16;
00234 Word32 word1, word2;
00235 Word64 word;
00236 vector<Word64> words;
00237
00238
00239 int data[8][4][32][3];
00240
00241 for (int i=0; i<8; ++i)
00242 for (int j=0; j<4; ++j)
00243 for (int k=0; k<32;++k)
00244 for (int l=0; l<3; ++l)
00245 data[i][j][k][l] = 1000;
00246
00247 int kchip = 0;
00248 int pace = 0;
00249 for (Digis::const_iterator itr = digis.begin(); itr != digis.end(); ++itr) {
00250
00251 if (itr->first != fedId) continue;
00252 const DetDigis & detDigis = itr->second;
00253
00254 for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); ++it) {
00255
00256 const ESDataFrame& dataframe = (*it);
00257 const ESDetId& detId = dataframe.id();
00258
00259 for (int is=0; is<dataframe.size(); ++is) {
00260
00261 int ix = detId.six()-30;
00262 int iy = detId.siy()-19;
00263
00264 if (ix<2 && iy<2) kchip = 1;
00265 else if (ix<2 && iy>=2) kchip = 2;
00266 else if (ix>=2 && iy>=2) kchip = 3;
00267 else if (ix>=2 && iy<2) kchip = 4;
00268
00269 if ((ix%2)==0 && (iy%2)==0) pace = 1;
00270 else if ((ix%2)==0 && (iy%2)==1) pace = 2;
00271 else if ((ix%2)==1 && (iy%2)==1) pace = 3;
00272 else if ((ix%2)==1 && (iy%2)==0) pace = 4;
00273
00274 data[(detId.plane()-1)*4+(kchip-1)][pace-1][detId.strip()-1][is] = dataframe.sample(is).adc();
00275 cout<<"Digi : "<<detId.plane()<<" "<<detId.six()<<" "<<detId.siy()<<" "<<detId.strip()<<" "<<dataframe.sample(is).adc()<<endl;
00276 }
00277 }
00278 }
00279
00280 map<int, vector<Word16> > map_data;
00281 map_data.clear();
00282
00283 for (int i=0; i<8; ++i) {
00284
00285 for (int wl=0; wl<4; ++wl) {
00286 if (wl == 0) {
00287 word16 = (0 << 0);
00288 } else if (wl == 1) {
00289 word16 = (0 << 0);
00290 } else if (wl == 2) {
00291 word16 = (0 << 0);
00292 } else if (wl == 3) {
00293 word16 = ((i+1) << 0);
00294 }
00295 map_data[i].push_back(word16);
00296 if (debug_) cout<<i<<" "<<wl<<" "<<print(word16)<<endl;
00297 }
00298
00299 for (int j=0; j<3; ++j) {
00300 for (int wl=0; wl<34; ++wl) {
00301
00302 if (wl == 0) {
00303 word16 = (1 << 8) | 2;
00304 map_data[i].push_back(word16);
00305 if (debug_) cout<<i<<" "<<wl<<" "<<print(word16)<<endl;
00306 } else if (wl == 1) {
00307 word16 = (3 << 8) | 4;
00308 map_data[i].push_back(word16);
00309 if (debug_) cout<<i<<" "<<wl<<" "<<print(word16)<<endl;
00310 } else {
00311 word16 = (data[i][0][wl-2][j] << 4) | (data[i][1][wl-2][j] >> 8);
00312 map_data[i].push_back(word16);
00313 if (debug_) cout<<i<<" "<<wl<<" "<<print(word16)<<endl;
00314 word16 = (data[i][1][wl-2][j] << 8) | (data[i][2][wl-2][j] >> 4);
00315 map_data[i].push_back(word16);
00316 if (debug_) cout<<i<<" "<<wl<<" "<<print(word16)<<endl;
00317 word16 = (data[i][2][wl-2][j] << 12) | (data[i][3][wl-2][j] >> 0);
00318 map_data[i].push_back(word16);
00319 if (debug_) cout<<i<<" "<<wl<<" "<<print(word16)<<endl;
00320 }
00321 }
00322 }
00323
00324 word16 = 0;
00325 map_data[i].push_back(word16);
00326 }
00327
00328
00329 vector<Word16> kdata;
00330 for (int i=0; i<299; ++i) {
00331
00332
00333 word = 0;
00334 kdata = map_data[0];
00335 word = ((Word64(kdata[i]) & 0xff) << 0) | word;
00336 kdata = map_data[1];
00337 word = ((Word64(kdata[i]) & 0xff) << 8) | word;
00338 kdata = map_data[2];
00339 word = ((Word64(kdata[i]) & 0xff) << 16) | word;
00340 kdata = map_data[3];
00341 word = ((Word64(kdata[i]) & 0xff) << 24) | word;
00342 kdata = map_data[4];
00343 word = ((Word64(kdata[i]) & 0xff) << 32) | word;
00344 kdata = map_data[5];
00345 word = ((Word64(kdata[i]) & 0xff) << 40) | word;
00346 words.push_back(word);
00347
00348 word = 0;
00349 kdata = map_data[6];
00350 word = ((Word64(kdata[i]) & 0xff) << 0) | word;
00351 kdata = map_data[7];
00352 word = ((Word64(kdata[i]) & 0xff) << 8) | word;
00353 words.push_back(word);
00354
00355
00356 word = 0;
00357 kdata = map_data[0];
00358 word = ((Word64(kdata[i]) & 0xff00) >> 8) | word;
00359 kdata = map_data[1];
00360 word = ((Word64(kdata[i]) & 0xff00) >> 0) | word;
00361 kdata = map_data[2];
00362 word = ((Word64(kdata[i]) & 0xff00) << 8) | word;
00363 kdata = map_data[3];
00364 word = ((Word64(kdata[i]) & 0xff00) << 16) | word;
00365 kdata = map_data[4];
00366 word = ((Word64(kdata[i]) & 0xff00) << 24) | word;
00367 kdata = map_data[5];
00368 word = ((Word64(kdata[i]) & 0xff00) << 32) | word;
00369 words.push_back(word);
00370
00371 word = 0;
00372 kdata = map_data[6];
00373 word = ((Word64(kdata[i]) & 0xff00) >> 8) | word;
00374 kdata = map_data[7];
00375 word = ((Word64(kdata[i]) & 0xff00) >> 0) | word;
00376 words.push_back(word);
00377 }
00378
00379 int dataSize = (words.size() + 8) * sizeof(Word64);
00380
00381
00382 vector<Word64> DCCwords;
00383 word2 = (0 << sDHEAD) | (1 <<sDH) | (run_number_ << sDRUN);
00384 word1 = (dataSize << sDEL);
00385 word = (Word64(word2) << 32 ) | Word64(word1);
00386 DCCwords.push_back(word);
00387 word2 = (0 << sDHEAD) | (2 <<sDH);
00388 word1 = 0;
00389 word = (Word64(word2) << 32 ) | Word64(word1);
00390 DCCwords.push_back(word);
00391 word2 = (0 << sDHEAD) | (3 <<sDH) | (1 << sDVMAJOR) | (1 << sDVMINOR);
00392 word1 = (orbit_number_ << sDORBIT);
00393 word = (Word64(word2) << 32 ) | Word64(word1);
00394 DCCwords.push_back(word);
00395 word2 = (0 << sDHEAD) | (4 <<sDH);
00396 word1 = 0;
00397 word = (Word64(word2) << 32 ) | Word64(word1);
00398 DCCwords.push_back(word);
00399 word2 = (0 << sDHEAD) | (5 <<sDH);
00400 word1 = 0;
00401 word = (Word64(word2) << 32 ) | Word64(word1);
00402 DCCwords.push_back(word);
00403 word2 = (0 << sDHEAD) | (6 <<sDH);
00404 word1 = 0;
00405 word = (Word64(word2) << 32 ) | Word64(word1);
00406 DCCwords.push_back(word);
00407
00408
00409 FEDRawData * rawData = new FEDRawData(dataSize);
00410
00411 Word64 * w = reinterpret_cast<Word64* >(rawData->data());
00412
00413
00414 FEDHeader::set( reinterpret_cast<unsigned char*>(w), trgtype_, lv1_, bx_, fedId);
00415 w++;
00416
00417
00418 for (unsigned int i=0; i<DCCwords.size(); ++i) {
00419 if (debug_) cout<<"DCC : "<<print(DCCwords[i])<<endl;
00420 *w = DCCwords[i];
00421 w++;
00422 }
00423
00424
00425 for (unsigned int i=0; i<words.size(); ++i) {
00426 *w = words[i];
00427 if (debug_) cout<<"Event data : "<<i<<" "<<print(words[i])<<endl;
00428 w++;
00429 }
00430
00431
00432 FEDTrailer::set( reinterpret_cast<unsigned char*>(w), dataSize/sizeof(Word64),
00433 evf::compute_crc(rawData->data(), dataSize),
00434 0, 0);
00435 w++;
00436
00437 return rawData;
00438 }
00439
00440 string ESDataFormatter::print(const Word64 & word) const
00441 {
00442 ostringstream str;
00443 str << "Word64: " << reinterpret_cast<const bitset<64>&> (word);
00444 return str.str();
00445 }
00446
00447 string ESDataFormatter::print(const Word16 & word) const
00448 {
00449 ostringstream str;
00450 str << "Word16: " << reinterpret_cast<const bitset<16>&> (word);
00451 return str.str();
00452 }