Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TBranch.cxx
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#include "TBranchCacheInfo.h"
13
14#include "TBranch.h"
15
16#include "Bytes.h"
17#include "Compression.h"
18#include "TBasket.h"
19#include "TBranchBrowsable.h"
20#include "TBrowser.h"
21#include "TBuffer.h"
22#include "TClass.h"
23#include "TBufferFile.h"
24#include "TClonesArray.h"
25#include "TFile.h"
26#include "TLeaf.h"
27#include "TLeafB.h"
28#include "TLeafC.h"
29#include "TLeafD.h"
30#include "TLeafD32.h"
31#include "TLeafF.h"
32#include "TLeafF16.h"
33#include "TLeafI.h"
34#include "TLeafL.h"
35#include "TLeafG.h"
36#include "TLeafO.h"
37#include "TLeafObject.h"
38#include "TLeafS.h"
39#include "TMessage.h"
40#include "TROOT.h"
41#include "TSystem.h"
42#include "TMath.h"
43#include "TTree.h"
44#include "TTreeCache.h"
45#include "TTreeCacheUnzip.h"
46#include "TVirtualMutex.h"
47#include "TVirtualPad.h"
48#include "TVirtualPerfStats.h"
49#include "strlcpy.h"
50#include "snprintf.h"
51
52#include "TBranchIMTHelper.h"
53
54#include "ROOT/TIOFeatures.hxx"
55
56#include <atomic>
57#include <cstddef>
58#include <cstring>
59#include <cstdio>
60
61
63
64/** \class TBranch
65\ingroup tree
66
67A TTree is a list of TBranches
68
69A TBranch supports:
70 - The list of TLeaf describing this branch.
71 - The list of TBasket (branch buffers).
72
73See TBranch structure in TTree.
74
75See also specialized branches:
76 - TBranchObject in case the branch is one object
77 - TBranchClones in case the branch is an array of clone objects
78*/
79
81
82
83
84////////////////////////////////////////////////////////////////////////////////
85/// Default constructor. Used for I/O by default.
86
88: TNamed()
89, TAttFill(0, 1001)
90, fCompress(0)
91, fBasketSize(32000)
92, fEntryOffsetLen(1000)
93, fWriteBasket(0)
94, fEntryNumber(0)
95, fExtraBasket(nullptr)
96, fOffset(0)
97, fMaxBaskets(10)
98, fNBaskets(0)
99, fSplitLevel(0)
100, fNleaves(0)
101, fReadBasket(0)
102, fReadEntry(-1)
103, fFirstBasketEntry(-1)
104, fNextBasketEntry(-1)
105, fCurrentBasket(0)
106, fEntries(0)
107, fFirstEntry(0)
108, fTotBytes(0)
109, fZipBytes(0)
110, fBranches()
111, fLeaves()
112, fBaskets(fMaxBaskets)
113, fBasketBytes(0)
114, fBasketEntry(0)
115, fBasketSeek(0)
116, fTree(0)
117, fMother(0)
118, fParent(0)
119, fAddress(0)
120, fDirectory(0)
121, fFileName("")
122, fEntryBuffer(0)
123, fTransientBuffer(0)
124, fBrowsables(0)
125, fBulk(*this)
126, fSkipZip(kFALSE)
127, fReadLeaves(&TBranch::ReadLeavesImpl)
128, fFillLeaves(&TBranch::FillLeavesImpl)
129{
131}
132
133////////////////////////////////////////////////////////////////////////////////
134/// Create a Branch as a child of a Tree
135///
136/// * address is the address of the first item of a structure
137/// or the address of a pointer to an object (see example in TTree.cxx).
138/// * leaflist is the concatenation of all the variable names and types
139/// separated by a colon character :
140/// The variable name and the variable type are separated by a
141/// slash (/). The variable type must be 1 character. (Characters
142/// after the first are legal and will be appended to the visible
143/// name of the leaf, but have no effect.) If no type is given, the
144/// type of the variable is assumed to be the same as the previous
145/// variable. If the first variable does not have a type, it is
146/// assumed of type F by default. The list of currently supported
147/// types is given below:
148/// - `C` : a character string terminated by the 0 character
149/// - `B` : an 8 bit signed integer (`Char_t`)
150/// - `b` : an 8 bit unsigned integer (`UChar_t`)
151/// - `S` : a 16 bit signed integer (`Short_t`)
152/// - `s` : a 16 bit unsigned integer (`UShort_t`)
153/// - `I` : a 32 bit signed integer (`Int_t`)
154/// - `i` : a 32 bit unsigned integer (`UInt_t`)
155/// - `F` : a 32 bit floating point (`Float_t`)
156/// - `f` : a 24 bit floating point with truncated mantissa (`Float16_t`)
157/// - `D` : a 64 bit floating point (`Double_t`)
158/// - `d` : a 24 bit truncated floating point (`Double32_t`)
159/// - `L` : a 64 bit signed integer (`Long64_t`)
160/// - `l` : a 64 bit unsigned integer (`ULong64_t`)
161/// - `G` : a long signed integer, stored as 64 bit (`Long_t`)
162/// - `g` : a long unsigned integer, stored as 64 bit (`ULong_t`)
163/// - `O` : [the letter `o`, not a zero] a boolean (`Bool_t`)
164///
165/// Arrays of values are supported with the following syntax:
166/// - If leaf name has the form var[nelem], where nelem is alphanumeric, then
167/// if nelem is a leaf name, it is used as the variable size of the array,
168/// otherwise return 0.
169/// The leaf referred to by nelem **MUST** be an int (/I),
170/// - If leaf name has the form var[nelem], where nelem is a non-negative integers, then
171/// it is used as the fixed size of the array.
172/// - If leaf name has the form of a multi dimension array (e.g. var[nelem][nelem2])
173/// where nelem and nelem2 are non-negative integers) then
174/// it is used as a 2 dimensional array of fixed size.
175/// - In case of the truncated floating point types (Float16_t and Double32_t) you can
176/// furthermore specify the range in the style [xmin,xmax] or [xmin,xmax,nbits] after
177/// the type character. See `TStreamerElement::GetRange()` for further information.
178/// - Any of other form is not supported.
179///
180/// Note that the TTree will assume that all the item are contiguous in memory.
181/// On some platform, this is not always true of the member of a struct or a class,
182/// due to padding and alignment. Sorting your data member in order of decreasing
183/// sizeof usually leads to their being contiguous in memory.
184///
185/// * bufsize is the buffer size in bytes for this branch
186/// The default value is 32000 bytes and should be ok for most cases.
187/// You can specify a larger value (e.g. 256000) if your Tree is not split
188/// and each entry is large (Megabytes)
189/// A small value for bufsize is optimum if you intend to access
190/// the entries in the Tree randomly and your Tree is in split mode.
191///
192/// See an example of a Branch definition in the TTree constructor.
193///
194/// Note that in case the data type is an object, this branch can contain
195/// only this object.
196///
197/// Note that this function is invoked by TTree::Branch
198
199TBranch::TBranch(TTree *tree, const char *name, void *address, const char *leaflist, Int_t basketsize, Int_t compress)
200 : TNamed(name, leaflist)
201, TAttFill(0, 1001)
202, fCompress(compress)
203, fBasketSize((basketsize < 100) ? 100 : basketsize)
204, fEntryOffsetLen(0)
205, fWriteBasket(0)
206, fEntryNumber(0)
207, fExtraBasket(nullptr)
208, fIOFeatures(tree ? tree->GetIOFeatures().GetFeatures() : 0)
209, fOffset(0)
210, fMaxBaskets(10)
211, fNBaskets(0)
212, fSplitLevel(0)
213, fNleaves(0)
214, fReadBasket(0)
215, fReadEntry(-1)
216, fFirstBasketEntry(-1)
217, fNextBasketEntry(-1)
218, fCurrentBasket(0)
219, fEntries(0)
220, fFirstEntry(0)
221, fTotBytes(0)
222, fZipBytes(0)
223, fBranches()
224, fLeaves()
225, fBaskets(fMaxBaskets)
226, fBasketBytes(0)
227, fBasketEntry(0)
228, fBasketSeek(0)
229, fTree(tree)
230, fMother(0)
231, fParent(0)
232, fAddress((char *)address)
233, fDirectory(fTree->GetDirectory())
234, fFileName("")
235, fEntryBuffer(0)
236, fTransientBuffer(0)
237, fBrowsables(0)
238, fBulk(*this)
239, fSkipZip(kFALSE)
240, fReadLeaves(&TBranch::ReadLeavesImpl)
241, fFillLeaves(&TBranch::FillLeavesImpl)
242{
243 Init(name,leaflist,compress);
244}
245
246////////////////////////////////////////////////////////////////////////////////
247/// Create a Branch as a child of another Branch
248///
249/// See documentation for
250/// TBranch::TBranch(TTree *, const char *, void *, const char *, Int_t, Int_t)
251
252TBranch::TBranch(TBranch *parent, const char *name, void *address, const char *leaflist, Int_t basketsize,
253 Int_t compress)
254: TNamed(name, leaflist)
255, TAttFill(0, 1001)
256, fCompress(compress)
257, fBasketSize((basketsize < 100) ? 100 : basketsize)
258, fEntryOffsetLen(0)
259, fWriteBasket(0)
260, fEntryNumber(0)
261, fExtraBasket(nullptr)
262, fIOFeatures(parent->fIOFeatures)
263, fOffset(0)
264, fMaxBaskets(10)
265, fNBaskets(0)
266, fSplitLevel(0)
267, fNleaves(0)
268, fReadBasket(0)
269, fReadEntry(-1)
270, fFirstBasketEntry(-1)
271, fNextBasketEntry(-1)
272, fCurrentBasket(0)
273, fEntries(0)
274, fFirstEntry(0)
275, fTotBytes(0)
276, fZipBytes(0)
277, fBranches()
278, fLeaves()
279, fBaskets(fMaxBaskets)
280, fBasketBytes(0)
281, fBasketEntry(0)
282, fBasketSeek(0)
283, fTree(parent ? parent->GetTree() : 0)
284, fMother(parent ? parent->GetMother() : 0)
285, fParent(parent)
286, fAddress((char *)address)
287, fDirectory(fTree ? fTree->GetDirectory() : 0)
288, fFileName("")
289, fEntryBuffer(0)
290, fTransientBuffer(0)
291, fBrowsables(0)
292, fBulk(*this)
293, fSkipZip(kFALSE)
294, fReadLeaves(&TBranch::ReadLeavesImpl)
295, fFillLeaves(&TBranch::FillLeavesImpl)
296{
297 Init(name,leaflist,compress);
298}
299
300void TBranch::Init(const char* name, const char* leaflist, Int_t compress)
301{
302 // Initialization routine called from the constructor. This should NOT be made virtual.
303
305 if ((compress == -1) && fTree->GetDirectory()) {
306 TFile* bfile = fTree->GetDirectory()->GetFile();
307 if (bfile) {
309 }
310 }
311
315
316 for (Int_t i = 0; i < fMaxBaskets; ++i) {
317 fBasketBytes[i] = 0;
318 fBasketEntry[i] = 0;
319 fBasketSeek[i] = 0;
320 }
321
322 //
323 // Decode the leaflist (search for : as separator).
324 //
325
326 char* nameBegin = const_cast<char*>(leaflist);
327 Int_t offset = 0;
328 auto len = strlen(leaflist);
329 // FIXME: Make these string streams instead.
330 char* leafname = new char[len + 1];
331 char* leaftype = new char[320];
332 // Note: The default leaf type is a float.
333 strlcpy(leaftype, "F",320);
334 char* pos = const_cast<char*>(leaflist);
335 const char* leaflistEnd = leaflist + len;
336 for (; pos <= leaflistEnd; ++pos) {
337 // -- Scan leaf specification and create leaves.
338 if ((*pos == ':') || (*pos == 0)) {
339 // -- Reached end of a leaf spec, create a leaf.
340 Int_t lenName = pos - nameBegin;
341 char* ctype = 0;
342 if (lenName) {
343 strncpy(leafname, nameBegin, lenName);
344 leafname[lenName] = 0;
345 ctype = strstr(leafname, "/");
346 if (ctype) {
347 *ctype = 0;
348 strlcpy(leaftype, ctype + 1,320);
349 }
350 }
351 if (lenName == 0 || ctype == leafname) {
352 Warning("TBranch","No name was given to the leaf number '%d' in the leaflist of the branch '%s'.",fNleaves,name);
353 snprintf(leafname,640,"__noname%d",fNleaves);
354 }
355 TLeaf* leaf = 0;
356 if (leaftype[1] == '[' && !strchr(leaftype, ',')) {
357 Warning("TBranch", "Array size for branch '%s' must be specified after leaf name, not after the type name!", name);
358 // and continue for backward compatibility?
359 } else if (leaftype[1] && !strchr(leaftype, ',')) {
360 Warning("TBranch", "Extra characters after type tag '%s' for branch '%s'; must be one character.", leaftype, name);
361 // and continue for backward compatibility?
362 }
363 if (*leaftype == 'C') {
364 leaf = new TLeafC(this, leafname, leaftype);
365 } else if (*leaftype == 'O') {
366 leaf = new TLeafO(this, leafname, leaftype);
367 } else if (*leaftype == 'B') {
368 leaf = new TLeafB(this, leafname, leaftype);
369 } else if (*leaftype == 'b') {
370 leaf = new TLeafB(this, leafname, leaftype);
371 leaf->SetUnsigned();
372 } else if (*leaftype == 'S') {
373 leaf = new TLeafS(this, leafname, leaftype);
374 } else if (*leaftype == 's') {
375 leaf = new TLeafS(this, leafname, leaftype);
376 leaf->SetUnsigned();
377 } else if (*leaftype == 'I') {
378 leaf = new TLeafI(this, leafname, leaftype);
379 } else if (*leaftype == 'i') {
380 leaf = new TLeafI(this, leafname, leaftype);
381 leaf->SetUnsigned();
382 } else if (*leaftype == 'F') {
383 leaf = new TLeafF(this, leafname, leaftype);
384 } else if (*leaftype == 'f') {
385 leaf = new TLeafF16(this, leafname, leaftype);
386 } else if (*leaftype == 'L') {
387 leaf = new TLeafL(this, leafname, leaftype);
388 } else if (*leaftype == 'l') {
389 leaf = new TLeafL(this, leafname, leaftype);
390 leaf->SetUnsigned();
391 } else if (*leaftype == 'D') {
392 leaf = new TLeafD(this, leafname, leaftype);
393 } else if (*leaftype == 'd') {
394 leaf = new TLeafD32(this, leafname, leaftype);
395 } else if (*leaftype == 'G') {
396 leaf = new TLeafG(this, leafname, leaftype);
397 } else if (*leaftype == 'g') {
398 leaf = new TLeafG(this, leafname, leaftype);
399 leaf->SetUnsigned();
400 }
401 if (!leaf) {
402 Error("TLeaf", "Illegal data type for %s/%s", name, leaflist);
403 delete[] leaftype;
404 delete [] leafname;
405 MakeZombie();
406 return;
407 }
408 if (leaf->IsZombie()) {
409 delete leaf;
410 leaf = 0;
411 auto msg = "Illegal leaf: %s/%s. If this is a variable size C array it's possible that the branch holding the size is not available.";
412 Error("TBranch", msg, name, leaflist);
413 delete [] leafname;
414 delete[] leaftype;
415 MakeZombie();
416 return;
417 }
418 leaf->SetBranch(this);
419 leaf->SetAddress((char*) (fAddress + offset));
420 leaf->SetOffset(offset);
421 if (leaf->GetLeafCount()) {
422 // -- Leaf is a varying length array, we need an offset array.
423 fEntryOffsetLen = 1000;
424 }
425 if (leaf->InheritsFrom(TLeafC::Class())) {
426 // -- Leaf is a character string, we need an offset array.
427 fEntryOffsetLen = 1000;
428 }
429 ++fNleaves;
430 fLeaves.Add(leaf);
431 fTree->GetListOfLeaves()->Add(leaf);
432 if (*pos == 0) {
433 // -- We reached the end of the leaf specification.
434 break;
435 }
436 nameBegin = pos + 1;
437 offset += leaf->GetLenType() * leaf->GetLen();
438 }
439 }
440 delete[] leafname;
441 leafname = 0;
442 delete[] leaftype;
443 leaftype = 0;
444
445}
446
447////////////////////////////////////////////////////////////////////////////////
448/// Destructor.
449
451{
452 delete fBrowsables;
453 fBrowsables = 0;
454
455 // Note: We do *not* have ownership of the buffer.
456 fEntryBuffer = 0;
457
458 delete [] fBasketSeek;
459 fBasketSeek = 0;
460
461 delete [] fBasketEntry;
462 fBasketEntry = 0;
463
464 delete [] fBasketBytes;
465 fBasketBytes = 0;
466
468 delete fExtraBasket;
470 fNBaskets = 0;
471 fCurrentBasket = 0;
473 fNextBasketEntry = -1;
474
475 // Remove our leaves from our tree's list of leaves.
476 if (fTree) {
478 if (lst && lst->GetLast()!=-1) {
479 lst->RemoveAll(&fLeaves);
480 }
481 }
482 // And delete our leaves.
483 fLeaves.Delete();
484
486
487 // If we are in a directory and that directory is not the same
488 // directory that our tree is in, then try to find an open file
489 // with the name fFileName. If we find one, delete that file.
490 // We are attempting to close any alternate file which we have
491 // been directed to write our baskets to.
492 // FIXME: We make no attempt to check if someone else might be
493 // using this file. This is very user hostile. A violation
494 // of the principle of least surprises.
495 //
496 // Warning. Must use FindObject by name instead of fDirectory->GetFile()
497 // because two branches may point to the same file and the file
498 // may have already been deleted in the previous branch.
499 if (fDirectory && (!fTree || fDirectory != fTree->GetDirectory())) {
500 TString bFileName( GetRealFileName() );
501
503 TFile* file = (TFile*)gROOT->GetListOfFiles()->FindObject(bFileName);
504 if (file){
505 file->Close();
506 delete file;
507 file = 0;
508 }
509 }
510
511 fTree = 0;
512 fDirectory = 0;
513
514 if (fTransientBuffer) {
515 delete fTransientBuffer;
517 }
518}
519
520////////////////////////////////////////////////////////////////////////////////
521/// Returns the transient buffer currently used by this TBranch for reading/writing baskets.
522
524{
525 if (fTransientBuffer) {
528 }
529 return fTransientBuffer;
530 }
532 return fTransientBuffer;
533}
534
535////////////////////////////////////////////////////////////////////////////////
536/// Add the basket to this branch.
537///
538/// Warning: if the basket are not 'flushed/copied' in the same
539/// order as they were created, this will induce a slow down in
540/// the insert (since we'll need to move all the record that are
541/// entere 'too early').
542/// Warning we also assume that the __current__ write basket is
543/// not present (aka has been removed) or is empty (no entries).
544
545void TBranch::AddBasket(TBasket& b, Bool_t ondisk, Long64_t startEntry)
546{
547 TBasket *basket = &b;
548
549 basket->SetBranch(this);
550
551 if (fWriteBasket >= fMaxBaskets) {
553 }
554 Int_t where = fWriteBasket;
555
556 if (where && startEntry < fBasketEntry[where-1]) {
557 // Need to find the right location and move the possible baskets
558
559 if (!ondisk) {
560 Warning("AddBasket","The assumption that out-of-order basket only comes from disk based ntuple is false.");
561 }
562
563 if (startEntry < fBasketEntry[0]) {
564 where = 0;
565 } else {
566 for(Int_t i=fWriteBasket-1; i>=0; --i) {
567 if (fBasketEntry[i] < startEntry) {
568 where = i+1;
569 break;
570 } else if (fBasketEntry[i] == startEntry) {
571 Error("AddBasket","An out-of-order basket matches the entry number of an existing basket.");
572 }
573 }
574 }
575
576 if (where < fWriteBasket) {
577 // We shall move the content of the array
578 for (Int_t j=fWriteBasket; j > where; --j) {
579 fBasketEntry[j] = fBasketEntry[j-1];
580 fBasketBytes[j] = fBasketBytes[j-1];
581 fBasketSeek[j] = fBasketSeek[j-1];
582 }
583 }
584 }
585 fBasketEntry[where] = startEntry;
586
587 TBasket *existing = (TBasket*)fBaskets.At(fWriteBasket);
588 if (existing && existing->GetNevBuf()) {
589 Fatal("AddBasket", "Dropping non-empty 'write' basket in %s %s",
590 GetTree()->GetName(), GetName());
591 }
592 delete existing;
593 if (ondisk) {
594 fBasketBytes[where] = basket->GetNbytes(); // not for in mem
595 fBasketSeek[where] = basket->GetSeekKey(); // not for in mem
597 ++fWriteBasket;
598 } else {
599 ++fNBaskets;
600 // The basket we are adding becomes the new 'write' basket.
603 }
604
605 fEntries += basket->GetNevBuf();
606 fEntryNumber += basket->GetNevBuf();
607 if (ondisk) {
608 fTotBytes += basket->GetObjlen() + basket->GetKeylen() ;
609 fZipBytes += basket->GetNbytes();
610 fTree->AddTotBytes(basket->GetObjlen() + basket->GetKeylen());
611 fTree->AddZipBytes(basket->GetNbytes());
612 }
613}
614
615////////////////////////////////////////////////////////////////////////////////
616/// Add the start entry of the write basket (not yet created)
617
619{
620 if (fWriteBasket >= fMaxBaskets) {
622 }
623 Int_t where = fWriteBasket;
624
625 if (where && startEntry < fBasketEntry[where-1]) {
626 // Need to find the right location and move the possible baskets
627
628 Fatal("AddBasket","The last basket must have the highest entry number (%s/%lld/%d).",GetName(),startEntry,fWriteBasket);
629
630 }
631 // The first basket (should) always start at zero. If we are asked to update
632 // it, this likely to be from merging 'empty' branches (base class node and the likes)
633 if (where) {
634 fBasketEntry[where] = startEntry;
636 }
637}
638
639////////////////////////////////////////////////////////////////////////////////
640/// Loop on all leaves of this branch to back fill Basket buffer.
641///
642/// Use this routine instead of TBranch::Fill when filling a branch individually
643/// to catch up with the number of entries already in the TTree.
644///
645/// First it calls TBranch::Fill and then if the number of entries of the branch
646/// reach one of TTree cluster's boundary, the basket is flushed.
647///
648/// The function returns the number of bytes committed to the memory basket.
649/// If a write error occurs, the number of bytes returned is -1.
650/// If no data are written, because e.g. the branch is disabled,
651/// the number of bytes returned is 0.
652///
653/// To insure that the baskets of each cluster are located close by in the
654/// file, when back-filling multiple branches make sure to call BackFill
655/// for the same entry for all the branches consecutively
656/// ~~~ {.cpp}
657/// for( auto e = 0; e < tree->GetEntries(); ++e ) { // loop over entries.
658/// for( auto branch : branchCollection) {
659/// ... Make change to the data associated with the branch ...
660/// branch->BackFill();
661/// }
662/// }
663/// // Since we loop over all the branches for each new entry
664/// // all the baskets for a cluster are consecutive in the file.
665/// ~~~
666/// rather than doing all the entries of one branch at a time.
667/// ~~~ {.cpp}
668/// // Do NOT do things in the following order, it will lead to
669/// // poorly clustered files.
670/// for(auto branch : branchCollection) {
671/// for( auto e = 0; e < tree->GetEntries(); ++e ) { // loop over entries.
672/// ... Make change to the data associated with the branch ...
673/// branch->BackFill();
674/// }
675/// }
676/// // Since we loop over all the entries for one branch
677/// // all the baskets for that branch are consecutive.
678/// ~~~
679
681
682 // Get the end of the next cluster.
683 auto cluster = GetTree()->GetClusterIterator( GetEntries() );
684 cluster.Next();
685 auto endCluster = cluster.GetNextEntry();
686
687 auto result = FillImpl(nullptr);
688
689 if ( result && GetEntries() >= endCluster ) {
690 FlushBaskets();
691 }
692
693 return result;
694}
695
696////////////////////////////////////////////////////////////////////////////////
697/// Browser interface.
698
700{
701 if (fNleaves > 1) {
703 } else {
704 // Get the name and strip any extra brackets
705 // in order to get the full arrays.
706 TString name = GetName();
707 Int_t pos = name.First('[');
708 if (pos!=kNPOS) name.Remove(pos);
709
710 GetTree()->Draw(name, "", b ? b->GetDrawOption() : "");
711 if (gPad) gPad->Update();
712 }
713}
714
715 ///////////////////////////////////////////////////////////////////////////////
716 /// Loop on all branch baskets. If the file where branch buffers reside is
717 /// writable, free the disk space associated to the baskets of the branch,
718 /// then call Reset(). If the option contains "all", delete also the baskets
719 /// for the subbranches.
720 /// The branch is reset.
721 ///
722 /// NOTE that this function must be used with extreme care. Deleting branch baskets
723 /// fragments the file and may introduce inefficiencies when adding new entries
724 /// in the Tree or later on when reading the Tree.
725
727{
728 TString opt = option;
729 opt.ToLower();
730 TFile *file = GetFile(0);
731
733 for(Int_t i=0; i<fWriteBasket; i++) {
734 if (fBasketSeek[i]) file->MakeFree(fBasketSeek[i],fBasketSeek[i]+fBasketBytes[i]-1);
735 }
736 }
737
738 // process subbranches
739 if (opt.Contains("all")) {
741 Int_t nb = lb->GetEntriesFast();
742 for (Int_t j = 0; j < nb; j++) {
743 TBranch* branch = (TBranch*) lb->UncheckedAt(j);
744 if (branch) branch->DeleteBaskets("all");
745 }
746 }
747 DropBaskets("all");
748 Reset();
749}
750
751////////////////////////////////////////////////////////////////////////////////
752/// Loop on all branch baskets. Drop all baskets from memory except readbasket.
753/// If the option contains "all", drop all baskets including
754/// read- and write-baskets (unless they are not stored individually on disk).
755/// The option "all" also lead to DropBaskets being called on the sub-branches.
756
758{
759 Bool_t all = kFALSE;
760 if (options && options[0]) {
761 TString opt = options;
762 opt.ToLower();
763 if (opt.Contains("all")) all = kTRUE;
764 }
765
766 TBasket *basket;
767 Int_t nbaskets = fBaskets.GetEntriesFast();
768
769 if ( (fNBaskets>1) || all ) {
770 //slow case
771 for (Int_t i=0;i<nbaskets;i++) {
772 basket = (TBasket*)fBaskets.UncheckedAt(i);
773 if (!basket) continue;
774 if ((i == fReadBasket || i == fWriteBasket) && !all) continue;
775 // if the basket is not yet on file but already has event in it
776 // we must continue to avoid dropping the basket (and thus losing data)
777 if (fBasketBytes[i]==0 && basket->GetNevBuf() > 0) continue;
778 basket->DropBuffers();
779 --fNBaskets;
781 if (basket == fCurrentBasket) {
782 fCurrentBasket = 0;
784 fNextBasketEntry = -1;
785 }
786 delete basket;
787 }
788
789 // process subbranches
790 if (all) {
792 Int_t nb = lb->GetEntriesFast();
793 for (Int_t j = 0; j < nb; j++) {
794 TBranch* branch = (TBranch*) lb->UncheckedAt(j);
795 if (!branch) continue;
796 branch->DropBaskets("all");
797 }
798 }
799 } else {
800 //fast case
801 if (nbaskets > 0) {
802 Int_t i = fBaskets.GetLast();
803 basket = (TBasket*)fBaskets.UncheckedAt(i);
804 if (basket && fBasketBytes[i]!=0) {
805 basket->DropBuffers();
806 if (basket == fCurrentBasket) {
807 fCurrentBasket = 0;
809 fNextBasketEntry = -1;
810 }
811 delete basket;
812 fBaskets.AddAt(0,i);
813 fBaskets.SetLast(-1);
814 fNBaskets = 0;
815 }
816 }
817 }
818
819}
820
821////////////////////////////////////////////////////////////////////////////////
822/// Increase BasketEntry buffer of a minimum of 10 locations
823/// and a maximum of 50 per cent of current size.
824
826{
827 Int_t newsize = TMath::Max(10,Int_t(1.5*fMaxBaskets));
830 newsize*sizeof(Long64_t),fMaxBaskets*sizeof(Long64_t));
832 newsize*sizeof(Long64_t),fMaxBaskets*sizeof(Long64_t));
833
834 fMaxBaskets = newsize;
835
836 fBaskets.Expand(newsize);
837
838 for (Int_t i=fWriteBasket;i<fMaxBaskets;i++) {
839 fBasketBytes[i] = 0;
840 fBasketEntry[i] = 0;
841 fBasketSeek[i] = 0;
842 }
843}
844
845////////////////////////////////////////////////////////////////////////////////
846/// Loop on all leaves of this branch to fill Basket buffer.
847///
848/// If TBranchIMTHelper is non-null and it is time to WriteBasket, then we will
849/// use TBB to compress in parallel.
850///
851/// The function returns the number of bytes committed to the memory basket.
852/// If a write error occurs, the number of bytes returned is -1.
853/// If no data are written, because e.g. the branch is disabled,
854/// the number of bytes returned is 0.
855
857{
858 if (TestBit(kDoNotProcess)) {
859 return 0;
860 }
861
863 if (!basket) {
864 basket = fTree->CreateBasket(this); // create a new basket
865 if (!basket) return 0;
866 ++fNBaskets;
868 }
869 TBuffer* buf = basket->GetBufferRef();
870
871 // Fill basket buffer.
872
873 Int_t nsize = 0;
874
875 if (buf->IsReading()) {
876 basket->SetWriteMode();
877 }
878
880 buf->ResetMap();
881 }
882
883 Int_t lnew = 0;
884 Int_t nbytes = 0;
885
886 if (fEntryBuffer) {
887 nbytes = FillEntryBuffer(basket,buf,lnew);
888 } else {
889 Int_t lold = buf->Length();
890 basket->Update(lold);
891 ++fEntries;
892 ++fEntryNumber;
893 (this->*fFillLeaves)(*buf);
894 if (buf->GetMapCount()) {
895 // The map is used.
897 }
898 lnew = buf->Length();
899 nbytes = lnew - lold;
900 }
901
902 if (fEntryOffsetLen) {
903 Int_t nevbuf = basket->GetNevBuf();
904 // Total size in bytes of EntryOffset table.
905 nsize = nevbuf * sizeof(Int_t);
906 } else {
907 if (!basket->GetNevBufSize()) {
908 basket->SetNevBufSize(nbytes);
909 }
910 }
911
912 // Should we create a new basket?
913 // fSkipZip force one entry per buffer (old stuff still maintained for CDF)
914 // Transfer full compressed buffer only
915
916 // If GetAutoFlush() is less than zero, then we are determining the end of the autocluster
917 // based upon the number of bytes already flushed. This is incompatible with one-basket-per-cluster
918 // (since we will grow the basket indefinitely and never flush!). Hence, we wait until the
919 // first event cluster is written out and *then* enable one-basket-per-cluster mode.
920 bool noFlushAtCluster = !fTree->TestBit(TTree::kOnlyFlushAtCluster) || (fTree->GetAutoFlush() < 0);
921
922 if (noFlushAtCluster && !fTree->TestBit(TTree::kCircular) &&
924 ((lnew + (2 * nsize) + nbytes) >= fBasketSize))) {
925 Int_t nout = WriteBasketImpl(basket, fWriteBasket, imtHelper);
926 if (nout < 0) Error("TBranch::Fill", "Failed to write out basket.\n");
927 return (nout >= 0) ? nbytes : -1;
928 }
929 return nbytes;
930}
931
932////////////////////////////////////////////////////////////////////////////////
933/// Copy the data from fEntryBuffer into the current basket.
934
936{
937 Int_t nbytes = 0;
938 Int_t objectStart = 0;
939 Int_t last = 0;
940 Int_t lold = buf->Length();
941
942 // Handle the special case of fEntryBuffer != 0
943 if (fEntryBuffer->IsA() == TMessage::Class()) {
944 objectStart = 8;
945 }
947 // The buffer given as input has not been decompressed.
948 if (basket->GetNevBuf()) {
949 // If the basket already contains entry we need to close it
950 // out. (This is because we can only transfer full compressed
951 // buffer)
953 // And restart from scratch
954 return Fill();
955 }
956 Int_t startpos = fEntryBuffer->Length();
958 static TBasket toread_fLast;
960 toread_fLast.Streamer(*fEntryBuffer);
962 last = toread_fLast.GetLast();
963 // last now contains the decompressed number of bytes.
964 fEntryBuffer->SetBufferOffset(startpos);
965 buf->SetBufferOffset(0);
967 basket->Update(lold);
968 } else {
969 // We are required to copy starting at the version number (so not
970 // including the class name.
971 // See if byte count is here, if not it class still be a newClass
972 const UInt_t kNewClassTag = 0xFFFFFFFF;
973 const UInt_t kByteCountMask = 0x40000000; // OR the byte count with this
974 UInt_t tag = 0;
975 UInt_t startpos = fEntryBuffer->Length();
976 fEntryBuffer->SetBufferOffset(objectStart);
977 *fEntryBuffer >> tag;
978 if (tag & kByteCountMask) {
979 *fEntryBuffer >> tag;
980 }
981 if (tag == kNewClassTag) {
982 UInt_t maxsize = 256;
983 char* s = new char[maxsize];
984 Int_t name_start = fEntryBuffer->Length();
985 fEntryBuffer->ReadString(s, maxsize); // Reads at most maxsize - 1 characters, plus null at end.
986 while (strlen(s) == (maxsize - 1)) {
987 // The classname is too large, try again with a large buffer.
988 fEntryBuffer->SetBufferOffset(name_start);
989 maxsize *= 2;
990 delete[] s;
991 s = new char[maxsize];
992 fEntryBuffer->ReadString(s, maxsize); // Reads at most maxsize - 1 characters, plus null at end
993 }
994 delete[] s;
995 } else {
996 fEntryBuffer->SetBufferOffset(objectStart);
997 }
998 objectStart = fEntryBuffer->Length();
999 fEntryBuffer->SetBufferOffset(startpos);
1000 basket->Update(lold, objectStart - fEntryBuffer->GetBufferDisplacement());
1001 }
1002 fEntries++;
1003 fEntryNumber++;
1004 UInt_t len = 0;
1005 UInt_t startpos = fEntryBuffer->Length();
1006 if (startpos > UInt_t(objectStart)) {
1007 // We assume this buffer have just been directly filled
1008 // the current position in the buffer indicates the end of the object!
1009 len = fEntryBuffer->Length() - objectStart;
1010 } else {
1011 // The buffer have been acquired either via TSocket or via
1012 // TBuffer::SetBuffer(newloc,newsize)
1013 // Only the actual size of the memory buffer gives us an hint about where
1014 // the object ends.
1015 len = fEntryBuffer->BufferSize() - objectStart;
1016 }
1017 buf->WriteBuf(fEntryBuffer->Buffer() + objectStart, len);
1019 // The original buffer came pre-compressed and thus the buffer Length
1020 // does not really show the really object size
1021 // lnew = nbytes = basket->GetLast();
1022 nbytes = last;
1023 lnew = last;
1024 } else {
1025 lnew = buf->Length();
1026 nbytes = lnew - lold;
1027 }
1028
1029 return nbytes;
1030}
1031
1032////////////////////////////////////////////////////////////////////////////////
1033/// Find the immediate sub-branch with passed name.
1034
1036{
1037 // We allow the user to pass only the last dotted component of the name.
1038 std::string longnm;
1039 longnm.reserve(fName.Length()+strlen(name)+3);
1040 longnm = fName.Data();
1041 if (longnm[longnm.length()-1]==']') {
1042 std::size_t dim = longnm.find_first_of("[");
1043 if (dim != std::string::npos) {
1044 longnm.erase(dim);
1045 }
1046 }
1047 if (longnm[longnm.length()-1] != '.') {
1048 longnm += '.';
1049 }
1050 longnm += name;
1051 UInt_t namelen = strlen(name);
1052
1053 Int_t nbranches = fBranches.GetEntries();
1054 TBranch* branch = 0;
1055 for(Int_t i = 0; i < nbranches; ++i) {
1056 branch = (TBranch*) fBranches.UncheckedAt(i);
1057
1058 const char *brname = branch->fName.Data();
1059 UInt_t brlen = branch->fName.Length();
1060 if (brname[brlen-1]==']') {
1061 const char *dim = strchr(brname,'[');
1062 if (dim) {
1063 brlen = dim - brname;
1064 }
1065 }
1066 if (namelen == brlen /* same effective size */
1067 && strncmp(name,brname,brlen) == 0) {
1068 return branch;
1069 }
1070 if (brlen == (size_t)longnm.length()
1071 && strncmp(longnm.c_str(),brname,brlen) == 0) {
1072 return branch;
1073 }
1074 }
1075 return 0;
1076}
1077
1078////////////////////////////////////////////////////////////////////////////////
1079/// Find the leaf corresponding to the name 'searchname'.
1080
1081TLeaf* TBranch::FindLeaf(const char* searchname)
1082{
1083 TString leafname;
1084 TString leaftitle;
1085 TString longname;
1086 TString longtitle;
1087
1088 // We allow the user to pass only the last dotted component of the name.
1089 TIter next(GetListOfLeaves());
1090 TLeaf* leaf = 0;
1091 while ((leaf = (TLeaf*) next())) {
1092 leafname = leaf->GetName();
1093 Ssiz_t dim = leafname.First('[');
1094 if (dim >= 0) leafname.Remove(dim);
1095
1096 if (leafname == searchname) return leaf;
1097
1098 // The leaf element contains the branch name in its name, let's use the title.
1099 leaftitle = leaf->GetTitle();
1100 dim = leaftitle.First('[');
1101 if (dim >= 0) leaftitle.Remove(dim);
1102
1103 if (leaftitle == searchname) return leaf;
1104
1105 TBranch* branch = leaf->GetBranch();
1106 if (branch) {
1107 longname.Form("%s.%s",branch->GetName(),leafname.Data());
1108 dim = longname.First('[');
1109 if (dim>=0) longname.Remove(dim);
1110 if (longname == searchname) return leaf;
1111
1112 // The leaf element contains the branch name in its name.
1113 longname.Form("%s.%s",branch->GetName(),searchname);
1114 if (longname==leafname) return leaf;
1115
1116 longtitle.Form("%s.%s",branch->GetName(),leaftitle.Data());
1117 dim = longtitle.First('[');
1118 if (dim>=0) longtitle.Remove(dim);
1119 if (longtitle == searchname) return leaf;
1120
1121 // The following is for the case where the branch is only
1122 // a sub-branch. Since we do not see it through
1123 // TTree::GetListOfBranches, we need to see it indirectly.
1124 // This is the less sturdy part of this search ... it may
1125 // need refining ...
1126 if (strstr(searchname, ".") && !strcmp(searchname, branch->GetName())) return leaf;
1127 }
1128 }
1129 return 0;
1130}
1131
1132////////////////////////////////////////////////////////////////////////////////
1133/// Flush to disk all the baskets of this branch and any of subbranches.
1134/// Return the number of bytes written or -1 in case of write error.
1135
1137{
1138 UInt_t nerror = 0;
1139 Int_t nbytes = 0;
1140
1141 Int_t maxbasket = fWriteBasket + 1;
1142 // The following protection is not necessary since we should always
1143 // have fWriteBasket < fBasket.GetSize()
1144 //if (fBaskets.GetSize() < maxbasket) {
1145 // maxbasket = fBaskets.GetSize();
1146 //}
1147 for(Int_t i=0; i != maxbasket; ++i) {
1148 if (fBaskets.UncheckedAt(i)) {
1149 Int_t nwrite = FlushOneBasket(i);
1150 if (nwrite<0) {
1151 ++nerror;
1152 } else {
1153 nbytes += nwrite;
1154 }
1155 }
1156 }
1158 for (Int_t i = 0; i < len; ++i) {
1159 TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
1160 if (!branch) {
1161 continue;
1162 }
1163 Int_t nwrite = branch->FlushBaskets();
1164 if (nwrite<0) {
1165 ++nerror;
1166 } else {
1167 nbytes += nwrite;
1168 }
1169 }
1170 if (nerror) {
1171 return -1;
1172 } else {
1173 return nbytes;
1174 }
1175}
1176
1177////////////////////////////////////////////////////////////////////////////////
1178/// If we have a write basket in memory and it contains some entries and
1179/// has not yet been written to disk, we write it and delete it from memory.
1180/// Return the number of bytes written;
1181
1183{
1184 Int_t nbytes = 0;
1186 TBasket *basket = (TBasket*)fBaskets.UncheckedAt(ibasket);
1187
1188 if (basket) {
1189 if (basket->GetNevBuf()
1190 && fBasketSeek[ibasket]==0) {
1191 // If the basket already contains entry we need to close it out.
1192 // (This is because we can only transfer full compressed buffer)
1193
1194 if (basket->GetBufferRef()->IsReading()) {
1195 basket->SetWriteMode();
1196 }
1197 nbytes = WriteBasket(basket,ibasket);
1198
1199 } else {
1200 // If the basket is empty or has already been written.
1201 if ((Int_t)ibasket==fWriteBasket) {
1202 // Nothing to do.
1203 } else {
1204 basket->DropBuffers();
1205 if (basket == fCurrentBasket) {
1206 fCurrentBasket = 0;
1207 fFirstBasketEntry = -1;
1208 fNextBasketEntry = -1;
1209 }
1210 delete basket;
1211 --fNBaskets;
1212 fBaskets[ibasket] = 0;
1213 }
1214 }
1215 }
1216 }
1217 return nbytes;
1218}
1219
1220////////////////////////////////////////////////////////////////////////////////
1221/// Return pointer to basket basketnumber in this Branch
1222///
1223/// If a new buffer must be created and the user_buffer argument is non-null,
1224/// then the memory in the user_buffer will be shared with the returned TBasket.
1225
1226TBasket* TBranch::GetBasketImpl(Int_t basketnumber, TBuffer *user_buffer)
1227{
1228 // This counter in the sequential case collects errors coming also from
1229 // different files (suppose to have a program reading f1.root, f2.root ...)
1230 // In the mt case, it is made atomic: it safely collects errors from
1231 // different files processed simultaneously.
1232 static std::atomic<Int_t> nerrors(0);
1233
1234 // reference to an existing basket in memory ?
1235 if (basketnumber <0 || basketnumber > fWriteBasket) return 0;
1236 TBasket *basket = (TBasket*)fBaskets.UncheckedAt(basketnumber);
1237 if (basket) return basket;
1238 if (basketnumber == fWriteBasket) return 0;
1239
1240 // create/decode basket parameters from buffer
1241 TFile *file = GetFile(0);
1242 if (file == 0) {
1243 return 0;
1244 }
1245 // if cluster pre-fetching or retaining is on, do not re-use existing baskets
1246 // unless a new cluster is used.
1248 basket = GetFreshCluster(user_buffer);
1249 else
1250 basket = GetFreshBasket(basketnumber, user_buffer);
1251
1252 // fSkipZip is old stuff still maintained for CDF
1254 if (fBasketBytes[basketnumber] == 0) {
1255 fBasketBytes[basketnumber] = basket->ReadBasketBytes(fBasketSeek[basketnumber],file);
1256 }
1257 //add branch to cache (if any)
1258 {
1259 R__LOCKGUARD_IMT(gROOTMutex); // Lock for parallel TTree I/O
1261 if (pf){
1262 if (pf->IsLearning()) pf->LearnBranch(this, kFALSE);
1263 if (fSkipZip) pf->SetSkipZip();
1264 }
1265 }
1266
1267 //now read basket
1268 Int_t badread = basket->ReadBasketBuffers(fBasketSeek[basketnumber],fBasketBytes[basketnumber],file);
1269 if (R__unlikely(badread || basket->GetSeekKey() != fBasketSeek[basketnumber] || basket->IsZombie())) {
1270 nerrors++;
1271 if (nerrors > 10) return 0;
1272 if (nerrors == 10) {
1273 printf(" file probably overwritten: stopping reporting error messages\n");
1274 if (fBasketSeek[basketnumber] > 2000000000) {
1275 printf("===>File is more than 2 Gigabytes\n");
1276 return 0;
1277 }
1278 if (fBasketSeek[basketnumber] > 1000000000) {
1279 printf("===>Your file is may be bigger than the maximum file size allowed on your system\n");
1280 printf(" Check your AFS maximum file size limit for example\n");
1281 return 0;
1282 }
1283 }
1284 Error("GetBasket","File: %s at byte:%lld, branch:%s, entry:%lld, badread=%d, nerrors=%d, basketnumber=%d",file->GetName(),basket->GetSeekKey(),GetName(),fReadEntry,badread,nerrors.load(),basketnumber);
1285 return 0;
1286 }
1287
1288 ++fNBaskets;
1289
1290 fCacheInfo.SetUsed(basketnumber);
1291 auto perfStats = GetTree()->GetPerfStats();
1292 if (perfStats)
1293 perfStats->SetUsed(this, basketnumber);
1294
1295 fBaskets.AddAt(basket,basketnumber);
1296 return basket;
1297}
1298
1299////////////////////////////////////////////////////////////////////////////////
1300/// Return address of basket in the file
1301
1303{
1304 if (basketnumber <0 || basketnumber > fWriteBasket) return 0;
1305 return fBasketSeek[basketnumber];
1306}
1307
1308////////////////////////////////////////////////////////////////////////////////
1309/// Returns (and, if 0, creates) browsable objects for this branch
1310/// See TVirtualBranchBrowsable::FillListOfBrowsables.
1311
1313 if (fBrowsables) return fBrowsables;
1314 fBrowsables=new TList();
1316 return fBrowsables;
1317}
1318
1319////////////////////////////////////////////////////////////////////////////////
1320/// Return the name of the user class whose content is stored in this branch,
1321/// if any. If this branch was created using the 'leaflist' technique, this
1322/// function returns an empty string.
1323
1324const char * TBranch::GetClassName() const
1325{
1326 return "";
1327}
1328
1329////////////////////////////////////////////////////////////////////////////////
1330/// Return icon name depending on type of branch.
1331
1332const char* TBranch::GetIconName() const
1333{
1334 if (IsFolder())
1335 return "TBranchElement-folder";
1336 else
1337 return "TBranchElement-leaf";
1338}
1339
1340////////////////////////////////////////////////////////////////////////////////
1341/// A helper function to locate the correct basket - and its first entry.
1342/// Extracted to a common private function because it is needed by both GetEntry
1343/// and GetBulkEntries. It should not be called directly.
1344///
1345/// If a new basket must be constructed and the user_buffer is provided, then
1346/// the user_buffer will back the memory of the newly-constructed basket.
1347///
1348/// Assumes that this branch is enabled.
1349///
1350/// Returns -1 if the entry does not exist
1351/// Returns -2 in case of error
1352/// Returns the index of the basket in case of success.
1354 TBuffer *user_buffer)
1355{
1356 Long64_t updatedNext = fNextBasketEntry;
1357 Long64_t entry = fReadEntry;
1358 if (R__likely(fCurrentBasket && fFirstBasketEntry <= entry && entry < fNextBasketEntry)) {
1359 // We have found the basket containing this entry.
1360 // make sure basket buffers are in memory.
1361 basket = fCurrentBasket;
1363 return fReadBasket;
1364 } else {
1365 if ((entry < fFirstEntry) || (entry >= fEntryNumber)) {
1366 return -1;
1367 }
1369 Long64_t last = fNextBasketEntry - 1;
1370 // Are we still in the same ReadBasket?
1371 if ((entry < first) || (entry > last)) {
1373 if (fReadBasket < 0) {
1374 fNextBasketEntry = -1;
1375 Error("GetBasketAndFirst", "In the branch %s, no basket contains the entry %lld\n", GetName(), entry);
1376 return -2;
1377 }
1378 if (fReadBasket == fWriteBasket) {
1380 } else {
1382 }
1383 updatedNext = fNextBasketEntry;
1385 }
1386 // We have found the basket containing this entry.
1387 // make sure basket buffers are in memory.
1389 if (!basket) {
1390 basket = GetBasketImpl(fReadBasket, user_buffer);
1391 if (!basket) {
1392 fCurrentBasket = 0;
1393 fFirstBasketEntry = -1;
1394 fNextBasketEntry = -1;
1395 return -2;
1396 }
1397 if (fTree->GetClusterPrefetch()) {
1398 TTree::TClusterIterator clusterIterator = fTree->GetClusterIterator(entry);
1399 clusterIterator.Next();
1400 Int_t nextClusterEntry = clusterIterator.GetNextEntry();
1401 for (Int_t i = fReadBasket + 1; i < fMaxBaskets && fBasketEntry[i] < nextClusterEntry; i++) {
1402 GetBasket(i);
1403 }
1404 }
1405 // Getting the next basket might reset the current one and
1406 // cause a reset of the first / next basket entries back to -1.
1408 fNextBasketEntry = updatedNext;
1409 if (user_buffer) {
1410 // Disassociate basket from memory buffer for bulk IO
1411 // When the user provides a memory buffer (i.e., for bulk IO), we should
1412 // make sure to drop all references to that buffer in the TTree afterward.
1413 fCurrentBasket = nullptr;
1414 fBaskets[fReadBasket] = nullptr;
1415 } else {
1416 fCurrentBasket = basket;
1417 }
1418 } else {
1419 fCurrentBasket = basket;
1420 }
1421 return fReadBasket;
1422 }
1423}
1424
1425////////////////////////////////////////////////////////////////////////////////
1426/// Returns true if this branch supports bulk IO, false otherwise.
1427///
1428/// This will return true if all the various preconditions necessary hold true
1429/// to perform bulk IO (reasonable type, single TLeaf, etc); the bulk IO may
1430/// still fail, depending on the contents of the individual TBaskets loaded.
1432 return (fNleaves == 1) &&
1433 (static_cast<TLeaf*>(fLeaves.UncheckedAt(0))->GetDeserializeType() != TLeaf::DeserializeType::kExternal);
1434}
1435
1436////////////////////////////////////////////////////////////////////////////////
1437/// Read as many events as possible into the given buffer, using zero-copy
1438/// mechanisms.
1439///
1440/// Returns -1 in case of a failure. On success, returns a (non-zero) number of
1441/// events of the type held by this branch currently in the buffer.
1442///
1443/// On success, the caller should be able to access the contents of buf as
1444///
1445/// static_cast<T*>(buf.GetCurrent())
1446///
1447/// where T is the type stored on this branch. The array's length is the return
1448/// value of this function.
1449///
1450/// NOTES:
1451/// - This interface is meant to be used by higher-level, type-safe wrappers, not
1452/// by end-users.
1453/// - This only returns events
1454///
1455
1457{
1458 // TODO: eventually support multiple leaves.
1459 if (R__unlikely(fNleaves != 1)) return -1;
1460 TLeaf *leaf = static_cast<TLeaf*>(fLeaves.UncheckedAt(0));
1462 return -1;
1463 }
1464
1465 // Remember which entry we are reading.
1466 fReadEntry = entry;
1467
1468 Bool_t enabled = !TestBit(kDoNotProcess);
1469 if (R__unlikely(!enabled)) return -1;
1470 TBasket *basket = nullptr;
1472 Int_t result = GetBasketAndFirst(basket, first, &user_buf);
1473 if (R__unlikely(result < 0)) return -1;
1474 // Only support reading from full clusters.
1475 if (R__unlikely(entry != first)) {
1476 //printf("Failed to read from full cluster; first entry is %ld; requested entry is %ld.\n", first, entry);
1477 return -1;
1478 }
1479
1480 basket->PrepareBasket(entry);
1481 TBuffer* buf = basket->GetBufferRef();
1482
1483 // Test for very old ROOT files.
1484 if (R__unlikely(!buf)) {
1485 Error("GetBulkEntries", "Failed to get a new buffer.\n");
1486 return -1;
1487 }
1488 // Test for displacements, which aren't supported in fast mode.
1489 if (R__unlikely(basket->GetDisplacement())) {
1490 Error("GetBulkEntries", "Basket has displacement.\n");
1491 return -1;
1492 }
1493
1494 if (&user_buf != buf) {
1495 // The basket was already in memory and might (and might not) be backed by persistent
1496 // storage.
1497 R__ASSERT(result == fReadBasket);
1498 if (fBasketSeek[fReadBasket]) {
1499 // It is backed, so we can be destructive
1500 user_buf.SetBuffer(buf->Buffer(), buf->BufferSize());
1502 fCurrentBasket = nullptr;
1503 fBaskets[fReadBasket] = nullptr;
1504 } else {
1505 // This is the only copy, we can't return it as is to the user, just make a copy.
1506 if (user_buf.BufferSize() < buf->BufferSize()) {
1507 user_buf.AutoExpand(buf->BufferSize());
1508 }
1509 memcpy(user_buf.Buffer(), buf->Buffer(), buf->BufferSize());
1510 }
1511 }
1512
1513 Int_t bufbegin = basket->GetKeylen();
1514 user_buf.SetBufferOffset(bufbegin);
1515
1517 //printf("Requesting %d events; fNextBasketEntry=%lld; first=%lld.\n", N, fNextBasketEntry, first);
1518 if (R__unlikely(!leaf->ReadBasketFast(user_buf, N))) {
1519 Error("GetBulkEntries", "Leaf failed to read.\n");
1520 return -1;
1521 }
1522 user_buf.SetBufferOffset(bufbegin);
1523
1524 if (fCurrentBasket == nullptr) {
1525 R__ASSERT(fExtraBasket == nullptr && "fExtraBasket should have been set to nullptr by GetFreshBasket");
1526 fExtraBasket = basket;
1527 basket->DisownBuffer();
1528 }
1529
1530 return N;
1531}
1532
1533// TODO: Template this and the call above; only difference is the TLeaf function (ReadBasketFast vs
1534// ReadBasketSerialized
1536{
1537 // TODO: eventually support multiple leaves.
1538 if (R__unlikely(fNleaves != 1)) { return -1; }
1539 TLeaf *leaf = static_cast<TLeaf*>(fLeaves.UncheckedAt(0));
1541 Error("GetEntriesSerialized", "Encountered a branch with destructive deserialization; failing.");
1542 return -1;
1543 }
1544
1545 // Remember which entry we are reading.
1546 fReadEntry = entry;
1547
1548 Bool_t enabled = !TestBit(kDoNotProcess);
1549 if (R__unlikely(!enabled)) { return -1; }
1550 TBasket *basket = nullptr;
1552 Int_t result = GetBasketAndFirst(basket, first, &user_buf);
1553 if (R__unlikely(result < 0)) { return -1; }
1554 // Only support reading from full clusters.
1555 if (R__unlikely(entry != first)) {
1556 Error("GetEntriesSerialized", "Failed to read from full cluster; first entry is %lld; requested entry is %lld.\n", first, entry);
1557 return -1;
1558 }
1559
1560 basket->PrepareBasket(entry);
1561 TBuffer* buf = basket->GetBufferRef();
1562
1563 // Test for very old ROOT files.
1564 if (R__unlikely(!buf)) {
1565 Error("GetEntriesSerialized", "Failed to get a new buffer.\n");
1566 return -1;
1567 }
1568 // Test for displacements, which aren't supported in fast mode.
1569 if (R__unlikely(basket->GetDisplacement())) {
1570 Error("GetEntriesSerialized", "Basket has displacement.\n");
1571 return -1;
1572 }
1573
1574 if (&user_buf != buf) {
1575 // The basket was already in memory and might (and might not) be backed by persistent
1576 // storage.
1577 R__ASSERT(result == fReadBasket);
1578 if (fBasketSeek[fReadBasket]) {
1579 // It is backed, so we can be destructive
1580 user_buf.SetBuffer(buf->Buffer(), buf->BufferSize());
1582 fCurrentBasket = nullptr;
1583 fBaskets[fReadBasket] = nullptr;
1584 } else {
1585 // This is the only copy, we can't return it as is to the user, just make a copy.
1586 if (user_buf.BufferSize() < buf->BufferSize()) {
1587 user_buf.AutoExpand(buf->BufferSize());
1588 }
1589 memcpy(user_buf.Buffer(), buf->Buffer(), buf->BufferSize());
1590 }
1591 }
1592
1593 Int_t bufbegin = basket->GetKeylen();
1594 user_buf.SetBufferOffset(bufbegin);
1595
1597 //Info("GetEntriesSerialized", "Requesting %d events; fNextBasketEntry=%lld; first=%lld.\n", N, fNextBasketEntry, first);
1598
1599 user_buf.SetBufferOffset(bufbegin);
1600
1601 if (count_buf) {
1602 TLeaf *count_leaf = leaf->GetLeafCount();
1603 if (count_leaf) {
1604 //printf("Getting leaf count entries.\n");
1605 TBranch *count_branch = count_leaf->GetBranch();
1606 if (R__unlikely(count_branch->GetEntriesSerialized(entry, *count_buf) < 0)) {
1607 Error("GetEntriesSerialized", "Failed to read count leaf.\n");
1608 return -1;
1609 }
1610 } else {
1611 // TODO: if you ask for a count on a fixed-size branch, maybe we should
1612 // just fail?
1613 Int_t entry_count_serialized;
1614 char *tmp_ptr = reinterpret_cast<char*>(&entry_count_serialized);
1615 tobuf(tmp_ptr, leaf->GetLenType() * leaf->GetNdata());
1616 Int_t cur_offset = count_buf->GetCurrent() - count_buf->Buffer();
1617 for (int idx=0; idx<N; idx++) {
1618 *count_buf << entry_count_serialized;
1619 }
1620 count_buf->SetBufferOffset(cur_offset);
1621 }
1622 }
1623
1624 if (fCurrentBasket == nullptr) {
1625 R__ASSERT(fExtraBasket == nullptr && "fExtraBasket should have been set to nullptr by GetFreshBasket");
1626 fExtraBasket = basket;
1627 basket->DisownBuffer();
1628 }
1629
1630 return N;
1631}
1632
1633////////////////////////////////////////////////////////////////////////////////
1634/// Read all leaves of entry and return total number of bytes read.
1635///
1636/// The input argument "entry" is the entry number in the current tree.
1637/// In case of a TChain, the entry number in the current Tree must be found
1638/// before calling this function. For example:
1639///
1640///~~~ {.cpp}
1641/// TChain* chain = ...;
1642/// Long64_t localEntry = chain->LoadTree(entry);
1643/// branch->GetEntry(localEntry);
1644///~~~
1645///
1646/// The function returns the number of bytes read from the input buffer.
1647/// If entry does not exist, the function returns 0.
1648/// If an I/O error occurs, the function returns -1.
1649///
1650/// See IMPORTANT REMARKS in TTree::GetEntry.
1651
1653{
1654 // Remember which entry we are reading.
1655 fReadEntry = entry;
1656
1657 if (R__unlikely(TestBit(kDoNotProcess) && !getall)) { return 0; }
1658
1659 TBasket *basket; // will be initialized in the if/then clauses.
1661
1662 Int_t result = GetBasketAndFirst(basket, first, nullptr);
1663 if (R__unlikely(result < 0)) { return result + 1; }
1664
1665 basket->PrepareBasket(entry);
1666 TBuffer* buf = basket->GetBufferRef();
1667
1668 // This test necessary to read very old Root files (NvE).
1669 if (R__unlikely(!buf)) {
1670 TFile* file = GetFile(0);
1671 if (!file) return -1;
1673 buf = basket->GetBufferRef();
1674 }
1675
1676 // Set entry offset in buffer.
1678 buf->ResetMap();
1679 }
1680 if (R__unlikely(!buf->IsReading())) {
1681 basket->SetReadMode();
1682 }
1683
1684 Int_t* entryOffset = basket->GetEntryOffset();
1685 Int_t bufbegin = 0;
1686 if (entryOffset) {
1687 bufbegin = entryOffset[entry-first];
1688 buf->SetBufferOffset(bufbegin);
1689 Int_t* displacement = basket->GetDisplacement();
1690 if (R__unlikely(displacement)) {
1691 buf->SetBufferDisplacement(displacement[entry-first]);
1692 }
1693 } else {
1694 bufbegin = basket->GetKeylen() + ((entry-first) * basket->GetNevBufSize());
1695 buf->SetBufferOffset(bufbegin);
1696 }
1697
1698 // Int_t bufbegin = buf->Length();
1699 (this->*fReadLeaves)(*buf);
1700 return buf->Length() - bufbegin;
1701}
1702
1703////////////////////////////////////////////////////////////////////////////////
1704/// Read all leaves of an entry and export buffers to real objects in a TClonesArray list.
1705///
1706/// Returns total number of bytes read.
1707
1709{
1710 // Remember which entry we are reading.
1711 fReadEntry = entry;
1712
1713 if (TestBit(kDoNotProcess)) {
1714 return 0;
1715 }
1716 if ((entry < 0) || (entry >= fEntryNumber)) {
1717 return 0;
1718 }
1719 Int_t nbytes = 0;
1721 Long64_t last = fNextBasketEntry - 1;
1722 // Are we still in the same ReadBasket?
1723 if ((entry < first) || (entry > last)) {
1725 if (fReadBasket < 0) {
1726 fNextBasketEntry = -1;
1727 Error("In the branch %s, no basket contains the entry %d\n", GetName(), entry);
1728 return -1;
1729 }
1730 if (fReadBasket == fWriteBasket) {
1732 } else {
1734 }
1736 }
1737
1738 // We have found the basket containing this entry.
1739 // Make sure basket buffers are in memory.
1740 TBasket* basket = GetBasketImpl(fReadBasket, nullptr);
1741 fCurrentBasket = basket;
1742 if (!basket) {
1743 fFirstBasketEntry = -1;
1744 fNextBasketEntry = -1;
1745 return 0;
1746 }
1747 TBuffer* buf = basket->GetBufferRef();
1748 // Set entry offset in buffer and read data from all leaves.
1750 buf->ResetMap();
1751 }
1752 if (R__unlikely(!buf->IsReading())) {
1753 basket->SetReadMode();
1754 }
1755 Int_t* entryOffset = basket->GetEntryOffset();
1756 Int_t bufbegin = 0;
1757 if (entryOffset) {
1758 bufbegin = entryOffset[entry-first];
1759 buf->SetBufferOffset(bufbegin);
1760 Int_t* displacement = basket->GetDisplacement();
1761 if (R__unlikely(displacement)) {
1762 buf->SetBufferDisplacement(displacement[entry-first]);
1763 }
1764 } else {
1765 bufbegin = basket->GetKeylen() + ((entry-first) * basket->GetNevBufSize());
1766 buf->SetBufferOffset(bufbegin);
1767 }
1768 TLeaf* leaf = (TLeaf*) fLeaves.UncheckedAt(0);
1769 leaf->ReadBasketExport(*buf, li, nentries);
1770 nbytes = buf->Length() - bufbegin;
1771 return nbytes;
1772}
1773
1774////////////////////////////////////////////////////////////////////////////////
1775/// Fill expectedClass and expectedType with information on the data type of the
1776/// object/values contained in this branch (and thus the type of pointers
1777/// expected to be passed to Set[Branch]Address
1778/// return 0 in case of success and > 0 in case of failure.
1779
1780Int_t TBranch::GetExpectedType(TClass *&expectedClass,EDataType &expectedType)
1781{
1782 expectedClass = 0;
1783 expectedType = kOther_t;
1784 TLeaf* l = (TLeaf*) GetListOfLeaves()->At(0);
1785 if (l) {
1786 expectedType = (EDataType) gROOT->GetType(l->GetTypeName())->GetType();
1787 return 0;
1788 } else {
1789 Error("GetExpectedType", "Did not find any leaves in %s",GetName());
1790 return 1;
1791 }
1792}
1793
1794////////////////////////////////////////////////////////////////////////////////
1795/// Return pointer to the file where branch buffers reside, returns 0
1796/// in case branch buffers reside in the same file as tree header.
1797/// If mode is 1 the branch buffer file is recreated.
1798
1800{
1801 if (fDirectory) return fDirectory->GetFile();
1802
1803 // check if a file with this name is in the list of Root files
1804 TFile *file = 0;
1805 {
1807 file = (TFile*)gROOT->GetListOfFiles()->FindObject(fFileName.Data());
1808 if (file) {
1809 fDirectory = file;
1810 return file;
1811 }
1812 }
1813
1814 if (fFileName.Length() == 0) return 0;
1815
1816 TString bFileName( GetRealFileName() );
1817
1818 // Open file (new file if mode = 1)
1819 {
1821 if (mode) file = TFile::Open(bFileName, "recreate");
1822 else file = TFile::Open(bFileName);
1823 }
1824 if (!file) return 0;
1825 if (file->IsZombie()) {delete file; return 0;}
1827 return file;
1828}
1829
1830////////////////////////////////////////////////////////////////////////////////
1831/// Return a fresh basket by either reusing an existing basket that needs
1832/// to be drop (according to TTree::MemoryFull) or create a new one.
1833///
1834/// If the user_buffer argument is non-null, then the memory in the
1835/// user-provided buffer will be utilized by the underlying basket.
1836///
1837/// The basket number is used to estimate the required buffer size
1838/// and try to optimize memory usage and number of memory allocation.
1839
1840TBasket* TBranch::GetFreshBasket(Int_t basketnumber, TBuffer* user_buffer)
1841{
1842 TBasket *basket = 0;
1843 if (user_buffer && fExtraBasket) {
1844 basket = fExtraBasket;
1845 fExtraBasket = nullptr;
1846 basket->AdoptBuffer(user_buffer);
1847 } else {
1848 if (GetTree()->MemoryFull(0)) {
1849 if (fNBaskets==1) {
1850 // Steal the existing basket
1851 Int_t oldindex = fBaskets.GetLast();
1852 basket = (TBasket*)fBaskets.UncheckedAt(oldindex);
1853 if (!basket) {
1854 fBaskets.SetLast(-2); // For recalculation of Last.
1855 oldindex = fBaskets.GetLast();
1856 if (oldindex != fBaskets.LowerBound()-1) {
1857 basket = (TBasket*)fBaskets.UncheckedAt(oldindex);
1858 }
1859 }
1860 if (basket && fBasketBytes[oldindex]!=0) {
1861 if (basket == fCurrentBasket) {
1862 fCurrentBasket = 0;
1863 fFirstBasketEntry = -1;
1864 fNextBasketEntry = -1;
1865 }
1866 fBaskets.AddAt(0,oldindex);
1867 fBaskets.SetLast(-1);
1868 fNBaskets = 0;
1869 basket->ReadResetBuffer(basketnumber);
1870#ifdef R__TRACK_BASKET_ALLOC_TIME
1871 fTree->AddAllocationTime(basket->GetResetAllocationTime());
1872#endif
1874 } else {
1875 basket = fTree->CreateBasket(this);
1876 }
1877 } else if (fNBaskets == 0) {
1878 // There is nothing to drop!
1879 basket = fTree->CreateBasket(this);
1880 } else {
1881 // Memory is full and there is more than one basket,
1882 // Let DropBaskets do it job.
1883 DropBaskets();
1884 basket = fTree->CreateBasket(this);
1885 }
1886 } else {
1887 basket = fTree->CreateBasket(this);
1888 }
1889 if (user_buffer)
1890 basket->AdoptBuffer(user_buffer);
1891 }
1892 return basket;
1893}
1894
1895////////////////////////////////////////////////////////////////////////////////
1896/// Drops the cluster two behind the current cluster and returns a fresh basket
1897/// by either reusing or creating a new one
1898
1900{
1901 TBasket *basket = 0;
1902
1903 auto CreateOrReuseBasket = [this, user_buffer]() -> TBasket* {
1904 TBasket *newbasket = nullptr;
1905 if (fExtraBasket) {
1906 newbasket = fExtraBasket;
1907 fExtraBasket = nullptr;
1908 } else {
1909 newbasket = fTree->CreateBasket(this);
1910 }
1911 if (user_buffer)
1912 newbasket->AdoptBuffer(user_buffer);
1913 return newbasket;
1914 };
1915
1916 // If GetClusterIterator is called with a negative entry then GetStartEntry will be 0
1917 // So we need to check if we reach the zero before we have gone back (1-VirtualSize) clusters
1918 // if this is the case, we want to keep everything in memory so we return a new basket
1920 if (iter.GetStartEntry() == 0) {
1921 return CreateOrReuseBasket();
1922 }
1923
1924 // Iterate backwards (1-VirtualSize) clusters to reach cluster to be unloaded from memory,
1925 // skipped if VirtualSize > 0.
1926 for (Int_t j = 0; j < -fTree->GetMaxVirtualSize(); j++) {
1927 if (iter.Previous() == 0) {
1928 return CreateOrReuseBasket();
1929 }
1930 }
1931
1932 Int_t entryToUnload = iter.Previous();
1933 // Finds the basket to unload from memory. Since the basket should be close to current
1934 // basket, just iterate backwards until the correct basket is reached. This should
1935 // be fast as long as the number of baskets per cluster is small
1936 Int_t basketToUnload = fReadBasket;
1937 while (fBasketEntry[basketToUnload] != entryToUnload) {
1938 basketToUnload--;
1939 if (basketToUnload < 0) {
1940 return CreateOrReuseBasket();
1941 }
1942 }
1943
1944 // Retrieves the basket that is going to be unloaded from memory. If the basket did not
1945 // exist, create a new one
1946 basket = (TBasket *)fBaskets.UncheckedAt(basketToUnload);
1947 if (basket) {
1948 fBaskets.AddAt(0, basketToUnload);
1949 --fNBaskets;
1950 } else {
1951 basket = CreateOrReuseBasket();
1952 }
1953 ++basketToUnload;
1954
1955 // Clear the rest of the baskets. While it would be ideal to reuse these baskets
1956 // for other baskets in the new cluster. It would require the function to go
1957 // beyond its current scope. In the ideal case when each cluster only has 1 basket
1958 // this will perform well
1959 iter.Next();
1960 while (fBasketEntry[basketToUnload] < iter.GetStartEntry()) {
1961 TBasket *oldbasket = (TBasket *)fBaskets.UncheckedAt(basketToUnload);
1962 if (oldbasket) {
1963 oldbasket->DropBuffers();
1964 delete oldbasket;
1965 fBaskets.AddAt(0, basketToUnload);
1966 --fNBaskets;
1967 }
1968 ++basketToUnload;
1969 }
1970 fBaskets.SetLast(-1);
1971 return basket;
1972}
1973
1974////////////////////////////////////////////////////////////////////////////////
1975/// Return the 'full' name of the branch. In particular prefix the mother's name
1976/// when it does not end in a trailing dot and thus is not part of the branch name
1978{
1979 TBranch* mother = GetMother();
1980 if (!mother || mother==this) {
1981 return fName;
1982 }
1983 TString motherName(mother->GetName());
1984 if (motherName.Length() && (motherName[motherName.Length()-1] == '.')) {
1985 return fName;
1986 }
1987 return motherName + "." + fName;
1988}
1989
1990////////////////////////////////////////////////////////////////////////////////
1991/// Return pointer to the 1st Leaf named name in thisBranch
1992
1993TLeaf* TBranch::GetLeaf(const char* name) const
1994{
1995 Int_t i;
1996 for (i=0;i<fNleaves;i++) {
1997 TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(i);
1998 if (!strcmp(leaf->GetName(),name)) return leaf;
1999 }
2000 return 0;
2001}
2002
2003////////////////////////////////////////////////////////////////////////////////
2004/// Get real file name
2005
2007{
2008 if (fFileName.Length()==0) {
2009 return fFileName;
2010 }
2011 TString bFileName = fFileName;
2012
2013 // check if branch file name is absolute or a URL (e.g. root://host/...)
2014 char *bname = gSystem->ExpandPathName(fFileName.Data());
2015 if (!gSystem->IsAbsoluteFileName(bname) && !strstr(bname, ":/") && fTree && fTree->GetCurrentFile()) {
2016
2017 // if not, get filename where tree header is stored
2018 const char *tfn = fTree->GetCurrentFile()->GetName();
2019
2020 // If it is an archive file we need a special treatment
2021 TUrl arc(tfn);
2022 if (strlen(arc.GetAnchor()) > 0) {
2024 bFileName = arc.GetUrl();
2025 } else {
2026 // if this is an absolute path or a URL then prepend this path
2027 // to the branch file name
2028 char *tname = gSystem->ExpandPathName(tfn);
2029 if (gSystem->IsAbsoluteFileName(tname) || strstr(tname, ":/")) {
2030 bFileName = gSystem->GetDirName(tname);
2031 bFileName += "/";
2032 bFileName += fFileName;
2033 }
2034 delete [] tname;
2035 }
2036 }
2037 delete [] bname;
2038
2039 return bFileName;
2040}
2041
2042////////////////////////////////////////////////////////////////////////////////
2043/// Return all elements of one row unpacked in internal array fValues
2044/// [Actually just returns 1 (?)]
2045
2047{
2048 return 1;
2049}
2050
2051////////////////////////////////////////////////////////////////////////////////
2052/// Return whether this branch is in a mode where the object are decomposed
2053/// or not (Also known as MakeClass mode).
2054
2056{
2057 // Regular TBranch and TBrancObject can not be in makeClass mode
2058
2059 return kFALSE;
2060}
2061
2062////////////////////////////////////////////////////////////////////////////////
2063/// Get our top-level parent branch in the tree.
2064
2066{
2067 if (fMother) return fMother;
2068
2069 {
2070 TBranch *parent = fParent;
2071 while(parent) {
2072 if (parent->fMother) {
2073 const_cast<TBranch*>(this)->fMother = parent->fMother; // We can not yet use the 'mutable' keyword
2074 return fMother;
2075 }
2076 if (!parent->fParent) {
2077 // This is the top node
2078 const_cast<TBranch*>(this)->fMother = parent; // We can not yet use the 'mutable' keyword
2079 return fMother;
2080 }
2081 parent = parent->fParent;
2082 }
2083 }
2084
2085 const TObjArray* array = fTree->GetListOfBranches();
2086 Int_t n = array->GetEntriesFast();
2087 for (Int_t i = 0; i < n; ++i) {
2088 TBranch* branch = (TBranch*) array->UncheckedAt(i);
2089 TBranch* parent = branch->GetSubBranch(this);
2090 if (parent) {
2091 const_cast<TBranch*>(this)->fMother = branch; // We can not yet use the 'mutable' keyword
2092 return branch;
2093 }
2094 }
2095 return 0;
2096}
2097
2098////////////////////////////////////////////////////////////////////////////////
2099/// Find the parent branch of child.
2100/// Return 0 if child is not in this branch hierarchy.
2101
2103{
2104 // Handle error condition, if the parameter is us, we cannot find the parent.
2105 if (this == child) {
2106 // Note: We cast away any const-ness of "this".
2107 return (TBranch*) this;
2108 }
2109
2110 if (child->fParent) {
2111 return child->fParent;
2112 }
2113
2115 for (Int_t i = 0; i < len; ++i) {
2116 TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
2117 if (!branch) {
2118 continue;
2119 }
2120 if (branch == child) {
2121 // We are the direct parent of child.
2122 // Note: We cast away any const-ness of "this".
2123 const_cast<TBranch*>(child)->fParent = (TBranch*)this; // We can not yet use the 'mutable' keyword
2124 return (TBranch*) this;
2125 }
2126 // FIXME: This is a tail-recursion!
2127 TBranch* parent = branch->GetSubBranch(child);
2128 if (parent) {
2129 return parent;
2130 }
2131 }
2132 // We failed to find the parent.
2133 return 0;
2134}
2135
2136////////////////////////////////////////////////////////////////////////////////
2137/// Return total number of bytes in the branch (including current buffer)
2138
2140{
2142 // This intentionally only store the TBranch part and thus slightly
2143 // under-estimate the space used.
2144 // Since the TBranchElement part contains pointers to other branches (branch count),
2145 // doing regular Streaming would end up including those and thus greatly over-estimate
2146 // the size used.
2147 const_cast<TBranch *>(this)->TBranch::Streamer(b);
2148
2149 Long64_t totbytes = 0;
2150 if (fZipBytes > 0) totbytes = fTotBytes;
2151 return totbytes + b.Length();
2152}
2153
2154////////////////////////////////////////////////////////////////////////////////
2155/// Return total number of bytes in the branch (excluding current buffer)
2156/// if option ="*" includes all sub-branches of this branch too
2157
2159{
2160 Long64_t totbytes = fTotBytes;
2161 if (!option) return totbytes;
2162 if (option[0] != '*') return totbytes;
2163 //scan sub-branches
2165 for (Int_t i = 0; i < len; ++i) {
2166 TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
2167 if (branch) totbytes += branch->GetTotBytes(option);
2168 }
2169 return totbytes;
2170}
2171
2172////////////////////////////////////////////////////////////////////////////////
2173/// Return total number of zip bytes in the branch
2174/// if option ="*" includes all sub-branches of this branch too
2175
2177{
2178 Long64_t zipbytes = fZipBytes;
2179 if (!option) return zipbytes;
2180 if (option[0] != '*') return zipbytes;
2181 //scan sub-branches
2183 for (Int_t i = 0; i < len; ++i) {
2184 TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
2185 if (branch) zipbytes += branch->GetZipBytes(option);
2186 }
2187 return zipbytes;
2188}
2189
2190////////////////////////////////////////////////////////////////////////////////
2191/// Returns the IO settings currently in use for this branch.
2192
2194{
2195 return fIOFeatures;
2196}
2197
2198////////////////////////////////////////////////////////////////////////////////
2199/// Return kTRUE if an existing object in a TBranchObject must be deleted.
2200
2202{
2203 return TestBit(kAutoDelete);
2204}
2205
2206////////////////////////////////////////////////////////////////////////////////
2207/// Return kTRUE if more than one leaf or browsables, kFALSE otherwise.
2208
2210{
2211 if (fNleaves > 1) {
2212 return kTRUE;
2213 }
2214 TList* browsables = const_cast<TBranch*>(this)->GetBrowsables();
2215 return browsables && browsables->GetSize();
2216}
2217
2218////////////////////////////////////////////////////////////////////////////////
2219/// keep a maximum of fMaxEntries in memory
2220
2222{
2223 Int_t dentries = (Int_t) (fEntries - maxEntries);
2224 TBasket* basket = (TBasket*) fBaskets.UncheckedAt(0);
2225 if (basket) basket->MoveEntries(dentries);
2226 fEntries = maxEntries;
2227 fEntryNumber = maxEntries;
2228 //loop on sub branches
2230 for (Int_t i = 0; i < nb; ++i) {
2231 TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
2232 branch->KeepCircular(maxEntries);
2233 }
2234}
2235
2236////////////////////////////////////////////////////////////////////////////////
2237/// Baskets associated to this branch are forced to be in memory.
2238/// You can call TTree::SetMaxVirtualSize(maxmemory) to instruct
2239/// the system that the total size of the imported baskets does not
2240/// exceed maxmemory bytes.
2241///
2242/// The function returns the number of baskets that have been put in memory.
2243/// This method may be called to force all baskets of one or more branches
2244/// in memory when random access to entries in this branch is required.
2245/// See also TTree::LoadBaskets to load all baskets of all branches in memory.
2246
2248{
2249 Int_t nimported = 0;
2250 Int_t nbaskets = fWriteBasket;
2251 TFile *file = GetFile(0);
2252 if (!file) return 0;
2253 TBasket *basket;
2254 for (Int_t i=0;i<nbaskets;i++) {
2255 basket = (TBasket*)fBaskets.UncheckedAt(i);
2256 if (basket) continue;
2257 basket = GetFreshBasket(i, nullptr);
2258 if (fBasketBytes[i] == 0) {
2260 }
2261 Int_t badread = basket->ReadBasketBuffers(fBasketSeek[i],fBasketBytes[i],file);
2262 if (badread) {
2263 Error("Loadbaskets","Error while reading basket buffer %d of branch %s",i,GetName());
2264 return -1;
2265 }
2266 ++fNBaskets;
2267 fBaskets.AddAt(basket,i);
2268 nimported++;
2269 }
2270 return nimported;
2271}
2272
2273////////////////////////////////////////////////////////////////////////////////
2274/// Print TBranch parameters
2275///
2276/// If options contains "basketsInfo" print the entry number, location and size
2277/// of each baskets.
2278
2279void TBranch::Print(Option_t *option) const
2280{
2281 const int kLINEND = 77;
2282 Float_t cx = 1;
2283
2284 TString titleContent(GetTitle());
2285 if ( titleContent == GetName() ) {
2286 titleContent.Clear();
2287 }
2288
2289 if (fLeaves.GetEntries() == 1) {
2290 if (titleContent.Length()>=2 && titleContent[titleContent.Length()-2]=='/' && isalpha(titleContent[titleContent.Length()-1])) {
2291 // The type is already encoded. Nothing to do.
2292 } else {
2293 TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(0);
2294 if (titleContent.Length()) {
2295 titleContent.Prepend(" ");
2296 }
2297 // titleContent.Append("type: ");
2298 titleContent.Prepend(leaf->GetTypeName());
2299 }
2300 }
2301 Int_t titleLength = titleContent.Length();
2302
2303 Int_t aLength = titleLength + strlen(GetName());
2304 aLength += (aLength / 54 + 1) * 80 + 100;
2305 if (aLength < 200) aLength = 200;
2306 char *bline = new char[aLength];
2307
2308 Long64_t totBytes = GetTotalSize();
2309 if (fZipBytes) cx = (fTotBytes+0.00001)/fZipBytes;
2310 if (titleLength) snprintf(bline,aLength,"*Br%5d :%-9s : %-54s *",fgCount,GetName(),titleContent.Data());
2311 else snprintf(bline,aLength,"*Br%5d :%-9s : %-54s *",fgCount,GetName()," ");
2312 if (strlen(bline) > UInt_t(kLINEND)) {
2313 char *tmp = new char[strlen(bline)+1];
2314 if (titleLength) strlcpy(tmp, titleContent.Data(),strlen(bline)+1);
2315 snprintf(bline,aLength,"*Br%5d :%-9s : ",fgCount,GetName());
2316 int pos = strlen (bline);
2317 int npos = pos;
2318 int beg=0, end;
2319 while (beg < titleLength) {
2320 for (end=beg+1; end < titleLength-1; end ++)
2321 if (tmp[end] == ':') break;
2322 if (npos + end-beg+1 >= 78) {
2323 while (npos < kLINEND) {
2324 bline[pos ++] = ' ';
2325 npos ++;
2326 }
2327 bline[pos ++] = '*';
2328 bline[pos ++] = '\n';
2329 bline[pos ++] = '*';
2330 npos = 1;
2331 for (; npos < 12; npos ++)
2332 bline[pos ++] = ' ';
2333 bline[pos-2] = '|';
2334 }
2335 for (int n = beg; n <= end; n ++)
2336 bline[pos+n-beg] = tmp[n];
2337 pos += end-beg+1;
2338 npos += end-beg+1;
2339 beg = end+1;
2340 }
2341 while (npos < kLINEND) {
2342 bline[pos ++] = ' ';
2343 npos ++;
2344 }
2345 bline[pos ++] = '*';
2346 bline[pos] = '\0';
2347 delete[] tmp;
2348 }
2349 Printf("%s", bline);
2350
2351 if (fTotBytes > 2000000000) {
2352 Printf("*Entries :%lld : Total Size=%11lld bytes File Size = %lld *",fEntries,totBytes,fZipBytes);
2353 } else {
2354 if (fZipBytes > 0) {
2355 Printf("*Entries :%9lld : Total Size=%11lld bytes File Size = %10lld *",fEntries,totBytes,fZipBytes);
2356 } else {
2357 if (fWriteBasket > 0) {
2358 Printf("*Entries :%9lld : Total Size=%11lld bytes All baskets in memory *",fEntries,totBytes);
2359 } else {
2360 Printf("*Entries :%9lld : Total Size=%11lld bytes One basket in memory *",fEntries,totBytes);
2361 }
2362 }
2363 }
2364 Printf("*Baskets :%9d : Basket Size=%11d bytes Compression= %6.2f *",fWriteBasket,fBasketSize,cx);
2365
2366 if (strncmp(option,"basketsInfo",strlen("basketsInfo"))==0) {
2367 Int_t nbaskets = fWriteBasket;
2368 for (Int_t i=0;i<nbaskets;i++) {
2369 Printf("*Basket #%4d entry=%6lld pos=%6lld size=%5d",
2370 i, fBasketEntry[i], fBasketSeek[i], fBasketBytes[i]);
2371 }
2372 }
2373
2374 Printf("*............................................................................*");
2375 delete [] bline;
2376 fgCount++;
2377}
2378
2379////////////////////////////////////////////////////////////////////////////////
2380/// Print the information we have about which basket is currently cached and
2381/// whether they have been 'used'/'read' from the cache.
2382
2384{
2386}
2387
2388////////////////////////////////////////////////////////////////////////////////
2389/// Loop on all leaves of this branch to read Basket buffer.
2390
2392{
2393 // fLeaves->ReadBasket(basket);
2394}
2395
2396////////////////////////////////////////////////////////////////////////////////
2397/// Loop on all leaves of this branch to read Basket buffer.
2398
2400{
2401 for (Int_t i = 0; i < fNleaves; ++i) {
2402 TLeaf* leaf = (TLeaf*) fLeaves.UncheckedAt(i);
2403 leaf->ReadBasket(b);
2404 }
2405}
2406
2407////////////////////////////////////////////////////////////////////////////////
2408/// Read zero leaves without the overhead of a loop.
2409
2411{
2412}
2413
2414////////////////////////////////////////////////////////////////////////////////
2415/// Read one leaf without the overhead of a loop.
2416
2418{
2420}
2421
2422////////////////////////////////////////////////////////////////////////////////
2423/// Read two leaves without the overhead of a loop.
2424
2426{
2429}
2430
2431////////////////////////////////////////////////////////////////////////////////
2432/// Loop on all leaves of this branch to fill Basket buffer.
2433
2435{
2436 for (Int_t i = 0; i < fNleaves; ++i) {
2437 TLeaf* leaf = (TLeaf*) fLeaves.UncheckedAt(i);
2438 leaf->FillBasket(b);
2439 }
2440}
2441
2442////////////////////////////////////////////////////////////////////////////////
2443/// Refresh this branch using new information in b
2444/// This function is called by TTree::Refresh
2445
2447{
2448 if (b==0) return;
2449
2450 fEntryOffsetLen = b->fEntryOffsetLen;
2451 fWriteBasket = b->fWriteBasket;
2452 fEntryNumber = b->fEntryNumber;
2453 fMaxBaskets = b->fMaxBaskets;
2454 fEntries = b->fEntries;
2455 fTotBytes = b->fTotBytes;
2456 fZipBytes = b->fZipBytes;
2457 fReadBasket = 0;
2458 fReadEntry = -1;
2459 fFirstBasketEntry = -1;
2460 fNextBasketEntry = -1;
2461 fCurrentBasket = 0;
2462 delete [] fBasketBytes;
2463 delete [] fBasketEntry;
2464 delete [] fBasketSeek;
2468 Int_t i;
2469 for (i=0;i<fMaxBaskets;i++) {
2470 fBasketBytes[i] = b->fBasketBytes[i];
2471 fBasketEntry[i] = b->fBasketEntry[i];
2472 fBasketSeek[i] = b->fBasketSeek[i];
2473 }
2474 fBaskets.Delete();
2475 Int_t nbaskets = b->fBaskets.GetSize();
2476 fBaskets.Expand(nbaskets);
2477 // If the current fWritebasket is in memory, take it (just swap)
2478 // from the Tree being read
2479 TBasket *basket = (TBasket*)b->fBaskets.UncheckedAt(fWriteBasket);
2480 fBaskets.AddAt(basket,fWriteBasket);
2481 if (basket) {
2482 fNBaskets = 1;
2483 --(b->fNBaskets);
2484 b->fBaskets.RemoveAt(fWriteBasket);
2485 basket->SetBranch(this);
2486 }
2487}
2488
2489////////////////////////////////////////////////////////////////////////////////
2490/// Reset a Branch.
2491///
2492/// - Existing buffers are deleted.
2493/// - Entries, max and min are reset.
2494
2496{
2497 fReadBasket = 0;
2498 fReadEntry = -1;
2499 fFirstBasketEntry = -1;
2500 fNextBasketEntry = -1;
2501 fCurrentBasket = 0;
2502 fWriteBasket = 0;
2503 fEntries = 0;
2504 fTotBytes = 0;
2505 fZipBytes = 0;
2506 fEntryNumber = 0;
2507
2508 if (fBasketBytes) {
2509 for (Int_t i = 0; i < fMaxBaskets; ++i) {
2510 fBasketBytes[i] = 0;
2511 }
2512 }
2513
2514 if (fBasketEntry) {
2515 for (Int_t i = 0; i < fMaxBaskets; ++i) {
2516 fBasketEntry[i] = 0;
2517 }
2518 }
2519
2520 if (fBasketSeek) {
2521 for (Int_t i = 0; i < fMaxBaskets; ++i) {
2522 fBasketSeek[i] = 0;
2523 }
2524 }
2525
2526 fBaskets.Delete();
2527 fNBaskets = 0;
2528}
2529
2530////////////////////////////////////////////////////////////////////////////////
2531/// Reset a Branch.
2532///
2533/// - Existing buffers are deleted.
2534/// - Entries, max and min are reset.
2535
2537{
2538 fReadBasket = 0;
2539 fReadEntry = -1;
2540 fFirstBasketEntry = -1;
2541 fNextBasketEntry = -1;
2542 fCurrentBasket = 0;
2543 fWriteBasket = 0;
2544 fEntries = 0;
2545 fTotBytes = 0;
2546 fZipBytes = 0;
2547 fEntryNumber = 0;
2548
2549 if (fBasketBytes) {
2550 for (Int_t i = 0; i < fMaxBaskets; ++i) {
2551 fBasketBytes[i] = 0;
2552 }
2553 }
2554
2555 if (fBasketEntry) {
2556 for (Int_t i = 0; i < fMaxBaskets; ++i) {
2557 fBasketEntry[i] = 0;
2558 }
2559 }
2560
2561 if (fBasketSeek) {
2562 for (Int_t i = 0; i < fMaxBaskets; ++i) {
2563 fBasketSeek[i] = 0;
2564 }
2565 }
2566
2567 TBasket *reusebasket = (TBasket*)fBaskets[fWriteBasket];
2568 if (reusebasket) {
2570 } else {
2571 reusebasket = (TBasket*)fBaskets[fReadBasket];
2572 if (reusebasket) {
2573 fBaskets[fReadBasket] = 0;
2574 }
2575 }
2576 fBaskets.Delete();
2577 if (reusebasket) {
2578 fNBaskets = 1;
2579 reusebasket->WriteReset();
2580 fBaskets[0] = reusebasket;
2581 } else {
2582 fNBaskets = 0;
2583 }
2584}
2585
2586////////////////////////////////////////////////////////////////////////////////
2587/// Reset the address of the branch.
2588
2590{
2591 fAddress = 0;
2592
2593 // Reset last read entry number, we have will had new user object now.
2594 fReadEntry = -1;
2595
2596 for (Int_t i = 0; i < fNleaves; ++i) {
2597 TLeaf* leaf = (TLeaf*) fLeaves.UncheckedAt(i);
2598 leaf->SetAddress(0);
2599 }
2600
2601 Int_t nbranches = fBranches.GetEntriesFast();
2602 for (Int_t i = 0; i < nbranches; ++i) {
2603 TBranch* abranch = (TBranch*) fBranches[i];
2604 // FIXME: This is a tail recursion.
2605 abranch->ResetAddress();
2606 }
2607}
2608
2609////////////////////////////////////////////////////////////////////////////////
2610/// Static function resetting fgCount
2611
2613{
2614 fgCount = 0;
2615}
2616
2617////////////////////////////////////////////////////////////////////////////////
2618/// Set address of this branch.
2619
2620void TBranch::SetAddress(void* addr)
2621{
2622 if (TestBit(kDoNotProcess)) {
2623 return;
2624 }
2625 fReadEntry = -1;
2626 fFirstBasketEntry = -1;
2627 fNextBasketEntry = -1;
2628 fAddress = (char*) addr;
2629 for (Int_t i = 0; i < fNleaves; ++i) {
2630 TLeaf* leaf = (TLeaf*) fLeaves.UncheckedAt(i);
2631 Int_t offset = leaf->GetOffset();
2632 if (TestBit(kIsClone)) {
2633 offset = 0;
2634 }
2635 if (fAddress) leaf->SetAddress(fAddress + offset);
2636 else leaf->SetAddress(0);
2637 }
2638}
2639
2640////////////////////////////////////////////////////////////////////////////////
2641/// Set the automatic delete bit.
2642///
2643/// This bit is used by TBranchObject::ReadBasket to decide if an object
2644/// referenced by a TBranchObject must be deleted or not before reading
2645/// a new entry.
2646///
2647/// If autodel is kTRUE, this existing object will be deleted, a new object
2648/// created by the default constructor, then read from disk by the streamer.
2649///
2650/// If autodel is kFALSE, the existing object is not deleted. Root assumes
2651/// that the user is taking care of deleting any internal object or array
2652/// (this can be done in the streamer).
2653
2655{
2656 if (autodel) {
2657 SetBit(kAutoDelete, 1);
2658 } else {
2659 SetBit(kAutoDelete, 0);
2660 }
2661}
2662
2663////////////////////////////////////////////////////////////////////////////////
2664/// Set the basket size
2665/// The function makes sure that the basket size is greater than fEntryOffsetlen
2666
2668{
2669 Int_t minsize = 100 + fName.Length();
2670 if (buffsize < minsize+fEntryOffsetLen) buffsize = minsize+fEntryOffsetLen;
2671 fBasketSize = buffsize;
2672 TBasket *basket = (TBasket*)fBaskets[fWriteBasket];
2673 if (basket) {
2674 basket->AdjustSize(fBasketSize);
2675 }
2676}
2677
2678////////////////////////////////////////////////////////////////////////////////
2679/// Set address of this branch directly from a TBuffer to avoid streaming.
2680///
2681/// Note: We do not take ownership of the buffer.
2682
2684{
2685 // Check this is possible
2686 if ( (fNleaves != 1)
2687 || (strcmp("TLeafObject",fLeaves.UncheckedAt(0)->ClassName())!=0) ) {
2688 Error("TBranch::SetAddress","Filling from a TBuffer can only be done with a not split object branch. Request ignored.");
2689 } else {
2690 fReadEntry = -1;
2691 fNextBasketEntry = -1;
2692 fFirstBasketEntry = -1;
2693 // Note: We do not take ownership of the buffer.
2694 fEntryBuffer = buf;
2695 }
2696}
2697
2698////////////////////////////////////////////////////////////////////////////////
2699/// Set compression algorithm.
2700
2702{
2703 if (algorithm < 0 || algorithm >= ROOT::RCompressionSetting::EAlgorithm::kUndefined) algorithm = 0;
2704 if (fCompress < 0) {
2706 } else {
2707 int level = fCompress % 100;
2708 fCompress = 100 * algorithm + level;
2709 }
2710
2712 for (Int_t i=0;i<nb;i++) {
2713 TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
2714 branch->SetCompressionAlgorithm(algorithm);
2715 }
2716}
2717
2718////////////////////////////////////////////////////////////////////////////////
2719/// Set compression level.
2720
2722{
2723 if (level < 0) level = 0;
2724 if (level > 99) level = 99;
2725 if (fCompress < 0) {
2726 fCompress = level;
2727 } else {
2728 int algorithm = fCompress / 100;
2729 if (algorithm >= ROOT::RCompressionSetting::EAlgorithm::kUndefined) algorithm = 0;
2730 fCompress = 100 * algorithm + level;
2731 }
2732
2734 for (Int_t i=0;i<nb;i++) {
2735 TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
2736 branch->SetCompressionLevel(level);
2737 }
2738}
2739
2740////////////////////////////////////////////////////////////////////////////////
2741/// Set compression settings.
2742
2744{
2745 fCompress = settings;
2746
2748 for (Int_t i=0;i<nb;i++) {
2749 TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
2750 branch->SetCompressionSettings(settings);
2751 }
2752}
2753
2754////////////////////////////////////////////////////////////////////////////////
2755/// Update the default value for the branch's fEntryOffsetLen if and only if
2756/// it was already non zero (and the new value is not zero)
2757/// If updateExisting is true, also update all the existing branches.
2758
2759void TBranch::SetEntryOffsetLen(Int_t newdefault, Bool_t updateExisting)
2760{
2761 if (fEntryOffsetLen && newdefault) {
2762 fEntryOffsetLen = newdefault;
2763 }
2764 if (updateExisting) {
2765 TIter next( GetListOfBranches() );
2766 TBranch *b;
2767 while ( ( b = (TBranch*)next() ) ) {
2768 b->SetEntryOffsetLen( newdefault, kTRUE );
2769 }
2770 }
2771}
2772
2773////////////////////////////////////////////////////////////////////////////////
2774/// Set the number of entries in this branch.
2775
2777{
2778 fEntries = entries;
2779 fEntryNumber = entries;
2780}
2781
2782////////////////////////////////////////////////////////////////////////////////
2783/// Set file where this branch writes/reads its buffers.
2784/// By default the branch buffers reside in the file where the
2785/// Tree was created.
2786/// If the file name where the tree was created is an absolute
2787/// path name or an URL (e.g. or root://host/...)
2788/// and if the fname is not an absolute path name or an URL then
2789/// the path of the tree file is prepended to fname to make the
2790/// branch file relative to the tree file. In this case one can
2791/// move the tree + all branch files to a different location in
2792/// the file system and still access the branch files.
2793/// The ROOT file will be connected only when necessary.
2794/// If called by TBranch::Fill (via TBasket::WriteFile), the file
2795/// will be created with the option "recreate".
2796/// If called by TBranch::GetEntry (via TBranch::GetBasket), the file
2797/// will be opened in read mode.
2798/// To open a file in "update" mode or with a certain compression
2799/// level, use TBranch::SetFile(TFile *file).
2800
2802{
2803 if (file == 0) file = fTree->GetCurrentFile();
2805 if (file == fTree->GetCurrentFile()) fFileName = "";
2806 else fFileName = file->GetName();
2807
2808 if (file && fCompress == -1) {
2809 fCompress = file->GetCompressionLevel();
2810 }
2811
2812 // Apply to all existing baskets.
2813 TIter nextb(GetListOfBaskets());
2814 TBasket *basket;
2815 while ((basket = (TBasket*)nextb())) {
2816 basket->SetParent(file);
2817 }
2818
2819 // Apply to sub-branches as well.
2820 TIter next(GetListOfBranches());
2821 TBranch *branch;
2822 while ((branch = (TBranch*)next())) {
2823 branch->SetFile(file);
2824 }
2825}
2826
2827////////////////////////////////////////////////////////////////////////////////
2828/// Set file where this branch writes/reads its buffers.
2829/// By default the branch buffers reside in the file where the
2830/// Tree was created.
2831/// If the file name where the tree was created is an absolute
2832/// path name or an URL (e.g. root://host/...)
2833/// and if the fname is not an absolute path name or an URL then
2834/// the path of the tree file is prepended to fname to make the
2835/// branch file relative to the tree file. In this case one can
2836/// move the tree + all branch files to a different location in
2837/// the file system and still access the branch files.
2838/// The ROOT file will be connected only when necessary.
2839/// If called by TBranch::Fill (via TBasket::WriteFile), the file
2840/// will be created with the option "recreate".
2841/// If called by TBranch::GetEntry (via TBranch::GetBasket), the file
2842/// will be opened in read mode.
2843/// To open a file in "update" mode or with a certain compression
2844/// level, use TBranch::SetFile(TFile *file).
2845
2846void TBranch::SetFile(const char* fname)
2847{
2848 fFileName = fname;
2849 fDirectory = 0;
2850
2851 //apply to sub-branches as well
2852 TIter next(GetListOfBranches());
2853 TBranch *branch;
2854 while ((branch = (TBranch*)next())) {
2855 branch->SetFile(fname);
2856 }
2857}
2858
2859////////////////////////////////////////////////////////////////////////////////
2860/// Set the branch in a mode where the object are decomposed
2861/// (Also known as MakeClass mode).
2862/// Return whether the setting was possible (it is not possible for
2863/// TBranch and TBranchObject).
2864
2866{
2867 // Regular TBranch and TBrancObject can not be in makeClass mode
2868 return kFALSE;
2869}
2870
2871////////////////////////////////////////////////////////////////////////////////
2872/// Set object this branch is pointing to.
2873
2874void TBranch::SetObject(void * /* obj */)
2875{
2876 if (TestBit(kDoNotProcess)) {
2877 return;
2878 }
2879 Warning("SetObject","is not supported in TBranch objects");
2880}
2881
2882////////////////////////////////////////////////////////////////////////////////
2883/// Set branch status to Process or DoNotProcess.
2884
2886{
2887 if (status) ResetBit(kDoNotProcess);
2888 else SetBit(kDoNotProcess);
2889}
2890
2891////////////////////////////////////////////////////////////////////////////////
2892/// Stream a class object
2893
2894void TBranch::Streamer(TBuffer& b)
2895{
2896 if (b.IsReading()) {
2897 UInt_t R__s, R__c;
2898 fTree = 0; // Will be set by TTree::Streamer
2899 fAddress = 0;
2900 gROOT->SetReadingObject(kTRUE);
2901
2902 // Reset transients.
2904 fCurrentBasket = 0;
2905 fFirstBasketEntry = -1;
2906 fNextBasketEntry = -1;
2907
2908 Version_t v = b.ReadVersion(&R__s, &R__c);
2909 if (v > 9) {
2910 b.ReadClassBuffer(TBranch::Class(), this, v, R__s, R__c);
2911
2912 if (fWriteBasket>=fBaskets.GetSize()) {
2914 }
2915 fDirectory = 0;
2917 for (Int_t i=0;i<fNleaves;i++) {
2918 TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(i);
2919 leaf->SetBranch(this);
2920 }
2921 auto nbranches = fBranches.GetEntriesFast();
2922 for (Int_t i=0;i<nbranches;i++) {
2924 br->fParent = this;
2925 }
2926
2927 fNBaskets = 0;
2928 for (Int_t j = fWriteBasket; j>=0; --j) {
2930 if (bk) {
2931 bk->SetBranch(this);
2932 // GetTree()->IncrementTotalBuffers(bk->GetBufferSize());
2933 ++fNBaskets;
2934 }
2935 }
2936 if (fWriteBasket >= fMaxBaskets) {
2937 //old versions may need this fix
2942
2943 }
2945 gROOT->SetReadingObject(kFALSE);
2946 if (IsA() == TBranch::Class()) {
2947 if (fNleaves == 0) {
2949 } else if (fNleaves == 1) {
2951 } else if (fNleaves == 2) {
2953 } else {
2955 }
2956 }
2957 return;
2958 }
2959 //====process old versions before automatic schema evolution
2960 Int_t n,i,j,ijunk;
2961 if (v > 5) {
2962 Stat_t djunk;
2963 TNamed::Streamer(b);
2964 if (v > 7) TAttFill::Streamer(b);
2965 b >> fCompress;
2966 b >> fBasketSize;
2967 b >> fEntryOffsetLen;
2968 b >> fWriteBasket;
2969 b >> ijunk; fEntryNumber = (Long64_t)ijunk;
2970 b >> fOffset;
2971 b >> fMaxBaskets;
2972 if (v > 6) b >> fSplitLevel;
2973 b >> djunk; fEntries = (Long64_t)djunk;
2974 b >> djunk; fTotBytes = (Long64_t)djunk;
2975 b >> djunk; fZipBytes = (Long64_t)djunk;
2976
2977 fBranches.Streamer(b);
2978 fLeaves.Streamer(b);
2979 fBaskets.Streamer(b);
2983 Char_t isArray;
2984 b >> isArray;
2985 b.ReadFastArray(fBasketBytes,fMaxBaskets);
2986 b >> isArray;
2987 for (i=0;i<fMaxBaskets;i++) {b >> ijunk; fBasketEntry[i] = ijunk;}
2988 b >> isArray;
2989 for (i=0;i<fMaxBaskets;i++) {
2990 if (isArray == 2) b >> fBasketSeek[i];
2991 else {Int_t bsize; b >> bsize; fBasketSeek[i] = (Long64_t)bsize;};
2992 }
2993 fFileName.Streamer(b);
2994 b.CheckByteCount(R__s, R__c, TBranch::IsA());
2995 fDirectory = 0;
2997 for (i=0;i<fNleaves;i++) {
2998 TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(i);
2999 leaf->SetBranch(this);
3000 }
3001 fNBaskets = 0;
3002 for (j = fWriteBasket; j >= 0; --j) {
3004 if (bk) {
3005 bk->SetBranch(this);
3006 //GetTree()->IncrementTotalBuffers(bk->GetBufferSize());
3007 ++fNBaskets;
3008 }
3009 }
3010 if (fWriteBasket >= fMaxBaskets) {
3011 //old versions may need this fix
3016
3017 }
3018 // Check Byte Count is not needed since it was done in ReadBuffer
3020 gROOT->SetReadingObject(kFALSE);
3021 b.CheckByteCount(R__s, R__c, TBranch::IsA());
3022 if (IsA() == TBranch::Class()) {
3023 if (fNleaves == 0) {
3025 } else if (fNleaves == 1) {
3027 } else if (fNleaves == 2) {
3029 } else {
3031 }
3032 }
3033 return;
3034 }
3035 //====process very old versions
3036 Stat_t djunk;
3037 TNamed::Streamer(b);
3038 b >> fCompress;
3039 b >> fBasketSize;
3040 b >> fEntryOffsetLen;
3041 b >> fMaxBaskets;
3042 b >> fWriteBasket;
3043 b >> ijunk; fEntryNumber = (Long64_t)ijunk;
3044 b >> djunk; fEntries = (Long64_t)djunk;
3045 b >> djunk; fTotBytes = (Long64_t)djunk;
3046 b >> djunk; fZipBytes = (Long64_t)djunk;
3047 b >> fOffset;
3048 fBranches.Streamer(b);
3049 fLeaves.Streamer(b);
3051 for (i=0;i<fNleaves;i++) {
3052 TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(i);
3053 leaf->SetBranch(this);
3054 }
3055 fBaskets.Streamer(b);
3056 for (j = fWriteBasket; j > 0; --j) {
3058 if (bk) {
3059 bk->SetBranch(this);
3060 //GetTree()->IncrementTotalBuffers(bk->GetBufferSize());
3061 }
3062 }
3064 b >> n;
3065 for (i=0;i<n;i++) {b >> ijunk; fBasketEntry[i] = ijunk;}
3067 if (v > 4) {
3068 n = b.ReadArray(fBasketBytes);
3069 } else {
3070 for (n=0;n<fMaxBaskets;n++) fBasketBytes[n] = 0;
3071 }
3072 if (v < 2) {
3074 for (n=0;n<fWriteBasket;n++) {
3075 TBasket *basket = GetBasketImpl(n, nullptr);
3076 fBasketSeek[n] = basket ? basket->GetSeekKey() : 0;
3077 }
3078 } else {
3080 b >> n;
3081 for (n=0;n<fMaxBaskets;n++) {
3082 Int_t aseek;
3083 b >> aseek;
3084 fBasketSeek[n] = Long64_t(aseek);
3085 }
3086 }
3087 if (v > 2) {
3088 fFileName.Streamer(b);
3089 }
3090 fDirectory = 0;
3091 if (v < 4) SetAutoDelete(kTRUE);
3093 gROOT->SetReadingObject(kFALSE);
3094 b.CheckByteCount(R__s, R__c, TBranch::IsA());
3095 //====end of old versions
3096 if (IsA() == TBranch::Class()) {
3097 if (fNleaves == 0) {
3099 } else if (fNleaves == 1) {
3101 } else if (fNleaves == 2) {
3103 } else {
3105 }
3106 }
3107 } else {
3108 Int_t maxBaskets = fMaxBaskets;
3110 Int_t lastBasket = fMaxBaskets;
3111 if (fMaxBaskets < 10) fMaxBaskets = 10;
3112
3113 TBasket **stash = new TBasket *[lastBasket];
3114 for (Int_t i = 0; i < lastBasket; ++i) {
3115 TBasket *ba = (TBasket *)fBaskets.UncheckedAt(i);
3116 if (ba && (fBasketBytes[i] || ba->GetNevBuf()==0)) {
3117 // Already on disk or empty.
3118 stash[i] = ba;
3119 fBaskets[i] = nullptr;
3120 } else {
3121 stash[i] = nullptr;
3122 }
3123 }
3124
3125 b.WriteClassBuffer(TBranch::Class(), this);
3126
3127 for (Int_t i = 0; i < lastBasket; ++i) {
3128 if (stash[i]) fBaskets[i] = stash[i];
3129 }
3130
3131 delete[] stash;
3132 fMaxBaskets = maxBaskets;
3133 }
3134}
3135
3136////////////////////////////////////////////////////////////////////////////////
3137/// Write the current basket to disk and return the number of bytes
3138/// written to the file.
3139
3141{
3142 Int_t nevbuf = basket->GetNevBuf();
3143 if (fEntryOffsetLen > 10 && (4*nevbuf) < fEntryOffsetLen ) {
3144 // Make sure that the fEntryOffset array does not stay large unnecessarily.
3145 fEntryOffsetLen = nevbuf < 3 ? 10 : 4*nevbuf; // assume some fluctuations.
3146 } else if (fEntryOffsetLen && nevbuf > fEntryOffsetLen) {
3147 // Increase the array ...
3148 fEntryOffsetLen = 2*nevbuf; // assume some fluctuations.
3149 }
3150
3151 // Note: captures `basket`, `where`, and `this` by value; modifies the TBranch and basket,
3152 // as we make a copy of the pointer. We cannot capture `basket` by reference as the pointer
3153 // itself might be modified after `WriteBasketImpl` exits.
3154 auto doUpdates = [=]() {
3155 Int_t nout = basket->WriteBuffer(); // Write buffer
3156 if (nout < 0)
3157 Error("WriteBasketImpl", "basket's WriteBuffer failed.");
3158 fBasketBytes[where] = basket->GetNbytes();
3159 fBasketSeek[where] = basket->GetSeekKey();
3160 Int_t addbytes = basket->GetObjlen() + basket->GetKeylen();
3161 TBasket *reusebasket = 0;
3162 if (nout>0) {
3163 // The Basket was written so we can now safely reuse it.
3164 fBaskets[where] = 0;
3165
3166 reusebasket = basket;
3167 reusebasket->WriteReset();
3168
3169 fZipBytes += nout;
3170 fTotBytes += addbytes;
3171 fTree->AddTotBytes(addbytes);
3172 fTree->AddZipBytes(nout);
3173#ifdef R__TRACK_BASKET_ALLOC_TIME
3174 fTree->AddAllocationTime(reusebasket->GetResetAllocationTime());
3175#endif
3177 }
3178
3179 if (where==fWriteBasket) {
3180 ++fWriteBasket;
3181 if (fWriteBasket >= fMaxBaskets) {
3183 }
3184 if (reusebasket && reusebasket == fCurrentBasket) {
3185 // The 'current' basket has Reset, so if we need it we will need
3186 // to reload it.
3187 fCurrentBasket = 0;
3188 fFirstBasketEntry = -1;
3189 fNextBasketEntry = -1;
3190 }
3193 } else {
3194 --fNBaskets;
3195 fBaskets[where] = 0;
3196 basket->DropBuffers();
3197 if (basket == fCurrentBasket) {
3198 fCurrentBasket = 0;
3199 fFirstBasketEntry = -1;
3200 fNextBasketEntry = -1;
3201 }
3202 delete basket;
3203 }
3204 return nout;
3205 };
3206 if (imtHelper) {
3207 imtHelper->Run(doUpdates);
3208 return 0;
3209 } else {
3210 return doUpdates();
3211 }
3212}
3213
3214////////////////////////////////////////////////////////////////////////////////
3215///set the first entry number (case of TBranchSTL)
3216
3218{
3219 fFirstEntry = entry;
3220 fEntries = 0;
3221 fEntryNumber = entry;
3222 if( fBasketEntry )
3223 fBasketEntry[0] = entry;
3224 for( Int_t i = 0; i < fBranches.GetEntriesFast(); ++i )
3225 ((TBranch*)fBranches[i])->SetFirstEntry( entry );
3226}
3227
3228////////////////////////////////////////////////////////////////////////////////
3229/// If the branch address is not set, we set all addresses starting with
3230/// the top level parent branch.
3231
3233{
3234 SetAddress(nullptr); // in some cases, this triggers setting of the address
3235}
3236
3237////////////////////////////////////////////////////////////////////////////////
3238/// Refresh the value of fDirectory (i.e. where this branch writes/reads its buffers)
3239/// with the current value of fTree->GetCurrentFile unless this branch has been
3240/// redirected to a different file. Also update the sub-branches.
3241
3243{
3245 if (fFileName.Length() == 0) {
3246 fDirectory = file;
3247
3248 // Apply to all existing baskets.
3249 TIter nextb(GetListOfBaskets());
3250 TBasket *basket;
3251 while ((basket = (TBasket*)nextb())) {
3252 basket->SetParent(file);
3253 }
3254 }
3255
3256 // Apply to sub-branches as well.
3257 TIter next(GetListOfBranches());
3258 TBranch *branch;
3259 while ((branch = (TBranch*)next())) {
3260 branch->UpdateFile();
3261 }
3262}
void tobuf(char *&buf, Bool_t x)
Definition Bytes.h:55
#define R__likely(expr)
Definition RConfig.hxx:599
#define R__unlikely(expr)
Definition RConfig.hxx:598
#define b(i)
Definition RSha256.hxx:100
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
const Ssiz_t kNPOS
Definition RtypesCore.h:124
int Int_t
Definition RtypesCore.h:45
short Version_t
Definition RtypesCore.h:65
char Char_t
Definition RtypesCore.h:37
unsigned int UInt_t
Definition RtypesCore.h:46
const Bool_t kFALSE
Definition RtypesCore.h:101
double Stat_t
Definition RtypesCore.h:86
long long Long64_t
Definition RtypesCore.h:80
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
const UInt_t kNewClassTag
const UInt_t kByteCountMask
EDataType
Definition TDataType.h:28
@ kOther_t
Definition TDataType.h:32
#define R__ASSERT(e)
Definition TError.h:118
#define N
char name[80]
Definition TGX11.cxx:110
int nentries
R__EXTERN TVirtualMutex * gROOTMutex
Definition TROOT.h:63
#define gROOT
Definition TROOT.h:404
void Printf(const char *fmt,...)
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
#define R__LOCKGUARD_IMT(mutex)
#define R__LOCKGUARD(mutex)
#define gPad
#define snprintf
Definition civetweb.c:1540
void SetUsed(Int_t basketNumber)
Mark if the basket has been marked as 'used'.
void Print(const char *owner, Long64_t *entries) const
Print the info we have for the baskets.
A helper class for managing IMT work during TTree:Fill operations.
TIOFeatures provides the end-user with the ability to change the IO behavior of data written via a TT...
Fill Area Attributes class.
Definition TAttFill.h:19
Manages buffers for branches of a Tree.
Definition TBasket.h:34
void AdoptBuffer(TBuffer *user_buffer)
Adopt a buffer from an external entity.
Definition TBasket.cxx:721
Int_t GetNevBufSize() const
Definition TBasket.h:130
Int_t * GetEntryOffset()
Definition TBasket.h:124
Int_t * GetDisplacement() const
Definition TBasket.h:123
Int_t GetNevBuf() const
Definition TBasket.h:129
void DisownBuffer()
Disown all references to the internal buffer - some other object likely now owns it.
Definition TBasket.cxx:713
void SetBranch(TBranch *branch)
Definition TBasket.h:148
Int_t ReadBasketBuffers(Long64_t pos, Int_t len, TFile *file)
Read basket buffers in memory and cleanup.
Definition TBasket.cxx:464
virtual Int_t DropBuffers()
Drop buffers of this basket if it is not the current basket.
Definition TBasket.cxx:173
virtual void PrepareBasket(Long64_t)
Definition TBasket.h:133
virtual void MoveEntries(Int_t dentries)
Remove the first dentries of this basket, moving entries at dentries to the start of the buffer.
Definition TBasket.cxx:310
void SetNevBufSize(Int_t n)
Definition TBasket.h:149
Int_t GetBufferSize() const
Definition TBasket.h:122
virtual void AdjustSize(Int_t newsize)
Increase the size of the current fBuffer up to newsize.
Definition TBasket.cxx:127
virtual void SetReadMode()
Set read mode of basket.
Definition TBasket.cxx:925
virtual void SetWriteMode()
Set write mode of basket.
Definition TBasket.cxx:934
Bool_t GetResetAllocationCount() const
Definition TBasket.h:143
Int_t ReadBasketBytes(Long64_t pos, TFile *file)
Read basket buffers in memory and cleanup.
Definition TBasket.cxx:698
virtual void ReadResetBuffer(Int_t basketnumber)
Reset the read basket TBuffer memory allocation if needed.
Definition TBasket.cxx:733
virtual Int_t WriteBuffer()
Write buffer of this basket on the current file.
Definition TBasket.cxx:1131
Int_t GetLast() const
Definition TBasket.h:131
void Update(Int_t newlast)
Definition TBasket.h:152
virtual void WriteReset()
Reset the write basket to the starting state.
Definition TBasket.cxx:806
A TTree is a list of TBranches.
Definition TBranch.h:89
virtual TLeaf * GetLeaf(const char *name) const
Return pointer to the 1st Leaf named name in thisBranch.
Definition TBranch.cxx:1993
virtual void SetupAddresses()
If the branch address is not set, we set all addresses starting with the top level parent branch.
Definition TBranch.cxx:3232
virtual void ResetAddress()
Reset the address of the branch.
Definition TBranch.cxx:2589
virtual void SetAutoDelete(Bool_t autodel=kTRUE)
Set the automatic delete bit.
Definition TBranch.cxx:2654
TString fFileName
Name of file where buffers are stored ("" if in same file as Tree header)
Definition TBranch.h:145
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any.
Definition TBranch.cxx:1324
const char * GetIconName() const
Return icon name depending on type of branch.
Definition TBranch.cxx:1332
TBasket * GetFreshBasket(Int_t basketnumber, TBuffer *user_buffer)
Return a fresh basket by either reusing an existing basket that needs to be drop (according to TTree:...
Definition TBranch.cxx:1840
TBasket * GetBasketImpl(Int_t basket, TBuffer *user_buffer)
Return pointer to basket basketnumber in this Branch.
Definition TBranch.cxx:1226
Int_t fEntryOffsetLen
Initial Length of fEntryOffset table in the basket buffers.
Definition TBranch.h:115
virtual void DeleteBaskets(Option_t *option="")
Loop on all branch baskets.
Definition TBranch.cxx:726
virtual Long64_t GetBasketSeek(Int_t basket) const
Return address of basket in the file.
Definition TBranch.cxx:1302
TBranch()
Default constructor. Used for I/O by default.
Definition TBranch.cxx:87
void SetCompressionSettings(Int_t settings=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault)
Set compression settings.
Definition TBranch.cxx:2743
Int_t BackFill()
Loop on all leaves of this branch to back fill Basket buffer.
Definition TBranch.cxx:680
Int_t fMaxBaskets
Maximum number of Baskets so far.
Definition TBranch.h:121
Long64_t fTotBytes
Total number of bytes in all leaves before compression.
Definition TBranch.h:132
TBuffer * fTransientBuffer
! Pointer to the current transient buffer.
Definition TBranch.h:147
virtual void ReadBasket(TBuffer &b)
Loop on all leaves of this branch to read Basket buffer.
Definition TBranch.cxx:2391
TTree * GetTree() const
Definition TBranch.h:248
FillLeaves_t fFillLeaves
! Pointer to the FillLeaves implementation to use.
Definition TBranch.h:159
virtual TString GetFullName() const
Return the 'full' name of the branch.
Definition TBranch.cxx:1977
@ kAutoDelete
Definition TBranch.h:106
@ kDoNotUseBufferMap
If set, at least one of the entry in the branch will use the buffer's map of classname and objects.
Definition TBranch.h:108
@ kIsClone
To indicate a TBranchClones.
Definition TBranch.h:102
@ kDoNotProcess
Active bit for branches.
Definition TBranch.h:101
TObjArray fLeaves
-> List of leaves of this branch
Definition TBranch.h:135
Int_t GetBasketAndFirst(TBasket *&basket, Long64_t &first, TBuffer *user_buffer)
A helper function to locate the correct basket - and its first entry.
Definition TBranch.cxx:1353
char * fAddress
! Address of 1st leaf (variable or object)
Definition TBranch.h:143
virtual void DropBaskets(Option_t *option="")
Loop on all branch baskets.
Definition TBranch.cxx:757
TObjArray * GetListOfBranches()
Definition TBranch.h:242
virtual TList * GetBrowsables()
Returns (and, if 0, creates) browsable objects for this branch See TVirtualBranchBrowsable::FillListO...
Definition TBranch.cxx:1312
TList * fBrowsables
! List of TVirtualBranchBrowsables used for Browse()
Definition TBranch.h:148
void ReadLeavesImpl(TBuffer &b)
Loop on all leaves of this branch to read Basket buffer.
Definition TBranch.cxx:2399
Int_t fOffset
Offset of this branch.
Definition TBranch.h:120
Long64_t * fBasketEntry
[fMaxBaskets] Table of first entry in each basket
Definition TBranch.h:138
Int_t GetEntriesSerialized(Long64_t N, TBuffer &user_buf)
Definition TBranch.h:181
virtual void SetEntryOffsetLen(Int_t len, Bool_t updateSubBranches=kFALSE)
Update the default value for the branch's fEntryOffsetLen if and only if it was already non zero (and...
Definition TBranch.cxx:2759
void ExpandBasketArrays()
Increase BasketEntry buffer of a minimum of 10 locations and a maximum of 50 per cent of current size...
Definition TBranch.cxx:825
void Init(const char *name, const char *leaflist, Int_t compress)
Definition TBranch.cxx:300
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Definition TBranch.cxx:1652
TIOFeatures GetIOFeatures() const
Returns the IO settings currently in use for this branch.
Definition TBranch.cxx:2193
void FillLeavesImpl(TBuffer &b)
Loop on all leaves of this branch to fill Basket buffer.
Definition TBranch.cxx:2434
Long64_t fReadEntry
! Current entry number when reading
Definition TBranch.h:126
virtual void AddBasket(TBasket &b, Bool_t ondisk, Long64_t startEntry)
Add the basket to this branch.
Definition TBranch.cxx:545
static void ResetCount()
Static function resetting fgCount.
Definition TBranch.cxx:2612
TBranch * GetSubBranch(const TBranch *br) const
Find the parent branch of child.
Definition TBranch.cxx:2102
ReadLeaves_t fReadLeaves
! Pointer to the ReadLeaves implementation to use.
Definition TBranch.h:157
virtual void SetObject(void *objadd)
Set object this branch is pointing to.
Definition TBranch.cxx:2874
Int_t FlushBaskets()
Flush to disk all the baskets of this branch and any of subbranches.
Definition TBranch.cxx:1136
void ReadLeaves2Impl(TBuffer &b)
Read two leaves without the overhead of a loop.
Definition TBranch.cxx:2425
virtual void SetAddress(void *add)
Set address of this branch.
Definition TBranch.cxx:2620
static Int_t fgCount
! branch counter
Definition TBranch.h:112
virtual void AddLastBasket(Long64_t startEntry)
Add the start entry of the write basket (not yet created)
Definition TBranch.cxx:618
TBasket * GetBasket(Int_t basket)
Definition TBranch.h:209
Int_t fNBaskets
! Number of baskets in memory
Definition TBranch.h:122
void ReadLeaves1Impl(TBuffer &b)
Read one leaf without the overhead of a loop.
Definition TBranch.cxx:2417
Int_t GetBulkEntries(Long64_t, TBuffer &)
Read as many events as possible into the given buffer, using zero-copy mechanisms.
Definition TBranch.cxx:1456
Bool_t IsFolder() const
Return kTRUE if more than one leaf or browsables, kFALSE otherwise.
Definition TBranch.cxx:2209
virtual Int_t GetEntryExport(Long64_t entry, Int_t getall, TClonesArray *list, Int_t n)
Read all leaves of an entry and export buffers to real objects in a TClonesArray list.
Definition TBranch.cxx:1708
Long64_t fZipBytes
Total number of bytes in all leaves after compression.
Definition TBranch.h:133
TIOFeatures fIOFeatures
IO features for newly-created baskets.
Definition TBranch.h:119
void SetCompressionAlgorithm(Int_t algorithm=ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
Set compression algorithm.
Definition TBranch.cxx:2701
Bool_t IsAutoDelete() const
Return kTRUE if an existing object in a TBranchObject must be deleted.
Definition TBranch.cxx:2201
virtual TLeaf * FindLeaf(const char *name)
Find the leaf corresponding to the name 'searchname'.
Definition TBranch.cxx:1081
CacheInfo_t fCacheInfo
! Hold info about which basket are in the cache and if they have been retrieved from the cache.
Definition TBranch.h:154
TObjArray * GetListOfBaskets()
Definition TBranch.h:241
virtual void SetBufferAddress(TBuffer *entryBuffer)
Set address of this branch directly from a TBuffer to avoid streaming.
Definition TBranch.cxx:2683
Long64_t GetEntries() const
Definition TBranch.h:247
Int_t fNleaves
! Number of leaves
Definition TBranch.h:124
Int_t fSplitLevel
Branch split level.
Definition TBranch.h:123
Int_t WriteBasketImpl(TBasket *basket, Int_t where, ROOT::Internal::TBranchIMTHelper *)
Write the current basket to disk and return the number of bytes written to the file.
Definition TBranch.cxx:3140
virtual void UpdateFile()
Refresh the value of fDirectory (i.e.
Definition TBranch.cxx:3242
Int_t * fBasketBytes
[fMaxBaskets] Length of baskets on file
Definition TBranch.h:137
virtual void Print(Option_t *option="") const
Print TBranch parameters.
Definition TBranch.cxx:2279
Long64_t fNextBasketEntry
! Next entry that will requires us to go to the next basket
Definition TBranch.h:128
virtual ~TBranch()
Destructor.
Definition TBranch.cxx:450
Int_t FillEntryBuffer(TBasket *basket, TBuffer *buf, Int_t &lnew)
Copy the data from fEntryBuffer into the current basket.
Definition TBranch.cxx:935
virtual TFile * GetFile(Int_t mode=0)
Return pointer to the file where branch buffers reside, returns 0 in case branch buffers reside in th...
Definition TBranch.cxx:1799
virtual void Browse(TBrowser *b)
Browser interface.
Definition TBranch.cxx:699
TObjArray fBranches
-> List of Branches of this branch
Definition TBranch.h:134
virtual void KeepCircular(Long64_t maxEntries)
keep a maximum of fMaxEntries in memory
Definition TBranch.cxx:2221
virtual void ResetAfterMerge(TFileMergeInfo *)
Reset a Branch.
Definition TBranch.cxx:2536
void ReadLeaves0Impl(TBuffer &b)
Read zero leaves without the overhead of a loop.
Definition TBranch.cxx:2410
virtual Bool_t GetMakeClass() const
Return whether this branch is in a mode where the object are decomposed or not (Also known as MakeCla...
Definition TBranch.cxx:2055
TString GetRealFileName() const
Get real file name.
Definition TBranch.cxx:2006
virtual TBranch * FindBranch(const char *name)
Find the immediate sub-branch with passed name.
Definition TBranch.cxx:1035
TDirectory * fDirectory
! Pointer to directory where this branch buffers are stored
Definition TBranch.h:144
void PrintCacheInfo() const
Print the information we have about which basket is currently cached and whether they have been 'used...
Definition TBranch.cxx:2383
TObjArray fBaskets
-> List of baskets of this branch
Definition TBranch.h:136
virtual Int_t LoadBaskets()
Baskets associated to this branch are forced to be in memory.
Definition TBranch.cxx:2247
Bool_t fSkipZip
! After being read, the buffer will not be unzipped.
Definition TBranch.h:151
TBranch * fMother
! Pointer to top-level parent branch in the tree.
Definition TBranch.h:141
Long64_t GetTotBytes(Option_t *option="") const
Return total number of bytes in the branch (excluding current buffer) if option ="*" includes all sub...
Definition TBranch.cxx:2158
virtual Bool_t SetMakeClass(Bool_t decomposeObj=kTRUE)
Set the branch in a mode where the object are decomposed (Also known as MakeClass mode).
Definition TBranch.cxx:2865
virtual void SetFile(TFile *file=0)
Set file where this branch writes/reads its buffers.
Definition TBranch.cxx:2801
TBranch * fParent
! Pointer to parent branch.
Definition TBranch.h:142
Int_t WriteBasket(TBasket *basket, Int_t where)
Definition TBranch.h:171
Int_t FlushOneBasket(UInt_t which)
If we have a write basket in memory and it contains some entries and has not yet been written to disk...
Definition TBranch.cxx:1182
virtual Int_t GetExpectedType(TClass *&clptr, EDataType &type)
Fill expectedClass and expectedType with information on the data type of the object/values contained ...
Definition TBranch.cxx:1780
virtual void SetFirstEntry(Long64_t entry)
set the first entry number (case of TBranchSTL)
Definition TBranch.cxx:3217
Long64_t GetTotalSize(Option_t *option="") const
Return total number of bytes in the branch (including current buffer)
Definition TBranch.cxx:2139
Long64_t GetZipBytes(Option_t *option="") const
Return total number of zip bytes in the branch if option ="*" includes all sub-branches of this branc...
Definition TBranch.cxx:2176
virtual void SetBasketSize(Int_t buffsize)
Set the basket size The function makes sure that the basket size is greater than fEntryOffsetlen.
Definition TBranch.cxx:2667
virtual void Refresh(TBranch *b)
Refresh this branch using new information in b This function is called by TTree::Refresh.
Definition TBranch.cxx:2446
Int_t fWriteBasket
Last basket number written.
Definition TBranch.h:116
Long64_t * fBasketSeek
[fMaxBaskets] Addresses of baskets on file
Definition TBranch.h:139
virtual void SetStatus(Bool_t status=1)
Set branch status to Process or DoNotProcess.
Definition TBranch.cxx:2885
TObjArray * GetListOfLeaves()
Definition TBranch.h:243
virtual void SetEntries(Long64_t entries)
Set the number of entries in this branch.
Definition TBranch.cxx:2776
Int_t fReadBasket
! Current basket number when reading
Definition TBranch.h:125
Long64_t fFirstEntry
Number of the first entry in this branch.
Definition TBranch.h:131
TBasket * fExtraBasket
! Allocated basket not currently holding any data.
Definition TBranch.h:118
virtual Int_t GetRow(Int_t row)
Return all elements of one row unpacked in internal array fValues [Actually just returns 1 (?...
Definition TBranch.cxx:2046
Int_t fBasketSize
Initial Size of Basket Buffer.
Definition TBranch.h:114
Int_t Fill()
Definition TBranch.h:201
virtual void Reset(Option_t *option="")
Reset a Branch.
Definition TBranch.cxx:2495
Long64_t fEntryNumber
Current entry number (last one filled in this branch)
Definition TBranch.h:117
TBranch * GetMother() const
Get our top-level parent branch in the tree.
Definition TBranch.cxx:2065
Int_t fCompress
Compression level and algorithm.
Definition TBranch.h:113
TBuffer * GetTransientBuffer(Int_t size)
Returns the transient buffer currently used by this TBranch for reading/writing baskets.
Definition TBranch.cxx:523
virtual Int_t FillImpl(ROOT::Internal::TBranchIMTHelper *)
Loop on all leaves of this branch to fill Basket buffer.
Definition TBranch.cxx:856
TBuffer * fEntryBuffer
! Buffer used to directly pass the content without streaming
Definition TBranch.h:146
TBasket * fCurrentBasket
! Pointer to the current basket.
Definition TBranch.h:129
Long64_t fFirstBasketEntry
! First entry in the current basket.
Definition TBranch.h:127
void SetCompressionLevel(Int_t level=ROOT::RCompressionSetting::ELevel::kUseMin)
Set compression level.
Definition TBranch.cxx:2721
Long64_t fEntries
Number of entries.
Definition TBranch.h:130
TBasket * GetFreshCluster(TBuffer *user_buffer)
Drops the cluster two behind the current cluster and returns a fresh basket by either reusing or crea...
Definition TBranch.cxx:1899
Bool_t SupportsBulkRead() const
Returns true if this branch supports bulk IO, false otherwise.
Definition TBranch.cxx:1431
TTree * fTree
! Pointer to Tree header
Definition TBranch.h:140
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket.
Definition TBufferFile.h:47
@ kNotDecompressed
Definition TBufferIO.h:66
Buffer base class used for serializing objects.
Definition TBuffer.h:43
void SetWriteMode()
Set buffer in write mode.
Definition TBuffer.cxx:315
char * GetCurrent() const
Definition TBuffer.h:97
void Expand(Int_t newsize, Bool_t copy=kTRUE)
Expand (or shrink) the I/O buffer to newsize bytes.
Definition TBuffer.cxx:223
virtual Int_t GetBufferDisplacement() const =0
Int_t BufferSize() const
Definition TBuffer.h:98
@ kIsOwner
Definition TBuffer.h:75
@ kWrite
Definition TBuffer.h:73
@ kRead
Definition TBuffer.h:73
void AutoExpand(Int_t size_needed)
Automatically calculate a new size and expand the buffer to fit at least size_needed.
Definition TBuffer.cxx:158
void SetBuffer(void *buf, UInt_t bufsiz=0, Bool_t adopt=kTRUE, ReAllocCharFun_t reallocfunc=0)
Sets a new buffer in an existing TBuffer object.
Definition TBuffer.cxx:187
@ kMinimalSize
Definition TBuffer.h:78
Bool_t IsReading() const
Definition TBuffer.h:86
virtual void ResetMap()=0
void SetBufferOffset(Int_t offset=0)
Definition TBuffer.h:93
virtual Int_t GetMapCount() const =0
void SetReadMode()
Set buffer in read mode.
Definition TBuffer.cxx:302
virtual void WriteBuf(const void *buf, Int_t max)=0
virtual void SetBufferDisplacement()=0
virtual char * ReadString(char *s, Int_t max)=0
Int_t Length() const
Definition TBuffer.h:100
char * Buffer() const
Definition TBuffer.h:96
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:80
An array of clone (identical) objects.
void Browse(TBrowser *b)
Browse this collection (called by TBrowser).
virtual void RemoveAll(TCollection *col)
Remove all objects in collection col from this collection.
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
TDirectory::TContext keeps track and restore the current directory.
Definition TDirectory.h:89
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TFile * GetFile() const
Definition TDirectory.h:220
virtual Bool_t IsWritable() const
Definition TDirectory.h:237
A cache when reading files over the network.
virtual void SetSkipZip(Bool_t=kTRUE)
virtual Bool_t IsLearning() const
virtual Int_t LearnBranch(TBranch *, Bool_t=kFALSE)
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
Int_t GetCompressionSettings() const
Definition TFile.h:401
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4025
virtual Long64_t GetSeekKey() const
Definition TKey.h:90
Int_t GetKeylen() const
Definition TKey.h:85
Int_t GetObjlen() const
Definition TKey.h:88
Int_t GetNbytes() const
Definition TKey.h:87
virtual void SetParent(const TObject *parent)
Set parent in key buffer.
Definition TKey.cxx:1294
TBuffer * GetBufferRef() const
Definition TKey.h:80
A TLeaf for an 8 bit Integer data type.
Definition TLeafB.h:26
A TLeaf for a variable length string.
Definition TLeafC.h:26
A TLeaf for a 24 bit truncated floating point data type.
Definition TLeafD32.h:28
A TLeaf for a 64 bit floating point data type.
Definition TLeafD.h:26
A TLeaf for a 24 bit truncated floating point data type.
Definition TLeafF16.h:27
A TLeaf for a 32 bit floating point data type.
Definition TLeafF.h:26
A TLeaf for a 64 bit Integer data type.
Definition TLeafG.h:27
A TLeaf for an Integer data type.
Definition TLeafI.h:27
A TLeaf for a 64 bit Integer data type.
Definition TLeafL.h:27
A TLeaf for a bool data type.
Definition TLeafO.h:26
A TLeaf for a 16 bit Integer data type.
Definition TLeafS.h:26
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
virtual Int_t GetLenType() const
Definition TLeaf.h:133
virtual const char * GetTypeName() const
Definition TLeaf.h:139
virtual Int_t GetLen() const
Return the number of effective elements of this leaf, for the current entry.
Definition TLeaf.cxx:404
virtual TLeaf * GetLeafCount() const
If this leaf stores a variable-sized array or a multi-dimensional array whose last dimension has vari...
Definition TLeaf.h:121
virtual DeserializeType GetDeserializeType() const
Definition TLeaf.h:117
virtual void ReadBasketExport(TBuffer &, TClonesArray *, Int_t)
Definition TLeaf.h:153
virtual void SetAddress(void *add=0)
Definition TLeaf.h:185
virtual Int_t GetNdata() const
Definition TLeaf.h:136
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition TLeaf.cxx:180
TBranch * GetBranch() const
Definition TLeaf.h:116
virtual void SetOffset(Int_t offset=0)
Definition TLeaf.h:164
virtual bool ReadBasketFast(TBuffer &, Long64_t)
Definition TLeaf.h:154
virtual void SetBranch(TBranch *branch)
Definition TLeaf.h:161
virtual void SetUnsigned()
Definition TLeaf.h:166
virtual Int_t GetOffset() const
Definition TLeaf.h:137
virtual void ReadBasket(TBuffer &)
Definition TLeaf.h:152
A doubly linked list.
Definition TList.h:38
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
TString fName
Definition TNamed.h:32
virtual const char * GetTitle() const
Returns title of object.
Definition TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
An array of TObjects.
Definition TObjArray.h:31
Int_t GetEntriesFast() const
Definition TObjArray.h:58
virtual void Expand(Int_t newSize)
Expand or shrink the array to newSize elements.
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
void Add(TObject *obj)
Definition TObjArray.h:68
Int_t GetEntries() const
Return the number of objects in array (i.e.
TObject * UncheckedAt(Int_t i) const
Definition TObjArray.h:84
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
void SetLast(Int_t last)
Set index of last object in array, effectively truncating the array.
Int_t GetLast() const
Return index of last object in array.
virtual TObject * Remove(TObject *obj)
Remove object from array.
Int_t LowerBound() const
Definition TObjArray.h:91
virtual void AddAt(TObject *obj, Int_t idx)
Add object at position ids.
virtual TObject * RemoveAt(Int_t idx)
Remove object at index idx.
TObject * At(Int_t idx) const
Definition TObjArray.h:164
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:201
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:200
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:949
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition TObject.h:153
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:766
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:515
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:963
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:991
void MakeZombie()
Definition TObject.h:53
void ResetBit(UInt_t f)
Definition TObject.h:200
static void * ReAlloc(void *vp, size_t size)
Reallocate (i.e.
Definition TStorage.cxx:183
static Int_t * ReAllocInt(Int_t *vp, size_t size, size_t oldsize)
Reallocate (i.e.
Definition TStorage.cxx:295
Basic string class.
Definition TString.h:136
Ssiz_t Length() const
Definition TString.h:410
void ToLower()
Change string to lower-case.
Definition TString.cxx:1150
void Clear()
Clear string without changing its capacity.
Definition TString.cxx:1201
Ssiz_t First(char c) const
Find first occurrence of a character c.
Definition TString.cxx:523
const char * Data() const
Definition TString.h:369
TString & Prepend(const char *cs)
Definition TString.h:661
TString & Remove(Ssiz_t pos)
Definition TString.h:673
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2314
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:624
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1274
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition TSystem.cxx:935
virtual Bool_t IsAbsoluteFileName(const char *dir)
Return true if dir is an absolute pathname.
Definition TSystem.cxx:952
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
Definition TSystem.cxx:1032
Helper class to iterate over cluster of baskets.
Definition TTree.h:267
Long64_t Previous()
Move on to the previous cluster and return the starting entry of this previous cluster.
Definition TTree.cxx:693
Long64_t GetStartEntry()
Definition TTree.h:299
Long64_t Next()
Move on to the next cluster and return the starting entry of this next cluster.
Definition TTree.cxx:649
Long64_t GetNextEntry()
Definition TTree.h:304
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual TVirtualPerfStats * GetPerfStats() const
Definition TTree.h:503
virtual TClusterIterator GetClusterIterator(Long64_t firstentry)
Return an iterator over the cluster of baskets starting at firstentry.
Definition TTree.cxx:5451
void AddAllocationCount(UInt_t count)
Definition TTree.h:334
virtual TObjArray * GetListOfLeaves()
Definition TTree.h:486
TFile * GetCurrentFile() const
Return pointer to the current file.
Definition TTree.cxx:5463
virtual void IncrementTotalBuffers(Int_t nbytes)
Definition TTree.h:543
TDirectory * GetDirectory() const
Definition TTree.h:459
TTreeCache * GetReadCache(TFile *file) const
Find and return the TTreeCache registered with the file and which may contain branches for us.
Definition TTree.cxx:6302
virtual TObjArray * GetListOfBranches()
Definition TTree.h:485
virtual void AddZipBytes(Int_t zip)
Definition TTree.h:329
virtual TBasket * CreateBasket(TBranch *)
Create a basket for this tree and given branch.
Definition TTree.cxx:3725
@ kOnlyFlushAtCluster
If set, the branch's buffers will grow until an event cluster boundary is hit, guaranteeing a basket ...
Definition TTree.h:253
@ kCircular
Definition TTree.h:249
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition TTree.h:428
virtual Bool_t GetClusterPrefetch() const
Definition TTree.h:454
virtual void AddTotBytes(Int_t tot)
Definition TTree.h:328
virtual Long64_t GetAutoFlush() const
Definition TTree.h:444
virtual Long64_t GetMaxVirtualSize() const
Definition TTree.h:497
This class represents a WWW compatible URL.
Definition TUrl.h:33
const char * GetAnchor() const
Definition TUrl.h:70
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
Definition TUrl.cxx:389
void SetAnchor(const char *anchor)
Definition TUrl.h:86
static Int_t FillListOfBrowsables(TList &list, const TBranch *branch, const TVirtualBranchBrowsable *parent=nullptr)
Askes all registered generators to fill their browsables into the list.
virtual void SetUsed(TBranch *b, size_t basketNumber)=0
const Int_t n
Definition legend1.C:16
Short_t Max(Short_t a, Short_t b)
Definition TMathBase.h:208
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Definition TMathBase.h:274
Definition file.py:1
Definition first.py:1
Definition tree.py:1
@ kUndefined
Undefined compression algorithm (must be kept the last of the list in case a new algorithm is added).
@ kUseMin
Compression level reserved when we are not sure what to use (1 is for the fastest compression)
Definition Compression.h:68
auto * l
Definition textangle.C:4