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

Last change on this file since 4787 was 4760, 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::CheckEnvLocal()
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 kTRUE;
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 CheckEnv();
539
540 // --------------------------------------------------------------------------------
541
542 *fLog << inf;
543 fLog->Separator(GetDescriptor());
544 *fLog << "Calculate MPedestalCam from Runs ";
545 if (fSequence.IsValid())
546 *fLog << fSequence.GetName() << endl;
547 else
548 if (fRuns)
549 *fLog << fRuns->GetRunsAsString() << endl;
550 else
551 *fLog << "in Resource File." << endl;
552 *fLog << endl;
553
554 // --------------------------------------------------------------------------------
555
556 MParList plist;
557 MTaskList tlist;
558 plist.AddToList(&tlist);
559 plist.AddToList(this); // take care of fDisplay!
560
561 MReadMarsFile read("Events");
562 MRawFileRead rawread(NULL);
563
564 MDirIter iter;
565 if (fSequence.IsValid())
566 {
567 const Int_t n0 = fUseData ? fSequence.SetupDatRuns(iter, fPathData) : fSequence.SetupPedRuns(iter, fPathData);
568 const Int_t n1 = fUseData ? fSequence.GetNumDatRuns() : fSequence.GetNumPedRuns();
569 if (n0==0)
570 {
571 *fLog << err << "ERROR - No input files of sequence found!" << endl;
572 return kFALSE;
573 }
574 if (n0!=n1)
575 {
576 *fLog << err << "ERROR - Number of files found (" << n0 << ") doesn't match number of files in sequence (" << n1 << ")" << endl;
577 return kFALSE;
578 }
579 }
580
581 if (fDataCheck)
582 {
583 if (fRuns || fSequence.IsValid())
584 rawread.AddFiles(fSequence.IsValid() ? iter : *fRuns);
585 tlist.AddToList(&rawread);
586 }
587 else
588 {
589 read.DisableAutoScheme();
590 if (fRuns || fSequence.IsValid())
591 read.AddFiles(fSequence.IsValid() ? iter : *fRuns);
592 tlist.AddToList(&read);
593 }
594
595 // Setup Tasklist
596 plist.AddToList(&fPedestalCam);
597 plist.AddToList(&fBadPixels);
598
599 MGeomApply geomapl;
600 MBadPixelsMerge merge(&fBadPixels);
601
602 MPedCalcPedRun pedcalc;
603
604 MTaskEnv taskenv("ExtractPedestal");
605 taskenv.SetDefault(&pedcalc);
606
607 if (fExtractor)
608 {
609 pedcalc.SetWindowSize((Int_t)fExtractor->GetNumHiGainSamples());
610 pedcalc.SetRange(fExtractor->GetHiGainFirst(), fExtractor->GetHiGainLast());
611 }
612 /*
613 else
614 {
615 *fLog << warn << GetDescriptor();
616 *fLog << ": No extractor has been chosen, take default number of FADC samples " << endl;
617 }
618 */
619
620 tlist.AddToList(&geomapl);
621 tlist.AddToList(&merge);
622 tlist.AddToList(&taskenv);
623
624 //
625 // Execute the eventloop
626 //
627 MEvtLoop evtloop(fName);
628 evtloop.SetParList(&plist);
629 evtloop.SetDisplay(fDisplay);
630 evtloop.SetLogStream(fLog);
631 if (!SetupEnv(evtloop))
632 return kFALSE;
633
634 // if (!WriteEventloop(evtloop))
635 // return kFALSE;
636
637 // Execute first analysis
638 if (!evtloop.Eventloop(fMaxEvents))
639 {
640 *fLog << err << GetDescriptor() << ": Failed." << endl;
641 return kFALSE;
642 }
643
644 tlist.PrintStatistics();
645
646 DisplayResult(plist);
647
648 if (!WriteResult())
649 return kFALSE;
650
651 *fLog << all << GetDescriptor() << ": Done." << endl;
652 *fLog << endl << endl;
653
654 return kTRUE;
655}
Note: See TracBrowser for help on using the repository browser.