CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/EventFilter/ESDigiToRaw/src/ESDataFormatterV1_1.cc

Go to the documentation of this file.
00001 
00002 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00003 #include "DataFormats/EcalDetId/interface/EcalDetIdCollections.h"
00004 #include "EventFilter/FEDInterface/interface/FEDHeader.h"
00005 #include "EventFilter/FEDInterface/interface/FEDTrailer.h"
00006 #include "FWCore/Utilities/interface/CRC16.h"
00007 
00008 #include "EventFilter/ESDigiToRaw/src/ESDataFormatterV1_1.h"
00009 
00010 const int ESDataFormatterV1_1::bDHEAD    = 2;
00011 const int ESDataFormatterV1_1::bDH       = 6;
00012 const int ESDataFormatterV1_1::bDEL      = 24;
00013 const int ESDataFormatterV1_1::bDERR     = 8;
00014 const int ESDataFormatterV1_1::bDRUN     = 24;
00015 const int ESDataFormatterV1_1::bDRUNTYPE = 32;
00016 const int ESDataFormatterV1_1::bDTRGTYPE = 16;
00017 const int ESDataFormatterV1_1::bDCOMFLAG = 8;
00018 const int ESDataFormatterV1_1::bDORBIT   = 32;
00019 const int ESDataFormatterV1_1::bDVMAJOR  = 8;
00020 const int ESDataFormatterV1_1::bDVMINOR  = 8;
00021 const int ESDataFormatterV1_1::bDCH      = 4; 
00022 const int ESDataFormatterV1_1::bDOPTO    = 8;
00023 
00024 const int ESDataFormatterV1_1::sDHEAD    = 26;
00025 const int ESDataFormatterV1_1::sDH       = 24;
00026 const int ESDataFormatterV1_1::sDEL      = 0;
00027 const int ESDataFormatterV1_1::sDERR     = bDEL + sDEL;
00028 const int ESDataFormatterV1_1::sDRUN     = 0;
00029 const int ESDataFormatterV1_1::sDRUNTYPE = 0;
00030 const int ESDataFormatterV1_1::sDTRGTYPE = 0;
00031 const int ESDataFormatterV1_1::sDCOMFLAG = bDTRGTYPE + sDTRGTYPE;
00032 const int ESDataFormatterV1_1::sDORBIT   = 0;
00033 const int ESDataFormatterV1_1::sDVMINOR  = 8;
00034 const int ESDataFormatterV1_1::sDVMAJOR  = bDVMINOR + sDVMINOR;
00035 const int ESDataFormatterV1_1::sDCH      = 0;
00036 const int ESDataFormatterV1_1::sDOPTO    = 16;
00037 
00038 const int ESDataFormatterV1_1::bKEC    = 8;   // KCHIP packet event counter
00039 const int ESDataFormatterV1_1::bKFLAG2 = 8;
00040 const int ESDataFormatterV1_1::bKBC    = 12;  // KCHIP packet bunch counter
00041 const int ESDataFormatterV1_1::bKFLAG1 = 4;
00042 const int ESDataFormatterV1_1::bKET    = 1;
00043 const int ESDataFormatterV1_1::bKCRC   = 1;
00044 const int ESDataFormatterV1_1::bKCE    = 1;
00045 const int ESDataFormatterV1_1::bKID    = 11;
00046 const int ESDataFormatterV1_1::bFIBER  = 6;   // Fiber number
00047 const int ESDataFormatterV1_1::bKHEAD1 = 2;
00048 const int ESDataFormatterV1_1::bKHEAD2 = 2;
00049 
00050 const int ESDataFormatterV1_1::sKEC    = 0;  
00051 const int ESDataFormatterV1_1::sKFLAG2 = bKEC + sKEC;
00052 const int ESDataFormatterV1_1::sKBC    = bKFLAG2 + sKFLAG2; 
00053 const int ESDataFormatterV1_1::sKFLAG1 = bKBC + sKBC;
00054 const int ESDataFormatterV1_1::sKET    = 0;
00055 const int ESDataFormatterV1_1::sKCRC   = bKET + sKET;
00056 const int ESDataFormatterV1_1::sKCE    = bKCRC + sKCRC;
00057 const int ESDataFormatterV1_1::sKID    = bKCE + sKCE + 5;
00058 const int ESDataFormatterV1_1::sFIBER  = bKID + sKID + 1;  
00059 const int ESDataFormatterV1_1::sKHEAD1 = bFIBER + sFIBER + 2;
00060 const int ESDataFormatterV1_1::sKHEAD2 = bKHEAD1 + sKHEAD1;
00061 
00062 const int ESDataFormatterV1_1::bADC0  = 16;
00063 const int ESDataFormatterV1_1::bADC1  = 16;
00064 const int ESDataFormatterV1_1::bADC2  = 16;
00065 const int ESDataFormatterV1_1::bPACE  = 2;
00066 const int ESDataFormatterV1_1::bSTRIP = 5;
00067 const int ESDataFormatterV1_1::bE0    = 1;
00068 const int ESDataFormatterV1_1::bE1    = 1;
00069 const int ESDataFormatterV1_1::bHEAD  = 2;
00070 
00071 const int ESDataFormatterV1_1::sADC0  = 0;
00072 const int ESDataFormatterV1_1::sADC1  = bADC0 + sADC0;
00073 const int ESDataFormatterV1_1::sADC2  = 0;
00074 const int ESDataFormatterV1_1::sPACE  = bADC2 + sADC2;
00075 const int ESDataFormatterV1_1::sSTRIP = bPACE + sPACE; 
00076 const int ESDataFormatterV1_1::sE0    = bSTRIP + sSTRIP + 1;
00077 const int ESDataFormatterV1_1::sE1    = bE0 + sE0;
00078 const int ESDataFormatterV1_1::sHEAD  = bE1 + sE1 + 4;
00079 
00080 using namespace std; 
00081 using namespace edm; 
00082 
00083 
00084 ESDataFormatterV1_1::ESDataFormatterV1_1(const ParameterSet& ps) 
00085   : ESDataFormatter(ps) {
00086 }
00087 
00088 ESDataFormatterV1_1::~ESDataFormatterV1_1() {
00089 }
00090 
00091 // FEDRawData * ESDataFormatterV1_1::DigiToRawDCC(int fedId, const Digis & digis) {
00092 
00093 //   int ts[3] = {0, 0, 0};
00094 //   Word32 word1, word2;
00095 //   Word64 word;
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 
00111 //   // DCC words
00112 //   vector<Word64> DCCwords;
00113 //   word2 = (run_number_ << 0) ;
00114 //   word1 = 0;
00115 //   word =  (Word64(word2) << 32 ) | Word64(word1);
00116 
00117 //   DCCwords.push_back(word);
00118 //   for (int i=0; i<5; ++i) {
00119 //     word = 0;
00120 //     DCCwords.push_back(word);
00121 //   }
00122 
00123 //   FEDRawData * rawData = new FEDRawData(0);
00124 
00125 //   return rawData;
00126 // }
00127 
00128 void ESDataFormatterV1_1::DigiToRaw(int fedId, Digis& digis, FEDRawData& fedRawData) {
00129 
00130   map<int, vector<Word64> > map_data;
00131   map_data.clear();  
00132 
00133   int ts[3] = {0, 0, 0};
00134   Word32 word1, word2;
00135   Word64 word;
00136   vector<Word64> words;
00137 
00138 
00139     const DetDigis& detDigis = digis[fedId];
00140 
00141 //     if (detDigis==digis.end()) { 
00142 //       cout << "ESDataFormatterV1_1::DigiToRaw : could not find digi vector in digis map for fedID: " 
00143 //         << fedId << endl ; 
00144 //       return 0; 
00145 //     } 
00146 
00147     for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); ++it) {
00148       
00149       const ESDataFrame& dataframe = (*it);            
00150       const ESDetId& detId = dataframe.id();     
00151       
00152       for (int is=0; is<dataframe.size(); ++is) ts[is] = dataframe.sample(is).adc();            
00153       
00154       //  calculate fake kchip and pace id 
00155       int kchip = -1;
00156       int pace = -1;
00157       int ix = -1;
00158       int iy = -1;
00159       
00160       ix = detId.six() % 2;
00161       iy = detId.siy() % 2;
00162       if (ix == 1 && iy == 1)
00163         pace = 0;
00164       else if (ix == 0 && iy == 1)
00165         pace = 1;
00166       else if (ix == 1 && iy == 0) 
00167         pace = 2;
00168       else if (ix == 0 && iy == 0)
00169         pace = 3;
00170       
00171       ix = (1 + detId.six()) / 2;
00172       iy = (1 + detId.siy()) / 2;  
00173       if (detId.zside() == 1 && detId.plane() == 1) 
00174         kchip = ix + (iy-1)*20 - 1;
00175       else if (detId.zside() == 1 && detId.plane() == 2) 
00176         kchip = ix + (iy-1)*20 + 399;
00177       else if (detId.zside() == -1 && detId.plane() == 1) 
00178         kchip = ix + (iy-1)*20 + 799;
00179       else if (detId.zside() == -1 && detId.plane() == 2) 
00180         kchip = ix + (iy-1)*20 + 1199;
00181       
00182       if (debug_) cout<<"Si : "<<detId.zside()<<" "<<detId.plane()<<" "<<detId.six()<<" "<<detId.siy()<<" "<<detId.strip()<<" ("<<kchip<<","<<pace<<") "<<ts[2]<<" "<<ts[1]<<" "<<ts[0]<<endl;
00183 
00184       word1 = (ts[1] << sADC1) | (ts[0] << sADC0);
00185       word2 = (2 << sHEAD) | (0 << sE1) | (0 << sE0) | ((detId.strip()-1) << sSTRIP) | (pace << sPACE) | (ts[2] << sADC2);
00186       word  = (Word64(word2) << 32 ) | Word64(word1);
00187       
00188       map_data[kchip].push_back(word);
00189     }
00190 
00191 
00192   map<int, vector<Word64> >::const_iterator kit;
00193   for (kit=map_data.begin(); kit!=map_data.end(); ++kit) {
00194 
00195     if (debug_) cout<<"KCHIP : "<<kit->first<<endl;
00196 
00197     word1 = (0 << sKFLAG1) | (0 << sKBC) | (0 << sKFLAG2) | (0 << sKEC);                                                       
00198     word2 = (1 << sKHEAD2) | (0 << sKHEAD1) | (0 << sFIBER) | (kit->first << sKID) | (0 << sKCE) | (0 << sKCRC) | (0 << sKET);
00199     word  = (Word64(word2) << 32 ) | Word64(word1);                                                                            
00200 
00201     if (debug_) cout<<"KCHIP : "<<print(word)<<endl; 
00202     words.push_back(word);           
00203 
00204     const vector<Word64> & data = kit->second; 
00205     for (unsigned int id=0; id<data.size(); ++id) {
00206       if (debug_) cout<<"Data  : "<<print(data[id])<<endl;
00207       words.push_back(data[id]);
00208     }
00209 
00210   } 
00211 
00212   int dataSize = (words.size() + 8) * sizeof(Word64);
00213   
00214   // DCC words
00215   vector<Word64> DCCwords;
00216   word2 = (0 << sDHEAD) | (1 <<sDH) | (run_number_ << sDRUN);
00217   word1 = (dataSize << sDEL);
00218   word  = (Word64(word2) << 32 ) | Word64(word1);
00219   DCCwords.push_back(word);
00220   word2 = (0 << sDHEAD) | (2 <<sDH);
00221   word1 = 0;
00222   word  = (Word64(word2) << 32 ) | Word64(word1);
00223   DCCwords.push_back(word);
00224   word2 = (0 << sDHEAD) | (3 <<sDH) | (1 << sDVMAJOR) | (1 << sDVMINOR); 
00225   word1 = (orbit_number_ << sDORBIT);
00226   word  = (Word64(word2) << 32 ) | Word64(word1);
00227   DCCwords.push_back(word);
00228   word2 = (0 << sDHEAD) | (4 <<sDH);
00229   word1 = 0;
00230   word  = (Word64(word2) << 32 ) | Word64(word1);
00231   DCCwords.push_back(word);
00232   word2 = (0 << sDHEAD) | (5 <<sDH);
00233   word1 = 0;
00234   word  = (Word64(word2) << 32 ) | Word64(word1);
00235   DCCwords.push_back(word);
00236   word2 = (0 << sDHEAD) | (6 <<sDH);
00237   word1 = 0;
00238   word  = (Word64(word2) << 32 ) | Word64(word1);
00239   DCCwords.push_back(word);
00240   
00241   // Output (data size in Bytes)
00242   fedRawData.resize(dataSize);
00243 
00244   Word64 * w = reinterpret_cast<Word64* >(fedRawData.data());
00245   
00246   // header
00247   FEDHeader::set( reinterpret_cast<unsigned char*>(w), trgtype_, lv1_, bx_, fedId); 
00248   w++;
00249 
00250   // ES-DCC 
00251   for (unsigned int i=0; i<DCCwords.size(); ++i) {
00252     if (debug_) cout<<"DCC  : "<<print(DCCwords[i])<<endl;
00253     *w = DCCwords[i];
00254     w++;
00255   }
00256 
00257   // event data
00258   for (unsigned int i=0; i<words.size(); ++i) {
00259     *w = words[i];
00260     w++;  
00261   }
00262 
00263   // trailer
00264   FEDTrailer::set( reinterpret_cast<unsigned char*>(w), dataSize/sizeof(Word64), 
00265                    evf::compute_crc(fedRawData.data(), dataSize),
00266                    0, 0);
00267   w++;
00268  
00269 
00270 }
00271