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

Last change on this file since 5869 was 5842, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 30.3 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::ProcessFile()
786{
787 if (!fSequence.IsValid())
788 {
789 if (!fRuns)
790 {
791 *fLog << err << "Neither AddRuns nor SetSequence nor SetEnv was called... abort." << endl;
792 return kFALSE;
793 }
794 if (fRuns && fRuns->GetNumRuns() != fRuns->GetNumEntries())
795 {
796 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
797 return kFALSE;
798 }
799 }
800
801 if (!CheckEnv())
802 return kFALSE;
803
804 // --------------------------------------------------------------------------------
805
806 const TString type = IsUseData() ? "data" : "pedestal";
807
808 *fLog << inf;
809 fLog->Separator(GetDescriptor());
810 *fLog << "Calculate MPedestalCam from " << type << "-runs ";
811 if (fSequence.IsValid())
812 *fLog << fSequence.GetName() << endl;
813 else
814 if (fRuns)
815 *fLog << fRuns->GetRunsAsString() << endl;
816 else
817 *fLog << "in Resource File." << endl;
818 *fLog << endl;
819
820 // --------------------------------------------------------------------------------
821
822 MParList plist;
823 MTaskList tlist;
824 plist.AddToList(&tlist);
825 plist.AddToList(this); // take care of fDisplay!
826
827 MReadMarsFile read("Events");
828 MRawFileRead rawread(NULL);
829
830 MDirIter iter;
831 if (fSequence.IsValid())
832 {
833 const Int_t n0 = IsUseData() ? fSequence.SetupDatRuns(iter, fPathData, "D", fDataCheck) : fSequence.SetupPedRuns(iter, fPathData, "P", fDataCheck);
834 const Int_t n1 = IsUseData() ? fSequence.GetNumDatRuns() : fSequence.GetNumPedRuns();
835 if (n0==0)
836 {
837 *fLog << err << "ERROR - No " << type << " input files of sequence found in " << (fPathData.IsNull()?"<defaul>":fPathData.Data()) << endl;
838 return kFALSE;
839 }
840 if (n0!=n1)
841 {
842 *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;
843 return kFALSE;
844 }
845 }
846
847 if (fDataCheck)
848 {
849 if (fRuns || fSequence.IsValid())
850 rawread.AddFiles(fSequence.IsValid() ? iter : *fRuns);
851 tlist.AddToList(&rawread);
852 }
853 else
854 {
855 read.DisableAutoScheme();
856 if (fRuns || fSequence.IsValid())
857 read.AddFiles(fSequence.IsValid() ? iter : *fRuns);
858 tlist.AddToList(&read);
859 }
860
861 // Setup Tasklist
862 plist.AddToList(&fPedestalCamOut);
863 plist.AddToList(&fBadPixels);
864
865 MGeomApply geomapl;
866 MBadPixelsMerge merge(&fBadPixels);
867
868 MPedCalcPedRun pedcalc;
869 pedcalc.SetPedestalUpdate(kFALSE);
870
871 MPedCalcFromLoGain pedlogain;
872 pedlogain.SetPedestalUpdate(kFALSE);
873
874 MHPedestalCam hpedcam;
875 hpedcam.SetRenorm(kTRUE);
876
877 MFillH fillped(&hpedcam, "MExtractedSignalCam", "FillPedestalCam");
878 fillped.SetBit(MFillH::kDoNotDisplay);
879
880 tlist.AddToList(&geomapl);
881 tlist.AddToList(&merge);
882
883 if (!fPathIn.IsNull())
884 {
885 fExtractor = ReadCalibration();
886 if (!fExtractor)
887 return kFALSE;
888
889 *fLog << all;
890 *fLog << underline << "Signal Extractor found in calibration file:" << endl;
891 fExtractor->Print();
892 *fLog << endl;
893 }
894
895 // ----------------------------------------------------------------------
896 // Now we make sure, that in all cases the ranges are setup correctly
897 // ----------------------------------------------------------------------
898 MTaskEnv taskenv("ExtractPedestal");
899 switch (fExtractType)
900 {
901 case kUsePedRun:
902 // In case other than 'fundamental' second argument is obsolete
903 pedcalc.SetExtractWindow(0, 13); // kUsePedRun
904 taskenv.SetDefault(&pedcalc);
905 tlist.AddToList(&taskenv);
906 break;
907
908 case kUseData:
909 // In case other than 'fundamental' second argument is obsolete
910 pedlogain.SetExtractWindow(15, 28); // kUseData
911 taskenv.SetDefault(&pedlogain);
912 tlist.AddToList(&taskenv);
913 break;
914
915 case kUseHists:
916 if (!fExtractor)
917 {
918 *fLog << err << GetDescriptor() << " - ERROR: ";
919 *fLog << "Extraction Type is kUseHists, but no extractor was set" << endl;
920 return kFALSE;
921 }
922
923 tlist.AddToList(fExtractor);
924 tlist.AddToList(&fillped);
925 break;
926 }
927
928 pedcalc.SetPedestalsIn(&fPedestalCamIn);
929 pedlogain.SetPedestalsIn(&fPedestalCamIn);
930
931 // kFundamental
932 if (fExtractor)
933 {
934 if (!fExtractor->InheritsFrom("MExtractTimeAndCharge"))
935 {
936 const Float_t f = 0.5+fExtractor->GetHiGainFirst();
937 const Float_t n = 0.5+fExtractor->GetNumHiGainSamples();
938 pedcalc.SetExtractWindow((Int_t)f, (Int_t)n);
939 pedlogain.SetExtractWindow((Int_t)(15+f), (Int_t)(15+n));
940
941 if (fExtractionType!=kFundamental)
942 {
943 *fLog << inf;
944 *fLog << "Signal extractor doesn't inherit from MExtractTimeAndCharge..." << endl;
945 *fLog << " --> falling back to fundamental pedestal extraction." << endl;
946 fExtractionType=kFundamental;
947 }
948 }
949
950 if (fExtractionType!=kFundamental)
951 {
952 pedcalc.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
953 pedlogain.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
954
955 pedcalc.SetExtractor((MExtractTimeAndCharge*)fExtractor);
956 pedlogain.SetExtractor((MExtractTimeAndCharge*)fExtractor);
957 }
958 }
959
960 /*
961 switch (fExtractType)
962 {
963 case kUseData:
964 *fLog << all << "TYPE: USEDATA " << fExtractor << endl;
965 taskenv.SetDefault(&pedlogain);
966 tlist.AddToList(&taskenv);
967
968 if (!SetupExtractor(plist, pedlogain))
969 {
970 *fLog << all << "SETTING TO: " << fExtractor << " " << fExtractor->GetNumHiGainSamples() << endl;
971 fExtractor->Print();
972 pedlogain.SetExtractWindow(15, (Int_t)TMath::Nint(fExtractor->GetNumHiGainSamples()));
973 }
974 break;
975
976 case kUsePedRun:
977 *fLog << all << "TYPE: USEPEDRUN " << fExtractor << endl;
978 taskenv.SetDefault(&pedcalc);
979 tlist.AddToList(&taskenv);
980
981 if (!SetupExtractor(plist, pedcalc))
982 pedcalc.SetExtractWindow(fExtractor->GetHiGainFirst(), TMath::Nint(fExtractor->GetNumHiGainSamples()));
983 break;
984
985 case kUseHists:
986 if (!fExtractor)
987 {
988 *fLog << err << GetDescriptor() << " - ERROR: ";
989 *fLog << "Extraction Type is kUseHists, but no extractor was set" << endl;
990 return kFALSE;
991 }
992
993 tlist.AddToList(fExtractor);
994 tlist.AddToList(&fillped);
995 break;
996 } */
997
998 /*
999 if (!fPathIn.IsNull())
1000 {
1001 delete fExtractor;
1002 fExtractor = 0;
1003 }
1004 */
1005 //
1006 // Execute the eventloop
1007 //
1008 MEvtLoop evtloop(fName);
1009 evtloop.SetParList(&plist);
1010 evtloop.SetDisplay(fDisplay);
1011 evtloop.SetLogStream(fLog);
1012 if (!SetupEnv(evtloop))
1013 return kFALSE;
1014
1015 // if (!WriteEventloop(evtloop))
1016 // return kFALSE;
1017
1018 // Execute first analysis
1019 if (!evtloop.Eventloop(fMaxEvents))
1020 {
1021 *fLog << err << GetDescriptor() << ": Failed." << endl;
1022 return kFALSE;
1023 }
1024
1025 tlist.PrintStatistics();
1026
1027 DisplayResult(plist);
1028
1029 if (!WriteResult())
1030 return kFALSE;
1031
1032 *fLog << all << GetDescriptor() << ": Done." << endl;
1033 *fLog << endl << endl;
1034
1035 return kTRUE;
1036}
Note: See TracBrowser for help on using the repository browser.