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

Last change on this file since 8646 was 8582, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 38.3 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 "MCalibrationPix.h"
74#include "MHPedestalPix.h"
75#include "MHCalibrationPulseTimeCam.h"
76#include "MCalibrationPulseTimeCam.h"
77
78// tasks
79#include "MReadMarsFile.h"
80#include "MRawFileRead.h"
81#include "MRawEvtData.h"
82#include "MGeomApply.h"
83#include "MPedestalSubtract.h"
84#include "MTriggerPatternDecode.h"
85#include "MBadPixelsMerge.h"
86#include "MFillH.h"
87#include "MPedCalcPedRun.h"
88#include "MPedCalcFromLoGain.h"
89#include "MFTriggerPattern.h"
90#include "MBadPixelsCalc.h"
91
92#include "MPedPhotCam.h"
93#include "MPedPhotPix.h"
94#include "MPedestalCam.h"
95#include "MPedestalPix.h"
96#include "MPedestalSubtract.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(const 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.GetFileName();
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 (fSequence.IsMonteCarlo() && 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//
827Bool_t MJPedestal::WritePulsePos(TObject *obj) const
828{
829 if (IsNoStorage())
830 return kTRUE;
831
832 const TString name(Form("signal%08d.root", fSequence.GetSequence()));
833
834 TObjArray arr;
835 arr.Add(obj);
836 return WriteContainer(arr, name, fOverwrite?"RECREATE":"NEW");
837}
838
839Int_t MJPedestal::PulsePosCheck(const MParList &plist) const
840{
841 if (fIsPixelCheck)
842 {
843 MHPedestalCam *hcam = (MHPedestalCam*)plist.FindObject("MHPedestalCam");
844 if (hcam)
845 {
846 MHPedestalPix &pix1 = (MHPedestalPix&)(*hcam)[fCheckedPixId];
847 pix1.DrawClone("");
848 }
849 }
850
851 if (!fIsPulsePosCheck)
852 return kTRUE;
853
854 // FIXME:
855 // The MC cannot run over the first 2000 pedestal events since almost all
856 // events are empty, therefore a pulse pos. check is not possible, either.
857 // For the moment, have to fix the problem hardcoded...
858 //
859 // MMcEvt *evt = (MMcEvt*)plist.FindObject("MMcEvt");
860 // const Float_t meanpulsetime = evt->GetFadcTimeJitter();
861 Float_t meanpulsetime = 4.5;
862 Float_t rmspulsetime = 1.0;
863
864 MCalibrationPulseTimeCam *cam = NULL;
865 if (!fSequence.IsMonteCarlo())
866 {
867 if (fIsPixelCheck)
868 {
869 MHCalibrationPulseTimeCam *hcam = (MHCalibrationPulseTimeCam*)plist.FindObject("MHCalibrationPulseTimeCam");
870 if (!hcam)
871 {
872 *fLog << err << "MHCalibrationPulseTimeCam not found... abort." << endl;
873 return kFALSE;
874 }
875 hcam->DrawClone();
876 gPad->SaveAs(Form("%s/PulsePosTest_all.root",fPathOut.Data()));
877
878 MHCalibrationPix &pix = (*hcam)[fCheckedPixId];
879 pix.DrawClone();
880 gPad->SaveAs(Form("%s/PulsePosTest_Pixel%04d.root",fPathOut.Data(),fCheckedPixId));
881 }
882
883 cam = (MCalibrationPulseTimeCam*)plist.FindObject("MCalibrationPulseTimeCam");
884 if (!cam)
885 {
886 *fLog << err << "MCalibrationPulseTimeCam not found... abort." << endl;
887 return kFALSE;
888 }
889
890 meanpulsetime = cam->GetAverageArea(0).GetHiGainMean();
891 rmspulsetime = cam->GetAverageArea(0).GetHiGainRms();
892 }
893
894 if (!WritePulsePos(cam))
895 return kFALSE;
896
897 *fLog << all << "Mean pulse time/Avg pos.of maximum (" << (fSequence.IsMonteCarlo()?"MC":"cosmics") << "): ";
898 *fLog << meanpulsetime << "+-" << rmspulsetime << endl;
899
900 MExtractTimeAndCharge *ext = dynamic_cast<MExtractTimeAndCharge*>(fExtractor);
901 if (!ext)
902 {
903 *fLog << warn << "WARNING - no extractor found inheriting from MExtractTimeAndCharge... no pulse position check." << endl;
904 return kTRUE;
905 }
906
907 const Int_t hi0 = ext->GetHiGainFirst();
908 const Int_t lo1 = ext->GetLoGainLast();
909 Int_t hi1 = ext->GetHiGainLast();
910 Int_t lo0 = ext->GetLoGainFirst();
911
912 //
913 // This is for data without lo-gains
914 //
915 const Bool_t haslo = ext->HasLoGain();
916
917 //
918 // Get the ranges for the new extractor setting. The window
919 // size is always rounded to the next higher integer.
920 //
921 const Int_t wshigain = ext->GetWindowSizeHiGain();
922 const Int_t wslogain = ext->GetWindowSizeLoGain();
923
924 //
925 // Here we calculate the end of the lo-gain range
926 // as it is done in MExtractTimeAndCharge
927 //
928 const Double_t poshi = meanpulsetime;
929 const Double_t poslo = poshi + ext->GetOffsetLoGain();
930 const Double_t poslo2 = poslo + ext->GetLoGainStartShift();
931
932 //
933 // Do the right side checks range checks
934 //
935 if (poshi+wshigain+fExtractWinRight > hi1-0.5)
936 {
937 *fLog << err;
938 *fLog << "ERROR - Pulse is too much to the right, out of hi-gain range [";
939 *fLog << hi0 << "," << hi1 << "]" << endl;
940 *fLog << endl;
941 return -2;
942 }
943
944 if (haslo && poslo+wslogain+fExtractWinRight > lo1-0.5)
945 {
946 *fLog << err;
947 *fLog << "ERROR - Pulse is too much to the right, out of lo-gain range [";
948 *fLog << lo0 << "," << lo1 << "]" << endl;
949 return -2;
950 }
951
952 //
953 // Do the left side checks range checks
954 //
955 if (poshi-fExtractWinLeft < hi0+0.5)
956 {
957 *fLog << err;
958 *fLog << "ERROR - Pulse is too much to the left, out of hi-gain range [";
959 *fLog << hi0 << "," << hi1 << "]" << endl;
960 return -3;
961 }
962
963 if (haslo && poslo2-fExtractWinLeft < lo0+0.5)
964 {
965 *fLog << warn;
966 *fLog << "WARNING - Pulse is too much to the left, out of lo-gain range [";
967 *fLog << lo0 << "," << lo1 << "]" << endl;
968 *fLog << "Trying to match extraction window and pulse position..." << endl;
969
970 //
971 // Set and store the new ranges
972 //
973 Int_t shift = 0;
974 while (poslo2-fExtractWinLeft < lo0+0.5)
975 {
976 hi1--;
977 lo0--;
978
979 if (poshi+wshigain+fExtractWinRight > hi1-0.5)
980 {
981 *fLog << err << "ERROR - No proper extraction window found.... abort." << endl;
982 return -3;
983 }
984 }
985
986 *fLog << "Changed extraction window by " << shift;
987 *fLog << "slices to hi-gain [" << hi0 << "," << hi1;
988 *fLog << "] and lo-gain [" << lo0 << "," << lo1 << "]" << endl;
989
990 ext->SetRange(hi0, hi1, lo0, lo1);
991 }
992
993 return kTRUE;
994}
995
996Int_t MJPedestal::Process()
997{
998 if (!fSequence.IsValid())
999 {
1000 *fLog << err << "ERROR - Sequence invalid..." << endl;
1001 return kFALSE;
1002 }
1003
1004 // --------------------------------------------------------------------------------
1005
1006 const TString type = IsUseData() ? "data" : "pedestal";
1007
1008 *fLog << inf;
1009 fLog->Separator(GetDescriptor());
1010 *fLog << "Calculate MPedestalCam from " << type << "-runs ";
1011 *fLog << fSequence.GetFileName() << endl;
1012 *fLog << endl;
1013
1014 // --------------------------------------------------------------------------------
1015
1016 if (!CheckEnv())
1017 return kFALSE;
1018
1019 MParList plist;
1020 MTaskList tlist;
1021 plist.AddToList(&tlist);
1022 plist.AddToList(this); // take care of fDisplay!
1023
1024 MReadMarsFile read("Events");
1025 MRawFileRead rawread(NULL);
1026
1027 MDirIter iter;
1028 if (fSequence.IsValid())
1029 {
1030 const Int_t n0 = IsUseData()
1031 ? fSequence.SetupDatRuns(iter, 0, !fSequence.IsMonteCarlo())
1032 : fSequence.SetupPedRuns(iter, 0, !fSequence.IsMonteCarlo());
1033
1034 if (n0<=0)
1035 return kFALSE;
1036 }
1037
1038 if (!fSequence.IsMonteCarlo())
1039 {
1040 rawread.AddFiles(iter);
1041 tlist.AddToList(&rawread);
1042 }
1043 else
1044 {
1045 read.DisableAutoScheme();
1046 read.AddFiles(iter);
1047 tlist.AddToList(&read);
1048 }
1049
1050 // Setup Tasklist
1051 plist.AddToList(&fPedestalCamOut);
1052 plist.AddToList(&fBadPixels);
1053
1054 MGeomApply geomapl;
1055 MBadPixelsMerge merge(&fBadPixels);
1056
1057 MPedCalcPedRun pedcalc;
1058 //pedcalc.SetPedestalUpdate(kFALSE);
1059
1060 MPedCalcFromLoGain pedlogain;
1061 pedlogain.SetPedestalUpdate(kFALSE);
1062
1063 MHPedestalCam hpedcam;
1064 hpedcam.SetPedestalsOut(&fPedestalCamOut);
1065 if (fExtractionType != kFundamental)
1066 hpedcam.SetRenorm(kTRUE);
1067
1068 // To have it in the parlist for MEnv!
1069 MHCalibrationPulseTimeCam pulcam;
1070 plist.AddToList(&pulcam);
1071 MFillH fillpul(&pulcam, "MPedestalSubtractedEvt", "FillPulseTime");
1072 fillpul.SetBit(MFillH::kDoNotDisplay);
1073
1074 tlist.AddToList(&geomapl);
1075 tlist.AddToList(&merge);
1076
1077 if (!fPathIn.IsNull())
1078 {
1079 fExtractor = ReadCalibration();
1080 if (!fExtractor)
1081 return kFALSE;
1082
1083 // The requested setup might have been overwritten
1084 if (!CheckEnv(*fExtractor))
1085 return kFALSE;
1086
1087 *fLog << all;
1088 *fLog << underline << "Signal Extractor found in calibration file and setup:" << endl;
1089 fExtractor->Print();
1090 *fLog << endl;
1091 }
1092
1093 //
1094 // Read bad pixels from outside
1095 //
1096 if (!fBadPixelsFile.IsNull())
1097 {
1098 *fLog << inf << "Excluding: " << fBadPixelsFile << endl;
1099 ifstream fin(fBadPixelsFile);
1100 fBadPixels.AsciiRead(fin);
1101 }
1102
1103 // This will make that for data with version less than 5, where
1104 // trigger patterns were not yet correct, all the events in the real
1105 // data file will be processed. In any case there are no interleaved
1106 // calibration events in such data, so this is fine.
1107 MTriggerPatternDecode decode;
1108 MFTriggerPattern fcalib("CalibFilter");
1109 fcalib.SetDefault(kTRUE);
1110 fcalib.DenyCalibration();
1111 fcalib.DenyPedestal();
1112
1113 tlist.AddToList(&decode);
1114
1115 // produce pedestal subtracted raw-data
1116 MPedestalSubtract pedsub;
1117 if (fExtractor && fExtractionType!=kFundamental)
1118 pedsub.SetPedestalCam(&fPedestalCamIn);
1119 else
1120 pedsub.SetNamePedestalCam(""); // Only copy hi- and lo-gain together!
1121 tlist.AddToList(&pedsub);
1122
1123 if (fIsPulsePosCheck)
1124 {
1125 fillpul.SetFilter(&fcalib);
1126 tlist.AddToList(&fcalib);
1127 tlist.AddToList(&fillpul);
1128 }
1129
1130 //MFillH fillC("MHPedestalCor", "MPedestalSubtractedEvt", "FillAcor");
1131 //fillC.SetNameTab("Acor");
1132 //if (fExtractor && fExtractionType==kWithExtractorRndm)
1133 // tlist.AddToList(&fillC);
1134
1135 // ----------------------------------------------------------------------
1136 // Now we make sure, that in all cases the ranges are setup correctly
1137 // ----------------------------------------------------------------------
1138 MTaskEnv taskenv("ExtractPedestal");
1139
1140 //------------------------------
1141 MFTriggerPattern ftp2("PedestalFilter");
1142 ftp2.SetDefault(kTRUE);
1143 ftp2.DenyCalibration();
1144 ftp2.RequirePedestal();
1145
1146 if (!fSequence.IsMonteCarlo() && (!fExtractor || !fExtractor->HasLoGain()))
1147 {
1148 taskenv.SetFilter(&ftp2);
1149 tlist.AddToList(&ftp2);
1150 }
1151 //------------------------------
1152
1153 switch (fExtractType)
1154 {
1155 case kUsePedRun:
1156 // In case other than 'fundamental' second argument is obsolete
1157 // pedcalc.SetExtractWindow(0,14); // kUsePedRun (take default from class)
1158 taskenv.SetDefault(&pedcalc);
1159 tlist.AddToList(&taskenv);
1160 break;
1161
1162 case kUseData:
1163 // In case other than 'fundamental' second argument is obsolete
1164 // pedlogain.SetExtractWindow(15,14); // kUseData (take default from class)
1165 taskenv.SetDefault(&pedlogain);
1166 tlist.AddToList(&taskenv);
1167 break;
1168 }
1169
1170 if (fIsUseHists && fExtractor)
1171 {
1172 if (fExtractor->InheritsFrom("MExtractTimeAndCharge"))
1173 {
1174 if (fExtractionType!=kFundamental)
1175 {
1176 const MExtractTimeAndCharge &e = *static_cast<MExtractTimeAndCharge*>(fExtractor);
1177 hpedcam.SetFitStart(-5*e.GetWindowSizeHiGain());
1178 }
1179 else
1180 hpedcam.SetFitStart(10.);
1181 }
1182
1183 plist.AddToList(&hpedcam);
1184 }
1185
1186 pedcalc.SetPedestalsOut(&fPedestalCamOut);
1187 pedlogain.SetPedestalsOut(&fPedestalCamOut);
1188
1189 // kFundamental
1190 if (fExtractor)
1191 {
1192 if (fExtractionType!=kFundamental)
1193 {
1194 pedcalc.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1195 pedlogain.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1196
1197 pedcalc.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1198 pedlogain.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1199 }
1200 else
1201 {
1202 pedcalc.SetRangeFromExtractor(*fExtractor);
1203 pedlogain.SetRangeFromExtractor(*fExtractor);
1204 }
1205
1206 if (!fExtractor->InheritsFrom("MExtractTimeAndCharge") && fExtractionType!=kFundamental)
1207 {
1208 *fLog << inf;
1209 *fLog << "Signal extractor doesn't inherit from MExtractTimeAndCharge..." << endl;
1210 *fLog << " --> falling back to fundamental pedestal extraction." << endl;
1211 fExtractionType=kFundamental;
1212 }
1213 }
1214 else
1215 {
1216 *fLog << warn << GetDescriptor() << ": WARNING - No extractor has been handed over! " << endl;
1217 *fLog << "Taking default window for pedestal extraction. The calculated pedestal RMS" << endl;
1218 *fLog << "will probably not match with future pedestal RMS' from different extraction" << endl;
1219 *fLog << "windows." << endl;
1220 }
1221
1222
1223 /*
1224 MHCamEvent evt0(0, "Ped", "Pedestal;;P [cnts/sl]");
1225 MHCamEvent evt1(2, "PedRms", "Pedestal RMS;;\\sigma_{p} [cnts/sl]");
1226
1227 MFillH fill0(&evt0, &fPedestalCamOut, "FillPedestal");
1228 MFillH fill1(&evt1, &fPedestalCamOut, "FillPedRms");
1229
1230 tlist.AddToList(&fill0);
1231 tlist.AddToList(&fill1);
1232 */
1233
1234 //
1235 // Execute the eventloop
1236 //
1237 MEvtLoop evtloop(fName);
1238 evtloop.SetParList(&plist);
1239 evtloop.SetDisplay(fDisplay);
1240 evtloop.SetLogStream(fLog);
1241 if (!SetupEnv(evtloop))
1242 return kFALSE;
1243
1244 // if (!WriteEventloop(evtloop))
1245 // return kFALSE;
1246
1247 // Execute first analysis
1248 if (!evtloop.Eventloop(fMaxEvents))
1249 {
1250 *fLog << err << GetDescriptor() << ": Failed." << endl;
1251 return kFALSE;
1252 }
1253
1254 if (fDeadPixelCheck)
1255 {
1256 MBadPixelsCalc calc;
1257 calc.SetPedestalLevelVarianceLo(4.5);
1258 calc.SetPedestalLevelVarianceHi();
1259 calc.SetPedestalLevel();
1260 if (!CheckEnv(calc))
1261 return kFALSE;
1262 calc.SetGeomCam(dynamic_cast<MGeomCam*>(plist.FindObject("MGeomCam")));
1263 if (!calc.CheckPedestalRms(fBadPixels, fPedestalCamOut))
1264 {
1265 *fLog << err << "ERROR - MBadPixelsCalc::CheckPedestalRms failed...." << endl;
1266 return kFALSE;
1267 }
1268 }
1269
1270 if (fDisplayType!=kDisplayNone)
1271 DisplayResult(plist);
1272
1273 // if (!WriteResult())
1274 // return kFALSE;
1275
1276 const Int_t rc = PulsePosCheck(plist);
1277 if (rc<1)
1278 return rc;
1279
1280 *fLog << all << GetDescriptor() << ": Done." << endl;
1281 *fLog << endl << endl;
1282
1283 return kTRUE;
1284}
Note: See TracBrowser for help on using the repository browser.