source: trunk/MagicSoft/Mars/mhflux/MHFalseSource.cc@ 5915

Last change on this file since 5915 was 5901, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 37.0 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 <TF2.h>
118#include <TH2.h>
119#include <TGraph.h>
120#include <TStyle.h>
121#include <TCanvas.h>
122#include <TRandom.h>
123#include <TPaveText.h>
124#include <TStopwatch.h>
125
126#include "MGeomCam.h"
127#include "MSrcPosCam.h"
128#include "MHillasSrc.h"
129#include "MTime.h"
130#include "MObservatory.h"
131#include "MPointingPos.h"
132#include "MAstroCatalog.h"
133#include "MAstroSky2Local.h"
134#include "MStatusDisplay.h"
135
136#include "MMath.h"
137#include "MAlphaFitter.h"
138
139#include "MBinning.h"
140#include "MParList.h"
141
142#include "MLog.h"
143#include "MLogManip.h"
144
145ClassImp(MHFalseSource);
146
147using namespace std;
148
149//class MHillasExt;
150
151// --------------------------------------------------------------------------
152//
153// Default Constructor
154//
155MHFalseSource::MHFalseSource(const char *name, const char *title)
156 : fTime(0), fPointPos(0), fObservatory(0), fMm2Deg(-1), fAlphaCut(12.5),
157 fBgMean(55), fMinDist(-1), fMaxDist(-1), fMinDW(-1), fMaxDW(-1),
158 fHistOff(0)
159{
160 //
161 // set the name and title of this object
162 //
163 fName = name ? name : "MHFalseSource";
164 fTitle = title ? title : "3D-plot of Alpha vs x, y";
165
166 fHist.SetDirectory(NULL);
167
168 fHist.SetName("Alpha");
169 fHist.SetTitle("3D-plot of Alpha vs x, y");
170 fHist.SetXTitle("x [\\circ]");
171 fHist.SetYTitle("y [\\circ]");
172 fHist.SetZTitle("\\alpha [\\circ]");
173}
174
175void MHFalseSource::MakeSymmetric(TH1 *h)
176{
177 h->SetMinimum();
178 h->SetMaximum();
179
180 const Float_t min = TMath::Abs(h->GetMinimum());
181 const Float_t max = TMath::Abs(h->GetMaximum());
182
183 const Float_t absmax = TMath::Max(min, max)*1.002;
184
185 h->SetMaximum( absmax);
186 h->SetMinimum(-absmax);
187}
188
189// --------------------------------------------------------------------------
190//
191// Set the alpha cut (|alpha|<fAlphaCut) which is use to estimate the
192// number of excess events
193//
194void MHFalseSource::SetAlphaCut(Float_t alpha)
195{
196 if (alpha<0)
197 *fLog << warn << "Alpha<0... taking absolute value." << endl;
198
199 fAlphaCut = TMath::Abs(alpha);
200
201 Modified();
202}
203
204// --------------------------------------------------------------------------
205//
206// Set mean alpha around which the off sample is determined
207// (fBgMean-fAlphaCut/2<|fAlpha|<fBgMean+fAlphaCut/2) which is use
208// to estimate the number of off events
209//
210void MHFalseSource::SetBgMean(Float_t alpha)
211{
212 if (alpha<0)
213 *fLog << warn << "Alpha<0... taking absolute value." << endl;
214
215 fBgMean = TMath::Abs(alpha);
216
217 Modified();
218}
219
220// --------------------------------------------------------------------------
221//
222// Calculate Significance as
223// significance = (s-b)/sqrt(s+k*k*b) mit k=s/b
224//
225// s: total number of events in signal region
226// b: number of background events in signal region
227//
228Double_t MHFalseSource::Significance(Double_t s, Double_t b)
229{
230 return MMath::SignificanceSym(s, b);
231}
232
233// --------------------------------------------------------------------------
234//
235// calculates the significance according to Li & Ma
236// ApJ 272 (1983) 317, Formula 17
237//
238// s // s: number of on events
239// b // b: number of off events
240// alpha = t_on/t_off; // t: observation time
241//
242Double_t MHFalseSource::SignificanceLiMa(Double_t s, Double_t b, Double_t alpha)
243{
244 return MMath::SignificanceLiMaSigned(s, b);
245}
246
247// --------------------------------------------------------------------------
248//
249// Set binnings (takes BinningFalseSource) and prepare filling of the
250// histogram.
251//
252// Also search for MTime, MObservatory and MPointingPos
253//
254//MHillasExt *ext=0;
255Bool_t MHFalseSource::SetupFill(const MParList *plist)
256{
257 const MGeomCam *geom = (MGeomCam*)plist->FindObject("MGeomCam");
258 if (!geom)
259 {
260 *fLog << err << "MGeomCam not found... aborting." << endl;
261 return kFALSE;
262 }
263 /*
264 ext = (MHillasExt*)plist->FindObject("MHillasExt");
265 if (!ext)
266 {
267 *fLog << err << "MHillasExt not found... aborting." << endl;
268 return kFALSE;
269 }
270 */
271 fMm2Deg = geom->GetConvMm2Deg();
272
273 if (fName!=(TString)"MHFalseSourceOff" && fHistOff==NULL)
274 {
275 MHFalseSource *hoff = (MHFalseSource*)plist->FindObject("MHFalseSourceOff", "MHFalseSource");
276 if (!hoff)
277 *fLog << inf << "No MHFalseSourceOff [MHFalseSource] found... using current data only!" << endl;
278 else
279 {
280 *fLog << inf << "MHFalseSource [MHFalseSource] found... using on-off mode!" << endl;
281 SetOffData(*hoff);
282 }
283 }
284
285 if (fHistOff)
286 MH::SetBinning(&fHist, fHistOff);
287 else
288 {
289 MBinning binsa;
290 binsa.SetEdges(18, 0, 90);
291
292 const MBinning *bins = (MBinning*)plist->FindObject("BinningFalseSource");
293 if (!bins)
294 {
295 const Float_t r = (geom ? geom->GetMaxRadius()/3 : 200)*fMm2Deg;
296
297 MBinning b;
298 b.SetEdges(20, -r, r);
299 SetBinning(&fHist, &b, &b, &binsa);
300 }
301 else
302 SetBinning(&fHist, bins, bins, &binsa);
303 }
304
305 fPointPos = (MPointingPos*)plist->FindObject(AddSerialNumber("MPointingPos"));
306 if (!fPointPos)
307 *fLog << warn << "MPointingPos not found... no derotation." << endl;
308
309 fTime = (MTime*)plist->FindObject(AddSerialNumber("MTime"));
310 if (!fTime)
311 *fLog << warn << "MTime not found... no derotation." << endl;
312
313 fSrcPos = (MSrcPosCam*)plist->FindObject(AddSerialNumber("MSrcPosCam"));
314 if (!fSrcPos)
315 *fLog << warn << "MSrcPosCam not found... no translation." << endl;
316
317 fObservatory = (MObservatory*)plist->FindObject(AddSerialNumber("MObservatory"));
318 if (!fObservatory)
319 *fLog << warn << "MObservatory not found... no derotation." << endl;
320
321 // FIXME: Because the pointing position could change we must check
322 // for the current pointing position and add a offset in the
323 // Fill function!
324 fRa = fPointPos ? fPointPos->GetRa() : 0;
325 fDec = fPointPos ? fPointPos->GetDec() : 90;
326
327 return kTRUE;
328}
329
330// --------------------------------------------------------------------------
331//
332// Fill the histogram. For details see the code or the class description
333//
334Bool_t MHFalseSource::Fill(const MParContainer *par, const Stat_t w)
335{
336 const MHillas *hil = dynamic_cast<const MHillas*>(par);
337 if (!hil)
338 {
339 *fLog << err << "MHFalseSource::Fill: No container specified!" << endl;
340 return kFALSE;
341 }
342
343 // Get max radius...
344 const Double_t maxr = 0.98*TMath::Abs(fHist.GetBinCenter(1));
345
346 // Get camera rotation angle
347 Double_t rho = 0;
348 if (fTime && fObservatory && fPointPos)
349 rho = fPointPos->RotationAngle(*fObservatory, *fTime);
350 //if (fPointPos)
351 // rho = fPointPos->RotationAngle(*fObservatory);
352
353 // Create necessary containers for calculation
354 MSrcPosCam src;
355 MHillasSrc hsrc;
356 hsrc.SetSrcPos(&src);
357
358 // Get number of bins and bin-centers
359 const Int_t nx = fHist.GetNbinsX();
360 const Int_t ny = fHist.GetNbinsY();
361 Axis_t cx[nx];
362 Axis_t cy[ny];
363 fHist.GetXaxis()->GetCenter(cx);
364 fHist.GetYaxis()->GetCenter(cy);
365
366 for (int ix=0; ix<nx; ix++)
367 {
368 for (int iy=0; iy<ny; iy++)
369 {
370 // check distance... to get a circle plot
371 if (TMath::Hypot(cx[ix], cy[iy])>maxr)
372 continue;
373
374 // rotate center of bin
375 // precalculation of sin/cos doesn't accelerate
376 TVector2 v(cx[ix], cy[iy]);
377 if (rho!=0)
378 v=v.Rotate(rho);
379
380 // convert degrees to millimeters
381 v *= 1./fMm2Deg;
382
383 if (fSrcPos)
384 v += fSrcPos->GetXY();
385
386 src.SetXY(v);
387
388 // Source dependant hillas parameters
389 if (hsrc.Calc(*hil/*, ext*/)>0)
390 {
391 *fLog << warn << "Calculation of MHillasSrc failed for x=" << cx[ix] << " y=" << cy[iy] << endl;
392 return kFALSE;
393 }
394
395 // FIXME: This should be replaced by an external MFilter
396 // and/or MTaskList
397 // Source dependant distance cut
398 if (fMinDist>0 && hsrc.GetDist()*fMm2Deg<fMinDist)
399 continue;
400 if (fMaxDist>0 && hsrc.GetDist()*fMm2Deg>fMaxDist)
401 continue;
402
403 if (fMaxDW>0 && hsrc.GetDist()>fMaxDW*hil->GetWidth())
404 continue;
405 if (fMinDW<0 && hsrc.GetDist()<fMinDW*hil->GetWidth())
406 continue;
407
408 // Fill histogram
409 const Double_t alpha = hsrc.GetAlpha();
410 fHist.Fill(cx[ix], cy[iy], TMath::Abs(alpha), w);
411 }
412 }
413
414 return kTRUE;
415}
416
417// --------------------------------------------------------------------------
418//
419// Create projection for off data, taking sum of bin contents of
420// range (fBgMean-fAlphaCut/2, fBgMean+fAlphaCut) Making sure to take
421// the same number of bins than for on-data
422//
423void MHFalseSource::ProjectOff(const TH3D &src, TH2D *h2, TH2D *all)
424{
425 TAxis &axe = *src.GetZaxis();
426
427 // Find range to cut (left edge and width)
428 const Int_t f = axe.FindBin(fBgMean-fAlphaCut/2);
429 const Int_t l = axe.FindBin(fAlphaCut)+f-1;
430
431 axe.SetRange(f, l);
432 const Float_t cut1 = axe.GetBinLowEdge(f);
433 const Float_t cut2 = axe.GetBinUpEdge(l);
434 h2->SetTitle(Form("Distribution of %.1f\\circ<|\\alpha|<%.1f\\circ in x,y", cut1, cut2));
435
436 // Get projection for range
437 TH2D *p = (TH2D*)src.Project3D("yx_off");
438
439 // Reset range
440 axe.SetRange(0,9999);
441
442 // Move contents from projection to h2
443 h2->Reset();
444 h2->Add(p, all->GetMaximum());
445 h2->Divide(all);
446
447 // Delete p
448 delete p;
449
450 // Set Minimum as minimum value Greater Than 0
451 h2->SetMinimum(GetMinimumGT(*h2));
452}
453
454// --------------------------------------------------------------------------
455//
456// Create projection for on data, taking sum of bin contents of
457// range (0, fAlphaCut)
458//
459void MHFalseSource::ProjectOn(const TH3D &src, TH2D *h3, TH2D *all)
460{
461 TAxis &axe = *src.GetZaxis();
462
463 // Find range to cut
464 axe.SetRangeUser(0, fAlphaCut);
465 const Float_t cut = axe.GetBinUpEdge(axe.GetLast());
466 h3->SetTitle(Form("Distribution of |\\alpha|<%.1f\\circ in x,y", cut));
467
468 // Get projection for range
469 TH2D *p = (TH2D*)src.Project3D("yx_on");
470
471 // Reset range
472 axe.SetRange(0,9999);
473
474 // Move contents from projection to h3
475 h3->Reset();
476 h3->Add(p, all->GetMaximum());
477 h3->Divide(all);
478
479 // Delete p
480 delete p;
481
482 // Set Minimum as minimum value Greater Than 0
483 h3->SetMinimum(GetMinimumGT(*h3));
484}
485
486// --------------------------------------------------------------------------
487//
488// Create projection for all data, taking sum of bin contents of
489// range (0, 90) - corresponding to the number of entries in this slice.
490//
491void MHFalseSource::ProjectAll(TH2D *h3)
492{
493 h3->SetTitle("Number of entries");
494
495 // Get projection for range
496 TH2D *p = (TH2D*)fHist.Project3D(Form("yx_%d", gRandom->Uniform(999999)));
497 p->SetDirectory(0);
498
499 // Move contents from projection to h3
500 h3->Reset();
501 h3->Add(p);
502 delete p;
503
504 // Set Minimum as minimum value Greater Than 0
505 h3->SetMinimum(GetMinimumGT(*h3));
506}
507
508void MHFalseSource::ProjectOnOff(TH2D *h2, TH2D *h0)
509{
510 ProjectOn(*fHistOff, h2, h0);
511
512 TH2D h;
513 MH::SetBinning(&h, h2);
514
515 // Divide by number of entries in off region (of off-data)
516 ProjectOff(*fHistOff, &h, h0);
517 h2->Divide(&h);
518
519 // Multiply by number of entries in off region (of on-data)
520 ProjectOff(fHist, &h, h0);
521 h2->Multiply(&h);
522
523 // Recalculate Minimum
524 h2->SetMinimum(GetMinimumGT(*h2));
525}
526
527// --------------------------------------------------------------------------
528//
529// Update the projections and paint them
530//
531void MHFalseSource::Paint(Option_t *opt)
532{
533 // Set pretty color palette
534 gStyle->SetPalette(1, 0);
535
536 TVirtualPad *padsave = gPad;
537
538 TH1D* h1;
539 TH2D* h0;
540 TH2D* h2;
541 TH2D* h3;
542 TH2D* h4;
543 TH2D* h5;
544
545 /*
546 fHistProjAll = Form("All_%p", this);
547 fHistProjOn = Form("On_%p", this);
548 fHistProjOff = Form("Off_%p", this);
549 fHistProjDiff = Form("Diff_%p", this);
550 fHistProjAll = Form("All_%p", this);
551 */
552
553 // Update projection of all-events
554 padsave->GetPad(2)->cd(3);
555 if ((h0 = (TH2D*)gPad->FindObject("Alpha_yx_all")))
556 ProjectAll(h0);
557
558 // Update projection of on-events
559 padsave->GetPad(1)->cd(1);
560 if ((h3 = (TH2D*)gPad->FindObject("Alpha_yx_on")))
561 ProjectOn(fHist, h3, h0);
562
563 // Update projection of off-events
564 padsave->GetPad(1)->cd(3);
565 if ((h2 = (TH2D*)gPad->FindObject("Alpha_yx_off")))
566 {
567 if (!fHistOff)
568 ProjectOff(fHist, h2, h0);
569 else
570 ProjectOnOff(h2, h0);
571 }
572
573 padsave->GetPad(2)->cd(2);
574 if ((h5 = (TH2D*)gPad->FindObject("Alpha_yx_diff")))
575 {
576 h5->Add(h2, h3, -1);
577 MakeSymmetric(h5);
578 }
579
580 // Update projection of significance
581 padsave->GetPad(1)->cd(2);
582 if (h2 && h3 && (h4 = (TH2D*)gPad->FindObject("Alpha_yx_sig")))
583 {
584 const Int_t nx = h4->GetXaxis()->GetNbins();
585 const Int_t ny = h4->GetYaxis()->GetNbins();
586 //const Int_t nr = nx*nx + ny*ny;
587
588 Int_t maxx=nx/2;
589 Int_t maxy=ny/2;
590
591 Int_t max = h4->GetBin(nx, ny);
592
593 for (int ix=1; ix<=nx; ix++)
594 for (int iy=1; iy<=ny; iy++)
595 {
596 const Int_t n = h4->GetBin(ix, iy);
597
598 const Double_t s = h3->GetBinContent(n);
599 const Double_t b = h2->GetBinContent(n);
600
601 const Double_t sig = SignificanceLiMa(s, b);
602
603 h4->SetBinContent(n, sig);
604
605 if (sig>h4->GetBinContent(max) && sig>0/* && (ix-nx/2)*(ix-nx/2)+(iy-ny/2)*(iy-ny/2)<nr*nr/9*/)
606 {
607 max = n;
608 maxx=ix;
609 maxy=iy;
610 }
611 }
612
613 MakeSymmetric(h4);
614
615 // Update projection of 'the best alpha-plot'
616 padsave->GetPad(2)->cd(1);
617 if ((h1 = (TH1D*)gPad->FindObject("Alpha_z")) && max>0)
618 {
619 const Double_t x = h4->GetXaxis()->GetBinCenter(maxx);
620 const Double_t y = h4->GetYaxis()->GetBinCenter(maxy);
621 const Double_t s = h4->GetBinContent(max);
622
623 // This is because a 3D histogram x and y are vice versa
624 // Than for their projections
625 TH1 *h = fHist.ProjectionZ("Alpha_z", maxx, maxx, maxy, maxy);
626 h->SetTitle(Form("Distribution of \\alpha for x=%.2f y=%.2f (S_{max}=%.1f\\sigma)", x, y, s));
627
628 TH1D *h0=0;
629 if ((h0 = (TH1D*)gPad->FindObject("AlphaOff_z")))
630 {
631 fHistOff->ProjectionZ("AlphaOff_z", maxx, maxx, maxy, maxy);
632
633 const Int_t f = h0->GetXaxis()->FindFixBin(fBgMean-fAlphaCut/2);
634 const Int_t l = h0->GetXaxis()->FindFixBin(fAlphaCut)+f-1;
635 h0->Scale(h1->Integral(f, l)/h0->Integral(f, l));
636 //h0->Scale(h1->GetEntries()/h0->GetEntries());
637
638 }
639 }
640 }
641
642 gPad = padsave;
643}
644
645// --------------------------------------------------------------------------
646//
647// Get the MAstroCatalog corresponding to fRa, fDec. The limiting magnitude
648// is set to 9, while the fov is equal to the current fov of the false
649// source plot.
650//
651TObject *MHFalseSource::GetCatalog()
652{
653 const Double_t maxr = 0.98*TMath::Abs(fHist.GetBinCenter(1));
654
655 // Create catalog...
656 MAstroCatalog *stars = new MAstroCatalog;
657 stars->SetLimMag(9);
658 stars->SetGuiActive(kFALSE);
659 stars->SetRadiusFOV(maxr);
660 stars->SetRaDec(fRa*TMath::DegToRad()*15, fDec*TMath::DegToRad());
661 stars->ReadBSC("bsc5.dat");
662
663 stars->SetBit(kCanDelete);
664 return stars;
665}
666
667// --------------------------------------------------------------------------
668//
669// Draw the histogram
670//
671void MHFalseSource::Draw(Option_t *opt)
672{
673 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
674 pad->SetBorderMode(0);
675
676 AppendPad("");
677
678 pad->Divide(1, 2, 0, 0.03);
679
680// TObject *catalog = GetCatalog();
681
682 // Initialize upper part
683 pad->cd(1);
684 // Make sure that the catalog is deleted only once
685 // Normally this is not done by root, because it is not necessary...
686 // but in this case it is necessary, because the catalog is
687 // deleted by the first pad and the second one tries to do the same!
688// gROOT->GetListOfCleanups()->Add(gPad);
689 gPad->SetBorderMode(0);
690 gPad->Divide(3, 1);
691
692 // PAD #1
693 pad->GetPad(1)->cd(1);
694 gPad->SetBorderMode(0);
695 fHist.GetZaxis()->SetRangeUser(0,fAlphaCut);
696 TH1 *h3 = fHist.Project3D("yx_on");
697 fHist.GetZaxis()->SetRange(0,9999);
698 h3->SetDirectory(NULL);
699 h3->SetXTitle(fHist.GetXaxis()->GetTitle());
700 h3->SetYTitle(fHist.GetYaxis()->GetTitle());
701 h3->Draw("colz");
702 h3->SetBit(kCanDelete);
703// catalog->Draw("mirror same *");
704
705 // PAD #2
706 pad->GetPad(1)->cd(2);
707 gPad->SetBorderMode(0);
708 fHist.GetZaxis()->SetRange(0,0);
709 TH1 *h4 = fHist.Project3D("yx_sig"); // Do this to get the correct binning....
710 fHist.GetZaxis()->SetRange(0,9999);
711 h4->SetTitle("Significance");
712 h4->SetDirectory(NULL);
713 h4->SetXTitle(fHist.GetXaxis()->GetTitle());
714 h4->SetYTitle(fHist.GetYaxis()->GetTitle());
715 h4->Draw("colz");
716 h4->SetBit(kCanDelete);
717// catalog->Draw("mirror same *");
718
719 // PAD #3
720 pad->GetPad(1)->cd(3);
721 gPad->SetBorderMode(0);
722 TH1 *h2 = 0;
723 if (fHistOff)
724 {
725 fHistOff->GetZaxis()->SetRangeUser(0,fAlphaCut);
726 h2 = fHistOff->Project3D("yx_off");
727 fHistOff->GetZaxis()->SetRange(0,9999);
728 }
729 else
730 {
731 fHist.GetZaxis()->SetRangeUser(fBgMean-fAlphaCut/2, fBgMean+fAlphaCut/2);
732 h2 = fHist.Project3D("yx_off");
733 fHist.GetZaxis()->SetRange(0,9999);
734 }
735 h2->SetDirectory(NULL);
736 h2->SetXTitle(fHist.GetXaxis()->GetTitle());
737 h2->SetYTitle(fHist.GetYaxis()->GetTitle());
738 h2->Draw("colz");
739 h2->SetBit(kCanDelete);
740// catalog->Draw("mirror same *");
741
742 // Initialize lower part
743 pad->cd(2);
744 // Make sure that the catalog is deleted only once
745// gROOT->GetListOfCleanups()->Add(gPad);
746 gPad->SetBorderMode(0);
747 gPad->Divide(3, 1);
748
749 // PAD #4
750 pad->GetPad(2)->cd(1);
751 gPad->SetBorderMode(0);
752 TH1 *h1 = fHist.ProjectionZ("Alpha_z");
753 h1->SetDirectory(NULL);
754 h1->SetTitle("Distribution of \\alpha");
755 h1->SetXTitle(fHist.GetZaxis()->GetTitle());
756 h1->SetYTitle("Counts");
757 h1->Draw();
758 h1->SetBit(kCanDelete);
759 if (fHistOff)
760 {
761 h1->SetLineColor(kGreen);
762
763 h1 = fHistOff->ProjectionZ("AlphaOff_z");
764 h1->SetDirectory(NULL);
765 h1->SetTitle("Distribution of \\alpha");
766 h1->SetXTitle(fHistOff->GetZaxis()->GetTitle());
767 h1->SetYTitle("Counts");
768 h1->Draw("same");
769 h1->SetBit(kCanDelete);
770 h1->SetLineColor(kRed);
771 }
772
773 // PAD #5
774 pad->GetPad(2)->cd(2);
775 gPad->SetBorderMode(0);
776 TH1 *h5 = (TH1*)h3->Clone("Alpha_yx_diff");
777 h5->Add(h2, -1);
778 h5->SetTitle("Difference of on- and off-distribution");
779 h5->SetDirectory(NULL);
780 h5->Draw("colz");
781 h5->SetBit(kCanDelete);
782// catalog->Draw("mirror same *");
783
784 // PAD #6
785 pad->GetPad(2)->cd(3);
786 gPad->SetBorderMode(0);
787 TH1 *h0 = fHist.Project3D("yx_all");
788 h0->SetDirectory(NULL);
789 h0->SetXTitle(fHist.GetXaxis()->GetTitle());
790 h0->SetYTitle(fHist.GetYaxis()->GetTitle());
791 h0->Draw("colz");
792 h0->SetBit(kCanDelete);
793// catalog->Draw("mirror same *");
794}
795
796// --------------------------------------------------------------------------
797//
798// Everything which is in the main pad belongs to this class!
799//
800Int_t MHFalseSource::DistancetoPrimitive(Int_t px, Int_t py)
801{
802 return 0;
803}
804
805// --------------------------------------------------------------------------
806//
807// Set all sub-pads to Modified()
808//
809void MHFalseSource::Modified()
810{
811 if (!gPad)
812 return;
813
814 TVirtualPad *padsave = gPad;
815 padsave->Modified();
816 padsave->GetPad(1)->cd(1);
817 gPad->Modified();
818 padsave->GetPad(1)->cd(3);
819 gPad->Modified();
820 padsave->GetPad(2)->cd(1);
821 gPad->Modified();
822 padsave->GetPad(2)->cd(2);
823 gPad->Modified();
824 padsave->GetPad(2)->cd(3);
825 gPad->Modified();
826 gPad->cd();
827}
828
829Double_t FcnGauss2d(Double_t *x, Double_t *par)
830{
831 TVector2 v = TVector2(x[0], x[1]).Rotate(par[5]*TMath::DegToRad());
832
833 const Double_t g0 = TMath::Gaus(v.X(), par[1], par[2]);
834 const Double_t g1 = TMath::Gaus(v.Y(), par[3], par[4]);
835
836 //Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE);
837 return par[0]*g0*g1;
838}
839
840// --------------------------------------------------------------------------
841//
842// This is a preliminary implementation of a alpha-fit procedure for
843// all possible source positions. It will be moved into its own
844// more powerfull class soon.
845//
846// The fit function is "gaus(0)+pol2(3)" which is equivalent to:
847// [0]*exp(-0.5*((x-[1])/[2])^2) + [3] + [4]*x + [5]*x^2
848// or
849// A*exp(-0.5*((x-mu)/sigma)^2) + a + b*x + c*x^2
850//
851// Parameter [1] is fixed to 0 while the alpha peak should be
852// symmetric around alpha=0.
853//
854// Parameter [4] is fixed to 0 because the first derivative at
855// alpha=0 should be 0, too.
856//
857// In a first step the background is fitted between bgmin and bgmax,
858// while the parameters [0]=0 and [2]=1 are fixed.
859//
860// In a second step the signal region (alpha<sigmax) is fittet using
861// the whole function with parameters [1], [3], [4] and [5] fixed.
862//
863// The number of excess and background events are calculated as
864// s = int(0, sigint, gaus(0)+pol2(3))
865// b = int(0, sigint, pol2(3))
866//
867// The Significance is calculated using the Significance() member
868// function.
869//
870void MHFalseSource::FitSignificance(Float_t sigint, Float_t sigmax, Float_t bgmin, Float_t bgmax, Byte_t polynom)
871{
872// TObject *catalog = GetCatalog();
873
874 TH1D h0a("A", "", 50, 0, 4000);
875 TH1D h4a("chisq1", "", 50, 0, 35);
876 //TH1D h5a("prob1", "", 50, 0, 1.1);
877 TH1D h6("signifcance", "", 50, -20, 20);
878
879 TH1D h1("mu", "Parameter \\mu", 50, -1, 1);
880 TH1D h2("sigma", "Parameter \\sigma", 50, 0, 90);
881 TH1D h3("b", "Parameter b", 50, -0.1, 0.1);
882
883 TH1D h0b("a", "Parameter a (red), A (blue)", 50, 0, 4000);
884 TH1D h4b("\\chi^{2}", "\\chi^{2} (red, green) / significance (black)", 50, 0, 35);
885 //TH1D h5b("prob", "Fit probability: Bg(red), F(blue)", 50, 0, 1.1);
886
887 h0a.SetLineColor(kBlue);
888 h4a.SetLineColor(kBlue);
889 //h5a.SetLineColor(kBlue);
890 h0b.SetLineColor(kRed);
891 h4b.SetLineColor(kRed);
892 //h5b.SetLineColor(kRed);
893
894 TH1 *hist = fHist.Project3D("yx_fit");
895 hist->SetDirectory(0);
896 TH1 *hists = fHist.Project3D("yx_fit");
897 hists->SetDirectory(0);
898 TH1 *histb = fHist.Project3D("yx_fit");
899 histb->SetDirectory(0);
900 hist->Reset();
901 hists->Reset();
902 histb->Reset();
903 hist->SetNameTitle("Significance",
904 Form("Fit Region: Signal<%.1f\\circ, %.1f\\circ<Bg<%.1f\\circ",
905 sigmax, bgmin, bgmax));
906 hists->SetName("Excess");
907 histb->SetName("Background");
908 hist->SetXTitle(fHist.GetXaxis()->GetTitle());
909 hists->SetXTitle(fHist.GetXaxis()->GetTitle());
910 histb->SetXTitle(fHist.GetXaxis()->GetTitle());
911 hist->SetYTitle(fHist.GetYaxis()->GetTitle());
912 hists->SetYTitle(fHist.GetYaxis()->GetTitle());
913 histb->SetYTitle(fHist.GetYaxis()->GetTitle());
914
915 const Double_t w = fHist.GetZaxis()->GetBinWidth(1);
916
917 TArrayD maxpar;
918
919 /* func.SetParName(0, "A");
920 * func.SetParName(1, "mu");
921 * func.SetParName(2, "sigma");
922 */
923
924 const Int_t nx = hist->GetXaxis()->GetNbins();
925 const Int_t ny = hist->GetYaxis()->GetNbins();
926 //const Int_t nr = nx*nx+ny*ny;
927
928 Double_t maxalpha0=0;
929 Double_t maxs=3;
930
931 Int_t maxx=0;
932 Int_t maxy=0;
933
934 TStopwatch clk;
935 clk.Start();
936
937 *fLog << inf;
938 *fLog << "Signal fit: alpha < " << sigmax << endl;
939 *fLog << "Integration: alpha < " << sigint << endl;
940 *fLog << "Background fit: " << bgmin << " < alpha < " << bgmax << endl;
941 *fLog << "Polynom order: " << (int)polynom << endl;
942 *fLog << "Fitting False Source Plot..." << flush;
943
944 TH1 *h0 = fHist.Project3D("yx_entries");
945 Float_t entries = h0->GetMaximum();
946 delete h0;
947
948 MAlphaFitter fit;
949 fit.SetSignalIntegralMax(sigint);
950 fit.SetSignalFitMax(sigmax);
951 fit.SetBackgroundFitMin(bgmin);
952 fit.SetBackgroundFitMax(bgmax);
953 fit.SetPolynomOrder(polynom);
954
955 TH1D *h=0, *hoff=0;
956 Double_t scale = 1;
957 for (int ix=1; ix<=nx; ix++)
958 for (int iy=1; iy<=ny; iy++)
959 {
960 // This is because a 3D histogram x and y are vice versa
961 // Than for their projections
962 h = fHist.ProjectionZ("AlphaFit", ix, ix, iy, iy);
963
964 if (h->GetEntries()==0)
965 continue;
966
967 h->Scale(entries/h->GetEntries());
968
969 if (fHistOff)
970 {
971 hoff = fHistOff->ProjectionZ("AlphaFitOff", ix, ix, iy, iy);
972 hoff->Scale(entries/h->GetEntries());
973 scale = fit.Scale(*hoff, *h);
974 }
975
976 if (!fit.Fit(*h, hoff, scale))
977 continue;
978
979 const Double_t alpha0 = h->GetBinContent(1);
980 if (alpha0>maxalpha0)
981 maxalpha0=alpha0;
982
983 // Fill results into some histograms
984 h0a.Fill(fit.GetGausA()); // gaus-A
985 h0b.Fill(fit.GetCoefficient(3)); // 3
986 h1.Fill(fit.GetGausMu()); // mu
987 h2.Fill(fit.GetGausSigma()); // sigma-gaus
988 if (polynom>1 && !fHistOff)
989 h3.Fill(fit.GetCoefficient(5));
990 h4b.Fill(fit.GetChiSqSignal());
991
992 const Double_t sig = fit.GetSignificance();
993 const Double_t b = fit.GetEventsBackground();
994 const Double_t s = fit.GetEventsSignal();
995
996 const Int_t n = hist->GetBin(ix, iy);
997 hists->SetBinContent(n, s-b);
998 histb->SetBinContent(n, b);
999
1000 hist->SetBinContent(n, sig);
1001 if (sig!=0)
1002 h6.Fill(sig);
1003
1004 if (sig>maxs)
1005 {
1006 maxs = sig;
1007 maxx = ix;
1008 maxy = iy;
1009 maxpar = fit.GetCoefficients();
1010 }
1011 }
1012
1013 *fLog << "Done." << endl;
1014
1015 h0a.GetXaxis()->SetRangeUser(0, maxalpha0*1.5);
1016 h0b.GetXaxis()->SetRangeUser(0, maxalpha0*1.5);
1017
1018 hists->SetTitle(Form("Excess events for \\alpha<%.0f\\circ (N_{max}=%d)", sigint, (int)hists->GetMaximum()));
1019 histb->SetTitle(Form("Background events for \\alpha<%.0f\\circ", sigint));
1020
1021 //hists->SetMinimum(GetMinimumGT(*hists));
1022 histb->SetMinimum(GetMinimumGT(*histb));
1023
1024 MakeSymmetric(hists);
1025 MakeSymmetric(hist);
1026
1027 clk.Stop();
1028 clk.Print("m");
1029
1030 TCanvas *c=new TCanvas;
1031
1032 gStyle->SetPalette(1, 0);
1033
1034 c->Divide(3,2, 0, 0);
1035 c->cd(1);
1036 gPad->SetBorderMode(0);
1037 hists->Draw("colz");
1038 hists->SetBit(kCanDelete);
1039// catalog->Draw("mirror same *");
1040 c->cd(2);
1041 gPad->SetBorderMode(0);
1042 hist->Draw("colz");
1043 hist->SetBit(kCanDelete);
1044
1045
1046 const Double_t maxr = 0.9*TMath::Abs(fHist.GetBinCenter(1));
1047 TF2 f2d("Gaus-2D", FcnGauss2d, -maxr, maxr, -maxr, maxr, 6);
1048 f2d.SetLineWidth(1);
1049 f2d.SetParName(0, "Max sigma");
1050 f2d.SetParName(1, "Mean_1 deg");
1051 f2d.SetParName(2, "Sigma_1 deg");
1052 f2d.SetParName(3, "Mean_2 deg");
1053 f2d.SetParName(4, "Sigma_2 deg");
1054 f2d.SetParName(5, "Phi deg");
1055 f2d.SetParLimits(1, -maxr/2, maxr/2); // mu_1
1056 f2d.SetParLimits(3, -maxr/2, maxr/2); // mu_2
1057 f2d.SetParLimits(2, 0, maxr); // sigma_1
1058 f2d.SetParLimits(4, 0, maxr); // sigma_2
1059 f2d.SetParLimits(5, 0, 45); // phi
1060 f2d.SetParameter(0, maxs); // A
1061 f2d.SetParameter(1, hist->GetXaxis()->GetBinCenter(maxx)); // mu_1
1062 f2d.SetParameter(2, 0.1); // sigma_1
1063 f2d.SetParameter(3, hist->GetYaxis()->GetBinCenter(maxy)); // mu_2
1064 f2d.SetParameter(4, 0.1); // sigma_2
1065 f2d.FixParameter(5, 0); // phi
1066 hist->Fit(&f2d, "NI0R");
1067 f2d.DrawCopy("cont2same");
1068
1069
1070// catalog->Draw("mirror same *");
1071 c->cd(3);
1072 gPad->SetBorderMode(0);
1073 histb->Draw("colz");
1074 histb->SetBit(kCanDelete);
1075// catalog->Draw("mirror same *");
1076 c->cd(4);
1077 gPad->Divide(1,3, 0, 0);
1078 TVirtualPad *p=gPad;
1079 p->SetBorderMode(0);
1080 p->cd(1);
1081 gPad->SetBorderMode(0);
1082 h0b.DrawCopy();
1083 h0a.DrawCopy("same");
1084 p->cd(2);
1085 gPad->SetBorderMode(0);
1086 h3.DrawCopy();
1087 p->cd(3);
1088 gPad->SetBorderMode(0);
1089 h2.DrawCopy();
1090 c->cd(6);
1091 gPad->Divide(1,2, 0, 0);
1092 TVirtualPad *q=gPad;
1093 q->SetBorderMode(0);
1094 q->cd(1);
1095 gPad->SetBorderMode(0);
1096 gPad->SetBorderMode(0);
1097 h4b.DrawCopy();
1098 h4a.DrawCopy("same");
1099 h6.DrawCopy("same");
1100 q->cd(2);
1101 gPad->SetBorderMode(0);
1102 //h5b.DrawCopy();
1103 //h5a.DrawCopy("same");
1104 c->cd(5);
1105 gPad->SetBorderMode(0);
1106 if (maxx>0 && maxy>0)
1107 {
1108 const char *title = Form(" \\alpha for x=%.2f y=%.2f (S_{max}=%.1f\\sigma) ",
1109 hist->GetXaxis()->GetBinCenter(maxx),
1110 hist->GetYaxis()->GetBinCenter(maxy), maxs);
1111
1112 h = fHist.ProjectionZ("AlphaFit", maxx, maxx, maxy, maxy);
1113 h->Scale(entries/h->GetEntries());
1114
1115 h->SetDirectory(NULL);
1116 h->SetNameTitle("Result \\alpha", title);
1117 h->SetBit(kCanDelete);
1118 h->SetXTitle("\\alpha [\\circ]");
1119 h->SetYTitle("Counts");
1120 h->Draw();
1121
1122 if (fHistOff)
1123 {
1124 h->SetLineColor(kGreen);
1125
1126 TH1D *hof=fHistOff->ProjectionZ("AlphaFitOff", maxx, maxx, maxy, maxy);
1127 hof->Scale(entries/hof->GetEntries());
1128
1129 fit.Scale(*(TH1D*)hof, *(TH1D*)h);
1130
1131 hof->SetLineColor(kRed);
1132 hof->SetDirectory(NULL);
1133 hof->SetNameTitle("Result \\alpha", title);
1134 hof->SetBit(kCanDelete);
1135 hof->SetXTitle("\\alpha [\\circ]");
1136 hof->SetYTitle("Counts");
1137 hof->Draw("same");
1138
1139 TH1D *diff = (TH1D*)h->Clone("AlphaFitOnOff");
1140 diff->Add(hof, -1);
1141 diff->SetLineColor(kBlue);
1142 diff->SetNameTitle("Result On-Off \\alpha", title);
1143 diff->SetBit(kCanDelete);
1144 diff->SetXTitle("\\alpha [\\circ]");
1145 diff->SetYTitle("Counts");
1146 diff->Draw("same");
1147
1148 h->SetMinimum(diff->GetMinimum()<0 ? diff->GetMinimum()*1.2 : 0);
1149
1150 TLine l;
1151 l.DrawLine(0, 0, 90, 0);
1152 }
1153
1154 TF1 f1("f1", Form("gaus(0) + pol%d(3)", fHistOff ? 0 : polynom), 0, 90);
1155 TF1 f2("f2", Form("gaus(0) + pol%d(3)", fHistOff ? 0 : polynom), 0, 90);
1156 f1.SetParameters(maxpar.GetArray());
1157 f2.SetParameters(maxpar.GetArray());
1158 f2.FixParameter(0, 0);
1159 f2.FixParameter(1, 0);
1160 f2.FixParameter(2, 1);
1161 f1.SetLineColor(kGreen);
1162 f2.SetLineColor(kRed);
1163
1164 f2.DrawCopy("same");
1165 f1.DrawCopy("same");
1166
1167 TPaveText *leg = new TPaveText(0.35, 0.10, 0.90, 0.35, "brNDC");
1168 leg->SetBorderSize(1);
1169 leg->SetTextSize(0.04);
1170 leg->AddText(0.5, 0.82, Form("A * exp(-(\\frac{x-\\mu}{\\sigma})^{2}/2) + pol%d", polynom))->SetTextAlign(22);
1171 //leg->AddText(0.5, 0.82, "A * exp(-(\\frac{x-\\mu}{\\sigma})^{2}/2) + b*x^{2} + a")->SetTextAlign(22);
1172 leg->AddLine(0, 0.65, 0, 0.65);
1173 leg->AddText(0.06, 0.54, Form("A=%.2f", maxpar[0]))->SetTextAlign(11);
1174 leg->AddText(0.06, 0.34, Form("\\sigma=%.2f", maxpar[2]))->SetTextAlign(11);
1175 leg->AddText(0.06, 0.14, Form("\\mu=%.2f (fix)", maxpar[1]))->SetTextAlign(11);
1176 leg->AddText(0.60, 0.54, Form("a=%.2f", maxpar[3]))->SetTextAlign(11);
1177 leg->AddText(0.60, 0.34, Form("b=%.2f (fix)", maxpar[4]))->SetTextAlign(11);
1178 if (polynom>1)
1179 leg->AddText(0.60, 0.14, Form("c=%.2f", !fHistOff?maxpar[5]:0))->SetTextAlign(11);
1180 leg->SetBit(kCanDelete);
1181 leg->Draw();
1182
1183 q->cd(2);
1184
1185 TGraph *g = new TGraph;
1186 g->SetPoint(0, 0, 0);
1187
1188 Int_t max=0;
1189 Float_t maxsig=0;
1190 for (int i=1; i<89; i++)
1191 {
1192 const Double_t s = f1.Integral(0, (float)i)/w;
1193 const Double_t b = f2.Integral(0, (float)i)/w;
1194
1195 const Double_t sig = SignificanceLiMa(s, b);
1196
1197 g->SetPoint(g->GetN(), i, sig);
1198
1199 if (sig>maxsig)
1200 {
1201 max = i;
1202 maxsig = sig;
1203 }
1204 }
1205
1206 g->SetNameTitle("SigVs\\alpha", "Significance vs \\alpha");
1207 g->GetHistogram()->SetXTitle("\\alpha_{0} [\\circ]");
1208 g->GetHistogram()->SetYTitle("Significance");
1209 g->SetBit(kCanDelete);
1210 g->Draw("AP");
1211
1212 leg = new TPaveText(0.35, 0.10, 0.90, 0.25, "brNDC");
1213 leg->SetBorderSize(1);
1214 leg->SetTextSize(0.1);
1215 leg->AddText(Form("S_{max}=%.1f\\sigma at \\alpha_{max}=%d\\circ", maxsig, max));
1216 leg->SetBit(kCanDelete);
1217 leg->Draw();
1218 }
1219}
Note: See TracBrowser for help on using the repository browser.