source: fact/tools/pyscripts/pyfact/next_steps/calfactfits_new.h

Last change on this file was 17689, checked in by dneise, 11 years ago
some new deveolpment. not really working .. just some thoughts
File size: 9.6 KB
Line 
1//********************************
2//
3// Class CalFactFits
4// Wrapper class for factfits.h (or fits.h, 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// Try /usr/lib/x86_64-linux-gnu/libz.so at ETH for the libz.so file.
20//
21// Note: the produced izstream_h.so must be in LD_LIBRARY_PATH, e.g.
22// export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/guest/kraehenb/software/PythonIncludes/
23//
24// Usage in Python:
25// See pyscripts/examples/CalFitsTest.py
26//
27//********************************
28
29//ToDo: shared library creation debuggen
30
31#ifndef CALFACTFITS_H
32#define CALFACTFITS_H
33
34#include <cstdio>
35#include <string>
36
37#ifndef __MARS__
38#include <vector>
39#include <iomanip>
40#include <iostream>
41#define gLog cerr
42#define ___err___ ""
43#define ___all___ ""
44#else
45#include "MLog.h"
46#include "MLogManip.h"
47#define ___err___ err
48#define ___all___ all
49#endif
50
51#include <stdexcept>
52
53#include "extern_Mars_mcore/factfits.h"
54
55class CalFactFits
56{
57public:
58 //No standard constructor CalFactFits()!
59
60 //Direct handlers of the files
61 factfits datafile, calibfile;
62
63 //Basic file parameters
64 UInt_t calib_nroi, calib_npix;
65 UInt_t calib_blm_size, calib_gm_size, calib_tom_size;
66 UInt_t data_nroi, data_npix, data_ndata;
67
68 //Common variables
69 UInt_t nroi, npix, nevents;
70
71 // The same variables as above, but this time
72 // they are doubled in memory, so the modulo operation is not necessary.
73 // idea by TPK again
74 // since we like to work with doubles for some reason, I don't know yet
75 // I cast the float to double already in this stage.
76
77 double* baseline;
78 double* gain;
79 double* trigger_offset;
80 //Using <vector> instead of arrays makes no visible difference
81 //ToDo: use arrays of size 1440x1024 (x2 for wrap-arounds) and read all variables into those
82
83 //Event variables
84 UInt_t event_id;
85 UShort_t event_triggertype;
86 int16_t* event_data;
87 int16_t* event_offset;
88 int32_t* event_boardtimes;
89 double* npcaldata;
90
91 // Switches, to control if or if not to omit the actual calibration and return
92 // the uncalibrated data instead of the calibrated
93 bool do_calibration; // default: true
94 bool do_despiking; // default: true
95 double single_spike_limit; // default: 25
96 double double_spike_outer_limit; // default: 22
97 double double_spike_inner_limit; // default: 4
98
99 void SetDoCalibration( bool do_it){
100 do_calibration = do_it;
101 }
102 void SetDoDespiking( bool do_it){
103 do_despiking = do_it;
104 }
105 void SetSingleSpikeLimit( double limit ){
106 single_spike_limit = limit;
107 }
108 void SetDoubleSpikeOuterLimit( double limit ){
109 double_spike_outer_limit = limit;
110 }
111 void SetDoubleSpikeInnerLimit( double limit ){
112 double_spike_inner_limit = limit;
113 }
114
115
116
117 CalFactFits(const string &datafilename, const string &calibfilename) //Constructor with two filenames
118 : datafile(datafilename),
119 calibfile(calibfilename),
120 npcaldata(NULL)
121 {
122 calib_nroi = calibfile.GetUInt("NROI");
123 calib_npix = calibfile.GetUInt("NPIX");
124 data_nroi = datafile.GetUInt("NROI");
125 data_npix = datafile.GetUInt("NPIX");
126 data_ndata = datafile.GetN("Data");
127
128 calib_blm_size = calibfile.GetN("BaselineMean")/calib_npix;
129 calib_gm_size = calibfile.GetN("GainMean")/calib_npix;
130 calib_tom_size = calibfile.GetN("TriggerOffsetMean")/calib_npix;
131
132 do_calibration=true;
133 do_despiking=true;
134 single_spike_limit=25;
135 double_spike_outer_limit=22;
136 double_spike_inner_limit=4;
137
138 //Define the common variables
139 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)) {
140 nroi = data_nroi;
141 npix = data_npix;
142 }
143 else {
144 ostringstream str;
145 str << "Data/calib file error: NROI mismatch, NPIX mismatch, data column size wrong or calib columns mismatch.";
146 throw runtime_error(str.str());
147 }
148 nevents = datafile.GetNumRows();
149
150 //Read the calibration data
151 //Calibration variables
152 float* calib_baselinemean;
153 float* calib_gainmean;
154 float* calib_triggeroffsetmean;
155 calib_baselinemean = new float[calibfile.GetN("BaselineMean")];
156 calibfile.SetPtrAddress("BaselineMean", calib_baselinemean, calibfile.GetN("BaselineMean"));
157 baseline = new double[calibfile.GetN("BaselineMean")*2];
158
159 calib_gainmean = new float[calibfile.GetN("GainMean")];
160 calibfile.SetPtrAddress("GainMean", calib_gainmean, calibfile.GetN("GainMean"));
161 gain = new double[calibfile.GetN("GainMean")*2];
162
163 calib_triggeroffsetmean = new float[calibfile.GetN("TriggerOffsetMean")];
164 calibfile.SetPtrAddress("TriggerOffsetMean", calib_triggeroffsetmean, calibfile.GetN("TriggerOffsetMean"));
165 trigger_offset = new double[calibfile.GetN("TriggerOffsetMean")*2];
166
167 calibfile.GetRow(0);
168 copy_calib_constants_twice( calib_baselinemean, baseline, 4096./2000., calib_npix, calib_blm_size);
169 copy_calib_constants_twice( calib_gainmean, gain, 1./( 1907.35 /4096. *2000.), calib_npix, calib_gm_size);
170 copy_calib_constants_twice( calib_triggeroffsetmean, trigger_offset, 4096. /2000., calib_npix, calib_tom_size);
171 delete[] calib_baselinemean;
172 delete[] calib_gainmean;
173 delete[] calib_triggeroffsetmean;
174
175 datafile.SetRefAddress("EventNum", event_id);
176 datafile.SetRefAddress("TriggerType", event_triggertype);
177
178 event_data = new int16_t[data_ndata];
179 datafile.SetPtrAddress("Data", event_data, data_ndata);
180
181 event_offset = new int16_t[datafile.GetN("StartCellData")];
182 datafile.SetPtrAddress("StartCellData", event_offset, datafile.GetN("StartCellData"));
183
184 event_boardtimes = new int32_t[datafile.GetN("BoardTime")];
185 datafile.SetPtrAddress("BoardTime", event_boardtimes, datafile.GetN("BoardTime"));
186 }
187
188 void copy_calib_constants_twice( float *src, double *dest, double scale=1., int npix, int roi){
189 for (int pix=0 ; pix < npix; ++pix) {
190 for (int sl = 0; sl < roi; ++sl) {
191 int orig_index = pix * roi + sl;
192 int new_index1 = 2*pix * roi + sl;
193 int new_index2 = (2*pix+1) * roi + sl;
194 dest[new_index2] = dest[new_index1] = scale * src[orig_index];
195 }
196 }
197 }
198
199 ~CalFactFits() //Standard destructor
200 {
201 delete[] event_data;
202 delete[] event_offset;
203 delete[] event_boardtimes;
204 delete[] baseline;
205 delete[] gain;
206 delete[] trigger_offset;
207 }
208
209 //Read calibrated event into the event variables
210 bool GetCalEvent( omit_calibration=false, omit_despiking=false)
211 {
212 if(!npcaldata) {
213 ostringstream str;
214 str << "Pointer to the calibrated data not initialized!";
215 throw runtime_error(str.str());
216 }
217 if(datafile.GetNextRow() == false) {
218 return false;
219 }
220 else
221 {
222 UInt_t drs_calib_offset;
223 double raw, raw_bsl, shifted;
224 if ( do_calibration) {
225 for(UInt_t pixel = 0; pixel < data_npix; pixel++) {
226 for(UInt_t slice = 0; slice < data_nroi; slice++) {
227 drs_calib_offset = slice + event_offset[pixel];
228 raw = (double)event_data[pixel*data_nroi+slice];
229 raw_bsl = raw - baseline[pixel*calib_blm_size*2+drs_calib_offset];
230 shifted = raw_bsl - trigger_offset[pixel*data_nroi*2+slice];
231 npcaldata[pixel*data_nroi+slice] = shifted / gain[pixel*calib_blm_size*2+drs_calib_offset];
232 //Note: data_nroi=calib_nroi, calib_blm_size=calib_gm_size
233 }
234
235 if ( do_despiking ){
236 // second loop for despiking
237 // stolen from TBs MARS ... DrsCalib.h or something like that
238 double *p = npcaldata+(pixel*data_nroi);
239 for (size_t i=1; i<data_nroi-2; i++) {
240 if (p[i]-p[i-1]>single_spike_limit && p[i]-p[i+1]>single_spike_limit) {
241 p[i] = (p[i-1]+p[i+1])/2;
242 }
243 if (p[i]-p[i-1]>double_spike_outer_limit
244 && fabs(p[i]-p[i+1])<double_spike_inner_limit
245 && p[i+1]-p[i+2]>double_spike_outer_limit) {
246 p[i] = (p[i-1]+p[i+2])/2;
247 p[i+1] = p[i];
248 }
249 }
250 } // end of if do_despiking
251
252 }
253 else { // not doing calibration
254 for(UInt_t pixel = 0; pixel < data_npix; pixel++) {
255 for(UInt_t slice = 0; slice < data_nroi; slice++) {
256 npcaldata[pixel*data_nroi+slice] = (double)event_data[pixel*data_nroi+slice];
257 }
258 }
259 }
260 }
261 return true;
262 }
263
264 void SetNpcaldataPtr(double *numpyptr) //Set the pointer for the calibrated data to the numpy array
265 {
266 npcaldata = numpyptr;
267 return;
268 }
269};
270#endif /* CALFACTFITS_H */
Note: See TracBrowser for help on using the repository browser.