SetBinLabel and TickDivisions

Hello,
when i use SetBinLabel, my TH1F starts to ignore the SetNdivisions… what can I do in order to get the ticks drawn as i want them? (I know, I could draw them with Line, but there should be a way, to draw ticks even with BinLabels…)

Greetings,
Thomas

TCanvas *c3 = new TCanvas("c3"); TH1F * h3= new TH1F ("data_truth", "Data Truth", 12, 3, 6); h3->SetBinContent(5,5); h3->SetBinContent(6,6); h3->GetXaxis()->SetNdivisions(414); h3->GetXaxis()->SetBinLabel(5,"Bin 1"); h3->GetXaxis()->SetBinLabel(6,"Bin 2h"); h3->Draw();

force the number of divisions, ie instead of

h3->GetXaxis()->SetNdivisions(414); do

h3->GetXaxis()->SetNdivisions(-414); see root.cern.ch/root/html/TAttAxis. … Ndivisions

Rene

Sounds good, but doesnt work either… I get the same result as with -414 …

Olivier will follow your problem on Monday. Meanwhile identify your version of ROOT?

Rene

Thanks…
I tried with 5.18 and 5.17.04

When you define yourself the bin labels only the main tick marks are drawn. One per bin. SetNdivisions has no effect. Try to open the graphics editor and select the X axis. then change the divisions. You will see that it does not affect the display. Only the main ticks are drawn.

Hi,

I have the same problem and I would like to know if this is still true. In this case, I want to have only primary divisions shown every 9 bins, then I want to change the label from multiples of 9 to something more meaningful (still integers however, although I don’t know any other way than SetBinLabel to change that, which accepts only char input for labels). Here’s a simple example illustrating what I want to do.

{
  stringstream bin;
  int gliid;

  TH1D *GLImax_h = new TH1D("Maximum displacement per GLI","Maximum displacement per GLI",144,0,144);
  for (int m=0;m<16;++m) {
    gliid=100*(m/8+1)+(m%8)*10+1;
    for (int j=0;j<9;++j) {
      GLImax_h->Fill(9*m+j,j);
      if (j==0) {
        bin.str("");
        bin << gliid;
        GLImax_h->GetXaxis()->SetBinLabel(9*m+j+1,bin.str().c_str());
      }
      ++gliid;
    }
  }
  GLImax_h->SetName("GLI max");
  GLImax_h->GetYaxis()->SetTitle("Absolute Displacement (#mum)");
  GLImax_h->GetXaxis()->SetTitle("GLI Id");
  GLImax_h->GetXaxis()->SetNdivisions(-16);
  GLImax_h->Draw("hist");
}

When the SetBinLabel is commented out, the SetNdivisions call works like a charm. As soon as I add the SetBinLabel, I get as you say, one division per bin. Is there any workaround? I am using ROOT version 5.30.

Thanks in advance,

Andrée

Yes

You should define some blank labels in between.

[quote=“couet”][quote]
I would like to know if this is still true.
[/quote]

Yes
[/quote]

Ok, thanks.

[quote=“couet”]

You should define some blank labels in between.[/quote]

I am not sure to understand what you mean. I want the tickmarks gone for everything else than multiples of 9, not the labels…

Thanks,

Andrée

One way I see would be to draw 2 axis. one with the labels and no ticks,
And a 2nd one on top with the tick you want and no labels.
Or may be be changing the number of axis divisions . Have you tried it ?

[quote=“couet”]One way I see would be to draw 2 axis. one with the labels and no ticks,
And a 2nd one on top with the tick you want and no labels. [/quote]

Ah, that could work indeed (no I haven’t tried yet). Simple question though, how do I make the first one (labels, no ticks), since when I set the Labels, I lose all control on the ticks? I tried to do the following without any success:

{
  stringstream bin;
  int gliid;

  TH1D *GLImax_h = new TH1D("Maximum displacement per GLI","Maximum displacement per GLI",144,0,144);
  for (int m=0;m<16;++m) {
    gliid=100*(m/8+1)+(m%8)*10+1;
    for (int j=0;j<9;++j) {
      GLImax_h->Fill(9*m+j,j);
      if (j==0) {
        bin.str("");
        bin << gliid;
        GLImax_h->GetXaxis()->SetBinLabel(9*m+j+1,bin.str().c_str());
      }
      ++gliid;
    }
  }
  GLImax_h->SetName("GLI max");
  GLImax_h->GetYaxis()->SetTitle("Absolute Displacement (#mum)");
  GLImax_h->GetXaxis()->SetTitle("GLI Id");
  GLImax_h->GetXaxis()->SetNdivisions(0);
  GLImax_h->Draw("hist");
}

Unless you meant to add two axes on top of a blank slate (no labels, no ticks on the histo axis)?

[quote=“couet”]
Or may be be changing the number of axis divisions . Have you tried it ?[/quote]

Sorry, I’m confused again. Changing the number of divisions is what I tried originally… Can you explain in more details what you mean by this?

Thanks,

Andrée

You should make the ticks length very small.

yes you are right … in that case the ticks are always equal to the number of bins… It was confused.

Good morning,

So I tried the said solution, and there is only one remaining problem: TGaxis doesn’t treat the Ndivisions properly. Code below:

{
  stringstream bin;
  int gliid;

  TH1D *GLImax_h = new TH1D("Maximum displacement per GLI","Maximum displacement per GLI",144,0,144);
  TF1 *f1=new TF1("f1","x",0,144);
  TGaxis *labels = new TGaxis(0,0,144,0,"f1",16);
  for (int m=0;m<16;++m) {
    gliid=100*(m/8+1)+(m%8)*10+1;
    for (int j=0;j<9;++j) {
      GLImax_h->Fill(9*m+j,j);
      if (j==0) {
        bin.str("");
        bin << gliid;
        GLImax_h->GetXaxis()->SetBinLabel(9*m+j+1,bin.str().c_str());
      }
      ++gliid;
    }
  }
  GLImax_h->SetName("GLI max");
  GLImax_h->GetYaxis()->SetTitle("Absolute Displacement (#mum)");
  GLImax_h->GetXaxis()->SetTitle("GLI Id");
  GLImax_h->SetTickLength(0,"X");
  GLImax_h->Draw("hist");
  labels->SetNdivisions(16); //Reiterate previous request in TGaxis declaration
  labels->SetLabelSize(0);
  labels->Draw();

With this, I get the labels right without tickmarks, but the TGaxis has fixed Ndivisions (14, one every 10 bins). I tried to play with the example provided in root.cern.ch/root/html/TGaxis.html#TGaxis:TGaxis%2 to see how tickmarks worked for TGaxis, and it seems that even in the example, ndiv=16 is not behaving as I want (i.e. one tickmark every 9 bins). Is there something fundamentally wrong about this number? Any further clues?

Thanks in advance,

Andrée

Finally, I got it to work by asking for the TGaxis to use 16 bins! Working code below:

{
  stringstream bin;
  int gliid;

  TCanvas *c1 = new TCanvas();
  TH1D *GLImax_h = new TH1D("Maximum displacement per GLI","Maximum displacement per GLI",144,0,144);
  for (int m=0;m<16;++m) {
    gliid=100*(m/8+1)+(m%8)*10+1;
    for (int j=0;j<9;++j) {
      GLImax_h->Fill(9*m+j,j+1);
      if (j==0) {
        bin.str("");
        bin << gliid;
        GLImax_h->GetXaxis()->SetBinLabel(9*m+j+1,bin.str().c_str());
      }
      ++gliid;
    }
  }
  GLImax_h->SetName("GLI max");
  GLImax_h->GetYaxis()->SetTitle("Absolute Displacement (#mum)");
  GLImax_h->GetXaxis()->SetTitle("GLI Id");
  GLImax_h->SetTickLength(0,"X");
  GLImax_h->Draw("hist");
  c1->Update();
  TF1 *f1=new TF1("f1","x",0,16);
  TGaxis *labels = new TGaxis(0,c1->GetUymin(),144,c1->GetUymin(),"f1",16);
  labels->SetLabelSize(0);
  labels->Draw();
}

Cheers,

Andrée

Good,
Sorry for the late answer i was n holidays.