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

Last change on this file since 3120 was 3069, 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 obj1->AddNotify(fPedestalCam);
177
178 c.cd(x+y);
179 gPad->SetBorderMode(0);
180 obj1->Draw();
181
182 c.cd(x+2*y);
183 gPad->SetBorderMode(0);
184 DrawProjection(obj1, fit);
185}
186
187void MJPedestal::DisplayResult(MParList &plist)
188{
189 if (!fDisplay)
190 return;
191
192 //
193 // Update display
194 //
195 TString title = fDisplay->GetTitle();
196 title += "-- Pedestal ";
197 title += fRuns->GetRunsAsString();
198 title += " --";
199 fDisplay->SetTitle(title);
200
201 //
202 // Get container from list
203 //
204 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
205
206 //
207 // Create container to display
208 //
209 MHCamera disp0(geomcam, "MPedestalCam;ped", "Pedestals");
210 MHCamera disp1(geomcam, "MPedestalCam;rms", "Pedestal RMS");
211
212 disp0.SetCamContent(fPedestalCam, 0);
213 disp0.SetCamError (fPedestalCam, 1);
214
215 disp1.SetCamContent(fPedestalCam, 2);
216 disp1.SetCamError (fPedestalCam, 3);
217
218 disp0.SetYTitle("P [fadc/slice]");
219 disp1.SetYTitle("P_{RMS} [fadc/slice]");
220
221 //
222 // Display data
223 //
224 TCanvas &c3 = fDisplay->AddTab("Pedestals");
225 c3.Divide(2,3);
226
227 CamDraw(c3, 1, 2, disp0, 0);
228 CamDraw(c3, 2, 2, disp1, 1);
229}
230
231Bool_t MJPedestal::WriteResult()
232{
233 if (fOutputPath.IsNull())
234 return kTRUE;
235
236 const TString oname(GetOutputFile());
237
238 *fLog << inf << "Writing to file: " << oname << endl;
239
240 TFile file(oname, "UPDATE");
241
242 if (fDisplay && fDisplay->Write()<=0)
243 {
244 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
245 return kFALSE;
246 }
247
248 if (fPedestalCam.Write()<=0)
249 {
250 *fLog << err << "Unable to write MPedestalCam to " << oname << endl;
251 return kFALSE;
252 }
253
254 if (fBadPixels.Write()<=0)
255 {
256 *fLog << err << "Unable to write MBadPixelsCam to " << oname << endl;
257 return kFALSE;
258 }
259
260 return kTRUE;
261}
262
263void MJPedestal::SetOutputPath(const char *path)
264{
265 fOutputPath = path;
266 if (fOutputPath.EndsWith("/"))
267 fOutputPath = fOutputPath(0, fOutputPath.Length()-1);
268}
269
270Bool_t MJPedestal::Process()
271{
272 if (!ReadPedestalCam())
273 return ProcessFile();
274
275 return kTRUE;
276}
277
278Bool_t MJPedestal::ProcessFile()
279{
280 if (!fRuns)
281 {
282 *fLog << err << "No Runs choosen... abort." << endl;
283 return kFALSE;
284 }
285 if (fRuns->GetNumRuns() != fRuns->GetNumEntries())
286 {
287 *fLog << err << "Number of files found doesn't metch number of runs... abort." << endl;
288 return kFALSE;
289 }
290
291 *fLog << inf;
292 fLog->Separator(GetDescriptor());
293 *fLog << "Calculate MPedestalCam from Runs " << fRuns->GetRunsAsString() << endl;
294 *fLog << endl;
295
296 MReadMarsFile read("Events");
297 read.DisableAutoScheme();
298 static_cast<MRead&>(read).AddFiles(*fRuns);
299
300 // Enable logging to file
301 //*fLog.SetOutputFile(lname, kTRUE);
302
303 // Setup Tasklist
304 MParList plist;
305 plist.AddToList(&fPedestalCam);
306
307 MTaskList tlist;
308 plist.AddToList(&tlist);
309
310 MGeomApply geomapl;
311 MBadPixelsMerge merge(&fBadPixels);
312 //MExtractSignal sigcalc;
313 MPedCalcPedRun pedcalc;
314
315 tlist.AddToList(&read);
316 tlist.AddToList(&geomapl);
317 tlist.AddToList(&merge);
318 //tlist.AddToList(&sigcalc);
319 tlist.AddToList(&pedcalc);
320
321 //
322 // Execute the eventloop
323 //
324 MEvtLoop evtloop(fName);
325 evtloop.SetParList(&plist);
326 evtloop.SetDisplay(fDisplay);
327 evtloop.SetLogStream(fLog);
328
329 // Execute first analysis
330 if (!evtloop.Eventloop())
331 {
332 *fLog << err << GetDescriptor() << ": Failed." << endl;
333 return kFALSE;
334 }
335
336 tlist.PrintStatistics();
337
338 DisplayResult(plist);
339
340 if (!WriteResult())
341 return kFALSE;
342
343 *fLog << inf << GetDescriptor() << ": Done." << endl;
344
345 return kTRUE;
346}
Note: See TracBrowser for help on using the repository browser.