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

Last change on this file since 2461 was 2414, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 16.9 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-2003
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// * Bottom Left Corner:
48// Naive quality factor: Ag/sqrt(Ah)
49// * Bottom Right Corner:
50// - black: Acceprtance Gammas vs. Acceptance Hadrons
51// - blue cross: minimum of distance to (0, 1)
52//
53// As a default MHHadronness searches for the container "MHadronness".
54// This can be overwritten by a different pointer specified in the
55// Fill function (No type check is done!) Use a different name in
56// MFillH.
57//
58// If you are using filtercuts which gives you only two discrete values
59// of the hadronness (0.25 and 0.75) you may want to use MHHadronness(2).
60// If you request Q05() from such a MHHadronness instance you will get
61// Acc_g/sqrt(Acc_h)
62//
63////////////////////////////////////////////////////////////////////////////
64#include "MHHadronness.h"
65
66#include <TPad.h>
67#include <TGraph.h>
68#include <TStyle.h>
69#include <TCanvas.h>
70#include <TMarker.h>
71
72#include "MParList.h"
73#include "MBinning.h"
74#include "MHMatrix.h"
75#include "MHadronness.h"
76
77#include "MLog.h"
78#include "MLogManip.h"
79
80#include "MMcEvt.hxx"
81
82ClassImp(MHHadronness);
83
84using namespace std;
85
86// --------------------------------------------------------------------------
87//
88// Setup histograms, nbins is the number of bins used for the evaluation.
89// The default is 100 bins.
90//
91MHHadronness::MHHadronness(Int_t nbins, const char *name, const char *title)
92 : fMatrix(NULL)
93{
94 //
95 // set the name and title of this object
96 //
97 fName = name ? name : "MHHadronness";
98 fTitle = title ? title : "Gamma/Hadron Separation Quality Histograms";
99
100 fGraph = new TGraph;
101 fGraph->SetTitle("Acceptance Gammas vs. Hadrons");
102 fGraph->SetMarkerStyle(kFullDotSmall);
103
104 fGhness = new TH1D("Ghness", "Acceptance vs. Hadronness (Gammas)", nbins, 0, 1);
105 fPhness = new TH1D("Phness", "Acceptance vs. Hadronness (Hadrons)", nbins, 0, 1);
106 fGhness->SetXTitle("Hadronness");
107 fPhness->SetXTitle("Hadronness");
108 fGhness->SetYTitle("Acceptance");
109 fPhness->SetYTitle("Acceptance");
110 fPhness->SetLineColor(kRed);
111 fGhness->SetDirectory(NULL);
112 fPhness->SetDirectory(NULL);
113
114 fIntGhness = new TH1D("AccGammas", "Integral Acceptance vs. Hadronness (Gammas)", nbins, 0, 1);
115 fIntPhness = new TH1D("AccHadrons", "Integral Acceptance vs. Hadronness (Hadrons)", nbins, 0, 1);
116 fIntGhness->SetXTitle("Hadronness");
117 fIntPhness->SetXTitle("Hadronness");
118 fIntGhness->SetYTitle("Acceptance");
119 fIntPhness->SetYTitle("Acceptance");
120 fIntGhness->SetMaximum(1.1);
121 fIntPhness->SetMaximum(1.1);
122 fIntGhness->SetDirectory(NULL);
123 fIntPhness->SetDirectory(NULL);
124 fIntPhness->SetLineColor(kRed);
125 fIntGhness->SetBit(TH1::kNoStats);
126 fIntPhness->SetBit(TH1::kNoStats);
127
128 fQfac = new TGraph;
129 fQfac->SetTitle(" Naive Quality factor ");
130 fQfac->SetMarkerStyle(kFullDotSmall);
131}
132
133// --------------------------------------------------------------------------
134//
135// Delete the histograms.
136//
137MHHadronness::~MHHadronness()
138{
139 delete fGhness;
140 delete fIntGhness;
141 delete fPhness;
142 delete fIntPhness;
143 delete fQfac;
144 delete fGraph;
145}
146
147// --------------------------------------------------------------------------
148//
149// Setup Filling of the histograms. It needs:
150// MMcEvt and MHadronness
151//
152Bool_t MHHadronness::SetupFill(const MParList *plist)
153{
154 if (!fMatrix)
155 {
156 fMcEvt = (MMcEvt*)plist->FindObject("MMcEvt");
157 if (!fMcEvt)
158 {
159 *fLog << err << dbginf << "MMcEvt not found... aborting." << endl;
160 return kFALSE;
161 }
162 }
163
164 fHadronness = (MHadronness*)plist->FindObject("MHadronness");
165
166 fGhness->Reset();
167 fPhness->Reset();
168
169 /*
170 MBinning* bins = (MBinning*)plist->FindObject("BinningHadronness");
171 if (!bins)
172 {
173 *fLog << err << dbginf << "BinningHadronness [MBinning] not found... aborting." << endl;
174 return kFALSE;
175 }
176
177 SetBinning(&fHist, binsalpha, binsenergy, binstheta);
178
179 fHist.Sumw2();
180 */
181
182 return kTRUE;
183}
184
185// --------------------------------------------------------------------------
186//
187// Fill the Hadronness from a MHadronness container into the corresponding
188// histogram dependant on the particle id.
189//
190// Every particle Id different than kGAMMA is considered a hadron.
191//
192// If you call Fill with a pointer (eg. from MFillH) this container is
193// used as a hadronness (Warning: No type check is done!) otherwise
194// the default is used ("MHadronness")
195//
196// Sometimes a distance is calculated as NaN (not a number). Such events
197// are skipped at the moment.
198//
199Bool_t MHHadronness::Fill(const MParContainer *par, const Stat_t w)
200{
201 // Preliminary Workaround: FIXME!
202 if (!par && !fHadronness)
203 {
204 *fLog << err << "MHHadronness::Fill: No MHadronness container specified!" << endl;
205 return kFALSE;
206 }
207
208 const MHadronness &had = par ? *(MHadronness*)par : *fHadronness;
209
210 const Double_t h = had.GetHadronness();
211
212 if (TMath::IsNaN(h))
213 return kCONTINUE;
214
215 const Int_t particleid = fMatrix ? (Int_t)(*fMatrix)[fMap] : fMcEvt->GetPartId();
216
217 if (particleid==kGAMMA)
218 fGhness->Fill(h, w);
219 else
220 fPhness->Fill(h, w);
221
222 return kTRUE;
223}
224
225// --------------------------------------------------------------------------
226//
227// Returns the quality factor at gamma acceptance 0.5.
228//
229// If the histogram containes only two bins we return the
230// naive quality: ag/sqrt(ah)
231// with ag the acceptance of gammas and ah the acceptance of hadrons.
232//
233// You can use this (nbins=2) in case of some kind of filter cuts giving
234// only a result: gamma yes/no (means discrete values of hadronness 0.25
235// or 0.75)
236//
237// FIXME: In the later case weights cannot be used!
238//
239Float_t MHHadronness::GetQ05() const
240{
241 if (fGhness->GetNbinsX()==2)
242 {
243 // acceptance of all gamma-like gammas (h<0.5)
244 const Double_t ig = fGhness->GetBinContent(1);
245
246 // acceptance of all gamma-like hadrons (h<0.5)
247 const Double_t ip = fPhness->GetBinContent(1);
248
249 if (ip==0)
250 return 0; // FIXME!
251
252 // naive quality factor
253 const Double_t q = ig / sqrt(ip);
254
255 *fLog << all << ip << "/" << ig << ": " << q << endl;
256
257 return q;
258 }
259
260 const Int_t n = fQfac->GetN();
261
262 Double_t val1x=0;
263 Double_t val2x=1;
264
265 Double_t val1y=0;
266 Double_t val2y=0;
267
268 for (Int_t i=1; i<=n; i++)
269 {
270 Double_t x, y;
271
272 fQfac->GetPoint(i, x, y);
273
274 if (x<0.5 && x>val1x)
275 {
276 val1x = x;
277 val1y = y;
278 }
279
280 if (x>0.5 && x<val2x)
281 {
282 val2x = x;
283 val2y = y;
284 }
285 }
286
287 //*fLog << dbg << val1x << "/" << val1y << " " << val2x << "/" << val2y << endl;
288
289 return val2x-val1x == 0 ? 0 : val1y - (val2y-val1y)/(val2x-val1x) * (val1x-0.5);
290}
291
292// --------------------------------------------------------------------------
293//
294// Finalize the histograms:
295// - integrate the hadroness histograms --> acceptance
296// - fill the Minimum Distance histogram (formular see class description)
297// - fill the Quality histogram (formular see class description)
298//
299void MHHadronness::CalcGraph(Double_t sumg, Double_t sump)
300{
301 Int_t n = fGhness->GetNbinsX();
302
303 fGraph->Set(n);
304 fQfac->Set(n);
305
306 // Calculate acceptances
307 Float_t max=0;
308
309 for (Int_t i=1; i<=n; i++)
310 {
311 const Stat_t ip = fPhness->Integral(1, i)/sump;
312 const Stat_t ig = fGhness->Integral(1, i)/sumg;
313
314 fIntPhness->SetBinContent(i, ip);
315 fIntGhness->SetBinContent(i, ig);
316
317 fGraph->SetPoint(i, ip, ig);
318
319 if (ip<=0)
320 continue;
321
322 const Double_t val = ig/sqrt(ip);
323 fQfac->SetPoint(i, ig, val);
324
325 if (val>max)
326 max = val;
327 }
328
329 fQfac->SetMaximum(max*1.05);
330}
331
332Bool_t MHHadronness::Finalize()
333{
334 const Stat_t sumg = fGhness->Integral();
335 const Stat_t sump = fPhness->Integral();
336
337 *fLog << inf << "Sum Hadronness: gammas=" << sumg << " hadrons=" << sump << endl;
338
339 // Normalize photon distribution
340 if (sumg>0)
341 fGhness->Scale(1./sumg);
342 else
343 *fLog << warn << "Cannot calculate hadronness for 'gammas'." << endl;
344
345 // Normalize hadron distribution
346 if (sump>0)
347 fPhness->Scale(1./sump);
348 else
349 *fLog << warn << "Cannot calculate hadronness for 'hadrons'." << endl;
350
351 CalcGraph(1, 1);
352
353 return kTRUE;
354}
355
356void MHHadronness::Paint(Option_t *opt)
357{
358 Stat_t sumg = fGhness->Integral();
359 Stat_t sump = fPhness->Integral();
360
361 // Normalize photon distribution
362 if (sumg<=0)
363 sumg=1;
364
365 // Normalize hadron distribution
366 if (sump<=0)
367 sump=1;
368
369 CalcGraph(sumg, sump);
370}
371
372// --------------------------------------------------------------------------
373//
374// Search the corresponding points for the given hadron acceptance (acchad)
375// and interpolate the tow points (linear)
376//
377Double_t MHHadronness::GetGammaAcceptance(Double_t acchad) const
378{
379 const Int_t n = fGraph->GetN();
380 const Double_t *x = fGraph->GetX();
381 const Double_t *y = fGraph->GetY();
382
383 Int_t i = 0;
384 while (i<n && x[i]<acchad)
385 i++;
386
387 if (i==0 || i==n)
388 return 0;
389
390 if (i==n-1)
391 i--;
392
393 const Double_t x1 = x[i-1];
394 const Double_t y1 = y[i-1];
395
396 const Double_t x2 = x[i];
397 const Double_t y2 = y[i];
398
399 return (y2-y1)/(x2-x1) * (acchad-x2) + y2;
400}
401
402// --------------------------------------------------------------------------
403//
404// Search the corresponding points for the given gamma acceptance (accgam)
405// and interpolate the tow points (linear)
406//
407Double_t MHHadronness::GetHadronAcceptance(Double_t accgam) const
408{
409 const Int_t n = fGraph->GetN();
410 const Double_t *x = fGraph->GetX();
411 const Double_t *y = fGraph->GetY();
412
413 Int_t i = 0;
414 while (i<n && y[i]<accgam)
415 i++;
416
417 if (i==0 || i==n)
418 return 0;
419
420 if (i==n-1)
421 i--;
422
423 const Double_t x1 = y[i-1];
424 const Double_t y1 = x[i-1];
425
426 const Double_t x2 = y[i];
427 const Double_t y2 = x[i];
428
429 return (y2-y1)/(x2-x1) * (accgam-x2) + y2;
430}
431
432// --------------------------------------------------------------------------
433//
434// Search the hadronness corresponding to a given hadron acceptance.
435//
436Double_t MHHadronness::GetHadronness(Double_t acchad) const
437{
438 for (int i=1; i<fIntPhness->GetNbinsX()+1; i++)
439 if (fIntPhness->GetBinContent(i)>acchad)
440 return fIntPhness->GetBinLowEdge(i);
441
442 return -1;
443}
444
445// --------------------------------------------------------------------------
446//
447// Print the corresponding Gammas Acceptance for a hadron acceptance of
448// 10%, 20%, 30%, 40% and 50%. Also the minimum distance to the optimum
449// acceptance and the corresponding acceptances and hadroness value is
450// printed, together with the maximum Q-factor.
451//
452void MHHadronness::Print(Option_t *) const
453{
454 *fLog << all;
455 *fLog << underline << GetDescriptor() << endl;
456
457 if (fGraph->GetN()==0)
458 {
459 *fLog << " <No Entries>" << endl;
460 return;
461 }
462
463 *fLog << "Used " << fGhness->GetEntries() << " Gammas and " << fPhness->GetEntries() << " Hadrons." << endl;
464 *fLog << "acc(hadron) acc(gamma) acc(g)/acc(h) h" << endl <<endl;
465
466 *fLog << " 0.005 " << Form("%6.3f", GetGammaAcceptance(0.005)) << " " << Form("%6.3f", GetGammaAcceptance(0.005)/0.005) << " " << GetHadronness(0.005) << endl;
467 *fLog << " 0.02 " << Form("%6.3f", GetGammaAcceptance(0.02)) << " " << Form("%6.3f", GetGammaAcceptance(0.02)/0.02) << " " << GetHadronness(0.02) << endl;
468 *fLog << " 0.05 " << Form("%6.3f", GetGammaAcceptance(0.05)) << " " << Form("%6.3f", GetGammaAcceptance(0.05)/0.05) << " " << GetHadronness(0.05) << endl;
469 *fLog << " 0.1 " << Form("%6.3f", GetGammaAcceptance(0.1 )) << " " << Form("%6.3f", GetGammaAcceptance(0.1)/0.1) << " " << GetHadronness(0.1) << endl;
470 *fLog << " 0.2 " << Form("%6.3f", GetGammaAcceptance(0.2 )) << " " << Form("%6.3f", GetGammaAcceptance(0.2)/0.2) << " " << GetHadronness(0.2) << endl;
471 *fLog << " 0.3 " << Form("%6.3f", GetGammaAcceptance(0.3 )) << " " << Form("%6.3f", GetGammaAcceptance(0.3)/0.3) << " " << GetHadronness(0.3) << endl;
472 *fLog << Form("%6.3f", GetHadronAcceptance(0.1)) << " 0.1 " << endl;
473 *fLog << Form("%6.3f", GetHadronAcceptance(0.2)) << " 0.2 " << endl;
474 *fLog << Form("%6.3f", GetHadronAcceptance(0.3)) << " 0.3 " << endl;
475 *fLog << Form("%6.3f", GetHadronAcceptance(0.4)) << " 0.4 " << endl;
476 *fLog << Form("%6.3f", GetHadronAcceptance(0.5)) << " 0.5 " << endl;
477 *fLog << Form("%6.3f", GetHadronAcceptance(0.6)) << " 0.6 " << endl;
478 *fLog << Form("%6.3f", GetHadronAcceptance(0.7)) << " 0.7 " << endl;
479 *fLog << Form("%6.3f", GetHadronAcceptance(0.8)) << " 0.8 " << endl;
480 *fLog << endl;
481
482 *fLog << "Q-Factor @ Acc Gammas=0.5: Q(0.5)=" << Form("%.1f", GetQ05()) << endl;
483 *fLog << " Acc Hadrons = " << Form("%5.1f", GetHadronAcceptance(0.5)*100) << "%" << endl;
484 *fLog << endl;
485}
486
487// --------------------------------------------------------------------------
488//
489// Draw all histograms. (For the Meaning see class description)
490//
491void MHHadronness::Draw(Option_t *)
492{
493 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas("Hadronness", fTitle);
494 pad->SetBorderMode(0);
495
496 AppendPad("");
497
498 pad->Divide(2, 2);
499
500 TH1 *h;
501
502 pad->cd(1);
503 gPad->SetBorderMode(0);
504 //gStyle->SetOptStat(10);
505 MH::DrawSame(*fGhness, *fPhness, "Hadronness"); // Displ both stat boxes
506
507 pad->cd(2);
508 gPad->SetBorderMode(0);
509 gPad->SetGridx();
510 gPad->SetGridy();
511 fIntGhness->Draw();
512 fIntPhness->Draw("same");
513
514 pad->cd(3);
515 gPad->SetBorderMode(0);
516 gPad->SetGridx();
517 gPad->SetGridy();
518 fQfac->Draw("A*");
519 gPad->Modified();
520 gPad->Update();
521 if ((h=fQfac->GetHistogram()))
522 {
523 h->GetXaxis()->SetRangeUser(0, 1);
524 h->SetXTitle("Acceptance Gammas");
525 h->SetYTitle("Quality");
526 fQfac->Draw("P");
527 gPad->Modified();
528 gPad->Update();
529 }
530
531 pad->cd(4);
532 gPad->SetBorderMode(0);
533 gPad->SetGridx();
534 gPad->SetGridy();
535 fGraph->Draw("AC");
536 gPad->Modified();
537 gPad->Update();
538 if ((h=fGraph->GetHistogram()))
539 {
540 h->GetXaxis()->SetRangeUser(0, 1);
541 h->SetXTitle("Acceptance Hadrons");
542 h->SetYTitle("Acceptance Gammas");
543 fGraph->SetMaximum(1);
544 fGraph->Draw("P");
545 gPad->Modified();
546 gPad->Update();
547 }
548}
549
550// --------------------------------------------------------------------------
551//
552// You can use this function if you want to use a MHMatrix instead of
553// MMcEvt. This function adds all necessary columns to the
554// given matrix. Afterward you should fill the matrix with the corresponding
555// data (eg from a file by using MHMatrix::Fill). If you now loop
556// through the matrix (eg using MMatrixLoop) MHHadronness::Fill
557// will take the values from the matrix instead of the containers.
558//
559void MHHadronness::InitMapping(MHMatrix *mat)
560{
561 if (fMatrix)
562 return;
563
564 fMatrix = mat;
565 fMap = fMatrix->AddColumn("MMcEvt.fPartId");
566}
567
568void MHHadronness::StopMapping()
569{
570 fMatrix = NULL;
571}
572
Note: See TracBrowser for help on using the repository browser.