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

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