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

Last change on this file since 6724 was 6722, checked in by gaug, 21 years ago
*** empty log message ***
File size: 34.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// 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#include <TH2F.h>
48
49// mars core
50#include "MLog.h"
51#include "MLogManip.h"
52
53#include "MTaskEnv.h"
54#include "MSequence.h"
55#include "MRunIter.h"
56#include "MParList.h"
57#include "MTaskList.h"
58#include "MEvtLoop.h"
59
60#include "MStatusDisplay.h"
61#include "MFEventSelector.h"
62
63// Other basic classes
64#include "MExtractTimeAndCharge.h"
65
66// parameter containers
67#include "MGeomCam.h"
68#include "MHCamera.h"
69#include "MPedestalPix.h"
70
71#include "MCalibrationPedCam.h"
72#include "MCalibrationPix.h"
73#include "MHPedestalPix.h"
74#include "MHCalibrationPulseTimeCam.h"
75
76// tasks
77#include "MReadMarsFile.h"
78#include "MRawFileRead.h"
79#include "MGeomApply.h"
80#include "MBadPixelsMerge.h"
81#include "MFillH.h"
82#include "MPedCalcPedRun.h"
83#include "MPedCalcFromLoGain.h"
84
85#include <fstream>
86
87ClassImp(MJPedestal);
88
89using namespace std;
90
91const TString MJPedestal::fgReferenceFile = "mjobs/pedestalref.rc";
92const TString MJPedestal::fgBadPixelsFile = "mjobs/badpixels_0_559.rc";
93// --------------------------------------------------------------------------
94//
95// Default constructor.
96//
97// Sets:
98// - fExtractor to NULL,
99// - fExtractType to kUsePedRun
100// - fStorage to Normal Storage
101// - fExtractorResolution to kFALSE
102//
103MJPedestal::MJPedestal(const char *name, const char *title)
104 : fExtractor(NULL), fDisplayType(kDisplayDataCheck),
105 fExtractType(kUsePedRun), fExtractionType(kFundamental), fIsUseHists(kFALSE)
106{
107 fName = name ? name : "MJPedestal";
108 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
109
110 SetUsePedRun();
111 SetPathIn("");
112 SetReferenceFile();
113 SetBadPixelsFile();
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
153 if (fSequence.IsValid())
154 return Form("pedest%08d.root", fSequence.GetSequence());
155
156 if (!fRuns)
157 return "";
158
159 return Form("%s-F0.root", (const char*)fRuns->GetRunsAsFileName());
160}
161
162//---------------------------------------------------------------------------------
163//
164// Try to read an existing MPedestalCam from a previously created output file.
165// If found, also an MBadPixelsCam and the corresponding display is read.
166//
167// In case of Storage type "kNoStorage" or if the file is not found or the
168// MPedestalCam cannot be read, return kFALSE, otherwise kTRUE.
169//
170Bool_t MJPedestal::ReadPedestalCam()
171{
172 const TString fname = GetOutputFile();
173
174 *fLog << inf << "Reading pedestals from file: " << fname << endl;
175
176 TFile file(fname, "READ");
177 if (fPedestalCamIn.Read()<=0)
178 {
179 *fLog << err << "Unable to read incoming MPedestalCam from " << fname << endl;
180 return kFALSE;
181 }
182
183 if (fPedestalCamOut.Read()<=0)
184 {
185 *fLog << err << "Unable to read outgoing MPedestalCam from " << fname << endl;
186 return kFALSE;
187 }
188
189 if (file.FindKey("MBadPixelsCam"))
190 {
191 MBadPixelsCam bad;
192 if (bad.Read()<=0)
193 {
194 *fLog << err << "Unable to read MBadPixelsCam from " << fname << endl;
195 return kFALSE;
196 }
197 fBadPixels.Merge(bad);
198 }
199
200 if (fDisplay && !fDisplay->GetCanvas("Pedestals"))
201 fDisplay->Read();
202
203 return kTRUE;
204}
205
206MExtractor *MJPedestal::ReadCalibration() const
207{
208 const TString fname = Form("%s/calib%08d.root",fPathIn.Data(), fSequence.GetSequence());
209
210 *fLog << inf << "Reading extractor from file: " << fname << endl;
211
212 TFile file(fname, "READ");
213 if (!file.IsOpen())
214 {
215 *fLog << err << dbginf << "ERROR - Could not open file " << fname << endl;
216 return NULL;
217 }
218
219 TObject *o = file.Get("ExtractSignal");
220 if (o && !o->InheritsFrom(MExtractor::Class()))
221 {
222 *fLog << err << dbginf << "ERROR - ExtractSignal read from " << fname << " doesn't inherit from MExtractor!" << endl;
223 return NULL;
224 }
225
226 return o ? (MExtractor*)o->Clone("ExtractSignal") : NULL;
227}
228
229//---------------------------------------------------------------------------------
230//
231// Display the results.
232// If Display type "kDataCheck" was chosen, also the reference lines are displayed.
233//
234void MJPedestal::DisplayResult(MParList &plist)
235{
236 if (!fDisplay)
237 return;
238
239 //
240 // Update display
241 //
242 TString title = fDisplay->GetTitle();
243 title += "-- Pedestal ";
244 if (fSequence.IsValid())
245 title += fSequence.GetName();
246 else
247 if (fRuns) // FIXME: What to do if an environmentfile was used?
248 title += fRuns->GetRunsAsString();
249 title += " --";
250 fDisplay->SetTitle(title);
251
252 //
253 // Get container from list
254 //
255 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
256 MCalibrationPedCam &calpedcam = *(MCalibrationPedCam*)plist.FindObject("MCalibrationPedCam");
257
258 //
259 // Create container to display
260 //
261 MHCamera disp0 (geomcam, "MPedestalCam;ped", "Mean Pedestal");
262 MHCamera disp1 (geomcam, "MPedestalCam;RMS", "Pedestal RMS");
263 MHCamera disp2 (geomcam, "MCalibPedCam;histmean", "Mean Pedestal (Hist.)");
264 MHCamera disp3 (geomcam, "MCalibPedCam;histsigma", "Pedestal RMS (Hist.)");
265 MHCamera disp4 (geomcam, "MCalibPedCam;ped", "Mean Pedestal");
266 MHCamera disp5 (geomcam, "MCalibPedCam;RMS", "Pedestal RMS");
267 MHCamera disp6 (geomcam, "MCalibDiffCam;ped", "Diff. Mean Pedestal (Hist.)");
268 MHCamera disp7 (geomcam, "MCalibDiffCam;RMS", "Diff. Pedestal RMS (Hist.)");
269 MHCamera disp8 (geomcam, "MCalibDiffCam;ped", "Diff. Mean Pedestal");
270 MHCamera disp9 (geomcam, "MCalibDiffCam;AbsRMS", "Diff. Abs. Pedestal RMS");
271 MHCamera disp10(geomcam, "MCalibDiffCam;RelRMS", "Diff. Rel. Pedestal RMS");
272
273 disp0.SetCamContent(fPedestalCamOut, 0);
274 disp0.SetCamError (fPedestalCamOut, 1);
275
276 disp1.SetCamContent(fPedestalCamOut, 2);
277 disp1.SetCamError (fPedestalCamOut, 3);
278
279 if (fIsUseHists)
280 {
281 disp2.SetCamContent(calpedcam, 0);
282 disp2.SetCamError (calpedcam, 1);
283
284 disp3.SetCamContent(calpedcam, 2);
285 disp3.SetCamError (calpedcam, 3);
286
287 disp4.SetCamContent(calpedcam, 5);
288 disp4.SetCamError (calpedcam, 6);
289
290 disp5.SetCamContent(calpedcam, 7);
291 disp5.SetCamError (calpedcam, 8);
292
293 for (UInt_t i=0;i<geomcam.GetNumPixels();i++)
294 {
295
296 MPedestalPix &ped = fPedestalCamOut[i];
297 MCalibrationPix &hist = calpedcam [i];
298 MBadPixelsPix &bad = fBadPixels[i];
299
300 if (bad.IsUnsuitable())
301 continue;
302
303 disp6.Fill(i,ped.GetPedestal()-hist.GetHiGainMean());
304 disp6.SetUsed(i);
305
306 disp7.Fill(i,hist.GetHiGainSigma()-ped.GetPedestalRms());
307 if (TMath::Abs(ped.GetPedestalRms()-hist.GetHiGainSigma()) < 4.0)
308 disp7.SetUsed(i);
309
310 disp8.Fill(i,ped.GetPedestal()-hist.GetLoGainMean());
311 disp8.SetUsed(i);
312
313 disp9.Fill(i,hist.GetLoGainSigma()-ped.GetPedestalRms());
314 if (TMath::Abs(hist.GetLoGainSigma() - ped.GetPedestalRms()) < 4.0)
315 disp9.SetUsed(i);
316 }
317 }
318
319 if (fExtractionType!=kFundamental/*fExtractorResolution*/)
320 {
321 for (UInt_t i=0;i<geomcam.GetNumPixels();i++)
322 {
323
324 MPedestalPix &pedo = fPedestalCamOut[i];
325 MPedestalPix &pedi = fPedestalCamIn[i];
326 MBadPixelsPix &bad = fBadPixels[i];
327
328 if (bad.IsUnsuitable())
329 continue;
330
331 const Float_t diff = pedo.GetPedestalRms()-pedi.GetPedestalRms();
332 const Float_t sum = 0.5*(pedo.GetPedestalRms()+pedi.GetPedestalRms());
333
334 disp9.Fill(i,pedo.GetPedestalRms()-pedi.GetPedestalRms());
335 if (pedo.IsValid() && pedi.IsValid())
336 disp9.SetUsed(i);
337
338 disp10.Fill(i,sum == 0. ? 0. : diff/sum);
339 if (pedo.IsValid() && pedi.IsValid() && sum != 0.)
340 disp10.SetUsed(i);
341 }
342 }
343
344 disp0.SetYTitle("P [cts/slice]");
345 disp1.SetYTitle("P_{rms} [cts/slice]");
346 disp2.SetYTitle("Hist. Mean [cts/slice]");
347 disp3.SetYTitle("Hist. Sigma [cts/slice]");
348 disp4.SetYTitle("Calc. Mean [cts/slice]");
349 disp5.SetYTitle("Calc. RMS [cts/slice]");
350 disp6.SetYTitle("Diff. Mean [cts/slice]");
351 disp7.SetYTitle("Diff. RMS [cts/slice]");
352 disp8.SetYTitle("Diff. Mean [cts/slice]");
353 disp9.SetYTitle("Abs.Diff.RMS [cts/slice]");
354 disp10.SetYTitle("Rel.Diff.RMS [1]");
355
356 //
357 // Display data
358 //
359 if (fDisplayType != kDisplayDataCheck && !fIsUseHists && fExtractionType==kFundamental/*fExtractorResolution*/)
360 {
361 TCanvas &c3 = fDisplay->AddTab("Pedestals");
362 c3.Divide(2,3);
363
364 disp0.CamDraw(c3, 1, 2, 1);
365 disp1.CamDraw(c3, 2, 2, 6);
366 return;
367 }
368
369 if (fIsUseHists)
370 {
371
372 TCanvas &c3 = fDisplay->AddTab("Extractor Hist.");
373 c3.Divide(2,3);
374
375 disp2.CamDraw(c3, 1, 2, 1);
376 disp3.CamDraw(c3, 2, 2, 5);
377
378 TCanvas &c4 = fDisplay->AddTab("Extractor Calc.");
379 c4.Divide(2,3);
380
381 disp4.CamDraw(c4, 1, 2, 1);
382 disp5.CamDraw(c4, 2, 2, 5);
383
384
385 TCanvas &c5 = fDisplay->AddTab("Difference Hist.");
386 c5.Divide(2,3);
387
388 disp6.CamDraw(c5, 1, 2, 1);
389 disp7.CamDraw(c5, 2, 2, 5);
390
391 TCanvas &c6 = fDisplay->AddTab("Difference Calc.");
392 c6.Divide(2,3);
393
394 disp8.CamDraw(c6, 1, 2, 1);
395 disp9.CamDraw(c6, 2, 2, 5);
396 return;
397 }
398
399 if (fDisplayType == kDisplayDataCheck)
400 {
401
402 TCanvas &c3 = fDisplay->AddTab(fExtractionType!=kFundamental/*fExtractorResolution*/ ? "PedExtrd" : "Ped");
403 c3.Divide(2,3);
404
405 c3.cd(1);
406 gPad->SetBorderMode(0);
407 gPad->SetTicks();
408 MHCamera *obj1=(MHCamera*)disp0.DrawCopy("hist");
409 //
410 // for the datacheck, fix the ranges!!
411 //
412 if (fExtractionType==kFundamental/*!fExtractorResolution*/)
413 {
414 obj1->SetMinimum(fPedestalMin);
415 obj1->SetMaximum(fPedestalMax);
416 }
417 //
418 // Set the datacheck sizes:
419 //
420 FixDataCheckHist((TH1D*)obj1);
421 //
422 // set reference lines
423 //
424 DisplayReferenceLines(obj1,0);
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 line.SetLineColor(kBlue);
588
589 TLegend *leg = new TLegend(0.6,0.75,0.9,0.99);
590 leg->SetBit(kCanDelete);
591
592 if (fExtractionType==kWithExtractorRndm && !(what))
593 {
594 TLine *l0 = line.DrawLine(0,0.,cam->GetNbinsX(),0.);
595 l0->SetBit(kCanDelete);
596 leg->AddEntry(l0, "Reference","l");
597 leg->Draw();
598 return;
599 }
600
601 line.SetLineColor(kBlue);
602 TLine *l1 = line.DrawLine(0, what ? fRefPedRmsGalacticInner : fRefPedGalactic,
603 x, what ? fRefPedRmsGalacticInner : fRefPedGalactic);
604 l1->SetBit(kCanDelete);
605 line.SetLineColor(kYellow);
606 TLine *l2 = line.DrawLine(0, what ? fRefPedRmsExtraGalacticInner : fRefPedExtraGalactic,
607 x, what ? fRefPedRmsExtraGalacticInner : fRefPedExtraGalactic);
608 l2->SetBit(kCanDelete);
609 line.SetLineColor(kMagenta);
610 TLine *l3 = line.DrawLine(0, what ? fRefPedRmsClosedLidsInner : fRefPedClosedLids,
611 x, what ? fRefPedRmsClosedLidsInner : fRefPedClosedLids);
612 l3->SetBit(kCanDelete);
613
614 if (geom->InheritsFrom("MGeomCamMagic"))
615 if (what)
616 {
617 const Double_t x2 = cam->GetNbinsX();
618
619 line.SetLineColor(kBlue);
620 line.DrawLine(398, fRefPedRmsGalacticOuter,
621 x2, fRefPedRmsGalacticOuter);
622
623 line.SetLineColor(kYellow);
624 line.DrawLine(398, fRefPedRmsExtraGalacticOuter,
625 x2, fRefPedRmsExtraGalacticOuter);
626
627 line.SetLineColor(kMagenta);
628 line.DrawLine(398, fRefPedRmsClosedLidsOuter,
629 x2, fRefPedRmsClosedLidsOuter);
630 }
631
632
633 leg->AddEntry(l1, "Galactic Source","l");
634 leg->AddEntry(l2, "Extra-Galactic Source","l");
635 leg->AddEntry(l3, "Closed Lids","l");
636 leg->Draw();
637}
638
639void MJPedestal::DisplayOutliers(TH1D *hist) const
640{
641 const Float_t mean = hist->GetFunction("gaus")->GetParameter(1);
642 const Float_t lolim = mean - 3.5*hist->GetFunction("gaus")->GetParameter(2);
643 const Float_t uplim = mean + 3.5*hist->GetFunction("gaus")->GetParameter(2);
644 const Stat_t dead = hist->Integral(0,hist->FindBin(lolim)-1);
645 const Stat_t noisy = hist->Integral(hist->FindBin(uplim)+1,hist->GetNbinsX()+1);
646
647 TLatex deadtex;
648 deadtex.SetTextSize(0.06);
649 deadtex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.1,Form("%3i dead pixels",(Int_t)dead));
650
651 TLatex noisytex;
652 noisytex.SetTextSize(0.06);
653 noisytex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.2,Form("%3i noisy pixels",(Int_t)noisy));
654}
655
656void MJPedestal::FixDataCheckHist(TH1D *hist) const
657{
658 hist->SetDirectory(NULL);
659 hist->SetStats(0);
660
661 //
662 // set the labels bigger
663 //
664 TAxis *xaxe = hist->GetXaxis();
665 TAxis *yaxe = hist->GetYaxis();
666
667 xaxe->CenterTitle();
668 yaxe->CenterTitle();
669 xaxe->SetTitleSize(0.06);
670 yaxe->SetTitleSize(0.06);
671 xaxe->SetTitleOffset(0.8);
672 yaxe->SetTitleOffset(0.5);
673 xaxe->SetLabelSize(0.05);
674 yaxe->SetLabelSize(0.05);
675}
676
677/*
678Bool_t MJPedestal::WriteEventloop(MEvtLoop &evtloop) const
679{
680 if (fOutputPath.IsNull())
681 return kTRUE;
682
683 const TString oname(GetOutputFile());
684
685 *fLog << inf << "Writing to file: " << oname << endl;
686
687 TFile file(oname, fOverwrite?"RECREATE":"NEW", "File created by MJPedestal", 9);
688 if (!file.IsOpen())
689 {
690 *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
691 return kFALSE;
692 }
693
694 if (evtloop.Write(fName)<=0)
695 {
696 *fLog << err << "Unable to write MEvtloop to " << oname << endl;
697 return kFALSE;
698 }
699
700 return kTRUE;
701}
702*/
703
704void MJPedestal::SetExtractor(MExtractor* ext)
705{
706 if (ext)
707 {
708 if (fExtractor)
709 delete fExtractor;
710 fExtractor = ext ? (MExtractor*)ext->Clone(ext->GetName()) : NULL;
711 }
712 else
713 fExtractor = 0;
714}
715
716// --------------------------------------------------------------------------
717//
718// Read the following values from resource file:
719//
720// PedestalMin
721// PedestalMax
722//
723// PedRmsMin
724// PedRmsMax
725//
726// RefPedClosedLids
727// RefPedExtraGalactic
728// RefPedGalactic
729//
730// RefPedRmsClosedLidsInner
731// RefPedRmsExtraGalacticInner
732// RefPedRmsGalacticInner
733// RefPedRmsClosedLidsOuter
734// RefPedRmsExtraGalacticOuter
735// RefPedRmsGalacticOuter
736//
737void MJPedestal::ReadReferenceFile()
738{
739 TEnv refenv(fReferenceFile);
740
741 fPedestalMin = refenv.GetValue("PedestalMin",fPedestalMin);
742 fPedestalMax = refenv.GetValue("PedestalMax",fPedestalMax);
743 fPedRmsMin = refenv.GetValue("PedRmsMin",fPedRmsMin);
744 fPedRmsMax = refenv.GetValue("PedRmsMax",fPedRmsMax);
745 fRefPedClosedLids = refenv.GetValue("RefPedClosedLids",fRefPedClosedLids);
746 fRefPedExtraGalactic = refenv.GetValue("RefPedExtraGalactic",fRefPedExtraGalactic);
747 fRefPedGalactic = refenv.GetValue("RefPedGalactic",fRefPedGalactic);
748 fRefPedRmsClosedLidsInner = refenv.GetValue("RefPedRmsClosedLidsInner",fRefPedRmsClosedLidsInner);
749 fRefPedRmsExtraGalacticInner = refenv.GetValue("RefPedRmsExtraGalacticInner",fRefPedRmsExtraGalacticInner);
750 fRefPedRmsGalacticInner = refenv.GetValue("RefPedRmsGalacticInner",fRefPedRmsGalacticInner);
751 fRefPedRmsClosedLidsOuter = refenv.GetValue("RefPedRmsClosedLidsOuter",fRefPedRmsClosedLidsOuter);
752 fRefPedRmsExtraGalacticOuter = refenv.GetValue("RefPedRmsExtraGalacticOuter",fRefPedRmsExtraGalacticOuter);
753 fRefPedRmsGalacticOuter = refenv.GetValue("RefPedRmsGalacticOuter",fRefPedRmsGalacticOuter);
754}
755
756// --------------------------------------------------------------------------
757//
758// The following resource options are available:
759//
760// Do a datacheck run (read raw-data and enable display)
761// Prefix.DataCheck: Yes, No <default>
762//
763// Setup display type
764// Prefix.Display: normal <default>, datacheck, none
765//
766// Use cosmic data instead of pedestal data (DatRuns)
767// Prefix.UseData: Yes, No <default>
768//
769// Write an output file with pedestals and status-display
770// Prefix.DisableOutput: Yes, No <default>
771//
772// Name of a file containing reference values (see ReadReferenceFile)
773// Prefix.ReferenceFile: filename
774// (see ReadReferenceFile)
775//
776Bool_t MJPedestal::CheckEnvLocal()
777{
778 if (HasEnv("Display"))
779 {
780 TString type = GetEnv("Display", "normal");
781 type.ToLower();
782 if (type==(TString)"normal")
783 fDisplayType = kDisplayNormal;
784 if (type==(TString)"datacheck")
785 fDisplayType = kDisplayDataCheck;
786 if (type==(TString)"none")
787 fDisplayType = kDisplayNone;
788 }
789
790 if (!MJCalib::CheckEnvLocal())
791 return kFALSE;
792
793 if (HasEnv("UseData"))
794 fExtractType = GetEnv("UseData",kFALSE) ? kUseData : kUsePedRun;
795
796 if (IsUseMC() && fExtractType==kUseData)
797 {
798 // The reason is, that the standard data files contains empty
799 // (untriggered) events. If we would loop over the default 500
800 // first events of the data file you would calculate the
801 // pedestal from only some single events...
802 *fLog << inf;
803 *fLog << "Sorry, you cannot extract the starting pedestal from the first" << endl;
804 *fLog << "events in your data files... using pedestal file instead. The" << endl;
805 *fLog << "result should not differ..." << endl;
806 fExtractType = kUsePedRun;
807 }
808
809
810 if (HasEnv("UseHists"))
811 if (GetEnv("UseHists",kFALSE))
812 fIsUseHists = kTRUE;
813
814 SetNoStorage(GetEnv("DisableOutput", IsNoStorage()));
815
816 MTaskEnv tenv("ExtractSignal");
817 tenv.SetDefault(fExtractor);
818
819 if (tenv.ReadEnv(*GetEnv(), GetEnvPrefix()+".ExtractSignal", GetEnvDebug()>2)==kERROR)
820 return kFALSE;
821
822 if (fExtractor==tenv.GetTask())
823 return kTRUE;
824
825 if (!tenv.GetTask()->InheritsFrom(MExtractor::Class()))
826 {
827 *fLog << err << "ERROR: ExtractSignal from resource file doesn't inherit from MExtractor.... abort." << endl;
828 return kFALSE;
829 }
830
831 SetExtractor((MExtractor*)tenv.GetTask());
832
833 fBadPixelsFile = GetEnv("BadPixelsFile",fBadPixelsFile.Data());
834 fReferenceFile = GetEnv("ReferenceFile",fReferenceFile.Data());
835 ReadReferenceFile();
836
837 return kTRUE;
838}
839
840//---------------------------------------------------------------------------------
841//
842// Try to write the created MPedestalCam in the output file.
843// If possible, also an MBadPixelsCam and the corresponding display is written.
844//
845// In case of Storage type "kNoStorage" or if any of the containers
846// cannot be written, return kFALSE, otherwise kTRUE.
847//
848Bool_t MJPedestal::WriteResult()
849{
850 if (IsNoStorage())
851 return kTRUE;
852
853 TObjArray cont;
854
855 cont.Add(&fPedestalCamOut);
856 cont.Add(&fBadPixels);
857
858 return WriteContainer(cont, GetOutputFileName(), fOverwrite?"RECREATE":"NEW");
859}
860
861Bool_t MJPedestal::Process()
862{
863 if (!ReadPedestalCam())
864 return ProcessFile();
865
866 return kTRUE;
867}
868
869Bool_t MJPedestal::ProcessFile()
870{
871 if (!fSequence.IsValid())
872 {
873 if (!fRuns)
874 {
875 *fLog << err << "Neither AddRuns nor SetSequence nor SetEnv was called... abort." << endl;
876 return kFALSE;
877 }
878 if (fRuns && fRuns->GetNumRuns() != fRuns->GetNumEntries())
879 {
880 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
881 return kFALSE;
882 }
883 }
884
885 if (!CheckEnv())
886 return kFALSE;
887
888 // --------------------------------------------------------------------------------
889
890 const TString type = IsUseData() ? "data" : "pedestal";
891
892 *fLog << inf;
893 fLog->Separator(GetDescriptor());
894 *fLog << "Calculate MPedestalCam from " << type << "-runs ";
895 if (fSequence.IsValid())
896 *fLog << fSequence.GetName() << endl;
897 else
898 if (fRuns)
899 *fLog << fRuns->GetRunsAsString() << endl;
900 else
901 *fLog << "in Resource File." << endl;
902 *fLog << endl;
903
904 // --------------------------------------------------------------------------------
905
906 MParList plist;
907 MTaskList tlist;
908 plist.AddToList(&tlist);
909 plist.AddToList(this); // take care of fDisplay!
910
911 MReadMarsFile read("Events");
912 MRawFileRead rawread(NULL);
913
914 MDirIter iter;
915 if (fSequence.IsValid())
916 {
917 const Int_t n0 = IsUseData()
918 ? fSequence.SetupDatRuns(iter, fPathData, "D", IsUseRawData())
919 : fSequence.SetupPedRuns(iter, fPathData, "P", IsUseRawData());
920 const Int_t n1 = IsUseData()
921 ? fSequence.GetNumDatRuns()
922 : fSequence.GetNumPedRuns();
923 if (n0==0)
924 {
925 *fLog << err << "ERROR - No " << type << " input files of sequence found in " << (fPathData.IsNull()?"<default>":fPathData.Data()) << endl;
926 return kFALSE;
927 }
928 if (n0!=n1)
929 {
930 *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;
931 return kFALSE;
932 }
933 }
934
935 if (IsUseRawData())
936 {
937 if (fRuns || fSequence.IsValid())
938 rawread.AddFiles(fSequence.IsValid() ? iter : *fRuns);
939 tlist.AddToList(&rawread);
940 }
941 else
942 {
943 read.DisableAutoScheme();
944 if (fRuns || fSequence.IsValid())
945 read.AddFiles(fSequence.IsValid() ? iter : *fRuns);
946 tlist.AddToList(&read);
947 }
948
949 // Setup Tasklist
950 plist.AddToList(&fPedestalCamOut);
951 plist.AddToList(&fBadPixels);
952
953 //
954 // Read bad pixels from outside
955 //
956 if (!fBadPixelsFile.IsNull())
957 {
958 *fLog << inf << "Excluding: " << fBadPixelsFile << endl;
959 ifstream fin(fBadPixelsFile.Data());
960 fBadPixels.AsciiRead((istream&)fin);
961 }
962
963 MGeomApply geomapl;
964 MBadPixelsMerge merge(&fBadPixels);
965
966 MPedCalcPedRun pedcalc;
967 pedcalc.SetPedestalUpdate(kFALSE);
968
969 MPedCalcFromLoGain pedlogain;
970 pedlogain.SetPedestalUpdate(kFALSE);
971
972 MHPedestalCam hpedcam;
973 // fPedestalHist.SetRenorm(kTRUE);
974
975 MPedestalCam pedinter;
976 pedinter.SetName("MPedestalCamIntermediate");
977
978 MFillH fillped(&hpedcam, "MPedestalCamIntermediate", "FillPedCam");
979 MFillH fillpul("MHCalibrationPulseTimeCam", "MExtractedSignalCam", "FillPulseTime");
980 // MFillH fillped(&fPedestalHist, "MPedestalCamIntermediate", "FillPedCam");
981 fillped.SetBit(MFillH::kDoNotDisplay);
982 fillpul.SetBit(MFillH::kDoNotDisplay);
983
984 tlist.AddToList(&geomapl);
985 tlist.AddToList(&merge);
986
987 MFEventSelector sel;
988 sel.SetNumSelectEvts(0);
989
990 if (fExtractionType!=kFundamental)
991 {
992 fExtractor->SetFilter(&sel);
993 tlist.AddToList(&sel);
994 tlist.AddToList(fExtractor);
995 }
996
997 if (!fPathIn.IsNull())
998 {
999 fExtractor = ReadCalibration();
1000 if (!fExtractor)
1001 return kFALSE;
1002
1003 *fLog << all;
1004 *fLog << underline << "Signal Extractor found in calibration file:" << endl;
1005 fExtractor->Print();
1006 *fLog << endl;
1007 }
1008
1009 // ----------------------------------------------------------------------
1010 // Now we make sure, that in all cases the ranges are setup correctly
1011 // ----------------------------------------------------------------------
1012 MTaskEnv taskenv("ExtractPedestal");
1013 switch (fExtractType)
1014 {
1015 case kUsePedRun:
1016 // In case other than 'fundamental' second argument is obsolete
1017 // pedcalc.SetExtractWindow(0,14); // kUsePedRun (take default from class)
1018 taskenv.SetDefault(&pedcalc);
1019 tlist.AddToList(&taskenv);
1020 break;
1021
1022 case kUseData:
1023 // In case other than 'fundamental' second argument is obsolete
1024 // pedlogain.SetExtractWindow(15,14); // kUseData (take default from class)
1025 taskenv.SetDefault(&pedlogain);
1026 tlist.AddToList(&taskenv);
1027 break;
1028 }
1029
1030 if (fIsUseHists)
1031 {
1032 pedcalc.SetIntermediateStorage();
1033 pedlogain.SetIntermediateStorage();
1034 plist.AddToList(&pedinter);
1035 plist.AddToList(&hpedcam);
1036 // plist.AddToList(&fPedestalHist);
1037 tlist.AddToList(&fillped);
1038 }
1039
1040 pedcalc.SetPedestalsIn(&fPedestalCamIn);
1041 pedlogain.SetPedestalsIn(&fPedestalCamIn);
1042
1043 pedcalc.SetPedestalsInter(&pedinter);
1044 pedlogain.SetPedestalsInter(&pedinter);
1045 pedcalc.SetPedestalsOut(&fPedestalCamOut);
1046 pedlogain.SetPedestalsOut(&fPedestalCamOut);
1047
1048 // kFundamental
1049 if (fExtractor)
1050 {
1051
1052 if (fExtractionType!=kFundamental)
1053 {
1054 pedcalc.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1055 pedlogain.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1056
1057 pedcalc.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1058 pedlogain.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1059 }
1060
1061 if (fExtractor->InheritsFrom("MExtractTimeAndCharge"))
1062 {
1063
1064 const Float_t f = 0.1+fExtractor->GetHiGainFirst();
1065 const Int_t win = ((MExtractTimeAndCharge*)fExtractor)->GetWindowSizeHiGain();
1066 pedcalc.SetExtractWindow((Int_t)f, win);
1067 pedlogain.SetExtractWindow((Int_t)(15+f), win);
1068
1069 }
1070 else
1071 {
1072 const Float_t f = 0.1+fExtractor->GetHiGainFirst();
1073 const Float_t n = 0.1+fExtractor->GetNumHiGainSamples();
1074 pedcalc.SetExtractWindow((Int_t)f, (Int_t)n);
1075 pedlogain.SetExtractWindow((Int_t)(15+f), (Int_t)n);
1076
1077 if (fExtractionType!=kFundamental)
1078 {
1079 *fLog << inf;
1080 *fLog << "Signal extractor doesn't inherit from MExtractTimeAndCharge..." << endl;
1081 *fLog << " --> falling back to fundamental pedestal extraction." << endl;
1082 fExtractionType=kFundamental;
1083 }
1084 }
1085
1086
1087 }
1088 else
1089 {
1090 *fLog << warn << GetDescriptor() << ": WARNING - No extractor has been handed over! " << endl;
1091 *fLog << "Taking default window for pedestal extraction. The calculated pedestal RMS" << endl;
1092 *fLog << "will probably not match with future pedestal RMS' from different extraction" << endl;
1093 *fLog << "windows." << endl;
1094 }
1095
1096 /*
1097 switch (fExtractType)
1098 {
1099 case kUseData:
1100 *fLog << all << "TYPE: USEDATA " << fExtractor << endl;
1101 taskenv.SetDefault(&pedlogain);
1102 tlist.AddToList(&taskenv);
1103
1104 if (!SetupExtractor(plist, pedlogain))
1105 {
1106 *fLog << all << "SETTING TO: " << fExtractor << " " << fExtractor->GetNumHiGainSamples() << endl;
1107 fExtractor->Print();
1108 pedlogain.SetExtractWindow(15, (Int_t)TMath::Nint(fExtractor->GetNumHiGainSamples()));
1109 }
1110 break;
1111
1112 case kUsePedRun:
1113 *fLog << all << "TYPE: USEPEDRUN " << fExtractor << endl;
1114 taskenv.SetDefault(&pedcalc);
1115 tlist.AddToList(&taskenv);
1116
1117 if (!SetupExtractor(plist, pedcalc))
1118 pedcalc.SetExtractWindow(fExtractor->GetHiGainFirst(), TMath::Nint(fExtractor->GetNumHiGainSamples()));
1119 break;
1120
1121 case kUseHists:
1122 if (!fExtractor)
1123 {
1124 *fLog << err << GetDescriptor() << " - ERROR: ";
1125 *fLog << "Extraction Type is kUseHists, but no extractor was set" << endl;
1126 return kFALSE;
1127 }
1128
1129 tlist.AddToList(fExtractor);
1130 tlist.AddToList(&fillped);
1131 break;
1132 } */
1133
1134 /*
1135 if (!fPathIn.IsNull())
1136 {
1137 delete fExtractor;
1138 fExtractor = 0;
1139 }
1140 */
1141
1142
1143 //
1144 // Execute the eventloop
1145 //
1146 MEvtLoop evtloop(fName);
1147 evtloop.SetParList(&plist);
1148 evtloop.SetDisplay(fDisplay);
1149 evtloop.SetLogStream(fLog);
1150 if (!SetupEnv(evtloop))
1151 return kFALSE;
1152
1153 // if (!WriteEventloop(evtloop))
1154 // return kFALSE;
1155
1156 // Execute first analysis
1157 if (!evtloop.Eventloop(fMaxEvents))
1158 {
1159 *fLog << err << GetDescriptor() << ": Failed." << endl;
1160 return kFALSE;
1161 }
1162
1163 tlist.PrintStatistics();
1164
1165 if (fDisplayType!=kDisplayNone)
1166 DisplayResult(plist);
1167
1168 if (!WriteResult())
1169 return kFALSE;
1170
1171 *fLog << all << GetDescriptor() << ": Done." << endl;
1172 *fLog << endl << endl;
1173
1174 return kTRUE;
1175}
Note: See TracBrowser for help on using the repository browser.