source: trunk/MagicSoft/Mars/mhist/MHFalseSource.cc@ 4723

Last change on this file since 4723 was 4704, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 33.3 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, 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MHFalseSource
28//
29// Create a false source plot. For the Binning in x,y the object MBinning
30// "BinningFalseSource" is taken from the paremeter list.
31//
32// The binning in alpha is currently fixed as 18bins from 0 to 90deg.
33//
34// If MTime, MObservatory and MPointingPos is available in the paremeter
35// list each image is derotated.
36//
37// MHFalseSource fills a 3D histogram with alpha distribution for
38// each (derotated) x and y position.
39//
40// Only a radius of 1.2deg around the camera center is taken into account.
41//
42// The displayed histogram is the projection of alpha<fAlphaCut into
43// the x,y plain and the projection of alpha>90-fAlphaCut
44//
45// By using the context menu (find it in a small region between the single
46// pads) you can change the AlphaCut 'online'
47//
48// Each Z-Projection (Alpha-histogram) is scaled such, that the number
49// of entries fits the maximum number of entries in all Z-Projections.
50// This should correct for the different acceptance in the center
51// and at the vorder of the camera. This, however, produces more noise
52// at the border.
53//
54// Here is a slightly simplified version of the algorithm:
55// ------------------------------------------------------
56// MHillas hil; // Taken as second argument in MFillH
57//
58// MSrcPosCam src;
59// MHillasSrc hsrc;
60// hsrc.SetSrcPos(&src);
61//
62// for (int ix=0; ix<nx; ix++)
63// for (int iy=0; iy<ny; iy++)
64// {
65// TVector2 v(cx[ix], cy[iy]); //cx center of x-bin ix
66// if (rho!=0) //cy center of y-bin iy
67// v=v.Rotate(rho); //image rotation angle
68//
69// src.SetXY(v); //source position in the camera
70//
71// if (!hsrc.Calc(hil)) //calc source dependant hillas
72// return;
73//
74// //fill absolute alpha into histogram
75// const Double_t alpha = hsrc.GetAlpha();
76// fHist.Fill(cx[ix], cy[iy], TMath::Abs(alpha), w);
77// }
78// }
79//
80// Use MHFalse Source like this:
81// -----------------------------
82// MFillH fill("MHFalseSource", "MHillas");
83// or
84// MHFalseSource hist;
85// hist.SetAlphaCut(12.5); // The default value
86// hist.SetBgmean(55); // The default value
87// MFillH fill(&hist, "MHillas");
88//
89// To be implemented:
90// ------------------
91// - a switch to use alpha or |alpha|
92// - taking the binning for alpha from the parlist (binning is
93// currently fixed)
94// - a possibility to change the fit-function (eg using a different TF1)
95// - a possibility to change the fit algorithm (eg which paremeters
96// are fixed at which time)
97// - use a different varaible than alpha
98// - a possiblity to change the algorithm which is used to calculate
99// alpha (or another parameter) is missing (this could be done using
100// a tasklist somewhere...)
101// - a more clever (and faster) algorithm to fill the histogram, eg by
102// calculating alpha once and fill the two coils around the mean
103// - more drawing options...
104// - Move Significance() to a more 'general' place and implement
105// also different algorithms like (Li/Ma)
106// - implement fit for best alpha distribution -- online (Paint)
107// - currently a constant pointing position is assumed in Fill()
108// - the center of rotation need not to be the camera center
109// - ERRORS on each alpha bin to estimate the chi^2 correctly!
110// (sqrt(N)/binwidth) needed for WOlfgangs proposed caluclation
111// of alpha(Li/Ma)
112//
113//////////////////////////////////////////////////////////////////////////////
114#include "MHFalseSource.h"
115
116#include <TF1.h>
117#include <TH2.h>
118#include <TGraph.h>
119#include <TStyle.h>
120#include <TCanvas.h>
121#include <TPaveText.h>
122#include <TStopwatch.h>
123
124#include "MGeomCam.h"
125#include "MSrcPosCam.h"
126#include "MHillasSrc.h"
127#include "MTime.h"
128#include "MObservatory.h"
129#include "MPointingPos.h"
130#include "MAstroCatalog.h"
131#include "MAstroSky2Local.h"
132#include "MStatusDisplay.h"
133#include "MMath.h"
134
135#include "MBinning.h"
136#include "MParList.h"
137
138#include "MLog.h"
139#include "MLogManip.h"
140
141ClassImp(MHFalseSource);
142
143using namespace std;
144
145// --------------------------------------------------------------------------
146//
147// Default Constructor
148//
149MHFalseSource::MHFalseSource(const char *name, const char *title)
150 : fTime(0), fPointPos(0), fObservatory(0), fMm2Deg(-1), fAlphaCut(12.5),
151 fBgMean(55), fMinDist(-1), fMaxDist(-1), fMinLD(-1), fMaxLD(-1)
152{
153 //
154 // set the name and title of this object
155 //
156 fName = name ? name : "MHFalseSource";
157 fTitle = title ? title : "3D-plot of Alpha vs x, y";
158
159 fHist.SetDirectory(NULL);
160
161 fHist.SetName("Alpha");
162 fHist.SetTitle("3D-plot of Alpha vs x, y");
163 fHist.SetXTitle("x [\\circ]");
164 fHist.SetYTitle("y [\\circ]");
165 fHist.SetZTitle("\\alpha [\\circ]");
166}
167
168void MHFalseSource::MakeSymmetric(TH1 *h)
169{
170 const Float_t min = TMath::Abs(h->GetMinimum());
171 const Float_t max = TMath::Abs(h->GetMaximum());
172
173 const Float_t absmax = TMath::Max(min, max)*1.002;
174
175 h->SetMaximum( absmax);
176 h->SetMinimum(-absmax);
177}
178
179// --------------------------------------------------------------------------
180//
181// Set the alpha cut (|alpha|<fAlphaCut) which is use to estimate the
182// number of excess events
183//
184void MHFalseSource::SetAlphaCut(Float_t alpha)
185{
186 if (alpha<0)
187 *fLog << warn << "Alpha<0... taking absolute value." << endl;
188
189 fAlphaCut = TMath::Abs(alpha);
190
191 Modified();
192}
193
194// --------------------------------------------------------------------------
195//
196// Set mean alpha around which the off sample is determined
197// (fBgMean-fAlphaCut/2<|fAlpha|<fBgMean+fAlphaCut/2) which is use
198// to estimate the number of off events
199//
200void MHFalseSource::SetBgMean(Float_t alpha)
201{
202 if (alpha<0)
203 *fLog << warn << "Alpha<0... taking absolute value." << endl;
204
205 fBgMean = TMath::Abs(alpha);
206
207 Modified();
208}
209
210// --------------------------------------------------------------------------
211//
212// Calculate Significance as
213// significance = (s-b)/sqrt(s+k*k*b) mit k=s/b
214//
215// s: total number of events in signal region
216// b: number of background events in signal region
217//
218Double_t MHFalseSource::Significance(Double_t s, Double_t b)
219{
220 return MMath::SignificanceSym(s, b);
221}
222
223// --------------------------------------------------------------------------
224//
225// calculates the significance according to Li & Ma
226// ApJ 272 (1983) 317, Formula 17
227//
228// s // s: number of on events
229// b // b: number of off events
230// alpha = t_on/t_off; // t: observation time
231//
232Double_t MHFalseSource::SignificanceLiMa(Double_t s, Double_t b, Double_t alpha)
233{
234 return MMath::SignificanceLiMaSigned(s, b);
235}
236
237// --------------------------------------------------------------------------
238//
239// Set binnings (takes BinningFalseSource) and prepare filling of the
240// histogram.
241//
242// Also search for MTime, MObservatory and MPointingPos
243//
244Bool_t MHFalseSource::SetupFill(const MParList *plist)
245{
246 const MGeomCam *geom = (MGeomCam*)plist->FindObject("MGeomCam");
247 if (!geom)
248 {
249 *fLog << err << "MGeomCam not found... aborting." << endl;
250 return kFALSE;
251 }
252
253 fMm2Deg = geom->GetConvMm2Deg();
254
255 MBinning binsa;
256 binsa.SetEdges(18, 0, 90);
257
258 const MBinning *bins = (MBinning*)plist->FindObject("BinningFalseSource");
259 if (!bins)
260 {
261 const Float_t r = (geom ? geom->GetMaxRadius()/3 : 200)*fMm2Deg;
262
263 MBinning b;
264 b.SetEdges(20, -r, r);
265 SetBinning(&fHist, &b, &b, &binsa);
266 }
267 else
268 SetBinning(&fHist, bins, bins, &binsa);
269
270 fPointPos = (MPointingPos*)plist->FindObject(AddSerialNumber("MPointingPos"));
271 if (!fPointPos)
272 *fLog << warn << "MPointingPos not found... no derotation." << endl;
273
274 fTime = (MTime*)plist->FindObject(AddSerialNumber("MTime"));
275 if (!fTime)
276 *fLog << warn << "MTime not found... no derotation." << endl;
277
278 fObservatory = (MObservatory*)plist->FindObject(AddSerialNumber("MObservatory"));
279 if (!fObservatory)
280 *fLog << warn << "MObservatory not found... no derotation." << endl;
281
282 // FIXME: Because the pointing position could change we must check
283 // for the current pointing position and add a offset in the
284 // Fill function!
285 fRa = fPointPos->GetRa();
286 fDec = fPointPos->GetDec();
287
288 return kTRUE;
289}
290
291// --------------------------------------------------------------------------
292//
293// Fill the histogram. For details see the code or the class description
294//
295Bool_t MHFalseSource::Fill(const MParContainer *par, const Stat_t w)
296{
297 const MHillas *hil = dynamic_cast<const MHillas*>(par);
298 if (!hil)
299 {
300 *fLog << err << "MHFalseSource::Fill: No container specified!" << endl;
301 return kFALSE;
302 }
303
304 // Get max radius...
305 const Double_t maxr = 0.98*TMath::Abs(fHist.GetBinCenter(1));
306
307 // Get camera rotation angle
308 Double_t rho = 0;
309 if (fTime && fObservatory && fPointPos)
310 rho = fPointPos->RotationAngle(*fObservatory, *fTime);
311 //if (fPointPos)
312 // rho = fPointPos->RotationAngle(*fObservatory);
313
314 // Create necessary containers for calculation
315 MSrcPosCam src;
316 MHillasSrc hsrc;
317 hsrc.SetSrcPos(&src);
318
319 // Get number of bins and bin-centers
320 const Int_t nx = fHist.GetNbinsX();
321 const Int_t ny = fHist.GetNbinsY();
322 Axis_t cx[nx];
323 Axis_t cy[ny];
324 fHist.GetXaxis()->GetCenter(cx);
325 fHist.GetYaxis()->GetCenter(cy);
326
327 for (int ix=0; ix<nx; ix++)
328 {
329 for (int iy=0; iy<ny; iy++)
330 {
331 // check distance... to get a circle plot
332 if (TMath::Hypot(cx[ix], cy[iy])>maxr)
333 continue;
334
335 // rotate center of bin
336 // precalculation of sin/cos doesn't accelerate
337 TVector2 v(cx[ix], cy[iy]);
338 if (rho!=0)
339 v=v.Rotate(rho);
340
341 // convert degrees to millimeters
342 v *= 1./fMm2Deg;
343 src.SetXY(v);
344
345 // Source dependant hillas parameters
346 if (!hsrc.Calc(*hil))
347 {
348 *fLog << warn << "Calculation of MHillasSrc failed for x=" << cx[ix] << " y=" << cy[iy] << endl;
349 return kFALSE;
350 }
351
352 // FIXME: This should be replaced by an external MFilter
353 // and/or MTaskList
354 // Source dependant distance cut
355 if (fMinDist>0 && hsrc.GetDist()*fMm2Deg<fMinDist)
356 continue;
357 if (fMaxDist>0 && hsrc.GetDist()*fMm2Deg>fMaxDist)
358 continue;
359
360 if (fMaxLD>0 && hil->GetLength()>fMaxLD*hsrc.GetDist())
361 continue;
362 if (fMinLD>0 && hil->GetLength()<fMinLD*hsrc.GetDist())
363 continue;
364
365 // Fill histogram
366 const Double_t alpha = hsrc.GetAlpha();
367 fHist.Fill(cx[ix], cy[iy], TMath::Abs(alpha), w);
368 }
369 }
370
371 return kTRUE;
372}
373
374// --------------------------------------------------------------------------
375//
376// Create projection for off data, taking sum of bin contents of
377// range (fBgMean-fAlphaCut/2, fBgMean+fAlphaCut) Making sure to take
378// the same number of bins than for on-data
379//
380void MHFalseSource::ProjectOff(TH2D *h2, TH2D *all)
381{
382 TAxis &axe = *fHist.GetZaxis();
383
384 // Find range to cut (left edge and width)
385 const Int_t f = axe.FindBin(fBgMean-fAlphaCut/2);
386 const Int_t l = axe.FindBin(fAlphaCut)+f-1;
387
388 axe.SetRange(f, l);
389 const Float_t cut1 = axe.GetBinLowEdge(f);
390 const Float_t cut2 = axe.GetBinUpEdge(l);
391 h2->SetTitle(Form("Distribution of %.1f\\circ<|\\alpha|<%.1f\\circ in x,y", cut1, cut2));
392
393 // Get projection for range
394 TH2D *p = (TH2D*)fHist.Project3D("yx_off");
395
396 // Reset range
397 axe.SetRange(0,9999);
398
399 // Move contents from projection to h2
400 h2->Reset();
401 h2->Add(p, all->GetMaximum());
402 h2->Divide(all);
403
404 // Delete p
405 delete p;
406
407 // Set Minimum as minimum value Greater Than 0
408 h2->SetMinimum(GetMinimumGT(*h2));
409}
410
411// --------------------------------------------------------------------------
412//
413// Create projection for on data, taking sum of bin contents of
414// range (0, fAlphaCut)
415//
416void MHFalseSource::ProjectOn(TH2D *h3, TH2D *all)
417{
418 TAxis &axe = *fHist.GetZaxis();
419
420 // Find range to cut
421 axe.SetRangeUser(0, fAlphaCut);
422 const Float_t cut = axe.GetBinUpEdge(axe.GetLast());
423 h3->SetTitle(Form("Distribution of |\\alpha|<%.1f\\circ in x,y", cut));
424
425 // Get projection for range
426 TH2D *p = (TH2D*)fHist.Project3D("yx_on");
427
428 // Reset range
429 axe.SetRange(0,9999);
430
431 // Move contents from projection to h3
432 h3->Reset();
433 h3->Add(p, all->GetMaximum());
434 h3->Divide(all);
435
436 // Delete p
437 delete p;
438
439 // Set Minimum as minimum value Greater Than 0
440 h3->SetMinimum(GetMinimumGT(*h3));
441}
442
443// --------------------------------------------------------------------------
444//
445// Create projection for all data, taking sum of bin contents of
446// range (0, 90) - corresponding to the number of entries in this slice.
447//
448void MHFalseSource::ProjectAll(TH2D *h3)
449{
450 h3->SetTitle("Number of entries");
451
452 // Get projection for range
453 TH2D *p = (TH2D*)fHist.Project3D("yx_all");
454
455 // Move contents from projection to h3
456 h3->Reset();
457 h3->Add(p);
458 delete p;
459
460 // Set Minimum as minimum value Greater Than 0
461 h3->SetMinimum(GetMinimumGT(*h3));
462}
463
464// --------------------------------------------------------------------------
465//
466// Update the projections and paint them
467//
468void MHFalseSource::Paint(Option_t *opt)
469{
470 // Set pretty color palette
471 gStyle->SetPalette(1, 0);
472
473 TVirtualPad *padsave = gPad;
474
475 TH1D* h1;
476 TH2D* h0;
477 TH2D* h2;
478 TH2D* h3;
479 TH2D* h4;
480 TH2D* h5;
481
482 // Update projection of all-events
483 padsave->GetPad(2)->cd(3);
484 if ((h0 = (TH2D*)gPad->FindObject("Alpha_yx_all")))
485 ProjectAll(h0);
486
487 // Update projection of on-events
488 padsave->GetPad(1)->cd(1);
489 if ((h3 = (TH2D*)gPad->FindObject("Alpha_yx_on")))
490 ProjectOn(h3, h0);
491
492 // Update projection of off-events
493 padsave->GetPad(1)->cd(3);
494 if ((h2 = (TH2D*)gPad->FindObject("Alpha_yx_off")))
495 ProjectOff(h2, h0);
496
497 padsave->GetPad(2)->cd(2);
498 if ((h5 = (TH2D*)gPad->FindObject("Alpha_yx_diff")))
499 {
500 h5->Add(h3, h2, -1);
501 MakeSymmetric(h5);
502 }
503
504 // Update projection of significance
505 padsave->GetPad(1)->cd(2);
506 if (h2 && h3 && (h4 = (TH2D*)gPad->FindObject("Alpha_yx_sig")))
507 {
508 const Int_t nx = h4->GetXaxis()->GetNbins();
509 const Int_t ny = h4->GetYaxis()->GetNbins();
510 //const Int_t nr = nx*nx + ny*ny;
511
512 Int_t maxx=nx/2;
513 Int_t maxy=ny/2;
514
515 Int_t max = h4->GetBin(nx, ny);
516
517 for (int ix=1; ix<=nx; ix++)
518 for (int iy=1; iy<=ny; iy++)
519 {
520 const Int_t n = h4->GetBin(ix, iy);
521
522 const Double_t s = h3->GetBinContent(n);
523 const Double_t b = h2->GetBinContent(n);
524
525 const Double_t sig = SignificanceLiMa(s, b);
526
527 h4->SetBinContent(n, sig);
528
529 if (sig>h4->GetBinContent(max) && sig>0/* && (ix-nx/2)*(ix-nx/2)+(iy-ny/2)*(iy-ny/2)<nr*nr/9*/)
530 {
531 max = n;
532 maxx=ix;
533 maxy=iy;
534 }
535 }
536
537 MakeSymmetric(h4);
538
539 // Update projection of 'the best alpha-plot'
540 padsave->GetPad(2)->cd(1);
541 if ((h1 = (TH1D*)gPad->FindObject("Alpha")) && max>0)
542 {
543 const Double_t x = h4->GetXaxis()->GetBinCenter(maxx);
544 const Double_t y = h4->GetYaxis()->GetBinCenter(maxy);
545 const Double_t s = h4->GetBinContent(max);
546
547 // This is because a 3D histogram x and y are vice versa
548 // Than for their projections
549 TH1 *h = fHist.ProjectionZ("Alpha", maxx, maxx, maxy, maxy);
550 h->SetTitle(Form("Distribution of \\alpha for x=%.2f y=%.2f (\\sigma_{max}=%.1f)", x, y, s));
551 }
552 }
553
554 gPad = padsave;
555}
556
557// --------------------------------------------------------------------------
558//
559// Get the MAstroCatalog corresponding to fRa, fDec. The limiting magnitude
560// is set to 9, while the fov is equal to the current fov of the false
561// source plot.
562//
563TObject *MHFalseSource::GetCatalog()
564{
565 const Double_t maxr = 0.98*TMath::Abs(fHist.GetBinCenter(1));
566
567 // Create catalog...
568 MAstroCatalog *stars = new MAstroCatalog;
569 stars->SetLimMag(9);
570 stars->SetGuiActive(kFALSE);
571 stars->SetRadiusFOV(maxr);
572 stars->SetRaDec(fRa*TMath::DegToRad()*15, fDec*TMath::DegToRad());
573 stars->ReadBSC("bsc5.dat");
574
575 *fLog << err << "FIXME - The catalog will never be deleted, because this crashes!" << endl;
576
577 stars->SetBit(kCanDelete);
578
579 return stars;
580}
581
582// --------------------------------------------------------------------------
583//
584// Draw the histogram
585//
586void MHFalseSource::Draw(Option_t *opt)
587{
588 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
589 pad->SetBorderMode(0);
590
591 AppendPad("");
592
593 pad->Divide(1, 2, 0, 0.03);
594
595 *fLog << err << "FIXME - Plotting the catalog is broken!" << endl;
596
597 TObject *catalog = GetCatalog();
598
599 // Initialize upper part
600 pad->cd(1);
601 gPad->SetBorderMode(0);
602 gPad->Divide(3, 1);
603
604 // PAD #1
605 pad->GetPad(1)->cd(1);
606 gPad->SetBorderMode(0);
607 fHist.GetZaxis()->SetRangeUser(0,fAlphaCut);
608 TH1 *h3 = fHist.Project3D("yx_on");
609 fHist.GetZaxis()->SetRange(0,9999);
610 h3->SetDirectory(NULL);
611 h3->SetXTitle(fHist.GetXaxis()->GetTitle());
612 h3->SetYTitle(fHist.GetYaxis()->GetTitle());
613 h3->Draw("colz");
614 h3->SetBit(kCanDelete);
615 catalog->Draw("mirror same *");
616
617 // PAD #2
618 pad->GetPad(1)->cd(2);
619 gPad->SetBorderMode(0);
620 fHist.GetZaxis()->SetRange(0,0);
621 TH1 *h4 = fHist.Project3D("yx_sig"); // Do this to get the correct binning....
622 fHist.GetZaxis()->SetRange(0,9999);
623 h4->SetTitle("Significance");
624 h4->SetDirectory(NULL);
625 h4->SetXTitle(fHist.GetXaxis()->GetTitle());
626 h4->SetYTitle(fHist.GetYaxis()->GetTitle());
627 h4->Draw("colz");
628 h4->SetBit(kCanDelete);
629 catalog->Draw("mirror same *");
630
631 // PAD #3
632 pad->GetPad(1)->cd(3);
633 gPad->SetBorderMode(0);
634 fHist.GetZaxis()->SetRangeUser(fBgMean-fAlphaCut/2, fBgMean+fAlphaCut/2);
635 TH1 *h2 = fHist.Project3D("yx_off");
636 h2->SetDirectory(NULL);
637 h2->SetXTitle(fHist.GetXaxis()->GetTitle());
638 h2->SetYTitle(fHist.GetYaxis()->GetTitle());
639 h2->Draw("colz");
640 h2->SetBit(kCanDelete);
641 catalog->Draw("mirror same *");
642
643 // Initialize lower part
644 pad->cd(2);
645 gPad->SetBorderMode(0);
646 gPad->Divide(3, 1);
647
648 // PAD #4
649 pad->GetPad(2)->cd(1);
650 gPad->SetBorderMode(0);
651 TH1 *h1 = fHist.ProjectionZ("Alpha");
652 h1->SetDirectory(NULL);
653 h1->SetTitle("Distribution of \\alpha");
654 h1->SetXTitle(fHist.GetZaxis()->GetTitle());
655 h1->SetYTitle("Counts");
656 h1->Draw(opt);
657 h1->SetBit(kCanDelete);
658
659 // PAD #5
660 pad->GetPad(2)->cd(2);
661 gPad->SetBorderMode(0);
662 TH1 *h5 = (TH1*)h3->Clone("Alpha_yx_diff");
663 h5->Add(h2, -1);
664 h5->SetTitle("Difference of on- and off-distribution");
665 h5->SetDirectory(NULL);
666 h5->Draw("colz");
667 h5->SetBit(kCanDelete);
668 catalog->Draw("mirror same *");
669
670 // PAD #6
671 pad->GetPad(2)->cd(3);
672 gPad->SetBorderMode(0);
673 TH1 *h0 = fHist.Project3D("yx_all");
674 h0->SetDirectory(NULL);
675 h0->SetXTitle(fHist.GetXaxis()->GetTitle());
676 h0->SetYTitle(fHist.GetYaxis()->GetTitle());
677 h0->Draw("colz");
678 h0->SetBit(kCanDelete);
679 catalog->Draw("mirror same *");
680}
681
682// --------------------------------------------------------------------------
683//
684// Everything which is in the main pad belongs to this class!
685//
686Int_t MHFalseSource::DistancetoPrimitive(Int_t px, Int_t py)
687{
688 return 0;
689}
690
691// --------------------------------------------------------------------------
692//
693// Set all sub-pads to Modified()
694//
695void MHFalseSource::Modified()
696{
697 if (!gPad)
698 return;
699
700 TVirtualPad *padsave = gPad;
701 padsave->Modified();
702 padsave->GetPad(1)->cd(1);
703 gPad->Modified();
704 padsave->GetPad(1)->cd(3);
705 gPad->Modified();
706 padsave->GetPad(2)->cd(1);
707 gPad->Modified();
708 padsave->GetPad(2)->cd(2);
709 gPad->Modified();
710 padsave->GetPad(2)->cd(3);
711 gPad->Modified();
712 gPad->cd();
713}
714
715// --------------------------------------------------------------------------
716//
717// This is a preliminary implementation of a alpha-fit procedure for
718// all possible source positions. It will be moved into its own
719// more powerfull class soon.
720//
721// The fit function is "gaus(0)+pol2(3)" which is equivalent to:
722// [0]*exp(-0.5*((x-[1])/[2])^2) + [3] + [4]*x + [5]*x^2
723// or
724// A*exp(-0.5*((x-mu)/sigma)^2) + a + b*x + c*x^2
725//
726// Parameter [1] is fixed to 0 while the alpha peak should be
727// symmetric around alpha=0.
728//
729// Parameter [4] is fixed to 0 because the first derivative at
730// alpha=0 should be 0, too.
731//
732// In a first step the background is fitted between bgmin and bgmax,
733// while the parameters [0]=0 and [2]=1 are fixed.
734//
735// In a second step the signal region (alpha<sigmax) is fittet using
736// the whole function with parameters [1], [3], [4] and [5] fixed.
737//
738// The number of excess and background events are calculated as
739// s = int(0, sigint, gaus(0)+pol2(3))
740// b = int(0, sigint, pol2(3))
741//
742// The Significance is calculated using the Significance() member
743// function.
744//
745void MHFalseSource::FitSignificance(Float_t sigint, Float_t sigmax, Float_t bgmin, Float_t bgmax, Byte_t polynom)
746{
747 TObject *catalog = GetCatalog();
748
749 TH1D h0a("A", "", 50, 0, 4000);
750 TH1D h4a("chisq1", "", 50, 0, 35);
751 //TH1D h5a("prob1", "", 50, 0, 1.1);
752 TH1D h6("signifcance", "", 50, -20, 20);
753
754 TH1D h1("mu", "Parameter \\mu", 50, -1, 1);
755 TH1D h2("sigma", "Parameter \\sigma", 50, 0, 90);
756 TH1D h3("b", "Parameter b", 50, -0.1, 0.1);
757
758 TH1D h0b("a", "Parameter a (red), A (blue)", 50, 0, 4000);
759 TH1D h4b("\\chi^{2}", "\\chi^{2} (red, green) / significance (black)", 50, 0, 35);
760 //TH1D h5b("prob", "Fit probability: Bg(red), F(blue)", 50, 0, 1.1);
761
762 h0a.SetLineColor(kBlue);
763 h4a.SetLineColor(kBlue);
764 //h5a.SetLineColor(kBlue);
765 h0b.SetLineColor(kRed);
766 h4b.SetLineColor(kRed);
767 //h5b.SetLineColor(kRed);
768
769 TH1 *hist = fHist.Project3D("yx_fit");
770 hist->SetDirectory(0);
771 TH1 *hists = fHist.Project3D("yx_fit");
772 hists->SetDirectory(0);
773 TH1 *histb = fHist.Project3D("yx_fit");
774 histb->SetDirectory(0);
775 hist->Reset();
776 hists->Reset();
777 histb->Reset();
778 hist->SetNameTitle("Significance",
779 Form("Fit Region: Signal<%.1f\\circ, %.1f\\circ<Bg<%.1f\\circ",
780 sigmax, bgmin, bgmax));
781 hists->SetName("Excess");
782 histb->SetName("Background");
783 hist->SetXTitle(fHist.GetXaxis()->GetTitle());
784 hists->SetXTitle(fHist.GetXaxis()->GetTitle());
785 histb->SetXTitle(fHist.GetXaxis()->GetTitle());
786 hist->SetYTitle(fHist.GetYaxis()->GetTitle());
787 hists->SetYTitle(fHist.GetYaxis()->GetTitle());
788 histb->SetYTitle(fHist.GetYaxis()->GetTitle());
789
790 const Double_t w = fHist.GetZaxis()->GetBinWidth(1);
791
792 // xmin, xmax, npar
793 //TF1 func("MyFunc", fcn, 0, 90, 6);
794 // Implementing the function yourself is only about 5% faster
795 TF1 func("", Form("gaus(0) + pol%d(3)", polynom), 0, 90);
796 //TF1 func("", Form("[0]*(TMath::Gaus(x, [1], [2])+TMath::Gaus(x, -[1], [2]))+pol%d(3)", polynom), 0, 90);
797 TArrayD maxpar(func.GetNpar());
798
799 /* func.SetParName(0, "A");
800 * func.SetParName(1, "mu");
801 * func.SetParName(2, "sigma");
802 */
803
804 func.FixParameter(1, 0);
805 func.FixParameter(4, 0);
806 func.SetParLimits(2, 0, 90);
807 func.SetParLimits(3, -1, 1);
808
809 const Int_t nx = hist->GetXaxis()->GetNbins();
810 const Int_t ny = hist->GetYaxis()->GetNbins();
811 //const Int_t nr = nx*nx+ny*ny;
812
813 Double_t maxalpha0=0;
814 Double_t maxs=3;
815
816 Int_t maxx=0;
817 Int_t maxy=0;
818
819 TStopwatch clk;
820 clk.Start();
821
822 *fLog << inf;
823 *fLog << "Signal fit: alpha < " << sigmax << endl;
824 *fLog << "Integration: alpha < " << sigint << endl;
825 *fLog << "Background fit: " << bgmin << " < alpha < " << bgmax << endl;
826 *fLog << "Polynom order: " << (int)polynom << endl;
827 *fLog << "Fitting False Source Plot..." << flush;
828
829 TH1 *h0 = fHist.Project3D("yx_entries");
830 Float_t entries = h0->GetMaximum();
831 delete h0;
832
833 TH1 *h=0;
834 for (int ix=1; ix<=nx; ix++)
835 for (int iy=1; iy<=ny; iy++)
836 {
837 // This is because a 3D histogram x and y are vice versa
838 // Than for their projections
839 h = fHist.ProjectionZ("AlphaFit", ix, ix, iy, iy);
840
841 const Double_t alpha0 = h->GetBinContent(1);
842
843 // Check for the regios which is not filled...
844 if (alpha0==0)
845 continue;
846
847 h->Scale(entries/h->GetEntries());
848
849 if (alpha0>maxalpha0)
850 maxalpha0=alpha0;
851
852 // First fit a polynom in the off region
853 func.FixParameter(0, 0);
854 func.FixParameter(2, 1);
855 func.ReleaseParameter(3);
856
857 for (int i=5; i<func.GetNpar(); i++)
858 func.ReleaseParameter(i);
859
860 h->Fit(&func, "N0Q", "", bgmin, bgmax);
861
862 h4a.Fill(func.GetChisquare());
863 //h5a.Fill(func.GetProb());
864
865 //func.SetParLimits(0, 0.5*h->GetBinContent(1), 1.5*h->GetBinContent(1));
866 //func.SetParLimits(2, 5, 90);
867
868 func.ReleaseParameter(0);
869 //func.ReleaseParameter(1);
870 func.ReleaseParameter(2);
871 func.FixParameter(3, func.GetParameter(3));
872 for (int i=5; i<func.GetNpar(); i++)
873 func.FixParameter(i, func.GetParameter(i));
874
875 // Do not allow signals smaller than the background
876 const Double_t A = alpha0-func.GetParameter(3);
877 const Double_t dA = TMath::Abs(A);
878 func.SetParLimits(0, -dA*4, dA*4);
879 func.SetParLimits(2, 0, 90);
880
881 // Now fit a gaus in the on region on top of the polynom
882 func.SetParameter(0, A);
883 func.SetParameter(2, sigmax*0.75);
884
885 h->Fit(&func, "N0Q", "", 0, sigmax);
886
887 TArrayD p(func.GetNpar(), func.GetParameters());
888
889 // Fill results into some histograms
890 h0a.Fill(p[0]);
891 h0b.Fill(p[3]);
892 h1.Fill(p[1]);
893 h2.Fill(p[2]);
894 if (polynom>1)
895 h3.Fill(p[5]);
896 h4b.Fill(func.GetChisquare());
897 //h5b.Fill(func.GetProb());
898
899 // Implementing the integral as analytical function
900 // gives the same result in the order of 10e-5
901 // and it is not faster at all...
902 //const Bool_t ok = /*p[0]>=0 && /*p[0]<alpha0*2 &&*/ p[2]>1.75;// && p[2]<88.5;
903 /*
904 if (p[0]<-fabs(alpha0-p[3])*7 && p[2]<alphaw/3)
905 {
906 func.SetParameter(0, alpha0-p[3]);
907 cout << p[2] << " " << p[0] << " " << alpha0-p[3] << endl;
908 }
909 */
910
911 // The fitted function returned units of
912 // counts bin binwidth. To get the correct number
913 // of events we must adapt the functions by dividing
914 // the result of the integration by the bin-width
915 const Double_t s = func.Integral(0, sigint)/w;
916
917 func.SetParameter(0, 0);
918 func.SetParameter(2, 1);
919
920 const Double_t b = func.Integral(0, sigint)/w;
921 const Double_t sig = SignificanceLiMa(s, b);
922
923 const Int_t n = hist->GetBin(ix, iy);
924 hists->SetBinContent(n, s-b);
925 histb->SetBinContent(n, b);
926
927 hist->SetBinContent(n, sig);
928 if (sig!=0)
929 h6.Fill(sig);
930
931 if (sig>maxs/* && (ix-nx/2)*(ix-nx/2)+(iy-ny/2)*(iy-ny/2)<nr*nr/9*/)
932 {
933 maxs = sig;
934 maxx = ix;
935 maxy = iy;
936 maxpar = p;
937 }
938 }
939
940 *fLog << inf << "done." << endl;
941
942 h0a.GetXaxis()->SetRangeUser(0, maxalpha0*1.5);
943 h0b.GetXaxis()->SetRangeUser(0, maxalpha0*1.5);
944
945 hists->SetTitle(Form("Excess events for \\alpha<%.0f\\circ (N_{max}=%d)", sigint, (int)hists->GetMaximum()));
946 histb->SetTitle(Form("Background events for \\alpha<%.0f\\circ", sigint));
947
948 //hists->SetMinimum(GetMinimumGT(*hists));
949 histb->SetMinimum(GetMinimumGT(*histb));
950
951 MakeSymmetric(hists);
952 MakeSymmetric(hist);
953
954 clk.Stop();
955 clk.Print("m");
956
957 TCanvas *c=new TCanvas;
958
959 gStyle->SetPalette(1, 0);
960
961 c->Divide(3,2, 0, 0);
962 c->cd(1);
963 gPad->SetBorderMode(0);
964 hists->Draw("colz");
965 hists->SetBit(kCanDelete);
966 catalog->Draw("mirror same *");
967 c->cd(2);
968 gPad->SetBorderMode(0);
969 hist->Draw("colz");
970 hist->SetBit(kCanDelete);
971 catalog->Draw("mirror same *");
972 c->cd(3);
973 gPad->SetBorderMode(0);
974 histb->Draw("colz");
975 histb->SetBit(kCanDelete);
976 catalog->Draw("mirror same *");
977 c->cd(4);
978 gPad->Divide(1,3, 0, 0);
979 TVirtualPad *p=gPad;
980 p->SetBorderMode(0);
981 p->cd(1);
982 gPad->SetBorderMode(0);
983 h0b.DrawCopy();
984 h0a.DrawCopy("same");
985 p->cd(2);
986 gPad->SetBorderMode(0);
987 h3.DrawCopy();
988 p->cd(3);
989 gPad->SetBorderMode(0);
990 h2.DrawCopy();
991 c->cd(6);
992 gPad->Divide(1,2, 0, 0);
993 TVirtualPad *q=gPad;
994 q->SetBorderMode(0);
995 q->cd(1);
996 gPad->SetBorderMode(0);
997 gPad->SetBorderMode(0);
998 h4b.DrawCopy();
999 h4a.DrawCopy("same");
1000 h6.DrawCopy("same");
1001 q->cd(2);
1002 gPad->SetBorderMode(0);
1003 //h5b.DrawCopy();
1004 //h5a.DrawCopy("same");
1005 c->cd(5);
1006 gPad->SetBorderMode(0);
1007 if (maxx>0 && maxy>0)
1008 {
1009 const char *title = Form(" \\alpha for x=%.2f y=%.2f (\\sigma_{max}=%.1f) ",
1010 hist->GetXaxis()->GetBinCenter(maxx),
1011 hist->GetYaxis()->GetBinCenter(maxy), maxs);
1012
1013 TH1 *result = fHist.ProjectionZ("AlphaFit", maxx, maxx, maxy, maxy);
1014 result->Scale(entries/h->GetEntries());
1015
1016 result->SetDirectory(NULL);
1017 result->SetNameTitle("Result \\alpha", title);
1018 result->SetBit(kCanDelete);
1019 result->SetXTitle("\\alpha [\\circ]");
1020 result->SetYTitle("Counts");
1021 result->Draw();
1022
1023 TF1 f1("", func.GetExpFormula(), 0, 90);
1024 TF1 f2("", func.GetExpFormula(), 0, 90);
1025 f1.SetParameters(maxpar.GetArray());
1026 f2.SetParameters(maxpar.GetArray());
1027 f2.FixParameter(0, 0);
1028 f2.FixParameter(1, 0);
1029 f2.FixParameter(2, 1);
1030 f1.SetLineColor(kGreen);
1031 f2.SetLineColor(kRed);
1032
1033 f2.DrawCopy("same");
1034 f1.DrawCopy("same");
1035
1036 TPaveText *leg = new TPaveText(0.35, 0.10, 0.90, 0.35, "brNDC");
1037 leg->SetBorderSize(1);
1038 leg->SetTextSize(0.04);
1039 leg->AddText(0.5, 0.82, Form("A * exp(-(\\frac{x-\\mu}{\\sigma})^{2}/2) + pol%d", polynom))->SetTextAlign(22);
1040 //leg->AddText(0.5, 0.82, "A * exp(-(\\frac{x-\\mu}{\\sigma})^{2}/2) + b*x^{2} + a")->SetTextAlign(22);
1041 leg->AddLine(0, 0.65, 0, 0.65);
1042 leg->AddText(0.06, 0.54, Form("A=%.2f", maxpar[0]))->SetTextAlign(11);
1043 leg->AddText(0.06, 0.34, Form("\\sigma=%.2f", maxpar[2]))->SetTextAlign(11);
1044 leg->AddText(0.06, 0.14, Form("\\mu=%.2f (fix)", maxpar[1]))->SetTextAlign(11);
1045 leg->AddText(0.60, 0.54, Form("a=%.2f", maxpar[3]))->SetTextAlign(11);
1046 leg->AddText(0.60, 0.34, Form("b=%.2f (fix)", maxpar[4]))->SetTextAlign(11);
1047 if (polynom>1)
1048 leg->AddText(0.60, 0.14, Form("c=%.2f", maxpar[5]))->SetTextAlign(11);
1049 leg->SetBit(kCanDelete);
1050 leg->Draw();
1051
1052 q->cd(2);
1053
1054 TGraph *g = new TGraph;
1055 g->SetPoint(0, 0, 0);
1056
1057 Int_t max=0;
1058 Float_t maxsig=0;
1059 for (int i=1; i<89; i++)
1060 {
1061 const Double_t s = f1.Integral(0, (float)i)/w;
1062 const Double_t b = f2.Integral(0, (float)i)/w;
1063
1064 const Double_t sig = SignificanceLiMa(s, b);
1065
1066 g->SetPoint(g->GetN(), i, sig);
1067
1068 if (sig>maxsig)
1069 {
1070 max = i;
1071 maxsig = sig;
1072 }
1073 }
1074
1075 g->SetNameTitle("SigVs\\alpha", "Significance vs \\alpha");
1076 g->GetHistogram()->SetXTitle("\\alpha_{0} [\\circ]");
1077 g->GetHistogram()->SetYTitle("Significance");
1078 g->SetBit(kCanDelete);
1079 g->Draw("AP");
1080
1081 leg = new TPaveText(0.35, 0.10, 0.90, 0.25, "brNDC");
1082 leg->SetBorderSize(1);
1083 leg->SetTextSize(0.1);
1084 leg->AddText(Form("\\sigma_{max}=%.1f at \\alpha_{max}=%d\\circ", maxsig, max));
1085 leg->SetBit(kCanDelete);
1086 leg->Draw();
1087 }
1088}
Note: See TracBrowser for help on using the repository browser.