Re: [ROOT] TGraph and vector

From: Colin Bernet (bernet@hep.saclay.cea.fr)
Date: Tue Jan 08 2002 - 16:53:24 MET


Hi Rene,

> 
> What do you mean by "compiled with STL" ?
> 
> If your class includes STL collections, ROOT can process this class.
> rootcint can generate the code for I/O for these classes.
> Classes containing STL collections can also be put in ROOT Trees.

I guess Michael is looking for a way to store the values of x and y in
vectors instead of arrays of floats. then he doesn't have to care about
the array size when filling them, and if the number of points he has is
bigger than the size of the container, new memory is allocated.

What he wants is probably something like :
TGraph::TGraph(const vector<float>& x, const vector<float>&y)

Then the class TGraph has to know vector...

> 
> However, ROOT Trees with classes containing STL code need the original
> library with the classes to read the objects.
> 
> We hope to implement one day the possibility to automatically split
> a member like vector<MyClass> as we do today for a TClonesArray.
> 
> Concerning your statement with:
>     float *x = malloc(100);
> this should be totally prohibited. Always use new instead.
> Mixing malloc and new is a big source of troubles

there is no realloc equivalent in C++. If you want to do that without
using STL containers, which is
the case here, I couldn't figure out something else than using malloc,
realloc,
AND free.

If you do the allocation, reallocation and free in the same function,
there should be no problem.

cheers,
Colin

> 
> Rene Brun
> 
> 
> > root classes don't know about it. I also think it would be pretty
> > useful to have TTree/Branch classes know about the vector container.
> > 
> > Anyway, even if you don't know how many points you have in your file, or
> > how many points you want, you can reallocate memory for your arrays
> > manually :
> > 
> > float *x = malloc(100);
> > 
> > //and if you read more than 100 points :
> > realloc(x,200);
> > 
> > // now you have 100 more slots in your array. but the previous data are
> > // sill there
> > 
> > that's basically what vector is doing : it allocates a given (can be
> > specified) amount of memory, and doubles it if you go over this limit.
> > Doubling the allocated memory is more efficient, cause memory
> > allocation costs time.
> > Keep track of the size of your array so you can reallocate afterwards
> > again if needed.
> > 
> > Happy new year !
> > 
> > Colin
> > 
> > cbernet@cea.fr
> > 
> > > Dear Rooters!
> > >
> > > I just remember vaguely that there was somethin behing root and not
> > > using the stl container classes, but do not remember anymore ..
> > >
> > > 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?
> > >
> > >
> > > Cheers
> > > Michael
> > >
> 



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