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

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