GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
THILDataParser.cxx
Go to the documentation of this file.
1#include "THILDataParser.h"
3
4#include <bitset>
5
6#include "TChannel.h"
7#include "Globals.h"
8
9#include "TScalerQueue.h"
10
11#include "TEpicsFrag.h"
12#include "TParsingDiagnostics.h"
13
14#include "Rtypes.h"
15
16#include "THILFragment.h"
17#include "TBadFragment.h"
18
24
28
29int THILDataParser::Process(std::shared_ptr<TRawEvent> rawEvent)
30{
31 /// Process this TRlmdEvent using the provided data parser.
32 /// Returns the total number of fragments read (good and bad).
33 // right now the parser only returns the total number of fragments read
34 // so we assume (for now) that all fragments are good fragments
35 int eventsProcessed = 0;
36 std::shared_ptr<TRlmdEvent> rlmdEvent = std::static_pointer_cast<TRlmdEvent>(rawEvent);
37 uint32_t size = rlmdEvent->GetDataSize();
38
39 try {
40 eventsProcessed += EagleEventToFragment(size, reinterpret_cast<uint16_t*>(rlmdEvent->GetData()));
41 for(int i = 0; i < eventsProcessed; ++i) rawEvent->IncrementGoodFrags();
42 } catch(THILDataParserException& e) {
43 eventsProcessed = -e.GetFailedWord();
44 if(!TGRSIOptions::Get()->SuppressErrors()) {
45 if(!TGRSIOptions::Get()->LogErrors()) {
46 std::cout << std::endl
47 << e.what();
48 }
49 }
50 }
51
54
55 return eventsProcessed;
56}
57
58int THILDataParser::EagleEventToFragment(uint32_t size, uint16_t* data)
59{
60 std::shared_ptr<THILFragment> eventFrag;
61 int nofEvents = 0;
62
63 // all data needs to be byte swapped!
64 // 0 - length of used data
65 uint16_t usedData = ByteSwap(data[0]);
66 if(usedData % 2 == 1) {
68 }
69 // convert used data from bytes to 16bit words
70 usedData /= 2;
71 // 1 - always 0x0200 w/o byte swapping
72 if(data[1] != 0x0200) {
74 }
75 // 2+3 - buffer number
76 uint32_t bufferNumber = ByteSwap(data[2]);
77 bufferNumber = (bufferNumber << 16) | ByteSwap(data[3]);
78 uint16_t eventLength = 0;
79 uint16_t eventId = 0;
80 uint32_t acquisitionClk = 0;
81 uint32_t cpuClk = 0;
82 uint32_t eventNumber = 0;
83 uint64_t eventTime = 0;
84 uint16_t pattern = 0;
85 uint16_t usTime = 0;
86 std::bitset<16> bitPattern;
87 std::vector<uint8_t> geId;
88 std::vector<uint16_t> geTime;
89 std::vector<uint16_t> geEnergy;
90 std::vector<uint8_t> siId;
91 std::vector<uint16_t> siEnergy;
92 //std::ofstream debugFile("debug.txt", std::ios::app);
93 //debugFile<<std::hex;
94 for(size_t i = 4; i < usedData && i < size;) { // incrementation is done within the loop
95 //debugFile<<"Parsing buffer from 0x"<<i<<" to 0x"<<usedData<<std::endl;
96 // 4+x - length should not be zero
97 eventLength = ByteSwap(data[i++]);
98 if(eventLength == 0) {
100 }
101 if(eventLength % 2 == 1) {
103 }
104 // 5+x - event id
105 eventId = ByteSwap(data[i++]);
106 if(eventId == 0x0001) {
107 acquisitionClk = ByteSwap(data[i++]);
108 acquisitionClk = (acquisitionClk << 16) | ByteSwap(data[i++]);
109 cpuClk = ByteSwap(data[i++]);
110 cpuClk = (cpuClk << 16) | ByteSwap(data[i++]);
111 // we advanced i 6 times so the event length should be 12 and we are done with this event
112 if(eventLength != 0x000c) {
114 }
115 //debugFile<<"acquisitionClk 0x"<<acquisitionClk<<", cpuClk 0x"<<cpuClk<<std::endl;
116 continue;
117 }
118 eventLength /= 2; // convert from bytes to 16bit words
119 // for all other IDs we only care about the high nibble
120 // we already read two 16bit words of the event
121 switch((eventId >> 8) & 0xff) {
122 case 0x11: // sync
123 //debugFile<<"found sync pattern, moving index from "<<i;
124 i += eventLength - 2;
125 //debugFile<<" to "<<i<<std::endl;
126 break;
127 case 0x12: // scaler ??
128 //debugFile<<"found scaler pattern, moving index from "<<i;
129 i += eventLength - 2;
130 //debugFile<<" to "<<i<<std::endl;
131 break;
132 case 0x70:
133 //debugFile<<"found 0x70 pattern, moving index from "<<i;
134 i += eventLength - 2;
135 //debugFile<<" to "<<i<<std::endl;
136 break;
137 case 0x74:
138 //debugFile<<"found 0x74 pattern, moving index from "<<i;
139 i += eventLength - 2;
140 //debugFile<<" to "<<i<<std::endl;
141 break;
142 case 0x75: // CEFE bit pattern
143 eventNumber = ByteSwap(data[i++]);
144 eventNumber = (eventNumber << 16) | ByteSwap(data[i++]);
145 eventTime = ByteSwap(data[i++]);
146 eventTime = (eventTime << 16) | ByteSwap(data[i++]);
147 eventTime = (eventTime << 16) | ByteSwap(data[i++]);
148 eventTime = (eventTime << 16) | ByteSwap(data[i++]);
149 // CEFE 0
150 // pattern and time in microseconds
151 pattern = ByteSwap(data[i++]);
152 if((pattern & 0xdf00) != 0x5500) {
154 }
155 usTime = ByteSwap(data[i++]);
156 // pattern and hit pattern (plus loop for energies, ids, and times)
157 pattern = ByteSwap(data[i++]);
158 if((pattern & 0xdf00) != 0x5500) {
160 }
161 //debugFile<<eventNumber<<": time 0x"<<eventTime<<" microseconds 0x"<<usTime<<" pattern 0x"<<pattern<<" at "<<i-1;
162 bitPattern = ByteSwap(data[i++]);
163 //debugFile<<" bit pattern 0x"<<bitPattern.to_ulong()<<" at "<<i-1<<std::endl;
164 if(bitPattern.any()) {
165 //debugFile<<"found Ge ";
166 for(int ch = 0; ch < 16; ++ch) {
167 if(bitPattern.test(ch)) {
168 //debugFile<<ch<<" ";
169 // for each bit that is set we should find one energy word and a combined ID and time word
170 // each preceded by a "pattern word"
171 pattern = ByteSwap(data[i++]);
172 if((pattern & 0xdf00) != 0x5500) {
174 }
175 geEnergy.push_back(ByteSwap(data[i++]));
176 pattern = ByteSwap(data[i++]);
177 if((pattern & 0xdf00) != 0x5500) {
179 }
180 geId.push_back((ByteSwap(data[i]) >> 12) & 0xf);
181 geTime.push_back(ByteSwap(data[i++]) & 0xfff);
182 }
183 }
184 //debugFile<<std::endl;
185 }
186 // CEFE 1
187 pattern = ByteSwap(data[i++]);
188 if((pattern & 0xdf00) != 0x5500) {
190 }
191 bitPattern = ByteSwap(data[i++]);
192 if(bitPattern.any()) {
193 //debugFile<<"found Si1 ";
194 for(int ch = 0; ch < 16; ++ch) {
195 if(bitPattern.test(ch)) {
196 //debugFile<<ch<<" ";
197 // for each bit that is set we should find one combined ID and energy word
198 pattern = ByteSwap(data[i++]);
199 if((pattern & 0xdf00) != 0x5500) {
201 }
202 siId.push_back((ByteSwap(data[i]) >> 12) & 0xf);
203 siEnergy.push_back(ByteSwap(data[i++]) & 0xfff);
204 }
205 }
206 //debugFile<<std::endl;
207 }
208 // CEFE 2
209 pattern = ByteSwap(data[i++]);
210 if((pattern & 0xdf00) != 0x5500) {
212 }
213 bitPattern = ByteSwap(data[i++]);
214 if(bitPattern.any()) {
215 //debugFile<<"found Si2 ";
216 for(int ch = 0; ch < 16; ++ch) {
217 if(bitPattern.test(ch)) {
218 //debugFile<<ch<<" ";
219 // for each bit that is set we should find one combined ID and energy word
220 pattern = ByteSwap(data[i++]);
221 if((pattern & 0xdf00) != 0x5500) {
223 }
224 siId.push_back(((ByteSwap(data[i]) >> 12) & 0xf) + 16);
225 siEnergy.push_back(ByteSwap(data[i++]) & 0xfff);
226 }
227 }
228 //debugFile<<std::endl;
229 }
230 // CEFE 3
231 pattern = ByteSwap(data[i++]);
232 if((pattern & 0xdf00) != 0x5500) {
234 }
235 bitPattern = ByteSwap(data[i++]);
236 if(bitPattern.any()) {
237 //debugFile<<"found Si3 ";
238 for(int ch = 0; ch < 16; ++ch) {
239 if(bitPattern.test(ch)) {
240 //debugFile<<ch<<" ";
241 // for each bit that is set we should find one combined ID and energy word
242 pattern = ByteSwap(data[i++]);
243 if((pattern & 0xdf00) != 0x5500) {
245 }
246 siId.push_back(((ByteSwap(data[i]) >> 12) & 0xf) + 32);
247 siEnergy.push_back(ByteSwap(data[i++]) & 0xfff);
248 }
249 }
250 //debugFile<<std::endl;
251 }
252 //debugFile<<geId.size()<<" Ge and "<<siId.size()<<" Si"<<std::endl;
253 //debugFile<<"# Ge# GeEn GeTime Si# SiEn"<<std::endl;
254 //for(size_t h = 0; h < geId.size(); ++h) {
255 // if(h < siId.size()) {
256 // debugFile<<std::setw(2)<<h<<" "<<std::setw(3)<<(int)geId.at(h)<<" "<<std::setw(4)<<geEnergy.at(h)<<" "<<std::setw(6)<<geTime.at(h)<<" "<<std::setw(3)<<(int)siId.at(h)<<" "<<std::setw(4)<<siEnergy.at(h)<<std::endl;
257 // } else {
258 // debugFile<<std::setw(2)<<h<<" "<<std::setw(3)<<(int)geId.at(h)<<" "<<std::setw(4)<<geEnergy.at(h)<<" "<<std::setw(6)<<geTime.at(h)<<std::endl;
259 // }
260 //}
261 //debugFile<<"============================"<<std::endl;
262
263 eventFrag = std::make_shared<THILFragment>();
264 // default address as this event contains multiple hits with different addresses
265 // but this makes the unpacking loop create the right detector
266 eventFrag->SetAddress(0x100);
267 eventFrag->GermaniumId(geId);
268 eventFrag->GermaniumEnergy(geEnergy);
269 eventFrag->GermaniumTime(geTime);
270 eventFrag->SiliconId(siId);
271 eventFrag->SiliconEnergy(siEnergy);
272 //eventFrag->AcquisitionClock(acquisitionClk);
273 //eventFrag->CpuClock(cpuClk);
274 eventFrag->EventNumber(eventNumber);
275 eventFrag->EventTime(eventTime);
276 eventFrag->MicrosecondTime(usTime);
277 if(eventFrag->Good()) {
278 Push(GoodOutputQueues(), eventFrag);
279 ++nofEvents;
280 } else {
281 Push(*BadOutputQueue(), std::make_shared<TBadFragment>(*eventFrag));
282 }
283 if(RecordDiag()) {
284 TParsingDiagnostics::Get()->GoodFragment(eventFrag->GetDetectorType());
285 }
286
287 geId.clear();
288 geEnergy.clear();
289 geTime.clear();
290 siId.clear();
291 siEnergy.clear();
292 break;
293 case 0xaa: // start
294 //debugFile<<"found start pattern, moving index from "<<i;
295 i += eventLength - 2;
296 //debugFile<<" to "<<i<<std::endl;
297 break;
298 default:
300 break;
301 };
302 }
303 //debugFile.close();
304
305 return nofEvents;
306}
std::vector< std::shared_ptr< ThreadsafeQueue< std::shared_ptr< const TFragment > > > > & GoodOutputQueues()
void DecrementInputSize()
virtual std::shared_ptr< ThreadsafeQueue< std::shared_ptr< const TBadFragment > > > & BadOutputQueue()
Definition TDataParser.h:73
void Push(ThreadsafeQueue< std::shared_ptr< const TBadFragment > > &queue, const std::shared_ptr< TBadFragment > &frag)
bool RecordDiag() const
void IncrementItemsPopped()
static TGRSIOptions * Get(int argc=0, char **argv=nullptr)
Do not use!
const char * what() const noexcept override
EDataParserState fState
uint16_t ByteSwap(const uint16_t &val)
int EagleEventToFragment(uint32_t size, uint16_t *data)
int Process(std::shared_ptr< TRawEvent >) override
void GoodFragment(const std::shared_ptr< const TFragment > &)
static TParsingDiagnostics * Get(bool verbose=false)
Definition TSingleton.h:33