One macro to produce two canvases with different styles

My goal is to be able to produce multiple canvases with one macro and change the TStyle for each canvas independently. I tried to follow the example in [root-forum.cern.ch/t/different-style-in-same-canvas-seterrorx/13807/1 but it is not working for me.

I have attached an example macro. I have one canvas, on which I want to plot a uniformly-binned histogram with no horizontal error bars. On the other canvas, I want to plot a variably-binned histogram with horizontal error bars. The TExec technique appears to have no effect - the global style default is used for both histograms. (I also tried adding the TExec to the list of histogram functions, as shown in the commented code in the macro, and this did not work either.) How can I get this to work?
twohist.C (548 Bytes)

You can try something like:

   TCanvas *c1 = new TCanvas("c1");
   TH1F *h1 = new TH1F("h1","h1",10,-3,3);
   h1->FillRandom("gaus",10000);
   c1->DrawFrame(h1->GetXaxis()->GetXmin(), h1->GetMinimum(),
                 h1->GetXaxis()->GetXmax(), h1->GetMaximum());
   TExec* ex1 = new TExec("ex1","gStyle->SetErrorX(0.0);");
   ex1->Draw();
   h1->Draw("PE SAME");

I just tried this code. Now, the style from ex1 is applied to both canvases, even though I define a different style in ex2 that should apply to the second canvas. How can I reliably apply different styles to each canvas?
twohist2.C (763 Bytes)

you should do a similar thing for the 2nd canvas of course

Oh, I found the problem. I thought I had applied your code completely to the second canvas, but I forgot to change the histogram draw command to “PE same”. I guess the “same” part is necessary for the drawn histogram to pick up the style from the TExec?

Same means “use the current coordinates” in that case those defined by DrawFrame. TExec cannot be the first item in the canvas. That’s why you first need to Draw the frame, then TExec … then the histo… do c1.ls() and you will see the content of the canvas… TExec appearing 2nd. With the previous version of your macro TExec was not listed.