source: fact/tools/pyscripts/sandbox/kraehenb/calfits.h@ 13446

Last change on this file since 13446 was 13426, checked in by kraehenb, 13 years ago
Use arrays with dynamic memory instead of vectors.
File size: 5.4 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: alle Event-Parameter zugänglich, 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
51class CalFits
52{
53public:
54 //No standard constructor CalFits()!
55
56 //Direct handlers of the files
57 fits datafile, calibfile; //Class name should be PyFits or better FactPyFits...
58
59 //Basic file parameters
60 UInt_t calib_nroi, calib_npix;
61 UInt_t calib_blm_size, calib_gm_size, calib_tom_size;
62 UInt_t data_nroi, data_npix, data_ndata;
63
64 //Common variables
65 UInt_t nroi, npix, nevents;
66
67 //Calibration variables
68 float* calib_baselinemean;
69 float* calib_gainmean;
70 float* calib_triggeroffsetmean;
71 //ToDo: use arrays of size 1440x1024 (x2 for wrap-arounds) and read all variables into those
72
73 //Event variables
74 UInt_t event_id;
75 UShort_t event_triggertype;
76 int16_t* event_data;
77 int16_t* event_offset;
78 int32_t* event_boardtimes;
79 double* npcaldata;
80
81 CalFits(const string &datafilename, const string &calibfilename) //Constructor with two filenames
82 : datafile(datafilename),
83 calibfile(calibfilename),
84 npcaldata(NULL)
85 {
86 //Read basic parameters of the two files
87// std::cout << "...Reading basic file parameters..." << std::endl;
88 calib_nroi = calibfile.GetUInt("NROI");
89 calib_npix = calibfile.GetUInt("NPIX");
90 data_nroi = datafile.GetUInt("NROI");
91 data_npix = datafile.GetUInt("NPIX");
92 data_ndata = datafile.GetN("Data");
93
94 calib_blm_size = calibfile.GetN("BaselineMean")/calib_npix;
95 calib_gm_size = calibfile.GetN("GainMean")/calib_npix;
96 calib_tom_size = calibfile.GetN("TriggerOffsetMean")/calib_npix;
97
98// std::cout << "Column sizes: " << calib_blm_size << " " << calib_gm_size << " " << calib_tom_size << std::endl;
99
100 //Define the common variables
101 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)) {
102 nroi = data_nroi;
103 npix = data_npix;
104 }
105 else {
106 ostringstream str;
107 str << "Data/calib file error: NROI mismatch, NPIX mismatch, data column size wrong or calib columns mismatch.";
108#ifdef __EXCEPTIONS
109 throw runtime_error(str.str());
110#else
111 gLog << ___err___ << "ERROR - " << str.str() << endl;
112 return;
113#endif
114 }
115 nevents = datafile.GetNumRows();
116
117 //Read the calibration data
118// std::cout << "...Reading calibration data..." << std::endl;
119 calib_baselinemean = new float[calibfile.GetN("BaselineMean")];
120 calibfile.SetPtrAddress("BaselineMean", calib_baselinemean, calibfile.GetN("BaselineMean"));
121 calib_gainmean = new float[calibfile.GetN("GainMean")];
122 calibfile.SetPtrAddress("GainMean", calib_gainmean, calibfile.GetN("GainMean"));
123 calib_triggeroffsetmean = new float[calibfile.GetN("TriggerOffsetMean")];
124 calibfile.SetPtrAddress("TriggerOffsetMean", calib_triggeroffsetmean, calibfile.GetN("TriggerOffsetMean"));
125 calibfile.GetRow(0);
126
127 //Set the event pointers
128// std::cout << "...Setting event pointers..." << std::endl;
129 datafile.SetRefAddress("EventNum", event_id);
130 datafile.SetRefAddress("TriggerType", event_triggertype);
131
132 event_data = new int16_t[data_ndata];
133 datafile.SetPtrAddress("Data", event_data, data_ndata);
134
135 event_offset = new int16_t[datafile.GetN("StartCellData")];
136 datafile.SetPtrAddress("StartCellData", event_offset, datafile.GetN("StartCellData"));
137
138 event_boardtimes = new int32_t[datafile.GetN("BoardTime")];
139 datafile.SetPtrAddress("BoardTime", event_boardtimes, datafile.GetN("BoardTime"));
140 }
141
142 ~CalFits()
143 {
144 delete[] calib_baselinemean;
145 delete[] calib_gainmean;
146 delete[] calib_triggeroffsetmean;
147 delete[] event_data;
148 delete[] event_offset;
149 delete[] event_boardtimes;
150 }
151
152 bool GetCalEvent()
153 {
154 if(!npcaldata) {
155 ostringstream str;
156 str << "Pointer to the calibrated data not initialized!";
157#ifdef __EXCEPTIONS
158 throw runtime_error(str.str());
159#else
160 gLog << ___err___ << "ERROR - " << str.str() << endl;
161 return false;
162#endif
163 }
164 if(datafile.GetNextRow() == false) {
165// std::cout << "Last event reached..." << std::endl;
166 return false;
167 }
168 else {
169 UInt_t drs_calib_offset;
170 for(UInt_t pixel=0;pixel<data_npix;pixel++) {
171 for(UInt_t slice=0;slice<data_nroi;slice++) {
172 drs_calib_offset = (slice+event_offset[pixel])%calib_blm_size;
173 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);
174 //Note: data_nroi=calib_nroi, calib_blm_size=calib_gm_size
175 }
176 }
177 }
178 return true;
179 }
180
181 void SetNpcaldataPtr(double *ptr)
182 {
183 npcaldata = ptr;
184 return;
185 }
186};
187#endif
Note: See TracBrowser for help on using the repository browser.