source: tags/Mars-V0.9.3/mjobs/MJPedestal.cc

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