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

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