Analysis-Utilities 26.9.9
C++/ROOT utilities for nuclear measurement data analysis
Loading...
Searching...
No Matches
BinaryUtils.hpp
Go to the documentation of this file.
1#ifndef BINARYUTILS_H
2#define BINARYUTILS_H
3
4#include <TArrayS.h>
5#include <TMath.h>
6#include <TROOT.h>
7#include <TString.h>
8#include <TSystem.h>
9#include <bitset>
10#include <fstream>
11#include <iostream>
12#include <vector>
13
32
39struct RawHit {
40 UShort_t board;
41 UShort_t channel;
42 UShort_t energy;
43 ULong64_t timestamp;
44 UInt_t flags;
45};
46
55protected:
56 std::ifstream file;
57 TString filename;
58 Bool_t file_open;
59 Long64_t bytes_read;
60
61public:
64
66 virtual ~BinaryReader() {
67 if (file.is_open()) {
68 file.close();
69 }
70 }
71
78 virtual Bool_t Open(const char *fname) {
79 filename = fname;
80 file.open(fname, std::ios::binary);
81
82 if (!file.is_open()) {
83 std::cerr << "ERROR: Cannot open file " << fname << std::endl;
84 file_open = kFALSE;
85 return kFALSE;
86 }
87
88 file_open = kTRUE;
89 bytes_read = 0;
90 return kTRUE;
91 }
92
94 virtual void Close() {
95 if (file.is_open()) {
96 file.close();
97 }
98 file_open = kFALSE;
99 }
100
102 Bool_t IsOpen() const { return file_open; }
104 TString GetFileName() const { return filename; }
106 Long64_t GetBytesRead() const { return bytes_read; }
107
109 Bool_t IsEOF() const { return !file_open || file.eof(); }
110
116 virtual Bool_t ReadEvent() = 0;
117};
118
127public:
141
144 static const UInt_t DEADTIME_OCCURRED = 0x1;
145 static const UInt_t TIMESTAMP_ROLLOVER = 0x2;
146 static const UInt_t TIMESTAMP_RESET_EXT = 0x4;
147 static const UInt_t FAKE_EVENT = 0x8;
148 static const UInt_t MEMORY_FULL = 0x10;
149 static const UInt_t TRIGGER_LOST = 0x20;
150 static const UInt_t N_TRIGGERS_LOST = 0x40;
151 static const UInt_t SATURATION_IN_GATE = 0x80;
152 static const UInt_t TRIGGERS_1024_COUNTED = 0x100;
153 static const UInt_t FIRST_AFTER_BUSY = 0x200;
154 static const UInt_t INPUT_SATURATING = 0x400;
155 static const UInt_t N_TRIGGERS_COUNTED = 0x800;
156 static const UInt_t NOT_MATCHED_TIMEFILTER = 0x1000;
157 static const UInt_t FINE_TIMESTAMP = 0x4000;
158 static const UInt_t PILEUP = 0x8000;
159 static const UInt_t PLL_LOCK_LOSS = 0x80000;
160 static const UInt_t OVER_TEMPERATURE = 0x100000;
161 static const UInt_t ADC_SHUTDOWN = 0x200000;
163
164 UShort_t header;
165 UShort_t board;
166 UShort_t channel;
167 ULong64_t timestamp;
168 UShort_t energy_ch;
169 Double_t energy_cal;
171 UInt_t flags;
173 UInt_t num_samples;
174 TArrayS samples;
175
177 Bool_t hasEnergyCh() const { return (header & 0x0001); }
179 Bool_t hasEnergyCal() const { return (header & 0x0002); }
181 Bool_t hasEnergyShort() const { return (header & 0x0004); }
183 Bool_t hasWaveform() const { return (header & 0x0008); }
185 UChar_t getControlBits() const { return header & 0x000F; }
186
188 Bool_t hasDeadtime() const { return flags & DEADTIME_OCCURRED; }
190 Bool_t hasTimestampRollover() const { return flags & TIMESTAMP_ROLLOVER; }
192 Bool_t hasTimestampResetExt() const { return flags & TIMESTAMP_RESET_EXT; }
194 Bool_t isFakeEvent() const { return flags & FAKE_EVENT; }
196 Bool_t hasMemoryFull() const { return flags & MEMORY_FULL; }
198 Bool_t hasTriggerLost() const { return flags & TRIGGER_LOST; }
200 Bool_t hasNTriggersLost() const { return flags & N_TRIGGERS_LOST; }
202 Bool_t hasSaturation() const { return flags & SATURATION_IN_GATE; }
204 Bool_t has1024Triggers() const { return flags & TRIGGERS_1024_COUNTED; }
206 Bool_t isFirstAfterBusy() const { return flags & FIRST_AFTER_BUSY; }
208 Bool_t isInputSaturating() const { return flags & INPUT_SATURATING; }
210 Bool_t hasNTriggersCounted() const { return flags & N_TRIGGERS_COUNTED; }
212 Bool_t isNotMatchedTimeFilter() const {
214 }
215
216 Bool_t hasFineTimestamp() const { return flags & FINE_TIMESTAMP; }
218 Bool_t isPileup() const { return flags & PILEUP; }
220 Bool_t hasPLLLockLoss() const { return flags & PLL_LOCK_LOSS; }
222 Bool_t isOverTemperature() const { return flags & OVER_TEMPERATURE; }
224 Bool_t isADCShutdown() const { return flags & ADC_SHUTDOWN; }
225
228 TString getWaveformCodeName() const;
230 std::vector<TString> getActiveFlags() const;
234 void Print() const;
236 void PrintHeader() const;
238 void PrintFlags() const;
240 void PrintWaveform() const;
241
243 CoMPASSData();
244 virtual ~CoMPASSData() {}
245};
246
254private:
255 UShort_t global_header;
256 CoMPASSData current_event;
257
258public:
260 CoMPASSReader() : BinaryReader(), global_header(0) {}
261
262 virtual ~CoMPASSReader() {}
263
269 Bool_t Open(const char *fname) override;
280 Bool_t Open(const char *fname, UShort_t header_override);
281
299 Bool_t ReadEvent() override;
300
303 const CoMPASSData &GetCurrentEvent() const { return current_event; }
306 CoMPASSData &GetCurrentEvent() { return current_event; }
307
309 UShort_t GetGlobalHeader() const { return global_header; }
310};
311
316public:
317 UInt_t event_size;
318 UInt_t board_id;
319 UInt_t pattern;
320 UInt_t channel;
323 UInt_t dc_offset;
327 TArrayS samples;
328
329 void Print() const;
330
333 virtual ~WaveDump742Data() {}
334};
335
340private:
341 WaveDump742Data current_event;
342 Bool_t corrections_enabled;
343
344public:
350 WaveDump742Reader(Bool_t with_corrections = kTRUE)
351 : BinaryReader(), corrections_enabled(with_corrections) {}
352
354
355 Bool_t ReadEvent() override;
356
359 const WaveDump742Data &GetCurrentEvent() const { return current_event; }
362 WaveDump742Data &GetCurrentEvent() { return current_event; }
363
366 void SetCorrectionsEnabled(Bool_t enable) { corrections_enabled = enable; }
368 Bool_t GetCorrectionsEnabled() const { return corrections_enabled; }
369};
370
378struct SOLHit {
379 UChar_t channel;
380 UShort_t energy;
381 UShort_t energy_short;
382 ULong64_t timestamp;
383 UShort_t fine_timestamp;
384 UShort_t flags_high;
385 UShort_t flags_low;
386 UChar_t data_type;
387 Bool_t is_psd;
389 UChar_t board_fail;
390 UChar_t flush;
391 UShort_t trigger_thr;
392 ULong64_t event_size;
393 UInt_t agg_counter;
394 ULong64_t block_id;
395 ULong64_t trace_len;
397 UChar_t ana_probe_type[2];
398 UChar_t dig_probe_type[4];
399};
400
410class SOLData {
411public:
418 enum DataType : UChar_t {
419 ALL = 0,
424 Raw = 10
425 };
426
428 UShort_t block_header;
429 UChar_t channel;
430 UShort_t energy;
431 UShort_t energy_short;
432 ULong64_t timestamp;
434 UShort_t flags_high;
435 UShort_t flags_low;
436 UChar_t data_type;
437 Bool_t is_psd;
439 UChar_t board_fail;
440 UChar_t flush;
441 UShort_t trigger_thr;
442 ULong64_t event_size;
444 ULong64_t trace_len;
445 ULong64_t block_id;
446 UChar_t ana_probe_type[2];
447 UChar_t dig_probe_type[4];
448
449 // Trace buffer, stored exactly as the SOLARIS DAQ writes it (SolReader.h in
450 // SOLARIS_DAQ): contiguous blocks, NOT interleaved samples. ALL format:
451 // [trace0[0..len-1] as Int32][trace1[0..len-1] as Int32]
452 // [dig0[0..len-1]][dig1[0..len-1]][dig2[0..len-1]][dig3[0..len-1]]
453 // i.e. 12 bytes per sample in total. OneTrace format: [trace0[0..len-1]] as
454 // Int32, 4 bytes per sample. Each accessor returns a pointer to its block,
455 // indexed by sample.
458 std::vector<Char_t> trace_data;
459
462 Bool_t hasTraces() const {
463 return (data_type == ALL || data_type == OneTrace) && trace_len > 0;
464 }
465
467 UInt_t getSamples() const { return static_cast<UInt_t>(trace_len); }
468
477 const Int_t *getAnalog0() const {
478 const std::size_t len = static_cast<std::size_t>(trace_len);
479 if ((data_type == ALL && trace_data.size() >= len * 12) ||
480 (data_type == OneTrace && trace_data.size() >= len * 4)) {
481 return reinterpret_cast<const Int_t *>(trace_data.data());
482 }
483 return nullptr;
484 }
485
494 const Int_t *getAnalog1() const {
495 const std::size_t len = static_cast<std::size_t>(trace_len);
496 if (data_type == ALL && trace_data.size() >= len * 12) {
497 return reinterpret_cast<const Int_t *>(trace_data.data() + len * 4);
498 }
499 return nullptr;
500 }
501
511 const UChar_t *getDigital(UInt_t ch) const {
512 const std::size_t len = static_cast<std::size_t>(trace_len);
513 if (ch > 3 || data_type != ALL || trace_data.size() < len * 12) {
514 return nullptr;
515 }
516 return reinterpret_cast<const UChar_t *>(trace_data.data() + len * 8 +
517 len * ch);
518 }
519
527 const Int_t *getOneTrace() const {
528 if (data_type == OneTrace &&
529 trace_data.size() >=
530 static_cast<std::size_t>(trace_len) * sizeof(Int_t)) {
531 return reinterpret_cast<const Int_t *>(trace_data.data());
532 }
533 return nullptr;
534 }
535
537 void clearTraces() { trace_data.clear(); }
538
540 TString getDataTypeName() const;
541 void Print() const;
542
544 SOLData();
546};
547
551class SOLReader : public BinaryReader {
552private:
553 SOLData current_event;
554 Long64_t block_id;
555 Bool_t skip_traces;
556
557public:
559 SOLReader() : BinaryReader(), block_id(0), skip_traces(kFALSE) {}
560
561 virtual ~SOLReader() {}
562
568 void SetSkipTraces(Bool_t skip) { skip_traces = skip; }
570 Bool_t GetSkipTraces() const { return skip_traces; }
571
572 Bool_t ReadEvent() override;
573
576 const SOLData &GetCurrentEvent() const { return current_event; }
579 SOLData &GetCurrentEvent() { return current_event; }
580
582 Long64_t GetBlockID() const { return block_id; }
583
592 SOLHit ToHit() const;
593
611 static std::vector<TString> SplitSolFileByTime(const char *inputFile,
612 const char *outputDir,
613 Double_t chunkSeconds,
614 Int_t &totalBlocks,
615 Int_t &totalChunks);
616};
617
618#endif
Long64_t bytes_read
BinaryReader()
Construct a reader with no file open.
Bool_t IsOpen() const
Whether a file is currently open.
Long64_t GetBytesRead() const
Bytes consumed so far, for progress reporting.
virtual ~BinaryReader()
Closes the file if it is still open.
std::ifstream file
TString filename
virtual Bool_t Open(const char *fname)
Open a binary file for reading.
virtual Bool_t ReadEvent()=0
Decode the next record into the derived reader's current event.
TString GetFileName() const
Path most recently passed to Open().
virtual void Close()
Close the file. Safe to call when nothing is open.
Bool_t IsEOF() const
Whether the file is exhausted or was never opened.
One decoded CoMPASS event, with header-bit and status-flag accessors.
static const UInt_t FIRST_AFTER_BUSY
static const UInt_t PLL_LOCK_LOSS
Bool_t hasDeadtime() const
Deadtime occurred before this event.
Bool_t isFirstAfterBusy() const
First event after a busy period.
UShort_t energy_short_ch
Short-gate energy; valid if hasEnergyShort().
UShort_t channel
Channel index on that board.
UShort_t energy_ch
Energy in ADC channel units; valid if hasEnergyCh().
std::vector< TString > getActiveFlags() const
Names of every status flag currently set in flags.
static const UInt_t MEMORY_FULL
static const UInt_t N_TRIGGERS_COUNTED
static const UInt_t N_TRIGGERS_LOST
void Print() const
Print the whole event to stdout.
TArrayS samples
Waveform samples; valid if hasWaveform().
void PrintFlags() const
Print the active status flags to stdout.
Bool_t isOverTemperature() const
The board reported over-temperature.
static const UInt_t DEADTIME_OCCURRED
UShort_t board
Digitiser board id.
static const UInt_t TRIGGERS_1024_COUNTED
Bool_t hasTriggerLost() const
At least one trigger was lost.
static const UInt_t FINE_TIMESTAMP
Bool_t hasPLLLockLoss() const
The PLL lost lock.
static const UInt_t SATURATION_IN_GATE
Bool_t isPileup() const
Pileup was detected.
static const UInt_t INPUT_SATURATING
Bool_t hasNTriggersCounted() const
The counted-trigger field is present.
virtual ~CoMPASSData()
static const UInt_t TRIGGER_LOST
static const UInt_t NOT_MATCHED_TIMEFILTER
static const UInt_t PILEUP
UInt_t flags
Status bits; see the flag constants above.
Bool_t hasTimestampRollover() const
The timestamp counter rolled over.
UChar_t getControlBits() const
The four header control bits as a nibble.
Bool_t hasEnergyShort() const
Whether the record carries a short-gate energy (header bit 2).
Double_t energy_cal
Calibrated energy; valid if hasEnergyCal().
Bool_t hasFineTimestamp() const
A fine timestamp is available.
Bool_t isNotMatchedTimeFilter() const
The event failed the coincidence time filter.
Bool_t hasMemoryFull() const
Board memory was full.
ULong64_t timestamp
Acquisition timestamp, in picoseconds.
static const UInt_t FAKE_EVENT
Bool_t has1024Triggers() const
1024 triggers have been counted.
WaveformCode
Which CoMPASS processing stage a stored waveform came from.
UShort_t header
Global header word; says which fields are valid.
static const UInt_t ADC_SHUTDOWN
Bool_t isInputSaturating() const
The input is currently saturating.
UInt_t num_samples
Length of samples.
static const UInt_t TIMESTAMP_ROLLOVER
Bool_t hasSaturation() const
The input saturated within the integration gate.
static const UInt_t OVER_TEMPERATURE
CoMPASSData()
Construct with all fields zeroed.
static const UInt_t TIMESTAMP_RESET_EXT
Bool_t hasEnergyCal() const
Whether the record carries a calibrated energy (header bit 1).
TString getWaveformCodeName() const
Human-readable name for waveform_code.
void PrintHeader() const
Print the header fields to stdout.
Bool_t isADCShutdown() const
The ADC shut down.
Bool_t hasNTriggersLost() const
The lost-trigger count field is present.
Bool_t isFakeEvent() const
Synthetic event inserted by the acquisition.
Bool_t hasEnergyCh() const
Whether the record carries an uncalibrated energy (header bit 0).
void PrintWaveform() const
Print the waveform samples to stdout.
UChar_t waveform_code
Processing stage of samples; see WaveformCode.
Bool_t hasTimestampResetExt() const
An external timestamp reset was applied.
Bool_t hasWaveform() const
Whether the record carries waveform samples (header bit 3).
const CoMPASSData & GetCurrentEvent() const
The event most recently read.
virtual ~CoMPASSReader()
CoMPASSReader()
Construct a reader with no file open.
Bool_t ReadEvent() override
Decode the next event into the current-event object.
CoMPASSData & GetCurrentEvent()
Mutable access to the event most recently read.
UShort_t GetGlobalHeader() const
The global header in force, read from the file or overridden.
Bool_t Open(const char *fname) override
Open a file and read its global header.
One decoded SOL block, optionally carrying its traces.
UChar_t down_sampling
UShort_t energy_short
void Print() const
UInt_t agg_counter
Bool_t is_psd
void clearTraces()
Release the trace buffer, invalidating every trace pointer.
UChar_t ana_probe_type[2]
ULong64_t block_id
UShort_t block_header
Raw block header word.
UChar_t board_fail
Bool_t hasTraces() const
Whether this block carries traces.
UShort_t trigger_thr
const UChar_t * getDigital(UInt_t ch) const
Pointer to one digital probe trace.
std::vector< Char_t > trace_data
Raw trace bytes, exactly as written by the DAQ.
UShort_t energy
UChar_t flush
const Int_t * getAnalog0() const
Pointer to the first analogue trace.
UChar_t data_type
const Int_t * getOneTrace() const
Pointer to the single trace of a OneTrace block.
TString getDataTypeName() const
Human-readable name for data_type.
ULong64_t timestamp
UShort_t fine_timestamp
UShort_t flags_high
UChar_t channel
DataType
SOL block layout, which decides both the fields and the traces present.
UInt_t getSamples() const
Samples per trace in this block.
SOLData()
Construct with all fields zeroed and no traces.
ULong64_t event_size
UChar_t dig_probe_type[4]
const Int_t * getAnalog1() const
Pointer to the second analogue trace.
ULong64_t trace_len
UShort_t flags_low
SOLHit ToHit() const
Copy the current block's header fields into a standalone SOLHit.
static std::vector< TString > SplitSolFileByTime(const char *inputFile, const char *outputDir, Double_t chunkSeconds, Int_t &totalBlocks, Int_t &totalChunks)
Split a run file into time-bounded chunks.
Long64_t GetBlockID() const
Zero-based index of the block most recently read.
SOLData & GetCurrentEvent()
Mutable access to the block most recently read.
SOLReader()
Construct a reader with no file open and traces enabled.
virtual ~SOLReader()
const SOLData & GetCurrentEvent() const
The block most recently read.
Bool_t ReadEvent() override
Decode the next record into the derived reader's current event.
Bool_t GetSkipTraces() const
Whether trace payloads are being skipped.
void SetSkipTraces(Bool_t skip)
Skip trace payloads while still parsing every block.
One decoded WaveDump event from a DT5742-family digitiser.
UInt_t pattern
Front-panel I/O pattern word.
WaveDump742Data()
Construct with all fields zeroed.
UInt_t start_index_cell
Switched-capacitor start cell.
UInt_t group_trigger_time_tag
Group trigger time tag.
virtual ~WaveDump742Data()
TArrayS samples
Waveform samples, in ADC counts.
UInt_t event_counter
Board's running event counter.
UInt_t board_id
Digitiser board id.
void Print() const
UInt_t event_size
Record size in 32-bit words.
UInt_t channel
Channel index.
UInt_t dc_offset
DC offset applied to this channel.
void SetCorrectionsEnabled(Bool_t enable)
Turn the DT5742 timing corrections on or off.
WaveDump742Data & GetCurrentEvent()
Mutable access to the event most recently read.
Bool_t GetCorrectionsEnabled() const
Whether the timing corrections are being applied.
Bool_t ReadEvent() override
Decode the next record into the derived reader's current event.
virtual ~WaveDump742Reader()
WaveDump742Reader(Bool_t with_corrections=kTRUE)
Construct a reader with no file open.
const WaveDump742Data & GetCurrentEvent() const
The event most recently read.
Minimal CoMPASS hit: header fields only, no waveform.
UInt_t flags
CoMPASS status bits; see CoMPASSData's constants.
UShort_t energy
Energy in ADC channel units.
UShort_t board
Digitiser board id.
UShort_t channel
Channel index on that board.
ULong64_t timestamp
Acquisition timestamp, in picoseconds.
Lightweight SOL block: header fields only, traces stripped.
Bool_t is_psd
Whether this block came from a PSD firmware.
UChar_t data_type
Block format; see SOLData::DataType.
UChar_t board_fail
Board failure indicator.
ULong64_t event_size
Size of the source block, in bytes.
UShort_t energy_short
Short-gate energy; meaningful when is_psd.
UChar_t dig_probe_type[4]
Digital probe selection for traces 0 to 3.
ULong64_t trace_len
Samples the source block carried, though this struct stores none of them.
UChar_t down_sampling
Trace downsampling factor applied on-board.
UShort_t trigger_thr
Trigger threshold in force.
ULong64_t timestamp
Coarse timestamp, in nanoseconds.
UChar_t flush
Flush indicator.
ULong64_t block_id
Zero-based index of this block in the file.
UChar_t ana_probe_type[2]
Analogue probe selection for traces 0 and 1.
UShort_t flags_high
Upper status word.
UShort_t energy
Long-gate energy, in ADC channel units.
UChar_t channel
Channel index.
UShort_t fine_timestamp
Sub-nanosecond refinement of timestamp.
UInt_t agg_counter
Aggregate counter from the board.
UShort_t flags_low
Lower status word.