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

Last change on this file since 4759 was 4759, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 17.3 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::fgRefPedRmsClosedLidsInner = 1.7;
80const Float_t MJPedestal::fgRefPedRmsExtraGalacticInner = 5.6;
81const Float_t MJPedestal::fgRefPedRmsGalacticInner = 6.92;
82const Float_t MJPedestal::fgRefPedRmsClosedLidsOuter = 1.7;
83const Float_t MJPedestal::fgRefPedRmsExtraGalacticOuter = 3.35;
84const Float_t MJPedestal::fgRefPedRmsGalacticOuter = 4.2;
85// --------------------------------------------------------------------------
86//
87// Default constructor.
88//
89// Sets fRuns to 0, fExtractor to NULL, fDataCheck to kFALSE
90//
91MJPedestal::MJPedestal(const char *name, const char *title)
92 : fRuns(0), fExtractor(NULL), fDisplayType(kNormalDisplay),
93 fDataCheck(kFALSE), fUseData(kFALSE)
94{
95 fName = name ? name : "MJPedestal";
96 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
97}
98
99const char* MJPedestal::GetOutputFile() const
100{
101 if (fSequence.IsValid())
102 return Form("%s/pedest%06d.root", (const char*)fPathOut, fSequence.GetSequence());
103
104 if (!fRuns)
105 return "";
106
107 return Form("%s/%s-F0.root", (const char*)fPathOut, (const char*)fRuns->GetRunsAsFileName());
108}
109
110Bool_t MJPedestal::ReadPedestalCam()
111{
112 const TString fname = GetOutputFile();
113
114 if (gSystem->AccessPathName(fname, kFileExists))
115 {
116 *fLog << warn << "Input file " << fname << " doesn't exist, will create it." << endl;
117 return kFALSE;
118 }
119
120 *fLog << inf << "Reading from file: " << fname << endl;
121
122 TFile file(fname, "READ");
123 if (fPedestalCam.Read()<=0)
124 {
125 *fLog << err << "Unable to read MPedestalCam from " << fname << endl;
126 return kFALSE;
127 }
128
129 if (file.FindKey("MBadPixelsCam"))
130 {
131 MBadPixelsCam bad;
132 if (bad.Read()<=0)
133 {
134 *fLog << err << "Unable to read MBadPixelsCam from " << fname << endl;
135 return kFALSE;
136 }
137 fBadPixels.Merge(bad);
138 }
139
140 if (fDisplay && !fDisplay->GetCanvas("Pedestals"))
141 fDisplay->Read();
142
143 return kTRUE;
144}
145
146void MJPedestal::DisplayResult(MParList &plist)
147{
148 if (!fDisplay)
149 return;
150
151 //
152 // Update display
153 //
154 TString title = fDisplay->GetTitle();
155 title += "-- Pedestal ";
156 if (fSequence.IsValid())
157 title += fSequence.GetName();
158 else
159 if (fRuns) // FIXME: What to do if an environmentfile was used?
160 title += fRuns->GetRunsAsString();
161 title += " --";
162 fDisplay->SetTitle(title);
163
164 //
165 // Get container from list
166 //
167 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
168
169 //
170 // Create container to display
171 //
172 MHCamera disp0(geomcam, "MPedestalCam;ped", "Mean Pedestal");
173 MHCamera disp1(geomcam, "MPedestalCam;RMS", "Pedestal RMS");
174
175 disp0.SetCamContent(fPedestalCam, 0);
176 disp0.SetCamError (fPedestalCam, 1);
177
178 disp1.SetCamContent(fPedestalCam, 2);
179 disp1.SetCamError (fPedestalCam, 3);
180
181 disp0.SetYTitle("Pedestal [counts/slice]");
182 disp1.SetYTitle("RMS [counts/slice]");
183
184 //
185 // Display data
186 //
187 TCanvas &c3 = fDisplay->AddTab("Pedestals");
188 c3.Divide(2,3);
189
190 if (fDisplayType != kDataCheckDisplay)
191 {
192 disp0.CamDraw(c3, 1, 2, 1);
193 disp1.CamDraw(c3, 2, 2, 6);
194 return;
195 }
196
197 c3.cd(1);
198 gPad->SetBorderMode(0);
199 gPad->SetTicks();
200 MHCamera *obj1=(MHCamera*)disp0.DrawCopy("hist");
201 //
202 // for the datacheck, fix the ranges!!
203 //
204 obj1->SetMinimum(fgPedestalMin);
205 obj1->SetMaximum(fgPedestalMax);
206
207 //
208 // Set the datacheck sizes:
209 //
210 FixDataCheckHist((TH1D*)obj1);
211 //
212 // set reference lines
213 //
214 DisplayReferenceLines(obj1,0);
215
216 //
217 // end reference lines
218 //
219 c3.cd(3);
220 gPad->SetBorderMode(0);
221 obj1->SetPrettyPalette();
222 obj1->Draw();
223
224 c3.cd(5);
225 gPad->SetBorderMode(0);
226 gPad->SetTicks();
227 TH1D *obj2 = (TH1D*)obj1->Projection(obj1->GetName());
228 obj2->Draw();
229 obj2->SetBit(kCanDelete);
230 obj2->Fit("gaus","Q");
231 obj2->GetFunction("gaus")->SetLineColor(kYellow);
232 //
233 // Set the datacheck sizes:
234 //
235 FixDataCheckHist(obj2);
236 obj2->SetStats(1);
237
238 c3.cd(2);
239 gPad->SetBorderMode(0);
240 gPad->SetTicks();
241 MHCamera *obj3=(MHCamera*)disp1.DrawCopy("hist");
242 //
243 // for the datacheck, fix the ranges!!
244 //
245 obj3->SetMinimum(fgPedRmsMin);
246 obj3->SetMaximum(fgPedRmsMax);
247 //
248 // Set the datacheck sizes:
249 //
250 FixDataCheckHist((TH1D*)obj3);
251 //
252 // set reference lines
253 //
254 DisplayReferenceLines(obj1,1);
255
256 c3.cd(4);
257 gPad->SetBorderMode(0);
258 obj3->SetPrettyPalette();
259 obj3->Draw();
260
261 c3.cd(6);
262 gPad->SetBorderMode(0);
263
264 if (geomcam.InheritsFrom("MGeomCamMagic"))
265 {
266 TArrayI inner(1);
267 inner[0] = 0;
268
269 TArrayI outer(1);
270 outer[0] = 1;
271
272 TArrayI s0(6);
273 s0[0] = 6;
274 s0[1] = 1;
275 s0[2] = 2;
276 s0[3] = 3;
277 s0[4] = 4;
278 s0[5] = 5;
279
280 TArrayI s1(3);
281 s1[0] = 6;
282 s1[1] = 1;
283 s1[2] = 2;
284
285 TArrayI s2(3);
286 s2[0] = 3;
287 s2[1] = 4;
288 s2[2] = 5;
289
290 TVirtualPad *pad = gPad;
291 pad->Divide(2,1);
292
293 TH1D *inout[2];
294 inout[0] = disp1.ProjectionS(s0, inner, "Inner");
295 inout[1] = disp1.ProjectionS(s0, outer, "Outer");
296 FixDataCheckHist(inout[0]);
297 FixDataCheckHist(inout[1]);
298
299 inout[0]->SetTitle(Form("%s %s",disp1.GetTitle(),"Inner"));
300 inout[1]->SetTitle(Form("%s %s",disp1.GetTitle(),"Outer"));
301
302
303 for (int i=0; i<2; i++)
304 {
305 pad->cd(i+1);
306 gPad->SetBorderMode(0);
307 gPad->SetTicks();
308
309 inout[i]->SetDirectory(NULL);
310 inout[i]->SetLineColor(kRed+i);
311 inout[i]->SetBit(kCanDelete);
312 inout[i]->Draw();
313 inout[i]->Fit("gaus", "Q");
314
315 TLegend *leg2 = new TLegend(0.6,0.2,0.9,0.55);
316 leg2->SetHeader(inout[i]->GetName());
317 leg2->AddEntry(inout[i], inout[i]->GetName(), "l");
318
319 //
320 // Display the outliers as dead and noisy pixels
321 //
322 DisplayOutliers(inout[i]);
323
324 //
325 // Display the two half of the camera separately
326 //
327 TH1D *half[2];
328 half[0] = disp1.ProjectionS(s1, i==0 ? inner : outer , "Sector 6-1-2");
329 half[1] = disp1.ProjectionS(s2, i==0 ? inner : outer , "Sector 3-4-5");
330
331 for (int j=0; j<2; j++)
332 {
333 half[j]->SetLineColor(kRed+i+2*j+1);
334 half[j]->SetDirectory(NULL);
335 half[j]->SetBit(kCanDelete);
336 half[j]->Draw("same");
337 leg2->AddEntry(half[j], half[j]->GetName(), "l");
338 }
339 leg2->Draw();
340 }
341 }
342}
343
344void MJPedestal::DisplayReferenceLines(MHCamera *cam, const Int_t what) const
345{
346
347 Double_t x = cam->GetNbinsX();
348
349 const MGeomCam *geom = cam->GetGeometry();
350
351 if (geom->InheritsFrom("MGeomCamMagic"))
352 x = what ? 397 : cam->GetNbinsX();
353
354 TLine line;
355 line.SetLineStyle(kDashed);
356 line.SetLineWidth(3);
357
358 line.SetLineColor(kBlue);
359 TLine *l1 = line.DrawLine(0, what ? fgRefPedRmsGalacticInner : fgRefPedGalactic,
360 x, what ? fgRefPedRmsGalacticInner : fgRefPedGalactic);
361
362 line.SetLineColor(kYellow);
363 TLine *l2 = line.DrawLine(0, what ? fgRefPedRmsExtraGalacticInner : fgRefPedExtraGalactic,
364 x, what ? fgRefPedRmsExtraGalacticInner : fgRefPedExtraGalactic);
365
366 line.SetLineColor(kMagenta);
367 TLine *l3 = line.DrawLine(0, what ? fgRefPedRmsClosedLidsInner : fgRefPedClosedLids,
368 x, what ? fgRefPedRmsClosedLidsInner : fgRefPedClosedLids);
369
370 if (geom->InheritsFrom("MGeomCamMagic"))
371 if (what)
372 {
373 const Double_t x2 = cam->GetNbinsX();
374
375 line.SetLineColor(kBlue);
376 line.DrawLine(398, fgRefPedRmsGalacticOuter,
377 x2, fgRefPedRmsGalacticOuter);
378
379 line.SetLineColor(kYellow);
380 line.DrawLine(398, fgRefPedRmsExtraGalacticOuter,
381 x2, fgRefPedRmsExtraGalacticOuter);
382
383 line.SetLineColor(kMagenta);
384 line.DrawLine(398, fgRefPedRmsClosedLidsOuter,
385 x2, fgRefPedRmsClosedLidsOuter);
386 }
387
388
389 TLegend *leg = new TLegend(0.4,0.75,0.7,0.99);
390 leg->SetBit(kCanDelete);
391 leg->AddEntry(l1, "Galactic Source","l");
392 leg->AddEntry(l2, "Extra-Galactic Source","l");
393 leg->AddEntry(l3, "Closed Lids","l");
394 leg->Draw();
395}
396
397void MJPedestal::DisplayOutliers(TH1D *hist) const
398{
399 const Float_t mean = hist->GetFunction("gaus")->GetParameter(1);
400 const Float_t lolim = mean - 3.5*hist->GetFunction("gaus")->GetParameter(2);
401 const Float_t uplim = mean + 3.5*hist->GetFunction("gaus")->GetParameter(2);
402 const Stat_t dead = hist->Integral(0,hist->FindBin(lolim)-1);
403 const Stat_t noisy = hist->Integral(hist->FindBin(uplim)+1,hist->GetNbinsX()+1);
404
405 TLatex deadtex;
406 deadtex.SetTextSize(0.06);
407 deadtex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.1,Form("%3i dead pixels",(Int_t)dead));
408
409 TLatex noisytex;
410 noisytex.SetTextSize(0.06);
411 noisytex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.2,Form("%3i noisy pixels",(Int_t)noisy));
412}
413
414void MJPedestal::FixDataCheckHist(TH1D *hist) const
415{
416
417 hist->SetDirectory(NULL);
418 hist->SetStats(0);
419
420 //
421 // set the labels bigger
422 //
423 TAxis *xaxe = hist->GetXaxis();
424 TAxis *yaxe = hist->GetYaxis();
425
426 xaxe->CenterTitle();
427 yaxe->CenterTitle();
428 xaxe->SetTitleSize(0.06);
429 yaxe->SetTitleSize(0.06);
430 xaxe->SetTitleOffset(0.8);
431 yaxe->SetTitleOffset(0.5);
432 xaxe->SetLabelSize(0.05);
433 yaxe->SetLabelSize(0.05);
434
435}
436/*
437Bool_t MJPedestal::WriteEventloop(MEvtLoop &evtloop) const
438{
439 if (fOutputPath.IsNull())
440 return kTRUE;
441
442 const TString oname(GetOutputFile());
443
444 *fLog << inf << "Writing to file: " << oname << endl;
445
446 TFile file(oname, fOverwrite?"RECREATE":"NEW", "File created by MJPedestal", 9);
447 if (!file.IsOpen())
448 {
449 *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
450 return kFALSE;
451 }
452
453 if (evtloop.Write(fName)<=0)
454 {
455 *fLog << err << "Unable to write MEvtloop to " << oname << endl;
456 return kFALSE;
457 }
458
459 return kTRUE;
460}
461*/
462
463Bool_t MJPedestal::CheckEnv()
464{
465 if (HasEnv("DataCheckDisplay"))
466 fDisplayType = GetEnv("DataCheckDisplay", kFALSE) ? kDataCheckDisplay : kNormalDisplay;
467
468 SetDataCheck(GetEnv("DataCheck", fDataCheck));
469 SetOverwrite(GetEnv("Overwrite", fOverwrite));
470
471 return MJob::CheckEnv();
472}
473
474Bool_t MJPedestal::WriteResult()
475{
476 if (fPathOut.IsNull())
477 return kTRUE;
478
479 const TString oname(GetOutputFile());
480
481 *fLog << inf << "Writing to file: " << oname << endl;
482
483 TFile file(oname, "UPDATE", "File created by MJPedestal", 9);
484 if (!file.IsOpen())
485 {
486 *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
487 return kFALSE;
488 }
489
490 if (fDisplay && fDisplay->Write()<=0)
491 {
492 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
493 return kFALSE;
494 }
495
496 if (fPedestalCam.Write()<=0)
497 {
498 *fLog << err << "Unable to write MPedestalCam to " << oname << endl;
499 return kFALSE;
500 }
501
502 if (fBadPixels.Write()<=0)
503 {
504 *fLog << err << "Unable to write MBadPixelsCam to " << oname << endl;
505 return kFALSE;
506 }
507
508 return kTRUE;
509}
510
511Bool_t MJPedestal::Process()
512{
513 if (!ReadPedestalCam())
514 return ProcessFile();
515
516 return kTRUE;
517}
518
519Bool_t MJPedestal::ProcessFile()
520{
521 if (!fSequence.IsValid())
522 {
523 if (!fRuns)
524 {
525 *fLog << err << "Neither AddRuns nor SetSequence nor SetEnv was called... abort." << endl;
526 return kFALSE;
527 }
528 if (fRuns && fRuns->GetNumRuns() != fRuns->GetNumEntries())
529 {
530 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
531 return kFALSE;
532 }
533 }
534
535 //if (!CheckEnv())
536 // return kFALSE;
537
538 if (GetEnv())
539 CheckEnv();
540
541 // --------------------------------------------------------------------------------
542
543 *fLog << inf;
544 fLog->Separator(GetDescriptor());
545 *fLog << "Calculate MPedestalCam from Runs ";
546 if (fSequence.IsValid())
547 *fLog << fSequence.GetName() << endl;
548 else
549 if (fRuns)
550 *fLog << fRuns->GetRunsAsString() << endl;
551 else
552 *fLog << "in Resource File." << endl;
553 *fLog << endl;
554
555 // --------------------------------------------------------------------------------
556
557 MParList plist;
558 MTaskList tlist;
559 plist.AddToList(&tlist);
560 plist.AddToList(this); // take care of fDisplay!
561
562 MReadMarsFile read("Events");
563 MRawFileRead rawread(NULL);
564
565 MDirIter iter;
566 if (fSequence.IsValid())
567 {
568 const Int_t n0 = fUseData ? fSequence.SetupDatRuns(iter, fPathData) : fSequence.SetupPedRuns(iter, fPathData);
569 const Int_t n1 = fUseData ? fSequence.GetNumDatRuns() : fSequence.GetNumPedRuns();
570 if (n0==0)
571 {
572 *fLog << err << "ERROR - No input files of sequence found!" << endl;
573 return kFALSE;
574 }
575 if (n0!=n1)
576 {
577 *fLog << err << "ERROR - Number of files found (" << n0 << ") doesn't match number of files in sequence (" << n1 << ")" << endl;
578 return kFALSE;
579 }
580 }
581
582 if (fDataCheck)
583 {
584 if (fRuns || fSequence.IsValid())
585 rawread.AddFiles(fSequence.IsValid() ? iter : *fRuns);
586 tlist.AddToList(&rawread);
587 }
588 else
589 {
590 read.DisableAutoScheme();
591 if (fRuns || fSequence.IsValid())
592 read.AddFiles(fSequence.IsValid() ? iter : *fRuns);
593 tlist.AddToList(&read);
594 }
595
596 // Setup Tasklist
597 plist.AddToList(&fPedestalCam);
598 plist.AddToList(&fBadPixels);
599
600 MGeomApply geomapl;
601 MBadPixelsMerge merge(&fBadPixels);
602
603 MPedCalcPedRun pedcalc;
604
605 MTaskEnv taskenv("ExtractPedestal");
606 taskenv.SetDefault(&pedcalc);
607
608 if (fExtractor)
609 {
610 pedcalc.SetWindowSize((Int_t)fExtractor->GetNumHiGainSamples());
611 pedcalc.SetRange(fExtractor->GetHiGainFirst(), fExtractor->GetHiGainLast());
612 }
613 /*
614 else
615 {
616 *fLog << warn << GetDescriptor();
617 *fLog << ": No extractor has been chosen, take default number of FADC samples " << endl;
618 }
619 */
620
621 tlist.AddToList(&geomapl);
622 tlist.AddToList(&merge);
623 tlist.AddToList(&taskenv);
624
625 //
626 // Execute the eventloop
627 //
628 MEvtLoop evtloop(fName);
629 evtloop.SetParList(&plist);
630 evtloop.SetDisplay(fDisplay);
631 evtloop.SetLogStream(fLog);
632 if (!SetupEnv(evtloop))
633 return kFALSE;
634
635 // if (!WriteEventloop(evtloop))
636 // return kFALSE;
637
638 // Execute first analysis
639 if (!evtloop.Eventloop(fMaxEvents))
640 {
641 *fLog << err << GetDescriptor() << ": Failed." << endl;
642 return kFALSE;
643 }
644
645 tlist.PrintStatistics();
646
647 DisplayResult(plist);
648
649 if (!WriteResult())
650 return kFALSE;
651
652 *fLog << all << GetDescriptor() << ": Done." << endl;
653 *fLog << endl << endl;
654
655 return kTRUE;
656}
Note: See TracBrowser for help on using the repository browser.