source: drsdaq/RawDataCTX.cc@ 24

Last change on this file since 24 was 22, checked in by ogrimm, 16 years ago
First commit of drsdaq program
File size: 6.4 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 // Read board structures
45 BStruct = new BoardStructure [RHeader->NCMCBoards];
46 if(fread(BStruct, sizeof(BoardStructure), RHeader->NCMCBoards, Rawfile) != RHeader->NCMCBoards) {
47 if(!Silent) printf("Error: Could not read (all) board structures\n");
48 fclose(Rawfile);
49 delete[] BStruct;
50 return CTX_BSTRUCT;
51 }
52 // Allocate memory for event data
53 Data = new short[RHeader->NCMCBoards*RHeader->NChips*RHeader->NChannels*RHeader->Samples];
54 FileOpen = true;
55
56 // If requested, print run header (including board structures) to file
57 if(fptr != NULL) {
58 int RAhour, RAmin, RAsec, DEChour, DECmin, DECsec;
59
60 fprintf(fptr, "Magic number %x\n", RHeader->MagicNum);
61 fprintf(fptr, "Data format: %d\n", RHeader->DataFormat);
62 fprintf(fptr, "DAQ compilation: %s\n", RHeader->DAQVersion);
63 fprintf(fptr, "Source: %s\n", RHeader->Source);
64 fprintf(fptr, "Run type: %c\n", RHeader->Type);
65 fprintf(fptr, "Run number: %u\n", RHeader->RunNumber);
66 fprintf(fptr, "Events: %u\n", RHeader->Events);
67 fprintf(fptr, "CMC Boards: %u\n", RHeader->NCMCBoards);
68 fprintf(fptr, "DRS chips: %u\n", RHeader->NChips);
69 fprintf(fptr, "Channels/chip: %u\n", RHeader->NChannels);
70 fprintf(fptr, "Samples: %u\n", RHeader->Samples);
71 fprintf(fptr, "Offset: %d\n", RHeader->Offset);
72
73 RAsec = (int) (RHeader->SourceRA)%60;
74 RAmin = (int) (RHeader->SourceRA/60)%60;
75 RAhour = (int) (RHeader->SourceRA/3600);
76 DECsec = (int) (RHeader->SourceDEC)%60;
77 DECmin = (int) (RHeader->SourceDEC/60)%60;
78 DEChour = (int) (RHeader->SourceDEC/3600);
79
80 fprintf(fptr, "Source RA: %f (%d %d %d)\n", RHeader->SourceRA, RAhour, RAmin, RAsec);
81 fprintf(fptr, "Source DEC: %f (%d %d %d)\n", RHeader->SourceDEC, DEChour, DECmin, DECsec);
82
83 RAsec = (int) (RHeader->TelescopeRA)%60;
84 RAmin = (int) (RHeader->TelescopeRA/60)%60;
85 RAhour = (int) (RHeader->TelescopeRA/3600);
86 DECsec = (int) (RHeader->TelescopeDEC)%60;
87 DECmin = (int) (RHeader->TelescopeDEC/60)%60;
88 DEChour = (int) (RHeader->TelescopeDEC/3600);
89
90 fprintf(fptr, "Telescope RA: %f (%d %d %d)\n", RHeader->TelescopeRA, RAhour, RAmin, RAsec);
91 fprintf(fptr, "Telescope DEC: %f (%d %d %d)\n", RHeader->TelescopeDEC, DEChour, DECmin, DECsec);
92
93 fprintf(fptr, "Start year: %u\n", RHeader->StartYear);
94 fprintf(fptr, "Start month: %u\n", RHeader->StartMonth);
95 fprintf(fptr, "Start day: %u\n", RHeader->StartDay);
96 fprintf(fptr, "Start hour: %u\n", RHeader->StartHour);
97 fprintf(fptr, "Start minute: %u\n", RHeader->StartMinute);
98 fprintf(fptr, "Start second: %u\n", RHeader->StartSecond);
99 fprintf(fptr, "End year: %u\n", RHeader->EndYear);
100 fprintf(fptr, "End month: %u\n", RHeader->EndMonth);
101 fprintf(fptr, "End day: %u\n", RHeader->EndDay);
102 fprintf(fptr, "End hour: %u\n", RHeader->EndHour);
103 fprintf(fptr, "End minute: %u\n", RHeader->EndMinute);
104 fprintf(fptr, "End second: %u\n", RHeader->EndSecond);
105
106 for (unsigned int i=0; i<RHeader->NCMCBoards; i++) {
107 fprintf(fptr, "*** Board %d ***\n", i);
108 fprintf(fptr, "Serial number: %d\n", BStruct[i].SerialNo);
109 fprintf(fptr, "Sampling frequency [GHz]: %.3f\n", BStruct[i].NomFreq);
110 fprintf(fptr, "Temperature [C]: %.3f\n", BStruct[i].BoardTemp);
111 fprintf(fptr, "Scale factor: %.3f\n", BStruct[i].ScaleFactor);
112 }
113 }
114 return CTX_OK;
115}
116
117// *** Close raw data file
118CTX_ErrCode RawDataCTX::CloseDataFile() {
119
120 if(!FileOpen) return CTX_NOTOPEN;
121
122 if (fclose(Rawfile) == EOF) {
123 if(!Silent) perror("Could not close file");
124 return CTX_FCLOSE;
125 }
126
127 delete[] BStruct;
128 delete[] Data;
129
130 FileOpen = false;
131 return CTX_OK;
132}
133
134// *** Read next event from file
135CTX_ErrCode RawDataCTX::ReadEvent(int EventNo, FILE *fptr) {
136
137 if(!FileOpen) {
138 if(!Silent) printf("Error: No data file open.\n");
139 return CTX_NOTOPEN;
140 }
141
142 // Move file pointer to desired event header (if zero read next event)
143 if(EventNo!=0 && fseek(Rawfile, sizeof(RunHeader)+sizeof(BoardStructure)*RHeader->NCMCBoards+(EventNo-1)*(sizeof(EventHeader)+RHeader->NCMCBoards*RHeader->NChips*
144 RHeader->NChannels*RHeader->Samples*sizeof(short)), SEEK_SET)!=0) {
145 if(!Silent) printf("Error: Could not move to requested event\n");
146 return CTX_SEEK;
147 }
148
149 // Read event header
150 if (fread(EHeader, sizeof(EventHeader), 1, Rawfile) != 1) {
151 if(feof(Rawfile)==0) {
152 if(!Silent) printf("Error: Could not read event header\n");
153 return CTX_EHEADER;
154 }
155 else return CTX_EOF;
156 }
157 if(fread(Data, sizeof(short), RHeader->NCMCBoards*RHeader->NChips*RHeader->NChannels*RHeader->Samples, Rawfile)
158 != RHeader->NCMCBoards*RHeader->NChips*RHeader->NChannels*RHeader->Samples) {
159 if(!Silent) printf("Error: Could not read (all) data\n");
160 return CTX_DATA;
161 }
162
163 // If requested, print event header to file
164 if(fptr != NULL) {
165 fprintf(fptr, "Name: %s\n", EHeader->Name);
166 fprintf(fptr, "Event number: %u\n", EHeader->EventNumber);
167 fprintf(fptr, "Time [s]: %f\n", EHeader->TimeSec);
168 fprintf(fptr, "Trigger type: 0X%0X\n", EHeader->TriggerType);
169 }
170
171 return CTX_OK;
172}
173
174// *** Check if file currently open
175bool RawDataCTX::IsFileOpen() {
176 return FileOpen;
177}
178
Note: See TracBrowser for help on using the repository browser.