CMS 3D CMS Logo

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