CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/EventFilter/HcalRawToDigi/src/HcalDCCHeader.cc

Go to the documentation of this file.
00001 /*  
00002  *  $Date: 2008/09/11 13:19:22 $
00003  *  $Revision: 1.5 $
00004  *  \author J. Mans -- UMD
00005  */
00006 #include "EventFilter/HcalRawToDigi/interface/HcalHTRData.h"
00007 #include "EventFilter/HcalRawToDigi/interface/HcalDCCHeader.h"
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 #include <string.h>
00010 #include <stdint.h>
00011 
00012 const int HcalDCCHeader::SPIGOT_COUNT = 15;
00013 
00014 HcalDCCHeader::HcalDCCHeader() { }
00015 
00016 unsigned int HcalDCCHeader::getTotalLengthBytes() const { 
00017   unsigned int totalSize=sizeof(HcalDCCHeader);
00018   for (int i=0; i<SPIGOT_COUNT; i++) 
00019     totalSize+=(spigotInfo[i]&0x3FF)*4;
00020   return totalSize;
00021 }
00022 
00023 void HcalDCCHeader::getSpigotData(int nspigot, HcalHTRData& decodeTool) const {
00024   edm::LogWarning("HCAL-Unpacker") << "Using unsafe getSpigotData without length controls.  Not recommended!  Replace with new getSpigotData call";
00025   getSpigotData(nspigot,decodeTool,10000000);
00026 }
00027   
00028 
00029 int HcalDCCHeader::getSpigotData(int nspigot, HcalHTRData& decodeTool, int validSize) const {
00030   const unsigned short* base=((unsigned short*)this)+sizeof(HcalDCCHeader)/sizeof(unsigned short);
00031   int offset=0,i,len=0;
00032   for (i=0; i<=nspigot; i++) {
00033     offset+=len;
00034     len=(spigotInfo[i]&0x3FF)*2;
00035   }
00036   if ((offset+len+sizeof(HcalDCCHeader)/sizeof(unsigned short))<(validSize/sizeof(unsigned short))) {
00037     decodeTool.adoptData(base+offset,len);
00038     return 0;
00039   } else { return -1; }
00040 }
00041 
00042 void HcalDCCHeader::clear() {
00043   commondataformat0=0;
00044   commondataformat1=0x50000000u;
00045   commondataformat2=0;
00046   commondataformat3=0;
00047   dcch0=0x1; // format version 1
00048   dcch1=0;
00049   for (int i=0; i<18; i++) 
00050     spigotInfo[i]=0;
00051 }
00052 
00053 void HcalDCCHeader::setHeader(int sourceid, int bcn, int l1aN, int orbN) {
00054   commondataformat0=0x8|((sourceid&0xFFF)<<8)|((bcn&0xFFF)<<20);
00055   commondataformat1=0x50000000u|(l1aN&0xFFFFFF);
00056 }
00057 
00058 void HcalDCCHeader::copySpigotData(unsigned int spigot_id, const HcalHTRData& data, bool valid, unsigned char LRB_error_word) {
00059   if (spigot_id>=(unsigned int)SPIGOT_COUNT) return;
00060   // construct the spigot info
00061   spigotInfo[spigot_id]=(data.getRawLength()/2)|0xc000;
00062   if (valid) spigotInfo[spigot_id]|=0x2000;
00063   spigotInfo[spigot_id]|=(LRB_error_word<<16)|((data.getErrorsWord()&0xFF)<<24);
00064   // status info...
00065   if (valid) dcch0|=(1<<(spigot_id+14));
00066   // copy
00067   unsigned int lenSoFar=0;
00068   for (unsigned int i=0; i<spigot_id; i++) lenSoFar+=getSpigotDataLength(i);
00069   unsigned short* startingPoint=((unsigned short*)this)+sizeof(HcalDCCHeader)/sizeof(unsigned short)+lenSoFar*2;
00070   memcpy(startingPoint,data.getRawData(),sizeof(unsigned short)*data.getRawLength());
00071   // update the trailer...
00072   lenSoFar+=data.getRawLength()/2; // 32-bit words
00073   uint32_t* trailer=((uint32_t*)this)+sizeof(HcalDCCHeader)/sizeof(uint32_t)+lenSoFar;
00074   int len64=sizeof(HcalDCCHeader)/8+lenSoFar/2+1; 
00075   trailer[1]=0;
00076   trailer[0]=0xA0000000u|len64;
00077 }
00078 
00079 std::ostream& operator<<(std::ostream& s, const HcalDCCHeader& head) {
00080 
00081   for (int i=0; i<HcalDCCHeader::SPIGOT_COUNT; i++) {
00082     s << "Spigot " << i << " : " << head.getSpigotDataLength(i) << " bytes, ";
00083     if (head.getSpigotEnabled(i)) s << "E";
00084     if (head.getSpigotPresent(i)) s << "P";
00085     if (head.getSpigotValid(i)) s << "V";
00086     s << ". Error codes: " << std::hex << int(head.getSpigotErrorBits(i)) << "," << int(head.getLRBErrorBits(i)) << std::dec;
00087     s << std::endl;
00088   }
00089   return s;
00090 }