source: trunk/MagicSoft/Mars/mjobs/MJCalibration.cc@ 3252

Last change on this file since 3252 was 3205, checked in by gaug, 21 years ago
*** empty log message ***
File size: 15.9 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!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MJCalibration
28//
29/////////////////////////////////////////////////////////////////////////////
30#include "MJCalibration.h"
31
32#include <TF1.h>
33#include <TFile.h>
34#include <TStyle.h>
35#include <TCanvas.h>
36#include <TSystem.h>
37
38#include "MLog.h"
39#include "MLogManip.h"
40
41#include "MRunIter.h"
42#include "MParList.h"
43#include "MTaskList.h"
44#include "MEvtLoop.h"
45
46#include "MHCamera.h"
47
48#include "MPedestalCam.h"
49#include "MCalibrationCam.h"
50
51#include "MReadMarsFile.h"
52#include "MGeomApply.h"
53#include "MBadPixelsMerge.h"
54#include "MExtractSignal.h"
55#include "MExtractPINDiode.h"
56#include "MExtractBlindPixel.h"
57#include "MExtractSignal2.h"
58#include "MCalibrationCalc.h"
59#include "MFCosmics.h"
60#include "MContinue.h"
61#include "MFillH.h"
62
63#include "MJCalibration.h"
64#include "MStatusDisplay.h"
65
66ClassImp(MJCalibration);
67using namespace std;
68
69MJCalibration::MJCalibration(const char *name, const char *title) : fRuns(0)
70{
71 fName = name ? name : "MJCalibration";
72 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
73}
74
75void MJCalibration::DrawProjection(MHCamera *obj1, Int_t fit) const
76{
77
78 TH1D *obj2 = (TH1D*)obj1->Projection();
79 obj2->Draw();
80 obj2->SetBit(kCanDelete);
81
82 const Double_t min = obj2->GetBinCenter(obj2->GetXaxis()->GetFirst());
83 const Double_t max = obj2->GetBinCenter(obj2->GetXaxis()->GetLast());
84 const Double_t integ = obj2->Integral("width")/2.5;
85 const Double_t mean = obj2->GetMean();
86 const Double_t rms = obj2->GetRMS();
87 const Double_t width = max-min;
88
89 const TString dgausformula = "([0]-[3])/[2]*exp(-0.5*(x-[1])*(x-[1])/[2]/[2])"
90 "+[3]/[5]*exp(-0.5*(x-[4])*(x-[4])/[5]/[5])";
91
92 const TString tgausformula = "([0]-[3]-[6])/[2]*exp(-0.5*(x-[1])*(x-[1])/[2]/[2])"
93 "+[3]/[5]*exp(-0.5*(x-[4])*(x-[4])/[5]/[5])"
94 "+[6]/[8]*exp(-0.5*(x-[7])*(x-[7])/[8]/[8])";
95 TF1 *f=0;
96 switch (fit)
97 {
98 case 1:
99 f = new TF1("sgaus", "gaus(0)", min, max);
100 f->SetLineColor(kYellow);
101 f->SetBit(kCanDelete);
102 f->SetParNames("Area", "#mu", "#sigma");
103 f->SetParameters(integ/rms, mean, rms);
104 f->SetParLimits(0, 0, integ);
105 f->SetParLimits(1, min, max);
106 f->SetParLimits(2, 0, width/1.5);
107
108 obj2->Fit(f, "QLR");
109 break;
110
111 case 2:
112 f = new TF1("dgaus",dgausformula.Data(),min,max);
113 f->SetLineColor(kYellow);
114 f->SetBit(kCanDelete);
115 f->SetParNames("A_{tot}", "#mu1", "#sigma1", "A2", "#mu2", "#sigma2");
116 f->SetParameters(integ,(min+mean)/2.,width/4.,
117 integ/width/2.,(max+mean)/2.,width/4.);
118 // The left-sided Gauss
119 f->SetParLimits(0,integ-1.5 , integ+1.5);
120 f->SetParLimits(1,min+(width/10.), mean);
121 f->SetParLimits(2,0 , width/2.);
122 // The right-sided Gauss
123 f->SetParLimits(3,0 , integ);
124 f->SetParLimits(4,mean, max-(width/10.));
125 f->SetParLimits(5,0 , width/2.);
126 obj2->Fit(f,"QLRM");
127 break;
128
129 case 3:
130 f = new TF1("tgaus",tgausformula.Data(),min,max);
131 f->SetLineColor(kYellow);
132 f->SetBit(kCanDelete);
133 f->SetParNames("A_{tot}","#mu_{1}","#sigma_{1}",
134 "A_{2}","#mu_{2}","#sigma_{2}",
135 "A_{3}","#mu_{3}","#sigma_{3}");
136 f->SetParameters(integ,(min+mean)/2,width/4.,
137 integ/width/3.,(max+mean)/2.,width/4.,
138 integ/width/3.,mean,width/2.);
139 // The left-sided Gauss
140 f->SetParLimits(0,integ-1.5,integ+1.5);
141 f->SetParLimits(1,min+(width/10.),mean);
142 f->SetParLimits(2,width/15.,width/2.);
143 // The right-sided Gauss
144 f->SetParLimits(3,0.,integ);
145 f->SetParLimits(4,mean,max-(width/10.));
146 f->SetParLimits(5,width/15.,width/2.);
147 // The Gauss describing the outliers
148 f->SetParLimits(6,0.,integ);
149 f->SetParLimits(7,min,max);
150 f->SetParLimits(8,width/4.,width/1.5);
151 obj2->Fit(f,"QLRM");
152 break;
153
154 case 4:
155 obj2->Fit("pol0", "Q");
156 obj2->GetFunction("pol0")->SetLineColor(kYellow);
157 break;
158
159 case 9:
160 break;
161
162 default:
163 obj2->Fit("gaus", "Q");
164 obj2->GetFunction("gaus")->SetLineColor(kYellow);
165 break;
166 }
167}
168
169void MJCalibration::CamDraw(TCanvas &c, const Int_t x, const Int_t y, const MHCamera &cam1, const Int_t fit)
170{
171 c.cd(x);
172 gPad->SetBorderMode(0);
173 MHCamera *obj1=(MHCamera*)cam1.DrawCopy("hist");
174 obj1->AddNotify(&fCalibrationCam);
175
176 c.cd(x+y);
177 gPad->SetBorderMode(0);
178 obj1->Draw();
179
180 if (!fit)
181 return;
182
183 c.cd(x+2*y);
184 gPad->SetBorderMode(0);
185 DrawProjection(obj1, fit);
186}
187
188
189void MJCalibration::DisplayResult(MParList &plist)
190{
191 if (!fDisplay)
192 return;
193
194 //
195 // Update display
196 //
197 TString title = fDisplay->GetTitle();
198 title += "-- Calibration ";
199 title += fRuns->GetRunsAsString();
200 title += " --";
201 fDisplay->SetTitle(title);
202
203 //
204 // Get container from list
205 //
206 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
207
208 // Create histograms to display
209 MHCamera disp1 (geomcam, "Cal;Charge", "Fitted Mean Charges");
210 MHCamera disp2 (geomcam, "Cal;SigmaCharge", "Sigma of Fitted Charges");
211 MHCamera disp3 (geomcam, "Cal;FitProb", "Probability of Fit");
212 MHCamera disp4 (geomcam, "Cal;RSigma", "Reduced Sigmas");
213 MHCamera disp5 (geomcam, "Cal;RSigma/Charge", "Reduced Sigma per Charge");
214 MHCamera disp6 (geomcam, "Cal;FFactorPhe", "Nr. of Phe's (F-Factor Method)");
215 MHCamera disp7 (geomcam, "Cal;FFactorConv", "Conversion Factor (F-Factor Method)");
216 MHCamera disp8 (geomcam, "Cal;FFactorFFactor", "Total F-Factor (F-Factor Method)");
217 MHCamera disp9 (geomcam, "Cal;BlindPixPh", "Photon flux inside plexiglass (Blind Pixel Method)");
218 MHCamera disp10(geomcam, "Cal;BlindPixConv", "Conversion Factor (Blind Pixel Method)");
219 MHCamera disp11(geomcam, "Cal;BlindPixFFactor","Total F-Factor (Blind Pixel Method)");
220 MHCamera disp12(geomcam, "Cal;PINDiodePh", "Photons flux outside plexiglass (PIN Diode Method)");
221 MHCamera disp13(geomcam, "Cal;PINDiodeConv", "Conversion Factor (PIN Diode Method)");
222 MHCamera disp14(geomcam, "Cal;PINDiodeFFactor","Total F-Factor (PIN Diode Method)");
223 MHCamera disp15(geomcam, "Cal;Excluded", "Pixels previously excluded");
224 MHCamera disp16(geomcam, "Cal;NotFitted", "Pixels that could not be fitted");
225 MHCamera disp17(geomcam, "Cal;NotFitValid", "Pixels with not valid fit results");
226 MHCamera disp18(geomcam, "Cal;Oscillation", "Oscillating Pixels");
227 MHCamera disp19(geomcam, "Cal;Saturation", "Pixels with saturated Hi Gain");
228
229 // Fitted charge means and sigmas
230 disp1.SetCamContent(fCalibrationCam, 0);
231 disp1.SetCamError( fCalibrationCam, 1);
232 disp2.SetCamContent(fCalibrationCam, 2);
233 disp2.SetCamError( fCalibrationCam, 3);
234 // Fit probabilities
235 disp3.SetCamContent(fCalibrationCam, 4);
236
237 // Reduced Sigmas and reduced sigmas per charge
238 disp4.SetCamContent(fCalibrationCam, 5);
239 disp4.SetCamError( fCalibrationCam, 6);
240 disp5.SetCamContent(fCalibrationCam, 7);
241 disp5.SetCamError( fCalibrationCam, 8);
242
243 // F-Factor Method
244 disp6.SetCamContent(fCalibrationCam, 9);
245 disp6.SetCamError( fCalibrationCam, 10);
246 disp7.SetCamContent(fCalibrationCam, 11);
247 disp7.SetCamError( fCalibrationCam, 12);
248 disp8.SetCamContent(fCalibrationCam, 13);
249 disp8.SetCamError( fCalibrationCam, 14);
250
251 /// Blind Pixel Method
252 disp9.SetCamContent(fCalibrationCam, 15);
253 disp9.SetCamError( fCalibrationCam, 16);
254 disp10.SetCamContent(fCalibrationCam,17);
255 disp10.SetCamError( fCalibrationCam,18);
256 disp11.SetCamContent(fCalibrationCam,19);
257 disp11.SetCamError( fCalibrationCam,20);
258
259 // PIN Diode Method
260 disp12.SetCamContent(fCalibrationCam,21);
261 disp12.SetCamError( fCalibrationCam,22);
262 disp13.SetCamContent(fCalibrationCam,23);
263 disp13.SetCamError( fCalibrationCam,24);
264 disp14.SetCamContent(fCalibrationCam,25);
265 disp14.SetCamError( fCalibrationCam,26);
266
267 // Pixels with defects
268 disp15.SetCamContent(fCalibrationCam,27);
269 disp16.SetCamContent(fCalibrationCam,28);
270 disp17.SetCamContent(fCalibrationCam,29);
271 disp18.SetCamContent(fCalibrationCam,30);
272
273 // Lo Gain calibration
274 disp19.SetCamContent(fCalibrationCam,31);
275
276 disp1.SetYTitle("Q [FADC units]");
277 disp2.SetYTitle("\\sigma_{Q} [FADC units]");
278 disp3.SetYTitle("P_{Q} [1]");
279
280 disp4.SetYTitle("\\sqrt{\\sigma^{2}_{Q} - RMS^{2}_{Ped}} [FADC Counts]");
281 disp5.SetYTitle("Red.Sigma/<Q> [1]");
282
283 disp6.SetYTitle("PhE [#]");
284 disp7.SetYTitle("Conv.Factor [PhE/FADC units]");
285 disp8.SetYTitle("\\sqrt{N_{PhE}}*\\sigma_{Q}/\\mu_{Q} [1]");
286
287 disp9.SetYTitle("Phot.flux [ph/mm^{2}]");
288 disp10.SetYTitle("Conv.Factor [Phot/FADC Count]");
289 disp11.SetYTitle("\\sqrt{N_{Ph}}*\\sigma_{Q}/\\mu_{Q} [1]");
290
291 disp12.SetYTitle("Phot.flux [ph/mm^{2}]");
292 disp13.SetYTitle("Conv.Factor [Phot/FADC Count]");
293 disp14.SetYTitle("\\sqrt{N_{Ph}}*\\sigma_{Q}/\\mu_{Q} [1]");
294
295 disp15.SetYTitle("[1]");
296 disp16.SetYTitle("[1]");
297 disp17.SetYTitle("[1]");
298 disp18.SetYTitle("[1]");
299
300 gStyle->SetOptStat(1111);
301 gStyle->SetOptFit();
302
303 // Charges
304 TCanvas &c1 = fDisplay->AddTab("Fit.Charge");
305 c1.Divide(2, 3);
306
307 CamDraw(c1, 1, 2, disp1, 2);
308 CamDraw(c1, 2, 2, disp2, 2);
309
310 // Fit Probability
311 TCanvas &c2 = fDisplay->AddTab("Fit.Prob");
312 c2.Divide(1,3);
313
314 CamDraw(c2, 1, 1, disp3, 4);
315
316 // Reduced Sigmas
317 TCanvas &c3 = fDisplay->AddTab("Red.Sigma");
318 c3.Divide(2,3);
319
320 CamDraw(c3, 1, 2, disp4, 2);
321 CamDraw(c3, 2, 2, disp5, 2);
322
323 // F-Factor Method
324 TCanvas &c4 = fDisplay->AddTab("F-Factor");
325 c4.Divide(3,3);
326
327 CamDraw(c4, 1, 3, disp6, 2);
328 CamDraw(c4, 2, 3, disp7, 2);
329 CamDraw(c4, 3, 3, disp8, 2);
330
331 // Blind Pixel Method
332 TCanvas &c5 = fDisplay->AddTab("BlindPix");
333 c5.Divide(3, 3);
334
335 CamDraw(c5, 1, 3, disp9, 9);
336 CamDraw(c5, 2, 3, disp10, 2);
337 CamDraw(c5, 3, 3, disp11, 2);
338
339 // PIN Diode Method
340 TCanvas &c6 = fDisplay->AddTab("PINDiode");
341 c6.Divide(3,3);
342
343 CamDraw(c6, 1, 3, disp12, 9);
344 CamDraw(c6, 2, 3, disp13, 2);
345 CamDraw(c6, 3, 3, disp14, 2);
346
347 // Defects
348 TCanvas &c7 = fDisplay->AddTab("Defects");
349 c7.Divide(4,2);
350
351 CamDraw(c7, 1, 4, disp15, 0);
352 CamDraw(c7, 2, 4, disp16, 0);
353 CamDraw(c7, 3, 4, disp17, 0);
354 CamDraw(c7, 4, 4, disp18, 0);
355
356 // Lo Gain Calibration
357 TCanvas &c8 = fDisplay->AddTab("LowGain");
358 c8.Divide(1,3);
359
360 CamDraw(c8, 1, 1, disp19, 0);
361}
362
363Bool_t MJCalibration::WriteResult()
364{
365 if (fOutputPath.IsNull())
366 return kTRUE;
367
368 const TString oname(GetOutputFile());
369
370 *fLog << inf << "Writing to file: " << oname << endl;
371
372 TFile file(oname, "UPDATE");
373
374 if (fDisplay && fDisplay->Write()<=0)
375 {
376 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
377 return kFALSE;
378 }
379
380 if (fCalibrationCam.Write()<=0)
381 {
382 *fLog << err << "Unable to write MCalibrationCam to " << oname << endl;
383 return kFALSE;
384 }
385
386 if (fBadPixels.Write()<=0)
387 {
388 *fLog << err << "Unable to write MBadPixelsCam to " << oname << endl;
389 return kFALSE;
390 }
391
392 return kTRUE;
393
394}
395
396void MJCalibration::SetOutputPath(const char *path)
397{
398 fOutputPath = path;
399 if (fOutputPath.EndsWith("/"))
400 fOutputPath = fOutputPath(0, fOutputPath.Length()-1);
401}
402
403Bool_t MJCalibration::Process(MPedestalCam &pedcam)
404{
405 if (!ReadCalibrationCam())
406 return ProcessFile(pedcam);
407
408 return kTRUE;
409}
410
411TString MJCalibration::GetOutputFile() const
412{
413 if (!fRuns)
414 return "";
415
416 return Form("%s/%s-F1.root", (const char*)fOutputPath, (const char*)fRuns->GetRunsAsFileName());
417}
418
419Bool_t MJCalibration::ReadCalibrationCam()
420{
421 const TString fname = GetOutputFile();
422
423 if (gSystem->AccessPathName(fname, kFileExists))
424 {
425 *fLog << err << "Input file " << fname << " doesn't exist." << endl;
426 return kFALSE;
427 }
428
429 *fLog << inf << "Reading from file: " << fname << endl;
430
431 TFile file(fname, "READ");
432 if (fCalibrationCam.Read()<=0)
433 {
434 *fLog << "Unable to read MCalibrationCam from " << fname << endl;
435 return kFALSE;
436 }
437
438 if (file.FindKey("MBadPixelsCam"))
439 {
440 MBadPixelsCam bad;
441 if (bad.Read()<=0)
442 {
443 *fLog << "Unable to read MBadPixelsCam from " << fname << endl;
444 return kFALSE;
445 }
446 fBadPixels.Merge(bad);
447 }
448
449 if (fDisplay /*&& !fDisplay->GetCanvas("Pedestals")*/) // FIXME!
450 fDisplay->Read();
451
452 return kTRUE;
453}
454
455
456Bool_t MJCalibration::ProcessFile(MPedestalCam &pedcam)
457{
458 if (!fRuns)
459 {
460 *fLog << err << "No Runs choosen... abort." << endl;
461 return kFALSE;
462 }
463 if (fRuns->GetNumRuns() != fRuns->GetNumEntries())
464 {
465 *fLog << err << "Number of files found doesn't metch number of runs... abort." << endl;
466 return kFALSE;
467 }
468
469 *fLog << inf;
470 fLog->Separator(GetDescriptor());
471 *fLog << "Calculate MCalibrationCam from Runs " << fRuns->GetRunsAsString() << endl;
472 *fLog << endl;
473
474 MReadMarsFile read("Events");
475 read.DisableAutoScheme();
476 static_cast<MRead&>(read).AddFiles(*fRuns);
477
478 // Setup Tasklist
479 MParList plist;
480 plist.AddToList(&pedcam);
481 plist.AddToList(&fCalibrationCam);
482
483 MTaskList tlist;
484 plist.AddToList(&tlist);
485
486 MGeomApply apply;
487 MBadPixelsMerge merge(&fBadPixels);
488 // MExtractSignal extract; // Do not use this at the moment...
489 MExtractPINDiode pinext;
490 MExtractBlindPixel blindext;
491 MExtractSignal2 extract;
492 MCalibrationCalc calcalc;
493
494 MFillH fillpin("MHCalibrationChargePINDiode", "MExtractedSignalPINDiode");
495 //
496 // Apply a filter against cosmics
497 // (will have to be needed in the future
498 // when the calibration hardware-trigger is working)
499 //
500 MFCosmics cosmics;
501 MContinue cont(&cosmics);
502
503 //
504 // As long, as we don't have digital modules,
505 // we have to set the color of the pulser LED by hand
506 //
507 calcalc.SetPulserColor(MCalibrationCalc::kECT1);
508 //calcalc.SkipBlindPixelFit();
509
510 tlist.AddToList(&read);
511 tlist.AddToList(&apply);
512 tlist.AddToList(&merge);
513 tlist.AddToList(&extract);
514 tlist.AddToList(&pinext);
515 tlist.AddToList(&blindext);
516 tlist.AddToList(&cont);
517 tlist.AddToList(&calcalc);
518 tlist.AddToList(&fillpin);
519
520 // Create and setup the eventloop
521 MEvtLoop evtloop(fName);
522 evtloop.SetParList(&plist);
523 evtloop.SetDisplay(fDisplay);
524 evtloop.SetLogStream(fLog);
525
526 // Execute first analysis
527 if (!evtloop.Eventloop())
528 {
529 *fLog << err << GetDescriptor() << ": Failed." << endl;
530 return kFALSE;
531 }
532
533 tlist.PrintStatistics();
534
535 DisplayResult(plist);
536
537 if (!WriteResult())
538 return kFALSE;
539
540 *fLog << inf << GetDescriptor() << ": Done." << endl;
541
542 return kTRUE;
543}
Note: See TracBrowser for help on using the repository browser.