Cloning ROOT file

The code below makes a new ROOT file “newnew.root”, with the correct contents, when I use the line:
mychain->SetBranchAddress(“hit_EnergyF”, &hit_Energy);
However, in this case, I don’t have access to the value of hit_Energy inside the code (and cannot manipulate it). I.e., hit_Energy is always 0.
However, when I do:
mychain->SetBranchAddress(“hit_Energy”, &hit_Energy);
I do have access to hit_Energy (it does have a value inside the code), but then, when I later open “newnew.root”, the value for hit_Energy is always 0.
Why? And how can I make this work properly?

[code]void Main()
{
mychain = new TChain(“GmSDTTree”);
mychain->Add(“GmSDTTreeUA_tree_1000.root”);

Int_t nentries = (Int_t) mychain->GetEntries();
cout << "nentries: " << nentries << endl;

// output root file
TFile fnew("newnew.root", "RECREATE");
TTree* newtree = mychain->CloneTree(0);

// Load parts of tree in memory
Double_t    hit_Energy;
mychain->SetBranchAddress("hit_EnergyF", &hit_Energy);

// Loop over entries
for (int i = 0; i < 10000 /*nentries*/ ; i++) 
{
	mychain->GetEntry(i);
	newtree->Fill();
}
newtree->AutoSave();
newtree->FlushBaskets();

fnew.cd();
newtree->Print();
newtree->AutoSave();

}
[/code]

Ok, got the answer myself. The names of the pointers should not be the same as the name of the TREE variables. The names in the brackets should be identical to the TREE variable names.

It still does not explain why I do have to explicitly create a branchaddres for the new ROOT file, once I have accessed a variable in the old ROOT file. Why do I have to do “hit_Energy_new = hit_Energy_old;” ?

Like this:

[code]void Main()
{
mychain = new TChain(“GmSDTTree”);
mychain->Add(“GmSDTTreeUA_tree_1000.root”);

Int_t nentries = (Int_t) mychain->GetEntries();
cout << "nentries: " << nentries << endl;

// output root file
TFile fnew("newnew.root", "RECREATE");
TTree* newtree = mychain->CloneTree(0);

// Load parts of tree in memory
Double_t    hit_Energy_old;
mychain->SetBranchAddress("hit_Energy", &hit_Energy_old);

Double_t    hit_Energy_new;
newtree->SetBranchAddress("hit_Energy", &hit_Energy_new);

// Loop over entries
for (int i = 0; i < 10000 /*nentries*/ ; i++) 
{
	mychain->GetEntry(i);
	newtree->Fill();

cout << hit_Energy_old << endl;
hit_Energy_new = hit_Energy_old;
}
newtree->AutoSave();
newtree->FlushBaskets();

fnew.cd();
newtree->Print();
newtree->AutoSave();

}
[/code]

[quote] Double_t hit_Energy;
mychain->SetBranchAddress(“hit_EnergyF”, &hit_Energy);[/quote]Is the branch ‘hit_EnergyF’ really containing a double (the ‘F’ sounds like it would be a float instead).

[quote]It still does not explain why I do have to explicitly create a branchaddres for the new ROOT file, once I have accessed a variable in the old ROOT file[/quote]You should not have to. The address are suppose to be propagated from the original to the clone.

Philippe.