RE: Deleting a TKey from a TFile

From: Lee, Kerry T. \(JSC-SF\)[UHCL] <kerry.t.lee_at_nasa.gov>
Date: Fri, 19 May 2006 13:43:22 -0500


I did as you suggested and it works fine unless the TTree in which the TKey is pointing to contains data. Below is some code illustrating the problem. I ran the following commands

root [0] .L FileTest.C++
Info in <TUnixSystem::ACLiC>: creating shared library /home/kerrylee/./FileTest_C.so root [1] test()
No TestTree object found
(int)0

root [2] system("ls -l testing.root")
-rw-r--r-- 1 kerrylee kerrylee 81962 May 19 13:35 testing.root
(const int)0

root [3] test()
TestTree already exists. Do you want to replace it (y or n)? y deleting TestTree
(int)0

root [4] system("ls -l testing.root")
-rw-r--r-- 1 kerrylee kerrylee 159566 May 19 13:36 testing.root
(const int)0

root [5] test()
TestTree already exists. Do you want to replace it (y or n)? y deleting TestTree
(int)0

root [6] system("ls -l testing.root")
-rw-r--r-- 1 kerrylee kerrylee 221169 May 19 13:38 testing.root
(const int)0

As you can see the file is growing. If I comment out the t->Fill() line all the file does not grow. I assume this is because the TTree itself needs to be deleted from the file also. Is there a way to delete not only the TKey pointing to the TTree but also remove the TTree so that the space will be reused later?

Thanks
Kerry

#include "TSystem.h"
#include "TFile.h"
#include "TString.h"
#include "Riostream.h"
#include "TRandom3.h"
#include "TTree.h"

int test(){
  TString DataFile="testing.root";
  TFile *f = new TFile(DataFile,"UPDATE");   char ans = 'n';
  if(f->FindObjectAny("TestTree")){
    cout<<"TestTree already exists. Do you want to replace it (y or n)? ";     cin>>ans;
    if(ans == 'n') {
      return -1;
    } else {

      std::cout<<"deleting TestTree"<<std::endl;
      f->Delete("TestTree;*");
      f->Close();
      delete f;
      f = new TFile(DataFile,"UPDATE");

    }
  } else cout<<"No TestTree object found"<<endl;   TTree *t = new TTree("TestTree","this is a test");   TRandom3 *ran = new TRandom3();
  double g;
  t->Branch("G",&g,"G/D");
  for(int i=0;i<10000;i++){
    g=ran->Gaus();
    t->Fill();
  }
  f->Write();
  f->Close();
  return 0;
}

-----Original Message-----
From: owner-roottalk_at_pcroot.cern.ch on behalf of Rene Brun Sent: Fri 5/12/2006 12:11 AM
To: Lee, Kerry T. (JSC-SF)[UHCL]
Cc: roottalk_at_pcroot.cern.ch
Subject: Re: [ROOT] Deleting a TKey from a TFile  

What you see is the expected behaviour. The gap left in the file when deleting an object will only be reused if you close and reopen the file. This is done on purpose to support the case of multiple readers accessing a file written by another process.

Rene Brun

On
Thu, 11 May 2006, Lee, Kerry T. (JSC-SF)[UHCL] wrote:

> Dear ROOT team,
>
> I am using ROOT 5.11/02 compiled on a linux machine with gcc 3.4.4.
>
> I am attempting to delete a TKey within a TFile, but it does not reduce the size of the file as I would expect.       Below is a couple short examples to illustrate what I see.
>
>
> root [0] TFile *f = new TFile("testing2.root","RECREATE");
> root [1] f->Write();
> root [2] f->Close();
> root [3] .q
>
> the size of the file is then 330 bytes as shown below
>
> [kerrylee_at_jsc-sf-2148872 ~]$ ls -l testing2.root
> -rw-r--r--  1 kerrylee kerrylee 330 May 11 12:44 testing2.root
>
> Now if I write an empty TTree to a file.
>
> root [0] TFile *f = new TFile("testing.root","RECREATE");
> root [1] TTree *t = new TTree("TestTree","This is a test");
> root [2] f->Write();
> root [3] f->Close();
> root [4] .q
> [kerrylee_at_jsc-sf-2148872 ~]$ ls -l testing.root
> -rw-r--r--  1 kerrylee kerrylee 4223 May 11 12:53 testing.root
>
> Then try to delete the tree. I do not see a change in the file size even though ls() shows that the TTree as being deleted.
>
> root [0] TFile *f = new TFile("testing.root","UPDATE");
> root [1] f->ls();
> TFile**         testing.root
> TFile*         testing.root
>  KEY: TTree    TestTree;1      This is a test
> root [2] f->Delete("TestTree;1");
> root [3] f->Flush();
> root [4] f->ls()
> TFile**         testing.root
> TFile*         testing.root
> root [5] f->Close();
> root [6] .q
> [kerrylee_at_jsc-sf-2148872 ~]$ ls -l testing.root
> -rw-r--r--  1 kerrylee kerrylee 4223 May 11 12:55 testing.root
>
>
> I would expect that the updated file should be 330 bytes. Have I misunderstood how to remove a TKey properly?
>
> Thanks
> Kerry
>
>
>
>
Received on Fri May 19 2006 - 20:43:47 MEST

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:31:58 MET