Logo ROOT  
Reference Guide
TTreeGeneratorBase.cxx
Go to the documentation of this file.
1// @(#)root/treeplayer:$Id$
2// Author: Akos Hajdu 13/08/2015
3
4/*************************************************************************
5 * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers and al. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TTreeGeneratorBase.h"
13
14#include "TBranchElement.h"
15#include "TClass.h"
16#include "TClassEdit.h"
17#include "TClonesArray.h"
18#include "TDirectory.h"
19#include "TFile.h"
20#include "TStreamerElement.h"
21#include "TStreamerInfo.h"
22#include "TTree.h"
25
26namespace ROOT {
27namespace Internal {
28
29 ////////////////////////////////////////////////////////////////////////////////
30 /// Constructor.
31
32 TTreeGeneratorBase::TTreeGeneratorBase(TTree *tree, const char *option) : fTree(tree), fOptionStr(option) { }
33
34 ////////////////////////////////////////////////////////////////////////////////
35 /// Add a header inclusion request. If the header is already included it will
36 /// not be included again.
37
39 {
40 if (cl==0) return;
41
42 // Check if already included
44 if (obj) return;
45
46 TString directive;
47
48 // Extract inner class from collection
51 }
52
53 // Construct directive
54 Int_t stlType;
55 if (0 == strcmp(cl->GetName(), "string")) { // Check if it is a string
56 directive = "#include <string>\n";
57 } else if (cl->GetCollectionProxy() && (stlType = cl->GetCollectionType())) { // Check if it is an STL container
58 const char *what = "";
59 switch(stlType) {
60 case ROOT::kSTLvector: what = "vector"; break;
61 case ROOT::kSTLlist: what = "list"; break;
62 case ROOT::kSTLforwardlist: what = "forward_list"; break;
63 case -ROOT::kSTLdeque: // same as positive
64 case ROOT::kSTLdeque: what = "deque"; break;
65 case -ROOT::kSTLmap: // same as positive
66 case ROOT::kSTLmap: what = "map"; break;
67 case -ROOT::kSTLmultimap: // same as positive
68 case ROOT::kSTLmultimap: what = "map"; break;
69 case -ROOT::kSTLset: // same as positive
70 case ROOT::kSTLset: what = "set"; break;
71 case -ROOT::kSTLmultiset: // same as positive
72 case ROOT::kSTLmultiset: what = "set"; break;
73 case -ROOT::kSTLunorderedset: // same as positive
74 case ROOT::kSTLunorderedset: what = "unordered_set"; break;
75 case -ROOT::kSTLunorderedmultiset: // same as positive
76 case ROOT::kSTLunorderedmultiset: what = "unordered_multiset"; break;
77 case -ROOT::kSTLunorderedmap: // same as positive
78 case ROOT::kSTLunorderedmap: what = "unordered_map"; break;
79 case -ROOT::kSTLunorderedmultimap: // same as positive
80 case ROOT::kSTLunorderedmultimap: what = "unordered_multimap"; break;
81 }
82 if (what[0]) {
83 directive = "#include <";
84 directive.Append(what);
85 directive.Append(">\n");
86 }
87 } else if (cl->GetDeclFileName() && strlen(cl->GetDeclFileName()) ) { // Custom file
88 const char *filename = cl->GetDeclFileName();
89
90 if (!filename) return;
91
92#ifdef R__WIN32
93 TString inclPath("include;prec_stl"); // GetHtml()->GetIncludePath());
94#else
95 TString inclPath("include:prec_stl"); // GetHtml()->GetIncludePath());
96#endif
97 Ssiz_t posDelim = 0;
98 TString inclDir;
99 TString sIncl(filename);
100#ifdef R__WIN32
101 const char* pdelim = ";";
102 static const char ddelim = '\\';
103#else
104 const char* pdelim = ":";
105 static const char ddelim = '/';
106#endif
107 while (inclPath.Tokenize(inclDir, posDelim, pdelim))
108 {
109 if (sIncl.BeginsWith(inclDir)) {
110 filename += inclDir.Length();
111 if (filename[0] == ddelim || filename[0] == '/') {
112 ++filename;
113 }
114 break;
115 }
116 }
117 directive = Form("#include \"%s\"\n",filename);
118 } else if (TClassEdit::IsStdPair(cl->GetName())) {
119 TClassEdit::TSplitType split(cl->GetName());
120 // 4 elements expected: "pair", "first type name", "second type name", "trailing stars"
121 // However legacy code had a test for 3, we will leave it here until
122 // a test is developed (or found :) ) that exercise these lines of code.
123 if (split.fElements.size() == 3 || split.fElements.size() == 4) {
124 for (int arg = 1; arg < 3; ++arg) {
125 TClass* clArg = TClass::GetClass(split.fElements[arg].c_str());
126 if (clArg) AddHeader(clArg);
127 }
128 }
129 }
130 // Add directive (if it is not added already)
131 if (directive.Length()) {
132 TIter i( &fListOfHeaders );
133 for(TNamed *n = (TNamed *)i(); n; n = (TNamed*)i()) {
134 if (directive == n->GetTitle()) {
135 return;
136 }
137 }
138 fListOfHeaders.Add(new TNamed(cl->GetName(), directive.Data()));
139 }
140 }
141
142 ////////////////////////////////////////////////////////////////////////////////
143 /// Add a header inclusion request. If the header is already included it will
144 /// not be included again.
145
146 void TTreeGeneratorBase::AddHeader(const char *classname)
147 {
148 AddHeader(TClass::GetClass(classname));
149 }
150
151 ////////////////////////////////////////////////////////////////////////////////
152 /// Get name of class inside a container.
153
155 {
156 TString cname = branch->GetClonesName();
157 if (cname.Length()==0) {
158 // We may have any unsplit clones array
159 Long64_t i = branch->GetTree()->GetReadEntry();
160 if (i<0) i = 0;
161 branch->GetEntry(i);
162 char *obj = branch->GetObject();
163
164 TBranchElement *parent = (TBranchElement*)branch->GetMother()->GetSubBranch(branch);
165 const char *pclname = parent->GetClassName();
166
167 TClass *clparent = TClass::GetClass(pclname);
168 // TClass *clm = TClass::GetClass(GetClassName());
169 Int_t lOffset = 0; // offset in the local streamerInfo.
170 if (clparent) {
171 const char *ename = 0;
172 if (element) {
173 ename = element->GetName();
174 lOffset = clparent->GetStreamerInfo()->GetOffset(ename);
175 } else {
176 lOffset = 0;
177 }
178 }
179 else Error("AnalyzeBranch", "Missing parent for %s.", branch->GetName());
180
181 TClonesArray *arr;
182 if (ispointer) {
183 arr = (TClonesArray*)*(void**)(obj+lOffset);
184 } else {
185 arr = (TClonesArray*)(obj+lOffset);
186 }
187 cname = arr->GetClass()->GetName();
188 }
189 if (cname.Length()==0) {
190 Error("AnalyzeBranch",
191 "Introspection of TClonesArray in older file not implemented yet.");
192 }
193 return cname;
194 }
195
196 ////////////////////////////////////////////////////////////////////////////////
197 /// Check if element is a base class and if yes, return the base class.
198
200 {
201 TStreamerBase *base = dynamic_cast<TStreamerBase*>(element);
202 if (base) {
204 if (info) return info;
205 }
206 return 0;
207 }
208
209 ////////////////////////////////////////////////////////////////////////////////
210 /// Return the correct TStreamerInfo of class 'cl' in the list of branches
211 /// (current) [Assuming these branches correspond to a flattened version of
212 /// the class.]
213
215 {
216 TVirtualStreamerInfo *objInfo = 0;
217 TBranchElement *b = 0;
218 TString cname = cl->GetName();
219
220 while( ( b = (TBranchElement*)current() ) ) {
221 if ( cname == b->GetInfo()->GetName() ) {
222 objInfo = b->GetInfo();
223 break;
224 }
225 }
226 if (objInfo == 0 && branch->GetTree()->GetDirectory()->GetFile()) {
227 const TList *infolist = branch->GetTree()->GetDirectory()->GetFile()->GetStreamerInfoCache();
228 if (infolist) {
230 if (i) {
231 // NOTE: Is this correct for Foreigh classes?
233 }
234 }
235 }
236 if (objInfo == 0) {
237 // We still haven't found it ... this is likely to be an STL collection .. anyway, use the current StreamerInfo.
238 objInfo = cl->GetStreamerInfo();
239 }
240 return objInfo;
241 }
242
243} // namespace Internal
244} // namespace ROOT
#define b(i)
Definition: RSha256.hxx:100
long long Long64_t
Definition: RtypesCore.h:71
void Error(const char *location, const char *msgfmt,...)
char * Form(const char *fmt,...)
void AddHeader(TClass *cl)
Add a header inclusion request.
TVirtualStreamerInfo * GetStreamerInfo(TBranch *branch, TIter current, TClass *cl)
Return the correct TStreamerInfo of class 'cl' in the list of branches (current) [Assuming these bran...
TVirtualStreamerInfo * GetBaseClass(TStreamerElement *element)
Check if element is a base class and if yes, return the base class.
TString GetContainedClassName(TBranchElement *branch, TStreamerElement *element, Bool_t ispointer)
Get name of class inside a container.
TTreeGeneratorBase(TTree *tree, const char *option)
Constructor.
A Branch for the case of an object.
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any.
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of a BranchElement and return total number of bytes.
char * GetObject() const
Return a pointer to our object.
virtual const char * GetClonesName() const
A TTree is a list of TBranches.
Definition: TBranch.h:91
TTree * GetTree() const
Definition: TBranch.h:250
TBranch * GetSubBranch(const TBranch *br) const
Find the parent branch of child.
Definition: TBranch.cxx:2028
TBranch * GetMother() const
Get our top-level parent branch in the tree.
Definition: TBranch.cxx:1991
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
ROOT::ESTLType GetCollectionType() const
Return the 'type' of the STL the TClass is representing.
Definition: TClass.cxx:2866
TVirtualStreamerInfo * GetStreamerInfo(Int_t version=0, Bool_t isTransient=kFALSE) const
returns a pointer to the TVirtualStreamerInfo object for version If the object does not exist,...
Definition: TClass.cxx:4562
TVirtualCollectionProxy * GetCollectionProxy() const
Return the proxy describing the collection (if any).
Definition: TClass.cxx:2877
const char * GetDeclFileName() const
Return name of the file containing the declaration of this class.
Definition: TClass.cxx:3431
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition: TClass.cxx:2948
An array of clone (identical) objects.
Definition: TClonesArray.h:32
TClass * GetClass() const
Definition: TClonesArray.h:56
virtual TFile * GetFile() const
Definition: TDirectory.h:163
const TList * GetStreamerInfoCache()
Returns the cached list of StreamerInfos used in this file.
Definition: TFile.cxx:1278
A doubly linked list.
Definition: TList.h:44
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:577
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:37
TVirtualStreamerInfo * GetBaseStreamerInfo() const
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
const char * Data() const
Definition: TString.h:364
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2197
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:610
TString & Append(const char *cs)
Definition: TString.h:559
A TTree represents a columnar dataset.
Definition: TTree.h:78
TDirectory * GetDirectory() const
Definition: TTree.h:456
virtual Long64_t GetReadEntry() const
Definition: TTree.h:503
virtual TClass * GetValueClass() const =0
Abstract Interface class describing Streamer information for one class.
virtual Int_t GetOffset(const char *) const =0
virtual Int_t GetClassVersion() const =0
const Int_t n
Definition: legend1.C:16
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21
@ kSTLmap
Definition: ESTLType.h:33
@ kSTLunorderedmultiset
Definition: ESTLType.h:43
@ kSTLset
Definition: ESTLType.h:35
@ kSTLmultiset
Definition: ESTLType.h:36
@ kSTLdeque
Definition: ESTLType.h:32
@ kSTLvector
Definition: ESTLType.h:30
@ kSTLunorderedmultimap
Definition: ESTLType.h:45
@ kSTLunorderedset
Definition: ESTLType.h:42
@ kSTLlist
Definition: ESTLType.h:31
@ kSTLforwardlist
Definition: ESTLType.h:41
@ kSTLunorderedmap
Definition: ESTLType.h:44
@ kSTLmultimap
Definition: ESTLType.h:34
bool IsStdPair(std::string_view name)
Definition: TClassEdit.h:190
Definition: tree.py:1
static const char * what
Definition: stlLoader.cc:6
std::vector< std::string > fElements
Definition: TClassEdit.h:140