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

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