# Manipulating Histogram Data and Fill in a New Histogram

**URL:** <https://root-forum.cern.ch/t/manipulating-histogram-data-and-fill-in-a-new-histogram/53742>\
**Category:** Newbie\
**Created:** [February 23, 2023, 6:00am UTC](https://root-forum.cern.ch/t/manipulating-histogram-data-and-fill-in-a-new-histogram/53742 "2023-02-23T06:00:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [February 23, 2023, 6:00am UTC](https://root-forum.cern.ch/t/manipulating-histogram-data-and-fill-in-a-new-histogram/53742/1 "2023-02-23T06:00:37Z")

</div>

I have a histogram in a ROOT file whose x-axis is channel number and y-axis is number of hits. I am trying to divide number of hits by total entries of a histogram and plot this hitrate in new histogram where x-axis is channel number and y-axis in hitrate.

```auto
11 void code() {
 12 TFile *file = new TFile("onmon_r1169_s1-92.root", "READ");
 13 TH1F *hist = (TH1F*)file->Get("SSDProfile_14");
 14 int entries = hist->GetEntries();
 15 // cout << entries << endl; //Output: 503071
 16 
 17 TH1F* new_hist = new TH1F("hitrate", "hitrate per channel", 100, 0, 700);
 18 
 19 for (int i = 0; i <= entries; i++) {
 20 //hist->GetBinContent(i);
 21 double bin_content = hist->GetBinContent(i); 
 22 double hitrate = bin_content/entries;
 23 new_hist->SetBinContent(i, hitrate);
 24 }       
 25 
 26 new_hist->Draw();
 27 
 28 }

```

However, the histogram I produced is not as expected. I think I am messing up on GetBinContent part. Please let me know how I can fix the issue. Thank you.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 23, 2023, 7:19am UTC](https://root-forum.cern.ch/t/manipulating-histogram-data-and-fill-in-a-new-histogram/53742/2 "2023-02-23T07:19:30Z")

</div>

You should do loop over the numver of bins `hist->GetNbinsX()`.

```auto
...
for (int i = 1; i <= hist->GetNbinsX(); i++) {
...

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 23, 2023, 7:54am UTC](https://root-forum.cern.ch/t/manipulating-histogram-data-and-fill-in-a-new-histogram/53742/3 "2023-02-23T07:54:24Z")

</div>

```cpp
TH1F *new_hist = (TH1F*)hist->Clone("hitrate");
new_hist->SetTitle("hitrate per channel;channel;hitrate");
new_hist->Scale(1. / new_hist->Integral());
new_hist->Draw("HIST");

```
