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

Last change on this file since 5973 was 5957, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 31.1 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#include "MExtractTimeAndCharge.h"
61
62// parameter containers
63#include "MGeomCam.h"
64#include "MHCamera.h"
65#include "MPedestalCam.h"
66#include "MPedestalPix.h"
67#include "MBadPixelsCam.h"
68
69#include "MCalibrationPedCam.h"
70#include "MCalibrationPix.h"
71#include "MHPedestalCam.h"
72#include "MHPedestalPix.h"
73
74// tasks
75#include "MReadMarsFile.h"
76#include "MRawFileRead.h"
77#include "MGeomApply.h"
78#include "MBadPixelsMerge.h"
79#include "MFillH.h"
80#include "MPedCalcPedRun.h"
81#include "MPedCalcFromLoGain.h"
82
83ClassImp(MJPedestal);
84
85using namespace std;
86
87const Double_t MJPedestal::fgPedestalMin = 4.;
88const Double_t MJPedestal::fgPedestalMax = 16.;
89const Double_t MJPedestal::fgPedRmsMin = 0.;
90const Double_t MJPedestal::fgPedRmsMax = 20.;
91
92const Float_t MJPedestal::fgRefPedClosedLids = 9.635;
93const Float_t MJPedestal::fgRefPedExtraGalactic = 9.93;
94const Float_t MJPedestal::fgRefPedGalactic = 10.03;
95const Float_t MJPedestal::fgRefPedRmsClosedLidsInner = 1.7;
96const Float_t MJPedestal::fgRefPedRmsExtraGalacticInner = 5.6;
97const Float_t MJPedestal::fgRefPedRmsGalacticInner = 6.92;
98const Float_t MJPedestal::fgRefPedRmsClosedLidsOuter = 1.7;
99const Float_t MJPedestal::fgRefPedRmsExtraGalacticOuter = 3.35;
100const Float_t MJPedestal::fgRefPedRmsGalacticOuter = 4.2;
101
102// --------------------------------------------------------------------------
103//
104// Default constructor.
105//
106// Sets:
107// - fRuns to 0,
108// - fExtractor to NULL,
109// - fDataCheck to kFALSE,
110// - fExtractType to kUsePedRun
111// - fStorage to Normal Storage
112// - fExtractorResolution to kFALSE
113//
114MJPedestal::MJPedestal(const char *name, const char *title)
115 : fRuns(0), fExtractor(NULL), fDisplayType(kNormalDisplay),
116 fDataCheck(kFALSE), fExtractType(kUsePedRun), fExtractionType(kFundamental)
117{
118 fName = name ? name : "MJPedestal";
119 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
120
121 SetNormalStorage();
122 SetUsePedRun();
123 SetPathIn("");
124}
125
126MJPedestal::~MJPedestal()
127{
128 if (fExtractor)
129 delete fExtractor;
130}
131
132const char* MJPedestal::GetOutputFile() const
133{
134 if (fSequence.IsValid())
135 return Form("%s/pedest%08d.root", (const char*)fPathOut, fSequence.GetSequence());
136
137 if (!fRuns)
138 return "";
139
140 return Form("%s/%s-F0.root", (const char*)fPathOut, (const char*)fRuns->GetRunsAsFileName());
141}
142
143//---------------------------------------------------------------------------------
144//
145// Try to read an existing MPedestalCam from a previously created output file.
146// If found, also an MBadPixelsCam and the corresponding display is read.
147//
148// In case of Storage type "kNoStorage" or if the file is not found or the
149// MPedestalCam cannot be read, return kFALSE, otherwise kTRUE.
150//
151Bool_t MJPedestal::ReadPedestalCam()
152{
153 const TString fname = GetOutputFile();
154
155 *fLog << inf << "Reading pedestals from file: " << fname << endl;
156
157 TFile file(fname, "READ");
158 if (fPedestalCamIn.Read()<=0)
159 {
160 *fLog << err << "Unable to read incoming MPedestalCam from " << fname << endl;
161 return kFALSE;
162 }
163
164 if (fPedestalCamOut.Read()<=0)
165 {
166 *fLog << err << "Unable to read outgoing MPedestalCam from " << fname << endl;
167 return kFALSE;
168 }
169
170 if (file.FindKey("MBadPixelsCam"))
171 {
172 MBadPixelsCam bad;
173 if (bad.Read()<=0)
174 {
175 *fLog << err << "Unable to read MBadPixelsCam from " << fname << endl;
176 return kFALSE;
177 }
178 fBadPixels.Merge(bad);
179 }
180
181 if (fDisplay && !fDisplay->GetCanvas("Pedestals"))
182 fDisplay->Read();
183
184 return kTRUE;
185}
186
187MExtractor *MJPedestal::ReadCalibration() const
188{
189 const TString fname = Form("%s/calib%08d.root",fPathIn.Data(), fSequence.GetSequence());
190
191 *fLog << inf << "Reading extractor from file: " << fname << endl;
192
193 TFile file(fname, "READ");
194 if (!file.IsOpen())
195 {
196 *fLog << err << dbginf << "ERROR - Could not open file " << fname << endl;
197 return NULL;
198 }
199
200 TObject *o = file.Get("ExtractSignal");
201 if (o && !o->InheritsFrom(MExtractor::Class()))
202 {
203 *fLog << err << dbginf << "ERROR - ExtractSignal read from " << fname << " doesn't inherit from MExtractor!" << endl;
204 return NULL;
205 }
206
207 return o ? (MExtractor*)o->Clone("ExtractSignal") : 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 (fExtractionType!=kFundamental/*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("P [cts/slice]");
326 disp1.SetYTitle("P_{rms} [cts/slice]");
327 disp2.SetYTitle("Hist. Mean [cts/slice]");
328 disp3.SetYTitle("Hist. Sigma [cts/slice]");
329 disp4.SetYTitle("Calc. Mean [cts/slice]");
330 disp5.SetYTitle("Calc. RMS [cts/slice]");
331 disp6.SetYTitle("Diff. Mean [cts/slice]");
332 disp7.SetYTitle("Diff. RMS [cts/slice]");
333 disp8.SetYTitle("Diff. Mean [cts/slice]");
334 disp9.SetYTitle("Abs.Diff.RMS [cts/slice]");
335 disp10.SetYTitle("Rel.Diff.RMS [1]");
336
337 //
338 // Display data
339 //
340 if (fDisplayType != kDataCheckDisplay && fExtractType != kUseHists && fExtractionType==kFundamental/*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(fExtractionType!=kFundamental/*fExtractorResolution*/ ? "PedExtrd" : "Ped");
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 (fExtractionType==kFundamental/*!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 (fExtractionType!=kFundamental/*fExtractorResolution*/)
538 {
539
540 TCanvas &c3 = fDisplay->AddTab(fExtractionType==kWithExtractor?"PedExtrd":"PedRndm");
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(fExtractionType==kWithExtractor?"PedDiff":"DiffRndm");
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
674void MJPedestal::SetExtractor(MExtractor* ext)
675{
676 if (ext)
677 {
678 if (fExtractor)
679 delete fExtractor;
680 fExtractor = ext ? (MExtractor*)ext->Clone(ext->GetName()) : NULL;
681 }
682 else
683 fExtractor = 0;
684}
685
686// --------------------------------------------------------------------------
687//
688// The following resource options are available:
689//
690// Do a datacheck run (read raw-data and enable display)
691// Prefix.DataCheck: Yes, No <default>
692//
693// Show data check display
694// Prefix.DataCheckDisplay: Yes, No <default>
695//
696// Use cosmic data instead of pedestal data (DatRuns)
697// Prefix.UseData: Yes, No <default>
698//
699// Write an output file with pedestals and status-display
700// Prefix.DisableOutput: Yes, No <default>
701//
702Bool_t MJPedestal::CheckEnvLocal()
703{
704 SetDataCheck(GetEnv("DataCheck", fDataCheck));
705
706 if (HasEnv("DataCheckDisplay"))
707 fDisplayType = GetEnv("DataCheckDisplay", kFALSE) ? kDataCheckDisplay : kNormalDisplay;
708
709 if (HasEnv("UseData"))
710 fExtractType = GetEnv("UseData",kFALSE) ? kUseData : kUsePedRun;
711
712 if (HasEnv("UseHists"))
713 if (GetEnv("UseHists",kFALSE))
714 fExtractType = kUseHists;
715
716 //fExtractorResolution = GetEnv("ExtractionType", fExtractorResolution);
717
718 SetNoStorage(GetEnv("DisableOutput", IsNoStorage()));
719
720 MTaskEnv tenv("ExtractSignal");
721 tenv.SetDefault(fExtractor);
722
723 if (tenv.ReadEnv(*GetEnv(), GetEnvPrefix()+".ExtractSignal", GetEnvDebug())==kERROR)
724 return kFALSE;
725
726 if (fExtractor==tenv.GetTask())
727 return kTRUE;
728
729 if (!tenv.GetTask()->InheritsFrom(MExtractor::Class()))
730 {
731 *fLog << err << "ERROR: ExtractSignal from resource file doesn't inherit from MExtractor.... abort." << endl;
732 return kFALSE;
733 }
734
735 SetExtractor((MExtractor*)tenv.GetTask());
736
737 return kTRUE;
738}
739
740//---------------------------------------------------------------------------------
741//
742// Try to write the created MPedestalCam in the output file.
743// If possible, also an MBadPixelsCam and the corresponding display is written.
744//
745// In case of Storage type "kNoStorage" or if any of the containers
746// cannot be written, return kFALSE, otherwise kTRUE.
747//
748Bool_t MJPedestal::WriteResult()
749{
750 if (IsNoStorage())
751 return kTRUE;
752
753 if (fPathOut.IsNull())
754 {
755 *fLog << inf << "No output path specified via SetPathOut - no output written." << endl;
756 return kTRUE;
757 }
758
759 const TString oname(GetOutputFile());
760
761 *fLog << inf << "Writing to file: " << oname << endl;
762
763 TFile file(oname, "UPDATE", "File created by MJPedestal", 9);
764 if (!file.IsOpen())
765 {
766 *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
767 return kFALSE;
768 }
769
770 TObjArray cont;
771
772 if (fDisplay)
773 cont.Add(fDisplay);
774
775 cont.Add(&fPedestalCamOut);
776 cont.Add(&fBadPixels);
777
778 return WriteContainer(cont);
779}
780
781Bool_t MJPedestal::Process()
782{
783 if (!ReadPedestalCam())
784 return ProcessFile();
785
786 return kTRUE;
787}
788
789Bool_t MJPedestal::ProcessFile()
790{
791 if (!fSequence.IsValid())
792 {
793 if (!fRuns)
794 {
795 *fLog << err << "Neither AddRuns nor SetSequence nor SetEnv was called... abort." << endl;
796 return kFALSE;
797 }
798 if (fRuns && fRuns->GetNumRuns() != fRuns->GetNumEntries())
799 {
800 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
801 return kFALSE;
802 }
803 }
804
805 if (!CheckEnv())
806 return kFALSE;
807
808 // --------------------------------------------------------------------------------
809
810 const TString type = IsUseData() ? "data" : "pedestal";
811
812 *fLog << inf;
813 fLog->Separator(GetDescriptor());
814 *fLog << "Calculate MPedestalCam from " << type << "-runs ";
815 if (fSequence.IsValid())
816 *fLog << fSequence.GetName() << endl;
817 else
818 if (fRuns)
819 *fLog << fRuns->GetRunsAsString() << endl;
820 else
821 *fLog << "in Resource File." << endl;
822 *fLog << endl;
823
824 // --------------------------------------------------------------------------------
825
826 MParList plist;
827 MTaskList tlist;
828 plist.AddToList(&tlist);
829 plist.AddToList(this); // take care of fDisplay!
830
831 MReadMarsFile read("Events");
832 MRawFileRead rawread(NULL);
833
834 MDirIter iter;
835 if (fSequence.IsValid())
836 {
837 const Int_t n0 = IsUseData() ? fSequence.SetupDatRuns(iter, fPathData, "D", fDataCheck) : fSequence.SetupPedRuns(iter, fPathData, "P", fDataCheck);
838 const Int_t n1 = IsUseData() ? fSequence.GetNumDatRuns() : fSequence.GetNumPedRuns();
839 if (n0==0)
840 {
841 *fLog << err << "ERROR - No " << type << " input files of sequence found in " << (fPathData.IsNull()?"<defaul>":fPathData.Data()) << endl;
842 return kFALSE;
843 }
844 if (n0!=n1)
845 {
846 *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;
847 return kFALSE;
848 }
849 }
850
851 if (fDataCheck)
852 {
853 if (fRuns || fSequence.IsValid())
854 rawread.AddFiles(fSequence.IsValid() ? iter : *fRuns);
855 tlist.AddToList(&rawread);
856 }
857 else
858 {
859 read.DisableAutoScheme();
860 if (fRuns || fSequence.IsValid())
861 read.AddFiles(fSequence.IsValid() ? iter : *fRuns);
862 tlist.AddToList(&read);
863 }
864
865 // Setup Tasklist
866 plist.AddToList(&fPedestalCamOut);
867 plist.AddToList(&fBadPixels);
868
869 MGeomApply geomapl;
870 MBadPixelsMerge merge(&fBadPixels);
871
872 MPedCalcPedRun pedcalc;
873 pedcalc.SetPedestalUpdate(kFALSE);
874
875 MPedCalcFromLoGain pedlogain;
876 pedlogain.SetPedestalUpdate(kFALSE);
877
878 MHPedestalCam hpedcam;
879 hpedcam.SetRenorm(kTRUE);
880
881 MFillH fillped(&hpedcam, "MExtractedSignalCam", "FillPedestalCam");
882 fillped.SetBit(MFillH::kDoNotDisplay);
883
884 tlist.AddToList(&geomapl);
885 tlist.AddToList(&merge);
886
887 if (!fPathIn.IsNull())
888 {
889 fExtractor = ReadCalibration();
890 if (!fExtractor)
891 return kFALSE;
892
893 *fLog << all;
894 *fLog << underline << "Signal Extractor found in calibration file:" << endl;
895 fExtractor->Print();
896 *fLog << endl;
897 }
898
899 // ----------------------------------------------------------------------
900 // Now we make sure, that in all cases the ranges are setup correctly
901 // ----------------------------------------------------------------------
902 MTaskEnv taskenv("ExtractPedestal");
903 switch (fExtractType)
904 {
905 case kUsePedRun:
906 // In case other than 'fundamental' second argument is obsolete
907 // pedcalc.SetExtractWindow(0,14); // kUsePedRun (take default from class)
908 taskenv.SetDefault(&pedcalc);
909 tlist.AddToList(&taskenv);
910 break;
911
912 case kUseData:
913 // In case other than 'fundamental' second argument is obsolete
914 // pedlogain.SetExtractWindow(15,14); // kUseData (take default from class)
915 taskenv.SetDefault(&pedlogain);
916 tlist.AddToList(&taskenv);
917 break;
918
919 case kUseHists:
920 if (!fExtractor)
921 {
922 *fLog << err << GetDescriptor() << " - ERROR: ";
923 *fLog << "Extraction Type is kUseHists, but no extractor was set" << endl;
924 return kFALSE;
925 }
926
927 tlist.AddToList(fExtractor);
928 tlist.AddToList(&fillped);
929 break;
930 }
931
932 pedcalc.SetPedestalsIn(&fPedestalCamIn);
933 pedlogain.SetPedestalsIn(&fPedestalCamIn);
934
935 // kFundamental
936 if (fExtractor)
937 {
938
939 if (fExtractor->InheritsFrom("MExtractTimeAndCharge"))
940 {
941
942 const Float_t f = 0.5+fExtractor->GetHiGainFirst();
943 const Int_t win = ((MExtractTimeAndCharge*)fExtractor)->GetWindowSizeHiGain();
944 pedcalc.SetExtractWindow((Int_t)f, win);
945 pedlogain.SetExtractWindow((Int_t)(15+f), win);
946
947 }
948 else
949 {
950 const Float_t f = 0.5+fExtractor->GetHiGainFirst();
951 const Float_t n = 0.5+fExtractor->GetNumHiGainSamples();
952 pedcalc.SetExtractWindow((Int_t)f, (Int_t)n);
953 pedlogain.SetExtractWindow((Int_t)(15+f), (Int_t)n);
954
955 if (fExtractionType!=kFundamental)
956 {
957 *fLog << inf;
958 *fLog << "Signal extractor doesn't inherit from MExtractTimeAndCharge..." << endl;
959 *fLog << " --> falling back to fundamental pedestal extraction." << endl;
960 fExtractionType=kFundamental;
961 }
962 }
963
964
965 if (fExtractionType!=kFundamental)
966 {
967 pedcalc.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
968 pedlogain.SetRandomCalculation(fExtractionType==kWithExtractorRndm);
969
970 pedcalc.SetExtractor((MExtractTimeAndCharge*)fExtractor);
971 pedlogain.SetExtractor((MExtractTimeAndCharge*)fExtractor);
972 }
973 }
974 else
975 {
976 *fLog << warn << GetDescriptor() << ": WARNING - No extractor has been handed over! " << endl;
977 *fLog << "Taking default window for pedestal extraction. The calculated pedestal RMS" << endl;
978 *fLog << "will probably not match with future pedestal RMS' from different extraction" << endl;
979 *fLog << "windows." << endl;
980 }
981
982 /*
983 switch (fExtractType)
984 {
985 case kUseData:
986 *fLog << all << "TYPE: USEDATA " << fExtractor << endl;
987 taskenv.SetDefault(&pedlogain);
988 tlist.AddToList(&taskenv);
989
990 if (!SetupExtractor(plist, pedlogain))
991 {
992 *fLog << all << "SETTING TO: " << fExtractor << " " << fExtractor->GetNumHiGainSamples() << endl;
993 fExtractor->Print();
994 pedlogain.SetExtractWindow(15, (Int_t)TMath::Nint(fExtractor->GetNumHiGainSamples()));
995 }
996 break;
997
998 case kUsePedRun:
999 *fLog << all << "TYPE: USEPEDRUN " << fExtractor << endl;
1000 taskenv.SetDefault(&pedcalc);
1001 tlist.AddToList(&taskenv);
1002
1003 if (!SetupExtractor(plist, pedcalc))
1004 pedcalc.SetExtractWindow(fExtractor->GetHiGainFirst(), TMath::Nint(fExtractor->GetNumHiGainSamples()));
1005 break;
1006
1007 case kUseHists:
1008 if (!fExtractor)
1009 {
1010 *fLog << err << GetDescriptor() << " - ERROR: ";
1011 *fLog << "Extraction Type is kUseHists, but no extractor was set" << endl;
1012 return kFALSE;
1013 }
1014
1015 tlist.AddToList(fExtractor);
1016 tlist.AddToList(&fillped);
1017 break;
1018 } */
1019
1020 /*
1021 if (!fPathIn.IsNull())
1022 {
1023 delete fExtractor;
1024 fExtractor = 0;
1025 }
1026 */
1027 //
1028 // Execute the eventloop
1029 //
1030 MEvtLoop evtloop(fName);
1031 evtloop.SetParList(&plist);
1032 evtloop.SetDisplay(fDisplay);
1033 evtloop.SetLogStream(fLog);
1034 if (!SetupEnv(evtloop))
1035 return kFALSE;
1036
1037 // if (!WriteEventloop(evtloop))
1038 // return kFALSE;
1039
1040 // Execute first analysis
1041 if (!evtloop.Eventloop(fMaxEvents))
1042 {
1043 *fLog << err << GetDescriptor() << ": Failed." << endl;
1044 return kFALSE;
1045 }
1046
1047 tlist.PrintStatistics();
1048
1049 DisplayResult(plist);
1050
1051 if (!WriteResult())
1052 return kFALSE;
1053
1054 *fLog << all << GetDescriptor() << ": Done." << endl;
1055 *fLog << endl << endl;
1056
1057 return kTRUE;
1058}
Note: See TracBrowser for help on using the repository browser.