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

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