PPM-DU analysis  2.3
conversion and analysis of the PPM-DU data
 All Classes Files Functions Variables Pages
/home/alex/Work/Research/Km3NeT/PPM-DU/ppm-du-v2.3/rawReader.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include <iomanip>
5 #include <boost/lexical_cast.hpp>
6 #include <climits>
7 #include <vector>
8 #include <math.h>
9 
10 #include "rawReader.h"
11 #include "domHeader.h"
12 #include "pmt.h"
13 #include "signal.h"
14 #include "constants.h"
15 //#include <TRandom3.h>
16 
17 std::vector<std::vector<MyEvent> > allHits;
18 
19 uint16_t
21  if (data == 0)
22  throw "Hit un-initialized";
23  uint8_t header = data[0];
24  uint16_t chan = ((header >> 0) & 0x3); // 2 lower bits of channel
25  chan |= ((header >> 3) & (0x7 << 2)); // 3 higher bits of channel
26  return chan;
27 }
28 
29 uint16_t
31  if (data == 0)
32  throw "Hit un-initialized";
33  uint8_t header = reinterpret_cast<uint16_t*>(data)[0];
34  uint16_t type = ((header >> 2) & 0x7); // bits 2-4
35  return type;
36 }
37 
38 uint32_t
40  if (data == 0)
41  throw "Hit un-initialized";
42  uint32_t ts = reinterpret_cast<uint32_t*>(data)[0];
43  utl::BinIO::swap(ts);
44  return ts & 0x00FFFFFF;
45 }
46 
47 uint16_t
49  if (data == 0)
50  throw "Hit un-initialized";
51  uint16_t patt = reinterpret_cast<uint16_t*>(data + 4)[0];
52  utl::BinIO::swap(patt);
53  return patt;
54 }
55 
56 void
58  if (data == 0)
59  throw "Hit un-initialized";
60  for (size_t i = 0; i < SIZE_BYTES; ++i) {
61  std::cout << std::hex << std::setfill('0') << std::setw(2) << (uint32_t) data[i];
62  }
63  std::cout << std::dec << std::endl;
64 }
65 
66 void
68  if (data == 0)
69  throw "Hit un-initialized";
70  std::cout << std::dec
71  << std::setfill(' ') << std::setw(3) << Channel()
72  << std::setw(2) << Type()
73  << std::setw(9) << TimeStamp()
74  << std::hex << std::setfill('0') << " 0x" << std::setw(4) << Pattern()
75  << std::dec << std::endl;
76 }
77 
78 /********************************************/
79 
80 bool
81 frame::Read(std::istream& input) {
82  if (rawPreamble == 0)
83  rawPreamble = new uint8_t[PREAMBLE_SIZE_BYTES];
84  input.read(reinterpret_cast<char*>(rawPreamble), PREAMBLE_SIZE_BYTES);
85  if (input.fail())
86  return false;
87  if (rawData != 0)
88  delete [] rawData;
89  rawData = new uint8_t[DataSize_bytes()];
90  input.read(reinterpret_cast<char*>(rawData), DataSize_bytes());
91  if (input.fail())
92  return false;
93  return true;
94 }
95 
96 uint8_t
98  uint8_t st = GetNumber<uint8_t>(20u);
99  return st;
100 }
101 
102 uint32_t
104  uint32_t Nb = reinterpret_cast<uint32_t*>(rawPreamble + 20u)[0];
105  utl::BinIO::swap(Nb);
106  return Nb & 0x00FFFFFF;
107 }
108 
109 hit
110 frame::CurHit(size_t iHit) {
111  if (rawData == 0)
112  throw "frame not initialized";
113  if (iHit >= NbItems())
114  throw "Hit is out of bounds";
115  return hit(rawData + iHit * hit::SIZE_BYTES);
116 }
117 
118 void
120  if (rawPreamble == 0)
121  throw "Raw Preamble un-initialized";
122  std::cout << std::string(95, '=') << std::endl;
123  for (size_t i = 0; i < PREAMBLE_SIZE_BYTES; ++i) {
124  std::cout << std::hex << std::setfill('0') << std::setw(2) << (uint32_t) rawPreamble[i] << ' ';
125  }
126  std::cout << std::dec << std::endl;
127 }
128 
129 void
131  if (rawPreamble == 0)
132  throw "Raw Preamble un-initialized";
133  std::cout << std::string(95, '=') << std::endl;
134  std::cout << std::dec
135  << std::setfill(' ') << std::setw(8) << FrameSize()
136  << std::setw(2) << ItemType()
137  << std::setw(3) << FrameTarget()
138  << std::setfill('0') << std::setw(17) << FrameTime()
139  << std::setfill(' ') << std::setw(7) << FrameIndex()
140  << std::setfill('0') << " 0x" << std::hex << std::setw(2) << Status() << std::dec
141  << std::setfill(' ') << std::setw(9) << NbItems()
142  << std::setw(6) << DomId()
143  << std::setw(2) << StreamId()
144  << std::setw(6) << RunNumber()
145  << std::endl;
146 }
147 
148 void
150  for (size_t i = 0; i < NbItems(); ++i) {
151  std::cout << std::dec << std::setfill(' ') << std::setw(6) << i << ": ";
152  CurHit(i).HexDump();
153  }
154  std::cout << std::dec << std::endl;
155 }
156 
157 void
159  for (size_t i = 0; i < NbItems(); ++i) {
160  std::cout << std::dec << std::setfill(' ') << std::setw(6) << i << ": ";
161  CurHit(i).Dump();
162  }
163  std::cout << std::dec << std::endl;
164 }
165 
166 void
168  if (rawData == 0)
169  throw "Raw Data buffer un-initialized";
170  for (size_t i = NbItems() * hit::SIZE_BYTES; i < DataSize_bytes(); ++i) {
171  std::cout << std::hex << std::setfill('0') << std::setw(2) << (uint32_t) rawData[i] << ' ';
172  }
173  std::cout << std::dec << std::endl;
174 }
175 
176 //**********************************************/
177 
178 int cpt = 0;
180 
181 int
182 ReplacePmtId(const int dom, const int first) {
183  if (dom == 103)
184  return old2NewPmtMapCorrected2[first];
185  return old2NewPmtMapCorrected01[first];
186 }
187 
188 void
189 WriteSlice(dom& curDom) {
190  //std::cout << " runNumber " << Header.runNumber << " slice " << Header.sliceIndex
191  // << " time " << Header.sliceTime << std::endl;
192  domHeader curHeader;
193  curHeader.SetRunNumber(Header.runNumber);
194  curHeader.SetDomId(Header.domId);
195  curHeader.SetDomIndex(Header.sliceIndex);
196  curHeader.SetDomTime(Header.sliceTime);
197  std::vector<pmt> pmtData;
198  curDom.SetDomHeader(curHeader);
199  //TRandom3 rand;
200  for (int nPm = 0; nPm < pmtNb; ++nPm) {
201  if (!allHits.size()) continue;
202  std::vector<MyEvent> event;
203  event = allHits.at(nPm);
204  int eventSize = event.size();
205  //const int timeOffset = nPm*100;
206  const int timeOffset = 0;
207  pmt curPmt(ReplacePmtId(Header.domId, nPm), timeOffset);
208  std::vector<signal> pmtSignals;
209  if (!eventSize) continue;
210  for (int i = 0; i < eventSize; ++i) {
211  const long long int value = 8*event[i].timeStamp + event[i].firstBit + timeOffset;
212  //const long long int value = rand.Uniform(pow(2, 27));
213  //if ((nPm == 0) && (Header.domId == 101))
214  //if (Header.domId == 101)
215  // std::cout << nPm << ' ' << 8*event[i].timeStamp + event[i].firstBit << ' ' << event[i].firstBit
216  // << ' ' << event[i].widthPat << std::endl;
217  pmtSignals.push_back(signal(value, event[i].widthPat));
218  //if (value < 0) std::cout << value << std::endl;
219  }
220  curPmt.SetSignal(pmtSignals);
221  pmtData.push_back(curPmt);
222  pmtSignals.clear();
223  curPmt.ClearSignalData();
224  event.clear();
225  event.resize(1);
226  cpt += eventSize;
227  }
228  curDom.SetPmtsData(pmtData);
229  pmtData.clear();
230 }
231 
232 int
233 Analyse(frame& thisFrame, bool toWrite) {
234  int ichan;
235  uint32_t curTS;
236  int curFirstBit;
237  int curLastBit;
238  int curWidthPat;
239  uint32_t lastTS[pmtNb];
240  // event est un vecteur de MyEvent
241  std::vector<MyEvent> event;
242  MyEvent hitChan;
243  uint16_t curPattern;
244  int nbItems = thisFrame.NbItems();
245  //int nbItems = int((thisFrame.FrameSize() - 9)*4./6.);
246  //if (nbItems == 197379) return -1;
247  //if (nbItems == 0) return -1;
248  for(int i = 0; i < pmtNb; ++i) {
249  lastTS[i] = 0;
250  }
251  allHits.resize(pmtNb);
252  for (int it = 0; it < nbItems; ++it) {
253  if(thisFrame.CurHit(it).data == 0) {
254  return -1;
255  }
256  ichan = thisFrame.CurHit(it).Channel();
257  curTS = thisFrame.CurHit(it).TimeStamp();
258  curPattern = thisFrame.CurHit(it).Pattern();
259  if((curFirstBit = GetFirstBit(curPattern, 0)) == -10)
260  return -2;
261  if((curLastBit = GetLastBit(curPattern, curFirstBit)) == -10)
262  return -3;
263  curWidthPat = curLastBit - curFirstBit;
264 
265  // c'est la que je bidouille
266  if ((ichan > 30) || (ichan < 0)) continue;
267  if (abs((int)(lastTS[ichan] - curTS)) == 2) {
268  if (allHits.at(ichan).size() > 0) {
269  int previous = allHits.at(ichan).size() - 1;
270  allHits.at(ichan).at(previous).widthPat += curWidthPat;
271  }
272  } else {
273  hitChan.timeStamp = curTS;
274  hitChan.firstBit = curFirstBit;
275  hitChan.widthPat = curWidthPat;
276  //if (ichan == 0)
277  // std::cout << ichan << ' ' << curTS << std::endl;
278  // fill vector with structure
279  allHits.at(ichan).push_back(hitChan);
280  }
281  // save time stamp and hit number of the channel
282  lastTS[ichan] = curTS;
283  //if ((thisFrame.DomId() == 101) && (ichan == 1) && (nbItems < 100000))
284  }
285  /*
286  if (toWrite == true) {
287  if (thisFrame.DomId() == 102) {
288  for (unsigned int ch = 0; ch < allHits.size(); ++ch) {
289  for (unsigned int it = 0; it < allHits[ch].size(); ++it) {
290  const uint64_t time = uint64_t((thisFrame.FrameIndex() - 1)*pow(2, 27)) + uint64_t(timeOffsetDom[int(thisFrame.DomId()) - 101]*1e9 + fineDomOffset);
291  std::cout << time << ' ' << ch << ' '
292  << 8*allHits[ch][it].timeStamp + int(allHits[ch][it].firstBit) << ' '
293  << int(allHits[ch][it].widthPat) << std::endl;
294  }
295  }
296  }
297  }
298  */
299  return 1;
300 }
301 
302 int
303 GetFirstBit(size_t pattern, int firstBit) {
304  for (size_t i = firstBit; i < (int(sizeof(pattern))*CHAR_BIT); ++i) {
305  if ((pattern & (1 << i)) != 0) {
306  return i;
307  }
308  }
309  return -10;
310 }
311 
312 int
313 GetLastBit(size_t pattern, int firstBit) {
314  for (int i = firstBit; i < (int(sizeof(pattern))*CHAR_BIT); ++i) {
315  if ((pattern & (1 << i)) == 0) {
316  return i;
317  }
318  }
319  return -10;
320 }
321 
322 long long int
323 FillRaw(std::ifstream& str, std::vector<dom>& vSlice, long long int bitPosition, bool toWrite) {
324  cpt = 0;
325  size_t nFrames = ~0u;
326  frame curFrame;
327  int nanalysed = 0;
328  str.seekg(bitPosition);
329  long long int newPosition = bitPosition;
330  while (nFrames-- > 0) {
331  bool toFill = true;
332  if (not curFrame.Read(str))
333  break;
334  if (curFrame.StreamId() == 6) continue;
335  Header.runNumber = curFrame.RunNumber();
336  Header.domId = curFrame.DomId();
337  Header.sliceIndex = curFrame.FrameIndex();
338  const int fineDomOffset = fineTimeOffsetDom[curFrame.DomId() - 101];
339  Header.sliceTime = uint64_t((curFrame.FrameIndex() - 1)*pow(2, 27) + timeOffsetDom[curFrame.DomId() - 101]*1e9) + fineDomOffset;
340  //Header.sliceTime = uint64_t(curFrame.FrameTime());
341  if (toWrite == true) {
342  std::cout << curFrame.FrameSize() << " 0 0 " << curFrame.FrameIndex() << ' '
343  << (curFrame.FrameIndex() - 1 )*pow(2, 27) + timeOffsetDom[curFrame.DomId() - 101]*1e9 + fineDomOffset
344  //<< 8*curFrame.FrameTime()
345  << " 0 0 " << curFrame.FrameSize() << ' ' << curFrame.NbItems() << ' '
346  << curFrame.DomId() << ' ' << curFrame.StreamId() << " 0" << std::endl;
347  }
348 
349  allHits.clear();
350  allHits.resize(0);
351  switch(Analyse(curFrame, toWrite)) {
352  case 0:
353  toFill = false;
354  break;
355  case 1:
356  toFill = true;
357  break;
358  case -1:
359  std::cout << "error: empty frame" << std::endl;
360  toFill = false;
361  break;
362  case -2:
363  std::cout << "error: pb with firstBit" << std::endl;
364  toFill = false;
365  break;
366  case -3:
367  std::cout << "error: pb with lastBit" << std::endl;
368  toFill = false;
369  break;
370  default:
371  toFill = false;
372  break;
373  }
374  /*
375  if (toWrite == true) {
376  if (curFrame.DomId() == 102) {
377  for (int ch = 0; ch < allHits.size(); ++ch) {
378  for (int it = 0; it < allHits[ch].size(); ++it) {
379  std::cout << ch << ' ' << allHits[ch][it].timeStamp << ' '
380  << allHits[ch][it].firstBit << ' '
381  << allHits[ch][it].widthPat << std::endl;
382  }
383  }
384  }
385  }
386  */
387  nanalysed++;
388  newPosition = str.tellg();
389  if ((newPosition - bitPosition) > 2.0e9)
390  break;
391  if (toFill) {
392  dom curDom;
393  WriteSlice(curDom);
394  //if (int(curFrame.NbItems()) == 197379) {
395  //std::cout << int(curFrame.Status()) << std::endl;
396  vSlice.push_back(curDom);
397  //}
398  }
399  }
400  std::cerr << "number of frames analysed: "<< nanalysed
401  << " - total number of hits " << cpt << std::endl;
402  return newPosition;
403 }
404 
405 long long int
406 GetFileLength(std::ifstream& str) {
407  str.seekg(0, std::ifstream::end);
408  long long int theSize = str.tellg();
409  str.seekg(0, std::ifstream::beg);
410  return theSize;
411 }
412 
413 bool
414 IsNoisySlice(const int signalNb, const double limitNb)
415 {
416  return (signalNb > limitNb) ? true : false;
417 }
void SetDomTime(const long long int time)
set the time slice of the dom
Definition: domHeader.h:69
uint16_t StreamId()
Definition: rawReader.h:100
void Dump()
Definition: rawReader.cc:67
uint16_t Pattern()
Definition: rawReader.cc:48
void SetSignal(std::vector< signal > list)
set the signal of the pmt (to use only when reading the binary)
Definition: pmt.h:50
void SetDomIndex(const int index)
set the index of the dom slice
Definition: domHeader.h:65
void HexDump()
Definition: rawReader.cc:57
hit CurHit(size_t iHit)
Definition: rawReader.cc:110
organisation of the data - dom level
Definition: dom.h:35
int ReplacePmtId(const int dom, const int first)
Definition: rawReader.cc:182
long long int timeStamp
Definition: rawReader.h:55
uint16_t FrameTarget()
Definition: rawReader.h:94
size_t DataSize_bytes()
Definition: rawReader.h:110
uint32_t RunNumber()
Definition: rawReader.h:101
const int pmtNb
Definition: constants.h:9
uint8_t * data
Definition: rawReader.h:81
used to read raw data
Definition: rawReader.h:68
uint32_t FrameSize()
Definition: rawReader.h:92
uint32_t domId
Definition: rawReader.h:62
long long int FillRaw(std::ifstream &str, std::vector< dom > &vSlice, long long int bitPosition, bool toWrite)
Definition: rawReader.cc:323
uint32_t sliceIndex
Definition: rawReader.h:63
bool Read(std::istream &input)
Definition: rawReader.cc:81
Fill an event.
Definition: event.h:33
uint64_t sliceTime
Definition: rawReader.h:64
uint16_t DomId()
Definition: rawReader.h:99
used to read raw data
Definition: rawReader.h:84
Identify and fill the pmts with data.
Definition: pmt.h:34
void SetPmtsData(const std::vector< pmt > pmts)
set the filled pmts in the dom
Definition: dom.h:58
std::vector< std::vector< MyEvent > > allHits
Definition: rawReader.cc:17
sliceHead Header
Definition: rawReader.cc:179
uint8_t Status()
Definition: rawReader.cc:97
void SetDomId(const int nb)
set the id of the dom
Definition: domHeader.h:61
uint32_t TimeStamp()
Definition: rawReader.cc:39
uint16_t Channel()
Definition: rawReader.cc:20
void HexDumpHits()
Definition: rawReader.cc:149
const int old2NewPmtMapCorrected2[pmtNb]
Definition: constants.h:40
void SetDomHeader(const domHeader &eHeader)
set the information in the header (see domHeader class)
Definition: dom.h:43
int firstBit
Definition: rawReader.h:56
int GetLastBit(size_t pattern, int firstBit)
Definition: rawReader.cc:313
const double fineTimeOffsetDom[domNb]
Definition: constants.h:44
uint16_t Type()
Definition: rawReader.cc:30
uint16_t ItemType()
Definition: rawReader.h:93
void HexDumpTail()
Definition: rawReader.cc:167
uint32_t runNumber
Definition: rawReader.h:61
define the pmt signal
Definition: signal.h:30
uint64_t FrameTime()
Definition: rawReader.h:95
void DumpHits()
Definition: rawReader.cc:158
bool IsNoisySlice(const int signalNb, const double limitNb)
Definition: rawReader.cc:414
int cpt
Definition: rawReader.cc:178
uint32_t NbItems()
Definition: rawReader.cc:103
long long int GetFileLength(std::ifstream &str)
Definition: rawReader.cc:406
void SetRunNumber(const int nb)
set the id of the run
Definition: domHeader.h:57
int widthPat
Definition: rawReader.h:57
static const size_t PREAMBLE_SIZE_BYTES
Definition: rawReader.h:87
void WriteSlice(dom &curDom)
Definition: rawReader.cc:189
Fill a dom header from a binary file.
Definition: domHeader.h:30
int Analyse(frame &thisFrame, bool toWrite)
Definition: rawReader.cc:233
void HexDumpPreamble()
Definition: rawReader.cc:119
void DumpPreamble()
Definition: rawReader.cc:130
static const size_t SIZE_BYTES
Definition: rawReader.h:71
int GetFirstBit(size_t pattern, int firstBit)
Definition: rawReader.cc:303
const int old2NewPmtMapCorrected01[pmtNb]
Definition: constants.h:38
uint32_t FrameIndex()
Definition: rawReader.h:96
void ClearSignalData()
clear the pmt signal before to fill it again
Definition: pmt.h:58
const int timeOffsetDom[domNb]
Definition: constants.h:43