SetBinContent and SetBinError with label

Dear experts,
I have an hist labels on the x-axis. I want to use SetBinContent and SetBinError for each bin_labeled.
I do not see “SetBinContent(char name, Double_t content)” method. So if I use “SetBinContent(Int_t bin, Double_t content)”, is it going to do it properly?
Regards

yes it should. Just give it a try.

Dear Couet,
thank you for your answer. I’m just surprise that when I have a label hist and then when I do “hist->fill(int bin, double weight)” instead of “hist->fill(char label, double weight)”, it does not fill the histogram properly?
Regards

Fill takes the value you give as parameter (a value on the X axis or a text label), convert it into the corresponding bin number and fill that bin . SetBinContent directly put the value given as 2nd parameter into the bin corresponding to the number given as 1st parameter.

Dear Couet,
I wrote a small macro (1), and you can see that line (a) ou (b) do not produce the same histogram. Did I made a mistake?
Regards

(1)

void test(){

  vector<string> vname{"a", "b", "c", "d", "e"};

  TH1D* hist = new TH1D ("hist", "hist", 5, 0, 5);

  for(int i=0; i<5; i++) hist->GetXaxis()->SetBinLabel(i+1, vname[i].c_str());

  for(int i=1; i<5; i++) hist->Fill(i, i*2);     // (a)
  //for(int i=1; i<5; i++) hist->Fill(vname[i-1].c_str(), i*2);   // (b)

  hist->Draw();

}

yes basically that’s what I explained you. To get the same histogram do:

{

   vector<string> vname{"a", "b", "c", "d", "e"};

   TH1D* hist = new TH1D ("hist", "hist", 5, 0, 5);

   for(int i=0; i<5; i++) hist->GetXaxis()->SetBinLabel(i+1, vname[i].c_str());

   for(int i=1; i<5; i++) hist->SetBinContent(i, i*2);     // (a)
   //  for(int i=1; i<5; i++) hist->Fill(vname[i-1].c_str(), i*2);   // (b)

   hist->Draw("hist");

}

I draw with option “hist” to ease the comparison.

Dear Couet,
I’m concerned about the Fill method as SetBinContent is kind of diffrent/strange (errors…).
Thank you for your help.
Regards

They are different. Just read the doc.

Dear Couet,
are you sure that “SetBinContent(Int_t bin, Double_t content)” works properly with label?! In my case it does not work.
Regards

That’s what I get. Seems ok seems to me:

{
   vector<string> vname{"a", "b", "c", "d", "e"};
   TH1D* hist = new TH1D ("hist", "hist", 5, 0, 5);
   for(int i=0; i<5; i++) hist->GetXaxis()->SetBinLabel(i+1, vname[i].c_str());
   //for(int i=1; i<5; i++) hist->SetBinContent(i, i*2); hist->SetTitle("SetBinContent");// (a)
   for(int i=1; i<5; i++) hist->Fill(vname[i-1].c_str(), i*2); hist->SetTitle("Fill");// (b)
   hist->Draw("hist");
}


No, there is no overload for SetBinContent that takes a const char* parameter, as you already found out in your first post.
You need to calculate the bin using: int bin = hist->GetXaxis()->FindBin(the_label_cstr);

Dear Couet and behrenhoff,
using int bin = hist->GetXaxis()->FindBin(the_label_cstr); seems to work better than SetBinContent(Int_t bin, Double_t content) but I can’t confirm.
Thank you for your help.
Regards

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.