1 | /*
|
---|
2 | * factfits.h
|
---|
3 | *
|
---|
4 | * Created on: May 26, 2013
|
---|
5 | * Author: lyard
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef MARS_FACTFITS
|
---|
9 | #define MARS_FACTFITS
|
---|
10 |
|
---|
11 | #include "zfits.h"
|
---|
12 |
|
---|
13 | #ifndef __MARS__
|
---|
14 | namespace std
|
---|
15 | {
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | class factfits : public zfits
|
---|
19 | {
|
---|
20 | public:
|
---|
21 | // Default constructor
|
---|
22 | factfits(const string& fname, const string& tableName="", bool force=false) :
|
---|
23 | zfits(fname, tableName, force),
|
---|
24 | fOffsetCalibration(0),
|
---|
25 | fOffsetStartCellData(0),
|
---|
26 | fOffsetData(0),
|
---|
27 | fNumRoi(0)
|
---|
28 | {
|
---|
29 | if (init())
|
---|
30 | readDrsCalib(fname);
|
---|
31 | }
|
---|
32 |
|
---|
33 | // Alternative constructor
|
---|
34 | factfits(const string& fname, const string& fout, const string& tableName, bool force=false) :
|
---|
35 | zfits(fname, fout, tableName, force),
|
---|
36 | fOffsetCalibration(0),
|
---|
37 | fOffsetStartCellData(0),
|
---|
38 | fOffsetData(0),
|
---|
39 | fNumRoi(0)
|
---|
40 | {
|
---|
41 | if (init())
|
---|
42 | readDrsCalib(fname);
|
---|
43 | }
|
---|
44 |
|
---|
45 | private:
|
---|
46 |
|
---|
47 | void StageRow(size_t row, char* dest)
|
---|
48 | {
|
---|
49 | zfits::StageRow(row, dest);
|
---|
50 | // This file does not contain fact data or no calibration to be applied
|
---|
51 | if (fOffsetCalibration.empty())
|
---|
52 | return;
|
---|
53 |
|
---|
54 | //re-get the pointer to the data to access the offsets
|
---|
55 | const uint8_t offset = (row*fTable.bytes_per_row)%4;
|
---|
56 |
|
---|
57 | int16_t *startCell = reinterpret_cast<int16_t*>(fBufferRow.data() + offset + fOffsetStartCellData);
|
---|
58 | int16_t *data = reinterpret_cast<int16_t*>(fBufferRow.data() + offset + fOffsetData);
|
---|
59 |
|
---|
60 | /*
|
---|
61 | for (uint32_t i=0; i<1440*1024; i+=1024, startCell++)
|
---|
62 | {
|
---|
63 | if (*startCell < 0)
|
---|
64 | {
|
---|
65 | data += fNumRoi;
|
---|
66 | continue;
|
---|
67 | }
|
---|
68 | for (uint32_t j=0; j<fNumRoi; j++, data++)
|
---|
69 | *data += fOffsetCalibration[i + (*startCell+j)%1024];
|
---|
70 | }
|
---|
71 | */
|
---|
72 |
|
---|
73 | // This version is faster because the compilers optimization
|
---|
74 | // is not biased by the evaluation of %1024
|
---|
75 | for (int ch=0; ch<1440; ch++)
|
---|
76 | {
|
---|
77 | if (startCell[ch]<0)
|
---|
78 | {
|
---|
79 | data += fNumRoi;
|
---|
80 | continue;
|
---|
81 | }
|
---|
82 |
|
---|
83 | const int16_t modStart = startCell[ch] % 1024;
|
---|
84 | const int16_t *off = fOffsetCalibration.data() + ch*1024;
|
---|
85 |
|
---|
86 | const int16_t *cal = off+modStart;
|
---|
87 | const int16_t *end = data+fNumRoi;
|
---|
88 |
|
---|
89 | if (modStart+fNumRoi>1024)
|
---|
90 | {
|
---|
91 | while (cal<off+1024)
|
---|
92 | *data++ += *cal++;
|
---|
93 |
|
---|
94 | cal = off;
|
---|
95 | }
|
---|
96 | while (data<end)
|
---|
97 | *data++ += *cal++;
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|
101 |
|
---|
102 | bool init()
|
---|
103 | {
|
---|
104 | if (!HasKey("NPIX") || !HasKey("NROI"))
|
---|
105 | return false;
|
---|
106 |
|
---|
107 | if (Get<uint16_t>("NPIX")!=1440)
|
---|
108 | return false;
|
---|
109 |
|
---|
110 | fNumRoi = Get<uint16_t>("NROI");
|
---|
111 | if (fNumRoi>1024)
|
---|
112 | return false;
|
---|
113 |
|
---|
114 | // check column details for Data
|
---|
115 | const Table::Columns::const_iterator it = fTable.cols.find("Data");
|
---|
116 | if (it==fTable.cols.end() || it->second.num!=1440*fNumRoi || it->second.type!='I')
|
---|
117 | return false;
|
---|
118 |
|
---|
119 | // check column details for StartCellData
|
---|
120 | const Table::Columns::const_iterator is = fTable.cols.find("StartCellData");
|
---|
121 | if (is==fTable.cols.end() || is->second.num!=1440 || is->second.type!='I')
|
---|
122 | return false;
|
---|
123 |
|
---|
124 | fOffsetStartCellData = is->second.offset;
|
---|
125 | fOffsetData = it->second.offset;
|
---|
126 |
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 |
|
---|
130 | // Read the Drs calibration data
|
---|
131 | void readDrsCalib(const string& fileName)
|
---|
132 | {
|
---|
133 | //should not be mandatory, but improves the perfs a lot when reading not compressed, gzipped files
|
---|
134 | if (!IsCompressedFITS())
|
---|
135 | return;
|
---|
136 |
|
---|
137 | zfits calib(fileName, "ZDrsCellOffsets");
|
---|
138 |
|
---|
139 | if (calib.bad())
|
---|
140 | {
|
---|
141 | clear(rdstate()|ios::badbit);
|
---|
142 | return;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (calib.eof())
|
---|
146 | return;
|
---|
147 |
|
---|
148 | // Check correct size and format of table
|
---|
149 | if (calib.GetNumRows() != 1)
|
---|
150 | {
|
---|
151 | clear(rdstate()|ios::badbit);
|
---|
152 | #ifdef __EXCEPTIONS
|
---|
153 | throw runtime_error("Table 'ZDrsCellOffsets' found, but not with one row as expected");
|
---|
154 | #else
|
---|
155 | gLog << ___err___ << "ERROR - Table 'ZDrsCellOffsets' found, but not with one row as expected" << endl;
|
---|
156 | return;
|
---|
157 | #endif
|
---|
158 | }
|
---|
159 | if (calib.GetStr("TTYPE1") != "OffsetCalibration")
|
---|
160 | {
|
---|
161 | clear(rdstate()|ios::badbit);
|
---|
162 | #ifdef __EXCEPTIONS
|
---|
163 | throw runtime_error("Table 'DrsCalib' found, but first column is not the one expected");
|
---|
164 | #else
|
---|
165 | gLog << ___err___ << "ERROR - Table 'DrsCalib' found, but first column is not the one expected" << endl;
|
---|
166 | return;
|
---|
167 | #endif
|
---|
168 | }
|
---|
169 | if (calib.GetStr("TFORM1") != "1474560I") // 1024*1440
|
---|
170 | {
|
---|
171 | clear(rdstate()|ios::badbit);
|
---|
172 | #ifdef __EXCEPTIONS
|
---|
173 | throw runtime_error("Table 'DrsCalib' has wrong column format (TFROM1)");
|
---|
174 | #else
|
---|
175 | gLog << ___err___ << "ERROR - Table 'DrsCalib' has wrong column format (TFORM1)" << endl;
|
---|
176 | return;
|
---|
177 | #endif
|
---|
178 | }
|
---|
179 |
|
---|
180 | fOffsetCalibration.resize(1024*1440);
|
---|
181 |
|
---|
182 | calib.SetPtrAddress("OffsetCalibration", fOffsetCalibration.data());
|
---|
183 | if (calib.GetNextRow())
|
---|
184 | return;
|
---|
185 |
|
---|
186 | clear(rdstate()|ios::badbit);
|
---|
187 |
|
---|
188 | #ifdef __EXCEPTIONS
|
---|
189 | throw runtime_error("Reading column 'OffsetCalibration' failed.");
|
---|
190 | #else
|
---|
191 | gLog << ___err___ << "ERROR - Reading column 'OffsetCalibration' failed." << endl;
|
---|
192 | #endif
|
---|
193 |
|
---|
194 | }
|
---|
195 |
|
---|
196 | vector<int16_t> fOffsetCalibration; ///< integer values of the drs calibration used for compression
|
---|
197 |
|
---|
198 | size_t fOffsetStartCellData;
|
---|
199 | size_t fOffsetData;
|
---|
200 |
|
---|
201 | uint16_t fNumRoi;
|
---|
202 |
|
---|
203 | #warning Time marker channels currently unhandled
|
---|
204 |
|
---|
205 | }; //class factfits
|
---|
206 |
|
---|
207 | #ifndef __MARS__
|
---|
208 | }; //namespace std
|
---|
209 | #endif
|
---|
210 |
|
---|
211 | #endif
|
---|