MiniballSort
Loading...
Searching...
No Matches
MedConverter.cc
Go to the documentation of this file.
1#include "MedConverter.hh"
2
3// Function to process header words and then the data
4void MiniballMedConverter::ProcessEvent( unsigned long nblock ){
5
6 // Get number of subevents in the data and loop over them
8 for( unsigned int i = 0; i < ndata; i++ ){
9
10 // Pointer to this subevent
12 if( !mbs_sevt ) continue;
13
14 //std::cout << "Event ID: " << std::dec << ev->GetEventID();
15 //std::cout << ", Sub-event: " << mbs_sevt->GetSubEventID();
16 //std::cout << std::endl << "Sub-event type: 0x" << std::hex;
17 //std::cout << mbs_sevt->GetSubEventType() << ", ";
18 //std::cout << mbs_sevt->GetSubEventDescription();
19 //std::cout << std::dec << std::endl;
20
21
22 // Normal DGF data
23 if( set->CheckVmeModuleIsDgf( mbs_sevt->GetModuleID() ) )
25
26 // DGF Scalers
27 else if( set->CheckVmeModuleIsDgfScaler( mbs_sevt->GetModuleID() ) )
29
30 // General ADCs
31 else if( set->CheckVmeModuleIsAdc( mbs_sevt->GetModuleID() ) ) {
32
33 // Mesytec ADCs
38
39 // Assume it is otherwise a CAEN ADC
40 else ProcessCaenAdcData();
41
42 }
43
44 // Pattern unit
45 else if( set->CheckVmeModuleIsPattern( mbs_sevt->GetModuleID() ) )
47
48 // Scaler data
49 else if( set->CheckVmeModuleIsScaler( mbs_sevt->GetModuleID() ) )
51
52 // Timestamp data from MBS
54 continue;
55
56 // Otherwise print an error for now (expand more types later)
57 else {
58
59 std::cerr << "Unrecognised VME module ID (" << std::dec;
60 std::cerr << mbs_sevt->GetModuleID() << "), with sub-event type 0x";
61 std::cerr << std::hex << mbs_sevt->GetSubEventType() << " (";
62 std::cerr << mbs_sevt->GetSubEventDescription() << ")";
63 std::cerr << std::dec << std::endl;
64 continue;
65
66 }
67
68 }
69
70 // Suppress unused warnings
71 (void)nblock;
72
73 return;
74
75}
76
77//-----------------------------------------------------------------------------
78// Treat a Mesytec ADC data item
80
81 // Loop over all the available data
82 unsigned int i = 0;
83 while( i < mbs_sevt->GetNumberOfData() ){
84
85 // Header of the sub event (module number in first word)
86 unsigned short header = mbs_sevt->GetData(i++);
87 unsigned short mod = header & MESYTEC_MADC_MODULE_ID;
88
89 // Convert to logical module from Marabou numbering
90 if( mod >= set->GetNumberOfAdcModules() )
91 mod = set->GetMesytecAdcModuleNumber( mod );
92 else mod--; // Mesytec modules count from 1 in Marabou!!
93
94 // Get second word of the header and test format
95 header = mbs_sevt->GetData(i++);
96 if( header & MESYTEC_MADC_OUTPUT_FORMAT ) {
97
98 std::cerr << __PRETTY_FUNCTION__ << ": Error, output ";
99 std::cerr << "format (highest bit) should be zero (header = ";
100 std::cerr << std::hex << header << std::dec << ")" << std::endl;
101 return;
102
103 }
104
105 // If format is fine, we have the word count in here
106 int wc = (header & MESYTEC_MADC_WORD_COUNT);
107
108 // check number of Channels (MESYTEC_MADC_NBOFCHAN=32 + 1 word End of Event + 1 extended timestamp)
109 // -> skip TOTAL subevent if wrong number of Channels
110 if( wc <= 0 || wc > (int)set->GetNumberOfMesytecAdcChannels() + 2 ) {
111
112 std::cout << __PRETTY_FUNCTION__ << ": read event nr. ";
113 std::cout << my_event_id << ": wrong Word Count: ";
114 std::cout << wc << " -> skip TOTAL subevent" << std::endl;
115 return;
116
117 }
118
119 // Loop over number of channels for which data follows (maybe also extended timestamp)
120 // Might not be all channel, so use wc, but wc includes the end of event
121 std::vector<unsigned short> ch_vec;
122 std::vector<unsigned short> qshort_vec;
123 long long Timestamp = 0;
124 bool clipped = false;
125 for( unsigned short ch = 0; ch < wc - 1; ch++ ) {
126
127 // Check if type of word (highest two bits) is end of event
128 unsigned short test = mbs_sevt->GetData(i++);
129
130 // 00 => data, 11 => end of event
132
133 std::cerr << "Error, found end of event data (" << test << ") after " << ch+1;
134 std::cerr << " words but should have a word count of " << wc << std::endl;
135 return;
136
137 }
138
139 // What if only one of the higest two bits is non-zero? weird
140 else if( test & MESYTEC_MADC_END_OF_EVENT ) {
141
142 std::cerr << "Error, found weird data (" << test << ") after ";
143 std::cerr << ch+1 << " words with word count of " << wc << std::endl;
144 return;
145
146 }
147
148 // Check whether this is the extended timestamp
150
151 Timestamp = ((long long)mbs_sevt->GetData(i++)) << MESYTEC_MADC_EXTENDED_TIMESTAMP_SHIFT;
152 //Timestamp &= MESYTEC_MADC_EXTENDED_TIMESTAMP_MASK;
153 continue;
154
155 }
156
157 // If we don't have the timestamp here, test has the channel number
158 // Get actual Channel number (bits[21...16] of data word)
159 unsigned short chanNo = (test & MESYTEC_MADC_CHANNEL_NUMBER);
160 ch_vec.push_back( chanNo );
161
162 // But the energy is in the next word, lowest 12 bits
163 unsigned short Qshort = (mbs_sevt->GetData(i++) & MESYTEC_MADC_VALUE);
164 qshort_vec.push_back( Qshort );
165
166 // Check the out of range bit
168 clipped = true;
169 else clipped = false;
170
171 } // loop over wc - 1
172
173 // Trailer of the sub event (module number in first word)
174 unsigned int trailer = ((unsigned int)mbs_sevt->GetData(i++) << 16) & 0xffff0000;
175 trailer |= ((unsigned int)mbs_sevt->GetData(i++)) & 0x0000ffff;
176
177 // Check type of word (highest two bits should be set)
179
180 std::cout << __PRETTY_FUNCTION__ << ": read event nr. " << my_event_id;
181 std::cout << ": wrong EOE word of type: " << ( trailer & MESYTEC_MADC_END_OF_EVENT );
182 std::cout << " -> skip TOTAL subevent" << std::endl;
183 return;
184
185 }
186
187 // Merge the full time stamp and add the DGF delay
188 Timestamp |= ( trailer & MESYTEC_MADC_TIMESTAMP );
189 Timestamp += set->GetDgfTimestampDelay();
190 Timestamp *= set->GetMesytecAdcTimestampUnits();
191
192 // Now we have the data, fill the tree
193 for( unsigned item = 0; item < qshort_vec.size(); item++ ){
194
195 // Clear the old stuff
196 adc_data->ClearData();
197 //data_packet->ClearData();
198
199 // Some basic info for every event
200 adc_data->SetEventID( my_event_id );
201
202 // Calculate energy and threshold
203 float energy = cal->AdcEnergy( mod, ch_vec[item], qshort_vec[item] );
204 bool thresh = cal->AdcThreshold( mod, ch_vec[item] );
205
206 // Corrected time for ADCs
207 long long time_corr = Timestamp;
208 time_corr += cal->AdcTime( mod, ch_vec[item] );
209
210 // Set values for data item
211 adc_data->SetTime( time_corr ); // only works for MADC, CAEN needs reconstruction
212 adc_data->SetQshort( qshort_vec[item] );
213 adc_data->SetModule( mod );
214 adc_data->SetChannel( (char)ch_vec[item] );
215 adc_data->SetEnergy( energy );
216 adc_data->SetThreshold( thresh );
217 adc_data->SetClipped( clipped );
218
219 // Fill the tree
220 if( !flag_source ) {
221 std::shared_ptr<MiniballDataPackets> data_packet =
222 std::make_shared<MiniballDataPackets>( adc_data );
223 data_vector.emplace_back( data_packet );
224 data_map.push_back( std::make_pair<unsigned long,double>(
225 data_vector.size()-1, data_packet->GetTime() ) );
226 }
227
228 // Fill histograms
229 hadc_qshort[mod][ch_vec[item]]->Fill( qshort_vec[item] );
230 hadc_cal[mod][ch_vec[item]]->Fill( energy );
231
232 }
233
234 }
235
236 return;
237
238}
239
240//-----------------------------------------------------------------------------
241// Treat a CAEN ADC data item
243
244 // Do nothing with it for now, because I'm lazy
245 return;
246
247}
248
249//-----------------------------------------------------------------------------
250// Decode the pattern unit data and add it to the packets
252
253 // Test data comes from scaler unit?
255
256 std::cerr << __PRETTY_FUNCTION__;
257 std::cerr << ": Error - got data from VME module id ";
258 std::cerr << mbs_sevt->GetModuleID();
259 std::cerr << ", which is defined as a pattern unit but has the";
260 std::cerr << " wrong data format: " << std::hex;
261 std::cerr << mbs_sevt->GetSubEventType() << std::dec << std::endl;
262 return;
263
264 }
265
266 //mbs_sevt->Show();
267
268 // Loop over data and reconstruct header and data
269 unsigned int i = 0;
270 while( i < mbs_sevt->GetNumberOfData() ) {
271
272 // Get the header
273 unsigned short header = mbs_sevt->GetData(i++);
274 if( ( header & SIS3600_D_HDR ) == 0 ) {
275
276 std::cout << __PRETTY_FUNCTION__ << ": read event nr. ";
277 std::cout << my_event_id << ", sub event nr. ";
278 std::cout << (int)mbs_sevt->GetSubEventID();
279 std::cout << ": wrong Header word of type 0x" << std::hex;
280 std::cout << header << " -> skip subevent" << std::dec << std::endl;
281 return;
282
283 }
284
285 // Get the module number
286 short mod = set->GetPatternUnitNumber( header & SIS3600_MSERIAL );
287 if( mod < 0 ) return;
288
289 // Next item is the word count
290 unsigned short wc = mbs_sevt->GetData(i++);
291
292 // Loop over data and reconstruct integers
293 for( unsigned short j = 0; j < wc-2; j++ ) {
294
295 unsigned int data;
296 data = ( (unsigned int)mbs_sevt->GetData(i++) ) << 16;
297 data |= ( (unsigned int)mbs_sevt->GetData(i++) );
298
299 if( data > 0 )
300 mbsinfo_packet->AddPattern( mod, j, data );
301
302 }
303
304 }
305
306 return;
307
308}
309
310//-----------------------------------------------------------------------------
311// Decode the scaler unit data and add it to the packets
313
314 // Test data comes from scaler unit?
316
317 std::cerr << __PRETTY_FUNCTION__;
318 std::cerr << ": Error - got data from VME module id ";
319 std::cerr << mbs_sevt->GetModuleID();
320 std::cerr << ", which is defined as a scaler unit but has the";
321 std::cerr << " wrong data format: " << std::hex;
322 std::cerr << mbs_sevt->GetSubEventType() << std::dec << std::endl;
323 return;
324
325 }
326
327 // Check we have an even number of data
328 if( mbs_sevt->GetNumberOfData() % 2 != 0 ){
329
330 std::cerr << __PRETTY_FUNCTION__;
331 std::cerr << ": Error - expecting even number of words in MbsSubEvent but there are ";
332 std::cerr << mbs_sevt->GetNumberOfData() << std::endl;
333 return;
334
335 }
336
337 // Loop over data and reconstruct integers
338 for( unsigned int i = 0; i < mbs_sevt->GetNumberOfData()/2; i++ ) {
339
340 unsigned int data;
341 data = ( (unsigned int)mbs_sevt->GetData(i+0) ) << 16;
342 data |= ( (unsigned int)mbs_sevt->GetData(i+1) );
343
344 if( data > 0 )
345 mbsinfo_packet->AddScaler( i, data );
346
347 }
348
349 return;
350
351}
352
353//-----------------------------------------------------------------------------
354// Treat a DGF scaler data item
356
357 // Test data comes from DGFscaler unit?
358 if( !set->CheckVmeModuleIsDgfScaler( mbs_sevt->GetModuleID() ) ){
359
360 std::cerr << __PRETTY_FUNCTION__;
361 std::cerr << ": Error - got data from VME module id ";
362 std::cerr << mbs_sevt->GetModuleID();
363 std::cerr << ", which looks like a DGF scaler module but it isn't";
364 std::cerr << " defined as such in settings file" << std::endl;
365 return;
366
367 }
368
369 // Loop over all the available data
370 unsigned int i = 0;
371 while( i < mbs_sevt->GetNumberOfData() ){
372
373 // Finish the data?
375 break;
376
377 // Check for magic word at the start
379
380 std::cerr << "Internal dgf scalers: data out of phase - 0x";
381 std::cerr << std::hex << mbs_sevt->GetData(--i);
382 std::cerr << " (should be magic word 0x" << DGF_SCALER_MAGIC_WORD ;
383 std::cerr << ")" << std::dec << std::endl;
384 return;
385
386 }
387
388 // Get the header data
389 int wc = mbs_sevt->GetData(i++); // word count
390 int clu = mbs_sevt->GetData(i++); // cluster id
391 int mod = mbs_sevt->GetData(i++); // module id
392
393 // Break if we have nonsense
394 if( clu < 0 || mod < 0 ) return;
395 if( wc < 0 ){
396 std::cerr << "Word count negative: " << wc << std::endl;
397 return;
398 }
399 if( (int)mbs_sevt->GetNumberOfData() - (int)(i-2) < DGF_SCALER_MIN_SIZE ){
400 std::cerr << "Not enough data left in sub event for new DGF scaler: ";
401 std::cerr << mbs_sevt->GetNumberOfData() - (i-2) << " < minsize(";
402 std::cerr << DGF_SCALER_MIN_SIZE << ")" << std::endl;
403 return;
404 }
405 if( (int)mbs_sevt->GetNumberOfData() - (int)(i-2) < wc ){
406 std::cerr << "Not enough data left in sub event for new DGF scaler: ";
407 std::cerr << mbs_sevt->GetNumberOfData() - (i-2) << " < wc(" << wc << ")" << std::endl;
408 return;
409 }
410
411 // Get the real time data
412 unsigned int index = i + DGF_SCALER_INDEX_REALTIME;
413 long long realTime = mbs_sevt->GetData(index);
414 realTime |= ( (unsigned long long)mbs_sevt->GetData(index+1) ) << 16;
415 realTime |= ( (unsigned long long)mbs_sevt->GetData(index+2) ) << 32;
416
417 // Get the run time data
418 index = i + DGF_SCALER_INDEX_RUNTIME;
419 long long runTime = mbs_sevt->GetData(index);
420 runTime |= ( (unsigned long long)mbs_sevt->GetData(index+1) ) << 16;
421 runTime |= ( (unsigned long long)mbs_sevt->GetData(index+2) ) << 32;
422
423 // Get the GSLT time data
424 index = i + DGF_SCALER_INDEX_GSLTTIME;
425 long long gsltTime = mbs_sevt->GetData(index);
426 gsltTime |= ( (unsigned long long)mbs_sevt->GetData(index+1) ) << 16;
427 gsltTime |= ( (unsigned long long)mbs_sevt->GetData(index+2) ) << 32;
428
429 // Get the number of events
430 index = i + DGF_SCALER_INDEX_NEVENTS;
431 long long nEvents = mbs_sevt->GetData(index);
432 nEvents |= ( (unsigned long long)mbs_sevt->GetData(index+1) ) << 16;
433 nEvents |= ( (unsigned long long)mbs_sevt->GetData(index+2) ) << 32;
434
435 // Make a DGF scaler event and add it to the packets
436 DgfScalerData s( set->GetNumberOfDgfChannels() );
437 s.SetModule( mod );
438 s.SetClusterID( clu );
439 s.SetRealTime( realTime );
440 s.SetRunTime( runTime );
441 s.SetGSLTTime( gsltTime );
442
443 // Get channel by channel data
444 for( unsigned int j = 0; j < set->GetNumberOfDgfChannels(); j++ ){
445
446 index = i + DGF_SCALER_INDEX_CH_OFFSET;
447 index += j * DGF_SCALER_INDEX_CH_SIZE;
448
449 // Get LiveTime for this channel
450 long long liveTime = mbs_sevt->GetData(index);
451 liveTime |= ( (unsigned long long)mbs_sevt->GetData(index+1) ) << 16;
452 liveTime |= ( (unsigned long long)mbs_sevt->GetData(index+2) ) << 32;
453
454 // Get FastPeak for this channel
455 int fastPeak = mbs_sevt->GetData(index+3);
456 fastPeak |= ( (unsigned long long)mbs_sevt->GetData(index+4) ) << 16;
457
458 s.SetLiveTime( j, liveTime );
459 s.SetFastPeak( j, fastPeak );
460
461 }
462
463 // Move forward to next data item
464 i += wc - 4; // 4 header words included in word count
465
466 }
467
468 return;
469
470}
471
472
473//-----------------------------------------------------------------------------
474// Treat a DGF data item
476
477 // Test data comes from scaler unit?
479
480 std::cerr << __PRETTY_FUNCTION__;
481 std::cerr << ": Error - got data from VME module id ";
482 std::cerr << mbs_sevt->GetModuleID();
483 std::cerr << ", which looks like a DGF module but it isn't";
484 std::cerr << " defined as such in settings file" << std::endl;
485 return;
486
487 }
488
489 // Loop over all the available data
490 int i = 0;
491 int wc = mbs_sevt->GetNumberOfData();
492 while( i < wc ){
493
494 //std::cout << std::dec << "Readout nr.: " << ev->GetEventID()-2586617;
495 //std::cout << ", wc = " << wc << ", length = " << mbs_sevt->GetDataLength();
496 //std::cout << std::hex << ", DGF data(0) = 0x" << mbs_sevt->GetData(0) << std::endl;
497
498 // Header of the sub event
499 unsigned short start = i;
500 unsigned short length = mbs_sevt->GetData(i++);
501 unsigned short end = start + length;
502 unsigned short mod = mbs_sevt->GetData(i++);
503
504 // Check length
505 if( end > mbs_sevt->GetDataLength() ){
506
507 std::cout << __PRETTY_FUNCTION__ << ": XIA wrong buffer length: ";
508 std::cout << std::dec << length << " vs. " << mbs_sevt->GetDataLength() * sizeof(unsigned short);
509 std::cout << ", Start: " << start;
510 std::cout << ", End: " << end << std::endl;
511 return; // skip total subevent
512
513 }
514
515 // Get buffer format descriptor (=RUNTASK)
516 unsigned short format = mbs_sevt->GetData(i++);
517
518 // Parameters for holding the time
519 unsigned short RunTimeA, RunTimeB, RunTimeC;
520 unsigned int BufferTime;
521 unsigned long long RunTime;
522
523 // Check if known buffer format
524 if( ( format != STD_LM_BUFFORMAT ) &&
525 ( format != COMP_LM_BUFFORMAT ) &&
526 ( format != COMP_FLM_BUFFORMAT ) &&
527 ( format != COMP3_LM_BUFFORMAT ) &&
528 ( format != STD_FLM_BUFFORMAT ) ) {
529
530 std::cout << __PRETTY_FUNCTION__ << ": read out event ";
531 std::cout << my_event_id << ": wrong buffer format: ";
532 std::cout << format << " !!!" << std::endl;
533 return; // skip total subevent
534
535 }
536
537 else {
538
539 // read words 3-5 of buffer header: 3.: high-, 4.: middle-, 5.: low-word of run start time
540 RunTimeA = mbs_sevt->GetData(i++); // high
541 RunTimeB = mbs_sevt->GetData(i++); // mid
542 RunTimeC = mbs_sevt->GetData(i++); // low
543
544 // set 'buffertime' and 'runtime'
545 RunTime = ( (unsigned long long)RunTimeA << 32) & 0xffff000000000000;
546 BufferTime = ( (unsigned int)RunTimeB << 16) & 0xffff0000;
547 BufferTime |= ( (unsigned int)RunTimeC ) & 0x0000ffff;
548 RunTime |= ( BufferTime & 0xffffffff);
549
550 // Update timestamp using a DGF module
551 if( my_good_tm_stp == 0 )
552 my_good_tm_stp = RunTime * set->GetDgfTimestampUnits();
553 //my_good_tm_stp = RunTime;
554
555 }
556
557 // from here on: always check buffer format (=RUNTASK) before filling 'dgf' or 'dgf_rt259'
558 // beam dump module has format 'COMP3_LM_BUFFORMAT'
559 // determine corrected module number
560 mod = set->GetDgfModuleNumber( mod );
561
562 // Work out what type of DGF event we have
563 int DgfType = mbs_sevt->GetSubEventType() - XIA_EVENT;
564 (void)DgfType; // not yet used anywhere
565
566 // Get data for this module
567 while( i < end ){
568
569 // Read 3 words of event header
570 // 1.: hitpattern, 2.: high-, 3.: low-word of event time
571 unsigned short HitPattern = mbs_sevt->GetData(i++);
572 unsigned short EventTimeHigh = mbs_sevt->GetData(i++);
573 unsigned short EventTimeLow = mbs_sevt->GetData(i++);
574
575 // Set 'eventtime'
576 long long EventTime = (( (long long)EventTimeHigh ) << 16) & 0xffff0000;
577 EventTime |= (long long)EventTimeLow & 0x0000ffff;
578
579 // Check for overflow and build full event time
580 if( EventTime <= BufferTime ) RunTimeA++;
581 EventTime |= (( (long long)RunTimeA ) << 32) & 0xffff00000000;
582
583
584 // check hitpattern: at least one channel bit has to be set
585 // for all but TS_EBIS_T1_T2_MODULE and 4 CD TS-modules
586 // _all_ timestamp modules with RUNTASK!=259 -> 'ModuleNumber'
587 // can be directly compared with 'analysis module number'
588 if( !( HitPattern & 0xf ) && !set->IsTimestampModule(mod) ) {
589
590 std::cout << __PRETTY_FUNCTION__ << ": XIA hitpattern error: hitpattern = ";
591 std::cout << HitPattern << mod << ": module: " << mod;
592 std::cout << ", i: " << i << ", end: " << end;
593 std::cout << ", Data in SubEvent: " << mbs_sevt->GetNumberOfData();
594 std::cout << std::endl;
595
596 // skip module, but not full sub event
597 i = end;
598 break;
599
600 }
601
602 else {
603
604 // Process hit for all channels
605 for( unsigned int ch = 0; ch < set->GetNumberOfDgfChannels(); ch++ ) {
606
607 // Channel mask to make sure it's got data
608 unsigned int ChannelMask = 1 << ch;
609 if( ( HitPattern & ChannelMask ) != 0 ) {
610
611 //std::cout << std::hex << HitPattern << " & ";
612 //std::cout << ChannelMask << " = " << ( HitPattern & ChannelMask );
613 //std::cout << std::dec << std::endl;
614
615 // Now different data following for RUNTASK=259 resp. others
616 // RUNTASK!=259: now #of words for this channel
617 unsigned short ChannelLength;
618 if( format != COMP3_LM_BUFFORMAT )
619 ChannelLength = mbs_sevt->GetData(i++);
620
621 // Next 2 words: fast trigger time & energy for ALL diff. RUNTASKs
622 unsigned short FastTriggerTime = mbs_sevt->GetData(i++);
623 unsigned short Qshort = mbs_sevt->GetData(i++);
624
625 // Sort out long fast trigger time and wrap around
626 unsigned long long LongFastTriggerTime = FastTriggerTime;
627 if( FastTriggerTime > EventTimeLow )
628 LongFastTriggerTime += 65536ll*EventTimeHigh + 65536ll*65536ll*RunTimeA;
629 else
630 LongFastTriggerTime += 65536ll*EventTimeHigh + 65536ll + 65536ll*65536ll*RunTimeA;
631
632 // Get calibrated energy and check threshold
633 float energy = cal->DgfEnergy( mod, ch, Qshort );
634 bool thresh = cal->DgfThreshold( mod, ch );
635
636 // For RUNTASK!=259, now 6 user PSA values (& possible trace) follow
637 std::vector<unsigned short> UserValues;
638 std::vector<unsigned short> trace;
639 if( format != COMP3_LM_BUFFORMAT ) {
640
641 // Get 6 user values for PSA
642 for( unsigned char j = 0; j < 6; j++ )
643 UserValues.push_back( mbs_sevt->GetData(i++) );
644
645 // Read out trace
646 unsigned int TraceLength = (int)ChannelLength - CHANHEADLEN;
647 for( unsigned char j = 0; j < TraceLength; j++ )
648 trace.push_back( mbs_sevt->GetData(i++) );
649
650 } // check format for PSA and trace
651
652 //-----------
653 // Now let's add the data
654 //------------
655
656 // Fill histograms
657 hdgf_qshort[mod][ch]->Fill( Qshort );
658 hdgf_cal[mod][ch]->Fill( energy );
659
660 // Clear the old stuff
661 dgf_data->ClearData();
662 info_data->ClearData();
663 //data_packet->ClearData();
664
665 //std::cout << "DGF module " << mod << ", time = ";
666 //std::cout << LongFastTriggerTime << ", Qshort = ";
667 //std::cout << Qshort << std::endl;
668
669 long long time_corr = LongFastTriggerTime * set->GetDgfTimestampUnits();
670 time_corr += cal->DgfTime( mod, ch );
671
672 // Check if it's a timestamper!
673 if( set->IsTimestampModule( mod ) ) {
674
675 // Check if it matches EBIS, T1 or SC
676 char mycode = -1;
677 if( mod == set->GetEBISDgf() && ch == set->GetEBISChannel() )
678 mycode = set->GetEBISCode();
679 else if( mod == set->GetT1Dgf() && ch == set->GetT1Channel() )
680 mycode = set->GetT1Code();
681 else if( mod == set->GetSCDgf() && ch == set->GetSCChannel() )
682 mycode = set->GetSCCode();
683
684 // Useful only if it matched
685 if( mycode >= 0 ) {
686
687 // Set values for data item
688 info_data->SetEventID( my_event_id );
689 info_data->SetTime( time_corr );
690 info_data->SetCode( mycode );
691 info_data->SetBoard( mod );
692
693 // Fill the tree
694 if( !flag_source ) {
695 std::shared_ptr<MiniballDataPackets> data_packet =
696 std::make_shared<MiniballDataPackets>( info_data );
697 data_vector.emplace_back( data_packet );
698 data_map.push_back( std::make_pair<unsigned long,double>(
699 data_vector.size()-1, data_packet->GetTime() ) );
700 }
701
702 }
703
704 }
705
706 else {
707
708 // Set values for data item
709 dgf_data->SetEventID( my_event_id );
710 dgf_data->SetRunTime( RunTime * set->GetDgfTimestampUnits() );
711 dgf_data->SetEventTime( EventTime * set->GetDgfTimestampUnits() );
712 dgf_data->SetFastTriggerTime( FastTriggerTime * set->GetDgfTimestampUnits() );
713 dgf_data->SetLongFastTriggerTime( time_corr );
714 dgf_data->SetHitPattern( HitPattern );
715 dgf_data->SetQshort( Qshort );
716 dgf_data->SetModule( mod );
717 dgf_data->SetChannel( ch );
718 dgf_data->SetEnergy( energy );
719 dgf_data->SetThreshold( thresh );
720 dgf_data->SetUserValues( UserValues );
721 dgf_data->SetUserValues( trace );
722
723 // Fill the tree
724 if( !flag_source ) {
725 std::shared_ptr<MiniballDataPackets> data_packet =
726 std::make_shared<MiniballDataPackets>( dgf_data );
727 data_vector.emplace_back( data_packet );
728 data_map.push_back( std::make_pair<unsigned long,double>(
729 data_vector.size()-1, data_packet->GetTime() ) );
730 }
731
732 }
733
734 } // channel mask, we have data in this channel
735
736 } // loop over channels
737
738 } // else of 'if(!(HitPattern&0xf) && ModuleNumber!=TS_EBIS_T1_T2_MODULE...)'
739
740 } // i < mod: data for this module
741
742 // MBS data: always 32bit words -> if len odd -> fill word has to be skipped
743 //std::cout << std::dec << "length = " << length << ", wc = " << wc << ", i = " << i << std::endl;
744 if( length & 1 ) i++;
745
746 } // data in this sub event
747
748 return;
749
750}
751
752
753// Function to run the conversion for a single file
754int MiniballMedConverter::ConvertFile( std::string input_file_name,
755 unsigned long start_subevt,
756 long end_subevt ) {
757
758 // Uncomment to force only a few subevts - debug
759 //end_subevt = 1000;
760
761 // Read the file.
762 std::ifstream input_file( input_file_name, std::ios::in|std::ios::binary );
763 if( !input_file.is_open() ){
764
765 std::cout << "Cannot open " << input_file_name << std::endl;
766 return -1;
767
768 }
769
770 // Reset counters to zero for every file
771 StartFile();
772
773 // Calculate the size of the file.
774 input_file.seekg( 0, input_file.end );
775 unsigned long long size_end = input_file.tellg();
776 input_file.seekg( 0, input_file.beg );
777 unsigned long long size_beg = input_file.tellg();
778 unsigned long long FILE_SIZE = size_end - size_beg;
779
780 // Calculate the number of blocks in the file.
781 unsigned long BLOCKS_NUM = FILE_SIZE / set->GetBlockSize();
782
783 std::cout << "Opening file: " << input_file_name << std::endl;
784 sslogs << "File size = " << (double)FILE_SIZE/1024./1024.;
785 sslogs << " MB" << std::endl;
786 std::cout << sslogs.str() << std::endl;
787 sslogs.str( std::string() ); // clean up
788
789 // Close the file
790 input_file.close();
791
792 // Create an MED data instance and set block/buffer size etc
793 MBS mbs;
794 mbs.SetBufferSize( set->GetBlockSize() );
795 mbs.OpenMedFile( input_file_name );
796
797 // Loop over all the MBS Events
798 unsigned long mbsevt = 0, nblock = 0;
799 for( mbsevt = 0; ; mbsevt++ ){
800
801 // Calculate how many blocks we have used and progress
802 nblock = mbs.GetBufferCount();
803 if( nblock % 200 == 0 || nblock+1 == BLOCKS_NUM ) {
804
805 // Percent complete
806 float percent = (float)(nblock+1)*100.0/(float)BLOCKS_NUM;
807
808 // Progress bar in GUI
809 if( _prog_ ){
810
811 prog->SetPosition( percent );
812 gSystem->ProcessEvents();
813
814 }
815
816 // Progress bar in terminal
817 std::cout << " " << std::setw(8) << std::setprecision(4);
818 std::cout << percent << "%\r";
819 std::cout.flush();
820
821 }
822
823 // Get the next event - returns nullptr at the end of the file and sets eof to true
824 ev = mbs.GetNextMedEvent();
825 if( mbs.IsEof() ) break;
826 if( !ev ) continue;
828
829 // Don't bother if we're not running
830 if( !mbs.IsRunning() ) continue;
831
832 if( my_event_id == 0 )
833 std::cout << "Bad event ID in data" << std::endl;
834
835
836 // Check if we are before the start sub event or after the end sub events
837 if( mbsevt < start_subevt || ( (long)mbsevt > end_subevt && end_subevt > 0 ) )
838 continue;
839
840 // Process current block
841 mbsinfo_packet->ClearData();
842 ProcessEvent( mbsevt );
843
844 // Write the MBS event info
845 mbsinfo_packet->SetTime( my_good_tm_stp );
846 mbsinfo_packet->SetEventID( my_event_id );
847 mbsinfo_tree->Fill();
848
849 } // loop - mbsevt < MBS_EVENTS
850
851 // Close the file
852 mbs.CloseFile();
853
854 // Print stats
855 std::cout << std::endl;
856
857 return mbsevt;
858
859}
#define MBS_STYPE_VME_SIS_3
Definition MbsDefines.hh:66
#define COMP3_LM_BUFFORMAT
#define MESYTEC_MADC_WORD_COUNT
#define MESYTEC_MADC_OUTPUT_FORMAT
#define STD_LM_BUFFORMAT
#define MESYTEC_MADC_VALUE
#define DGF_SCALER_INDEX_CH_OFFSET
#define STD_FLM_BUFFORMAT
#define MBS_STYPE_CAMAC_DGF_3
Definition MbsDefines.hh:52
#define XIA_EVENT
#define MESYTEC_MADC_EXTENDED_TIMESTAMP_SHIFT
#define MBS_STYPE_TIME_STAMP
Definition MbsDefines.hh:82
#define DGF_SCALER_INDEX_NEVENTS
#define COMP_FLM_BUFFORMAT
#define MESYTEC_MADC_END_OF_EVENT
#define DGF_SCALER_MIN_SIZE
#define MBS_STYPE_VME_MADC_2
Definition MbsDefines.hh:77
#define MESYTEC_MADC_OUT_OF_RANGE
#define CHANHEADLEN
#define DGF_SCALER_END_OF_BUFFER
#define SIS3600_MSERIAL
#define MESYTEC_MADC_TIMESTAMP
#define MBS_STYPE_CAMAC_WO_ID_1
Definition MbsDefines.hh:45
#define MESYTEC_MADC_CHANNEL_NUMBER
#define SIS3600_D_HDR
#define MESYTEC_MADC_EXTENDED_TIMESTAMP
#define DGF_SCALER_INDEX_REALTIME
#define MBS_STYPE_VME_MADC_3
Definition MbsDefines.hh:78
#define COMP_LM_BUFFORMAT
#define DGF_SCALER_INDEX_CH_SIZE
#define DGF_SCALER_INDEX_GSLTTIME
#define DGF_SCALER_INDEX_RUNTIME
#define MESYTEC_MADC_MODULE_ID
#define DGF_SCALER_MAGIC_WORD
#define MBS_STYPE_VME_MADC_1
Definition MbsDefines.hh:76
void SetGSLTTime(long long time)
void SetModule(unsigned short id)
void SetRealTime(long long time)
void SetFastPeak(unsigned short i, unsigned int fp)
void SetClusterID(unsigned short id)
void SetLiveTime(unsigned short i, long long time)
void SetRunTime(long long time)
const MBSSubEvent * GetSubEvent(unsigned int i) const
Definition MbsFormat.hh:318
unsigned int GetNumberOfSubEvents() const
Definition MbsFormat.hh:317
ULong_t GetEventID() const
Definition MbsFormat.hh:300
short GetData(unsigned int i) const
Definition MbsFormat.hh:214
unsigned int GetDataLength() const
Definition MbsFormat.hh:238
unsigned long GetSubEventID() const
Definition MbsFormat.hh:237
unsigned short GetModuleID() const
Definition MbsFormat.hh:241
unsigned int GetSubEventType() const
Definition MbsFormat.hh:234
unsigned int GetNumberOfData() const
Definition MbsFormat.hh:218
std::string GetSubEventDescription() const
Definition MbsFormat.hh:235
void CloseFile()
Definition MbsFormat.cc:172
const MBSEvent * GetNextMedEvent()
Definition MbsFormat.cc:300
void OpenMedFile(std::string _filename)
Definition MbsFormat.cc:113
void SetBufferSize(unsigned int size)
Definition MbsFormat.hh:414
bool IsEof()
Definition MbsFormat.hh:447
UInt_t GetBufferCount()
Definition MbsFormat.hh:422
bool IsRunning()
Definition MbsFormat.hh:446
std::vector< std::vector< TH1F * > > hdgf_cal
Definition Converter.hh:199
std::shared_ptr< MiniballCalibration > cal
Definition Converter.hh:220
std::shared_ptr< DgfData > dgf_data
Definition Converter.hh:164
std::shared_ptr< MiniballSettings > set
Definition Converter.hh:217
std::vector< std::shared_ptr< MiniballDataPackets > > data_vector
Definition Converter.hh:170
std::vector< std::vector< TH1F * > > hadc_cal
Definition Converter.hh:201
std::shared_ptr< InfoData > info_data
Definition Converter.hh:167
std::vector< std::pair< unsigned long, double > > data_map
Definition Converter.hh:171
std::vector< std::vector< TH1F * > > hdgf_qshort
Definition Converter.hh:198
std::shared_ptr< AdcData > adc_data
Definition Converter.hh:165
std::stringstream sslogs
Definition Converter.hh:105
std::shared_ptr< TGProgressBar > prog
Definition Converter.hh:224
std::vector< std::vector< TH1F * > > hadc_qshort
Definition Converter.hh:200
unsigned long long my_good_tm_stp
Definition Converter.hh:116
unsigned long long my_event_id
Definition Converter.hh:120
std::shared_ptr< MBSInfoPackets > mbsinfo_packet
Definition Converter.hh:162
const MBSSubEvent * mbs_sevt
const MBSEvent * ev
int ConvertFile(std::string input_file_name, unsigned long start_block=0, long end_block=-1)
void ProcessEvent(unsigned long nblock)