AlcapDAQ  1
TOctalFADCBankReader.cpp
Go to the documentation of this file.
1 #include "TOctalFADCBankReader.h"
2 #include "midas.h"
3 #include <stdio.h>
4 #include <algorithm>
5 
6 
7 using std::string;
8 using std::vector;
9 
11  : fBankName(bankname)
12 {
13 }
14 
15 void TOctalFADCBankReader::ProcessEvent(EVENT_HEADER* pheader, void *pevent)
16 {
17  ClearEvent();
18 
19  unsigned char* raw; // Points at the raw data, one byte at a time.
20  int bankSize = bk_locate(pevent,fBankName.c_str(),&raw);
21  int nBlocks = bankSize / kDataBlockSize;
22 
23  vector<int> islandSamples;
24  islandSamples.reserve(kSamplesArrayInitialSize);
25 
26  int islandTimestamp = 0; // time of first 4-sample block in stitched island
27  int lastTimestamp = 0; // To see the end of the last island.
28  bool firstIsland = true; // Beginning of first island is not end of an island
29  for(int i=0; i < nBlocks; i++){
30  // data format:
31  //
32  // bits
33  // 79 FADC 0 / 1
34  // 78-52 timestamp
35  // 51-48 overflow
36  // 47-36 sampleB0
37  // 35-24 sampleA0
38  // 23-12 sampleB1
39  // 11-0 sampleA1
40  //
41  // Island samples in the order A1 B1 A0 B0
42 
43  int timestamp = ((raw[i*10+0] & 0x7f) << 20) |
44  (raw[i*10+1] << 12) |
45  (raw[i*10+2] << 4) |
46  (raw[i*10+3] >> 4);
47  bool overflowB0 = ((raw[i*10+3] & 0x08) != 0);
48  bool overflowA0 = ((raw[i*10+3] & 0x04) != 0);
49  bool overflowB1 = ((raw[i*10+3] & 0x02) != 0);
50  bool overflowA1 = ((raw[i*10+3] & 0x01) != 0);
51  int sampleB0 = (overflowB0 << 12) |
52  (raw[i*10+4] << 4) |
53  (raw[i*10+5] >> 4);
54  int sampleA0 = (overflowA0 << 12) |
55  ((raw[i*10+5] & 0xf) << 8) |
56  (raw[i*10+6]);
57  int sampleB1 = (overflowB1 << 12) |
58  (raw[i*10+7] << 4) |
59  (raw[i*10+8] >> 4);
60  int sampleA1 = (overflowA1 << 12) |
61  ((raw[i*10+8] & 0xf) << 8) |
62  (raw[i*10+9]);
63 
64  if(timestamp != lastTimestamp + 1) {
65  if(!firstIsland) {
66  if(islandSamples.size() > 4) {
67  // This is a new island, so put the old
68  // island on fData and start again.
69  // Remember, time stamps are actually 4 samples long
70  int channel = fBankName.at(1) - 'a';
71  fData.push_back(TOctalFADCIsland(islandTimestamp*4,islandSamples,channel));
72 
73 
74 #if 0
75  printf("Time = %d, nsamples = %d, char = %c , channel = %d\n",
76  islandTimestamp, islandSamples.size(),fBankName.at(1),channel);
77  for(int k=0; k<islandSamples.size(); k++){
78  printf("%d ",islandSamples[k]);
79  }
80  printf("\n");
81 #endif
82  islandSamples.clear();
83  }
84  // Ignore islands with 4 or less samples.
85  }
86  firstIsland = false;
87  islandTimestamp = timestamp;
88  }
89  lastTimestamp = timestamp;
90 
91 #if 0
92  for(int k=0; k<10; k++){
93  printf("%x ",raw[i*10 + k]);
94  }
95  printf("\n");
96  printf("fadc=%x t=%2x %2x %2x %2x %2x\n", i, timestamp,
97  sampleA1, sampleB1, sampleA0,sampleB0);
98 #endif
99 
100  islandSamples.push_back(sampleA1);
101  islandSamples.push_back(sampleB1);
102  islandSamples.push_back(sampleA0);
103  islandSamples.push_back(sampleB0);
104  }
105 
106  if(fData.size()>1)
107  {
108  //Time sort islands
109  std::sort(fData.begin(), fData.end(),TOctalFADCIsland::TimeSortFADCIslands());
110  StitchIslands();
111  } // Some Pulses are split over two separate islands. Put them together again
112  // printf(".............. FADC islands sorted .............\n");
113 }
114 
116 {
117 
118  vector<TOctalFADCIsland> fData_temp;
119  //fData_temp.clear();
120 
121  TOctalFADCIsland last = fData.at(0);
122  //if(fData.size()>1){printf("Size first Island: %d\n",fData.at(1).GetNSamples());}
123  fData_temp.push_back(last);
124  int counter_temp = 0; int counter = 0;
125 
126  for(int i = 1; i<fData.size(); i++)
127  {
128  //if(fData.at(i).GetFADCChannel()==2){printf("Time new = %d, End time last = %d, Difference = %d, Size new = %d, Size old =%d\n",fData.at(i).GetTime(),last.GetTime() + last.GetNSamples(),fData.at(i).GetTime() - (last.GetTime() + last.GetNSamples()),fData.at(i).GetNSamples(),last.GetNSamples());}
129  if(fData.at(i).GetTime() - (last.GetTime() + last.GetNSamples()) >2 )
130  {
131  last = fData.at(i);
132  fData_temp.push_back(last);
133  counter_temp++;
134  //printf("OK\n");
135  }
136  else
137  {
138  vector<int>& samples = fData_temp.at(counter_temp).GetSampleVector();
139  //int timestamp = fData_temp.at(counter_temp).GetTime();
140  //int channel = fData_temp.at(counter_temp).GetFADCChannel();
141  //if(fData.at(i).GetFADCChannel()==2){printf("Stitch!!! : Size samples before = %d",samples.size());}
142 
143  for(int j=0;j<fData.at(i).GetNSamples();j++)
144  {
145  samples.push_back(fData.at(i).GetSample(j));
146  }
147  //if(fData.at(i).GetFADCChannel()==2){printf(" Size samples after = %d \n",samples.size());}
148  //fData_temp.erase(fData_temp.end());
149  //fData_temp.push_back(TOctalFADCIsland(timestamp,samples,channel));
150  counter ++;
151  last = fData_temp.at(counter_temp);
152  }
153  }
154 
155 //printf("Size original: %d, size stitched: %d, counter %d\n",fData.size(),fData_temp.size(),counter);
156 
157 fData.clear();
158 fData = fData_temp;
159 fData_temp.clear();
160 
161 //printf("Size new: %d, size temp: %d, counter %d , counter_temp %d\n",fData.size(),fData_temp.size(),counter,counter_temp);
162 
163 }