CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/EventFilter/CastorRawToDigi/src/CastorCTDCHeader.cc

Go to the documentation of this file.
00001 /*  
00002  *  $Date: 2009/02/20 17:46:26 $
00003  *  $Revision: 1.1 $
00004  *  \author A. Campbell -- DESY
00005  */
00006 #include "EventFilter/CastorRawToDigi/interface/CastorCORData.h"
00007 #include "EventFilter/CastorRawToDigi/interface/CastorMergerData.h"
00008 #include "EventFilter/CastorRawToDigi/interface/CastorCTDCHeader.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 #include <string.h>
00011 #include <stdint.h>
00012 
00013 const int CastorCTDCHeader::SPIGOT_COUNT = 2; // COR spigots - does not include merger pay load
00014 
00015 CastorCTDCHeader::CastorCTDCHeader() { }
00016 
00017 unsigned int CastorCTDCHeader::getTotalLengthBytes() const { 
00018   unsigned int totalSize=sizeof(CastorCTDCHeader);
00019   for (int i=0; i<SPIGOT_COUNT+1; i++)    // includes merger pay load
00020     totalSize+=(spigotInfo[i]&0x3FF)*4;
00021   return totalSize; // doesn't include the trailer
00022 }  
00023 
00024 int CastorCTDCHeader::getSpigotData(int nspigot, CastorCORData& decodeTool, int validSize) const {
00025   const unsigned short* base=((unsigned short*)this)+sizeof(CastorCTDCHeader)/sizeof(unsigned short);
00026   int offset=0,i,len=0;
00027   for (i=0; i<=nspigot; i++) {
00028     offset+=len;
00029     len=(spigotInfo[i]&0x3FF)*2;
00030   }
00031   if ((offset+len+sizeof(CastorCTDCHeader)/sizeof(unsigned short))<(validSize/sizeof(unsigned short))) {
00032     decodeTool.adoptData(base+offset,len);
00033     return 0;
00034   } else { return -1; }
00035 }
00036 
00037 void CastorCTDCHeader::clear() {
00038   commondataformat0=0;
00039   commondataformat1=0x50000000u;
00040   commondataformat2=0;
00041   commondataformat3=0;
00042   ctdch0=0x1; // format version 1
00043   ctdch1=0;
00044   for (int i=0; i<3; i++) 
00045     spigotInfo[i]=0;
00046   spigotInfo[3]=0x12345678; // end DCC header pattern
00047 
00048 }
00049 
00050 void CastorCTDCHeader::setHeader(int sourceid, int bcn, int l1aN, int orbN) {
00051   commondataformat0=0x8|((sourceid&0xFFF)<<8)|((bcn&0xFFF)<<20);
00052   commondataformat1=0x50000000u|(l1aN&0xFFFFFF);
00053 }
00054 
00055 void CastorCTDCHeader::copySpigotData(unsigned int spigot_id, const CastorCORData& data, bool valid, unsigned char LRB_error_word) {
00056   if (spigot_id>=(unsigned int)SPIGOT_COUNT) return;
00057   // construct the spigot info
00058   spigotInfo[spigot_id]=(data.getRawLength()/2)|0xc000;
00059   if (valid) spigotInfo[spigot_id]|=0x2000;
00060   spigotInfo[spigot_id]|=(LRB_error_word<<16)|((data.getErrorsWord()&0xFF)<<24);
00061   // status info...
00062   if (valid) ctdch0|=(1<<(spigot_id+14));
00063   // copy
00064   unsigned int lenSoFar=0;
00065   for (unsigned int i=0; i<spigot_id; i++) lenSoFar+=getSpigotDataLength(i);
00066   unsigned short* startingPoint=((unsigned short*)this)+sizeof(CastorCTDCHeader)/sizeof(unsigned short)+lenSoFar*2;
00067   memcpy(startingPoint,data.getRawData(),sizeof(unsigned short)*data.getRawLength());
00068   // update the trailer...
00069   lenSoFar+=data.getRawLength()/2; // 32-bit words
00070   uint32_t* trailer=((uint32_t*)this)+sizeof(CastorCTDCHeader)/sizeof(uint32_t)+lenSoFar;
00071   int len64=sizeof(CastorCTDCHeader)/8+lenSoFar/2+1; 
00072   trailer[1]=0;
00073   trailer[0]=0xA0000000u|len64;
00074 }
00075 
00076 void CastorCTDCHeader::copyMergerData(const CastorMergerData& data, bool valid) {
00077   unsigned int spigot_id = 2;
00078   // construct the spigot info
00079   spigotInfo[spigot_id]=(data.getRawLength()/2)|0xc000; // Enabled & Present
00080   if (valid) spigotInfo[spigot_id]|=0x2000; // Valid
00081   spigotInfo[spigot_id]|=((data.getErrorsWord()&0xFF)<<24);
00082   // status info...
00083   if (valid) ctdch0|=(1<<(spigot_id+14));
00084   // copy
00085   unsigned int lenSoFar=0;
00086   for (unsigned int i=0; i<spigot_id; i++) lenSoFar+=getSpigotDataLength(i);
00087   unsigned short* startingPoint=((unsigned short*)this)+sizeof(CastorCTDCHeader)/sizeof(unsigned short)+lenSoFar*2;
00088   memcpy(startingPoint,data.getRawData(),sizeof(unsigned short)*data.getRawLength());
00089   // update the trailer...
00090   lenSoFar+=data.getRawLength()/2; // 32-bit words
00091   uint32_t* trailer=((uint32_t*)this)+sizeof(CastorCTDCHeader)/sizeof(uint32_t)+lenSoFar;
00092   int len64=sizeof(CastorCTDCHeader)/8+lenSoFar/2+1; 
00093   trailer[1]=0;
00094   trailer[0]=0xA0000000u|len64;
00095 }
00096 
00097 std::ostream& operator<<(std::ostream& s, const CastorCTDCHeader& head) {
00098 
00099   for (int i=0; i<CastorCTDCHeader::SPIGOT_COUNT+1; i++) {
00100     s << "Spigot " << i << " : " << head.getSpigotDataLength(i) << " bytes, ";
00101     if (head.getSpigotEnabled(i)) s << "E";
00102     if (head.getSpigotPresent(i)) s << "P";
00103     if (head.getSpigotValid(i)) s << "V";
00104     s << ". Error codes: " << std::hex << int(head.getSpigotErrorBits(i)) << std::dec;
00105     s << std::endl;
00106   }
00107   return s;
00108 }