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)/sizeof(uint32_t);
495 break;
496 case DModuleType::FADC125: // f125
497 f125conf = new Df125BORConfig;
498 dest = (uint32_t*)&f125conf->rocid;
499 sizeof_dest = sizeof(f125config)/sizeof(uint32_t);
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)/sizeof(uint32_t);
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)/sizeof(uint32_t);
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 ){
527 stringstream ss;
528 ss << "BOR module bank size does not match structure! " << module_len << " > " << sizeof_dest << " 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 // Set extra words to "0" at end of structure
534 // in case we are processing data from older firmware
535 for(uint32_t i=0; i<sizeof_dest; i++) *dest++ = i<module_len ? (*src++):0;
536
537 // Extract certain derived values to fill in convenience members
538 if(f250conf ) f250conf->FillDerived();
539
540 // Store object for use in this and subsequent events
541 if(f250conf ) borptrs->vDf250BORConfig.push_back(f250conf);
542 if(f125conf ) borptrs->vDf125BORConfig.push_back(f125conf);
543 if(F1TDCconf ) borptrs->vDF1TDCBORConfig.push_back(F1TDCconf);
544 if(caen1190conf) borptrs->vDCAEN1290TDCBORConfig.push_back(caen1190conf);
545
546 iptr = &iptr[module_len];
547 }
548
549 iptr = iend_crate; // ensure we're pointing past this crate
550 }
551
552 // Sort the BOR config events now so we don't have to do it for every event
553 borptrs->Sort();
554
555}
556
557//---------------------------------
558// ParseTSscalerBank
559//---------------------------------
560void DEVIOWorkerThread::ParseTSscalerBank(uint32_t* &iptr, uint32_t *iend)
561{
562 uint32_t Nwords = ((uint64_t)iend - (uint64_t)iptr)/sizeof(uint32_t);
563 uint32_t Nwords_expected = (6+32+16+32+16);
564 if(Nwords != Nwords_expected){
565 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<565<<" "
<< "TS bank size does not match expected!!" << endl;
566 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<566<<" "
<< "Found " << Nwords << " words. Expected " << Nwords_expected << endl;
567
568 }else{
569 // n.b. Get the last event here since if this is a block
570 // of events, the last should be the actual sync event.
571 DParsedEvent *pe = current_parsed_events.back();
572 DL1Info *s = pe->NEW_DL1Info();
573 s->nsync = *iptr++;
574 s->trig_number = *iptr++;
575 s->live_time = *iptr++;
576 s->busy_time = *iptr++;
577 s->live_inst = *iptr++;
578 s->unix_time = *iptr++;
579 for(uint32_t i=0; i<32; i++) s->gtp_sc.push_back ( *iptr++ );
580 for(uint32_t i=0; i<16; i++) s->fp_sc.push_back ( *iptr++ );
581 for(uint32_t i=0; i<32; i++) s->gtp_rate.push_back( *iptr++ );
582 for(uint32_t i=0; i<16; i++) s->fp_rate.push_back ( *iptr++ );
583 }
584
585 iptr = iend;
586}
587
588//---------------------------------
589// Parsef250scalerBank
590//---------------------------------
591void DEVIOWorkerThread::Parsef250scalerBank(uint32_t* &iptr, uint32_t *iend)
592{
593 iptr = &iptr[(*iptr) + 1];
594}
595
596//---------------------------------
597// ParseControlEvent
598//---------------------------------
599void DEVIOWorkerThread::ParseControlEvent(uint32_t* &iptr, uint32_t *iend)
600{
601 for(auto pe : current_parsed_events) pe->event_status_bits |= (1<<kSTATUS_CONTROL_EVENT);
602
603 iptr = &iptr[(*iptr) + 1];
604}
605
606//---------------------------------
607// ParsePhysicsBank
608//---------------------------------
609void DEVIOWorkerThread::ParsePhysicsBank(uint32_t* &iptr, uint32_t *iend)
610{
611
612 for(auto pe : current_parsed_events) pe->event_status_bits |= (1<<kSTATUS_PHYSICS_EVENT);
613
614 uint32_t physics_event_len = *iptr++;
615 uint32_t *iend_physics_event = &iptr[physics_event_len];
616 iptr++;
617
618 // Built Trigger Bank
619 uint32_t built_trigger_bank_len = *iptr;
620 uint32_t *iend_built_trigger_bank = &iptr[built_trigger_bank_len+1];
621 ParseBuiltTriggerBank(iptr, iend_built_trigger_bank);
622 iptr = iend_built_trigger_bank;
623
624 // Loop over Data banks
625 while( iptr < iend_physics_event ) {
626
627 uint32_t data_bank_len = *iptr;
628 uint32_t *iend_data_bank = &iptr[data_bank_len+1];
629
630 ParseDataBank(iptr, iend_data_bank);
631
632 iptr = iend_data_bank;
633 }
634
635 iptr = iend_physics_event;
636}
637
638//---------------------------------
639// ParseBuiltTriggerBank
640//---------------------------------
641void DEVIOWorkerThread::ParseBuiltTriggerBank(uint32_t* &iptr, uint32_t *iend)
642{
643 if(!PARSE_TRIGGER) return;
644
645 iptr++; // advance past length word
646 uint32_t mask = 0xFF202000;
647 if( ((*iptr) & mask) != mask ){
648 stringstream ss;
649 ss << "Bad header word in Built Trigger Bank: " << hex << *iptr;
650 throw JException(ss.str(), __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__650);
651 }
652
653 uint32_t tag = (*iptr)>>16; // 0xFF2X
654 uint32_t Nrocs = (*iptr++) & 0xFF;
655 uint32_t Mevents = current_parsed_events.size();
656
657 // sanity check:
658 if(Mevents == 0) {
659 stringstream ss;
660 ss << "DEVIOWorkerThread::ParseBuiltTriggerBank() called with zero events! "<<endl;
661 throw JException(ss.str(), __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__661);
662 }
663
664
665 //-------- Common data (64bit)
666 uint32_t common_header64 = *iptr++;
667 uint32_t common_header64_len = common_header64 & 0xFFFF;
668 uint64_t *iptr64 = (uint64_t*)iptr;
669 iptr = &iptr[common_header64_len];
670
671 // First event number
672 uint64_t first_event_num = *iptr64++;
673
674 // Hi and lo 32bit words in 64bit numbers seem to be
675 // switched for events read from ET, but not read from
676 // file. Not sure if this is in the swapping routine
677 if(event_source->source_type==event_source->kETSource) first_event_num = (first_event_num>>32) | (first_event_num<<32);
678
679 // Average timestamps
680 uint32_t Ntimestamps = (common_header64_len/2)-1;
681 if(tag & 0x2) Ntimestamps--; // subtract 1 for run number/type word if present
682 vector<uint64_t> avg_timestamps;
683 for(uint32_t i=0; i<Ntimestamps; i++) avg_timestamps.push_back(*iptr64++);
684
685 // run number and run type
686 uint32_t run_number = 0;
687 uint32_t run_type = 0;
688 if(tag & 0x02){
689 run_number = (*iptr64) >> 32;
690 run_type = (*iptr64) & 0xFFFFFFFF;
691 iptr64++;
692 }
693
694 //-------- Common data (16bit)
695 uint32_t common_header16 = *iptr++;
696 uint32_t common_header16_len = common_header16 & 0xFFFF;
697 uint16_t *iptr16 = (uint16_t*)iptr;
698 iptr = &iptr[common_header16_len];
699
700 vector<uint16_t> event_types;
701 for(uint32_t i=0; i<Mevents; i++) event_types.push_back(*iptr16++);
702
703 //-------- ROC data (32bit)
704 for(uint32_t iroc=0; iroc<Nrocs; iroc++){
705 uint32_t common_header32 = *iptr++;
706 uint32_t common_header32_len = common_header32 & 0xFFFF;
707 uint32_t rocid = common_header32 >> 24;
708
709 uint32_t Nwords_per_event = common_header32_len/Mevents;
710 for(auto pe : current_parsed_events){
711
712 DCODAROCInfo *codarocinfo = pe->NEW_DCODAROCInfo();
713 codarocinfo->rocid = rocid;
714
715 uint64_t ts_low = *iptr++;
716 uint64_t ts_high = *iptr++;
717 codarocinfo->timestamp = (ts_high<<32) + ts_low;
718 codarocinfo->misc.clear(); // could be recycled from previous event
719 for(uint32_t i=2; i<Nwords_per_event; i++) codarocinfo->misc.push_back(*iptr++);
720
721 if(iptr > iend){
722 throw JException("Bad data format in ParseBuiltTriggerBank!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__722);
723 }
724 }
725 }
726
727 //-------- Make DCODAEventInfo objects
728 uint64_t ievent = 0;
729 for(auto pe : current_parsed_events){
730
731 pe->run_number = run_number; // may be overwritten in JEventSource_EVIOpp::GetEvent()
732
733 DCODAEventInfo *codaeventinfo = pe->NEW_DCODAEventInfo();
734 codaeventinfo->run_number = run_number;
735 codaeventinfo->run_type = run_type;
736 codaeventinfo->event_number = first_event_num + ievent;
737 codaeventinfo->event_type = event_types.empty() ? 0:event_types[ievent];
738 codaeventinfo->avg_timestamp = avg_timestamps.empty() ? 0:avg_timestamps[ievent];
739 ievent++;
740 }
741}
742
743//---------------------------------
744// ParseDataBank
745//---------------------------------
746void DEVIOWorkerThread::ParseDataBank(uint32_t* &iptr, uint32_t *iend)
747{
748 // Physics Event's Data Bank header
749 iptr++; // advance past data bank length word
750 uint32_t rocid = ((*iptr)>>16) & 0xFFF;
751 iptr++;
752
753 // Loop over Data Block Banks
754 while(iptr < iend){
755
756 uint32_t data_block_bank_len = *iptr++;
757 uint32_t *iend_data_block_bank = &iptr[data_block_bank_len];
758 uint32_t data_block_bank_header = *iptr++;
759
760 // Not sure where this comes from, but it needs to be skipped if present
761 while( (*iptr==0xF800FAFA) && (iptr<iend) ) iptr++;
762
763 uint32_t det_id = (data_block_bank_header>>16) & 0xFFF;
764 switch(det_id){
765
766 case 20:
767 ParseCAEN1190(rocid, iptr, iend_data_block_bank);
768 break;
769
770 case 0x55:
771 ParseModuleConfiguration(rocid, iptr, iend_data_block_bank);
772 break;
773
774 case 0x56:
775 ParseEventTagBank(iptr, iend_data_block_bank);
776 break;
777
778 case 0:
779 case 1:
780 case 3:
781 case 6: // flash 250 module, MMD 2014/2/4
782 case 16: // flash 125 module (CDC), DL 2014/6/19
783 case 26: // F1 TDC module (BCAL), MMD 2014-07-31
784 ParseJLabModuleData(rocid, iptr, iend_data_block_bank);
785 break;
786
787 // These were implemented in the ROL for sync events
788 // as 0xEE02 and 0xEE05. However, that violates the
789 // spec. which reserves the top 4 bits as status bits
790 // (the first "E" should really be a "1". We just check
791 // other 12 bits here.
792 case 0xE02:
793 ParseTSscalerBank(iptr, iend);
794 break;
795 case 0xE05:
796 Parsef250scalerBank(iptr, iend);
797 break;
798 case 0xE10: // really wish Sascha would share when he does this stuff!
799 Parsef250scalerBank(iptr, iend);
800 break;
801
802 // When we write out single events in the offline, we also can save some
803 // higher level data objects to save disk space and speed up
804 // specialized processing (e.g. pi0 calibration)
805 case 0xD01:
806 ParseDVertexBank(iptr, iend);
807 break;
808
809 case 5:
810 // old ROL Beni used had this but I don't think its
811 // been used for years. Run 10390 seems to have
812 // this though (???)
813 break;
814
815
816 default:
817 jerr<<"Unknown module type ("<<det_id<<" = " << hex << det_id << dec << " ) encountered" << endl;
818// if(VERBOSE>5){
819 cout << "----- First few words to help with debugging -----" << endl;
820 cout.flush(); cerr.flush();
821 DumpBinary(&iptr[-2], iend, 32, &iptr[-1]);
822// }
823 }
824
825 iptr = iend_data_block_bank;
826 }
827
828}
829
830//----------------
831// ParseTIBank
832//----------------
833void DEVIOWorkerThread::ParseTIBank(uint32_t rocid, uint32_t* &iptr, uint32_t* iend)
834{
835 while(iptr<iend && ((*iptr) & 0xF8000000) != 0x88000000) iptr++; // Skip to JLab block trailer
836 iptr++; // advance past JLab block trailer
837 while(iptr<iend && *iptr == 0xF8000000) iptr++; // skip filler words after block trailer
838 //iptr = iend;
839}
840
841//----------------
842// ParseCAEN1190
843//----------------
844void DEVIOWorkerThread::ParseCAEN1190(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
845{
846 if(!PARSE_CAEN1290TDC){ iptr = &iptr[(*iptr) + 1]; return; }
847
848 /// Parse data from a CAEN 1190 or 1290 module
849 /// (See ppg. 72-74 of V1290_REV15.pdf manual)
850
851 uint32_t slot = 0;
852 uint32_t event_count = 0;
853 uint32_t word_count = 0;
854 uint32_t trigger_time_tag = 0;
855 uint32_t tdc_num = 0;
856 uint32_t event_id = 0;
857 uint32_t bunch_id = 0;
858
859 // We need to accomodate multi-event blocks where
860 // events are entangled (i.e. hits from event 1
861 // are mixed in between those of event 2,3,4,
862 // etc... With CAEN modules, we only know which
863 // event a hit came from by looking at the event_id
864 // in the TDC header. This value is only 12 bits
865 // and could roll over within an event block. This
866 // means we need to keep track of the order we
867 // encounter them in so it is maintained in the
868 // "events" container. The event_id order is kept
869 // in the "event_id_order" vector.
870 map<uint32_t, DParsedEvent*> events_by_event_id;
871
872 auto pe_iter = current_parsed_events.begin();
873 DParsedEvent *pe = NULL__null;
874
875 while(iptr<iend){
876
877 // This word appears to be appended to the data.
878 // Probably in the ROL. Ignore it if found.
879 if(*iptr == 0xd00dd00d) {
880 if(VERBOSE>7) cout << " CAEN skipping 0xd00dd00d word" << endl;
881 iptr++;
882 continue;
883 }
884
885 uint32_t type = (*iptr) >> 27;
886 uint32_t edge = 0; // 1=trailing, 0=leading
887 uint32_t channel = 0;
888 uint32_t tdc = 0;
889 uint32_t error_flags = 0;
890 switch(type){
891 case 0b01000: // Global Header
892 slot = (*iptr) & 0x1f;
893 event_count = ((*iptr)>>5) & 0xffffff;
894 if(VERBOSE>7) cout << " CAEN TDC Global Header (slot=" << slot << " , event count=" << event_count << ")" << endl;
895 break;
896 case 0b10000: // Global Trailer
897 slot = (*iptr) & 0x1f;
898 word_count = ((*iptr)>>5) & 0x7ffff;
899 if(VERBOSE>7) cout << " CAEN TDC Global Trailer (slot=" << slot << " , word count=" << word_count << ")" << endl;
900 slot = event_count = word_count = trigger_time_tag = tdc_num = event_id = bunch_id = 0;
901 break;
902 case 0b10001: // Global Trigger Time Tag
903 trigger_time_tag = ((*iptr)>>5) & 0x7ffffff;
904 if(VERBOSE>7) cout << " CAEN TDC Global Trigger Time Tag (tag=" << trigger_time_tag << ")" << endl;
905 break;
906 case 0b00001: // TDC Header
907 tdc_num = ((*iptr)>>24) & 0x03;
908 event_id = ((*iptr)>>12) & 0x0fff;
909 bunch_id = (*iptr) & 0x0fff;
910 if(events_by_event_id.find(event_id) == events_by_event_id.end()){
911 if(pe_iter == current_parsed_events.end()){
912 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<912<<" "
<< "CAEN1290TDC parser sees more events than CODA header! (>" << current_parsed_events.size() << ")" << endl;
913 for( auto p : events_by_event_id) cout << "id=" << p.first << endl;
914 iptr = iend;
915 exit(-1); // should we exit, or try and continue??
916 return;
917 }
918 pe = *pe_iter++;
919 events_by_event_id[event_id] = pe;
920 }else{
921 pe = events_by_event_id[event_id];
922 }
923 if(VERBOSE>7) cout << " CAEN TDC TDC Header (tdc=" << tdc_num <<" , event id=" << event_id <<" , bunch id=" << bunch_id << ")" << endl;
924 break;
925 case 0b00000: // TDC Measurement
926 edge = ((*iptr)>>26) & 0x01;
927 channel = ((*iptr)>>21) & 0x1f;
928 tdc = ((*iptr)>>0) & 0x1fffff;
929 if(VERBOSE>7) cout << " CAEN TDC TDC Measurement (" << (edge ? "trailing":"leading") << " , channel=" << channel << " , tdc=" << tdc << ")" << endl;
930
931 // Create DCAEN1290TDCHit object
932 if(pe) pe->NEW_DCAEN1290TDCHit(rocid, slot, channel, 0, edge, tdc_num, event_id, bunch_id, tdc);
933 break;
934 case 0b00100: // TDC Error
935 error_flags = (*iptr) & 0x7fff;
936 if(VERBOSE>7) cout << " CAEN TDC TDC Error (err flags=0x" << hex << error_flags << dec << ")" << endl;
937 break;
938 case 0b00011: // TDC Trailer
939 tdc_num = ((*iptr)>>24) & 0x03;
940 event_id = ((*iptr)>>12) & 0x0fff;
941 word_count = ((*iptr)>>0) & 0x0fff;
942 if(VERBOSE>7) cout << " CAEN TDC TDC Trailer (tdc=" << tdc_num <<" , event id=" << event_id <<" , word count=" << word_count << ")" << endl;
943 tdc_num = event_id = bunch_id = 0;
944 break;
945 case 0b11000: // Filler Word
946 if(VERBOSE>7) cout << " CAEN TDC Filler Word" << endl;
947 break;
948 default:
949 cout << "Unknown datatype: 0x" << hex << type << " full word: "<< *iptr << dec << endl;
950 }
951
952 iptr++;
953 }
954
955}
956
957//----------------
958// ParseModuleConfiguration
959//----------------
960void DEVIOWorkerThread::ParseModuleConfiguration(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
961{
962 if(!PARSE_CONFIG){ iptr = &iptr[(*iptr) + 1]; return; }
963
964 /// Parse a bank of module configuration data. These are configuration values
965 /// programmed into the module at the beginning of the run that may be needed
966 /// in the offline. For example, the number of samples to sum in a FADC pulse
967 /// integral.
968 ///
969 /// The bank has one or more sections, each describing parameters applicable
970 /// to a number of modules as indicated by a 24bit slot mask.
971 ///
972 /// This bank should appear only once per DAQ event which, if in multi-event
973 /// block mode, may have multiple L1 events. The parameters here will apply
974 /// to all L1 events in the block. This method will put the config objects
975 /// into each event in current_parsed_events. The config objects are duplicated
976 /// as needed so each event has its own, indepenent set of config object.
977
978 while(iptr < iend){
979 uint32_t slot_mask = (*iptr) & 0xFFFFFF;
980 uint32_t Nvals = ((*iptr) >> 24) & 0xFF;
981 iptr++;
982
983 // Events will be created in the first event (i.e. using its pool)
984 // but pointers are saved so we can use them to construct identical
985 // objects in all other event later
986 DParsedEvent *pe = current_parsed_events.front();
987
988 Df250Config *f250config = NULL__null;
989 Df125Config *f125config = NULL__null;
990 DF1TDCConfig *f1tdcconfig = NULL__null;
991 DCAEN1290TDCConfig *caen1290tdcconfig = NULL__null;
992
993 // Loop over all parameters in this section
994 for(uint32_t i=0; i< Nvals; i++){
995 if( iptr >= iend){
996 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<996<<" "
<< "DAQ Configuration bank corrupt! slot_mask=0x" << hex << slot_mask << dec << " Nvals="<< Nvals << endl;
997 exit(-1);
998 }
999
1000 daq_param_type ptype = (daq_param_type)((*iptr)>>16);
1001 uint16_t val = (*iptr) & 0xFFFF;
1002
1003 if(VERBOSE>6) cout << " DAQ parameter of type: 0x" << hex << ptype << dec << " found with value: " << val << endl;
1004
1005 // Create config object of correct type if needed and copy
1006 // parameter value into it.
1007 switch(ptype>>8){
1008
1009 // f250
1010 case 0x05:
1011 if( !f250config ) f250config = pe->NEW_Df250Config(rocid, slot_mask);
1012 switch(ptype){
1013 case kPARAM250_NSA : f250config->NSA = val; break;
1014 case kPARAM250_NSB : f250config->NSB = val; break;
1015 case kPARAM250_NSA_NSB : f250config->NSA_NSB = val; break;
1016 case kPARAM250_NPED : f250config->NPED = val; break;
1017 default: _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1017<<" "
<< "UNKNOWN DAQ Config Parameter type: 0x" << hex << ptype << dec << endl;
1018 }
1019 break;
1020
1021 // f125
1022 case 0x0F:
1023 if( !f125config ) f125config = pe->NEW_Df125Config(rocid, slot_mask);
1024 switch(ptype){
1025 case kPARAM125_NSA : f125config->NSA = val; break;
1026 case kPARAM125_NSB : f125config->NSB = val; break;
1027 case kPARAM125_NSA_NSB : f125config->NSA_NSB = val; break;
1028 case kPARAM125_NPED : f125config->NPED = val; break;
1029 case kPARAM125_WINWIDTH : f125config->WINWIDTH = val; break;
1030 case kPARAM125_PL : f125config->PL = val; break;
1031 case kPARAM125_NW : f125config->NW = val; break;
1032 case kPARAM125_NPK : f125config->NPK = val; break;
1033 case kPARAM125_P1 : f125config->P1 = val; break;
1034 case kPARAM125_P2 : f125config->P2 = val; break;
1035 case kPARAM125_PG : f125config->PG = val; break;
1036 case kPARAM125_IE : f125config->IE = val; break;
1037 case kPARAM125_H : f125config->H = val; break;
1038 case kPARAM125_TH : f125config->TH = val; break;
1039 case kPARAM125_TL : f125config->TL = val; break;
1040 case kPARAM125_IBIT : f125config->IBIT = val; break;
1041 case kPARAM125_ABIT : f125config->ABIT = val; break;
1042 case kPARAM125_PBIT : f125config->PBIT = val; break;
1043 default: _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1043<<" "
<< "UNKNOWN DAQ Config Parameter type: 0x" << hex << ptype << dec << endl;
1044 }
1045 break;
1046
1047 // F1TDC
1048 case 0x06:
1049 if( !f1tdcconfig ) f1tdcconfig = pe->NEW_DF1TDCConfig(rocid, slot_mask);
1050 switch(ptype){
1051 case kPARAMF1_REFCNT : f1tdcconfig->REFCNT = val; break;
1052 case kPARAMF1_TRIGWIN : f1tdcconfig->TRIGWIN = val; break;
1053 case kPARAMF1_TRIGLAT : f1tdcconfig->TRIGLAT = val; break;
1054 case kPARAMF1_HSDIV : f1tdcconfig->HSDIV = val; break;
1055 case kPARAMF1_BINSIZE : f1tdcconfig->BINSIZE = val; break;
1056 case kPARAMF1_REFCLKDIV : f1tdcconfig->REFCLKDIV = val; break;
1057 default: _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1057<<" "
<< "UNKNOWN DAQ Config Parameter type: 0x" << hex << ptype << dec << endl;
1058 }
1059 break;
1060
1061 // caen1290
1062 case 0x10:
1063 if( !caen1290tdcconfig ) caen1290tdcconfig = pe->NEW_DCAEN1290TDCConfig(rocid, slot_mask);
1064 switch(ptype){
1065 case kPARAMCAEN1290_WINWIDTH : caen1290tdcconfig->WINWIDTH = val; break;
1066 case kPARAMCAEN1290_WINOFFSET : caen1290tdcconfig->WINOFFSET = val; break;
1067 default: _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1067<<" "
<< "UNKNOWN DAQ Config Parameter type: 0x" << hex << ptype << dec << endl;
1068 }
1069 break;
1070
1071 default:
1072 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1072<<" "
<< "Unknown module type: 0x" << hex << (ptype>>8) << endl;
1073 exit(-1);
1074 }
1075
1076
1077 iptr++;
1078 }
1079
1080 // Make copies of all config objects for all other events
1081 for(auto tpe : current_parsed_events){
1082
1083 if(tpe == pe) continue; // first event already owns objects so skip it
1084
1085 if(f250config ) tpe->NEW_Df250Config(f250config);
1086 if(f125config ) tpe->NEW_Df125Config(f125config);
1087 if(f1tdcconfig ) tpe->NEW_DF1TDCConfig(f1tdcconfig);
1088 if(caen1290tdcconfig) tpe->NEW_DCAEN1290TDCConfig(caen1290tdcconfig);
1089 }
1090 }
1091}
1092
1093//----------------
1094// ParseJLabModuleData
1095//----------------
1096void DEVIOWorkerThread::ParseJLabModuleData(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
1097{
1098
1099 while(iptr<iend){
1100
1101 // Get module type from next word (bits 18-21)
1102 uint32_t mod_id = ((*iptr) >> 18) & 0x000F;
1103 MODULE_TYPE type = (MODULE_TYPE)mod_id;
1104 //cout << " rocid=" << rocid << " Encountered module type: " << type << " (=" << DModuleType::GetModule(type).GetName() << ") word=" << hex << (*iptr) << dec << endl;
1105
1106 switch(type){
1107 case DModuleType::FADC250:
1108 Parsef250Bank(rocid, iptr, iend);
1109 break;
1110
1111 case DModuleType::FADC125:
1112 Parsef125Bank(rocid, iptr, iend);
1113 break;
1114
1115 case DModuleType::F1TDC32:
1116 ParseF1TDCBank(rocid, iptr, iend);
1117 break;
1118
1119 case DModuleType::F1TDC48:
1120 ParseF1TDCBank(rocid, iptr, iend);
1121 break;
1122
1123 case DModuleType::TID:
1124 ParseTIBank(rocid, iptr, iend);
1125 /*
1126 // Ignore this data and skip over it
1127 while(iptr<iend && ((*iptr) & 0xF8000000) != 0x88000000) iptr++; // Skip to JLab block trailer
1128 iptr++; // advance past JLab block trailer
1129 while(iptr<iend && *iptr == 0xF8000000) iptr++; // skip filler words after block trailer
1130 break;
1131 */
1132 break;
1133
1134 case DModuleType::UNKNOWN:
1135 default:
1136 jerr<<"Unknown module type ("<<mod_id<<") iptr=0x" << hex << iptr << dec << endl;
1137
1138 while(iptr<iend && ((*iptr) & 0xF8000000) != 0x88000000) iptr++; // Skip to JLab block trailer
1139 iptr++; // advance past JLab block trailer
1140 while(iptr<iend && *iptr == 0xF8000000) iptr++; // skip filler words after block trailer
1141 break;
1142 }
1143 }
1144
1145}
1146
1147//----------------
1148// Parsef250Bank
1149//----------------
1150void DEVIOWorkerThread::Parsef250Bank(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
1151{
1152 if(!PARSE_F250){ iptr = &iptr[(*iptr) + 1]; return; }
1153
1154 auto pe_iter = current_parsed_events.begin();
1155 DParsedEvent *pe = NULL__null;
1156
1157 uint32_t slot = 0;
1158 uint32_t itrigger = -1;
1159
1160 // Loop over data words
1161 for(; iptr<iend; iptr++){
1162
1163 // Skip all non-data-type-defining words at this
1164 // level. When we do encounter one, the appropriate
1165 // case block below should handle parsing all of
1166 // the data continuation words and advance the iptr.
1167 if(((*iptr>>31) & 0x1) == 0)continue;
1168
1169 uint32_t data_type = (*iptr>>27) & 0x0F;
1170 switch(data_type){
1171 case 0: // Block Header
1172 slot = (*iptr>>22) & 0x1F;
1173 if(VERBOSE>7) cout << " FADC250 Block Header: slot="<<slot<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1174 break;
1175 case 1: // Block Trailer
1176 pe_iter = current_parsed_events.begin();
1177 pe = NULL__null;
1178 if(VERBOSE>7) cout << " FADC250 Block Trailer"<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1179 break;
1180 case 2: // Event Header
1181 itrigger = (*iptr>>0) & 0x3FFFFF;
1182 pe = *pe_iter++;
1183 if(VERBOSE>7) cout << " FADC250 Event Header: itrigger="<<itrigger<<", rocid="<<rocid<<", slot="<<slot<<")" <<" ("<<hex<<*iptr<<dec<<")" <<endl;
1184 break;
1185 case 3: // Trigger Time
1186 {
1187 uint64_t t = ((*iptr)&0xFFFFFF)<<0;
1188 iptr++;
1189 if(((*iptr>>31) & 0x1) == 0){
1190 t += ((*iptr)&0xFFFFFF)<<24; // from word on the street: second trigger time word is optional!!??
1191 if(VERBOSE>7) cout << " Trigger time high word="<<(((*iptr)&0xFFFFFF))<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1192 }else{
1193 iptr--;
1194 }
1195 if(VERBOSE>7) cout << " FADC250 Trigger Time: t="<<t<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1196 if(pe) pe->NEW_Df250TriggerTime(rocid, slot, itrigger, t);
1197 }
1198 break;
1199 case 4: // Window Raw Data
1200 // iptr passed by reference and so will be updated automatically
1201 if(VERBOSE>7) cout << " FADC250 Window Raw Data"<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1202 if(pe) MakeDf250WindowRawData(pe, rocid, slot, itrigger, iptr);
1203 break;
1204 case 5: // Window Sum
1205 {
1206 uint32_t channel = (*iptr>>23) & 0x0F;
1207 uint32_t sum = (*iptr>>0) & 0x3FFFFF;
1208 uint32_t overflow = (*iptr>>22) & 0x1;
1209 if(VERBOSE>7) cout << " FADC250 Window Sum"<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1210 if(pe) pe->NEW_Df250WindowSum(rocid, slot, channel, itrigger, sum, overflow);
1211 }
1212 break;
1213 case 6: // Pulse Raw Data
1214// MakeDf250PulseRawData(objs, rocid, slot, itrigger, iptr);
1215 if(VERBOSE>7) cout << " FADC250 Pulse Raw Data"<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1216 break;
1217 case 7: // Pulse Integral
1218 {
1219 uint32_t channel = (*iptr>>23) & 0x0F;
1220 uint32_t pulse_number = (*iptr>>21) & 0x03;
1221 uint32_t quality_factor = (*iptr>>19) & 0x03;
1222 uint32_t sum = (*iptr>>0) & 0x7FFFF;
1223 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1224 uint32_t nsamples_pedestal = 1; // The firmware returns an already divided pedestal
1225 uint32_t pedestal = 0; // This will be replaced by the one from Df250PulsePedestal in GetObjects
1226 if(VERBOSE>7) cout << " FADC250 Pulse Integral: chan="<<channel<<" pulse_number="<<pulse_number<<" sum="<<sum<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1227 if(pe) pe->NEW_Df250PulseIntegral(rocid, slot, channel, itrigger, pulse_number, quality_factor, sum, pedestal, nsamples_integral, nsamples_pedestal);
1228 }
1229 break;
1230 case 8: // Pulse Time
1231 {
1232 uint32_t channel = (*iptr>>23) & 0x0F;
1233 uint32_t pulse_number = (*iptr>>21) & 0x03;
1234 uint32_t quality_factor = (*iptr>>19) & 0x03;
1235 uint32_t pulse_time = (*iptr>>0) & 0x7FFFF;
1236 if(VERBOSE>7) cout << " FADC250 Pulse Time: chan="<<channel<<" pulse_number="<<pulse_number<<" pulse_time="<<pulse_time<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1237 if(pe) pe->NEW_Df250PulseTime(rocid, slot, channel, itrigger, pulse_number, quality_factor, pulse_time);
1238 }
1239 break;
1240 case 9: // Pulse Data (firmware instroduce in Fall 2016)
1241 {
1242 // from word 1
1243 uint32_t event_number_within_block = (*iptr>>19) & 0xFF;
1244 uint32_t channel = (*iptr>>15) & 0x0F;
1245 bool QF_pedestal = (*iptr>>14) & 0x01;
1246 uint32_t pedestal = (*iptr>>0 ) & 0x3FFF;
1247
1248 // event_number_within_block=0 indicates error
1249 if(event_number_within_block==0){
1250 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1250<<" "
<<"event_number_within_block==0. This indicates a bug in firmware." << endl;
1251 exit(-1);
1252 }
1253
1254 // Event headers may be supressed so determine event from hit data
1255 if( (event_number_within_block > current_parsed_events.size()) ) throw JException("Bad f250 event number", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1255);
1256 pe_iter = current_parsed_events.begin();
1257 advance( pe_iter, event_number_within_block-1 );
1258 pe = *pe_iter++;
1259
1260 itrigger = event_number_within_block; // is this right?
1261 uint32_t pulse_number = 0;
1262
1263 while( (*++iptr>>31) == 0 ){
1264
1265 if( (*iptr>>30) != 0x01) throw JException("Bad f250 Pulse Data!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1265);
1266
1267 // from word 2
1268 uint32_t integral = (*iptr>>12) & 0x3FFFF;
1269 bool QF_NSA_beyond_PTW = (*iptr>>11) & 0x01;
1270 bool QF_overflow = (*iptr>>10) & 0x01;
1271 bool QF_underflow = (*iptr>>9 ) & 0x01;
1272 uint32_t nsamples_over_threshold = (*iptr>>0 ) & 0x1FF;
1273
1274 iptr++;
1275 if( (*iptr>>30) != 0x00) throw JException("Bad f250 Pulse Data!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1275);
1276
1277 // from word 3
1278 uint32_t course_time = (*iptr>>21) & 0x1FF;//< 4 ns/count
1279 uint32_t fine_time = (*iptr>>15) & 0x3F;//< 0.0625 ns/count
1280 uint32_t pulse_peak = (*iptr>>3 ) & 0xFFF;
1281 bool QF_vpeak_beyond_NSA = (*iptr>>2 ) & 0x01;
1282 bool QF_vpeak_not_found = (*iptr>>1 ) & 0x01;
1283 bool QF_bad_pedestal = (*iptr>>0 ) & 0x01;
1284
1285 if( pe ) {
1286 pe->NEW_Df250PulseData(rocid, slot, channel, itrigger
1287 , event_number_within_block
1288 , QF_pedestal
1289 , pedestal
1290 , integral
1291 , QF_NSA_beyond_PTW
1292 , QF_overflow
1293 , QF_underflow
1294 , nsamples_over_threshold
1295 , course_time
1296 , fine_time
1297 , pulse_peak
1298 , QF_vpeak_beyond_NSA
1299 , QF_vpeak_not_found
1300 , QF_bad_pedestal
1301 , pulse_number++);
1302 }
1303 }
1304 iptr--; // backup so when outer loop advances, it points to next data defining word
1305
1306 }
1307 break;
1308 case 10: // Pulse Pedestal
1309 {
1310 uint32_t channel = (*iptr>>23) & 0x0F;
1311 uint32_t pulse_number = (*iptr>>21) & 0x03;
1312 uint32_t pedestal = (*iptr>>12) & 0x1FF;
1313 uint32_t pulse_peak = (*iptr>>0) & 0xFFF;
1314 if(VERBOSE>7) cout << " FADC250 Pulse Pedestal chan="<<channel<<" pulse_number="<<pulse_number<<" pedestal="<<pedestal<<" pulse_peak="<<pulse_peak<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1315 if(pe) pe->NEW_Df250PulsePedestal(rocid, slot, channel, itrigger, pulse_number, pedestal, pulse_peak);
1316 }
1317 break;
1318 case 13: // Event Trailer
1319 // This is marked "suppressed for normal readout – debug mode only" in the
1320 // current manual (v2). It does not contain any data so the most we could do here
1321 // is return early. I'm hesitant to do that though since it would mean
1322 // different behavior for debug mode data as regular data.
1323 case 14: // Data not valid (empty module)
1324 case 15: // Filler (non-data) word
1325 if(VERBOSE>7) cout << " FADC250 Event Trailer, Data not Valid, or Filler word ("<<data_type<<")"<<" ("<<hex<<*iptr<<dec<<")"<<endl;
1326 break;
1327 }
1328 }
1329
1330 // Chop off filler words
1331 for(; iptr<iend; iptr++){
1332 if(((*iptr)&0xf8000000) != 0xf8000000) break;
1333 }
1334}
1335
1336//----------------
1337// MakeDf250WindowRawData
1338//----------------
1339void DEVIOWorkerThread::MakeDf250WindowRawData(DParsedEvent *pe, uint32_t rocid, uint32_t slot, uint32_t itrigger, uint32_t* &iptr)
1340{
1341 uint32_t channel = (*iptr>>23) & 0x0F;
1342 uint32_t window_width = (*iptr>>0) & 0x0FFF;
1343
1344 Df250WindowRawData *wrd = pe->NEW_Df250WindowRawData(rocid, slot, channel, itrigger);
1345
1346 for(uint32_t isample=0; isample<window_width; isample +=2){
1347
1348 // Advance to next word
1349 iptr++;
1350
1351 // Make sure this is a data continuation word, if not, stop here
1352 if(((*iptr>>31) & 0x1) != 0x0){
1353 iptr--; // calling method expects us to point to last word in block
1354 break;
1355 }
1356
1357 bool invalid_1 = (*iptr>>29) & 0x1;
1358 bool invalid_2 = (*iptr>>13) & 0x1;
1359 uint16_t sample_1 = 0;
1360 uint16_t sample_2 = 0;
1361 if(!invalid_1)sample_1 = (*iptr>>16) & 0x1FFF;
1362 if(!invalid_2)sample_2 = (*iptr>>0) & 0x1FFF;
1363
1364 // Sample 1
1365 wrd->samples.push_back(sample_1);
1366 wrd->invalid_samples |= invalid_1;
1367 wrd->overflow |= (sample_1>>12) & 0x1;
1368
1369 if(((isample+2) == window_width) && invalid_2)break; // skip last sample if flagged as invalid
1370
1371 // Sample 2
1372 wrd->samples.push_back(sample_2);
1373 wrd->invalid_samples |= invalid_2;
1374 wrd->overflow |= (sample_2>>12) & 0x1;
1375 }
1376}
1377
1378//----------------
1379// Parsef125Bank
1380//----------------
1381void DEVIOWorkerThread::Parsef125Bank(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
1382{
1383 if(!PARSE_F125){ iptr = &iptr[(*iptr) + 1]; return; }
1384
1385 auto pe_iter = current_parsed_events.begin();
1386 DParsedEvent *pe = NULL__null;
1387
1388 uint32_t slot=0;
1389 uint32_t itrigger = -1;
1390 uint32_t last_itrigger = -2;
1391 uint32_t last_pulse_time_channel=0;
1392 uint32_t last_slot = -1;
1393 uint32_t last_channel = -1;
1394
1395 // Loop over data words
1396 for(; iptr<iend; iptr++){
1397
1398 // Skip all non-data-type-defining words at this
1399 // level. When we do encounter one, the appropriate
1400 // case block below should handle parsing all of
1401 // the data continuation words and advance the iptr.
1402 if(((*iptr>>31) & 0x1) == 0)continue;
1403
1404 uint32_t data_type = (*iptr>>27) & 0x0F;
1405 switch(data_type){
1406 case 0: // Block Header
1407 slot = (*iptr>>22) & 0x1F;
1408 if(VERBOSE>7) cout << " FADC125 Block Header: slot="<<slot<<endl;
1409 break;
1410 case 1: // Block Trailer
1411 pe_iter = current_parsed_events.begin();
1412 pe = NULL__null;
1413 break;
1414 case 2: // Event Header
1415 //slot_event_header = (*iptr>>22) & 0x1F;
1416 itrigger = (*iptr>>0) & 0x3FFFFFF;
1417 pe = *pe_iter++;
1418 if(VERBOSE>7) cout << " FADC125 Event Header: itrigger="<<itrigger<<" last_itrigger="<<last_itrigger<<", rocid="<<rocid<<", slot="<<slot <<endl;
1419 break;
1420 case 3: // Trigger Time
1421 {
1422 uint64_t t = ((*iptr)&0xFFFFFF)<<0;
1423 iptr++;
1424 if(((*iptr>>31) & 0x1) == 0){
1425 t += ((*iptr)&0xFFFFFF)<<24; // from word on the street: second trigger time word is optional!!??
1426 }else{
1427 iptr--;
1428 }
1429 if(VERBOSE>7) cout << " FADC125 Trigger Time (t="<<t<<")"<<endl;
1430 if(pe) pe->NEW_Df125TriggerTime(rocid, slot, itrigger, t);
1431 }
1432 break;
1433 case 4: // Window Raw Data
1434 // iptr passed by reference and so will be updated automatically
1435 if(VERBOSE>7) cout << " FADC125 Window Raw Data"<<endl;
1436 if(pe) MakeDf125WindowRawData(pe, rocid, slot, itrigger, iptr);
1437 break;
1438
1439 case 5: // CDC pulse data (new) (GlueX-doc-2274-v8)
1440 {
1441 // Word 1:
1442 uint32_t word1 = *iptr;
1443 uint32_t channel = (*iptr>>20) & 0x7F;
1444 uint32_t pulse_number = (*iptr>>15) & 0x1F;
1445 uint32_t pulse_time = (*iptr>>4 ) & 0x7FF;
1446 uint32_t quality_factor = (*iptr>>3 ) & 0x1; //time QF bit
1447 uint32_t overflow_count = (*iptr>>0 ) & 0x7;
1448 if(VERBOSE>7){
1449 cout << " FADC125 CDC Pulse Data word1: " << hex << (*iptr) << dec << endl;
1450 cout << " FADC125 CDC Pulse Data (chan="<<channel<<" pulse="<<pulse_number<<" time="<<pulse_time<<" QF="<<quality_factor<<" OC="<<overflow_count<<")"<<endl;
1451 }
1452
1453 // Word 2:
1454 ++iptr;
1455 if(iptr>=iend){
1456 jerr << " Truncated f125 CDC hit (block ends before continuation word!)" << endl;
1457 continue;
1458 }
1459 if( ((*iptr>>31) & 0x1) != 0 ){
1460 jerr << " Truncated f125 CDC hit (missing continuation word!)" << endl;
1461 continue;
1462 }
1463 uint32_t word2 = *iptr;
1464 uint32_t pedestal = (*iptr>>23) & 0xFF;
1465 uint32_t sum = (*iptr>>9 ) & 0x3FFF;
1466 uint32_t pulse_peak = (*iptr>>0 ) & 0x1FF;
1467 if(VERBOSE>7){
1468 cout << " FADC125 CDC Pulse Data word2: " << hex << (*iptr) << dec << endl;
1469 cout << " FADC125 CDC Pulse Data (pedestal="<<pedestal<<" sum="<<sum<<" peak="<<pulse_peak<<")"<<endl;
1470 }
1471
1472 // Create hit objects
1473 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1474 uint32_t nsamples_pedestal = 1; // The firmware pedestal divided by 2^PBIT where PBIT is a config. parameter
1475
1476 if( pe ) {
1477 pe->NEW_Df125CDCPulse(rocid, slot, channel, itrigger
1478 , pulse_number // NPK
1479 , pulse_time // le_time
1480 , quality_factor // time_quality_bit
1481 , overflow_count // overflow_count
1482 , pedestal // pedestal
1483 , sum // integral
1484 , pulse_peak // first_max_amp
1485 , word1 // word1
1486 , word2 // word2
1487 , nsamples_pedestal // nsamples_pedestal
1488 , nsamples_integral // nsamples_integral
1489 , false); // emulated
1490 }
1491 }
1492 break;
1493
1494 case 6: // FDC pulse data-integral (new) (GlueX-doc-2274-v8)
1495 {
1496 // Word 1:
1497 uint32_t word1 = *iptr;
1498 uint32_t channel = (*iptr>>20) & 0x7F;
1499 uint32_t pulse_number = (*iptr>>15) & 0x1F;
1500 uint32_t pulse_time = (*iptr>>4 ) & 0x7FF;
1501 uint32_t quality_factor = (*iptr>>3 ) & 0x1; //time QF bit
1502 uint32_t overflow_count = (*iptr>>0 ) & 0x7;
1503 if(VERBOSE>7){
1504 cout << " FADC125 FDC Pulse Data(integral) word1: " << hex << (*iptr) << dec << endl;
1505 cout << " FADC125 FDC Pulse Data (chan="<<channel<<" pulse="<<pulse_number<<" time="<<pulse_time<<" QF="<<quality_factor<<" OC="<<overflow_count<<")"<<endl;
1506 }
1507
1508 // Word 2:
1509 ++iptr;
1510 if(iptr>=iend){
1511 jerr << " Truncated f125 FDC hit (block ends before continuation word!)" << endl;
1512 continue;
1513 }
1514 if( ((*iptr>>31) & 0x1) != 0 ){
1515 jerr << " Truncated f125 FDC hit (missing continuation word!)" << endl;
1516 continue;
1517 }
1518 uint32_t word2 = *iptr;
1519 uint32_t pulse_peak = 0;
1520 uint32_t sum = (*iptr>>19) & 0xFFF;
1521 uint32_t peak_time = (*iptr>>11) & 0xFF;
1522 uint32_t pedestal = (*iptr>>0 ) & 0x7FF;
1523 if(VERBOSE>7){
1524 cout << " FADC125 FDC Pulse Data(integral) word2: " << hex << (*iptr) << dec << endl;
1525 cout << " FADC125 FDC Pulse Data (integral="<<sum<<" time="<<peak_time<<" pedestal="<<pedestal<<")"<<endl;
1526 }
1527
1528 // Create hit objects
1529 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1530 uint32_t nsamples_pedestal = 1; // The firmware pedestal divided by 2^PBIT where PBIT is a config. parameter
1531
1532 if( pe ) {
1533 pe->NEW_Df125FDCPulse(rocid, slot, channel, itrigger
1534 , pulse_number // NPK
1535 , pulse_time // le_time
1536 , quality_factor // time_quality_bit
1537 , overflow_count // overflow_count
1538 , pedestal // pedestal
1539 , sum // integral
1540 , pulse_peak // peak_amp
1541 , peak_time // peak_time
1542 , word1 // word1
1543 , word2 // word2
1544 , nsamples_pedestal // nsamples_pedestal
1545 , nsamples_integral // nsamples_integral
1546 , false); // emulated
1547 }
1548 }
1549 break;
1550
1551 case 7: // Pulse Integral
1552 {
1553 if(VERBOSE>7) cout << " FADC125 Pulse Integral"<<endl;
1554 uint32_t channel = (*iptr>>20) & 0x7F;
1555 uint32_t sum = (*iptr>>0) & 0xFFFFF;
1556 uint32_t quality_factor = 0;
1557 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1558 uint32_t nsamples_pedestal = 1; // The firmware returns an already divided pedestal
1559 uint32_t pedestal = 0; // This will be replaced by the one from Df250PulsePedestal in GetObjects
1560 uint32_t pulse_number = 0;
1561 if (last_slot == slot && last_channel == channel) pulse_number = 1;
1562 last_slot = slot;
1563 last_channel = channel;
1564 if( pe ) pe->NEW_Df125PulseIntegral(rocid, slot, channel, itrigger, pulse_number, quality_factor, sum, pedestal, nsamples_integral, nsamples_pedestal);
1565 }
1566 break;
1567 case 8: // Pulse Time
1568 {
1569 if(VERBOSE>7) cout << " FADC125 Pulse Time"<<endl;
1570 uint32_t channel = (*iptr>>20) & 0x7F;
1571 uint32_t pulse_number = (*iptr>>18) & 0x03;
1572 uint32_t pulse_time = (*iptr>>0) & 0xFFFF;
1573 uint32_t quality_factor = 0;
1574 if( pe ) pe->NEW_Df125PulseTime(rocid, slot, channel, itrigger, pulse_number, quality_factor, pulse_time);
1575 last_pulse_time_channel = channel;
1576 }
1577 break;
1578
1579 case 9: // FDC pulse data-peak (new) (GlueX-doc-2274-v8)
1580 {
1581 // Word 1:
1582 uint32_t word1 = *iptr;
1583 uint32_t channel = (*iptr>>20) & 0x7F;
1584 uint32_t pulse_number = (*iptr>>15) & 0x1F;
1585 uint32_t pulse_time = (*iptr>>4 ) & 0x7FF;
1586 uint32_t quality_factor = (*iptr>>3 ) & 0x1; //time QF bit
1587 uint32_t overflow_count = (*iptr>>0 ) & 0x7;
1588 if(VERBOSE>7){
1589 cout << " FADC125 FDC Pulse Data(peak) word1: " << hex << (*iptr) << dec << endl;
1590 cout << " FADC125 FDC Pulse Data (chan="<<channel<<" pulse="<<pulse_number<<" time="<<pulse_time<<" QF="<<quality_factor<<" OC="<<overflow_count<<")"<<endl;
1591 }
1592
1593 // Word 2:
1594 ++iptr;
1595 if(iptr>=iend){
1596 jerr << " Truncated f125 FDC hit (block ends before continuation word!)" << endl;
1597 continue;
1598 }
1599 if( ((*iptr>>31) & 0x1) != 0 ){
1600 jerr << " Truncated f125 FDC hit (missing continuation word!)" << endl;
1601 continue;
1602 }
1603 uint32_t word2 = *iptr;
1604 uint32_t pulse_peak = (*iptr>>19) & 0xFFF;
1605 uint32_t sum = 0;
1606 uint32_t peak_time = (*iptr>>11) & 0xFF;
1607 uint32_t pedestal = (*iptr>>0 ) & 0x7FF;
1608 if(VERBOSE>7){
1609 cout << " FADC125 FDC Pulse Data(peak) word2: " << hex << (*iptr) << dec << endl;
1610 cout << " FADC125 FDC Pulse Data (integral="<<sum<<" time="<<peak_time<<" pedestal="<<pedestal<<")"<<endl;
1611 }
1612
1613 // Create hit objects
1614 uint32_t nsamples_integral = 0; // must be overwritten later in GetObjects with value from Df125Config value
1615 uint32_t nsamples_pedestal = 1; // The firmware pedestal divided by 2^PBIT where PBIT is a config. parameter
1616
1617 if( pe ) {
1618 pe->NEW_Df125FDCPulse(rocid, slot, channel, itrigger
1619 , pulse_number // NPK
1620 , pulse_time // le_time
1621 , quality_factor // time_quality_bit
1622 , overflow_count // overflow_count
1623 , pedestal // pedestal
1624 , sum // integral
1625 , pulse_peak // peak_amp
1626 , peak_time // peak_time
1627 , word1 // word1
1628 , word2 // word2
1629 , nsamples_pedestal // nsamples_pedestal
1630 , nsamples_integral // nsamples_integral
1631 , false); // emulated
1632 }
1633 }
1634 break;
1635
1636 case 10: // Pulse Pedestal (consistent with Beni's hand-edited version of Cody's document)
1637 {
1638 if(VERBOSE>7) cout << " FADC125 Pulse Pedestal"<<endl;
1639 //channel = (*iptr>>20) & 0x7F;
1640 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")
1641 uint32_t pulse_number = (*iptr>>21) & 0x03;
1642 uint32_t pedestal = (*iptr>>12) & 0x1FF;
1643 uint32_t pulse_peak = (*iptr>>0) & 0xFFF;
1644 uint32_t nsamples_pedestal = 1; // The firmware returns an already divided pedestal
1645 if( pe ) pe->NEW_Df125PulsePedestal(rocid, slot, channel, itrigger, pulse_number, pedestal, pulse_peak, nsamples_pedestal);
1646 }
1647 break;
1648
1649 case 13: // Event Trailer
1650 case 14: // Data not valid (empty module)
1651 case 15: // Filler (non-data) word
1652 if(VERBOSE>7) cout << " FADC125 ignored data type: " << data_type <<endl;
1653 break;
1654 }
1655 }
1656
1657 // Chop off filler words
1658 for(; iptr<iend; iptr++){
1659 if(((*iptr)&0xf8000000) != 0xf8000000) break;
1660 }
1661}
1662
1663//----------------
1664// MakeDf125WindowRawData
1665//----------------
1666void DEVIOWorkerThread::MakeDf125WindowRawData(DParsedEvent *pe, uint32_t rocid, uint32_t slot, uint32_t itrigger, uint32_t* &iptr)
1667{
1668 uint32_t channel = (*iptr>>20) & 0x7F;
1669 uint32_t window_width = (*iptr>>0) & 0x0FFF;
1670
1671 Df125WindowRawData *wrd = pe->NEW_Df125WindowRawData(rocid, slot, channel, itrigger);
1672
1673 for(uint32_t isample=0; isample<window_width; isample +=2){
1674
1675 // Advance to next word
1676 iptr++;
1677
1678 // Make sure this is a data continuation word, if not, stop here
1679 if(((*iptr>>31) & 0x1) != 0x0)break;
1680
1681 bool invalid_1 = (*iptr>>29) & 0x1;
1682 bool invalid_2 = (*iptr>>13) & 0x1;
1683 uint16_t sample_1 = 0;
1684 uint16_t sample_2 = 0;
1685 if(!invalid_1)sample_1 = (*iptr>>16) & 0x1FFF;
1686 if(!invalid_2)sample_2 = (*iptr>>0) & 0x1FFF;
1687
1688 // Sample 1
1689 wrd->samples.push_back(sample_1);
1690 wrd->invalid_samples |= invalid_1;
1691 wrd->overflow |= (sample_1>>12) & 0x1;
1692
1693 if((isample+2) == window_width && invalid_2)break; // skip last sample if flagged as invalid
1694
1695 // Sample 2
1696 wrd->samples.push_back(sample_2);
1697 wrd->invalid_samples |= invalid_2;
1698 wrd->overflow |= (sample_2>>12) & 0x1;
1699 }
1700}
1701
1702//----------------
1703// ParseF1TDCBank
1704//----------------
1705void DEVIOWorkerThread::ParseF1TDCBank(uint32_t rocid, uint32_t* &iptr, uint32_t *iend)
1706{
1707 if(!PARSE_F1TDC){ iptr = &iptr[(*iptr) + 1]; return; }
1708
1709 uint32_t *istart = iptr;
1710
1711 auto pe_iter = current_parsed_events.begin();
1712 DParsedEvent *pe = NULL__null;
1713
1714 uint32_t slot = 0;
1715 uint32_t modtype = 0;
1716 uint32_t itrigger = -1;
1717 uint32_t trig_time_f1header = 0;
1718
1719 // Some early data had a marker word at just before the actual F1 data
1720 if(*iptr == 0xf1daffff) iptr++;
1721
1722 // Loop over data words
1723 for(; iptr<iend; iptr++){
1724
1725 // Skip all non-data-type-defining words at this
1726 // level. When we do encounter one, the appropriate
1727 // case block below should handle parsing all of
1728 // the data continuation words and advance the iptr.
1729 if(((*iptr>>31) & 0x1) == 0)continue;
1730
1731 uint32_t data_type = (*iptr>>27) & 0x0F;
1732 switch(data_type){
1733 case 0: // Block Header
1734 slot = (*iptr)>>22 & 0x001F;
1735 modtype = (*iptr)>>18 & 0x000F; // should match a DModuleType::type_id_t
1736 if(VERBOSE>7) cout << " F1 Block Header: slot=" << slot << " modtype=" << modtype << endl;
1737 break;
1738
1739 case 1: // Block Trailer
1740 pe_iter = current_parsed_events.begin();
1741 pe = NULL__null;
1742 if(VERBOSE>7) cout << " F1 Block Trailer" << endl;
1743 break;
1744
1745 case 2: // Event Header
1746 {
1747 pe = *pe_iter++;
1748 itrigger = (*iptr)>>0 & 0x0003FFFFF;
1749 if(VERBOSE>7) {
1750 uint32_t slot_event_header = (*iptr)>>22 & 0x00000001F;
1751 cout << " F1 Event Header: slot=" << slot_event_header << " itrigger=" << itrigger << endl;
1752 }
1753 }
1754 break;
1755
1756 case 3: // Trigger time
1757 {
1758 uint64_t t = ((*iptr)&0xFFFFFF)<<0;
1759 iptr++;
1760 if(((*iptr>>31) & 0x1) == 0){
1761 t += ((*iptr)&0xFFFFFF)<<24; // from word on the street: second trigger time word is optional!!??
1762 }else{
1763 iptr--;
1764 }
1765 if(VERBOSE>7) cout << " F1TDC Trigger Time (t="<<t<<")"<<endl;
1766 if(pe) pe->NEW_DF1TDCTriggerTime(rocid, slot, itrigger, t);
1767 }
1768 break;
1769
1770 case 8: // F1 Chip Header
1771 trig_time_f1header = ((*iptr)>> 7) & 0x1FF;
1772 if(VERBOSE>7) {
1773 uint32_t chip_f1header = ((*iptr)>> 3) & 0x07;
1774 uint32_t chan_on_chip_f1header = ((*iptr)>> 0) & 0x07; // this is always 7 in real data!
1775 uint32_t itrigger_f1header = ((*iptr)>>16) & 0x3F;
1776 cout << " Found F1 header: chip=" << chip_f1header << " chan=" << chan_on_chip_f1header << " itrig=" << itrigger_f1header << " trig_time=" << trig_time_f1header << endl;
1777 }
1778 break;
1779
1780 case 7: // F1 Data
1781 {
1782 uint32_t chip = (*iptr>>19) & 0x07;
1783 uint32_t chan_on_chip = (*iptr>>16) & 0x07;
1784 uint32_t time = (*iptr>> 0) & 0xFFFF;
1785 uint32_t channel = F1TDC_channel(chip, chan_on_chip, modtype);
1786 if(VERBOSE>7) cout << " Found F1 data : chip=" << chip << " chan=" << chan_on_chip << " time=" << time << endl;
1787 if(pe) pe->NEW_DF1TDCHit(rocid, slot, channel, itrigger, trig_time_f1header, time, *iptr, MODULE_TYPE(modtype));
1788 }
1789 break;
1790
1791 case 15: // Filler word
1792 if(VERBOSE>7) cout << " F1 filler word" << endl;
1793 case 14: // Data not valid (how to handle this?)
1794 break;
1795
1796 default:
1797 cerr<<endl;
1798 cout.flush(); cerr.flush();
1799 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1799<<" "
<<"Unknown data word in F1TDC block. Dumping for debugging:" << endl;
1800 for(const uint32_t *iiptr = istart; iiptr<iend; iiptr++){
1801 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1801<<" "
<<"0x"<<hex<<*iiptr<<dec;
1802 if(iiptr == iptr)cerr<<" <----";
1803 switch( (*iiptr) & 0xF8000000 ){
1804 case 0x80000000: cerr << " F1 Block Header"; break;
1805 case 0x90000000: cerr << " F1 Event Header"; break;
1806 case 0x98000000: cerr << " F1 Trigger time"; break;
1807 case 0xC0000000: cerr << " F1 Header"; break;
1808 case 0xB8000000: cerr << " F1 Data"; break;
1809 case 0x88000000: cerr << " F1 Block Trailer"; break;
1810 case 0xF8000000: cerr << " Filler word"; break;
1811 case 0xF0000000: cerr << " <module has no valid data>"; break;
1812 default: break;
1813 }
1814 cerr<<endl;
1815 if(iiptr > (iptr+4)) break;
1816 }
1817 throw JException("Unexpected word type in F1TDC block!", __FILE__"libraries/DAQ/DEVIOWorkerThread.cc", __LINE__1817);
1818 break;
1819 }
1820 }
1821
1822 // Skip filler words
1823 while(iptr<iend && (*iptr&0xF8000000)==0xF8000000)iptr++;
1824}
1825
1826//----------------
1827// ParseDVertexBank
1828//----------------
1829void DEVIOWorkerThread::ParseDVertexBank(uint32_t* &iptr, uint32_t *iend)
1830{
1831 uint32_t Nwords = ((uint64_t)iend - (uint64_t)iptr)/sizeof(uint32_t);
1832 uint32_t Nwords_expected = 11; // ?
1833 if(Nwords != Nwords_expected){
1834 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1834<<" "
<< "DVertex size does not match expected!!" << endl;
1835 _DBG_std::cerr<<"libraries/DAQ/DEVIOWorkerThread.cc"<<
":"<<1835<<" "
<< "Found " << Nwords << " words. Expected " << Nwords_expected << endl;
1836 }else{
1837 DParsedEvent *pe = current_parsed_events.back();
1838 DVertex *the_vertex = pe->NEW_DVertex();
1839
1840 uint64_t in_word = *iptr++; // 1st word, lo word; 2nd word, hi word
1841 uint64_t in_word_hi = *iptr++;
1842 in_word |= in_word_hi<<32;
1843 //uint64_t hi_word = *iptr++;
1844 double vertex_x_pos;
1845 memcpy(&vertex_x_pos, &in_word, sizeof(double));
1846 in_word = *iptr++; in_word_hi = *iptr++;
1847 in_word |= in_word_hi<<32;
1848 double vertex_y_pos;
1849 memcpy(&vertex_y_pos, &in_word, sizeof(double));
1850 in_word = *iptr++; in_word_hi = *iptr++;
1851 in_word |= in_word_hi<<32;
1852 double vertex_z_pos;
1853 memcpy(&vertex_z_pos, &in_word, sizeof(double));
1854 in_word = *iptr++; in_word_hi = *iptr++;
1855 in_word |= in_word_hi<<32;
1856 double vertex_t;
1857 memcpy(&vertex_t, &in_word, sizeof(double));
1858
1859 DVector3 vertex_position(vertex_x_pos, vertex_y_pos, vertex_z_pos);
1860 the_vertex->dSpacetimeVertex = DLorentzVector(vertex_position, vertex_t);
1861 the_vertex->dKinFitNDF = *iptr++;
1862
1863 in_word = *iptr++; in_word_hi = *iptr++;
1864 in_word |= in_word_hi<<32;
1865 memcpy(&(the_vertex->dKinFitChiSq), &in_word, sizeof(double));
1866 }
1867}
1868
1869
1870//----------------
1871// LinkAllAssociations
1872//----------------
1873void DEVIOWorkerThread::LinkAllAssociations(void)
1874{
1875
1876 /// Find objects that should be linked as "associated objects"
1877 /// of one another and add to each other's list.
1878 for( auto pe : current_parsed_events){
1879
1880 //----------------- Sort hit objects
1881
1882 // fADC250 (n.b. Df250PulseData values overwritten in JEventSource_EVIOpp::LinkBORassociations)
1883 if(pe->vDf250PulseData.size()>1 ) sort(pe->vDf250PulseData.begin(), pe->vDf250PulseData.end(), SortByPulseNumber<Df250PulseData> );
1884 if(pe->vDf250PulseIntegral.size()>1) sort(pe->vDf250PulseIntegral.begin(), pe->vDf250PulseIntegral.end(), SortByPulseNumber<Df250PulseIntegral> );
1885 if(pe->vDf250PulseTime.size()>1 ) sort(pe->vDf250PulseTime.begin(), pe->vDf250PulseTime.end(), SortByPulseNumber<Df250PulseTime> );
1886 if(pe->vDf250PulsePedestal.size()>1) sort(pe->vDf250PulsePedestal.begin(), pe->vDf250PulsePedestal.end(), SortByPulseNumber<Df250PulsePedestal> );
1887 if(pe->vDf250WindowRawData.size()>1) sort(pe->vDf250WindowRawData.begin(), pe->vDf250WindowRawData.end(), SortByChannel<Df250WindowRawData> );
1888
1889 // fADC125
1890 if(pe->vDf125PulseIntegral.size()>1) sort(pe->vDf125PulseIntegral.begin(), pe->vDf125PulseIntegral.end(), SortByPulseNumber<Df125PulseIntegral> );
1891 if(pe->vDf125CDCPulse.size()>1 ) sort(pe->vDf125CDCPulse.begin(), pe->vDf125CDCPulse.end(), SortByChannel<Df125CDCPulse> );
1892 if(pe->vDf125FDCPulse.size()>1 ) sort(pe->vDf125FDCPulse.begin(), pe->vDf125FDCPulse.end(), SortByChannel<Df125FDCPulse> );
1893 if(pe->vDf125PulseTime.size()>1 ) sort(pe->vDf125PulseTime.begin(), pe->vDf125PulseTime.end(), SortByPulseNumber<Df125PulseTime> );
1894 if(pe->vDf125PulsePedestal.size()>1) sort(pe->vDf125PulsePedestal.begin(), pe->vDf125PulsePedestal.end(), SortByPulseNumber<Df125PulsePedestal> );
1895 if(pe->vDf125WindowRawData.size()>1) sort(pe->vDf125WindowRawData.begin(), pe->vDf125WindowRawData.end(), SortByChannel<Df125WindowRawData> );
1896
1897 // F1TDC
1898 if(pe->vDF1TDCHit.size()>1 ) sort(pe->vDF1TDCHit.begin(), pe->vDF1TDCHit.end(), SortByModule<DF1TDCHit> );
1899
1900 // CAEN1290TDC
1901 if(pe->vDCAEN1290TDCHit.size()>1 ) sort(pe->vDCAEN1290TDCHit.begin(), pe->vDCAEN1290TDCHit.end(), SortByModule<DCAEN1290TDCHit> );
1902
1903
1904 //----------------- Link hit objects
1905
1906 // Connect Df250 pulse objects
1907 LinkPulse(pe->vDf250PulseTime, pe->vDf250PulseIntegral);
1908 LinkPulsePedCopy(pe->vDf250PulsePedestal, pe->vDf250PulseIntegral);
1909
1910 // Connect Df125 pulse objects
1911 LinkPulse(pe->vDf125PulseTime, pe->vDf125PulseIntegral);
1912 LinkPulsePedCopy(pe->vDf125PulsePedestal, pe->vDf125PulseIntegral);
1913
1914 // Connect Df250 window raw data objects
1915 if(!pe->vDf250WindowRawData.empty()){
1916 LinkConfig(pe->vDf250Config, pe->vDf250WindowRawData);
1917 LinkModule(pe->vDf250TriggerTime, pe->vDf250WindowRawData);
1918 LinkChannel(pe->vDf250WindowRawData, pe->vDf250PulseIntegral);
1919 LinkChannel(pe->vDf250WindowRawData, pe->vDf250PulseTime);
1920 LinkChannel(pe->vDf250WindowRawData, pe->vDf250PulsePedestal);
1921 LinkChannel(pe->vDf250WindowRawData, pe->vDf250PulseData);
1922 }
1923
1924 // Connect Df125 window raw data objects
1925 if(!pe->vDf125WindowRawData.empty()){
1926 LinkConfig(pe->vDf125Config, pe->vDf125WindowRawData);
1927 LinkModule(pe->vDf125TriggerTime, pe->vDf125WindowRawData);
1928 LinkChannel(pe->vDf125WindowRawData, pe->vDf125PulseIntegral);
1929 LinkChannel(pe->vDf125WindowRawData, pe->vDf125PulseTime);
1930 LinkChannel(pe->vDf125WindowRawData, pe->vDf125PulsePedestal);
1931 LinkChannel(pe->vDf125WindowRawData, pe->vDf125CDCPulse);
1932 LinkChannel(pe->vDf125WindowRawData, pe->vDf125FDCPulse);
1933 }
1934
1935 //----------------- Optionally link config objects (on by default)
1936 if(LINK_CONFIG){
1937 if(pe->vDf250Config.size()>1 ) sort(pe->vDf250Config.begin(), pe->vDf250Config.end(), SortByROCID<Df250Config> );
1938 if(pe->vDf125Config.size()>1 ) sort(pe->vDf125Config.begin(), pe->vDf125Config.end(), SortByROCID<Df125Config> );
1939 if(pe->vDF1TDCConfig.size()>1 ) sort(pe->vDF1TDCConfig.begin(), pe->vDF1TDCConfig.end(), SortByROCID<DF1TDCConfig> );
1940 if(pe->vDCAEN1290TDCConfig.size()>1) sort(pe->vDCAEN1290TDCConfig.begin(), pe->vDCAEN1290TDCConfig.end(), SortByROCID<DCAEN1290TDCConfig> );
1941
1942 LinkConfigSamplesCopy(pe->vDf250Config, pe->vDf250PulseIntegral);
1943 LinkConfigSamplesCopy(pe->vDf250Config, pe->vDf250PulseData);
1944 LinkConfigSamplesCopy(pe->vDf125Config, pe->vDf125PulseIntegral);
1945 LinkConfigSamplesCopy(pe->vDf125Config, pe->vDf125CDCPulse);
1946 LinkConfigSamplesCopy(pe->vDf125Config, pe->vDf125FDCPulse);
1947 LinkConfig(pe->vDF1TDCConfig, pe->vDF1TDCHit);
1948 LinkConfig(pe->vDCAEN1290TDCConfig, pe->vDCAEN1290TDCHit);
1949 }
1950
1951 //----------------- Optionally link trigger time objects (off by default)
1952 if(LINK_TRIGGERTIME){
1953 if(pe->vDf250TriggerTime.size()>1 ) sort(pe->vDf250TriggerTime.begin(), pe->vDf250TriggerTime.end(), SortByModule<Df250TriggerTime> );
1954 if(pe->vDf125TriggerTime.size()>1 ) sort(pe->vDf125TriggerTime.begin(), pe->vDf125TriggerTime.end(), SortByModule<Df125TriggerTime> );
1955 if(pe->vDF1TDCTriggerTime.size()>1 ) sort(pe->vDF1TDCTriggerTime.begin(), pe->vDF1TDCTriggerTime.end(), SortByModule<DF1TDCTriggerTime> );
1956
1957 LinkModule(pe->vDf250TriggerTime, pe->vDf250PulseIntegral);
1958 LinkModule(pe->vDf125TriggerTime, pe->vDf125PulseIntegral);
1959 LinkModule(pe->vDf125TriggerTime, pe->vDf125CDCPulse);
1960 LinkModule(pe->vDf125TriggerTime, pe->vDf125FDCPulse);
1961 LinkModule(pe->vDF1TDCTriggerTime, pe->vDF1TDCHit);
1962 }
1963 }
1964
1965}
1966
1967//----------------
1968// DumpBinary
1969//----------------
1970void DEVIOWorkerThread::DumpBinary(const uint32_t *iptr, const uint32_t *iend, uint32_t MaxWords, const uint32_t *imark)
1971{
1972 /// This is used for debugging. It will print to the screen the words
1973 /// starting at the address given by iptr and ending just before iend
1974 /// or for MaxWords words, whichever comes first. If iend is NULL,
1975 /// then MaxWords will be printed. If MaxWords is zero then it is ignored
1976 /// and only iend is checked. If both iend==NULL and MaxWords==0, then
1977 /// only the word at iptr is printed.
1978
1979 cout << "Dumping binary: istart=" << hex << iptr << " iend=" << iend << " MaxWords=" << dec << MaxWords << endl;
1980
1981 if(iend==NULL__null && MaxWords==0) MaxWords=1;
1982 if(MaxWords==0) MaxWords = (uint32_t)0xffffffff;
1983
1984 uint32_t Nwords=0;
1985 while(iptr!=iend && Nwords<MaxWords){
1986
1987 // line1 is hex and line2 is decimal
1988 stringstream line1, line2;
1989
1990 // print words in columns 8 words wide. First part is
1991 // reserved for word number
1992 uint32_t Ncols = 8;
1993 line1 << setw(5) << Nwords;
1994 line2 << string(5, ' ');
1995
1996 // Loop over columns
1997 for(uint32_t i=0; i<Ncols; i++, iptr++, Nwords++){
1998
1999 if(iptr == iend) break;
2000 if(Nwords>=MaxWords) break;
2001
2002 stringstream iptr_hex;
2003 iptr_hex << hex << "0x" << *iptr;
2004
2005 string mark = (iptr==imark ? "*":" ");
2006
2007 line1 << setw(12) << iptr_hex.str() << mark;
2008 line2 << setw(12) << *iptr << mark;
2009 }
2010
2011 cout << line1.str() << endl;
2012 cout << line2.str() << endl;
2013 cout << endl;
2014 }
2015}
2016
2017