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 (!strncmp(cl->GetName(), "pair<", 5)
119 || !strncmp(cl->GetName(), "std::pair<", 10)) {
120 TClassEdit::TSplitType split(cl->GetName());
121 if (split.fElements.size() == 3) {
122 for (int arg = 1; arg < 3; ++arg) {
123 TClass* clArg = TClass::GetClass(split.fElements[arg].c_str());
124 if (clArg) AddHeader(clArg);
125 }
126 }
127 }
128 // Add directive (if it is not added already)
129 if (directive.Length()) {
130 TIter i( &fListOfHeaders );
131 for(TNamed *n = (TNamed *)i(); n; n = (TNamed*)i()) {
132 if (directive == n->GetTitle()) {
133 return;
134 }
135 }
136 fListOfHeaders.Add(new TNamed(cl->GetName(), directive.Data()));
137 }
138 }
139
140 ////////////////////////////////////////////////////////////////////////////////
141 /// Add a header inclusion request. If the header is already included it will
142 /// not be included again.
143
144 void TTreeGeneratorBase::AddHeader(const char *classname)
145 {
146 AddHeader(TClass::GetClass(classname));
147 }
148
149 ////////////////////////////////////////////////////////////////////////////////
150 /// Get name of class inside a container.
151
153 {
154 TString cname = branch->GetClonesName();
155 if (cname.Length()==0) {
156 // We may have any unsplit clones array
157 Long64_t i = branch->GetTree()->GetReadEntry();
158 if (i<0) i = 0;
159 branch->GetEntry(i);
160 char *obj = branch->GetObject();
161
162 TBranchElement *parent = (TBranchElement*)branch->GetMother()->GetSubBranch(branch);
163 const char *pclname = parent->GetClassName();
164
165 TClass *clparent = TClass::GetClass(pclname);
166 // TClass *clm = TClass::GetClass(GetClassName());
167 Int_t lOffset = 0; // offset in the local streamerInfo.
168 if (clparent) {
169 const char *ename = 0;
170 if (element) {
171 ename = element->GetName();
172 lOffset = clparent->GetStreamerInfo()->GetOffset(ename);
173 } else {
174 lOffset = 0;
175 }
176 }
177 else Error("AnalyzeBranch", "Missing parent for %s.", branch->GetName());
178
179 TClonesArray *arr;
180 if (ispointer) {
181 arr = (TClonesArray*)*(void**)(obj+lOffset);
182 } else {
183 arr = (TClonesArray*)(obj+lOffset);
184 }
185 cname = arr->GetClass()->GetName();
186 }
187 if (cname.Length()==0) {
188 Error("AnalyzeBranch",
189 "Introspection of TClonesArray in older file not implemented yet.");
190 }
191 return cname;
192 }
193
194 ////////////////////////////////////////////////////////////////////////////////
195 /// Check if element is a base class and if yes, return the base class.
196
198 {
199 TStreamerBase *base = dynamic_cast<TStreamerBase*>(element);
200 if (base) {
202 if (info) return info;
203 }
204 return 0;
205 }
206
207 ////////////////////////////////////////////////////////////////////////////////
208 /// Return the correct TStreamerInfo of class 'cl' in the list of branches
209 /// (current) [Assuming these branches correspond to a flattened version of
210 /// the class.]
211
213 {
214 TVirtualStreamerInfo *objInfo = 0;
215 TBranchElement *b = 0;
216 TString cname = cl->GetName();
217
218 while( ( b = (TBranchElement*)current() ) ) {
219 if ( cname == b->GetInfo()->GetName() ) {
220 objInfo = b->GetInfo();
221 break;
222 }
223 }
224 if (objInfo == 0 && branch->GetTree()->GetDirectory()->GetFile()) {
225 const TList *infolist = branch->GetTree()->GetDirectory()->GetFile()->GetStreamerInfoCache();
226 if (infolist) {
228 if (i) {
229 // NOTE: Is this correct for Foreigh classes?
231 }
232 }
233 }
234 if (objInfo == 0) {
235 // We still haven't found it ... this is likely to be an STL collection .. anyway, use the current StreamerInfo.
236 objInfo = cl->GetStreamerInfo();
237 }
238 return objInfo;
239 }
240
241} // namespace Internal
242} // namespace ROOT
#define b(i)
Definition: RSha256.hxx:100
int Int_t
Definition: RtypesCore.h:41
int Ssiz_t
Definition: RtypesCore.h:63
bool Bool_t
Definition: RtypesCore.h:59
long long Long64_t
Definition: RtypesCore.h:69
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:2000
TBranch * GetMother() const
Get our top-level parent branch in the tree.
Definition: TBranch.cxx:1979
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:75
TVirtualStreamerInfo * GetStreamerInfo(Int_t version=0) const
returns a pointer to the TVirtualStreamerInfo object for version If the object does not exist,...
Definition: TClass.cxx:4440
ROOT::ESTLType GetCollectionType() const
Return the 'type' of the STL the TClass is representing.
Definition: TClass.cxx:2824
TVirtualCollectionProxy * GetCollectionProxy() const
Return the proxy describing the collection (if any).
Definition: TClass.cxx:2835
const char * GetDeclFileName() const
Return name of the file containing the declaration of this class.
Definition: TClass.cxx:3323
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:2906
An array of clone (identical) objects.
Definition: TClonesArray.h:32
TClass * GetClass() const
Definition: TClonesArray.h:56
virtual TFile * GetFile() const
Definition: TDirectory.h:157
const TList * GetStreamerInfoCache()
Returns the cached list of StreamerInfos used in this file.
Definition: TFile.cxx:1261
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:575
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:72
TDirectory * GetDirectory() const
Definition: TTree.h:449
virtual Long64_t GetReadEntry() const
Definition: TTree.h:496
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
VSD Structures.
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
Definition: tree.py:1
std::vector< std::string > fElements
Definition: TClassEdit.h:140