CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/EventFilter/EcalDigiToRaw/src/BlockFormatter.cc

Go to the documentation of this file.
00001 #include <memory>
00002 
00003 // user include files
00004 
00005 
00006 #include "EventFilter/EcalDigiToRaw/interface/EcalDigiToRaw.h"
00007 #include "EventFilter/EcalDigiToRaw/interface/BlockFormatter.h"
00008 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00009 #include "DataFormats/EcalDetId/interface/EcalElectronicsId.h"
00010 
00011 
00012 using namespace std;
00013 
00014 BlockFormatter::BlockFormatter() {
00015 }
00016 
00017 BlockFormatter::~BlockFormatter() {
00018 }
00019 
00020 
00021 void BlockFormatter::SetParam(EcalDigiToRaw* base) {
00022 
00023  pcounter_ = (base -> GetCounter());
00024  debug_   = base -> GetDebug();
00025  porbit_number_ = (base -> GetOrbit());
00026  plv1_ = (base -> GetLV1());
00027  pbx_  = (base -> GetBX());
00028  prunnumber_ = (base -> GetRunNumber());
00029  doBarrel_ = base -> GetDoBarrel();
00030  doEndCap_ = base -> GetDoEndCap();
00031  plistDCCId_ = base -> GetListDCCId();
00032  doTCC_ = base -> GetDoTCC();
00033  doSR_ = base -> GetDoSR();
00034  doTower_ = base -> GetDoTower();
00035 
00036 }
00037 
00038 
00039 void BlockFormatter::DigiToRaw(FEDRawDataCollection* productRawData) {
00040 
00041  int run_number = *prunnumber_;
00042  int orbit_number_ = *porbit_number_;
00043  int bx = *pbx_;
00044  int lv1 = *plv1_;
00045 
00046  if (debug_) cout << "in BlockFormatter::DigiToRaw  run_number orbit_number bx lv1 " << dec << run_number << " " <<
00047          orbit_number_ << " " << bx << " " << lv1 << endl;
00048 
00049  for (int idcc=1; idcc <= 54; idcc++) {
00050         if ( (! doBarrel_) && 
00051              (idcc >= EcalElectronicsId::MIN_DCCID_EBM && idcc <= EcalElectronicsId::MAX_DCCID_EBP)) continue;
00052         if ( (! doEndCap_) && 
00053              (idcc <= EcalElectronicsId::MAX_DCCID_EEM || idcc >= EcalElectronicsId::MIN_DCCID_EEP)) continue;
00054  
00055         int FEDid = FEDNumbering::MINECALFEDID + idcc;
00056         FEDRawData& rawdata = productRawData -> FEDData(FEDid);
00057         unsigned char * pData;
00058         short int DCC_ERRORS = 0;
00059 
00060         if (rawdata.size() == 0) {
00061                 rawdata.resize(8);
00062                 pData = rawdata.data();
00063 
00064                 Word64 word = 0x18 + ((FEDid & 0xFFF)<<8)
00065                             + ((Word64)((Word64)bx & 0xFFF)<<20)
00066                             + ((Word64)((Word64)lv1 & 0xFFFFFF)<<32)
00067                             + (Word64)((Word64)0x51<<56);
00068                 Word64* pw = reinterpret_cast<Word64*>(const_cast<unsigned char*>(pData));
00069                 *pw = word;    // DAQ header
00070 
00071                 rawdata.resize(rawdata.size() + 8*8);   // DCC header
00072                 pData = rawdata.data();
00073                 pData[11] = DCC_ERRORS & 0xFF;
00074                 pData[12] = run_number & 0xFF;
00075                 pData[13] = (run_number >>8) & 0xFF;
00076                 pData[14] = (run_number >> 16) & 0xFF;
00077                 pData[15] = 0x01;
00078 
00079                 for (int i=16; i <= 22; i++) {
00080                  pData[i] = 0;    // to be filled for local data taking or calibration
00081                 }
00082                 pData[23] = 0x02;
00083                 pData[24] = orbit_number_ & 0xFF;
00084                 pData[25] = (orbit_number_ >>8) & 0xFF;
00085                 pData[26] = (orbit_number_ >>16) & 0xFF;
00086                 pData[27] = (orbit_number_ >>24) & 0xFF;
00087                 int SRenable_ = 1;
00088                 int SR = SRenable_;
00089                 int ZS = 0;
00090                 int TZS = 0;
00091                 // int SR_CHSTATUS = 0;
00092                 pData[28] = (SR&0x1) + ((ZS&0x1)<<1) + ((TZS&0x1)<<2);
00093                 pData[31] = 0x03;
00094 
00095                 for (int i=0; i<=4; i++) {
00096                   for (int j=0; j<7; j++) {
00097                    pData[32 +8*i + j] = 0;
00098                   }
00099                   pData[32 +8*i + 7] = 0x04;
00100                 }
00101 
00102         } // endif rawdatasize == 0
00103  } // loop on id
00104 
00105 }
00106 
00107 
00108 
00109 void BlockFormatter::print(FEDRawData& rawdata) {
00110         int size = rawdata.size();
00111         cout << "Print RawData  size " << dec << size << endl;
00112         unsigned char* pData = rawdata.data();
00113 
00114         int n = size/8;
00115         for (int i=0; i < n; i++) {
00116                 for (int j=7; j>=0; j--) {
00117                   if (8*i+j <= size) cout << hex << (int)pData[8*i+j] << " ";
00118                 }
00119                 cout << endl;
00120         }
00121 }
00122 
00123 
00124 
00125 void BlockFormatter::CleanUp(FEDRawDataCollection* productRawData,
00126                                 map<int, map<int,int> >* FEDorder ) {
00127 
00128 
00129  for (int id=0; id < 36 + 18; id++) {
00130         if ( (! doBarrel_) && (id >= 9 && id <= 44)) continue;
00131         if ( (! doEndCap_) && (id <= 8 || id >= 45)) continue;
00132 
00133         int FEDid = FEDNumbering::MINECALFEDID + id +1;
00134         FEDRawData& rawdata = productRawData -> FEDData(FEDid);
00135 
00136         // ---- if raw need not be made for a given fed, set its size to empty and return 
00137         if ( find( (*plistDCCId_).begin(), (*plistDCCId_).end(), (id+1) ) == (*plistDCCId_).end() )
00138         {
00139             rawdata.resize( 0 );
00140             continue;
00141         }
00142 
00143         // ---- Add the trailer word
00144         int lastline = rawdata.size();
00145         rawdata.resize( lastline + 8);
00146         unsigned char * pData = rawdata.data(); 
00147         int event_length = (lastline + 8) / 8;   // in 64 bits words
00148 
00149         pData[lastline+7] = 0xa0;
00150         // pData[lastline+4] = event_length & 0xFFFFFF;
00151         pData[lastline+4] = event_length & 0xFF;
00152         pData[lastline+5] = (event_length >> 8) & 0xFF;
00153         pData[lastline+6] = (event_length >> 16) & 0xFF;
00154         int event_status = 0;
00155         pData[lastline+1] = event_status & 0x0F;
00156         int tts = 0 <<4;
00157         pData[lastline] = tts & 0xF0;
00158 
00159         // ---- Write the event length in the DCC header
00160         // pData[8] = event_length & 0xFFFFFF;
00161         pData[8] = event_length & 0xFF;
00162         pData[9] = (event_length >> 8) & 0xFF;
00163         pData[10] = (event_length >> 16) & 0xFF;
00164         
00165         // cout << " in BlockFormatter::CleanUp. FEDid = " << FEDid << " event_length*8 " << dec << event_length*8 << endl;
00166 
00167         map<int, map<int,int> >::iterator fen = FEDorder -> find(FEDid);
00168 
00169         bool FED_has_data = true;
00170         if (fen == FEDorder->end()) FED_has_data = false;
00171         if (debug_ && (! FED_has_data)) cout << " FEDid is not in FEDorder ! " << endl;
00172         if ( ! FED_has_data) {
00173                 int ch_status = 7;
00174                 for (int iFE=1; iFE <= 68; iFE++) {
00175                         int irow = (iFE-1) / 14;
00176                         int kval = ( (iFE-1) % 14) / 2;
00177                         if (iFE % 2 ==1) pData[32 + 8*irow + kval] |= ch_status & 0xFF;
00178                         else pData[32 + 8*irow + kval] |= ((ch_status <<4) & 0xFF);
00179                 }
00180         }
00181 
00182         if (FED_has_data) {
00183         map<int, int>& FEorder = (*fen).second;
00184 
00185         for (int iFE=1; iFE <= 68; iFE++) {
00186           map<int,int>::iterator fe = FEorder.find(iFE);
00187           int ch_status = 0;
00188           if (fe == FEorder.end())      // FE not present due to SRP, update CH_status
00189                 ch_status = 7;          // CH_SUPPRESS
00190           int irow = (iFE-1) / 14;
00191           int kval = ( (iFE-1) % 14) / 2;
00192           if (iFE % 2 ==1) pData[32 + 8*irow + kval] |= ch_status & 0xFF;
00193           else pData[32 + 8*irow + kval] |= ((ch_status <<4) & 0xFF);
00194 
00195         }
00196         }
00197         
00198  }
00199 }
00200 
00201 
00202 void BlockFormatter::PrintSizes(FEDRawDataCollection* productRawData) {
00203 
00204 
00205  for (int id=0; id < 36 + 18; id++) {
00206 
00207         // if ( (! doBarrel_) && (id >= 9 && id <= 44)) continue;
00208         // if ( (! doEndCap_) && (id <= 8 || id >= 45)) continue;
00209 
00210 
00211         int FEDid = FEDNumbering::MINECALFEDID + id;
00212         FEDRawData& rawdata = productRawData -> FEDData(FEDid);
00213         if (rawdata.size() > 0)
00214         cout << "Size of FED id " << dec << FEDid << " is : " << dec << rawdata.size() << endl;
00215 
00216  }
00217 }
00218 
00219