Numerical integration from a TH1F, GSLIntegrator

Dear Rooters,
I have some data in a tree. Let admit I have a branch with peculiar values of “Time”.

  1. I fill an THF1 histogram with Time. For one Time value t_0, I would integrate the histogram for t>t_0, then iterate it for each t_i values for t_i > t_0. See my code :
#include <TH1F.h>
#include <TTree.h>
#include <TGraph.h>

using namespace std;

void root_extract(){

 TFile *file = TFile::Open("file_name");
 TTree *tree = (TTree*)file->Get("tree_name");
 Double_t time;

 TH1F *htime = new TH1F("h_time","Time histogram",nbins,xmin_time,xmax_time);
 TH1F *hintegration = new TH1F("h_integration","Integration",nbins,xmin_integration,xmax_integration);

 for (Long64_t i=0;i<nbins;i++) {
    Double_t TimeBinContent = htime->GetBinContent(i);
    Double_t TimeXValue = htime->GetXaxis()->GetBinCenter(i);
    Int_t bin = htime->GetXaxis()->FindBin(i);
    TAxis *axis = htime->GetXaxis();
    Int_t bmin = axis->FindBin(TimeXValue);
    Int_t bmax = axis->FindBin(xmax_time);
    Double_t integral = (htime->Integral(bmin,bmax));

I think this is working. But I’m not totally sure of what I am exactly doing, because I do not take into account the TimeBinContent of each bin : should I do
double integral = htime->Integral(bmin,bmax))*TimeBinContent; ?

  1. Filling an histogram with the TTree Time values lead to a lost of information, cause of the binning. Does it exist a way of doing this integration without filling a tree ? I mean, directly working with the TTree data tree->GetEntry(i) ? Maybe something with GSLIntegrator ?

Thanks for the reading, hoping this is all clear.

ROOT Version 6.08/06

I have no idea what you are trying to achieve but, if you want the sum of the bin contents multiplied by the bin width in x, try:

double integral = htime->Integral(bmin, bmax, "width");

thanks for the reply, it worked.

But for the second part of my question, I really would like to do this integration without going through the histogram. Do you think it is possible ?


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