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 | for (uint32_t i=0; i<1440*1024; i+=1024, startCell++)
|
---|
61 | {
|
---|
62 | if ((*startCell) > -1)
|
---|
63 | for (uint32_t j=0; j<fNumRoi; j++, data++)
|
---|
64 | *data += fOffsetCalibration[i + (*startCell+j)%1024];
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | bool init()
|
---|
69 | {
|
---|
70 | if (!HasKey("NPIX") || !HasKey("NROI"))
|
---|
71 | return false;
|
---|
72 |
|
---|
73 | if (Get<uint16_t>("NPIX")!=1440)
|
---|
74 | return false;
|
---|
75 |
|
---|
76 | fNumRoi = Get<uint16_t>("NROI");
|
---|
77 | if (fNumRoi>1024)
|
---|
78 | return false;
|
---|
79 |
|
---|
80 | // check column details for Data
|
---|
81 | const Table::Columns::const_iterator it = fTable.cols.find("Data");
|
---|
82 | if (it==fTable.cols.end() || it->second.num!=1440*fNumRoi || it->second.type!='I')
|
---|
83 | return false;
|
---|
84 |
|
---|
85 | // check column details for StartCellData
|
---|
86 | const Table::Columns::const_iterator is = fTable.cols.find("StartCellData");
|
---|
87 | if (is==fTable.cols.end() || is->second.num!=1440 || is->second.type!='I')
|
---|
88 | return false;
|
---|
89 |
|
---|
90 | fOffsetStartCellData = is->second.offset;
|
---|
91 | fOffsetData = it->second.offset;
|
---|
92 |
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 |
|
---|
96 | // Read the Drs calibration data
|
---|
97 | void readDrsCalib(const string& fileName)
|
---|
98 | {
|
---|
99 | //should not be mandatory, but improves the perfs a lot when reading not compressed, gzipped files
|
---|
100 | if (!IsCompressedFITS())
|
---|
101 | return;
|
---|
102 |
|
---|
103 | zfits calib(fileName, "IntCalibration");
|
---|
104 |
|
---|
105 | if (calib.bad())
|
---|
106 | {
|
---|
107 | clear(rdstate()|ios::badbit);
|
---|
108 | return;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (calib.eof())
|
---|
112 | return;
|
---|
113 |
|
---|
114 | // Check correct size and format of table
|
---|
115 | if (calib.GetNumRows() != 1)
|
---|
116 | {
|
---|
117 | clear(rdstate()|ios::badbit);
|
---|
118 | #ifdef __EXCEPTIONS
|
---|
119 | throw runtime_error("Table 'IntCalibration' found, but not with one row as expected");
|
---|
120 | #else
|
---|
121 | gLog << ___err___ << "ERROR - Table 'IntCalibration' found, but not with one row as expected" << endl;
|
---|
122 | return;
|
---|
123 | #endif
|
---|
124 | }
|
---|
125 | if (calib.GetStr("TTYPE1") != "OffsetCalibration")
|
---|
126 | {
|
---|
127 | clear(rdstate()|ios::badbit);
|
---|
128 | #ifdef __EXCEPTIONS
|
---|
129 | throw runtime_error("Table 'DrsCalib' found, but first column is not the one expected");
|
---|
130 | #else
|
---|
131 | gLog << ___err___ << "ERROR - Table 'DrsCalib' found, but first column is not the one expected" << endl;
|
---|
132 | return;
|
---|
133 | #endif
|
---|
134 | }
|
---|
135 | if (calib.GetStr("TFORM1") != "1474560I") // 1024*1440
|
---|
136 | {
|
---|
137 | clear(rdstate()|ios::badbit);
|
---|
138 | #ifdef __EXCEPTIONS
|
---|
139 | throw runtime_error("Table 'DrsCalib' has wrong column format (TFROM1)");
|
---|
140 | #else
|
---|
141 | gLog << ___err___ << "ERROR - Table 'DrsCalib' has wrong column format (TFORM1)" << endl;
|
---|
142 | return;
|
---|
143 | #endif
|
---|
144 | }
|
---|
145 |
|
---|
146 | fOffsetCalibration.resize(1024*1440);
|
---|
147 |
|
---|
148 | calib.SetPtrAddress("OffsetCalibration", fOffsetCalibration.data());
|
---|
149 | if (calib.GetNextRow())
|
---|
150 | return;
|
---|
151 |
|
---|
152 | clear(rdstate()|ios::badbit);
|
---|
153 |
|
---|
154 | #ifdef __EXCEPTIONS
|
---|
155 | throw runtime_error("Reading column 'OffsetCalibration' failed.");
|
---|
156 | #else
|
---|
157 | gLog << ___err___ << "ERROR - Reading column 'OffsetCalibration' failed." << endl;
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | }
|
---|
161 |
|
---|
162 | vector<int16_t> fOffsetCalibration; ///< integer values of the drs calibration used for compression
|
---|
163 |
|
---|
164 | size_t fOffsetStartCellData;
|
---|
165 | size_t fOffsetData;
|
---|
166 |
|
---|
167 | uint16_t fNumRoi;
|
---|
168 |
|
---|
169 | #warning Time marker channels currently unhandled
|
---|
170 |
|
---|
171 | }; //class factfits
|
---|
172 |
|
---|
173 | #ifndef __MARS__
|
---|
174 | }; //namespace std
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | #endif
|
---|