Append to already saved ntuple

Hi,

is it possible to append new entries to an ntuple, which has already been saved to a root file? So, open the root file, access the ntuple and append new entries to the existing ones.

thanx,
Balint

Simply open the file in "update"mode, read the Tree header in memory
andcontinue to fill.

Rene

It surely doesn’t work in this way:


#include <TNtuple.h>
#include <TFile.h>

void size(){

TNtuple * data1 = new TNtuple(“data”, “data”, “x:y”);

for(int i = 0; i < 1000; i++){
data1->Fill(gRandom->Rndm(), gRandom->Rndm());
}
TFile * file1 = new TFile(“file1.root”, “recreate”);
data1->Write();
file1->Close();

TFile * file2 = new TFile(“file1.root”, “update”);
TNtuple * data2 = (TNtuple*)file2->Get(“data”);
for(int j = 0; j < 1000; j++){
data2->Fill(gRandom->Rndm(), gRandom->Rndm());
}

file2->Close();
}


After running this file1.root should have 2000 entries, but when checking it I got:

C:\Documents and Settings\Balint\Tif\HTiRydberg\PC\noclus>root.exe -l file1.root
root [0]
Attaching file file1.root as _file0…
root [1] .ls
TFile** file1.root
TFile* file1.root
KEY: TNtuple data;1 data
root [2] data->GetEntries()
(const Long64_t)1000
root [3]

So, what is the problem here?

Thanx,
Balint

You had several problems in your code. See working code below

[code]#include <TNtuple.h>
#include <TFile.h>

void size(){

TFile * file = new TFile(“file1.root”, “recreate”);
TNtuple * data = new TNtuple(“data”, “data”, “x:y”);

for(int i = 0; i < 1000; i++){
data->Fill(gRandom->Rndm(), gRandom->Rndm());
}
data->Write();
delete file;

file = new TFile(“file1.root”, “update”);
data = (TNtuple*)file->Get(“data”);
for(int j = 0; j < 1000; j++){
data->Fill(gRandom->Rndm(), gRandom->Rndm());
}

data->Write();
delete file;
}
[/code]

Rene

Thanx for the help. (I’ll read the docs, I promise, when I’ll have some spare time :slight_smile: )

Balint