# Applying cut on histogram without changing axis range

**URL:** <https://root-forum.cern.ch/t/applying-cut-on-histogram-without-changing-axis-range/53835>\
**Category:** Newbie\
**Created:** [March 1, 2023, 3:34pm UTC](https://root-forum.cern.ch/t/applying-cut-on-histogram-without-changing-axis-range/53835 "2023-03-01T15:34:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [March 1, 2023, 3:34pm UTC](https://root-forum.cern.ch/t/applying-cut-on-histogram-without-changing-axis-range/53835/1 "2023-03-01T15:34:38Z")

</div>

Hello,  
it’s a very basic question, but I have looked everywhere and could not find a satisfying solution.  
I have a histogram in a .root file, which does not have a TTree structure. What I want to do is to apply cut on that histogram but so that I do not change axis ranges.  
So, for example, if I have a pT distribution from that file, which is TH1D, I want to create a new histogram from that one, but with only pT \< 100.  
How do I do this?  
I tried getting bin content, etc. but somehow it does not work.  
Thanks!

---

<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:** [March 1, 2023, 4:04pm UTC](https://root-forum.cern.ch/t/applying-cut-on-histogram-without-changing-axis-range/53835/2 "2023-03-01T16:04:40Z")

</div>

> [@beg1nner](#):
>
> I have a histogram in a .root file, which does not have a TTree structure

Histograms (TH1, TH2 …) are not TTree. So a histogram cannot have “_a TTree structure_”. So let’s assume you have a TTree.

> [@beg1nner](#):
>
> What I want to do is to apply cut on that histogram

Let’s assume you apply the cut on a TTree.

> [@beg1nner](#):
>
> so that I do not change axis ranges

Yes, when you apply a cut on a tree variable, the automatic X-axis limits computation is different from the one you get when you plot the variable without a cut. You can force the axis limits by creating a histogram before doing the plot:

```auto
auto h1 = new TH1D ("h1","h1",100,x1,x2);
tree->Draw("x>>h1","cut_expression");

```

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [March 1, 2023, 4:12pm UTC](https://root-forum.cern.ch/t/applying-cut-on-histogram-without-changing-axis-range/53835/3 "2023-03-01T16:12:03Z")

</div>

What I meant is that a root file does not have a TTree structure. So, unfortunately I cannot use it like you suggested…  
I have TDirectory and a histogram in that TDirectory, which I access using `TDirectoryFile`. Here is the way I retrieve that histogram:

```auto
   TDirectoryFile* myDir = static_cast<TDirectoryFile*>(f->Get("Directory"));
    myDir->cd();
    TList *list = static_cast<TList*>(myDir->GetListOfKeys());
    
    TKey *key1 = static_cast<TKey*>(list->FindObject("pT"));
    
    TObject *obj1 = static_cast<TKey*>(key1)->ReadObj();
    
    TH1D *pT_hist = static_cast<TH1D*>(obj1);
    

```

And now I want to create another histogram, which will have same axis ranges as the retrieved one, but without data points after pT \> 100.

---

<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:** [March 1, 2023, 4:16pm UTC](https://root-forum.cern.ch/t/applying-cut-on-histogram-without-changing-axis-range/53835/4 "2023-03-01T16:16:31Z")

</div>

Retrieve the bin corresponding to 100 in `pT_hist` and loop from that bin until the last bin  
with `pT_hist->SetBinContent(i,0);`
