Persisting alias of a ttree

Hi,

I’m currently trying to write a macro with the purpose to persist aliases set in a TTree, i.e. write either a copy of the existing tree with extra branches with precomupted numbers corresponding to the list of aliases, or perhaps a friend tree.

Based on the example here Adding a branch to an existing tree, I tried with the following code:

t->SetBranchStatus("*",1);
t->SetEstimate(-1);

TList *alilist = t->GetListOfAliases();
Double_t var[1000]; 

for (int j=0; j<=alilist->GetLast(); ++j) 
{
	TString aliname = ((TNamed*)alilist->At(j))->GetName();
	TString fml     = t->GetAlias(aliname);
	TBranch *b      = t->Branch(aliname, &var[j], aliname+"/D");

	t->Draw(fml, "", "goff");		

	for (int i=0; i<t->GetEntries(); ++i) 
        {
		t->GetEntry(i);
		var[j] = t->GetV1()[i];
		b->Fill();
	}
}
TFile *f=new TFile(fnameNewTree,"RECREATE");
t->SetDirectory(f);
t->Write();
f->Close();

But this leads to the situation, that the new branches are filled (in the TTree in the output file), but all original branches are empty with the error.

Error in TFile::ReadBuffer: error reading all requested bytes from file friend_data714.root, got 0 of 23901

Anybody an idea how I can fix this?

Best regards and thanks in advance,
Klaus


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


The data is already read by Draw, no need to call GetEntry. The b is created for a single double (likely as expected) and thus b->Fill() only looks at var[0].

TFile *f=new TFile(fnameNewTree,"RECREATE");
t->SetDirectory(f);
t->Write();
f->Close();

will only copy the meta data. The data will be stored in which ever file the TTree belongs too when baskets are full during the b->Fill()

Try

t->SetBranchStatus("*",1);
t->SetEstimate(-1);

TList *alilist = t->GetListOfAliases();
Double_t var;

// Alternative you can (instead) open the input file in UPDATE mode.
TFile *f=new TFile(fnameNewTree,"RECREATE");
TTree *newtree = t->CloneTree(-1,"fast"); // Copy the existing data in the new file.

for (int j=0; j<=alilist->GetLast(); ++j) 
{
	TString aliname = ((TNamed*)alilist->At(j))->GetName();
	TString fml     = t->GetAlias(aliname);
	TBranch *b      = newtree->Branch(aliname, &var, aliname+"/D");

	t->Draw(fml, "", "goff");		

	for (int i=0; i<t->GetEntries(); ++i) 
        {
		var = t->GetV1()[i];
		b->Fill();
	}
}

f->Write();
delete f;

Thanks a lot, that did it!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.