source: trunk/MagicSoft/Mars/mhist/MHHadronness.cc@ 1587

Last change on this file since 1587 was 1574, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 14.5 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 5/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Abelardo Moralejo <mailto:moralejo@pd.infn.it>
20!
21! Copyright: MAGIC Software Development, 2000-2002
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MHHadronness
29//
30// This is histogram is a way to evaluate the quality of a gamma/hadron
31// seperation method. It is filled from a MHadronness container, which
32// stores a hadroness for the current event. The Value must be in the
33// range [0,1]. To fill the histograms correctly the information
34// whether it is a gamma or hadron (not a gamma) must be available from
35// a MMcEvt container.
36//
37// In the constructor you can change the number of used bns for the
38// evaluation.
39//
40// The meaning of the histograms (Draw, DrawClone) are the following:
41// * Upper Left Corner:
42// - black: histogram of all hadronesses for gammas
43// - red: histogram of all hadronesses for non gammas
44// * Upper Right Corner:
45// - black: acceptance of gammas (Ag) vs. the hadroness
46// - red: acceptance of non gammas (Ah) vs. the hadroness
47// - blue: 2D distance of (acceptance_hadrons, acceptances_gammas)
48// to optimum (0, 1)
49// 1-sqrt(Ag*Ag + (1-Ah)*(1-Ah))
50// * Bottom Left Corner:
51// Naive quality factor: Ag/sqrt(Ah)
52// * Bottom Right Corner:
53// - black: Acceprtance Gammas vs. Acceptance Hadrons
54// - blue cross: minimum of distance to (0, 1)
55//
56////////////////////////////////////////////////////////////////////////////
57#include "MHHadronness.h"
58
59#include <TPad.h>
60#include <TGraph.h>
61#include <TStyle.h>
62#include <TCanvas.h>
63#include <TMarker.h>
64
65#include "MLog.h"
66#include "MLogManip.h"
67
68#include "MParList.h"
69#include "MBinning.h"
70#include "MHadronness.h"
71
72#include "MMcEvt.hxx"
73
74ClassImp(MHHadronness);
75
76// --------------------------------------------------------------------------
77//
78// Setup histograms, nbins is the number of bins used for the evaluation.
79// The default is 100 bins.
80//
81MHHadronness::MHHadronness(Int_t nbins, const char *name, const char *title)
82{
83 //
84 // set the name and title of this object
85 //
86 fName = name ? name : "MHHadronness";
87 fTitle = title ? title : "Gamma/Hadron Separation Quality Histograms";
88
89 fGraph = new TGraph;
90 fGraph->SetTitle("Acceptance Gammas vs. Hadrons");
91 fGraph->SetMaximum(1);
92
93 fGhness = new TH1D("Ghness", "Hadronness", nbins, 0, 1);
94 fPhness = new TH1D("Phness", "Hadronness", nbins, 0, 1);
95 fGhness->SetXTitle("Hadronness");
96 fPhness->SetXTitle("Hadronness");
97 fGhness->SetYTitle("Counts");
98 fPhness->SetYTitle("Counts");
99
100 fIntGhness = new TH1D("AccGammas", "Acceptance", nbins, 0, 1);
101 fIntPhness = new TH1D("AccHadrons", "Acceptance", nbins, 0, 1);
102 fIntGhness->SetXTitle("Hadronness");
103 fIntPhness->SetXTitle("Hadronness");
104 fIntGhness->SetYTitle("Acceptance");
105 fIntPhness->SetYTitle("Acceptance");
106
107 /*
108 fQfac = new TH1D("Qfac", "Naive Quality factor", nbins, 0, 1);
109 fQfac->SetXTitle("Hadronness");
110 fQfac->SetYTitle("Quality");
111 */
112 fQfac = new TGraph;
113 fQfac->SetTitle(" Naive Quality factor ");
114
115 fMinDist = new TH1D("MinDist", "Minimum Dist to (0, 1)", nbins, 0, 1);
116 fMinDist->SetXTitle("Hadronness");
117 fMinDist->SetYTitle("Distance");
118
119 //fQfac->SetDirectory(NULL);
120 fGhness->SetDirectory(NULL);
121 fPhness->SetDirectory(NULL);
122 fMinDist->SetDirectory(NULL);
123 fIntGhness->SetDirectory(NULL);
124 fIntPhness->SetDirectory(NULL);
125}
126
127// --------------------------------------------------------------------------
128//
129// Delete the histograms.
130//
131MHHadronness::~MHHadronness()
132{
133 delete fGhness;
134 delete fIntGhness;
135 delete fPhness;
136 delete fIntPhness;
137 delete fQfac;
138 delete fMinDist;
139 delete fGraph;
140}
141
142// --------------------------------------------------------------------------
143//
144// Setup Filling of the histograms. It needs:
145// MMcEvt and MHadronness
146//
147Bool_t MHHadronness::SetupFill(const MParList *plist)
148{
149 fMcEvt = (MMcEvt*)plist->FindObject("MMcEvt");
150 if (!fMcEvt)
151 {
152 *fLog << err << dbginf << "MMcEvt not found... aborting." << endl;
153 return kFALSE;
154 }
155
156 fHadronness = (MHadronness*)plist->FindObject("MHadronness");
157 if (!fHadronness)
158 {
159 *fLog << err << dbginf << "MHadronness not found... aborting." << endl;
160 return kFALSE;
161 }
162
163 /*
164 MBinning* bins = (MBinning*)plist->FindObject("BinningHadronness");
165 if (!bins)
166 {
167 *fLog << err << dbginf << "BinningHadronness [MBinning] not found... aborting." << endl;
168 return kFALSE;
169 }
170
171 SetBinning(&fHist, binsalpha, binsenergy, binstheta);
172
173 fHist.Sumw2();
174 */
175
176 return kTRUE;
177}
178
179// --------------------------------------------------------------------------
180//
181// Fill the Hadronness from a MHadronness container into the corresponding
182// histogram dependant on the particle id.
183//
184Bool_t MHHadronness::Fill(const MParContainer *par)
185{
186 if (fMcEvt->GetPartId()==kGAMMA)
187 fGhness->Fill(fHadronness->GetHadronness());
188 else
189 fPhness->Fill(fHadronness->GetHadronness());
190
191 return kTRUE;
192}
193
194Float_t MHHadronness::GetQ05() const
195{
196 Int_t n = fQfac->GetN();
197
198 Double_t val1x=0;
199 Double_t val2x=1;
200
201 Double_t val1y=0;
202 Double_t val2y=0;
203
204 for (Int_t i=1; i<=n; i++)
205 {
206 Double_t x, y;
207
208 fQfac->GetPoint(i, x, y);
209
210 if (x<0.5 && x>val1x)
211 {
212 val1x = x;
213 val1y = y;
214 }
215
216 if (x>0.5 && x<val2x)
217 {
218 val2x = x;
219 val2y = y;
220 }
221 }
222
223 return val1y - (val2y-val1y)/(val2x-val1x) * (val1x-0.5);
224}
225
226// --------------------------------------------------------------------------
227//
228// Finalize the histograms:
229// - integrate the hadroness histograms --> acceptance
230// - fill the Minimum Distance histogram (formular see class description)
231// - fill the Quality histogram (formular see class description)
232//
233Bool_t MHHadronness::Finalize()
234{
235 Int_t n = fGhness->GetNbinsX();
236
237 fGraph->Set(n);
238 fQfac->Set(n);
239
240 const Stat_t sumg = fGhness->Integral(1, n+1);
241 const Stat_t sump = fPhness->Integral(1, n+1);
242
243 Float_t max=0;
244
245 for (Int_t i=1; i<=n; i++)
246 {
247 const Stat_t ip = fPhness->Integral(1, i)/sump;
248 const Stat_t ig = fGhness->Integral(1, i)/sumg;
249
250 fIntPhness->SetBinContent(i, ip);
251 fIntGhness->SetBinContent(i, ig);
252
253 fGraph->SetPoint(i, ip, ig);
254
255 if (ip<=0)
256 continue;
257
258 fMinDist->SetBinContent(i, 1-sqrt(ip*ip + (ig-1)*(ig-1)));
259
260 Double_t val = ig/sqrt(ip);
261 fQfac->SetPoint(i, ig, val);
262
263 if (val>max)
264 max = val;
265 }
266
267 fQfac->SetMaximum(max*1.05);
268
269 return kTRUE;
270}
271
272// --------------------------------------------------------------------------
273//
274// Search the corresponding points for the given hadron acceptance (acchad)
275// and interpolate the tow points (linear)
276//
277Double_t MHHadronness::GetGammaAcceptance(Double_t acchad) const
278{
279 const Int_t n = fGraph->GetN();
280 const Double_t *x = fGraph->GetX();
281 const Double_t *y = fGraph->GetY();
282
283 Int_t i = 0;
284 while (i<n && x[i]<acchad)
285 i++;
286
287 if (i==0 || i==n)
288 return 0;
289
290 if (i==n-1)
291 i--;
292
293 const Double_t x1 = x[i-1];
294 const Double_t y1 = y[i-1];
295
296 const Double_t x2 = x[i];
297 const Double_t y2 = y[i];
298
299 return (y2-y1)/(x2-x1) * (acchad-x2) + y2;
300}
301
302// --------------------------------------------------------------------------
303//
304// Search the corresponding points for the given gamma acceptance (accgam)
305// and interpolate the tow points (linear)
306//
307Double_t MHHadronness::GetHadronAcceptance(Double_t accgam) const
308{
309 const Int_t n = fGraph->GetN();
310 const Double_t *x = fGraph->GetX();
311 const Double_t *y = fGraph->GetY();
312
313 Int_t i = 0;
314 while (i<n && y[i]<accgam)
315 i++;
316
317 if (i==0 || i==n)
318 return 0;
319
320 if (i==n-1)
321 i--;
322
323 const Double_t x1 = y[i-1];
324 const Double_t y1 = x[i-1];
325
326 const Double_t x2 = y[i];
327 const Double_t y2 = x[i];
328
329 return (y2-y1)/(x2-x1) * (accgam-x2) + y2;
330}
331
332// --------------------------------------------------------------------------
333//
334// Print the corresponding Gammas Acceptance for a hadron acceptance of
335// 10%, 20%, 30%, 40% and 50%. Also the minimum distance to the optimum
336// acceptance and the corresponding acceptances and hadroness value is
337// printed, together with the maximum Q-factor.
338//
339void MHHadronness::Print(Option_t *) const
340{
341 *fLog << all;
342 *fLog << "Hadronness histograms:" << endl;
343 *fLog << "---------------------" << endl;
344
345 if (fGraph->GetN()==0)
346 {
347 *fLog << " <No Entries>" << endl;
348 return;
349 }
350
351 *fLog << "Used " << fGhness->GetEntries() << " Gammas and " << fPhness->GetEntries() << " Hadrons." << endl;
352 *fLog << "Acc Gammas @ 1% Hadron-acc: " << Form("%3.0f", GetGammaAcceptance(0.01)*100) << "%" << endl;
353 *fLog << "Acc Gammas @ 2% Hadron-acc: " << Form("%3.0f", GetGammaAcceptance(0.02)*100) << "%" << endl;
354 *fLog << "Acc Gammas @ 5% Hadron-acc: " << Form("%3.0f", GetGammaAcceptance(0.05)*100) << "%" << endl;
355 *fLog << "Acc Gammas @ 10% Hadron-acc: " << Form("%3.0f", GetGammaAcceptance(0.1)*100) << "%" << endl;
356 *fLog << "Acc Gammas @ 20% Hadron-acc: " << Form("%3.0f", GetGammaAcceptance(0.2)*100) << "%" << endl;
357 *fLog << "Acc Gammas @ 30% Hadron-acc: " << Form("%3.0f", GetGammaAcceptance(0.3)*100) << "%" << endl;
358 *fLog << "Acc Gammas @ 40% Hadron-acc: " << Form("%3.0f", GetGammaAcceptance(0.4)*100) << "%" << endl;
359 *fLog << "Acc Gammas @ 50% Hadron-acc: " << Form("%3.0f", GetGammaAcceptance(0.5)*100) << "%" << endl;
360
361 const Int_t h = fMinDist->GetMaximumBin();
362 *fLog << "Minimum Distance to (0, 1): " << Form("%.2f", 1-fMinDist->GetMaximum()) << " @ H=" << fMinDist->GetBinCenter(h) << endl;
363 *fLog << " Acc Gammas = " << Form("%3.0f", fIntGhness->GetBinContent(h)*100) << "%, ";
364 *fLog << "Acc Hadrons = " << Form("%3.0f", fIntPhness->GetBinContent(h)*100) << "%" << endl;
365
366 *fLog << "Q-Factor @ Acc Gammas=0.5: Q(0.5)=" << Form("%.1f", GetQ05()) << endl;
367 *fLog << " Acc Hadrons = " << Form("%5.1f", GetHadronAcceptance(0.5)*100) << "%" << endl;
368 *fLog << endl;
369}
370
371// --------------------------------------------------------------------------
372//
373// Draw clone of all histograms. (For the Meaning see class description)
374//
375TObject *MHHadronness::DrawClone(Option_t *opt) const
376{
377 if (fGraph->GetN()==0)
378 return NULL;
379
380 TCanvas &c = *MakeDefCanvas("Hadronness", fTitle);
381 c.Divide(2, 2);
382
383 gROOT->SetSelectedPad(NULL);
384
385 c.cd(1);
386 gStyle->SetOptStat(10);
387 Getghness()->DrawCopy();
388 Getphness()->SetLineColor(kRed);
389 Getphness()->DrawCopy("same");
390 c.cd(2);
391 gStyle->SetOptStat(0);
392 Getighness()->DrawCopy();
393 Getiphness()->SetLineColor(kRed);
394 Getiphness()->DrawCopy("same");
395 fMinDist->SetLineColor(kBlue);
396 fMinDist->DrawCopy("same");
397 c.cd(3);
398 //GetQfac()->DrawCopy();
399 TGraph &g2 = (TGraph&)*fQfac->DrawClone("A*");
400 g2.SetBit(kCanDelete);
401 gPad->Modified();
402 gPad->Update();
403 if (g2.GetHistogram())
404 {
405 g2.GetXaxis()->SetRangeUser(0, 1);
406 g2.GetXaxis()->SetTitle("Acceptance Gammas");
407 g2.GetYaxis()->SetTitle("Quality");
408 g2.SetMarkerStyle(kFullDotSmall);
409 g2.Draw("P");
410
411 gPad->Modified();
412 gPad->Update();
413 }
414 c.cd(4);
415 gPad->Modified();
416 gPad->Update();
417 TGraph &g = (TGraph&)*fGraph->DrawClone("AC");
418 g.SetBit(kCanDelete);
419 gPad->Modified();
420 gPad->Update();
421 if (g.GetHistogram())
422 {
423 g.GetXaxis()->SetRangeUser(0, 1);
424 g.GetXaxis()->SetTitle("Acceptance Hadrons");
425 g.GetYaxis()->SetTitle("Acceptance Gammas");
426 g.SetMarkerStyle(kFullDotSmall);
427 g.Draw("P");
428
429 gPad->Modified();
430 gPad->Update();
431 }
432 const Int_t h = fMinDist->GetMaximumBin();
433 TMarker *m = new TMarker(fIntPhness->GetBinContent(h),
434 fIntGhness->GetBinContent(h), kStar);
435 m->SetMarkerColor(kBlue);
436 m->SetBit(kCanDelete);
437 m->Draw();
438 return &c;
439}
440
441// --------------------------------------------------------------------------
442//
443// Draw all histograms. (For the Meaning see class description)
444//
445void MHHadronness::Draw(Option_t *)
446{
447 if (fGraph->GetN()==0)
448 return;
449
450 if (!gPad)
451 MakeDefCanvas("Hadronness", fTitle);
452
453 gPad->Divide(2, 2);
454
455 gPad->cd(1);
456 gStyle->SetOptStat(10);
457 Getghness()->Draw();
458 Getphness()->SetLineColor(kRed);
459 Getphness()->Draw("same");
460 gPad->cd(2);
461 gStyle->SetOptStat(0);
462 Getighness()->Draw();
463 Getiphness()->SetLineColor(kRed);
464 Getiphness()->Draw("same");
465 fMinDist->SetLineColor(kBlue);
466 fMinDist->Draw("same");
467 gPad->cd(3);
468 //GetQfac()->Draw();
469 fQfac->Draw("A*");
470 gPad->Modified();
471 gPad->Update();
472 if (fQfac->GetHistogram())
473 {
474 fQfac->GetXaxis()->SetRangeUser(0, 1);
475 fQfac->GetXaxis()->SetTitle("Acceptance Gammas");
476 fQfac->GetYaxis()->SetTitle("Quality");
477 fQfac->SetMarkerStyle(kFullDotSmall);
478 fQfac->Draw("P");
479
480 gPad->Modified();
481 gPad->Update();
482 }
483 gPad->cd(4);
484 gPad->Modified();
485 gPad->Update();
486 fGraph->Draw("AC");
487 gPad->Modified();
488 gPad->Update();
489 if (fGraph->GetHistogram())
490 {
491 fGraph->GetXaxis()->SetRangeUser(0, 1);
492 fGraph->GetXaxis()->SetTitle("Acceptance Hadrons");
493 fGraph->GetYaxis()->SetTitle("Acceptance Gammas");
494 fGraph->SetMarkerStyle(kFullDotSmall);
495 fGraph->Draw("P");
496 gPad->Modified();
497 gPad->Update();
498 }
499 const Int_t h = fMinDist->GetMaximumBin();
500 TMarker *m = new TMarker(fIntPhness->GetBinContent(h),
501 fIntGhness->GetBinContent(h), kStar);
502 m->SetMarkerColor(kBlue);
503 m->SetBit(kCanDelete);
504 m->Draw();
505}
Note: See TracBrowser for help on using the repository browser.