Applying cut on histogram without changing axis range

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!

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

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

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 h1 = new TH1D ("h1","h1",100,x1,x2);
tree->Draw("x>>h1","cut_expression");

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:

   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.

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

1 Like