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

Last change on this file since 4636 was 4616, checked in by gaug, 20 years ago
*** empty log message ***
File size: 15.8 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)
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
443
444Bool_t MJPedestal::WriteResult()
445{
446 if (fOutputPath.IsNull())
447 return kTRUE;
448
449 const TString oname(GetOutputFile());
450
451 *fLog << inf << "Writing to file: " << oname << endl;
452
453 TFile file(oname, "UPDATE");
454
455 if (fDisplay && fDisplay->Write()<=0)
456 {
457 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
458 return kFALSE;
459 }
460
461 if (fPedestalCam.Write()<=0)
462 {
463 *fLog << err << "Unable to write MPedestalCam to " << oname << endl;
464 return kFALSE;
465 }
466
467 if (fBadPixels.Write()<=0)
468 {
469 *fLog << err << "Unable to write MBadPixelsCam to " << oname << endl;
470 return kFALSE;
471 }
472
473 return kTRUE;
474}
475
476void MJPedestal::SetOutputPath(const char *path)
477{
478 fOutputPath = path;
479 if (fOutputPath.EndsWith("/"))
480 fOutputPath = fOutputPath(0, fOutputPath.Length()-1);
481}
482
483void MJPedestal::SetEnv(const char *env)
484{
485 if (fEnv)
486 delete fEnv;
487 fEnv = new TEnv(env);
488}
489
490Bool_t MJPedestal::Process()
491{
492 if (!ReadPedestalCam())
493 return ProcessFile();
494
495 return kTRUE;
496}
497
498void MJPedestal::CheckEnv()
499{
500 if (!fEnv)
501 return;
502
503 TString e1 = fEnv->GetValue("MJPedestal.OutputPath", "");
504 if (!e1.IsNull())
505 {
506 e1.ReplaceAll("\015", "");
507 SetOutputPath(e1);
508 }
509}
510
511Bool_t MJPedestal::ProcessFile()
512{
513 if (!fRuns && !fEnv && !fSequence)
514 {
515 *fLog << err << "Neither AddRuns nor SetSequence nor SetEnv was called... abort." << endl;
516 return kFALSE;
517 }
518 if (!fSequence && fRuns && fRuns->GetNumRuns() != fRuns->GetNumEntries())
519 {
520 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
521 return kFALSE;
522 }
523
524 *fLog << inf;
525 fLog->Separator(GetDescriptor());
526 *fLog << "Calculate MPedestalCam from Runs ";
527 if (fSequence)
528 *fLog << fSequence->GetName() << endl;
529 else
530 if (fRuns)
531 *fLog << fRuns->GetRunsAsString() << endl;
532 else
533 *fLog << "in " << fEnv->GetName() << endl;
534 *fLog << endl;
535
536 CheckEnv();
537
538 MParList plist;
539 MTaskList tlist;
540 plist.AddToList(&tlist);
541
542 MReadMarsFile read("Events");
543 MRawFileRead rawread(NULL);
544
545 MDirIter iter;
546 if (fSequence)
547 fSequence->SetupPedRuns(iter);
548
549 if (fDataCheck)
550 {
551 if (fRuns || fSequence)
552 rawread.AddFiles(fSequence ? iter : *fRuns);
553 tlist.AddToList(&rawread);
554 }
555 else
556 {
557 read.DisableAutoScheme();
558 if (fRuns || fSequence)
559 static_cast<MRead&>(read).AddFiles(fSequence ? iter : *fRuns);
560 tlist.AddToList(&read);
561 }
562 // Enable logging to file
563 //*fLog.SetOutputFile(lname, kTRUE);
564
565 // Setup Tasklist
566 plist.AddToList(&fPedestalCam);
567 plist.AddToList(&fBadPixels);
568
569 MGeomApply geomapl;
570 MBadPixelsMerge merge(&fBadPixels);
571
572 MPedCalcPedRun pedcalc;
573
574 MTaskEnv taskenv("ExtractPedestal");
575 taskenv.SetDefault(&pedcalc);
576
577 if (fExtractor)
578 {
579 pedcalc.SetWindowSize((Int_t)fExtractor->GetNumHiGainSamples());
580 pedcalc.SetRange(fExtractor->GetHiGainFirst(), fExtractor->GetHiGainLast());
581 }
582 /*
583 else
584 {
585 *fLog << warn << GetDescriptor();
586 *fLog << ": No extractor has been chosen, take default number of FADC samples " << endl;
587 }
588 */
589
590 tlist.AddToList(&geomapl);
591 tlist.AddToList(&merge);
592 tlist.AddToList(&taskenv);
593
594 //
595 // Execute the eventloop
596 //
597 MEvtLoop evtloop(fName);
598 evtloop.SetParList(&plist);
599 evtloop.SetDisplay(fDisplay);
600 evtloop.SetLogStream(fLog);
601 if (fEnv)
602 evtloop.ReadEnv(*fEnv);
603
604 // Execute first analysis
605 if (!evtloop.Eventloop())
606 {
607 *fLog << err << GetDescriptor() << ": Failed." << endl;
608 return kFALSE;
609 }
610
611 tlist.PrintStatistics();
612
613 DisplayResult(plist);
614
615 if (!WriteResult())
616 return kFALSE;
617
618 *fLog << inf << GetDescriptor() << ": Done." << endl;
619
620 return kTRUE;
621}
Note: See TracBrowser for help on using the repository browser.