source: drsdaq/RawDataCTX.cc@ 44

Last change on this file since 44 was 44, checked in by ogrimm, 15 years ago
Raw data format streamlined, revision tracking in run header
File size: 7.0 KB
Line 
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
14RawDataCTX::RawDataCTX(bool BeSilent) {
15 RHeader = new RunHeader;
16 EHeader = new EventHeader;
17 FileOpen = false;
18 Silent = BeSilent;
19}
20
21// *** Destructor
22RawDataCTX::~RawDataCTX() {
23 if(FileOpen) CloseDataFile();
24
25 delete RHeader;
26 delete EHeader;
27}
28
29// *** Open raw data file
30CTX_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 // Check if version of this software is not older than raw data format
51 if (RHeader->DataFormat > DATA_FORMAT) {
52 if(!Silent) printf("Error: Data format too new, incompatible with this read-out routine\n");
53 fclose(Rawfile);
54 return CTX_VERSION;
55 }
56 // Check if allocated headers are long enough
57 if (RHeader->RunHeaderSize>sizeof(RunHeader) || RHeader->BoardStructureSize>sizeof(BoardStructure) || RHeader->EventHeaderSize>sizeof(EventHeader)) {
58 if(!Silent) printf("Error: Header size(s) too long (there must be a problem with the data version!)\n");
59 fclose(Rawfile);
60 return CTX_VERSION;
61 }
62
63 // Read board structures
64 BStruct = new BoardStructure [RHeader->NCMCBoards];
65 for(unsigned int i=0; i<RHeader->NCMCBoards; i++) {
66 if(fread(&BStruct[i], RHeader->BoardStructureSize, 1, Rawfile) != 1) {
67 if(!Silent) printf("Error: Could not read board structure of board number %d\n",i+1);
68 fclose(Rawfile);
69 delete[] BStruct;
70 return CTX_BSTRUCT;
71 }
72 }
73 // Allocate memory for event data
74 Data = new short[RHeader->NCMCBoards*RHeader->NChips*RHeader->NChannels*RHeader->Samples];
75 FileOpen = true;
76
77 // If requested, print run header (including board structures) to file
78 if(fptr != NULL) {
79 fprintf(fptr, "Magic number %x (%s)\n", RHeader->MagicNum, RHeader->MagicNum==MAGICNUM_CLOSED?"OK":(RHeader->MagicNum==MAGICNUM_OPEN?"File not closed":"Error"));
80 fprintf(fptr, "Data format: %u\n", RHeader->DataFormat);
81 fprintf(fptr, "Software revision: %u\n", RHeader->SoftwareRevision);
82
83 fprintf(fptr, "Run header size: %d\n", RHeader->RunHeaderSize);
84 fprintf(fptr, "Event header size: %d\n", RHeader->EventHeaderSize);
85 fprintf(fptr, "Board structure size: %d\n", RHeader->BoardStructureSize);
86
87 fprintf(fptr, "Description: %s\n", RHeader->Description);
88 fprintf(fptr, "Run type: %u\n", RHeader->Type);
89 fprintf(fptr, "Run number: %u\n", RHeader->RunNumber);
90 fprintf(fptr, "File number: %u\n", RHeader->FileNumber);
91
92 fprintf(fptr, "Events: %u\n", RHeader->Events);
93 fprintf(fptr, "CMC Boards: %u\n", RHeader->NCMCBoards);
94 fprintf(fptr, "DRS chips: %u\n", RHeader->NChips);
95 fprintf(fptr, "Channels/chip: %u\n", RHeader->NChannels);
96 fprintf(fptr, "Samples: %u\n", RHeader->Samples);
97 fprintf(fptr, "Offset: %u\n", RHeader->Offset);
98
99 fprintf(fptr, "Start second: %u - UTC %s", RHeader->StartSecond, asctime(gmtime((time_t *) &RHeader->StartSecond)));
100 fprintf(fptr, " microsecond: %u\n", RHeader->StartMicrosecond);
101 fprintf(fptr, "End second: %u - UTC %s", RHeader->EndSecond, asctime(gmtime((time_t *) &RHeader->EndSecond)));
102 fprintf(fptr, " microsecond: %u\n", RHeader->EndMicrosecond);
103
104 for (unsigned int i=0; i<RHeader->NCMCBoards; i++) {
105 fprintf(fptr, "*** Board %d ***\n", i);
106 fprintf(fptr, "Serial number: %d\n", BStruct[i].SerialNo);
107 fprintf(fptr, "Sampling frequency [GHz]: %.3f\n", BStruct[i].NomFreq);
108 fprintf(fptr, "Temperature [C]: %.3f\n", BStruct[i].BoardTemp);
109 fprintf(fptr, "Scale factor: %.3f\n", BStruct[i].ScaleFactor);
110 }
111 }
112 return CTX_OK;
113}
114
115// *** Close raw data file
116CTX_ErrCode RawDataCTX::CloseDataFile() {
117
118 if(!FileOpen) return CTX_NOTOPEN;
119
120 if (fclose(Rawfile) == EOF) {
121 if(!Silent) perror("Could not close file");
122 return CTX_FCLOSE;
123 }
124
125 delete[] BStruct;
126 delete[] Data;
127
128 FileOpen = false;
129 return CTX_OK;
130}
131
132// *** Read next event from file
133CTX_ErrCode RawDataCTX::ReadEvent(unsigned int EventNo, FILE *fptr) {
134
135 if (!FileOpen) {
136 if(!Silent) printf("Error: No data file open.\n");
137 return CTX_NOTOPEN;
138 }
139
140/* // Move file pointer to desired event header (if zero read next event)
141 if(EventNo!=0 && fseek(Rawfile, RHeader->RunHeaderSize+RHeader->BoardStructureSize*RHeader->NCMCBoards+(EventNo-1)*(RHeader->EventHeaderSize+RHeader->NCMCBoards*RHeader->NChips*
142 RHeader->NChannels*RHeader->Samples*sizeof(short)), SEEK_SET)!=0) {
143 if(!Silent) printf("Error: Could not move to requested event\n");
144 return CTX_SEEK;
145 }
146 */
147
148 // Move file pointer to desired event header (if zero read next event)
149 if (EventNo != 0) {
150 bool SEEK_OK=true;
151 if (fseek(Rawfile, RHeader->RunHeaderSize+RHeader->BoardStructureSize*RHeader->NCMCBoards, SEEK_SET) != 0) SEEK_OK=false;
152 while(SEEK_OK) {
153 if (fread(EHeader, RHeader->EventHeaderSize, 1, Rawfile) != 0) SEEK_OK = false;
154 else {
155 if (EHeader->EventNumber==EventNo) break;
156 if (fseek(Rawfile, EHeader->EventSize, SEEK_CUR) != 0) SEEK_OK = false;
157 }
158 }
159 if(!SEEK_OK) {
160 if(!Silent) printf("Error: Could not move to requested event\n");
161 return CTX_SEEK;
162 }
163 }
164 // Read event header
165 else if (fread(EHeader, RHeader->EventHeaderSize, 1, Rawfile) != 1) {
166 if (feof(Rawfile)==0) {
167 if (!Silent) printf("Error: Could not read event header\n");
168 return CTX_EHEADER;
169 }
170 else return CTX_EOF;
171 }
172 if(fread(Data, 1, EHeader->EventSize, Rawfile) != EHeader->EventSize) {
173 if(!Silent) printf("Error: Could not read (all) event data\n");
174 return CTX_DATA;
175 }
176
177 // If requested, print event header to file
178 if(fptr != NULL) {
179 fprintf(fptr, "Event number: %u\n", EHeader->EventNumber);
180 fprintf(fptr, "Time [sec]: %u - UTC %s", EHeader->Seconds, asctime(gmtime((time_t *) &EHeader->Seconds)));
181 fprintf(fptr, "Time [usec]: %u\n", EHeader->Microseconds);
182 fprintf(fptr, "Trigger type: 0x%0X\n", EHeader->TriggerType);
183 fprintf(fptr, "Size [byte]: %u\n", EHeader->EventSize);
184 }
185
186 return CTX_OK;
187}
188
189// *** Check if file currently open
190bool RawDataCTX::IsFileOpen() {
191 return FileOpen;
192}
193
Note: See TracBrowser for help on using the repository browser.