source: tags/Mars-V2.4/mhist/MHHadronness.cc

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