From d225f942b59b5b022cf635a16e90f84cad59819d Mon Sep 17 00:00:00 2001
From: Philippe Canal <pcanal@fnal.gov>
Date: Tue, 8 Sep 2020 18:30:37 -0500
Subject: [PATCH] Correct FullName of the index leaf of a collection

---
 tree/tree/inc/TBranch.h          |  2 +-
 tree/tree/inc/TBranchElement.h   |  1 +
 tree/tree/src/TBranchElement.cxx | 33 ++++++++++++++++++++++++++++----
 tree/tree/src/TLeafElement.cxx   |  7 ++++++-
 4 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/tree/tree/inc/TBranch.h b/tree/tree/inc/TBranch.h
index 234e5d37da9..a0c128c9b67 100644
--- a/tree/tree/inc/TBranch.h
+++ b/tree/tree/inc/TBranch.h
@@ -222,7 +222,7 @@ public:
    virtual Int_t     GetEntryExport(Long64_t entry, Int_t getall, TClonesArray *list, Int_t n);
            Int_t     GetEntryOffsetLen() const { return fEntryOffsetLen; }
            Int_t     GetEvent(Long64_t entry=0) {return GetEntry(entry);}
-   TString           GetFullName() const;
+   virtual TString   GetFullName() const;
    const char       *GetIconName() const;
    virtual Int_t     GetExpectedType(TClass *&clptr,EDataType &type);
    virtual TLeaf    *GetLeaf(const char *name) const;
diff --git a/tree/tree/inc/TBranchElement.h b/tree/tree/inc/TBranchElement.h
index 2d19840596d..4a4ba9993ea 100644
--- a/tree/tree/inc/TBranchElement.h
+++ b/tree/tree/inc/TBranchElement.h
@@ -190,6 +190,7 @@ public:
    TClass                  *GetCurrentClass(); // Class referenced by transient description
    virtual Int_t            GetEntry(Long64_t entry = 0, Int_t getall = 0);
    virtual Int_t            GetExpectedType(TClass *&clptr,EDataType &type);
+   virtual TString          GetFullName() const;
            const char      *GetIconName() const;
            Int_t            GetID() const { return fID; }
            TStreamerInfo   *GetInfo() const;
diff --git a/tree/tree/src/TBranchElement.cxx b/tree/tree/src/TBranchElement.cxx
index a0bcb3ae09d..4055f07317f 100644
--- a/tree/tree/src/TBranchElement.cxx
+++ b/tree/tree/src/TBranchElement.cxx
@@ -540,6 +540,8 @@ void TBranchElement::Init(TTree *tree, TBranch *parent,const char* bname, TStrea
                element->SetTitle(atitle.Data());
             }
             TString branchname( name );
+            if (branchname.EndsWith("."))
+               branchname.Remove(branchname.Length()-1);
             branchname += "_";
             SetTitle(branchname);
             leaf->SetName(branchname);
@@ -592,6 +594,8 @@ void TBranchElement::Init(TTree *tree, TBranch *parent,const char* bname, TStrea
                   element->SetTitle(atitle.Data());
                }
                TString branchname (name);
+               if (branchname.EndsWith("."))
+                  branchname.Remove(branchname.Length()-1);
                branchname += "_";
                SetTitle(branchname);
                leaf->SetName(branchname);
@@ -731,8 +735,12 @@ void TBranchElement::Init(TTree *tree, TBranch *parent, const char* bname, TClon
    fDirectory     = fTree->GetDirectory();
    fFileName      = "";
 
-   SetName(bname);
-   const char* name = GetName();
+   TString name( bname );
+   if (name[name.Length()-1]=='.') {
+      name.Remove(name.Length()-1);
+   }
+
+   SetName(name);
    SetTitle(name);
    //fClassName = fInfo->GetName();
    fCompress = compress;
@@ -773,7 +781,7 @@ void TBranchElement::Init(TTree *tree, TBranch *parent, const char* bname, TClon
       // ===> create sub branches for each data member of a TClonesArray
       fClonesName = clonesClass->GetName();
       fClonesClass = clonesClass;
-      std::string branchname = name + std::string("_");
+      std::string branchname = name.Data() + std::string("_");
       SetTitle(branchname.c_str());
       leaf->SetName(branchname.c_str());
       leaf->SetTitle(branchname.c_str());
@@ -2739,6 +2747,23 @@ Int_t TBranchElement::GetExpectedType(TClass *&expectedClass,EDataType &expected
    return 0;
 }
 
+////////////////////////////////////////////////////////////////////////////////
+/// Return the 'full' name of the branch.  In particular prefix  the mother's name
+/// when it does not end in a trailing dot and thus is not part of the branch name
+TString TBranchElement::GetFullName() const
+{
+   TBranchElement* mother = static_cast<TBranchElement*>(GetMother());
+   if (!mother || mother==this || mother->GetType() == 3 || mother->GetType() == 4) {
+      // The parent's name is already included in the name for split TClonesArray and STL collections
+      return fName;
+   }
+   TString motherName(mother->GetName());
+   if (motherName.Length() && (motherName[motherName.Length()-1] == '.')) {
+      return fName;
+   }
+   return motherName + "." + fName;
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 /// Return icon name depending on type of branch element.
 
@@ -3132,7 +3157,7 @@ void TBranchElement::InitializeOffsets()
       {
          TBranch *br = GetMother()->GetSubBranch( this );
          stlParentName = br->GetName();
-         stlParentName.Strip( TString::kTrailing, '.' );
+         stlParentName = stlParentName.Strip( TString::kTrailing, '.' );
 
          // We may ourself contain the 'Mother' branch name.
          // To avoid code duplication, we delegate the removal
diff --git a/tree/tree/src/TLeafElement.cxx b/tree/tree/src/TLeafElement.cxx
index 2ed8da6ad50..92cc2321f48 100644
--- a/tree/tree/src/TLeafElement.cxx
+++ b/tree/tree/src/TLeafElement.cxx
@@ -162,7 +162,12 @@ TMethodCall *TLeafElement::GetMethodCall(const char * /*name*/)
 
 TString TLeafElement::GetFullName() const
 {
-   return GetBranch()->GetFullName();
+   TBranchElement *br = static_cast<TBranchElement*>(GetBranch());
+   if (br->GetType() == 3 || br->GetType() == 4) {
+      //      return TString(br->GetFullName()) + "." + GetName() +  "_";
+      return TString(br->GetFullName()) + "_";
+   } else
+      return GetBranch()->GetFullName();
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-- 
GitLab