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

Last change on this file since 8245 was 8245, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 38.4 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 = 2.5;
105const Float_t MJPedestal::fgExtractWinRight = 4.5;
106
107// --------------------------------------------------------------------------
108//
109// Default constructor.
110//
111// Sets:
112// - fExtractor to NULL,
113// - fExtractType to kUsePedRun
114// - fStorage to Normal Storage
115// - fExtractorResolution to kFALSE
116//
117MJPedestal::MJPedestal(const char *name, const char *title)
118 : fExtractor(NULL), fDisplayType(kDisplayDataCheck),
119 fExtractType(kUsePedRun), fExtractionType(kFundamental),
120 fIsUseHists(kFALSE), fDeadPixelCheck(kFALSE)
121{
122 fName = name ? name : "MJPedestal";
123 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
124
125 SetUsePedRun();
126 SetPathIn("");
127 SetReferenceFile();
128 SetBadPixelsFile();
129
130 SetExtractWinLeft();
131 SetExtractWinRight();
132 //
133 // Default references for case that no value references file is there
134 // (should not occur)
135 //
136
137 fPedestalMin = 4.;
138 fPedestalMax = 16.;
139 fPedRmsMin = 0.;
140 fPedRmsMax = 20.;
141 fRefPedClosedLids = 9.635;
142 fRefPedExtraGalactic = 9.93;
143 fRefPedGalactic = 10.03;
144 fRefPedRmsClosedLidsInner = 1.7;
145 fRefPedRmsExtraGalacticInner = 5.6;
146 fRefPedRmsGalacticInner = 6.92;
147 fRefPedRmsClosedLidsOuter = 1.7;
148 fRefPedRmsExtraGalacticOuter = 3.35;
149 fRefPedRmsGalacticOuter = 4.2;
150}
151
152MJPedestal::~MJPedestal()
153{
154 if (fExtractor)
155 delete fExtractor;
156}
157
158const char* MJPedestal::GetOutputFileName() const
159{
160 return Form("pedest%08d.root", fSequence.GetSequence());
161}
162
163MExtractor *MJPedestal::ReadCalibration()
164{
165 const TString fname = Form("%s/calib%08d.root", fPathIn.Data(), fSequence.GetSequence());
166
167 *fLog << inf << "Reading extractor from file: " << fname << endl;
168
169 TFile file(fname, "READ");
170 if (!file.IsOpen())
171 {
172 *fLog << err << dbginf << "ERROR - Could not open file " << fname << endl;
173 return NULL;
174 }
175
176 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 Int_t numhigainsamples = 0;
862 Int_t numlogainsamples = 0;
863
864 Float_t meanpulsetime = 0.;
865 Float_t rmspulsetime = 0.;
866
867 if (IsUseMC())
868 {
869 //
870 // FIXME:
871 // The MC cannot run over the first 2000 pedestal events since almost all
872 // events are empty, therefore a pulse pos. check is not possible, either.
873 // For the moment, have to fix the problem hardcoded...
874 //
875 // MMcEvt *evt = (MMcEvt*)plist.FindObject("MMcEvt");
876 // const Float_t meanpulsetime = evt->GetFadcTimeJitter();
877 meanpulsetime = 4.5;
878 rmspulsetime = 1.0;
879
880 numhigainsamples = 15;
881 numlogainsamples = 15;
882
883 }
884 else
885 {
886 if (fIsPixelCheck)
887 {
888 MHCalibrationPulseTimeCam *hcam = (MHCalibrationPulseTimeCam*)plist.FindObject("MHCalibrationPulseTimeCam");
889 if (!hcam)
890 {
891 *fLog << err << "MHCalibrationPulseTimeCam not found... abort." << endl;
892 return kFALSE;
893 }
894 hcam->DrawClone();
895 gPad->SaveAs(Form("%s/PulsePosTest_all.root",fPathOut.Data()));
896
897 MHCalibrationPix &pix = (*hcam)[fCheckedPixId];
898 pix.DrawClone();
899 gPad->SaveAs(Form("%s/PulsePosTest_Pixel%04d.root",fPathOut.Data(),fCheckedPixId));
900 }
901
902 MCalibrationPulseTimeCam *cam = (MCalibrationPulseTimeCam*)plist.FindObject("MCalibrationPulseTimeCam");
903 if (!cam)
904 {
905 *fLog << err << "MCalibrationPulseTimeCam not found... abort." << endl;
906 return kFALSE;
907 }
908
909 meanpulsetime = cam->GetAverageArea(0).GetHiGainMean();
910 rmspulsetime = cam->GetAverageArea(0).GetHiGainRms();
911
912 MRawEvtData *data = (MRawEvtData*)plist.FindObject("MRawEvtData");
913 if (!data)
914 {
915 *fLog << err << "MRawEvtData not found... abort." << endl;
916 return kFALSE;
917 }
918
919 numhigainsamples = data->GetNumHiGainSamples();
920 numlogainsamples = data->GetNumLoGainSamples();
921 }
922
923 *fLog << all << "Mean pulse time (" << (IsUseMC()?"MC":"cosmics") << "): ";
924 *fLog << meanpulsetime << "+-" << rmspulsetime << endl;
925
926 const MExtractTimeAndCharge *ext = dynamic_cast<MExtractTimeAndCharge*>(fExtractor);
927
928 //
929 // Get the ranges for the new extractor setting
930 //
931 const Int_t newfirst = TMath::Nint(meanpulsetime-fExtractWinLeft);
932
933 Int_t wshigain = ext ? ext->GetWindowSizeHiGain() : 6;
934 if (wshigain > 6)
935 wshigain = 6;
936
937 Int_t wslogain = ext ? ext->GetWindowSizeLoGain() : 4;
938 if (wslogain > 4)
939 wslogain = 4;
940
941 const Int_t newlast = TMath::Nint(meanpulsetime+fExtractWinRight);
942
943 *fLog << underline;
944 *fLog << "Try to set new range limits: (" << newfirst << "," << newlast;
945 *fLog << "+" << wshigain << "," << newfirst-1 << "," << newlast << "+";
946 *fLog << wslogain << ")" << endl;
947
948 //
949 // Check the ranges for the new extractor setting
950 //
951 if (newfirst < 0)
952 {
953 //*fLog << err << "Pulse is too much to the left, cannot go below 0!" << endl;
954 //return -1;
955 }
956 if (newlast+wshigain > numhigainsamples+numlogainsamples-1)
957 {
958 *fLog << err << "Pulse is too much to the right, cannot go beyond limits: ";
959 *fLog << numhigainsamples << "+" << numlogainsamples << "-1" << endl;
960 *fLog << " Cannot extract at all!" << endl;
961 return -3;
962 }
963 if (newlast+wslogain > numlogainsamples)
964 {
965 *fLog << err << "Pulse is too much to the right, cannot go beyond logain limits!" << endl;
966 *fLog << endl;
967 *fLog << "Try to use a different extractor (e.g. with a window size of only 4 sl.) or:" << endl;
968 *fLog << "Set the limit to a lower value (callisto.rc):" << endl;
969 *fLog << " MJPedestalY2:ExtractWinRight: 4.5" << endl;
970 *fLog << "(ATTENTION, you will lose late cosmics pulses!)" << endl;
971 *fLog << endl;
972 return -2;
973 }
974
975 return kTRUE;
976/*
977 //
978 // Set and store the new ranges
979 //
980 const Int_t hi0 = newfirst;
981 const Int_t hi1 = newlast+wshigain;
982
983 const Int_t lo0 = newfirst>0 ? newfirst-1 : newfirst;
984 const Int_t lo1 = numlogainsamples-1;
985
986 fExtractor->SetRange(hi0, hi1, lo0, lo1);
987 return kTRUE;*/
988}
989
990Int_t MJPedestal::Process()
991{
992 if (!fSequence.IsValid())
993 {
994 *fLog << err << "ERROR - Sequence invalid..." << endl;
995 return kFALSE;
996 }
997
998 // --------------------------------------------------------------------------------
999
1000 const TString type = IsUseData() ? "data" : "pedestal";
1001
1002 *fLog << inf;
1003 fLog->Separator(GetDescriptor());
1004 *fLog << "Calculate MPedestalCam from " << type << "-runs ";
1005 *fLog << fSequence.GetFileName() << endl;
1006 *fLog << endl;
1007
1008 // --------------------------------------------------------------------------------
1009
1010 if (!CheckEnv())
1011 return kFALSE;
1012
1013 MParList plist;
1014 MTaskList tlist;
1015 plist.AddToList(&tlist);
1016 plist.AddToList(this); // take care of fDisplay!
1017
1018 MReadMarsFile read("Events");
1019 MRawFileRead rawread(NULL);
1020
1021 MDirIter iter;
1022 if (fSequence.IsValid())
1023 {
1024 const Int_t n0 = IsUseData()
1025 ? fSequence.SetupDatRuns(iter, 0, IsUseRawData())
1026 : fSequence.SetupPedRuns(iter, 0, IsUseRawData());
1027
1028 if (n0<=0)
1029 return kFALSE;
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);
1055 fBadPixels.AsciiRead(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))
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 tlist.AddToList(&decode);
1113
1114 if (fIsPulsePosCheck)
1115 {
1116 fillpul.SetFilter(&fcalib);
1117 tlist.AddToList(&fcalib);
1118 tlist.AddToList(&fillpul);
1119 }
1120
1121 // produce pedestal subtracted raw-data
1122 MPedestalSubtract pedsub;
1123 if (fExtractor && fExtractionType!=kFundamental)
1124 pedsub.SetPedestalCam(&fPedestalCamIn);
1125 else
1126 pedsub.SetNamePedestalCam(""); // Only copy hi- and lo-gain together!
1127 tlist.AddToList(&pedsub);
1128
1129 // ----------------------------------------------------------------------
1130 // Now we make sure, that in all cases the ranges are setup correctly
1131 // ----------------------------------------------------------------------
1132 MTaskEnv taskenv("ExtractPedestal");
1133 switch (fExtractType)
1134 {
1135 case kUsePedRun:
1136 // In case other than 'fundamental' second argument is obsolete
1137 // pedcalc.SetExtractWindow(0,14); // kUsePedRun (take default from class)
1138 taskenv.SetDefault(&pedcalc);
1139 tlist.AddToList(&taskenv);
1140 break;
1141
1142 case kUseData:
1143 // In case other than 'fundamental' second argument is obsolete
1144 // pedlogain.SetExtractWindow(15,14); // kUseData (take default from class)
1145 taskenv.SetDefault(&pedlogain);
1146 tlist.AddToList(&taskenv);
1147 break;
1148 }
1149
1150 if (fIsUseHists && fExtractor)
1151 {
1152 if (fExtractor->InheritsFrom("MExtractTimeAndCharge"))
1153 {
1154 if (fExtractionType!=kFundamental)
1155 {
1156 const MExtractTimeAndCharge &e = *static_cast<MExtractTimeAndCharge*>(fExtractor);
1157 hpedcam.SetFitStart(-5*e.GetWindowSizeHiGain());
1158 }
1159 else
1160 hpedcam.SetFitStart(10.);
1161 }
1162
1163 pedcalc.SetIntermediateStorage();
1164 pedlogain.SetIntermediateStorage();
1165 plist.AddToList(&pedinter);
1166 plist.AddToList(&hpedcam);
1167 // plist.AddToList(&fPedestalHist);
1168 tlist.AddToList(&fillped);
1169 }
1170
1171 pedcalc.SetPedestalsIn(&fPedestalCamIn);
1172 pedlogain.SetPedestalsIn(&fPedestalCamIn);
1173
1174 pedcalc.SetPedestalsInter(&pedinter);
1175 pedlogain.SetPedestalsInter(&pedinter);
1176 pedcalc.SetPedestalsOut(&fPedestalCamOut);
1177 pedlogain.SetPedestalsOut(&fPedestalCamOut);
1178
1179 // kFundamental
1180 if (fExtractor)
1181 {
1182 fExtractor->SetPedestals(&fPedestalCamIn);
1183
1184 if (fExtractionType!=kFundamental)
1185 {
1186 pedcalc.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1187 pedlogain.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1188
1189 pedcalc.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1190 pedlogain.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1191 }
1192 else
1193 {
1194 // The window size of the extractor is not yet initialized,
1195 // so we have to stick to the extraction range
1196 const Int_t f = fExtractor->GetHiGainFirst();
1197 const Int_t l = fExtractor->GetHiGainLast();
1198 const Int_t w = (l-f+1)&~1;
1199
1200 // Setup to use the hi-gain extraction window in the lo-gain
1201 // range (the start of the lo-gain range is added automatically
1202 // by MPedCalcFromLoGain)
1203 pedcalc.SetExtractWindow(f, w);
1204 pedlogain.SetExtractWindow(f, w);
1205 }
1206
1207 if (!fExtractor->InheritsFrom("MExtractTimeAndCharge") && fExtractionType!=kFundamental)
1208 {
1209 *fLog << inf;
1210 *fLog << "Signal extractor doesn't inherit from MExtractTimeAndCharge..." << endl;
1211 *fLog << " --> falling back to fundamental pedestal extraction." << endl;
1212 fExtractionType=kFundamental;
1213 }
1214 }
1215 else
1216 {
1217 *fLog << warn << GetDescriptor() << ": WARNING - No extractor has been handed over! " << endl;
1218 *fLog << "Taking default window for pedestal extraction. The calculated pedestal RMS" << endl;
1219 *fLog << "will probably not match with future pedestal RMS' from different extraction" << endl;
1220 *fLog << "windows." << endl;
1221 }
1222
1223 //
1224 // Execute the eventloop
1225 //
1226 MEvtLoop evtloop(fName);
1227 evtloop.SetParList(&plist);
1228 evtloop.SetDisplay(fDisplay);
1229 evtloop.SetLogStream(fLog);
1230 if (!SetupEnv(evtloop))
1231 return kFALSE;
1232
1233 // if (!WriteEventloop(evtloop))
1234 // return kFALSE;
1235
1236 // Execute first analysis
1237 if (!evtloop.Eventloop(fMaxEvents))
1238 {
1239 *fLog << err << GetDescriptor() << ": Failed." << endl;
1240 return kFALSE;
1241 }
1242
1243 if (fDeadPixelCheck)
1244 {
1245 MBadPixelsCalc calc;
1246 calc.SetPedestalLevelVarianceLo(4.5);
1247 calc.SetPedestalLevelVarianceHi();
1248 calc.SetPedestalLevel();
1249 if (!CheckEnv(calc))
1250 return kFALSE;
1251 calc.SetGeomCam(dynamic_cast<MGeomCam*>(plist.FindObject("MGeomCam")));
1252 if (!calc.CheckPedestalRms(fBadPixels, fPedestalCamOut))
1253 {
1254 *fLog << err << "ERROR - MBadPixelsCalc::CheckPedestalRms failed...." << endl;
1255 return kFALSE;
1256 }
1257 }
1258
1259 if (fDisplayType!=kDisplayNone)
1260 DisplayResult(plist);
1261
1262 if (!WriteResult())
1263 return kFALSE;
1264
1265 const Int_t rc = PulsePosCheck(plist);
1266 if (rc<1)
1267 return rc;
1268
1269 *fLog << all << GetDescriptor() << ": Done." << endl;
1270 *fLog << endl << endl;
1271
1272 return kTRUE;
1273}
Note: See TracBrowser for help on using the repository browser.