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

Last change on this file since 3077 was 3077, checked in by gaug, 21 years ago
*** empty log message ***
File size: 15.4 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 1/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
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 "MExtractSignal2.h"
56#include "MCalibrationCalc.h"
57
58#include "MJCalibration.h"
59#include "MStatusDisplay.h"
60
61ClassImp(MJCalibration);
62using namespace std;
63
64MJCalibration::MJCalibration(const char *name, const char *title) : fRuns(0)
65{
66 fName = name ? name : "MJCalibration";
67 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
68}
69
70void MJCalibration::DrawProjection(MHCamera *obj1, Int_t fit) const
71{
72
73 TH1D *obj2 = (TH1D*)obj1->Projection();
74 obj2->Draw();
75 obj2->SetBit(kCanDelete);
76
77 const Double_t min = obj2->GetBinCenter(obj2->GetXaxis()->GetFirst());
78 const Double_t max = obj2->GetBinCenter(obj2->GetXaxis()->GetLast());
79 const Double_t integ = obj2->Integral("width")/2.5;
80 const Double_t mean = obj2->GetMean();
81 const Double_t rms = obj2->GetRMS();
82 const Double_t width = max-min;
83
84 const TString dgausformula = "([0]-[3])/[2]*exp(-0.5*(x-[1])*(x-[1])/[2]/[2])"
85 "+[3]/[5]*exp(-0.5*(x-[4])*(x-[4])/[5]/[5])";
86
87 const TString tgausformula = "([0]-[3]-[6])/[2]*exp(-0.5*(x-[1])*(x-[1])/[2]/[2])"
88 "+[3]/[5]*exp(-0.5*(x-[4])*(x-[4])/[5]/[5])"
89 "+[6]/[8]*exp(-0.5*(x-[7])*(x-[7])/[8]/[8])";
90 TF1 *f=0;
91 switch (fit)
92 {
93 case 1:
94 f = new TF1("sgaus", "gaus(0)", min, max);
95 f->SetLineColor(kYellow);
96 f->SetBit(kCanDelete);
97 f->SetParNames("Area", "#mu", "#sigma");
98 f->SetParameters(integ/rms, mean, rms);
99 f->SetParLimits(0, 0, integ);
100 f->SetParLimits(1, min, max);
101 f->SetParLimits(2, 0, width/1.5);
102
103 obj2->Fit(f, "QLR");
104 break;
105
106 case 2:
107 f = new TF1("dgaus",dgausformula.Data(),min,max);
108 f->SetLineColor(kYellow);
109 f->SetBit(kCanDelete);
110 f->SetParNames("A_{tot}", "#mu1", "#sigma1", "A2", "#mu2", "#sigma2");
111 f->SetParameters(integ,(min+mean)/2.,width/4.,
112 integ/width/2.,(max+mean)/2.,width/4.);
113 // The left-sided Gauss
114 f->SetParLimits(0,integ-1.5 , integ+1.5);
115 f->SetParLimits(1,min+(width/10.), mean);
116 f->SetParLimits(2,0 , width/2.);
117 // The right-sided Gauss
118 f->SetParLimits(3,0 , integ);
119 f->SetParLimits(4,mean, max-(width/10.));
120 f->SetParLimits(5,0 , width/2.);
121 obj2->Fit(f,"QLRM");
122 break;
123
124 case 3:
125 f = new TF1("tgaus",tgausformula.Data(),min,max);
126 f->SetLineColor(kYellow);
127 f->SetBit(kCanDelete);
128 f->SetParNames("A_{tot}","#mu_{1}","#sigma_{1}",
129 "A_{2}","#mu_{2}","#sigma_{2}",
130 "A_{3}","#mu_{3}","#sigma_{3}");
131 f->SetParameters(integ,(min+mean)/2,width/4.,
132 integ/width/3.,(max+mean)/2.,width/4.,
133 integ/width/3.,mean,width/2.);
134 // The left-sided Gauss
135 f->SetParLimits(0,integ-1.5,integ+1.5);
136 f->SetParLimits(1,min+(width/10.),mean);
137 f->SetParLimits(2,width/15.,width/2.);
138 // The right-sided Gauss
139 f->SetParLimits(3,0.,integ);
140 f->SetParLimits(4,mean,max-(width/10.));
141 f->SetParLimits(5,width/15.,width/2.);
142 // The Gauss describing the outliers
143 f->SetParLimits(6,0.,integ);
144 f->SetParLimits(7,min,max);
145 f->SetParLimits(8,width/4.,width/1.5);
146 obj2->Fit(f,"QLRM");
147 break;
148
149 case 4:
150 obj2->Fit("pol0", "Q");
151 obj2->GetFunction("pol0")->SetLineColor(kYellow);
152 break;
153
154 case 9:
155 break;
156
157 default:
158 obj2->Fit("gaus", "Q");
159 obj2->GetFunction("gaus")->SetLineColor(kYellow);
160 break;
161 }
162}
163
164void MJCalibration::CamDraw(TCanvas &c, const Int_t x, const Int_t y, const MHCamera &cam1, const Int_t fit)
165{
166
167
168 c.cd(x);
169 gPad->SetBorderMode(0);
170 MHCamera *obj1=(MHCamera*)cam1.DrawCopy("hist");
171 obj1->AddNotify(fCalibrationCam);
172
173 c.cd(x+y);
174 gPad->SetBorderMode(0);
175 obj1->Draw();
176
177 if (fit)
178 {
179 c.cd(x+2*y);
180 gPad->SetBorderMode(0);
181 DrawProjection(obj1, fit);
182 }
183}
184
185
186void MJCalibration::DisplayResult(MParList &plist)
187{
188 if (!fDisplay)
189 return;
190
191 //
192 // Update display
193 //
194 TString title = fDisplay->GetTitle();
195 title += "-- Calibration ";
196 title += fRuns->GetRunsAsString();
197 title += " --";
198 fDisplay->SetTitle(title);
199
200 //
201 // Get container from list
202 //
203 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
204
205 // Create histograms to display
206 MHCamera disp1 (geomcam, "Cal;Charge", "Fitted Mean Charges");
207 MHCamera disp2 (geomcam, "Cal;SigmaCharge", "Sigma of Fitted Charges");
208 MHCamera disp3 (geomcam, "Cal;FitProb", "Probability of Fit");
209 MHCamera disp4 (geomcam, "Cal;RSigma", "Reduced Sigmas");
210 MHCamera disp5 (geomcam, "Cal;RSigma/Charge", "Reduced Sigma per Charge");
211 MHCamera disp6 (geomcam, "Cal;FFactorPhe", "Nr. of Phe's (F-Factor Method)");
212 MHCamera disp7 (geomcam, "Cal;FFactorConv", "Conversion Factor (F-Factor Method)");
213 MHCamera disp8 (geomcam, "Cal;FFactorFFactor", "Total F-Factor (F-Factor Method)");
214 MHCamera disp9 (geomcam, "Cal;BlindPixPh", "Photon flux inside plexiglass (Blind Pixel Method)");
215 MHCamera disp10 (geomcam, "Cal;BlindPixConv", "Conversion Factor (Blind Pixel Method)");
216 MHCamera disp11 (geomcam, "Cal;BlindPixFFactor","Total F-Factor (Blind Pixel Method)");
217 MHCamera disp12 (geomcam, "Cal;PINDiodePh", "Photons flux outside plexiglass (PIN Diode Method)");
218 MHCamera disp13 (geomcam, "Cal;PINDiodeConv", "Conversion Factor (PIN Diode Method)");
219 MHCamera disp14 (geomcam, "Cal;PINDiodeFFactor","Total F-Factor (PIN Diode Method)");
220 MHCamera disp15 (geomcam, "Cal;Excluded", "Pixels previously excluded");
221 MHCamera disp16 (geomcam, "Cal;NotFitted", "Pixels that could not be fitted");
222 MHCamera disp17 (geomcam, "Cal;NotFitValid", "Pixels with not valid fit results");
223 MHCamera disp18 (geomcam, "Cal;Oscillating", "Oscillating Pixels");
224 MHCamera disp19 (geomcam, "Cal;Saturation", "Pixels with saturated Hi Gain");
225
226
227 // Fitted charge means and sigmas
228 disp1.SetCamContent(fCalibrationCam, 0);
229 disp1.SetCamError( fCalibrationCam, 1);
230 disp2.SetCamContent(fCalibrationCam, 2);
231 disp2.SetCamError( fCalibrationCam, 3);
232 // Fit probabilities
233 disp3.SetCamContent(fCalibrationCam, 4);
234
235 // Reduced Sigmas and reduced sigmas per charge
236 disp4.SetCamContent(fCalibrationCam, 5);
237 disp4.SetCamError( fCalibrationCam, 6);
238 disp5.SetCamContent(fCalibrationCam, 7);
239 disp5.SetCamError( fCalibrationCam, 8);
240
241 // F-Factor Method
242 disp6.SetCamContent(fCalibrationCam, 9);
243 disp6.SetCamError( fCalibrationCam, 10);
244 disp7.SetCamContent(fCalibrationCam, 11);
245 disp7.SetCamError( fCalibrationCam, 12);
246 disp8.SetCamContent(fCalibrationCam, 13);
247 disp8.SetCamError( fCalibrationCam, 14);
248
249 /// Blind Pixel Method
250 disp9.SetCamContent(fCalibrationCam, 15);
251 disp9.SetCamError( fCalibrationCam, 16);
252 disp10.SetCamContent(fCalibrationCam,17);
253 disp10.SetCamError( fCalibrationCam,18);
254 disp11.SetCamContent(fCalibrationCam,19);
255 disp11.SetCamError( fCalibrationCam,20);
256
257 // PIN Diode Method
258 disp12.SetCamContent(fCalibrationCam,21);
259 disp12.SetCamError( fCalibrationCam,22);
260 disp13.SetCamContent(fCalibrationCam,23);
261 disp13.SetCamError( fCalibrationCam,24);
262 disp14.SetCamContent(fCalibrationCam,25);
263 disp14.SetCamError( fCalibrationCam,26);
264
265 // Pixels with defects
266 disp15.SetCamContent(fCalibrationCam,27);
267 disp16.SetCamContent(fCalibrationCam,28);
268 disp17.SetCamContent(fCalibrationCam,29);
269 disp18.SetCamContent(fCalibrationCam,30);
270
271 // Lo Gain calibration
272 disp19.SetCamContent(fCalibrationCam,31);
273
274
275 disp1.SetYTitle("Charge [FADC units]");
276 disp2.SetYTitle("\\sigma_{Charge} [FADC units]");
277 disp3.SetYTitle("P_{Charge} [1]");
278
279 disp4.SetYTitle("\\sqrt{\\sigma^{2}_{Charge} - RMS^{2}_{Ped}} [FADC Counts]");
280 disp5.SetYTitle("Reduced Sigma / Mean Charge [1]");
281
282 disp6.SetYTitle("Nr. Photo-Electrons [1]");
283 disp7.SetYTitle("Conversion Factor [PhE/FADC Count]");
284 disp8.SetYTitle("\\sqrt{N_{PhE}}*\\sigma_{Charge}/\\mu_{Charge} [1]");
285
286 disp9.SetYTitle("Photon flux [ph/mm^2]");
287 disp10.SetYTitle("Conversion Factor [Phot/FADC Count]");
288 disp11.SetYTitle("\\sqrt{N_{Ph}}*\\sigma_{Charge}/\\mu_{Charge} [1]");
289
290 disp12.SetYTitle("Photon flux [ph/mm^2]");
291 disp13.SetYTitle("Conversion Factor [Phot/FADC Count]");
292 disp14.SetYTitle("\\sqrt{N_{Ph}}*\\sigma_{Charge}/\\mu_{Charge} [1]");
293
294 disp15.SetYTitle("[1]");
295 disp16.SetYTitle("[1]");
296 disp17.SetYTitle("[1]");
297 disp18.SetYTitle("[1]");
298
299 gStyle->SetOptStat(1111);
300 gStyle->SetOptFit();
301
302 // Charges
303 TCanvas &c1 = fDisplay->AddTab("Fit.Charge");
304 c1.Divide(2, 3);
305
306 CamDraw(c1, 1, 2, disp1, 2);
307 CamDraw(c1, 2, 2, disp2, 2);
308
309 // Fit Probability
310 TCanvas &c2 = fDisplay->AddTab("Fit.Prob");
311 c2.Divide(1,3);
312
313 CamDraw(c2, 1, 1, disp3, 4);
314
315 // Reduced Sigmas
316 TCanvas &c3 = fDisplay->AddTab("Red.Sigma");
317 c3.Divide(2,3);
318
319 CamDraw(c3, 1, 2, disp4, 2);
320 CamDraw(c3, 2, 2, disp5, 2);
321
322 // F-Factor Method
323 TCanvas &c4 = fDisplay->AddTab("F-Factor");
324 c4.Divide(3,3);
325
326 CamDraw(c4, 1, 3, disp6, 2);
327 CamDraw(c4, 2, 3, disp7, 2);
328 CamDraw(c4, 3, 3, disp8, 2);
329
330 // Blind Pixel Method
331 TCanvas &c5 = fDisplay->AddTab("BlindPix");
332 c5.Divide(3, 3);
333
334 CamDraw(c5, 1, 3, disp9, 9);
335 CamDraw(c5, 2, 3, disp10, 2);
336 CamDraw(c5, 3, 3, disp11, 2);
337
338 // PIN Diode Method
339 TCanvas &c6 = fDisplay->AddTab("PINDiode");
340 c6.Divide(3,3);
341
342 CamDraw(c6, 1, 3, disp12, 9);
343 CamDraw(c6, 2, 3, disp13, 2);
344 CamDraw(c6, 3, 3, disp14, 2);
345
346 // Defects
347 TCanvas &c7 = fDisplay->AddTab("Defects");
348 c7.Divide(4,2);
349
350 CamDraw(c7, 1, 4, disp15, 0);
351 CamDraw(c7, 2, 4, disp16, 0);
352 CamDraw(c7, 3, 4, disp17, 0);
353 CamDraw(c7, 4, 4, disp18, 0);
354
355 // Lo Gain Calibration
356 TCanvas &c8 = fDisplay->AddTab("LowGain");
357 c8.Divide(1,3);
358
359 CamDraw(c8, 1, 4, disp19, 0);
360
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 MExtractSignal2 extract;
490 MCalibrationCalc calcalc;
491
492 //
493 // As long, as we don't have digital modules,
494 // we have to set the color of the pulser LED by hand
495 //
496 calcalc.SetPulserColor(MCalibrationCalc::kECT1);
497 //calcalc.SkipBlindPixelFit();
498
499 tlist.AddToList(&read);
500 tlist.AddToList(&apply);
501 tlist.AddToList(&merge);
502 tlist.AddToList(&extract);
503 tlist.AddToList(&calcalc);
504
505 // Create and setup the eventloop
506 MEvtLoop evtloop(fName);
507 evtloop.SetParList(&plist);
508 evtloop.SetDisplay(fDisplay);
509 evtloop.SetLogStream(fLog);
510
511 // Execute first analysis
512 if (!evtloop.Eventloop())
513 {
514 *fLog << err << GetDescriptor() << ": Failed." << endl;
515 return kFALSE;
516 }
517
518 tlist.PrintStatistics();
519
520 DisplayResult(plist);
521
522 if (!WriteResult())
523 return kFALSE;
524
525 *fLog << inf << GetDescriptor() << ": Done." << endl;
526
527 return kTRUE;
528}
Note: See TracBrowser for help on using the repository browser.