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

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