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

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