CMS 3D CMS Logo

MatacqRawEvent.cc
Go to the documentation of this file.
1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 8; -*-
2 /*
3  * Original author: Ph. Gras CEA/Saclay
4  */
5 
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <fstream>
14 #include <vector>
15 #include <iostream>
16 #include <iomanip>
17 #include <ctime>
18 #include <limits>
19 
20 #define CMSSW
21 
22 #ifdef CMSSW //compilation within CMSSW framework
23 
26 
27 static inline void throwExcept(const std::string& s){
28  throw cms::Exception("Matacq") << s;
29 }
30 
31 #else //compilation outside CMSSW framework (e.g. online)
32 
33 #include "MatacqRawEvent.h"
34 #include <stdexcept>
35 static inline void throwExcept(const std::string& s){
36  throw std::runtime_error(s.c_str());
37 }
38 
39 #endif //CMSSW not defined
40 
41 using namespace std;
42 
43 //DAQ header fields:
54 
55 //Matacq header fields:
60 // for data format version >=2:
62 // for data format version >=3:
80 
81 void MatacqRawEvent::setRawData(const unsigned char* pData, size_t maxSize){
82  error = 0;
83  const int16le_t *begin16 = (const int16le_t *) pData;
84  const uint32le_t *begin32 = (const uint32le_t *) pData;
85  if(maxSize < 6*4){
86  error = errorLength;
87  return;
88  }
89 
90  daqHeader = begin32;
91  matacqDataFormatVersion = read32(begin32, formatVersion32);
92  freqGHz = read32(begin32, freqGHz32);
93  channelCount = read32(begin32, channelCount32);
94  timeStamp.tv_sec = read32(begin32, timeStamp32);
95  int headerLen = 24; //in bytes
96  if(matacqDataFormatVersion>=2){
97  tTrigPs = read32(begin32, tTrigPs32);
98  headerLen += 4;
99  } else{
100  tTrigPs = numeric_limits<int>::max();
101  }
102 
103  if(matacqDataFormatVersion>=3){
104  orbitId = read32(begin32, orbitId32);
105  vernier[0] = read32(begin32, vernier0_32);
106  vernier[1] = read32(begin32, vernier1_32);
107  vernier[2] = read32(begin32, vernier2_32);
108  vernier[3] = read32(begin32, vernier3_32);
109  timeStamp.tv_usec = read32(begin32, timeStampMicroSec32);
110  trigRec = read32(begin32, trigRec32, true);
111  postTrig = read32(begin32, postTrig32);
112  delayA = read32(begin32, delayA32, true);
113  emtcDelay = read32(begin32, emtcDelay32, true);
114  emtcPhase = read32(begin32, emtcPhase32, true);
115  attenuation_dB = read32(begin32, attenuation_dB32, true);
116  laserPower = read32(begin32, laserPower32, true);
117  headerLen = 64;
118  } else{
119  orbitId = 0;
120  vernier[0] = -1;
121  vernier[1] = -1;
122  vernier[2] = -1;
123  vernier[3] = -1;
124  trigRec = -1;
125  postTrig = -1;
126  delayA = -1;
127  emtcDelay = -1;
128  emtcPhase = -1;
129  attenuation_dB = -1;
130  laserPower = -1;
131  }
132 
133  const int nCh = getChannelCount();
134  channelData.resize(nCh);
135 
136  const int16le_t *pData16 = (const int16le_t *) (pData+headerLen);
137 
138  for(int iCh=0; iCh<nCh; ++iCh){
139  if((size_t)(pData16-begin16)>maxSize){
140  throwExcept(string("Corrupted or truncated data"));
141  }
142  //channel id:
143  channelData[iCh].chId = *(pData16++);
144  //number of time samples for this channel:
145  channelData[iCh].nSamples = *(pData16++);
146  //pointer to time sample data of this channel:
147  channelData[iCh].samples = pData16;
148  //moves to next channel data block:
149  if(channelData[iCh].nSamples<0){
150  throwExcept(string("Corrupted or truncated data"));
151  }
152  pData16 += channelData[iCh].nSamples;
153  }
154 
155  //data trailer chekes:
156  //FED header is aligned on 64-bit=>padding to skip
157  int padding = (4-(pData16-begin16))%4;
158  if(padding<0) padding+=4;
159  pData16 += padding;
160  if((size_t)(pData16-begin16)>maxSize){
161  throwExcept(string("Corrupted or truncated data"));
162  }
163  uint32le_t* trailer32 = (uint32le_t*)(pData16);
164  fragLen = trailer32[1]&0xFFFFFF;
165 
166  //cout << "Event fragment length including headers: " << fragLen
167  // << " 64-bit words\n";
168 
169  //FIXME: I am expecting the event length specifies in the header to
170  //include the header, while it is not the case in current TB 2006 data
171  const int nHeaders = 3;
172  if(fragLen!=read32(begin32,dccLen32)+nHeaders
173  && fragLen != read32(begin32,dccLen32)){
174  //cout << "Error: fragment length is not consistent with DCC "
175  // "length\n";
176  error |= errorLengthConsistency;
177  }
178 
179  //skip trailers
180  const int trailerLen = 4;
181  pData16 += trailerLen;
182 
183  parsedLen = (pData16-begin16) / 4;
184 
185  if((pData16-begin16)!=(4*fragLen)){
186  error |= errorLength;
187  }
188 
189  if((size_t)(pData16-begin16)>maxSize){
190  throwExcept(string("Corrupted or truncated data"));
191  }
192 
193  //some checks
194  if(getBoe()!=0x5){
195  error |= errorWrongBoe;
196  }
197 }
198 
200  bool ovfTrans){
201  uint32_t result = pData[spec32.offset] & spec32.mask;
202  uint32_t mask = spec32.mask;
203  while((mask&0x1) == 0){
204  mask >>= 1;
205  result >>= 1;
206  }
207  if(ovfTrans){
208  //overflow bit (MSB) mask:
209  mask = ((mask >>1) + 1);
210  if(result & mask) result = (uint32_t)-1;
211  }
212  return result;
213 }
static const field32spec_t vernier1_32
static const field32spec_t fedId32
static const field32spec_t boeType32
static int read32(const uint32le_t *pData, field32spec_t spec, bool ovfTrans=false)
static const field32spec_t runNum32
static const field32spec_t timeStamp32
static const field32spec_t trigType32
static const field32spec_t tTrigPs32
static const field32spec_t channelCount32
static const field32spec_t emtcPhase32
void setRawData(const unsigned char *buffer, size_t bufferSize)
static const field32spec_t dccLen32
static const field32spec_t fov32
static const field32spec_t attenuation_dB32
static const field32spec_t lv132
static const field32spec_t vernier0_32
static const field32spec_t vernier3_32
static const field32spec_t postTrig32
constexpr size_t nSamples
static const field32spec_t timeStampMicroSec32
static const field32spec_t trigRec32
static const field32spec_t vernier2_32
static const field32spec_t emtcDelay32
static const field32spec_t side32
static const field32spec_t dccId32
static const field32spec_t triggerType32
static const field32spec_t color32
static const field32spec_t delayA32
static const field32spec_t orbitId32
static const field32spec_t formatVersion32
static const field32spec_t dccErrors32
static const field32spec_t h1Marker32
static const field32spec_t freqGHz32
static const field32spec_t laserPower32
static void throwExcept(const std::string &s)
static const field32spec_t bxId32