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

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