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

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