Removing events outside the contour

Hello all,

I have a script which draws contour to the TH2 histo. I need to eliminate the events outside the contour. Here is a piece of the script:

TGraph *down = new TGraph(n,m_min, y); // contour 
TGraph *up = new TGraph(n,m_max, y); // contour
TCutG *mycutup = new TCutG("mycutup",n);  // cut the events 
TCutG *mycutdown = new TCutG("mycutdown",n); //cut the events

h2->Draw("colz, [mycutup, -mycutdown]"); // just draw the events inside the graphs.

The things is here, TCutG helps me not to draw the events outside the contour but it does not remove them indeed. I mean, the entry number same as the histogram which not applied TCutG. How to remove those events? Btw, TCutG may not be the most effective selection for removing events since the up and down Graphs are not actually closed contour itself. I also attached the output. Any help would be appreciated, thanks.

I think you should

  • make a new histogram with the same limit and bin number as the original one.
  • fill it by looping over all bins of the original one.
  • In that loop, for each bin of the original histogram you call TGutG::IsInside to decide if that bin should go in the new one or not…

I do not see a better solution right now.

Yes, I did the very same thing to your idea please check below;

// hist_new1 is the original histogram and TH2

Int_t binx = hist_new1->GetXaxis()->GetNbins(); 
Int_t biny = hist_new1->GetYaxis()->GetNbins();
int ent = 0;

TH2F *h2; // new histo to draw the original one

for (int t =1; t < binx; t++)
    xlow = hist_new1->GetXaxis()->GetBinUpEdge(t+1);
    for (int z =1; z< biny; z++)
        ylow = hist_new1->GetYaxis()->GetBinUpEdge(z+1);

        bool DownIn = false;
        bool DownOut = false;
        bool UpIn = false;
        bool UpOut = false;

        if( mycutdown->IsInside(xlow, ylow))  DownOut = true;
        else DownIn = true;

        if ( mycutup->IsInside(xlow, ylow))  UpIn = true;
        else UpOut = true;

        Double_t entries = hist_new1->GetEntries();

          if (UpIn && DownIn && (hist_new1->GetBinContent(t,z) > 0 )) {
         h2->SetBinContent(t,z, hist_new1->GetBinContent(t,z));


It seems working except the baldness of the top of the plot see the attached files. I think it is because the gap between the two graphs.

If the two plots are drawn with the option COLZ, the top one has less bins along X and Y axis than the bottom one.

Yes sure, since I apply the “if (UpIn && DownIn && (hist_new1->GetBinContent(t,z) > 0 )” to top plot and no cut bottom one. I did not get your point.

You said :

I guess what you called “baldness” is the fact the colored squares for each bins are bigger on the top plot than on the bottom one ? … right ??

I think it is the fact, indeed. The unbinned points between up and down graphs that make the contour.

Ok … let me rephrase …

I understand the top plot is produced using:


how did you produce the bottom one ?

again with the “colz” option.

And please check that the bottom also has the “baldness” but just not that clear.

yes, but it looks like on the bottom plot that either the histogram you plot has very small bin or you are plotting a scatter plot from a ntuple or tree.

So, I do not understand what you call “baldness” … can you clarified ? … is it the line thickness ? … reduce the graph line width in that case.

I marked them see the attachment please.

And yes, top plot is plotted from a ntuple.
marked.pdf (182 KB)

I must be stupid … I do not understand what you are showing … the areas you are pointing have nothing special compared to the rest of the plot seems to me …

I see the bottom plot has a little gap between the two TCutG … otherwise there is nothing wrong … is its the little gap you are complaining about ?

I still do not understand what you mean by “baldness”…

Yes yes, I was just complaining about that :slight_smile: Since there is a gap between two graphs, the contour is not being closed. And so, TCutG does not keep some of the events inside the contour.

Actually your answer and the job that I did the same with your reply, solved the issue of removing events outside the contour.