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

Last change on this file since 4651 was 4641, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 16.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), 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
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 fMaxEvents = fEnv->GetValue("MJPedestal.MaxEvents", fMaxEvents);
511 fUseData = fEnv->GetValue("MJPedestal.UseData", fUseData);
512}
513
514Bool_t MJPedestal::ProcessFile()
515{
516 if (!fRuns && !fEnv && !fSequence)
517 {
518 *fLog << err << "Neither AddRuns nor SetSequence nor SetEnv was called... abort." << endl;
519 return kFALSE;
520 }
521 if (!fSequence && fRuns && fRuns->GetNumRuns() != fRuns->GetNumEntries())
522 {
523 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
524 return kFALSE;
525 }
526
527 *fLog << inf;
528 fLog->Separator(GetDescriptor());
529 *fLog << "Calculate MPedestalCam from Runs ";
530 if (fSequence)
531 *fLog << fSequence->GetName() << endl;
532 else
533 if (fRuns)
534 *fLog << fRuns->GetRunsAsString() << endl;
535 else
536 *fLog << "in " << fEnv->GetName() << endl;
537 *fLog << endl;
538
539 CheckEnv();
540
541 MParList plist;
542 MTaskList tlist;
543 plist.AddToList(&tlist);
544
545 MReadMarsFile read("Events");
546 MRawFileRead rawread(NULL);
547
548 MDirIter iter;
549 if (fSequence)
550 fUseData ? fSequence->SetupDatRuns(iter) : fSequence->SetupPedRuns(iter);
551
552 if (fDataCheck)
553 {
554 if (fRuns || fSequence)
555 rawread.AddFiles(fSequence ? iter : *fRuns);
556 tlist.AddToList(&rawread);
557 }
558 else
559 {
560 read.DisableAutoScheme();
561 if (fRuns || fSequence)
562 static_cast<MRead&>(read).AddFiles(fSequence ? iter : *fRuns);
563 tlist.AddToList(&read);
564 }
565 // Enable logging to file
566 //*fLog.SetOutputFile(lname, kTRUE);
567
568 // Setup Tasklist
569 plist.AddToList(&fPedestalCam);
570 plist.AddToList(&fBadPixels);
571
572 MGeomApply geomapl;
573 MBadPixelsMerge merge(&fBadPixels);
574
575 MPedCalcPedRun pedcalc;
576
577 MTaskEnv taskenv("ExtractPedestal");
578 taskenv.SetDefault(&pedcalc);
579
580 if (fExtractor)
581 {
582 pedcalc.SetWindowSize((Int_t)fExtractor->GetNumHiGainSamples());
583 pedcalc.SetRange(fExtractor->GetHiGainFirst(), fExtractor->GetHiGainLast());
584 }
585 /*
586 else
587 {
588 *fLog << warn << GetDescriptor();
589 *fLog << ": No extractor has been chosen, take default number of FADC samples " << endl;
590 }
591 */
592
593 tlist.AddToList(&geomapl);
594 tlist.AddToList(&merge);
595 tlist.AddToList(&taskenv);
596
597 //
598 // Execute the eventloop
599 //
600 MEvtLoop evtloop(fName);
601 evtloop.SetParList(&plist);
602 evtloop.SetDisplay(fDisplay);
603 evtloop.SetLogStream(fLog);
604 if (fEnv)
605 evtloop.ReadEnv(*fEnv);
606
607 // Execute first analysis
608 if (!evtloop.Eventloop(fMaxEvents))
609 {
610 *fLog << err << GetDescriptor() << ": Failed." << endl;
611 return kFALSE;
612 }
613
614 tlist.PrintStatistics();
615
616 DisplayResult(plist);
617
618 if (!WriteResult())
619 return kFALSE;
620
621 *fLog << inf << GetDescriptor() << ": Done." << endl;
622
623 return kTRUE;
624}
Note: See TracBrowser for help on using the repository browser.