Problem using TCutG

Hi,
I’ve got some trouble with the following piece of code (a version that creates the needed tree is attached to this post)

void tcutgdemo()
{
  gStyle->SetPalette(1);
    
  if (gDirectory == gROOT )
    TFile* f = new TFile("cuttest.root","recreate");

  TTree* tree = (TTree*) gDirectory->Get("bigtree");
  // create example tree, if no tree found...
  if (tree == NULL)
    {
      ...
    }
  
  double ulx[4] = {40.,-2.,40.,40.};
  double uly[4] = {10., 70.,70., 0.};

  double lrx[4]={-60,-20,-60,-60};
  double lry[4]={ 20,-50,-50, 20};
  
  TCutG* upperleft = new TCutG( "upperleft" , 4, uly, ulx );
  TCutG* lowerright= new TCutG( "lowerright", 4, lry, lrx );

  upperleft->SetVarX("mipposemc[0]");
  upperleft->SetVarY("mipposemc[1]");
  lowerright->SetVarX("mipposemc[0]");
  lowerright->SetVarY("mipposemc[1]");


  c1 = new TCanvas( "c1","c1");

  TH2F* ulhist = new TH2F("ulhist","ulhist",20,-100,100,20,-100,100);
  TH2F* lrhist = new TH2F("lrhist","lrhist",20,-100,100,20,-100,100);

  //  tree->Draw("mipposemc[0]:mipposemc[1]>>ulhist","mipposemc[0]>10&&upperleft","colz");
  tree->Draw("mipposemc[0]:mipposemc[1]>>ulhist","mipposemc[0]>10&&upperleft","box");
  tree->Draw("mipposemc[0]:mipposemc[1]>>lrhist","mipposemc[0]<-10&&lowerright","box");

  ulhist->Draw("colz");
  lrhist->Draw("colz same");

  //  delete upperleft;
  //  delete lowerright;
}

If you execute this script several times (usually two or three times) without touching anything. The root crashes.

root [0] .x tcutgdemo.C
TTree bigtree not found
root [1] .x tcutgdemo.C
Warning in <TH1::Build>: Replacing existing histogram: ulhist (Potential memory leak).
Warning in <TH1::Build>: Replacing existing histogram: lrhist (Potential memory leak).
root [2] .x tcutgdemo.C
Warning in <TH1::Build>: Replacing existing histogram: ulhist (Potential memory leak).
Warning in <TH1::Build>: Replacing existing histogram: lrhist (Potential memory leak).
root [3] .x tcutgdemo.C
Warning in <TH1::Build>: Replacing existing histogram: ulhist (Potential memory leak).
Warning in <TH1::Build>: Replacing existing histogram: lrhist (Potential memory leak).
root [4] .x tcutgdemo.C

 *** Break *** segmentation violation
(no debugging symbols found)
Using host libthread_db library "/lib/tls/libthread_db.so.1".
Attaching to program: /proc/20028/exe, process 20028
[Thread debugging using libthread_db enabled]
[New Thread -1218579808 (LWP 20028)]
0x011804de in __waitpid_nocancel () from /lib/tls/libc.so.6
Thread 1 (Thread -1218579808 (LWP 20028)):
#0  0x011804de in __waitpid_nocancel () from /lib/tls/libc.so.6
#1  0x011151c4 in do_system () from /lib/tls/libc.so.6
#2  0x00c8bd7f in system () from /lib/tls/libpthread.so.0
#3  0x005ef911 in TUnixSystem::Exec () from /opt/products/root/5.14.00/lib/libCore.so.5.14
#4  0x005efd2b in TUnixSystem::StackTrace () from /opt/products/root/5.14.00/lib/libCore.so.5.14
#5  0x005ede37 in TUnixSystem::DispatchSignals () from /opt/products/root/5.14.00/lib/libCore.so.5.14
...

I’ve tested this with ROOT 5.14 and ROOT 5.16. Surprisingly the number of calls needed to crash ROOT differs from try to try.

Are there some object ownerships rules, that I disobey or is there a different reason why this code does not work?

Cheers, J.
tcutgdemo.C (1.56 KB)

I suggest to modify your script as shown in the attachment.

Rene
tcutgdemo.C (1.6 KB)

Unfortunately deleting the TFile and reopen it doesn’t help if you place more code dealing with TCutG after my (minimal) example. I’ve attached a modified version of the “improved” example, where I essentially copy and pasted the code to create two more histograms.

Now ROOT crashes less often, but still crashes:

[code]root [0]
.x tcutgdemo2.C
root [1] .x tcutgdemo2.C
root [2]
*** Break *** segmentation violation
(no debugging symbols found)
Using host libthread_db library “/lib/tls/libthread_db.so.1”.
Attaching to program: /proc/21219/exe, process 21219
(no debugging symbols found)…done.
(no debugging symbols found)…done.
[Thread debugging using libthread_db enabled]
[New Thread -1218576256 (LWP 21219)]
(no debugging symbols found)…done.
(no debugging symbols found)…done.
(no debugging symbols found)…done.
(no debugging symbols found)…done.
(no debugging symbols found)…done.

0x0132d4de in __waitpid_nocancel () from /lib/tls/libc.so.6
Thread 1 (Thread -1218576256 (LWP 21219)):
#0 0x0132d4de in __waitpid_nocancel () from /lib/tls/libc.so.6
#1 0x012c21c4 in do_system () from /lib/tls/libc.so.6
#2 0x00599d7f in system () from /lib/tls/libpthread.so.0
#3 0x00836911 in TUnixSystem::Exec () from /opt/products/root/5.14.00/lib/libCore.so.5.14
#4 0x00836d2b in TUnixSystem::StackTrace () from /opt/products/root/5.14.00/lib/libCore.so.5.14
#5 0x00834e37 in TUnixSystem::DispatchSignals () from /opt/products/root/5.14.00/lib/libCore.so.5.14

[/code]

Sometimes even not before I leave ROOT:

root [0] .x tcutgdemo2.C
root [1] .x tcutgdemo2.C
root [2] .x tcutgdemo2.C
root [3] .q

 *** Break *** segmentation violation
(no debugging symbols found)
Using host libthread_db library "/lib/tls/libthread_db.so.

tcutgdemo2.C (2.23 KB)