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

Last change on this file since 16331 was 16285, checked in by lyard, 11 years ago
added new files for fitsCompressor and compressed files read headers
File size: 4.9 KB
Line 
1/*
2 * factfits.h
3 *
4 * Created on: May 26, 2013
5 * Author: lyard
6 */
7
8#ifndef FACTFITS_H_
9#define FACTFITS_H_
10
11#include "zfits.h"
12
13
14#ifndef __MARS__
15namespace std
16{
17#else
18using namespace std;
19#endif
20
21class factFits : public zfits
22{
23public:
24 /*
25 * Default constructor
26 */
27 factFits(const string& fname,
28 const string& tableName="",
29 bool force=false,
30 bool mightFail=false) : zfits(fname, tableName, force, mightFail),
31 fOffsetCalibration(0),
32 fStartCellOffset(0)
33 {
34 readDrsCalib(fname);
35 GetStartCellOffset();
36 }
37 /*
38 * Alternative constructor
39 */
40 factFits(const string& fname,
41 const string& fout,
42 const string& tableName="",
43 bool force=false,
44 bool mightFail=false): zfits(fname, fout, tableName, force, mightFail),
45 fOffsetCalibration(0),
46 fStartCellOffset(0)
47 {
48 readDrsCalib(fname);
49 GetStartCellOffset();
50 }
51 /*
52 * Default destrctor
53 */
54 ~factFits()
55 {}
56 /*
57 * Overload of zfits.h
58 */
59#if !defined(__MARS__) && !defined(__CINT__)
60 virtual bool GetRow(size_t row, bool check=true)
61#else
62 virtual bool GetRowNum(size_t row, bool check=true)
63#endif
64 {
65#if !defined(__MARS__) && !defined(__CINT__)
66 const bool isGood = zfits::GetRow(row, check);
67#else
68 const bool isGood = zfits::GetRowNum(row, check);
69#endif
70 //row is loaded in internal memory. Should we un-apply the integer drs ?
71 if (!isGood) return false;
72 if (!fTable.isCompressed) return true;
73 if (fOffsetCalibration.size() == 0) return true;
74
75 //now Drs un-Calibrate, if required
76 const Pointers::iterator dtaIt = fPointers.find("Data");
77 if (dtaIt == fPointers.end()) return true;
78
79 //re-get the pointer to the data to access the offsets
80 const uint8_t offset = (row*fTable.bytes_per_row)%4;
81 const char* ptr = fBufferRow.data() + offset;
82
83 //get column details for Data
84 const Table::Columns::const_iterator dataColIt = fTable.cols.find("Data");
85 if (dataColIt == fTable.cols.end()) return false;
86 const Table::Column& dataColumn = dataColIt->second;
87 const uint32_t numSlices = dataColumn.num/1440;
88
89 //Drs un-calibrate !
90 for (uint32_t i=0;i<1440;i++)
91 {
92 const int32_t thisStartCell = reinterpret_cast<const int16_t*>(ptr+fStartCellOffset)[i];
93 for (uint32_t j=0;j<numSlices;j++)
94 reinterpret_cast<int16_t*>(dtaIt->second)[numSlices*i+j] += fOffsetCalibration[1024*i + (thisStartCell+j)%1024];
95 }
96 return true;
97 }
98
99private:
100
101 /*
102 * Read the Drs calibration data
103 */
104 void readDrsCalib(const string& fileName)
105 {
106 zfits calib(fileName, "DrsCalib", false, true);
107
108 const bool isDrsCalibTable = (bool)calib;
109 if (!isDrsCalibTable) return;
110 // Check correct size and format of table
111 if (calib.GetNumRows() != 1)
112 {
113#ifdef __EXCEPTIONS
114 throw runtime_error("ERROR: DrsCalib table found, but not with one row as expected");
115#else
116 gLog << ___err___ << "ERROR: DrsCalib table found, but not with one row as expected" << endl;
117 return;
118#endif
119 }
120 if (calib.GetStr("TTYPE1") != "OffsetCalibration")
121 {
122#ifdef __EXCEPTIONS
123 throw runtime_error("ERROR: DrsCalib table found, but first column is not the one expected");
124#else
125 gLog << ___err___ << "ERROR: DrsCalib table found, but first column is not the one expected" << endl;
126 return;
127#endif
128 }
129 if (calib.GetStr("TFORM1") != "1474560I")
130 {
131#ifdef __EXCEPTIONS
132 throw runtime_error("ERROR: DrsCalib table found, but has wrong column format");
133#else
134 gLog << ___err___ << "ERROR: DrsCalib table found, but has wrong column format" << endl;
135 return;
136#endif
137 }
138
139 fOffsetCalibration.resize(1024*1440);
140 calib.SetPtrAddress("OffsetCalibration",fOffsetCalibration.data());
141
142 calib.GetNextRow();
143 }
144
145 /*
146 * Get the offset of the StartCell column
147 */
148 void GetStartCellOffset()
149 {
150 for (Table::Columns::const_iterator jt=fTable.cols.begin(); jt != fTable.cols.end(); jt++)
151 {
152 if (jt->first == "StartCellData")
153 {
154 fStartCellOffset = jt->second.offset;
155 break;
156 }
157 }
158 }
159
160 vector<int16_t> fOffsetCalibration; ///< integer values of the drs calibration used for compression
161 size_t fStartCellOffset; ///< offset of the StartCellData column in the uncompressed data
162
163}; //class factfits
164
165#ifndef __MARS__
166}; //namespace std
167#endif
168
169#endif /* FACTFITS_H_ */
Note: See TracBrowser for help on using the repository browser.