source: trunk/MagicSoft/Mars/manalysis/MPedCalUpdate.cc@ 5207

Last change on this file since 5207 was 3779, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 6.1 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 03/2004 <mailto:tbretz@astro.uni.wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MPedCalUpdate
28//
29// This task reads files stored by MJ* storing pedestals (F2) and
30// calibration constants (F1) while an eventloop is running.
31//
32// The update is done according to the run-number. The information
33// which ped/cal files corresponds to which run-number is stored in
34// an ascii file. In a next step this ascii file can be created
35// automatically (hopefully from the Database)
36//
37/////////////////////////////////////////////////////////////////////////////
38#include "MPedCalUpdate.h"
39
40#include <fstream>
41#include <TFile.h>
42
43#include "MLog.h"
44#include "MLogManip.h"
45
46#include "MParList.h"
47
48#include "MRawRunHeader.h"
49#include "MPedPhotCam.h"
50#include "MCalibrationChargeCam.h"
51#include "MBadPixelsCam.h"
52
53ClassImp(MPedCalUpdate);
54
55using namespace std;
56
57static const TString gsDefName = "MPedCalUpdate";
58static const TString gsDefTitle = "";
59
60// --------------------------------------------------------------------------
61//
62// Default constructor.
63//
64MPedCalUpdate::MPedCalUpdate(const char *fname, const char *name, const char *title)
65 : fPedPhot(0), fCalCam(0), fFileName(fname)
66{
67 fName = name ? name : gsDefName.Data();
68 fTitle = title ? title : gsDefTitle.Data();
69
70 fLast=0;
71}
72
73Bool_t MPedCalUpdate::ReadPed(const char *fname)
74{
75 TFile file(fname, "READ");
76 if (fPedPhot->Read()<=0)
77 {
78 *fLog << err << "Unable to read MPedPhotCam from " << fname << endl;
79 return kFALSE;
80 }
81
82 if (file.FindKey("MBadPixelsCam"))
83 {
84 MBadPixelsCam bad;
85 if (bad.Read()<=0)
86 {
87 *fLog << err << "Unable to read MBadPixelsCam from " << fname << endl;
88 return kFALSE;
89 }
90 fBadPix->Merge(bad);
91 }
92 return kTRUE;
93}
94
95Bool_t MPedCalUpdate::ReadCal(const char *fname)
96{
97 TFile file(fname, "READ");
98 if (fCalCam->Read()<=0)
99 {
100 *fLog << err << "Unable to read MCalibrationChargeCam from " << fname << endl;
101 return kFALSE;
102 }
103
104 if (file.FindKey("MBadPixelsCam"))
105 {
106 MBadPixelsCam bad;
107 if (bad.Read()<=0)
108 {
109 *fLog << "Unable to read MBadPixelsCam from " << fname << endl;
110 return kFALSE;
111 }
112 fBadPix->Merge(bad);
113 }
114 return kTRUE;
115}
116
117Bool_t MPedCalUpdate::ReadPC(TString &line)
118{
119 const Int_t f = line.First(' ');
120 if (f<0)
121 {
122 *fLog << err << "Entry for pedestal file not found!" << endl;
123 return kFALSE;
124 }
125
126 const Int_t l = line.Last(' ')+1;
127 if (l<0)
128 {
129 *fLog << err << "Entry for calibration file not found!" << endl;
130 return kFALSE;
131 }
132
133 const TString fname1 = line(0, f);
134 const TString fname2 = line(l, line.Length());
135
136 TString mid(line(f, l-f));
137 mid=mid.Strip(TString::kBoth);
138 if (!mid.IsNull())
139 {
140 *fLog << err << "Found three filenames '" << fname1 << "' '" << mid << "' '" << fname2 << "'" << endl;
141 return kFALSE;
142 }
143
144 if (!ReadPed(fname1))
145 return kFALSE;
146
147 if (!ReadCal(fname2))
148 return kFALSE;
149
150 return kTRUE;
151}
152
153// --------------------------------------------------------------------------
154//
155//
156Bool_t MPedCalUpdate::ReInit(MParList *pList)
157{
158 MRawRunHeader *header = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
159 if (!header)
160 {
161 *fLog << err << "MRawRunHeader not found... aborting." << endl;
162 return kFALSE;
163 }
164 fPedPhot = (MPedPhotCam*)pList->FindCreateObj("MPedPhotCam");
165 if (!fPedPhot)
166 return kFALSE;
167 fBadPix = (MBadPixelsCam*)pList->FindCreateObj("MBadPixelsCam");
168 if (!fBadPix)
169 return kFALSE;
170 fCalCam = (MCalibrationChargeCam*)pList->FindCreateObj("MCalibrationChargeCam");
171 if (!fCalCam)
172 return kFALSE;
173
174 const Int_t run = header->GetRunNumber();
175
176 ifstream fin(fFileName);
177 if (!fin)
178 {
179 *fLog << err << "Cannot open file: " << fFileName << endl;
180 return kFALSE;
181 }
182
183 Int_t last = 0;
184
185 TString readline;
186
187 while (1)
188 {
189 // Read line from file
190 TString line;
191 line.ReadLine(fin);
192 if (!fin && !readline.IsNull() && fLast!=last)
193 {
194 *fLog << dbg << "Reading line " << readline << endl;
195 fLast = last;
196 return ReadPC(readline);
197 }
198
199 if (!fin)
200 break;
201
202 line = line.Strip(TString::kBoth);
203
204 // Interprete line
205 Int_t len, num;
206 if (sscanf(line.Data(), "%d %n", &num, &len)!=1)
207 continue;
208
209 // Check whether entries are in increasing order
210 if (num<=last)
211 {
212 *fLog << err << "Runs must be in increasing order..." << endl;
213 return kFALSE;
214 }
215 last = num;
216
217 // Check if a new set of files must be read
218 if (num<=run)
219 {
220 readline=line;
221 readline.Remove(0, len);
222 continue;
223 }
224
225 // This is the case if the correct files were already read
226 if (fLast==num)
227 return kTRUE;
228
229 // Read files given in new line
230 *fLog << dbg << "Reading line " << readline << endl;
231 fLast = num;
232 return ReadPC(readline);
233 }
234
235 return kTRUE;
236}
Note: See TracBrowser for help on using the repository browser.