How to properly call the Histogram in a function?

Hi All

I’m trying to make a function that can take a histogram, and return a value of interest.
In a isolated case I made this code

void HDerivative(TString filename)
{
    TFile *f1 = new TFile(filename);
    TH1D* hdiff = (TH1D*)f1->Get("hdiff1");
    TAxis* axis = hdiff -> GetXaxis();
    double BinNum = hdiff->GetNbinsX();
    double BinWidth = axis->GetBinWidth(1);
    TH1D* HDyDx = new TH1D("HDyDx","",BinNum,0,(BinNum-1)*BinWidth);

    int smooth_num = 5;
    int a0 = 0;
    int a1 = 0;
    double b0 = 0;
    double b1 = 0;

    for (int i = 0; i < BinNum; i++)
    {
	for (int j = 0; j < (2*smooth_num)+1; j++)
	{
	    a0 = a0 + hdiff->GetBinContent(i+j-smooth_num);
	    a1 = a1 + hdiff->GetBinContent(i+j-smooth_num+1);

	}
	b0 = a0 / (2*smooth_num+1);
	b1 = a1 / (2*smooth_num+1);
	HDyDx -> SetBinContent(i,b1-b0);
	a0 = 0;
	a1 = 0;
    }

    double    peak	= HDyDx -> GetMaximumBin();
    double_t Flat	= HDyDx->FindFirstBinAbove(0,1,peak+(smooth_num*2+1),-1)*BinWidth;
    cout << peak << endl;
    cout << Flat << endl;
    HDyDx -> Draw();
}

This properly gives out the value of interest “Flat” that I’m looking for.

But now I’m trying to make it part of another piece of code, and it looks like

double_t HDerivative(TH1D* hist)
{
    TH1D* hdiff = (TH1D*)hist->Clone();
    TAxis* axis = hdiff -> GetXaxis();
    // rest of the function
    return Flat;
}

void macro1()
{
    TH1D* hdiff1 = new TH1D("hdiff1","", 1000,0,1e6);
    //fill histogram loop
    double_t Flat = HDerivative(hdiff1);
}

The problem I’m having is that I don’t think doing HDerivative(hdiff1) is properly importing the histogram information into the function, and as a result my return is incorrect. Anyone see where my error is?

I can provide more information if needed.

Of course there is a lot of code missing in your example but, assuming the “fill histogram part” is correct and the “rest of the function” also, your code seems correct. Can you send something we can run reproducing the problem ?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.