Adding individual points to TGraph

Hello all.

I am writing analysis code for an arbitrary number of events, and want to add a point to a graph for each event. The analyze() method runs once per event, and is the method in which I fill my various histograms, which I can add one entry to at a time. However it doesn’t appear that there is a good way to add individual points to my graph - it seems to want me to collate all of my data points and then add them at the end, which would require messing around with TVectorD objects (which I would rather avoid) or arrays (which I can’t initialise to size without knowing the number of events in advance of compilation).

I have tried using the TGraph::set(n) and TGraph::setPoint(i,x,y) methods to resize the graph and add the new entry on each call of analyze(), but it seemed to do nothing, as I got no graph at the end of it, as well as it being rather clunky. Is there a better way that will actually work? Perhaps I am missing a method for TGraph equivalent to TH1x::Fill(x) ? Many thanks.

The TGraph::SetPoint method does exactly that i.e.: " Adding individual points to TGraph"
Do you have a small example showing what does not work according to you ?

I was under the impression that the graph had a finite size as given in the constructor - TGraph::TGraph(n, *x, *y), where n is the size. What would happen if I try to do SetPoint(N+1, x, y) for a graph defined to have a size of N?

Unfortunately I don’t have the code I used before now - but what I was doing was something like:

TGraph g = new TGraph(); // using the blank constructor
analyze(Event e) {
  g->set(g->getN()+1); // increase size by one
  g->setPoint(g->getN()-1, x, y); // add entry to the final entry (initialised to 0 by TGraph::set())
g->Draw(); // run once after all events analyzed

where analyze(Event e) is called once per event.

1 Like

The point will be added … no problem …

You can even add N+10 … and the points between N+1 and N+9 will be set to (0,0)

1 Like

Right - that makes things a lot easier than I thought then. Thanks very much!

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