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

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