CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/EventFilter/EcalTBRawToDigi/src/MatacqRawEvent.cc

Go to the documentation of this file.
00001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 8; -*-
00002 /*
00003  * $Id: MatacqRawEvent.cc,v 1.5 2010/08/06 20:24:29 wmtan Exp $
00004  * Original author: Ph. Gras CEA/Saclay 
00005  */
00006 
00012 #include <unistd.h>
00013 #include <stdlib.h>
00014 #include <fstream>
00015 #include <vector>
00016 #include <iostream>
00017 #include <iomanip>
00018 #include <ctime>
00019 #include <limits>
00020 #include <stdexcept>
00021 #include "EventFilter/EcalTBRawToDigi/src/MatacqRawEvent.h"
00022 
00023 
00024 const MatacqTBRawEvent::field32spec_t MatacqTBRawEvent::fov32             = {0, 0x000000F0};
00025 const MatacqTBRawEvent::field32spec_t MatacqTBRawEvent::fedId32           = {0, 0x000FFF00};
00026 const MatacqTBRawEvent::field32spec_t MatacqTBRawEvent::bxId32            = {0, 0xFFF00000};
00027 const MatacqTBRawEvent::field32spec_t MatacqTBRawEvent::lv132             = {1, 0x00FFFFFF};
00028 const MatacqTBRawEvent::field32spec_t MatacqTBRawEvent::triggerType32     = {1, 0x0F000000};
00029 const MatacqTBRawEvent::field32spec_t MatacqTBRawEvent::boeType32         = {1, 0xF0000000};
00030 const MatacqTBRawEvent::field32spec_t MatacqTBRawEvent::dccLen32          = {2, 0x00FFFFFF};
00031 const MatacqTBRawEvent::field32spec_t MatacqTBRawEvent::dccErrors32       = {2, 0xFF000000};
00032 const MatacqTBRawEvent::field32spec_t MatacqTBRawEvent::runNum32          = {3, 0x00FFFFFF};
00033 const MatacqTBRawEvent::field32spec_t MatacqTBRawEvent::h1Marker32        = {3, 0xF0000000};
00034 
00035 void MatacqTBRawEvent::setRawData(const unsigned char* pData, size_t maxSize){
00036   error = 0;
00037   int16le_t* begin16 = (int16le_t*) pData;
00038   int16le_t* pData16 = begin16;
00039   daqHeader = (uint32le_t*) pData16;
00040   const int daqHeaderLen = 16; //in bytes 
00041   pData16 += daqHeaderLen/sizeof(pData16[0]);
00042   matacqHeader = (matacqHeader_t*) pData16;
00043   pData16 += sizeof(matacqHeader_t)/sizeof(pData16[0]);
00044   if(getMatacqDataFormatVersion()>=2){//trigger position present
00045     tTrigPs = *((int32_t*) pData16);
00046     pData16 += 2;
00047   } else{
00048     tTrigPs = std::numeric_limits<int>::max();    
00049   }
00050   const int nCh = getChannelCount();
00051   channelData.resize(nCh);
00052   for(int iCh=0; iCh<nCh; ++iCh){
00053     //channel id:
00054     channelData[iCh].chId = *(pData16++);
00055     //number of time samples for this channel:
00056     channelData[iCh].nSamples = *(pData16++);
00057     //pointer to time sample data of this channel:
00058     channelData[iCh].samples = pData16;
00059     //moves to next channel data block:
00060     pData16 += channelData[iCh].nSamples;
00061   }
00062   
00063   //data trailer chekes:
00064   //FED header is aligned on 64-bit=>padding to skip
00065   int padding = (4-(pData16-begin16))%4;
00066   if(padding<0) padding+=4;
00067   pData16 += padding;
00068   uint32le_t* trailer32 = (uint32le_t*)(pData16);
00069   fragLen = trailer32[1]&0xFFFFFF;
00070   
00071   //std::cout << "Event fragment length including headers: " << fragLen
00072   //     << " 64-bit words\n";
00073   
00074   //FIXME: I am expecting the event length specifies in the header to
00075   //include the header, while it is not the case in current TB 2006 data
00076   const int nHeaders = 3;
00077   if(fragLen!=read32(daqHeader,dccLen32)+nHeaders
00078      && fragLen != read32(daqHeader,dccLen32)){
00079     //std::cout << "Error: fragment length is not consistent with DCC "
00080     //  "length\n";
00081     error |= errorLengthConsistency;
00082   }
00083   
00084   //skip trailers
00085   const int trailerLen = 4;
00086   pData16 += trailerLen;
00087   
00088   parsedLen = (pData16-begin16) / 4;
00089 
00090   if((pData16-begin16)!=(4*fragLen)){
00091     error |= errorLength;
00092   }
00093 
00094   if((size_t)(pData16-begin16)>maxSize){
00095     throw std::runtime_error(std::string("Corrupted or truncated data"));
00096   }
00097 
00098   //some checks
00099   if(getBoe()!=0x5){
00100     error |= errorWrongBoe;
00101   }
00102 }
00103 
00104 int MatacqTBRawEvent::read32(uint32le_t* pData, field32spec_t spec32) const{
00105   int result =  pData[spec32.offset] & spec32.mask;
00106   int mask = spec32.mask;
00107   while((mask&0x1) == 0){
00108       mask >>= 1;
00109       result >>= 1;
00110     }
00111   return result;
00112 }