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

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