Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:39

0001 
0002 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0003 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
0004 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
0005 #include "DataFormats/EcalDetId/interface/EcalDetIdCollections.h"
0006 #include "FWCore/Utilities/interface/CRC16.h"
0007 
0008 #include "EventFilter/ESDigiToRaw/src/ESDataFormatterV1_1.h"
0009 
0010 const int ESDataFormatterV1_1::bDHEAD = 2;
0011 const int ESDataFormatterV1_1::bDH = 6;
0012 const int ESDataFormatterV1_1::bDEL = 24;
0013 const int ESDataFormatterV1_1::bDERR = 8;
0014 const int ESDataFormatterV1_1::bDRUN = 24;
0015 const int ESDataFormatterV1_1::bDRUNTYPE = 32;
0016 const int ESDataFormatterV1_1::bDTRGTYPE = 16;
0017 const int ESDataFormatterV1_1::bDCOMFLAG = 8;
0018 const int ESDataFormatterV1_1::bDORBIT = 32;
0019 const int ESDataFormatterV1_1::bDVMAJOR = 8;
0020 const int ESDataFormatterV1_1::bDVMINOR = 8;
0021 const int ESDataFormatterV1_1::bDCH = 4;
0022 const int ESDataFormatterV1_1::bDOPTO = 8;
0023 
0024 const int ESDataFormatterV1_1::sDHEAD = 26;
0025 const int ESDataFormatterV1_1::sDH = 24;
0026 const int ESDataFormatterV1_1::sDEL = 0;
0027 const int ESDataFormatterV1_1::sDERR = bDEL + sDEL;
0028 const int ESDataFormatterV1_1::sDRUN = 0;
0029 const int ESDataFormatterV1_1::sDRUNTYPE = 0;
0030 const int ESDataFormatterV1_1::sDTRGTYPE = 0;
0031 const int ESDataFormatterV1_1::sDCOMFLAG = bDTRGTYPE + sDTRGTYPE;
0032 const int ESDataFormatterV1_1::sDORBIT = 0;
0033 const int ESDataFormatterV1_1::sDVMINOR = 8;
0034 const int ESDataFormatterV1_1::sDVMAJOR = bDVMINOR + sDVMINOR;
0035 const int ESDataFormatterV1_1::sDCH = 0;
0036 const int ESDataFormatterV1_1::sDOPTO = 16;
0037 
0038 const int ESDataFormatterV1_1::bKEC = 8;  // KCHIP packet event counter
0039 const int ESDataFormatterV1_1::bKFLAG2 = 8;
0040 const int ESDataFormatterV1_1::bKBC = 12;  // KCHIP packet bunch counter
0041 const int ESDataFormatterV1_1::bKFLAG1 = 4;
0042 const int ESDataFormatterV1_1::bKET = 1;
0043 const int ESDataFormatterV1_1::bKCRC = 1;
0044 const int ESDataFormatterV1_1::bKCE = 1;
0045 const int ESDataFormatterV1_1::bKID = 11;
0046 const int ESDataFormatterV1_1::bFIBER = 6;  // Fiber number
0047 const int ESDataFormatterV1_1::bKHEAD1 = 2;
0048 const int ESDataFormatterV1_1::bKHEAD2 = 2;
0049 
0050 const int ESDataFormatterV1_1::sKEC = 0;
0051 const int ESDataFormatterV1_1::sKFLAG2 = bKEC + sKEC;
0052 const int ESDataFormatterV1_1::sKBC = bKFLAG2 + sKFLAG2;
0053 const int ESDataFormatterV1_1::sKFLAG1 = bKBC + sKBC;
0054 const int ESDataFormatterV1_1::sKET = 0;
0055 const int ESDataFormatterV1_1::sKCRC = bKET + sKET;
0056 const int ESDataFormatterV1_1::sKCE = bKCRC + sKCRC;
0057 const int ESDataFormatterV1_1::sKID = bKCE + sKCE + 5;
0058 const int ESDataFormatterV1_1::sFIBER = bKID + sKID + 1;
0059 const int ESDataFormatterV1_1::sKHEAD1 = bFIBER + sFIBER + 2;
0060 const int ESDataFormatterV1_1::sKHEAD2 = bKHEAD1 + sKHEAD1;
0061 
0062 const int ESDataFormatterV1_1::bADC0 = 16;
0063 const int ESDataFormatterV1_1::bADC1 = 16;
0064 const int ESDataFormatterV1_1::bADC2 = 16;
0065 const int ESDataFormatterV1_1::bPACE = 2;
0066 const int ESDataFormatterV1_1::bSTRIP = 5;
0067 const int ESDataFormatterV1_1::bE0 = 1;
0068 const int ESDataFormatterV1_1::bE1 = 1;
0069 const int ESDataFormatterV1_1::bHEAD = 2;
0070 
0071 const int ESDataFormatterV1_1::sADC0 = 0;
0072 const int ESDataFormatterV1_1::sADC1 = bADC0 + sADC0;
0073 const int ESDataFormatterV1_1::sADC2 = 0;
0074 const int ESDataFormatterV1_1::sPACE = bADC2 + sADC2;
0075 const int ESDataFormatterV1_1::sSTRIP = bPACE + sPACE;
0076 const int ESDataFormatterV1_1::sE0 = bSTRIP + sSTRIP + 1;
0077 const int ESDataFormatterV1_1::sE1 = bE0 + sE0;
0078 const int ESDataFormatterV1_1::sHEAD = bE1 + sE1 + 4;
0079 
0080 using namespace std;
0081 using namespace edm;
0082 
0083 ESDataFormatterV1_1::ESDataFormatterV1_1(const ParameterSet& ps) : ESDataFormatter(ps) {}
0084 
0085 ESDataFormatterV1_1::~ESDataFormatterV1_1() {}
0086 
0087 // FEDRawData * ESDataFormatterV1_1::DigiToRawDCC(int fedId, const Digis & digis) {
0088 
0089 //   int ts[3] = {0, 0, 0};
0090 //   Word32 word1, word2;
0091 //   Word64 word;
0092 
0093 //   for (Digis::const_iterator itr = digis.begin(); itr != digis.end(); ++itr) {
0094 
0095 //     if (itr->first != fedId) continue;
0096 //     const DetDigis & detDigis = itr->second;
0097 
0098 //     for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); ++it) {
0099 
0100 //       const ESDataFrame& dataframe = (*it);
0101 // //       const ESDetId& detId = dataframe.id();
0102 
0103 //       for (int is=0; is<dataframe.size(); ++is) ts[is] = dataframe.sample(is).adc();
0104 //     }
0105 //   }
0106 
0107 //   // DCC words
0108 //   vector<Word64> DCCwords;
0109 //   word2 = (run_number_ << 0) ;
0110 //   word1 = 0;
0111 //   word =  (Word64(word2) << 32 ) | Word64(word1);
0112 
0113 //   DCCwords.push_back(word);
0114 //   for (int i=0; i<5; ++i) {
0115 //     word = 0;
0116 //     DCCwords.push_back(word);
0117 //   }
0118 
0119 //   FEDRawData * rawData = new FEDRawData(0);
0120 
0121 //   return rawData;
0122 // }
0123 
0124 void ESDataFormatterV1_1::DigiToRaw(int fedId, Digis& digis, FEDRawData& fedRawData, const Meta_Data& meta_data) const {
0125   map<int, vector<Word64> > map_data;
0126   map_data.clear();
0127 
0128   int ts[3] = {0, 0, 0};
0129   Word32 word1, word2;
0130   Word64 word;
0131   vector<Word64> words;
0132 
0133   const DetDigis& detDigis = digis[fedId];
0134 
0135   //     if (detDigis==digis.end()) {
0136   //       cout << "ESDataFormatterV1_1::DigiToRaw : could not find digi vector in digis map for fedID: "
0137   //       << fedId << endl ;
0138   //       return 0;
0139   //     }
0140 
0141   for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); ++it) {
0142     const ESDataFrame& dataframe = (*it);
0143     const ESDetId& detId = dataframe.id();
0144 
0145     for (int is = 0; is < dataframe.size(); ++is)
0146       ts[is] = dataframe.sample(is).adc();
0147 
0148     //  calculate fake kchip and pace id
0149     int kchip = -1;
0150     int pace = -1;
0151     int ix = -1;
0152     int iy = -1;
0153 
0154     ix = detId.six() % 2;
0155     iy = detId.siy() % 2;
0156     if (ix == 1 && iy == 1)
0157       pace = 0;
0158     else if (ix == 0 && iy == 1)
0159       pace = 1;
0160     else if (ix == 1 && iy == 0)
0161       pace = 2;
0162     else if (ix == 0 && iy == 0)
0163       pace = 3;
0164 
0165     ix = (1 + detId.six()) / 2;
0166     iy = (1 + detId.siy()) / 2;
0167     if (detId.zside() == 1 && detId.plane() == 1)
0168       kchip = ix + (iy - 1) * 20 - 1;
0169     else if (detId.zside() == 1 && detId.plane() == 2)
0170       kchip = ix + (iy - 1) * 20 + 399;
0171     else if (detId.zside() == -1 && detId.plane() == 1)
0172       kchip = ix + (iy - 1) * 20 + 799;
0173     else if (detId.zside() == -1 && detId.plane() == 2)
0174       kchip = ix + (iy - 1) * 20 + 1199;
0175 
0176     if (debug_)
0177       cout << "Si : " << detId.zside() << " " << detId.plane() << " " << detId.six() << " " << detId.siy() << " "
0178            << detId.strip() << " (" << kchip << "," << pace << ") " << ts[2] << " " << ts[1] << " " << ts[0] << endl;
0179 
0180     word1 = (ts[1] << sADC1) | (ts[0] << sADC0);
0181     word2 =
0182         (2 << sHEAD) | (0 << sE1) | (0 << sE0) | ((detId.strip() - 1) << sSTRIP) | (pace << sPACE) | (ts[2] << sADC2);
0183     word = (Word64(word2) << 32) | Word64(word1);
0184 
0185     map_data[kchip].push_back(word);
0186   }
0187 
0188   map<int, vector<Word64> >::const_iterator kit;
0189   for (kit = map_data.begin(); kit != map_data.end(); ++kit) {
0190     if (debug_)
0191       cout << "KCHIP : " << kit->first << endl;
0192 
0193     word1 = (0 << sKFLAG1) | (0 << sKBC) | (0 << sKFLAG2) | (0 << sKEC);
0194     word2 = (1 << sKHEAD2) | (0 << sKHEAD1) | (0 << sFIBER) | (kit->first << sKID) | (0 << sKCE) | (0 << sKCRC) |
0195             (0 << sKET);
0196     word = (Word64(word2) << 32) | Word64(word1);
0197 
0198     if (debug_)
0199       cout << "KCHIP : " << print(word) << endl;
0200     words.push_back(word);
0201 
0202     const vector<Word64>& data = kit->second;
0203     for (unsigned int id = 0; id < data.size(); ++id) {
0204       if (debug_)
0205         cout << "Data  : " << print(data[id]) << endl;
0206       words.push_back(data[id]);
0207     }
0208   }
0209 
0210   int dataSize = (words.size() + 8) * sizeof(Word64);
0211 
0212   // DCC words
0213   vector<Word64> DCCwords;
0214   word2 = (0 << sDHEAD) | (1 << sDH) | (meta_data.run_number << sDRUN);
0215   word1 = (dataSize << sDEL);
0216   word = (Word64(word2) << 32) | Word64(word1);
0217   DCCwords.push_back(word);
0218   word2 = (0 << sDHEAD) | (2 << sDH);
0219   word1 = 0;
0220   word = (Word64(word2) << 32) | Word64(word1);
0221   DCCwords.push_back(word);
0222   word2 = (0 << sDHEAD) | (3 << sDH) | (1 << sDVMAJOR) | (1 << sDVMINOR);
0223   word1 = (meta_data.orbit_number << sDORBIT);
0224   word = (Word64(word2) << 32) | Word64(word1);
0225   DCCwords.push_back(word);
0226   word2 = (0 << sDHEAD) | (4 << sDH);
0227   word1 = 0;
0228   word = (Word64(word2) << 32) | Word64(word1);
0229   DCCwords.push_back(word);
0230   word2 = (0 << sDHEAD) | (5 << sDH);
0231   word1 = 0;
0232   word = (Word64(word2) << 32) | Word64(word1);
0233   DCCwords.push_back(word);
0234   word2 = (0 << sDHEAD) | (6 << sDH);
0235   word1 = 0;
0236   word = (Word64(word2) << 32) | Word64(word1);
0237   DCCwords.push_back(word);
0238 
0239   // Output (data size in Bytes)
0240   fedRawData.resize(dataSize);
0241 
0242   Word64* w = reinterpret_cast<Word64*>(fedRawData.data());
0243 
0244   // header
0245   FEDHeader::set(reinterpret_cast<unsigned char*>(w), trgtype_, meta_data.lv1, meta_data.bx, fedId);
0246   w++;
0247 
0248   // ES-DCC
0249   for (unsigned int i = 0; i < DCCwords.size(); ++i) {
0250     if (debug_)
0251       cout << "DCC  : " << print(DCCwords[i]) << endl;
0252     *w = DCCwords[i];
0253     w++;
0254   }
0255 
0256   // event data
0257   for (unsigned int i = 0; i < words.size(); ++i) {
0258     *w = words[i];
0259     w++;
0260   }
0261 
0262   // trailer
0263   FEDTrailer::set(reinterpret_cast<unsigned char*>(w),
0264                   dataSize / sizeof(Word64),
0265                   evf::compute_crc(fedRawData.data(), dataSize),
0266                   0,
0267                   0);
0268   w++;
0269 }