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

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