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

Last change on this file since 5028 was 4895, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 17.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::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
463// --------------------------------------------------------------------------
464//
465// The following resource options are available:
466// Prefix.DataCheckDisplay: Yes, No
467// Prefix.DataCheck: Yes, No
468// Prefix.UseData: Yes, No
469//
470Bool_t MJPedestal::CheckEnvLocal()
471{
472 if (HasEnv("DataCheckDisplay"))
473 fDisplayType = GetEnv("DataCheckDisplay", kFALSE) ? kDataCheckDisplay : kNormalDisplay;
474
475 SetDataCheck(GetEnv("DataCheck", fDataCheck));
476 SetUseData(GetEnv("UseData", fUseData));
477
478 return kTRUE;
479}
480
481Bool_t MJPedestal::WriteResult()
482{
483 if (fPathOut.IsNull())
484 return kTRUE;
485
486 const TString oname(GetOutputFile());
487
488 *fLog << inf << "Writing to file: " << oname << endl;
489
490 TFile file(oname, "UPDATE", "File created by MJPedestal", 9);
491 if (!file.IsOpen())
492 {
493 *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
494 return kFALSE;
495 }
496
497 if (fDisplay && fDisplay->Write()<=0)
498 {
499 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
500 return kFALSE;
501 }
502
503 if (fPedestalCam.Write()<=0)
504 {
505 *fLog << err << "Unable to write MPedestalCam to " << oname << endl;
506 return kFALSE;
507 }
508
509 if (fBadPixels.Write()<=0)
510 {
511 *fLog << err << "Unable to write MBadPixelsCam to " << oname << endl;
512 return kFALSE;
513 }
514
515 return kTRUE;
516}
517
518Bool_t MJPedestal::Process()
519{
520 if (!ReadPedestalCam())
521 return ProcessFile();
522
523 return kTRUE;
524}
525
526Bool_t MJPedestal::ProcessFile()
527{
528 if (!fSequence.IsValid())
529 {
530 if (!fRuns)
531 {
532 *fLog << err << "Neither AddRuns nor SetSequence nor SetEnv was called... abort." << endl;
533 return kFALSE;
534 }
535 if (fRuns && fRuns->GetNumRuns() != fRuns->GetNumEntries())
536 {
537 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
538 return kFALSE;
539 }
540 }
541
542 //if (!CheckEnv())
543 // return kFALSE;
544
545 CheckEnv();
546
547 // --------------------------------------------------------------------------------
548
549 *fLog << inf;
550 fLog->Separator(GetDescriptor());
551 *fLog << "Calculate MPedestalCam from Runs ";
552 if (fSequence.IsValid())
553 *fLog << fSequence.GetName() << endl;
554 else
555 if (fRuns)
556 *fLog << fRuns->GetRunsAsString() << endl;
557 else
558 *fLog << "in Resource File." << endl;
559 *fLog << endl;
560
561 // --------------------------------------------------------------------------------
562
563 MParList plist;
564 MTaskList tlist;
565 plist.AddToList(&tlist);
566 plist.AddToList(this); // take care of fDisplay!
567
568 MReadMarsFile read("Events");
569 MRawFileRead rawread(NULL);
570
571 MDirIter iter;
572 if (fSequence.IsValid())
573 {
574 const TString type = fUseData ? "data" : "pedestal";
575 const Int_t n0 = fUseData ? fSequence.SetupDatRuns(iter, fPathData) : fSequence.SetupPedRuns(iter, fPathData);
576 const Int_t n1 = fUseData ? fSequence.GetNumDatRuns() : fSequence.GetNumPedRuns();
577 if (n0==0)
578 {
579 *fLog << err << "ERROR - No " << type << " input files of sequence found in " << fPathData << endl;
580 return kFALSE;
581 }
582 if (n0!=n1)
583 {
584 *fLog << err << "ERROR - Number of " << type << " files found (" << n0 << ") in " << fPathData << " doesn't match number of files in sequence (" << n1 << ")" << endl;
585 return kFALSE;
586 }
587 }
588
589 if (fDataCheck)
590 {
591 if (fRuns || fSequence.IsValid())
592 rawread.AddFiles(fSequence.IsValid() ? iter : *fRuns);
593 tlist.AddToList(&rawread);
594 }
595 else
596 {
597 read.DisableAutoScheme();
598 if (fRuns || fSequence.IsValid())
599 read.AddFiles(fSequence.IsValid() ? iter : *fRuns);
600 tlist.AddToList(&read);
601 }
602
603 // Setup Tasklist
604 plist.AddToList(&fPedestalCam);
605 plist.AddToList(&fBadPixels);
606
607 MGeomApply geomapl;
608 MBadPixelsMerge merge(&fBadPixels);
609
610 MPedCalcPedRun pedcalc;
611
612 MTaskEnv taskenv("ExtractPedestal");
613 taskenv.SetDefault(&pedcalc);
614
615 if (fExtractor)
616 {
617 pedcalc.SetWindowSize((Int_t)fExtractor->GetNumHiGainSamples());
618 pedcalc.SetRange(fExtractor->GetHiGainFirst(), fExtractor->GetHiGainLast());
619 }
620 /*
621 else
622 {
623 *fLog << warn << GetDescriptor();
624 *fLog << ": No extractor has been chosen, take default number of FADC samples " << endl;
625 }
626 */
627
628 tlist.AddToList(&geomapl);
629 tlist.AddToList(&merge);
630 tlist.AddToList(&taskenv);
631
632 //
633 // Execute the eventloop
634 //
635 MEvtLoop evtloop(fName);
636 evtloop.SetParList(&plist);
637 evtloop.SetDisplay(fDisplay);
638 evtloop.SetLogStream(fLog);
639 if (!SetupEnv(evtloop))
640 return kFALSE;
641
642 // if (!WriteEventloop(evtloop))
643 // return kFALSE;
644
645 // Execute first analysis
646 if (!evtloop.Eventloop(fMaxEvents))
647 {
648 *fLog << err << GetDescriptor() << ": Failed." << endl;
649 return kFALSE;
650 }
651
652 tlist.PrintStatistics();
653
654 DisplayResult(plist);
655
656 if (!WriteResult())
657 return kFALSE;
658
659 *fLog << all << GetDescriptor() << ": Done." << endl;
660 *fLog << endl << endl;
661
662 return kTRUE;
663}
Note: See TracBrowser for help on using the repository browser.