SND@LHC Software
Loading...
Searching...
No Matches
ShipTdcSource.cxx
Go to the documentation of this file.
1#include <stdexcept>
2#include "ShipTdcSource.h"
3#include "FairLogger.h"
4#include "FairEventHeader.h"
5#include "ShipUnpack.h"
7
8ShipTdcSource::ShipTdcSource() : fFilename("tdcdata.bin") {}
9
10ShipTdcSource::ShipTdcSource(TString filename) : fFilename(std::move(filename)) {}
11
12ShipTdcSource::ShipTdcSource(const ShipTdcSource &source) : FairOnlineSource(source) {}
13
15
17{
18 fIn = TFile::Open(fFilename + "?filetype=raw", "read");
19 return kTRUE;
20}
21
23{
24 LOG(DEBUG) << "Closing file " << fFilename;
25 fIn->Close();
26}
27
28Int_t ShipTdcSource::UnpackEventFrame(Int_t *data, Int_t total_size)
29{
30 auto mf = reinterpret_cast<DataFrame *>(data);
31 total_size -= sizeof(DataFrame);
32 data = reinterpret_cast<Int_t *>(&(mf->hits));
33 auto frameTime = mf->header.frameTime;
34 while (total_size > 0) {
35 auto df = reinterpret_cast<DataFrame *>(data);
36 Int_t size = df->header.size;
37 uint16_t partitionId = df->header.partitionId;
38 LOG(DEBUG) << "ShipTdcSource: PartitionId " << std::hex << partitionId << std::dec;
39 if (!Unpack(data, size, partitionId)) {
40 LOG(WARNING) << "ShipTdcSource: Failed to Unpack.";
41 LOG(WARNING) << "ShipTdcSource: Maybe missing unpacker for PartitionId " << std::hex << partitionId << std::dec;
42 return 3;
43 }
44 data += size / sizeof(Int_t);
45 total_size -= size;
46 }
47 assert(total_size == 0);
48 return (frameTime == EoS) ? 1 : 0;
49}
50
52{
53 auto df = new (buffer) DataFrame();
54 if (!fIn->ReadBuffer(reinterpret_cast<char *>(df), sizeof(DataFrame))) {
55 size_t size = df->header.size;
56 auto frameTime = df->header.frameTime;
57 uint16_t partitionId = df->header.partitionId;
58 if (!fIn->ReadBuffer(reinterpret_cast<char *>(df->hits), size - sizeof(DataFrame))) {
59 switch (frameTime) {
60 case SoS: LOG(INFO) << "ShipTdcSource: SoS frame."; return 2;
61 case EoS: LOG(INFO) << "ShipTdcSource: EoS frame."; break;
62 default: break;
63 }
64 fEventTime = double(frameTime) * 25;
65 if (partitionId == 0x8000) {
66 LOG(DEBUG) << "ShipTdcSource: Event builder meta frame.";
67 if (fEventTime > 5000000000 && frameTime != EoS && frameTime != SoS) {
68 LOG(WARNING) << "Late event:";
69 for (int i = 0; i < size; i++) {
70 if (i % 4 == 0) {
71 std::cout << ' ';
72 } else if (i % 16 == 0) {
73 std::cout << '\n';
74 }
75 std::cout << std::hex << +buffer[i] << std::dec;
76 }
77 std::cout << std::endl;
78 }
79 return UnpackEventFrame(reinterpret_cast<Int_t *>(&buffer), size);
80 }
81 LOG(DEBUG) << "ShipTdcSource: PartitionId " << std::hex << partitionId << std::dec;
82
83 if (Unpack(reinterpret_cast<Int_t *>(&buffer), size, partitionId)) {
84 return (frameTime == EoS) ? 1 : 0;
85 }
86 LOG(WARNING) << "ShipTdcSource: Failed to Unpack.";
87 LOG(WARNING) << "ShipTdcSource: Maybe missing unpacker for PartitionId " << std::hex << partitionId << std::dec;
88 return 3;
89 }
90 LOG(WARNING) << "ShipTdcSource: Failed to read hits.";
91 return 2;
92 }
93 return 1;
94}
95
96Bool_t ShipTdcSource::Unpack(Int_t *data, Int_t size, uint16_t partitionId)
97{
98 try {
99 return fUnpackerMap.at(partitionId)->DoUnpack(data, size);
100 } catch (const std::out_of_range &oor) {
101 LOG(WARNING) << "ShipTdcSource: Failed to find suitable unpacker.";
102 return kFALSE;
103 }
104}
105
106void ShipTdcSource::FillEventHeader(FairEventHeader *feh)
107{
108 // TODO add frame times per partition?, -1 if not present?
109 feh->SetEventTime(fEventTime);
110 feh->SetRunId(fRunId);
111 return;
112}
113
std::map< uint16_t, FairUnpack * > fUnpackerMap
void FillEventHeader(FairEventHeader *feh)
unsigned char buffer[UINT16_MAX]
virtual ~ShipTdcSource()
Double_t fEventTime
Bool_t Unpack(Int_t *data, Int_t size, uint16_t partitionId)
virtual void Close()
Int_t UnpackEventFrame(Int_t *data, Int_t total_size)
TString fFilename
virtual Int_t ReadEvent(UInt_t=0)
virtual Bool_t Init()
ClassImp(ecalContFact) ecalContFact