Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TBasket.h
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 19/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_TBasket
13 #define ROOT_TBasket
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // TBasket //
18 // //
19 // The TBasket objects are created at run time to collect TTree entries //
20 // in buffers. When a Basket is full, it is written to the file. //
21 // The Basket is kept in memory if there is enough space. //
22 // (see the fMaxVirtualsize of TTree). //
23 // //
24 // The Basket class derives from TKey. //
25 //////////////////////////////////////////////////////////////////////////
26 
27 
28 #include "TKey.h"
29 
30 class TFile;
31 class TTree;
32 class TBranch;
33 
34 class TBasket : public TKey {
35 friend class TBranch;
36 
37 private:
38  TBasket(const TBasket&); ///< TBasket objects are not copiable.
39  TBasket& operator=(const TBasket&); ///< TBasket objects are not copiable.
40 
41  // Internal corner cases for ReadBasketBuffers
42  Int_t ReadBasketBuffersUnzip(char*, Int_t, Bool_t, TFile*);
43  Int_t ReadBasketBuffersUncompressedCase();
44 
45  // Helper for managing the compressed buffer.
46  void InitializeCompressedBuffer(Int_t len, TFile* file);
47 
48  // Handles special logic around deleting / reseting the entry offset pointer.
49  void ResetEntryOffset();
50 
51  // Get entry offset as result of a calculation.
52  Int_t *GetCalculatedEntryOffset();
53 
54  // Returns true if the underlying TLeaf can regenerate the entry offsets for us.
55  Bool_t CanGenerateOffsetArray();
56 
57  // Manage buffer ownership.
58  void DisownBuffer();
59  void AdoptBuffer(TBuffer *user_buffer);
60 
61 protected:
62  Int_t fBufferSize{0}; ///< fBuffer length in bytes
63  Int_t fNevBufSize{0}; ///< Length in Int_t of fEntryOffset OR fixed length of each entry if fEntryOffset is null!
64  Int_t fNevBuf{0}; ///< Number of entries in basket
65  Int_t fLast{0}; ///< Pointer to last used byte in basket
66  Bool_t fHeaderOnly{kFALSE}; ///< True when only the basket header must be read/written
67  UChar_t fIOBits{0}; ///<!IO feature flags. Serialized in custom portion of streamer to avoid forward compat issues unless needed.
68  Bool_t fOwnsCompressedBuffer{kFALSE}; ///<! Whether or not we own the compressed buffer.
69  Bool_t fReadEntryOffset{kFALSE}; ///<!Set to true if offset array was read from a file.
70  Int_t *fDisplacement{nullptr}; ///<![fNevBuf] Displacement of entries in fBuffer(TKey)
71  Int_t *fEntryOffset{nullptr}; ///<[fNevBuf] Offset of entries in fBuffer(TKey); generated at runtime. Special value
72  /// of `-1` indicates that the offset generation MUST be performed on first read.
73  TBranch *fBranch{nullptr}; ///<Pointer to the basket support branch
74  TBuffer *fCompressedBufferRef{nullptr}; ///<! Compressed buffer.
75  Int_t fLastWriteBufferSize[3] = {0,0,0}; ///<! Size of the buffer last three buffers we wrote it to disk
76  Bool_t fResetAllocation{false}; ///<! True if last reset re-allocated the memory
77  UChar_t fNextBufferSizeRecord{0}; ///<! Index into fLastWriteBufferSize of the last buffer written to disk
78 #ifdef R__TRACK_BASKET_ALLOC_TIME
79  ULong64_t fResetAllocationTime{0}; ///<! Time spent reallocating baskets in microseconds during last Reset operation.
80 #endif
81 
82 public:
83  // The IO bits flag is to provide improved forward-compatibility detection.
84  // Any new non-forward compatibility flags related serialization should be
85  // added here. When a new flag is added, set it in the kSupported field;
86  //
87  // The values and names of this (and EUnsupportedIOBits) enum need not be aligned
88  // with the values of the various TIOFeatures enums, as there's a clean separation
89  // between these two interfaces. Practically, it is reasonable to keep them as aligned
90  // as possible in order to avoid confusion.
91  //
92  // If (fIOBits & ~kSupported) is non-zero -- i.e., an unknown IO flag is set
93  // in the fIOBits -- then the zombie flag will be set for this object.
94  //
95  enum class EIOBits : Char_t {
96  // The following to bits are reserved for now; when supported, set
97  // kSupported = kGenerateOffsetMap | kBasketClassMap
98  kGenerateOffsetMap = BIT(0),
99  // kBasketClassMap = BIT(1),
100  kSupported = kGenerateOffsetMap
101  };
102  // This enum covers IOBits that are known to this ROOT release but
103  // not supported; provides a mechanism for us to have experimental
104  // changes that are not going go into a supported release.
105  //
106  // (kUnsupported | kSupported) should result in the '|' of all IOBits.
107  enum class EUnsupportedIOBits : Char_t { kUnsupported = 0 };
108  // The number of known, defined IOBits.
109  static constexpr int kIOBitCount = 1;
110 
111  TBasket();
112  TBasket(TDirectory *motherDir);
113  TBasket(const char *name, const char *title, TBranch *branch);
114  virtual ~TBasket();
115 
116  virtual void AdjustSize(Int_t newsize);
117  virtual void DeleteEntryOffset();
118  virtual Int_t DropBuffers();
119  TBranch *GetBranch() const {return fBranch;}
120  Int_t GetBufferSize() const {return fBufferSize;}
121  Int_t *GetDisplacement() const {return fDisplacement;}
122  Int_t *GetEntryOffset()
123  {
124  return R__likely(fEntryOffset != reinterpret_cast<Int_t *>(-1)) ? fEntryOffset : GetCalculatedEntryOffset();
125  }
126  Int_t GetEntryPointer(Int_t Entry);
127  Int_t GetNevBuf() const {return fNevBuf;}
128  Int_t GetNevBufSize() const {return fNevBufSize;}
129  Int_t GetLast() const {return fLast;}
130  virtual void MoveEntries(Int_t dentries);
131  virtual void PrepareBasket(Long64_t /* entry */) {};
132  Int_t ReadBasketBuffers(Long64_t pos, Int_t len, TFile *file);
133  Int_t ReadBasketBytes(Long64_t pos, TFile *file);
134  virtual void Reset();
135 
136 // Time spent reseting basket sizes (typically, at event cluster boundaries), in microseconds
137 #ifdef R__TRACK_BASKET_ALLOC_TIME
138  ULong64_t GetResetAllocationTime() const { return fResetAllocationTime; }
139 #endif
140  // Count of resets performed of basket size.
141  Bool_t GetResetAllocationCount() const { return fResetAllocation; }
142 
143  Int_t LoadBasketBuffers(Long64_t pos, Int_t len, TFile *file, TTree *tree = 0);
144  Long64_t CopyTo(TFile *to);
145 
146  void SetBranch(TBranch *branch) { fBranch = branch; }
147  void SetNevBufSize(Int_t n) { fNevBufSize=n; }
148  virtual void SetReadMode();
149  virtual void SetWriteMode();
150  inline void Update(Int_t newlast) { Update(newlast,newlast); };
151  virtual void Update(Int_t newlast, Int_t skipped);
152  virtual Int_t WriteBuffer();
153 
154  ClassDef(TBasket, 3); // the TBranch buffers
155 };
156 
157 #endif