Re: [ROOT] TGraph and vector

From: Alexandr Malusek (Alexandr.Malusek@imv.liu.se)
Date: Tue Jan 08 2002 - 19:33:39 MET


Michael Wiesmann <wiesmann@e18.physik.tu-muenchen.de> writes:

> At the moment I want to draw some TGraphErrors and read the data from
> file, number of points not necessarily known. Using a stl vector<>
> would be very convenient, but TGraph prefers to have double* or float*
> ...  Is there any elegant OO way to cope with this problem?

STL vector<> can be used in compiled code. (I've been using it with
ROOT on Linux for a long time and haven't seen any problems).

I'm not an expert in C++ so I'll cite "The C++ Standard Library" book
(1999):

: The C++ standard library does not state clearly whether the elements
: of a vector are required to be in contiguous memory. However, it is
: the intention that this is guaranteed and it will be fixed due to a
: defect report.

So the following code should be OK:

$ cat gr.C
#include <vector>
#include "TGraph.h"

void gr();

void gr()
{
  std::vector<Double_t> x;
  std::vector<Double_t> y;
  Int_t dim = 32;

  for (Int_t i = 0; i < dim; i++)
    {
      x.push_back(i);
      y.push_back(i*i);
    }

  TGraph *g = new TGraph(dim, &x[0], &y[0]);
  g->Draw("AL");
}

root [0] .X gr.C++
Creating shared library /home/malusek/test/vec/./gr_C.so
<TCanvas::MakeDefCanvas>: created default TCanvas with name c1
root [1]

The graph is plotted OK.

$ g++ --version
2.95.3

--
Alexandr.Malusek@imv.liu.se



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:37 MET