Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TBranch.h
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 12/01/96
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
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 #ifndef ROOT_TBranch
13 #define ROOT_TBranch
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TBranch //
19 // //
20 // A TTree object is a list of TBranchs. //
21 // A TBranch describes the branch data structure and supports : //
22 // the list of TBaskets (branch buffers) associated to this branch. //
23 // the list of TLeaves (branch description) //
24 //////////////////////////////////////////////////////////////////////////
25 
26 #include <memory>
27 
28 #include "Compression.h"
29 #include "TAttFill.h"
30 #include "TBranchCacheInfo.h"
31 #include "TDataType.h"
32 #include "TNamed.h"
33 #include "TObjArray.h"
34 #include "ROOT/TIOFeatures.hxx"
35 
36 class TTree;
37 class TBasket;
38 class TLeaf;
39 class TBrowser;
40 class TDirectory;
41 class TFile;
42 class TClonesArray;
43 class TTreeCloner;
44 class TTreeCache;
45 
46 namespace ROOT {
47 namespace Experimental {
48 namespace Internal {
49 class TBulkBranchRead;
50 }
51 }
52 namespace Internal {
53 class TBranchIMTHelper; ///< A helper class for managing IMT work during TTree:Fill operations.
54 }
55 }
56 
57 const Int_t kDoNotProcess = BIT(10); // Active bit for branches
58 const Int_t kIsClone = BIT(11); // to indicate a TBranchClones
59 const Int_t kBranchObject = BIT(12); // branch is a TObject*
60 const Int_t kBranchAny = BIT(17); // branch is an object*
61 const Int_t kMapObject = kBranchObject | kBranchAny;
62 
63 namespace ROOT {
64 namespace Experimental {
65 namespace Internal {
66 
67 ///\class TBulkBranchRead
68 /// Helper class for reading many branch entries at once to optimize throughput.
69 class TBulkBranchRead {
70 
71  friend class ::TBranch;
72 
73 public:
74  Int_t GetBulkEntries(Long64_t evt, TBuffer &user_buf);
75  Int_t GetEntriesSerialized(Long64_t evt, TBuffer &user_buf);
76  Int_t GetEntriesSerialized(Long64_t evt, TBuffer &user_buf, TBuffer *count_buf);
77  Bool_t SupportsBulkRead() const;
78 
79 private:
80  TBulkBranchRead(TBranch &parent)
81  : fParent(parent)
82  {}
83 
84  TBranch &fParent;
85 };
86 }
87 }
88 }
89 
90 class TBranch : public TNamed , public TAttFill {
91  using TIOFeatures = ROOT::TIOFeatures;
92 
93 protected:
94  friend class TTreeCache;
95  friend class TTreeCloner;
96  friend class TTree;
97  friend class ROOT::Experimental::Internal::TBulkBranchRead;
98 
99  // TBranch status bits
100  enum EStatusBits {
101  kDoNotProcess = ::kDoNotProcess, // Active bit for branches
102  kIsClone = ::kIsClone, // to indicate a TBranchClones
103  kBranchObject = ::kBranchObject, // branch is a TObject*
104  kBranchAny = ::kBranchAny, // branch is an object*
105  // kMapObject = kBranchObject | kBranchAny;
106  kAutoDelete = BIT(15),
107 
108  kDoNotUseBufferMap = BIT(22) // If set, at least one of the entry in the branch will use the buffer's map of classname and objects.
109  };
110 
111  using BulkObj = ROOT::Experimental::Internal::TBulkBranchRead;
112  static Int_t fgCount; ///<! branch counter
113  Int_t fCompress; ///< Compression level and algorithm
114  Int_t fBasketSize; ///< Initial Size of Basket Buffer
115  Int_t fEntryOffsetLen; ///< Initial Length of fEntryOffset table in the basket buffers
116  Int_t fWriteBasket; ///< Last basket number written
117  Long64_t fEntryNumber; ///< Current entry number (last one filled in this branch)
118  TBasket *fExtraBasket; ///<! Allocated basket not currently holding any data.
119  TIOFeatures fIOFeatures; ///< IO features for newly-created baskets.
120  Int_t fOffset; ///< Offset of this branch
121  Int_t fMaxBaskets; ///< Maximum number of Baskets so far
122  Int_t fNBaskets; ///<! Number of baskets in memory
123  Int_t fSplitLevel; ///< Branch split level
124  Int_t fNleaves; ///<! Number of leaves
125  Int_t fReadBasket; ///<! Current basket number when reading
126  Long64_t fReadEntry; ///<! Current entry number when reading
127  Long64_t fFirstBasketEntry; ///<! First entry in the current basket.
128  Long64_t fNextBasketEntry; ///<! Next entry that will requires us to go to the next basket
129  TBasket *fCurrentBasket; ///<! Pointer to the current basket.
130  Long64_t fEntries; ///< Number of entries
131  Long64_t fFirstEntry; ///< Number of the first entry in this branch
132  Long64_t fTotBytes; ///< Total number of bytes in all leaves before compression
133  Long64_t fZipBytes; ///< Total number of bytes in all leaves after compression
134  TObjArray fBranches; ///< -> List of Branches of this branch
135  TObjArray fLeaves; ///< -> List of leaves of this branch
136  TObjArray fBaskets; ///< -> List of baskets of this branch
137  Int_t *fBasketBytes; ///<[fMaxBaskets] Length of baskets on file
138  Long64_t *fBasketEntry; ///<[fMaxBaskets] Table of first entry in each basket
139  Long64_t *fBasketSeek; ///<[fMaxBaskets] Addresses of baskets on file
140  TTree *fTree; ///<! Pointer to Tree header
141  TBranch *fMother; ///<! Pointer to top-level parent branch in the tree.
142  TBranch *fParent; ///<! Pointer to parent branch.
143  char *fAddress; ///<! Address of 1st leaf (variable or object)
144  TDirectory *fDirectory; ///<! Pointer to directory where this branch buffers are stored
145  TString fFileName; ///< Name of file where buffers are stored ("" if in same file as Tree header)
146  TBuffer *fEntryBuffer; ///<! Buffer used to directly pass the content without streaming
147  TBuffer *fTransientBuffer; ///<! Pointer to the current transient buffer.
148  TList *fBrowsables; ///<! List of TVirtualBranchBrowsables used for Browse()
149  BulkObj fBulk; ///<! Helper for performing bulk IO
150 
151  Bool_t fSkipZip; ///<! After being read, the buffer will not be unzipped.
152 
153  using CacheInfo_t = ROOT::Internal::TBranchCacheInfo;
154  CacheInfo_t fCacheInfo; ///<! Hold info about which basket are in the cache and if they have been retrieved from the cache.
155 
156  typedef void (TBranch::*ReadLeaves_t)(TBuffer &b);
157  ReadLeaves_t fReadLeaves; ///<! Pointer to the ReadLeaves implementation to use.
158  typedef void (TBranch::*FillLeaves_t)(TBuffer &b);
159  FillLeaves_t fFillLeaves; ///<! Pointer to the FillLeaves implementation to use.
160  void ReadLeavesImpl(TBuffer &b);
161  void ReadLeaves0Impl(TBuffer &b);
162  void ReadLeaves1Impl(TBuffer &b);
163  void ReadLeaves2Impl(TBuffer &b);
164  void FillLeavesImpl(TBuffer &b);
165 
166  void SetSkipZip(Bool_t skip = kTRUE) { fSkipZip = skip; }
167  void Init(const char *name, const char *leaflist, Int_t compress);
168 
169  TBasket *GetFreshBasket(TBuffer *user_buffer);
170  TBasket *GetFreshCluster();
171  Int_t WriteBasket(TBasket* basket, Int_t where) { return WriteBasketImpl(basket, where, nullptr); }
172 
173  TString GetRealFileName() const;
174 
175 private:
176  Int_t GetBasketAndFirst(TBasket*& basket, Long64_t& first, TBuffer* user_buffer);
177  TBasket *GetBasketImpl(Int_t basket, TBuffer* user_buffer);
178  Int_t GetBulkEntries(Long64_t, TBuffer&);
179  Int_t GetEntriesSerialized(Long64_t N, TBuffer& user_buf) {return GetEntriesSerialized(N, user_buf, nullptr);}
180  Int_t GetEntriesSerialized(Long64_t, TBuffer&, TBuffer*);
181  Int_t FillEntryBuffer(TBasket* basket,TBuffer* buf, Int_t& lnew);
182  Int_t WriteBasketImpl(TBasket* basket, Int_t where, ROOT::Internal::TBranchIMTHelper *);
183  TBranch(const TBranch&) = delete; // not implemented
184  TBranch& operator=(const TBranch&) = delete; // not implemented
185 
186 public:
187  TBranch();
188  TBranch(TTree *tree, const char *name, void *address, const char *leaflist, Int_t basketsize=32000, Int_t compress = ROOT::RCompressionSetting::EAlgorithm::kInherit);
189  TBranch(TBranch *parent, const char *name, void *address, const char *leaflist, Int_t basketsize=32000, Int_t compress = ROOT::RCompressionSetting::EAlgorithm::kInherit);
190  virtual ~TBranch();
191 
192  virtual void AddBasket(TBasket &b, Bool_t ondisk, Long64_t startEntry);
193  virtual void AddLastBasket(Long64_t startEntry);
194  Int_t BackFill();
195  virtual void Browse(TBrowser *b);
196  virtual void DeleteBaskets(Option_t* option="");
197  virtual void DropBaskets(Option_t *option = "");
198  void ExpandBasketArrays();
199  Int_t Fill() { return FillImpl(nullptr); }
200  virtual Int_t FillImpl(ROOT::Internal::TBranchIMTHelper *);
201  virtual TBranch *FindBranch(const char *name);
202  virtual TLeaf *FindLeaf(const char *name);
203  Int_t FlushBaskets();
204  Int_t FlushOneBasket(UInt_t which);
205 
206  virtual char *GetAddress() const {return fAddress;}
207  TBasket *GetBasket(Int_t basket) {return GetBasketImpl(basket, nullptr);}
208  Int_t *GetBasketBytes() const {return fBasketBytes;}
209  Long64_t *GetBasketEntry() const {return fBasketEntry;}
210  virtual Long64_t GetBasketSeek(Int_t basket) const;
211  virtual Int_t GetBasketSize() const {return fBasketSize;}
212  ROOT::Experimental::Internal::TBulkBranchRead &GetBulkRead() { return fBulk; }
213  virtual TList *GetBrowsables();
214  virtual const char* GetClassName() const;
215  Int_t GetCompressionAlgorithm() const;
216  Int_t GetCompressionLevel() const;
217  Int_t GetCompressionSettings() const;
218  TDirectory *GetDirectory() const {return fDirectory;}
219  virtual Int_t GetEntry(Long64_t entry=0, Int_t getall = 0);
220  virtual Int_t GetEntryExport(Long64_t entry, Int_t getall, TClonesArray *list, Int_t n);
221  Int_t GetEntryOffsetLen() const { return fEntryOffsetLen; }
222  Int_t GetEvent(Long64_t entry=0) {return GetEntry(entry);}
223  const char *GetIconName() const;
224  virtual Int_t GetExpectedType(TClass *&clptr,EDataType &type);
225  virtual TLeaf *GetLeaf(const char *name) const;
226  virtual TFile *GetFile(Int_t mode=0);
227  const char *GetFileName() const {return fFileName.Data();}
228  Int_t GetOffset() const {return fOffset;}
229  Int_t GetReadBasket() const {return fReadBasket;}
230  Long64_t GetReadEntry() const {return fReadEntry;}
231  Int_t GetWriteBasket() const {return fWriteBasket;}
232  Long64_t GetTotalSize(Option_t *option="") const;
233  Long64_t GetTotBytes(Option_t *option="") const;
234  Long64_t GetZipBytes(Option_t *option="") const;
235  Long64_t GetEntryNumber() const {return fEntryNumber;}
236  Long64_t GetFirstEntry() const {return fFirstEntry; }
237  TIOFeatures GetIOFeatures() const;
238  TObjArray *GetListOfBaskets() {return &fBaskets;}
239  TObjArray *GetListOfBranches() {return &fBranches;}
240  TObjArray *GetListOfLeaves() {return &fLeaves;}
241  Int_t GetMaxBaskets() const {return fMaxBaskets;}
242  Int_t GetNleaves() const {return fNleaves;}
243  Int_t GetSplitLevel() const {return fSplitLevel;}
244  Long64_t GetEntries() const {return fEntries;}
245  TTree *GetTree() const {return fTree;}
246  virtual Int_t GetRow(Int_t row);
247  virtual Bool_t GetMakeClass() const;
248  TBranch *GetMother() const;
249  TBranch *GetSubBranch(const TBranch *br) const;
250  TBuffer *GetTransientBuffer(Int_t size);
251  Bool_t IsAutoDelete() const;
252  Bool_t IsFolder() const;
253  virtual void KeepCircular(Long64_t maxEntries);
254  virtual Int_t LoadBaskets();
255  virtual void Print(Option_t *option="") const;
256  void PrintCacheInfo() const;
257  virtual void ReadBasket(TBuffer &b);
258  virtual void Refresh(TBranch *b);
259  virtual void Reset(Option_t *option="");
260  virtual void ResetAfterMerge(TFileMergeInfo *);
261  virtual void ResetAddress();
262  virtual void ResetReadEntry() {fReadEntry = -1;}
263  virtual void SetAddress(void *add);
264  virtual void SetObject(void *objadd);
265  virtual void SetAutoDelete(Bool_t autodel=kTRUE);
266  virtual void SetBasketSize(Int_t buffsize);
267  virtual void SetBufferAddress(TBuffer *entryBuffer);
268  void SetCompressionAlgorithm(Int_t algorithm = ROOT::RCompressionSetting::EAlgorithm::kUseGlobal);
269  void SetCompressionLevel(Int_t level = ROOT::RCompressionSetting::ELevel::kUseMin);
270  void SetCompressionSettings(Int_t settings = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault);
271  virtual void SetEntries(Long64_t entries);
272  virtual void SetEntryOffsetLen(Int_t len, Bool_t updateSubBranches = kFALSE);
273  virtual void SetFirstEntry( Long64_t entry );
274  virtual void SetFile(TFile *file=0);
275  virtual void SetFile(const char *filename);
276  void SetIOFeatures(TIOFeatures &features) {fIOFeatures = features;}
277  virtual Bool_t SetMakeClass(Bool_t decomposeObj = kTRUE);
278  virtual void SetOffset(Int_t offset=0) {fOffset=offset;}
279  virtual void SetStatus(Bool_t status=1);
280  virtual void SetTree(TTree *tree) { fTree = tree;}
281  virtual void SetupAddresses();
282  Bool_t SupportsBulkRead() const;
283  virtual void UpdateAddress() {;}
284  virtual void UpdateFile();
285 
286  static void ResetCount();
287 
288  ClassDef(TBranch, 13); // Branch descriptor
289 };
290 
291 //______________________________________________________________________________
292 inline Int_t TBranch::GetCompressionAlgorithm() const
293 {
294  return (fCompress < 0) ? -1 : fCompress / 100;
295 }
296 
297 //______________________________________________________________________________
298 inline Int_t TBranch::GetCompressionLevel() const
299 {
300  return (fCompress < 0) ? -1 : fCompress % 100;
301 }
302 
303 //______________________________________________________________________________
304 inline Int_t TBranch::GetCompressionSettings() const
305 {
306  return (fCompress < 0) ? -1 : fCompress;
307 }
308 
309 namespace ROOT {
310 namespace Experimental {
311 namespace Internal {
312 
313 inline Int_t TBulkBranchRead::GetBulkEntries(Long64_t evt, TBuffer& user_buf) { return fParent.GetBulkEntries(evt, user_buf); }
314 inline Int_t TBulkBranchRead::GetEntriesSerialized(Long64_t evt, TBuffer& user_buf) { return fParent.GetEntriesSerialized(evt, user_buf); }
315 inline Int_t TBulkBranchRead::GetEntriesSerialized(Long64_t evt, TBuffer& user_buf, TBuffer* count_buf) { return fParent.GetEntriesSerialized(evt, user_buf, count_buf); }
316 inline Bool_t TBulkBranchRead::SupportsBulkRead() const { return fParent.SupportsBulkRead(); }
317 
318 } // Internal
319 } // Experimental
320 } // ROOT
321 
322 #endif