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

Last change on this file since 4601 was 4601, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 13.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! 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 <TEnv.h>
35#include <TFile.h>
36#include <TLine.h>
37#include <TLatex.h>
38#include <TString.h>
39#include <TCanvas.h>
40#include <TSystem.h>
41#include <TLegend.h>
42
43#include "MLog.h"
44#include "MLogManip.h"
45
46#include "MTaskEnv.h"
47#include "MSequence.h"
48#include "MRunIter.h"
49#include "MParList.h"
50#include "MTaskList.h"
51#include "MEvtLoop.h"
52#include "MExtractor.h"
53
54#include "MStatusDisplay.h"
55
56#include "MGeomCam.h"
57#include "MHCamera.h"
58#include "MPedestalCam.h"
59#include "MBadPixelsCam.h"
60
61#include "MReadMarsFile.h"
62#include "MRawFileRead.h"
63#include "MGeomApply.h"
64#include "MBadPixelsMerge.h"
65#include "MPedCalcPedRun.h"
66
67ClassImp(MJPedestal);
68
69using namespace std;
70
71const Double_t MJPedestal::fgPedestalMin = 4.;
72const Double_t MJPedestal::fgPedestalMax = 16.;
73const Double_t MJPedestal::fgPedRmsMin = 0.;
74const Double_t MJPedestal::fgPedRmsMax = 20.;
75
76const Float_t MJPedestal::fgRefPedClosedLids = 9.635;
77const Float_t MJPedestal::fgRefPedExtraGalactic = 9.93;
78const Float_t MJPedestal::fgRefPedGalactic = 10.03;
79const Float_t MJPedestal::fgRefPedRmsClosedLids = 1.7;
80const Float_t MJPedestal::fgRefPedRmsExtraGalactic = 5.6;
81const Float_t MJPedestal::fgRefPedRmsGalactic = 6.92;
82
83// --------------------------------------------------------------------------
84//
85// Default constructor.
86//
87// Sets fRuns to 0, fExtractor to NULL, fDataCheck to kFALSE
88//
89MJPedestal::MJPedestal(const char *name, const char *title)
90 : fEnv(0), fRuns(0), fSequence(0), fExtractor(NULL), fDisplayType(kNormalDisplay),
91 fDataCheck(kFALSE)
92{
93 fName = name ? name : "MJPedestal";
94 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
95}
96
97MJPedestal::~MJPedestal()
98{
99 if (fEnv)
100 delete fEnv;
101}
102
103const char* MJPedestal::GetOutputFile() const
104{
105 if (fSequence)
106 return Form("%s/calped%05d", (const char*)fOutputPath, fSequence->GetSequence());
107
108 if (!fRuns)
109 return "";
110
111 return Form("%s/%s-F0.root", (const char*)fOutputPath, (const char*)fRuns->GetRunsAsFileName());
112}
113
114Bool_t MJPedestal::ReadPedestalCam()
115{
116 const TString fname = GetOutputFile();
117
118 if (gSystem->AccessPathName(fname, kFileExists))
119 {
120 *fLog << warn << "Input file " << fname << " doesn't exist, will create it." << endl;
121 return kFALSE;
122 }
123
124 *fLog << inf << "Reading from file: " << fname << endl;
125
126 TFile file(fname, "READ");
127 if (fPedestalCam.Read()<=0)
128 {
129 *fLog << err << "Unable to read MPedestalCam from " << fname << endl;
130 return kFALSE;
131 }
132
133 if (file.FindKey("MBadPixelsCam"))
134 {
135 MBadPixelsCam bad;
136 if (bad.Read()<=0)
137 {
138 *fLog << err << "Unable to read MBadPixelsCam from " << fname << endl;
139 return kFALSE;
140 }
141 fBadPixels.Merge(bad);
142 }
143
144 if (fDisplay && !fDisplay->GetCanvas("Pedestals"))
145 fDisplay->Read();
146
147 return kTRUE;
148}
149
150void MJPedestal::DisplayResult(MParList &plist)
151{
152 if (!fDisplay)
153 return;
154
155 //
156 // Update display
157 //
158 TString title = fDisplay->GetTitle();
159 title += "-- Pedestal ";
160 if (fSequence)
161 title += fSequence->GetName();
162 else
163 if (fRuns) // FIXME: What to do if an environmentfile was used?
164 title += fRuns->GetRunsAsString();
165 title += " --";
166 fDisplay->SetTitle(title);
167
168 //
169 // Get container from list
170 //
171 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
172
173 //
174 // Create container to display
175 //
176 MHCamera disp0(geomcam, "MPedestalCam;ped", "Mean Pedestal");
177 MHCamera disp1(geomcam, "MPedestalCam;RMS", "Pedestal RMS");
178
179 disp0.SetCamContent(fPedestalCam, 0);
180 disp0.SetCamError (fPedestalCam, 1);
181
182 disp1.SetCamContent(fPedestalCam, 2);
183 disp1.SetCamError (fPedestalCam, 3);
184
185 disp0.SetYTitle("P [fadc/slice]");
186 disp1.SetYTitle("\\sigma_{P} [fadc/slice]");
187
188 //
189 // Display data
190 //
191 TCanvas &c3 = fDisplay->AddTab("Pedestals");
192 c3.Divide(2,3);
193
194 if (fDisplayType != kDataCheckDisplay)
195 {
196 disp0.CamDraw(c3, 1, 2, 1);
197 disp1.CamDraw(c3, 2, 2, 6);
198 return;
199 }
200
201 c3.cd(1);
202 gPad->SetBorderMode(0);
203 gPad->SetTicks();
204 MHCamera *obj1=(MHCamera*)disp0.DrawCopy("hist");
205 obj1->SetDirectory(NULL);
206
207 //
208 // for the datacheck, fix the ranges!!
209 //
210 obj1->SetMinimum(fgPedestalMin);
211 obj1->SetMaximum(fgPedestalMax);
212
213 //
214 // set reference lines
215 //
216 DisplayReferenceLines(obj1,0);
217
218 //
219 // end reference lines
220 //
221 c3.cd(3);
222 gPad->SetBorderMode(0);
223 obj1->SetPrettyPalette();
224 obj1->Draw();
225
226 c3.cd(5);
227 gPad->SetBorderMode(0);
228 gPad->SetTicks();
229 obj1->DrawProjection(7);
230
231 c3.cd(2);
232 gPad->SetBorderMode(0);
233 gPad->SetTicks();
234 MHCamera *obj2=(MHCamera*)disp1.DrawCopy("hist");
235 obj2->SetDirectory(NULL);
236 obj2->SetMinimum(fgPedRmsMin);
237 obj2->SetMaximum(fgPedRmsMax);
238
239 //
240 // set reference lines
241 //
242 DisplayReferenceLines(obj1,1);
243
244 c3.cd(4);
245 gPad->SetBorderMode(0);
246 obj2->SetPrettyPalette();
247 obj2->Draw();
248
249 c3.cd(6);
250 gPad->SetBorderMode(0);
251
252 if (geomcam.InheritsFrom("MGeomCamMagic"))
253 {
254 TArrayI inner(1);
255 inner[0] = 0;
256
257 TArrayI outer(1);
258 outer[0] = 1;
259
260 TArrayI s0(6);
261 s0[0] = 6;
262 s0[1] = 1;
263 s0[2] = 2;
264 s0[3] = 3;
265 s0[4] = 4;
266 s0[5] = 5;
267
268 TArrayI s1(3);
269 s1[0] = 6;
270 s1[1] = 1;
271 s1[2] = 2;
272
273 TArrayI s2(3);
274 s2[0] = 3;
275 s2[1] = 4;
276 s2[2] = 5;
277
278 TVirtualPad *pad = gPad;
279 pad->Divide(2,1);
280
281 TH1D *inout[2];
282 inout[0] = disp1.ProjectionS(s0, inner, "Inner");
283 inout[1] = disp1.ProjectionS(s0, outer, "Outer");
284
285 for (int i=0; i<2; i++)
286 {
287 pad->cd(i+1);
288 gPad->SetBorderMode(0);
289 gPad->SetTicks();
290
291 inout[i]->SetDirectory(NULL);
292 inout[i]->SetLineColor(kRed+i);
293 inout[i]->SetBit(kCanDelete);
294 inout[i]->Draw();
295 inout[i]->Fit("gaus", "Q");
296
297 TLegend *leg2 = new TLegend(0.6,0.2,0.9,0.55);
298 leg2->SetHeader(inout[i]->GetName());
299 leg2->AddEntry(inout[i], inout[i]->GetName(), "l");
300
301 //
302 // Display the outliers as dead and noisy pixels
303 //
304 DisplayOutliers(inout[i]);
305
306 //
307 // Display the two half of the camera separately
308 //
309 TH1D *half[2];
310 half[0] = disp1.ProjectionS(s1, i==0 ? inner : outer , "Sector 6-1-2");
311 half[1] = disp1.ProjectionS(s2, i==0 ? inner : outer , "Sector 3-4-5");
312
313 for (int j=0; j<2; j++)
314 {
315 half[j]->SetLineColor(kRed+i+2*j+1);
316 half[j]->SetDirectory(NULL);
317 half[j]->SetBit(kCanDelete);
318 half[j]->Draw("same");
319 leg2->AddEntry(half[j], half[j]->GetName(), "l");
320 }
321 leg2->Draw();
322 }
323 }
324}
325
326void MJPedestal::DisplayReferenceLines(MHCamera *cam, const Int_t what) const
327{
328 const Double_t x = cam->GetNbinsX();
329
330 TLine line;
331 line.SetLineStyle(kDashed);
332 line.SetLineWidth(3);
333
334 line.SetLineColor(kBlue);
335 TLine *l1 = line.DrawLine(0, what ? fgRefPedRmsGalactic : fgRefPedGalactic,
336 x, what ? fgRefPedRmsGalactic : fgRefPedGalactic);
337
338 line.SetLineColor(kYellow);
339 TLine *l2 = line.DrawLine(0, what ? fgRefPedRmsExtraGalactic : fgRefPedExtraGalactic,
340 x, what ? fgRefPedRmsExtraGalactic : fgRefPedExtraGalactic);
341
342 line.SetLineColor(kMagenta);
343 TLine *l3 = line.DrawLine(0, what ? fgRefPedRmsClosedLids : fgRefPedClosedLids,
344 x, what ? fgRefPedRmsClosedLids : fgRefPedClosedLids);
345
346
347 TLegend *leg = new TLegend(0.4,0.75,0.7,0.99);
348 leg->SetBit(kCanDelete);
349 leg->AddEntry(l1, "Galactic Source","l");
350 leg->AddEntry(l2, "Extra-Galactic Source","l");
351 leg->AddEntry(l3, "Closed Lids","l");
352 leg->Draw();
353}
354
355void MJPedestal::DisplayOutliers(TH1D *hist) const
356{
357 const Float_t mean = hist->GetFunction("gaus")->GetParameter(1);
358 const Float_t lolim = mean - 3.5*hist->GetFunction("gaus")->GetParameter(2);
359 const Float_t uplim = mean + 3.5*hist->GetFunction("gaus")->GetParameter(2);
360 const Stat_t dead = hist->Integral(0,hist->FindBin(lolim)-1);
361 const Stat_t noisy = hist->Integral(hist->FindBin(uplim)+1,hist->GetNbinsX()+1);
362
363 TLatex deadtex;
364 deadtex.SetTextSize(0.06);
365 deadtex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.1,Form("%3i dead pixels",(Int_t)dead));
366
367 TLatex noisytex;
368 noisytex.SetTextSize(0.06);
369 noisytex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.2,Form("%3i noisy pixels",(Int_t)noisy));
370}
371
372Bool_t MJPedestal::WriteResult()
373{
374 if (fOutputPath.IsNull())
375 return kTRUE;
376
377 const TString oname(GetOutputFile());
378
379 *fLog << inf << "Writing to file: " << oname << endl;
380
381 TFile file(oname, "UPDATE");
382
383 if (fDisplay && fDisplay->Write()<=0)
384 {
385 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
386 return kFALSE;
387 }
388
389 if (fPedestalCam.Write()<=0)
390 {
391 *fLog << err << "Unable to write MPedestalCam to " << oname << endl;
392 return kFALSE;
393 }
394
395 if (fBadPixels.Write()<=0)
396 {
397 *fLog << err << "Unable to write MBadPixelsCam to " << oname << endl;
398 return kFALSE;
399 }
400
401 return kTRUE;
402}
403
404void MJPedestal::SetOutputPath(const char *path)
405{
406 fOutputPath = path;
407 if (fOutputPath.EndsWith("/"))
408 fOutputPath = fOutputPath(0, fOutputPath.Length()-1);
409}
410
411void MJPedestal::SetEnv(const char *env)
412{
413 if (fEnv)
414 delete fEnv;
415 fEnv = new TEnv(env);
416}
417
418Bool_t MJPedestal::Process()
419{
420 if (!ReadPedestalCam())
421 return ProcessFile();
422
423 return kTRUE;
424}
425
426Bool_t MJPedestal::ProcessFile()
427{
428 if (!fRuns && !fEnv && !fSequence)
429 {
430 *fLog << err << "Neither AddRuns nor SetSequence nor SetEnv was called... abort." << endl;
431 return kFALSE;
432 }
433 if (!fSequence && fRuns && fRuns->GetNumRuns() != fRuns->GetNumEntries())
434 {
435 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
436 return kFALSE;
437 }
438
439 *fLog << inf;
440 fLog->Separator(GetDescriptor());
441 *fLog << "Calculate MPedestalCam from Runs ";
442 if (fSequence)
443 *fLog << fSequence->GetName() << endl;
444 else
445 if (fRuns)
446 *fLog << fRuns->GetRunsAsString() << endl;
447 else
448 *fLog << "in " << fEnv->GetName() << endl;
449 *fLog << endl;
450
451 MParList plist;
452 MTaskList tlist;
453 plist.AddToList(&tlist);
454
455 MReadMarsFile read("Events");
456 MRawFileRead rawread(NULL);
457
458 MDirIter iter;
459 if (fSequence)
460 fSequence->SetupPedRuns(iter);
461
462 if (fDataCheck)
463 {
464 if (fRuns || fSequence)
465 rawread.AddFiles(fSequence ? iter : *fRuns);
466 tlist.AddToList(&rawread);
467 }
468 else
469 {
470 read.DisableAutoScheme();
471 if (fRuns || fSequence)
472 static_cast<MRead&>(read).AddFiles(fSequence ? iter : *fRuns);
473 tlist.AddToList(&read);
474 }
475 // Enable logging to file
476 //*fLog.SetOutputFile(lname, kTRUE);
477
478 // Setup Tasklist
479 plist.AddToList(&fPedestalCam);
480 plist.AddToList(&fBadPixels);
481
482 MGeomApply geomapl;
483 MBadPixelsMerge merge(&fBadPixels);
484
485 MPedCalcPedRun pedcalc;
486
487 MTaskEnv taskenv("ExtractPedestal");
488 taskenv.SetDefault(&pedcalc);
489
490 if (fExtractor)
491 {
492 pedcalc.SetWindowSize((Int_t)fExtractor->GetNumHiGainSamples());
493 pedcalc.SetRange(fExtractor->GetHiGainFirst(), fExtractor->GetHiGainLast());
494 }
495 else
496 {
497 *fLog << warn << GetDescriptor();
498 *fLog << ": No extractor has been chosen, take default number of FADC samples " << endl;
499 }
500
501 tlist.AddToList(&geomapl);
502 tlist.AddToList(&merge);
503 tlist.AddToList(&taskenv);
504
505 //
506 // Execute the eventloop
507 //
508 MEvtLoop evtloop(fName);
509 evtloop.SetParList(&plist);
510 evtloop.SetDisplay(fDisplay);
511 evtloop.SetLogStream(fLog);
512 if (fEnv)
513 evtloop.ReadEnv(*fEnv);
514
515 // Execute first analysis
516 if (!evtloop.Eventloop())
517 {
518 *fLog << err << GetDescriptor() << ": Failed." << endl;
519 return kFALSE;
520 }
521
522 tlist.PrintStatistics();
523
524 DisplayResult(plist);
525
526 if (!WriteResult())
527 return kFALSE;
528
529 *fLog << inf << GetDescriptor() << ": Done." << endl;
530
531 return kTRUE;
532}
Note: See TracBrowser for help on using the repository browser.