1 | /********************************************************************\
|
---|
2 |
|
---|
3 | RawDataCTX.cc
|
---|
4 |
|
---|
5 | Class for raw data handling of the DRS 2-based DAQ
|
---|
6 |
|
---|
7 | Oliver Grimm
|
---|
8 |
|
---|
9 | \********************************************************************/
|
---|
10 |
|
---|
11 | #include "RawDataCTX.h"
|
---|
12 |
|
---|
13 | // *** Constructor
|
---|
14 | RawDataCTX::RawDataCTX(bool BeSilent) {
|
---|
15 | RHeader = new RunHeader;
|
---|
16 | EHeader = new EventHeader;
|
---|
17 | FileOpen = false;
|
---|
18 | Silent = BeSilent;
|
---|
19 | }
|
---|
20 |
|
---|
21 | // *** Destructor
|
---|
22 | RawDataCTX::~RawDataCTX() {
|
---|
23 | if(FileOpen) CloseDataFile();
|
---|
24 |
|
---|
25 | delete RHeader;
|
---|
26 | delete EHeader;
|
---|
27 | }
|
---|
28 |
|
---|
29 | // *** Open raw data file
|
---|
30 | CTX_ErrCode RawDataCTX::OpenDataFile(char *Filename, FILE *fptr) {
|
---|
31 |
|
---|
32 | if(FileOpen) CloseDataFile();
|
---|
33 | // Open file
|
---|
34 | if ((Rawfile = fopen(Filename, "r")) == NULL) {
|
---|
35 | if(!Silent) printf("Error: Could not open file: %s\n", Filename);
|
---|
36 | return CTX_FOPEN;
|
---|
37 | }
|
---|
38 | // Read run header
|
---|
39 | if (fread(RHeader, sizeof(RunHeader), 1, Rawfile) != 1) {
|
---|
40 | if(!Silent) printf("Error: Could not read run header\n");
|
---|
41 | fclose(Rawfile);
|
---|
42 | return CTX_RHEADER;
|
---|
43 | }
|
---|
44 | // Check magic number of run header
|
---|
45 | if (RHeader->MagicNum!=MAGICNUM_OPEN && RHeader->MagicNum!=MAGICNUM_CLOSED && RHeader->MagicNum!=MAGICNUM_ERROR) {
|
---|
46 | if(!Silent) printf("Error: Magic number of run header incorrect\n");
|
---|
47 | fclose(Rawfile);
|
---|
48 | return CTX_RHEADER;
|
---|
49 | }
|
---|
50 | // Read board structures
|
---|
51 | BStruct = new BoardStructure [RHeader->NCMCBoards];
|
---|
52 | if(fread(BStruct, sizeof(BoardStructure), RHeader->NCMCBoards, Rawfile) != RHeader->NCMCBoards) {
|
---|
53 | if(!Silent) printf("Error: Could not read (all) board structures\n");
|
---|
54 | fclose(Rawfile);
|
---|
55 | delete[] BStruct;
|
---|
56 | return CTX_BSTRUCT;
|
---|
57 | }
|
---|
58 | // Allocate memory for event data
|
---|
59 | Data = new short[RHeader->NCMCBoards*RHeader->NChips*RHeader->NChannels*RHeader->Samples];
|
---|
60 | FileOpen = true;
|
---|
61 |
|
---|
62 | // If requested, print run header (including board structures) to file
|
---|
63 | if(fptr != NULL) {
|
---|
64 | fprintf(fptr, "Magic number %x (%s)\n", RHeader->MagicNum, RHeader->MagicNum==MAGICNUM_CLOSED?"OK":(RHeader->MagicNum==MAGICNUM_OPEN?"File not closed":"Error"));
|
---|
65 | fprintf(fptr, "Data format: %d\n", RHeader->DataFormat);
|
---|
66 | fprintf(fptr, "Run header size: %d\n", RHeader->RunHeaderSize);
|
---|
67 | fprintf(fptr, "Event header size: %d\n", RHeader->EventHeaderSize);
|
---|
68 | fprintf(fptr, "Board structure size: %d\n", RHeader->BoardStructureSize);
|
---|
69 |
|
---|
70 | fprintf(fptr, "DAQ compilation: %s\n", RHeader->DAQVersion);
|
---|
71 | fprintf(fptr, "Source: %s\n", RHeader->Source);
|
---|
72 | fprintf(fptr, "Run type: %c\n", RHeader->Type);
|
---|
73 | fprintf(fptr, "Run number: %u\n", RHeader->RunNumber);
|
---|
74 | fprintf(fptr, "File number: %u\n", RHeader->FileNumber);
|
---|
75 | fprintf(fptr, "Events: %u\n", RHeader->Events);
|
---|
76 | fprintf(fptr, "CMC Boards: %u\n", RHeader->NCMCBoards);
|
---|
77 | fprintf(fptr, "DRS chips: %u\n", RHeader->NChips);
|
---|
78 | fprintf(fptr, "Channels/chip: %u\n", RHeader->NChannels);
|
---|
79 | fprintf(fptr, "Samples: %u\n", RHeader->Samples);
|
---|
80 | fprintf(fptr, "Offset: %d\n", RHeader->Offset);
|
---|
81 |
|
---|
82 | fprintf(fptr, "Source RA: %f\n", RHeader->SourceRA);
|
---|
83 | fprintf(fptr, "Source DEC: %f\n", RHeader->SourceDEC);
|
---|
84 | fprintf(fptr, "Telescope RA: %f\n", RHeader->TelescopeRA);
|
---|
85 | fprintf(fptr, "Telescope DEC: %f\n", RHeader->TelescopeDEC);
|
---|
86 |
|
---|
87 | fprintf(fptr, "Start year: %u\n", RHeader->StartYear);
|
---|
88 | fprintf(fptr, "Start month: %u\n", RHeader->StartMonth);
|
---|
89 | fprintf(fptr, "Start day: %u\n", RHeader->StartDay);
|
---|
90 | fprintf(fptr, "Start hour: %u\n", RHeader->StartHour);
|
---|
91 | fprintf(fptr, "Start minute: %u\n", RHeader->StartMinute);
|
---|
92 | fprintf(fptr, "Start second: %u\n", RHeader->StartSecond);
|
---|
93 | fprintf(fptr, "End year: %u\n", RHeader->EndYear);
|
---|
94 | fprintf(fptr, "End month: %u\n", RHeader->EndMonth);
|
---|
95 | fprintf(fptr, "End day: %u\n", RHeader->EndDay);
|
---|
96 | fprintf(fptr, "End hour: %u\n", RHeader->EndHour);
|
---|
97 | fprintf(fptr, "End minute: %u\n", RHeader->EndMinute);
|
---|
98 | fprintf(fptr, "End second: %u\n", RHeader->EndSecond);
|
---|
99 |
|
---|
100 | for (unsigned int i=0; i<RHeader->NCMCBoards; i++) {
|
---|
101 | fprintf(fptr, "*** Board %d ***\n", i);
|
---|
102 | fprintf(fptr, "Serial number: %d\n", BStruct[i].SerialNo);
|
---|
103 | fprintf(fptr, "Sampling frequency [GHz]: %.3f\n", BStruct[i].NomFreq);
|
---|
104 | fprintf(fptr, "Temperature [C]: %.3f\n", BStruct[i].BoardTemp);
|
---|
105 | fprintf(fptr, "Scale factor: %.3f\n", BStruct[i].ScaleFactor);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | return CTX_OK;
|
---|
109 | }
|
---|
110 |
|
---|
111 | // *** Close raw data file
|
---|
112 | CTX_ErrCode RawDataCTX::CloseDataFile() {
|
---|
113 |
|
---|
114 | if(!FileOpen) return CTX_NOTOPEN;
|
---|
115 |
|
---|
116 | if (fclose(Rawfile) == EOF) {
|
---|
117 | if(!Silent) perror("Could not close file");
|
---|
118 | return CTX_FCLOSE;
|
---|
119 | }
|
---|
120 |
|
---|
121 | delete[] BStruct;
|
---|
122 | delete[] Data;
|
---|
123 |
|
---|
124 | FileOpen = false;
|
---|
125 | return CTX_OK;
|
---|
126 | }
|
---|
127 |
|
---|
128 | // *** Read next event from file
|
---|
129 | CTX_ErrCode RawDataCTX::ReadEvent(int EventNo, FILE *fptr) {
|
---|
130 |
|
---|
131 | if(!FileOpen) {
|
---|
132 | if(!Silent) printf("Error: No data file open.\n");
|
---|
133 | return CTX_NOTOPEN;
|
---|
134 | }
|
---|
135 |
|
---|
136 | // Move file pointer to desired event header (if zero read next event)
|
---|
137 | if(EventNo!=0 && fseek(Rawfile, sizeof(RunHeader)+sizeof(BoardStructure)*RHeader->NCMCBoards+(EventNo-1)*(sizeof(EventHeader)+RHeader->NCMCBoards*RHeader->NChips*
|
---|
138 | RHeader->NChannels*RHeader->Samples*sizeof(short)), SEEK_SET)!=0) {
|
---|
139 | if(!Silent) printf("Error: Could not move to requested event\n");
|
---|
140 | return CTX_SEEK;
|
---|
141 | }
|
---|
142 |
|
---|
143 | // Read event header
|
---|
144 | if (fread(EHeader, sizeof(EventHeader), 1, Rawfile) != 1) {
|
---|
145 | if(feof(Rawfile)==0) {
|
---|
146 | if(!Silent) printf("Error: Could not read event header\n");
|
---|
147 | return CTX_EHEADER;
|
---|
148 | }
|
---|
149 | else return CTX_EOF;
|
---|
150 | }
|
---|
151 | if(fread(Data, sizeof(short), RHeader->NCMCBoards*RHeader->NChips*RHeader->NChannels*RHeader->Samples, Rawfile)
|
---|
152 | != RHeader->NCMCBoards*RHeader->NChips*RHeader->NChannels*RHeader->Samples) {
|
---|
153 | if(!Silent) printf("Error: Could not read (all) data\n");
|
---|
154 | return CTX_DATA;
|
---|
155 | }
|
---|
156 |
|
---|
157 | // If requested, print event header to file
|
---|
158 | if(fptr != NULL) {
|
---|
159 | fprintf(fptr, "Name: %s\n", EHeader->Name);
|
---|
160 | fprintf(fptr, "Event number: %u\n", EHeader->EventNumber);
|
---|
161 | fprintf(fptr, "Time [s]: %f\n", EHeader->TimeSec);
|
---|
162 | fprintf(fptr, "Trigger type: 0X%0X\n", EHeader->TriggerType);
|
---|
163 | }
|
---|
164 |
|
---|
165 | return CTX_OK;
|
---|
166 | }
|
---|
167 |
|
---|
168 | // *** Check if file currently open
|
---|
169 | bool RawDataCTX::IsFileOpen() {
|
---|
170 | return FileOpen;
|
---|
171 | }
|
---|
172 |
|
---|