From e2d0b7e94aea1f93dc6a760594c8c164d5a3b6cc Mon Sep 17 00:00:00 2001
From: Rene Brun <Rene.Brun@cern.ch>
Date: Sun, 6 Jul 2003 19:41:49 +0000
Subject: [PATCH] From Philippe: This patch comments the fact after
 TTree::CloneTree and until the original is deleted, the addresses of the
 original are passed on to all the clones.  Also the addresses of the clones
 are reset when the original is deleted.

This patch also simplify the implementation of TChain::Merge to take advantage of the new TTree::CloneTree feature.


git-svn-id: http://root.cern.ch/svn/root/trunk@6838 27541ba8-7e3a-0410-8455-c3a389f83636
---
 test/stress.cxx                |  7 +-----
 tree/src/TChain.cxx            | 44 ++++------------------------------
 tree/src/TTree.cxx             | 24 +++++++++++++------
 treeplayer/src/TTreePlayer.cxx | 13 +++++++---
 4 files changed, 32 insertions(+), 56 deletions(-)

diff --git a/test/stress.cxx b/test/stress.cxx
index fd7916fd440..1fb2d40ade5 100644
--- a/test/stress.cxx
+++ b/test/stress.cxx
@@ -1,4 +1,4 @@
-// @(#)root/test:$Name:  $:$Id: stress.cxx,v 1.44 2003/06/17 16:45:25 brun Exp $
+// @(#)root/test:$Name:  $:$Id: stress.cxx,v 1.45 2003/06/22 13:51:01 brun Exp $
 // Author: Rene Brun   05/11/98
 
 /////////////////////////////////////////////////////////////////
@@ -1258,16 +1258,11 @@ void stress13()
       chain->Add(filename);
    }
 
-   Event *event = 0;
-   chain->SetBranchAddress("event",&event);
-
    chain->Merge("Event.root");
 
    Double_t chentries = chain->GetEntries();
    delete chain;
 
-   event->ResetHistogramPointer(); // fH was deleted above!!
-   delete event;
    Event::Reset();
    gROOT->GetList()->Delete();
 
diff --git a/tree/src/TChain.cxx b/tree/src/TChain.cxx
index 017b54639dc..b6849dc65ac 100644
--- a/tree/src/TChain.cxx
+++ b/tree/src/TChain.cxx
@@ -1,4 +1,4 @@
-// @(#)root/tree:$Name:  $:$Id: TChain.cxx,v 1.69 2003/06/30 15:45:52 brun Exp $
+// @(#)root/tree:$Name:  $:$Id: TChain.cxx,v 1.70 2003/07/04 13:27:35 brun Exp $
 // Author: Rene Brun   03/02/97
 
 /*************************************************************************
@@ -932,24 +932,16 @@ Int_t TChain::Merge(TFile *file, Int_t basketsize, Option_t *option)
 //     if (basketsize > 1000, the basket size for all branches of the
 //     new Tree will be set to basketsize.
 //
-// IMPORTANT Note 1: Before invoking this function, the branch addresses
-//            of the TTree must have been set.
 //  example using the file generated in $ROOTSYS/test/Event
 //  merge two copies of Event.root
 //
 //        gSystem.Load("libEvent");
-//        Event *event = new Event();
 //        TChain ch("T");
-//        ch.SetBranchAddress("event",&event);
 //        ch.Add("Event1.root");
 //        ch.Add("Event2.root");
 //        ch.Merge("all.root");
 //
-//  The SetBranchAddress statement is not necessary if the Tree
-//  contains only basic types (case of files converted from hbook)
-//  NOTE that the merged Tree contains only the active branches.
-//
-// IMPORTANT Note 2: AUTOMATIC FILE OVERFLOW
+// IMPORTANT Note 1: AUTOMATIC FILE OVERFLOW
 // -----------------------------------------
 // When merging many files, it may happen that the resulting file
 // reaches a size > TTree::fgMaxTreeSize (default = 1.9 GBytes). In this case
@@ -958,7 +950,7 @@ Int_t TChain::Merge(TFile *file, Int_t basketsize, Option_t *option)
 // will be named "merged_1.root", "merged_2.root", etc.
 // fgMaxTreeSize may be modified via the static function TTree::SetMaxTreeSize.
 //
-// IMPORTANT Note 3: The input file is automatically closed and deleted.
+// IMPORTANT Note 2: The input file is automatically closed and deleted.
 // This is required because in general the automatic file overflow described
 // above may happen during the merge.
 //
@@ -971,7 +963,7 @@ Int_t TChain::Merge(TFile *file, Int_t basketsize, Option_t *option)
 
 // Clone Chain tree
    //file->cd();  //in case a user wants to write in a file/subdir
-   TTree *hnew = (TTree*)fTree->CloneTree(0);
+   TTree *hnew = CloneTree(0);
    hnew->SetAutoSave(2000000000);
 
 // May be reset branches compression level?
@@ -1000,34 +992,6 @@ Int_t TChain::Merge(TFile *file, Int_t basketsize, Option_t *option)
    Int_t nentries = Int_t(GetEntriesFast());
    for (Int_t i=0;i<nentries;i++) {
       if (GetEntry(i) <= 0) break;
-      if (treeNumber != fTreeNumber) {
-         treeNumber = fTreeNumber;
-         TIter next(fTree->GetListOfBranches());
-         Bool_t failed = kFALSE;
-         while ((branch = (TBranch*)next())) {
-            TBranch *new_branch = hnew->GetBranch( branch->GetName() );
-            if (!new_branch) continue;
-            void *add = branch->GetAddress();
-            // in case branch addresses have not been set, give a last chance
-            // for simple Trees (h2root converted for example)
-            if (!add) {
-               TLeaf *leaf, *new_leaf;
-               TIter next_l(branch->GetListOfLeaves());
-               while ((leaf = (TLeaf*) next_l())) {
-                  add = leaf->GetValuePointer();
-                  if (add) {
-                     new_leaf = new_branch->GetLeaf(leaf->GetName());
-                     if(new_leaf) new_leaf->SetAddress(add);
-                  } else {
-                     failed = kTRUE;
-                  }
-               }
-            } else {
-               new_branch->SetAddress(add);
-            }
-            if (failed) Warning("Merge","Tree branch addresses not defined");
-         }
-      }
       hnew->Fill();
    }
 
diff --git a/tree/src/TTree.cxx b/tree/src/TTree.cxx
index f39e55d34fb..ee10b00223b 100644
--- a/tree/src/TTree.cxx
+++ b/tree/src/TTree.cxx
@@ -1,4 +1,4 @@
-// @(#)root/tree:$Name:  $:$Id: TTree.cxx,v 1.149 2003/06/30 15:45:52 brun Exp $
+// @(#)root/tree:$Name:  $:$Id: TTree.cxx,v 1.150 2003/07/04 13:27:35 brun Exp $
 // Author: Rene Brun   12/01/96
 
 /*************************************************************************
@@ -1479,9 +1479,14 @@ TTree *TTree::CloneTree(Int_t nentries, Option_t *)
 // option is reserved for future use
 // Note that only active branches are copied.
 //
-// IMPORTANT: Before invoking this function, the branch addresses
-//            of this TTree must have been set if one or more branches
-//            is not a basic type.
+// IMPORTANT: The cloned tree stays connected with this tree until this tree
+//            is deleted.  In particular, any changes in branch addresses
+//            in this tree are forwarded to the clone trees.  Any changes 
+//            made to the branch addresses of the cloned trees are over-ridden
+//            anytime this tree changes its branch addresses.
+//            Once this tree is deleted, all the addresses of the cloned tree
+//            are reset to their default values.
+//
 // For examples of CloneTree, see tutorials
 //  -copytree:
 //    Example of Root macro to copy a subset of a Tree to a new Tree
@@ -1630,9 +1635,14 @@ TTree *TTree::CopyTree(const char *selection, Option_t *option, Int_t nentries,
 //*-*-*-*-*-*-*-*-*copy a Tree with selection*-*-*-*-*-*
 //*-*              ==========================
 //
-// IMPORTANT: Before invoking this function, the branch addresses
-//            of this TTree must have been set if one or more branches
-//            is not a basic type.
+// IMPORTANT: The copied tree stays connected with this tree until this tree
+//            is deleted.  In particular, any changes in branch addresses
+//            in this tree are forwarded to the clone trees.  Any changes 
+//            made to the branch addresses of the copied trees are over-ridden
+//            anytime this tree changes its branch addresses.
+//            Once this tree is deleted, all the addresses of the copied tree
+//            are reset to their default values.
+//
 // For examples of CloneTree, see tutorials
 //  -copytree:
 //    Example of Root macro to copy a subset of a Tree to a new Tree
diff --git a/treeplayer/src/TTreePlayer.cxx b/treeplayer/src/TTreePlayer.cxx
index bc73a14b739..d966480dd92 100644
--- a/treeplayer/src/TTreePlayer.cxx
+++ b/treeplayer/src/TTreePlayer.cxx
@@ -1,4 +1,4 @@
-// @(#)root/treeplayer:$Name:  $:$Id: TTreePlayer.cxx,v 1.127 2003/06/27 11:02:34 rdm Exp $
+// @(#)root/treeplayer:$Name:  $:$Id: TTreePlayer.cxx,v 1.128 2003/07/04 13:27:35 brun Exp $
 // Author: Rene Brun   12/01/96
 
 /*************************************************************************
@@ -314,7 +314,14 @@ TTree *TTreePlayer::CopyTree(const char *selection, Option_t *, Int_t nentries,
    // nentries is the number of entries to process (default is all)
    // first is the first entry to process (default is 0)
    //
-   // Note that the branch addresses must be correctly set before calling this function
+   // IMPORTANT: The copied tree stays connected with this tree until this tree
+   //            is deleted.  In particular, any changes in branch addresses
+   //            in this tree are forwarded to the clone trees.  Any changes 
+   //            made to the branch addresses of the copied trees are over-ridden
+   //            anytime this tree changes its branch addresses.
+   //            Once this tree is deleted, all the addresses of the copied tree
+   //            are reset to their default values.
+   //
    // The following example illustrates how to copy some events from the Tree
    // generated in $ROOTSYS/test/Event
    //
@@ -328,7 +335,7 @@ TTree *TTreePlayer::CopyTree(const char *selection, Option_t *, Int_t nentries,
    //   T2->Write();
 
 
-  // we make a copy of the tree header
+   // we make a copy of the tree header
    TTree *tree = fTree->CloneTree(0);
    if (tree == 0) return 0;
 
-- 
GitLab