I’ve run into a strange situation and I can’t seem to determine what is happening. I am creating two graphs and setting the title and axis titles, then writing them to file without plotting them. When I open the output file and plot them only one has the axis titles set and the other has blank titles. Here is the smallest example I could produce with the issue.
Yes, you have changed the constructor to use dynamic memory. This does resolve the issue, but now I have to manage it and delete every graph at the end of the routine and ensure that the pointer values are cleaned up to avoid a segmentation fault.
Why does using the new operator resolve the issue? I would not have expected a difference between dynamic and static memory.
In addition, if I move my SetTitle calls about the SetPoint calls I no longer get titles even with dynamic memory allocation:
Why does using the new operator resolve the issue?
This is because of a bug/feature of the TGraph copy constructor. It does not copy the underlying histogram and thus the axis information. (And the copy constructor is used when resized an std::vector). You can work-around the issue with:
Yes, the underlying histogram for TGraph is a feature which is there since the very beginning of ROOT. Title and axis titles are hold by this histogram as you can see here. As stated at the beginning of the TGraph reference guide: " TGraph was a light weight object to start with"… I guess that’s why the copy constructor was kept like that. I have to check what is the best solution between modifying the copy constructor or adding a note in the TGraph documentation.