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

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