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__
|
---|
14 | namespace std
|
---|
15 | {
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | class factfits : public zfits
|
---|
19 | {
|
---|
20 | public:
|
---|
21 | // Default constructor
|
---|
22 | factfits(const string& fname, const string& tableName="", bool force=false) :
|
---|
23 | zfits(fname, tableName, force)
|
---|
24 | {
|
---|
25 | if (init())
|
---|
26 | readDrsCalib(fname);
|
---|
27 | }
|
---|
28 |
|
---|
29 | // Alternative constructor
|
---|
30 | factfits(const string& fname, const string& fout, const string& tableName, bool force=false) :
|
---|
31 | zfits(fname, fout, tableName, force)
|
---|
32 | {
|
---|
33 | if (init())
|
---|
34 | readDrsCalib(fname);
|
---|
35 | }
|
---|
36 |
|
---|
37 | //
|
---|
38 | #if !defined(__MARS__) && !defined(__CINT__)
|
---|
39 | bool GetRow(size_t row, bool check=true)
|
---|
40 | #else
|
---|
41 | bool GetRowNum(size_t row, bool check=true)
|
---|
42 | #endif
|
---|
43 | {
|
---|
44 | #if !defined(__MARS__) && !defined(__CINT__)
|
---|
45 | if (!zfits::GetRow(row, check))
|
---|
46 | return false;
|
---|
47 | #else
|
---|
48 | if (!zfits::GetRowNum(row, check))
|
---|
49 | return false;
|
---|
50 | #endif
|
---|
51 | // This file does not contain fact data or no calibration to be applied
|
---|
52 | if (fOffsetCalibration.empty())
|
---|
53 | return true;
|
---|
54 |
|
---|
55 | //re-get the pointer to the data to access the offsets
|
---|
56 | const uint8_t offset = (row*fTable.bytes_per_row)%4;
|
---|
57 |
|
---|
58 | int16_t *startCell = reinterpret_cast<int16_t*>(fBufferRow.data() + offset + fOffsetStartCellData);
|
---|
59 |
|
---|
60 | const Pointers::iterator dtaIt = fPointers.find("Data");
|
---|
61 | if (dtaIt == fPointers.end()) return true;
|
---|
62 | int16_t *data = reinterpret_cast<int16_t*>(dtaIt->second);
|
---|
63 |
|
---|
64 | for (uint32_t i=0; i<1440*1024; i+=1024, startCell++)
|
---|
65 | {
|
---|
66 | for (uint32_t j=0; j<fNumRoi; j++, data++)
|
---|
67 | *data += fOffsetCalibration[i + (*startCell+j)%1024];
|
---|
68 | }
|
---|
69 |
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 |
|
---|
73 | private:
|
---|
74 |
|
---|
75 | bool init()
|
---|
76 | {
|
---|
77 | if (!HasKey("NPIX") || !HasKey("NROI"))
|
---|
78 | return false;
|
---|
79 |
|
---|
80 | if (Get<uint16_t>("NPIX")!=1440)
|
---|
81 | return false;
|
---|
82 |
|
---|
83 | fNumRoi = Get<uint16_t>("NROI");
|
---|
84 | if (fNumRoi>1024)
|
---|
85 | return false;
|
---|
86 |
|
---|
87 | // check column details for Data
|
---|
88 | const Table::Columns::const_iterator it = fTable.cols.find("Data");
|
---|
89 | if (it==fTable.cols.end() || it->second.num!=1440*fNumRoi || it->second.type!='I')
|
---|
90 | return false;
|
---|
91 |
|
---|
92 | // check column details for StartCellData
|
---|
93 | const Table::Columns::const_iterator is = fTable.cols.find("StartCellData");
|
---|
94 | if (is==fTable.cols.end() || is->second.num!=1440 || is->second.type!='I')
|
---|
95 | return false;
|
---|
96 |
|
---|
97 | fOffsetStartCellData = is->second.offset;
|
---|
98 | fOffsetData = it->second.offset;
|
---|
99 |
|
---|
100 | return true;
|
---|
101 | }
|
---|
102 |
|
---|
103 | // Read the Drs calibration data
|
---|
104 | void readDrsCalib(const string& fileName)
|
---|
105 | {
|
---|
106 | zfits calib(fileName, "DrsCalib");
|
---|
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 'DrsCalib' found, but not with one row as expected");
|
---|
122 | #else
|
---|
123 | gLog << ___err___ << "ERROR - Table 'DrsCalib' 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 fOffsetData;
|
---|
167 | size_t fOffsetStartCellData;
|
---|
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
|
---|