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

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