Sorry, I attached a non-working version of the macro. Here is the correct
one.
Thomas.
void test()
{
//
// Write a simple file file simple objects...
//
TFile f("test.root", "recreate");
TTree *t = new TTree("Events", "Test");
TNamed *named = new TNamed;
TNamed *named2 = new TNamed;
t->Branch("TNamed.", "TNamed", &named);
t->Branch("TNamed2.", "TNamed", &named2);
for (int i=0; i<20000; i++)
{
named->SetName(Form("Name #%d", i));
t->Fill();
}
f.Write();
f.Close();
TStopwatch w;
//
// This is much much too slow. It should be as fast as chain2
// below, but because GetEntry() is not called before setting
// the branch status it's slow like a snake!
//
// For our own class (a copy of TArrayD but derived from TNamed)
// this also results in a big memory leak!
//
TChain chain("Events");
chain.Add("test.root");
chain.SetBranchAddress("TNamed.", &named);
chain.SetBranchStatus("*", 0);
chain.SetBranchStatus("TNamed.*", 1);
w.Start();
for (int i=0; i<chain.GetEntries(); i++)
{
chain.GetEntry(i);
if (i%5000==0) named->Print();
}
w.Stop();
w.Print();
//
// Reading the first entry before setting the branch status
// results in only reading "TNamed."
//
TChain chain2("Events");
chain2.Add("test.root");
chain2.SetBranchAddress("TNamed.", &named);
chain2.GetEntry(0);
chain2.SetBranchStatus("*", 0);
chain2.SetBranchStatus("TNamed.*", 1);
w.Start();
for (int i=0; i<chain2.GetEntries(); i++)
{
chain2.GetEntry(i);
if (i%5000==0) named->Print();
}
w.Stop();
w.Print();
//
// For comparison: Not setting the branch status is about
// two times slower, becuase "TNamed." and "TNamed2." are read
//
TChain chain3("Events");
chain3.Add("test.root");
chain3.SetBranchAddress("TNamed.", &named);
w.Start();
for (int i=0; i<chain3.GetEntries(); i++)
{
chain3.GetEntry(i);
if (i%5000==0) named->Print();
}
w.Stop();
w.Print();
}
This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:16 MET