source: trunk/Mars/mmuon/MHSingleMuon.cc@ 14906

Last change on this file since 14906 was 14896, checked in by tbretz, 12 years ago
Added MCalibrateFact and MCalibrateDrsTimes
File size: 17.5 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): Keiichi Mase, 10/2004
19! Author(s): Markus Meyer, 02/2005 <mailto:meyer@astro.uni-wuerzburg.de>
20! Author(s): Thomas Bretz, 04/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
21!
22! Copyright: MAGIC Software Development, 2000-2005
23!
24!
25\* ======================================================================== */
26
27/////////////////////////////////////////////////////////////////////////////
28//
29// MHSingleMuon
30//
31// This class is a histogram class for displaying the radial (fHistWidth)
32// and the azimuthal (fHistPhi) intensity distribution for one muon.
33// You can retrieve the histogram (TH1F) using the function GetHistPhi()
34// or GetHistWidth().
35// From these histograms the fraction of the ring segment (ArcPhi) and the
36// Width of the muon ring (ArcWidth) is calculated.
37//
38// First, the radius and center of the ring has to be calculted by
39// MMuonSearchParCalc
40// After that the histograms has to be filled in the following way:
41//
42// MFillH fillmuon("MHSingleMuon", "", "FillMuon");
43//
44// The allowed region to estimate ArcPhi is a certain margin around the
45// radius. The default value is 0.2 deg (60mm). If the estimated radius
46// of the arc is 1.0 deg, the pixel contents in the radius range from
47// 0.8 deg to 1.2 deg are fill in the histogram.
48//
49// For ArcPhi only bins over a certain threshold are supposed to be part
50// of the ring.
51// For ArcWidth, the same algorithm is used to determine the fit region
52// for a gaussian fit to the radial intensity distribution. The ArcWidth
53// is defined as the sigma value of the gaussian fit.
54//
55// The binning of the histograms can be changed in the following way:
56//
57// MBinning bins1("BinningMuonWidth");
58// MBinning bins2("BinningArcPhi");
59// bins1.SetEdges(28, 0.3, 1.7);
60// bins2.SetEdges(20, -180,180);
61// plist.AddToList(&bins1);
62// plist.AddToList(&bins2);
63//
64// The values for the thresholds and the margin are saved in MMuonSetup.
65// They can be easily changed in star.rc.
66//
67// Please have in mind, that changes in this basic parameters will change
68// your results!!
69//
70// InputContainer:
71// - MGeomCam
72// - MMuonSearchPar
73//
74//
75// Class Version 2:
76// ----------------
77// + Double_t fRelTimeMean; // Result of the gaus fit to the arrival time
78// + Double_t fRelTimeSigma; // Result of the gaus fit to the arrival time
79//
80////////////////////////////////////////////////////////////////////////////
81#include "MHSingleMuon.h"
82
83#include <TF1.h>
84#include <TPad.h>
85#include <TCanvas.h>
86
87#include "MLog.h"
88#include "MLogManip.h"
89
90#include "MBinning.h"
91#include "MParList.h"
92
93#include "MGeomCam.h"
94#include "MGeomPix.h"
95
96#include "MSignalCam.h"
97#include "MSignalPix.h"
98
99#include "MMuonSetup.h"
100#include "MMuonCalibPar.h"
101#include "MMuonSearchPar.h"
102
103ClassImp(MHSingleMuon);
104
105using namespace std;
106
107// --------------------------------------------------------------------------
108//
109// Setup histograms
110//
111MHSingleMuon::MHSingleMuon(const char *name, const char *title) :
112 fSignalCam(0), fMuonSearchPar(0), fGeomCam(0), fMargin(0)
113{
114 fName = name ? name : "MHSingleMuon";
115 fTitle = title ? title : "Histograms of muon parameters";
116
117 fHistPhi.SetName("HistPhi");
118 fHistPhi.SetTitle("HistPhi");
119 fHistPhi.SetXTitle("\\phi [\\circ]");
120 fHistPhi.SetYTitle("sum of ADC");
121 fHistPhi.SetDirectory(NULL);
122 fHistPhi.SetFillStyle(4000);
123 fHistPhi.UseCurrentStyle();
124
125 fHistWidth.SetName("HistWidth");
126 fHistWidth.SetTitle("HistWidth");
127 fHistWidth.SetXTitle("distance from the ring center [\\circ]");
128 fHistWidth.SetYTitle("sum of ADC");
129 fHistWidth.SetDirectory(NULL);
130 fHistWidth.SetFillStyle(4000);
131 fHistWidth.UseCurrentStyle();
132
133 fHistTime.SetName("HistTime");
134 fHistTime.SetTitle("HistTime");
135 fHistTime.SetXTitle("timing difference");
136 fHistTime.SetYTitle("Counts");
137 fHistTime.SetDirectory(NULL);
138 fHistTime.SetFillStyle(4000);
139 fHistTime.UseCurrentStyle();
140
141 MBinning bins;
142 bins.SetEdges(20, -180, 180);
143 bins.Apply(fHistPhi);
144
145 bins.SetEdges(28, 0.3, 1.7);
146 bins.Apply(fHistWidth);
147
148 bins.SetEdges(101, -33, 33); // +/- 33ns
149 bins.Apply(fHistTime);
150}
151
152// --------------------------------------------------------------------------
153//
154// Setup the Binning for the histograms automatically if the correct
155// instances of MBinning
156//
157Bool_t MHSingleMuon::SetupFill(const MParList *plist)
158{
159 fGeomCam = (MGeomCam*)plist->FindObject("MGeomCam");
160 if (!fGeomCam)
161 {
162 *fLog << warn << "MGeomCam not found... abort." << endl;
163 return kFALSE;
164 }
165 fMuonSearchPar = (MMuonSearchPar*)plist->FindObject("MMuonSearchPar");
166 if (!fMuonSearchPar)
167 {
168 *fLog << warn << "MMuonSearchPar not found... abort." << endl;
169 return kFALSE;
170 }
171 fSignalCam = (MSignalCam*)plist->FindObject("MSignalCam");
172 if (!fSignalCam)
173 {
174 *fLog << warn << "MSignalCam not found... abort." << endl;
175 return kFALSE;
176 }
177
178 MMuonSetup *setup = (MMuonSetup*)const_cast<MParList*>(plist)->FindCreateObj("MMuonSetup");
179 if (!setup)
180 return kFALSE;
181
182 fMargin = setup->GetMargin()/fGeomCam->GetConvMm2Deg();
183
184 ApplyBinning(*plist, "ArcPhi", fHistPhi);
185 ApplyBinning(*plist, "MuonWidth", fHistWidth);
186 ApplyBinning(*plist, "MuonTime", fHistTime);
187
188 return kTRUE;
189}
190
191// --------------------------------------------------------------------------
192//
193// Fill the histograms with data from a MMuonCalibPar and
194// MMuonSearchPar container.
195//
196Int_t MHSingleMuon::Fill(const MParContainer *par, const Stat_t w)
197{
198 fRelTimeMean = 0;
199 fRelTimeSigma = -1;
200
201 fHistPhi.Reset();
202 fHistWidth.Reset();
203 fHistTime.Reset();
204
205 const Int_t entries = fSignalCam->GetNumPixels();
206
207 // the position of the center of a muon ring
208 const Float_t cenx = fMuonSearchPar->GetCenterX();
209 const Float_t ceny = fMuonSearchPar->GetCenterY();
210
211 for (Int_t i=0; i<entries; i++)
212 {
213 const MSignalPix &pix = (*fSignalCam)[i];
214 if (fUseCleanedSignal && !pix.IsPixelUsed())
215 continue;
216
217 const MGeom &gpix = (*fGeomCam)[i];
218
219 const Float_t dx = gpix.GetX() - cenx;
220 const Float_t dy = gpix.GetY() - ceny;
221
222 const Float_t dist = TMath::Hypot(dx, dy);
223
224 // if the signal is not near the estimated circle, it is ignored.
225 if (TMath::Abs(dist-fMuonSearchPar->GetRadius())<fMargin)
226 {
227 // The arrival time is aligned around 0 for smaller
228 // and more stable histogram range
229 fHistTime.Fill(pix.GetArrivalTime()-fMuonSearchPar->GetTime());
230 }
231
232 // use only the inner pixels. FIXME: This is geometry dependent
233 if (gpix.GetAidx()>0)
234 continue;
235
236 fHistWidth.Fill(dist*fGeomCam->GetConvMm2Deg(), pix.GetNumPhotons());
237 }
238
239 if (!fUseCleanedSignal)
240 {
241 // Setup the function and perform the fit
242 TF1 g1("g1", "gaus");//, -fHistTime.GetXmin(), fHistTime.GetXmax());
243
244 // Choose starting values as accurate as possible
245 g1.SetParameter(0, fHistTime.GetMaximum());
246 g1.SetParameter(1, 0);
247 g1.SetParameter(2, 0.7); // FIXME! GetRMS instead???
248
249 // According to fMuonSearchPar->GetTimeRMS() identified muons
250 // do not have an arrival time rms>3
251 g1.SetParLimits(1, -1.7, 1.7);
252 g1.SetParLimits(2, 0, 3.4);
253
254 // options : N do not store the function, do not draw
255 // I use integral of function in bin rather than value at bin center
256 // R use the range specified in the function range
257 // Q quiet mode
258 if (fHistTime.Fit(&g1, "QNB"))
259 return kTRUE;
260
261 fRelTimeMean = g1.GetParameter(1);
262 fRelTimeSigma = g1.GetParameter(2);
263 }
264 else
265 {
266 fRelTimeMean = fMuonSearchPar->GetTime();
267 fRelTimeSigma = fMuonSearchPar->GetTimeRms();
268 }
269
270 // The mean arrival time which was subtracted before will
271 // be added again, now
272 const Double_t tm0 = fMuonSearchPar->GetTime()+fRelTimeMean;
273
274 for (Int_t i=0; i<entries; i++)
275 {
276 const MSignalPix &pix = (*fSignalCam)[i];
277 if (fUseCleanedSignal && !pix.IsPixelUsed())
278 continue;
279
280 const MGeom &gpix = (*fGeomCam)[i];
281
282 const Float_t dx = gpix.GetX() - cenx;
283 const Float_t dy = gpix.GetY() - ceny;
284
285 const Float_t dist = TMath::Hypot(dx, dy);
286
287 // if the signal is not near the estimated circle, it is ignored.
288 if (TMath::Abs(dist-fMuonSearchPar->GetRadius())<fMargin &&
289 (fUseCleanedSignal || TMath::Abs(pix.GetArrivalTime()-tm0) < 2*fRelTimeSigma))
290 {
291 fHistPhi.Fill(TMath::ATan2(dx, dy)*TMath::RadToDeg(), pix.GetNumPhotons());
292 }
293 }
294
295 return kTRUE;
296
297/*
298 // Because the errors (sqrt(content)) are only scaled by a fixed
299 // factor, and the absolute value of the error is nowhere
300 // needed we skip this step
301
302 // error estimation (temporarily)
303 // The error is estimated from the signal. In order to do so, we have to
304 // once convert the signal from ADC to photo-electron. Then we can get
305 // the fluctuation such as F-factor*sqrt(phe).
306 // Up to now, the error of pedestal is not taken into accout. This is not
307 // of course correct. We will include this soon.
308 const Double_t Ffactor = 1.4;
309 for (Int_t i=0; i<fHistPhi.GetNbinsX()+1; i++)
310 fHistPhi.SetBinError(i, fHistPhi.GetBinError(i)*Ffactor);
311
312 for (Int_t i=0; i<fHistWidth.GetNbinsX()+1; i++)
313 fHistWidth.SetBinError(i, fHistWidth.GetBinError(i)*Ffactor);
314
315 return kTRUE;
316 */
317}
318
319// --------------------------------------------------------------------------
320//
321// Find the first bins starting at the bin with maximum content in both
322// directions which are below threshold.
323// If in a range of half the histogram size in both directions no bin
324// below the threshold is found, kFALSE is returned.
325//
326Bool_t MHSingleMuon::FindRangeAboveThreshold(const TProfile &h, Float_t thres, Int_t &first, Int_t &last) const
327{
328 const Int_t n = h.GetNbinsX();
329 const Int_t maxbin = h.GetMaximumBin();
330 const Int_t edge = maxbin+n/2;
331
332 // Search from the peak to the right
333 last = -1;
334 for (Int_t i=maxbin; i<edge; i++)
335 {
336 const Float_t val = h.GetBinContent(i%n + 1);
337 if (val<thres)
338 {
339 last = i%n+1;
340 break;
341 }
342 }
343
344 // Search from the peak to the left
345 first = -1;
346 for (Int_t i=maxbin+n-1; i>=edge; i--)
347 {
348 const Float_t val = h.GetBinContent(i%n + 1);
349 if (val<thres)
350 {
351 first = i%n+1;
352 break;
353 }
354 }
355
356 return first>=0 && last>=0;
357}
358
359// --------------------------------------------------------------------------
360//
361// Photon distribution along the estimated circle is fitted with theoritical
362// function in order to get some more information such as Arc Phi and Arc
363// Length.
364//
365Bool_t MHSingleMuon::CalcPhi(Double_t thres, Double_t &peakphi, Double_t &arcphi) const
366{
367 if (fHistPhi.GetMaximum()<thres)
368 return kFALSE;
369
370 peakphi = 180.-fHistPhi.GetBinCenter(fHistPhi.GetMaximumBin());
371
372 // Now find the position at which the peak edges crosses the threshold
373 Int_t first, last;
374
375 FindRangeAboveThreshold(fHistPhi, thres, first, last);
376
377 const Int_t n = fHistPhi.GetNbinsX();
378 const Int_t edge = fHistPhi.GetMaximumBin()+n/2;
379 if (first<0)
380 first = (edge-1)%n+1;
381 if (last<0)
382 last = edge%n+1;;
383
384 const Float_t startfitval = fHistPhi.GetBinLowEdge(first+1);
385 const Float_t endfitval = fHistPhi.GetBinLowEdge(last);
386
387 arcphi = last-1<first ? 360+(endfitval-startfitval) : endfitval-startfitval;
388
389 //if (fEnableImpactCalc)
390 // CalcImpact(effbinnum, startfitval, endfitval);
391
392 return kTRUE;
393}
394
395// --------------------------------------------------------------------------
396//
397// Photon distribution of distance from the center of estimated ring is
398// fitted in order to get some more information such as ARC WIDTH which
399// can represent to the PSF of our reflector.
400//
401// thres: Threshold above zero to determin the edges of the peak which
402// is used as fit range
403// width: ArcWidth returned in deg
404// chi: Chi^2/NDF of the fit
405//
406Bool_t MHSingleMuon::CalcWidth(Double_t thres, Double_t &width, Double_t &chi)
407{
408 Int_t first, last;
409 if (!FindRangeAboveThreshold(fHistWidth, thres, first, last))
410 return kFALSE;
411
412 // This happens in some cases
413 const Int_t n = fHistWidth.GetNbinsX()/2;
414 const Int_t m = fHistWidth.GetMaximumBin();
415 if (first>last)
416 {
417 if (m>n) // If maximum is on the right side of histogram
418 last = n;
419 else
420 first = 0; // If maximum is on the left side of histogram
421 }
422
423 if (last-first<=3)
424 return kFALSE;
425
426 // Now get the fit range
427 const Float_t startfitval = fHistWidth.GetBinLowEdge(first+1);
428 const Float_t endfitval = fHistWidth.GetBinLowEdge(last);
429
430 // Setup the function and perform the fit
431 TF1 f1("f1", "gaus + [3]", startfitval, endfitval);
432 f1.SetLineColor(kBlue);
433
434 // Choose starting values as accurate as possible
435 f1.SetParameter(0, fHistWidth.GetMaximum());
436 f1.SetParameter(1, fHistWidth.GetBinCenter(m));
437// f1.SetParameter(2, (endfitval-startfitval)/2);
438 f1.SetParameter(2, 0.1);
439 f1.SetParameter(3, 1.8);
440
441 // options : N do not store the function, do not draw
442 // I use integral of function in bin rather than value at bin center
443 // R use the range specified in the function range
444 // Q quiet mode
445// fHistWidth.Fit(&f1, "QRO");
446 if (fHistWidth.Fit(&f1, "QRN"))
447 return kFALSE;
448
449 chi = f1.GetChisquare()/f1.GetNDF();
450 width = f1.GetParameter(2);
451
452 return kTRUE;
453}
454
455/*
456// --------------------------------------------------------------------------
457//
458// An impact parameter is calculated by fitting the histogram of photon
459// distribution along the circle with a theoritical model.
460// (See G. Vacanti et. al., Astroparticle Physics 2, 1994, 1-11.
461// The function (6) is used here.)
462//
463// By default this calculation is suppressed because this calculation is
464// very time consuming. If you want to calculate an impact parameter,
465// you can call the function of EnableImpactCalc().
466//
467void MMuonCalibParCalc::CalcImpact(Int_t effbinnum, Float_t startfitval, Float_t endfitval)
468{
469 // Fit the distribution with Vacanti function. The function is different
470 // for the impact parameter of inside or outside of our reflector.
471 // Then two different functions are applied to the photon distribution,
472 // and the one which give us smaller chisquare value is taken as a
473 // proper one.
474
475 Double_t val1,err1,val2,err2;
476 // impact parameter inside mirror radius (8.5m)
477 TString func1;
478 Float_t tmpval = (*fMuonSearchPar).GetRadius()*(*fGeomCam).GetConvMm2Deg()*TMath::DegToRad();
479 tmpval = sin(2.*tmpval)*8.5;
480 func1 += "[0]*";
481 func1 += tmpval;
482 func1 += "*(sqrt(1.-([1]/8.5)**2*sin((x-[2])*3.1415926/180.)**2)+([1]/8.5)*cos((x-[2])*3.1415926/180.))";
483
484 TF1 f1("f1",func1,startfitval,endfitval);
485 f1.SetParameters(2000,3,0);
486 f1.SetParLimits(1,0,8.5);
487 f1.SetParLimits(2,-180.,180.);
488
489 fMuonCalibPar->fHistPhi->Fit("f1","RQEM");
490
491 Float_t chi1 = -1;
492 Float_t chi2 = -1;
493 if(effbinnum>3)
494 chi1 = f1.GetChisquare()/((Float_t)(effbinnum-3));
495
496 gMinuit->GetParameter(1,val1,err1); // get the estimated IP
497 Float_t estip1 = val1;
498
499 // impact parameter beyond mirror area (8.5m)
500 TString func2;
501 Float_t tmpval2 = (*fMuonSearchPar).GetRadius()*(*fGeomCam).GetConvMm2Deg()*TMath::DegToRad();
502 tmpval2 = sin(2.*tmpval2)*8.5*2.;
503 func2 += "[0]*";
504 func2 += tmpval2;
505 func2 += "*sqrt(1.-(([1]/8.5)*sin((x-[2])*3.1415926/180.))**2)";
506
507 TF1 f2("f2",func2,startfitval,endfitval);
508 f2.SetParameters(2000,20,0);
509 f2.SetParLimits(1,8.5,300.);
510 f2.SetParLimits(2,-180.,180.);
511
512 fMuonCalibPar->fHistPhi->Fit("f2","RQEM+");
513
514 if(effbinnum>3)
515 chi2 = f2.GetChisquare()/((Float_t)(effbinnum-3));
516
517 gMinuit->GetParameter(1,val2,err2); // get the estimated IP
518 Float_t estip2 = val2;
519
520 if(effbinnum<=3)
521 {
522 fMuonCalibPar->SetEstImpact(-1.);
523 }
524 if(chi2 > chi1)
525 {
526 fMuonCalibPar->SetEstImpact(estip1);
527 fMuonCalibPar->SetChiArcPhi(chi1);
528 }
529 else
530 {
531 fMuonCalibPar->SetEstImpact(estip2);
532 fMuonCalibPar->SetChiArcPhi(chi2);
533 }
534}
535*/
536
537Float_t MHSingleMuon::CalcSize() const
538{
539 const Int_t n = fHistPhi.GetNbinsX();
540
541 Double_t sz=0;
542 for (Int_t i=1; i<=n; i++)
543 sz += fHistPhi.GetBinContent(i)*fHistPhi.GetBinEntries(i);
544
545 return sz;
546}
547
548void MHSingleMuon::Paint(Option_t *o)
549{
550 TF1 *f = fHistWidth.GetFunction("f1");
551 if (f)
552 f->ResetBit(1<<9);
553}
554
555void MHSingleMuon::Draw(Option_t *o)
556{
557 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
558 pad->SetBorderMode(0);
559
560 AppendPad("");
561
562 pad->Divide(1,2);
563
564 pad->cd(1);
565 gPad->SetBorderMode(0);
566 fHistPhi.Draw();
567
568 pad->cd(2);
569 gPad->SetBorderMode(0);
570 fHistWidth.Draw();
571}
Note: See TracBrowser for help on using the repository browser.