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

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