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

Last change on this file since 6078 was 6078, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 32.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-2004
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MJPedestal
29//
30// Resource file entries are case sensitive!
31//
32/////////////////////////////////////////////////////////////////////////////
33#include "MJPedestal.h"
34
35// root classes
36#include <TF1.h>
37#include <TEnv.h>
38#include <TFile.h>
39#include <TLine.h>
40#include <TLatex.h>
41#include <TString.h>
42#include <TCanvas.h>
43#include <TSystem.h>
44#include <TLegend.h>
45#include <TPad.h>
46#include <TEnv.h>
47
48// mars core
49#include "MLog.h"
50#include "MLogManip.h"
51
52#include "MTaskEnv.h"
53#include "MSequence.h"
54#include "MRunIter.h"
55#include "MParList.h"
56#include "MTaskList.h"
57#include "MEvtLoop.h"
58
59#include "MStatusDisplay.h"
60
61// Other basic classes
62#include "MExtractor.h"
63#include "MExtractTimeAndCharge.h"
64
65// parameter containers
66#include "MGeomCam.h"
67#include "MHCamera.h"
68#include "MPedestalCam.h"
69#include "MPedestalPix.h"
70#include "MBadPixelsCam.h"
71
72#include "MCalibrationPedCam.h"
73#include "MCalibrationPix.h"
74#include "MHPedestalCam.h"
75#include "MHPedestalPix.h"
76
77// tasks
78#include "MReadMarsFile.h"
79#include "MRawFileRead.h"
80#include "MGeomApply.h"
81#include "MBadPixelsMerge.h"
82#include "MFillH.h"
83#include "MPedCalcPedRun.h"
84#include "MPedCalcFromLoGain.h"
85
86ClassImp(MJPedestal);
87
88using namespace std;
89
90const TString MJPedestal::fgReferenceFile = "mjobs/pedestalref.rc";
91// --------------------------------------------------------------------------
92//
93// Default constructor.
94//
95// Sets:
96// - fRuns to 0,
97// - fExtractor to NULL,
98// - fDataCheck to kFALSE,
99// - fExtractType to kUsePedRun
100// - fStorage to Normal Storage
101// - fExtractorResolution to kFALSE
102//
103MJPedestal::MJPedestal(const char *name, const char *title)
104 : fRuns(0), fExtractor(NULL), fDisplayType(kDisplayDataCheck),
105 fDataCheck(kFALSE), fExtractType(kUsePedRun), fExtractionType(kFundamental)
106{
107 fName = name ? name : "MJPedestal";
108 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
109
110 SetNormalStorage();
111 SetUsePedRun();
112 SetPathIn("");
113 SetReferenceFile();
114
115 //
116 // Default references for case that no value references file is there
117 // (should not occur)
118 //
119
120 fPedestalMin = 4.;
121 fPedestalMax = 16.;
122 fPedRmsMin = 0.;
123 fPedRmsMax = 20.;
124 fRefPedClosedLids = 9.635;
125 fRefPedExtraGalactic = 9.93;
126 fRefPedGalactic = 10.03;
127 fRefPedRmsClosedLidsInner = 1.7;
128 fRefPedRmsExtraGalacticInner = 5.6;
129 fRefPedRmsGalacticInner = 6.92;
130 fRefPedRmsClosedLidsOuter = 1.7;
131 fRefPedRmsExtraGalacticOuter = 3.35;
132 fRefPedRmsGalacticOuter = 4.2;
133}
134
135MJPedestal::~MJPedestal()
136{
137 if (fExtractor)
138 delete fExtractor;
139}
140
141const char* MJPedestal::GetOutputFile() const
142{
143 const TString name(GetOutputFileName());
144 if (name.IsNull())
145 return "";
146
147 return Form("%s/%s", fPathOut.Data(), name.Data());
148}
149
150const char* MJPedestal::GetOutputFileName() const
151{
152 if (fSequence.IsValid())
153 return Form("pedest%08d.root", fSequence.GetSequence());
154
155 if (!fRuns)
156 return "";
157
158 return Form("%s-F0.root", (const char*)fRuns->GetRunsAsFileName());
159}
160
161//---------------------------------------------------------------------------------
162//
163// Try to read an existing MPedestalCam from a previously created output file.
164// If found, also an MBadPixelsCam and the corresponding display is read.
165//
166// In case of Storage type "kNoStorage" or if the file is not found or the
167// MPedestalCam cannot be read, return kFALSE, otherwise kTRUE.
168//
169Bool_t MJPedestal::ReadPedestalCam()
170{
171 const TString fname = GetOutputFile();
172
173 *fLog << inf << "Reading pedestals from file: " << fname << endl;
174
175 TFile file(fname, "READ");
176 if (fPedestalCamIn.Read()<=0)
177 {
178 *fLog << err << "Unable to read incoming MPedestalCam from " << fname << endl;
179 return kFALSE;
180 }
181
182 if (fPedestalCamOut.Read()<=0)
183 {
184 *fLog << err << "Unable to read outgoing MPedestalCam from " << fname << endl;
185 return kFALSE;
186 }
187
188 if (file.FindKey("MBadPixelsCam"))
189 {
190 MBadPixelsCam bad;
191 if (bad.Read()<=0)
192 {
193 *fLog << err << "Unable to read MBadPixelsCam from " << fname << endl;
194 return kFALSE;
195 }
196 fBadPixels.Merge(bad);
197 }
198
199 if (fDisplay && !fDisplay->GetCanvas("Pedestals"))
200 fDisplay->Read();
201
202 return kTRUE;
203}
204
205MExtractor *MJPedestal::ReadCalibration() const
206{
207 const TString fname = Form("%s/calib%08d.root",fPathIn.Data(), fSequence.GetSequence());
208
209 *fLog << inf << "Reading extractor from file: " << fname << endl;
210
211 TFile file(fname, "READ");
212 if (!file.IsOpen())
213 {
214 *fLog << err << dbginf << "ERROR - Could not open file " << fname << endl;
215 return NULL;
216 }
217
218 TObject *o = file.Get("ExtractSignal");
219 if (o && !o->InheritsFrom(MExtractor::Class()))
220 {
221 *fLog << err << dbginf << "ERROR - ExtractSignal read from " << fname << " doesn't inherit from MExtractor!" << endl;
222 return NULL;
223 }
224
225 return o ? (MExtractor*)o->Clone("ExtractSignal") : NULL;
226}
227
228//---------------------------------------------------------------------------------
229//
230// Display the results.
231// If Display type "kDataCheck" was chosen, also the reference lines are displayed.
232//
233void MJPedestal::DisplayResult(MParList &plist)
234{
235 if (!fDisplay)
236 return;
237
238 //
239 // Update display
240 //
241 TString title = fDisplay->GetTitle();
242 title += "-- Pedestal ";
243 if (fSequence.IsValid())
244 title += fSequence.GetName();
245 else
246 if (fRuns) // FIXME: What to do if an environmentfile was used?
247 title += fRuns->GetRunsAsString();
248 title += " --";
249 fDisplay->SetTitle(title);
250
251 //
252 // Get container from list
253 //
254 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
255 MCalibrationPedCam &calpedcam = *(MCalibrationPedCam*)plist.FindObject("MCalibrationPedCam");
256
257 //
258 // Create container to display
259 //
260 MHCamera disp0 (geomcam, "MPedestalCam;ped", "Mean Pedestal");
261 MHCamera disp1 (geomcam, "MPedestalCam;RMS", "Pedestal RMS");
262 MHCamera disp2 (geomcam, "MCalibPedCam;histmean", "Mean Pedestal (Hist.)");
263 MHCamera disp3 (geomcam, "MCalibPedCam;histsigma", "Pedestal RMS (Hist.)");
264 MHCamera disp4 (geomcam, "MCalibPedCam;ped", "Mean Pedestal");
265 MHCamera disp5 (geomcam, "MCalibPedCam;RMS", "Pedestal RMS");
266 MHCamera disp6 (geomcam, "MCalibDiffCam;ped", "Diff. Mean Pedestal (Hist.)");
267 MHCamera disp7 (geomcam, "MCalibDiffCam;RMS", "Diff. Pedestal RMS (Hist.)");
268 MHCamera disp8 (geomcam, "MCalibDiffCam;ped", "Diff. Mean Pedestal");
269 MHCamera disp9 (geomcam, "MCalibDiffCam;AbsRMS", "Diff. Abs. Pedestal RMS");
270 MHCamera disp10(geomcam, "MCalibDiffCam;RelRMS", "Diff. Rel. Pedestal RMS");
271
272 disp0.SetCamContent(fPedestalCamOut, 0);
273 disp0.SetCamError (fPedestalCamOut, 1);
274
275 disp1.SetCamContent(fPedestalCamOut, 2);
276 disp1.SetCamError (fPedestalCamOut, 3);
277
278 if (fExtractType == kUseHists)
279 {
280 disp2.SetCamContent(calpedcam, 0);
281 disp2.SetCamError (calpedcam, 1);
282
283 disp3.SetCamContent(calpedcam, 2);
284 disp3.SetCamError (calpedcam, 3);
285
286 disp4.SetCamContent(calpedcam, 5);
287 disp4.SetCamError (calpedcam, 6);
288
289 disp5.SetCamContent(calpedcam, 7);
290 disp5.SetCamError (calpedcam, 8);
291
292 for (UInt_t i=0;i<geomcam.GetNumPixels();i++)
293 {
294
295 MPedestalPix &ped = fPedestalCamOut[i];
296 MCalibrationPix &hist = calpedcam [i];
297 MBadPixelsPix &bad = fBadPixels[i];
298
299 if (bad.IsUnsuitable())
300 continue;
301
302 disp6.Fill(i,ped.GetPedestal()-hist.GetHiGainMean());
303 disp6.SetUsed(i);
304
305 disp7.Fill(i,hist.GetHiGainSigma()-ped.GetPedestalRms());
306 if (TMath::Abs(ped.GetPedestalRms()-hist.GetHiGainSigma()) < 4.0)
307 disp7.SetUsed(i);
308
309 disp8.Fill(i,ped.GetPedestal()-hist.GetLoGainMean());
310 disp8.SetUsed(i);
311
312 disp9.Fill(i,hist.GetLoGainSigma()-ped.GetPedestalRms());
313 if (TMath::Abs(hist.GetLoGainSigma() - ped.GetPedestalRms()) < 4.0)
314 disp9.SetUsed(i);
315 }
316 }
317
318 if (fExtractionType!=kFundamental/*fExtractorResolution*/)
319 {
320 for (UInt_t i=0;i<geomcam.GetNumPixels();i++)
321 {
322
323 MPedestalPix &pedo = fPedestalCamOut[i];
324 MPedestalPix &pedi = fPedestalCamIn[i];
325 MBadPixelsPix &bad = fBadPixels[i];
326
327 if (bad.IsUnsuitable())
328 continue;
329
330 const Float_t diff = pedo.GetPedestalRms()-pedi.GetPedestalRms();
331 const Float_t sum = 0.5*(pedo.GetPedestalRms()+pedi.GetPedestalRms());
332
333 disp9.Fill(i,pedo.GetPedestalRms()-pedi.GetPedestalRms());
334 if (pedo.IsValid() && pedi.IsValid())
335 disp9.SetUsed(i);
336
337 disp10.Fill(i,sum == 0. ? 0. : diff/sum);
338 if (pedo.IsValid() && pedi.IsValid() && sum != 0.)
339 disp10.SetUsed(i);
340 }
341 }
342
343 disp0.SetYTitle("P [cts/slice]");
344 disp1.SetYTitle("P_{rms} [cts/slice]");
345 disp2.SetYTitle("Hist. Mean [cts/slice]");
346 disp3.SetYTitle("Hist. Sigma [cts/slice]");
347 disp4.SetYTitle("Calc. Mean [cts/slice]");
348 disp5.SetYTitle("Calc. RMS [cts/slice]");
349 disp6.SetYTitle("Diff. Mean [cts/slice]");
350 disp7.SetYTitle("Diff. RMS [cts/slice]");
351 disp8.SetYTitle("Diff. Mean [cts/slice]");
352 disp9.SetYTitle("Abs.Diff.RMS [cts/slice]");
353 disp10.SetYTitle("Rel.Diff.RMS [1]");
354
355 //
356 // Display data
357 //
358 if (fDisplayType != kDisplayDataCheck && fExtractType != kUseHists && fExtractionType==kFundamental/*fExtractorResolution*/)
359 {
360 TCanvas &c3 = fDisplay->AddTab("Pedestals");
361 c3.Divide(2,3);
362
363 disp0.CamDraw(c3, 1, 2, 1);
364 disp1.CamDraw(c3, 2, 2, 6);
365 return;
366 }
367
368 if (fExtractType == kUseHists)
369 {
370
371 TCanvas &c3 = fDisplay->AddTab("Extractor Hist.");
372 c3.Divide(2,3);
373
374 disp2.CamDraw(c3, 1, 2, 1);
375 disp3.CamDraw(c3, 2, 2, 5);
376
377 TCanvas &c4 = fDisplay->AddTab("Extractor Calc.");
378 c4.Divide(2,3);
379
380 disp4.CamDraw(c4, 1, 2, 1);
381 disp5.CamDraw(c4, 2, 2, 5);
382
383
384 TCanvas &c5 = fDisplay->AddTab("Difference Hist.");
385 c5.Divide(2,3);
386
387 disp6.CamDraw(c5, 1, 2, 1);
388 disp7.CamDraw(c5, 2, 2, 5);
389
390 TCanvas &c6 = fDisplay->AddTab("Difference Calc.");
391 c6.Divide(2,3);
392
393 disp8.CamDraw(c6, 1, 2, 1);
394 disp9.CamDraw(c6, 2, 2, 5);
395 return;
396 }
397
398 if (fDisplayType == kDisplayDataCheck)
399 {
400
401 TCanvas &c3 = fDisplay->AddTab(fExtractionType!=kFundamental/*fExtractorResolution*/ ? "PedExtrd" : "Ped");
402 c3.Divide(2,3);
403
404 c3.cd(1);
405 gPad->SetBorderMode(0);
406 gPad->SetTicks();
407 MHCamera *obj1=(MHCamera*)disp0.DrawCopy("hist");
408 //
409 // for the datacheck, fix the ranges!!
410 //
411 if (fExtractionType==kFundamental/*!fExtractorResolution*/)
412 {
413 obj1->SetMinimum(fPedestalMin);
414 obj1->SetMaximum(fPedestalMax);
415 //
416 // Set the datacheck sizes:
417 //
418 FixDataCheckHist((TH1D*)obj1);
419 //
420 // set reference lines
421 //
422 DisplayReferenceLines(obj1,0);
423 }
424
425 //
426 // end reference lines
427 //
428 c3.cd(3);
429 gPad->SetBorderMode(0);
430 obj1->SetPrettyPalette();
431 obj1->Draw();
432
433 c3.cd(5);
434 gPad->SetBorderMode(0);
435 gPad->SetTicks();
436 TH1D *obj2 = (TH1D*)obj1->Projection(obj1->GetName());
437 obj2->Draw();
438 obj2->SetBit(kCanDelete);
439 obj2->Fit("gaus","Q");
440 obj2->GetFunction("gaus")->SetLineColor(kYellow);
441 //
442 // Set the datacheck sizes:
443 //
444 FixDataCheckHist(obj2);
445 obj2->SetStats(1);
446
447 c3.cd(2);
448 gPad->SetBorderMode(0);
449 gPad->SetTicks();
450 MHCamera *obj3=(MHCamera*)disp1.DrawCopy("hist");
451 //
452 // for the datacheck, fix the ranges!!
453 //
454 obj3->SetMinimum(fPedRmsMin);
455 obj3->SetMaximum(fPedRmsMax);
456 //
457 // Set the datacheck sizes:
458 //
459 FixDataCheckHist((TH1D*)obj3);
460 //
461 // set reference lines
462 //
463 DisplayReferenceLines(obj3,1);
464
465 c3.cd(4);
466 gPad->SetBorderMode(0);
467 obj3->SetPrettyPalette();
468 obj3->Draw();
469
470 c3.cd(6);
471 gPad->SetBorderMode(0);
472
473 if (geomcam.InheritsFrom("MGeomCamMagic"))
474 {
475 TArrayI inner(1);
476 inner[0] = 0;
477
478 TArrayI outer(1);
479 outer[0] = 1;
480
481 TArrayI s0(6);
482 s0[0] = 6;
483 s0[1] = 1;
484 s0[2] = 2;
485 s0[3] = 3;
486 s0[4] = 4;
487 s0[5] = 5;
488
489 TArrayI s1(3);
490 s1[0] = 6;
491 s1[1] = 1;
492 s1[2] = 2;
493
494 TArrayI s2(3);
495 s2[0] = 3;
496 s2[1] = 4;
497 s2[2] = 5;
498
499 TVirtualPad *pad = gPad;
500 pad->Divide(2,1);
501
502 TH1D *inout[2];
503 inout[0] = disp1.ProjectionS(s0, inner, "Inner");
504 inout[1] = disp1.ProjectionS(s0, outer, "Outer");
505 FixDataCheckHist(inout[0]);
506 FixDataCheckHist(inout[1]);
507
508 inout[0]->SetTitle(Form("%s %s",disp1.GetTitle(),"Inner"));
509 inout[1]->SetTitle(Form("%s %s",disp1.GetTitle(),"Outer"));
510
511
512 for (int i=0; i<2; i++)
513 {
514 pad->cd(i+1);
515 gPad->SetBorderMode(0);
516 gPad->SetTicks();
517
518 inout[i]->SetDirectory(NULL);
519 inout[i]->SetLineColor(kRed+i);
520 inout[i]->SetBit(kCanDelete);
521 inout[i]->Draw();
522 inout[i]->Fit("gaus", "Q");
523
524 TLegend *leg2 = new TLegend(0.6,0.2,0.9,0.55);
525 leg2->SetHeader(inout[i]->GetName());
526 leg2->AddEntry(inout[i], inout[i]->GetName(), "l");
527
528 //
529 // Display the outliers as dead and noisy pixels
530 //
531 DisplayOutliers(inout[i]);
532
533 //
534 // Display the two half of the camera separately
535 //
536 TH1D *half[2];
537 half[0] = disp1.ProjectionS(s1, i==0 ? inner : outer , "Sector 6-1-2");
538 half[1] = disp1.ProjectionS(s2, i==0 ? inner : outer , "Sector 3-4-5");
539
540 for (int j=0; j<2; j++)
541 {
542 half[j]->SetLineColor(kRed+i+2*j+1);
543 half[j]->SetDirectory(NULL);
544 half[j]->SetBit(kCanDelete);
545 half[j]->Draw("same");
546 leg2->AddEntry(half[j], half[j]->GetName(), "l");
547 }
548 leg2->Draw();
549 delete leg2;
550 }
551 return;
552 }
553 }
554
555 if (fExtractionType!=kFundamental/*fExtractorResolution*/)
556 {
557
558 TCanvas &c3 = fDisplay->AddTab(fExtractionType==kWithExtractor?"PedExtrd":"PedRndm");
559 c3.Divide(2,3);
560
561 disp0.CamDraw(c3, 1, 2, 1);
562 disp1.CamDraw(c3, 2, 2, 6);
563
564 TCanvas &c13 = fDisplay->AddTab(fExtractionType==kWithExtractor?"DiffExtrd":"DiffRndm");
565 c13.Divide(2,3);
566
567 disp9.CamDraw(c13, 1, 2, 5);
568 disp10.CamDraw(c13, 2, 2, 5);
569 return;
570 }
571}
572
573
574void MJPedestal::DisplayReferenceLines(MHCamera *cam, const Int_t what) const
575{
576
577 Double_t x = cam->GetNbinsX();
578
579 const MGeomCam *geom = cam->GetGeometry();
580
581 if (geom->InheritsFrom("MGeomCamMagic"))
582 x = what ? 397 : cam->GetNbinsX();
583
584 TLine line;
585 line.SetLineStyle(kDashed);
586 line.SetLineWidth(3);
587
588 line.SetLineColor(kBlue);
589 TLine *l1 = line.DrawLine(0, what ? fRefPedRmsGalacticInner : fRefPedGalactic,
590 x, what ? fRefPedRmsGalacticInner : fRefPedGalactic);
591
592 line.SetLineColor(kYellow);
593 TLine *l2 = line.DrawLine(0, what ? fRefPedRmsExtraGalacticInner : fRefPedExtraGalactic,
594 x, what ? fRefPedRmsExtraGalacticInner : fRefPedExtraGalactic);
595
596 line.SetLineColor(kMagenta);
597 TLine *l3 = line.DrawLine(0, what ? fRefPedRmsClosedLidsInner : fRefPedClosedLids,
598 x, what ? fRefPedRmsClosedLidsInner : fRefPedClosedLids);
599
600 if (geom->InheritsFrom("MGeomCamMagic"))
601 if (what)
602 {
603 const Double_t x2 = cam->GetNbinsX();
604
605 line.SetLineColor(kBlue);
606 line.DrawLine(398, fRefPedRmsGalacticOuter,
607 x2, fRefPedRmsGalacticOuter);
608
609 line.SetLineColor(kYellow);
610 line.DrawLine(398, fRefPedRmsExtraGalacticOuter,
611 x2, fRefPedRmsExtraGalacticOuter);
612
613 line.SetLineColor(kMagenta);
614 line.DrawLine(398, fRefPedRmsClosedLidsOuter,
615 x2, fRefPedRmsClosedLidsOuter);
616 }
617
618
619 TLegend *leg = new TLegend(0.4,0.75,0.7,0.99);
620 leg->SetBit(kCanDelete);
621 leg->AddEntry(l1, "Galactic Source","l");
622 leg->AddEntry(l2, "Extra-Galactic Source","l");
623 leg->AddEntry(l3, "Closed Lids","l");
624 leg->Draw();
625}
626
627void MJPedestal::DisplayOutliers(TH1D *hist) const
628{
629 const Float_t mean = hist->GetFunction("gaus")->GetParameter(1);
630 const Float_t lolim = mean - 3.5*hist->GetFunction("gaus")->GetParameter(2);
631 const Float_t uplim = mean + 3.5*hist->GetFunction("gaus")->GetParameter(2);
632 const Stat_t dead = hist->Integral(0,hist->FindBin(lolim)-1);
633 const Stat_t noisy = hist->Integral(hist->FindBin(uplim)+1,hist->GetNbinsX()+1);
634
635 TLatex deadtex;
636 deadtex.SetTextSize(0.06);
637 deadtex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.1,Form("%3i dead pixels",(Int_t)dead));
638
639 TLatex noisytex;
640 noisytex.SetTextSize(0.06);
641 noisytex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.2,Form("%3i noisy pixels",(Int_t)noisy));
642}
643
644void MJPedestal::FixDataCheckHist(TH1D *hist) const
645{
646 hist->SetDirectory(NULL);
647 hist->SetStats(0);
648
649 //
650 // set the labels bigger
651 //
652 TAxis *xaxe = hist->GetXaxis();
653 TAxis *yaxe = hist->GetYaxis();
654
655 xaxe->CenterTitle();
656 yaxe->CenterTitle();
657 xaxe->SetTitleSize(0.06);
658 yaxe->SetTitleSize(0.06);
659 xaxe->SetTitleOffset(0.8);
660 yaxe->SetTitleOffset(0.5);
661 xaxe->SetLabelSize(0.05);
662 yaxe->SetLabelSize(0.05);
663}
664
665/*
666Bool_t MJPedestal::WriteEventloop(MEvtLoop &evtloop) const
667{
668 if (fOutputPath.IsNull())
669 return kTRUE;
670
671 const TString oname(GetOutputFile());
672
673 *fLog << inf << "Writing to file: " << oname << endl;
674
675 TFile file(oname, fOverwrite?"RECREATE":"NEW", "File created by MJPedestal", 9);
676 if (!file.IsOpen())
677 {
678 *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
679 return kFALSE;
680 }
681
682 if (evtloop.Write(fName)<=0)
683 {
684 *fLog << err << "Unable to write MEvtloop to " << oname << endl;
685 return kFALSE;
686 }
687
688 return kTRUE;
689}
690*/
691
692void MJPedestal::SetExtractor(MExtractor* ext)
693{
694 if (ext)
695 {
696 if (fExtractor)
697 delete fExtractor;
698 fExtractor = ext ? (MExtractor*)ext->Clone(ext->GetName()) : NULL;
699 }
700 else
701 fExtractor = 0;
702}
703
704// --------------------------------------------------------------------------
705//
706// The following resource options are available:
707//
708// Do a datacheck run (read raw-data and enable display)
709// Prefix.DataCheck: Yes, No <default>
710//
711// Setup display type
712// Prefix.Display: normal <default>, datacheck, none
713//
714// Use cosmic data instead of pedestal data (DatRuns)
715// Prefix.UseData: Yes, No <default>
716//
717// Write an output file with pedestals and status-display
718// Prefix.DisableOutput: Yes, No <default>
719//
720Bool_t MJPedestal::CheckEnvLocal()
721{
722 SetDataCheck(GetEnv("DataCheck", fDataCheck));
723
724 if (HasEnv("Display"))
725 {
726 TString type = GetEnv("Display", "normal");
727 type.ToLower();
728 if (type==(TString)"normal")
729 fDisplayType = kDisplayNormal;
730 if (type==(TString)"datacheck")
731 fDisplayType = kDisplayDataCheck;
732 if (type==(TString)"none")
733 fDisplayType = kDisplayNone;
734 }
735
736 if (HasEnv("UseData"))
737 fExtractType = GetEnv("UseData",kFALSE) ? kUseData : kUsePedRun;
738
739 if (HasEnv("UseHists"))
740 if (GetEnv("UseHists",kFALSE))
741 fExtractType = kUseHists;
742
743 SetNoStorage(GetEnv("DisableOutput", IsNoStorage()));
744
745 MTaskEnv tenv("ExtractSignal");
746 tenv.SetDefault(fExtractor);
747
748 if (tenv.ReadEnv(*GetEnv(), GetEnvPrefix()+".ExtractSignal", GetEnvDebug())==kERROR)
749 return kFALSE;
750
751 if (fExtractor==tenv.GetTask())
752 return kTRUE;
753
754 if (!tenv.GetTask()->InheritsFrom(MExtractor::Class()))
755 {
756 *fLog << err << "ERROR: ExtractSignal from resource file doesn't inherit from MExtractor.... abort." << endl;
757 return kFALSE;
758 }
759
760 SetExtractor((MExtractor*)tenv.GetTask());
761
762 fReferenceFile = GetEnv("ReferenceFile",fReferenceFile.Data());
763
764 TEnv refenv(fReferenceFile);
765
766 fPedestalMin = refenv.GetValue("PedestalMin",fPedestalMin);
767 fPedestalMax = refenv.GetValue("PedestalMax",fPedestalMax);
768 fPedRmsMin = refenv.GetValue("PedRmsMin",fPedRmsMin);
769 fPedRmsMax = refenv.GetValue("PedRmsMax",fPedRmsMax);
770 fRefPedClosedLids = refenv.GetValue("RefPedClosedLids",fRefPedClosedLids);
771 fRefPedExtraGalactic = refenv.GetValue("RefPedExtraGalactic",fRefPedExtraGalactic);
772 fRefPedGalactic = refenv.GetValue("RefPedGalactic",fRefPedGalactic);
773 fRefPedRmsClosedLidsInner = refenv.GetValue("RefPedRmsClosedLidsInner",fRefPedRmsClosedLidsInner);
774 fRefPedRmsExtraGalacticInner = refenv.GetValue("RefPedRmsExtraGalacticInner",fRefPedRmsExtraGalacticInner);
775 fRefPedRmsGalacticInner = refenv.GetValue("RefPedRmsGalacticInner",fRefPedRmsGalacticInner);
776 fRefPedRmsClosedLidsOuter = refenv.GetValue("RefPedRmsClosedLidsOuter",fRefPedRmsClosedLidsOuter);
777 fRefPedRmsExtraGalacticOuter = refenv.GetValue("RefPedRmsExtraGalacticOuter",fRefPedRmsExtraGalacticOuter);
778 fRefPedRmsGalacticOuter = refenv.GetValue("RefPedRmsGalacticOuter",fRefPedRmsGalacticOuter);
779
780 return kTRUE;
781}
782
783//---------------------------------------------------------------------------------
784//
785// Try to write the created MPedestalCam in the output file.
786// If possible, also an MBadPixelsCam and the corresponding display is written.
787//
788// In case of Storage type "kNoStorage" or if any of the containers
789// cannot be written, return kFALSE, otherwise kTRUE.
790//
791Bool_t MJPedestal::WriteResult()
792{
793 if (IsNoStorage())
794 return kTRUE;
795
796 TObjArray cont;
797
798 cont.Add(&fPedestalCamOut);
799 cont.Add(&fBadPixels);
800
801 return WriteContainer(cont, GetOutputFileName(), fOverwrite?"RECREATE":"NEW");
802}
803
804Bool_t MJPedestal::Process()
805{
806 if (!ReadPedestalCam())
807 return ProcessFile();
808
809 return kTRUE;
810}
811
812Bool_t MJPedestal::ProcessFile()
813{
814 if (!fSequence.IsValid())
815 {
816 if (!fRuns)
817 {
818 *fLog << err << "Neither AddRuns nor SetSequence nor SetEnv was called... abort." << endl;
819 return kFALSE;
820 }
821 if (fRuns && fRuns->GetNumRuns() != fRuns->GetNumEntries())
822 {
823 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
824 return kFALSE;
825 }
826 }
827
828 if (!CheckEnv())
829 return kFALSE;
830
831 // --------------------------------------------------------------------------------
832
833 const TString type = IsUseData() ? "data" : "pedestal";
834
835 *fLog << inf;
836 fLog->Separator(GetDescriptor());
837 *fLog << "Calculate MPedestalCam from " << type << "-runs ";
838 if (fSequence.IsValid())
839 *fLog << fSequence.GetName() << endl;
840 else
841 if (fRuns)
842 *fLog << fRuns->GetRunsAsString() << endl;
843 else
844 *fLog << "in Resource File." << endl;
845 *fLog << endl;
846
847 // --------------------------------------------------------------------------------
848
849 MParList plist;
850 MTaskList tlist;
851 plist.AddToList(&tlist);
852 plist.AddToList(this); // take care of fDisplay!
853
854 MReadMarsFile read("Events");
855 MRawFileRead rawread(NULL);
856
857 MDirIter iter;
858 if (fSequence.IsValid())
859 {
860 const Int_t n0 = IsUseData() ? fSequence.SetupDatRuns(iter, fPathData, "D", fDataCheck) : fSequence.SetupPedRuns(iter, fPathData, "P", fDataCheck);
861 const Int_t n1 = IsUseData() ? fSequence.GetNumDatRuns() : fSequence.GetNumPedRuns();
862 if (n0==0)
863 {
864 *fLog << err << "ERROR - No " << type << " input files of sequence found in " << (fPathData.IsNull()?"<defaul>":fPathData.Data()) << endl;
865 return kFALSE;
866 }
867 if (n0!=n1)
868 {
869 *fLog << err << "ERROR - Number of " << type << " files found (" << n0 << ") in " << (fPathData.IsNull()?"<defaul>":fPathData.Data()) << " doesn't match number of files in sequence (" << n1 << ")" << endl;
870 return kFALSE;
871 }
872 }
873
874 if (fDataCheck)
875 {
876 if (fRuns || fSequence.IsValid())
877 rawread.AddFiles(fSequence.IsValid() ? iter : *fRuns);
878 tlist.AddToList(&rawread);
879 }
880 else
881 {
882 read.DisableAutoScheme();
883 if (fRuns || fSequence.IsValid())
884 read.AddFiles(fSequence.IsValid() ? iter : *fRuns);
885 tlist.AddToList(&read);
886 }
887
888 // Setup Tasklist
889 plist.AddToList(&fPedestalCamOut);
890 plist.AddToList(&fBadPixels);
891
892 MGeomApply geomapl;
893 MBadPixelsMerge merge(&fBadPixels);
894
895 MPedCalcPedRun pedcalc;
896 pedcalc.SetPedestalUpdate(kFALSE);
897
898 MPedCalcFromLoGain pedlogain;
899 pedlogain.SetPedestalUpdate(kFALSE);
900
901 MHPedestalCam hpedcam;
902 hpedcam.SetRenorm(kTRUE);
903
904 MFillH fillped(&hpedcam, "MExtractedSignalCam", "FillPedestalCam");
905 fillped.SetBit(MFillH::kDoNotDisplay);
906
907 tlist.AddToList(&geomapl);
908 tlist.AddToList(&merge);
909
910 if (!fPathIn.IsNull())
911 {
912 fExtractor = ReadCalibration();
913 if (!fExtractor)
914 return kFALSE;
915
916 *fLog << all;
917 *fLog << underline << "Signal Extractor found in calibration file:" << endl;
918 fExtractor->Print();
919 *fLog << endl;
920 }
921
922 // ----------------------------------------------------------------------
923 // Now we make sure, that in all cases the ranges are setup correctly
924 // ----------------------------------------------------------------------
925 MTaskEnv taskenv("ExtractPedestal");
926 switch (fExtractType)
927 {
928 case kUsePedRun:
929 // In case other than 'fundamental' second argument is obsolete
930 // pedcalc.SetExtractWindow(0,14); // kUsePedRun (take default from class)
931 taskenv.SetDefault(&pedcalc);
932 tlist.AddToList(&taskenv);
933 break;
934
935 case kUseData:
936 // In case other than 'fundamental' second argument is obsolete
937 // pedlogain.SetExtractWindow(15,14); // kUseData (take default from class)
938 taskenv.SetDefault(&pedlogain);
939 tlist.AddToList(&taskenv);
940 break;
941
942 case kUseHists:
943 if (!fExtractor)
944 {
945 *fLog << err << GetDescriptor() << " - ERROR: ";
946 *fLog << "Extraction Type is kUseHists, but no extractor was set" << endl;
947 return kFALSE;
948 }
949
950 tlist.AddToList(fExtractor);
951 tlist.AddToList(&fillped);
952 break;
953 }
954
955 pedcalc.SetPedestalsIn(&fPedestalCamIn);
956 pedlogain.SetPedestalsIn(&fPedestalCamIn);
957
958 // kFundamental
959 if (fExtractor)
960 {
961
962 if (fExtractor->InheritsFrom("MExtractTimeAndCharge"))
963 {
964
965 const Float_t f = 0.5+fExtractor->GetHiGainFirst();
966 const Int_t win = ((MExtractTimeAndCharge*)fExtractor)->GetWindowSizeHiGain();
967 pedcalc.SetExtractWindow((Int_t)f, win);
968 pedlogain.SetExtractWindow((Int_t)(15+f), win);
969
970 }
971 else
972 {
973 const Float_t f = 0.5+fExtractor->GetHiGainFirst();
974 const Float_t n = 0.5+fExtractor->GetNumHiGainSamples();
975 pedcalc.SetExtractWindow((Int_t)f, (Int_t)n);
976 pedlogain.SetExtractWindow((Int_t)(15+f), (Int_t)n);
977
978 if (fExtractionType!=kFundamental)
979 {
980 *fLog << inf;
981 *fLog << "Signal extractor doesn't inherit from MExtractTimeAndCharge..." << endl;
982 *fLog << " --> falling back to fundamental pedestal extraction." << endl;
983 fExtractionType=kFundamental;
984 }
985 }
986
987
988 if (fExtractionType!=kFundamental)
989 {
990 pedcalc.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
991 pedlogain.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
992
993 pedcalc.SetExtractor((MExtractTimeAndCharge*)fExtractor);
994 pedlogain.SetExtractor((MExtractTimeAndCharge*)fExtractor);
995 }
996 }
997 else
998 {
999 *fLog << warn << GetDescriptor() << ": WARNING - No extractor has been handed over! " << endl;
1000 *fLog << "Taking default window for pedestal extraction. The calculated pedestal RMS" << endl;
1001 *fLog << "will probably not match with future pedestal RMS' from different extraction" << endl;
1002 *fLog << "windows." << endl;
1003 }
1004
1005 /*
1006 switch (fExtractType)
1007 {
1008 case kUseData:
1009 *fLog << all << "TYPE: USEDATA " << fExtractor << endl;
1010 taskenv.SetDefault(&pedlogain);
1011 tlist.AddToList(&taskenv);
1012
1013 if (!SetupExtractor(plist, pedlogain))
1014 {
1015 *fLog << all << "SETTING TO: " << fExtractor << " " << fExtractor->GetNumHiGainSamples() << endl;
1016 fExtractor->Print();
1017 pedlogain.SetExtractWindow(15, (Int_t)TMath::Nint(fExtractor->GetNumHiGainSamples()));
1018 }
1019 break;
1020
1021 case kUsePedRun:
1022 *fLog << all << "TYPE: USEPEDRUN " << fExtractor << endl;
1023 taskenv.SetDefault(&pedcalc);
1024 tlist.AddToList(&taskenv);
1025
1026 if (!SetupExtractor(plist, pedcalc))
1027 pedcalc.SetExtractWindow(fExtractor->GetHiGainFirst(), TMath::Nint(fExtractor->GetNumHiGainSamples()));
1028 break;
1029
1030 case kUseHists:
1031 if (!fExtractor)
1032 {
1033 *fLog << err << GetDescriptor() << " - ERROR: ";
1034 *fLog << "Extraction Type is kUseHists, but no extractor was set" << endl;
1035 return kFALSE;
1036 }
1037
1038 tlist.AddToList(fExtractor);
1039 tlist.AddToList(&fillped);
1040 break;
1041 } */
1042
1043 /*
1044 if (!fPathIn.IsNull())
1045 {
1046 delete fExtractor;
1047 fExtractor = 0;
1048 }
1049 */
1050 //
1051 // Execute the eventloop
1052 //
1053 MEvtLoop evtloop(fName);
1054 evtloop.SetParList(&plist);
1055 evtloop.SetDisplay(fDisplay);
1056 evtloop.SetLogStream(fLog);
1057 if (!SetupEnv(evtloop))
1058 return kFALSE;
1059
1060 // if (!WriteEventloop(evtloop))
1061 // return kFALSE;
1062
1063 // Execute first analysis
1064 if (!evtloop.Eventloop(fMaxEvents))
1065 {
1066 *fLog << err << GetDescriptor() << ": Failed." << endl;
1067 return kFALSE;
1068 }
1069
1070 tlist.PrintStatistics();
1071
1072 if (fDisplayType!=kDisplayNone)
1073 DisplayResult(plist);
1074
1075 if (!WriteResult())
1076 return kFALSE;
1077
1078 *fLog << all << GetDescriptor() << ": Done." << endl;
1079 *fLog << endl << endl;
1080
1081 return kTRUE;
1082}
Note: See TracBrowser for help on using the repository browser.