Storing canvas in ROOT file

Dear ROOT team,

I encountered an unexpected feature when saving a Canvas to a ROOT file. I tried the following with ROOT 5.28/00 and 5.30.

I create a simple histogram, set line and fill attributes and plot it in a canvas. Then I write this canvas to a root file. If I open the ROOT file later and retrieve the canvas, all information about the histogram line and fill colors are lost.

Is this behaviour expected? I attached a short macro repoducing this issue.

Cheers
Christian

Canvas.C (251 Bytes)

  1. I ran you macro
  2. save the canvas in a .root file
  3. exit root
  4. run “root -n”
  5. draw the canvas from the root file …

and I get the save picture as the one I got when I ran your macro first.
I am using the ROOT trunk

Hi,

I have also been having trouble saving histogram attributes within a canvas to a root file. I am using this version of root on lxplus from afs:

ROOT 5.32/00 (tags/v5-32-00@42375, Dec 02 2011, 12:42:25 on linuxx8664gcc)

Anyways I have traced my problem to my rootrc and the rootlogon.C file that I tell it to load. After, for me, ATLAS style is loaded and enforced via my rootlogin.C, then all my histograms will look the same in any canvas that is stored to a root file. However histograms contained within a THStack, will retain their attributes when the canvas is saved to the root file. Easiest way to reproduce what I am talking about:

{
gROOT->SetStyle("Plain");
gROOT->ForceStyle();
}

have the above loaded via your .rootrc:

Rint.Logon: ~\blah.C

then just run the earlier attachment from the beginning of the thread.

So why would c1->SaveAs(“c.png”) preserve the attributes I gave to the histogram, while c1->SaveAs(“c.root”) does not. Also why is the .rootrc loaded in compiled code? that is with an
int main(){} w/o instantiation of a TRint/TApplication.

Thanks,
Justin

[quote]Also why is the .rootrc loaded in compiled code? … that is with an
int main(){} w/o instantiation of a TRint/TApplication. [/quote]This is a feature. Even when ROOT is not configured explicitly, a TROOT object is created which loads the .rootrc. However without a TRint object, the rootlogon will not be executed.

[quote]However histograms contained within a THStack, will retain their attributes when the canvas is saved to the root file. [/quote]I am not sure I understand what you mean. By definition all object should stored their state as is when being written to a root file (anything else would be a bug, wouldn’t it?). Similarly, the png is a snapshot of exactly how the objects were configured (and in the case of the png, it can then only be updated via a graphic editor).

Could you be more explicit in the step you take and how the result differ from what you expect?

Cheers,
Philippe.

Hi Philippe,

Ok I see what is going on now. The attributes are saved properly in the root file, however, when I just load up root again, the current style defined by my rootlogon.C takes over and potentially strips the colors away. This can be a little shocking when you are attempting to overlay a few 2d scatter plots and am wondering why the root canvas doesn’t show the different marker colors/styles that you expected, but the eps does.

root canvasfile.root
[1] gROOT->ForceStyle(0)
[2] can->Draw()

seems to do what I expect now.

However histograms contained within a THStack, will retain their attributes when the canvas is saved to the root file.

I mean that what I say above about the rootlogon.C taking over and changing the histogram attributes in memory in the root session after ones macro is finished, does not seem to apply to histograms within a stack. This is the point that got me stuck for awhile, most of my canvases are stacks with a data histogram and a stack of MC. So some of my scirpts seemed to save the root canvas as I would expect, some would not. The difference was the use of THStack. I have attached a main program that illustrates the problem/feature by filling a random histogram h. Set the fill color to be red. Draw the histogram, save the canvas to a file. Then add this histogram to a THStack. Draw the THStack by itself on another canvas. Save the canvas to the root file. Close the root file. Open, draw the two canvases in an interactive root session through TRint. If you have a trivial rootlogon.C such as:

{
gROOT->SetStyle(“Plain”);
gROOT->ForceStyle();
}

Then you will see the embedded histogram w/i the stack retains its color while the original histogram does not.

Cheers,
Justin

p.s. of course you are right about the rootlogon.C not being loaded in an int main(), I was just confused.
canvas.cxx (842 Bytes)

Yes. that’s how it should work.

Hi Couet,

I agree this is how it should work for THStacks. However, perhaps this post will be helpful for others who get confused why sometimes their saved canvases show up black, and other times with color. This behavior has gotten me several times. Also I don’t know if this should be changed or not, but wouldn’t one expect that all histograms embedded within a canvas should retain the attributes that the user supplied (of course raw objects should reflect the current style)? I understand a lone histogram having its attributes changed to whatever is in the current style, but a canvas full of numerous plots? Either way this subtle difference in behavior can be a bit confusing (histograms within a stack within a canvas vs. histograms within a canvas). I personally would prefer that histograms within a TCanvas structure were not altered based on the current style, if I go through the trouble of making a TCanvas, I’ll also apply the style changes I think are most appropriate at the time of creation.

gROOT->ForceStyle(0); //is easy but a little annoying.

Cheers,
Justin

[quote]I personally would prefer that histograms within a TCanvas structure were not altered based on the current style,[/quote]ForceStyle(0) is the default! (i.e. remove the call from rootlogon to get the behavior you are looking for).

Cheers,
Philippe.

I have a related question. If a TCanvas is created with gROOT->SetOptTitle(0) and then saved in a ROOT file, when you open the file later and look at the canvas you find the title is displayed.

I guess this is because this setting is a global setting and is not associated with the TCanvas or anything drawn on it? Is there a way around this?

Thanks,
David

Yes, put that setting in your rootlogon.C if you always want it.