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

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