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

Last change on this file since 6634 was 6569, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 34.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-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
62// Other basic classes
63#include "MExtractTimeAndCharge.h"
64
65// parameter containers
66#include "MGeomCam.h"
67#include "MHCamera.h"
68#include "MPedestalPix.h"
69
70#include "MCalibrationPedCam.h"
71#include "MCalibrationPix.h"
72#include "MHPedestalPix.h"
73
74// tasks
75#include "MReadMarsFile.h"
76#include "MRawFileRead.h"
77#include "MGeomApply.h"
78#include "MBadPixelsMerge.h"
79#include "MFillH.h"
80#include "MPedCalcPedRun.h"
81#include "MPedCalcFromLoGain.h"
82
83#include <fstream>
84
85ClassImp(MJPedestal);
86
87using namespace std;
88
89const TString MJPedestal::fgReferenceFile = "mjobs/pedestalref.rc";
90const TString MJPedestal::fgBadPixelsFile = "mjobs/badpixels_0_559.rc";
91// --------------------------------------------------------------------------
92//
93// Default constructor.
94//
95// Sets:
96// - fExtractor to NULL,
97// - fExtractType to kUsePedRun
98// - fStorage to Normal Storage
99// - fExtractorResolution to kFALSE
100//
101MJPedestal::MJPedestal(const char *name, const char *title)
102 : fExtractor(NULL), fDisplayType(kDisplayDataCheck),
103 fExtractType(kUsePedRun), fExtractionType(kFundamental), fIsUseHists(kFALSE)
104{
105 fName = name ? name : "MJPedestal";
106 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
107
108 SetNormalStorage();
109 SetUsePedRun();
110 SetPathIn("");
111 SetReferenceFile();
112 SetBadPixelsFile();
113
114 //
115 // Default references for case that no value references file is there
116 // (should not occur)
117 //
118
119 fPedestalMin = 4.;
120 fPedestalMax = 16.;
121 fPedRmsMin = 0.;
122 fPedRmsMax = 20.;
123 fRefPedClosedLids = 9.635;
124 fRefPedExtraGalactic = 9.93;
125 fRefPedGalactic = 10.03;
126 fRefPedRmsClosedLidsInner = 1.7;
127 fRefPedRmsExtraGalacticInner = 5.6;
128 fRefPedRmsGalacticInner = 6.92;
129 fRefPedRmsClosedLidsOuter = 1.7;
130 fRefPedRmsExtraGalacticOuter = 3.35;
131 fRefPedRmsGalacticOuter = 4.2;
132}
133
134MJPedestal::~MJPedestal()
135{
136 if (fExtractor)
137 delete fExtractor;
138}
139
140const char* MJPedestal::GetOutputFile() const
141{
142 const TString name(GetOutputFileName());
143 if (name.IsNull())
144 return "";
145
146 return Form("%s/%s", fPathOut.Data(), name.Data());
147}
148
149const char* MJPedestal::GetOutputFileName() const
150{
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 (fIsUseHists)
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 && !fIsUseHists && 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 (fIsUseHists)
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 //
417 // Set the datacheck sizes:
418 //
419 FixDataCheckHist((TH1D*)obj1);
420 //
421 // set reference lines
422 //
423 DisplayReferenceLines(obj1,0);
424 //
425 // end reference lines
426 //
427 c3.cd(3);
428 gPad->SetBorderMode(0);
429 obj1->SetPrettyPalette();
430 obj1->Draw();
431
432 c3.cd(5);
433 gPad->SetBorderMode(0);
434 gPad->SetTicks();
435 TH1D *obj2 = (TH1D*)obj1->Projection(obj1->GetName());
436 obj2->Draw();
437 obj2->SetBit(kCanDelete);
438 obj2->Fit("gaus","Q");
439 obj2->GetFunction("gaus")->SetLineColor(kYellow);
440 //
441 // Set the datacheck sizes:
442 //
443 FixDataCheckHist(obj2);
444 obj2->SetStats(1);
445
446 c3.cd(2);
447 gPad->SetBorderMode(0);
448 gPad->SetTicks();
449 MHCamera *obj3=(MHCamera*)disp1.DrawCopy("hist");
450 //
451 // for the datacheck, fix the ranges!!
452 //
453 obj3->SetMinimum(fPedRmsMin);
454 obj3->SetMaximum(fPedRmsMax);
455 //
456 // Set the datacheck sizes:
457 //
458 FixDataCheckHist((TH1D*)obj3);
459 //
460 // set reference lines
461 //
462 DisplayReferenceLines(obj3,1);
463
464 c3.cd(4);
465 gPad->SetBorderMode(0);
466 obj3->SetPrettyPalette();
467 obj3->Draw();
468
469 c3.cd(6);
470 gPad->SetBorderMode(0);
471
472 if (geomcam.InheritsFrom("MGeomCamMagic"))
473 {
474 TArrayI inner(1);
475 inner[0] = 0;
476
477 TArrayI outer(1);
478 outer[0] = 1;
479
480 TArrayI s0(6);
481 s0[0] = 6;
482 s0[1] = 1;
483 s0[2] = 2;
484 s0[3] = 3;
485 s0[4] = 4;
486 s0[5] = 5;
487
488 TArrayI s1(3);
489 s1[0] = 6;
490 s1[1] = 1;
491 s1[2] = 2;
492
493 TArrayI s2(3);
494 s2[0] = 3;
495 s2[1] = 4;
496 s2[2] = 5;
497
498 TVirtualPad *pad = gPad;
499 pad->Divide(2,1);
500
501 TH1D *inout[2];
502 inout[0] = disp1.ProjectionS(s0, inner, "Inner");
503 inout[1] = disp1.ProjectionS(s0, outer, "Outer");
504 FixDataCheckHist(inout[0]);
505 FixDataCheckHist(inout[1]);
506
507 inout[0]->SetTitle(Form("%s %s",disp1.GetTitle(),"Inner"));
508 inout[1]->SetTitle(Form("%s %s",disp1.GetTitle(),"Outer"));
509
510
511 for (int i=0; i<2; i++)
512 {
513 pad->cd(i+1);
514 gPad->SetBorderMode(0);
515 gPad->SetTicks();
516
517 inout[i]->SetDirectory(NULL);
518 inout[i]->SetLineColor(kRed+i);
519 inout[i]->SetBit(kCanDelete);
520 inout[i]->Draw();
521 inout[i]->Fit("gaus", "Q");
522
523 TLegend *leg2 = new TLegend(0.6,0.2,0.9,0.55);
524 leg2->SetHeader(inout[i]->GetName());
525 leg2->AddEntry(inout[i], inout[i]->GetName(), "l");
526
527 //
528 // Display the outliers as dead and noisy pixels
529 //
530 DisplayOutliers(inout[i]);
531
532 //
533 // Display the two half of the camera separately
534 //
535 TH1D *half[2];
536 half[0] = disp1.ProjectionS(s1, i==0 ? inner : outer , "Sector 6-1-2");
537 half[1] = disp1.ProjectionS(s2, i==0 ? inner : outer , "Sector 3-4-5");
538
539 for (int j=0; j<2; j++)
540 {
541 half[j]->SetLineColor(kRed+i+2*j+1);
542 half[j]->SetDirectory(NULL);
543 half[j]->SetBit(kCanDelete);
544 half[j]->Draw("same");
545 leg2->AddEntry(half[j], half[j]->GetName(), "l");
546 }
547 leg2->Draw();
548 delete leg2;
549 }
550 return;
551 }
552 }
553
554 if (fExtractionType!=kFundamental/*fExtractorResolution*/)
555 {
556
557 TCanvas &c3 = fDisplay->AddTab(fExtractionType==kWithExtractor?"PedExtrd":"PedRndm");
558 c3.Divide(2,3);
559
560 disp0.CamDraw(c3, 1, 2, 1);
561 disp1.CamDraw(c3, 2, 2, 6);
562
563 TCanvas &c13 = fDisplay->AddTab(fExtractionType==kWithExtractor?"DiffExtrd":"DiffRndm");
564 c13.Divide(2,3);
565
566 disp9.CamDraw(c13, 1, 2, 5);
567 disp10.CamDraw(c13, 2, 2, 5);
568 return;
569 }
570}
571
572
573void MJPedestal::DisplayReferenceLines(MHCamera *cam, const Int_t what) const
574{
575
576 Double_t x = cam->GetNbinsX();
577
578 const MGeomCam *geom = cam->GetGeometry();
579
580 if (geom->InheritsFrom("MGeomCamMagic"))
581 x = what ? 397 : cam->GetNbinsX();
582
583 TLine line;
584 line.SetLineStyle(kDashed);
585 line.SetLineWidth(3);
586 line.SetLineColor(kBlue);
587
588 TLegend *leg = new TLegend(0.6,0.75,0.9,0.99);
589 leg->SetBit(kCanDelete);
590
591 if (fExtractionType==kWithExtractorRndm && !(what))
592 {
593 TLine *l0 = line.DrawLine(0,0.,cam->GetNbinsX(),0.);
594 l0->SetBit(kCanDelete);
595 leg->AddEntry(l0, "Reference","l");
596 leg->Draw();
597 return;
598 }
599
600 line.SetLineColor(kBlue);
601 TLine *l1 = line.DrawLine(0, what ? fRefPedRmsGalacticInner : fRefPedGalactic,
602 x, what ? fRefPedRmsGalacticInner : fRefPedGalactic);
603 l1->SetBit(kCanDelete);
604 line.SetLineColor(kYellow);
605 TLine *l2 = line.DrawLine(0, what ? fRefPedRmsExtraGalacticInner : fRefPedExtraGalactic,
606 x, what ? fRefPedRmsExtraGalacticInner : fRefPedExtraGalactic);
607 l2->SetBit(kCanDelete);
608 line.SetLineColor(kMagenta);
609 TLine *l3 = line.DrawLine(0, what ? fRefPedRmsClosedLidsInner : fRefPedClosedLids,
610 x, what ? fRefPedRmsClosedLidsInner : fRefPedClosedLids);
611 l3->SetBit(kCanDelete);
612
613 if (geom->InheritsFrom("MGeomCamMagic"))
614 if (what)
615 {
616 const Double_t x2 = cam->GetNbinsX();
617
618 line.SetLineColor(kBlue);
619 line.DrawLine(398, fRefPedRmsGalacticOuter,
620 x2, fRefPedRmsGalacticOuter);
621
622 line.SetLineColor(kYellow);
623 line.DrawLine(398, fRefPedRmsExtraGalacticOuter,
624 x2, fRefPedRmsExtraGalacticOuter);
625
626 line.SetLineColor(kMagenta);
627 line.DrawLine(398, fRefPedRmsClosedLidsOuter,
628 x2, fRefPedRmsClosedLidsOuter);
629 }
630
631
632 leg->AddEntry(l1, "Galactic Source","l");
633 leg->AddEntry(l2, "Extra-Galactic Source","l");
634 leg->AddEntry(l3, "Closed Lids","l");
635 leg->Draw();
636}
637
638void MJPedestal::DisplayOutliers(TH1D *hist) const
639{
640 const Float_t mean = hist->GetFunction("gaus")->GetParameter(1);
641 const Float_t lolim = mean - 3.5*hist->GetFunction("gaus")->GetParameter(2);
642 const Float_t uplim = mean + 3.5*hist->GetFunction("gaus")->GetParameter(2);
643 const Stat_t dead = hist->Integral(0,hist->FindBin(lolim)-1);
644 const Stat_t noisy = hist->Integral(hist->FindBin(uplim)+1,hist->GetNbinsX()+1);
645
646 TLatex deadtex;
647 deadtex.SetTextSize(0.06);
648 deadtex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.1,Form("%3i dead pixels",(Int_t)dead));
649
650 TLatex noisytex;
651 noisytex.SetTextSize(0.06);
652 noisytex.DrawLatex(0.1,hist->GetBinContent(hist->GetMaximumBin())/1.2,Form("%3i noisy pixels",(Int_t)noisy));
653}
654
655void MJPedestal::FixDataCheckHist(TH1D *hist) const
656{
657 hist->SetDirectory(NULL);
658 hist->SetStats(0);
659
660 //
661 // set the labels bigger
662 //
663 TAxis *xaxe = hist->GetXaxis();
664 TAxis *yaxe = hist->GetYaxis();
665
666 xaxe->CenterTitle();
667 yaxe->CenterTitle();
668 xaxe->SetTitleSize(0.06);
669 yaxe->SetTitleSize(0.06);
670 xaxe->SetTitleOffset(0.8);
671 yaxe->SetTitleOffset(0.5);
672 xaxe->SetLabelSize(0.05);
673 yaxe->SetLabelSize(0.05);
674}
675
676/*
677Bool_t MJPedestal::WriteEventloop(MEvtLoop &evtloop) const
678{
679 if (fOutputPath.IsNull())
680 return kTRUE;
681
682 const TString oname(GetOutputFile());
683
684 *fLog << inf << "Writing to file: " << oname << endl;
685
686 TFile file(oname, fOverwrite?"RECREATE":"NEW", "File created by MJPedestal", 9);
687 if (!file.IsOpen())
688 {
689 *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
690 return kFALSE;
691 }
692
693 if (evtloop.Write(fName)<=0)
694 {
695 *fLog << err << "Unable to write MEvtloop to " << oname << endl;
696 return kFALSE;
697 }
698
699 return kTRUE;
700}
701*/
702
703void MJPedestal::SetExtractor(MExtractor* ext)
704{
705 if (ext)
706 {
707 if (fExtractor)
708 delete fExtractor;
709 fExtractor = ext ? (MExtractor*)ext->Clone(ext->GetName()) : NULL;
710 }
711 else
712 fExtractor = 0;
713}
714
715// --------------------------------------------------------------------------
716//
717// Read the following values from resource file:
718//
719// PedestalMin
720// PedestalMax
721//
722// PedRmsMin
723// PedRmsMax
724//
725// RefPedClosedLids
726// RefPedExtraGalactic
727// RefPedGalactic
728//
729// RefPedRmsClosedLidsInner
730// RefPedRmsExtraGalacticInner
731// RefPedRmsGalacticInner
732// RefPedRmsClosedLidsOuter
733// RefPedRmsExtraGalacticOuter
734// RefPedRmsGalacticOuter
735//
736void MJPedestal::ReadReferenceFile()
737{
738 TEnv refenv(fReferenceFile);
739
740 fPedestalMin = refenv.GetValue("PedestalMin",fPedestalMin);
741 fPedestalMax = refenv.GetValue("PedestalMax",fPedestalMax);
742 fPedRmsMin = refenv.GetValue("PedRmsMin",fPedRmsMin);
743 fPedRmsMax = refenv.GetValue("PedRmsMax",fPedRmsMax);
744 fRefPedClosedLids = refenv.GetValue("RefPedClosedLids",fRefPedClosedLids);
745 fRefPedExtraGalactic = refenv.GetValue("RefPedExtraGalactic",fRefPedExtraGalactic);
746 fRefPedGalactic = refenv.GetValue("RefPedGalactic",fRefPedGalactic);
747 fRefPedRmsClosedLidsInner = refenv.GetValue("RefPedRmsClosedLidsInner",fRefPedRmsClosedLidsInner);
748 fRefPedRmsExtraGalacticInner = refenv.GetValue("RefPedRmsExtraGalacticInner",fRefPedRmsExtraGalacticInner);
749 fRefPedRmsGalacticInner = refenv.GetValue("RefPedRmsGalacticInner",fRefPedRmsGalacticInner);
750 fRefPedRmsClosedLidsOuter = refenv.GetValue("RefPedRmsClosedLidsOuter",fRefPedRmsClosedLidsOuter);
751 fRefPedRmsExtraGalacticOuter = refenv.GetValue("RefPedRmsExtraGalacticOuter",fRefPedRmsExtraGalacticOuter);
752 fRefPedRmsGalacticOuter = refenv.GetValue("RefPedRmsGalacticOuter",fRefPedRmsGalacticOuter);
753}
754
755// --------------------------------------------------------------------------
756//
757// The following resource options are available:
758//
759// Do a datacheck run (read raw-data and enable display)
760// Prefix.DataCheck: Yes, No <default>
761//
762// Setup display type
763// Prefix.Display: normal <default>, datacheck, none
764//
765// Use cosmic data instead of pedestal data (DatRuns)
766// Prefix.UseData: Yes, No <default>
767//
768// Write an output file with pedestals and status-display
769// Prefix.DisableOutput: Yes, No <default>
770//
771// Name of a file containing reference values (see ReadReferenceFile)
772// Prefix.ReferenceFile: filename
773// (see ReadReferenceFile)
774//
775Bool_t MJPedestal::CheckEnvLocal()
776{
777 if (HasEnv("Display"))
778 {
779 TString type = GetEnv("Display", "normal");
780 type.ToLower();
781 if (type==(TString)"normal")
782 fDisplayType = kDisplayNormal;
783 if (type==(TString)"datacheck")
784 fDisplayType = kDisplayDataCheck;
785 if (type==(TString)"none")
786 fDisplayType = kDisplayNone;
787 }
788
789 if (!MJCalib::CheckEnvLocal())
790 return kFALSE;
791
792 if (HasEnv("UseData"))
793 fExtractType = GetEnv("UseData",kFALSE) ? kUseData : kUsePedRun;
794
795 if (IsUseMC() && fExtractType==kUseData)
796 {
797 // The reason is, that the standard data files contains empty
798 // (untriggered) events. If we would loop over the default 500
799 // first events of the data file you would calculate the
800 // pedestal from only some single events...
801 *fLog << inf;
802 *fLog << "Sorry, you cannot extract the starting pedestal from the first" << endl;
803 *fLog << "events in your data files... using pedestal file instead. The" << endl;
804 *fLog << "result should not differ..." << endl;
805 fExtractType = kUsePedRun;
806 }
807
808
809 if (HasEnv("UseHists"))
810 if (GetEnv("UseHists",kFALSE))
811 fIsUseHists = kTRUE;
812
813 SetNoStorage(GetEnv("DisableOutput", IsNoStorage()));
814
815 MTaskEnv tenv("ExtractSignal");
816 tenv.SetDefault(fExtractor);
817
818 if (tenv.ReadEnv(*GetEnv(), GetEnvPrefix()+".ExtractSignal", GetEnvDebug()>2)==kERROR)
819 return kFALSE;
820
821 if (fExtractor==tenv.GetTask())
822 return kTRUE;
823
824 if (!tenv.GetTask()->InheritsFrom(MExtractor::Class()))
825 {
826 *fLog << err << "ERROR: ExtractSignal from resource file doesn't inherit from MExtractor.... abort." << endl;
827 return kFALSE;
828 }
829
830 SetExtractor((MExtractor*)tenv.GetTask());
831
832 fBadPixelsFile = GetEnv("BadPixelsFile",fBadPixelsFile.Data());
833 fReferenceFile = GetEnv("ReferenceFile",fReferenceFile.Data());
834 ReadReferenceFile();
835
836 return kTRUE;
837}
838
839//---------------------------------------------------------------------------------
840//
841// Try to write the created MPedestalCam in the output file.
842// If possible, also an MBadPixelsCam and the corresponding display is written.
843//
844// In case of Storage type "kNoStorage" or if any of the containers
845// cannot be written, return kFALSE, otherwise kTRUE.
846//
847Bool_t MJPedestal::WriteResult()
848{
849 if (IsNoStorage())
850 return kTRUE;
851
852 TObjArray cont;
853
854 cont.Add(&fPedestalCamOut);
855 cont.Add(&fBadPixels);
856
857 return WriteContainer(cont, GetOutputFileName(), fOverwrite?"RECREATE":"NEW");
858}
859
860Bool_t MJPedestal::Process()
861{
862 if (!ReadPedestalCam())
863 return ProcessFile();
864
865 return kTRUE;
866}
867
868Bool_t MJPedestal::ProcessFile()
869{
870 if (!fSequence.IsValid())
871 {
872 if (!fRuns)
873 {
874 *fLog << err << "Neither AddRuns nor SetSequence nor SetEnv was called... abort." << endl;
875 return kFALSE;
876 }
877 if (fRuns && fRuns->GetNumRuns() != fRuns->GetNumEntries())
878 {
879 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
880 return kFALSE;
881 }
882 }
883
884 if (!CheckEnv())
885 return kFALSE;
886
887 // --------------------------------------------------------------------------------
888
889 const TString type = IsUseData() ? "data" : "pedestal";
890
891 *fLog << inf;
892 fLog->Separator(GetDescriptor());
893 *fLog << "Calculate MPedestalCam from " << type << "-runs ";
894 if (fSequence.IsValid())
895 *fLog << fSequence.GetName() << endl;
896 else
897 if (fRuns)
898 *fLog << fRuns->GetRunsAsString() << endl;
899 else
900 *fLog << "in Resource File." << endl;
901 *fLog << endl;
902
903 // --------------------------------------------------------------------------------
904
905 MParList plist;
906 MTaskList tlist;
907 plist.AddToList(&tlist);
908 plist.AddToList(this); // take care of fDisplay!
909
910 MReadMarsFile read("Events");
911 MRawFileRead rawread(NULL);
912
913 MDirIter iter;
914 if (fSequence.IsValid())
915 {
916 const Int_t n0 = IsUseData()
917 ? fSequence.SetupDatRuns(iter, fPathData, "D", IsUseRawData())
918 : fSequence.SetupPedRuns(iter, fPathData, "P", IsUseRawData());
919 const Int_t n1 = IsUseData()
920 ? fSequence.GetNumDatRuns()
921 : fSequence.GetNumPedRuns();
922 if (n0==0)
923 {
924 *fLog << err << "ERROR - No " << type << " input files of sequence found in " << (fPathData.IsNull()?"<default>":fPathData.Data()) << endl;
925 return kFALSE;
926 }
927 if (n0!=n1)
928 {
929 *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;
930 return kFALSE;
931 }
932 }
933
934 if (IsUseRawData())
935 {
936 if (fRuns || fSequence.IsValid())
937 rawread.AddFiles(fSequence.IsValid() ? iter : *fRuns);
938 tlist.AddToList(&rawread);
939 }
940 else
941 {
942 read.DisableAutoScheme();
943 if (fRuns || fSequence.IsValid())
944 read.AddFiles(fSequence.IsValid() ? iter : *fRuns);
945 tlist.AddToList(&read);
946 }
947
948 // Setup Tasklist
949 plist.AddToList(&fPedestalCamOut);
950 plist.AddToList(&fBadPixels);
951
952 //
953 // Read bad pixels from outside
954 //
955 if (!fBadPixelsFile.IsNull())
956 {
957 *fLog << inf << "Excluding: " << fBadPixelsFile << endl;
958 ifstream fin(fBadPixelsFile.Data());
959 fBadPixels.AsciiRead((istream&)fin);
960 }
961
962 MGeomApply geomapl;
963 MBadPixelsMerge merge(&fBadPixels);
964
965 MPedCalcPedRun pedcalc;
966 pedcalc.SetPedestalUpdate(kFALSE);
967
968 MPedCalcFromLoGain pedlogain;
969 pedlogain.SetPedestalUpdate(kFALSE);
970
971 MHPedestalCam hpedcam;
972 // fPedestalHist.SetRenorm(kTRUE);
973
974 MPedestalCam pedinter;
975 pedinter.SetName("MPedestalCamIntermediate");
976
977 MFillH fillped(&hpedcam, "MPedestalCamIntermediate", "FillPedCam");
978 // MFillH fillped(&fPedestalHist, "MPedestalCamIntermediate", "FillPedCam");
979 // fillped.SetBit(MFillH::kDoNotDisplay);
980
981 tlist.AddToList(&geomapl);
982 tlist.AddToList(&merge);
983
984 if (!fPathIn.IsNull())
985 {
986 fExtractor = ReadCalibration();
987 if (!fExtractor)
988 return kFALSE;
989
990 *fLog << all;
991 *fLog << underline << "Signal Extractor found in calibration file:" << endl;
992 fExtractor->Print();
993 *fLog << endl;
994 }
995
996 // ----------------------------------------------------------------------
997 // Now we make sure, that in all cases the ranges are setup correctly
998 // ----------------------------------------------------------------------
999 MTaskEnv taskenv("ExtractPedestal");
1000 switch (fExtractType)
1001 {
1002 case kUsePedRun:
1003 // In case other than 'fundamental' second argument is obsolete
1004 // pedcalc.SetExtractWindow(0,14); // kUsePedRun (take default from class)
1005 taskenv.SetDefault(&pedcalc);
1006 tlist.AddToList(&taskenv);
1007 break;
1008
1009 case kUseData:
1010 // In case other than 'fundamental' second argument is obsolete
1011 // pedlogain.SetExtractWindow(15,14); // kUseData (take default from class)
1012 taskenv.SetDefault(&pedlogain);
1013 tlist.AddToList(&taskenv);
1014 break;
1015 }
1016
1017 if (fIsUseHists)
1018 {
1019 pedcalc.SetIntermediateStorage();
1020 pedlogain.SetIntermediateStorage();
1021 plist.AddToList(&pedinter);
1022 plist.AddToList(&hpedcam);
1023 // plist.AddToList(&fPedestalHist);
1024 tlist.AddToList(&fillped);
1025 }
1026
1027 pedcalc.SetPedestalsIn(&fPedestalCamIn);
1028 pedlogain.SetPedestalsIn(&fPedestalCamIn);
1029
1030 pedcalc.SetPedestalsInter(&pedinter);
1031 pedlogain.SetPedestalsInter(&pedinter);
1032 pedcalc.SetPedestalsOut(&fPedestalCamOut);
1033 pedlogain.SetPedestalsOut(&fPedestalCamOut);
1034
1035 // kFundamental
1036 if (fExtractor)
1037 {
1038
1039 if (fExtractor->InheritsFrom("MExtractTimeAndCharge"))
1040 {
1041
1042 const Float_t f = 0.5+fExtractor->GetHiGainFirst();
1043 const Int_t win = ((MExtractTimeAndCharge*)fExtractor)->GetWindowSizeHiGain();
1044 pedcalc.SetExtractWindow((Int_t)f, win);
1045 pedlogain.SetExtractWindow((Int_t)(15+f), win);
1046
1047 }
1048 else
1049 {
1050 const Float_t f = 0.5+fExtractor->GetHiGainFirst();
1051 const Float_t n = 0.5+fExtractor->GetNumHiGainSamples();
1052 pedcalc.SetExtractWindow((Int_t)f, (Int_t)n);
1053 pedlogain.SetExtractWindow((Int_t)(15+f), (Int_t)n);
1054
1055 if (fExtractionType!=kFundamental)
1056 {
1057 *fLog << inf;
1058 *fLog << "Signal extractor doesn't inherit from MExtractTimeAndCharge..." << endl;
1059 *fLog << " --> falling back to fundamental pedestal extraction." << endl;
1060 fExtractionType=kFundamental;
1061 }
1062 }
1063
1064
1065 if (fExtractionType!=kFundamental)
1066 {
1067 pedcalc.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1068 pedlogain.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
1069
1070 pedcalc.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1071 pedlogain.SetExtractor((MExtractTimeAndCharge*)fExtractor);
1072 }
1073 }
1074 else
1075 {
1076 *fLog << warn << GetDescriptor() << ": WARNING - No extractor has been handed over! " << endl;
1077 *fLog << "Taking default window for pedestal extraction. The calculated pedestal RMS" << endl;
1078 *fLog << "will probably not match with future pedestal RMS' from different extraction" << endl;
1079 *fLog << "windows." << endl;
1080 }
1081
1082 /*
1083 switch (fExtractType)
1084 {
1085 case kUseData:
1086 *fLog << all << "TYPE: USEDATA " << fExtractor << endl;
1087 taskenv.SetDefault(&pedlogain);
1088 tlist.AddToList(&taskenv);
1089
1090 if (!SetupExtractor(plist, pedlogain))
1091 {
1092 *fLog << all << "SETTING TO: " << fExtractor << " " << fExtractor->GetNumHiGainSamples() << endl;
1093 fExtractor->Print();
1094 pedlogain.SetExtractWindow(15, (Int_t)TMath::Nint(fExtractor->GetNumHiGainSamples()));
1095 }
1096 break;
1097
1098 case kUsePedRun:
1099 *fLog << all << "TYPE: USEPEDRUN " << fExtractor << endl;
1100 taskenv.SetDefault(&pedcalc);
1101 tlist.AddToList(&taskenv);
1102
1103 if (!SetupExtractor(plist, pedcalc))
1104 pedcalc.SetExtractWindow(fExtractor->GetHiGainFirst(), TMath::Nint(fExtractor->GetNumHiGainSamples()));
1105 break;
1106
1107 case kUseHists:
1108 if (!fExtractor)
1109 {
1110 *fLog << err << GetDescriptor() << " - ERROR: ";
1111 *fLog << "Extraction Type is kUseHists, but no extractor was set" << endl;
1112 return kFALSE;
1113 }
1114
1115 tlist.AddToList(fExtractor);
1116 tlist.AddToList(&fillped);
1117 break;
1118 } */
1119
1120 /*
1121 if (!fPathIn.IsNull())
1122 {
1123 delete fExtractor;
1124 fExtractor = 0;
1125 }
1126 */
1127 //
1128 // Execute the eventloop
1129 //
1130 MEvtLoop evtloop(fName);
1131 evtloop.SetParList(&plist);
1132 evtloop.SetDisplay(fDisplay);
1133 evtloop.SetLogStream(fLog);
1134 if (!SetupEnv(evtloop))
1135 return kFALSE;
1136
1137 // if (!WriteEventloop(evtloop))
1138 // return kFALSE;
1139
1140 // Execute first analysis
1141 if (!evtloop.Eventloop(fMaxEvents))
1142 {
1143 *fLog << err << GetDescriptor() << ": Failed." << endl;
1144 return kFALSE;
1145 }
1146
1147 tlist.PrintStatistics();
1148
1149 if (fDisplayType!=kDisplayNone)
1150 DisplayResult(plist);
1151
1152 if (!WriteResult())
1153 return kFALSE;
1154
1155 *fLog << all << GetDescriptor() << ": Done." << endl;
1156 *fLog << endl << endl;
1157
1158 return kTRUE;
1159}
Note: See TracBrowser for help on using the repository browser.