Sorting in a root file

Hello All,

I am trying to sort a Tree and write to a new root file. but my code crashes.

[code]in = TFile::Open(“unsorted.root”);
TTree SinglesTree;
SinglesTree = (TTree
)gDirectory->Get(“Singles”);
SinglesTree->Draw(“time”,"",“goff”);
nentries = SinglesTree->GetEntries();
long long *index = new long long[nentries];
TMath::Sort(nentries,SinglesTree->GetV1(),index,0);
TTree *SinglesTree_Out;
SinglesTree_Out = SinglesTree->CloneTree(0);
for (IndexNo=0;IndexNo<nentries;IndexNo++){
SinglesTree->GetEntry(index[IndexNo]);
SinglesTree_Out->Fill();
}
delete in;

out = TFile::Open(“sortedroot”,“recreate”);
SinglesTree_Out->Write();
delete out;[/code]

When I compile this, it seems ok and there are no compilation errors, but when running it I get the following error:

Error in <TTree::Fill>: Failed filling branch:Singles.comptVolName, nbytes=-1 This error is symptomatic of a Tree created as a memory-resident Tree Instead of doing: TTree *T = new TTree(...) TFile *f = new TFile(...) you should do: TFile *f = new TFile(...) TTree *T = new TTree(...) Error in <TTree::Fill>: Failed filling branch:Singles.RayleighVolName, nbytes=-1 This error is symptomatic of a Tree created as a memory-resident Tree Instead of doing: TTree *T = new TTree(...) TFile *f = new TFile(...) you should do: TFile *f = new TFile(...) TTree *T = new TTree(...) ... ...

Any idea what I am doing wrong? Many thanks.

Karthik.

Hi,

as it says, you should put SinglesTree_Out into a new file by opening sortedroot before calling SinglesTree_Out = SinglesTree->CloneTree(0);

Cheers, Axel.

Thanks Axel. I should have guessed the solution.

I have a question on the same topic though. I tried the working code on a small root file (8000 entries) and it successfully sorted it though. Then I tried it on a larger root file (5001281 entries) and it just hangs.

Is there something I need to be aware of when running this code on a large data set?

Thanks.

Hi,

sorting 5M elements takes a lot longer than sorting 9k elements. TMath::Sort() should take NlogN, i.e. it should take about 1000 times longer. If that isn’t it we’ll need the actual code to be able to reproduce it. Or you attach gdb to root.exe (yes, “.exe” even on non-Windows) yourself and see where it’s stuck (“bt” will show the current backtrace).

Cheers, Axel.

Hello Axel,

I am a little perplexed by my root sorting code. One one computer it keeps running and the same code on another computer crashes. The root version is 5.26 on both of them.

Here is the code:

TFile           *in = TFile::Open("Unsorted.root");
TTree           *SinglesTree;
SinglesTree     = (TTree*)gDirectory->Get("Singles");
nentries = SinglesTree->GetEntries();
SinglesTree->Draw("time","","goff");
SinglesTree->SetBranchAddress("time",&time);
printf("Starting sorting ... \n");
Int_t	*index = new Int_t[nentries];
TMath::Sort(nentries,SinglesTree->GetV1(),index,down);
printf("Sorting done...\n");
...

When executed:

[quote]Starting sorting …

*** Break *** segmentation violation
Segmentation fault[/quote]

Thanks for you help.
Karthik.

Hi,

nentries = SinglesTree->GetEntries(); SinglesTree->Draw("time","","goff"); ..... Int_t *index = new Int_t[nentries]; TMath::Sort(nentries,SinglesTree->GetV1(),index,down);The default is for TTree::Draw to keep accessible via GetV1 only 1000000 values. If nentries is greater than this value, you are request TMath::Sort to read beyond the end of the array. To change the default value use: nentries = SinglesTree->GetEntries(); SinglesTree->SetEstimate(nentries); SinglesTree->Draw("time","","goff"); ..... Int_t *index = new Int_t[nentries]; TMath::Sort(nentries,SinglesTree->GetV1(),index,down);.

Cheers,
Philippe.