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

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