# Getting content of sub-range of a histogram and set in a new one

**URL:** <https://root-forum.cern.ch/t/getting-content-of-sub-range-of-a-histogram-and-set-in-a-new-one/45218>\
**Category:** Newbie\
**Created:** [June 2, 2021, 2:58pm UTC](https://root-forum.cern.ch/t/getting-content-of-sub-range-of-a-histogram-and-set-in-a-new-one/45218 "2021-06-02T14:58:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haidar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/96bed5/32.png) [@Haidar](https://root-forum.cern.ch/u/Haidar)\
**Post date:** [June 2, 2021, 2:58pm UTC](https://root-forum.cern.ch/t/getting-content-of-sub-range-of-a-histogram-and-set-in-a-new-one/45218/1 "2021-06-02T14:58:10Z")

</div>

Hi ROOTers,  
I have a histogram with variable binning and I want set sub-range of its content into a new one. I thought of using SetRangeUser(min,max) but then I realized that this method only affects the visualization but not the object itself. So I try to make a code to do that

```auto
void trucateHisto()
{
    TFile *file = new TFile("file.root", "read");

    TH1D *histo = (TH1D *)file->Get("myhisto;1");
    // the original histo hasthe following binning
    // double binningOld[] = {5, 10, 20, 30, 40, 50, 60, 70, 80, 100, 120, 140, 200, 250};
    double binning[] = {10, 20, 30, 40, 50, 60, 70, 80, 100, 120, 140}; // binning of the new histo
    int nBins = sizeof(binning) / sizeof(binning[0]) - 1;

    TH1D *histoNew = new TH1D("histoNew", "histoNew", nBins, binning);

    for (int i = 1; i < histo->GetNbinsX(); i++)
    {
        double cont = histo->GetBinContent(i);
        double err = histo->GetBinError(i);

        for (int j = 1; j < histoNew->GetNbinsX(); j++)
        {
            histoNew->SetBinContent(i + 1, cont);
            histoNew->SetBinError(i + 1, err);
        }
    }
    histo->Draw();
    histoNew->SetLineColor(kRed);
    histoNew->Draw("same");
}

```

I think I must be doing something wrong in my loop because when I draw the two histograms I see a shift in the new histogram. Any help is highly appreciated.  
[file.root](https://root-forum.cern.ch/uploads/short-url/fcoU4gW0gwYlpMdeluxIfPrGgE2.root) (4.0 KB)

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 2, 2021, 8:10pm UTC](https://root-forum.cern.ch/t/getting-content-of-sub-range-of-a-histogram-and-set-in-a-new-one/45218/2 "2021-06-02T20:10:11Z")

</div>

Hi,

what is the meaning of this innermost `for` loop?

> [@Haidar](#):
>
> ```auto
> for (int j = 1; j < histoNew->GetNbinsX(); j++)
> {
> histoNew->SetBinContent(i + 1, cont);
> histoNew->SetBinError(i + 1, err);
> }
> 
> ```

Your `j` iterator is never used in it, so why bother?

Finally, the

> [@Haidar](#):
>
> ```auto
> for (int i = 1; i < histo->GetNbinsX(); i++)
> 
> ```

in the outermost loop should really become

```auto
for (int i = 1; i <= histo->GetNbinsX(); i++)

```

---

<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:** [June 4, 2021, 8:09am UTC](https://root-forum.cern.ch/t/getting-content-of-sub-range-of-a-histogram-and-set-in-a-new-one/45218/3 "2021-06-04T08:09:29Z")

</div>

As @yus pointed your current implementation has several inconsistencies.  
There is several ways to achieve this.  
A possible one is:

```auto
{
   auto file = new TFile("file.root", "read");

   TH1D *histo = (TH1D *)file->Get("myhisto");
   double binning[] = {10, 20, 30, 40, 50, 60, 70, 80, 100, 120, 140}; // binning of the new histo
   int nBins = sizeof(binning) / sizeof(binning[0]) - 1;

   auto histoNew = new TH1D("histoNew", "histoNew", nBins, binning);

   int j =1;
   for (int i = 1; i < histo->GetNbinsX(); i++) {
      double cont = histo->GetBinContent(i);
      double err = histo->GetBinError(i);
      if (histo->GetXaxis()->GetBinLowEdge(i) >= histoNew->GetXaxis()->GetXmin() &&
         histo->GetXaxis()->GetBinLowEdge(i) < histoNew->GetXaxis()->GetXmax() ) {
         histoNew->SetBinContent(j,cont);
         histoNew->SetBinError(j,err);
         j++;
      }
   }
   auto C = new TCanvas();
   C->SetLogy();
   histo->Draw("hist");
   histoNew->SetLineColor(kRed);
   histoNew->SetLineWidth(3);
   histoNew->Draw("same hist");
}

```

 ![Screenshot 2021-06-04 at 10.07.50](https://root-forum.cern.ch/uploads/default/original/3X/b/4/b4aba42e81ab219644da4881548188659dd0267d.jpeg)
