# Median of histogram

**URL:** <https://root-forum.cern.ch/t/median-of-histogram/7626>\
**Category:** ROOT\
**Created:** [December 16, 2008, 10:47am UTC](https://root-forum.cern.ch/t/median-of-histogram/7626 "2008-12-16T10:47:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kshaw](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kshaw](https://root-forum.cern.ch/u/kshaw)\
**Post date:** [December 16, 2008, 10:47am UTC](https://root-forum.cern.ch/t/median-of-histogram/7626/1 "2008-12-16T10:47:05Z")

</div>

Hi

is there an easy way of getting the median of a histogram instead of the mean?

Thanks

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [January 8, 2009, 1:36pm UTC](https://root-forum.cern.ch/t/median-of-histogram/7626/2 "2009-01-08T13:36:40Z")

</div>

Hi,

we don’t have currently a function calculating t he mean of an histogram. It could be added in the next release.  
Anyway it is easy to implement, here is function calculating it, given a TH1D

```auto
#include "TH1.h"
#include "TMath.h"
#include <vector>

double Median(const TH1D * h1) { 

   int n = h1->GetXaxis()->GetNbins();  
   std::vector<double> x(n);
   h1->GetXaxis()->GetCenter( &x[0] );
   const double * y = h1->GetArray(); 
   // exclude underflow/overflows from bin content array y
   return TMath::Median(n, &x[0], &y[1]); 
}

```

Best Regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![kshaw](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kshaw](https://root-forum.cern.ch/u/kshaw)\
**Post date:** [January 15, 2009, 12:24pm UTC](https://root-forum.cern.ch/t/median-of-histogram/7626/3 "2009-01-15T12:24:47Z")

</div>

Just for the record this is how I did it in the end:

```auto
 int numBins = histo1->GetXaxis()->GetNbins();
  Double_t *x = new Double_t[numBins];
  Double_t* y = new Double_t[numBins];
  for (int i = 0; i < numBins; i++) {
    x[i] = histo1->GetBinCenter(i);
    y[i] = histo1->GetBinContent(i);
  }
  double MedianOfHisto = TMath::Median(numBins, &x[], &y[]);

```

---

<div class="post-metadata">

**Author:** ![\_Badger](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/_badger/32/21392_2.png) [@\_Badger](https://root-forum.cern.ch/u/_Badger)\
**Post date:** [October 1, 2016, 7:33pm UTC](https://root-forum.cern.ch/t/median-of-histogram/7626/4 "2016-10-01T19:33:42Z")

</div>

Thanks for this kshaw!

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [October 2, 2016, 1:09pm UTC](https://root-forum.cern.ch/t/median-of-histogram/7626/5 "2016-10-02T13:09:23Z")

</div>

The “median” is just the “0.5 quantile”:

```auto
{
  // create, fill and draw the histogram
  TF1 *f = new TF1("f", "TMath::Gaus(x, 5, 1)", 0, 10);
  TH1D *h = new TH1D("h", "h", 100, 0, 10);
  h->FillRandom("f");
  h->Draw();
  // calculate and print the "median"
  Double_t x, q;
  q = 0.5; // 0.5 for "median"
  h->ComputeIntegral(); // just a precaution
  h->GetQuantiles(1, &x, &q);
  std::cout << "median = " << x << std::endl;
}

```
