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

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