source: trunk/Mars/mcore/factofits.h@ 17242

Last change on this file since 17242 was 17242, checked in by tbretz, 11 years ago
Removed obsolete and potentially dangerous include
File size: 8.8 KB
Line 
1/*
2 * factofits.h
3 *
4 * Created on: Oct 16, 2013
5 * Author: lyard
6 */
7
8#ifndef FACTOFITS_H_
9#define FACTOFITS_H_
10
11#include "zofits.h"
12#include "DrsCalib.h"
13
14#ifndef __MARS__
15namespace std
16{
17#else
18using namespace std;
19#endif
20
21class factofits : public zofits
22{
23
24 public:
25
26 /// constructors
27 factofits(uint32_t numTiles=1000,
28 uint32_t rowPerTile=100,
29 uint64_t maxUsableMem=0) : zofits(numTiles, rowPerTile, maxUsableMem)
30 {
31 fStartCellsOffset = -1;
32 fDataOffset = -1;
33 }
34
35 factofits(const char* fname,
36 uint32_t numTiles=1000,
37 uint32_t rowPerTile=100,
38 uint64_t maxUsableMem=0) : zofits(fname, numTiles, rowPerTile, maxUsableMem)
39 {
40 fStartCellsOffset = -1;
41 fDataOffset = -1;
42 }
43
44 virtual ~factofits()
45 {
46 }
47
48 /// whether or not a calibration was given to the file writer
49 virtual bool IsOffsetCalibration()
50 {
51 return (fOffsetCalibration.size() != 0);
52 }
53
54 ///assign a given drs offset calibration
55 void SetDrsCalibration(const vector<float> &calib)
56 {
57 VerifyCalibrationSize(calib.size());
58
59 if (!IsOffsetCalibration())
60 fOffsetCalibration.resize(1440*1024);
61
62 for (uint32_t i=0; i<1440*1024; i++)
63 fOffsetCalibration[i] = (int16_t)(calib[i]*4096/2000);
64 }
65
66 ///assign a given drs offset calibration
67 void SetDrsCalibration(const vector<int16_t>& vec)
68 {
69 VerifyCalibrationSize(vec.size());
70
71 if (!IsOffsetCalibration())
72 fOffsetCalibration.resize(1440*1024);
73
74 for (uint32_t i=0; i<1440*1024; i++)
75 fOffsetCalibration[i] = vec[i];
76 }
77
78 ///assign a given drs offset calibration
79 void SetDrsCalibration(const DrsCalibration& drs)
80 {
81 if (drs.fNumOffset==0)
82 return;
83
84 VerifyCalibrationSize(drs.fOffset.size());
85
86 if (!IsOffsetCalibration())
87 fOffsetCalibration.resize(1440*1024);
88
89 for (uint32_t i=0; i<1024*1440; i++)
90 fOffsetCalibration[i] = drs.fOffset[i]/drs.fNumOffset;
91 }
92
93 ///Overload of the super function
94 bool WriteTableHeader(const char* name="DATA")
95 {
96 if (!zofits::WriteTableHeader(name))
97 return false;
98
99 if (IsOffsetCalibration())
100 {//retrieve the column storing the start cell offsets, if required.
101
102 for (auto it=fRealColumns.begin(); it!=fRealColumns.end(); it++)//Table.cols.begin(); it!= fTable.cols.end(); it++)
103 {
104 if (it->col.name == "StartCellData")
105 fStartCellsOffset = it->col.offset;
106
107 if (it->col.name == "Data")
108 {
109 fNumSlices = it->col.num;
110 fDataOffset = it->col.offset;
111 if (fNumSlices % 1440 != 0)
112 {
113#ifdef __EXCEPTIONS
114 throw runtime_error("Number of data samples not a multiple of 1440.");
115#else
116 gLog << ___err___ << "ERROR - Number of data samples not a multiple of 1440. Doing it uncalibrated." << endl;
117#endif
118 fOffsetCalibration.resize(0);
119 }
120 fNumSlices /= 1440;
121 }
122 }
123
124 if (fStartCellsOffset < 0)
125 {
126#ifdef __EXCEPTIONS
127 throw runtime_error("FACT Calibration requested, but \"StartCellData\" column not found.");
128#else
129 gLog << ___err___ << "ERROR - FACT Calibration requested, but \"StartCellData\" column not found. Doing it uncalibrated." << endl;
130#endif
131 //throw away the calibration data
132 fOffsetCalibration.resize(0);
133 }
134
135 if (fDataOffset < 0)
136 {
137#ifdef __EXCEPTIONS
138 throw runtime_error("FACT Calibration requested, but \"Data\" column not found.");
139#else
140 gLog << ___err___ << "ERROR - FACT Calibration requested, but \"Data\" column not found. Doing it uncalibrated." << endl;
141#endif
142 //throw away the calibration data
143 fOffsetCalibration.resize(0);
144 }
145 }
146
147 return true;
148 }
149
150 ///Actually write the drs calibration table
151 virtual bool WriteDrsOffsetsTable()
152 {
153 if (!IsOffsetCalibration())
154 return false;
155
156 ofits c;
157 c.SetStr("XTENSION", "BINTABLE" , "binary table extension");
158 c.SetInt("BITPIX" , 8 , "8-bit bytes");
159 c.SetInt("NAXIS" , 2 , "2-dimensional binary table");
160 c.SetInt("NAXIS1" , 1024*1440*2 , "width of table in bytes");
161 c.SetInt("NAXIS2" , 1 , "number of rows in table");
162 c.SetInt("PCOUNT" , 0 , "size of special data area");
163 c.SetInt("GCOUNT" , 1 , "one data group (required keyword)");
164 c.SetInt("TFIELDS" , 1 , "number of fields in each row");
165 c.SetStr("CHECKSUM", "0000000000000000" , "Checksum for the whole HDU");
166 c.SetStr("DATASUM" , " 0" , "Checksum for the data block");
167 c.SetStr("EXTNAME" , "ZDrsCellOffsets" , "name of this binary table extension");
168 c.SetStr("TTYPE1" , "OffsetCalibration" , "label for field 1");
169 c.SetStr("TFORM1" , "1474560I" , "data format of field: 2-byte INTEGER");
170 c.End();
171
172 vector<char> swappedOffsets;
173 swappedOffsets.resize(1024*1440*sizeof(int16_t));
174 revcpy<sizeof(int16_t)>(swappedOffsets.data(), (char*)(fOffsetCalibration.data()), 1024*1440);
175
176 Checksum datasum;
177 datasum.add(swappedOffsets.data(), sizeof(int16_t)*1024*1440);
178
179 ostringstream dataSumStr;
180 dataSumStr << datasum.val();
181 c.SetStr("DATASUM", dataSumStr.str());
182
183 datasum += c.WriteHeader(*this);
184
185 const off_t here_I_am = tellp();
186
187 c.SetStr("CHECKSUM", datasum.str());
188 c.WriteHeader(*this);
189
190 seekp(here_I_am);
191
192 write(swappedOffsets.data(), swappedOffsets.size());
193
194 AlignTo2880Bytes();
195
196 return good();
197 }
198
199 ///Apply the drs offset calibration (overload of super-method)
200 virtual void DrsOffsetCalibrate(char* target_location)
201 {
202 if (IsOffsetCalibration())
203 {
204 const int16_t* startCell = reinterpret_cast<int16_t*>(target_location + fStartCellsOffset);
205 int16_t* data = reinterpret_cast<int16_t*>(target_location + fDataOffset);
206
207 for (uint32_t ch=0; ch<1440; ch++)
208 {
209 if (startCell[ch] < 0)
210 {
211 data += fNumSlices;
212 continue;
213 }
214
215 const int16_t modStart = startCell[ch]%1024;
216 const int16_t *off = fOffsetCalibration.data() + ch*1024;
217
218 const int16_t* cal = off+modStart;
219 const int16_t* end_stride = data+fNumSlices;
220
221 if (modStart+fNumSlices > 1024)
222 {
223 while (cal < off+1024)
224 *data++ -= *cal++;
225 cal = off;
226 }
227
228 while (data<end_stride)
229 *data++ -= *cal++;
230 }
231 }
232 }
233
234private:
235 /// Checks if the size of the input calibration is ok
236 void VerifyCalibrationSize(uint32_t size)
237 {
238 if (size != 1440*1024)
239 {
240 ostringstream str;
241 str << "Cannot load calibration with anything else than 1440 pixels and 1024 samples per pixel. Got a total size of " << size;
242#ifdef __EXCEPTIONS
243 throw runtime_error(str.str());
244#else
245 gLog << ___err___ << "ERROR - " << str.str() << endl;
246#endif
247 }
248 }
249
250 //Offsets calibration stuff.
251 vector<int16_t> fOffsetCalibration; ///< The calibration itself
252 int32_t fStartCellsOffset; ///< Offset in bytes for the startcell data
253 int32_t fDataOffset; ///< Offset in bytes for the data
254 int32_t fNumSlices; ///< Number of samples per pixel per event
255
256}; //class factofits
257
258#ifndef __MARS
259}; //namespace std
260#endif
261
262#endif /* FACTOFITS_H_ */
Note: See TracBrowser for help on using the repository browser.