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

Last change on this file since 2230 was 2225, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 16.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, 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 return val2x-val1x == 0 ? 0 : val1y - (val2y-val1y)/(val2x-val1x) * (val1x-0.5);
288}
289
290// --------------------------------------------------------------------------
291//
292// Finalize the histograms:
293// - integrate the hadroness histograms --> acceptance
294// - fill the Minimum Distance histogram (formular see class description)
295// - fill the Quality histogram (formular see class description)
296//
297void MHHadronness::CalcGraph(Double_t sumg, Double_t sump)
298{
299 Int_t n = fGhness->GetNbinsX();
300
301 fGraph->Set(n);
302 fQfac->Set(n);
303
304 // Calculate acceptances
305 Float_t max=0;
306
307 for (Int_t i=1; i<=n; i++)
308 {
309 const Stat_t ip = fPhness->Integral(1, i)/sump;
310 const Stat_t ig = fGhness->Integral(1, i)/sumg;
311
312 fIntPhness->SetBinContent(i, ip);
313 fIntGhness->SetBinContent(i, ig);
314
315 fGraph->SetPoint(i, ip, ig);
316
317 if (ip<=0)
318 continue;
319
320 const Double_t val = ig/sqrt(ip);
321 fQfac->SetPoint(i, ig, val);
322
323 if (val>max)
324 max = val;
325 }
326
327 fQfac->SetMaximum(max*1.05);
328}
329
330Bool_t MHHadronness::Finalize()
331{
332 const Stat_t sumg = fGhness->Integral();
333 const Stat_t sump = fPhness->Integral();
334
335 *fLog << inf << "Sum Hadronness: gammas=" << sumg << " hadrons=" << sump << endl;
336
337 // Normalize photon distribution
338 if (sumg>0)
339 fGhness->Scale(1./sumg);
340 else
341 *fLog << warn << "Cannot calculate hadronness for 'gammas'." << endl;
342
343 // Normalize hadron distribution
344 if (sump>0)
345 fPhness->Scale(1./sump);
346 else
347 *fLog << warn << "Cannot calculate hadronness for 'hadrons'." << endl;
348
349 CalcGraph(1, 1);
350
351 return kTRUE;
352}
353
354void MHHadronness::Paint(Option_t *opt)
355{
356 Stat_t sumg = fGhness->Integral();
357 Stat_t sump = fPhness->Integral();
358
359 // Normalize photon distribution
360 if (sumg<=0)
361 sumg=1;
362
363 // Normalize hadron distribution
364 if (sump<=0)
365 sump=1;
366
367 CalcGraph(sumg, sump);
368}
369
370// --------------------------------------------------------------------------
371//
372// Search the corresponding points for the given hadron acceptance (acchad)
373// and interpolate the tow points (linear)
374//
375Double_t MHHadronness::GetGammaAcceptance(Double_t acchad) const
376{
377 const Int_t n = fGraph->GetN();
378 const Double_t *x = fGraph->GetX();
379 const Double_t *y = fGraph->GetY();
380
381 Int_t i = 0;
382 while (i<n && x[i]<acchad)
383 i++;
384
385 if (i==0 || i==n)
386 return 0;
387
388 if (i==n-1)
389 i--;
390
391 const Double_t x1 = x[i-1];
392 const Double_t y1 = y[i-1];
393
394 const Double_t x2 = x[i];
395 const Double_t y2 = y[i];
396
397 return (y2-y1)/(x2-x1) * (acchad-x2) + y2;
398}
399
400// --------------------------------------------------------------------------
401//
402// Search the corresponding points for the given gamma acceptance (accgam)
403// and interpolate the tow points (linear)
404//
405Double_t MHHadronness::GetHadronAcceptance(Double_t accgam) const
406{
407 const Int_t n = fGraph->GetN();
408 const Double_t *x = fGraph->GetX();
409 const Double_t *y = fGraph->GetY();
410
411 Int_t i = 0;
412 while (i<n && y[i]<accgam)
413 i++;
414
415 if (i==0 || i==n)
416 return 0;
417
418 if (i==n-1)
419 i--;
420
421 const Double_t x1 = y[i-1];
422 const Double_t y1 = x[i-1];
423
424 const Double_t x2 = y[i];
425 const Double_t y2 = x[i];
426
427 return (y2-y1)/(x2-x1) * (accgam-x2) + y2;
428}
429
430// --------------------------------------------------------------------------
431//
432// Print the corresponding Gammas Acceptance for a hadron acceptance of
433// 10%, 20%, 30%, 40% and 50%. Also the minimum distance to the optimum
434// acceptance and the corresponding acceptances and hadroness value is
435// printed, together with the maximum Q-factor.
436//
437void MHHadronness::Print(Option_t *) const
438{
439 *fLog << all;
440 *fLog << "Hadronness histograms:" << endl;
441 *fLog << "---------------------" << endl;
442
443 if (fGraph->GetN()==0)
444 {
445 *fLog << " <No Entries>" << endl;
446 return;
447 }
448
449 *fLog << "Used " << fGhness->GetEntries() << " Gammas and " << fPhness->GetEntries() << " Hadrons." << endl;
450 *fLog << "acc(hadron) acc(gamma) acc(g)/acc(h)" << endl <<endl;
451
452 *fLog << " 0.005 " << Form("%6.3f", GetGammaAcceptance(0.005)) << " " << Form("%6.3f", GetGammaAcceptance(0.005)/0.005) << endl;
453 *fLog << " 0.02 " << Form("%6.3f", GetGammaAcceptance(0.02)) << " " << Form("%6.3f", GetGammaAcceptance(0.02)/0.02) << endl;
454 *fLog << " 0.05 " << Form("%6.3f", GetGammaAcceptance(0.05)) << " " << Form("%6.3f", GetGammaAcceptance(0.05)/0.05) << endl;
455 *fLog << " 0.1 " << Form("%6.3f", GetGammaAcceptance(0.1 )) << " " << Form("%6.3f", GetGammaAcceptance(0.1)/0.1) << endl;
456 *fLog << " 0.2 " << Form("%6.3f", GetGammaAcceptance(0.2 )) << " " << Form("%6.3f", GetGammaAcceptance(0.2)/0.2) << endl;
457 *fLog << " 0.3 " << Form("%6.3f", GetGammaAcceptance(0.3 )) << " " << Form("%6.3f", GetGammaAcceptance(0.3)/0.3) << endl;
458 *fLog << Form("%6.3f", GetHadronAcceptance(0.1)) << " 0.1 " << endl;
459 *fLog << Form("%6.3f", GetHadronAcceptance(0.2)) << " 0.2 " << endl;
460 *fLog << Form("%6.3f", GetHadronAcceptance(0.3)) << " 0.3 " << endl;
461 *fLog << Form("%6.3f", GetHadronAcceptance(0.4)) << " 0.4 " << endl;
462 *fLog << Form("%6.3f", GetHadronAcceptance(0.5)) << " 0.5 " << endl;
463 *fLog << Form("%6.3f", GetHadronAcceptance(0.6)) << " 0.6 " << endl;
464 *fLog << Form("%6.3f", GetHadronAcceptance(0.7)) << " 0.7 " << endl;
465 *fLog << Form("%6.3f", GetHadronAcceptance(0.8)) << " 0.8 " << endl;
466 *fLog << endl;
467
468 *fLog << "Q-Factor @ Acc Gammas=0.5: Q(0.5)=" << Form("%.1f", GetQ05()) << endl;
469 *fLog << " Acc Hadrons = " << Form("%5.1f", GetHadronAcceptance(0.5)*100) << "%" << endl;
470 *fLog << endl;
471}
472
473// --------------------------------------------------------------------------
474//
475// Draw all histograms. (For the Meaning see class description)
476//
477void MHHadronness::Draw(Option_t *)
478{
479 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas("Hadronness", fTitle);
480 pad->SetBorderMode(0);
481
482 AppendPad("");
483
484 pad->Divide(2, 2);
485
486 TH1 *h;
487
488 pad->cd(1);
489 gPad->SetBorderMode(0);
490 //gStyle->SetOptStat(10);
491 MH::Draw(*fGhness, *fPhness, "Hadronness"); // Displ both stat boxes
492
493 pad->cd(2);
494 gPad->SetBorderMode(0);
495 fIntGhness->Draw();
496 fIntPhness->Draw("same");
497
498 pad->cd(3);
499 gPad->SetBorderMode(0);
500 fQfac->Draw("A*");
501 gPad->Modified();
502 gPad->Update();
503 if ((h=fQfac->GetHistogram()))
504 {
505 h->GetXaxis()->SetRangeUser(0, 1);
506 h->SetXTitle("Acceptance Gammas");
507 h->SetYTitle("Quality");
508 fQfac->Draw("P");
509 gPad->Modified();
510 gPad->Update();
511 }
512
513 pad->cd(4);
514 gPad->SetBorderMode(0);
515 fGraph->Draw("AC");
516 gPad->Modified();
517 gPad->Update();
518 if ((h=fGraph->GetHistogram()))
519 {
520 h->GetXaxis()->SetRangeUser(0, 1);
521 h->SetXTitle("Acceptance Hadrons");
522 h->SetYTitle("Acceptance Gammas");
523 fGraph->SetMaximum(1);
524 fGraph->Draw("P");
525 gPad->Modified();
526 gPad->Update();
527 }
528}
529
530// --------------------------------------------------------------------------
531//
532// You can use this function if you want to use a MHMatrix instead of
533// MMcEvt. This function adds all necessary columns to the
534// given matrix. Afterward you should fill the matrix with the corresponding
535// data (eg from a file by using MHMatrix::Fill). If you now loop
536// through the matrix (eg using MMatrixLoop) MHHadronness::Fill
537// will take the values from the matrix instead of the containers.
538//
539void MHHadronness::InitMapping(MHMatrix *mat)
540{
541 if (fMatrix)
542 return;
543
544 fMatrix = mat;
545 fMap = fMatrix->AddColumn("MMcEvt.fPartId");
546}
547
548void MHHadronness::StopMapping()
549{
550 fMatrix = NULL;
551}
552
Note: See TracBrowser for help on using the repository browser.