ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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"
24 #include "TVirtualStreamerInfo.h"
25 
26 namespace ROOT {
27 namespace 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
49  if (cl->GetCollectionProxy() && cl->GetCollectionProxy()->GetValueClass()) {
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)) {
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) {
227  TVirtualStreamerInfo *i = (TVirtualStreamerInfo *)infolist->FindObject(cname);
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
long long Long64_t
Definition: RtypesCore.h:69
Ssiz_t Length() const
Definition: TString.h:390
const char * current
Definition: demos.C:12
virtual TClass * GetValueClass() const =0
virtual Long64_t GetReadEntry() const
Definition: TTree.h:428
char * GetObject() const
Return a pointer to our object.
void AddHeader(TClass *cl)
Add a header inclusion request.
static const char * filename()
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:497
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:558
TString GetContainedClassName(TBranchElement *branch, TStreamerElement *element, Bool_t ispointer)
Get name of class inside a container.
def split
helpers ------------------------------------------------------------------—
Definition: ROOT.py:168
virtual const char * GetClonesName() const
const char * Data() const
Definition: TString.h:349
virtual Int_t GetClassVersion() const =0
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
TStreamerInfo * GetInfo() const
Get streamer info for the branch class.
virtual Int_t GetOffset(const char *) const =0
TString & Append(const char *cs)
Definition: TString.h:492
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of a BranchElement and return total number of bytes.
TTreeGeneratorBase(TTree *tree, const char *option)
Constructor.
A doubly linked list.
Definition: TList.h:47
static const char * what
Definition: stlLoader.cc:6
virtual TFile * GetFile() const
Definition: TDirectory.h:155
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:4256
TDirectory * GetDirectory() const
Definition: TTree.h:385
TBranch * GetMother() const
Get our top-level parent branch in the tree.
Definition: TBranch.cxx:1533
TVirtualStreamerInfo * GetStreamerInfo(TBranch *branch, TIter current, TClass *cl)
Return the correct TStreamerInfo of class 'cl' in the list of branches (current) [Assuming these bran...
char * Form(const char *fmt,...)
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
TBranch * GetSubBranch(const TBranch *br) const
Find the parent branch of child.
Definition: TBranch.cxx:1554
TTree * GetTree() const
Definition: TBranch.h:183
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2227
A Branch for the case of an object.
int Ssiz_t
Definition: RtypesCore.h:63
tuple tree
Definition: tree.py:24
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any. ...
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:2801
TVirtualCollectionProxy * GetCollectionProxy() const
Return the proxy describing the collection (if any).
Definition: TClass.cxx:2730
const TList * GetStreamerInfoCache()
Returns the cached list of StreamerInfos used in this file.
Definition: TFile.cxx:1298
Mother of all ROOT objects.
Definition: TObject.h:58
const char * GetDeclFileName() const
Definition: TClass.h:385
An array of clone (identical) objects.
Definition: TClonesArray.h:32
virtual void Add(TObject *obj)
Definition: TList.h:81
TVirtualStreamerInfo * GetBaseStreamerInfo() const
ROOT::ESTLType GetCollectionType() const
Return the 'type' of the STL the TClass is representing.
Definition: TClass.cxx:2719
A TTree object has a header with a name and a title.
Definition: TTree.h:98
A TTree is a list of TBranches.
Definition: TBranch.h:58
Abstract Interface class describing Streamer information for one class.
TVirtualStreamerInfo * GetBaseClass(TStreamerElement *element)
Check if element is a base class and if yes, return the base class.
TObject * obj
const Int_t n
Definition: legend1.C:16
void Error(ErrorHandler_t func, int code, const char *va_(fmt),...)
Write error message and call a handler, if required.