source: trunk/Mars/mcore/factfits.h@ 20084

Last change on this file since 20084 was 18835, checked in by tbretz, 8 years ago
Allow to reset the calibration as obtained from the file - this is as using zfits on a factfits file during runtime.
File size: 6.1 KB
Line 
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
13class factfits : public zfits
14{
15public:
16 // Default constructor
17 factfits(const std::string& fname, const std::string& tableName="", bool force=false) :
18 zfits(fname, tableName, force),
19 fOffsetCalibration(0),
20 fOffsetStartCellData(0),
21 fOffsetData(0),
22 fNumRoi(0)
23 {
24 if (init())
25 readDrsCalib(fname);
26 }
27
28 // Alternative constructor
29 factfits(const std::string& fname, const std::string& fout, const std::string& tableName, bool force=false) :
30 zfits(fname, fout, tableName, force),
31 fOffsetCalibration(0),
32 fOffsetStartCellData(0),
33 fOffsetData(0),
34 fNumRoi(0)
35 {
36 if (init())
37 readDrsCalib(fname);
38 }
39
40 const std::vector<int16_t> &GetOffsetCalibration() const { return fOffsetCalibration; }
41
42 void resetCalibration() { fOffsetCalibration.clear(); }
43
44private:
45
46 void StageRow(size_t row, char* dest)
47 {
48 zfits::StageRow(row, dest);
49
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_stride = 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_stride)
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 std::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()|std::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()|std::ios::badbit);
152#ifdef __EXCEPTIONS
153 throw std::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" << std::endl;
156 return;
157#endif
158 }
159 if (calib.GetStr("TTYPE1") != "OffsetCalibration")
160 {
161 clear(rdstate()|std::ios::badbit);
162#ifdef __EXCEPTIONS
163 throw std::runtime_error("Table 'ZDrsCellOffsets' found, but first column is not the one expected");
164#else
165 gLog << ___err___ << "ERROR - Table 'ZDrsCellOffsets' found, but first column is not the one expected" << std::endl;
166 return;
167#endif
168 }
169 bool isColumnPresent = false;
170 if (calib.HasKey("TFORM1") && calib.GetStr("TFORM1") == "1474560I") isColumnPresent = true;
171 if (calib.HasKey("ZFORM1") && calib.GetStr("ZFORM1") == "1474560I") isColumnPresent = true;
172 if (!isColumnPresent) // 1024*1440
173 {
174 clear(rdstate()|std::ios::badbit);
175#ifdef __EXCEPTIONS
176 throw std::runtime_error("Table 'ZDrsCellOffsets' has wrong column format (TFROM1)");
177#else
178 gLog << ___err___ << "ERROR - Table 'ZDrsCellOffsets' has wrong column format (TFORM1)" << std::endl;
179 return;
180#endif
181 }
182
183 fOffsetCalibration.resize(1024*1440);
184
185 calib.SetPtrAddress("OffsetCalibration", fOffsetCalibration.data());
186 if (calib.GetNextRow())
187 return;
188
189 clear(rdstate()|std::ios::badbit);
190
191#ifdef __EXCEPTIONS
192 throw std::runtime_error("Reading column 'OffsetCalibration' failed.");
193#else
194 gLog << ___err___ << "ERROR - Reading column 'OffsetCalibration' failed." << std::endl;
195#endif
196
197 }
198
199 std::vector<int16_t> fOffsetCalibration; ///< integer values of the drs calibration used for compression
200
201 size_t fOffsetStartCellData;
202 size_t fOffsetData;
203
204 uint16_t fNumRoi;
205
206#warning Time marker channels currently unhandled
207
208}; //class factfits
209
210#endif
Note: See TracBrowser for help on using the repository browser.