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

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