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_number_within_block=0 indicates error
1244 if(event_number_within_block==0){
1245 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1245<<" "
<<"event_number_within_block==0. This indicates a bug in firmware." << endl;
1246 exit(-1);
1247 }
1248
1249 // Event headers may be supressed so determine event from hit data
1250 if( (event_number_within_block > current_parsed_events.size()) ) throw JException("Bad f250 event number", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1250);
1251 pe_iter = current_parsed_events.begin();
1252 advance( pe_iter, event_number_within_block-1 );
1253 pe = *pe_iter++;
1254
1255 itrigger = event_number_within_block; // is this right?
1256 uint32_t pulse_number = 0;
1257
1258 while( (*++iptr>>31) == 0 ){
1259
1260 if( (*iptr>>30) != 0x01) throw JException("Bad f250 Pulse Data!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1260);
1261
1262 // from word 2
1263 uint32_t integral = (*iptr>>12) & 0x3FFFF;
1264 bool QF_NSA_beyond_PTW = (*iptr>>11) & 0x01;
1265 bool QF_overflow = (*iptr>>10) & 0x01;
1266 bool QF_underflow = (*iptr>>9 ) & 0x01;
1267 uint32_t nsamples_over_threshold = (*iptr>>0 ) & 0x1FF;
1268
1269 iptr++;
1270 if( (*iptr>>30) != 0x00) throw JException("Bad f250 Pulse Data!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1270);
1271
1272 // from word 3
1273 uint32_t course_time = (*iptr>>21) & 0x1FF;//< 4 ns/count
1274 uint32_t fine_time = (*iptr>>15) & 0x3F;//< 0.0625 ns/count
1275 uint32_t pulse_peak = (*iptr>>3 ) & 0xFFF;
1276 bool QF_vpeak_beyond_NSA = (*iptr>>2 ) & 0x01;
1277 bool QF_vpeak_not_found = (*iptr>>1 ) & 0x01;
1278 bool QF_bad_pedestal = (*iptr>>0 ) & 0x01;
1279
1280 if( pe ) {
1281 pe->NEW_Df250PulseData(rocid, slot, channel, itrigger
1282 , event_number_within_block
1283 , QF_pedestal
1284 , pedestal
1285 , integral
1286 , QF_NSA_beyond_PTW
1287 , QF_overflow
1288 , QF_underflow
1289 , nsamples_over_threshold
1290 , course_time
1291 , fine_time
1292 , pulse_peak
1293 , QF_vpeak_beyond_NSA
1294 , QF_vpeak_not_found
1295 , QF_bad_pedestal
1296 , pulse_number++);
1297 }
1298 }
1299 iptr--; // backup so when outer loop advances, it points to next data defining word
1300
1301 }
1302 break;
1303 case 10: // Pulse Pedestal
1304 {
1305 uint32_t channel = (*iptr>>23) & 0x0F;
1306 uint32_t pulse_number = (*iptr>>21) & 0x03;
1307 uint32_t pedestal = (*iptr>>12) & 0x1FF;
1308 uint32_t pulse_peak = (*iptr>>0) & 0xFFF;
1309 if(VERBOSE>7) cout << " FADC250 Pulse Pedestal chan="<<channel<<" pulse_number="<<pulse_number<<" pedestal="<<pedestal<<" pulse_peak="<<pulse_peak<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1310 if(pe) pe->NEW_Df250PulsePedestal(rocid, slot, channel, itrigger, pulse_number, pedestal, pulse_peak);
1311 }
1312 break;
1313 case 13: // Event Trailer
1314 // This is marked "suppressed for normal readout – debug mode only" in the
1315 // current manual (v2). It does not contain any data so the most we could do here
1316 // is return early. I'm hesitant to do that though since it would mean
1317 // different behavior for debug mode data as regular data.
1318 case 14: // Data not valid (empty module)
1319 case 15: // Filler (non-data) word
1320 if(VERBOSE>7) cout << " FADC250 Event Trailer, Data not Valid, or Filler word ("<<data_type<<")"<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1321 break;
1322 }
1323 }
1324
1325 // Chop off filler words
1326 for(; iptr<iend; iptr++){
1327 if(((*iptr)&0xf8000000) != 0xf8000000) break;
1328 }
1329}
1330
1331//----------------
1332// MakeDf250WindowRawData
1333//----------------
1334void DEVIOWorkerThread::MakeDf250WindowRawData(DParsedEvent *pe, uint32_t rocid, uint32_t slot, uint32_t itrigger, uint32_t* &iptr)
1335{
1336 uint32_t channel = (*iptr>>23) & 0x0F;
1337 uint32_t window_width = (*iptr>>0) & 0x0FFF;
1338
1339 Df250WindowRawData *wrd = pe->NEW_Df250WindowRawData(rocid, slot, channel, itrigger);
1340
1341 for(uint32_t isample=0; isample<window_width; isample +=2){
1342
1343 // Advance to next word
1344 iptr++;
1345
1346 // Make sure this is a data continuation word, if not, stop here
1347 if(((*iptr>>31) & 0x1) != 0x0){
1348 iptr--; // calling method expects us to point to last word in block
1349 break;
1350 }
1351
1352 bool invalid_1 = (*iptr>>29) & 0x1;
1353 bool invalid_2 = (*iptr>>13) & 0x1;
1354 uint16_t sample_1 = 0;
1355 uint16_t sample_2 = 0;
1356 if(!invalid_1)sample_1 = (*iptr>>16) & 0x1FFF;
1357 if(!invalid_2)sample_2 = (*iptr>>0) & 0x1FFF;
1358
1359 // Sample 1
1360 wrd->samples.push_back(sample_1);
1361 wrd->invalid_samples |= invalid_1;
1362 wrd->overflow |= (sample_1>>12) & 0x1;
1363
1364 if(((isample+2) == window_width) && invalid_2)break; // skip last sample if flagged as invalid
1365
1366 // Sample 2
1367 wrd->samples.push_back(sample_2);
1368 wrd->invalid_samples |= invalid_2;
1369 wrd->overflow |= (sample_2>>12) & 0x1;
1370 }
1371}
1372
1373//----------------
1374// Parsef125Bank
1375//----------------
1376void DEVIOWorkerThread::Parsef125Bank(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
1377{
1378 if(!PARSE_F125){ iptr = &iptr[(*iptr) + 1]; return; }
1379
1380 auto pe_iter = current_parsed_events.begin();
1381 DParsedEvent *pe = NULL__null;
1382
1383 uint32_t slot=0;
1384 uint32_t itrigger = -1;
1385 uint32_t last_itrigger = -2;
1386 uint32_t last_pulse_time_channel=0;
1387 uint32_t last_slot = -1;
1388 uint32_t last_channel = -1;
1389
1390 // Loop over data words
1391 for(; iptr<iend; iptr++){
1392
1393 // Skip all non-data-type-defining words at this
1394 // level. When we do encounter one, the appropriate
1395 // case block below should handle parsing all of
1396 // the data continuation words and advance the iptr.
1397 if(((*iptr>>31) & 0x1) == 0)continue;
1398
1399 uint32_t data_type = (*iptr>>27) & 0x0F;
1400 switch(data_type){
1401 case 0: // Block Header
1402 slot = (*iptr>>22) & 0x1F;
1403 if(VERBOSE>7) cout << " FADC125 Block Header: slot="<<slot<<endl;
1404 break;
1405 case 1: // Block Trailer
1406 pe_iter = current_parsed_events.begin();
1407 pe = NULL__null;
1408 break;
1409 case 2: // Event Header
1410 //slot_event_header = (*iptr>>22) & 0x1F;
1411 itrigger = (*iptr>>0) & 0x3FFFFFF;
1412 pe = *pe_iter++;
1413 if(VERBOSE>7) cout << " FADC125 Event Header: itrigger="<<itrigger<<" last_itrigger="<<last_itrigger<<", rocid="<<rocid<<", slot="<<slot <<endl;
1414 break;
1415 case 3: // Trigger Time
1416 {
1417 uint64_t t = ((*iptr)&0xFFFFFF)<<0;
1418 iptr++;
1419 if(((*iptr>>31) & 0x1) == 0){
1420 t += ((*iptr)&0xFFFFFF)<<24; // from word on the street: second trigger time word is optional!!??
1421 }else{
1422 iptr--;
1423 }
1424 if(VERBOSE>7) cout << " FADC125 Trigger Time (t="<<t<<")"<<endl;
1425 if(pe) pe->NEW_Df125TriggerTime(rocid, slot, itrigger, t);
1426 }
1427 break;
1428 case 4: // Window Raw Data
1429 // iptr passed by reference and so will be updated automatically
1430 if(VERBOSE>7) cout << " FADC125 Window Raw Data"<<endl;
1431 if(pe) MakeDf125WindowRawData(pe, rocid, slot, itrigger, iptr);
1432 break;
1433
1434 case 5: // CDC pulse data (new) (GlueX-doc-2274-v8)
1435 {
1436 // Word 1:
1437 uint32_t word1 = *iptr;
1438 uint32_t channel = (*iptr>>20) & 0x7F;
1439 uint32_t pulse_number = (*iptr>>15) & 0x1F;
1440 uint32_t pulse_time = (*iptr>>4 ) & 0x7FF;
1441 uint32_t quality_factor = (*iptr>>3 ) & 0x1; //time QF bit
1442 uint32_t overflow_count = (*iptr>>0 ) & 0x7;
1443 if(VERBOSE>7){
1444 cout << " FADC125 CDC Pulse Data word1: " << hex << (*iptr) << dec << endl;
1445 cout << " FADC125 CDC Pulse Data (chan="<<channel<<" pulse="<<pulse_number<<" time="<<pulse_time<<" QF="<<quality_factor<<" OC="<<overflow_count<<")"<<endl;
1446 }
1447
1448 // Word 2:
1449 ++iptr;
1450 if(iptr>=iend){
1451 jerr << " Truncated f125 CDC hit (block ends before continuation word!)" << endl;
1452 continue;
1453 }
1454 if( ((*iptr>>31) & 0x1) != 0 ){
1455 jerr << " Truncated f125 CDC hit (missing continuation word!)" << endl;
1456 continue;
1457 }
1458 uint32_t word2 = *iptr;
1459 uint32_t pedestal = (*iptr>>23) & 0xFF;
1460 uint32_t sum = (*iptr>>9 ) & 0x3FFF;
1461 uint32_t pulse_peak = (*iptr>>0 ) & 0x1FF;
1462 if(VERBOSE>7){
1463 cout << " FADC125 CDC Pulse Data word2: " << hex << (*iptr) << dec << endl;
1464 cout << " FADC125 CDC Pulse Data (pedestal="<<pedestal<<" sum="<<sum<<" peak="<<pulse_peak<<")"<<endl;
1465 }
1466
1467 // Create hit objects
1468 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1469 uint32_t nsamples_pedestal = 1; // The firmware pedestal divided by 2^PBIT where PBIT is a config. parameter
1470
1471 if( pe ) {
1472 pe->NEW_Df125CDCPulse(rocid, slot, channel, itrigger
1473 , pulse_number // NPK
1474 , pulse_time // le_time
1475 , quality_factor // time_quality_bit
1476 , overflow_count // overflow_count
1477 , pedestal // pedestal
1478 , sum // integral
1479 , pulse_peak // first_max_amp
1480 , word1 // word1
1481 , word2 // word2
1482 , nsamples_pedestal // nsamples_pedestal
1483 , nsamples_integral // nsamples_integral
1484 , false); // emulated
1485 }
1486 }
1487 break;
1488
1489 case 6: // FDC pulse data-integral (new) (GlueX-doc-2274-v8)
1490 {
1491 // Word 1:
1492 uint32_t word1 = *iptr;
1493 uint32_t channel = (*iptr>>20) & 0x7F;
1494 uint32_t pulse_number = (*iptr>>15) & 0x1F;
1495 uint32_t pulse_time = (*iptr>>4 ) & 0x7FF;
1496 uint32_t quality_factor = (*iptr>>3 ) & 0x1; //time QF bit
1497 uint32_t overflow_count = (*iptr>>0 ) & 0x7;
1498 if(VERBOSE>7){
1499 cout << " FADC125 FDC Pulse Data(integral) word1: " << hex << (*iptr) << dec << endl;
1500 cout << " FADC125 FDC Pulse Data (chan="<<channel<<" pulse="<<pulse_number<<" time="<<pulse_time<<" QF="<<quality_factor<<" OC="<<overflow_count<<")"<<endl;
1501 }
1502
1503 // Word 2:
1504 ++iptr;
1505 if(iptr>=iend){
1506 jerr << " Truncated f125 FDC hit (block ends before continuation word!)" << endl;
1507 continue;
1508 }
1509 if( ((*iptr>>31) & 0x1) != 0 ){
1510 jerr << " Truncated f125 FDC hit (missing continuation word!)" << endl;
1511 continue;
1512 }
1513 uint32_t word2 = *iptr;
1514 uint32_t pulse_peak = 0;
1515 uint32_t sum = (*iptr>>19) & 0xFFF;
1516 uint32_t peak_time = (*iptr>>11) & 0xFF;
1517 uint32_t pedestal = (*iptr>>0 ) & 0x7FF;
1518 if(VERBOSE>7){
1519 cout << " FADC125 FDC Pulse Data(integral) word2: " << hex << (*iptr) << dec << endl;
1520 cout << " FADC125 FDC Pulse Data (integral="<<sum<<" time="<<peak_time<<" pedestal="<<pedestal<<")"<<endl;
1521 }
1522
1523 // Create hit objects
1524 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1525 uint32_t nsamples_pedestal = 1; // The firmware pedestal divided by 2^PBIT where PBIT is a config. parameter
1526
1527 if( pe ) {
1528 pe->NEW_Df125FDCPulse(rocid, slot, channel, itrigger
1529 , pulse_number // NPK
1530 , pulse_time // le_time
1531 , quality_factor // time_quality_bit
1532 , overflow_count // overflow_count
1533 , pedestal // pedestal
1534 , sum // integral
1535 , pulse_peak // peak_amp
1536 , peak_time // peak_time
1537 , word1 // word1
1538 , word2 // word2
1539 , nsamples_pedestal // nsamples_pedestal
1540 , nsamples_integral // nsamples_integral
1541 , false); // emulated
1542 }
1543 }
1544 break;
1545
1546 case 7: // Pulse Integral
1547 {
1548 if(VERBOSE>7) cout << " FADC125 Pulse Integral"<<endl;
1549 uint32_t channel = (*iptr>>20) & 0x7F;
1550 uint32_t sum = (*iptr>>0) & 0xFFFFF;
1551 uint32_t quality_factor = 0;
1552 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1553 uint32_t nsamples_pedestal = 1; // The firmware returns an already divided pedestal
1554 uint32_t pedestal = 0; // This will be replaced by the one from Df250PulsePedestal in GetObjects
1555 uint32_t pulse_number = 0;
1556 if (last_slot == slot && last_channel == channel) pulse_number = 1;
1557 last_slot = slot;
1558 last_channel = channel;
1559 if( pe ) pe->NEW_Df125PulseIntegral(rocid, slot, channel, itrigger, pulse_number, quality_factor, sum, pedestal, nsamples_integral, nsamples_pedestal);
1560 }
1561 break;
1562 case 8: // Pulse Time
1563 {
1564 if(VERBOSE>7) cout << " FADC125 Pulse Time"<<endl;
1565 uint32_t channel = (*iptr>>20) & 0x7F;
1566 uint32_t pulse_number = (*iptr>>18) & 0x03;
1567 uint32_t pulse_time = (*iptr>>0) & 0xFFFF;
1568 uint32_t quality_factor = 0;
1569 if( pe ) pe->NEW_Df125PulseTime(rocid, slot, channel, itrigger, pulse_number, quality_factor, pulse_time);
1570 last_pulse_time_channel = channel;
1571 }
1572 break;
1573
1574 case 9: // FDC pulse data-peak (new) (GlueX-doc-2274-v8)
1575 {
1576 // Word 1:
1577 uint32_t word1 = *iptr;
1578 uint32_t channel = (*iptr>>20) & 0x7F;
1579 uint32_t pulse_number = (*iptr>>15) & 0x1F;
1580 uint32_t pulse_time = (*iptr>>4 ) & 0x7FF;
1581 uint32_t quality_factor = (*iptr>>3 ) & 0x1; //time QF bit
1582 uint32_t overflow_count = (*iptr>>0 ) & 0x7;
1583 if(VERBOSE>7){
1584 cout << " FADC125 FDC Pulse Data(peak) word1: " << hex << (*iptr) << dec << endl;
1585 cout << " FADC125 FDC Pulse Data (chan="<<channel<<" pulse="<<pulse_number<<" time="<<pulse_time<<" QF="<<quality_factor<<" OC="<<overflow_count<<")"<<endl;
1586 }
1587
1588 // Word 2:
1589 ++iptr;
1590 if(iptr>=iend){
1591 jerr << " Truncated f125 FDC hit (block ends before continuation word!)" << endl;
1592 continue;
1593 }
1594 if( ((*iptr>>31) & 0x1) != 0 ){
1595 jerr << " Truncated f125 FDC hit (missing continuation word!)" << endl;
1596 continue;
1597 }
1598 uint32_t word2 = *iptr;
1599 uint32_t pulse_peak = (*iptr>>19) & 0xFFF;
1600 uint32_t sum = 0;
1601 uint32_t peak_time = (*iptr>>11) & 0xFF;
1602 uint32_t pedestal = (*iptr>>0 ) & 0x7FF;
1603 if(VERBOSE>7){
1604 cout << " FADC125 FDC Pulse Data(peak) word2: " << hex << (*iptr) << dec << endl;
1605 cout << " FADC125 FDC Pulse Data (integral="<<sum<<" time="<<peak_time<<" pedestal="<<pedestal<<")"<<endl;
1606 }
1607
1608 // Create hit objects
1609 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1610 uint32_t nsamples_pedestal = 1; // The firmware pedestal divided by 2^PBIT where PBIT is a config. parameter
1611
1612 if( pe ) {
1613 pe->NEW_Df125FDCPulse(rocid, slot, channel, itrigger
1614 , pulse_number // NPK
1615 , pulse_time // le_time
1616 , quality_factor // time_quality_bit
1617 , overflow_count // overflow_count
1618 , pedestal // pedestal
1619 , sum // integral
1620 , pulse_peak // peak_amp
1621 , peak_time // peak_time
1622 , word1 // word1
1623 , word2 // word2
1624 , nsamples_pedestal // nsamples_pedestal
1625 , nsamples_integral // nsamples_integral
1626 , false); // emulated
1627 }
1628 }
1629 break;
1630
1631 case 10: // Pulse Pedestal (consistent with Beni's hand-edited version of Cody's document)
1632 {
1633 if(VERBOSE>7) cout << " FADC125 Pulse Pedestal"<<endl;
1634 //channel = (*iptr>>20) & 0x7F;
1635 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")
1636 uint32_t pulse_number = (*iptr>>21) & 0x03;
1637 uint32_t pedestal = (*iptr>>12) & 0x1FF;
1638 uint32_t pulse_peak = (*iptr>>0) & 0xFFF;
1639 uint32_t nsamples_pedestal = 1; // The firmware returns an already divided pedestal
1640 if( pe ) pe->NEW_Df125PulsePedestal(rocid, slot, channel, itrigger, pulse_number, pedestal, pulse_peak, nsamples_pedestal);
1641 }
1642 break;
1643
1644 case 13: // Event Trailer
1645 case 14: // Data not valid (empty module)
1646 case 15: // Filler (non-data) word
1647 if(VERBOSE>7) cout << " FADC125 ignored data type: " << data_type <<endl;
1648 break;
1649 }
1650 }
1651
1652 // Chop off filler words
1653 for(; iptr<iend; iptr++){
1654 if(((*iptr)&0xf8000000) != 0xf8000000) break;
1655 }
1656}
1657
1658//----------------
1659// MakeDf125WindowRawData
1660//----------------
1661void DEVIOWorkerThread::MakeDf125WindowRawData(DParsedEvent *pe, uint32_t rocid, uint32_t slot, uint32_t itrigger, uint32_t* &iptr)
1662{
1663 uint32_t channel = (*iptr>>20) & 0x7F;
1664 uint32_t window_width = (*iptr>>0) & 0x0FFF;
1665
1666 Df125WindowRawData *wrd = pe->NEW_Df125WindowRawData(rocid, slot, channel, itrigger);
1667
1668 for(uint32_t isample=0; isample<window_width; isample +=2){
1669
1670 // Advance to next word
1671 iptr++;
1672
1673 // Make sure this is a data continuation word, if not, stop here
1674 if(((*iptr>>31) & 0x1) != 0x0)break;
1675
1676 bool invalid_1 = (*iptr>>29) & 0x1;
1677 bool invalid_2 = (*iptr>>13) & 0x1;
1678 uint16_t sample_1 = 0;
1679 uint16_t sample_2 = 0;
1680 if(!invalid_1)sample_1 = (*iptr>>16) & 0x1FFF;
1681 if(!invalid_2)sample_2 = (*iptr>>0) & 0x1FFF;
1682
1683 // Sample 1
1684 wrd->samples.push_back(sample_1);
1685 wrd->invalid_samples |= invalid_1;
1686 wrd->overflow |= (sample_1>>12) & 0x1;
1687
1688 if((isample+2) == window_width && invalid_2)break; // skip last sample if flagged as invalid
1689
1690 // Sample 2
1691 wrd->samples.push_back(sample_2);
1692 wrd->invalid_samples |= invalid_2;
1693 wrd->overflow |= (sample_2>>12) & 0x1;
1694 }
1695}
1696
1697//----------------
1698// ParseF1TDCBank
1699//----------------
1700void DEVIOWorkerThread::ParseF1TDCBank(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
1701{
1702 if(!PARSE_F1TDC){ iptr = &iptr[(*iptr) + 1]; return; }
1703
1704 uint32_t *istart = iptr;
1705
1706 auto pe_iter = current_parsed_events.begin();
1707 DParsedEvent *pe = NULL__null;
1708
1709 uint32_t slot = 0;
1710 uint32_t modtype = 0;
1711 uint32_t itrigger = -1;
1712 uint32_t trig_time_f1header = 0;
1713
1714 // Some early data had a marker word at just before the actual F1 data
1715 if(*iptr == 0xf1daffff) iptr++;
1716
1717 // Loop over data words
1718 for(; iptr<iend; iptr++){
1719
1720 // Skip all non-data-type-defining words at this
1721 // level. When we do encounter one, the appropriate
1722 // case block below should handle parsing all of
1723 // the data continuation words and advance the iptr.
1724 if(((*iptr>>31) & 0x1) == 0)continue;
1725
1726 uint32_t data_type = (*iptr>>27) & 0x0F;
1727 switch(data_type){
1728 case 0: // Block Header
1729 slot = (*iptr)>>22 & 0x001F;
1730 modtype = (*iptr)>>18 & 0x000F; // should match a DModuleType::type_id_t
1731 if(VERBOSE>7) cout << " F1 Block Header: slot=" << slot << " modtype=" << modtype << endl;
1732 break;
1733
1734 case 1: // Block Trailer
1735 pe_iter = current_parsed_events.begin();
1736 pe = NULL__null;
1737 if(VERBOSE>7) cout << " F1 Block Trailer" << endl;
1738 break;
1739
1740 case 2: // Event Header
1741 {
1742 pe = *pe_iter++;
1743 itrigger = (*iptr)>>0 & 0x0003FFFFF;
1744 if(VERBOSE>7) {
1745 uint32_t slot_event_header = (*iptr)>>22 & 0x00000001F;
1746 cout << " F1 Event Header: slot=" << slot_event_header << " itrigger=" << itrigger << endl;
1747 }
1748 }
1749 break;
1750
1751 case 3: // Trigger time
1752 {
1753 uint64_t t = ((*iptr)&0xFFFFFF)<<0;
1754 iptr++;
1755 if(((*iptr>>31) & 0x1) == 0){
1756 t += ((*iptr)&0xFFFFFF)<<24; // from word on the street: second trigger time word is optional!!??
1757 }else{
1758 iptr--;
1759 }
1760 if(VERBOSE>7) cout << " F1TDC Trigger Time (t="<<t<<")"<<endl;
1761 if(pe) pe->NEW_DF1TDCTriggerTime(rocid, slot, itrigger, t);
1762 }
1763 break;
1764
1765 case 8: // F1 Chip Header
1766 trig_time_f1header = ((*iptr)>> 7) & 0x1FF;
1767 if(VERBOSE>7) {
1768 uint32_t chip_f1header = ((*iptr)>> 3) & 0x07;
1769 uint32_t chan_on_chip_f1header = ((*iptr)>> 0) & 0x07; // this is always 7 in real data!
1770 uint32_t itrigger_f1header = ((*iptr)>>16) & 0x3F;
1771 cout << " Found F1 header: chip=" << chip_f1header << " chan=" << chan_on_chip_f1header << " itrig=" << itrigger_f1header << " trig_time=" << trig_time_f1header << endl;
1772 }
1773 break;
1774
1775 case 7: // F1 Data
1776 {
1777 uint32_t chip = (*iptr>>19) & 0x07;
1778 uint32_t chan_on_chip = (*iptr>>16) & 0x07;
1779 uint32_t time = (*iptr>> 0) & 0xFFFF;
1780 uint32_t channel = F1TDC_channel(chip, chan_on_chip, modtype);
1781 if(VERBOSE>7) cout << " Found F1 data : chip=" << chip << " chan=" << chan_on_chip << " time=" << time << endl;
1782 if(pe) pe->NEW_DF1TDCHit(rocid, slot, channel, itrigger, trig_time_f1header, time, *iptr, MODULE_TYPE(modtype));
1783 }
1784 break;
1785
1786 case 15: // Filler word
1787 if(VERBOSE>7) cout << " F1 filler word" << endl;
1788 case 14: // Data not valid (how to handle this?)
1789 break;
1790
1791 default:
1792 cerr<<endl;
1793 cout.flush(); cerr.flush();
1794 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1794<<" "
<<"Unknown data word in F1TDC block. Dumping for debugging:" << endl;
1795 for(const uint32_t *iiptr = istart; iiptr<iend; iiptr++){
1796 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1796<<" "
<<"0x"<<hex<<*iiptr<<dec;
1797 if(iiptr == iptr)cerr<<" <----";
1798 switch( (*iiptr) & 0xF8000000 ){
1799 case 0x80000000: cerr << " F1 Block Header"; break;
1800 case 0x90000000: cerr << " F1 Event Header"; break;
1801 case 0x98000000: cerr << " F1 Trigger time"; break;
1802 case 0xC0000000: cerr << " F1 Header"; break;
1803 case 0xB8000000: cerr << " F1 Data"; break;
1804 case 0x88000000: cerr << " F1 Block Trailer"; break;
1805 case 0xF8000000: cerr << " Filler word"; break;
1806 case 0xF0000000: cerr << " <module has no valid data>"; break;
1807 default: break;
1808 }
1809 cerr<<endl;
1810 if(iiptr > (iptr+4)) break;
1811 }
1812 throw JException("Unexpected word type in F1TDC block!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1812);
1813 break;
1814 }
1815 }
1816
1817 // Skip filler words
1818 while(iptr<iend && (*iptr&0xF8000000)==0xF8000000)iptr++;
1819}
1820
1821//----------------
1822// ParseDVertexBank
1823//----------------
1824void DEVIOWorkerThread::ParseDVertexBank(uint32_t* &iptr, uint32_t *iend)
1825{
1826 uint32_t Nwords = ((uint64_t)iend - (uint64_t)iptr)/sizeof(uint32_t);
1827 uint32_t Nwords_expected = 11; // ?
1828 if(Nwords != Nwords_expected){
1829 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1829<<" "
<< "DVertex size does not match expected!!" << endl;
1830 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1830<<" "
<< "Found " << Nwords << " words. Expected " << Nwords_expected << endl;
1831 }else{
1832 DParsedEvent *pe = current_parsed_events.back();
1833 DVertex *the_vertex = pe->NEW_DVertex();
1834
1835 uint64_t in_word = *iptr++; // 1st word, lo word; 2nd word, hi word
1836 uint64_t in_word_hi = *iptr++;
1837 in_word |= in_word_hi<<32;
1838 //uint64_t hi_word = *iptr++;
1839 double vertex_x_pos;
1840 memcpy(&vertex_x_pos, &in_word, sizeof(double));
1841 in_word = *iptr++; in_word_hi = *iptr++;
1842 in_word |= in_word_hi<<32;
1843 double vertex_y_pos;
1844 memcpy(&vertex_y_pos, &in_word, sizeof(double));
1845 in_word = *iptr++; in_word_hi = *iptr++;
1846 in_word |= in_word_hi<<32;
1847 double vertex_z_pos;
1848 memcpy(&vertex_z_pos, &in_word, sizeof(double));
1849 in_word = *iptr++; in_word_hi = *iptr++;
1850 in_word |= in_word_hi<<32;
1851 double vertex_t;
1852 memcpy(&vertex_t, &in_word, sizeof(double));
1853
1854 DVector3 vertex_position(vertex_x_pos, vertex_y_pos, vertex_z_pos);
1855 the_vertex->dSpacetimeVertex = DLorentzVector(vertex_position, vertex_t);
1856 the_vertex->dKinFitNDF = *iptr++;
1857
1858 in_word = *iptr++; in_word_hi = *iptr++;
1859 in_word |= in_word_hi<<32;
1860 memcpy(&(the_vertex->dKinFitChiSq), &in_word, sizeof(double));
1861 }
1862}
1863
1864
1865//----------------
1866// LinkAllAssociations
1867//----------------
1868void DEVIOWorkerThread::LinkAllAssociations(void)
1869{
1870
1871 /// Find objects that should be linked as "associated objects"
1872 /// of one another and add to each other's list.
1873 for( auto pe : current_parsed_events){
1874
1875 //----------------- Sort hit objects
1876
1877 // fADC250
1878 if(pe->vDf250PulseData.size()>1 ) sort(pe->vDf250PulseData.begin(), pe->vDf250PulseData.end(), SortByPulseNumber<Df250PulseData> );
1879 if(pe->vDf250PulseIntegral.size()>1) sort(pe->vDf250PulseIntegral.begin(), pe->vDf250PulseIntegral.end(), SortByPulseNumber<Df250PulseIntegral> );
1880 if(pe->vDf250PulseTime.size()>1 ) sort(pe->vDf250PulseTime.begin(), pe->vDf250PulseTime.end(), SortByPulseNumber<Df250PulseTime> );
1881 if(pe->vDf250PulsePedestal.size()>1) sort(pe->vDf250PulsePedestal.begin(), pe->vDf250PulsePedestal.end(), SortByPulseNumber<Df250PulsePedestal> );
1882
1883 // fADC125
1884 if(pe->vDf125PulseIntegral.size()>1) sort(pe->vDf125PulseIntegral.begin(), pe->vDf125PulseIntegral.end(), SortByPulseNumber<Df125PulseIntegral> );
1885 if(pe->vDf125CDCPulse.size()>1 ) sort(pe->vDf125CDCPulse.begin(), pe->vDf125CDCPulse.end(), SortByChannel<Df125CDCPulse> );
1886 if(pe->vDf125FDCPulse.size()>1 ) sort(pe->vDf125FDCPulse.begin(), pe->vDf125FDCPulse.end(), SortByChannel<Df125FDCPulse> );
1887 if(pe->vDf125PulseTime.size()>1 ) sort(pe->vDf125PulseTime.begin(), pe->vDf125PulseTime.end(), SortByPulseNumber<Df125PulseTime> );
1888 if(pe->vDf125PulsePedestal.size()>1) sort(pe->vDf125PulsePedestal.begin(), pe->vDf125PulsePedestal.end(), SortByPulseNumber<Df125PulsePedestal> );
1889
1890 // F1TDC
1891 if(pe->vDF1TDCHit.size()>1 ) sort(pe->vDF1TDCHit.begin(), pe->vDF1TDCHit.end(), SortByModule<DF1TDCHit> );
1892
1893 // CAEN1290TDC
1894 if(pe->vDCAEN1290TDCHit.size()>1 ) sort(pe->vDCAEN1290TDCHit.begin(), pe->vDCAEN1290TDCHit.end(), SortByModule<DCAEN1290TDCHit> );
1895
1896
1897 //----------------- Link hit objects
1898
1899 // Connect Df250 pulse objects
1900 LinkPulse(pe->vDf250PulseTime, pe->vDf250PulseIntegral);
1901 LinkPulsePedCopy(pe->vDf250PulsePedestal, pe->vDf250PulseIntegral);
1902
1903 // Connect Df125 pulse objects
1904 LinkPulse(pe->vDf125PulseTime, pe->vDf125PulseIntegral);
1905 LinkPulsePedCopy(pe->vDf125PulsePedestal, pe->vDf125PulseIntegral);
1906
1907 // Connect Df250 window raw data objects
1908 if(!pe->vDf250WindowRawData.empty()){
1909 LinkConfig(pe->vDf250Config, pe->vDf250WindowRawData);
1910 LinkModule(pe->vDf250TriggerTime, pe->vDf250WindowRawData);
1911 LinkChannel(pe->vDf250WindowRawData, pe->vDf250PulseIntegral);
1912 LinkChannel(pe->vDf250WindowRawData, pe->vDf250PulseTime);
1913 LinkChannel(pe->vDf250WindowRawData, pe->vDf250PulsePedestal);
1914 }
1915
1916 // Connect Df125 window raw data objects
1917 if(!pe->vDf125WindowRawData.empty()){
1918 LinkConfig(pe->vDf125Config, pe->vDf125WindowRawData);
1919 LinkModule(pe->vDf125TriggerTime, pe->vDf125WindowRawData);
1920 LinkChannel(pe->vDf125WindowRawData, pe->vDf125PulseIntegral);
1921 LinkChannel(pe->vDf125WindowRawData, pe->vDf125PulseTime);
1922 LinkChannel(pe->vDf125WindowRawData, pe->vDf125PulsePedestal);
1923 LinkChannel(pe->vDf125WindowRawData, pe->vDf125CDCPulse);
1924 LinkChannel(pe->vDf125WindowRawData, pe->vDf125FDCPulse);
1925 }
1926
1927 //----------------- Optionally link config objects (on by default)
1928 if(LINK_CONFIG){
1929 if(pe->vDf250Config.size()>1 ) sort(pe->vDf250Config.begin(), pe->vDf250Config.end(), SortByROCID<Df250Config> );
1930 if(pe->vDf125Config.size()>1 ) sort(pe->vDf125Config.begin(), pe->vDf125Config.end(), SortByROCID<Df125Config> );
1931 if(pe->vDF1TDCConfig.size()>1 ) sort(pe->vDF1TDCConfig.begin(), pe->vDF1TDCConfig.end(), SortByROCID<DF1TDCConfig> );
1932 if(pe->vDCAEN1290TDCConfig.size()>1) sort(pe->vDCAEN1290TDCConfig.begin(), pe->vDCAEN1290TDCConfig.end(), SortByROCID<DCAEN1290TDCConfig> );
1933
1934 LinkConfigSamplesCopy(pe->vDf250Config, pe->vDf250PulseIntegral);
1935 LinkConfigSamplesCopy(pe->vDf250Config, pe->vDf250PulseData);
1936 LinkConfigSamplesCopy(pe->vDf125Config, pe->vDf125PulseIntegral);
1937 LinkConfigSamplesCopy(pe->vDf125Config, pe->vDf125CDCPulse);
1938 LinkConfigSamplesCopy(pe->vDf125Config, pe->vDf125FDCPulse);
1939 LinkConfig(pe->vDF1TDCConfig, pe->vDF1TDCHit);
1940 LinkConfig(pe->vDCAEN1290TDCConfig, pe->vDCAEN1290TDCHit);
1941 }
1942
1943 //----------------- Optionally link trigger time objects (off by default)
1944 if(LINK_TRIGGERTIME){
1945 if(pe->vDf250TriggerTime.size()>1 ) sort(pe->vDf250TriggerTime.begin(), pe->vDf250TriggerTime.end(), SortByModule<Df250TriggerTime> );
1946 if(pe->vDf125TriggerTime.size()>1 ) sort(pe->vDf125TriggerTime.begin(), pe->vDf125TriggerTime.end(), SortByModule<Df125TriggerTime> );
1947 if(pe->vDF1TDCTriggerTime.size()>1 ) sort(pe->vDF1TDCTriggerTime.begin(), pe->vDF1TDCTriggerTime.end(), SortByModule<DF1TDCTriggerTime> );
1948
1949 LinkModule(pe->vDf250TriggerTime, pe->vDf250PulseIntegral);
1950 LinkModule(pe->vDf125TriggerTime, pe->vDf125PulseIntegral);
1951 LinkModule(pe->vDf125TriggerTime, pe->vDf125CDCPulse);
1952 LinkModule(pe->vDf125TriggerTime, pe->vDf125FDCPulse);
1953 LinkModule(pe->vDF1TDCTriggerTime, pe->vDF1TDCHit);
1954 }
1955 }
1956
1957}
1958
1959//----------------
1960// DumpBinary
1961//----------------
1962void DEVIOWorkerThread::DumpBinary(const uint32_t *iptr, const uint32_t *iend, uint32_t MaxWords, const uint32_t *imark)
1963{
1964 /// This is used for debugging. It will print to the screen the words
1965 /// starting at the address given by iptr and ending just before iend
1966 /// or for MaxWords words, whichever comes first. If iend is NULL,
1967 /// then MaxWords will be printed. If MaxWords is zero then it is ignored
1968 /// and only iend is checked. If both iend==NULL and MaxWords==0, then
1969 /// only the word at iptr is printed.
1970
1971 cout << "Dumping binary: istart=" << hex << iptr << " iend=" << iend << " MaxWords=" << dec << MaxWords << endl;
1972
1973 if(iend==NULL__null && MaxWords==0) MaxWords=1;
1974 if(MaxWords==0) MaxWords = (uint32_t)0xffffffff;
1975
1976 uint32_t Nwords=0;
1977 while(iptr!=iend && Nwords<MaxWords){
1978
1979 // line1 is hex and line2 is decimal
1980 stringstream line1, line2;
1981
1982 // print words in columns 8 words wide. First part is
1983 // reserved for word number
1984 uint32_t Ncols = 8;
1985 line1 << setw(5) << Nwords;
1986 line2 << string(5, ' ');
1987
1988 // Loop over columns
1989 for(uint32_t i=0; i<Ncols; i++, iptr++, Nwords++){
1990
1991 if(iptr == iend) break;
1992 if(Nwords>=MaxWords) break;
1993
1994 stringstream iptr_hex;
1995 iptr_hex << hex << "0x" << *iptr;
1996
1997 string mark = (iptr==imark ? "*":" ");
1998
1999 line1 << setw(12) << iptr_hex.str() << mark;
2000 line2 << setw(12) << *iptr << mark;
2001 }
2002
2003 cout << line1.str() << endl;
2004 cout << line2.str() << endl;
2005 cout << endl;
2006 }
2007}
2008
2009