source: branches/Mars_IncreaseNsb/mdrs/MDrsCalibrationTime.cc

Last change on this file was 18155, checked in by tbretz, 10 years ago
We now precalculate the DRS offsets. This is faster and allows to read and write them directly to a fits file.
File size: 4.7 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 2013 <mailto:tbretz@physik.rwth-aachen.de>
19!
20! Copyright: MAGIC Software Development, 2000-2015
21!
22!
23\* ======================================================================== */
24
25///////////////////////////////////////////////////////////////////////
26//
27// MHDrsCalibration
28//
29///////////////////////////////////////////////////////////////////////
30#include "MDrsCalibrationTime.h"
31
32#include <TH1.h>
33#include <TGraph.h>
34
35ClassImp(MDrsCalibrationTime);
36
37using namespace std;
38
39bool MDrsCalibrationTime::SetDelays(const TH1 &cam)
40{
41 if (cam.GetDimension()!=1)
42 return false;
43
44 fDelays.assign(cam.GetNbinsX(), 0);
45
46 for (int i=0; i<cam.GetNbinsX(); i++)
47 fDelays[i] = cam.GetBinContent(i+1);
48
49 return true;
50}
51
52void MDrsCalibrationTime::SetDelays(const TGraph &g)
53{
54 fDelays.assign(g.GetY(), g.GetY()+g.GetN());
55}
56
57bool MDrsCalibrationTime::ReadFits(TString fname)
58{
59 gSystem->ExpandPathName(fname);
60
61 try
62 {
63 fits file(fname.Data());
64 if (!file)
65 throw runtime_error(strerror(errno));
66
67 if (file.GetStr("TELESCOP")!="FACT")
68 {
69 std::ostringstream msg;
70 msg << "Not a valid FACT file (TELESCOP not FACT in header)";
71 throw runtime_error(msg.str());
72 }
73
74 if (file.GetNumRows()!=1)
75 throw runtime_error("Number of rows in table is not 1.");
76
77 fNumSamples = file.GetUInt("NROI");
78 fNumChannels = file.GetUInt("NCH");
79 fNumEntries = file.GetUInt("NBTIME");
80
81 const double *d = reinterpret_cast<double*>(file.SetPtrAddress("CellOffset"));
82 if (!file.GetNextRow())
83 throw runtime_error("Read error.");
84
85 fOffsets.assign(d, d+fNumSamples*fNumChannels),
86 fDelays.resize(0);
87 }
88 catch (const exception &e)
89 {
90 *fLog << err << "Error reading from " << fname << ": " << e.what() << endl;
91 return false;
92 }
93
94 *fLog << inf << "Read DRS calibration file " << fname << endl;
95 return true;
96}
97
98bool MDrsCalibrationTime::WriteFits(const string &fname) const
99{
100 const Bool_t exists = !gSystem->AccessPathName(fname.c_str(), kFileExists);
101 if (exists)
102 {
103 *fLog << err << "File '" << fname << "' already exists." << endl;
104 return false;
105 }
106
107 try
108 {
109 ofits file(fname.c_str());
110 if (!file)
111 throw runtime_error(strerror(errno));
112
113 file.SetDefaultKeys();
114 file.AddColumnDouble(fNumSamples*fNumChannels, "CellOffset", "samples", "Integral cell offset");
115
116 file.SetInt("ADCRANGE", 2000, "Dynamic range of the ADC in mV");
117 file.SetInt("DACRANGE", 2500, "Dynamic range of the DAC in mV");
118 file.SetInt("ADC", 12, "Resolution of ADC in bits");
119 file.SetInt("DAC", 16, "Resolution of DAC in bits");
120 file.SetInt("NPIX", 1440, "Number of channels in the camera");
121 file.SetInt("NTM", 0, "Number of time marker channels");
122 file.SetInt("NROI", fNumSamples, "Region of interest");
123 file.SetInt("NCH", fNumChannels, "Number of chips");
124 file.SetInt("NBTIME", fNumEntries, "Num of entries for time calibration");
125
126 file.WriteTableHeader("DrsCellTimes");
127 //file.SetInt("NIGHT", night, "Night as int");
128
129 /*
130 file.SetStr("DATE-OBS", fDateObs, "First event of whole DRS calibration");
131 file.SetStr("DATE-END", fDateEnd, "Last event of whole DRS calibration");
132 file.SetStr("RUN-BEG", fDateRunBeg[0], "First event of run 0");
133 file.SetStr("RUN-END", fDateRunEnd[0], "Last event of run 0");
134 */
135
136 if (!file.WriteRow(fOffsets.data(), fOffsets.size()*sizeof(double)))
137 throw runtime_error("Write error.");
138 }
139 catch (const exception &e)
140 {
141 *fLog << err << "Error writing to " << fname << ": " << e.what() << endl;
142 return false;
143 }
144
145 *fLog << inf << "Wrote DRS calibration file " << fname << endl;
146 return true;
147}
Note: See TracBrowser for help on using the repository browser.