source: fact/tools/pyscripts/pyfact/calfactfits.h@ 13566

Last change on this file since 13566 was 13566, checked in by neise, 13 years ago
debugged memory allocation bug. mem of trigger_offset was allocated too small. baseline, gain and trigger_offset were filled in the wrong way in the contructor. in GetCalEvent() baseline, gain and trigger_offset were indexed wrongly. performance test to be done.
File size: 8.1 KB
Line 
1//********************************
2//
3// Class CalFactFits
4// Wrapper class for fits.h or pyfits.h
5// Provides fast access to calibrated events of FACT raw files
6//
7// written by Thomas Kraehenbuehl, ETH Zurich
8// tpk@phys.ethz.ch, +41 44 633 3973
9// April 2012
10//
11//********************************
12//
13// Compilation (root in pyfact directory)
14// root [0] gSystem->Load("/usr/lib64/libz.so");
15// root [1] .L izstream.h++
16// root [2] .L factfits.h++
17// root [3] .L calfactfits.h++
18//
19// Usage in Python:
20// See pyscripts/examples/CalFitsTest.py
21//
22//********************************
23
24//ToDo: shared library creation debuggen
25
26#ifndef CALFACTFITS_H
27#define CALFACTFITS_H
28
29#include <cstdio>
30#include <string>
31
32#ifndef __MARS__
33#include <vector>
34#include <iomanip>
35#include <iostream>
36#define gLog cerr
37#define ___err___ ""
38#define ___all___ ""
39#else
40#include "MLog.h"
41#include "MLogManip.h"
42#define ___err___ err
43#define ___all___ all
44#endif
45
46#ifdef __EXCEPTIONS
47#include <stdexcept>
48#endif
49
50#include "factfits.h"
51
52class CalFactFits
53{
54public:
55 //No standard constructor CalFactFits()!
56
57 //Direct handlers of the files
58 FactFits datafile, calibfile; //Class name should be PyFits or better FactPyFits...
59
60 //Basic file parameters
61 UInt_t calib_nroi, calib_npix;
62 UInt_t calib_blm_size, calib_gm_size, calib_tom_size;
63 UInt_t data_nroi, data_npix, data_ndata;
64
65 //Common variables
66 UInt_t nroi, npix, nevents;
67
68 //Calibration variables
69 float* calib_baselinemean;
70 float* calib_gainmean;
71 float* calib_triggeroffsetmean;
72 // The same variables as above, but this time
73 // they are doubled in memory, so the modulo operation is not necessary.
74 // idea by TPK again
75 // since we like to work with doubles for some reason, I don't know yet
76 // I cast the float to double already in this stage.
77
78 double* baseline;
79 double* gain;
80 double* trigger_offset;
81 //Using <vector> instead of arrays makes no visible difference
82 //ToDo: use arrays of size 1440x1024 (x2 for wrap-arounds) and read all variables into those
83
84 //Event variables
85 UInt_t event_id;
86 UShort_t event_triggertype;
87 int16_t* event_data;
88 int16_t* event_offset;
89 int32_t* event_boardtimes;
90 double* npcaldata;
91
92 CalFactFits(const string &datafilename, const string &calibfilename) //Constructor with two filenames
93 : datafile(datafilename),
94 calibfile(calibfilename),
95 npcaldata(NULL)
96 {
97 //cout << "Constructor called" << endl;
98 //Read basic parameters of the two files
99// std::cout << "...Reading basic file parameters..." << std::endl;
100 calib_nroi = calibfile.GetUInt("NROI");
101 calib_npix = calibfile.GetUInt("NPIX");
102 data_nroi = datafile.GetUInt("NROI");
103 data_npix = datafile.GetUInt("NPIX");
104 data_ndata = datafile.GetN("Data");
105
106 calib_blm_size = calibfile.GetN("BaselineMean")/calib_npix;
107 calib_gm_size = calibfile.GetN("GainMean")/calib_npix;
108 calib_tom_size = calibfile.GetN("TriggerOffsetMean")/calib_npix;
109
110// std::cout << "Column sizes: " << calib_blm_size << " " << calib_gm_size << " " << calib_tom_size << std::endl;
111
112 //Define the common variables
113 if((calib_nroi==data_nroi)&&(calib_npix==data_npix)&&(data_nroi*data_npix==data_ndata)&&(calib_blm_size==calib_gm_size)&&(calib_tom_size==calib_nroi)) {
114 nroi = data_nroi;
115 npix = data_npix;
116 }
117 else {
118 ostringstream str;
119 str << "Data/calib file error: NROI mismatch, NPIX mismatch, data column size wrong or calib columns mismatch.";
120#ifdef __EXCEPTIONS
121 throw runtime_error(str.str());
122#else
123 gLog << ___err___ << "ERROR - " << str.str() << endl;
124 return;
125#endif
126 }
127 nevents = datafile.GetNumRows();
128
129 //Read the calibration data
130// std::cout << "...Reading calibration data..." << std::endl;
131 calib_baselinemean = new float[calibfile.GetN("BaselineMean")];
132 calibfile.SetPtrAddress("BaselineMean", calib_baselinemean, calibfile.GetN("BaselineMean"));
133 baseline = new double[calibfile.GetN("BaselineMean")*2];
134
135 calib_gainmean = new float[calibfile.GetN("GainMean")];
136 calibfile.SetPtrAddress("GainMean", calib_gainmean, calibfile.GetN("GainMean"));
137 gain = new double[calibfile.GetN("GainMean")*2];
138
139 calib_triggeroffsetmean = new float[calibfile.GetN("TriggerOffsetMean")];
140 calibfile.SetPtrAddress("TriggerOffsetMean", calib_triggeroffsetmean, calibfile.GetN("TriggerOffsetMean"));
141 trigger_offset = new double[calibfile.GetN("TriggerOffsetMean")*2];
142
143 calibfile.GetRow(0);
144
145int orig_index, new_index1, new_index2;
146 for (int pix=0 ; pix < (int)calib_npix; ++pix)
147 {
148 for (int sl = 0; sl < (int)calib_blm_size; ++sl)
149 {
150 orig_index = pix * (int)calib_blm_size + sl;
151 new_index1 = 2*pix * (int)calib_blm_size + sl;
152 new_index2 = (2*pix+1) * (int)calib_blm_size + sl;
153
154 baseline[new_index2] = baseline[new_index1] = double(calib_baselinemean[orig_index]) *4096./2000.;
155 }
156 }
157 for (int pix=0 ; pix < (int)calib_npix; ++pix)
158 {
159 for (int sl = 0; sl < (int)calib_gm_size; ++sl)
160 {
161 orig_index = pix * (int)calib_gm_size + sl;
162 new_index1 = 2*pix * (int)calib_gm_size + sl;
163 new_index2 = (2*pix+1) * (int)calib_gm_size + sl;
164
165 gain[new_index2] = gain[new_index1] = double(calib_gainmean[orig_index]) /( 1907.35 /4096. *2000.);
166 }
167 }
168
169 for (int pix=0 ; pix < (int)calib_npix; ++pix)
170 {
171 for (int sl = 0; sl < (int)calib_tom_size; ++sl)
172 {
173 orig_index = pix * (int)calib_tom_size + sl;
174 new_index1 = 2*pix * (int)calib_tom_size + sl;
175 new_index2 = (2*pix+1) * (int)calib_tom_size + sl;
176
177 trigger_offset[new_index2] = trigger_offset[new_index1] = double(calib_triggeroffsetmean[orig_index]) *4096. /2000.;
178 }
179 }
180
181
182
183 //Set the event pointers
184// std::cout << "...Setting event pointers..." << std::endl;
185 datafile.SetRefAddress("EventNum", event_id);
186 datafile.SetRefAddress("TriggerType", event_triggertype);
187
188 event_data = new int16_t[data_ndata];
189 datafile.SetPtrAddress("Data", event_data, data_ndata);
190
191 event_offset = new int16_t[datafile.GetN("StartCellData")];
192 datafile.SetPtrAddress("StartCellData", event_offset, datafile.GetN("StartCellData"));
193
194 event_boardtimes = new int32_t[datafile.GetN("BoardTime")];
195 datafile.SetPtrAddress("BoardTime", event_boardtimes, datafile.GetN("BoardTime"));
196 }
197
198 ~CalFactFits() //Standard destructor
199 {
200 delete[] calib_baselinemean;
201 delete[] calib_gainmean;
202 delete[] calib_triggeroffsetmean;
203 delete[] event_data;
204 delete[] event_offset;
205 delete[] event_boardtimes;
206 delete[] baseline;
207 delete[] gain;
208 delete[] trigger_offset;
209 }
210
211 bool GetCalEvent() //Read calibrated event into the event variables
212 {
213 if(!npcaldata) {
214 ostringstream str;
215 str << "Pointer to the calibrated data not initialized!";
216#ifdef __EXCEPTIONS
217 throw runtime_error(str.str());
218#else
219 gLog << ___err___ << "ERROR - " << str.str() << endl;
220 return false;
221#endif
222 }
223 if(datafile.GetNextRow() == false) {
224// std::cout << "Last event reached..." << std::endl;
225 return false;
226 }
227 else {
228 UInt_t drs_calib_offset;
229 double raw, raw_bsl, shifted;
230 for(UInt_t pixel = 0; pixel < data_npix; pixel++)
231 {
232 for(UInt_t slice = 0; slice < data_nroi; slice++)
233 {
234 drs_calib_offset = slice + event_offset[pixel];
235 raw = (double)event_data[pixel*data_nroi+slice];
236 raw_bsl = raw - baseline[pixel*calib_blm_size*2+drs_calib_offset];
237 shifted = raw_bsl - trigger_offset[pixel*data_nroi*2+slice];
238 npcaldata[pixel*data_nroi+slice] = shifted / gain[pixel*calib_blm_size*2+drs_calib_offset];
239 //Note: data_nroi=calib_nroi, calib_blm_size=calib_gm_size
240 }
241 }
242 }
243 return true;
244 }
245
246 void SetNpcaldataPtr(double *numpyptr) //Set the pointer for the calibrated data to the numpy array
247 {
248 npcaldata = numpyptr;
249 return;
250 }
251};
252#endif /* CALFACTFITS_H */
Note: See TracBrowser for help on using the repository browser.