Problem with TMath::Median

Dear ROOTers,

I’m trying to use TMath::Median using some code kindly provided by Rene. Median() however always returns either 0 or such values as 2.92501e-301. Please see the attached code below. I call the function with


assuming that histo is a TH1F *.

Thank you,


Double_t MediMean::median1(TH1 *h1) {
   //compute the median for 1-d histogram h1
   Long64_t nbins = h1->GetXaxis()->GetNbins();
   cout << "nbins : " << nbins << endl;
   Double_t *x = new double[nbins];
   Double_t *y = new double[nbins];
   for (Int_t i=0;i<nbins>GetXaxis()->GetBinCenter(i+1);
       y[i] = h1->GetBinContent(i+1);
       cout << "y[" << i << "] : " << y[i] << endl;
   Double_t median = TMath::Median(nbins,x,y);
   delete [] x;
   delete [] y;
   return median;


I’m sorry, but I don’t quite understand what you are trying to do. Where are you filling the array x, the median of which you are trying to find? In TMath::Median(n, x, y, w) first parameter is the array size, second - the array, whose median you want, third - the weights (if you want a weighted median), fourth - a work array to avoid internal memory allocation.

I’d be happy to help, if you provide more info.