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

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