Bug Summary

File:libraries/DAQ/DEVIOWorkerThread.cc
Location:line 359, column 26
Description:Value stored to 'iend' is never read

Annotated Source Code

1/// $Id$
2//
3// File: DEVIOWorkerThread.cc
4// Created: Mon Mar 28 07:40:07 EDT 2016
5// Creator: davidl (on Darwin harriet.jlab.org 13.4.0 i386)
6//
7
8#include <unistd.h>
9
10#include "DEVIOWorkerThread.h"
11#include "JEventSource_EVIOpp.h"
12#include "LinkAssociations.h"
13
14#include <swap_bank.h>
15
16using namespace std;
17using namespace std::chrono;
18
19
20
21//---------------------------------
22// DEVIOWorkerThread (Constructor)
23//---------------------------------
24DEVIOWorkerThread::DEVIOWorkerThread(
25 JEventSource_EVIOpp *event_source
26 ,list<DParsedEvent*> &parsed_events
27 ,uint32_t &MAX_PARSED_EVENTS
28 ,mutex &PARSED_EVENTS_MUTEX
29 ,condition_variable &PARSED_EVENTS_CV
30 ):
31 event_source(event_source)
32 ,parsed_events(parsed_events)
33 ,MAX_PARSED_EVENTS(MAX_PARSED_EVENTS)
34 ,PARSED_EVENTS_MUTEX(PARSED_EVENTS_MUTEX)
35 ,PARSED_EVENTS_CV(PARSED_EVENTS_CV)
36 ,done(false)
37 ,thd(&DEVIOWorkerThread::Run,this)
38{
39 // n.b. in principal, the worker thread is started when the
40 // above constructor is hit and so may already be in Run()
41 // before executing anything below. The "done" variable is
42 // therefore initialized first to guarantee that if that
43 // happens, it gets to the cv.wait() call where it will wait
44 // for someone to notify it. That won't happen before this
45 // constructor completes so we do the remaining initializations
46 // below.
47
48 VERBOSE = 1;
49 Nrecycled = 0; // Incremented in JEventSource_EVIOpp::Dispatcher()
50 MAX_EVENT_RECYCLES = 1000; // In EVIO events (not L1 trigger events!) overwritten in JEventSource_EVIOpp constructor
51 MAX_OBJECT_RECYCLES = 1000; // overwritten in JEventSource_EVIOpp constructor
52 run_number_seed = 0; // Set in JEventSource_EVIOpp constructor
53
54 in_use = false;
55 jobtype = JOB_NONE;
56
57 buff_len = 100; // this will grow as needed
58 buff = new uint32_t[buff_len];
59
60 PARSE_F250 = true;
61 PARSE_F125 = true;
62 PARSE_F1TDC = true;
63 PARSE_CAEN1290TDC = true;
64 PARSE_CONFIG = true;
65 PARSE_BOR = true;
66 PARSE_EPICS = true;
67 PARSE_EVENTTAG = true;
68 PARSE_TRIGGER = true;
69
70 LINK_TRIGGERTIME = true;
71}
72
73//---------------------------------
74// ~DEVIOWorkerThread (Destructor)
75//---------------------------------
76DEVIOWorkerThread::~DEVIOWorkerThread()
77{
78 if(buff) delete[] buff;
79 for(auto pe : parsed_event_pool) delete pe;
80}
81
82//---------------------------------
83// Run
84//---------------------------------
85void DEVIOWorkerThread::Run(void)
86{
87 unique_lock<std::mutex> lck(mtx);
88
89 // Loop waiting for jobs or until told to quit
90 while(!done){
91
92 cv.wait_for(lck, std::chrono::milliseconds(1));
93
94 // In principle, in_use should never be false with a jobtype!=JOB_NONE
95 // In practice, this has happened, possibly due to compiler optimization
96 // reordering things in JEventSource_EVIOpp::Dispatcher. That led to
97 // attempting to process a buffer that was being written to. Avoid that
98 // condition by checking the in_use flag is really set.
99 if( !in_use ) continue;
100
101 try {
102
103 if( jobtype & JOB_SWAP ) swap_bank(buff, buff, swap32(buff[0])( (((buff[0]) >> 24) & 0x000000FF) | (((buff[0]) >>
8) & 0x0000FF00) | (((buff[0]) << 8) & 0x00FF0000
) | (((buff[0]) << 24) & 0xFF000000) )
+1 );
104
105 if( jobtype & JOB_FULL_PARSE ) MakeEvents();
106
107 if( jobtype & JOB_ASSOCIATE ) LinkAllAssociations();
108
109 if( !current_parsed_events.empty() ) PublishEvents();
110
111 } catch (exception &e) {
112 jerr << e.what() << endl;
113 for(auto pe : parsed_event_pool) delete pe; // delete all parsed events any any objects they hold
114 parsed_event_pool.clear();
115 current_parsed_events.clear(); // (these are also in parsed_event_pool so were already deleted)
116 //exit(-1);
117 }
118
119 // Reset and mark us as available for use
120 jobtype = JOB_NONE;
121 in_use = false;
122
123 if( jobtype & JOB_QUIT ) break;
124 }
125
126 in_use = false;
127}
128
129//---------------------------------
130// Finish
131//---------------------------------
132void DEVIOWorkerThread::Finish(bool wait_to_complete)
133{
134 /// Set the done flag so that the worker thread
135 /// will exit once it is done processing its current
136 /// job. The thread is notified to wake up in case
137 /// it is currently idle. If the wait_to_complete
138 /// flag is set (default), then the worker thread is
139 /// joined to guarantee the current job's processing
140 /// is completed before returning.
141 done = true;
142 cv.notify_all();
143 if(wait_to_complete) {
144 thd.join();
145 } else {
146 thd.detach();
147 }
148}
149
150//---------------------------------
151// Prune
152//---------------------------------
153void DEVIOWorkerThread::Prune(void)
154{
155 /// Delete any DParsedEvent objects not currently in use.
156 /// If the DParsedEvent object pool and their internal
157 /// hit object pools are allowed to continuously grow, it
158 /// will appear as a though there is a memory leak. Occasional
159 /// pruning will reduce the average memory footprint.
160 /// This is called from MakeEvents() every MAX_EVENT_RECYCLES
161 /// EVIO events processed by this worker thread.
162 /// Note that this is in EVIO events (i.e. possibly a block
163 /// of events) not in L1 trigger events.
164 ///
165 /// NOTE: We currently do NOT reduce the size of buff
166 /// here if it is too big. We may wish to do that at some point!
167
168 // Delete extra parsed events
169 vector<DParsedEvent*> tmp_events = parsed_event_pool;
170 parsed_event_pool.clear();
171 for(auto pe : tmp_events) {
172 if(pe->in_use)
173 parsed_event_pool.push_back(pe);
174 else
175 delete pe;
176
177 }
178}
179
180//---------------------------------
181// MakeEvents
182//---------------------------------
183void DEVIOWorkerThread::MakeEvents(void)
184{
185
186 /// Make DParsedEvent objects from data currently in buff.
187 /// This will look at the begining of the EVIO event to see
188 /// how many L1 events are in it. It will then grab that many
189 /// DParsedEvent objects from this threads pool , or create
190 /// new ones and add them all to the current_parsed_events
191 /// vector. These are then filled out later as the data is
192 /// parsed.
193
194 if(!current_parsed_events.empty()) throw JException("Attempting call to DEVIOWorkerThread::MakeEvents when current_parsed_events not empty!!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__194);
195
196 uint32_t *iptr = buff;
197
198 uint32_t M = 1;
199 uint64_t event_num = 0;
200
201 iptr++;
202 uint32_t mask = 0xFF001000;
203 if( ((*iptr)&mask) == mask ){
204 // Physics event
205 M = *(iptr)&0xFF;
206 uint64_t eventnum_lo = iptr[4];
207 uint64_t eventnum_hi = iptr[5];
208 event_num = (eventnum_hi<<32) + (eventnum_lo);
209 }
210
211 // Try and get M DParsedEvent objects from this thread's pool.
212 for(auto pe : parsed_event_pool){
213 if(pe->in_use) continue;
214 current_parsed_events.push_back(pe);
215 if( current_parsed_events.size() >= M ) break;
216 }
217
218 // Create new DParsedEvent objects if needed
219 while( current_parsed_events.size() < M ){
220 DParsedEvent *pe = new DParsedEvent(MAX_OBJECT_RECYCLES);
221 current_parsed_events.push_back(pe);
222 parsed_event_pool.push_back(pe);
223 }
224
225 // Set indexes for the parsed event objects
226 // and flag them as being in use.
227 for(auto pe : current_parsed_events){
228
229 pe->Clear(); // return previous event's objects to pools and clear vectors
230 pe->istreamorder = istreamorder;
231 pe->run_number = run_number_seed;
232 pe->event_number = event_num++;
233 pe->sync_flag = false;
234 pe->in_use = true;
235 pe->copied_to_factories = false;
236 pe->event_status_bits = 0;
237 pe->borptrs = NULL__null; // may be set by either ParseBORbank or JEventSource_EVIOpp::GetEvent
238 }
239
240 // Parse data in buffer to create data objects
241 ParseBank();
242
243 // Occasionally prune extra DParsedEvent objects as well as objects
244 // from the existing pools to reduce average memory usage. We do
245 // this after parsing so that not everything is deleted (objects
246 // being used this event will be returned to the pools later.)
247 if(++Nrecycled%MAX_EVENT_RECYCLES == 0) Prune();
248 for(auto pe : current_parsed_events){
249 if( ++pe->Nrecycled%pe->MAX_RECYCLES == 0) pe->Prune();
250 }
251}
252
253//---------------------------------
254// PublishEvents
255//---------------------------------
256void DEVIOWorkerThread::PublishEvents(void)
257{
258 /// Copy our "current_parsed_events" pointers into the global "parsed_events"
259 /// list making them available for consumption.
260
261 // Lock mutex so other threads can't modify parsed_events
262 unique_lock<mutex> lck(PARSED_EVENTS_MUTEX);
263
264 // Make sure we don't exceed the maximum number of simultaneous
265 // parsed events. If the done flag is set, go ahead and add
266 // this regardless
267 while( ((current_parsed_events.size()+parsed_events.size())>=MAX_PARSED_EVENTS) && !done ){
268 event_source->NPARSER_STALLED++;
269 PARSED_EVENTS_CV.wait_for(lck, std::chrono::milliseconds(1));
270 }
271
272 // Loop over all elements of parsed_events and insert
273 // these based on istreamorder so that the front element
274 // is the most recent.
275 bool inserted = false;
276 for(auto it = parsed_events.begin(); it!=parsed_events.end(); it++){
277 if( istreamorder < (*it)->istreamorder ){
278 parsed_events.insert(it, current_parsed_events.begin(), current_parsed_events.end());
279 inserted = true;
280 break;
281 }
282 }
283
284 // In case this should go at end of list
285 if(!inserted) parsed_events.insert(parsed_events.end(), current_parsed_events.begin(), current_parsed_events.end());
286
287 lck.unlock();
288 PARSED_EVENTS_CV.notify_all();
289
290 // Any events should now be published
291 current_parsed_events.clear();
292}
293
294//---------------------------------
295// ParseBank
296//---------------------------------
297void DEVIOWorkerThread::ParseBank(void)
298{
299
300 uint32_t *iptr = buff;
301 uint32_t *iend = &buff[buff[0]+1];
302
303 while(iptr < iend){
304 uint32_t event_len = iptr[0];
305 uint32_t event_head = iptr[1];
306 uint32_t tag = (event_head >> 16) & 0xFFFF;
307
308// _DBG_ << "0x" << hex << (uint64_t)iptr << dec << ": event_len=" << event_len << "tag=" << hex << tag << dec << endl;
309
310 switch(tag){
311 case 0x0060: ParseEPICSbank(iptr, iend); break;
312 case 0x0070: ParseBORbank(iptr, iend); break;
313
314 case 0xFFD0:
315 case 0xFFD1:
316 case 0xFFD2:
317 case 0xFFD3: ParseControlEvent(iptr, iend); break;
318
319 case 0xFF58:
320 case 0xFF78: current_parsed_events.back()->sync_flag = true;
321 case 0xFF50:
322 case 0xFF70: ParsePhysicsBank(iptr, iend); break;
323
324 default:
325 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<325<<" "
<< "Unknown outer EVIO bank tag: " << hex << tag << dec << endl;
326 iptr = &iptr[event_len+1];
327 if(event_len<1) iptr = iend;
328 }
329 }
330}
331
332//---------------------------------
333// ParseEventTagBank
334//---------------------------------
335void DEVIOWorkerThread::ParseEventTagBank(uint32_t* &iptr, uint32_t *iend)
336{
337 iptr = &iptr[(*iptr) + 1];
338}
339
340//---------------------------------
341// ParseEPICSbank
342//---------------------------------
343void DEVIOWorkerThread::ParseEPICSbank(uint32_t* &iptr, uint32_t *iend)
344{
345 if(!PARSE_EPICS){ iptr = iend; return; }
346
347 time_t timestamp=0;
348
349 // Outer bank
350 uint32_t *istart = iptr;
351 uint32_t epics_bank_len = *iptr++;
352 if(epics_bank_len < 1){
353 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<353<<" "
<< "bank_len<1 in EPICS event!" << endl;
354 iptr = iend;
355 return;
356 }
357
358 uint32_t *iend_epics = &iptr[epics_bank_len];
359 if( iend_epics < iend ) iend = iend_epics;
Value stored to 'iend' is never read
360
361 // Advance to first daughter bank
362 iptr++;
363
364 // Get pointer to first DParsedEvent
365 DParsedEvent *pe = current_parsed_events.front();
366 pe->event_status_bits |= (1<<kSTATUS_EPICS_EVENT);
367
368 // Loop over daughter banks
369 while( iptr < iend_epics ){
370
371 uint32_t bank_len = (*iptr)&0xFFFF;
372 uint32_t tag = ((*iptr)>>24)&0xFF;
373 iptr++;
374
375 if(tag == 0x61){
376 // timestamp bank
377 timestamp = *iptr;
378 }else if(tag == 0x62){
379 // EPICS data value
380 string nameval = (const char*)iptr;
381 pe->NEW_DEPICSvalue(timestamp, nameval);
382 }else{
383 // Unknown tag. Bail
384 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<384<<" "
<< "Unknown tag 0x" << hex << tag << dec << " in EPICS event!" <<endl;
385 DumpBinary(istart, iend_epics, 32, &iptr[-1]);
386 }
387
388 iptr = &iptr[bank_len];
389 }
390
391 iptr = iend_epics;
392}
393
394//---------------------------------
395// ParseBORbank
396//---------------------------------
397void DEVIOWorkerThread::ParseBORbank(uint32_t* &iptr, uint32_t *iend)
398{
399 /// Create BOR config objects from the EVIO bank and store them in
400 /// the event (should only be one since BOR events are not entangled).
401 /// These objects will eventually be inherited by the JEventSource_EVIOpp
402 /// object and passed to all subsequent events.
403
404 // Upon entry, iptr should point to length word of a bank of banks with tag=0x70
405 // indicating BOR event. Each bank contained within will represent one crate and
406 // will be a bank with tag=0x71 and num the rocid, containing tagsegments. Each tagsegment
407 // represents a single module with the tag containing the module type (bits 0-4) and
408 // slot (bits 5-10). The data in the tagsegments is uint32_t and maps to a data
409 // structure in bor_roc.h depending on the module type. Below is a summary of
410 // how this looks in memory:
411 //
412 // BOR event length
413 // BOR header
414 // crate bank length
415 // crate header
416 // module bank len/header
417 // module data ...
418 // module bank len/header
419 // module data ...
420 // ...
421 // crate bank length
422 // crate header
423 // ...
424
425 if(!PARSE_BOR){ iptr = &iptr[(*iptr) + 1]; return; }
426
427 // Make sure there is exactly 1 event in current_parsed_events
428 if(current_parsed_events.size() != 1){
429 stringstream ss;
430 ss << "DEVIOWorkerThread::ParseBORbank called for EVIO event with " << current_parsed_events.size() << " events in it. (Should be exactly 1!)";
431 throw JException(ss.str(), __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__431);
432 }
433
434 // Create new DBORptrs object and set pointer to it in DParsedEvent
435 // (see JEventSource_EVIOpp::GetEvent)
436 DParsedEvent *pe = current_parsed_events.front();
437 pe->event_status_bits |= (1<<kSTATUS_BOR_EVENT);
438 pe->borptrs = new DBORptrs();
439 DBORptrs* &borptrs = pe->borptrs;
440
441 // Make sure we have full event
442 uint32_t borevent_len = *iptr++;
443 uint32_t bank_len = (uint32_t)((uint64_t)iend - (uint64_t)iptr)/sizeof(uint32_t);
444 if(borevent_len > bank_len){
445 stringstream ss;
446 ss << "BOR: Size of bank doesn't match amount of data given (" << borevent_len << " > " << bank_len << ")";
447 throw JException(ss.str(), __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__447);
448 }
449 iend = &iptr[borevent_len]; // in case they give us too much data!
450
451 // Make sure BOR header word is right
452 uint32_t bor_header = *iptr++;
453 if(bor_header != 0x700e01){
454 stringstream ss;
455 ss << "Bad BOR header: 0x" << hex << bor_header;
456 throw JException(ss.str(), __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__456);
457 }
458
459 // Loop over crates
460 while(iptr<iend){
461 uint32_t crate_len = *iptr++;
462 uint32_t *iend_crate = &iptr[crate_len]; // points to first word after this crate
463 uint32_t crate_header = *iptr++;
464// uint32_t rocid = crate_header&0xFF;
465
466 // Make sure crate tag is right
467 if( (crate_header>>16) != 0x71 ){
468 stringstream ss;
469 ss << "Bad BOR crate header: 0x" << hex << (crate_header>>16);
470 throw JException(ss.str(), __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__470);
471 }
472
473 // Loop over modules
474 while(iptr<iend_crate){
475 uint32_t module_header = *iptr++;
476 uint32_t module_len = module_header&0xFFFF;
477 uint32_t modType = (module_header>>20)&0x1f;
478// uint32_t slot = (module_header>>25);
479// uint32_t *iend_module = &iptr[module_len]; // points to first word after this module
480
481 uint32_t *src = iptr;
482 uint32_t *dest = NULL__null;
483 uint32_t sizeof_dest = 0;
484
485 Df250BORConfig *f250conf = NULL__null;
486 Df125BORConfig *f125conf = NULL__null;
487 DF1TDCBORConfig *F1TDCconf = NULL__null;
488 DCAEN1290TDCBORConfig *caen1190conf = NULL__null;
489
490 switch(modType){
491 case DModuleType::FADC250: // f250
492 f250conf = new Df250BORConfig;
493 dest = (uint32_t*)&f250conf->rocid;
494 sizeof_dest = sizeof(f250config);
495 break;
496 case DModuleType::FADC125: // f125
497 f125conf = new Df125BORConfig;
498 dest = (uint32_t*)&f125conf->rocid;
499 sizeof_dest = sizeof(f125config);
500 break;
501
502 case DModuleType::F1TDC32: // F1TDCv2
503 case DModuleType::F1TDC48: // F1TDCv3
504 F1TDCconf = new DF1TDCBORConfig;
505 dest = (uint32_t*)&F1TDCconf->rocid;
506 sizeof_dest = sizeof(F1TDCconfig);
507 break;
508
509 case DModuleType::CAEN1190: // CAEN 1190 TDC
510 case DModuleType::CAEN1290: // CAEN 1290 TDC
511 caen1190conf = new DCAEN1290TDCBORConfig;
512 dest = (uint32_t*)&caen1190conf->rocid;
513 sizeof_dest = sizeof(caen1190config);
514 break;
515
516 default:
517 {
518 stringstream ss;
519 ss << "Unknown BOR module type: " << modType << " (module_header=0x"<<hex<<module_header<<")";
520 jerr << ss.str() << endl;
521 throw JException(ss.str(), __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__521);
522 }
523 }
524
525 // Check that the bank size and data structure size match.
526 if( module_len != (sizeof_dest/sizeof(uint32_t)) ){
527 stringstream ss;
528 ss << "BOR module bank size does not match structure! " << module_len << " != " << (sizeof_dest/sizeof(uint32_t)) << " for modType " << modType;
529 throw JException(ss.str(), __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__529);
530 }
531
532 // Copy bank data, assuming format is the same
533 for(uint32_t i=0; i<module_len; i++) *dest++ = *src++;
534
535 // Store object for use in this and subsequent events
536 if(f250conf ) borptrs->vDf250BORConfig.push_back(f250conf);
537 if(f125conf ) borptrs->vDf125BORConfig.push_back(f125conf);
538 if(F1TDCconf ) borptrs->vDF1TDCBORConfig.push_back(F1TDCconf);
539 if(caen1190conf) borptrs->vDCAEN1290TDCBORConfig.push_back(caen1190conf);
540
541 iptr = &iptr[module_len];
542 }
543
544 iptr = iend_crate; // ensure we're pointing past this crate
545 }
546
547 // Sort the BOR config events now so we don't have to do it for every event
548 borptrs->Sort();
549
550}
551
552//---------------------------------
553// ParseTSscalerBank
554//---------------------------------
555void DEVIOWorkerThread::ParseTSscalerBank(uint32_t* &iptr, uint32_t *iend)
556{
557 uint32_t Nwords = ((uint64_t)iend - (uint64_t)iptr)/sizeof(uint32_t);
558 uint32_t Nwords_expected = (6+32+16+32+16);
559 if(Nwords != Nwords_expected){
560 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<560<<" "
<< "TS bank size does not match expected!!" << endl;
561 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<561<<" "
<< "Found " << Nwords << " words. Expected " << Nwords_expected << endl;
562
563 }else{
564 // n.b. Get the last event here since if this is a block
565 // of events, the last should be the actual sync event.
566 DParsedEvent *pe = current_parsed_events.back();
567 DL1Info *s = pe->NEW_DL1Info();
568 s->nsync = *iptr++;
569 s->trig_number = *iptr++;
570 s->live_time = *iptr++;
571 s->busy_time = *iptr++;
572 s->live_inst = *iptr++;
573 s->unix_time = *iptr++;
574 for(uint32_t i=0; i<32; i++) s->gtp_sc.push_back ( *iptr++ );
575 for(uint32_t i=0; i<16; i++) s->fp_sc.push_back ( *iptr++ );
576 for(uint32_t i=0; i<32; i++) s->gtp_rate.push_back( *iptr++ );
577 for(uint32_t i=0; i<16; i++) s->fp_rate.push_back ( *iptr++ );
578 }
579
580 iptr = iend;
581}
582
583//---------------------------------
584// Parsef250scalerBank
585//---------------------------------
586void DEVIOWorkerThread::Parsef250scalerBank(uint32_t* &iptr, uint32_t *iend)
587{
588 iptr = &iptr[(*iptr) + 1];
589}
590
591//---------------------------------
592// ParseControlEvent
593//---------------------------------
594void DEVIOWorkerThread::ParseControlEvent(uint32_t* &iptr, uint32_t *iend)
595{
596 for(auto pe : current_parsed_events) pe->event_status_bits |= (1<<kSTATUS_CONTROL_EVENT);
597
598 iptr = &iptr[(*iptr) + 1];
599}
600
601//---------------------------------
602// ParsePhysicsBank
603//---------------------------------
604void DEVIOWorkerThread::ParsePhysicsBank(uint32_t* &iptr, uint32_t *iend)
605{
606
607 for(auto pe : current_parsed_events) pe->event_status_bits |= (1<<kSTATUS_PHYSICS_EVENT);
608
609 uint32_t physics_event_len = *iptr++;
610 uint32_t *iend_physics_event = &iptr[physics_event_len];
611 iptr++;
612
613 // Built Trigger Bank
614 uint32_t built_trigger_bank_len = *iptr;
615 uint32_t *iend_built_trigger_bank = &iptr[built_trigger_bank_len+1];
616 ParseBuiltTriggerBank(iptr, iend_built_trigger_bank);
617 iptr = iend_built_trigger_bank;
618
619 // Loop over Data banks
620 while( iptr < iend_physics_event ) {
621
622 uint32_t data_bank_len = *iptr;
623 uint32_t *iend_data_bank = &iptr[data_bank_len+1];
624
625 ParseDataBank(iptr, iend_data_bank);
626
627 iptr = iend_data_bank;
628 }
629
630 iptr = iend_physics_event;
631}
632
633//---------------------------------
634// ParseBuiltTriggerBank
635//---------------------------------
636void DEVIOWorkerThread::ParseBuiltTriggerBank(uint32_t* &iptr, uint32_t *iend)
637{
638 if(!PARSE_TRIGGER) return;
639
640 iptr++; // advance past length word
641 uint32_t mask = 0xFF202000;
642 if( ((*iptr) & mask) != mask ){
643 stringstream ss;
644 ss << "Bad header word in Built Trigger Bank: " << hex << *iptr;
645 throw JException(ss.str(), __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__645);
646 }
647
648 uint32_t tag = (*iptr)>>16; // 0xFF2X
649 uint32_t Nrocs = (*iptr++) & 0xFF;
650 uint32_t Mevents = current_parsed_events.size();
651
652 // sanity check:
653 if(Mevents == 0) {
654 stringstream ss;
655 ss << "DEVIOWorkerThread::ParseBuiltTriggerBank() called with zero events! "<<endl;
656 throw JException(ss.str(), __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__656);
657 }
658
659
660 //-------- Common data (64bit)
661 uint32_t common_header64 = *iptr++;
662 uint32_t common_header64_len = common_header64 & 0xFFFF;
663 uint64_t *iptr64 = (uint64_t*)iptr;
664 iptr = &iptr[common_header64_len];
665
666 // First event number
667 uint64_t first_event_num = *iptr64++;
668
669 // Hi and lo 32bit words in 64bit numbers seem to be
670 // switched for events read from ET, but not read from
671 // file. Not sure if this is in the swapping routine
672 if(event_source->source_type==event_source->kETSource) first_event_num = (first_event_num>>32) | (first_event_num<<32);
673
674 // Average timestamps
675 uint32_t Ntimestamps = (common_header64_len/2)-1;
676 if(tag & 0x2) Ntimestamps--; // subtract 1 for run number/type word if present
677 vector<uint64_t> avg_timestamps;
678 for(uint32_t i=0; i<Ntimestamps; i++) avg_timestamps.push_back(*iptr64++);
679
680 // run number and run type
681 uint32_t run_number = 0;
682 uint32_t run_type = 0;
683 if(tag & 0x02){
684 run_number = (*iptr64) >> 32;
685 run_type = (*iptr64) & 0xFFFFFFFF;
686 iptr64++;
687 }
688
689 //-------- Common data (16bit)
690 uint32_t common_header16 = *iptr++;
691 uint32_t common_header16_len = common_header16 & 0xFFFF;
692 uint16_t *iptr16 = (uint16_t*)iptr;
693 iptr = &iptr[common_header16_len];
694
695 vector<uint16_t> event_types;
696 for(uint32_t i=0; i<Mevents; i++) event_types.push_back(*iptr16++);
697
698 //-------- ROC data (32bit)
699 for(uint32_t iroc=0; iroc<Nrocs; iroc++){
700 uint32_t common_header32 = *iptr++;
701 uint32_t common_header32_len = common_header32 & 0xFFFF;
702 uint32_t rocid = common_header32 >> 24;
703
704 uint32_t Nwords_per_event = common_header32_len/Mevents;
705 for(auto pe : current_parsed_events){
706
707 DCODAROCInfo *codarocinfo = pe->NEW_DCODAROCInfo();
708 codarocinfo->rocid = rocid;
709
710 uint64_t ts_low = *iptr++;
711 uint64_t ts_high = *iptr++;
712 codarocinfo->timestamp = (ts_high<<32) + ts_low;
713 codarocinfo->misc.clear(); // could be recycled from previous event
714 for(uint32_t i=2; i<Nwords_per_event; i++) codarocinfo->misc.push_back(*iptr++);
715
716 if(iptr > iend){
717 throw JException("Bad data format in ParseBuiltTriggerBank!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__717);
718 }
719 }
720 }
721
722 //-------- Make DCODAEventInfo objects
723 uint64_t ievent = 0;
724 for(auto pe : current_parsed_events){
725
726 pe->run_number = run_number; // may be overwritten in JEventSource_EVIOpp::GetEvent()
727
728 DCODAEventInfo *codaeventinfo = pe->NEW_DCODAEventInfo();
729 codaeventinfo->run_number = run_number;
730 codaeventinfo->run_type = run_type;
731 codaeventinfo->event_number = first_event_num + ievent;
732 codaeventinfo->event_type = event_types.empty() ? 0:event_types[ievent];
733 codaeventinfo->avg_timestamp = avg_timestamps.empty() ? 0:avg_timestamps[ievent];
734 ievent++;
735 }
736}
737
738//---------------------------------
739// ParseDataBank
740//---------------------------------
741void DEVIOWorkerThread::ParseDataBank(uint32_t* &iptr, uint32_t *iend)
742{
743 // Physics Event's Data Bank header
744 iptr++; // advance past data bank length word
745 uint32_t rocid = ((*iptr)>>16) & 0xFFF;
746 iptr++;
747
748 // Loop over Data Block Banks
749 while(iptr < iend){
750
751 uint32_t data_block_bank_len = *iptr++;
752 uint32_t *iend_data_block_bank = &iptr[data_block_bank_len];
753 uint32_t data_block_bank_header = *iptr++;
754
755 // Not sure where this comes from, but it needs to be skipped if present
756 while( (*iptr==0xF800FAFA) && (iptr<iend) ) iptr++;
757
758 uint32_t det_id = (data_block_bank_header>>16) & 0xFFF;
759 switch(det_id){
760
761 case 20:
762 ParseCAEN1190(rocid, iptr, iend_data_block_bank);
763 break;
764
765 case 0x55:
766 ParseModuleConfiguration(rocid, iptr, iend_data_block_bank);
767 break;
768
769 case 0x56:
770 ParseEventTagBank(iptr, iend_data_block_bank);
771 break;
772
773 case 0:
774 case 1:
775 case 3:
776 case 6: // flash 250 module, MMD 2014/2/4
777 case 16: // flash 125 module (CDC), DL 2014/6/19
778 case 26: // F1 TDC module (BCAL), MMD 2014-07-31
779 ParseJLabModuleData(rocid, iptr, iend_data_block_bank);
780 break;
781
782 // These were implemented in the ROL for sync events
783 // as 0xEE02 and 0xEE05. However, that violates the
784 // spec. which reserves the top 4 bits as status bits
785 // (the first "E" should really be a "1". We just check
786 // other 12 bits here.
787 case 0xE02:
788 ParseTSscalerBank(iptr, iend);
789 break;
790 case 0xE05:
791 Parsef250scalerBank(iptr, iend);
792 break;
793 case 0xE10: // really wish Sascha would share when he does this stuff!
794 Parsef250scalerBank(iptr, iend);
795 break;
796
797 // When we write out single events in the offline, we also can save some
798 // higher level data objects to save disk space and speed up
799 // specialized processing (e.g. pi0 calibration)
800 case 0xD01:
801 ParseDVertexBank(iptr, iend);
802 break;
803
804 case 5:
805 // old ROL Beni used had this but I don't think its
806 // been used for years. Run 10390 seems to have
807 // this though (???)
808 break;
809
810
811 default:
812 jerr<<"Unknown module type ("<<det_id<<" = " << hex << det_id << dec << " ) encountered" << endl;
813// if(VERBOSE>5){
814 cout << "----- First few words to help with debugging -----" << endl;
815 cout.flush(); cerr.flush();
816 DumpBinary(&iptr[-2], iend, 32, &iptr[-1]);
817// }
818 }
819
820 iptr = iend_data_block_bank;
821 }
822
823}
824
825//----------------
826// ParseTIBank
827//----------------
828void DEVIOWorkerThread::ParseTIBank(uint32_t rocid, uint32_t* &iptr, uint32_t* iend)
829{
830 while(iptr<iend && ((*iptr) & 0xF8000000) != 0x88000000) iptr++; // Skip to JLab block trailer
831 iptr++; // advance past JLab block trailer
832 while(iptr<iend && *iptr == 0xF8000000) iptr++; // skip filler words after block trailer
833 //iptr = iend;
834}
835
836//----------------
837// ParseCAEN1190
838//----------------
839void DEVIOWorkerThread::ParseCAEN1190(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
840{
841 if(!PARSE_CAEN1290TDC){ iptr = &iptr[(*iptr) + 1]; return; }
842
843 /// Parse data from a CAEN 1190 or 1290 module
844 /// (See ppg. 72-74 of V1290_REV15.pdf manual)
845
846 uint32_t slot = 0;
847 uint32_t event_count = 0;
848 uint32_t word_count = 0;
849 uint32_t trigger_time_tag = 0;
850 uint32_t tdc_num = 0;
851 uint32_t event_id = 0;
852 uint32_t bunch_id = 0;
853
854 // We need to accomodate multi-event blocks where
855 // events are entangled (i.e. hits from event 1
856 // are mixed in between those of event 2,3,4,
857 // etc... With CAEN modules, we only know which
858 // event a hit came from by looking at the event_id
859 // in the TDC header. This value is only 12 bits
860 // and could roll over within an event block. This
861 // means we need to keep track of the order we
862 // encounter them in so it is maintained in the
863 // "events" container. The event_id order is kept
864 // in the "event_id_order" vector.
865 map<uint32_t, DParsedEvent*> events_by_event_id;
866
867 auto pe_iter = current_parsed_events.begin();
868 DParsedEvent *pe = NULL__null;
869
870 while(iptr<iend){
871
872 // This word appears to be appended to the data.
873 // Probably in the ROL. Ignore it if found.
874 if(*iptr == 0xd00dd00d) {
875 if(VERBOSE>7) cout << " CAEN skipping 0xd00dd00d word" << endl;
876 iptr++;
877 continue;
878 }
879
880 uint32_t type = (*iptr) >> 27;
881 uint32_t edge = 0; // 1=trailing, 0=leading
882 uint32_t channel = 0;
883 uint32_t tdc = 0;
884 uint32_t error_flags = 0;
885 switch(type){
886 case 0b01000: // Global Header
887 slot = (*iptr) & 0x1f;
888 event_count = ((*iptr)>>5) & 0xffffff;
889 if(VERBOSE>7) cout << " CAEN TDC Global Header (slot=" << slot << " , event count=" << event_count << ")" << endl;
890 break;
891 case 0b10000: // Global Trailer
892 slot = (*iptr) & 0x1f;
893 word_count = ((*iptr)>>5) & 0x7ffff;
894 if(VERBOSE>7) cout << " CAEN TDC Global Trailer (slot=" << slot << " , word count=" << word_count << ")" << endl;
895 slot = event_count = word_count = trigger_time_tag = tdc_num = event_id = bunch_id = 0;
896 break;
897 case 0b10001: // Global Trigger Time Tag
898 trigger_time_tag = ((*iptr)>>5) & 0x7ffffff;
899 if(VERBOSE>7) cout << " CAEN TDC Global Trigger Time Tag (tag=" << trigger_time_tag << ")" << endl;
900 break;
901 case 0b00001: // TDC Header
902 tdc_num = ((*iptr)>>24) & 0x03;
903 event_id = ((*iptr)>>12) & 0x0fff;
904 bunch_id = (*iptr) & 0x0fff;
905 if(events_by_event_id.find(event_id) == events_by_event_id.end()){
906 if(pe_iter == current_parsed_events.end()){
907 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<907<<" "
<< "CAEN1290TDC parser sees more events than CODA header! (>" << current_parsed_events.size() << ")" << endl;
908 for( auto p : events_by_event_id) cout << "id=" << p.first << endl;
909 iptr = iend;
910 exit(-1); // should we exit, or try and continue??
911 return;
912 }
913 pe = *pe_iter++;
914 events_by_event_id[event_id] = pe;
915 }else{
916 pe = events_by_event_id[event_id];
917 }
918 if(VERBOSE>7) cout << " CAEN TDC TDC Header (tdc=" << tdc_num <<" , event id=" << event_id <<" , bunch id=" << bunch_id << ")" << endl;
919 break;
920 case 0b00000: // TDC Measurement
921 edge = ((*iptr)>>26) & 0x01;
922 channel = ((*iptr)>>21) & 0x1f;
923 tdc = ((*iptr)>>0) & 0x1fffff;
924 if(VERBOSE>7) cout << " CAEN TDC TDC Measurement (" << (edge ? "trailing":"leading") << " , channel=" << channel << " , tdc=" << tdc << ")" << endl;
925
926 // Create DCAEN1290TDCHit object
927 if(pe) pe->NEW_DCAEN1290TDCHit(rocid, slot, channel, 0, edge, tdc_num, event_id, bunch_id, tdc);
928 break;
929 case 0b00100: // TDC Error
930 error_flags = (*iptr) & 0x7fff;
931 if(VERBOSE>7) cout << " CAEN TDC TDC Error (err flags=0x" << hex << error_flags << dec << ")" << endl;
932 break;
933 case 0b00011: // TDC Trailer
934 tdc_num = ((*iptr)>>24) & 0x03;
935 event_id = ((*iptr)>>12) & 0x0fff;
936 word_count = ((*iptr)>>0) & 0x0fff;
937 if(VERBOSE>7) cout << " CAEN TDC TDC Trailer (tdc=" << tdc_num <<" , event id=" << event_id <<" , word count=" << word_count << ")" << endl;
938 tdc_num = event_id = bunch_id = 0;
939 break;
940 case 0b11000: // Filler Word
941 if(VERBOSE>7) cout << " CAEN TDC Filler Word" << endl;
942 break;
943 default:
944 cout << "Unknown datatype: 0x" << hex << type << " full word: "<< *iptr << dec << endl;
945 }
946
947 iptr++;
948 }
949
950}
951
952//----------------
953// ParseModuleConfiguration
954//----------------
955void DEVIOWorkerThread::ParseModuleConfiguration(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
956{
957 if(!PARSE_CONFIG){ iptr = &iptr[(*iptr) + 1]; return; }
958
959 /// Parse a bank of module configuration data. These are configuration values
960 /// programmed into the module at the beginning of the run that may be needed
961 /// in the offline. For example, the number of samples to sum in a FADC pulse
962 /// integral.
963 ///
964 /// The bank has one or more sections, each describing parameters applicable
965 /// to a number of modules as indicated by a 24bit slot mask.
966 ///
967 /// This bank should appear only once per DAQ event which, if in multi-event
968 /// block mode, may have multiple L1 events. The parameters here will apply
969 /// to all L1 events in the block. This method will put the config objects
970 /// into each event in current_parsed_events. The config objects are duplicated
971 /// as needed so each event has its own, indepenent set of config object.
972
973 while(iptr < iend){
974 uint32_t slot_mask = (*iptr) & 0xFFFFFF;
975 uint32_t Nvals = ((*iptr) >> 24) & 0xFF;
976 iptr++;
977
978 // Events will be created in the first event (i.e. using its pool)
979 // but pointers are saved so we can use them to construct identical
980 // objects in all other event later
981 DParsedEvent *pe = current_parsed_events.front();
982
983 Df250Config *f250config = NULL__null;
984 Df125Config *f125config = NULL__null;
985 DF1TDCConfig *f1tdcconfig = NULL__null;
986 DCAEN1290TDCConfig *caen1290tdcconfig = NULL__null;
987
988 // Loop over all parameters in this section
989 for(uint32_t i=0; i< Nvals; i++){
990 if( iptr >= iend){
991 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<991<<" "
<< "DAQ Configuration bank corrupt! slot_mask=0x" << hex << slot_mask << dec << " Nvals="<< Nvals << endl;
992 exit(-1);
993 }
994
995 daq_param_type ptype = (daq_param_type)((*iptr)>>16);
996 uint16_t val = (*iptr) & 0xFFFF;
997
998 if(VERBOSE>6) cout << " DAQ parameter of type: 0x" << hex << ptype << dec << " found with value: " << val << endl;
999
1000 // Create config object of correct type if needed and copy
1001 // parameter value into it.
1002 switch(ptype>>8){
1003
1004 // f250
1005 case 0x05:
1006 if( !f250config ) f250config = pe->NEW_Df250Config(rocid, slot_mask);
1007 switch(ptype){
1008 case kPARAM250_NSA : f250config->NSA = val; break;
1009 case kPARAM250_NSB : f250config->NSB = val; break;
1010 case kPARAM250_NSA_NSB : f250config->NSA_NSB = val; break;
1011 case kPARAM250_NPED : f250config->NPED = val; break;
1012 default: _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1012<<" "
<< "UNKNOWN DAQ Config Parameter type: 0x" << hex << ptype << dec << endl;
1013 }
1014 break;
1015
1016 // f125
1017 case 0x0F:
1018 if( !f125config ) f125config = pe->NEW_Df125Config(rocid, slot_mask);
1019 switch(ptype){
1020 case kPARAM125_NSA : f125config->NSA = val; break;
1021 case kPARAM125_NSB : f125config->NSB = val; break;
1022 case kPARAM125_NSA_NSB : f125config->NSA_NSB = val; break;
1023 case kPARAM125_NPED : f125config->NPED = val; break;
1024 case kPARAM125_WINWIDTH : f125config->WINWIDTH = val; break;
1025 case kPARAM125_PL : f125config->PL = val; break;
1026 case kPARAM125_NW : f125config->NW = val; break;
1027 case kPARAM125_NPK : f125config->NPK = val; break;
1028 case kPARAM125_P1 : f125config->P1 = val; break;
1029 case kPARAM125_P2 : f125config->P2 = val; break;
1030 case kPARAM125_PG : f125config->PG = val; break;
1031 case kPARAM125_IE : f125config->IE = val; break;
1032 case kPARAM125_H : f125config->H = val; break;
1033 case kPARAM125_TH : f125config->TH = val; break;
1034 case kPARAM125_TL : f125config->TL = val; break;
1035 case kPARAM125_IBIT : f125config->IBIT = val; break;
1036 case kPARAM125_ABIT : f125config->ABIT = val; break;
1037 case kPARAM125_PBIT : f125config->PBIT = val; break;
1038 default: _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1038<<" "
<< "UNKNOWN DAQ Config Parameter type: 0x" << hex << ptype << dec << endl;
1039 }
1040 break;
1041
1042 // F1TDC
1043 case 0x06:
1044 if( !f1tdcconfig ) f1tdcconfig = pe->NEW_DF1TDCConfig(rocid, slot_mask);
1045 switch(ptype){
1046 case kPARAMF1_REFCNT : f1tdcconfig->REFCNT = val; break;
1047 case kPARAMF1_TRIGWIN : f1tdcconfig->TRIGWIN = val; break;
1048 case kPARAMF1_TRIGLAT : f1tdcconfig->TRIGLAT = val; break;
1049 case kPARAMF1_HSDIV : f1tdcconfig->HSDIV = val; break;
1050 case kPARAMF1_BINSIZE : f1tdcconfig->BINSIZE = val; break;
1051 case kPARAMF1_REFCLKDIV : f1tdcconfig->REFCLKDIV = val; break;
1052 default: _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1052<<" "
<< "UNKNOWN DAQ Config Parameter type: 0x" << hex << ptype << dec << endl;
1053 }
1054 break;
1055
1056 // caen1290
1057 case 0x10:
1058 if( !caen1290tdcconfig ) caen1290tdcconfig = pe->NEW_DCAEN1290TDCConfig(rocid, slot_mask);
1059 switch(ptype){
1060 case kPARAMCAEN1290_WINWIDTH : caen1290tdcconfig->WINWIDTH = val; break;
1061 case kPARAMCAEN1290_WINOFFSET : caen1290tdcconfig->WINOFFSET = val; break;
1062 default: _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1062<<" "
<< "UNKNOWN DAQ Config Parameter type: 0x" << hex << ptype << dec << endl;
1063 }
1064 break;
1065
1066 default:
1067 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1067<<" "
<< "Unknown module type: 0x" << hex << (ptype>>8) << endl;
1068 exit(-1);
1069 }
1070
1071
1072 iptr++;
1073 }
1074
1075 // Make copies of all config objects for all other events
1076 for(auto tpe : current_parsed_events){
1077
1078 if(tpe == pe) continue; // first event already owns objects so skip it
1079
1080 if(f250config ) tpe->NEW_Df250Config(f250config);
1081 if(f125config ) tpe->NEW_Df125Config(f125config);
1082 if(f1tdcconfig ) tpe->NEW_DF1TDCConfig(f1tdcconfig);
1083 if(caen1290tdcconfig) tpe->NEW_DCAEN1290TDCConfig(caen1290tdcconfig);
1084 }
1085 }
1086}
1087
1088//----------------
1089// ParseJLabModuleData
1090//----------------
1091void DEVIOWorkerThread::ParseJLabModuleData(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
1092{
1093
1094 while(iptr<iend){
1095
1096 // Get module type from next word (bits 18-21)
1097 uint32_t mod_id = ((*iptr) >> 18) & 0x000F;
1098 MODULE_TYPE type = (MODULE_TYPE)mod_id;
1099 //cout << " rocid=" << rocid << " Encountered module type: " << type << " (=" << DModuleType::GetModule(type).GetName() << ") word=" << hex << (*iptr) << dec << endl;
1100
1101 switch(type){
1102 case DModuleType::FADC250:
1103 Parsef250Bank(rocid, iptr, iend);
1104 break;
1105
1106 case DModuleType::FADC125:
1107 Parsef125Bank(rocid, iptr, iend);
1108 break;
1109
1110 case DModuleType::F1TDC32:
1111 ParseF1TDCBank(rocid, iptr, iend);
1112 break;
1113
1114 case DModuleType::F1TDC48:
1115 ParseF1TDCBank(rocid, iptr, iend);
1116 break;
1117
1118 case DModuleType::TID:
1119 ParseTIBank(rocid, iptr, iend);
1120 /*
1121 // Ignore this data and skip over it
1122 while(iptr<iend && ((*iptr) & 0xF8000000) != 0x88000000) iptr++; // Skip to JLab block trailer
1123 iptr++; // advance past JLab block trailer
1124 while(iptr<iend && *iptr == 0xF8000000) iptr++; // skip filler words after block trailer
1125 break;
1126 */
1127 break;
1128
1129 case DModuleType::UNKNOWN:
1130 default:
1131 jerr<<"Unknown module type ("<<mod_id<<") iptr=0x" << hex << iptr << dec << endl;
1132
1133 while(iptr<iend && ((*iptr) & 0xF8000000) != 0x88000000) iptr++; // Skip to JLab block trailer
1134 iptr++; // advance past JLab block trailer
1135 while(iptr<iend && *iptr == 0xF8000000) iptr++; // skip filler words after block trailer
1136 break;
1137 }
1138 }
1139
1140}
1141
1142//----------------
1143// Parsef250Bank
1144//----------------
1145void DEVIOWorkerThread::Parsef250Bank(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
1146{
1147 if(!PARSE_F250){ iptr = &iptr[(*iptr) + 1]; return; }
1148
1149 auto pe_iter = current_parsed_events.begin();
1150 DParsedEvent *pe = NULL__null;
1151
1152 uint32_t slot = 0;
1153 uint32_t itrigger = -1;
1154
1155 // Loop over data words
1156 for(; iptr<iend; iptr++){
1157
1158 // Skip all non-data-type-defining words at this
1159 // level. When we do encounter one, the appropriate
1160 // case block below should handle parsing all of
1161 // the data continuation words and advance the iptr.
1162 if(((*iptr>>31) & 0x1) == 0)continue;
1163
1164 uint32_t data_type = (*iptr>>27) & 0x0F;
1165 switch(data_type){
1166 case 0: // Block Header
1167 slot = (*iptr>>22) & 0x1F;
1168 if(VERBOSE>7) cout << " FADC250 Block Header: slot="<<slot<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1169 break;
1170 case 1: // Block Trailer
1171 pe_iter = current_parsed_events.begin();
1172 pe = NULL__null;
1173 if(VERBOSE>7) cout << " FADC250 Block Trailer"<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1174 break;
1175 case 2: // Event Header
1176 itrigger = (*iptr>>0) & 0x3FFFFF;
1177 pe = *pe_iter++;
1178 if(VERBOSE>7) cout << " FADC250 Event Header: itrigger="<<itrigger<<", rocid="<<rocid<<", slot="<<slot<<")" <<" ("<<hex<<*iptr<<dec<<")" <<endl;
1179 break;
1180 case 3: // Trigger Time
1181 {
1182 uint64_t t = ((*iptr)&0xFFFFFF)<<0;
1183 iptr++;
1184 if(((*iptr>>31) & 0x1) == 0){
1185 t += ((*iptr)&0xFFFFFF)<<24; // from word on the street: second trigger time word is optional!!??
1186 if(VERBOSE>7) cout << " Trigger time high word="<<(((*iptr)&0xFFFFFF))<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1187 }else{
1188 iptr--;
1189 }
1190 if(VERBOSE>7) cout << " FADC250 Trigger Time: t="<<t<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1191 if(pe) pe->NEW_Df250TriggerTime(rocid, slot, itrigger, t);
1192 }
1193 break;
1194 case 4: // Window Raw Data
1195 // iptr passed by reference and so will be updated automatically
1196 if(VERBOSE>7) cout << " FADC250 Window Raw Data"<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1197 if(pe) MakeDf250WindowRawData(pe, rocid, slot, itrigger, iptr);
1198 break;
1199 case 5: // Window Sum
1200 {
1201 uint32_t channel = (*iptr>>23) & 0x0F;
1202 uint32_t sum = (*iptr>>0) & 0x3FFFFF;
1203 uint32_t overflow = (*iptr>>22) & 0x1;
1204 if(VERBOSE>7) cout << " FADC250 Window Sum"<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1205 if(pe) pe->NEW_Df250WindowSum(rocid, slot, channel, itrigger, sum, overflow);
1206 }
1207 break;
1208 case 6: // Pulse Raw Data
1209// MakeDf250PulseRawData(objs, rocid, slot, itrigger, iptr);
1210 if(VERBOSE>7) cout << " FADC250 Pulse Raw Data"<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1211 break;
1212 case 7: // Pulse Integral
1213 {
1214 uint32_t channel = (*iptr>>23) & 0x0F;
1215 uint32_t pulse_number = (*iptr>>21) & 0x03;
1216 uint32_t quality_factor = (*iptr>>19) & 0x03;
1217 uint32_t sum = (*iptr>>0) & 0x7FFFF;
1218 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1219 uint32_t nsamples_pedestal = 1; // The firmware returns an already divided pedestal
1220 uint32_t pedestal = 0; // This will be replaced by the one from Df250PulsePedestal in GetObjects
1221 if(VERBOSE>7) cout << " FADC250 Pulse Integral: chan="<<channel<<" pulse_number="<<pulse_number<<" sum="<<sum<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1222 if(pe) pe->NEW_Df250PulseIntegral(rocid, slot, channel, itrigger, pulse_number, quality_factor, sum, pedestal, nsamples_integral, nsamples_pedestal);
1223 }
1224 break;
1225 case 8: // Pulse Time
1226 {
1227 uint32_t channel = (*iptr>>23) & 0x0F;
1228 uint32_t pulse_number = (*iptr>>21) & 0x03;
1229 uint32_t quality_factor = (*iptr>>19) & 0x03;
1230 uint32_t pulse_time = (*iptr>>0) & 0x7FFFF;
1231 if(VERBOSE>7) cout << " FADC250 Pulse Time: chan="<<channel<<" pulse_number="<<pulse_number<<" pulse_time="<<pulse_time<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1232 if(pe) pe->NEW_Df250PulseTime(rocid, slot, channel, itrigger, pulse_number, quality_factor, pulse_time);
1233 }
1234 break;
1235 case 9: // Pulse Data (firmware instroduce in Fall 2016)
1236 {
1237 // from word 1
1238 uint32_t event_number_within_block = (*iptr>>19) & 0xFF;
1239 uint32_t channel = (*iptr>>15) & 0x0F;
1240 bool QF_pedestal = (*iptr>>14) & 0x01;
1241 uint32_t pedestal = (*iptr>>0 ) & 0x3FFF;
1242
1243 // Event headers may be supressed so determine event from hit data
1244 if( (event_number_within_block > current_parsed_events.size()) ) throw JException("Bad f250 event number", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1244);
1245 pe_iter = current_parsed_events.begin();
1246 advance( pe_iter, event_number_within_block );
1247 pe = *pe_iter++;
1248
1249 itrigger = event_number_within_block; // is this right?
1250 uint32_t pulse_number = 0;
1251
1252 while( (*++iptr>>31) == 0 ){
1253
1254 if( (*iptr>>30) != 0x01) throw JException("Bad f250 Pulse Data!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1254);
1255
1256 // from word 2
1257 uint32_t integral = (*iptr>>12) & 0x3FFFF;
1258 bool QF_NSA_beyond_PTW = (*iptr>>11) & 0x01;
1259 bool QF_overflow = (*iptr>>10) & 0x01;
1260 bool QF_underflow = (*iptr>>9 ) & 0x01;
1261 uint32_t nsamples_over_threshold = (*iptr>>0 ) & 0x1FF;
1262
1263 iptr++;
1264 if( (*iptr>>30) != 0x00) throw JException("Bad f250 Pulse Data!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1264);
1265
1266 // from word 3
1267 uint32_t course_time = (*iptr>>21) & 0x1FF;//< 4 ns/count
1268 uint32_t fine_time = (*iptr>>15) & 0x3F;//< 0.0625 ns/count
1269 uint32_t pulse_peak = (*iptr>>3 ) & 0xFFF;
1270 bool QF_vpeak_beyond_NSA = (*iptr>>2 ) & 0x01;
1271 bool QF_vpeak_not_found = (*iptr>>1 ) & 0x01;
1272 bool QF_bad_pedestal = (*iptr>>0 ) & 0x01;
1273
1274 if( pe ) {
1275 pe->NEW_Df250PulseData(rocid, slot, channel, itrigger
1276 , event_number_within_block
1277 , QF_pedestal
1278 , pedestal
1279 , integral
1280 , QF_NSA_beyond_PTW
1281 , QF_overflow
1282 , QF_underflow
1283 , nsamples_over_threshold
1284 , course_time
1285 , fine_time
1286 , pulse_peak
1287 , QF_vpeak_beyond_NSA
1288 , QF_vpeak_not_found
1289 , QF_bad_pedestal
1290 , pulse_number++);
1291 }
1292 }
1293 iptr--; // backup so when outer loop advances, it points to next data defining word
1294
1295 }
1296 break;
1297 case 10: // Pulse Pedestal
1298 {
1299 uint32_t channel = (*iptr>>23) & 0x0F;
1300 uint32_t pulse_number = (*iptr>>21) & 0x03;
1301 uint32_t pedestal = (*iptr>>12) & 0x1FF;
1302 uint32_t pulse_peak = (*iptr>>0) & 0xFFF;
1303 if(VERBOSE>7) cout << " FADC250 Pulse Pedestal chan="<<channel<<" pulse_number="<<pulse_number<<" pedestal="<<pedestal<<" pulse_peak="<<pulse_peak<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1304 if(pe) pe->NEW_Df250PulsePedestal(rocid, slot, channel, itrigger, pulse_number, pedestal, pulse_peak);
1305 }
1306 break;
1307 case 13: // Event Trailer
1308 // This is marked "suppressed for normal readout – debug mode only" in the
1309 // current manual (v2). It does not contain any data so the most we could do here
1310 // is return early. I'm hesitant to do that though since it would mean
1311 // different behavior for debug mode data as regular data.
1312 case 14: // Data not valid (empty module)
1313 case 15: // Filler (non-data) word
1314 if(VERBOSE>7) cout << " FADC250 Event Trailer, Data not Valid, or Filler word ("<<data_type<<")"<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1315 break;
1316 }
1317 }
1318
1319 // Chop off filler words
1320 for(; iptr<iend; iptr++){
1321 if(((*iptr)&0xf8000000) != 0xf8000000) break;
1322 }
1323}
1324
1325//----------------
1326// MakeDf250WindowRawData
1327//----------------
1328void DEVIOWorkerThread::MakeDf250WindowRawData(DParsedEvent *pe, uint32_t rocid, uint32_t slot, uint32_t itrigger, uint32_t* &iptr)
1329{
1330 uint32_t channel = (*iptr>>23) & 0x0F;
1331 uint32_t window_width = (*iptr>>0) & 0x0FFF;
1332
1333 Df250WindowRawData *wrd = pe->NEW_Df250WindowRawData(rocid, slot, channel, itrigger);
1334
1335 for(uint32_t isample=0; isample<window_width; isample +=2){
1336
1337 // Advance to next word
1338 iptr++;
1339
1340 // Make sure this is a data continuation word, if not, stop here
1341 if(((*iptr>>31) & 0x1) != 0x0){
1342 iptr--; // calling method expects us to point to last word in block
1343 break;
1344 }
1345
1346 bool invalid_1 = (*iptr>>29) & 0x1;
1347 bool invalid_2 = (*iptr>>13) & 0x1;
1348 uint16_t sample_1 = 0;
1349 uint16_t sample_2 = 0;
1350 if(!invalid_1)sample_1 = (*iptr>>16) & 0x1FFF;
1351 if(!invalid_2)sample_2 = (*iptr>>0) & 0x1FFF;
1352
1353 // Sample 1
1354 wrd->samples.push_back(sample_1);
1355 wrd->invalid_samples |= invalid_1;
1356 wrd->overflow |= (sample_1>>12) & 0x1;
1357
1358 if(((isample+2) == window_width) && invalid_2)break; // skip last sample if flagged as invalid
1359
1360 // Sample 2
1361 wrd->samples.push_back(sample_2);
1362 wrd->invalid_samples |= invalid_2;
1363 wrd->overflow |= (sample_2>>12) & 0x1;
1364 }
1365}
1366
1367//----------------
1368// Parsef125Bank
1369//----------------
1370void DEVIOWorkerThread::Parsef125Bank(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
1371{
1372 if(!PARSE_F125){ iptr = &iptr[(*iptr) + 1]; return; }
1373
1374 auto pe_iter = current_parsed_events.begin();
1375 DParsedEvent *pe = NULL__null;
1376
1377 uint32_t slot=0;
1378 uint32_t itrigger = -1;
1379 uint32_t last_itrigger = -2;
1380 uint32_t last_pulse_time_channel=0;
1381 uint32_t last_slot = -1;
1382 uint32_t last_channel = -1;
1383
1384 // Loop over data words
1385 for(; iptr<iend; iptr++){
1386
1387 // Skip all non-data-type-defining words at this
1388 // level. When we do encounter one, the appropriate
1389 // case block below should handle parsing all of
1390 // the data continuation words and advance the iptr.
1391 if(((*iptr>>31) & 0x1) == 0)continue;
1392
1393 uint32_t data_type = (*iptr>>27) & 0x0F;
1394 switch(data_type){
1395 case 0: // Block Header
1396 slot = (*iptr>>22) & 0x1F;
1397 if(VERBOSE>7) cout << " FADC125 Block Header: slot="<<slot<<endl;
1398 break;
1399 case 1: // Block Trailer
1400 pe_iter = current_parsed_events.begin();
1401 pe = NULL__null;
1402 break;
1403 case 2: // Event Header
1404 //slot_event_header = (*iptr>>22) & 0x1F;
1405 itrigger = (*iptr>>0) & 0x3FFFFFF;
1406 pe = *pe_iter++;
1407 if(VERBOSE>7) cout << " FADC125 Event Header: itrigger="<<itrigger<<" last_itrigger="<<last_itrigger<<", rocid="<<rocid<<", slot="<<slot <<endl;
1408 break;
1409 case 3: // Trigger Time
1410 {
1411 uint64_t t = ((*iptr)&0xFFFFFF)<<0;
1412 iptr++;
1413 if(((*iptr>>31) & 0x1) == 0){
1414 t += ((*iptr)&0xFFFFFF)<<24; // from word on the street: second trigger time word is optional!!??
1415 }else{
1416 iptr--;
1417 }
1418 if(VERBOSE>7) cout << " FADC125 Trigger Time (t="<<t<<")"<<endl;
1419 if(pe) pe->NEW_Df125TriggerTime(rocid, slot, itrigger, t);
1420 }
1421 break;
1422 case 4: // Window Raw Data
1423 // iptr passed by reference and so will be updated automatically
1424 if(VERBOSE>7) cout << " FADC125 Window Raw Data"<<endl;
1425 if(pe) MakeDf125WindowRawData(pe, rocid, slot, itrigger, iptr);
1426 break;
1427
1428 case 5: // CDC pulse data (new) (GlueX-doc-2274-v8)
1429 {
1430 // Word 1:
1431 uint32_t word1 = *iptr;
1432 uint32_t channel = (*iptr>>20) & 0x7F;
1433 uint32_t pulse_number = (*iptr>>15) & 0x1F;
1434 uint32_t pulse_time = (*iptr>>4 ) & 0x7FF;
1435 uint32_t quality_factor = (*iptr>>3 ) & 0x1; //time QF bit
1436 uint32_t overflow_count = (*iptr>>0 ) & 0x7;
1437 if(VERBOSE>7){
1438 cout << " FADC125 CDC Pulse Data word1: " << hex << (*iptr) << dec << endl;
1439 cout << " FADC125 CDC Pulse Data (chan="<<channel<<" pulse="<<pulse_number<<" time="<<pulse_time<<" QF="<<quality_factor<<" OC="<<overflow_count<<")"<<endl;
1440 }
1441
1442 // Word 2:
1443 ++iptr;
1444 if(iptr>=iend){
1445 jerr << " Truncated f125 CDC hit (block ends before continuation word!)" << endl;
1446 continue;
1447 }
1448 if( ((*iptr>>31) & 0x1) != 0 ){
1449 jerr << " Truncated f125 CDC hit (missing continuation word!)" << endl;
1450 continue;
1451 }
1452 uint32_t word2 = *iptr;
1453 uint32_t pedestal = (*iptr>>23) & 0xFF;
1454 uint32_t sum = (*iptr>>9 ) & 0x3FFF;
1455 uint32_t pulse_peak = (*iptr>>0 ) & 0x1FF;
1456 if(VERBOSE>7){
1457 cout << " FADC125 CDC Pulse Data word2: " << hex << (*iptr) << dec << endl;
1458 cout << " FADC125 CDC Pulse Data (pedestal="<<pedestal<<" sum="<<sum<<" peak="<<pulse_peak<<")"<<endl;
1459 }
1460
1461 // Create hit objects
1462 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1463 uint32_t nsamples_pedestal = 1; // The firmware pedestal divided by 2^PBIT where PBIT is a config. parameter
1464
1465 if( pe ) {
1466 pe->NEW_Df125CDCPulse(rocid, slot, channel, itrigger
1467 , pulse_number // NPK
1468 , pulse_time // le_time
1469 , quality_factor // time_quality_bit
1470 , overflow_count // overflow_count
1471 , pedestal // pedestal
1472 , sum // integral
1473 , pulse_peak // first_max_amp
1474 , word1 // word1
1475 , word2 // word2
1476 , nsamples_pedestal // nsamples_pedestal
1477 , nsamples_integral // nsamples_integral
1478 , false); // emulated
1479 }
1480 }
1481 break;
1482
1483 case 6: // FDC pulse data-integral (new) (GlueX-doc-2274-v8)
1484 {
1485 // Word 1:
1486 uint32_t word1 = *iptr;
1487 uint32_t channel = (*iptr>>20) & 0x7F;
1488 uint32_t pulse_number = (*iptr>>15) & 0x1F;
1489 uint32_t pulse_time = (*iptr>>4 ) & 0x7FF;
1490 uint32_t quality_factor = (*iptr>>3 ) & 0x1; //time QF bit
1491 uint32_t overflow_count = (*iptr>>0 ) & 0x7;
1492 if(VERBOSE>7){
1493 cout << " FADC125 FDC Pulse Data(integral) word1: " << hex << (*iptr) << dec << endl;
1494 cout << " FADC125 FDC Pulse Data (chan="<<channel<<" pulse="<<pulse_number<<" time="<<pulse_time<<" QF="<<quality_factor<<" OC="<<overflow_count<<")"<<endl;
1495 }
1496
1497 // Word 2:
1498 ++iptr;
1499 if(iptr>=iend){
1500 jerr << " Truncated f125 FDC hit (block ends before continuation word!)" << endl;
1501 continue;
1502 }
1503 if( ((*iptr>>31) & 0x1) != 0 ){
1504 jerr << " Truncated f125 FDC hit (missing continuation word!)" << endl;
1505 continue;
1506 }
1507 uint32_t word2 = *iptr;
1508 uint32_t pulse_peak = 0;
1509 uint32_t sum = (*iptr>>19) & 0xFFF;
1510 uint32_t peak_time = (*iptr>>11) & 0xFF;
1511 uint32_t pedestal = (*iptr>>0 ) & 0x7FF;
1512 if(VERBOSE>7){
1513 cout << " FADC125 FDC Pulse Data(integral) word2: " << hex << (*iptr) << dec << endl;
1514 cout << " FADC125 FDC Pulse Data (integral="<<sum<<" time="<<peak_time<<" pedestal="<<pedestal<<")"<<endl;
1515 }
1516
1517 // Create hit objects
1518 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1519 uint32_t nsamples_pedestal = 1; // The firmware pedestal divided by 2^PBIT where PBIT is a config. parameter
1520
1521 if( pe ) {
1522 pe->NEW_Df125FDCPulse(rocid, slot, channel, itrigger
1523 , pulse_number // NPK
1524 , pulse_time // le_time
1525 , quality_factor // time_quality_bit
1526 , overflow_count // overflow_count
1527 , pedestal // pedestal
1528 , sum // integral
1529 , pulse_peak // peak_amp
1530 , peak_time // peak_time
1531 , word1 // word1
1532 , word2 // word2
1533 , nsamples_pedestal // nsamples_pedestal
1534 , nsamples_integral // nsamples_integral
1535 , false); // emulated
1536 }
1537 }
1538 break;
1539
1540 case 7: // Pulse Integral
1541 {
1542 if(VERBOSE>7) cout << " FADC125 Pulse Integral"<<endl;
1543 uint32_t channel = (*iptr>>20) & 0x7F;
1544 uint32_t sum = (*iptr>>0) & 0xFFFFF;
1545 uint32_t quality_factor = 0;
1546 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1547 uint32_t nsamples_pedestal = 1; // The firmware returns an already divided pedestal
1548 uint32_t pedestal = 0; // This will be replaced by the one from Df250PulsePedestal in GetObjects
1549 uint32_t pulse_number = 0;
1550 if (last_slot == slot && last_channel == channel) pulse_number = 1;
1551 last_slot = slot;
1552 last_channel = channel;
1553 if( pe ) pe->NEW_Df125PulseIntegral(rocid, slot, channel, itrigger, pulse_number, quality_factor, sum, pedestal, nsamples_integral, nsamples_pedestal);
1554 }
1555 break;
1556 case 8: // Pulse Time
1557 {
1558 if(VERBOSE>7) cout << " FADC125 Pulse Time"<<endl;
1559 uint32_t channel = (*iptr>>20) & 0x7F;
1560 uint32_t pulse_number = (*iptr>>18) & 0x03;
1561 uint32_t pulse_time = (*iptr>>0) & 0xFFFF;
1562 uint32_t quality_factor = 0;
1563 if( pe ) pe->NEW_Df125PulseTime(rocid, slot, channel, itrigger, pulse_number, quality_factor, pulse_time);
1564 last_pulse_time_channel = channel;
1565 }
1566 break;
1567
1568 case 9: // FDC pulse data-peak (new) (GlueX-doc-2274-v8)
1569 {
1570 // Word 1:
1571 uint32_t word1 = *iptr;
1572 uint32_t channel = (*iptr>>20) & 0x7F;
1573 uint32_t pulse_number = (*iptr>>15) & 0x1F;
1574 uint32_t pulse_time = (*iptr>>4 ) & 0x7FF;
1575 uint32_t quality_factor = (*iptr>>3 ) & 0x1; //time QF bit
1576 uint32_t overflow_count = (*iptr>>0 ) & 0x7;
1577 if(VERBOSE>7){
1578 cout << " FADC125 FDC Pulse Data(peak) word1: " << hex << (*iptr) << dec << endl;
1579 cout << " FADC125 FDC Pulse Data (chan="<<channel<<" pulse="<<pulse_number<<" time="<<pulse_time<<" QF="<<quality_factor<<" OC="<<overflow_count<<")"<<endl;
1580 }
1581
1582 // Word 2:
1583 ++iptr;
1584 if(iptr>=iend){
1585 jerr << " Truncated f125 FDC hit (block ends before continuation word!)" << endl;
1586 continue;
1587 }
1588 if( ((*iptr>>31) & 0x1) != 0 ){
1589 jerr << " Truncated f125 FDC hit (missing continuation word!)" << endl;
1590 continue;
1591 }
1592 uint32_t word2 = *iptr;
1593 uint32_t pulse_peak = (*iptr>>19) & 0xFFF;
1594 uint32_t sum = 0;
1595 uint32_t peak_time = (*iptr>>11) & 0xFF;
1596 uint32_t pedestal = (*iptr>>0 ) & 0x7FF;
1597 if(VERBOSE>7){
1598 cout << " FADC125 FDC Pulse Data(peak) word2: " << hex << (*iptr) << dec << endl;
1599 cout << " FADC125 FDC Pulse Data (integral="<<sum<<" time="<<peak_time<<" pedestal="<<pedestal<<")"<<endl;
1600 }
1601
1602 // Create hit objects
1603 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1604 uint32_t nsamples_pedestal = 1; // The firmware pedestal divided by 2^PBIT where PBIT is a config. parameter
1605
1606 if( pe ) {
1607 pe->NEW_Df125FDCPulse(rocid, slot, channel, itrigger
1608 , pulse_number // NPK
1609 , pulse_time // le_time
1610 , quality_factor // time_quality_bit
1611 , overflow_count // overflow_count
1612 , pedestal // pedestal
1613 , sum // integral
1614 , pulse_peak // peak_amp
1615 , peak_time // peak_time
1616 , word1 // word1
1617 , word2 // word2
1618 , nsamples_pedestal // nsamples_pedestal
1619 , nsamples_integral // nsamples_integral
1620 , false); // emulated
1621 }
1622 }
1623 break;
1624
1625 case 10: // Pulse Pedestal (consistent with Beni's hand-edited version of Cody's document)
1626 {
1627 if(VERBOSE>7) cout << " FADC125 Pulse Pedestal"<<endl;
1628 //channel = (*iptr>>20) & 0x7F;
1629 uint32_t channel = last_pulse_time_channel; // not enough bits to hold channel number so rely on proximity to Pulse Time in data stream (see "FADC125 dataformat 250 modes.docx")
1630 uint32_t pulse_number = (*iptr>>21) & 0x03;
1631 uint32_t pedestal = (*iptr>>12) & 0x1FF;
1632 uint32_t pulse_peak = (*iptr>>0) & 0xFFF;
1633 uint32_t nsamples_pedestal = 1; // The firmware returns an already divided pedestal
1634 if( pe ) pe->NEW_Df125PulsePedestal(rocid, slot, channel, itrigger, pulse_number, pedestal, pulse_peak, nsamples_pedestal);
1635 }
1636 break;
1637
1638 case 13: // Event Trailer
1639 case 14: // Data not valid (empty module)
1640 case 15: // Filler (non-data) word
1641 if(VERBOSE>7) cout << " FADC125 ignored data type: " << data_type <<endl;
1642 break;
1643 }
1644 }
1645
1646 // Chop off filler words
1647 for(; iptr<iend; iptr++){
1648 if(((*iptr)&0xf8000000) != 0xf8000000) break;
1649 }
1650}
1651
1652//----------------
1653// MakeDf125WindowRawData
1654//----------------
1655void DEVIOWorkerThread::MakeDf125WindowRawData(DParsedEvent *pe, uint32_t rocid, uint32_t slot, uint32_t itrigger, uint32_t* &iptr)
1656{
1657 uint32_t channel = (*iptr>>20) & 0x7F;
1658 uint32_t window_width = (*iptr>>0) & 0x0FFF;
1659
1660 Df125WindowRawData *wrd = pe->NEW_Df125WindowRawData(rocid, slot, channel, itrigger);
1661
1662 for(uint32_t isample=0; isample<window_width; isample +=2){
1663
1664 // Advance to next word
1665 iptr++;
1666
1667 // Make sure this is a data continuation word, if not, stop here
1668 if(((*iptr>>31) & 0x1) != 0x0)break;
1669
1670 bool invalid_1 = (*iptr>>29) & 0x1;
1671 bool invalid_2 = (*iptr>>13) & 0x1;
1672 uint16_t sample_1 = 0;
1673 uint16_t sample_2 = 0;
1674 if(!invalid_1)sample_1 = (*iptr>>16) & 0x1FFF;
1675 if(!invalid_2)sample_2 = (*iptr>>0) & 0x1FFF;
1676
1677 // Sample 1
1678 wrd->samples.push_back(sample_1);
1679 wrd->invalid_samples |= invalid_1;
1680 wrd->overflow |= (sample_1>>12) & 0x1;
1681
1682 if((isample+2) == window_width && invalid_2)break; // skip last sample if flagged as invalid
1683
1684 // Sample 2
1685 wrd->samples.push_back(sample_2);
1686 wrd->invalid_samples |= invalid_2;
1687 wrd->overflow |= (sample_2>>12) & 0x1;
1688 }
1689}
1690
1691//----------------
1692// ParseF1TDCBank
1693//----------------
1694void DEVIOWorkerThread::ParseF1TDCBank(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
1695{
1696 if(!PARSE_F1TDC){ iptr = &iptr[(*iptr) + 1]; return; }
1697
1698 uint32_t *istart = iptr;
1699
1700 auto pe_iter = current_parsed_events.begin();
1701 DParsedEvent *pe = NULL__null;
1702
1703 uint32_t slot = 0;
1704 uint32_t modtype = 0;
1705 uint32_t itrigger = -1;
1706 uint32_t trig_time_f1header = 0;
1707
1708 // Some early data had a marker word at just before the actual F1 data
1709 if(*iptr == 0xf1daffff) iptr++;
1710
1711 // Loop over data words
1712 for(; iptr<iend; iptr++){
1713
1714 // Skip all non-data-type-defining words at this
1715 // level. When we do encounter one, the appropriate
1716 // case block below should handle parsing all of
1717 // the data continuation words and advance the iptr.
1718 if(((*iptr>>31) & 0x1) == 0)continue;
1719
1720 uint32_t data_type = (*iptr>>27) & 0x0F;
1721 switch(data_type){
1722 case 0: // Block Header
1723 slot = (*iptr)>>22 & 0x001F;
1724 modtype = (*iptr)>>18 & 0x000F; // should match a DModuleType::type_id_t
1725 if(VERBOSE>7) cout << " F1 Block Header: slot=" << slot << " modtype=" << modtype << endl;
1726 break;
1727
1728 case 1: // Block Trailer
1729 pe_iter = current_parsed_events.begin();
1730 pe = NULL__null;
1731 if(VERBOSE>7) cout << " F1 Block Trailer" << endl;
1732 break;
1733
1734 case 2: // Event Header
1735 {
1736 pe = *pe_iter++;
1737 itrigger = (*iptr)>>0 & 0x0003FFFFF;
1738 if(VERBOSE>7) {
1739 uint32_t slot_event_header = (*iptr)>>22 & 0x00000001F;
1740 cout << " F1 Event Header: slot=" << slot_event_header << " itrigger=" << itrigger << endl;
1741 }
1742 }
1743 break;
1744
1745 case 3: // Trigger time
1746 {
1747 uint64_t t = ((*iptr)&0xFFFFFF)<<0;
1748 iptr++;
1749 if(((*iptr>>31) & 0x1) == 0){
1750 t += ((*iptr)&0xFFFFFF)<<24; // from word on the street: second trigger time word is optional!!??
1751 }else{
1752 iptr--;
1753 }
1754 if(VERBOSE>7) cout << " F1TDC Trigger Time (t="<<t<<")"<<endl;
1755 if(pe) pe->NEW_DF1TDCTriggerTime(rocid, slot, itrigger, t);
1756 }
1757 break;
1758
1759 case 8: // F1 Chip Header
1760 trig_time_f1header = ((*iptr)>> 7) & 0x1FF;
1761 if(VERBOSE>7) {
1762 uint32_t chip_f1header = ((*iptr)>> 3) & 0x07;
1763 uint32_t chan_on_chip_f1header = ((*iptr)>> 0) & 0x07; // this is always 7 in real data!
1764 uint32_t itrigger_f1header = ((*iptr)>>16) & 0x3F;
1765 cout << " Found F1 header: chip=" << chip_f1header << " chan=" << chan_on_chip_f1header << " itrig=" << itrigger_f1header << " trig_time=" << trig_time_f1header << endl;
1766 }
1767 break;
1768
1769 case 7: // F1 Data
1770 {
1771 uint32_t chip = (*iptr>>19) & 0x07;
1772 uint32_t chan_on_chip = (*iptr>>16) & 0x07;
1773 uint32_t time = (*iptr>> 0) & 0xFFFF;
1774 uint32_t channel = F1TDC_channel(chip, chan_on_chip, modtype);
1775 if(VERBOSE>7) cout << " Found F1 data : chip=" << chip << " chan=" << chan_on_chip << " time=" << time << endl;
1776 if(pe) pe->NEW_DF1TDCHit(rocid, slot, channel, itrigger, trig_time_f1header, time, *iptr, MODULE_TYPE(modtype));
1777 }
1778 break;
1779
1780 case 15: // Filler word
1781 if(VERBOSE>7) cout << " F1 filler word" << endl;
1782 case 14: // Data not valid (how to handle this?)
1783 break;
1784
1785 default:
1786 cerr<<endl;
1787 cout.flush(); cerr.flush();
1788 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1788<<" "
<<"Unknown data word in F1TDC block. Dumping for debugging:" << endl;
1789 for(const uint32_t *iiptr = istart; iiptr<iend; iiptr++){
1790 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1790<<" "
<<"0x"<<hex<<*iiptr<<dec;
1791 if(iiptr == iptr)cerr<<" <----";
1792 switch( (*iiptr) & 0xF8000000 ){
1793 case 0x80000000: cerr << " F1 Block Header"; break;
1794 case 0x90000000: cerr << " F1 Event Header"; break;
1795 case 0x98000000: cerr << " F1 Trigger time"; break;
1796 case 0xC0000000: cerr << " F1 Header"; break;
1797 case 0xB8000000: cerr << " F1 Data"; break;
1798 case 0x88000000: cerr << " F1 Block Trailer"; break;
1799 case 0xF8000000: cerr << " Filler word"; break;
1800 case 0xF0000000: cerr << " <module has no valid data>"; break;
1801 default: break;
1802 }
1803 cerr<<endl;
1804 if(iiptr > (iptr+4)) break;
1805 }
1806 throw JException("Unexpected word type in F1TDC block!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1806);
1807 break;
1808 }
1809 }
1810
1811 // Skip filler words
1812 while(iptr<iend && (*iptr&0xF8000000)==0xF8000000)iptr++;
1813}
1814
1815//----------------
1816// ParseDVertexBank
1817//----------------
1818void DEVIOWorkerThread::ParseDVertexBank(uint32_t* &iptr, uint32_t *iend)
1819{
1820 uint32_t Nwords = ((uint64_t)iend - (uint64_t)iptr)/sizeof(uint32_t);
1821 uint32_t Nwords_expected = 11; // ?
1822 if(Nwords != Nwords_expected){
1823 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1823<<" "
<< "DVertex size does not match expected!!" << endl;
1824 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1824<<" "
<< "Found " << Nwords << " words. Expected " << Nwords_expected << endl;
1825 }else{
1826 DParsedEvent *pe = current_parsed_events.back();
1827 DVertex *the_vertex = pe->NEW_DVertex();
1828
1829 uint64_t in_word = *iptr++; // 1st word, lo word; 2nd word, hi word
1830 uint64_t in_word_hi = *iptr++;
1831 in_word |= in_word_hi<<32;
1832 //uint64_t hi_word = *iptr++;
1833 double vertex_x_pos;
1834 memcpy(&vertex_x_pos, &in_word, sizeof(double));
1835 in_word = *iptr++; in_word_hi = *iptr++;
1836 in_word |= in_word_hi<<32;
1837 double vertex_y_pos;
1838 memcpy(&vertex_y_pos, &in_word, sizeof(double));
1839 in_word = *iptr++; in_word_hi = *iptr++;
1840 in_word |= in_word_hi<<32;
1841 double vertex_z_pos;
1842 memcpy(&vertex_z_pos, &in_word, sizeof(double));
1843 in_word = *iptr++; in_word_hi = *iptr++;
1844 in_word |= in_word_hi<<32;
1845 double vertex_t;
1846 memcpy(&vertex_t, &in_word, sizeof(double));
1847
1848 DVector3 vertex_position(vertex_x_pos, vertex_y_pos, vertex_z_pos);
1849 the_vertex->dSpacetimeVertex = DLorentzVector(vertex_position, vertex_t);
1850 the_vertex->dKinFitNDF = *iptr++;
1851
1852 in_word = *iptr++; in_word_hi = *iptr++;
1853 in_word |= in_word_hi<<32;
1854 memcpy(&(the_vertex->dKinFitChiSq), &in_word, sizeof(double));
1855 }
1856}
1857
1858
1859//----------------
1860// LinkAllAssociations
1861//----------------
1862void DEVIOWorkerThread::LinkAllAssociations(void)
1863{
1864
1865 /// Find objects that should be linked as "associated objects"
1866 /// of one another and add to each other's list.
1867 for( auto pe : current_parsed_events){
1868
1869 //----------------- Sort hit objects
1870
1871 // fADC250
1872 if(pe->vDf250PulseData.size()>1 ) sort(pe->vDf250PulseData.begin(), pe->vDf250PulseData.end(), SortByPulseNumber<Df250PulseData> );
1873 if(pe->vDf250PulseIntegral.size()>1) sort(pe->vDf250PulseIntegral.begin(), pe->vDf250PulseIntegral.end(), SortByPulseNumber<Df250PulseIntegral> );
1874 if(pe->vDf250PulseTime.size()>1 ) sort(pe->vDf250PulseTime.begin(), pe->vDf250PulseTime.end(), SortByPulseNumber<Df250PulseTime> );
1875 if(pe->vDf250PulsePedestal.size()>1) sort(pe->vDf250PulsePedestal.begin(), pe->vDf250PulsePedestal.end(), SortByPulseNumber<Df250PulsePedestal> );
1876
1877 // fADC125
1878 if(pe->vDf125PulseIntegral.size()>1) sort(pe->vDf125PulseIntegral.begin(), pe->vDf125PulseIntegral.end(), SortByPulseNumber<Df125PulseIntegral> );
1879 if(pe->vDf125CDCPulse.size()>1 ) sort(pe->vDf125CDCPulse.begin(), pe->vDf125CDCPulse.end(), SortByChannel<Df125CDCPulse> );
1880 if(pe->vDf125FDCPulse.size()>1 ) sort(pe->vDf125FDCPulse.begin(), pe->vDf125FDCPulse.end(), SortByChannel<Df125FDCPulse> );
1881 if(pe->vDf125PulseTime.size()>1 ) sort(pe->vDf125PulseTime.begin(), pe->vDf125PulseTime.end(), SortByPulseNumber<Df125PulseTime> );
1882 if(pe->vDf125PulsePedestal.size()>1) sort(pe->vDf125PulsePedestal.begin(), pe->vDf125PulsePedestal.end(), SortByPulseNumber<Df125PulsePedestal> );
1883
1884 // F1TDC
1885 if(pe->vDF1TDCHit.size()>1 ) sort(pe->vDF1TDCHit.begin(), pe->vDF1TDCHit.end(), SortByModule<DF1TDCHit> );
1886
1887 // CAEN1290TDC
1888 if(pe->vDCAEN1290TDCHit.size()>1 ) sort(pe->vDCAEN1290TDCHit.begin(), pe->vDCAEN1290TDCHit.end(), SortByModule<DCAEN1290TDCHit> );
1889
1890
1891 //----------------- Link hit objects
1892
1893 // Connect Df250 pulse objects
1894 LinkPulse(pe->vDf250PulseTime, pe->vDf250PulseIntegral);
1895 LinkPulsePedCopy(pe->vDf250PulsePedestal, pe->vDf250PulseIntegral);
1896
1897 // Connect Df125 pulse objects
1898 LinkPulse(pe->vDf125PulseTime, pe->vDf125PulseIntegral);
1899 LinkPulsePedCopy(pe->vDf125PulsePedestal, pe->vDf125PulseIntegral);
1900
1901 // Connect Df250 window raw data objects
1902 if(!pe->vDf250WindowRawData.empty()){
1903 LinkConfig(pe->vDf250Config, pe->vDf250WindowRawData);
1904 LinkModule(pe->vDf250TriggerTime, pe->vDf250WindowRawData);
1905 LinkChannel(pe->vDf250WindowRawData, pe->vDf250PulseIntegral);
1906 LinkChannel(pe->vDf250WindowRawData, pe->vDf250PulseTime);
1907 LinkChannel(pe->vDf250WindowRawData, pe->vDf250PulsePedestal);
1908 }
1909
1910 // Connect Df125 window raw data objects
1911 if(!pe->vDf125WindowRawData.empty()){
1912 LinkConfig(pe->vDf125Config, pe->vDf125WindowRawData);
1913 LinkModule(pe->vDf125TriggerTime, pe->vDf125WindowRawData);
1914 LinkChannel(pe->vDf125WindowRawData, pe->vDf125PulseIntegral);
1915 LinkChannel(pe->vDf125WindowRawData, pe->vDf125PulseTime);
1916 LinkChannel(pe->vDf125WindowRawData, pe->vDf125PulsePedestal);
1917 LinkChannel(pe->vDf125WindowRawData, pe->vDf125CDCPulse);
1918 LinkChannel(pe->vDf125WindowRawData, pe->vDf125FDCPulse);
1919 }
1920
1921 //----------------- Optionally link config objects (on by default)
1922 if(LINK_CONFIG){
1923 if(pe->vDf250Config.size()>1 ) sort(pe->vDf250Config.begin(), pe->vDf250Config.end(), SortByROCID<Df250Config> );
1924 if(pe->vDf125Config.size()>1 ) sort(pe->vDf125Config.begin(), pe->vDf125Config.end(), SortByROCID<Df125Config> );
1925 if(pe->vDF1TDCConfig.size()>1 ) sort(pe->vDF1TDCConfig.begin(), pe->vDF1TDCConfig.end(), SortByROCID<DF1TDCConfig> );
1926 if(pe->vDCAEN1290TDCConfig.size()>1) sort(pe->vDCAEN1290TDCConfig.begin(), pe->vDCAEN1290TDCConfig.end(), SortByROCID<DCAEN1290TDCConfig> );
1927
1928 LinkConfigSamplesCopy(pe->vDf250Config, pe->vDf250PulseIntegral);
1929 LinkConfigSamplesCopy(pe->vDf250Config, pe->vDf250PulseData);
1930 LinkConfigSamplesCopy(pe->vDf125Config, pe->vDf125PulseIntegral);
1931 LinkConfigSamplesCopy(pe->vDf125Config, pe->vDf125CDCPulse);
1932 LinkConfigSamplesCopy(pe->vDf125Config, pe->vDf125FDCPulse);
1933 LinkConfig(pe->vDF1TDCConfig, pe->vDF1TDCHit);
1934 LinkConfig(pe->vDCAEN1290TDCConfig, pe->vDCAEN1290TDCHit);
1935 }
1936
1937 //----------------- Optionally link trigger time objects (off by default)
1938 if(LINK_TRIGGERTIME){
1939 if(pe->vDf250TriggerTime.size()>1 ) sort(pe->vDf250TriggerTime.begin(), pe->vDf250TriggerTime.end(), SortByModule<Df250TriggerTime> );
1940 if(pe->vDf125TriggerTime.size()>1 ) sort(pe->vDf125TriggerTime.begin(), pe->vDf125TriggerTime.end(), SortByModule<Df125TriggerTime> );
1941 if(pe->vDF1TDCTriggerTime.size()>1 ) sort(pe->vDF1TDCTriggerTime.begin(), pe->vDF1TDCTriggerTime.end(), SortByModule<DF1TDCTriggerTime> );
1942
1943 LinkModule(pe->vDf250TriggerTime, pe->vDf250PulseIntegral);
1944 LinkModule(pe->vDf125TriggerTime, pe->vDf125PulseIntegral);
1945 LinkModule(pe->vDf125TriggerTime, pe->vDf125CDCPulse);
1946 LinkModule(pe->vDf125TriggerTime, pe->vDf125FDCPulse);
1947 LinkModule(pe->vDF1TDCTriggerTime, pe->vDF1TDCHit);
1948 }
1949 }
1950
1951}
1952
1953//----------------
1954// DumpBinary
1955//----------------
1956void DEVIOWorkerThread::DumpBinary(const uint32_t *iptr, const uint32_t *iend, uint32_t MaxWords, const uint32_t *imark)
1957{
1958 /// This is used for debugging. It will print to the screen the words
1959 /// starting at the address given by iptr and ending just before iend
1960 /// or for MaxWords words, whichever comes first. If iend is NULL,
1961 /// then MaxWords will be printed. If MaxWords is zero then it is ignored
1962 /// and only iend is checked. If both iend==NULL and MaxWords==0, then
1963 /// only the word at iptr is printed.
1964
1965 cout << "Dumping binary: istart=" << hex << iptr << " iend=" << iend << " MaxWords=" << dec << MaxWords << endl;
1966
1967 if(iend==NULL__null && MaxWords==0) MaxWords=1;
1968 if(MaxWords==0) MaxWords = (uint32_t)0xffffffff;
1969
1970 uint32_t Nwords=0;
1971 while(iptr!=iend && Nwords<MaxWords){
1972
1973 // line1 is hex and line2 is decimal
1974 stringstream line1, line2;
1975
1976 // print words in columns 8 words wide. First part is
1977 // reserved for word number
1978 uint32_t Ncols = 8;
1979 line1 << setw(5) << Nwords;
1980 line2 << string(5, ' ');
1981
1982 // Loop over columns
1983 for(uint32_t i=0; i<Ncols; i++, iptr++, Nwords++){
1984
1985 if(iptr == iend) break;
1986 if(Nwords>=MaxWords) break;
1987
1988 stringstream iptr_hex;
1989 iptr_hex << hex << "0x" << *iptr;
1990
1991 string mark = (iptr==imark ? "*":" ");
1992
1993 line1 << setw(12) << iptr_hex.str() << mark;
1994 line2 << setw(12) << *iptr << mark;
1995 }
1996
1997 cout << line1.str() << endl;
1998 cout << line2.str() << endl;
1999 cout << endl;
2000 }
2001}
2002
2003