source: trunk/MagicSoft/Mars/mjobs/MJPedestal.cc@ 3213

Last change on this file since 3213 was 3143, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 8.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, 1/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MJPedestal
28//
29/////////////////////////////////////////////////////////////////////////////
30#include "MJPedestal.h"
31
32#include <TF1.h>
33#include <TFile.h>
34#include <TStyle.h>
35#include <TString.h>
36#include <TCanvas.h>
37#include <TSystem.h>
38
39#include "MLog.h"
40#include "MLogManip.h"
41
42#include "MRunIter.h"
43#include "MParList.h"
44#include "MTaskList.h"
45#include "MEvtLoop.h"
46
47#include "MStatusDisplay.h"
48
49#include "MHCamera.h"
50#include "MPedestalCam.h"
51
52#include "MReadMarsFile.h"
53#include "MGeomApply.h"
54#include "MBadPixelsMerge.h"
55#include "MPedCalcPedRun.h"
56
57ClassImp(MJPedestal);
58
59using namespace std;
60
61MJPedestal::MJPedestal(const char *name, const char *title) : fRuns(0)
62{
63 fName = name ? name : "MJPedestal";
64 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
65}
66
67TString MJPedestal::GetOutputFile() const
68{
69 if (!fRuns)
70 return "";
71
72 return Form("%s/%s-F0.root", (const char*)fOutputPath, (const char*)fRuns->GetRunsAsFileName());
73}
74
75Bool_t MJPedestal::ReadPedestalCam()
76{
77 const TString fname = GetOutputFile();
78
79 if (gSystem->AccessPathName(fname, kFileExists))
80 {
81 *fLog << err << "Input file " << fname << " doesn't exist." << endl;
82 return kFALSE;
83 }
84
85 *fLog << inf << "Reading from file: " << fname << endl;
86
87 TFile file(fname, "READ");
88 if (fPedestalCam.Read()<=0)
89 {
90 *fLog << err << "Unable to read MPedestalCam from " << fname << endl;
91 return kFALSE;
92 }
93
94 if (file.FindKey("MBadPixelsCam"))
95 {
96 MBadPixelsCam bad;
97 if (bad.Read()<=0)
98 {
99 *fLog << err << "Unable to read MBadPixelsCam from " << fname << endl;
100 return kFALSE;
101 }
102 fBadPixels.Merge(bad);
103 }
104
105 if (fDisplay && !fDisplay->GetCanvas("Pedestals"))
106 fDisplay->Read();
107
108 return kTRUE;
109}
110
111void MJPedestal::DrawProjection(MHCamera *obj1, Int_t fit) const
112{
113 TH1D *obj2 = (TH1D*)obj1->Projection();
114 obj2->Draw();
115 obj2->SetBit(kCanDelete);
116
117 const Double_t min = obj2->GetBinCenter(obj2->GetXaxis()->GetFirst());
118 const Double_t max = obj2->GetBinCenter(obj2->GetXaxis()->GetLast());
119 const Double_t integ = obj2->Integral("width")/2.5066283;
120 const Double_t mean = obj2->GetMean();
121 const Double_t rms = obj2->GetRMS();
122 const Double_t width = max-min;
123
124 if (rms==0 || width==0)
125 return;
126
127 const TString dgausformula("([0]-[3])/[2]*exp(-0.5*(x-[1])*(x-[1])/[2]/[2])"
128 "+[3]/[5]*exp(-0.5*(x-[4])*(x-[4])/[5]/[5])");
129
130 TF1 *f=0;
131 switch (fit)
132 {
133 // FIXME: MAYBE add function to TH1?
134 case 0:
135 f = new TF1("sgaus", "gaus(0)", min, max);
136 f->SetLineColor(kYellow);
137 f->SetBit(kCanDelete);
138 f->SetParNames("Area", "#mu", "#sigma");
139 f->SetParameters(integ/rms, mean, rms);
140 f->SetParLimits(0, 0, integ);
141 f->SetParLimits(1, min, max);
142 f->SetParLimits(2, 0, width/1.5);
143 obj2->Fit(f, "QLRM");
144 break;
145
146 case 1:
147 f = new TF1("dgaus", dgausformula, min, max);
148 f->SetLineColor(kYellow);
149 f->SetBit(kCanDelete);
150 f->SetParNames("A_{tot}", "#mu_{1}", "#sigma_{1}", "A_{2}", "#mu_{2}", "#sigma_{2}");
151 f->SetParameters(integ, (min+mean)/2, width/4,
152 integ/width/2, (max+mean)/2, width/4);
153 // The left-sided Gauss
154 f->SetParLimits(0, integ-1.5, integ+1.5);
155 f->SetParLimits(1, min+(width/10), mean);
156 f->SetParLimits(2, 0, width/2);
157 // The right-sided Gauss
158 f->SetParLimits(3, 0, integ);
159 f->SetParLimits(4, mean, max-(width/10));
160 f->SetParLimits(5, 0, width/2);
161 obj2->Fit(f, "QLRM");
162 break;
163
164 default:
165 obj2->Fit("gaus", "Q");
166 obj2->GetFunction("gaus")->SetLineColor(kYellow);
167 break;
168 }
169}
170
171void MJPedestal::CamDraw(TCanvas &c, Int_t x, Int_t y, MHCamera &cam1, Int_t fit)
172{
173 c.cd(x);
174 gPad->SetBorderMode(0);
175 MHCamera *obj1=(MHCamera*)cam1.DrawCopy("hist");
176
177 obj1->AddNotify(&fPedestalCam);
178
179 c.cd(x+y);
180 gPad->SetBorderMode(0);
181 obj1->Draw();
182
183 c.cd(x+2*y);
184 gPad->SetBorderMode(0);
185 DrawProjection(obj1, fit);
186}
187
188void MJPedestal::DisplayResult(MParList &plist)
189{
190 if (!fDisplay)
191 return;
192
193 //
194 // Update display
195 //
196 TString title = fDisplay->GetTitle();
197 title += "-- Pedestal ";
198 title += fRuns->GetRunsAsString();
199 title += " --";
200 fDisplay->SetTitle(title);
201
202 //
203 // Get container from list
204 //
205 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
206
207 //
208 // Create container to display
209 //
210 MHCamera disp0(geomcam, "MPedestalCam;ped", "Pedestals");
211 MHCamera disp1(geomcam, "MPedestalCam;rms", "Pedestal RMS");
212
213 disp0.SetCamContent(fPedestalCam, 0);
214 disp0.SetCamError (fPedestalCam, 1);
215
216 disp1.SetCamContent(fPedestalCam, 2);
217 disp1.SetCamError (fPedestalCam, 3);
218
219 disp0.SetYTitle("P [fadc/slice]");
220 disp1.SetYTitle("P_{RMS} [fadc/slice]");
221
222 //
223 // Display data
224 //
225 TCanvas &c3 = fDisplay->AddTab("Pedestals");
226 c3.Divide(2,3);
227
228 CamDraw(c3, 1, 2, disp0, 0);
229 CamDraw(c3, 2, 2, disp1, 1);
230}
231
232Bool_t MJPedestal::WriteResult()
233{
234 if (fOutputPath.IsNull())
235 return kTRUE;
236
237 const TString oname(GetOutputFile());
238
239 *fLog << inf << "Writing to file: " << oname << endl;
240
241 TFile file(oname, "UPDATE");
242
243 if (fDisplay && fDisplay->Write()<=0)
244 {
245 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
246 return kFALSE;
247 }
248
249 if (fPedestalCam.Write()<=0)
250 {
251 *fLog << err << "Unable to write MPedestalCam to " << oname << endl;
252 return kFALSE;
253 }
254
255 if (fBadPixels.Write()<=0)
256 {
257 *fLog << err << "Unable to write MBadPixelsCam to " << oname << endl;
258 return kFALSE;
259 }
260
261 return kTRUE;
262}
263
264void MJPedestal::SetOutputPath(const char *path)
265{
266 fOutputPath = path;
267 if (fOutputPath.EndsWith("/"))
268 fOutputPath = fOutputPath(0, fOutputPath.Length()-1);
269}
270
271Bool_t MJPedestal::Process()
272{
273 if (!ReadPedestalCam())
274 return ProcessFile();
275
276 return kTRUE;
277}
278
279Bool_t MJPedestal::ProcessFile()
280{
281 if (!fRuns)
282 {
283 *fLog << err << "No Runs choosen... abort." << endl;
284 return kFALSE;
285 }
286 if (fRuns->GetNumRuns() != fRuns->GetNumEntries())
287 {
288 *fLog << err << "Number of files found doesn't metch number of runs... abort." << endl;
289 return kFALSE;
290 }
291
292 *fLog << inf;
293 fLog->Separator(GetDescriptor());
294 *fLog << "Calculate MPedestalCam from Runs " << fRuns->GetRunsAsString() << endl;
295 *fLog << endl;
296
297 MReadMarsFile read("Events");
298 read.DisableAutoScheme();
299 static_cast<MRead&>(read).AddFiles(*fRuns);
300
301 // Enable logging to file
302 //*fLog.SetOutputFile(lname, kTRUE);
303
304 // Setup Tasklist
305 MParList plist;
306 plist.AddToList(&fPedestalCam);
307
308 MTaskList tlist;
309 plist.AddToList(&tlist);
310
311 MGeomApply geomapl;
312 MBadPixelsMerge merge(&fBadPixels);
313 //MExtractSignal sigcalc;
314 MPedCalcPedRun pedcalc;
315
316 tlist.AddToList(&read);
317 tlist.AddToList(&geomapl);
318 tlist.AddToList(&merge);
319 //tlist.AddToList(&sigcalc);
320 tlist.AddToList(&pedcalc);
321
322 //
323 // Execute the eventloop
324 //
325 MEvtLoop evtloop(fName);
326 evtloop.SetParList(&plist);
327 evtloop.SetDisplay(fDisplay);
328 evtloop.SetLogStream(fLog);
329
330 // Execute first analysis
331 if (!evtloop.Eventloop())
332 {
333 *fLog << err << GetDescriptor() << ": Failed." << endl;
334 return kFALSE;
335 }
336
337 tlist.PrintStatistics();
338
339 DisplayResult(plist);
340
341 if (!WriteResult())
342 return kFALSE;
343
344 *fLog << inf << GetDescriptor() << ": Done." << endl;
345
346 return kTRUE;
347}
Note: See TracBrowser for help on using the repository browser.