CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 /*  
00002  *  $Date: 2009/12/28 23:16:05 $
00003  *  $Revision: 1.2 $
00004  *  \author A. Campbell - DESY
00005  */
00006 #ifndef HTBDAQ_DATA_STANDALONE
00007 #include "EventFilter/CastorRawToDigi/interface/CastorMergerData.h"
00008 #else
00009 #include "CastorMergerData.h"
00010 #endif
00011 #include <string.h>
00012 #include <stdio.h>
00013 #include <algorithm>
00014 
00015 CastorMergerData::CastorMergerData() : m_formatVersion(-2), m_rawLength(0), m_rawConst(0), m_ownData(0) { }
00016 CastorMergerData::CastorMergerData(const unsigned short* data, int length) {
00017   adoptData(data,length);
00018   m_ownData=0;
00019 }
00020 CastorMergerData::CastorMergerData(const CastorMergerData& hd) : m_formatVersion(hd.m_formatVersion), m_rawLength(hd.m_rawLength), m_rawConst(hd.m_rawConst), m_ownData(0) { }
00021 
00022 CastorMergerData::CastorMergerData(int version_to_create) : m_formatVersion(version_to_create) {
00023   allocate(version_to_create);
00024 }
00025 
00026 void CastorMergerData::allocate(int version_to_create) {
00027   m_formatVersion=version_to_create;
00028   // the needed space is for the biggest possible event...
00029   const int needed=0x200;
00030   // create a buffer big enough...
00031   m_ownData=new unsigned short[needed];
00032   m_rawLength=0;
00033   m_rawConst=m_ownData;
00034 }
00035 
00036 CastorMergerData& CastorMergerData::operator=(const CastorMergerData& hd) {
00037   if (m_ownData==0) {
00038     m_formatVersion=hd.m_formatVersion;
00039     m_rawLength=hd.m_rawLength;
00040     m_rawConst=hd.m_rawConst;
00041   }
00042   return (*this);
00043 }
00044 
00045 void CastorMergerData::adoptData(const unsigned short* data, int length) {
00046   m_rawLength=length;
00047   m_rawConst=data;
00048   if (m_rawLength<5) {
00049     m_formatVersion=-2; // invalid!
00050   } else {
00051     m_formatVersion=(m_rawConst[4]>>12)&0xF;
00052   }
00053 }
00054 
00055 // check :: not EE, length is reasonable, length matches wordcount
00056 //          length required for tp+daq is correct
00057 
00058 bool CastorMergerData::check() const {
00059      // length checks
00060     //  minimum length
00061     if (m_rawLength<6+12) return false;
00062     //  matches wordcount
00063     if (m_rawLength!=m_rawConst[m_rawLength-3]) return false;
00064  
00065   return true;
00066 }
00067 
00068 
00069 void CastorMergerData::unpack(
00070                          unsigned char* tp_lengths, unsigned short* tp_samples) const {
00071 
00072   if (tp_lengths!=0) memset(tp_lengths,0,1);
00073 
00074   int tp_words_total,headerLen,trailerLen;
00075   determineSectionLengths(tp_words_total,headerLen,trailerLen);
00076 
00077   int wordPtr;
00078   const unsigned short* tpBase=m_rawConst+headerLen;
00079   // process the trigger primitive words
00080   if (tp_lengths!=0) {
00081     for (wordPtr=0; wordPtr<tp_words_total; wordPtr++) {
00082        tp_samples[tp_lengths[0]]=tpBase[wordPtr];
00083        tp_lengths[0]++;
00084     }
00085   }
00086  
00087 }
00088 void CastorMergerData::determineSectionLengths(int& tpWords, int& headerWords, int& trailerWords) const {
00089  
00090     tpWords=m_rawConst[5]>>8; // should be 8 but could be up to 12
00091     headerWords=8;
00092     trailerWords=0; // minimum, may be more...
00093 }
00094 
00095 void CastorMergerData::determineStaticLengths(int& headerWords, int& trailerWords) const {
00096    headerWords=8;
00097    trailerWords=0; // minimum, may be more...
00098 
00099 }
00100 void CastorMergerData::pack(
00101                        unsigned char* tp_lengths, unsigned short* tp_samples) {
00102   
00103   int tp_words_total=0, headerLen, trailerLen;
00104   determineStaticLengths(headerLen,trailerLen);
00105 
00106   tp_words_total=0;
00107   int isample;
00108 
00109   // trigger words
00110   unsigned short* ptr=m_ownData+headerLen;
00111   if (tp_samples!=0 && tp_lengths!=0) {
00112       for (isample=0; isample<tp_lengths[0] && isample<12; isample++) {
00113         ptr[tp_words_total]=tp_samples[isample];
00114         tp_words_total++;
00115       }
00116   }
00117 
00118    m_ownData[5]=(tp_words_total<<8)|0x1;
00119    unsigned short totalLen=headerLen+tp_words_total+trailerLen;
00120 
00121    m_rawLength=totalLen;
00122    m_ownData[totalLen-2]=totalLen/2; // 32-bit words
00123    m_ownData[totalLen-3]=totalLen;
00124    m_ownData[totalLen-4]=tp_words_total;
00125  
00126 }
00127 
00128 void CastorMergerData::packHeaderTrailer(int L1Anumber, int bcn, int submodule, int orbitn, int pipeline, int ndd, int nps, int firmwareRev) {
00129   m_ownData[0]=L1Anumber&0xFF;
00130   m_ownData[1]=(L1Anumber&0xFFFF00)>>8;
00131 
00132   m_ownData[2]=0x8000; // Version is valid, no error bits - status bits need definition
00133   m_ownData[3]=((orbitn&0x1F)<<11)|(submodule&0x7FF);
00134   m_ownData[4]=((m_formatVersion&0xF)<<12)|(bcn&0xFFF);
00135   m_ownData[5]|=((nps&0xF)<<4)|0x1;
00136   m_ownData[6]=((firmwareRev&0x70000)>>3)|(firmwareRev&0x1FFF);
00137   m_ownData[7]=(pipeline&0xFF) | ((ndd&0x1F)<<8);
00138   m_ownData[m_rawLength-4]&=0x7FF;
00139   m_ownData[m_rawLength-4]|=(ndd&0x1F)<<11;
00140   
00141   m_ownData[m_rawLength-2]=m_rawLength/2; // 32-bit words
00142   m_ownData[m_rawLength-1]=(L1Anumber&0xFF)<<8;
00143 }
00144 
00145 unsigned int CastorMergerData::getOrbitNumber() const { 
00146   return (m_rawConst[3]>>11);
00147 }
00148 
00149 unsigned int CastorMergerData::getFirmwareRevision() const {
00150   return (m_rawConst[6]);
00151 }
00152 
00153 
00154 
00155