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

Last change on this file since 16863 was 16560, checked in by tbretz, 12 years ago
Performance improved and heavily tested version of the calibration algorithm.
File size: 5.6 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
13#ifndef __MARS__
14namespace std
15{
16#endif
17
18class factfits : public zfits
19{
20public:
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
45private:
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 continue;
65
66 for (uint32_t j=0; j<fNumRoi; j++, data++)
67 *data += fOffsetCalibration[i + (*startCell+j)%1024];
68 }
69 */
70
71 // This version is faster because the compilers optimization
72 // is not biased by the evaluation of %1024
73 for (int ch=0; ch<1440; ch++)
74 {
75 if (startCell[ch]<0)
76 {
77 data += fNumRoi;
78 continue;
79 }
80
81 const int16_t *off = fOffsetCalibration.data() + ch*1024;
82
83 const int16_t *cal = off+startCell[ch];
84 const int16_t *end = data+fNumRoi;
85
86 if (startCell[ch]+fNumRoi>1024)
87 {
88 while (cal<off+1024)
89 *data++ += *cal++;
90
91 cal = off;
92 }
93 while (data<end)
94 *data++ += *cal++;
95 }
96 }
97
98 bool init()
99 {
100 if (!HasKey("NPIX") || !HasKey("NROI"))
101 return false;
102
103 if (Get<uint16_t>("NPIX")!=1440)
104 return false;
105
106 fNumRoi = Get<uint16_t>("NROI");
107 if (fNumRoi>1024)
108 return false;
109
110 // check column details for Data
111 const Table::Columns::const_iterator it = fTable.cols.find("Data");
112 if (it==fTable.cols.end() || it->second.num!=1440*fNumRoi || it->second.type!='I')
113 return false;
114
115 // check column details for StartCellData
116 const Table::Columns::const_iterator is = fTable.cols.find("StartCellData");
117 if (is==fTable.cols.end() || is->second.num!=1440 || is->second.type!='I')
118 return false;
119
120 fOffsetStartCellData = is->second.offset;
121 fOffsetData = it->second.offset;
122
123 return true;
124 }
125
126 // Read the Drs calibration data
127 void readDrsCalib(const string& fileName)
128 {
129 //should not be mandatory, but improves the perfs a lot when reading not compressed, gzipped files
130 if (!IsCompressedFITS())
131 return;
132
133 zfits calib(fileName, "ZDrsCellOffsets");
134
135 if (calib.bad())
136 {
137 clear(rdstate()|ios::badbit);
138 return;
139 }
140
141 if (calib.eof())
142 return;
143
144 // Check correct size and format of table
145 if (calib.GetNumRows() != 1)
146 {
147 clear(rdstate()|ios::badbit);
148#ifdef __EXCEPTIONS
149 throw runtime_error("Table 'ZDrsCellOffsets' found, but not with one row as expected");
150#else
151 gLog << ___err___ << "ERROR - Table 'ZDrsCellOffsets' found, but not with one row as expected" << endl;
152 return;
153#endif
154 }
155 if (calib.GetStr("TTYPE1") != "OffsetCalibration")
156 {
157 clear(rdstate()|ios::badbit);
158#ifdef __EXCEPTIONS
159 throw runtime_error("Table 'DrsCalib' found, but first column is not the one expected");
160#else
161 gLog << ___err___ << "ERROR - Table 'DrsCalib' found, but first column is not the one expected" << endl;
162 return;
163#endif
164 }
165 if (calib.GetStr("TFORM1") != "1474560I") // 1024*1440
166 {
167 clear(rdstate()|ios::badbit);
168#ifdef __EXCEPTIONS
169 throw runtime_error("Table 'DrsCalib' has wrong column format (TFROM1)");
170#else
171 gLog << ___err___ << "ERROR - Table 'DrsCalib' has wrong column format (TFORM1)" << endl;
172 return;
173#endif
174 }
175
176 fOffsetCalibration.resize(1024*1440);
177
178 calib.SetPtrAddress("OffsetCalibration", fOffsetCalibration.data());
179 if (calib.GetNextRow())
180 return;
181
182 clear(rdstate()|ios::badbit);
183
184#ifdef __EXCEPTIONS
185 throw runtime_error("Reading column 'OffsetCalibration' failed.");
186#else
187 gLog << ___err___ << "ERROR - Reading column 'OffsetCalibration' failed." << endl;
188#endif
189
190 }
191
192 vector<int16_t> fOffsetCalibration; ///< integer values of the drs calibration used for compression
193
194 size_t fOffsetStartCellData;
195 size_t fOffsetData;
196
197 uint16_t fNumRoi;
198
199#warning Time marker channels currently unhandled
200
201}; //class factfits
202
203#ifndef __MARS__
204}; //namespace std
205#endif
206
207#endif
Note: See TracBrowser for help on using the repository browser.