test
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  * 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  unsigned char* begin = (unsigned char*) pData;
84  int16le_t* begin16 = (int16le_t*) pData;
85  uint32le_t* begin32 = (uint32le_t*) pData;
86  int16le_t* pData16 = begin16;
87  const int daqHeaderLen = 16; //in bytes
88  if(maxSize < 6*4){
89  error = errorLength;
90  return;
91  }
92  pData16 += daqHeaderLen/sizeof(pData16[0]);
93  // matacqHeader = (matacqHeader_t*) pData16;
94  daqHeader = begin32;
95  matacqDataFormatVersion = read32(begin32, formatVersion32);
96  freqGHz = read32(begin32, freqGHz32);
97  channelCount = read32(begin32, channelCount32);
98  timeStamp.tv_sec = read32(begin32, timeStamp32);
99  int headerLen = 24; //in bytes
100  if(matacqDataFormatVersion>=2){
101  tTrigPs = read32(begin32, tTrigPs32);
102  headerLen += 4;
103  } else{
104  tTrigPs = numeric_limits<int>::max();
105  }
106 
107  if(matacqDataFormatVersion>=3){
108  orbitId = read32(begin32, orbitId32);
109  vernier[0] = read32(begin32, vernier0_32);
110  vernier[1] = read32(begin32, vernier1_32);
111  vernier[2] = read32(begin32, vernier2_32);
112  vernier[3] = read32(begin32, vernier3_32);
113  timeStamp.tv_usec = read32(begin32, timeStampMicroSec32);
114  trigRec = read32(begin32, trigRec32, true);
115  postTrig = read32(begin32, postTrig32);
116  delayA = read32(begin32, delayA32, true);
117  emtcDelay = read32(begin32, emtcDelay32, true);
118  emtcPhase = read32(begin32, emtcPhase32, true);
119  attenuation_dB = read32(begin32, attenuation_dB32, true);
120  laserPower = read32(begin32, laserPower32, true);
121  headerLen = 64;
122  } else{
123  orbitId = 0;
124  vernier[0] = -1;
125  vernier[1] = -1;
126  vernier[2] = -1;
127  vernier[3] = -1;
128  trigRec = -1;
129  postTrig = -1;
130  delayA = -1;
131  emtcDelay = -1;
132  emtcPhase = -1;
133  attenuation_dB = -1;
134  laserPower = -1;
135  }
136 
137  const int nCh = getChannelCount();
138  channelData.resize(nCh);
139 
140  pData16 = (int16le_t*) (begin+headerLen);
141 
142  for(int iCh=0; iCh<nCh; ++iCh){
143  if((size_t)(pData16-begin16)>maxSize){
144  throwExcept(string("Corrupted or truncated data"));
145  }
146  //channel id:
147  channelData[iCh].chId = *(pData16++);
148  //number of time samples for this channel:
149  channelData[iCh].nSamples = *(pData16++);
150  //pointer to time sample data of this channel:
151  channelData[iCh].samples = pData16;
152  //moves to next channel data block:
153  if(channelData[iCh].nSamples<0){
154  throwExcept(string("Corrupted or truncated data"));
155  }
156  pData16 += channelData[iCh].nSamples;
157  }
158 
159  //data trailer chekes:
160  //FED header is aligned on 64-bit=>padding to skip
161  int padding = (4-(pData16-begin16))%4;
162  if(padding<0) padding+=4;
163  pData16 += padding;
164  if((size_t)(pData16-begin16)>maxSize){
165  throwExcept(string("Corrupted or truncated data"));
166  }
167  uint32le_t* trailer32 = (uint32le_t*)(pData16);
168  fragLen = trailer32[1]&0xFFFFFF;
169 
170  //cout << "Event fragment length including headers: " << fragLen
171  // << " 64-bit words\n";
172 
173  //FIXME: I am expecting the event length specifies in the header to
174  //include the header, while it is not the case in current TB 2006 data
175  const int nHeaders = 3;
176  if(fragLen!=read32(begin32,dccLen32)+nHeaders
177  && fragLen != read32(begin32,dccLen32)){
178  //cout << "Error: fragment length is not consistent with DCC "
179  // "length\n";
180  error |= errorLengthConsistency;
181  }
182 
183  //skip trailers
184  const int trailerLen = 4;
185  pData16 += trailerLen;
186 
187  parsedLen = (pData16-begin16) / 4;
188 
189  if((pData16-begin16)!=(4*fragLen)){
190  error |= errorLength;
191  }
192 
193  if((size_t)(pData16-begin16)>maxSize){
194  throwExcept(string("Corrupted or truncated data"));
195  }
196 
197  //some checks
198  if(getBoe()!=0x5){
199  error |= errorWrongBoe;
200  }
201 }
202 
204  bool ovfTrans){
205  uint32_t result = pData[spec32.offset] & spec32.mask;
206  uint32_t mask = spec32.mask;
207  while((mask&0x1) == 0){
208  mask >>= 1;
209  result >>= 1;
210  }
211  if(ovfTrans){
212  //overflow bit (MSB) mask:
213  mask = ((mask >>1) + 1);
214  if(result & mask) result = (uint32_t)-1;
215  }
216  return result;
217 }
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
tuple result
Definition: mps_fire.py:84
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
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
static const field32spec_t postTrig32
static int read32(uint32le_t *pData, field32spec_t spec, bool ovfTrans=false)
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
#define begin
Definition: vmac.h:30
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