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

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