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

**URL:** <https://root-forum.cern.ch/t/how-to-have-th1fs-setbincontent-with-char-xaxis/31562>\
**Category:** Newbie\
**Created:** [November 13, 2018, 6:50am UTC](https://root-forum.cern.ch/t/how-to-have-th1fs-setbincontent-with-char-xaxis/31562 "2018-11-13T06:50:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![trenta\_coollime](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/trenta_coollime/32/19159_2.png) [@trenta\_coollime](https://root-forum.cern.ch/u/trenta_coollime)\
**Post date:** [November 13, 2018, 6:50am UTC](https://root-forum.cern.ch/t/how-to-have-th1fs-setbincontent-with-char-xaxis/31562/1 "2018-11-13T06:50:25Z")

</div>

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

```auto
/// \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!

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [November 14, 2018, 8:44am UTC](https://root-forum.cern.ch/t/how-to-have-th1fs-setbincontent-with-char-xaxis/31562/2 "2018-11-14T08:44:56Z")

</div>

Hi,

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

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

```

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

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

```

Lorenzo
