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

Last change on this file since 6463 was 6340, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 34.1 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! Author(s): Markus Gaug, 4/2004 <mailto:markus@ifae.es>
20!
21! Copyright: MAGIC Software Development, 2000-2005
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MJPedestal
29//
30// Resource file entries are case sensitive!
31//
32/////////////////////////////////////////////////////////////////////////////
33#include "MJPedestal.h"
34
35// root classes
36#include <TF1.h>
37#include <TEnv.h>
38#include <TFile.h>
39#include <TLine.h>
40#include <TLatex.h>
41#include <TString.h>
42#include <TCanvas.h>
43#include <TSystem.h>
44#include <TLegend.h>
45#include <TPad.h>
46#include <TEnv.h>
47#include <TH2F.h>
48
49// mars core
50#include "MLog.h"
51#include "MLogManip.h"
52
53#include "MTaskEnv.h"
54#include "MSequence.h"
55#include "MRunIter.h"
56#include "MParList.h"
57#include "MTaskList.h"
58#include "MEvtLoop.h"
59
60#include "MStatusDisplay.h"
61
62// Other basic classes
63#include "MExtractTimeAndCharge.h"
64#include "MFEventSelector.h"
65
66// parameter containers
67#include "MGeomCam.h"
68#include "MHCamera.h"
69#include "MPedestalPix.h"
70
71#include "MCalibrationPedCam.h"
72#include "MCalibrationPix.h"
73#include "MHPedestalPix.h"
74
75// tasks
76#include "MReadMarsFile.h"
77#include "MRawFileRead.h"
78#include "MGeomApply.h"
79#include "MBadPixelsMerge.h"
80#include "MFillH.h"
81#include "MPedCalcPedRun.h"
82#include "MPedCalcFromLoGain.h"
83
84#include <fstream>
85
86ClassImp(MJPedestal);
87
88using namespace std;
89
90const TString MJPedestal::fgReferenceFile = "mjobs/pedestalref.rc";
91const TString MJPedestal::fgBadPixelsFile = "mjobs/badpixels_0_559.rc";
92// --------------------------------------------------------------------------
93//
94// Default constructor.
95//
96// Sets:
97// - fExtractor to NULL,
98// - fExtractType to kUsePedRun
99// - fStorage to Normal Storage
100// - fExtractorResolution to kFALSE
101//
102MJPedestal::MJPedestal(const char *name, const char *title)
103 : fExtractor(NULL), fDisplayType(kDisplayDataCheck),
104 fExtractType(kUsePedRun), fExtractionType(kFundamental), fIsUseHists(kFALSE)
105{
106 fName = name ? name : "MJPedestal";
107 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
108
109 SetNormalStorage();
110 SetUsePedRun();
111 SetPathIn("");
112 SetReferenceFile();
113 SetBadPixelsFile();
114
115 //
116 // Default references for case that no value references file is there
117 // (should not occur)
118 //
119
120 fPedestalMin = 4.;
121 fPedestalMax = 16.;
122 fPedRmsMin = 0.;
123 fPedRmsMax = 20.;
124 fRefPedClosedLids = 9.635;
125 fRefPedExtraGalactic = 9.93;
126 fRefPedGalactic = 10.03;
127 fRefPedRmsClosedLidsInner = 1.7;
128 fRefPedRmsExtraGalacticInner = 5.6;
129 fRefPedRmsGalacticInner = 6.92;
130 fRefPedRmsClosedLidsOuter = 1.7;
131 fRefPedRmsExtraGalacticOuter = 3.35;
132 fRefPedRmsGalacticOuter = 4.2;
133}
134
135MJPedestal::~MJPedestal()
136{
137 if (fExtractor)
138 delete fExtractor;
139}
140
141const char* MJPedestal::GetOutputFile() const
142{
143 const TString name(GetOutputFileName());
144 if (name.IsNull())
145 return "";
146
147 return Form("%s/%s", fPathOut.Data(), name.Data());
148}
149
150const char* MJPedestal::GetOutputFileName() const
151{
152
153 if (fSequence.IsValid())
154 return Form("pedest%08d.root", fSequence.GetSequence());
155
156 if (!fRuns)
157 return "";
158
159 return Form("%s-F0.root", (const char*)fRuns->GetRunsAsFileName());
160}
161
162//---------------------------------------------------------------------------------
163//
164// Try to read an existing MPedestalCam from a previously created output file.
165// If found, also an MBadPixelsCam and the corresponding display is read.
166//
167// In case of Storage type "kNoStorage" or if the file is not found or the
168// MPedestalCam cannot be read, return kFALSE, otherwise kTRUE.
169//
170Bool_t MJPedestal::ReadPedestalCam()
171{
172 const TString fname = GetOutputFile();
173
174 *fLog << inf << "Reading pedestals from file: " << fname << endl;
175
176 TFile file(fname, "READ");
177 if (fPedestalCamIn.Read()<=0)
178 {
179 *fLog << err << "Unable to read incoming MPedestalCam from " << fname << endl;
180 return kFALSE;
181 }
182
183 if (fPedestalCamOut.Read()<=0)
184 {
185 *fLog << err << "Unable to read outgoing MPedestalCam from " << fname << endl;
186 return kFALSE;
187 }
188
189 if (file.FindKey("MBadPixelsCam"))
190 {
191 MBadPixelsCam bad;
192 if (bad.Read()<=0)
193 {
194 *fLog << err << "Unable to read MBadPixelsCam from " << fname << endl;
195 return kFALSE;
196 }
197 fBadPixels.Merge(bad);
198 }
199
200 if (fDisplay && !fDisplay->GetCanvas("Pedestals"))
201 fDisplay->Read();
202
203 return kTRUE;
204}
205
206MExtractor *MJPedestal::ReadCalibration() const
207{
208 const TString fname = Form("%s/calib%08d.root",fPathIn.Data(), fSequence.GetSequence());
209
210 *fLog << inf << "Reading extractor from file: " << fname << endl;
211
212 TFile file(fname, "READ");
213 if (!file.IsOpen())
214 {
215 *fLog << err << dbginf << "ERROR - Could not open file " << fname << endl;
216 return NULL;
217 }
218
219 TObject *o = file.Get("ExtractSignal");
220 if (o && !o->InheritsFrom(MExtractor::Class()))
221 {
222 *fLog << err << dbginf << "ERROR - ExtractSignal read from " << fname << " doesn't inherit from MExtractor!" << endl;
223 return NULL;
224 }
225
226 return o ? (MExtractor*)o->Clone("ExtractSignal") : NULL;
227}
228
229//---------------------------------------------------------------------------------
230//
231// Display the results.
232// If Display type "kDataCheck" was chosen, also the reference lines are displayed.
233//
234void MJPedestal::DisplayResult(MParList &plist)
235{
236 if (!fDisplay)
237 return;
238
239 //
240 // Update display
241 //
242 TString title = fDisplay->GetTitle();
243 title += "-- Pedestal ";
244 if (fSequence.IsValid())
245 title += fSequence.GetName();
246 else
247 if (fRuns) // FIXME: What to do if an environmentfile was used?
248 title += fRuns->GetRunsAsString();
249 title += " --";
250 fDisplay->SetTitle(title);
251
252 //
253 // Get container from list
254 //
255 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
256 MCalibrationPedCam &calpedcam = *(MCalibrationPedCam*)plist.FindObject("MCalibrationPedCam");
257
258 //
259 // Create container to display
260 //
261 MHCamera disp0 (geomcam, "MPedestalCam;ped", "Mean Pedestal");
262 MHCamera disp1 (geomcam, "MPedestalCam;RMS", "Pedestal RMS");
263 MHCamera disp2 (geomcam, "MCalibPedCam;histmean", "Mean Pedestal (Hist.)");
264 MHCamera disp3 (geomcam, "MCalibPedCam;histsigma", "Pedestal RMS (Hist.)");
265 MHCamera disp4 (geomcam, "MCalibPedCam;ped", "Mean Pedestal");
266 MHCamera disp5 (geomcam, "MCalibPedCam;RMS", "Pedestal RMS");
267 MHCamera disp6 (geomcam, "MCalibDiffCam;ped", "Diff. Mean Pedestal (Hist.)");
268 MHCamera disp7 (geomcam, "MCalibDiffCam;RMS", "Diff. Pedestal RMS (Hist.)");
269 MHCamera disp8 (geomcam, "MCalibDiffCam;ped", "Diff. Mean Pedestal");
270 MHCamera disp9 (geomcam, "MCalibDiffCam;AbsRMS", "Diff. Abs. Pedestal RMS");
271 MHCamera disp10(geomcam, "MCalibDiffCam;RelRMS", "Diff. Rel. Pedestal RMS");
272
273 disp0.SetCamContent(fPedestalCamOut, 0);
274 disp0.SetCamError (fPedestalCamOut, 1);
275
276 disp1.SetCamContent(fPedestalCamOut, 2);
277 disp1.SetCamError (fPedestalCamOut, 3);
278
279 if (fIsUseHists)
280 {
281 disp2.SetCamContent(calpedcam, 0);
282 disp2.SetCamError (calpedcam, 1);
283
284 disp3.SetCamContent(calpedcam, 2);
285 disp3.SetCamError (calpedcam, 3);
286
287 disp4.SetCamContent(calpedcam, 5);
288 disp4.SetCamError (calpedcam, 6);
289
290 disp5.SetCamContent(calpedcam, 7);
291 disp5.SetCamError (calpedcam, 8);
292
293 for (UInt_t i=0;i<geomcam.GetNumPixels();i++)
294 {
295
296 MPedestalPix &ped = fPedestalCamOut[i];
297 MCalibrationPix &hist = calpedcam [i];
298 MBadPixelsPix &bad = fBadPixels[i];
299
300 if (bad.IsUnsuitable())
301 continue;
302
303 disp6.Fill(i,ped.GetPedestal()-hist.GetHiGainMean());
304 disp6.SetUsed(i);
305
306 disp7.Fill(i,hist.GetHiGainSigma()-ped.GetPedestalRms());
307 if (TMath::Abs(ped.GetPedestalRms()-hist.GetHiGainSigma()) < 4.0)
308 disp7.SetUsed(i);
309
310 disp8.Fill(i,ped.GetPedestal()-hist.GetLoGainMean());
311 disp8.SetUsed(i);
312
313 disp9.Fill(i,hist.GetLoGainSigma()-ped.GetPedestalRms());
314 if (TMath::Abs(hist.GetLoGainSigma() - ped.GetPedestalRms()) < 4.0)
315 disp9.SetUsed(i);
316 }
317 }
318
319 if (fExtractionType!=kFundamental/*fExtractorResolution*/)
320 {
321 for (UInt_t i=0;i<geomcam.GetNumPixels();i++)
322 {
323
324 MPedestalPix &pedo = fPedestalCamOut[i];
325 MPedestalPix &pedi = fPedestalCamIn[i];
326 MBadPixelsPix &bad = fBadPixels[i];
327
328 if (bad.IsUnsuitable())
329 continue;
330
331 const Float_t diff = pedo.GetPedestalRms()-pedi.GetPedestalRms();
332 const Float_t sum = 0.5*(pedo.GetPedestalRms()+pedi.GetPedestalRms());
333
334 disp9.Fill(i,pedo.GetPedestalRms()-pedi.GetPedestalRms());
335 if (pedo.IsValid() && pedi.IsValid())
336 disp9.SetUsed(i);
337
338 disp10.Fill(i,sum == 0. ? 0. : diff/sum);
339 if (pedo.IsValid() && pedi.IsValid() && sum != 0.)
340 disp10.SetUsed(i);
341 }
342 }
343
344 disp0.SetYTitle("P [cts/slice]");
345 disp1.SetYTitle("P_{rms} [cts/slice]");
346 disp2.SetYTitle("Hist. Mean [cts/slice]");
347 disp3.SetYTitle("Hist. Sigma [cts/slice]");
348 disp4.SetYTitle("Calc. Mean [cts/slice]");
349 disp5.SetYTitle("Calc. RMS [cts/slice]");
350 disp6.SetYTitle("Diff. Mean [cts/slice]");
351 disp7.SetYTitle("Diff. RMS [cts/slice]");
352 disp8.SetYTitle("Diff. Mean [cts/slice]");
353 disp9.SetYTitle("Abs.Diff.RMS [cts/slice]");
354 disp10.SetYTitle("Rel.Diff.RMS [1]");
355
356 //
357 // Display data
358 //
359 if (fDisplayType != kDisplayDataCheck && !fIsUseHists && fExtractionType==kFundamental/*fExtractorResolution*/)
360 {
361 TCanvas &c3 = fDisplay->AddTab("Pedestals");
362 c3.Divide(2,3);
363
364 disp0.CamDraw(c3, 1, 2, 1);
365 disp1.CamDraw(c3, 2, 2, 6);
366 return;
367 }
368
369 if (fIsUseHists)
370 {
371
372 TCanvas &c3 = fDisplay->AddTab("Extractor Hist.");
373 c3.Divide(2,3);
374
375 disp2.CamDraw(c3, 1, 2, 1);
376 disp3.CamDraw(c3, 2, 2, 5);
377
378 TCanvas &c4 = fDisplay->AddTab("Extractor Calc.");
379 c4.Divide(2,3);
380
381 disp4.CamDraw(c4, 1, 2, 1);
382 disp5.CamDraw(c4, 2, 2, 5);
383
384
385 TCanvas &c5 = fDisplay->AddTab("Difference Hist.");
386 c5.Divide(2,3);
387
388 disp6.CamDraw(c5, 1, 2, 1);
389 disp7.CamDraw(c5, 2, 2, 5);
390
391 TCanvas &c6 = fDisplay->AddTab("Difference Calc.");
392 c6.Divide(2,3);
393
394 disp8.CamDraw(c6, 1, 2, 1);
395 disp9.CamDraw(c6, 2, 2, 5);
396 return;
397 }
398
399 if (fDisplayType == kDisplayDataCheck)
400 {
401
402 TCanvas &c3 = fDisplay->AddTab(fExtractionType!=kFundamental/*fExtractorResolution*/ ? "PedExtrd" : "Ped");
403 c3.Divide(2,3);
404
405 c3.cd(1);
406 gPad->SetBorderMode(0);
407 gPad->SetTicks();
408 MHCamera *obj1=(MHCamera*)disp0.DrawCopy("hist");
409 //
410 // for the datacheck, fix the ranges!!
411 //
412 if (fExtractionType==kFundamental/*!fExtractorResolution*/)
413 {
414 obj1->SetMinimum(fPedestalMin);
415 obj1->SetMaximum(fPedestalMax);
416 }
417 //
418 // Set the datacheck sizes:
419 //
420 FixDataCheckHist((TH1D*)obj1);
421 //
422 // set reference lines
423 //
424 DisplayReferenceLines(obj1,0);
425 //
426 // end reference lines
427 //
428 c3.cd(3);
429 gPad->SetBorderMode(0);
430 obj1->SetPrettyPalette();
431 obj1->Draw();
432
433 c3.cd(5);
434 gPad->SetBorderMode(0);
435 gPad->SetTicks();
436 TH1D *obj2 = (TH1D*)obj1->Projection(obj1->GetName());
437 obj2->Draw();
438 obj2->SetBit(kCanDelete);
439 obj2->Fit("gaus","Q");
440 obj2->GetFunction("gaus")->SetLineColor(kYellow);
441 //
442 // Set the datacheck sizes:
443 //
444 FixDataCheckHist(obj2);
445 obj2->SetStats(1);
446
447 c3.cd(2);
448 gPad->SetBorderMode(0);
449 gPad->SetTicks();
450 MHCamera *obj3=(MHCamera*)disp1.DrawCopy("hist");
451 //
452 // for the datacheck, fix the ranges!!
453 //
454 obj3->SetMinimum(fPedRmsMin);
455 obj3->SetMaximum(fPedRmsMax);
456 //
457 // Set the datacheck sizes:
458 //
459 FixDataCheckHist((TH1D*)obj3);
460 //
461 // set reference lines
462 //
463 DisplayReferenceLines(obj3,1);
464
465 c3.cd(4);
466 gPad->SetBorderMode(0);
467 obj3->SetPrettyPalette();
468 obj3->Draw();
469
470 c3.cd(6);
471 gPad->SetBorderMode(0);
472
473 if (geomcam.InheritsFrom("MGeomCamMagic"))
474 {
475 TArrayI inner(1);
476 inner[0] = 0;
477
478 TArrayI outer(1);
479 outer[0] = 1;
480
481 TArrayI s0(6);
482 s0[0] = 6;
483 s0[1] = 1;
484 s0[2] = 2;
485 s0[3] = 3;
486 s0[4] = 4;
487 s0[5] = 5;
488
489 TArrayI s1(3);
490 s1[0] = 6;
491 s1[1] = 1;
492 s1[2] = 2;
493
494 TArrayI s2(3);
495 s2[0] = 3;
496 s2[1] = 4;
497 s2[2] = 5;
498
499 TVirtualPad *pad = gPad;
500 pad->Divide(2,1);
501
502 TH1D *inout[2];
503 inout[0] = disp1.ProjectionS(s0, inner, "Inner");
504 inout[1] = disp1.ProjectionS(s0, outer, "Outer");
505 FixDataCheckHist(inout[0]);
506 FixDataCheckHist(inout[1]);
507
508 inout[0]->SetTitle(Form("%s %s",disp1.GetTitle(),"Inner"));
509 inout[1]->SetTitle(Form("%s %s",disp1.GetTitle(),"Outer"));
510
511
512 for (int i=0; i<2; i++)
513 {
514 pad->cd(i+1);
515 gPad->SetBorderMode(0);
516 gPad->SetTicks();
517
518 inout[i]->SetDirectory(NULL);
519 inout[i]->SetLineColor(kRed+i);
520 inout[i]->SetBit(kCanDelete);
521 inout[i]->Draw();
522 inout[i]->Fit("gaus", "Q");
523
524 TLegend *leg2 = new TLegend(0.6,0.2,0.9,0.55);
525 leg2->SetHeader(inout[i]->GetName());
526 leg2->AddEntry(inout[i], inout[i]->GetName(), "l");
527
528 //
529 // Display the outliers as dead and noisy pixels
530 //
531 DisplayOutliers(inout[i]);
532
533 //
534 // Display the two half of the camera separately
535 //
536 TH1D *half[2];
537 half[0] = disp1.ProjectionS(s1, i==0 ? inner : outer , "Sector 6-1-2");
538 half[1] = disp1.ProjectionS(s2, i==0 ? inner : outer , "Sector 3-4-5");
539
540 for (int j=0; j<2; j++)
541 {
542 half[j]->SetLineColor(kRed+i+2*j+1);
543 half[j]->SetDirectory(NULL);
544 half[j]->SetBit(kCanDelete);
545 half[j]->Draw("same");
546 leg2->AddEntry(half[j], half[j]->GetName(), "l");
547 }
548 leg2->Draw();
549 delete leg2;
550 }
551 return;
552 }
553 }
554
555 if (fExtractionType!=kFundamental/*fExtractorResolution*/)
556 {
557
558 TCanvas &c3 = fDisplay->AddTab(fExtractionType==kWithExtractor?"PedExtrd":"PedRndm");
559 c3.Divide(2,3);
560
561 disp0.CamDraw(c3, 1, 2, 1);
562 disp1.CamDraw(c3, 2, 2, 6);
563
564 TCanvas &c13 = fDisplay->AddTab(fExtractionType==kWithExtractor?"DiffExtrd":"DiffRndm");
565 c13.Divide(2,3);
566
567 disp9.CamDraw(c13, 1, 2, 5);
568 disp10.CamDraw(c13, 2, 2, 5);
569 return;
570 }
571}
572
573
574void MJPedestal::DisplayReferenceLines(MHCamera *cam, const Int_t what) const
575{
576
577 Double_t x = cam->GetNbinsX();
578
579 const MGeomCam *geom = cam->GetGeometry();
580
581 if (geom->InheritsFrom("MGeomCamMagic"))
582 x = what ? 397 : cam->GetNbinsX();
583
584 TLine line;
585 line.SetLineStyle(kDashed);
586 line.SetLineWidth(3);
587 line.SetLineColor(kBlue);
588
589 TLegend *leg = new TLegend(0.6,0.75,0.9,0.99);
590 leg->SetBit(kCanDelete);
591
592 if (fExtractionType==kWithExtractorRndm && !(what))
593 {
594 TLine *l0 = line.DrawLine(0,0.,cam->GetNbinsX(),0.);
595 l0->SetBit(kCanDelete);
596 leg->AddEntry(l0, "Reference","l");
597 leg->Draw();
598 return;
599 }
600
601 line.SetLineColor(kBlue);
602 TLine *l1 = line.DrawLine(0, what ? fRefPedRmsGalacticInner : fRefPedGalactic,
603 x, what ? fRefPedRmsGalacticInner : fRefPedGalactic);
604 l1->SetBit(kCanDelete);
605 line.SetLineColor(kYellow);
606 TLine *l2 = line.DrawLine(0, what ? fRefPedRmsExtraGalacticInner : fRefPedExtraGalactic,
607 x, what ? fRefPedRmsExtraGalacticInner : fRefPedExtraGalactic);
608 l2->SetBit(kCanDelete);
609 line.SetLineColor(kMagenta);
610 TLine *l3 = line.DrawLine(0, what ? fRefPedRmsClosedLidsInner : fRefPedClosedLids,
611 x, what ? fRefPedRmsClosedLidsInner : fRefPedClosedLids);
612 l3->SetBit(kCanDelete);
613
614 if (geom->InheritsFrom("MGeomCamMagic"))
615 if (what)
616 {
617 const Double_t x2 = cam->GetNbinsX();
618
619 line.SetLineColor(kBlue);
620 line.DrawLine(398, fRefPedRmsGalacticOuter,
621 x2, fRefPedRmsGalacticOuter);
622
623 line.SetLineColor(kYellow);
624 line.DrawLine(398, fRefPedRmsExtraGalacticOuter,
625 x2, fRefPedRmsExtraGalacticOuter);
626
627 line.SetLineColor(kMagenta);
628 line.DrawLine(398, fRefPedRmsClosedLidsOuter,
629 x2, fRefPedRmsClosedLidsOuter);
630 }
631
632
633 leg->AddEntry(l1, "Galactic Source","l");
634 leg->AddEntry(l2, "Extra-Galactic Source","l");
635 leg->AddEntry(l3, "Closed Lids","l");
636 leg->Draw();
637}
638
639void MJPedestal::DisplayOutliers(TH1D *hist) const
640{
641 const Float_t mean = hist->GetFunction("gaus")->GetParameter(1);
642 const Float_t lolim = mean - 3.5*hist->GetFunction("gaus")->GetParameter(2);
643 const Float_t uplim = mean + 3.5*hist->GetFunction("gaus")->GetParameter(2);
644 const Stat_t dead = hist->Integral(0,hist->FindBin(lolim)-1);
645 const Stat_t noisy = hist->Integral(hist->FindBin(uplim)+1,hist->GetNbinsX()+1);
646
647 TLatex deadtex;
648 deadtex.SetTextSize(0.06);
649 deadtex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.1,Form("%3i dead pixels",(Int_t)dead));
650
651 TLatex noisytex;
652 noisytex.SetTextSize(0.06);
653 noisytex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.2,Form("%3i noisy pixels",(Int_t)noisy));
654}
655
656void MJPedestal::FixDataCheckHist(TH1D *hist) const
657{
658 hist->SetDirectory(NULL);
659 hist->SetStats(0);
660
661 //
662 // set the labels bigger
663 //
664 TAxis *xaxe = hist->GetXaxis();
665 TAxis *yaxe = hist->GetYaxis();
666
667 xaxe->CenterTitle();
668 yaxe->CenterTitle();
669 xaxe->SetTitleSize(0.06);
670 yaxe->SetTitleSize(0.06);
671 xaxe->SetTitleOffset(0.8);
672 yaxe->SetTitleOffset(0.5);
673 xaxe->SetLabelSize(0.05);
674 yaxe->SetLabelSize(0.05);
675}
676
677/*
678Bool_t MJPedestal::WriteEventloop(MEvtLoop &evtloop) const
679{
680 if (fOutputPath.IsNull())
681 return kTRUE;
682
683 const TString oname(GetOutputFile());
684
685 *fLog << inf << "Writing to file: " << oname << endl;
686
687 TFile file(oname, fOverwrite?"RECREATE":"NEW", "File created by MJPedestal", 9);
688 if (!file.IsOpen())
689 {
690 *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
691 return kFALSE;
692 }
693
694 if (evtloop.Write(fName)<=0)
695 {
696 *fLog << err << "Unable to write MEvtloop to " << oname << endl;
697 return kFALSE;
698 }
699
700 return kTRUE;
701}
702*/
703
704void MJPedestal::SetExtractor(MExtractor* ext)
705{
706 if (ext)
707 {
708 if (fExtractor)
709 delete fExtractor;
710 fExtractor = ext ? (MExtractor*)ext->Clone(ext->GetName()) : NULL;
711 }
712 else
713 fExtractor = 0;
714}
715
716// --------------------------------------------------------------------------
717//
718// Read the following values from resource file:
719//
720// PedestalMin
721// PedestalMax
722//
723// PedRmsMin
724// PedRmsMax
725//
726// RefPedClosedLids
727// RefPedExtraGalactic
728// RefPedGalactic
729//
730// RefPedRmsClosedLidsInner
731// RefPedRmsExtraGalacticInner
732// RefPedRmsGalacticInner
733// RefPedRmsClosedLidsOuter
734// RefPedRmsExtraGalacticOuter
735// RefPedRmsGalacticOuter
736//
737void MJPedestal::ReadReferenceFile()
738{
739 TEnv refenv(fReferenceFile);
740
741 fPedestalMin = refenv.GetValue("PedestalMin",fPedestalMin);
742 fPedestalMax = refenv.GetValue("PedestalMax",fPedestalMax);
743 fPedRmsMin = refenv.GetValue("PedRmsMin",fPedRmsMin);
744 fPedRmsMax = refenv.GetValue("PedRmsMax",fPedRmsMax);
745 fRefPedClosedLids = refenv.GetValue("RefPedClosedLids",fRefPedClosedLids);
746 fRefPedExtraGalactic = refenv.GetValue("RefPedExtraGalactic",fRefPedExtraGalactic);
747 fRefPedGalactic = refenv.GetValue("RefPedGalactic",fRefPedGalactic);
748 fRefPedRmsClosedLidsInner = refenv.GetValue("RefPedRmsClosedLidsInner",fRefPedRmsClosedLidsInner);
749 fRefPedRmsExtraGalacticInner = refenv.GetValue("RefPedRmsExtraGalacticInner",fRefPedRmsExtraGalacticInner);
750 fRefPedRmsGalacticInner = refenv.GetValue("RefPedRmsGalacticInner",fRefPedRmsGalacticInner);
751 fRefPedRmsClosedLidsOuter = refenv.GetValue("RefPedRmsClosedLidsOuter",fRefPedRmsClosedLidsOuter);
752 fRefPedRmsExtraGalacticOuter = refenv.GetValue("RefPedRmsExtraGalacticOuter",fRefPedRmsExtraGalacticOuter);
753 fRefPedRmsGalacticOuter = refenv.GetValue("RefPedRmsGalacticOuter",fRefPedRmsGalacticOuter);
754}
755
756// --------------------------------------------------------------------------
757//
758// The following resource options are available:
759//
760// Do a datacheck run (read raw-data and enable display)
761// Prefix.DataCheck: Yes, No <default>
762//
763// Setup display type
764// Prefix.Display: normal <default>, datacheck, none
765//
766// Use cosmic data instead of pedestal data (DatRuns)
767// Prefix.UseData: Yes, No <default>
768//
769// Write an output file with pedestals and status-display
770// Prefix.DisableOutput: Yes, No <default>
771//
772// Name of a file containing reference values (see ReadReferenceFile)
773// Prefix.ReferenceFile: filename
774// (see ReadReferenceFile)
775//
776Bool_t MJPedestal::CheckEnvLocal()
777{
778 if (HasEnv("Display"))
779 {
780 TString type = GetEnv("Display", "normal");
781 type.ToLower();
782 if (type==(TString)"normal")
783 fDisplayType = kDisplayNormal;
784 if (type==(TString)"datacheck")
785 fDisplayType = kDisplayDataCheck;
786 if (type==(TString)"none")
787 fDisplayType = kDisplayNone;
788 }
789
790 if (!MJCalib::CheckEnvLocal())
791 return kFALSE;
792
793 if (HasEnv("UseData"))
794 fExtractType = GetEnv("UseData",kFALSE) ? kUseData : kUsePedRun;
795
796 if (HasEnv("UseHists"))
797 if (GetEnv("UseHists",kFALSE))
798 fIsUseHists = kTRUE;
799
800 SetNoStorage(GetEnv("DisableOutput", IsNoStorage()));
801
802 MTaskEnv tenv("ExtractSignal");
803 tenv.SetDefault(fExtractor);
804
805 if (tenv.ReadEnv(*GetEnv(), GetEnvPrefix()+".ExtractSignal", GetEnvDebug())==kERROR)
806 return kFALSE;
807
808 if (fExtractor==tenv.GetTask())
809 return kTRUE;
810
811 if (!tenv.GetTask()->InheritsFrom(MExtractor::Class()))
812 {
813 *fLog << err << "ERROR: ExtractSignal from resource file doesn't inherit from MExtractor.... abort." << endl;
814 return kFALSE;
815 }
816
817 SetExtractor((MExtractor*)tenv.GetTask());
818
819 fBadPixelsFile = GetEnv("BadPixelsFile",fBadPixelsFile.Data());
820 fReferenceFile = GetEnv("ReferenceFile",fReferenceFile.Data());
821 ReadReferenceFile();
822
823 return kTRUE;
824}
825
826//---------------------------------------------------------------------------------
827//
828// Try to write the created MPedestalCam in the output file.
829// If possible, also an MBadPixelsCam and the corresponding display is written.
830//
831// In case of Storage type "kNoStorage" or if any of the containers
832// cannot be written, return kFALSE, otherwise kTRUE.
833//
834Bool_t MJPedestal::WriteResult()
835{
836 if (IsNoStorage())
837 return kTRUE;
838
839 TObjArray cont;
840
841 cont.Add(&fPedestalCamOut);
842 cont.Add(&fBadPixels);
843
844 return WriteContainer(cont, GetOutputFileName(), fOverwrite?"RECREATE":"NEW");
845}
846
847Bool_t MJPedestal::Process()
848{
849 if (!ReadPedestalCam())
850 return ProcessFile();
851
852 return kTRUE;
853}
854
855Bool_t MJPedestal::ProcessFile()
856{
857 if (!fSequence.IsValid())
858 {
859 if (!fRuns)
860 {
861 *fLog << err << "Neither AddRuns nor SetSequence nor SetEnv was called... abort." << endl;
862 return kFALSE;
863 }
864 if (fRuns && fRuns->GetNumRuns() != fRuns->GetNumEntries())
865 {
866 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
867 return kFALSE;
868 }
869 }
870
871 if (!CheckEnv())
872 return kFALSE;
873
874 // --------------------------------------------------------------------------------
875
876 const TString type = IsUseData() ? "data" : "pedestal";
877
878 *fLog << inf;
879 fLog->Separator(GetDescriptor());
880 *fLog << "Calculate MPedestalCam from " << type << "-runs ";
881 if (fSequence.IsValid())
882 *fLog << fSequence.GetName() << endl;
883 else
884 if (fRuns)
885 *fLog << fRuns->GetRunsAsString() << endl;
886 else
887 *fLog << "in Resource File." << endl;
888 *fLog << endl;
889
890 // --------------------------------------------------------------------------------
891
892 MParList plist;
893 MTaskList tlist;
894 plist.AddToList(&tlist);
895 plist.AddToList(this); // take care of fDisplay!
896
897 MReadMarsFile read("Events");
898 MRawFileRead rawread(NULL);
899
900 MDirIter iter;
901 if (fSequence.IsValid())
902 {
903 const Int_t n0 = IsUseData()
904 ? fSequence.SetupDatRuns(iter, fPathData, "D", IsUseRawData())
905 : fSequence.SetupPedRuns(iter, fPathData, "P", IsUseRawData());
906 const Int_t n1 = IsUseData()
907 ? fSequence.GetNumDatRuns()
908 : fSequence.GetNumPedRuns();
909 if (n0==0)
910 {
911 *fLog << err << "ERROR - No " << type << " input files of sequence found in " << (fPathData.IsNull()?"<default>":fPathData.Data()) << endl;
912 return kFALSE;
913 }
914 if (n0!=n1)
915 {
916 *fLog << err << "ERROR - Number of " << type << " files found (" << n0 << ") in " << (fPathData.IsNull()?"<default>":fPathData.Data()) << " doesn't match number of files in sequence (" << n1 << ")" << endl;
917 return kFALSE;
918 }
919 }
920
921 if (IsUseRawData())
922 {
923 if (fRuns || fSequence.IsValid())
924 rawread.AddFiles(fSequence.IsValid() ? iter : *fRuns);
925 tlist.AddToList(&rawread);
926 }
927 else
928 {
929 read.DisableAutoScheme();
930 if (fRuns || fSequence.IsValid())
931 read.AddFiles(fSequence.IsValid() ? iter : *fRuns);
932 tlist.AddToList(&read);
933 }
934
935 // Setup Tasklist
936 plist.AddToList(&fPedestalCamOut);
937 plist.AddToList(&fBadPixels);
938
939 //
940 // Read bad pixels from outside
941 //
942 if (!fBadPixelsFile.IsNull())
943 {
944 *fLog << inf << "Excluding: " << fBadPixelsFile << endl;
945 ifstream fin(fBadPixelsFile.Data());
946 fBadPixels.AsciiRead((istream&)fin);
947 }
948
949 MGeomApply geomapl;
950 MBadPixelsMerge merge(&fBadPixels);
951
952 MPedCalcPedRun pedcalc;
953 pedcalc.SetPedestalUpdate(kFALSE);
954
955 MPedCalcFromLoGain pedlogain;
956 pedlogain.SetPedestalUpdate(kFALSE);
957
958 MHPedestalCam hpedcam;
959 // fPedestalHist.SetRenorm(kTRUE);
960
961 MPedestalCam pedinter;
962 pedinter.SetName("MPedestalCamIntermediate");
963
964 MFillH fillped(&hpedcam, "MPedestalCamIntermediate", "FillPedCam");
965 // MFillH fillped(&fPedestalHist, "MPedestalCamIntermediate", "FillPedCam");
966 // fillped.SetBit(MFillH::kDoNotDisplay);
967
968 tlist.AddToList(&geomapl);
969 tlist.AddToList(&merge);
970
971 if (!fPathIn.IsNull())
972 {
973 fExtractor = ReadCalibration();
974 if (!fExtractor)
975 return kFALSE;
976
977 *fLog << all;
978 *fLog << underline << "Signal Extractor found in calibration file:" << endl;
979 fExtractor->Print();
980 *fLog << endl;
981 }
982
983 MFEventSelector sel;
984 sel.SetNumSelectEvts(0);
985
986 if (fExtractionType!=kFundamental)
987 {
988 fExtractor->SetFilter(&sel);
989 tlist.AddToList(&sel);
990 tlist.AddToList(fExtractor);
991 }
992
993 // ----------------------------------------------------------------------
994 // Now we make sure, that in all cases the ranges are setup correctly
995 // ----------------------------------------------------------------------
996 MTaskEnv taskenv("ExtractPedestal");
997 switch (fExtractType)
998 {
999 case kUsePedRun:
1000 // In case other than 'fundamental' second argument is obsolete
1001 // pedcalc.SetExtractWindow(0,14); // kUsePedRun (take default from class)
1002 taskenv.SetDefault(&pedcalc);
1003 tlist.AddToList(&taskenv);
1004 break;
1005
1006 case kUseData:
1007 // In case other than 'fundamental' second argument is obsolete
1008 // pedlogain.SetExtractWindow(15,14); // kUseData (take default from class)
1009 taskenv.SetDefault(&pedlogain);
1010 tlist.AddToList(&taskenv);
1011 break;
1012 }
1013
1014 if (fIsUseHists)
1015 {
1016 pedcalc.SetIntermediateStorage();
1017 pedlogain.SetIntermediateStorage();
1018 plist.AddToList(&pedinter);
1019 plist.AddToList(&hpedcam);
1020 // plist.AddToList(&fPedestalHist);
1021 tlist.AddToList(&fillped);
1022 }
1023
1024 pedcalc.SetPedestalsIn(&fPedestalCamIn);
1025 pedlogain.SetPedestalsIn(&fPedestalCamIn);
1026 pedcalc.SetPedestalsInter(&pedinter);
1027 pedlogain.SetPedestalsInter(&pedinter);
1028 pedcalc.SetPedestalsOut(&fPedestalCamOut);
1029 pedlogain.SetPedestalsOut(&fPedestalCamOut);
1030
1031 // kFundamental
1032 if (fExtractor)
1033 {
1034
1035 if (fExtractor->InheritsFrom("MExtractTimeAndCharge"))
1036 {
1037
1038 const Float_t f = 0.5+fExtractor->GetHiGainFirst();
1039 const Int_t win = ((MExtractTimeAndCharge*)fExtractor)->GetWindowSizeHiGain();
1040 pedcalc.SetExtractWindow((Int_t)f, win);
1041 pedlogain.SetExtractWindow((Int_t)(15+f), win);
1042
1043 }
1044 else
1045 {
1046 const Float_t f = 0.5+fExtractor->GetHiGainFirst();
1047 const Float_t n = 0.5+fExtractor->GetNumHiGainSamples();
1048 pedcalc.SetExtractWindow((Int_t)f, (Int_t)n);
1049 pedlogain.SetExtractWindow((Int_t)(15+f), (Int_t)n);
1050
1051 if (fExtractionType!=kFundamental)
1052 {
1053 *fLog << inf;
1054 *fLog << "Signal extractor doesn't inherit from MExtractTimeAndCharge..." << endl;
1055 *fLog << " --> falling back to fundamental pedestal extraction." << endl;
1056 fExtractionType=kFundamental;
1057 }
1058 }
1059
1060
1061 if (fExtractionType!=kFundamental)
1062 {
1063 pedcalc.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1064 pedlogain.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1065
1066 pedcalc.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1067 pedlogain.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1068 }
1069 }
1070 else
1071 {
1072 *fLog << warn << GetDescriptor() << ": WARNING - No extractor has been handed over! " << endl;
1073 *fLog << "Taking default window for pedestal extraction. The calculated pedestal RMS" << endl;
1074 *fLog << "will probably not match with future pedestal RMS' from different extraction" << endl;
1075 *fLog << "windows." << endl;
1076 }
1077
1078 /*
1079 switch (fExtractType)
1080 {
1081 case kUseData:
1082 *fLog << all << "TYPE: USEDATA " << fExtractor << endl;
1083 taskenv.SetDefault(&pedlogain);
1084 tlist.AddToList(&taskenv);
1085
1086 if (!SetupExtractor(plist, pedlogain))
1087 {
1088 *fLog << all << "SETTING TO: " << fExtractor << " " << fExtractor->GetNumHiGainSamples() << endl;
1089 fExtractor->Print();
1090 pedlogain.SetExtractWindow(15, (Int_t)TMath::Nint(fExtractor->GetNumHiGainSamples()));
1091 }
1092 break;
1093
1094 case kUsePedRun:
1095 *fLog << all << "TYPE: USEPEDRUN " << fExtractor << endl;
1096 taskenv.SetDefault(&pedcalc);
1097 tlist.AddToList(&taskenv);
1098
1099 if (!SetupExtractor(plist, pedcalc))
1100 pedcalc.SetExtractWindow(fExtractor->GetHiGainFirst(), TMath::Nint(fExtractor->GetNumHiGainSamples()));
1101 break;
1102
1103 case kUseHists:
1104 if (!fExtractor)
1105 {
1106 *fLog << err << GetDescriptor() << " - ERROR: ";
1107 *fLog << "Extraction Type is kUseHists, but no extractor was set" << endl;
1108 return kFALSE;
1109 }
1110
1111 tlist.AddToList(fExtractor);
1112 tlist.AddToList(&fillped);
1113 break;
1114 } */
1115
1116 /*
1117 if (!fPathIn.IsNull())
1118 {
1119 delete fExtractor;
1120 fExtractor = 0;
1121 }
1122 */
1123 //
1124 // Execute the eventloop
1125 //
1126 MEvtLoop evtloop(fName);
1127 evtloop.SetParList(&plist);
1128 evtloop.SetDisplay(fDisplay);
1129 evtloop.SetLogStream(fLog);
1130 if (!SetupEnv(evtloop))
1131 return kFALSE;
1132
1133 // if (!WriteEventloop(evtloop))
1134 // return kFALSE;
1135
1136 // Execute first analysis
1137 if (!evtloop.Eventloop(fMaxEvents))
1138 {
1139 *fLog << err << GetDescriptor() << ": Failed." << endl;
1140 return kFALSE;
1141 }
1142
1143 tlist.PrintStatistics();
1144
1145 if (fDisplayType!=kDisplayNone)
1146 DisplayResult(plist);
1147
1148 if (fExtractionType!=kFundamental)
1149 fExtractor->SetFilter(0);
1150
1151 if (!WriteResult())
1152 return kFALSE;
1153
1154 *fLog << all << GetDescriptor() << ": Done." << endl;
1155 *fLog << endl << endl;
1156
1157 return kTRUE;
1158}
Note: See TracBrowser for help on using the repository browser.