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

Last change on this file since 7043 was 7043, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 39.0 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 1/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19! 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 MTaskEnv tenv("ExtractSignal");
780 tenv.SetDefault(fExtractor);
781
782 if (tenv.ReadEnv(*GetEnv(), GetEnvPrefix()+".ExtractSignal", GetEnvDebug()>2)==kERROR)
783 return kFALSE;
784
785 if (fExtractor==tenv.GetTask())
786 return kTRUE;
787
788 if (!tenv.GetTask()->InheritsFrom(MExtractor::Class()))
789 {
790 *fLog << err << "ERROR: ExtractSignal from resource file doesn't inherit from MExtractor.... abort." << endl;
791 return kFALSE;
792 }
793
794 SetExtractor((MExtractor*)tenv.GetTask());
795
796 fBadPixelsFile = GetEnv("BadPixelsFile",fBadPixelsFile.Data());
797 fReferenceFile = GetEnv("ReferenceFile",fReferenceFile.Data());
798 ReadReferenceFile();
799
800 return kTRUE;
801}
802
803//---------------------------------------------------------------------------------
804//
805// Try to write the created MPedestalCam in the output file.
806// If possible, also an MBadPixelsCam and the corresponding display is written.
807//
808// In case of Storage type "kNoStorage" or if any of the containers
809// cannot be written, return kFALSE, otherwise kTRUE.
810//
811Bool_t MJPedestal::WriteResult()
812{
813 if (IsNoStorage())
814 return kTRUE;
815
816 TObjArray cont;
817
818 cont.Add(&fPedestalCamOut);
819 cont.Add(&fBadPixels);
820
821 return WriteContainer(cont, GetOutputFileName(), fOverwrite?"RECREATE":"NEW");
822}
823
824Bool_t MJPedestal::Process()
825{
826 if (!fSequence.IsValid())
827 {
828 *fLog << err << "ERROR - Sequence invalid..." << endl;
829 return kFALSE;
830 }
831
832 *fLog << inf;
833 fLog->Separator(GetDescriptor());
834 *fLog << "Calculate pedestal from Sequence #";
835 *fLog << fSequence.GetSequence() << endl << endl;
836
837 if (!CheckEnv())
838 return kFALSE;
839
840 // --------------------------------------------------------------------------------
841
842 const TString type = IsUseData() ? "data" : "pedestal";
843
844 *fLog << inf;
845 fLog->Separator(GetDescriptor());
846 *fLog << "Calculate MPedestalCam from " << type << "-runs ";
847 *fLog << fSequence.GetName() << endl;
848 *fLog << endl;
849
850 // --------------------------------------------------------------------------------
851
852 MParList plist;
853 MTaskList tlist;
854 plist.AddToList(&tlist);
855 plist.AddToList(this); // take care of fDisplay!
856
857 MReadMarsFile read("Events");
858 MRawFileRead rawread(NULL);
859
860 MDirIter iter;
861 if (fSequence.IsValid())
862 {
863 const Int_t n0 = IsUseData()
864 ? fSequence.SetupDatRuns(iter, fPathData, IsUseRawData())
865 : fSequence.SetupPedRuns(iter, fPathData, IsUseRawData());
866 const Int_t n1 = IsUseData()
867 ? fSequence.GetNumDatRuns()
868 : fSequence.GetNumPedRuns();
869 if (n0==0)
870 {
871 *fLog << err << "ERROR - No " << type << " input files of sequence found in " << (fPathData.IsNull()?"<default>":fPathData.Data()) << endl;
872 return kFALSE;
873 }
874 if (n0!=n1)
875 {
876 *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;
877 if (fLog->GetDebugLevel()>4)
878 {
879 *fLog << dbg << "Files which are searched:" << endl;
880 iter.Print();
881 }
882 return kFALSE;
883 }
884 }
885
886 if (IsUseRawData())
887 {
888 rawread.AddFiles(iter);
889 tlist.AddToList(&rawread);
890 }
891 else
892 {
893 read.DisableAutoScheme();
894 read.AddFiles(iter);
895 tlist.AddToList(&read);
896 }
897
898 // Setup Tasklist
899 plist.AddToList(&fPedestalCamOut);
900 plist.AddToList(&fBadPixels);
901
902 //
903 // Read bad pixels from outside
904 //
905 if (!fBadPixelsFile.IsNull())
906 {
907 *fLog << inf << "Excluding: " << fBadPixelsFile << endl;
908 ifstream fin(fBadPixelsFile.Data());
909 fBadPixels.AsciiRead((istream&)fin);
910 }
911
912 MGeomApply geomapl;
913 MBadPixelsMerge merge(&fBadPixels);
914
915 MPedCalcPedRun pedcalc;
916 pedcalc.SetPedestalUpdate(kFALSE);
917
918 MPedCalcFromLoGain pedlogain;
919 pedlogain.SetPedestalUpdate(kFALSE);
920
921 MHPedestalCam hpedcam;
922 if (fExtractionType != kFundamental)
923 hpedcam.SetRenorm(kTRUE);
924 // fPedestalHist.SetRenorm(kTRUE);
925 // fPedestalHist.SetPedestalsOut(&fPedestalCamOut);
926 hpedcam.SetPedestalsOut(&fPedestalCamOut);
927
928 MPedestalCam pedinter;
929 pedinter.SetName("MPedestalCamIntermediate");
930
931 MFillH fillped(&hpedcam, "MPedestalCamIntermediate", "FillPedCam");
932 // MFillH fillped(&fPedestalHist, "MPedestalCamIntermediate", "FillPedCam");
933 MFillH fillpul("MHCalibrationPulseTimeCam", "MRawEvtData", "FillPulseTime");
934 fillped.SetBit(MFillH::kDoNotDisplay);
935 fillpul.SetBit(MFillH::kDoNotDisplay);
936
937 tlist.AddToList(&geomapl);
938 tlist.AddToList(&merge);
939
940 if (!fPathIn.IsNull())
941 {
942 fExtractor = ReadCalibration();
943 if (!fExtractor)
944 return kFALSE;
945
946 *fLog << all;
947 *fLog << underline << "Signal Extractor found in calibration file:" << endl;
948 fExtractor->Print();
949 *fLog << endl;
950 }
951
952 // This will make that for data with version less than 5, where
953 // trigger patterns were not yet correct, all the events in the real
954 // data file will be processed. In any case there are no interleaved
955 // calibration events in such data, so this is fine.
956 MTriggerPatternDecode decode;
957 MFTriggerPattern fcalib("CalibFilter");
958 fcalib.SetDefault(kFALSE);
959 fcalib.RequireCalibration();
960 fcalib.SetInverted();
961
962 if (fIsPulsePosCheck)
963 {
964 fillpul.SetFilter(&fcalib);
965 tlist.AddToList(&decode);
966 tlist.AddToList(&fcalib);
967 tlist.AddToList(&fillpul);
968 }
969
970 // ----------------------------------------------------------------------
971 // Now we make sure, that in all cases the ranges are setup correctly
972 // ----------------------------------------------------------------------
973 MTaskEnv taskenv("ExtractPedestal");
974 switch (fExtractType)
975 {
976 case kUsePedRun:
977 // In case other than 'fundamental' second argument is obsolete
978 // pedcalc.SetExtractWindow(0,14); // kUsePedRun (take default from class)
979 taskenv.SetDefault(&pedcalc);
980 tlist.AddToList(&taskenv);
981 break;
982
983 case kUseData:
984 // In case other than 'fundamental' second argument is obsolete
985 // pedlogain.SetExtractWindow(15,14); // kUseData (take default from class)
986 taskenv.SetDefault(&pedlogain);
987 tlist.AddToList(&taskenv);
988 break;
989 }
990
991 if (fIsUseHists)
992 {
993 if (fExtractor->InheritsFrom("MExtractTimeAndCharge"))
994 {
995 if (fExtractionType!=kFundamental)
996 {
997 const MExtractTimeAndCharge &e = *static_cast<MExtractTimeAndCharge*>(fExtractor);
998 hpedcam.SetFitStart(-5*e.GetWindowSizeHiGain());
999 }
1000 else
1001 hpedcam.SetFitStart(10.);
1002 }
1003
1004 pedcalc.SetIntermediateStorage();
1005 pedlogain.SetIntermediateStorage();
1006 plist.AddToList(&pedinter);
1007 plist.AddToList(&hpedcam);
1008 // plist.AddToList(&fPedestalHist);
1009 tlist.AddToList(&fillped);
1010 }
1011
1012 pedcalc.SetPedestalsIn(&fPedestalCamIn);
1013 pedlogain.SetPedestalsIn(&fPedestalCamIn);
1014
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 (fExtractionType!=kFundamental)
1025 {
1026 pedcalc.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1027 pedlogain.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1028
1029 pedcalc.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1030 pedlogain.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1031 }
1032
1033 if (fExtractor->InheritsFrom("MExtractTimeAndCharge"))
1034 {
1035
1036 const Float_t f = 0.1+fExtractor->GetHiGainFirst();
1037 const Int_t win = ((MExtractTimeAndCharge*)fExtractor)->GetWindowSizeHiGain();
1038 pedcalc.SetExtractWindow((Int_t)f, win);
1039 pedlogain.SetExtractWindow((Int_t)(15+f), win);
1040
1041 }
1042 else
1043 {
1044 const Float_t f = 0.1+fExtractor->GetHiGainFirst();
1045 const Float_t n = 0.1+fExtractor->GetNumHiGainSamples();
1046 pedcalc.SetExtractWindow((Int_t)f, (Int_t)n);
1047 pedlogain.SetExtractWindow((Int_t)(15+f), (Int_t)n);
1048
1049 if (fExtractionType!=kFundamental)
1050 {
1051 *fLog << inf;
1052 *fLog << "Signal extractor doesn't inherit from MExtractTimeAndCharge..." << endl;
1053 *fLog << " --> falling back to fundamental pedestal extraction." << endl;
1054 fExtractionType=kFundamental;
1055 }
1056 }
1057
1058
1059 }
1060 else
1061 {
1062 *fLog << warn << GetDescriptor() << ": WARNING - No extractor has been handed over! " << endl;
1063 *fLog << "Taking default window for pedestal extraction. The calculated pedestal RMS" << endl;
1064 *fLog << "will probably not match with future pedestal RMS' from different extraction" << endl;
1065 *fLog << "windows." << endl;
1066 }
1067
1068 /*
1069 switch (fExtractType)
1070 {
1071 case kUseData:
1072 *fLog << all << "TYPE: USEDATA " << fExtractor << endl;
1073 taskenv.SetDefault(&pedlogain);
1074 tlist.AddToList(&taskenv);
1075
1076 if (!SetupExtractor(plist, pedlogain))
1077 {
1078 *fLog << all << "SETTING TO: " << fExtractor << " " << fExtractor->GetNumHiGainSamples() << endl;
1079 fExtractor->Print();
1080 pedlogain.SetExtractWindow(15, (Int_t)TMath::Nint(fExtractor->GetNumHiGainSamples()));
1081 }
1082 break;
1083
1084 case kUsePedRun:
1085 *fLog << all << "TYPE: USEPEDRUN " << fExtractor << endl;
1086 taskenv.SetDefault(&pedcalc);
1087 tlist.AddToList(&taskenv);
1088
1089 if (!SetupExtractor(plist, pedcalc))
1090 pedcalc.SetExtractWindow(fExtractor->GetHiGainFirst(), TMath::Nint(fExtractor->GetNumHiGainSamples()));
1091 break;
1092
1093 case kUseHists:
1094 if (!fExtractor)
1095 {
1096 *fLog << err << GetDescriptor() << " - ERROR: ";
1097 *fLog << "Extraction Type is kUseHists, but no extractor was set" << endl;
1098 return kFALSE;
1099 }
1100
1101 tlist.AddToList(fExtractor);
1102 tlist.AddToList(&fillped);
1103 break;
1104 } */
1105
1106 /*
1107 if (!fPathIn.IsNull())
1108 {
1109 delete fExtractor;
1110 fExtractor = 0;
1111 }
1112 */
1113
1114
1115 //
1116 // Execute the eventloop
1117 //
1118 MEvtLoop evtloop(fName);
1119 evtloop.SetParList(&plist);
1120 evtloop.SetDisplay(fDisplay);
1121 evtloop.SetLogStream(fLog);
1122 if (!SetupEnv(evtloop))
1123 return kFALSE;
1124
1125 // if (!WriteEventloop(evtloop))
1126 // return kFALSE;
1127
1128 // Execute first analysis
1129 if (!evtloop.Eventloop(fMaxEvents))
1130 {
1131 *fLog << err << GetDescriptor() << ": Failed." << endl;
1132 return kFALSE;
1133 }
1134
1135 tlist.PrintStatistics();
1136
1137 if (fIsPixelCheck)
1138 {
1139 MHPedestalCam *hcam = (MHPedestalCam*)plist.FindObject("MHPedestalCam");
1140 if (hcam)
1141 {
1142 MHPedestalPix &pix1 = (MHPedestalPix&)(*hcam)[fCheckedPixId];
1143 pix1.DrawClone("");
1144 }
1145 }
1146
1147 if (fDisplayType!=kDisplayNone)
1148 DisplayResult(plist);
1149
1150 if (!WriteResult())
1151 return kFALSE;
1152
1153 if (fIsPulsePosCheck)
1154 {
1155
1156 Int_t numhigainsamples = 0;
1157 Int_t numlogainsamples = 0;
1158 Float_t meanpulsetime = 0.;
1159 Float_t rmspulsetime = 0.;
1160
1161 if (IsUseMC())
1162 {
1163 //
1164 // FIXME:
1165 // The MC cannot run over the first 2000 pedestal events since almost all
1166 // events are empty, therefore a pulse pos. check is not possible, either.
1167 // For the moment, have to fix the problem hardcoded...
1168 //
1169 // MMcEvt *evt = (MMcEvt*)plist.FindObject("MMcEvt");
1170 // const Float_t meanpulsetime = evt->GetFadcTimeJitter();
1171 meanpulsetime = 4.5;
1172 rmspulsetime = 1.0;
1173
1174 *fLog << all << "Mean pulse time (MC): " << meanpulsetime << "+-" << rmspulsetime << endl;
1175
1176 numhigainsamples = 15;
1177 numlogainsamples = 15;
1178
1179 }
1180 else
1181 {
1182 MHCalibrationPulseTimeCam *hcam = (MHCalibrationPulseTimeCam*)plist.FindObject("MHCalibrationPulseTimeCam");
1183 if (fIsPixelCheck)
1184 {
1185 hcam->DrawClone();
1186 gPad->SaveAs(Form("%s/PulsePosTest_all.root",fPathOut.Data()));
1187 MHCalibrationPix &pix = (*hcam)[fCheckedPixId];
1188 pix.DrawClone();
1189 gPad->SaveAs(Form("%s/PulsePosTest_Pixel%04d.root",fPathOut.Data(),fCheckedPixId));
1190 }
1191
1192 MCalibrationPulseTimeCam *cam = (MCalibrationPulseTimeCam*)plist.FindObject("MCalibrationPulseTimeCam");
1193 if (!cam)
1194 {
1195 *fLog << err << "Could not determine mean pulse position, abort... " << endl;
1196 return kFALSE;
1197 }
1198
1199 meanpulsetime = cam->GetAverageArea(0).GetHiGainMean();
1200 rmspulsetime = cam->GetAverageArea(0).GetHiGainRms();
1201
1202 *fLog << all << "Mean pulse time (cosmics): " << meanpulsetime << "+-" << rmspulsetime << endl;
1203
1204 MRawEvtData *data = (MRawEvtData*)plist.FindObject("MRawEvtData");
1205
1206 numhigainsamples = data->GetNumHiGainSamples();
1207 numlogainsamples = data->GetNumLoGainSamples();
1208 }
1209
1210 //
1211 // Get the ranges for the new extractor setting
1212 //
1213 const Int_t newfirst = TMath::Nint(meanpulsetime-fExtractWinLeft);
1214 Int_t wshigain = fExtractor->InheritsFrom("MExtractTimeAndCharge")
1215 ? ((MExtractTimeAndCharge*)fExtractor)->GetWindowSizeHiGain()
1216 : 6;
1217
1218 if (wshigain > 6)
1219 wshigain = 6;
1220
1221 Int_t wslogain = fExtractor->InheritsFrom("MExtractTimeAndCharge")
1222 ? ((MExtractTimeAndCharge*)fExtractor)->GetWindowSizeLoGain()
1223 : 6;
1224 if (wslogain > 4)
1225 wslogain = 4;
1226
1227 const Int_t newlast = TMath::Nint(meanpulsetime+fExtractWinRight);
1228
1229 *fLog << all << underline
1230 << "Try to set new range limits: ("<<newfirst<<","<<newlast<<"+"<<wshigain
1231 <<","<<newfirst-1<<","<<newlast<<"+"<<wslogain<<")"<<endl;
1232 //
1233 // Check the ranges for the new extractor setting
1234 //
1235 if (newfirst < 0)
1236 {
1237 *fLog << err << "Pulse is too much to the left, cannot go below 0! " << endl;
1238 return kFALSE;
1239 }
1240 if (newlast+wshigain > numhigainsamples+numlogainsamples-1)
1241 {
1242 *fLog << err << "Pulse is too much to the right, cannot go beyond limits: "
1243 << numhigainsamples << "+" << numlogainsamples << "-1" << endl;
1244 *fLog << " Cannot extract at all! ... " << endl;
1245 return kFALSE;
1246 }
1247 if (newlast+wslogain > numlogainsamples)
1248 {
1249 *fLog << err << "Pulse is too much to the right, cannot go beyond logain limits! " << endl;
1250 *fLog << endl;
1251 *fLog << "Try to use a different extractor (e.g. with a window size of only 4 sl.) or:" << endl;
1252 *fLog << "Set the limit to a lower value (callisto.rc: line 329): " << endl;
1253 *fLog << " MJPedestalY2:ExtractWinRight: 4.5 " << endl;
1254 *fLog << "(ATTENTION, you will lose late cosmics pulses!)" << endl;
1255 *fLog << endl;
1256 return kFALSE;
1257 }
1258 //
1259 // Set and store the new ranges
1260 //
1261 fExtractor->SetRange(newfirst,newlast+wshigain,
1262 newfirst>0?newfirst-1:newfirst,numlogainsamples-1);
1263 }
1264
1265
1266 *fLog << all << GetDescriptor() << ": Done." << endl;
1267 *fLog << endl << endl;
1268
1269 return kTRUE;
1270}
Note: See TracBrowser for help on using the repository browser.