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

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