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

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