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

Last change on this file since 7365 was 7365, checked in by meyer, 20 years ago
*** empty log message ***
File size: 16.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////////////////////////////////////////////////////////////////////////////
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("number of pixels");
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(100, -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 (dist < fMuonSearchPar->GetRadius() + fMargin &&
216 dist > fMuonSearchPar->GetRadius() - fMargin)
217 {
218 fHistTime.Fill(pix.GetArrivalTime()-fMuonSearchPar->GetTime());
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.3);
233 g1.SetParLimits(1, -0.5, 0.5);
234 g1.SetParLimits(2, 0, 1);
235 // options : N do not store the function, do not draw
236 // I use integral of function in bin rather than value at bin center
237 // R use the range specified in the function range
238 // Q quiet mode
239 fHistTime.Fit(&g1, "QNRB");
240
241 // Double_t err;
242 Double_t mean, meanerr, sig, sigerr;
243 gMinuit->GetParameter(2, sig, sigerr); // get the sigma value
244 gMinuit->GetParameter(1, mean, meanerr); // get the sigma value
245
246 for (Int_t i=0; i<entries; i++)
247 {
248 const MSignalPix &pix = (*fSignalCam)[i];
249 const MGeomPix &gpix = (*fGeomCam)[i];
250
251 const Float_t dx = gpix.GetX() - cenx;
252 const Float_t dy = gpix.GetY() - ceny;
253
254 const Float_t dist = TMath::Hypot(dx, dy);
255
256 // if the signal is not near the estimated circle, it is ignored.
257 if (dist < fMuonSearchPar->GetRadius() + fMargin &&
258 dist > fMuonSearchPar->GetRadius() - fMargin)
259 {
260 if(TMath::Abs(pix.GetArrivalTime()-(fMuonSearchPar->GetTime()+mean))<2*sig)
261 fHistPhi.Fill(TMath::ATan2(dx, dy)*TMath::RadToDeg(), pix.GetNumPhotons());
262 }
263 }
264 // error estimation (temporaly)
265 // The error is estimated from the signal. In order to do so, we have to
266 // once convert the signal from ADC to photo-electron. Then we can get
267 // the fluctuation such as F-factor*sqrt(phe).
268 // Up to now, the error of pedestal is not taken into accout. This is not
269 // of course correct. We will include this soon.
270 const Double_t Ffactor = 1.4;
271 for (Int_t i=0; i<fHistPhi.GetNbinsX()+1; i++)
272 {
273 const Float_t abs = TMath::Abs(fHistPhi.GetBinContent(i));
274 const Float_t rougherr = TMath::Sqrt(abs)*Ffactor;
275
276 fHistPhi.SetBinError(i, rougherr);
277 }
278
279 for (Int_t i=0; i<fHistWidth.GetNbinsX()+1; i++)
280 {
281 const Float_t abs = TMath::Abs(fHistWidth.GetBinContent(i));
282 const Float_t rougherr = TMath::Sqrt(abs)*Ffactor;
283
284 fHistWidth.SetBinError(i, rougherr);
285 }
286
287 return kTRUE;
288}
289
290// --------------------------------------------------------------------------
291//
292// Find the first bins starting at the bin with maximum content in both
293// directions which are below threshold.
294// If in a range of half the histogram size in both directions no bin
295// below the threshold is found, kFALSE is returned.
296//
297Bool_t MHSingleMuon::FindRangeAboveThreshold(const TProfile &h, Float_t thres, Int_t &first, Int_t &last) const
298{
299 const Int_t n = h.GetNbinsX();
300 const Int_t maxbin = h.GetMaximumBin();
301 const Int_t edge = maxbin+n/2;
302
303 // Search from the peak to the right
304 last = -1;
305 for (Int_t i=maxbin; i<edge; i++)
306 {
307 const Float_t val = h.GetBinContent(i%n + 1);
308 if (val<thres)
309 {
310 last = i%n+1;
311 break;
312 }
313 }
314
315 // Search from the peak to the left
316 first = -1;
317 for (Int_t i=maxbin+n-1; i>=edge; i--)
318 {
319 const Float_t val = h.GetBinContent(i%n + 1);
320 if (val<thres)
321 {
322 first = i%n+1;
323 break;
324 }
325 }
326
327 return first>=0 && last>=0;
328}
329
330// --------------------------------------------------------------------------
331//
332// Photon distribution along the estimated circle is fitted with theoritical
333// function in order to get some more information such as Arc Phi and Arc
334// Length.
335//
336Bool_t MHSingleMuon::CalcPhi(Double_t thres, Double_t &peakphi, Double_t &arcphi) const
337{
338 if (fHistPhi.GetMaximum()<thres)
339 return kFALSE;
340
341 peakphi = 180.-fHistPhi.GetBinCenter(fHistPhi.GetMaximumBin());
342
343 // Now find the position at which the peak edges crosses the threshold
344 Int_t first, last;
345
346 FindRangeAboveThreshold(fHistPhi, thres, first, last);
347
348 const Int_t n = fHistPhi.GetNbinsX();
349 const Int_t edge = fHistPhi.GetMaximumBin()+n/2;
350 if (first<0)
351 first = (edge-1)%n+1;
352 if (last<0)
353 last = edge%n+1;;
354
355 const Float_t startfitval = fHistPhi.GetBinLowEdge(first+1);
356 const Float_t endfitval = fHistPhi.GetBinLowEdge(last);
357
358 arcphi = last-1<first ? 360+(endfitval-startfitval) : endfitval-startfitval;
359
360 //if (fEnableImpactCalc)
361 // CalcImpact(effbinnum, startfitval, endfitval);
362
363 return kTRUE;
364}
365
366// --------------------------------------------------------------------------
367//
368// Photon distribution of distance from the center of estimated ring is
369// fitted in order to get some more information such as ARC WIDTH which
370// can represent to the PSF of our reflector.
371//
372// thres: Threshold above zero to determin the edges of the peak which
373// is used as fit range
374// width: ArcWidth returned in deg
375// chi: Chi^2/NDF of the fit
376//
377Bool_t MHSingleMuon::CalcWidth(Double_t thres, Double_t &width, Double_t &chi)
378{
379 Int_t first, last;
380
381 if (!FindRangeAboveThreshold(fHistWidth, thres, first, last))
382 return kFALSE;
383
384 // This happens in some cases
385 const Int_t n = fHistWidth.GetNbinsX()/2;
386 const Int_t m = fHistWidth.GetMaximumBin();
387 if (first>last)
388 if (m>n) // If maximum is on the right side of histogram
389 last = n;
390 else
391 first = 0; // If maximum is on the left side of histogram
392
393 if (last-first<=3)
394 return kFALSE;
395
396 // Now get the fit range
397 const Float_t startfitval = fHistWidth.GetBinLowEdge(first+1);
398 const Float_t endfitval = fHistWidth.GetBinLowEdge(last);
399
400 // Setup the function and perform the fit
401 TF1 f1("f1", "gaus + [3]", startfitval, endfitval);
402 f1.SetLineColor(kBlue);
403
404 // Choose starting values as accurate as possible
405 f1.SetParameter(0, fHistWidth.GetMaximum());
406 f1.SetParameter(1, fHistWidth.GetBinCenter(m));
407// f1.SetParameter(2, (endfitval-startfitval)/2);
408 f1.SetParameter(2, 0.1);
409 f1.SetParameter(3, 1.8);
410
411 // options : N do not store the function, do not draw
412 // I use integral of function in bin rather than value at bin center
413 // R use the range specified in the function range
414 // Q quiet mode
415// fHistWidth.Fit(&f1, "QRO");
416 fHistWidth.Fit(&f1, "QRN");
417
418 chi = f1.GetChisquare()/f1.GetNDF();
419
420 Double_t err;
421 gMinuit->GetParameter(2, width, err); // get the sigma value
422
423 return kTRUE;
424}
425
426/*
427// --------------------------------------------------------------------------
428//
429// An impact parameter is calculated by fitting the histogram of photon
430// distribution along the circle with a theoritical model.
431// (See G. Vacanti et. al., Astroparticle Physics 2, 1994, 1-11.
432// The function (6) is used here.)
433//
434// By default this calculation is suppressed because this calculation is
435// very time consuming. If you want to calculate an impact parameter,
436// you can call the function of EnableImpactCalc().
437//
438void MMuonCalibParCalc::CalcImpact(Int_t effbinnum, Float_t startfitval, Float_t endfitval)
439{
440 // Fit the distribution with Vacanti function. The function is different
441 // for the impact parameter of inside or outside of our reflector.
442 // Then two different functions are applied to the photon distribution,
443 // and the one which give us smaller chisquare value is taken as a
444 // proper one.
445
446 Double_t val1,err1,val2,err2;
447 // impact parameter inside mirror radius (8.5m)
448 TString func1;
449 Float_t tmpval = (*fMuonSearchPar).GetRadius()*(*fGeomCam).GetConvMm2Deg()*TMath::DegToRad();
450 tmpval = sin(2.*tmpval)*8.5;
451 func1 += "[0]*";
452 func1 += tmpval;
453 func1 += "*(sqrt(1.-([1]/8.5)**2*sin((x-[2])*3.1415926/180.)**2)+([1]/8.5)*cos((x-[2])*3.1415926/180.))";
454
455 TF1 f1("f1",func1,startfitval,endfitval);
456 f1.SetParameters(2000,3,0);
457 f1.SetParLimits(1,0,8.5);
458 f1.SetParLimits(2,-180.,180.);
459
460 fMuonCalibPar->fHistPhi->Fit("f1","RQEM");
461
462 Float_t chi1 = -1;
463 Float_t chi2 = -1;
464 if(effbinnum>3)
465 chi1 = f1.GetChisquare()/((Float_t)(effbinnum-3));
466
467 gMinuit->GetParameter(1,val1,err1); // get the estimated IP
468 Float_t estip1 = val1;
469
470 // impact parameter beyond mirror area (8.5m)
471 TString func2;
472 Float_t tmpval2 = (*fMuonSearchPar).GetRadius()*(*fGeomCam).GetConvMm2Deg()*TMath::DegToRad();
473 tmpval2 = sin(2.*tmpval2)*8.5*2.;
474 func2 += "[0]*";
475 func2 += tmpval2;
476 func2 += "*sqrt(1.-(([1]/8.5)*sin((x-[2])*3.1415926/180.))**2)";
477
478 TF1 f2("f2",func2,startfitval,endfitval);
479 f2.SetParameters(2000,20,0);
480 f2.SetParLimits(1,8.5,300.);
481 f2.SetParLimits(2,-180.,180.);
482
483 fMuonCalibPar->fHistPhi->Fit("f2","RQEM+");
484
485 if(effbinnum>3)
486 chi2 = f2.GetChisquare()/((Float_t)(effbinnum-3));
487
488 gMinuit->GetParameter(1,val2,err2); // get the estimated IP
489 Float_t estip2 = val2;
490
491 if(effbinnum<=3)
492 {
493 fMuonCalibPar->SetEstImpact(-1.);
494 }
495 if(chi2 > chi1)
496 {
497 fMuonCalibPar->SetEstImpact(estip1);
498 fMuonCalibPar->SetChiArcPhi(chi1);
499 }
500 else
501 {
502 fMuonCalibPar->SetEstImpact(estip2);
503 fMuonCalibPar->SetChiArcPhi(chi2);
504 }
505}
506*/
507
508void MHSingleMuon::Paint(Option_t *o)
509{
510 TF1 *f = fHistWidth.GetFunction("f1");
511 if (f)
512 f->ResetBit(1<<9);
513}
514
515void MHSingleMuon::Draw(Option_t *o)
516{
517 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
518 pad->SetBorderMode(0);
519
520 AppendPad("");
521
522 pad->Divide(1,2);
523
524 pad->cd(1);
525 gPad->SetBorderMode(0);
526 fHistPhi.Draw();
527
528 pad->cd(2);
529 gPad->SetBorderMode(0);
530 fHistWidth.Draw();
531}
Note: See TracBrowser for help on using the repository browser.