Logo ROOT   6.08/02
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TTreeReaderArray.cxx
Go to the documentation of this file.
1 // @(#)root/treeplayer:$Id$
2 // Author: Axel Naumann, 2011-09-28
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, 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 "TTreeReaderArray.h"
13 
14 #include "TBranchClones.h"
15 #include "TBranchElement.h"
16 #include "TBranchRef.h"
17 #include "TBranchSTL.h"
18 #include "TBranchProxyDirector.h"
19 #include "TClassEdit.h"
20 #include "TLeaf.h"
21 #include "TROOT.h"
22 #include "TStreamerInfo.h"
23 #include "TStreamerElement.h"
24 #include "TTreeReader.h"
25 #include "TGenCollectionProxy.h"
26 #include "TRegexp.h"
27 
28 // pin vtable
30 
31 namespace {
32  using namespace ROOT::Internal;
33 
34  // Reader interface for clones arrays
35  class TClonesReader: public TVirtualCollectionReader {
36  public:
37  ~TClonesReader() {}
39  if (!proxy->Read()){
41  Error("TClonesReader::GetCA()", "Read error in TBranchProxy.");
42  return 0;
43  }
45  return (TClonesArray*) proxy->GetWhere();
46  }
47  virtual size_t GetSize(ROOT::Detail::TBranchProxy* proxy) {
48  TClonesArray *myClonesArray = GetCA(proxy);
49  if (myClonesArray){
50  return myClonesArray->GetEntries();
51  }
52  else return 0;
53  }
54  virtual void* At(ROOT::Detail::TBranchProxy* proxy, size_t idx) {
55  TClonesArray *myClonesArray = GetCA(proxy);
56  if (myClonesArray){
57  return myClonesArray->UncheckedAt(idx);
58  }
59  else return 0;
60  }
61  };
62 
63  // Reader interface for STL
64  class TSTLReader: public TVirtualCollectionReader {
65  public:
66  ~TSTLReader() {}
68  if (!proxy->Read()) {
70  Error("TSTLReader::GetCP()", "Read error in TBranchProxy.");
71  return 0;
72  }
73  if (!proxy->GetWhere()) {
74  Error("TSTLReader::GetCP()", "Logic error, proxy object not set in TBranchProxy.");
75  return 0;
76  }
77  if (proxy->IsaPointer()) {
78  if (proxy->GetWhere() && *(void**)proxy->GetWhere()){
79  ((TGenCollectionProxy*)proxy->GetCollection())->PopProxy();
80  ((TGenCollectionProxy*)proxy->GetCollection())->PushProxy(*(void**)proxy->GetWhere());
81  }
82  else return 0;
83  }
85  return (TVirtualCollectionProxy*) proxy->GetCollection();
86  }
87 
88  virtual size_t GetSize(ROOT::Detail::TBranchProxy* proxy) {
89  TVirtualCollectionProxy *myCollectionProxy = GetCP(proxy);
90  if (!myCollectionProxy) return 0;
91  return myCollectionProxy->Size();
92  }
93 
94  virtual void* At(ROOT::Detail::TBranchProxy* proxy, size_t idx) {
95  TVirtualCollectionProxy *myCollectionProxy = GetCP(proxy);
96  if (!myCollectionProxy) return 0;
97  if (myCollectionProxy->HasPointers()){
98  return *(void**)myCollectionProxy->At(idx);
99  }
100  else {
101  return myCollectionProxy->At(idx);
102  }
103  }
104  };
105 
106  class TCollectionLessSTLReader : public TVirtualCollectionReader {
107  private:
108  TVirtualCollectionProxy *localCollection;
109  public:
110  TCollectionLessSTLReader(TVirtualCollectionProxy *proxy) : localCollection(proxy) {}
111 
113  if (!proxy->Read()) {
114  fReadStatus = TTreeReaderValueBase::kReadError;
115  Error("TCollectionLessSTLReader::GetCP()", "Read error in TBranchProxy.");
116  return 0;
117  }
118  if (!proxy->GetWhere()) {
119  Error("TCollectionLessSTLReader::GetCP()", "Logic error, proxy object not set in TBranchProxy.");
120  return 0;
121  }
123  return localCollection;
124  }
125 
126  virtual size_t GetSize(ROOT::Detail::TBranchProxy* proxy) {
127  TVirtualCollectionProxy *myCollectionProxy = GetCP(proxy);
128  if (!myCollectionProxy) return 0;
129  TVirtualCollectionProxy::TPushPop ppRaii(myCollectionProxy, proxy->GetWhere());
130  return myCollectionProxy->Size();
131  }
132 
133  virtual void* At(ROOT::Detail::TBranchProxy* proxy, size_t idx) {
134  TVirtualCollectionProxy *myCollectionProxy = GetCP(proxy);
135  if (!myCollectionProxy) return 0;
136  TVirtualCollectionProxy::TPushPop ppRaii(myCollectionProxy, proxy->GetWhere());
137  if (myCollectionProxy->HasPointers()){
138  return *(void**)myCollectionProxy->At(idx);
139  } else {
140  return myCollectionProxy->At(idx);
141  }
142  }
143  };
144 
145 
146  // Reader interface for leaf list
147  // SEE TTreeProxyGenerator.cxx:1319: '//We have a top level raw type'
148  class TObjectArrayReader: public TVirtualCollectionReader {
149  private:
150  Int_t basicTypeSize;
151  public:
152  TObjectArrayReader() : basicTypeSize(-1) { }
153  ~TObjectArrayReader() {}
155  if (!proxy->Read()){
156  fReadStatus = TTreeReaderValueBase::kReadError;
157  Error("TObjectArrayReader::GetCP()", "Read error in TBranchProxy.");
158  return 0;
159  }
161  return (TVirtualCollectionProxy*) proxy->GetCollection();
162  }
163  virtual size_t GetSize(ROOT::Detail::TBranchProxy* proxy) {
164  TVirtualCollectionProxy *myCollectionProxy = GetCP(proxy);
165  if (!myCollectionProxy) return 0;
166  return myCollectionProxy->Size();
167  }
168  virtual void* At(ROOT::Detail::TBranchProxy* proxy, size_t idx) {
169  if (!proxy->Read()) return 0;
170 
171  Int_t objectSize;
172  void *array = (void*)proxy->GetStart();
173 
174  if (basicTypeSize == -1){
175  TClass *myClass = proxy->GetClass();
176  if (!myClass){
177  Error("TObjectArrayReader::At()", "Cannot get class info from branch proxy.");
178  return 0;
179  }
180  objectSize = myClass->GetClassSize();
181  }
182  else {
183  objectSize = basicTypeSize;
184  }
185  return (void*)((Byte_t*)array + (objectSize * idx));
186  }
187 
188  void SetBasicTypeSize(Int_t size){
189  basicTypeSize = size;
190  }
191  };
192 
193  class TArrayParameterSizeReader : public TObjectArrayReader {
194  private:
195  // The index can be of type int or unsigned int.
196  TTreeReaderValue<Int_t> fIndexReader;
197  bool fIsUnsigned = false;
198 
199  template <class T>
200  TTreeReaderValue<T>& GetIndexReader() {
201  return reinterpret_cast<TTreeReaderValue<T>&>(fIndexReader);
202  }
203  public:
204  TArrayParameterSizeReader(TTreeReader *treeReader, const char *branchName)
205  {
206  if (TLeaf* sizeLeaf = treeReader->GetTree()->FindLeaf(branchName)) {
207  fIsUnsigned = sizeLeaf->IsUnsigned();
208  if (fIsUnsigned) {
209  GetIndexReader<UInt_t>() = TTreeReaderValue<UInt_t>(*treeReader, branchName);
210  } else {
211  GetIndexReader<Int_t>() = TTreeReaderValue<Int_t>(*treeReader, branchName);
212  }
213  }
214  }
215 
216  virtual size_t GetSize(ROOT::Detail::TBranchProxy* /*proxy*/) {
217  if (fIsUnsigned)
218  return *GetIndexReader<UInt_t>();
219  return *GetIndexReader<Int_t>();
220  }
221  };
222 
223  // Reader interface for fixed size arrays
224  class TArrayFixedSizeReader : public TObjectArrayReader {
225  private:
226  Int_t size;
227 
228  public:
229  TArrayFixedSizeReader(Int_t sizeArg) : size(sizeArg) {}
230 
231  virtual size_t GetSize(ROOT::Detail::TBranchProxy* /*proxy*/) { return size; }
232  };
233 
234  class TBasicTypeArrayReader : public TVirtualCollectionReader {
235  public:
236  ~TBasicTypeArrayReader() {}
237 
239  if (!proxy->Read()){
240  fReadStatus = TTreeReaderValueBase::kReadError;
241  Error("TBasicTypeArrayReader::GetCP()", "Read error in TBranchProxy.");
242  return 0;
243  }
245  return (TVirtualCollectionProxy*) proxy->GetCollection();
246  }
247 
248  virtual size_t GetSize(ROOT::Detail::TBranchProxy* proxy){
249  TVirtualCollectionProxy *myCollectionProxy = GetCP(proxy);
250  if (!myCollectionProxy) return 0;
251  return myCollectionProxy->Size();
252  }
253 
254  virtual void* At(ROOT::Detail::TBranchProxy* proxy, size_t idx){
255  TVirtualCollectionProxy *myCollectionProxy = GetCP(proxy);
256  if (!myCollectionProxy) return 0;
257  return (Byte_t*)myCollectionProxy->At(idx) + proxy->GetOffset();
258  }
259  };
260 
261  class TBasicTypeClonesReader : public TClonesReader {
262  private:
263  Int_t offset;
264  public:
265  TBasicTypeClonesReader(Int_t offsetArg) : offset(offsetArg) {}
266 
267  virtual void* At(ROOT::Detail::TBranchProxy* proxy, size_t idx){
268  TClonesArray *myClonesArray = GetCA(proxy);
269  if (!myClonesArray) return 0;
270  return (Byte_t*)myClonesArray->At(idx) + offset;
271  }
272  };
273 
274  class TLeafReader : public TVirtualCollectionReader {
275  private:
276  TTreeReaderValueBase *valueReader;
277  Int_t elementSize;
278  public:
279  TLeafReader(TTreeReaderValueBase *valueReaderArg) : valueReader(valueReaderArg), elementSize(-1) {}
280 
281  virtual size_t GetSize(ROOT::Detail::TBranchProxy* /*proxy*/){
282  TLeaf *myLeaf = valueReader->GetLeaf();
283  return myLeaf ? myLeaf->GetLen() : 0; // Error will be printed by GetLeaf
284  }
285 
286  virtual void* At(ROOT::Detail::TBranchProxy* /*proxy*/, size_t idx){
287  ProxyRead();
288  void *address = valueReader->GetAddress();
289  if (elementSize == -1){
290  TLeaf *myLeaf = valueReader->GetLeaf();
291  if (!myLeaf) return 0; // Error will be printed by GetLeaf
292  elementSize = myLeaf->GetLenType();
293  }
294  return (Byte_t*)address + (elementSize * idx);
295  }
296 
297  protected:
298  void ProxyRead(){
299  valueReader->ProxyRead();
300  }
301  };
302 
303  class TLeafParameterSizeReader : public TLeafReader {
304  private:
305  TTreeReaderValue<Int_t> sizeReader;
306  public:
307  TLeafParameterSizeReader(TTreeReader *treeReader, const char *leafName, TTreeReaderValueBase *valueReaderArg) : TLeafReader(valueReaderArg), sizeReader(*treeReader, leafName) {}
308 
309  virtual size_t GetSize(ROOT::Detail::TBranchProxy* /*proxy*/){
310  ProxyRead();
311  return *sizeReader;
312  }
313  };
314 }
315 
316 /** \class TTreeReaderArray
317 
318 Extracts array data from a TTree.
319 */
320 
322 
323 ////////////////////////////////////////////////////////////////////////////////
324 /// Create the proxy object for our branch.
325 
326 void ROOT::Internal::TTreeReaderArrayBase::CreateProxy()
327 {
328  if (fProxy) {
329  return;
330  }
331 
332  fSetupStatus = kSetupInternalError; // Fallback; set to something concrete below.
333  if (!fTreeReader) {
334  Error("TTreeReaderArrayBase::CreateProxy()", "TTreeReader object not set / available for branch %s!",
335  fBranchName.Data());
336  fSetupStatus = kSetupTreeDestructed;
337  return;
338  }
339  if (!fDict) {
340  TBranch* br = fTreeReader->GetTree()->GetBranch(fBranchName);
341  const char* brDataType = "{UNDETERMINED}";
342  if (br) {
343  TDictionary* dictUnused = 0;
344  brDataType = GetBranchDataType(br, dictUnused);
345  }
346  Error("TTreeReaderArrayBase::CreateProxy()", "The template argument type T of %s accessing branch %s (which contains data of type %s) is not known to ROOT. You will need to create a dictionary for it.",
347  GetDerivedTypeName(), fBranchName.Data(), brDataType);
348  fSetupStatus = kSetupMissingDictionary;
349  return;
350  }
351 
352  // Access a branch's collection content (not the collection itself)
353  // through a proxy.
354  // Search for the branchname, determine what it contains, and wire the
355  // TBranchProxy representing it to us so we can access its data.
356 
357  TNamedBranchProxy* namedProxy = fTreeReader->FindProxy(fBranchName);
358  if (namedProxy && namedProxy->GetContentDict() == fDict) {
359  fSetupStatus = kSetupMatch;
360  fProxy = namedProxy->GetProxy();
361  if (!fImpl){
362  Fatal("TTreeReaderArrayBase::CreateProxy()", "No fImpl set!");
363  fSetupStatus = kSetupMissingDictionary;
364  }
365  return;
366  }
367 
368 
369  TDictionary* branchActualType = 0;
370  TBranch* branch = fTreeReader->GetTree()->GetBranch(fBranchName);
371  TLeaf *myLeaf = NULL;
372  if (!branch) {
373  if (fBranchName.Contains(".")){
374  TRegexp leafNameExpression ("\\.[a-zA-Z0-9_]+$");
375  TString leafName (fBranchName(leafNameExpression));
376  TString branchName = fBranchName(0, fBranchName.Length() - leafName.Length());
377  branch = fTreeReader->GetTree()->GetBranch(branchName);
378  if (!branch){
379  Error("TTreeReaderArrayBase::CreateProxy()", "The tree does not have a branch called %s. You could check with TTree::Print() for available branches.", fBranchName.Data());
380  fSetupStatus = kSetupMissingBranch;
381  fProxy = 0;
382  return;
383  }
384  else {
385  myLeaf = branch->GetLeaf(TString(leafName(1, leafName.Length())));
386  if (!myLeaf){
387  Error("TTreeReaderArrayBase::CreateProxy()", "The tree does not have a branch, nor a sub-branch called %s. You could check with TTree::Print() for available branches.", fBranchName.Data());
388  fSetupStatus = kSetupMissingBranch;
389  fProxy = 0;
390  return;
391  }
392  else {
393  TDictionary *tempDict = TDictionary::GetDictionary(myLeaf->GetTypeName());
394  if (!tempDict){
395  Error("TTreeReaderArrayBase::CreateProxy()", "Failed to get the dictionary for %s.", myLeaf->GetTypeName());
396  fSetupStatus = kSetupMissingDictionary;
397  fProxy = 0;
398  return;
399  }
400  else if (tempDict->IsA() == TDataType::Class() && TDictionary::GetDictionary(((TDataType*)tempDict)->GetTypeName()) == fDict){
401  //fLeafOffset = myLeaf->GetOffset() / 4;
402  branchActualType = fDict;
403  fLeaf = myLeaf;
404  fBranchName = branchName;
405  fLeafName = leafName(1, leafName.Length());
406  fSetupStatus = kSetupMatchLeaf;
407  }
408  else {
409  Error("TTreeReaderArrayBase::CreateProxy()", "Leaf of type %s cannot be read by TTreeReaderValue<%s>.", myLeaf->GetTypeName(), fDict->GetName());
410  fProxy = 0;
411  fSetupStatus = kSetupMismatch;
412  return;
413  }
414  }
415  }
416  }
417  else {
418  Error("TTreeReaderArrayBase::CreateProxy()", "The tree does not have a branch called %s. You could check with TTree::Print() for available branches.", fBranchName.Data());
419  fSetupStatus = kSetupMissingBranch;
420  fProxy = 0;
421  return;
422  }
423  }
424 
425  // Update named proxy's dictionary
426  if (namedProxy && !namedProxy->GetContentDict()) {
427  namedProxy->SetContentDict(fDict);
428  fProxy = namedProxy->GetProxy();
429  if (fProxy)
430  fSetupStatus = kSetupMatch;
431  }
432  else {
433  TString membername;
434 
435  bool isTopLevel = branch->GetMother() == branch;
436  if (!isTopLevel) {
437  membername = strrchr(branch->GetName(), '.');
438  if (membername.IsNull()) {
439  membername = branch->GetName();
440  }
441  }
442  namedProxy = new TNamedBranchProxy(fTreeReader->fDirector, branch, membername);
443  fTreeReader->GetProxies()->Add(namedProxy);
444  fProxy = namedProxy->GetProxy();
445  if (fProxy)
446  fSetupStatus = kSetupMatch;
447  else
448  fSetupStatus = kSetupMismatch;
449  }
450 
451  if (!myLeaf){
452  TString branchActualTypeName;
453  const char* nonCollTypeName = GetBranchContentDataType(branch, branchActualTypeName, branchActualType);
454  if (nonCollTypeName) {
455  Error("TTreeReaderArrayBase::CreateContentProxy()", "The branch %s contains data of type %s, which should be accessed through a TTreeReaderValue< %s >.",
456  fBranchName.Data(), nonCollTypeName, nonCollTypeName);
457  if (fSetupStatus == kSetupInternalError)
458  fSetupStatus = kSetupNotACollection;
459  fProxy = 0;
460  return;
461  }
462  if (!branchActualType) {
463  if (branchActualTypeName.IsNull()) {
464  Error("TTreeReaderArrayBase::CreateContentProxy()", "Cannot determine the type contained in the collection of branch %s. That's weird - please report!",
465  fBranchName.Data());
466  } else {
467  Error("TTreeReaderArrayBase::CreateContentProxy()", "The branch %s contains data of type %s, which does not have a dictionary.",
468  fBranchName.Data(), branchActualTypeName.Data());
469  if (fSetupStatus == kSetupInternalError)
470  fSetupStatus = kSetupMissingDictionary;
471  }
472  fProxy = 0;
473  return;
474  }
475 
476  if (fDict != branchActualType) {
477  Error("TTreeReaderArrayBase::CreateContentProxy()", "The branch %s contains data of type %s. It cannot be accessed by a TTreeReaderArray<%s>",
478  fBranchName.Data(), branchActualType->GetName(), fDict->GetName());
479  if (fSetupStatus == kSetupInternalError || fSetupStatus >= 0)
480  fSetupStatus = kSetupMismatch;
481 
482  // Update named proxy's dictionary
483  if (namedProxy && !namedProxy->GetContentDict()) {
484  namedProxy->SetContentDict(fDict);
485  }
486 
487  // fProxy = 0;
488  // return;
489  }
490  }
491 
492 
493 
494  // Access a branch's collection content (not the collection itself)
495  // through a proxy.
496  // Search for the branchname, determine what it contains, and wire the
497  // TBranchProxy representing it to us so we can access its data.
498  // A proxy for branch must not have been created before (i.e. check
499  // fProxies before calling this function!)
500 
501  if (myLeaf){
502  if (!myLeaf->GetLeafCount()){
503  fImpl = new TLeafReader(this);
504  }
505  else {
506  TString leafFullName = myLeaf->GetBranch()->GetName();
507  leafFullName += ".";
508  leafFullName += myLeaf->GetLeafCount()->GetName();
509  fImpl = new TLeafParameterSizeReader(fTreeReader, leafFullName.Data(), this);
510  }
511  fSetupStatus = kSetupMatchLeaf;
512  }
513  else if (branch->IsA() == TBranchElement::Class()) {
514  TBranchElement* branchElement = ((TBranchElement*)branch);
515 
516  TStreamerInfo *streamerInfo = branchElement->GetInfo();
517  Int_t id = branchElement->GetID();
518 
519  if (id >= 0){ // Not root node?
520  // Int_t offset = streamerInfo->GetOffsets()[id];
521  TStreamerElement *element = (TStreamerElement*)streamerInfo->GetElements()->At(id);
522  // Bool_t isPointer = element->IsaPointer();
523  // TClass *classPointer = element->GetClassPointer();
524 
525  if (fSetupStatus == kSetupInternalError)
526  fSetupStatus = kSetupMatch;
527  if (element->IsA() == TStreamerSTL::Class()){
528  fImpl = new TSTLReader();
529  }
530  else if (element->IsA() == TStreamerObject::Class()){
531  //fImpl = new TObjectArrayReader(); // BArray[12]
532 
533  if (element->GetClass() == TClonesArray::Class()){
534  fImpl = new TClonesReader();
535  }
536  else {
537  fImpl = new TArrayFixedSizeReader(element->GetArrayLength());
538  }
539  }
540  else if (element->IsA() == TStreamerLoop::Class()) {
541  fImpl = new TArrayParameterSizeReader(fTreeReader, branchElement->GetBranchCount()->GetName());
542  }
543  else if (element->IsA() == TStreamerBasicType::Class()){
544  if (branchElement->GetType() == TBranchElement::kSTLMemberNode){
545  fImpl = new TBasicTypeArrayReader();
546  }
547  else if (branchElement->GetType() == TBranchElement::kClonesMemberNode){
548  fImpl = new TBasicTypeClonesReader(element->GetOffset());
549  }
550  else {
551  fImpl = new TArrayFixedSizeReader(element->GetArrayLength());
552  ((TObjectArrayReader*)fImpl)->SetBasicTypeSize(((TDataType*)fDict)->Size());
553  }
554  }
555  else if (element->IsA() == TStreamerBasicPointer::Class()) {
556  fImpl = new TArrayParameterSizeReader(fTreeReader, branchElement->GetBranchCount()->GetName());
557  ((TArrayParameterSizeReader*)fImpl)->SetBasicTypeSize(((TDataType*)fDict)->Size());
558  }
559  else if (element->IsA() == TStreamerBase::Class()){
560  fImpl = new TClonesReader();
561  } else {
562  Error("TTreeReaderArrayBase::CreateProxy()",
563  "Cannot read branch %s: unhandled streamer element type %s",
564  fBranchName.Data(), element->IsA()->GetName());
565  fSetupStatus = kSetupInternalError;
566  }
567  }
568  else { // We are at root node?
569  if (branchElement->GetClass()->GetCollectionProxy()){
570  fImpl = new TCollectionLessSTLReader(branchElement->GetClass()->GetCollectionProxy());
571  }
572  }
573  } else if (branch->IsA() == TBranch::Class()) {
574  TLeaf *topLeaf = branch->GetLeaf(branch->GetName());
575  if (!topLeaf) {
576  Error("TTreeReaderArrayBase::CreateProxy", "Failed to get the top leaf from the branch");
577  fSetupStatus = kSetupMissingBranch;
578  return;
579  }
580  Int_t size = 0;
581  TLeaf *sizeLeaf = topLeaf->GetLeafCounter(size);
582  if (fSetupStatus == kSetupInternalError)
583  fSetupStatus = kSetupMatch;
584  if (!sizeLeaf) {
585  fImpl = new TArrayFixedSizeReader(size);
586  }
587  else {
588  fImpl = new TArrayParameterSizeReader(fTreeReader, sizeLeaf->GetName());
589  }
590  ((TObjectArrayReader*)fImpl)->SetBasicTypeSize(((TDataType*)fDict)->Size());
591  } else if (branch->IsA() == TBranchClones::Class()) {
592  Error("TTreeReaderArrayBase::CreateProxy", "Support for branches of type TBranchClones not implemented");
593  fSetupStatus = kSetupInternalError;
594  } else if (branch->IsA() == TBranchObject::Class()) {
595  Error("TTreeReaderArrayBase::CreateProxy", "Support for branches of type TBranchObject not implemented");
596  fSetupStatus = kSetupInternalError;
597  } else if (branch->IsA() == TBranchSTL::Class()) {
598  Error("TTreeReaderArrayBase::CreateProxy", "Support for branches of type TBranchSTL not implemented");
599  fImpl = new TSTLReader();
600  fSetupStatus = kSetupInternalError;
601  } else if (branch->IsA() == TBranchRef::Class()) {
602  Error("TTreeReaderArrayBase::CreateProxy", "Support for branches of type TBranchRef not implemented");
603  fSetupStatus = kSetupInternalError;
604  }
605 }
606 
607 ////////////////////////////////////////////////////////////////////////////////
608 /// Access a branch's collection content (not the collection itself)
609 /// through a proxy.
610 /// Retrieve the type of data contained in the collection stored by branch;
611 /// put its dictionary into dict, If there is no dictionary, put its type
612 /// name into contentTypeName.
613 /// The contentTypeName is set to NULL if the branch does not
614 /// contain a collection; in that case, the type of the branch is returned.
615 /// In all other cases, NULL is returned.
616 
618  TString& contentTypeName,
619  TDictionary* &dict)
620 {
621  dict = 0;
622  contentTypeName = "";
623  if (branch->IsA() == TBranchElement::Class()) {
624  TBranchElement* brElement = (TBranchElement*)branch;
625  if (brElement->GetType() == 4
626  || brElement->GetType() == 3) {
627  TVirtualCollectionProxy* collProxy = brElement->GetCollectionProxy();
628  if (collProxy) {
629  TClass *myClass = collProxy->GetValueClass();
630  if (!myClass){
631  Error("TTreeReaderArrayBase::GetBranchContentDataType()", "Could not get value class.");
632  return 0;
633  }
634  dict = TDictionary::GetDictionary(myClass->GetName());
635  if (!dict) dict = TDataType::GetDataType(collProxy->GetType());
636  }
637  if (!dict) {
638  // We don't know the dictionary, thus we need the content's type name.
639  // Determine it.
640  if (brElement->GetType() == 3) {
641  contentTypeName = brElement->GetClonesName();
642  dict = TDictionary::GetDictionary(brElement->GetClonesName());
643  return 0;
644  }
645  // STL:
646  TClassEdit::TSplitType splitType(brElement->GetClassName());
647  int isSTLCont = splitType.IsSTLCont();
648  if (!isSTLCont) {
649  Error("TTreeReaderArrayBase::GetBranchContentDataType()", "Cannot determine STL collection type of %s stored in branch %s", brElement->GetClassName(), branch->GetName());
650  return brElement->GetClassName();
651  }
652  bool isMap = isSTLCont == ROOT::kSTLmap
653  || isSTLCont == ROOT::kSTLmultimap;
654  if (isMap) contentTypeName = "std::pair< ";
655  contentTypeName += splitType.fElements[1];
656  if (isMap) {
657  contentTypeName += splitType.fElements[2];
658  contentTypeName += " >";
659  }
660  return 0;
661  }
662  return 0;
663  } else if (brElement->GetType() == 31
664  || brElement->GetType() == 41) {
665  // it's a member, extract from GetClass()'s streamer info
666  TClass* clData = 0;
667  EDataType dtData = kOther_t;
668  int ExpectedTypeRet = brElement->GetExpectedType(clData, dtData);
669  if (ExpectedTypeRet == 0) {
670  dict = clData;
671  if (!dict) {
672  dict = TDataType::GetDataType(dtData);
673  }
674  if (!dict) {
675  Error("TTreeReaderArrayBase::GetBranchContentDataType()", "The branch %s contains a data type %d for which the dictionary cannot be retrieved.",
676  branch->GetName(), (int)dtData);
677  contentTypeName = TDataType::GetTypeName(dtData);
678  return 0;
679  }
680  return 0;
681  } else if (ExpectedTypeRet == 1) {
682  int brID = brElement->GetID();
683  if (brID == -1) {
684  // top
685  Error("TTreeReaderArrayBase::GetBranchContentDataType()", "The branch %s contains data of type %s for which the dictionary does not exist. It's needed.",
686  branch->GetName(), brElement->GetClassName());
687  contentTypeName = brElement->GetClassName();
688  return 0;
689  }
690  // Either the data type name doesn't have an EDataType entry
691  // or the streamer info doesn't have a TClass* attached.
692  TStreamerElement* element =
693  (TStreamerElement*) brElement->GetInfo()->GetElement(brID);
694  contentTypeName = element->GetTypeName();
695  return 0;
696  }
697  /* else (ExpectedTypeRet == 2)*/
698  // The streamer info entry cannot be found.
699  // TBranchElement::GetExpectedType() has already complained.
700  return "{CANNOT DETERMINE TBranchElement DATA TYPE}";
701  }
702  else if (brElement->GetType() == TBranchElement::kLeafNode){
703  TStreamerInfo *streamerInfo = brElement->GetInfo();
704  Int_t id = brElement->GetID();
705 
706  if (id >= 0){
707  TStreamerElement *element = (TStreamerElement*)streamerInfo->GetElements()->At(id);
708 
709  if (element->IsA() == TStreamerSTL::Class()){
710  TClass *myClass = brElement->GetCurrentClass();
711  if (!myClass){
712  Error("TTreeReaderArrayBase::GetBranchDataType()", "Could not get class from branch element.");
713  return 0;
714  }
715  TVirtualCollectionProxy *myCollectionProxy = myClass->GetCollectionProxy();
716  if (!myCollectionProxy){
717  Error("TTreeReaderArrayBase::GetBranchDataType()", "Could not get collection proxy from STL class");
718  return 0;
719  }
720  // Try getting the contained class
721  dict = myCollectionProxy->GetValueClass();
722  // If it fails, try to get the contained type as a primitive type
723  if (!dict) dict = TDataType::GetDataType(myCollectionProxy->GetType());
724  if (!dict){
725  Error("TTreeReaderArrayBase::GetBranchDataType()", "Could not get valueClass from collectionProxy.");
726  return 0;
727  }
728  contentTypeName = dict->GetName();
729  return 0;
730  }
731  else if (element->IsA() == TStreamerObject::Class() && !strcmp(element->GetTypeName(), "TClonesArray")){
732  if (!fProxy->Setup() || !fProxy->Read()){
733  Error("TTreeReaderArrayBase::GetBranchContentDataType()", "Failed to get type from proxy, unable to check type");
734  contentTypeName = "UNKNOWN";
735  dict = 0;
736  return contentTypeName;
737  }
738  TClonesArray *myArray = (TClonesArray*)fProxy->GetWhere();
739  dict = myArray->GetClass();
740  contentTypeName = dict->GetName();
741  return 0;
742  }
743  else {
744  dict = brElement->GetCurrentClass();
745  if (!dict) {
746  TDictionary *myDataType = TDictionary::GetDictionary(brElement->GetTypeName());
747  dict = TDataType::GetDataType((EDataType)((TDataType*)myDataType)->GetType());
748  }
749  contentTypeName = brElement->GetTypeName();
750  return 0;
751  }
752  }
753  if (brElement->GetCurrentClass() == TClonesArray::Class()){
754  contentTypeName = "TClonesArray";
755  Warning("TTreeReaderArrayBase::GetBranchContentDataType()", "Not able to check type correctness, ignoring check");
756  dict = fDict;
757  fSetupStatus = kSetupNoCheck;
758  }
759  else if (!dict && (branch->GetSplitLevel() == 0 || brElement->GetClass()->GetCollectionProxy())){
760  // Try getting the contained class
761  dict = brElement->GetClass()->GetCollectionProxy()->GetValueClass();
762  // If it fails, try to get the contained type as a primitive type
763  if (!dict) dict = TDataType::GetDataType(brElement->GetClass()->GetCollectionProxy()->GetType());
764  if (dict) contentTypeName = dict->GetName();
765  return 0;
766  }
767  else if (!dict){
768  dict = brElement->GetClass();
769  contentTypeName = dict->GetName();
770  return 0;
771  }
772 
773  return 0;
774  }
775  return 0;
776  } else if (branch->IsA() == TBranch::Class()
777  || branch->IsA() == TBranchObject::Class()
778  || branch->IsA() == TBranchSTL::Class()) {
779  const char* dataTypeName = branch->GetClassName();
780  if ((!dataTypeName || !dataTypeName[0])
781  && branch->IsA() == TBranch::Class()) {
782  TLeaf *myLeaf = branch->GetLeaf(branch->GetName());
783  if (myLeaf){
784  TDictionary *myDataType = TDictionary::GetDictionary(myLeaf->GetTypeName());
785  if (myDataType && myDataType->IsA() == TDataType::Class()){
786  dict = TDataType::GetDataType((EDataType)((TDataType*)myDataType)->GetType());
787  contentTypeName = myLeaf->GetTypeName();
788  return 0;
789  }
790  }
791 
792  // leaflist. Can't represent.
793  Error("TTreeReaderArrayBase::GetBranchContentDataType()", "The branch %s was created using a leaf list and cannot be represented as a C++ type. Please access one of its siblings using a TTreeReaderArray:", branch->GetName());
794  TIter iLeaves(branch->GetListOfLeaves());
795  TLeaf* leaf = 0;
796  while ((leaf = (TLeaf*) iLeaves())) {
797  Error("TTreeReaderArrayBase::GetBranchContentDataType()", " %s.%s", branch->GetName(), leaf->GetName());
798  }
799  return 0;
800  }
801  if (dataTypeName) dict = TDictionary::GetDictionary(dataTypeName);
802  if (branch->IsA() == TBranchSTL::Class()){
803  Warning("TTreeReaderArrayBase::GetBranchContentDataType()", "Not able to check type correctness, ignoring check");
804  dict = fDict;
805  fSetupStatus = kSetupNoCheck;
806  return 0;
807  }
808  return dataTypeName;
809  } else if (branch->IsA() == TBranchClones::Class()) {
810  dict = TClonesArray::Class();
811  return "TClonesArray";
812  } else if (branch->IsA() == TBranchRef::Class()) {
813  // Can't represent.
814  Error("TTreeReaderArrayBase::GetBranchContentDataType()", "The branch %s is a TBranchRef and cannot be represented as a C++ type.", branch->GetName());
815  return 0;
816  } else {
817  Error("TTreeReaderArrayBase::GetBranchContentDataType()", "The branch %s is of type %s - something that is not handled yet.", branch->GetName(), branch->IsA()->GetName());
818  return 0;
819  }
820 
821  return 0;
822 }
virtual Int_t GetLen() const
Return the number of effective elements of this leaf.
Definition: TLeaf.cxx:278
Describe Streamer information for one class version.
Definition: TStreamerInfo.h:47
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:37
static TDataType * GetDataType(EDataType type)
Given a EDataType type, get the TDataType* that represents it.
Definition: TDataType.cxx:440
TVirtualCollectionProxy * GetCollectionProxy()
Return the collection proxy describing the branch content, if any.
const char * GetBranchContentDataType(TBranch *branch, TString &contentTypeName, TDictionary *&dict)
Access a branch&#39;s collection content (not the collection itself) through a proxy. ...
TString GetTypeName()
Get basic type of typedef, e,g.
Definition: TDataType.cxx:149
Int_t GetOffset() const
Int_t GetID() const
int IsSTLCont(int testAlloc=0) const
type : type name: vector&lt;list&lt;classA,allocator&gt;,allocator&gt; testAlloc: if true, we test allocator...
Definition: TClassEdit.cxx:154
virtual TLeaf * GetLeaf(const char *name) const
Return pointer to the 1st Leaf named name in thisBranch.
Definition: TBranch.cxx:1478
virtual Int_t GetExpectedType(TClass *&clptr, EDataType &type)
Fill expectedClass and expectedType with information on the data type of the object/values contained ...
const char * GetTypeName() const
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:48
void Fatal(const char *location, const char *msgfmt,...)
virtual TClass * GetValueClass() const =0
const char * Size
Definition: TXMLSetup.cxx:56
TTree * GetTree() const
Definition: TTreeReader.h:170
Regular expression class.
Definition: TRegexp.h:35
static TDictionary * GetDictionary(const char *name)
Definition: TDictionary.cxx:84
virtual void * GetStart(UInt_t=0)
Definition: TBranchProxy.h:205
int Int_t
Definition: RtypesCore.h:41
TBranch * GetBranch() const
Definition: TLeaf.h:70
Type GetType(const std::string &Name)
Definition: Systematics.cxx:34
const char * Class
Definition: TXMLSetup.cxx:64
Int_t GetSplitLevel() const
Definition: TBranch.h:182
virtual EDataType GetType() const =0
Int_t GetClassSize() const
Definition: TClass.h:383
const Detail::TBranchProxy * GetProxy() const
void SetContentDict(TDictionary *dict)
virtual const char * GetClonesName() const
TBranchElement * GetBranchCount() const
unsigned char Byte_t
Definition: RtypesCore.h:60
TStreamerInfo * GetInfo() const
Get streamer info for the branch class.
virtual TLeaf * GetLeafCounter(Int_t &countval) const
Return a pointer to the counter of this leaf.
Definition: TLeaf.cxx:167
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:91
virtual const char * GetTypeName() const
Definition: TLeaf.h:81
TObjArray * GetElements() const
Basic data type descriptor (datatype information is obtained from CINT).
Definition: TDataType.h:46
This class defines an abstract interface that must be implemented by all classes that contain diction...
Definition: TDictionary.h:162
virtual Bool_t HasPointers() const =0
TBranch * GetMother() const
Get our top-level parent branch in the tree.
Definition: TBranch.cxx:1551
TClass * GetCurrentClass()
Return a pointer to the current type of the data member corresponding to branch element.
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
void Warning(const char *location, const char *msgfmt,...)
virtual TClass * GetClass() const
TTree * GetTree() const
Definition: TBranch.h:184
virtual TLeaf * GetLeafCount() const
Definition: TLeaf.h:71
A Branch for the case of an object.
void * GetWhere() const
Definition: TBranchProxy.h:200
virtual const char * GetTypeName() const
Return type name of element in the branch.
#define ClassImp(name)
Definition: Rtypes.h:279
TVirtualCollectionProxy * GetCollection()
Definition: TBranchProxy.h:202
virtual Int_t GetLenType() const
Definition: TLeaf.h:75
TClass * GetClass() const
Definition: TClonesArray.h:57
Bool_t IsaPointer() const
Definition: TBranchProxy.h:135
EDataType
Definition: TDataType.h:30
virtual void * At(UInt_t idx)=0
TObjArray * GetListOfLeaves()
Definition: TBranch.h:179
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:494
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any. ...
TVirtualCollectionProxy * GetCollectionProxy() const
Return the proxy describing the collection (if any).
Definition: TClass.cxx:2801
TClass * GetClass() const
virtual UInt_t Size() const =0
typedef void((*Func_t)())
An array of clone (identical) objects.
Definition: TClonesArray.h:32
Int_t GetType() const
Proxy around an arbitrary container, which implements basic functionality and iteration.
#define NULL
Definition: Rtypes.h:82
TStreamerElement * GetElement(Int_t id) const
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
Base class for all the proxy object.
Definition: TBranchProxy.h:81
TDictionary * GetContentDict() const
A TTree is a list of TBranches.
Definition: TBranch.h:58
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any. ...
Definition: TBranch.cxx:1182
void Error(ErrorHandler_t func, int code, const char *va_(fmt),...)
Write error message and call a handler, if required.
Int_t GetArrayLength() const