source: trunk/MagicSoft/Mars/mmuon/MHSingleMuon.cc@ 8040

Last change on this file since 8040 was 7369, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 16.6 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////////////////////////////////////////////////////////////////////////////
76#include "MHSingleMuon.h"
77
78#include <TF1.h>
79#include <TMinuit.h>
80#include <TPad.h>
81#include <TCanvas.h>
82
83#include "MLog.h"
84#include "MLogManip.h"
85
86#include "MBinning.h"
87#include "MParList.h"
88
89#include "MGeomCam.h"
90#include "MGeomPix.h"
91
92#include "MSignalCam.h"
93#include "MSignalPix.h"
94
95#include "MMuonSetup.h"
96#include "MMuonCalibPar.h"
97#include "MMuonSearchPar.h"
98
99ClassImp(MHSingleMuon);
100
101using namespace std;
102
103// --------------------------------------------------------------------------
104//
105// Setup histograms
106//
107MHSingleMuon::MHSingleMuon(const char *name, const char *title) :
108 fSignalCam(0), fMuonSearchPar(0), fGeomCam(0), fMargin(0)
109{
110 fName = name ? name : "MHSingleMuon";
111 fTitle = title ? title : "Histograms of muon parameters";
112
113 fHistPhi.SetName("HistPhi");
114 fHistPhi.SetTitle("HistPhi");
115 fHistPhi.SetXTitle("\\phi [\\circ]");
116 fHistPhi.SetYTitle("sum of ADC");
117 fHistPhi.SetDirectory(NULL);
118 fHistPhi.SetFillStyle(4000);
119 fHistPhi.UseCurrentStyle();
120
121 fHistWidth.SetName("HistWidth");
122 fHistWidth.SetTitle("HistWidth");
123 fHistWidth.SetXTitle("distance from the ring center [\\circ]");
124 fHistWidth.SetYTitle("sum of ADC");
125 fHistWidth.SetDirectory(NULL);
126 fHistWidth.SetFillStyle(4000);
127 fHistWidth.UseCurrentStyle();
128
129 fHistTime.SetName("HistTime");
130 fHistTime.SetTitle("HistTime");
131 fHistTime.SetXTitle("timing difference");
132 fHistTime.SetYTitle("Counts");
133 fHistTime.SetDirectory(NULL);
134 fHistTime.SetFillStyle(4000);
135 fHistTime.UseCurrentStyle();
136
137 MBinning bins;
138 bins.SetEdges(20, -180, 180);
139 bins.Apply(fHistPhi);
140
141 bins.SetEdges(28, 0.3, 1.7);
142 bins.Apply(fHistWidth);
143
144 bins.SetEdges(101, -10, 10);
145 bins.Apply(fHistTime);
146}
147
148// --------------------------------------------------------------------------
149//
150// Setup the Binning for the histograms automatically if the correct
151// instances of MBinning
152//
153Bool_t MHSingleMuon::SetupFill(const MParList *plist)
154{
155 fGeomCam = (MGeomCam*)plist->FindObject("MGeomCam");
156 if (!fGeomCam)
157 {
158 *fLog << warn << "MGeomCam not found... abort." << endl;
159 return kFALSE;
160 }
161 fMuonSearchPar = (MMuonSearchPar*)plist->FindObject("MMuonSearchPar");
162 if (!fMuonSearchPar)
163 {
164 *fLog << warn << "MMuonSearchPar not found... abort." << endl;
165 return kFALSE;
166 }
167 fSignalCam = (MSignalCam*)plist->FindObject("MSignalCam");
168 if (!fSignalCam)
169 {
170 *fLog << warn << "MSignalCam not found... abort." << endl;
171 return kFALSE;
172 }
173
174 MMuonSetup *setup = (MMuonSetup*)const_cast<MParList*>(plist)->FindCreateObj("MMuonSetup");
175 if (!setup)
176 return kFALSE;
177
178 fMargin = setup->GetMargin()/fGeomCam->GetConvMm2Deg();
179
180 ApplyBinning(*plist, "ArcPhi", &fHistPhi);
181 ApplyBinning(*plist, "MuonWidth", &fHistWidth);
182 ApplyBinning(*plist, "MuonTime", &fHistTime);
183
184 return kTRUE;
185}
186
187// --------------------------------------------------------------------------
188//
189// Fill the histograms with data from a MMuonCalibPar and
190// MMuonSearchPar container.
191//
192Bool_t MHSingleMuon::Fill(const MParContainer *par, const Stat_t w)
193{
194 fHistPhi.Reset();
195 fHistWidth.Reset();
196 fHistTime.Reset();
197
198 const Int_t entries = fSignalCam->GetNumPixels();
199
200 // the position of the center of a muon ring
201 const Float_t cenx = fMuonSearchPar->GetCenterX();
202 const Float_t ceny = fMuonSearchPar->GetCenterY();
203
204 for (Int_t i=0; i<entries; i++)
205 {
206 const MSignalPix &pix = (*fSignalCam)[i];
207 const MGeomPix &gpix = (*fGeomCam)[i];
208
209 const Float_t dx = gpix.GetX() - cenx;
210 const Float_t dy = gpix.GetY() - ceny;
211
212 const Float_t dist = TMath::Hypot(dx, dy);
213
214 // if the signal is not near the estimated circle, it is ignored.
215 if (TMath::Abs(dist-fMuonSearchPar->GetRadius())<fMargin)
216 {
217 fHistTime.Fill(pix.GetArrivalTime()-fMuonSearchPar->GetTime());
218 }
219
220 // use only the inner pixles. FIXME: This is geometry dependent
221 if(i>397)
222 continue;
223
224 fHistWidth.Fill(dist*fGeomCam->GetConvMm2Deg(), pix.GetNumPhotons());
225 }
226 // Setup the function and perform the fit
227 TF1 g1("g1", "gaus", -10, 10);
228
229 // Choose starting values as accurate as possible
230 g1.SetParameter(0, fHistTime.GetMaximum());
231 g1.SetParameter(1, 0);
232 g1.SetParameter(2, 0.2);
233
234 g1.SetParLimits(1, -0.5, 0.5);
235 g1.SetParLimits(2, 0, 1);
236 // options : N do not store the function, do not draw
237 // I use integral of function in bin rather than value at bin center
238 // R use the range specified in the function range
239 // Q quiet mode
240 fHistTime.Fit(&g1, "QNRB");
241
242 // Double_t err;
243 Double_t sig, mean, dummy;
244 gMinuit->GetParameter(1, mean, dummy); // get the mean value
245 gMinuit->GetParameter(2, sig, dummy); // get the sigma value
246
247 for (Int_t i=0; i<entries; i++)
248 {
249 const MSignalPix &pix = (*fSignalCam)[i];
250 const MGeomPix &gpix = (*fGeomCam)[i];
251
252 const Float_t dx = gpix.GetX() - cenx;
253 const Float_t dy = gpix.GetY() - ceny;
254
255 const Float_t dist = TMath::Hypot(dx, dy);
256
257 // if the signal is not near the estimated circle, it is ignored.
258 if (TMath::Abs(dist-fMuonSearchPar->GetRadius())<fMargin &&
259 TMath::Abs(pix.GetArrivalTime()-fMuonSearchPar->GetTime()-mean) < 2*sig)
260 {
261 fHistPhi.Fill(TMath::ATan2(dx, dy)*TMath::RadToDeg(), pix.GetNumPhotons());
262 }
263 }
264
265 // error estimation (temporaly)
266 // The error is estimated from the signal. In order to do so, we have to
267 // once convert the signal from ADC to photo-electron. Then we can get
268 // the fluctuation such as F-factor*sqrt(phe).
269 // Up to now, the error of pedestal is not taken into accout. This is not
270 // of course correct. We will include this soon.
271 const Double_t Ffactor = 1.4;
272 for (Int_t i=0; i<fHistPhi.GetNbinsX()+1; i++)
273 {
274 const Float_t abs = TMath::Abs(fHistPhi.GetBinContent(i));
275 const Float_t rougherr = TMath::Sqrt(abs)*Ffactor;
276
277 fHistPhi.SetBinError(i, rougherr);
278 }
279
280 for (Int_t i=0; i<fHistWidth.GetNbinsX()+1; i++)
281 {
282 const Float_t abs = TMath::Abs(fHistWidth.GetBinContent(i));
283 const Float_t rougherr = TMath::Sqrt(abs)*Ffactor;
284
285 fHistWidth.SetBinError(i, rougherr);
286 }
287
288 return kTRUE;
289}
290
291// --------------------------------------------------------------------------
292//
293// Find the first bins starting at the bin with maximum content in both
294// directions which are below threshold.
295// If in a range of half the histogram size in both directions no bin
296// below the threshold is found, kFALSE is returned.
297//
298Bool_t MHSingleMuon::FindRangeAboveThreshold(const TProfile &h, Float_t thres, Int_t &first, Int_t &last) const
299{
300 const Int_t n = h.GetNbinsX();
301 const Int_t maxbin = h.GetMaximumBin();
302 const Int_t edge = maxbin+n/2;
303
304 // Search from the peak to the right
305 last = -1;
306 for (Int_t i=maxbin; i<edge; i++)
307 {
308 const Float_t val = h.GetBinContent(i%n + 1);
309 if (val<thres)
310 {
311 last = i%n+1;
312 break;
313 }
314 }
315
316 // Search from the peak to the left
317 first = -1;
318 for (Int_t i=maxbin+n-1; i>=edge; i--)
319 {
320 const Float_t val = h.GetBinContent(i%n + 1);
321 if (val<thres)
322 {
323 first = i%n+1;
324 break;
325 }
326 }
327
328 return first>=0 && last>=0;
329}
330
331// --------------------------------------------------------------------------
332//
333// Photon distribution along the estimated circle is fitted with theoritical
334// function in order to get some more information such as Arc Phi and Arc
335// Length.
336//
337Bool_t MHSingleMuon::CalcPhi(Double_t thres, Double_t &peakphi, Double_t &arcphi) const
338{
339 if (fHistPhi.GetMaximum()<thres)
340 return kFALSE;
341
342 peakphi = 180.-fHistPhi.GetBinCenter(fHistPhi.GetMaximumBin());
343
344 // Now find the position at which the peak edges crosses the threshold
345 Int_t first, last;
346
347 FindRangeAboveThreshold(fHistPhi, thres, first, last);
348
349 const Int_t n = fHistPhi.GetNbinsX();
350 const Int_t edge = fHistPhi.GetMaximumBin()+n/2;
351 if (first<0)
352 first = (edge-1)%n+1;
353 if (last<0)
354 last = edge%n+1;;
355
356 const Float_t startfitval = fHistPhi.GetBinLowEdge(first+1);
357 const Float_t endfitval = fHistPhi.GetBinLowEdge(last);
358
359 arcphi = last-1<first ? 360+(endfitval-startfitval) : endfitval-startfitval;
360
361 //if (fEnableImpactCalc)
362 // CalcImpact(effbinnum, startfitval, endfitval);
363
364 return kTRUE;
365}
366
367// --------------------------------------------------------------------------
368//
369// Photon distribution of distance from the center of estimated ring is
370// fitted in order to get some more information such as ARC WIDTH which
371// can represent to the PSF of our reflector.
372//
373// thres: Threshold above zero to determin the edges of the peak which
374// is used as fit range
375// width: ArcWidth returned in deg
376// chi: Chi^2/NDF of the fit
377//
378Bool_t MHSingleMuon::CalcWidth(Double_t thres, Double_t &width, Double_t &chi)
379{
380 Int_t first, last;
381
382 if (!FindRangeAboveThreshold(fHistWidth, thres, first, last))
383 return kFALSE;
384
385 // This happens in some cases
386 const Int_t n = fHistWidth.GetNbinsX()/2;
387 const Int_t m = fHistWidth.GetMaximumBin();
388 if (first>last)
389 if (m>n) // If maximum is on the right side of histogram
390 last = n;
391 else
392 first = 0; // If maximum is on the left side of histogram
393
394 if (last-first<=3)
395 return kFALSE;
396
397 // Now get the fit range
398 const Float_t startfitval = fHistWidth.GetBinLowEdge(first+1);
399 const Float_t endfitval = fHistWidth.GetBinLowEdge(last);
400
401 // Setup the function and perform the fit
402 TF1 f1("f1", "gaus + [3]", startfitval, endfitval);
403 f1.SetLineColor(kBlue);
404
405 // Choose starting values as accurate as possible
406 f1.SetParameter(0, fHistWidth.GetMaximum());
407 f1.SetParameter(1, fHistWidth.GetBinCenter(m));
408// f1.SetParameter(2, (endfitval-startfitval)/2);
409 f1.SetParameter(2, 0.1);
410 f1.SetParameter(3, 1.8);
411
412 // options : N do not store the function, do not draw
413 // I use integral of function in bin rather than value at bin center
414 // R use the range specified in the function range
415 // Q quiet mode
416// fHistWidth.Fit(&f1, "QRO");
417 fHistWidth.Fit(&f1, "QRN");
418
419 chi = f1.GetChisquare()/f1.GetNDF();
420
421 Double_t err;
422 gMinuit->GetParameter(2, width, err); // get the sigma value
423
424 return kTRUE;
425}
426
427/*
428// --------------------------------------------------------------------------
429//
430// An impact parameter is calculated by fitting the histogram of photon
431// distribution along the circle with a theoritical model.
432// (See G. Vacanti et. al., Astroparticle Physics 2, 1994, 1-11.
433// The function (6) is used here.)
434//
435// By default this calculation is suppressed because this calculation is
436// very time consuming. If you want to calculate an impact parameter,
437// you can call the function of EnableImpactCalc().
438//
439void MMuonCalibParCalc::CalcImpact(Int_t effbinnum, Float_t startfitval, Float_t endfitval)
440{
441 // Fit the distribution with Vacanti function. The function is different
442 // for the impact parameter of inside or outside of our reflector.
443 // Then two different functions are applied to the photon distribution,
444 // and the one which give us smaller chisquare value is taken as a
445 // proper one.
446
447 Double_t val1,err1,val2,err2;
448 // impact parameter inside mirror radius (8.5m)
449 TString func1;
450 Float_t tmpval = (*fMuonSearchPar).GetRadius()*(*fGeomCam).GetConvMm2Deg()*TMath::DegToRad();
451 tmpval = sin(2.*tmpval)*8.5;
452 func1 += "[0]*";
453 func1 += tmpval;
454 func1 += "*(sqrt(1.-([1]/8.5)**2*sin((x-[2])*3.1415926/180.)**2)+([1]/8.5)*cos((x-[2])*3.1415926/180.))";
455
456 TF1 f1("f1",func1,startfitval,endfitval);
457 f1.SetParameters(2000,3,0);
458 f1.SetParLimits(1,0,8.5);
459 f1.SetParLimits(2,-180.,180.);
460
461 fMuonCalibPar->fHistPhi->Fit("f1","RQEM");
462
463 Float_t chi1 = -1;
464 Float_t chi2 = -1;
465 if(effbinnum>3)
466 chi1 = f1.GetChisquare()/((Float_t)(effbinnum-3));
467
468 gMinuit->GetParameter(1,val1,err1); // get the estimated IP
469 Float_t estip1 = val1;
470
471 // impact parameter beyond mirror area (8.5m)
472 TString func2;
473 Float_t tmpval2 = (*fMuonSearchPar).GetRadius()*(*fGeomCam).GetConvMm2Deg()*TMath::DegToRad();
474 tmpval2 = sin(2.*tmpval2)*8.5*2.;
475 func2 += "[0]*";
476 func2 += tmpval2;
477 func2 += "*sqrt(1.-(([1]/8.5)*sin((x-[2])*3.1415926/180.))**2)";
478
479 TF1 f2("f2",func2,startfitval,endfitval);
480 f2.SetParameters(2000,20,0);
481 f2.SetParLimits(1,8.5,300.);
482 f2.SetParLimits(2,-180.,180.);
483
484 fMuonCalibPar->fHistPhi->Fit("f2","RQEM+");
485
486 if(effbinnum>3)
487 chi2 = f2.GetChisquare()/((Float_t)(effbinnum-3));
488
489 gMinuit->GetParameter(1,val2,err2); // get the estimated IP
490 Float_t estip2 = val2;
491
492 if(effbinnum<=3)
493 {
494 fMuonCalibPar->SetEstImpact(-1.);
495 }
496 if(chi2 > chi1)
497 {
498 fMuonCalibPar->SetEstImpact(estip1);
499 fMuonCalibPar->SetChiArcPhi(chi1);
500 }
501 else
502 {
503 fMuonCalibPar->SetEstImpact(estip2);
504 fMuonCalibPar->SetChiArcPhi(chi2);
505 }
506}
507*/
508
509Float_t MHSingleMuon::CalcSize() const
510{
511 const Int_t n = fHistPhi.GetNbinsX();
512
513 Double_t sz=0;
514 for (Int_t i=1; i<=n; i++)
515 sz += fHistPhi.GetBinContent(i)*fHistPhi.GetBinEntries(i);
516
517 return sz;
518}
519
520void MHSingleMuon::Paint(Option_t *o)
521{
522 TF1 *f = fHistWidth.GetFunction("f1");
523 if (f)
524 f->ResetBit(1<<9);
525}
526
527void MHSingleMuon::Draw(Option_t *o)
528{
529 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
530 pad->SetBorderMode(0);
531
532 AppendPad("");
533
534 pad->Divide(1,2);
535
536 pad->cd(1);
537 gPad->SetBorderMode(0);
538 fHistPhi.Draw();
539
540 pad->cd(2);
541 gPad->SetBorderMode(0);
542 fHistWidth.Draw();
543}
Note: See TracBrowser for help on using the repository browser.