Calculate the integral of graph plotted from a TTree

Hi Guys,
I’m trying to get the integral of a plot obtained from a tree.
Here my sample code:

//open a file
 TFile *f1 = new TFile("00000.root","READ");
    if ( f1->IsOpen() ) cout << "File opened successfully"  <<endl;

    TTree *t1;
    f1->GetObject("tree;2", t1);
    if(!t1) {
        cerr << "problem in retriving Tree";
        return 1;

    for(int i=0; i<NSEGMENTS; i++){
        t1->Draw("C3_ampl:C3_time>>histC3(100002, -1e-6, 5e-6)","","goff",1,i);  
        TH1F *histC3 = (TH1F*)gDirectory->Get("histC3");
        TAxis *axis = histC3->GetXaxis();
        int bmin = axis->FindBin(-0.05e-6);
        int bmax = axis->FindBin(1e-6);
        double integral = histC3->Integral(bmin,bmax);

In my tree I have 200 (NSEGMENTS = 200) different graph, each one composed by 100002 line. But I get always the same value of integral… so I think that this way is not working. How could I do?

Another strange behaviour is, but it is another question, when I try to Draw histC3

I get always the draw with dots and not with lines (I tried also the option “C” but with no success)

Ok I understood that

create a 2-Dim histogram and this explain why Integral doesn’t work and why histC3->Draw() gives a scatter plot.
Now I need a 1-d histogram or better I need a Tgraph like the ones produced by TTree when I call its Draw function.
From there I’ll can calculate the integral. Someone can help me?

In your Draw formula, is C3_ampl supposed to be the X value for filling the histogram, and C3_time supposed to be the weight?

It’s a bit difficult to help because it looks like your Draw command is trying to do two different things. Your draw command “C3_ampl:C3_time” implies that you are filling either a) a scatter plot, which would normally be done like this:

TGraph * graph = (TGraph *) gROOT->FindObject("graph");
graph = graph.Clone("MyGraph");

Afaik the only way to retrieve the scatter plot is by letting the Draw routine make a TGraph with the default name “graph”, then retrieve it and clone it to give it a new name.

If instead your command looks like this:

tree->Draw("Y:X >> h(nx,a,b,ny,c,d)");
TH2D * h = (TH2D*) gROOT->FindObject("h");

If you give the draw command a name and bin definitions (the nx, a, b etc), then instead of drawing a scatter plot TGraph, it fills a TH2 with the data. In this case you don’t have to clone it to give it a non-default name because you can name it in the draw command.

Now you are doing a draw with “Y:X” but you are only giving one axis worth of bins, so I don’t know what that means. If you want a scatter plot, you can’t give any bin details because it’s unbinned (you also can’t name it and have to make a clone). If you want a TH2, then you need to give two axes worth of bin details.

If you want a TH1, then I don’t know why you are using a “Y:X” command, so I am guessing maybe one of those variables is the weight? In that case, the weight goes into the second argument of TTree::Draw, where you currently have “”:

tree->Draw("X > h(nx,a,b)","W");

This will make a TH1D called h with nx bins from a to b, filled with the values X weighted by W.

I hope that helps. By the way it is likely an expert will point out that you don’t need to Clone the graphs, but that’s the way I do it and it seems to work.


Thanks Jean for your reply.
Sorry, maybe I was not clear in may explanation.
When I Draw the Tree in this manner :


I get a graph of the branch y versus the branch x. Instead when I use


I get, in h2, a TH2D histogram that is conceptually different from the graph that is plotted by this command. And from this I was confused and I did a mistake.

Now I want to calculate the integral of the curve plotted by that command and actually I solved my problem in this manner:
int n = t1->Draw(“C4_ampl:C4_time”,"",“goff”,1,i);
TGraph *graph = new TGraph(n,t1->GetV2(),t1->GetV1());
double compVeloce = graph->Integral(start, stop)

So thank for your reply but I solved my question.
I have to say that in some circumstances root is not very clear

Yes, I think it’s unfortunate that the subtle syntax difference between “X:Y” and “X:Y >> foo” makes the difference between getting an unbinned TGraph and a binned TH2D.

I had a somewhat frustrating conversation with some ROOT experts a few years ago when I ran into this problem. You can read their explanations here: TGraph saved to file shows up as TH2F

I’m glad you resolved your issue.


Frustrating conversation continues here.