How to have TH1F's SetBinContent with "char" xAxis

Hi, I am basically using the hlabels1.C tutorial script but with little modification.

/// \file
/// \ingroup tutorial_hist
/// \notebook
/// 1-D histograms with alphanumeric labels
///
/// \macro_image
/// \macro_code
///
/// \author Rene Brun

TCanvas *hlabels1()
{
   const Int_t nx = 20;
   const char *people[nx] = {"Jean","Pierre","Marie","Odile","Sebastien",
      "Fons","Rene","Nicolas","Xavier","Greg","Bjarne","Anton","Otto",
      "Eddy","Peter","Pasha","Philippe","Suzanne","Jeff","Valery"};
   Double_t foo [20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
   TCanvas *c1 = new TCanvas("c1","demo bin labels",10,10,900,500);
   c1->SetGrid();
   c1->SetTopMargin(0.15);
   TH1F *h = new TH1F("h","test",3,0,3);
   h->SetStats(0);
   h->SetFillColor(38);
   h->SetCanExtend(TH1::kAllAxes);
   for (Int_t i=0;i<20;i++) {
     //Int_t r = gRandom->Rndm()*20;
     h->Fill(People[i],1); 
   }
   h->LabelsDeflate();
   h->Draw();
   TPaveText *pt = new TPaveText(0.7,0.85,0.98,0.98,"brNDC");
   pt->SetFillColor(18);
   pt->SetTextAlign(12);
   pt->AddText("Use the axis Context Menu LabelsOption");
   pt->AddText(" \"a\"   to sort by alphabetic order");
   pt->AddText(" \">\"   to sort by decreasing values");
   pt->AddText(" \"<\"   to sort by increasing values");
   pt->Draw();
   return c1;
}

Basically, I want to have x-axis as the char as in this hlabels1.C tutorial and I want to have an array as weight of the fill function.
However, trial as h->Fill(peopel[i],foo[i]); results in disappearance of color of hist and wierd y-error bar that I never put in.

If I try to use SetBinContent, as h->SetBinContent(people[i],foo[i]);, it complains that the arguments do not follow any 3 options of SetBinContent (Int_t, Double_t)

How may I still use “char” axis but fill the histogram with weight? whether it’s using Fill or SetBinContent?

Thank you!

Hi,

It looks to me there is an error in your script. You should use lower case for people[i]

h->Fill(people[i],1);

If you want to use SetBinContent for a given label, you can just do, for example, the following:

h->SetBinContent(h->GetXaxis()->FindBin("Rene"),2);

Lorenzo

1 Like