Adjusting Y maximum range in pad automatically

Hi
I am drawing a set of histograms in my canvas and I always find myself adjusting the Y axis range of my pad because I guess root is taking the Y axis values of the first plotted histogram TH1 to be the standard for the session.

I can write a script to loop through all my histograms so to estimate the ymax from all of them and then loop again to adjust the h->SetMaximum(ymax) of all the histograms. Is there a better and more elegant way to do it?

Like instead of calling the second loop, could I tell my gPad to readjust its axis?

I guess there is another way to go around this problem like using the THStack object… right?

Cheers,
C.

2 Likes

Use a THStack and draw it with the “NOSTACK” drawing option.

1 Like

Hi
I implemented this option and it works… now I have another question. When I draw the canvas, I cannot longer interactively changed Yaxis dsiplay via TAxis::SetUserRange since the only way to modify the axis range is via THStack::SetMaximum() in the case of Y axis. I can do that on my code but is it possible to do it dynamically?

It would be very helpful, instead of compiling my code each time.

C Mos

I just tried with 5.34 to change interactively the range on the Y axis. It works.

Yes, you can click and drag right on the axis to “zoom in” or you can unzoom via right-clicked menu. However if you want to set the scale precisely using SetUserRange(), it just doesn’t work for THStack objects drawn in my Canvas.
Cheers,

The Y axis’ range is always set via SetMaximum and SetMinimum
root.cern.ch/drupal/content/how-set-ranges-axis

Thank you.
I can set the ranges in my code, that is not a problem except that I have to re-compile and run each time. On the other hand, if I draw my histograms in my canvas directly, then i could adjust the axis with SetUserRange function when my histograms are already drawn.

I need to use the THStack in my specific case. I am just trying to figure out if there is an easy way to adjust the axis via SetUserRange-like function.

Maybe there is not way…

Cheers,

{
   //-------------------------------------------

   TH1F *h1 = new TH1F("h1","Histo 1",100,-5,5);
   h1->FillRandom("gaus",10000);
   TH1F *h2 = new TH1F("h2","Histo 2",100,-5,5);
   h2->FillRandom("gaus",5000);
   TH1F *h3 = new TH1F("h3","Histo 3",100,-5,5);
   h3->FillRandom("gaus",2000);

   //-------------------------------------------

   h1->SetFillStyle(3004);
   h1->SetFillColor(1);

   h2->SetFillStyle(3004);
   h2->SetFillColor(2);

   h3->SetFillStyle(3004);
   h3->SetFillColor(3);

   //-------------------------------------------

   THStack *hs = new THStack("hs","A stack of histograms");

   hs->Add(h1);
   hs->Add(h2);
   hs->Add(h3);

   //-------------------------------------------
   TLegend *leg = new TLegend(0.1,0.6,0.4,0.9);

   leg->AddEntry(h1,"h1","f");
   leg->AddEntry(h2,"h2","f");
   leg->AddEntry(h3,"h3","f");
 
   //-------------------------------------------

   hs->Draw("nostack");
   hs->SetMinimum(100);
   hs->SetMaximum(200);
   leg->Draw();
}

Yes, my question is that after you execute the code and draw that, then if you right click on the y-axis and try to adjust the range using SetUserRange() function, it doesn’t work.

My program is not a macro so I have to recompile each time.

Cheers,

That’s because SetRangeUser has been done on all axis automatically when this menu is build there is no knowledge about the kind of axis (X Y or Z). The only way is to zoom using the nouse on the axis itself… but as you said it is less precise . In case of Y-Axis this option should be remove I guess.