source: fact/tools/pyscripts/sandbox/vogler/calfits.h@ 15446

Last change on this file since 15446 was 14173, checked in by vogler, 13 years ago
inital filling of my sandbox
File size: 5.6 KB
Line 
1//********************************
2//
3// Class Calfits
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 pyfits.h++
16// root [2] .L ../sandbox/kraehenb/calfits.h++
17//
18// Usage in Python:
19// See CalFitsTest.py
20//
21//********************************
22
23//ToDo: shared library creation debuggen
24
25#ifndef CALFITS_H
26#define CALFITS_H
27
28#include <cstdio>
29#include <string>
30
31#ifndef __MARS__
32#include <vector>
33#include <iomanip>
34#include <iostream>
35#define gLog cerr
36#define ___err___ ""
37#define ___all___ ""
38#else
39#include "MLog.h"
40#include "MLogManip.h"
41#define ___err___ err
42#define ___all___ all
43#endif
44
45#ifdef __EXCEPTIONS
46#include <stdexcept>
47#endif
48
49#include "../../pyfact/pyfits.h"
50
51
52class CalFits
53{
54public:
55 //No standard constructor CalFits()!
56
57 //Direct handlers of the files
58 fits 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 //Using <vector> instead of arrays makes no visible difference
73 //ToDo: use arrays of size 1440x1024 (x2 for wrap-arounds) and read all variables into those
74
75 //Event variables
76 UInt_t event_id;
77 UShort_t event_triggertype;
78 int16_t* event_data;
79 int16_t* event_offset;
80 int32_t* event_boardtimes;
81 double* npcaldata;
82
83 CalFits(const string &datafilename, const string &calibfilename) //Constructor with two filenames
84 : datafile(datafilename),
85 calibfile(calibfilename),
86 npcaldata(NULL)
87 {
88 //Read basic parameters of the two files
89// std::cout << "...Reading basic file parameters..." << std::endl;
90 calib_nroi = calibfile.GetUInt("NROI");
91 calib_npix = calibfile.GetUInt("NPIX");
92 data_nroi = datafile.GetUInt("NROI");
93 data_npix = datafile.GetUInt("NPIX");
94 data_ndata = datafile.GetN("Data");
95
96 calib_blm_size = calibfile.GetN("BaselineMean")/calib_npix;
97 calib_gm_size = calibfile.GetN("GainMean")/calib_npix;
98 calib_tom_size = calibfile.GetN("TriggerOffsetMean")/calib_npix;
99
100// std::cout << "Column sizes: " << calib_blm_size << " " << calib_gm_size << " " << calib_tom_size << std::endl;
101
102 //Define the common variables
103 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)) {
104 nroi = data_nroi;
105 npix = data_npix;
106 }
107 else {
108 ostringstream str;
109 str << "Data/calib file error: NROI mismatch, NPIX mismatch, data column size wrong or calib columns mismatch.";
110#ifdef __EXCEPTIONS
111 throw runtime_error(str.str());
112#else
113 gLog << ___err___ << "ERROR - " << str.str() << endl;
114 return;
115#endif
116 }
117 nevents = datafile.GetNumRows();
118
119 //Read the calibration data
120// std::cout << "...Reading calibration data..." << std::endl;
121 calib_baselinemean = new float[calibfile.GetN("BaselineMean")];
122 calibfile.SetPtrAddress("BaselineMean", calib_baselinemean, calibfile.GetN("BaselineMean"));
123 calib_gainmean = new float[calibfile.GetN("GainMean")];
124 calibfile.SetPtrAddress("GainMean", calib_gainmean, calibfile.GetN("GainMean"));
125 calib_triggeroffsetmean = new float[calibfile.GetN("TriggerOffsetMean")];
126 calibfile.SetPtrAddress("TriggerOffsetMean", calib_triggeroffsetmean, calibfile.GetN("TriggerOffsetMean"));
127 calibfile.GetRow(0);
128
129 //Set the event pointers
130// std::cout << "...Setting event pointers..." << std::endl;
131 datafile.SetRefAddress("EventNum", event_id);
132 datafile.SetRefAddress("TriggerType", event_triggertype);
133
134 event_data = new int16_t[data_ndata];
135 datafile.SetPtrAddress("Data", event_data, data_ndata);
136
137 event_offset = new int16_t[datafile.GetN("StartCellData")];
138 datafile.SetPtrAddress("StartCellData", event_offset, datafile.GetN("StartCellData"));
139
140 event_boardtimes = new int32_t[datafile.GetN("BoardTime")];
141 datafile.SetPtrAddress("BoardTime", event_boardtimes, datafile.GetN("BoardTime"));
142 }
143
144 ~CalFits() //Standard destructor
145 {
146 delete[] calib_baselinemean;
147 delete[] calib_gainmean;
148 delete[] calib_triggeroffsetmean;
149 delete[] event_data;
150 delete[] event_offset;
151 delete[] event_boardtimes;
152 }
153
154 bool GetCalEvent() //Read calibrated event into the event variables
155 {
156 if(!npcaldata) {
157 ostringstream str;
158 str << "Pointer to the calibrated data not initialized!";
159#ifdef __EXCEPTIONS
160 throw runtime_error(str.str());
161#else
162 gLog << ___err___ << "ERROR - " << str.str() << endl;
163 return false;
164#endif
165 }
166 if(datafile.GetNextRow() == false) {
167// std::cout << "Last event reached..." << std::endl;
168 return false;
169 }
170 else {
171 UInt_t drs_calib_offset;
172 for(UInt_t pixel=0;pixel<data_npix;pixel++) {
173 for(UInt_t slice=0;slice<data_nroi;slice++) {
174 drs_calib_offset = (slice+event_offset[pixel])%calib_blm_size;
175 npcaldata[pixel*data_nroi+slice] = double((event_data[pixel*data_nroi+slice]*2000./4096.-calib_baselinemean[pixel*calib_blm_size+drs_calib_offset]-calib_triggeroffsetmean[pixel*data_nroi+slice])/calib_gainmean[pixel*calib_blm_size+drs_calib_offset]*1907.35);
176 //Note: data_nroi=calib_nroi, calib_blm_size=calib_gm_size
177 }
178 }
179 }
180 return true;
181 }
182
183 void SetNpcaldataPtr(double *numpyptr) //Set the pointer for the calibrated data to the numpy array
184 {
185 npcaldata = numpyptr;
186 return;
187 }
188};
189#endif
Note: See TracBrowser for help on using the repository browser.