# Excluding bins of a histogram

Hi,

I’m plotting a histogram from an ntuple, I get the contents of each bin and what I then want to do is re-draw the histogram excluding any bins whose contents is greater than 15. I can’t find a way of doing this, what is the easiest way?

Thanks,

Lisa

It is not clear what you expect to see as a result?
-set the corresponding bins to 0?
if (yes) loop on all bins with content than 15 and set the content to 0,ie
for (int i=1;i<=nbins;i++} if (h.GetBinContent(i) >15) h.SetBinContentI,0);
-Set the maximum scale on y to be 15?
if yes call h.SetMaximum(15);

Rene

Hi Rene,

I’ve been trying to set the bins whose content is greater than 15 to zero and then re draw the histogram. I’ve written the code below:

TH1D *M29R_1str = new TH1D(“M29R 1 strip clusters”, “”, 2048, 0, 2048);
T->Draw(“ClusterStrip >> M29R 1 strip clusters”, “is R==1 && highThreshold==1 && SensorNumber==25 && CluSize==1”);
M29R_1str.SetXTitle(“Strip number”);
for (Int_t i=0; i<M29R_1str.GetXaxis()->GetNbins();i++){
M29R_1str.GetBinContent(i);

if(M29R_1str.GetBinContent(i) >15){
M29R_1str.SetBinContent(i, 0.0);
M29R_1str->Draw();
}

In this code 31 bins are set to zero but when I draw the histogram again I get 31 more entries than I did before I applied the cut. Is there a solution for this?

Thanks,

Lisa

Do:

```TH1D *M29R_1str = new TH1D("M29R_1str", "", 2048, 0, 2048); T->Draw("ClusterStrip >>M29R_1str", "is R==1 && highThreshold==1 && SensorNumber==25 && CluSize==1"); M29R_1str->SetXTitle("Strip number"); double NE = M29R_1str->GetEntries(); for (Int_t i=1; i<=M29R_1str->GetXaxis()->GetNbins();i++){ if(M29R_1str.GetBinContent(i) >15) M29R_1str.SetBinContent(i, 0.0); } M29R_1str->SetEntries(NE); M29R_1str->Draw(); ```
Rene

Hi Rene,

Thank you for your help. I have one more quick question… the number of entries in my histogram is now 2485, which is the same as before I applied the cut. Why is it not this value minus the number of entries in the bins that I have excluded?

Thanks,

Lisa

Well, simply replace the piece of code that I sent to you above by:

```TH1D *M29R_1str = new TH1D("M29R_1str", "", 2048, 0, 2048); T->Draw("ClusterStrip >>M29R_1str", "is R==1 && highThreshold==1 && SensorNumber==25 && CluSize==1"); M29R_1str->SetXTitle("Strip number"); double NE = M29R_1str->GetEntries(); for (Int_t i=1; i<=M29R_1str->GetXaxis()->GetNbins();i++){ double y = M29R_1str.GetBinContent(i); if(y >15) { M29R_1str.SetBinContent(i, 0.0); NE -= y; } } M29R_1str->SetEntries(NE); M29R_1str->Draw(); ```

Rene