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

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