Logo ROOT   master
Reference Guide
TTree.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  \defgroup tree Tree Library
13 
14  In order to store columnar datasets, ROOT provides the TTree, TChain,
15  TNtuple and TNtupleD classes.
16  The TTree class represents a columnar dataset. Any C++ type can be stored in the
17  columns. The TTree has allowed to store about **1 EB** of data coming from the LHC alone:
18  it is demonstrated to scale and it's battle tested. It has been optimized during the years
19  to reduce dataset sizes on disk and to deliver excellent runtime performance.
20  It allows to access only part of the columns of the datasets, too.
21  The TNtuple and TNtupleD classes are specialisations of the TTree class which can
22  only hold single precision and double precision floating-point numbers respectively;
23  The TChain is a collection of TTrees, which can be located also in different files.
24 
25 */
26 
27 /** \class TTree
28 \ingroup tree
29 
30 A TTree represents a columnar dataset. Any C++ type can be stored in its columns.
31 
32 A TTree, often called in jargon *tree*, consists of a list of independent columns or *branches*,
33 represented by the TBranch class.
34 Behind each branch, buffers are allocated automatically by ROOT.
35 Such buffers are automatically written to disk or kept in memory until the size stored in the
36 attribute fMaxVirtualSize is reached.
37 Variables of one branch are written to the same buffer. A branch buffer is
38 automatically compressed if the file compression attribute is set (default).
39 Branches may be written to different files (see TBranch::SetFile).
40 
41 The ROOT user can decide to make one single branch and serialize one object into
42 one single I/O buffer or to make several branches.
43 Making several branches is particularly interesting in the data analysis phase,
44 when it is desirable to have a high reading rate and not all columns are equally interesting
45 
46 ## Table of contents:
47 - [Creating a TTree](#creatingattree)
48 - [Add a Column of Fundamental Types and Arrays thereof](#addcolumnoffundamentaltypes)
49 - [Add a Column of a STL Collection instances](#addingacolumnofstl)
50 - [Add a column holding an object](#addingacolumnofobjs)
51 - [Add a column holding a TObjectArray](#addingacolumnofobjs)
52 - [Fill the tree](#fillthetree)
53 - [Add a column to an already existing Tree](#addcoltoexistingtree)
54 - [An Example](#fullexample)
55 
56 ## <a name="creatingattree"></a>Creating a TTree
57 
58 ~~~ {.cpp}
59  TTree tree(name, title)
60 ~~~
61 Creates a Tree with name and title.
62 
63 Various kinds of branches can be added to a tree:
64 - Variables representing fundamental types, simple classes/structures or list of variables: for example for C or Fortran
65 structures.
66 - Any C++ object or collection, provided by the STL or ROOT.
67 
68 In the following, the details about the creation of different types of branches are given.
69 
70 ## <a name="addcolumnoffundamentaltypes"></a>Add a column (`branch`) of fundamental types and arrays thereof
71 This strategy works also for lists of variables, e.g. to describe simple structures.
72 It is strongly reccomended to persistify those as objects rather than lists of leaves.
73 
74 ~~~ {.cpp}
75  auto branch = tree.Branch(branchname, address, leaflist, bufsize)
76 ~~~
77 - address is the address of the first item of a structure
78 - leaflist is the concatenation of all the variable names and types
79  separated by a colon character :
80  The variable name and the variable type are separated by a
81  slash (/). The variable type must be 1 character. (Characters
82  after the first are legal and will be appended to the visible
83  name of the leaf, but have no effect.) If no type is given, the
84  type of the variable is assumed to be the same as the previous
85  variable. If the first variable does not have a type, it is
86  assumed of type F by default. The list of currently supported
87  types is given below:
88  - `C` : a character string terminated by the 0 character
89  - `B` : an 8 bit signed integer (`Char_t`)
90  - `b` : an 8 bit unsigned integer (`UChar_t`)
91  - `S` : a 16 bit signed integer (`Short_t`)
92  - `s` : a 16 bit unsigned integer (`UShort_t`)
93  - `I` : a 32 bit signed integer (`Int_t`)
94  - `i` : a 32 bit unsigned integer (`UInt_t`)
95  - `F` : a 32 bit floating point (`Float_t`)
96  - `f` : a 24 bit floating point with truncated mantissa (`Float16_t`)
97  - `D` : a 64 bit floating point (`Double_t`)
98  - `d` : a 24 bit truncated floating point (`Double32_t`)
99  - `L` : a 64 bit signed integer (`Long64_t`)
100  - `l` : a 64 bit unsigned integer (`ULong64_t`)
101  - `O` : [the letter `o`, not a zero] a boolean (`Bool_t`)
102 
103  Examples:
104  - A int: "myVar/I"
105  - A float array with fixed size: "myArrfloat[42]/F"
106  - An double array with variable size, held by the `myvar` column: "myArrdouble[myvar]/D"
107  - An Double32_t array with variable size, held by the `myvar` column , with values between 0 and 16: "myArr[myvar]/d[0,10]"
108 
109 - If the address points to a single numerical variable, the leaflist is optional:
110 ~~~ {.cpp}
111  int value;
112  `tree->Branch(branchname, &value);`
113 ~~~
114 - If the address points to more than one numerical variable, we strongly recommend
115  that the variable be sorted in decreasing order of size. Any other order will
116  result in a non-portable TTree (i.e. you will not be able to read it back on a
117  platform with a different padding strategy).
118  We recommend to persistify objects rather than composite leaflists.
119 - In case of the truncated floating point types (Float16_t and Double32_t) you can
120  furthermore specify the range in the style [xmin,xmax] or [xmin,xmax,nbits] after
121  the type character. For example, for storing a variable size array `myArr` of
122  `Double32_t` with values within a range of `[0, 2*pi]` and the size of which is
123  stored in a branch called `myArrSize`, the syntax for the `leaflist` string would
124  be: `myArr[myArrSize]/d[0,twopi]`. Of course the number of bits could be specified,
125  the standard rules of opaque typedefs annotation are valid. For example, if only
126  18 bits were sufficient, the syntax would become: `myArr[myArrSize]/d[0,twopi,18]`
127 
128 ## <a name="addingacolumnofstl"></a>Adding a column of STL collection instances (e.g. std::vector, std::list, std::unordered_map)
129 
130 ~~~ {.cpp}
131  auto branch = tree.Branch( branchname, STLcollection, buffsize, splitlevel);
132 ~~~
133 STLcollection is the address of a pointer to std::vector, std::list,
134 std::deque, std::set or std::multiset containing pointers to objects.
135 If the splitlevel is a value bigger than 100 (TTree::kSplitCollectionOfPointers)
136 then the collection will be written in split mode, e.g. if it contains objects of
137 any types deriving from TTrack this function will sort the objects
138 based on their type and store them in separate branches in split
139 mode.
140 
141 ~~~ {.cpp}
142  branch->SetAddress(void *address)
143 ~~~
144 In case of dynamic structures changing with each entry for example, one must
145 redefine the branch address before filling the branch again.
146 This is done via the TBranch::SetAddress member function.
147 
148 ## <a name="addingacolumnofobjs">Add a column of objects
149 
150 ~~~ {.cpp}
151  MyClass object;
152  auto branch = tree.Branch(branchname, &object, bufsize, splitlevel)
153 ~~~
154 Note: The 2nd parameter must be the address of a valid object.
155  The object must not be destroyed (i.e. be deleted) until the TTree
156  is deleted or TTree::ResetBranchAddress is called.
157 
158 - if splitlevel=0, the object is serialized in the branch buffer.
159 - if splitlevel=1 (default), this branch will automatically be split
160  into subbranches, with one subbranch for each data member or object
161  of the object itself. In case the object member is a TClonesArray,
162  the mechanism described in case C is applied to this array.
163 - if splitlevel=2 ,this branch will automatically be split
164  into subbranches, with one subbranch for each data member or object
165  of the object itself. In case the object member is a TClonesArray,
166  it is processed as a TObject*, only one branch.
167 
168 Another available syntax is the following:
169 
170 ~~~ {.cpp}
171  auto branch = tree.Branch(branchname, &p_object, bufsize, splitlevel)
172  auto branch = tree.Branch(branchname, className, &p_object, bufsize, splitlevel)
173 ~~~
174 - p_object is a pointer to an object.
175 - If className is not specified, Branch uses the type of p_object to determine the
176  type of the object.
177 - If className is used to specify explicitly the object type, the className must
178  be of a type related to the one pointed to by the pointer. It should be either
179  a parent or derived class.
180 
181 Note: The pointer whose address is passed to TTree::Branch must not
182  be destroyed (i.e. go out of scope) until the TTree is deleted or
183  TTree::ResetBranchAddress is called.
184 
185 Note: The pointer p_object must be initialized before calling TTree::Branch
186 - Do either:
187 ~~~ {.cpp}
188  MyDataClass* p_object = nullptr;
189  tree.Branch(branchname, &p_object);
190 ~~~
191 - Or:
192 ~~~ {.cpp}
193  auto p_object = new MyDataClass;
194  tree.Branch(branchname, &p_object);
195 ~~~
196 Whether the pointer is set to zero or not, the ownership of the object
197 is not taken over by the TTree. I.e. even though an object will be allocated
198 by TTree::Branch if the pointer p_object is zero, the object will <b>not</b>
199 be deleted when the TTree is deleted.
200 
201 ## <a name="addingacolumnoftclonesarray">Add a column of TClonesArray instances
202 
203 *It is recommended to use STL containers instead of TClonesArrays*.
204 
205 ~~~ {.cpp}
206  // clonesarray is the address of a pointer to a TClonesArray.
207  auto branch = tree.Branch(branchname,clonesarray, bufsize, splitlevel)
208 ~~~
209 The TClonesArray is a direct access list of objects of the same class.
210 For example, if the TClonesArray is an array of TTrack objects,
211 this function will create one subbranch for each data member of
212 the object TTrack.
213 
214 ## <a name="fillthetree">Fill the Tree:
215 
216 A TTree instance is filled with the invocation of the TTree::Fill method:
217 ~~~ {.cpp}
218  tree.Fill()
219 ~~~
220 Upon its invocation, a loop on all defined branches takes place that for each branch invokes
221 the TBranch::Fill method.
222 
223 ## <a name="addcoltoexistingtree">Add a column to an already existing Tree
224 
225 You may want to add a branch to an existing tree. For example,
226 if one variable in the tree was computed with a certain algorithm,
227 you may want to try another algorithm and compare the results.
228 One solution is to add a new branch, fill it, and save the tree.
229 The code below adds a simple branch to an existing tree.
230 Note the kOverwrite option in the Write method, it overwrites the
231 existing tree. If it is not specified, two copies of the tree headers
232 are saved.
233 ~~~ {.cpp}
234  void tree3AddBranch() {
235  TFile f("tree3.root", "update");
236 
237  Float_t new_v;
238  auto t3 = f->Get<TTree>("t3");
239  auto newBranch = t3->Branch("new_v", &new_v, "new_v/F");
240 
241  Long64_t nentries = t3->GetEntries(); // read the number of entries in the t3
242 
243  for (Long64_t i = 0; i < nentries; i++) {
244  new_v = gRandom->Gaus(0, 1);
245  newBranch->Fill();
246  }
247 
248  t3->Write("", TObject::kOverwrite); // save only the new version of the tree
249  }
250 ~~~
251 It is not always possible to add branches to existing datasets stored in TFiles: for example,
252 these files might not be writeable, just readable. In addition, modifying in place a TTree
253 causes a new TTree instance to be written and the previous one to be deleted.
254 For this reasons, ROOT offers the concept of friends for TTree and TChain:
255 if is good practice to rely on friend trees rather than adding a branch manually.
256 
257 ## <a name="fullexample">An Example
258 
259 Begin_Macro
260 ../../../tutorials/tree/tree.C
261 End_Macro
262 
263 ~~~ {.cpp}
264  // A simple example with histograms and a tree
265  //
266  // This program creates :
267  // - a one dimensional histogram
268  // - a two dimensional histogram
269  // - a profile histogram
270  // - a tree
271  //
272  // These objects are filled with some random numbers and saved on a file.
273 
274  #include "TFile.h"
275  #include "TH1.h"
276  #include "TH2.h"
277  #include "TProfile.h"
278  #include "TRandom.h"
279  #include "TTree.h"
280 
281  //__________________________________________________________________________
282  main(int argc, char **argv)
283  {
284  // Create a new ROOT binary machine independent file.
285  // Note that this file may contain any kind of ROOT objects, histograms,trees
286  // pictures, graphics objects, detector geometries, tracks, events, etc..
287  // This file is now becoming the current directory.
288  TFile hfile("htree.root","RECREATE","Demo ROOT file with histograms & trees");
289 
290  // Create some histograms and a profile histogram
291  TH1F hpx("hpx","This is the px distribution",100,-4,4);
292  TH2F hpxpy("hpxpy","py ps px",40,-4,4,40,-4,4);
293  TProfile hprof("hprof","Profile of pz versus px",100,-4,4,0,20);
294 
295  // Define some simple structures
296  typedef struct {Float_t x,y,z;} POINT;
297  typedef struct {
298  Int_t ntrack,nseg,nvertex;
299  UInt_t flag;
300  Float_t temperature;
301  } EVENTN;
302  POINT point;
303  EVENTN eventn;
304 
305  // Create a ROOT Tree
306  TTree tree("T","An example of ROOT tree with a few branches");
307  tree.Branch("point",&point,"x:y:z");
308  tree.Branch("eventn",&eventn,"ntrack/I:nseg:nvertex:flag/i:temperature/F");
309  tree.Branch("hpx","TH1F",&hpx,128000,0);
310 
311  Float_t px,py,pz;
312 
313  // Here we start a loop on 1000 events
314  for ( Int_t i=0; i<1000; i++) {
315  gRandom->Rannor(px,py);
316  pz = px*px + py*py;
317  const auto random = gRandom->::Rndm(1);
318 
319  // Fill histograms
320  hpx.Fill(px);
321  hpxpy.Fill(px,py,1);
322  hprof.Fill(px,pz,1);
323 
324  // Fill structures
325  point.x = 10*(random-1);
326  point.y = 5*random;
327  point.z = 20*random;
328  eventn.ntrack = Int_t(100*random);
329  eventn.nseg = Int_t(2*eventn.ntrack);
330  eventn.nvertex = 1;
331  eventn.flag = Int_t(random+0.5);
332  eventn.temperature = 20+random;
333 
334  // Fill the tree. For each event, save the 2 structures and 3 objects
335  // In this simple example, the objects hpx, hprof and hpxpy are slightly
336  // different from event to event. We expect a big compression factor!
337  tree->Fill();
338  }
339  // End of the loop
340 
341  tree.Print();
342 
343  // Save all objects in this file
344  hfile.Write();
345 
346  // Close the file. Note that this is automatically done when you leave
347  // the application upon file destruction.
348  hfile.Close();
349 
350  return 0;
351 }
352 ~~~
353 */
354 
355 #include <ROOT/RConfig.hxx>
356 #include "TTree.h"
357 
358 #include "ROOT/TIOFeatures.hxx"
359 #include "TArrayC.h"
360 #include "TBufferFile.h"
361 #include "TBaseClass.h"
362 #include "TBasket.h"
363 #include "TBranchClones.h"
364 #include "TBranchElement.h"
365 #include "TBranchObject.h"
366 #include "TBranchRef.h"
367 #include "TBrowser.h"
368 #include "TClass.h"
369 #include "TClassEdit.h"
370 #include "TClonesArray.h"
371 #include "TCut.h"
372 #include "TDataMember.h"
373 #include "TDataType.h"
374 #include "TDirectory.h"
375 #include "TError.h"
376 #include "TEntryList.h"
377 #include "TEnv.h"
378 #include "TEventList.h"
379 #include "TFile.h"
380 #include "TFolder.h"
381 #include "TFriendElement.h"
382 #include "TInterpreter.h"
383 #include "TLeaf.h"
384 #include "TLeafB.h"
385 #include "TLeafC.h"
386 #include "TLeafD.h"
387 #include "TLeafElement.h"
388 #include "TLeafF.h"
389 #include "TLeafI.h"
390 #include "TLeafL.h"
391 #include "TLeafObject.h"
392 #include "TLeafS.h"
393 #include "TList.h"
394 #include "TMath.h"
395 #include "TROOT.h"
396 #include "TRealData.h"
397 #include "TRegexp.h"
398 #include "TStreamerElement.h"
399 #include "TStreamerInfo.h"
400 #include "TStyle.h"
401 #include "TSystem.h"
402 #include "TTreeCloner.h"
403 #include "TTreeCache.h"
404 #include "TTreeCacheUnzip.h"
405 #include "TVirtualCollectionProxy.h"
407 #include "TVirtualIndex.h"
408 #include "TVirtualPerfStats.h"
409 #include "TVirtualPad.h"
410 #include "TBranchSTL.h"
411 #include "TSchemaRuleSet.h"
412 #include "TFileMergeInfo.h"
413 #include "ROOT/StringConv.hxx"
414 #include "TVirtualMutex.h"
415 
416 #include "TBranchIMTHelper.h"
417 #include "TNotifyLink.h"
418 
419 #include <chrono>
420 #include <cstddef>
421 #include <iostream>
422 #include <fstream>
423 #include <sstream>
424 #include <string>
425 #include <stdio.h>
426 #include <limits.h>
427 #include <algorithm>
428 
429 #ifdef R__USE_IMT
430 #include "ROOT/TThreadExecutor.hxx"
431 #include <thread>
432 #include <string>
433 #include <sstream>
434 #endif
436 constexpr Int_t kNEntriesResort = 100;
438 
439 Int_t TTree::fgBranchStyle = 1; // Use new TBranch style with TBranchElement.
440 Long64_t TTree::fgMaxTreeSize = 100000000000LL;
441 
442 ClassImp(TTree);
443 
444 ////////////////////////////////////////////////////////////////////////////////
445 ////////////////////////////////////////////////////////////////////////////////
446 ////////////////////////////////////////////////////////////////////////////////
448 static char DataTypeToChar(EDataType datatype)
449 {
450  // Return the leaflist 'char' for a given datatype.
451 
452  switch(datatype) {
453  case kChar_t: return 'B';
454  case kUChar_t: return 'b';
455  case kBool_t: return 'O';
456  case kShort_t: return 'S';
457  case kUShort_t: return 's';
458  case kCounter:
459  case kInt_t: return 'I';
460  case kUInt_t: return 'i';
461  case kDouble_t: return 'D';
462  case kDouble32_t: return 'd';
463  case kFloat_t: return 'F';
464  case kFloat16_t: return 'f';
465  case kLong_t: return 0; // unsupported
466  case kULong_t: return 0; // unsupported?
467  case kchar: return 0; // unsupported
468  case kLong64_t: return 'L';
469  case kULong64_t: return 'l';
470 
471  case kCharStar: return 'C';
472  case kBits: return 0; //unsupported
473 
474  case kOther_t:
475  case kNoType_t:
476  default:
477  return 0;
478  }
479  return 0;
480 }
481 
482 ////////////////////////////////////////////////////////////////////////////////
483 /// \class TTree::TFriendLock
484 /// Helper class to prevent infinite recursion in the usage of TTree Friends.
485 
486 ////////////////////////////////////////////////////////////////////////////////
487 /// Record in tree that it has been used while recursively looks through the friends.
490 : fTree(tree)
491 {
492  // We could also add some code to acquire an actual
493  // lock to prevent multi-thread issues
494  fMethodBit = methodbit;
495  if (fTree) {
498  } else {
499  fPrevious = 0;
500  }
501 }
502 
503 ////////////////////////////////////////////////////////////////////////////////
504 /// Copy constructor.
507  fTree(tfl.fTree),
508  fMethodBit(tfl.fMethodBit),
509  fPrevious(tfl.fPrevious)
510 {
511 }
512 
513 ////////////////////////////////////////////////////////////////////////////////
514 /// Assignment operator.
517 {
518  if(this!=&tfl) {
519  fTree=tfl.fTree;
520  fMethodBit=tfl.fMethodBit;
521  fPrevious=tfl.fPrevious;
522  }
523  return *this;
524 }
525 
526 ////////////////////////////////////////////////////////////////////////////////
527 /// Restore the state of tree the same as before we set the lock.
530 {
531  if (fTree) {
532  if (!fPrevious) {
533  fTree->fFriendLockStatus &= ~(fMethodBit & kBitMask);
534  }
535  }
536 }
537 
538 ////////////////////////////////////////////////////////////////////////////////
539 /// \class TTree::TClusterIterator
540 /// Helper class to iterate over cluster of baskets.
541 
542 ////////////////////////////////////////////////////////////////////////////////
543 /// Regular constructor.
544 /// TTree is not set as const, since we might modify if it is a TChain.
546 TTree::TClusterIterator::TClusterIterator(TTree *tree, Long64_t firstEntry) : fTree(tree), fClusterRange(0), fStartEntry(0), fNextEntry(0), fEstimatedSize(-1)
547 {
548  if (fTree->fNClusterRange) {
549  // Find the correct cluster range.
550  //
551  // Since fClusterRangeEnd contains the inclusive upper end of the range, we need to search for the
552  // range that was containing the previous entry and add 1 (because BinarySearch consider the values
553  // to be the inclusive start of the bucket).
555 
556  Long64_t entryInRange;
557  Long64_t pedestal;
558  if (fClusterRange == 0) {
559  pedestal = 0;
560  entryInRange = firstEntry;
561  } else {
562  pedestal = fTree->fClusterRangeEnd[fClusterRange-1] + 1;
563  entryInRange = firstEntry - pedestal;
564  }
565  Long64_t autoflush;
567  autoflush = fTree->fAutoFlush;
568  } else {
569  autoflush = fTree->fClusterSize[fClusterRange];
570  }
571  if (autoflush <= 0) {
572  autoflush = GetEstimatedClusterSize();
573  }
574  fStartEntry = pedestal + entryInRange - entryInRange%autoflush;
575  } else if ( fTree->GetAutoFlush() <= 0 ) {
576  // Case of old files before November 9 2009 *or* small tree where AutoFlush was never set.
577  fStartEntry = firstEntry;
578  } else {
579  fStartEntry = firstEntry - firstEntry%fTree->GetAutoFlush();
580  }
581  fNextEntry = fStartEntry; // Position correctly for the first call to Next()
582 }
583 
584 ////////////////////////////////////////////////////////////////////////////////
585 /// Estimate the cluster size.
586 ///
587 /// In almost all cases, this quickly returns the size of the auto-flush
588 /// in the TTree.
589 ///
590 /// However, in the case where the cluster size was not fixed (old files and
591 /// case where autoflush was explicitly set to zero), we need estimate
592 /// a cluster size in relation to the size of the cache.
593 ///
594 /// After this value is calculated once for the TClusterIterator, it is
595 /// cached and reused in future calls.
598 {
599  auto autoFlush = fTree->GetAutoFlush();
600  if (autoFlush > 0) return autoFlush;
601  if (fEstimatedSize > 0) return fEstimatedSize;
602 
603  Long64_t zipBytes = fTree->GetZipBytes();
604  if (zipBytes == 0) {
605  fEstimatedSize = fTree->GetEntries() - 1;
606  } else {
607  Long64_t clusterEstimate = 1;
608  Long64_t cacheSize = fTree->GetCacheSize();
609  if (cacheSize == 0) {
610  // Humm ... let's double check on the file.
611  TFile *file = fTree->GetCurrentFile();
612  if (file) {
613  TFileCacheRead *cache = fTree->GetReadCache(file);
614  if (cache) {
615  cacheSize = cache->GetBufferSize();
616  }
617  }
618  }
619  // If neither file nor tree has a cache, use the current default.
620  if (cacheSize <= 0) {
621  cacheSize = 30000000;
622  }
623  clusterEstimate = fTree->GetEntries() * cacheSize / zipBytes;
624  // If there are no entries, then just default to 1.
625  fEstimatedSize = clusterEstimate ? clusterEstimate : 1;
626  }
627  return fEstimatedSize;
628 }
629 
630 ////////////////////////////////////////////////////////////////////////////////
631 /// Move on to the next cluster and return the starting entry
632 /// of this next cluster
635 {
636  fStartEntry = fNextEntry;
637  if (fTree->fNClusterRange || fTree->GetAutoFlush() > 0) {
638  if (fClusterRange == fTree->fNClusterRange) {
639  // We are looking at a range which size
640  // is defined by AutoFlush itself and goes to the GetEntries.
641  fNextEntry += GetEstimatedClusterSize();
642  } else {
643  if (fStartEntry > fTree->fClusterRangeEnd[fClusterRange]) {
644  ++fClusterRange;
645  }
646  if (fClusterRange == fTree->fNClusterRange) {
647  // We are looking at the last range which size
648  // is defined by AutoFlush itself and goes to the GetEntries.
649  fNextEntry += GetEstimatedClusterSize();
650  } else {
651  Long64_t clusterSize = fTree->fClusterSize[fClusterRange];
652  if (clusterSize == 0) {
653  clusterSize = GetEstimatedClusterSize();
654  }
655  fNextEntry += clusterSize;
656  if (fNextEntry > fTree->fClusterRangeEnd[fClusterRange]) {
657  // The last cluster of the range was a partial cluster,
658  // so the next cluster starts at the beginning of the
659  // next range.
660  fNextEntry = fTree->fClusterRangeEnd[fClusterRange] + 1;
661  }
662  }
663  }
664  } else {
665  // Case of old files before November 9 2009
666  fNextEntry = fStartEntry + GetEstimatedClusterSize();
667  }
668  if (fNextEntry > fTree->GetEntries()) {
669  fNextEntry = fTree->GetEntries();
670  }
671  return fStartEntry;
672 }
673 
674 ////////////////////////////////////////////////////////////////////////////////
675 /// Move on to the previous cluster and return the starting entry
676 /// of this previous cluster
679 {
680  fNextEntry = fStartEntry;
681  if (fTree->fNClusterRange || fTree->GetAutoFlush() > 0) {
682  if (fClusterRange == 0 || fTree->fNClusterRange == 0) {
683  // We are looking at a range which size
684  // is defined by AutoFlush itself.
685  fStartEntry -= GetEstimatedClusterSize();
686  } else {
687  if (fNextEntry <= fTree->fClusterRangeEnd[fClusterRange]) {
688  --fClusterRange;
689  }
690  if (fClusterRange == 0) {
691  // We are looking at the first range.
692  fStartEntry = 0;
693  } else {
694  Long64_t clusterSize = fTree->fClusterSize[fClusterRange];
695  if (clusterSize == 0) {
696  clusterSize = GetEstimatedClusterSize();
697  }
698  fStartEntry -= clusterSize;
699  }
700  }
701  } else {
702  // Case of old files before November 9 2009 or trees that never auto-flushed.
703  fStartEntry = fNextEntry - GetEstimatedClusterSize();
704  }
705  if (fStartEntry < 0) {
706  fStartEntry = 0;
707  }
708  return fStartEntry;
709 }
710 
711 ////////////////////////////////////////////////////////////////////////////////
712 ////////////////////////////////////////////////////////////////////////////////
713 ////////////////////////////////////////////////////////////////////////////////
714 
715 ////////////////////////////////////////////////////////////////////////////////
716 /// Default constructor and I/O constructor.
717 ///
718 /// Note: We do *not* insert ourself into the current directory.
719 ///
721 TTree::TTree()
722 : TNamed()
723 , TAttLine()
724 , TAttFill()
725 , TAttMarker()
726 , fEntries(0)
727 , fTotBytes(0)
728 , fZipBytes(0)
729 , fSavedBytes(0)
730 , fFlushedBytes(0)
731 , fWeight(1)
732 , fTimerInterval(0)
733 , fScanField(25)
734 , fUpdate(0)
736 , fNClusterRange(0)
737 , fMaxClusterRange(0)
738 , fMaxEntries(0)
739 , fMaxEntryLoop(0)
740 , fMaxVirtualSize(0)
741 , fAutoSave( -300000000)
742 , fAutoFlush(-30000000)
743 , fEstimate(1000000)
744 , fClusterRangeEnd(0)
745 , fClusterSize(0)
746 , fCacheSize(0)
747 , fChainOffset(0)
748 , fReadEntry(-1)
749 , fTotalBuffers(0)
750 , fPacketSize(100)
751 , fNfill(0)
752 , fDebug(0)
753 , fDebugMin(0)
754 , fDebugMax(9999999)
755 , fMakeClass(0)
756 , fFileNumber(0)
757 , fNotify(0)
758 , fDirectory(0)
759 , fBranches()
760 , fLeaves()
761 , fAliases(0)
762 , fEventList(0)
763 , fEntryList(0)
764 , fIndexValues()
765 , fIndex()
766 , fTreeIndex(0)
767 , fFriends(0)
768 , fExternalFriends(0)
769 , fPerfStats(0)
770 , fUserInfo(0)
771 , fPlayer(0)
772 , fClones(0)
773 , fBranchRef(0)
775 , fTransientBuffer(0)
781 {
782  fMaxEntries = 1000000000;
783  fMaxEntries *= 1000;
784 
785  fMaxEntryLoop = 1000000000;
786  fMaxEntryLoop *= 1000;
787 
789 }
790 
791 ////////////////////////////////////////////////////////////////////////////////
792 /// Normal tree constructor.
793 ///
794 /// The tree is created in the current directory.
795 /// Use the various functions Branch below to add branches to this tree.
796 ///
797 /// If the first character of title is a "/", the function assumes a folder name.
798 /// In this case, it creates automatically branches following the folder hierarchy.
799 /// splitlevel may be used in this case to control the split level.
801 TTree::TTree(const char* name, const char* title, Int_t splitlevel /* = 99 */,
802  TDirectory* dir /* = gDirectory*/)
803 : TNamed(name, title)
804 , TAttLine()
805 , TAttFill()
806 , TAttMarker()
807 , fEntries(0)
808 , fTotBytes(0)
809 , fZipBytes(0)
810 , fSavedBytes(0)
811 , fFlushedBytes(0)
812 , fWeight(1)
813 , fTimerInterval(0)
814 , fScanField(25)
815 , fUpdate(0)
816 , fDefaultEntryOffsetLen(1000)
817 , fNClusterRange(0)
818 , fMaxClusterRange(0)
819 , fMaxEntries(0)
820 , fMaxEntryLoop(0)
821 , fMaxVirtualSize(0)
822 , fAutoSave( -300000000)
823 , fAutoFlush(-30000000)
824 , fEstimate(1000000)
825 , fClusterRangeEnd(0)
826 , fClusterSize(0)
827 , fCacheSize(0)
828 , fChainOffset(0)
829 , fReadEntry(-1)
830 , fTotalBuffers(0)
831 , fPacketSize(100)
832 , fNfill(0)
833 , fDebug(0)
834 , fDebugMin(0)
835 , fDebugMax(9999999)
836 , fMakeClass(0)
837 , fFileNumber(0)
838 , fNotify(0)
839 , fDirectory(dir)
840 , fBranches()
841 , fLeaves()
842 , fAliases(0)
843 , fEventList(0)
844 , fEntryList(0)
845 , fIndexValues()
846 , fIndex()
847 , fTreeIndex(0)
848 , fFriends(0)
849 , fExternalFriends(0)
850 , fPerfStats(0)
851 , fUserInfo(0)
852 , fPlayer(0)
853 , fClones(0)
854 , fBranchRef(0)
855 , fFriendLockStatus(0)
856 , fTransientBuffer(0)
857 , fCacheDoAutoInit(kTRUE)
858 , fCacheDoClusterPrefetch(kFALSE)
859 , fCacheUserSet(kFALSE)
860 , fIMTEnabled(ROOT::IsImplicitMTEnabled())
861 , fNEntriesSinceSorting(0)
862 {
863  // TAttLine state.
867 
868  // TAttFill state.
871 
872  // TAttMarkerState.
876 
877  fMaxEntries = 1000000000;
878  fMaxEntries *= 1000;
879 
880  fMaxEntryLoop = 1000000000;
881  fMaxEntryLoop *= 1000;
882 
883  // Insert ourself into the current directory.
884  // FIXME: This is very annoying behaviour, we should
885  // be able to choose to not do this like we
886  // can with a histogram.
887  if (fDirectory) fDirectory->Append(this);
888 
890 
891  // If title starts with "/" and is a valid folder name, a superbranch
892  // is created.
893  // FIXME: Why?
894  if (strlen(title) > 2) {
895  if (title[0] == '/') {
896  Branch(title+1,32000,splitlevel);
897  }
898  }
899 }
900 
901 ////////////////////////////////////////////////////////////////////////////////
902 /// Destructor.
905 {
906  if (auto link = dynamic_cast<TNotifyLinkBase*>(fNotify)) {
907  link->Clear();
908  }
909  if (fAllocationCount && (gDebug > 0)) {
910  Info("TTree::~TTree", "For tree %s, allocation count is %u.", GetName(), fAllocationCount.load());
911 #ifdef R__TRACK_BASKET_ALLOC_TIME
912  Info("TTree::~TTree", "For tree %s, allocation time is %lluus.", GetName(), fAllocationTime.load());
913 #endif
914  }
915 
916  if (fDirectory) {
917  // We are in a directory, which may possibly be a file.
918  if (fDirectory->GetList()) {
919  // Remove us from the directory listing.
920  fDirectory->Remove(this);
921  }
922  //delete the file cache if it points to this Tree
924  MoveReadCache(file,0);
925  }
926  // We don't own the leaves in fLeaves, the branches do.
927  fLeaves.Clear();
928  // I'm ready to destroy any objects allocated by
929  // SetAddress() by my branches. If I have clones,
930  // tell them to zero their pointers to this shared
931  // memory.
932  if (fClones && fClones->GetEntries()) {
933  // I have clones.
934  // I am about to delete the objects created by
935  // SetAddress() which we are sharing, so tell
936  // the clones to release their pointers to them.
937  for (TObjLink* lnk = fClones->FirstLink(); lnk; lnk = lnk->Next()) {
938  TTree* clone = (TTree*) lnk->GetObject();
939  // clone->ResetBranchAddresses();
940 
941  // Reset only the branch we have set the address of.
942  CopyAddresses(clone,kTRUE);
943  }
944  }
945  // Get rid of our branches, note that this will also release
946  // any memory allocated by TBranchElement::SetAddress().
947  fBranches.Delete();
948  // FIXME: We must consider what to do with the reset of these if we are a clone.
949  delete fPlayer;
950  fPlayer = 0;
951  if (fExternalFriends) {
952  using namespace ROOT::Detail;
954  fetree->Reset();
955  fExternalFriends->Clear("nodelete");
957  }
958  if (fFriends) {
959  fFriends->Delete();
960  delete fFriends;
961  fFriends = 0;
962  }
963  if (fAliases) {
964  fAliases->Delete();
965  delete fAliases;
966  fAliases = 0;
967  }
968  if (fUserInfo) {
969  fUserInfo->Delete();
970  delete fUserInfo;
971  fUserInfo = 0;
972  }
973  if (fClones) {
974  // Clone trees should no longer be removed from fClones when they are deleted.
975  {
977  gROOT->GetListOfCleanups()->Remove(fClones);
978  }
979  // Note: fClones does not own its content.
980  delete fClones;
981  fClones = 0;
982  }
983  if (fEntryList) {
985  // Delete the entry list if it is marked to be deleted and it is not also
986  // owned by a directory. (Otherwise we would need to make sure that a
987  // TDirectoryFile that has a TTree in it does a 'slow' TList::Delete.
988  delete fEntryList;
989  fEntryList=0;
990  }
991  }
992  delete fTreeIndex;
993  fTreeIndex = 0;
994  delete fBranchRef;
995  fBranchRef = 0;
996  delete [] fClusterRangeEnd;
997  fClusterRangeEnd = 0;
998  delete [] fClusterSize;
999  fClusterSize = 0;
1000  // Must be done after the destruction of friends.
1001  // Note: We do *not* own our directory.
1002  fDirectory = 0;
1003 
1004  if (fTransientBuffer) {
1005  delete fTransientBuffer;
1006  fTransientBuffer = 0;
1007  }
1008 }
1009 
1010 ////////////////////////////////////////////////////////////////////////////////
1011 /// Returns the transient buffer currently used by this TTree for reading/writing baskets.
1014 {
1015  if (fTransientBuffer) {
1016  if (fTransientBuffer->BufferSize() < size) {
1017  fTransientBuffer->Expand(size);
1018  }
1019  return fTransientBuffer;
1020  }
1022  return fTransientBuffer;
1023 }
1024 
1025 ////////////////////////////////////////////////////////////////////////////////
1026 /// Add branch with name bname to the Tree cache.
1027 /// If bname="*" all branches are added to the cache.
1028 /// if subbranches is true all the branches of the subbranches are
1029 /// also put to the cache.
1030 ///
1031 /// Returns:
1032 /// - 0 branch added or already included
1033 /// - -1 on error
1035 Int_t TTree::AddBranchToCache(const char*bname, Bool_t subbranches)
1036 {
1037  if (!GetTree()) {
1038  if (LoadTree(0)<0) {
1039  Error("AddBranchToCache","Could not load a tree");
1040  return -1;
1041  }
1042  }
1043  if (GetTree()) {
1044  if (GetTree() != this) {
1045  return GetTree()->AddBranchToCache(bname, subbranches);
1046  }
1047  } else {
1048  Error("AddBranchToCache", "No tree is available. Branch was not added to the cache");
1049  return -1;
1050  }
1051 
1052  TFile *f = GetCurrentFile();
1053  if (!f) {
1054  Error("AddBranchToCache", "No file is available. Branch was not added to the cache");
1055  return -1;
1056  }
1057  TTreeCache *tc = GetReadCache(f,kTRUE);
1058  if (!tc) {
1059  Error("AddBranchToCache", "No cache is available, branch not added");
1060  return -1;
1061  }
1062  return tc->AddBranch(bname,subbranches);
1063 }
1064 
1065 ////////////////////////////////////////////////////////////////////////////////
1066 /// Add branch b to the Tree cache.
1067 /// if subbranches is true all the branches of the subbranches are
1068 /// also put to the cache.
1069 ///
1070 /// Returns:
1071 /// - 0 branch added or already included
1072 /// - -1 on error
1075 {
1076  if (!GetTree()) {
1077  if (LoadTree(0)<0) {
1078  Error("AddBranchToCache","Could not load a tree");
1079  return -1;
1080  }
1081  }
1082  if (GetTree()) {
1083  if (GetTree() != this) {
1084  Int_t res = GetTree()->AddBranchToCache(b, subbranches);
1085  if (res<0) {
1086  Error("AddBranchToCache", "Error adding branch");
1087  }
1088  return res;
1089  }
1090  } else {
1091  Error("AddBranchToCache", "No tree is available. Branch was not added to the cache");
1092  return -1;
1093  }
1094 
1095  TFile *f = GetCurrentFile();
1096  if (!f) {
1097  Error("AddBranchToCache", "No file is available. Branch was not added to the cache");
1098  return -1;
1099  }
1100  TTreeCache *tc = GetReadCache(f,kTRUE);
1101  if (!tc) {
1102  Error("AddBranchToCache", "No cache is available, branch not added");
1103  return -1;
1104  }
1105  return tc->AddBranch(b,subbranches);
1106 }
1107 
1108 ////////////////////////////////////////////////////////////////////////////////
1109 /// Remove the branch with name 'bname' from the Tree cache.
1110 /// If bname="*" all branches are removed from the cache.
1111 /// if subbranches is true all the branches of the subbranches are
1112 /// also removed from the cache.
1113 ///
1114 /// Returns:
1115 /// - 0 branch dropped or not in cache
1116 /// - -1 on error
1118 Int_t TTree::DropBranchFromCache(const char*bname, Bool_t subbranches)
1119 {
1120  if (!GetTree()) {
1121  if (LoadTree(0)<0) {
1122  Error("DropBranchFromCache","Could not load a tree");
1123  return -1;
1124  }
1125  }
1126  if (GetTree()) {
1127  if (GetTree() != this) {
1128  return GetTree()->DropBranchFromCache(bname, subbranches);
1129  }
1130  } else {
1131  Error("DropBranchFromCache", "No tree is available. Branch was not dropped from the cache");
1132  return -1;
1133  }
1134 
1135  TFile *f = GetCurrentFile();
1136  if (!f) {
1137  Error("DropBranchFromCache", "No file is available. Branch was not dropped from the cache");
1138  return -1;
1139  }
1140  TTreeCache *tc = GetReadCache(f,kTRUE);
1141  if (!tc) {
1142  Error("DropBranchFromCache", "No cache is available, branch not dropped");
1143  return -1;
1144  }
1145  return tc->DropBranch(bname,subbranches);
1146 }
1147 
1148 ////////////////////////////////////////////////////////////////////////////////
1149 /// Remove the branch b from the Tree cache.
1150 /// if subbranches is true all the branches of the subbranches are
1151 /// also removed from the cache.
1152 ///
1153 /// Returns:
1154 /// - 0 branch dropped or not in cache
1155 /// - -1 on error
1158 {
1159  if (!GetTree()) {
1160  if (LoadTree(0)<0) {
1161  Error("DropBranchFromCache","Could not load a tree");
1162  return -1;
1163  }
1164  }
1165  if (GetTree()) {
1166  if (GetTree() != this) {
1167  Int_t res = GetTree()->DropBranchFromCache(b, subbranches);
1168  if (res<0) {
1169  Error("DropBranchFromCache", "Error dropping branch");
1170  }
1171  return res;
1172  }
1173  } else {
1174  Error("DropBranchFromCache", "No tree is available. Branch was not dropped from the cache");
1175  return -1;
1176  }
1177 
1178  TFile *f = GetCurrentFile();
1179  if (!f) {
1180  Error("DropBranchFromCache", "No file is available. Branch was not dropped from the cache");
1181  return -1;
1182  }
1183  TTreeCache *tc = GetReadCache(f,kTRUE);
1184  if (!tc) {
1185  Error("DropBranchFromCache", "No cache is available, branch not dropped");
1186  return -1;
1187  }
1188  return tc->DropBranch(b,subbranches);
1189 }
1190 
1191 ////////////////////////////////////////////////////////////////////////////////
1192 /// Add a cloned tree to our list of trees to be notified whenever we change
1193 /// our branch addresses or when we are deleted.
1195 void TTree::AddClone(TTree* clone)
1196 {
1197  if (!fClones) {
1198  fClones = new TList();
1199  fClones->SetOwner(false);
1200  // So that the clones are automatically removed from the list when
1201  // they are deleted.
1202  {
1204  gROOT->GetListOfCleanups()->Add(fClones);
1205  }
1206  }
1207  if (!fClones->FindObject(clone)) {
1208  fClones->Add(clone);
1209  }
1210 }
1211 
1212 // Check whether mainTree and friendTree can be friends w.r.t. the kEntriesReshuffled bit.
1213 // In particular, if any has the bit set, then friendTree must have a TTreeIndex and the
1214 // branches used for indexing must be present in mainTree.
1215 // Return true if the trees can be friends, false otherwise.
1216 bool CheckReshuffling(TTree &mainTree, TTree &friendTree)
1217 {
1218  const auto isMainReshuffled = mainTree.TestBit(TTree::kEntriesReshuffled);
1219  const auto isFriendReshuffled = friendTree.TestBit(TTree::kEntriesReshuffled);
1220  const auto friendHasValidIndex = [&] {
1221  auto idx = friendTree.GetTreeIndex();
1222  return idx ? idx->IsValidFor(&mainTree) : kFALSE;
1223  }();
1224 
1225  if ((isMainReshuffled || isFriendReshuffled) && !friendHasValidIndex) {
1226  const auto reshuffledTreeName = isMainReshuffled ? mainTree.GetName() : friendTree.GetName();
1227  const auto msg = "Tree '%s' has the kEntriesReshuffled bit set, and cannot be used as friend nor can be added as "
1228  "a friend unless the main tree has a TTreeIndex on the friend tree '%s'. You can also unset the "
1229  "bit manually if you know what you are doing.";
1230  Error("AddFriend", msg, reshuffledTreeName, friendTree.GetName());
1231  return false;
1232  }
1233  return true;
1234 }
1235 
1236 ////////////////////////////////////////////////////////////////////////////////
1237 /// Add a TFriendElement to the list of friends.
1238 ///
1239 /// This function:
1240 /// - opens a file if filename is specified
1241 /// - reads a Tree with name treename from the file (current directory)
1242 /// - adds the Tree to the list of friends
1243 /// see other AddFriend functions
1244 ///
1245 /// A TFriendElement TF describes a TTree object TF in a file.
1246 /// When a TFriendElement TF is added to the the list of friends of an
1247 /// existing TTree T, any variable from TF can be referenced in a query
1248 /// to T.
1249 ///
1250 /// A tree keeps a list of friends. In the context of a tree (or a chain),
1251 /// friendship means unrestricted access to the friends data. In this way
1252 /// it is much like adding another branch to the tree without taking the risk
1253 /// of damaging it. To add a friend to the list, you can use the TTree::AddFriend
1254 /// method. The tree in the diagram below has two friends (friend_tree1 and
1255 /// friend_tree2) and now has access to the variables a,b,c,i,j,k,l and m.
1256 ///
1257 /// \image html ttree_friend1.png
1258 ///
1259 /// The AddFriend method has two parameters, the first is the tree name and the
1260 /// second is the name of the ROOT file where the friend tree is saved.
1261 /// AddFriend automatically opens the friend file. If no file name is given,
1262 /// the tree called ft1 is assumed to be in the same file as the original tree.
1263 ///
1264 /// tree.AddFriend("ft1","friendfile1.root");
1265 /// If the friend tree has the same name as the original tree, you can give it
1266 /// an alias in the context of the friendship:
1267 ///
1268 /// tree.AddFriend("tree1 = tree","friendfile1.root");
1269 /// Once the tree has friends, we can use TTree::Draw as if the friend's
1270 /// variables were in the original tree. To specify which tree to use in
1271 /// the Draw method, use the syntax:
1272 /// ~~~ {.cpp}
1273 /// <treeName>.<branchname>.<varname>
1274 /// ~~~
1275 /// If the variablename is enough to uniquely identify the variable, you can
1276 /// leave out the tree and/or branch name.
1277 /// For example, these commands generate a 3-d scatter plot of variable "var"
1278 /// in the TTree tree versus variable v1 in TTree ft1 versus variable v2 in
1279 /// TTree ft2.
1280 /// ~~~ {.cpp}
1281 /// tree.AddFriend("ft1","friendfile1.root");
1282 /// tree.AddFriend("ft2","friendfile2.root");
1283 /// tree.Draw("var:ft1.v1:ft2.v2");
1284 /// ~~~
1285 /// \image html ttree_friend2.png
1286 ///
1287 /// The picture illustrates the access of the tree and its friends with a
1288 /// Draw command.
1289 /// When AddFriend is called, the ROOT file is automatically opened and the
1290 /// friend tree (ft1) is read into memory. The new friend (ft1) is added to
1291 /// the list of friends of tree.
1292 /// The number of entries in the friend must be equal or greater to the number
1293 /// of entries of the original tree. If the friend tree has fewer entries a
1294 /// warning is given and the missing entries are not included in the histogram.
1295 /// To retrieve the list of friends from a tree use TTree::GetListOfFriends.
1296 /// When the tree is written to file (TTree::Write), the friends list is saved
1297 /// with it. And when the tree is retrieved, the trees on the friends list are
1298 /// also retrieved and the friendship restored.
1299 /// When a tree is deleted, the elements of the friend list are also deleted.
1300 /// It is possible to declare a friend tree that has the same internal
1301 /// structure (same branches and leaves) as the original tree, and compare the
1302 /// same values by specifying the tree.
1303 /// ~~~ {.cpp}
1304 /// tree.Draw("var:ft1.var:ft2.var")
1305 /// ~~~
1307 TFriendElement *TTree::AddFriend(const char *treename, const char *filename)
1308 {
1309  if (!fFriends) {
1310  fFriends = new TList();
1311  }
1312  TFriendElement *fe = new TFriendElement(this, treename, filename);
1313 
1314  TTree *t = fe->GetTree();
1315  bool canAddFriend = true;
1316  if (t) {
1317  canAddFriend = CheckReshuffling(*this, *t);
1318  if (!t->GetTreeIndex() && (t->GetEntries() < fEntries)) {
1319  Warning("AddFriend", "FriendElement %s in file %s has less entries %lld than its parent Tree: %lld", treename,
1320  filename, t->GetEntries(), fEntries);
1321  }
1322  } else {
1323  Error("AddFriend", "Cannot find tree '%s' in file '%s', friend not added", treename, filename);
1324  canAddFriend = false;
1325  }
1326 
1327  if (canAddFriend)
1328  fFriends->Add(fe);
1329  return fe;
1330 }
1331 
1332 ////////////////////////////////////////////////////////////////////////////////
1333 /// Add a TFriendElement to the list of friends.
1334 ///
1335 /// The TFile is managed by the user (e.g. the user must delete the file).
1336 /// For complete description see AddFriend(const char *, const char *).
1337 /// This function:
1338 /// - reads a Tree with name treename from the file
1339 /// - adds the Tree to the list of friends
1341 TFriendElement *TTree::AddFriend(const char *treename, TFile *file)
1342 {
1343  if (!fFriends) {
1344  fFriends = new TList();
1345  }
1346  TFriendElement *fe = new TFriendElement(this, treename, file);
1347  R__ASSERT(fe);
1348  TTree *t = fe->GetTree();
1349  bool canAddFriend = true;
1350  if (t) {
1351  canAddFriend = CheckReshuffling(*this, *t);
1352  if (!t->GetTreeIndex() && (t->GetEntries() < fEntries)) {
1353  Warning("AddFriend", "FriendElement %s in file %s has less entries %lld than its parent tree: %lld", treename,
1354  file->GetName(), t->GetEntries(), fEntries);
1355  }
1356  } else {
1357  Error("AddFriend", "Cannot find tree '%s' in file '%s', friend not added", treename, file->GetName());
1358  canAddFriend = false;
1359  }
1360 
1361  if (canAddFriend)
1362  fFriends->Add(fe);
1363  return fe;
1364 }
1365 
1366 ////////////////////////////////////////////////////////////////////////////////
1367 /// Add a TFriendElement to the list of friends.
1368 ///
1369 /// The TTree is managed by the user (e.g., the user must delete the file).
1370 /// For a complete description see AddFriend(const char *, const char *).
1372 TFriendElement *TTree::AddFriend(TTree *tree, const char *alias, Bool_t warn)
1373 {
1374  if (!tree) {
1375  return 0;
1376  }
1377  if (!fFriends) {
1378  fFriends = new TList();
1379  }
1380  TFriendElement *fe = new TFriendElement(this, tree, alias);
1381  R__ASSERT(fe); // this assert is for historical reasons. Don't remove it unless you understand all the consequences.
1382  TTree *t = fe->GetTree();
1383  if (warn && (t->GetEntries() < fEntries)) {
1384  Warning("AddFriend", "FriendElement '%s' in file '%s' has less entries %lld than its parent tree: %lld",
1385  tree->GetName(), fe->GetFile() ? fe->GetFile()->GetName() : "(memory resident)", t->GetEntries(),
1386  fEntries);
1387  }
1388  if (CheckReshuffling(*this, *t)) {
1389  fFriends->Add(fe);
1390  tree->RegisterExternalFriend(fe);
1391  }
1392  return fe;
1393 }
1394 
1395 ////////////////////////////////////////////////////////////////////////////////
1396 /// AutoSave tree header every fAutoSave bytes.
1397 ///
1398 /// When large Trees are produced, it is safe to activate the AutoSave
1399 /// procedure. Some branches may have buffers holding many entries.
1400 /// If fAutoSave is negative, AutoSave is automatically called by
1401 /// TTree::Fill when the number of bytes generated since the previous
1402 /// AutoSave is greater than -fAutoSave bytes.
1403 /// If fAutoSave is positive, AutoSave is automatically called by
1404 /// TTree::Fill every N entries.
1405 /// This function may also be invoked by the user.
1406 /// Each AutoSave generates a new key on the file.
1407 /// Once the key with the tree header has been written, the previous cycle
1408 /// (if any) is deleted.
1409 ///
1410 /// Note that calling TTree::AutoSave too frequently (or similarly calling
1411 /// TTree::SetAutoSave with a small value) is an expensive operation.
1412 /// You should make tests for your own application to find a compromise
1413 /// between speed and the quantity of information you may loose in case of
1414 /// a job crash.
1415 ///
1416 /// In case your program crashes before closing the file holding this tree,
1417 /// the file will be automatically recovered when you will connect the file
1418 /// in UPDATE mode.
1419 /// The Tree will be recovered at the status corresponding to the last AutoSave.
1420 ///
1421 /// if option contains "SaveSelf", gDirectory->SaveSelf() is called.
1422 /// This allows another process to analyze the Tree while the Tree is being filled.
1423 ///
1424 /// if option contains "FlushBaskets", TTree::FlushBaskets is called and all
1425 /// the current basket are closed-out and written to disk individually.
1426 ///
1427 /// By default the previous header is deleted after having written the new header.
1428 /// if option contains "Overwrite", the previous Tree header is deleted
1429 /// before written the new header. This option is slightly faster, but
1430 /// the default option is safer in case of a problem (disk quota exceeded)
1431 /// when writing the new header.
1432 ///
1433 /// The function returns the number of bytes written to the file.
1434 /// if the number of bytes is null, an error has occurred while writing
1435 /// the header to the file.
1436 ///
1437 /// ## How to write a Tree in one process and view it from another process
1438 ///
1439 /// The following two scripts illustrate how to do this.
1440 /// The script treew.C is executed by process1, treer.C by process2
1441 ///
1442 /// script treew.C:
1443 /// ~~~ {.cpp}
1444 /// void treew() {
1445 /// TFile f("test.root","recreate");
1446 /// TNtuple *ntuple = new TNtuple("ntuple","Demo","px:py:pz:random:i");
1447 /// Float_t px, py, pz;
1448 /// for ( Int_t i=0; i<10000000; i++) {
1449 /// gRandom->Rannor(px,py);
1450 /// pz = px*px + py*py;
1451 /// Float_t random = gRandom->Rndm(1);
1452 /// ntuple->Fill(px,py,pz,random,i);
1453 /// if (i%1000 == 1) ntuple->AutoSave("SaveSelf");
1454 /// }
1455 /// }
1456 /// ~~~
1457 /// script treer.C:
1458 /// ~~~ {.cpp}
1459 /// void treer() {
1460 /// TFile f("test.root");
1461 /// TTree *ntuple = (TTree*)f.Get("ntuple");
1462 /// TCanvas c1;
1463 /// Int_t first = 0;
1464 /// while(1) {
1465 /// if (first == 0) ntuple->Draw("px>>hpx", "","",10000000,first);
1466 /// else ntuple->Draw("px>>+hpx","","",10000000,first);
1467 /// first = (Int_t)ntuple->GetEntries();
1468 /// c1.Update();
1469 /// gSystem->Sleep(1000); //sleep 1 second
1470 /// ntuple->Refresh();
1471 /// }
1472 /// }
1473 /// ~~~
1476 {
1477  if (!fDirectory || fDirectory == gROOT || !fDirectory->IsWritable()) return 0;
1478  if (gDebug > 0) {
1479  Info("AutoSave", "Tree:%s after %lld bytes written\n",GetName(),GetTotBytes());
1480  }
1481  TString opt = option;
1482  opt.ToLower();
1483 
1484  if (opt.Contains("flushbaskets")) {
1485  if (gDebug > 0) Info("AutoSave", "calling FlushBaskets \n");
1486  FlushBasketsImpl();
1487  }
1488 
1490 
1492  Long64_t nbytes;
1493  if (opt.Contains("overwrite")) {
1494  nbytes = fDirectory->WriteTObject(this,"","overwrite");
1495  } else {
1496  nbytes = fDirectory->WriteTObject(this); //nbytes will be 0 if Write failed (disk space exceeded)
1497  if (nbytes && key) {
1498  key->Delete();
1499  delete key;
1500  }
1501  }
1502  // save StreamerInfo
1503  TFile *file = fDirectory->GetFile();
1504  if (file) file->WriteStreamerInfo();
1505 
1506  if (opt.Contains("saveself")) {
1507  fDirectory->SaveSelf();
1508  //the following line is required in case GetUserInfo contains a user class
1509  //for which the StreamerInfo must be written. One could probably be a bit faster (Rene)
1510  if (file) file->WriteHeader();
1511  }
1512 
1513  return nbytes;
1514 }
1515 
1516 namespace {
1517  // This error message is repeated several times in the code. We write it once.
1518  const char* writeStlWithoutProxyMsg = "The class requested (%s) for the branch \"%s\""
1519  " is an instance of an stl collection and does not have a compiled CollectionProxy."
1520  " Please generate the dictionary for this collection (%s) to avoid to write corrupted data.";
1521 }
1522 
1523 ////////////////////////////////////////////////////////////////////////////////
1524 /// Same as TTree::Branch() with added check that addobj matches className.
1525 ///
1526 /// See TTree::Branch() for other details.
1527 ///
1529 TBranch* TTree::BranchImp(const char* branchname, const char* classname, TClass* ptrClass, void* addobj, Int_t bufsize, Int_t splitlevel)
1530 {
1531  TClass* claim = TClass::GetClass(classname);
1532  if (!ptrClass) {
1533  if (claim && claim->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(claim->GetCollectionProxy())) {
1534  Error("Branch", writeStlWithoutProxyMsg,
1535  claim->GetName(), branchname, claim->GetName());
1536  return 0;
1537  }
1538  return Branch(branchname, classname, (void*) addobj, bufsize, splitlevel);
1539  }
1540  TClass* actualClass = 0;
1541  void** addr = (void**) addobj;
1542  if (addr) {
1543  actualClass = ptrClass->GetActualClass(*addr);
1544  }
1545  if (ptrClass && claim) {
1546  if (!(claim->InheritsFrom(ptrClass) || ptrClass->InheritsFrom(claim))) {
1547  // Note we currently do not warn in case of splicing or over-expectation).
1548  if (claim->IsLoaded() && ptrClass->IsLoaded() && strcmp( claim->GetTypeInfo()->name(), ptrClass->GetTypeInfo()->name() ) == 0) {
1549  // The type is the same according to the C++ type_info, we must be in the case of
1550  // a template of Double32_t. This is actually a correct case.
1551  } else {
1552  Error("Branch", "The class requested (%s) for \"%s\" is different from the type of the pointer passed (%s)",
1553  claim->GetName(), branchname, ptrClass->GetName());
1554  }
1555  } else if (actualClass && (claim != actualClass) && !actualClass->InheritsFrom(claim)) {
1556  if (claim->IsLoaded() && actualClass->IsLoaded() && strcmp( claim->GetTypeInfo()->name(), actualClass->GetTypeInfo()->name() ) == 0) {
1557  // The type is the same according to the C++ type_info, we must be in the case of
1558  // a template of Double32_t. This is actually a correct case.
1559  } else {
1560  Error("Branch", "The actual class (%s) of the object provided for the definition of the branch \"%s\" does not inherit from %s",
1561  actualClass->GetName(), branchname, claim->GetName());
1562  }
1563  }
1564  }
1565  if (claim && claim->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(claim->GetCollectionProxy())) {
1566  Error("Branch", writeStlWithoutProxyMsg,
1567  claim->GetName(), branchname, claim->GetName());
1568  return 0;
1569  }
1570  return Branch(branchname, classname, (void*) addobj, bufsize, splitlevel);
1571 }
1572 
1573 ////////////////////////////////////////////////////////////////////////////////
1574 /// Same as TTree::Branch but automatic detection of the class name.
1575 /// See TTree::Branch for other details.
1577 TBranch* TTree::BranchImp(const char* branchname, TClass* ptrClass, void* addobj, Int_t bufsize, Int_t splitlevel)
1578 {
1579  if (!ptrClass) {
1580  Error("Branch", "The pointer specified for %s is not of a class known to ROOT", branchname);
1581  return 0;
1582  }
1583  TClass* actualClass = 0;
1584  void** addr = (void**) addobj;
1585  if (addr && *addr) {
1586  actualClass = ptrClass->GetActualClass(*addr);
1587  if (!actualClass) {
1588  Warning("Branch", "The actual TClass corresponding to the object provided for the definition of the branch \"%s\" is missing.\n\tThe object will be truncated down to its %s part",
1589  branchname, ptrClass->GetName());
1590  actualClass = ptrClass;
1591  } else if ((ptrClass != actualClass) && !actualClass->InheritsFrom(ptrClass)) {
1592  Error("Branch", "The actual class (%s) of the object provided for the definition of the branch \"%s\" does not inherit from %s", actualClass->GetName(), branchname, ptrClass->GetName());
1593  return 0;
1594  }
1595  } else {
1596  actualClass = ptrClass;
1597  }
1598  if (actualClass && actualClass->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(actualClass->GetCollectionProxy())) {
1599  Error("Branch", writeStlWithoutProxyMsg,
1600  actualClass->GetName(), branchname, actualClass->GetName());
1601  return 0;
1602  }
1603  return Branch(branchname, actualClass->GetName(), (void*) addobj, bufsize, splitlevel);
1604 }
1605 
1606 ////////////////////////////////////////////////////////////////////////////////
1607 /// Same as TTree::Branch but automatic detection of the class name.
1608 /// See TTree::Branch for other details.
1610 TBranch* TTree::BranchImpRef(const char* branchname, const char *classname, TClass* ptrClass, void *addobj, Int_t bufsize, Int_t splitlevel)
1611 {
1612  TClass* claim = TClass::GetClass(classname);
1613  if (!ptrClass) {
1614  if (claim && claim->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(claim->GetCollectionProxy())) {
1615  Error("Branch", writeStlWithoutProxyMsg,
1616  claim->GetName(), branchname, claim->GetName());
1617  return 0;
1618  } else if (claim == 0) {
1619  Error("Branch", "The pointer specified for %s is not of a class known to ROOT and %s is not a known class", branchname, classname);
1620  return 0;
1621  }
1622  ptrClass = claim;
1623  }
1624  TClass* actualClass = 0;
1625  if (!addobj) {
1626  Error("Branch", "Reference interface requires a valid object (for branch: %s)!", branchname);
1627  return 0;
1628  }
1629  actualClass = ptrClass->GetActualClass(addobj);
1630  if (ptrClass && claim) {
1631  if (!(claim->InheritsFrom(ptrClass) || ptrClass->InheritsFrom(claim))) {
1632  // Note we currently do not warn in case of splicing or over-expectation).
1633  if (claim->IsLoaded() && ptrClass->IsLoaded() && strcmp( claim->GetTypeInfo()->name(), ptrClass->GetTypeInfo()->name() ) == 0) {
1634  // The type is the same according to the C++ type_info, we must be in the case of
1635  // a template of Double32_t. This is actually a correct case.
1636  } else {
1637  Error("Branch", "The class requested (%s) for \"%s\" is different from the type of the object passed (%s)",
1638  claim->GetName(), branchname, ptrClass->GetName());
1639  }
1640  } else if (actualClass && (claim != actualClass) && !actualClass->InheritsFrom(claim)) {
1641  if (claim->IsLoaded() && actualClass->IsLoaded() && strcmp( claim->GetTypeInfo()->name(), actualClass->GetTypeInfo()->name() ) == 0) {
1642  // The type is the same according to the C++ type_info, we must be in the case of
1643  // a template of Double32_t. This is actually a correct case.
1644  } else {
1645  Error("Branch", "The actual class (%s) of the object provided for the definition of the branch \"%s\" does not inherit from %s",
1646  actualClass->GetName(), branchname, claim->GetName());
1647  }
1648  }
1649  }
1650  if (!actualClass) {
1651  Warning("Branch", "The actual TClass corresponding to the object provided for the definition of the branch \"%s\" is missing.\n\tThe object will be truncated down to its %s part",
1652  branchname, ptrClass->GetName());
1653  actualClass = ptrClass;
1654  } else if ((ptrClass != actualClass) && !actualClass->InheritsFrom(ptrClass)) {
1655  Error("Branch", "The actual class (%s) of the object provided for the definition of the branch \"%s\" does not inherit from %s", actualClass->GetName(), branchname, ptrClass->GetName());
1656  return 0;
1657  }
1658  if (actualClass && actualClass->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(actualClass->GetCollectionProxy())) {
1659  Error("Branch", writeStlWithoutProxyMsg,
1660  actualClass->GetName(), branchname, actualClass->GetName());
1661  return 0;
1662  }
1663  return BronchExec(branchname, actualClass->GetName(), (void*) addobj, kFALSE, bufsize, splitlevel);
1664 }
1665 
1666 ////////////////////////////////////////////////////////////////////////////////
1667 /// Same as TTree::Branch but automatic detection of the class name.
1668 /// See TTree::Branch for other details.
1670 TBranch* TTree::BranchImpRef(const char* branchname, TClass* ptrClass, EDataType datatype, void* addobj, Int_t bufsize, Int_t splitlevel)
1671 {
1672  if (!ptrClass) {
1673  if (datatype == kOther_t || datatype == kNoType_t) {
1674  Error("Branch", "The pointer specified for %s is not of a class or type known to ROOT", branchname);
1675  } else {
1676  TString varname; varname.Form("%s/%c",branchname,DataTypeToChar(datatype));
1677  return Branch(branchname,addobj,varname.Data(),bufsize);
1678  }
1679  return 0;
1680  }
1681  TClass* actualClass = 0;
1682  if (!addobj) {
1683  Error("Branch", "Reference interface requires a valid object (for branch: %s)!", branchname);
1684  return 0;
1685  }
1686  actualClass = ptrClass->GetActualClass(addobj);
1687  if (!actualClass) {
1688  Warning("Branch", "The actual TClass corresponding to the object provided for the definition of the branch \"%s\" is missing.\n\tThe object will be truncated down to its %s part",
1689  branchname, ptrClass->GetName());
1690  actualClass = ptrClass;
1691  } else if ((ptrClass != actualClass) && !actualClass->InheritsFrom(ptrClass)) {
1692  Error("Branch", "The actual class (%s) of the object provided for the definition of the branch \"%s\" does not inherit from %s", actualClass->GetName(), branchname, ptrClass->GetName());
1693  return 0;
1694  }
1695  if (actualClass && actualClass->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(actualClass->GetCollectionProxy())) {
1696  Error("Branch", writeStlWithoutProxyMsg,
1697  actualClass->GetName(), branchname, actualClass->GetName());
1698  return 0;
1699  }
1700  return BronchExec(branchname, actualClass->GetName(), (void*) addobj, kFALSE, bufsize, splitlevel);
1701 }
1702 
1703 ////////////////////////////////////////////////////////////////////////////////
1704 // Wrapper to turn Branch call with an std::array into the relevant leaf list
1705 // call
1706 TBranch *TTree::BranchImpArr(const char *branchname, EDataType datatype, std::size_t N, void *addobj, Int_t bufsize,
1707  Int_t /* splitlevel */)
1708 {
1709  if (datatype == kOther_t || datatype == kNoType_t) {
1710  Error("Branch",
1711  "The inner type of the std::array passed specified for %s is not of a class or type known to ROOT",
1712  branchname);
1713  } else {
1714  TString varname;
1715  varname.Form("%s[%d]/%c", branchname, (int)N, DataTypeToChar(datatype));
1716  return Branch(branchname, addobj, varname.Data(), bufsize);
1717  }
1718  return nullptr;
1719 }
1720 
1721 ////////////////////////////////////////////////////////////////////////////////
1722 /// Deprecated function. Use next function instead.
1724 Int_t TTree::Branch(TList* li, Int_t bufsize /* = 32000 */ , Int_t splitlevel /* = 99 */)
1725 {
1726  return Branch((TCollection*) li, bufsize, splitlevel);
1727 }
1728 
1729 ////////////////////////////////////////////////////////////////////////////////
1730 /// Create one branch for each element in the collection.
1731 ///
1732 /// Each entry in the collection becomes a top level branch if the
1733 /// corresponding class is not a collection. If it is a collection, the entry
1734 /// in the collection becomes in turn top level branches, etc.
1735 /// The splitlevel is decreased by 1 every time a new collection is found.
1736 /// For example if list is a TObjArray*
1737 /// - if splitlevel = 1, one top level branch is created for each element
1738 /// of the TObjArray.
1739 /// - if splitlevel = 2, one top level branch is created for each array element.
1740 /// if, in turn, one of the array elements is a TCollection, one top level
1741 /// branch will be created for each element of this collection.
1742 ///
1743 /// In case a collection element is a TClonesArray, the special Tree constructor
1744 /// for TClonesArray is called.
1745 /// The collection itself cannot be a TClonesArray.
1746 ///
1747 /// The function returns the total number of branches created.
1748 ///
1749 /// If name is given, all branch names will be prefixed with name_.
1750 ///
1751 /// IMPORTANT NOTE1: This function should not be called with splitlevel < 1.
1752 ///
1753 /// IMPORTANT NOTE2: The branches created by this function will have names
1754 /// corresponding to the collection or object names. It is important
1755 /// to give names to collections to avoid misleading branch names or
1756 /// identical branch names. By default collections have a name equal to
1757 /// the corresponding class name, e.g. the default name for a TList is "TList".
1758 ///
1759 /// And in general, in case two or more master branches contain subbranches
1760 /// with identical names, one must add a "." (dot) character at the end
1761 /// of the master branch name. This will force the name of the subbranches
1762 /// to be of the form `master.subbranch` instead of simply `subbranch`.
1763 /// This situation happens when the top level object
1764 /// has two or more members referencing the same class.
1765 /// For example, if a Tree has two branches B1 and B2 corresponding
1766 /// to objects of the same class MyClass, one can do:
1767 /// ~~~ {.cpp}
1768 /// tree.Branch("B1.","MyClass",&b1,8000,1);
1769 /// tree.Branch("B2.","MyClass",&b2,8000,1);
1770 /// ~~~
1771 /// if MyClass has 3 members a,b,c, the two instructions above will generate
1772 /// subbranches called B1.a, B1.b ,B1.c, B2.a, B2.b, B2.c
1773 ///
1774 /// Example:
1775 /// ~~~ {.cpp}
1776 /// {
1777 /// TTree T("T","test list");
1778 /// TList *list = new TList();
1779 ///
1780 /// TObjArray *a1 = new TObjArray();
1781 /// a1->SetName("a1");
1782 /// list->Add(a1);
1783 /// TH1F *ha1a = new TH1F("ha1a","ha1",100,0,1);
1784 /// TH1F *ha1b = new TH1F("ha1b","ha1",100,0,1);
1785 /// a1->Add(ha1a);
1786 /// a1->Add(ha1b);
1787 /// TObjArray *b1 = new TObjArray();
1788 /// b1->SetName("b1");
1789 /// list->Add(b1);
1790 /// TH1F *hb1a = new TH1F("hb1a","hb1",100,0,1);
1791 /// TH1F *hb1b = new TH1F("hb1b","hb1",100,0,1);
1792 /// b1->Add(hb1a);
1793 /// b1->Add(hb1b);
1794 ///
1795 /// TObjArray *a2 = new TObjArray();
1796 /// a2->SetName("a2");
1797 /// list->Add(a2);
1798 /// TH1S *ha2a = new TH1S("ha2a","ha2",100,0,1);
1799 /// TH1S *ha2b = new TH1S("ha2b","ha2",100,0,1);
1800 /// a2->Add(ha2a);
1801 /// a2->Add(ha2b);
1802 ///
1803 /// T.Branch(list,16000,2);
1804 /// T.Print();
1805 /// }
1806 /// ~~~
1808 Int_t TTree::Branch(TCollection* li, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 99 */, const char* name /* = "" */)
1809 {
1810 
1811  if (!li) {
1812  return 0;
1813  }
1814  TObject* obj = 0;
1815  Int_t nbranches = GetListOfBranches()->GetEntries();
1816  if (li->InheritsFrom(TClonesArray::Class())) {
1817  Error("Branch", "Cannot call this constructor for a TClonesArray");
1818  return 0;
1819  }
1820  Int_t nch = strlen(name);
1821  TString branchname;
1822  TIter next(li);
1823  while ((obj = next())) {
1824  if ((splitlevel > 1) && obj->InheritsFrom(TCollection::Class()) && !obj->InheritsFrom(TClonesArray::Class())) {
1825  TCollection* col = (TCollection*) obj;
1826  if (nch) {
1827  branchname.Form("%s_%s_", name, col->GetName());
1828  } else {
1829  branchname.Form("%s_", col->GetName());
1830  }
1831  Branch(col, bufsize, splitlevel - 1, branchname);
1832  } else {
1833  if (nch && (name[nch-1] == '_')) {
1834  branchname.Form("%s%s", name, obj->GetName());
1835  } else {
1836  if (nch) {
1837  branchname.Form("%s_%s", name, obj->GetName());
1838  } else {
1839  branchname.Form("%s", obj->GetName());
1840  }
1841  }
1842  if (splitlevel > 99) {
1843  branchname += ".";
1844  }
1845  Bronch(branchname, obj->ClassName(), li->GetObjectRef(obj), bufsize, splitlevel - 1);
1846  }
1847  }
1848  return GetListOfBranches()->GetEntries() - nbranches;
1849 }
1850 
1851 ////////////////////////////////////////////////////////////////////////////////
1852 /// Create one branch for each element in the folder.
1853 /// Returns the total number of branches created.
1855 Int_t TTree::Branch(const char* foldername, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 99 */)
1856 {
1857  TObject* ob = gROOT->FindObjectAny(foldername);
1858  if (!ob) {
1859  return 0;
1860  }
1861  if (ob->IsA() != TFolder::Class()) {
1862  return 0;
1863  }
1864  Int_t nbranches = GetListOfBranches()->GetEntries();
1865  TFolder* folder = (TFolder*) ob;
1866  TIter next(folder->GetListOfFolders());
1867  TObject* obj = 0;
1868  char* curname = new char[1000];
1869  char occur[20];
1870  while ((obj = next())) {
1871  snprintf(curname,1000, "%s/%s", foldername, obj->GetName());
1872  if (obj->IsA() == TFolder::Class()) {
1873  Branch(curname, bufsize, splitlevel - 1);
1874  } else {
1875  void* add = (void*) folder->GetListOfFolders()->GetObjectRef(obj);
1876  for (Int_t i = 0; i < 1000; ++i) {
1877  if (curname[i] == 0) {
1878  break;
1879  }
1880  if (curname[i] == '/') {
1881  curname[i] = '.';
1882  }
1883  }
1884  Int_t noccur = folder->Occurence(obj);
1885  if (noccur > 0) {
1886  snprintf(occur,20, "_%d", noccur);
1887  strlcat(curname, occur,1000);
1888  }
1889  TBranchElement* br = (TBranchElement*) Bronch(curname, obj->ClassName(), add, bufsize, splitlevel - 1);
1890  if (br) br->SetBranchFolder();
1891  }
1892  }
1893  delete[] curname;
1894  return GetListOfBranches()->GetEntries() - nbranches;
1895 }
1896 
1897 ////////////////////////////////////////////////////////////////////////////////
1898 /// Create a new TTree Branch.
1899 ///
1900 /// This Branch constructor is provided to support non-objects in
1901 /// a Tree. The variables described in leaflist may be simple
1902 /// variables or structures. // See the two following
1903 /// constructors for writing objects in a Tree.
1904 ///
1905 /// By default the branch buffers are stored in the same file as the Tree.
1906 /// use TBranch::SetFile to specify a different file
1907 ///
1908 /// * address is the address of the first item of a structure.
1909 /// * leaflist is the concatenation of all the variable names and types
1910 /// separated by a colon character :
1911 /// The variable name and the variable type are separated by a slash (/).
1912 /// The variable type may be 0,1 or 2 characters. If no type is given,
1913 /// the type of the variable is assumed to be the same as the previous
1914 /// variable. If the first variable does not have a type, it is assumed
1915 /// of type F by default. The list of currently supported types is given below:
1916 /// - `C` : a character string terminated by the 0 character
1917 /// - `B` : an 8 bit signed integer (`Char_t`)
1918 /// - `b` : an 8 bit unsigned integer (`UChar_t`)
1919 /// - `S` : a 16 bit signed integer (`Short_t`)
1920 /// - `s` : a 16 bit unsigned integer (`UShort_t`)
1921 /// - `I` : a 32 bit signed integer (`Int_t`)
1922 /// - `i` : a 32 bit unsigned integer (`UInt_t`)
1923 /// - `F` : a 32 bit floating point (`Float_t`)
1924 /// - `f` : a 24 bit floating point with truncated mantissa (`Float16_t`)
1925 /// - `D` : a 64 bit floating point (`Double_t`)
1926 /// - `d` : a 24 bit truncated floating point (`Double32_t`)
1927 /// - `L` : a 64 bit signed integer (`Long64_t`)
1928 /// - `l` : a 64 bit unsigned integer (`ULong64_t`)
1929 /// - `O` : [the letter `o`, not a zero] a boolean (`Bool_t`)
1930 ///
1931 /// Arrays of values are supported with the following syntax:
1932 /// - If leaf name has the form var[nelem], where nelem is alphanumeric, then
1933 /// if nelem is a leaf name, it is used as the variable size of the array,
1934 /// otherwise return 0.
1935 /// - If leaf name has the form var[nelem], where nelem is a non-negative integer, then
1936 /// it is used as the fixed size of the array.
1937 /// - If leaf name has the form of a multi-dimensional array (e.g. var[nelem][nelem2])
1938 /// where nelem and nelem2 are non-negative integer) then
1939 /// it is used as a 2 dimensional array of fixed size.
1940 /// - In case of the truncated floating point types (Float16_t and Double32_t) you can
1941 /// furthermore specify the range in the style [xmin,xmax] or [xmin,xmax,nbits] after
1942 /// the type character. See `TStreamerElement::GetRange()` for further information.
1943 ///
1944 /// Any of other form is not supported.
1945 ///
1946 /// Note that the TTree will assume that all the item are contiguous in memory.
1947 /// On some platform, this is not always true of the member of a struct or a class,
1948 /// due to padding and alignment. Sorting your data member in order of decreasing
1949 /// sizeof usually leads to their being contiguous in memory.
1950 ///
1951 /// * bufsize is the buffer size in bytes for this branch
1952 /// The default value is 32000 bytes and should be ok for most cases.
1953 /// You can specify a larger value (e.g. 256000) if your Tree is not split
1954 /// and each entry is large (Megabytes)
1955 /// A small value for bufsize is optimum if you intend to access
1956 /// the entries in the Tree randomly and your Tree is in split mode.
1958 TBranch* TTree::Branch(const char* name, void* address, const char* leaflist, Int_t bufsize /* = 32000 */)
1959 {
1960  TBranch* branch = new TBranch(this, name, address, leaflist, bufsize);
1961  if (branch->IsZombie()) {
1962  delete branch;
1963  branch = 0;
1964  return 0;
1965  }
1966  fBranches.Add(branch);
1967  return branch;
1968 }
1969 
1970 ////////////////////////////////////////////////////////////////////////////////
1971 /// Create a new branch with the object of class classname at address addobj.
1972 ///
1973 /// WARNING:
1974 ///
1975 /// Starting with Root version 3.01, the Branch function uses the new style
1976 /// branches (TBranchElement). To get the old behaviour, you can:
1977 /// - call BranchOld or
1978 /// - call TTree::SetBranchStyle(0)
1979 ///
1980 /// Note that with the new style, classname does not need to derive from TObject.
1981 /// It must derived from TObject if the branch style has been set to 0 (old)
1982 ///
1983 /// Note: See the comments in TBranchElement::SetAddress() for a more
1984 /// detailed discussion of the meaning of the addobj parameter in
1985 /// the case of new-style branches.
1986 ///
1987 /// Use splitlevel < 0 instead of splitlevel=0 when the class
1988 /// has a custom Streamer
1989 ///
1990 /// Note: if the split level is set to the default (99), TTree::Branch will
1991 /// not issue a warning if the class can not be split.
1993 TBranch* TTree::Branch(const char* name, const char* classname, void* addobj, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 99 */)
1994 {
1995  if (fgBranchStyle == 1) {
1996  return Bronch(name, classname, addobj, bufsize, splitlevel);
1997  } else {
1998  if (splitlevel < 0) {
1999  splitlevel = 0;
2000  }
2001  return BranchOld(name, classname, addobj, bufsize, splitlevel);
2002  }
2003 }
2004 
2005 ////////////////////////////////////////////////////////////////////////////////
2006 /// Create a new TTree BranchObject.
2007 ///
2008 /// Build a TBranchObject for an object of class classname.
2009 /// addobj is the address of a pointer to an object of class classname.
2010 /// IMPORTANT: classname must derive from TObject.
2011 /// The class dictionary must be available (ClassDef in class header).
2012 ///
2013 /// This option requires access to the library where the corresponding class
2014 /// is defined. Accessing one single data member in the object implies
2015 /// reading the full object.
2016 /// See the next Branch constructor for a more efficient storage
2017 /// in case the entry consists of arrays of identical objects.
2018 ///
2019 /// By default the branch buffers are stored in the same file as the Tree.
2020 /// use TBranch::SetFile to specify a different file
2021 ///
2022 /// IMPORTANT NOTE about branch names:
2023 ///
2024 /// And in general, in case two or more master branches contain subbranches
2025 /// with identical names, one must add a "." (dot) character at the end
2026 /// of the master branch name. This will force the name of the subbranches
2027 /// to be of the form `master.subbranch` instead of simply `subbranch`.
2028 /// This situation happens when the top level object
2029 /// has two or more members referencing the same class.
2030 /// For example, if a Tree has two branches B1 and B2 corresponding
2031 /// to objects of the same class MyClass, one can do:
2032 /// ~~~ {.cpp}
2033 /// tree.Branch("B1.","MyClass",&b1,8000,1);
2034 /// tree.Branch("B2.","MyClass",&b2,8000,1);
2035 /// ~~~
2036 /// if MyClass has 3 members a,b,c, the two instructions above will generate
2037 /// subbranches called B1.a, B1.b ,B1.c, B2.a, B2.b, B2.c
2038 ///
2039 /// bufsize is the buffer size in bytes for this branch
2040 /// The default value is 32000 bytes and should be ok for most cases.
2041 /// You can specify a larger value (e.g. 256000) if your Tree is not split
2042 /// and each entry is large (Megabytes)
2043 /// A small value for bufsize is optimum if you intend to access
2044 /// the entries in the Tree randomly and your Tree is in split mode.
2046 TBranch* TTree::BranchOld(const char* name, const char* classname, void* addobj, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 1 */)
2047 {
2048  TClass* cl = TClass::GetClass(classname);
2049  if (!cl) {
2050  Error("BranchOld", "Cannot find class: '%s'", classname);
2051  return 0;
2052  }
2053  if (!cl->IsTObject()) {
2054  if (fgBranchStyle == 0) {
2055  Fatal("BranchOld", "The requested class ('%s') does not inherit from TObject.\n"
2056  "\tfgBranchStyle is set to zero requesting by default to use BranchOld.\n"
2057  "\tIf this is intentional use Bronch instead of Branch or BranchOld.", classname);
2058  } else {
2059  Fatal("BranchOld", "The requested class ('%s') does not inherit from TObject.\n"
2060  "\tYou can not use BranchOld to store objects of this type.",classname);
2061  }
2062  return 0;
2063  }
2064  TBranch* branch = new TBranchObject(this, name, classname, addobj, bufsize, splitlevel);
2065  fBranches.Add(branch);
2066  if (!splitlevel) {
2067  return branch;
2068  }
2069  // We are going to fully split the class now.
2070  TObjArray* blist = branch->GetListOfBranches();
2071  const char* rdname = 0;
2072  const char* dname = 0;
2073  TString branchname;
2074  char** apointer = (char**) addobj;
2075  TObject* obj = (TObject*) *apointer;
2076  Bool_t delobj = kFALSE;
2077  if (!obj) {
2078  obj = (TObject*) cl->New();
2079  delobj = kTRUE;
2080  }
2081  // Build the StreamerInfo if first time for the class.
2082  BuildStreamerInfo(cl, obj);
2083  // Loop on all public data members of the class and its base classes.
2084  Int_t lenName = strlen(name);
2085  Int_t isDot = 0;
2086  if (name[lenName-1] == '.') {
2087  isDot = 1;
2088  }
2089  TBranch* branch1 = 0;
2090  TRealData* rd = 0;
2091  TRealData* rdi = 0;
2092  TIter nexti(cl->GetListOfRealData());
2093  TIter next(cl->GetListOfRealData());
2094  // Note: This loop results in a full split because the
2095  // real data list includes all data members of
2096  // data members.
2097  while ((rd = (TRealData*) next())) {
2098  if (rd->TestBit(TRealData::kTransient)) continue;
2099 
2100  // Loop over all data members creating branches for each one.
2101  TDataMember* dm = rd->GetDataMember();
2102  if (!dm->IsPersistent()) {
2103  // Do not process members with an "!" as the first character in the comment field.
2104  continue;
2105  }
2106  if (rd->IsObject()) {
2107  // We skip data members of class type.
2108  // But we do build their real data, their
2109  // streamer info, and write their streamer
2110  // info to the current directory's file.
2111  // Oh yes, and we also do this for all of
2112  // their base classes.
2113  TClass* clm = TClass::GetClass(dm->GetFullTypeName());
2114  if (clm) {
2115  BuildStreamerInfo(clm, (char*) obj + rd->GetThisOffset());
2116  }
2117  continue;
2118  }
2119  rdname = rd->GetName();
2120  dname = dm->GetName();
2121  if (cl->CanIgnoreTObjectStreamer()) {
2122  // Skip the TObject base class data members.
2123  // FIXME: This prevents a user from ever
2124  // using these names themself!
2125  if (!strcmp(dname, "fBits")) {
2126  continue;
2127  }
2128  if (!strcmp(dname, "fUniqueID")) {
2129  continue;
2130  }
2131  }
2132  TDataType* dtype = dm->GetDataType();
2133  Int_t code = 0;
2134  if (dtype) {
2135  code = dm->GetDataType()->GetType();
2136  }
2137  // Encode branch name. Use real data member name
2138  branchname = rdname;
2139  if (isDot) {
2140  if (dm->IsaPointer()) {
2141  // FIXME: This is wrong! The asterisk is not usually in the front!
2142  branchname.Form("%s%s", name, &rdname[1]);
2143  } else {
2144  branchname.Form("%s%s", name, &rdname[0]);
2145  }
2146  }
2147  // FIXME: Change this to a string stream.
2148  TString leaflist;
2149  Int_t offset = rd->GetThisOffset();
2150  char* pointer = ((char*) obj) + offset;
2151  if (dm->IsaPointer()) {
2152  // We have a pointer to an object or a pointer to an array of basic types.
2153  TClass* clobj = 0;
2154  if (!dm->IsBasic()) {
2155  clobj = TClass::GetClass(dm->GetTypeName());
2156  }
2157  if (clobj && clobj->InheritsFrom(TClonesArray::Class())) {
2158  // We have a pointer to a clones array.
2159  char* cpointer = (char*) pointer;
2160  char** ppointer = (char**) cpointer;
2161  TClonesArray* li = (TClonesArray*) *ppointer;
2162  if (splitlevel != 2) {
2163  if (isDot) {
2164  branch1 = new TBranchClones(branch,branchname, pointer, bufsize);
2165  } else {
2166  // FIXME: This is wrong! The asterisk is not usually in the front!
2167  branch1 = new TBranchClones(branch,&branchname.Data()[1], pointer, bufsize);
2168  }
2169  blist->Add(branch1);
2170  } else {
2171  if (isDot) {
2172  branch1 = new TBranchObject(branch, branchname, li->ClassName(), pointer, bufsize);
2173  } else {
2174  // FIXME: This is wrong! The asterisk is not usually in the front!
2175  branch1 = new TBranchObject(branch, &branchname.Data()[1], li->ClassName(), pointer, bufsize);
2176  }
2177  blist->Add(branch1);
2178  }
2179  } else if (clobj) {
2180  // We have a pointer to an object.
2181  //
2182  // It must be a TObject object.
2183  if (!clobj->IsTObject()) {
2184  continue;
2185  }
2186  branch1 = new TBranchObject(branch, dname, clobj->GetName(), pointer, bufsize, 0);
2187  if (isDot) {
2188  branch1->SetName(branchname);
2189  } else {
2190  // FIXME: This is wrong! The asterisk is not usually in the front!
2191  // Do not use the first character (*).
2192  branch1->SetName(&branchname.Data()[1]);
2193  }
2194  blist->Add(branch1);
2195  } else {
2196  // We have a pointer to an array of basic types.
2197  //
2198  // Check the comments in the text of the code for an index specification.
2199  const char* index = dm->GetArrayIndex();
2200  if (index[0]) {
2201  // We are a pointer to a varying length array of basic types.
2202  //check that index is a valid data member name
2203  //if member is part of an object (e.g. fA and index=fN)
2204  //index must be changed from fN to fA.fN
2205  TString aindex (rd->GetName());
2206  Ssiz_t rdot = aindex.Last('.');
2207  if (rdot>=0) {
2208  aindex.Remove(rdot+1);
2209  aindex.Append(index);
2210  }
2211  nexti.Reset();
2212  while ((rdi = (TRealData*) nexti())) {
2213  if (rdi->TestBit(TRealData::kTransient)) continue;
2214 
2215  if (!strcmp(rdi->GetName(), index)) {
2216  break;
2217  }
2218  if (!strcmp(rdi->GetName(), aindex)) {
2219  index = rdi->GetName();
2220  break;
2221  }
2222  }
2223 
2224  char vcode = DataTypeToChar((EDataType)code);
2225  // Note that we differentiate between strings and
2226  // char array by the fact that there is NO specified
2227  // size for a string (see next if (code == 1)
2228 
2229  if (vcode) {
2230  leaflist.Form("%s[%s]/%c", &rdname[0], index, vcode);
2231  } else {
2232  Error("BranchOld", "Cannot create branch for rdname: %s code: %d", branchname.Data(), code);
2233  leaflist = "";
2234  }
2235  } else {
2236  // We are possibly a character string.
2237  if (code == 1) {
2238  // We are a character string.
2239  leaflist.Form("%s/%s", dname, "C");
2240  } else {
2241  // Invalid array specification.
2242  // FIXME: We need an error message here.
2243  continue;
2244  }
2245  }
2246  // There are '*' in both the branchname and leaflist, remove them.
2247  TString bname( branchname );
2248  bname.ReplaceAll("*","");
2249  leaflist.ReplaceAll("*","");
2250  // Add the branch to the tree and indicate that the address
2251  // is that of a pointer to be dereferenced before using.
2252  branch1 = new TBranch(branch, bname, *((void**) pointer), leaflist, bufsize);
2253  TLeaf* leaf = (TLeaf*) branch1->GetListOfLeaves()->At(0);
2255  leaf->SetAddress((void**) pointer);
2256  blist->Add(branch1);
2257  }
2258  } else if (dm->IsBasic()) {
2259  // We have a basic type.
2260 
2261  char vcode = DataTypeToChar((EDataType)code);
2262  if (vcode) {
2263  leaflist.Form("%s/%c", rdname, vcode);
2264  } else {
2265  Error("BranchOld", "Cannot create branch for rdname: %s code: %d", branchname.Data(), code);
2266  leaflist = "";
2267  }
2268  branch1 = new TBranch(branch, branchname, pointer, leaflist, bufsize);
2269  branch1->SetTitle(rdname);
2270  blist->Add(branch1);
2271  } else {
2272  // We have a class type.
2273  // Note: This cannot happen due to the rd->IsObject() test above.
2274  // FIXME: Put an error message here just in case.
2275  }
2276  if (branch1) {
2277  branch1->SetOffset(offset);
2278  } else {
2279  Warning("BranchOld", "Cannot process member: '%s'", rdname);
2280  }
2281  }
2282  if (delobj) {
2283  delete obj;
2284  obj = 0;
2285  }
2286  return branch;
2287 }
2288 
2289 ////////////////////////////////////////////////////////////////////////////////
2290 /// Build the optional branch supporting the TRefTable.
2291 /// This branch will keep all the information to find the branches
2292 /// containing referenced objects.
2293 ///
2294 /// At each Tree::Fill, the branch numbers containing the
2295 /// referenced objects are saved to the TBranchRef basket.
2296 /// When the Tree header is saved (via TTree::Write), the branch
2297 /// is saved keeping the information with the pointers to the branches
2298 /// having referenced objects.
2301 {
2302  if (!fBranchRef) {
2303  fBranchRef = new TBranchRef(this);
2304  }
2305  return fBranchRef;
2306 }
2307 
2308 ////////////////////////////////////////////////////////////////////////////////
2309 /// Create a new TTree BranchElement.
2310 ///
2311 /// ## WARNING about this new function
2312 ///
2313 /// This function is designed to replace the internal
2314 /// implementation of the old TTree::Branch (whose implementation
2315 /// has been moved to BranchOld).
2316 ///
2317 /// NOTE: The 'Bronch' method supports only one possible calls
2318 /// signature (where the object type has to be specified
2319 /// explicitly and the address must be the address of a pointer).
2320 /// For more flexibility use 'Branch'. Use Bronch only in (rare)
2321 /// cases (likely to be legacy cases) where both the new and old
2322 /// implementation of Branch needs to be used at the same time.
2323 ///
2324 /// This function is far more powerful than the old Branch
2325 /// function. It supports the full C++, including STL and has
2326 /// the same behaviour in split or non-split mode. classname does
2327 /// not have to derive from TObject. The function is based on
2328 /// the new TStreamerInfo.
2329 ///
2330 /// Build a TBranchElement for an object of class classname.
2331 ///
2332 /// addr is the address of a pointer to an object of class
2333 /// classname. The class dictionary must be available (ClassDef
2334 /// in class header).
2335 ///
2336 /// Note: See the comments in TBranchElement::SetAddress() for a more
2337 /// detailed discussion of the meaning of the addr parameter.
2338 ///
2339 /// This option requires access to the library where the
2340 /// corresponding class is defined. Accessing one single data
2341 /// member in the object implies reading the full object.
2342 ///
2343 /// By default the branch buffers are stored in the same file as the Tree.
2344 /// use TBranch::SetFile to specify a different file
2345 ///
2346 /// IMPORTANT NOTE about branch names:
2347 ///
2348 /// And in general, in case two or more master branches contain subbranches
2349 /// with identical names, one must add a "." (dot) character at the end
2350 /// of the master branch name. This will force the name of the subbranches
2351 /// to be of the form `master.subbranch` instead of simply `subbranch`.
2352 /// This situation happens when the top level object
2353 /// has two or more members referencing the same class.
2354 /// For example, if a Tree has two branches B1 and B2 corresponding
2355 /// to objects of the same class MyClass, one can do:
2356 /// ~~~ {.cpp}
2357 /// tree.Branch("B1.","MyClass",&b1,8000,1);
2358 /// tree.Branch("B2.","MyClass",&b2,8000,1);
2359 /// ~~~
2360 /// if MyClass has 3 members a,b,c, the two instructions above will generate
2361 /// subbranches called B1.a, B1.b ,B1.c, B2.a, B2.b, B2.c
2362 ///
2363 /// bufsize is the buffer size in bytes for this branch
2364 /// The default value is 32000 bytes and should be ok for most cases.
2365 /// You can specify a larger value (e.g. 256000) if your Tree is not split
2366 /// and each entry is large (Megabytes)
2367 /// A small value for bufsize is optimum if you intend to access
2368 /// the entries in the Tree randomly and your Tree is in split mode.
2369 ///
2370 /// Use splitlevel < 0 instead of splitlevel=0 when the class
2371 /// has a custom Streamer
2372 ///
2373 /// Note: if the split level is set to the default (99), TTree::Branch will
2374 /// not issue a warning if the class can not be split.
2376 TBranch* TTree::Bronch(const char* name, const char* classname, void* addr, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 99 */)
2377 {
2378  return BronchExec(name, classname, addr, kTRUE, bufsize, splitlevel);
2379 }
2380 
2381 ////////////////////////////////////////////////////////////////////////////////
2382 /// Helper function implementing TTree::Bronch and TTree::Branch(const char *name, T &obj);
2384 TBranch* TTree::BronchExec(const char* name, const char* classname, void* addr, Bool_t isptrptr, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 99 */)
2385 {
2386  TClass* cl = TClass::GetClass(classname);
2387  if (!cl) {
2388  Error("Bronch", "Cannot find class:%s", classname);
2389  return 0;
2390  }
2391 
2392  //if splitlevel <= 0 and class has a custom Streamer, we must create
2393  //a TBranchObject. We cannot assume that TClass::ReadBuffer is consistent
2394  //with the custom Streamer. The penalty is that one cannot process
2395  //this Tree without the class library containing the class.
2396 
2397  char* objptr = 0;
2398  if (!isptrptr) {
2399  objptr = (char*)addr;
2400  } else if (addr) {
2401  objptr = *((char**) addr);
2402  }
2403 
2404  if (cl == TClonesArray::Class()) {
2405  TClonesArray* clones = (TClonesArray*) objptr;
2406  if (!clones) {
2407  Error("Bronch", "Pointer to TClonesArray is null");
2408  return 0;
2409  }
2410  if (!clones->GetClass()) {
2411  Error("Bronch", "TClonesArray with no class defined in branch: %s", name);
2412  return 0;
2413  }
2414  if (!clones->GetClass()->HasDataMemberInfo()) {
2415  Error("Bronch", "TClonesArray with no dictionary defined in branch: %s", name);
2416  return 0;
2417  }
2418  bool hasCustomStreamer = clones->GetClass()->TestBit(TClass::kHasCustomStreamerMember);
2419  if (splitlevel > 0) {
2420  if (hasCustomStreamer)
2421  Warning("Bronch", "Using split mode on a class: %s with a custom Streamer", clones->GetClass()->GetName());
2422  } else {
2423  if (hasCustomStreamer) clones->BypassStreamer(kFALSE);
2424  TBranchObject *branch = new TBranchObject(this,name,classname,addr,bufsize,0,/*compress=*/ -1,isptrptr);
2425  fBranches.Add(branch);
2426  return branch;
2427  }
2428  }
2429 
2430  if (cl->GetCollectionProxy()) {
2431  TVirtualCollectionProxy* collProxy = cl->GetCollectionProxy();
2432  //if (!collProxy) {
2433  // Error("Bronch", "%s is missing its CollectionProxy (for branch %s)", classname, name);
2434  //}
2435  TClass* inklass = collProxy->GetValueClass();
2436  if (!inklass && (collProxy->GetType() == 0)) {
2437  Error("Bronch", "%s with no class defined in branch: %s", classname, name);
2438  return 0;
2439  }
2440  if ((splitlevel > 0) && inklass && (inklass->GetCollectionProxy() == 0)) {
2441  ROOT::ESTLType stl = cl->GetCollectionType();
2442  if ((stl != ROOT::kSTLmap) && (stl != ROOT::kSTLmultimap)) {
2443  if (!inklass->HasDataMemberInfo()) {
2444  Error("Bronch", "Container with no dictionary defined in branch: %s", name);
2445  return 0;
2446  }
2447  if (inklass->TestBit(TClass::kHasCustomStreamerMember)) {
2448  Warning("Bronch", "Using split mode on a class: %s with a custom Streamer", inklass->GetName());
2449  }
2450  }
2451  }
2452  //-------------------------------------------------------------------------
2453  // If the splitting switch is enabled, the split level is big enough and
2454  // the collection contains pointers we can split it
2455  //////////////////////////////////////////////////////////////////////////
2456 
2457  TBranch *branch;
2458  if( splitlevel > kSplitCollectionOfPointers && collProxy->HasPointers() )
2459  branch = new TBranchSTL( this, name, collProxy, bufsize, splitlevel );
2460  else
2461  branch = new TBranchElement(this, name, collProxy, bufsize, splitlevel);
2462  fBranches.Add(branch);
2463  if (isptrptr) {
2464  branch->SetAddress(addr);
2465  } else {
2466  branch->SetObject(addr);
2467  }
2468  return branch;
2469  }
2470 
2471  Bool_t hasCustomStreamer = kFALSE;
2472  if (!cl->HasDataMemberInfo() && !cl->GetCollectionProxy()) {
2473  Error("Bronch", "Cannot find dictionary for class: %s", classname);
2474  return 0;
2475  }
2476 
2478  // Not an STL container and the linkdef file had a "-" after the class name.
2479  hasCustomStreamer = kTRUE;
2480  }
2481 
2482  if (splitlevel < 0 || ((splitlevel == 0) && hasCustomStreamer && cl->IsTObject())) {
2483  TBranchObject* branch = new TBranchObject(this, name, classname, addr, bufsize, 0, /*compress=*/ ROOT::RCompressionSetting::EAlgorithm::kInherit, isptrptr);
2484  fBranches.Add(branch);
2485  return branch;
2486  }
2487 
2488  if (cl == TClonesArray::Class()) {
2489  // Special case of TClonesArray.
2490  // No dummy object is created.
2491  // The streamer info is not rebuilt unoptimized.
2492  // No dummy top-level branch is created.
2493  // No splitting is attempted.
2494  TBranchElement* branch = new TBranchElement(this, name, (TClonesArray*) objptr, bufsize, splitlevel%kSplitCollectionOfPointers);
2495  fBranches.Add(branch);
2496  if (isptrptr) {
2497  branch->SetAddress(addr);
2498  } else {
2499  branch->SetObject(addr);
2500  }
2501  return branch;
2502  }
2503 
2504  //
2505  // If we are not given an object to use as an i/o buffer
2506  // then create a temporary one which we will delete just
2507  // before returning.
2508  //
2509 
2510  Bool_t delobj = kFALSE;
2511 
2512  if (!objptr) {
2513  objptr = (char*) cl->New();
2514  delobj = kTRUE;
2515  }
2516 
2517  //
2518  // Avoid splitting unsplittable classes.
2519  //
2520 
2521  if ((splitlevel > 0) && !cl->CanSplit()) {
2522  if (splitlevel != 99) {
2523  Warning("Bronch", "%s cannot be split, resetting splitlevel to 0", cl->GetName());
2524  }
2525  splitlevel = 0;
2526  }
2527 
2528  //
2529  // Make sure the streamer info is built and fetch it.
2530  //
2531  // If we are splitting, then make sure the streamer info
2532  // is built unoptimized (data members are not combined).
2533  //
2534 
2535  TStreamerInfo* sinfo = BuildStreamerInfo(cl, objptr, splitlevel==0);
2536  if (!sinfo) {
2537  Error("Bronch", "Cannot build the StreamerInfo for class: %s", cl->GetName());
2538  return 0;
2539  }
2540 
2541  //
2542  // Create a dummy top level branch object.
2543  //
2544 
2545  Int_t id = -1;
2546  if (splitlevel > 0) {
2547  id = -2;
2548  }
2549  TBranchElement* branch = new TBranchElement(this, name, sinfo, id, objptr, bufsize, splitlevel);
2550  fBranches.Add(branch);
2551 
2552  //
2553  // Do splitting, if requested.
2554  //
2555 
2556  if (splitlevel%kSplitCollectionOfPointers > 0) {
2557  branch->Unroll(name, cl, sinfo, objptr, bufsize, splitlevel);
2558  }
2559 
2560  //
2561  // Setup our offsets into the user's i/o buffer.
2562  //
2563 
2564  if (isptrptr) {
2565  branch->SetAddress(addr);
2566  } else {
2567  branch->SetObject(addr);
2568  }
2569 
2570  if (delobj) {
2571  cl->Destructor(objptr);
2572  objptr = 0;
2573  }
2574 
2575  return branch;
2576 }
2577 
2578 ////////////////////////////////////////////////////////////////////////////////
2579 /// Browse content of the TTree.
2581 void TTree::Browse(TBrowser* b)
2582 {
2583  fBranches.Browse(b);
2584  if (fUserInfo) {
2585  if (strcmp("TList",fUserInfo->GetName())==0) {
2586  fUserInfo->SetName("UserInfo");
2587  b->Add(fUserInfo);
2588  fUserInfo->SetName("TList");
2589  } else {
2590  b->Add(fUserInfo);
2591  }
2592  }
2593 }
2594 
2595 ////////////////////////////////////////////////////////////////////////////////
2596 /// Build a Tree Index (default is TTreeIndex).
2597 /// See a description of the parameters and functionality in
2598 /// TTreeIndex::TTreeIndex().
2599 ///
2600 /// The return value is the number of entries in the Index (< 0 indicates failure).
2601 ///
2602 /// A TTreeIndex object pointed by fTreeIndex is created.
2603 /// This object will be automatically deleted by the TTree destructor.
2604 /// If an index is already existing, this is replaced by the new one without being
2605 /// deleted. This behaviour prevents the deletion of a previously external index
2606 /// assigned to the TTree via the TTree::SetTreeIndex() method.
2607 /// See also comments in TTree::SetTreeIndex().
2609 Int_t TTree::BuildIndex(const char* majorname, const char* minorname /* = "0" */)
2610 {
2611  fTreeIndex = GetPlayer()->BuildIndex(this, majorname, minorname);
2612  if (fTreeIndex->IsZombie()) {
2613  delete fTreeIndex;
2614  fTreeIndex = 0;
2615  return 0;
2616  }
2617  return fTreeIndex->GetN();
2618 }
2619 
2620 ////////////////////////////////////////////////////////////////////////////////
2621 /// Build StreamerInfo for class cl.
2622 /// pointer is an optional argument that may contain a pointer to an object of cl.
2624 TStreamerInfo* TTree::BuildStreamerInfo(TClass* cl, void* pointer /* = 0 */, Bool_t canOptimize /* = kTRUE */ )
2625 {
2626  if (!cl) {
2627  return 0;
2628  }
2629  cl->BuildRealData(pointer);
2631 
2632  // Create StreamerInfo for all base classes.
2633  TBaseClass* base = 0;
2634  TIter nextb(cl->GetListOfBases());
2635  while((base = (TBaseClass*) nextb())) {
2636  if (base->IsSTLContainer()) {
2637  continue;
2638  }
2639  TClass* clm = TClass::GetClass(base->GetName());
2640  BuildStreamerInfo(clm, pointer, canOptimize);
2641  }
2642  if (sinfo && fDirectory) {
2643  sinfo->ForceWriteInfo(fDirectory->GetFile());
2644  }
2645  return sinfo;
2646 }
2647 
2648 ////////////////////////////////////////////////////////////////////////////////
2649 /// Called by TTree::Fill() when file has reached its maximum fgMaxTreeSize.
2650 /// Create a new file. If the original file is named "myfile.root",
2651 /// subsequent files are named "myfile_1.root", "myfile_2.root", etc.
2652 ///
2653 /// Returns a pointer to the new file.
2654 ///
2655 /// Currently, the automatic change of file is restricted
2656 /// to the case where the tree is in the top level directory.
2657 /// The file should not contain sub-directories.
2658 ///
2659 /// Before switching to a new file, the tree header is written
2660 /// to the current file, then the current file is closed.
2661 ///
2662 /// To process the multiple files created by ChangeFile, one must use
2663 /// a TChain.
2664 ///
2665 /// The new file name has a suffix "_N" where N is equal to fFileNumber+1.
2666 /// By default a Root session starts with fFileNumber=0. One can set
2667 /// fFileNumber to a different value via TTree::SetFileNumber.
2668 /// In case a file named "_N" already exists, the function will try
2669 /// a file named "__N", then "___N", etc.
2670 ///
2671 /// fgMaxTreeSize can be set via the static function TTree::SetMaxTreeSize.
2672 /// The default value of fgMaxTreeSize is 100 Gigabytes.
2673 ///
2674 /// If the current file contains other objects like TH1 and TTree,
2675 /// these objects are automatically moved to the new file.
2676 ///
2677 /// IMPORTANT NOTE:
2678 ///
2679 /// Be careful when writing the final Tree header to the file!
2680 ///
2681 /// Don't do:
2682 /// ~~~ {.cpp}
2683 /// TFile *file = new TFile("myfile.root","recreate");
2684 /// TTree *T = new TTree("T","title");
2685 /// T->Fill(); //loop
2686 /// file->Write();
2687 /// file->Close();
2688 /// ~~~
2689 /// but do the following:
2690 /// ~~~ {.cpp}
2691 /// TFile *file = new TFile("myfile.root","recreate");
2692 /// TTree *T = new TTree("T","title");
2693 /// T->Fill(); //loop
2694 /// file = T->GetCurrentFile(); //to get the pointer to the current file
2695 /// file->Write();
2696 /// file->Close();
2697 /// ~~~
2700 {
2701  file->cd();
2702  Write();
2703  Reset();
2704  constexpr auto kBufSize = 2000;
2705  char* fname = new char[kBufSize];
2706  ++fFileNumber;
2707  char uscore[10];
2708  for (Int_t i = 0; i < 10; ++i) {
2709  uscore[i] = 0;
2710  }
2711  Int_t nus = 0;
2712  // Try to find a suitable file name that does not already exist.
2713  while (nus < 10) {
2714  uscore[nus] = '_';
2715  fname[0] = 0;
2716  strlcpy(fname, file->GetName(), kBufSize);
2717 
2718  if (fFileNumber > 1) {
2719  char* cunder = strrchr(fname, '_');
2720  if (cunder) {
2721  snprintf(cunder, kBufSize - Int_t(cunder - fname), "%s%d", uscore, fFileNumber);
2722  const char* cdot = strrchr(file->GetName(), '.');
2723  if (cdot) {
2724  strlcat(fname, cdot, kBufSize);
2725  }
2726  } else {
2727  char fcount[21];
2728  snprintf(fcount,21, "%s%d", uscore, fFileNumber);
2729  strlcat(fname, fcount, kBufSize);
2730  }
2731  } else {
2732  char* cdot = strrchr(fname, '.');
2733  if (cdot) {
2734  snprintf(cdot, kBufSize - Int_t(fname-cdot), "%s%d", uscore, fFileNumber);
2735  strlcat(fname, strrchr(file->GetName(), '.'), kBufSize);
2736  } else {
2737  char fcount[21];
2738  snprintf(fcount,21, "%s%d", uscore, fFileNumber);
2739  strlcat(fname, fcount, kBufSize);
2740  }
2741  }
2742  if (gSystem->AccessPathName(fname)) {
2743  break;
2744  }
2745  ++nus;
2746  Warning("ChangeFile", "file %s already exist, trying with %d underscores", fname, nus+1);
2747  }
2748  Int_t compress = file->GetCompressionSettings();
2749  TFile* newfile = TFile::Open(fname, "recreate", "chain files", compress);
2750  if (newfile == 0) {
2751  Error("Fill","Failed to open new file %s, continuing as a memory tree.",fname);
2752  } else {
2753  Printf("Fill: Switching to new file: %s", fname);
2754  }
2755  // The current directory may contain histograms and trees.
2756  // These objects must be moved to the new file.
2757  TBranch* branch = 0;
2758  TObject* obj = 0;
2759  while ((obj = file->GetList()->First())) {
2760  file->Remove(obj);
2761  // Histogram: just change the directory.
2762  if (obj->InheritsFrom("TH1")) {
2763  gROOT->ProcessLine(TString::Format("((%s*)0x%lx)->SetDirectory((TDirectory*)0x%lx);", obj->ClassName(), (Long_t) obj, (Long_t) newfile));
2764  continue;
2765  }
2766  // Tree: must save all trees in the old file, reset them.
2767  if (obj->InheritsFrom(TTree::Class())) {
2768  TTree* t = (TTree*) obj;
2769  if (t != this) {
2770  t->AutoSave();
2771  t->Reset();
2772  t->fFileNumber = fFileNumber;
2773  }
2774  t->SetDirectory(newfile);
2775  TIter nextb(t->GetListOfBranches());
2776  while ((branch = (TBranch*)nextb())) {
2777  branch->SetFile(newfile);
2778  }
2779  if (t->GetBranchRef()) {
2780  t->GetBranchRef()->SetFile(newfile);
2781  }
2782  continue;
2783  }
2784  // Not a TH1 or a TTree, move object to new file.
2785  if (newfile) newfile->Append(obj);
2786  file->Remove(obj);
2787  }
2788  delete file;
2789  file = 0;
2790  delete[] fname;
2791  fname = 0;
2792  return newfile;
2793 }
2794 
2795 ////////////////////////////////////////////////////////////////////////////////
2796 /// Check whether or not the address described by the last 3 parameters
2797 /// matches the content of the branch. If a Data Model Evolution conversion
2798 /// is involved, reset the fInfo of the branch.
2799 /// The return values are:
2800 //
2801 /// - kMissingBranch (-5) : Missing branch
2802 /// - kInternalError (-4) : Internal error (could not find the type corresponding to a data type number)
2803 /// - kMissingCompiledCollectionProxy (-3) : Missing compiled collection proxy for a compiled collection
2804 /// - kMismatch (-2) : Non-Class Pointer type given does not match the type expected by the branch
2805 /// - kClassMismatch (-1) : Class Pointer type given does not match the type expected by the branch
2806 /// - kMatch (0) : perfect match
2807 /// - kMatchConversion (1) : match with (I/O) conversion
2808 /// - kMatchConversionCollection (2) : match with (I/O) conversion of the content of a collection
2809 /// - kMakeClass (3) : MakeClass mode so we can not check.
2810 /// - kVoidPtr (4) : void* passed so no check was made.
2811 /// - kNoCheck (5) : Underlying TBranch not yet available so no check was made.
2813 Int_t TTree::CheckBranchAddressType(TBranch* branch, TClass* ptrClass, EDataType datatype, Bool_t isptr)
2814 {
2815  if (GetMakeClass()) {
2816  // If we are in MakeClass mode so we do not really use classes.
2817  return kMakeClass;
2818  }
2819 
2820  // Let's determine what we need!
2821  TClass* expectedClass = 0;
2822  EDataType expectedType = kOther_t;
2823  if (0 != branch->GetExpectedType(expectedClass,expectedType) ) {
2824  // Something went wrong, the warning message has already be issued.
2825  return kInternalError;
2826  }
2827  if (expectedClass && datatype == kOther_t && ptrClass == 0) {
2828  if (branch->InheritsFrom( TBranchElement::Class() )) {
2829  TBranchElement* bEl = (TBranchElement*)branch;
2830  bEl->SetTargetClass( expectedClass->GetName() );
2831  }
2832  if (expectedClass && expectedClass->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(expectedClass->GetCollectionProxy())) {
2833  Error("SetBranchAddress", "Unable to determine the type given for the address for \"%s\". "
2834  "The class expected (%s) refers to an stl collection and do not have a compiled CollectionProxy. "
2835  "Please generate the dictionary for this class (%s)",
2836  branch->GetName(), expectedClass->GetName(), expectedClass->GetName());
2838  }
2839  if (!expectedClass->IsLoaded()) {
2840  // The originally expected class does not have a dictionary, it is then plausible that the pointer being passed is the right type
2841  // (we really don't know). So let's express that.
2842  Error("SetBranchAddress", "Unable to determine the type given for the address for \"%s\". "
2843  "The class expected (%s) does not have a dictionary and needs to be emulated for I/O purposes but is being passed a compiled object."
2844  "Please generate the dictionary for this class (%s)",
2845  branch->GetName(), expectedClass->GetName(), expectedClass->GetName());
2846  } else {
2847  Error("SetBranchAddress", "Unable to determine the type given for the address for \"%s\". "
2848  "This is probably due to a missing dictionary, the original data class for this branch is %s.", branch->GetName(), expectedClass->GetName());
2849  }
2850  return kClassMismatch;
2851  }
2852  if (expectedClass && ptrClass && (branch->GetMother() == branch)) {
2853  // Top Level branch
2854  if (!isptr) {
2855  Error("SetBranchAddress", "The address for \"%s\" should be the address of a pointer!", branch->GetName());
2856  }
2857  }
2858  if (expectedType == kFloat16_t) {
2859  expectedType = kFloat_t;
2860  }
2861  if (expectedType == kDouble32_t) {
2862  expectedType = kDouble_t;
2863  }
2864  if (datatype == kFloat16_t) {
2865  datatype = kFloat_t;
2866  }
2867  if (datatype == kDouble32_t) {
2868  datatype = kDouble_t;
2869  }
2870 
2871  /////////////////////////////////////////////////////////////////////////////
2872  // Deal with the class renaming
2873  /////////////////////////////////////////////////////////////////////////////
2874 
2875  if( expectedClass && ptrClass &&
2876  expectedClass != ptrClass &&
2877  branch->InheritsFrom( TBranchElement::Class() ) &&
2878  ptrClass->GetSchemaRules() &&
2879  ptrClass->GetSchemaRules()->HasRuleWithSourceClass( expectedClass->GetName() ) ) {
2880  TBranchElement* bEl = (TBranchElement*)branch;
2881 
2882  if ( ptrClass->GetCollectionProxy() && expectedClass->GetCollectionProxy() ) {
2883  if (gDebug > 7)
2884  Info("SetBranchAddress", "Matching STL collection (at least according to the SchemaRuleSet when "
2885  "reading a %s into a %s",expectedClass->GetName(),ptrClass->GetName());
2886 
2887  bEl->SetTargetClass( ptrClass->GetName() );
2888  return kMatchConversion;
2889 
2890  } else if ( !ptrClass->GetConversionStreamerInfo( expectedClass, bEl->GetClassVersion() ) &&
2891  !ptrClass->FindConversionStreamerInfo( expectedClass, bEl->GetCheckSum() ) ) {
2892  Error("SetBranchAddress", "The pointer type given \"%s\" does not correspond to the type needed \"%s\" by the branch: %s", ptrClass->GetName(), bEl->GetClassName(), branch->GetName());
2893 
2894  bEl->SetTargetClass( expectedClass->GetName() );
2895  return kClassMismatch;
2896  }
2897  else {
2898 
2899  bEl->SetTargetClass( ptrClass->GetName() );
2900  return kMatchConversion;
2901  }
2902 
2903  } else if (expectedClass && ptrClass && !expectedClass->InheritsFrom(ptrClass)) {
2904 
2905  if (expectedClass->GetCollectionProxy() && ptrClass->GetCollectionProxy() &&
2906  branch->InheritsFrom( TBranchElement::Class() ) &&
2907  expectedClass->GetCollectionProxy()->GetValueClass() &&
2908  ptrClass->GetCollectionProxy()->GetValueClass() )
2909  {
2910  // In case of collection, we know how to convert them, if we know how to convert their content.
2911  // NOTE: we need to extend this to std::pair ...
2912 
2913  TClass *onfileValueClass = expectedClass->GetCollectionProxy()->GetValueClass();
2914  TClass *inmemValueClass = ptrClass->GetCollectionProxy()->GetValueClass();
2915 
2916  if (inmemValueClass->GetSchemaRules() &&
2917  inmemValueClass->GetSchemaRules()->HasRuleWithSourceClass(onfileValueClass->GetName() ) )
2918  {
2919  TBranchElement* bEl = (TBranchElement*)branch;
2920  bEl->SetTargetClass( ptrClass->GetName() );
2922  }
2923  }
2924 
2925  Error("SetBranchAddress", "The pointer type given (%s) does not correspond to the class needed (%s) by the branch: %s", ptrClass->GetName(), expectedClass->GetName(), branch->GetName());
2926  if (branch->InheritsFrom( TBranchElement::Class() )) {
2927  TBranchElement* bEl = (TBranchElement*)branch;
2928  bEl->SetTargetClass( expectedClass->GetName() );
2929  }
2930  return kClassMismatch;
2931 
2932  } else if ((expectedType != kOther_t) && (datatype != kOther_t) && (expectedType != kNoType_t) && (datatype != kNoType_t) && (expectedType != datatype)) {
2933  if (datatype != kChar_t) {
2934  // For backward compatibility we assume that (char*) was just a cast and/or a generic address
2935  Error("SetBranchAddress", "The pointer type given \"%s\" (%d) does not correspond to the type needed \"%s\" (%d) by the branch: %s",
2936  TDataType::GetTypeName(datatype), datatype, TDataType::GetTypeName(expectedType), expectedType, branch->GetName());
2937  return kMismatch;
2938  }
2939  } else if ((expectedClass && (datatype != kOther_t && datatype != kNoType_t && datatype != kInt_t)) ||
2940  (ptrClass && (expectedType != kOther_t && expectedType != kNoType_t && datatype != kInt_t)) ) {
2941  // Sometime a null pointer can look an int, avoid complaining in that case.
2942  if (expectedClass) {
2943  Error("SetBranchAddress", "The pointer type given \"%s\" (%d) does not correspond to the type needed \"%s\" by the branch: %s",
2944  TDataType::GetTypeName(datatype), datatype, expectedClass->GetName(), branch->GetName());
2945  if (branch->InheritsFrom( TBranchElement::Class() )) {
2946  TBranchElement* bEl = (TBranchElement*)branch;
2947  bEl->SetTargetClass( expectedClass->GetName() );
2948  }
2949  } else {
2950  // In this case, it is okay if the first data member is of the right type (to support the case where we are being passed
2951  // a struct).
2952  bool found = false;
2953  if (ptrClass->IsLoaded()) {
2954  TIter next(ptrClass->GetListOfRealData());
2955  TRealData *rdm;
2956  while ((rdm = (TRealData*)next())) {
2957  if (rdm->GetThisOffset() == 0) {
2958  TDataType *dmtype = rdm->GetDataMember()->GetDataType();
2959  if (dmtype) {
2960  EDataType etype = (EDataType)dmtype->GetType();
2961  if (etype == expectedType) {
2962  found = true;
2963  }
2964  }
2965  break;
2966  }
2967  }
2968  } else {
2969  TIter next(ptrClass->GetListOfDataMembers());
2970  TDataMember *dm;
2971  while ((dm = (TDataMember*)next())) {
2972  if (dm->GetOffset() == 0) {
2973  TDataType *dmtype = dm->GetDataType();
2974  if (dmtype) {
2975  EDataType etype = (EDataType)dmtype->GetType();
2976  if (etype == expectedType) {
2977  found = true;
2978  }
2979  }
2980  break;
2981  }
2982  }
2983  }
2984  if (found) {
2985  // let's check the size.
2986  TLeaf *last = (TLeaf*)branch->GetListOfLeaves()->Last();
2987  long len = last->GetOffset() + last->GetLenType() * last->GetLen();
2988  if (len <= ptrClass->Size()) {
2989  return kMatch;
2990  }
2991  }
2992  Error("SetBranchAddress", "The pointer type given \"%s\" does not correspond to the type needed \"%s\" (%d) by the branch: %s",
2993  ptrClass->GetName(), TDataType::GetTypeName(expectedType), expectedType, branch->GetName());
2994  }
2995  return kMismatch;
2996  }
2997  if (expectedClass && expectedClass->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(expectedClass->GetCollectionProxy())) {
2998  Error("SetBranchAddress", writeStlWithoutProxyMsg,
2999  expectedClass->GetName(), branch->GetName(), expectedClass->GetName());
3000  if (branch->InheritsFrom( TBranchElement::Class() )) {
3001  TBranchElement* bEl = (TBranchElement*)branch;
3002  bEl->SetTargetClass( expectedClass->GetName() );
3003  }
3005  }
3006  if (expectedClass && branch->InheritsFrom( TBranchElement::Class() )) {
3007  TBranchElement* bEl = (TBranchElement*)branch;
3008  bEl->SetTargetClass( expectedClass->GetName() );
3009  }
3010  return kMatch;
3011 }
3012 
3013 ////////////////////////////////////////////////////////////////////////////////
3014 /// Create a clone of this tree and copy nentries.
3015 ///
3016 /// By default copy all entries.
3017 /// The compression level of the cloned tree is set to the destination
3018 /// file's compression level.
3019 ///
3020 /// NOTE: Only active branches are copied.
3021 /// NOTE: If the TTree is a TChain, the structure of the first TTree
3022 /// is used for the copy.
3023 ///
3024 /// IMPORTANT: The cloned tree stays connected with this tree until
3025 /// this tree is deleted. In particular, any changes in
3026 /// branch addresses in this tree are forwarded to the
3027 /// clone trees, unless a branch in a clone tree has had
3028 /// its address changed, in which case that change stays in
3029 /// effect. When this tree is deleted, all the addresses of
3030 /// the cloned tree are reset to their default values.
3031 ///
3032 /// If 'option' contains the word 'fast' and nentries is -1, the
3033 /// cloning will be done without unzipping or unstreaming the baskets
3034 /// (i.e., a direct copy of the raw bytes on disk).
3035 ///
3036 /// When 'fast' is specified, 'option' can also contain a sorting
3037 /// order for the baskets in the output file.
3038 ///
3039 /// There are currently 3 supported sorting order:
3040 ///
3041 /// - SortBasketsByOffset (the default)
3042 /// - SortBasketsByBranch
3043 /// - SortBasketsByEntry
3044 ///
3045 /// When using SortBasketsByOffset the baskets are written in the
3046 /// output file in the same order as in the original file (i.e. the
3047 /// baskets are sorted by their offset in the original file; Usually
3048 /// this also means that the baskets are sorted by the index/number of
3049 /// the _last_ entry they contain)
3050 ///
3051 /// When using SortBasketsByBranch all the baskets of each individual
3052 /// branches are stored contiguously. This tends to optimize reading
3053 /// speed when reading a small number (1->5) of branches, since all
3054 /// their baskets will be clustered together instead of being spread
3055 /// across the file. However it might decrease the performance when
3056 /// reading more branches (or the full entry).
3057 ///
3058 /// When using SortBasketsByEntry the baskets with the lowest starting
3059 /// entry are written first. (i.e. the baskets are sorted by the
3060 /// index/number of the first entry they contain). This means that on
3061 /// the file the baskets will be in the order in which they will be
3062 /// needed when reading the whole tree sequentially.
3063 ///
3064 /// For examples of CloneTree, see tutorials:
3065 ///
3066 /// - copytree.C:
3067 /// A macro to copy a subset of a TTree to a new TTree.
3068 /// The input file has been generated by the program in
3069 /// $ROOTSYS/test/Event with: Event 1000 1 1 1
3070 ///
3071 /// - copytree2.C:
3072 /// A macro to copy a subset of a TTree to a new TTree.
3073 /// One branch of the new Tree is written to a separate file.
3074 /// The input file has been generated by the program in
3075 /// $ROOTSYS/test/Event with: Event 1000 1 1 1
3077 TTree* TTree::CloneTree(Long64_t nentries /* = -1 */, Option_t* option /* = "" */)
3078 {
3079  // Options
3080  Bool_t fastClone = kFALSE;
3081 
3082  TString opt = option;
3083  opt.ToLower();
3084  if (opt.Contains("fast")) {
3085  fastClone = kTRUE;
3086  }
3087 
3088  // If we are a chain, switch to the first tree.
3089  if ((fEntries > 0) && (LoadTree(0) < 0)) {
3090  // FIXME: We need an error message here.
3091  return 0;
3092  }
3093 
3094  // Note: For a tree we get the this pointer, for
3095  // a chain we get the chain's current tree.
3096  TTree* thistree = GetTree();
3097 
3098  // We will use this to override the IO features on the cloned branches.
3099  ROOT::TIOFeatures features = this->GetIOFeatures();
3100  ;
3101 
3102  // Note: For a chain, the returned clone will be
3103  // a clone of the chain's first tree.
3104  TTree* newtree = (TTree*) thistree->Clone();
3105  if (!newtree) {
3106  return 0;
3107  }
3108 
3109  // The clone should not delete any objects allocated by SetAddress().
3110  TObjArray* branches = newtree->GetListOfBranches();
3111  Int_t nb = branches->GetEntriesFast();
3112  for (Int_t i = 0; i < nb; ++i) {
3113  TBranch* br = (TBranch*) branches->UncheckedAt(i);
3114  if (br->InheritsFrom(TBranchElement::Class())) {
3115  ((TBranchElement*) br)->ResetDeleteObject();
3116  }
3117  }
3118 
3119  // Add the new tree to the list of clones so that
3120  // we can later inform it of changes to branch addresses.
3121  thistree->AddClone(newtree);
3122  if (thistree != this) {
3123  // In case this object is a TChain, add the clone
3124  // also to the TChain's list of clones.
3125  AddClone(newtree);
3126  }
3127 
3128  newtree->Reset();
3129 
3130  TDirectory* ndir = newtree->GetDirectory();
3131  TFile* nfile = 0;
3132  if (ndir) {
3133  nfile = ndir->GetFile();
3134  }
3135  Int_t newcomp = -1;
3136  if (nfile) {
3137  newcomp = nfile->GetCompressionSettings();
3138  }
3139 
3140  //
3141  // Delete non-active branches from the clone.
3142  //
3143  // Note: If we are a chain, this does nothing
3144  // since chains have no leaves.
3145  TObjArray* leaves = newtree->GetListOfLeaves();
3146  Int_t nleaves = leaves->GetEntriesFast();
3147  for (Int_t lndx = 0; lndx < nleaves; ++lndx) {
3148  TLeaf* leaf = (TLeaf*) leaves->UncheckedAt(lndx);
3149  if (!leaf) {
3150  continue;
3151  }
3152  TBranch* branch = leaf->GetBranch();
3153  if (branch && (newcomp > -1)) {
3154  branch->SetCompressionSettings(newcomp);
3155  }
3156  if (branch) branch->SetIOFeatures(features);
3157  if (!branch || !branch->TestBit(kDoNotProcess)) {
3158  continue;
3159  }
3160  // size might change at each iteration of the loop over the leaves.
3161  nb = branches->GetEntriesFast();
3162  for (Long64_t i = 0; i < nb; ++i) {
3163  TBranch* br = (TBranch*) branches->UncheckedAt(i);
3164  if (br == branch) {
3165  branches->RemoveAt(i);
3166  delete br;
3167  br = 0;
3168  branches->Compress();
3169  break;
3170  }
3171  TObjArray* lb = br->GetListOfBranches();
3172  Int_t nb1 = lb->GetEntriesFast();
3173  for (Int_t j = 0; j < nb1; ++j) {
3174  TBranch* b1 = (TBranch*) lb->UncheckedAt(j);
3175  if (!b1) {
3176  continue;
3177  }
3178  if (b1 == branch) {
3179  lb->RemoveAt(j);
3180  delete b1;
3181  b1 = 0;
3182  lb->Compress();
3183  break;
3184  }
3185  TObjArray* lb1 = b1->GetListOfBranches();
3186  Int_t nb2 = lb1->GetEntriesFast();
3187  for (Int_t k = 0; k < nb2; ++k) {
3188  TBranch* b2 = (TBranch*) lb1->UncheckedAt(k);
3189  if (!b2) {
3190  continue;
3191  }
3192  if (b2 == branch) {
3193  lb1->RemoveAt(k);
3194  delete b2;
3195  b2 = 0;
3196  lb1->Compress();
3197  break;
3198  }
3199  }
3200  }
3201  }
3202  }
3203  leaves->Compress();
3204 
3205  // Copy MakeClass status.
3206  newtree->SetMakeClass(fMakeClass);
3207 
3208  // Copy branch addresses.
3209  CopyAddresses(newtree);
3210 
3211  //
3212  // Copy entries if requested.
3213  //
3214 
3215  if (nentries != 0) {
3216  if (fastClone && (nentries < 0)) {
3217  if ( newtree->CopyEntries( this, -1, option ) < 0 ) {
3218  // There was a problem!
3219  Error("CloneTTree", "TTree has not been cloned\n");
3220  delete newtree;
3221  newtree = 0;
3222  return 0;
3223  }
3224  } else {
3225  newtree->CopyEntries( this, nentries, option );
3226  }
3227  }
3228 
3229  return newtree;
3230 }
3231 
3232 ////////////////////////////////////////////////////////////////////////////////
3233 /// Set branch addresses of passed tree equal to ours.
3234 /// If undo is true, reset the branch address instead of copying them.
3235 /// This insures 'separation' of a cloned tree from its original
3238 {
3239  // Copy branch addresses starting from branches.
3240  TObjArray* branches = GetListOfBranches();
3241  Int_t nbranches = branches->GetEntriesFast();
3242  for (Int_t i = 0; i < nbranches; ++i) {
3243  TBranch* branch = (TBranch*) branches->UncheckedAt(i);
3244  if (branch->TestBit(kDoNotProcess)) {
3245  continue;
3246  }
3247  if (undo) {
3248  TBranch* br = tree->GetBranch(branch->GetName());
3249  tree->ResetBranchAddress(br);
3250  } else {
3251  char* addr = branch->GetAddress();
3252  if (!addr) {
3253  if (branch->IsA() == TBranch::Class()) {
3254  // If the branch was created using a leaflist, the branch itself may not have
3255  // an address but the leaf might already.
3256  TLeaf *firstleaf = (TLeaf*)branch->GetListOfLeaves()->At(0);
3257  if (!firstleaf || firstleaf->GetValuePointer()) {
3258  // Either there is no leaf (and thus no point in copying the address)
3259  // or the leaf has an address but we can not copy it via the branche
3260  // this will be copied via the next loop (over the leaf).
3261  continue;
3262  }
3263  }
3264  // Note: This may cause an object to be allocated.
3265  branch->SetAddress(0);
3266  addr = branch->GetAddress();
3267  }
3268  // FIXME: The GetBranch() function is braindead and may
3269  // not find the branch!
3270  TBranch* br = tree->GetBranch(branch->GetName());
3271  if (br) {
3272  br->SetAddress(addr);
3273  // The copy does not own any object allocated by SetAddress().
3274  if (br->InheritsFrom(TBranchElement::Class())) {
3275  ((TBranchElement*) br)->ResetDeleteObject();
3276  }
3277  } else {
3278  Warning("CopyAddresses", "Could not find branch named '%s' in tree named '%s'", branch->GetName(), tree->GetName());
3279  }
3280  }
3281  }
3282 
3283  // Copy branch addresses starting from leaves.
3284  TObjArray* tleaves = tree->GetListOfLeaves();
3285  Int_t ntleaves = tleaves->GetEntriesFast();
3286  for (Int_t i = 0; i < ntleaves; ++i) {
3287  TLeaf* tleaf = (TLeaf*) tleaves->UncheckedAt(i);
3288  TBranch* tbranch = tleaf->GetBranch();
3289  TBranch* branch = GetBranch(tbranch->GetName());
3290  if (!branch) {
3291  continue;
3292  }
3293  TLeaf* leaf = branch->GetLeaf(tleaf->GetName());
3294  if (!leaf) {
3295  continue;
3296  }
3297  if (branch->TestBit(kDoNotProcess)) {
3298  continue;
3299  }
3300  if (undo) {
3301  // Now we know whether the address has been transfered
3302  tree->ResetBranchAddress(tbranch);
3303  } else {
3304  TBranchElement *mother = dynamic_cast<TBranchElement*>(leaf->GetBranch()->GetMother());
3305  if (leaf->GetLeafCount() && (leaf->TestBit(TLeaf::kNewValue) || !leaf->GetValuePointer() || (mother && mother->IsObjectOwner())) && tleaf->GetLeafCount())
3306  {
3307  // If it is an array and it was allocated by the leaf itself,
3308  // let's make sure it is large enough for the incoming data.
3309  if (leaf->GetLeafCount()->GetMaximum() < tleaf->GetLeafCount()->GetMaximum()) {
3310  leaf->GetLeafCount()->IncludeRange( tleaf->GetLeafCount() );
3311  if (leaf->GetValuePointer()) {
3312  if (leaf->IsA() == TLeafElement::Class() && mother)
3313  mother->ResetAddress();
3314  else
3315  leaf->SetAddress(nullptr);
3316  }
3317  }
3318  }
3319  if (!branch->GetAddress() && !leaf->GetValuePointer()) {
3320  // We should attempts to set the address of the branch.
3321  // something like:
3322  //(TBranchElement*)branch->GetMother()->SetAddress(0)
3323  //plus a few more subtilities (see TBranchElement::GetEntry).
3324  //but for now we go the simplest route:
3325  //
3326  // Note: This may result in the allocation of an object.
3327  branch->SetupAddresses();
3328  }
3329  if (branch->GetAddress()) {
3330  tree->SetBranchAddress(branch->GetName(), (void*) branch->GetAddress());
3331  TBranch* br = tree->GetBranch(branch->GetName());
3332  if (br) {
3333  // The copy does not own any object allocated by SetAddress().
3334  // FIXME: We do too much here, br may not be a top-level branch.
3335  if (br->InheritsFrom(TBranchElement::Class())) {
3336  ((TBranchElement*) br)->ResetDeleteObject();
3337  }
3338  } else {
3339  Warning("CopyAddresses", "Could not find branch named '%s' in tree named '%s'", branch->GetName(), tree->GetName());
3340  }
3341  } else {
3342  tleaf->SetAddress(leaf->GetValuePointer());
3343  }
3344  }
3345  }
3346 
3347  if (undo &&
3348  ( tree->IsA()->InheritsFrom("TNtuple") || tree->IsA()->InheritsFrom("TNtupleD") )
3349  ) {
3350  tree->ResetBranchAddresses();
3351  }
3352 }
3353 
3354 namespace {
3356  enum EOnIndexError { kDrop, kKeep, kBuild };
3357 
3358  static Bool_t R__HandleIndex(EOnIndexError onIndexError, TTree *newtree, TTree *oldtree)
3359  {
3360  // Return true if we should continue to handle indices, false otherwise.
3361 
3362  Bool_t withIndex = kTRUE;
3363 
3364  if ( newtree->GetTreeIndex() ) {
3365  if ( oldtree->GetTree()->GetTreeIndex() == 0 ) {
3366  switch (onIndexError) {
3367  case kDrop:
3368  delete newtree->GetTreeIndex();
3369  newtree->SetTreeIndex(0);
3370  withIndex = kFALSE;
3371  break;
3372  case kKeep:
3373  // Nothing to do really.
3374  break;
3375  case kBuild:
3376  // Build the index then copy it
3377  if (oldtree->GetTree()->BuildIndex(newtree->GetTreeIndex()->GetMajorName(), newtree->GetTreeIndex()->GetMinorName())) {
3378  newtree->GetTreeIndex()->Append(oldtree->GetTree()->GetTreeIndex(), kTRUE);
3379  // Clean up
3380  delete oldtree->GetTree()->GetTreeIndex();
3381  oldtree->GetTree()->SetTreeIndex(0);
3382  }
3383  break;
3384  }
3385  } else {
3386  newtree->GetTreeIndex()->Append(oldtree->GetTree()->GetTreeIndex(), kTRUE);
3387  }
3388  } else if ( oldtree->GetTree()->GetTreeIndex() != 0 ) {
3389  // We discover the first index in the middle of the chain.
3390  switch (onIndexError) {
3391  case kDrop:
3392  // Nothing to do really.
3393  break;
3394  case kKeep: {
3395  TVirtualIndex *index = (TVirtualIndex*) oldtree->GetTree()->GetTreeIndex()->Clone();
3396  index->SetTree(newtree);
3397  newtree->SetTreeIndex(index);
3398  break;
3399  }
3400  case kBuild:
3401  if (newtree->GetEntries() == 0) {
3402  // Start an index.
3403  TVirtualIndex *index = (TVirtualIndex*) oldtree->GetTree()->GetTreeIndex()->Clone();
3404  index->SetTree(newtree);
3405  newtree->SetTreeIndex(index);
3406  } else {
3407  // Build the index so far.
3408  if (newtree->BuildIndex(oldtree->GetTree()->GetTreeIndex()->GetMajorName(), oldtree->GetTree()->GetTreeIndex()->GetMinorName())) {
3409  newtree->GetTreeIndex()->Append(oldtree->GetTree()->GetTreeIndex(), kTRUE);
3410  }
3411  }
3412  break;
3413  }
3414  } else if ( onIndexError == kDrop ) {
3415  // There is no index on this or on tree->GetTree(), we know we have to ignore any further
3416  // index
3417  withIndex = kFALSE;
3418  }
3419  return withIndex;
3420  }
3421 }
3422 
3423 ////////////////////////////////////////////////////////////////////////////////
3424 /// Copy nentries from given tree to this tree.
3425 /// This routines assumes that the branches that intended to be copied are
3426 /// already connected. The typical case is that this tree was created using
3427 /// tree->CloneTree(0).
3428 ///
3429 /// By default copy all entries.
3430 ///
3431 /// Returns number of bytes copied to this tree.
3432 ///
3433 /// If 'option' contains the word 'fast' and nentries is -1, the cloning will be
3434 /// done without unzipping or unstreaming the baskets (i.e., a direct copy of the
3435 /// raw bytes on disk).
3436 ///
3437 /// When 'fast' is specified, 'option' can also contains a sorting order for the
3438 /// baskets in the output file.
3439 ///
3440 /// There are currently 3 supported sorting order:
3441 ///
3442 /// - SortBasketsByOffset (the default)
3443 /// - SortBasketsByBranch
3444 /// - SortBasketsByEntry
3445 ///
3446 /// See TTree::CloneTree for a detailed explanation of the semantics of these 3 options.
3447 ///
3448 /// If the tree or any of the underlying tree of the chain has an index, that index and any
3449 /// index in the subsequent underlying TTree objects will be merged.
3450 ///
3451 /// There are currently three 'options' to control this merging:
3452 /// - NoIndex : all the TTreeIndex object are dropped.
3453 /// - DropIndexOnError : if any of the underlying TTree object do no have a TTreeIndex,
3454 /// they are all dropped.
3455 /// - AsIsIndexOnError [default]: In case of missing TTreeIndex, the resulting TTree index has gaps.
3456 /// - BuildIndexOnError : If any of the underlying TTree objects do not have a TTreeIndex,
3457 /// all TTreeIndex are 'ignored' and the missing piece are rebuilt.
3459 Long64_t TTree::CopyEntries(TTree* tree, Long64_t nentries /* = -1 */, Option_t* option /* = "" */)
3460 {
3461  if (!tree) {
3462  return 0;
3463  }
3464  // Options
3465  TString opt = option;
3466  opt.ToLower();
3467  Bool_t fastClone = opt.Contains("fast");
3468  Bool_t withIndex = !opt.Contains("noindex");
3469  EOnIndexError onIndexError;
3470  if (opt.Contains("asisindex")) {
3471  onIndexError = kKeep;
3472  } else if (opt.Contains("buildindex")) {
3473  onIndexError = kBuild;
3474  } else if (opt.Contains("dropindex")) {
3475  onIndexError = kDrop;
3476  } else {
3477  onIndexError = kBuild;
3478  }
3479  Ssiz_t cacheSizeLoc = opt.Index("cachesize=");
3480  Int_t cacheSize = -1;
3481  if (cacheSizeLoc != TString::kNPOS) {
3482  // If the parse faile, cacheSize stays at -1.
3483  Ssiz_t cacheSizeEnd = opt.Index(" ",cacheSizeLoc+10) - (cacheSizeLoc+10);
3484  TSubString cacheSizeStr( opt(cacheSizeLoc+10,cacheSizeEnd) );
3485  auto parseResult = ROOT::FromHumanReadableSize(cacheSizeStr,cacheSize);
3486  if (parseResult == ROOT::EFromHumanReadableSize::kParseFail) {
3487  Warning("CopyEntries","The cachesize option can not be parsed: %s. The default size will be used.",cacheSizeStr.String().Data());
3488  } else if (parseResult == ROOT::EFromHumanReadableSize::kOverflow) {
3489  double m;
3490  const char *munit = nullptr;
3491  ROOT::ToHumanReadableSize(std::numeric_limits<decltype(cacheSize)>::max(),false,&m,&munit);
3492 
3493  Warning("CopyEntries","The cachesize option is too large: %s (%g%s max). The default size will be used.",cacheSizeStr.String().Data(),m,munit);
3494  }
3495  }
3496  if (gDebug > 0 && cacheSize != -1) Info("CopyEntries","Using Cache size: %d\n",cacheSize);
3497 
3498  Long64_t nbytes = 0;
3499  Long64_t treeEntries = tree->GetEntriesFast();
3500  if (nentries < 0) {
3501  nentries = treeEntries;
3502  } else if (nentries > treeEntries) {
3503  nentries = treeEntries;
3504  }
3505 
3506  if (fastClone && (nentries < 0 || nentries == tree->GetEntriesFast())) {
3507  // Quickly copy the basket without decompression and streaming.
3508  Long64_t totbytes = GetTotBytes();
3509  for (Long64_t i = 0; i < nentries; i += tree->GetTree()->GetEntries()) {
3510  if (tree->LoadTree(i) < 0) {
3511  break;
3512  }
3513  if ( withIndex ) {
3514  withIndex = R__HandleIndex( onIndexError, this, tree );
3515  }
3516  if (this->GetDirectory()) {
3517  TFile* file2 = this->GetDirectory()->GetFile();
3518  if (file2 && (file2->GetEND() > TTree::GetMaxTreeSize())) {
3519  if (this->GetDirectory() == (TDirectory*) file2) {
3520  this->ChangeFile(file2);
3521  }
3522  }
3523  }
3524  TTreeCloner cloner(tree->GetTree(), this, option, TTreeCloner::kNoWarnings);
3525  if (cloner.IsValid()) {
3526  this->SetEntries(this->GetEntries() + tree->GetTree()->GetEntries());
3527  if (cacheSize != -1) cloner.SetCacheSize(cacheSize);
3528  cloner.Exec();
3529  } else {
3530  if (i == 0) {
3531  Warning("CopyEntries","%s",cloner.GetWarning());
3532  // If the first cloning does not work, something is really wrong
3533  // (since apriori the source and target are exactly the same structure!)
3534  return -1;
3535  } else {
3536  if (cloner.NeedConversion()) {
3537  TTree *localtree = tree->GetTree();
3538  Long64_t tentries = localtree->GetEntries();
3539  for (Long64_t ii = 0; ii < tentries; ii++) {
3540  if (localtree->GetEntry(ii) <= 0) {
3541  break;
3542  }
3543  this->Fill();
3544  }
3545  if (this->GetTreeIndex()) {
3546  this->GetTreeIndex()->Append(tree->GetTree()->GetTreeIndex(), kTRUE);
3547  }
3548  } else {
3549  Warning("CopyEntries","%s",cloner.GetWarning());
3550  if (tree->GetDirectory() && tree->GetDirectory()->GetFile()) {
3551  Warning("CopyEntries", "Skipped file %s\n", tree->GetDirectory()->GetFile()->GetName());
3552  } else {
3553  Warning("CopyEntries", "Skipped file number %d\n", tree->GetTreeNumber());
3554  }
3555  }
3556  }
3557  }
3558 
3559  }
3560  if (this->GetTreeIndex()) {
3561  this->GetTreeIndex()->Append(0,kFALSE); // Force the sorting
3562  }
3563  nbytes = GetTotBytes() - totbytes;
3564  } else {
3565  if (nentries < 0) {
3566  nentries = treeEntries;
3567  } else if (nentries > treeEntries) {
3568  nentries = treeEntries;
3569  }
3570  Int_t treenumber = -1;
3571  for (Long64_t i = 0; i < nentries; i++) {
3572  if (tree->LoadTree(i) < 0) {
3573  break;
3574  }
3575  if (treenumber != tree->GetTreeNumber()) {
3576  if ( withIndex ) {
3577  withIndex = R__HandleIndex( onIndexError, this, tree );
3578  }
3579  treenumber = tree->GetTreeNumber();
3580  }
3581  if (tree->GetEntry(i) <= 0) {
3582  break;
3583  }
3584  nbytes += this->Fill();
3585  }
3586  if (this->GetTreeIndex()) {
3587  this->GetTreeIndex()->Append(0,kFALSE); // Force the sorting
3588  }
3589  }
3590  return nbytes;
3591 }
3592 
3593 ////////////////////////////////////////////////////////////////////////////////
3594 /// Copy a tree with selection.
3595 ///
3596 /// ### Important:
3597 ///
3598 /// The returned copied tree stays connected with the original tree
3599 /// until the original tree is deleted. In particular, any changes
3600 /// to the branch addresses in the original tree are also made to
3601 /// the copied tree. Any changes made to the branch addresses of the
3602 /// copied tree are overridden anytime the original tree changes its
3603 /// branch addresses. When the original tree is deleted, all the
3604 /// branch addresses of the copied tree are set to zero.
3605 ///
3606 /// For examples of CopyTree, see the tutorials:
3607 ///
3608 /// - copytree.C:
3609 /// Example macro to copy a subset of a tree to a new tree.
3610 /// The input file was generated by running the program in
3611 /// $ROOTSYS/test/Event in this way:
3612 /// ~~~ {.cpp}
3613 /// ./Event 1000 1 1 1
3614 /// ~~~
3615 /// - copytree2.C
3616 /// Example macro to copy a subset of a tree to a new tree.
3617 /// One branch of the new tree is written to a separate file.
3618 /// The input file was generated by running the program in
3619 /// $ROOTSYS/test/Event in this way:
3620 /// ~~~ {.cpp}
3621 /// ./Event 1000 1 1 1
3622 /// ~~~
3623 /// - copytree3.C
3624 /// Example macro to copy a subset of a tree to a new tree.
3625 /// Only selected entries are copied to the new tree.
3626 /// NOTE that only the active branches are copied.
3628 TTree* TTree::CopyTree(const char* selection, Option_t* option /* = 0 */, Long64_t nentries /* = TTree::kMaxEntries */, Long64_t firstentry /* = 0 */)
3629 {
3630  GetPlayer();
3631  if (fPlayer) {
3632  return fPlayer->CopyTree(selection, option, nentries, firstentry);
3633  }
3634  return 0;
3635 }
3636 
3637 ////////////////////////////////////////////////////////////////////////////////
3638 /// Create a basket for this tree and given branch.
3641 {
3642  if (!branch) {
3643  return 0;
3644  }
3645  return new TBasket(branch->GetName(), GetName(), branch);
3646 }
3647 
3648 ////////////////////////////////////////////////////////////////////////////////
3649 /// Delete this tree from memory or/and disk.
3650 ///
3651 /// - if option == "all" delete Tree object from memory AND from disk
3652 /// all baskets on disk are deleted. All keys with same name
3653 /// are deleted.
3654 /// - if option =="" only Tree object in memory is deleted.
3656 void TTree::Delete(Option_t* option /* = "" */)
3657 {
3658  TFile *file = GetCurrentFile();
3659 
3660  // delete all baskets and header from file
3661  if (file && !strcmp(option,"all")) {
3662  if (!file->IsWritable()) {
3663  Error("Delete","File : %s is not writable, cannot delete Tree:%s", file->GetName(),GetName());
3664  return;
3665  }
3666 
3667  //find key and import Tree header in memory
3668  TKey *key = fDirectory->GetKey(GetName());
3669  if (!key) return;
3670 
3671  TDirectory *dirsav = gDirectory;
3672  file->cd();
3673 
3674  //get list of leaves and loop on all the branches baskets
3675  TIter next(GetListOfLeaves());
3676  TLeaf *leaf;
3677  char header[16];
3678  Int_t ntot = 0;
3679  Int_t nbask = 0;
3680  Int_t nbytes,objlen,keylen;
3681  while ((leaf = (TLeaf*)next())) {
3682  TBranch *branch = leaf->GetBranch();
3683  Int_t nbaskets = branch->GetMaxBaskets();
3684  for (Int_t i=0;i<nbaskets;i++) {
3685  Long64_t pos = branch->GetBasketSeek(i);
3686  if (!pos) continue;
3687  TFile *branchFile = branch->GetFile();
3688  if (!branchFile) continue;
3689  branchFile->GetRecordHeader(header,pos,16,nbytes,objlen,keylen);
3690  if (nbytes <= 0) continue;
3691  branchFile->MakeFree(pos,pos+nbytes-1);
3692  ntot += nbytes;
3693  nbask++;
3694  }
3695  }
3696 
3697  // delete Tree header key and all keys with the same name
3698  // A Tree may have been saved many times. Previous cycles are invalid.
3699  while (key) {
3700  ntot += key->GetNbytes();
3701  key->Delete();
3702  delete key;
3703  key = fDirectory->GetKey(GetName());
3704  }
3705  if (dirsav) dirsav->cd();
3706  if (gDebug) Info("TTree::Delete", "Deleting Tree: %s: %d baskets deleted. Total space freed = %d bytes\n",GetName(),nbask,ntot);
3707  }
3708 
3709  if (fDirectory) {
3710  fDirectory->Remove(this);
3711  //delete the file cache if it points to this Tree
3712  MoveReadCache(file,0);
3713  fDirectory = 0;
3715  }
3716 
3717  // Delete object from CINT symbol table so it can not be used anymore.
3718  gCling->DeleteGlobal(this);
3719 
3720  // Warning: We have intentional invalidated this object while inside a member function!
3721  delete this;
3722 }
3723 
3724  ///////////////////////////////////////////////////////////////////////////////
3725  /// Called by TKey and TObject::Clone to automatically add us to a directory
3726  /// when we are read from a file.
3729 {
3730  if (fDirectory == dir) return;
3731  if (fDirectory) {
3732  fDirectory->Remove(this);
3733  // Delete or move the file cache if it points to this Tree
3734  TFile *file = fDirectory->GetFile();
3735  MoveReadCache(file,dir);
3736  }
3737  fDirectory = dir;
3738  TBranch* b = 0;
3739  TIter next(GetListOfBranches());
3740  while((b = (TBranch*) next())) {
3741  b->UpdateFile();
3742  }
3743  if (fBranchRef) {
3745  }
3746  if (fDirectory) fDirectory->Append(this);
3747 }
3748 
3749 ////////////////////////////////////////////////////////////////////////////////
3750 /// Draw expression varexp for specified entries.
3751 ///
3752 /// \return -1 in case of error or number of selected events in case of success.
3753 ///
3754 /// This function accepts TCut objects as arguments.
3755 /// Useful to use the string operator +
3756 ///
3757 /// Example:
3758 ///
3759 /// ~~~ {.cpp}
3760 /// ntuple.Draw("x",cut1+cut2+cut3);
3761 /// ~~~
3762 
3764 Long64_t TTree::Draw(const char* varexp, const TCut& selection, Option_t* option, Long64_t nentries, Long64_t firstentry)
3765 {
3766  return TTree::Draw(varexp, selection.GetTitle(), option, nentries, firstentry);
3767 }
3768 
3769 ////////////////////////////////////////////////////////////////////////////////
3770 /// Draw expression varexp for specified entries.
3771 ///
3772 /// \return -1 in case of error or number of selected events in case of success.
3773 ///
3774 /// \param [in] varexp is an expression of the general form
3775 /// - "e1" produces a 1-d histogram (TH1F) of expression "e1"
3776 /// - "e1:e2" produces an unbinned 2-d scatter-plot (TGraph) of "e1"
3777 /// on the y-axis versus "e2" on the x-axis
3778 /// - "e1:e2:e3" produces an unbinned 3-d scatter-plot (TPolyMarker3D) of "e1"
3779 /// vs "e2" vs "e3" on the x-, y-, z-axis, respectively.
3780 /// - "e1:e2:e3:e4" produces an unbinned 3-d scatter-plot (TPolyMarker3D) of "e1"
3781 /// vs "e2" vs "e3" and "e4" mapped on the current color palette.
3782 /// (to create histograms in the 2, 3, and 4 dimensional case,
3783 /// see section "Saving the result of Draw to an histogram")
3784 ///
3785 /// Example:
3786 /// - varexp = x simplest case: draw a 1-Dim distribution of column named x
3787 /// - varexp = sqrt(x) : draw distribution of sqrt(x)
3788 /// - varexp = x*y/z
3789 /// - varexp = y:sqrt(x) 2-Dim distribution of y versus sqrt(x)
3790 /// - varexp = px:py:pz:2.5*E produces a 3-d scatter-plot of px vs py ps pz
3791 /// and the color number of each marker will be 2.5*E.
3792 /// If the color number is negative it is set to 0.
3793 /// If the color number is greater than the current number of colors
3794 /// it is set to the highest color number.The default number of
3795 /// colors is 50. see TStyle::SetPalette for setting a new color palette.
3796 ///
3797 /// Note that the variables e1, e2 or e3 may contain a selection.
3798 /// example, if e1= x*(y<0), the value histogrammed will be x if y<0
3799 /// and will be 0 otherwise.
3800 ///
3801 /// The expressions can use all the operations and build-in functions
3802 /// supported by TFormula (See TFormula::Analyze), including free
3803 /// standing function taking numerical arguments (TMath::Bessel).
3804 /// In addition, you can call member functions taking numerical
3805 /// arguments. For example:
3806 /// ~~~ {.cpp}
3807 /// TMath::BreitWigner(fPx,3,2)
3808 /// event.GetHistogram().GetXaxis().GetXmax()
3809 /// ~~~
3810 /// Note: You can only pass expression that depend on the TTree's data
3811 /// to static functions and you can only call non-static member function
3812 /// with 'fixed' parameters.
3813 ///
3814 /// \param [in] selection is an expression with a combination of the columns.
3815 /// In a selection all the C++ operators are authorized.
3816 /// The value corresponding to the selection expression is used as a weight
3817 /// to fill the histogram.
3818 /// If the expression includes only boolean operations, the result
3819 /// is 0 or 1. If the result is 0, the histogram is not filled.
3820 /// In general, the expression may be of the form:
3821 /// ~~~ {.cpp}
3822 /// value*(boolean expression)
3823 /// ~~~
3824 /// if boolean expression is true, the histogram is filled with
3825 /// a `weight = value`.
3826 /// Examples:
3827 /// - selection1 = "x<y && sqrt(z)>3.2"
3828 /// - selection2 = "(x+y)*(sqrt(z)>3.2)"
3829 /// - selection1 returns a weight = 0 or 1
3830 /// - selection2 returns a weight = x+y if sqrt(z)>3.2
3831 /// returns a weight = 0 otherwise.
3832 ///
3833 /// \param [in] option is the drawing option.
3834 /// - When an histogram is produced it can be any histogram drawing option
3835 /// listed in THistPainter.
3836 /// - when no option is specified:
3837 /// - the default histogram drawing option is used
3838 /// if the expression is of the form "e1".
3839 /// - if the expression is of the form "e1:e2"or "e1:e2:e3" a cloud of
3840 /// unbinned 2D or 3D points is drawn respectively.
3841 /// - if the expression has four fields "e1:e2:e3:e4" a cloud of unbinned 3D
3842 /// points is produced with e1 vs e2 vs e3, and e4 is mapped on the current color
3843 /// palette.
3844 /// - If option COL is specified when varexp has three fields:
3845 /// ~~~ {.cpp}
3846 /// tree.Draw("e1:e2:e3","","col");
3847 /// ~~~
3848 /// a 2D scatter is produced with e1 vs e2, and e3 is mapped on the current
3849 /// color palette. The colors for e3 are evaluated once in linear scale before
3850 /// painting. Therefore changing the pad to log scale along Z as no effect
3851 /// on the colors.
3852 /// - if expression has more than four fields the option "PARA"or "CANDLE"
3853 /// can be used.
3854 /// - If option contains the string "goff", no graphics is generated.
3855 ///
3856 /// \param [in] nentries is the number of entries to process (default is all)
3857 ///
3858 /// \param [in] firstentry is the first entry to process (default is 0)
3859 ///
3860 /// ### Drawing expressions using arrays and array elements
3861 ///
3862 /// Let assumes, a leaf fMatrix, on the branch fEvent, which is a 3 by 3 array,
3863 /// or a TClonesArray.
3864 /// In a TTree::Draw expression you can now access fMatrix using the following
3865 /// syntaxes:
3866 ///
3867 /// | String passed | What is used for each entry of the tree
3868 /// |-----------------|--------------------------------------------------------|
3869 /// | `fMatrix` | the 9 elements of fMatrix |
3870 /// | `fMatrix[][]` | the 9 elements of fMatrix |
3871 /// | `fMatrix[2][2]` | only the elements fMatrix[2][2] |
3872 /// | `fMatrix[1]` | the 3 elements fMatrix[1][0], fMatrix[1][1] and fMatrix[1][2] |
3873 /// | `fMatrix[1][]` | the 3 elements fMatrix[1][0], fMatrix[1][1] and fMatrix[1][2] |
3874 /// | `fMatrix[][0]` | the 3 elements fMatrix[0][0], fMatrix[1][0] and fMatrix[2][0] |
3875 ///
3876 /// "fEvent.fMatrix...." same as "fMatrix..." (unless there is more than one leaf named fMatrix!).
3877 ///
3878 /// In summary, if a specific index is not specified for a dimension, TTree::Draw
3879 /// will loop through all the indices along this dimension. Leaving off the
3880 /// last (right most) dimension of specifying then with the two characters '[]'
3881 /// is equivalent. For variable size arrays (and TClonesArray) the range
3882 /// of the first dimension is recalculated for each entry of the tree.
3883 /// You can also specify the index as an expression of any other variables from the
3884 /// tree.
3885 ///
3886 /// TTree::Draw also now properly handling operations involving 2 or more arrays.
3887 ///
3888 /// Let assume a second matrix fResults[5][2], here are a sample of some
3889 /// of the possible combinations, the number of elements they produce and
3890 /// the loop used:
3891 ///
3892 /// | expression | element(s) | Loop |
3893 /// |----------------------------------|------------|--------------------------|
3894 /// | `fMatrix[2][1] - fResults[5][2]` | one | no loop |
3895 /// | `fMatrix[2][] - fResults[5][2]` | three | on 2nd dim fMatrix |
3896 /// | `fMatrix[2][] - fResults[5][]` | two | on both 2nd dimensions |
3897 /// | `fMatrix[][2] - fResults[][1]` | three | on both 1st dimensions |
3898 /// | `fMatrix[][2] - fResults[][]` | six | on both 1st and 2nd dimensions of fResults |
3899 /// | `fMatrix[][2] - fResults[3][]` | two | on 1st dim of fMatrix and 2nd of fResults (at the same time) |
3900 /// | `fMatrix[][] - fResults[][]` | six | on 1st dim then on 2nd dim |
3901 /// | `fMatrix[][fResult[][]]` | 30 | on 1st dim of fMatrix then on both dimensions of fResults. The value if fResults[j][k] is used as the second index of fMatrix.|
3902 ///
3903 ///
3904 /// In summary, TTree::Draw loops through all unspecified dimensions. To
3905 /// figure out the range of each loop, we match each unspecified dimension
3906 /// from left to right (ignoring ALL dimensions for which an index has been
3907 /// specified), in the equivalent loop matched dimensions use the same index
3908 /// and are restricted to the smallest range (of only the matched dimensions).
3909 /// When involving variable arrays, the range can of course be different
3910 /// for each entry of the tree.
3911 ///
3912 /// So the loop equivalent to "fMatrix[][2] - fResults[3][]" is:
3913 /// ~~~ {.cpp}
3914 /// for (Int_t i0; i < min(3,2); i++) {
3915 /// use the value of (fMatrix[i0][2] - fMatrix[3][i0])
3916 /// }
3917 /// ~~~
3918 /// So the loop equivalent to "fMatrix[][2] - fResults[][]" is:
3919 /// ~~~ {.cpp}
3920 /// for (Int_t i0; i < min(3,5); i++) {
3921 /// for (Int_t i1; i1 < 2; i1++) {
3922 /// use the value of (fMatrix[i0][2] - fMatrix[i0][i1])
3923 /// }
3924 /// }
3925 /// ~~~
3926 /// So the loop equivalent to "fMatrix[][] - fResults[][]" is:
3927 /// ~~~ {.cpp}
3928 /// for (Int_t i0; i < min(3,5); i++) {
3929 /// for (Int_t i1; i1 < min(3,2); i1++) {
3930 /// use the value of (fMatrix[i0][i1] - fMatrix[i0][i1])
3931 /// }
3932 /// }
3933 /// ~~~
3934 /// So the loop equivalent to "fMatrix[][fResults[][]]" is:
3935 /// ~~~ {.cpp}
3936 /// for (Int_t i0; i0 < 3; i0++) {
3937 /// for (Int_t j2; j2 < 5; j2++) {
3938 /// for (Int_t j3; j3 < 2; j3++) {
3939 /// i1 = fResults[j2][j3];
3940 /// use the value of fMatrix[i0][i1]
3941 /// }
3942 /// }
3943 /// ~~~
3944 /// ### Retrieving the result of Draw
3945 ///
3946 /// By default the temporary histogram created is called "htemp", but only in
3947 /// the one dimensional Draw("e1") it contains the TTree's data points. For
3948 /// a two dimensional Draw, the data is filled into a TGraph which is named
3949 /// "Graph". They can be retrieved by calling
3950 /// ~~~ {.cpp}
3951 /// TH1F *htemp = (TH1F*)gPad->GetPrimitive("htemp"); // 1D
3952 /// TGraph *graph = (TGraph*)gPad->GetPrimitive("Graph"); // 2D
3953 /// ~~~
3954 /// For a three and four dimensional Draw the TPolyMarker3D is unnamed, and
3955 /// cannot be retrieved.
3956 ///
3957 /// gPad always contains a TH1 derived object called "htemp" which allows to
3958 /// access the axes:
3959 /// ~~~ {.cpp}
3960 /// TGraph *graph = (TGraph*)gPad->GetPrimitive("Graph"); // 2D
3961 /// TH2F *htemp = (TH2F*)gPad->GetPrimitive("htemp"); // empty, but has axes
3962 /// TAxis *xaxis = htemp->GetXaxis();
3963 /// ~~~
3964 /// ### Saving the result of Draw to an histogram
3965 ///
3966 /// If varexp0 contains >>hnew (following the variable(s) name(s),
3967 /// the new histogram created is called hnew and it is kept in the current
3968 /// directory (and also the current pad). This works for all dimensions.
3969 ///
3970 /// Example:
3971 /// ~~~ {.cpp}
3972 /// tree.Draw("sqrt(x)>>hsqrt","y>0")
3973 /// ~~~
3974 /// will draw `sqrt(x)` and save the histogram as "hsqrt" in the current
3975 /// directory. To retrieve it do:
3976 /// ~~~ {.cpp}
3977 /// TH1F *hsqrt = (TH1F*)gDirectory->Get("hsqrt");
3978 /// ~~~
3979 /// The binning information is taken from the environment variables
3980 /// ~~~ {.cpp}
3981 /// Hist.Binning.?D.?
3982 /// ~~~
3983 /// In addition, the name of the histogram can be followed by up to 9
3984 /// numbers between '(' and ')', where the numbers describe the
3985 /// following:
3986 ///
3987 /// - 1 - bins in x-direction
3988 /// - 2 - lower limit in x-direction
3989 /// - 3 - upper limit in x-direction
3990 /// - 4-6 same for y-direction
3991 /// - 7-9 same for z-direction
3992 ///
3993 /// When a new binning is used the new value will become the default.
3994 /// Values can be skipped.
3995 ///
3996 /// Example:
3997 /// ~~~ {.cpp}
3998 /// tree.Draw("sqrt(x)>>hsqrt(500,10,20)")
3999 /// // plot sqrt(x) between 10 and 20 using 500 bins
4000 /// tree.Draw("sqrt(x):sin(y)>>hsqrt(100,10,60,50,.1,.5)")
4001 /// // plot sqrt(x) against sin(y)
4002 /// // 100 bins in x-direction; lower limit on x-axis is 10; upper limit is 60
4003 /// // 50 bins in y-direction; lower limit on y-axis is .1; upper limit is .5
4004 /// ~~~
4005 /// By default, the specified histogram is reset.
4006 /// To continue to append data to an existing histogram, use "+" in front
4007 /// of the histogram name.
4008 ///
4009 /// A '+' in front of the histogram name is ignored, when the name is followed by
4010 /// binning information as described in the previous paragraph.
4011 /// ~~~ {.cpp}
4012 /// tree.Draw("sqrt(x)>>+hsqrt","y>0")
4013 /// ~~~
4014 /// will not reset `hsqrt`, but will continue filling. This works for 1-D, 2-D
4015 /// and 3-D histograms.
4016 ///
4017 /// ### Accessing collection objects
4018 ///
4019 /// TTree::Draw default's handling of collections is to assume that any
4020 /// request on a collection pertain to it content. For example, if fTracks
4021 /// is a collection of Track objects, the following:
4022 /// ~~~ {.cpp}
4023 /// tree->Draw("event.fTracks.fPx");
4024 /// ~~~
4025 /// will plot the value of fPx for each Track objects inside the collection.
4026 /// Also
4027 /// ~~~ {.cpp}
4028 /// tree->Draw("event.fTracks.size()");
4029 /// ~~~
4030 /// would plot the result of the member function Track::size() for each
4031 /// Track object inside the collection.
4032 /// To access information about the collection itself, TTree::Draw support
4033 /// the '@' notation. If a variable which points to a collection is prefixed
4034 /// or postfixed with '@', the next part of the expression will pertain to
4035 /// the collection object. For example:
4036 /// ~~~ {.cpp}
4037 /// tree->Draw("event.@fTracks.size()");
4038 /// ~~~
4039 /// will plot the size of the collection referred to by `fTracks` (i.e the number
4040 /// of Track objects).
4041 ///
4042 /// ### Drawing 'objects'
4043 ///
4044 /// When a class has a member function named AsDouble or AsString, requesting
4045 /// to directly draw the object will imply a call to one of the 2 functions.
4046 /// If both AsDouble and AsString are present, AsDouble will be used.
4047 /// AsString can return either a char*, a std::string or a TString.s
4048 /// For example, the following
4049 /// ~~~ {.cpp}
4050 /// tree->Draw("event.myTTimeStamp");
4051 /// ~~~
4052 /// will draw the same histogram as
4053 /// ~~~ {.cpp}
4054 /// tree->Draw("event.myTTimeStamp.AsDouble()");
4055 /// ~~~
4056 /// In addition, when the object is a type TString or std::string, TTree::Draw
4057 /// will call respectively `TString::Data` and `std::string::c_str()`
4058 ///
4059 /// If the object is a TBits, the histogram will contain the index of the bit
4060 /// that are turned on.
4061 ///
4062 /// ### Retrieving information about the tree itself.
4063 ///
4064 /// You can refer to the tree (or chain) containing the data by using the
4065 /// string 'This'.
4066 /// You can then could any TTree methods. For example:
4067 /// ~~~ {.cpp}
4068 /// tree->Draw("This->GetReadEntry()");
4069 /// ~~~
4070 /// will display the local entry numbers be read.
4071 /// ~~~ {.cpp}
4072 /// tree->Draw("This->GetUserInfo()->At(0)->GetName()");
4073 /// ~~~
4074 /// will display the name of the first 'user info' object.
4075 ///
4076 /// ### Special functions and variables
4077 ///
4078 /// `Entry$`: A TTree::Draw formula can use the special variable `Entry$`
4079 /// to access the entry number being read. For example to draw every
4080 /// other entry use:
4081 /// ~~~ {.cpp}
4082 /// tree.Draw("myvar","Entry$%2==0");
4083 /// ~~~
4084 /// - `Entry$` : return the current entry number (`== TTree::GetReadEntry()`)
4085 /// - `LocalEntry$` : return the current entry number in the current tree of a
4086 /// chain (`== GetTree()->GetReadEntry()`)
4087 /// - `Entries$` : return the total number of entries (== TTree::GetEntries())
4088 /// - `LocalEntries$` : return the total number of entries in the current tree
4089 /// of a chain (== GetTree()->TTree::GetEntries())
4090 /// - `Length$` : return the total number of element of this formula for this
4091 /// entry (`==TTreeFormula::GetNdata()`)
4092 /// - `Iteration$` : return the current iteration over this formula for this
4093 /// entry (i.e. varies from 0 to `Length$`).
4094 /// - `Length$(formula )` : return the total number of element of the formula
4095 /// given as a parameter.
4096 /// - `Sum$(formula )` : return the sum of the value of the elements of the
4097 /// formula given as a parameter. For example the mean for all the elements in
4098 /// one entry can be calculated with: `Sum$(formula )/Length$(formula )`
4099 /// - `Min$(formula )` : return the minimun (within one TTree entry) of the value of the
4100 /// elements of the formula given as a parameter.
4101 /// - `Max$(formula )` : return the maximum (within one TTree entry) of the value of the
4102 /// elements of the formula given as a parameter.
4103 /// - `MinIf$(formula,condition)`
4104 /// - `MaxIf$(formula,condition)` : return the minimum (maximum) (within one TTree entry)
4105 /// of the value of the elements of the formula given as a parameter
4106 /// if they match the condition. If no element matches the condition,
4107 /// the result is zero. To avoid the resulting peak at zero, use the
4108 /// pattern:
4109 /// ~~~ {.cpp}
4110 /// tree->Draw("MinIf$(formula,condition)","condition");
4111 /// ~~~
4112 /// which will avoid calculation `MinIf$` for the entries that have no match
4113 /// for the condition.
4114 /// - `Alt$(primary,alternate)` : return the value of "primary" if it is available
4115 /// for the current iteration otherwise return the value of "alternate".
4116 /// For example, with arr1[3] and arr2[2]
4117 /// ~~~ {.cpp}
4118 /// tree->Draw("arr1+Alt$(arr2,0)");
4119 /// ~~~
4120 /// will draw arr1[0]+arr2[0] ; arr1[1]+arr2[1] and arr1[2]+0
4121 /// Or with a variable size array arr3
4122 /// ~~~ {.cpp}
4123 /// tree->Draw("Alt$(arr3[0],0)+Alt$(arr3[1],0)+Alt$(arr3[2],0)");
4124 /// ~~~
4125 /// will draw the sum arr3 for the index 0 to min(2,actual_size_of_arr3-1)
4126 /// As a comparison
4127 /// ~~~ {.cpp}
4128 /// tree->Draw("arr3[0]+arr3[1]+arr3[2]");
4129 /// ~~~
4130 /// will draw the sum arr3 for the index 0 to 2 only if the
4131 /// actual_size_of_arr3 is greater or equal to 3.
4132 /// Note that the array in 'primary' is flattened/linearized thus using
4133 /// `Alt$` with multi-dimensional arrays of different dimensions in unlikely
4134 /// to yield the expected results. To visualize a bit more what elements
4135 /// would be matched by TTree::Draw, TTree::Scan can be used:
4136 /// ~~~ {.cpp}
4137 /// tree->Scan("arr1:Alt$(arr2,0)");
4138 /// ~~~
4139 /// will print on one line the value of arr1 and (arr2,0) that will be
4140 /// matched by
4141 /// ~~~ {.cpp}
4142 /// tree->Draw("arr1-Alt$(arr2,0)");
4143 /// ~~~
4144 /// The ternary operator is not directly supported in TTree::Draw however, to plot the
4145 /// equivalent of `var2<20 ? -99 : var1`, you can use:
4146 /// ~~~ {.cpp}
4147 /// tree->Draw("(var2<20)*99+(var2>=20)*var1","");
4148 /// ~~~
4149 ///
4150 /// ### Drawing a user function accessing the TTree data directly
4151 ///
4152 /// If the formula contains a file name, TTree::MakeProxy will be used
4153 /// to load and execute this file. In particular it will draw the
4154 /// result of a function with the same name as the file. The function
4155 /// will be executed in a context where the name of the branches can
4156 /// be used as a C++ variable.
4157 ///
4158 /// For example draw px using the file hsimple.root (generated by the
4159 /// hsimple.C tutorial), we need a file named hsimple.cxx:
4160 /// ~~~ {.cpp}
4161 /// double hsimple() {
4162 /// return px;
4163 /// }
4164 /// ~~~
4165 /// MakeProxy can then be used indirectly via the TTree::Draw interface
4166 /// as follow:
4167 /// ~~~ {.cpp}
4168 /// new TFile("hsimple.root")
4169 /// ntuple->Draw("hsimple.cxx");
4170 /// ~~~
4171 /// A more complete example is available in the tutorials directory:
4172 /// `h1analysisProxy.cxx`, `h1analysProxy.h` and `h1analysisProxyCut.C`
4173 /// which reimplement the selector found in `h1analysis.C`
4174 ///
4175 /// The main features of this facility are:
4176 ///
4177 /// * on-demand loading of branches
4178 /// * ability to use the 'branchname' as if it was a data member
4179 /// * protection against array out-of-bound
4180 /// * ability to use the branch data as object (when the user code is available)
4181 ///
4182 /// See TTree::MakeProxy for more details.
4183 ///
4184 /// ### Making a Profile histogram
4185 ///
4186 /// In case of a 2-Dim expression, one can generate a TProfile histogram
4187 /// instead of a TH2F histogram by specifying option=prof or option=profs
4188 /// or option=profi or option=profg ; the trailing letter select the way
4189 /// the bin error are computed, See TProfile2D::SetErrorOption for
4190 /// details on the differences.
4191 /// The option=prof is automatically selected in case of y:x>>pf
4192 /// where pf is an existing TProfile histogram.
4193 ///
4194 /// ### Making a 2D Profile histogram
4195 ///
4196 /// In case of a 3-Dim expression, one can generate a TProfile2D histogram
4197 /// instead of a TH3F histogram by specifying option=prof or option=profs.
4198 /// or option=profi or option=profg ; the trailing letter select the way
4199 /// the bin error are computed, See TProfile2D::SetErrorOption for
4200 /// details on the differences.
4201 /// The option=prof is automatically selected in case of z:y:x>>pf
4202 /// where pf is an existing TProfile2D histogram.
4203 ///
4204 /// ### Making a 5D plot using GL
4205 ///
4206 /// If option GL5D is specified together with 5 variables, a 5D plot is drawn
4207 /// using OpenGL. See $ROOTSYS/tutorials/tree/staff.C as example.
4208 ///
4209 /// ### Making a parallel coordinates plot
4210 ///
4211 /// In case of a 2-Dim or more expression with the option=para, one can generate
4212 /// a parallel coordinates plot. With that option, the number of dimensions is
4213 /// arbitrary. Giving more than 4 variables without the option=para or
4214 /// option=candle or option=goff will produce an error.
4215 ///
4216 /// ### Making a candle sticks chart
4217 ///
4218 /// In case of a 2-Dim or more expression with the option=candle, one can generate
4219 /// a candle sticks chart. With that option, the number of dimensions is
4220 /// arbitrary. Giving more than 4 variables without the option=para or
4221 /// option=candle or option=goff will produce an error.
4222 ///
4223 /// ### Normalizing the output histogram to 1
4224 ///
4225 /// When option contains "norm" the output histogram is normalized to 1.
4226 ///
4227 /// ### Saving the result of Draw to a TEventList, a TEntryList or a TEntryListArray
4228 ///
4229 /// TTree::Draw can be used to fill a TEventList object (list of entry numbers)
4230 /// instead of histogramming one variable.
4231 /// If varexp0 has the form >>elist , a TEventList object named "elist"
4232 /// is created in the current directory. elist will contain the list
4233 /// of entry numbers satisfying the current selection.
4234 /// If option "entrylist" is used, a TEntryList object is created
4235 /// If the selection contains arrays, vectors or any container class and option
4236 /// "entrylistarray" is used, a TEntryListArray object is created
4237 /// containing also the subentries satisfying the selection, i.e. the indices of
4238 /// the branches which hold containers classes.
4239 /// Example:
4240 /// ~~~ {.cpp}
4241 /// tree.Draw(">>yplus","y>0")
4242 /// ~~~
4243 /// will create a TEventList object named "yplus" in the current directory.
4244 /// In an interactive session, one can type (after TTree::Draw)
4245 /// ~~~ {.cpp}
4246 /// yplus.Print("all")
4247 /// ~~~
4248 /// to print the list of entry numbers in the list.
4249 /// ~~~ {.cpp}
4250 /// tree.Draw(">>yplus", "y>0", "entrylist")
4251 /// ~~~
4252 /// will create a TEntryList object names "yplus" in the current directory
4253 /// ~~~ {.cpp}
4254 /// tree.Draw(">>yplus", "y>0", "entrylistarray")
4255 /// ~~~
4256 /// will create a TEntryListArray object names "yplus" in the current directory
4257 ///
4258 /// By default, the specified entry list is reset.
4259 /// To continue to append data to an existing list, use "+" in front
4260 /// of the list name;
4261 /// ~~~ {.cpp}
4262 /// tree.Draw(">>+yplus","y>0")
4263 /// ~~~
4264 /// will not reset yplus, but will enter the selected entries at the end
4265 /// of the existing list.
4266 ///
4267 /// ### Using a TEventList, TEntryList or TEntryListArray as Input
4268 ///
4269 /// Once a TEventList or a TEntryList object has been generated, it can be used as input
4270 /// for TTree::Draw. Use TTree::SetEventList or TTree::SetEntryList to set the
4271 /// current event list
4272 ///
4273 /// Example 1:
4274 /// ~~~ {.cpp}
4275 /// TEventList *elist = (TEventList*)gDirectory->Get("yplus");
4276 /// tree->SetEventList(elist);
4277 /// tree->Draw("py");
4278 /// ~~~
4279 /// Example 2:
4280 /// ~~~ {.cpp}
4281 /// TEntryList *elist = (TEntryList*)gDirectory->Get("yplus");
4282 /// tree->SetEntryList(elist);
4283 /// tree->Draw("py");
4284 /// ~~~
4285 /// If a TEventList object is used as input, a new TEntryList object is created
4286 /// inside the SetEventList function. In case of a TChain, all tree headers are loaded
4287 /// for this transformation. This new object is owned by the chain and is deleted
4288 /// with it, unless the user extracts it by calling GetEntryList() function.
4289 /// See also comments to SetEventList() function of TTree and TChain.
4290 ///
4291 /// If arrays are used in the selection criteria and TEntryListArray is not used,
4292 /// all the entries that have at least one element of the array that satisfy the selection
4293 /// are entered in the list.
4294 ///
4295 /// Example:
4296 /// ~~~ {.cpp}
4297 /// tree.Draw(">>pyplus","fTracks.fPy>0");
4298 /// tree->SetEventList(pyplus);
4299 /// tree->Draw("fTracks.fPy");
4300 /// ~~~
4301 /// will draw the fPy of ALL tracks in event with at least one track with
4302 /// a positive fPy.
4303 ///
4304 /// To select only the elements that did match the original selection
4305 /// use TEventList::SetReapplyCut or TEntryList::SetReapplyCut.
4306 ///
4307 /// Example:
4308 /// ~~~ {.cpp}
4309 /// tree.Draw(">>pyplus","fTracks.fPy>0");
4310 /// pyplus->SetReapplyCut(kTRUE);
4311 /// tree->SetEventList(pyplus);
4312 /// tree->Draw("fTracks.fPy");
4313 /// ~~~
4314 /// will draw the fPy of only the tracks that have a positive fPy.
4315 ///
4316 /// To draw only the elements that match a selection in case of arrays,
4317 /// you can also use TEntryListArray (faster in case of a more general selection).
4318 ///
4319 /// Example:
4320 /// ~~~ {.cpp}
4321 /// tree.Draw(">>pyplus","fTracks.fPy>0", "entrylistarray");
4322 /// tree->SetEntryList(pyplus);
4323 /// tree->Draw("fTracks.fPy");
4324 /// ~~~
4325 /// will draw the fPy of only the tracks that have a positive fPy,
4326 /// but without redoing the selection.
4327 ///
4328 /// Note: Use tree->SetEventList(0) if you do not want use the list as input.
4329 ///
4330 /// ### How to obtain more info from TTree::Draw
4331 ///
4332 /// Once TTree::Draw has been called, it is possible to access useful
4333 /// information still stored in the TTree object via the following functions:
4334 ///
4335 /// - GetSelectedRows() // return the number of values accepted by the selection expression. In case where no selection was specified, returns the number of values processed.
4336 /// - GetV1() // returns a pointer to the double array of V1
4337 /// - GetV2() // returns a pointer to the double array of V2
4338 /// - GetV3() // returns a pointer to the double array of V3
4339 /// - GetV4() // returns a pointer to the double array of V4
4340 /// - GetW() // returns a pointer to the double array of Weights where weight equal the result of the selection expression.
4341 ///
4342 /// where V1,V2,V3 correspond to the expressions in
4343 /// ~~~ {.cpp}
4344 /// TTree::Draw("V1:V2:V3:V4",selection);
4345 /// ~~~
4346 /// If the expression has more than 4 component use GetVal(index)
4347 ///
4348 /// Example:
4349 /// ~~~ {.cpp}
4350 /// Root > ntuple->Draw("py:px","pz>4");
4351 /// Root > TGraph *gr = new TGraph(ntuple->GetSelectedRows(),
4352 /// ntuple->GetV2(), ntuple->GetV1());
4353 /// Root > gr->Draw("ap"); //draw graph in current pad
4354 /// ~~~
4355 ///
4356 /// A more complete complete tutorial (treegetval.C) shows how to use the
4357 /// GetVal() method.
4358 ///
4359 /// creates a TGraph object with a number of points corresponding to the
4360 /// number of entries selected by the expression "pz>4", the x points of the graph
4361 /// being the px values of the Tree and the y points the py values.
4362 ///
4363 /// Important note: By default TTree::Draw creates the arrays obtained
4364 /// with GetW, GetV1, GetV2, GetV3, GetV4, GetVal with a length corresponding
4365 /// to the parameter fEstimate. The content will be the last `GetSelectedRows() % GetEstimate()`
4366 /// values calculated.
4367 /// By default fEstimate=1000000 and can be modified
4368 /// via TTree::SetEstimate. To keep in memory all the results (in case
4369 /// where there is only one result per entry), use
4370 /// ~~~ {.cpp}
4371 /// tree->SetEstimate(tree->GetEntries()+1); // same as tree->SetEstimate(-1);
4372 /// ~~~
4373 /// You must call SetEstimate if the expected number of selected rows
4374 /// you need to look at is greater than 1000000.
4375 ///
4376 /// You can use the option "goff" to turn off the graphics output
4377 /// of TTree::Draw in the above example.
4378 ///
4379 /// ### Automatic interface to TTree::Draw via the TTreeViewer
4380 ///
4381 /// A complete graphical interface to this function is implemented
4382 /// in the class TTreeViewer.
4383 /// To start the TTreeViewer, three possibilities:
4384 /// - select TTree context menu item "StartViewer"
4385 /// - type the command "TTreeViewer TV(treeName)"
4386 /// - execute statement "tree->StartViewer();"
4388 Long64_t TTree::Draw(const char* varexp, const char* selection, Option_t* option, Long64_t nentries, Long64_t firstentry)
4389 {
4390  GetPlayer();
4391  if (fPlayer)
4392  return fPlayer->DrawSelect(varexp,selection,option,nentries,firstentry);
4393  return -1;
4394 }
4395 
4396 ////////////////////////////////////////////////////////////////////////////////
4397 /// Remove some baskets from memory.
4399 void TTree::DropBaskets()
4400 {
4401  TBranch* branch = 0;
4403  for (Int_t i = 0; i < nb; ++i) {
4404  branch = (TBranch*) fBranches.UncheckedAt(i);
4405  branch->DropBaskets("all");
4406  }
4407 }
4408 
4409 ////////////////////////////////////////////////////////////////////////////////
4410 /// Drop branch buffers to accommodate nbytes below MaxVirtualsize.
4413 {
4414  // Be careful not to remove current read/write buffers.
4415  Int_t ndrop = 0;
4416  Int_t nleaves = fLeaves.GetEntriesFast();
4417  for (Int_t i = 0; i < nleaves; ++i) {
4418  TLeaf* leaf = (TLeaf*) fLeaves.UncheckedAt(i);
4419  TBranch* branch = (TBranch*) leaf->GetBranch();
4420  Int_t nbaskets = branch->GetListOfBaskets()->GetEntries();
4421  for (Int_t j = 0; j < nbaskets - 1; ++j) {
4422  if ((j == branch->GetReadBasket()) || (j == branch->GetWriteBasket())) {
4423  continue;
4424  }
4425  TBasket* basket = (TBasket*)branch->GetListOfBaskets()->UncheckedAt(j);
4426  if (basket) {
4427  ndrop += basket->DropBuffers();
4429  return;
4430  }
4431  }
4432  }
4433  }
4434 }
4435 
4436 ////////////////////////////////////////////////////////////////////////////////
4437 /// Fill all branches.
4438 ///
4439 /// This function loops on all the branches of this tree. For
4440 /// each branch, it copies to the branch buffer (basket) the current
4441 /// values of the leaves data types. If a leaf is a simple data type,
4442 /// a simple conversion to a machine independent format has to be done.
4443 ///
4444 /// This machine independent version of the data is copied into a
4445 /// basket (each branch has its own basket). When a basket is full
4446 /// (32k worth of data by default), it is then optionally compressed
4447 /// and written to disk (this operation is also called committing or
4448 /// 'flushing' the basket). The committed baskets are then
4449 /// immediately removed from memory.
4450 ///
4451 /// The function returns the number of bytes committed to the
4452 /// individual branches.
4453 ///
4454 /// If a write error occurs, the number of bytes returned is -1.
4455 ///
4456 /// If no data are written, because, e.g., the branch is disabled,
4457 /// the number of bytes returned is 0.
4458 ///
4459 /// __The baskets are flushed and the Tree header saved at regular intervals__
4460 ///
4461 /// At regular intervals, when the amount of data written so far is
4462 /// greater than fAutoFlush (see SetAutoFlush) all the baskets are flushed to disk.
4463 /// This makes future reading faster as it guarantees that baskets belonging to nearby
4464 /// entries will be on the same disk region.
4465 /// When the first call to flush the baskets happen, we also take this opportunity
4466 /// to optimize the baskets buffers.
4467 /// We also check if the amount of data written is greater than fAutoSave (see SetAutoSave).
4468 /// In this case we also write the Tree header. This makes the Tree recoverable up to this point
4469 /// in case the program writing the Tree crashes.
4470 /// The decisions to FlushBaskets and Auto Save can be made based either on the number
4471 /// of bytes written (fAutoFlush and fAutoSave negative) or on the number of entries
4472 /// written (fAutoFlush and fAutoSave positive).
4473 /// Note that the user can decide to call FlushBaskets and AutoSave in her event loop
4474 /// base on the number of events written instead of the number of bytes written.
4475 ///
4476 /// Note that calling FlushBaskets too often increases the IO time.
4477 ///
4478 /// Note that calling AutoSave too often increases the IO time and also the file size.
4481 {
4482  Int_t nbytes = 0;
4483  Int_t nwrite = 0;
4484  Int_t nerror = 0;
4485  Int_t nbranches = fBranches.GetEntriesFast();
4486 
4487  // Case of one single super branch. Automatically update
4488  // all the branch addresses if a new object was created.
4489  if (nbranches == 1)
4490  ((TBranch *)fBranches.UncheckedAt(0))->UpdateAddress();
4491 
4492  if (fBranchRef)
4493  fBranchRef->Clear();
4494 
4495 #ifdef R__USE_IMT
4496  const auto useIMT = ROOT::IsImplicitMTEnabled() && fIMTEnabled;
4498  if (useIMT) {
4499  fIMTFlush = true;
4500  fIMTZipBytes.store(0);
4501  fIMTTotBytes.store(0);
4502  }
4503 #endif
4504 
4505  for (Int_t i = 0; i < nbranches; ++i) {
4506  // Loop over all branches, filling and accumulating bytes written and error counts.
4507  TBranch *branch = (TBranch *)fBranches.UncheckedAt(i);
4508 
4509  if (branch->TestBit(kDoNotProcess))
4510  continue;
4511 
4512 #ifndef R__USE_IMT
4513  nwrite = branch->FillImpl(nullptr);
4514 #else
4515  nwrite = branch->FillImpl(useIMT ? &imtHelper : nullptr);
4516 #endif
4517  if (nwrite < 0) {
4518  if (nerror < 2) {
4519  Error("Fill", "Failed filling branch:%s.%s, nbytes=%d, entry=%lld\n"
4520  " This error is symptomatic of a Tree created as a memory-resident Tree\n"
4521  " Instead of doing:\n"
4522  " TTree *T = new TTree(...)\n"
4523  " TFile *f = new TFile(...)\n"
4524  " you should do:\n"
4525  " TFile *f = new TFile(...)\n"
4526  " TTree *T = new TTree(...)\n\n",
4527  GetName(), branch->GetName(), nwrite, fEntries + 1);
4528  } else {
4529  Error("Fill", "Failed filling branch:%s.%s, nbytes=%d, entry=%lld", GetName(), branch->GetName(), nwrite,
4530  fEntries + 1);
4531  }
4532  ++nerror;
4533  } else {
4534  nbytes += nwrite;
4535  }
4536  }
4537 
4538 #ifdef R__USE_IMT
4539  if (fIMTFlush) {
4540  imtHelper.Wait();
4541  fIMTFlush = false;
4542  const_cast<TTree *>(this)->AddTotBytes(fIMTTotBytes);
4543  const_cast<TTree *>(this)->AddZipBytes(fIMTZipBytes);
4544  nbytes += imtHelper.GetNbytes();
4545  nerror += imtHelper.GetNerrors();
4546  }
4547 #endif
4548 
4549  if (fBranchRef)
4550  fBranchRef->Fill();
4551 
4552  ++fEntries;
4553 
4554  if (fEntries > fMaxEntries)
4555  KeepCircular();
4556 
4557  if (gDebug > 0)
4558  Info("TTree::Fill", " - A: %d %lld %lld %lld %lld %lld %lld \n", nbytes, fEntries, fAutoFlush, fAutoSave,
4560 
4561  bool autoFlush = false;
4562  bool autoSave = false;
4563 
4564  if (fAutoFlush != 0 || fAutoSave != 0) {
4565  // Is it time to flush or autosave baskets?
4566  if (fFlushedBytes == 0) {
4567  // If fFlushedBytes == 0, it means we never flushed or saved, so
4568  // we need to check if it's time to do it and recompute the values
4569  // of fAutoFlush and fAutoSave in terms of the number of entries.
4570  // Decision can be based initially either on the number of bytes
4571  // or the number of entries written.
4572  Long64_t zipBytes = GetZipBytes();
4573 
4574  if (fAutoFlush)
4575  autoFlush = fAutoFlush < 0 ? (zipBytes > -fAutoFlush) : fEntries % fAutoFlush == 0;
4576 
4577  if (fAutoSave)
4578  autoSave = fAutoSave < 0 ? (zipBytes > -fAutoSave) : fEntries % fAutoSave == 0;
4579 
4580  if (autoFlush || autoSave) {
4581  // First call FlushBasket to make sure that fTotBytes is up to date.
4582  FlushBasketsImpl();
4583  autoFlush = false; // avoid auto flushing again later
4584 
4585  // When we are in one-basket-per-cluster mode, there is no need to optimize basket:
4586  // they will automatically grow to the size needed for an event cluster (with the basket
4587  // shrinking preventing them from growing too much larger than the actually-used space).
4589  OptimizeBaskets(GetTotBytes(), 1, "");
4590  if (gDebug > 0)
4591  Info("TTree::Fill", "OptimizeBaskets called at entry %lld, fZipBytes=%lld, fFlushedBytes=%lld\n",
4593  }
4595  fAutoFlush = fEntries; // Use test on entries rather than bytes
4596 
4597  // subsequently in run
4598  if (fAutoSave < 0) {
4599  // Set fAutoSave to the largest integer multiple of
4600  // fAutoFlush events such that fAutoSave*fFlushedBytes
4601  // < (minus the input value of fAutoSave)
4602  Long64_t totBytes = GetTotBytes();
4603  if (zipBytes != 0) {
4604  fAutoSave = TMath::Max(fAutoFlush, fEntries * ((-fAutoSave / zipBytes) / fEntries));
4605  } else if (totBytes != 0) {
4606  fAutoSave = TMath::Max(fAutoFlush, fEntries * ((-fAutoSave / totBytes) / fEntries));
4607  } else {
4608  TBufferFile b(TBuffer::kWrite, 10000);
4609  TTree::Class()->WriteBuffer(b, (TTree *)this);
4610  Long64_t total = b.Length();
4612  }
4613  } else if (fAutoSave > 0) {
4615  }
4616 
4617  if (fAutoSave != 0 && fEntries >= fAutoSave)
4618  autoSave = true;
4619 
4620  if (gDebug > 0)
4621  Info("TTree::Fill", "First AutoFlush. fAutoFlush = %lld, fAutoSave = %lld\n", fAutoFlush, fAutoSave);
4622  }
4623  } else {
4624  // Check if we need to auto flush
4625  if (fAutoFlush) {
4626  if (fNClusterRange == 0)
4627  autoFlush = fEntries > 1 && fEntries % fAutoFlush == 0;
4628  else
4629  autoFlush = (fEntries - (fClusterRangeEnd[fNClusterRange - 1] + 1)) % fAutoFlush == 0;
4630  }
4631  // Check if we need to auto save
4632  if (fAutoSave)
4633  autoSave = fEntries % fAutoSave == 0;
4634  }
4635  }
4636 
4637  if (autoFlush) {
4638  FlushBasketsImpl();
4639  if (gDebug > 0)
4640  Info("TTree::Fill", "FlushBaskets() called at entry %lld, fZipBytes=%lld, fFlushedBytes=%lld\n", fEntries,
4643  }
4644 
4645  if (autoSave) {
4646  AutoSave(); // does not call FlushBasketsImpl() again
4647  if (gDebug > 0)
4648  Info("TTree::Fill", "AutoSave called at entry %lld, fZipBytes=%lld, fSavedBytes=%lld\n", fEntries,
4650  }
4651 
4652  // Check that output file is still below the maximum size.
4653  // If above, close the current file and continue on a new file.
4654  // Currently, the automatic change of file is restricted
4655  // to the case where the tree is in the top level directory.
4656  if (fDirectory)
4657  if (TFile *file = fDirectory->GetFile())
4658  if ((TDirectory *)file == fDirectory && (file->GetEND() > fgMaxTreeSize))
4659  ChangeFile(file);
4660 
4661  return nerror == 0 ? nbytes : -1;
4662 }
4663 
4664 ////////////////////////////////////////////////////////////////////////////////
4665 /// Search in the array for a branch matching the branch name,
4666 /// with the branch possibly expressed as a 'full' path name (with dots).
4668 static TBranch *R__FindBranchHelper(TObjArray *list, const char *branchname) {
4669  if (list==0 || branchname == 0 || branchname[0] == '\0') return 0;
4670 
4671  Int_t nbranches = list->GetEntries();
4672 
4673  UInt_t brlen = strlen(branchname);
4674 
4675  for(Int_t index = 0; index < nbranches; ++index) {
4676  TBranch *where = (TBranch*)list->UncheckedAt(index);
4677 
4678  const char *name = where->GetName();
4679  UInt_t len = strlen(name);
4680  if (len && name[len-1]==']') {
4681  const char *dim = strchr(name,'[');
4682  if (dim) {
4683  len = dim - name;
4684  }
4685  }
4686  if (brlen == len && strncmp(branchname,name,len)==0) {
4687  return where;
4688  }
4689  TBranch *next = 0;
4690  if ((brlen >= len) && (branchname[len] == '.')
4691  && strncmp(name, branchname, len) == 0) {
4692  // The prefix subbranch name match the branch name.
4693 
4694  next = where->FindBranch(branchname);
4695  if (!next) {
4696  next = where->FindBranch(branchname+len+1);
4697  }
4698  if (next) return next;
4699  }
4700  const char *dot = strchr((char*)branchname,'.');
4701  if (dot) {
4702  if (len==(size_t)(dot-branchname) &&
4703  strncmp(branchname,name,dot-branchname)==0 ) {
4704  return R__FindBranchHelper(where->GetListOfBranches(),dot+1);
4705  }
4706  }
4707  }
4708  return 0;
4709 }
4710 
4711 ////////////////////////////////////////////////////////////////////////////////
4712 /// Return the branch that correspond to the path 'branchname', which can
4713 /// include the name of the tree or the omitted name of the parent branches.
4714 /// In case of ambiguity, returns the first match.
4716 TBranch* TTree::FindBranch(const char* branchname)
4717 {
4718  // We already have been visited while recursively looking
4719  // through the friends tree, let return
4721  return 0;
4722  }
4723 
4724  TBranch* branch = 0;
4725  // If the first part of the name match the TTree name, look for the right part in the
4726  // list of branches.
4727  // This will allow the branchname to be preceded by
4728  // the name of this tree.
4729  if (strncmp(fName.Data(),branchname,fName.Length())==0 && branchname[fName.Length()]=='.') {
4730  branch = R__FindBranchHelper( GetListOfBranches(), branchname + fName.Length() + 1);
4731  if (branch) return branch;
4732  }
4733  // If we did not find it, let's try to find the full name in the list of branches.
4734  branch = R__FindBranchHelper(GetListOfBranches(), branchname);
4735  if (branch) return branch;
4736 
4737  // If we still did not find, let's try to find it within each branch assuming it does not the branch name.
4738  TIter next(GetListOfBranches());
4739  while ((branch = (TBranch*) next())) {
4740  TBranch* nestedbranch = branch->FindBranch(branchname);
4741  if (nestedbranch) {
4742  return nestedbranch;
4743  }
4744  }
4745 
4746  // Search in list of friends.
4747  if (!fFriends) {
4748  return 0;
4749  }
4750  TFriendLock lock(this, kFindBranch);
4751  TIter nextf(fFriends);
4752  TFriendElement* fe = 0;
4753  while ((fe = (TFriendElement*) nextf())) {
4754  TTree* t = fe->GetTree();
4755  if (!t) {
4756  continue;
4757  }
4758  // If the alias is present replace it with the real name.
4759  const char *subbranch = strstr(branchname, fe->GetName());
4760  if (subbranch != branchname) {
4761  subbranch = 0;
4762  }
4763  if (subbranch) {
4764  subbranch += strlen(fe->GetName());
4765  if (*subbranch != '.') {
4766  subbranch = 0;
4767  } else {
4768  ++subbranch;
4769  }
4770  }
4771  std::ostringstream name;
4772  if (subbranch) {
4773  name << t->GetName() << "." << subbranch;
4774  } else {
4775  name << branchname;
4776  }
4777  branch = t->FindBranch(name.str().c_str());
4778  if (branch) {
4779  return branch;
4780  }
4781  }
4782  return 0;
4783 }
4784 
4785 ////////////////////////////////////////////////////////////////////////////////
4786 /// Find leaf..
4788 TLeaf* TTree::FindLeaf(const char* searchname)
4789 {
4790  // We already have been visited while recursively looking
4791  // through the friends tree, let's return.
4792  if (kFindLeaf & fFriendLockStatus) {
4793  return 0;
4794  }
4795 
4796  // This will allow the branchname to be preceded by
4797  // the name of this tree.
4798  char* subsearchname = (char*) strstr(searchname, GetName());
4799  if (subsearchname != searchname) {
4800  subsearchname = 0;
4801  }
4802  if (subsearchname) {
4803  subsearchname += strlen(GetName());
4804  if (*subsearchname != '.') {
4805  subsearchname = 0;
4806  } else {
4807  ++subsearchname;
4808  if (subsearchname[0]==0) {
4809  subsearchname = 0;
4810  }
4811  }
4812  }
4813 
4814  TString leafname;
4815  TString leaftitle;
4816  TString longname;
4817  TString longtitle;
4818 
4819  const bool searchnameHasDot = strchr(searchname, '.') != nullptr;
4820 
4821  // For leaves we allow for one level up to be prefixed to the name.
4822  TIter next(GetListOfLeaves());
4823  TLeaf* leaf = 0;
4824  while ((leaf = (TLeaf*) next())) {
4825  leafname = leaf->GetName();
4826  Ssiz_t dim = leafname.First('[');
4827  if (dim >= 0) leafname.Remove(dim);
4828 
4829  if (leafname == searchname) {
4830  return leaf;
4831  }
4832  if (subsearchname && leafname == subsearchname) {
4833  return leaf;
4834  }
4835  // The TLeafElement contains the branch name
4836  // in its name, let's use the title.
4837  leaftitle = leaf->GetTitle();
4838  dim = leaftitle.First('[');
4839  if (dim >= 0) leaftitle.Remove(dim);
4840 
4841  if (leaftitle == searchname) {
4842  return leaf;
4843  }
4844  if (subsearchname && leaftitle == subsearchname) {
4845  return leaf;
4846  }
4847  if (!searchnameHasDot)
4848  continue;
4849  TBranch* branch = leaf->GetBranch();
4850  if (branch) {
4851  longname.Form("%s.%s",branch->GetName(),leafname.Data());
4852  dim = longname.First('[');
4853  if (dim>=0) longname.Remove(dim);
4854  if (longname == searchname) {
4855  return leaf;
4856  }
4857  if (subsearchname && longname == subsearchname) {
4858  return leaf;
4859  }
4860  longtitle.Form("%s.%s",branch->GetName(),leaftitle.Data());
4861  dim = longtitle.First('[');
4862  if (dim>=0) longtitle.Remove(dim);
4863  if (longtitle == searchname) {
4864  return leaf;
4865  }
4866  if (subsearchname && longtitle == subsearchname) {
4867  return leaf;
4868  }
4869  // The following is for the case where the branch is only
4870  // a sub-branch. Since we do not see it through
4871  // TTree::GetListOfBranches, we need to see it indirectly.
4872  // This is the less sturdy part of this search ... it may
4873  // need refining ...
4874  if (strstr(searchname, ".") && !strcmp(searchname, branch->GetName())) {
4875  return leaf;
4876  }
4877  if (subsearchname && strstr(subsearchname, ".") && !strcmp(subsearchname, branch->GetName())) {
4878  return leaf;
4879  }
4880  }
4881  }
4882  // Search in list of friends.
4883  if (!fFriends) {
4884  return 0;
4885  }
4886  TFriendLock lock(this, kFindLeaf);
4887  TIter nextf(fFriends);
4888  TFriendElement* fe = 0;
4889  while ((fe = (TFriendElement*) nextf())) {
4890  TTree* t = fe->GetTree();
4891  if (!t) {
4892  continue;
4893  }
4894  // If the alias is present replace it with the real name.
4895  subsearchname = (char*) strstr(searchname, fe->GetName());
4896  if (subsearchname != searchname) {
4897  subsearchname = 0;
4898  }
4899  if (subsearchname) {
4900  subsearchname += strlen(fe->GetName());
4901  if (*subsearchname != '.') {
4902  subsearchname = 0;
4903  } else {
4904  ++subsearchname;
4905  }
4906  }
4907  if (subsearchname) {
4908  leafname.Form("%s.%s",t->GetName(),subsearchname);
4909  } else {
4910  leafname = searchname;
4911  }
4912  leaf = t->FindLeaf(leafname);
4913  if (leaf) {
4914  return leaf;
4915  }
4916  }
4917  return 0;
4918 }
4919 
4920 ////////////////////////////////////////////////////////////////////////////////
4921 /// Fit a projected item(s) from a tree.
4922 ///
4923 /// funcname is a TF1 function.
4924 ///
4925 /// See TTree::Draw() for explanations of the other parameters.
4926 ///
4927 /// By default the temporary histogram created is called htemp.
4928 /// If varexp contains >>hnew , the new histogram created is called hnew
4929 /// and it is kept in the current directory.
4930 ///
4931 /// The function returns the number of selected entries.
4932 ///
4933 /// Example:
4934 /// ~~~ {.cpp}
4935 /// tree.Fit(pol4,"sqrt(x)>>hsqrt","y>0")
4936 /// ~~~
4937 /// will fit sqrt(x) and save the histogram as "hsqrt" in the current
4938 /// directory.
4939 ///
4940 /// See also TTree::UnbinnedFit
4941 ///
4942 /// ## Return status
4943 ///
4944 /// The function returns the status of the histogram fit (see TH1::Fit)
4945 /// If no entries were selected, the function returns -1;
4946 /// (i.e. fitResult is null if the fit is OK)
4948 Int_t TTree::Fit(const char* funcname, const char* varexp, const char* selection, Option_t* option, Option_t* goption, Long64_t nentries, Long64_t firstentry)
4949 {
4950  GetPlayer();
4951  if (fPlayer) {
4952  return fPlayer->Fit(funcname, varexp, selection, option, goption, nentries, firstentry);
4953  }
4954  return -1;
4955 }
4956 
4957 namespace {
4958 struct BoolRAIIToggle {
4959  Bool_t &m_val;
4960 
4961  BoolRAIIToggle(Bool_t &val) : m_val(val) { m_val = true; }
4962  ~BoolRAIIToggle() { m_val = false; }
4963 };
4964 }
4965 
4966 ////////////////////////////////////////////////////////////////////////////////
4967 /// Write to disk all the basket that have not yet been individually written and
4968 /// create an event cluster boundary (by default).
4969 ///
4970 /// If the caller wishes to flush the baskets but not create an event cluster,
4971 /// then set create_cluster to false.
4972 ///
4973 /// If ROOT has IMT-mode enabled, this will launch multiple TBB tasks in parallel
4974 /// via TThreadExecutor to do this operation; one per basket compression. If the
4975 /// caller utilizes TBB also, care must be taken to prevent deadlocks.
4976 ///
4977 /// For example, let's say the caller holds mutex A and calls FlushBaskets; while
4978 /// TBB is waiting for the ROOT compression tasks to complete, it may decide to
4979 /// run another one of the user's tasks in this thread. If the second user task
4980 /// tries to acquire A, then a deadlock will occur. The example call sequence
4981 /// looks like this:
4982 ///
4983 /// - User acquires mutex A
4984 /// - User calls FlushBaskets.
4985 /// - ROOT launches N tasks and calls wait.
4986 /// - TBB schedules another user task, T2.
4987 /// - T2 tries to acquire mutex A.
4988 ///
4989 /// At this point, the thread will deadlock: the code may function with IMT-mode
4990 /// disabled if the user assumed the legacy code never would run their own TBB
4991 /// tasks.
4992 ///
4993 /// SO: users of TBB who want to enable IMT-mode should carefully review their
4994 /// locking patterns and make sure they hold no coarse-grained application
4995 /// locks when they invoke ROOT.
4996 ///
4997 /// Return the number of bytes written or -1 in case of write error.
4998 Int_t TTree::FlushBaskets(Bool_t create_cluster) const
4999 {
5000  Int_t retval = FlushBasketsImpl();
5001  if (retval == -1) return retval;
5002 
5003  if (create_cluster) const_cast<TTree *>(this)->MarkEventCluster();
5004  return retval;
5005 }
5006 
5007 ////////////////////////////////////////////////////////////////////////////////
5008 /// Internal implementation of the FlushBaskets algorithm.
5009 /// Unlike the public interface, this does NOT create an explicit event cluster
5010 /// boundary; it is up to the (internal) caller to determine whether that should
5011 /// done.
5012 ///
5013 /// Otherwise, the comments for FlushBaskets applies.
5014 ///
5016 {
5017  if (!fDirectory) return 0;
5018  Int_t nbytes = 0;
5019  Int_t nerror = 0;
5020  TObjArray *lb = const_cast<TTree*>(this)->GetListOfBranches();
5021  Int_t nb = lb->GetEntriesFast();
5022 
5023 #ifdef R__USE_IMT
5024  const auto useIMT = ROOT::IsImplicitMTEnabled() && fIMTEnabled;
5025  if (useIMT) {
5026  // ROOT-9668: here we need to check if the size of fSortedBranches is different from the
5027  // size of the list of branches before triggering the initialisation of the fSortedBranches
5028  // container to cover two cases:
5029  // 1. This is the first time we flush. fSortedBranches is empty and we need to fill it.
5030  // 2. We flushed at least once already but a branch has been be added to the tree since then
5031  if (fSortedBranches.size() != unsigned(nb)) { const_cast<TTree*>(this)->InitializeBranchLists(false); }
5032 
5033  BoolRAIIToggle sentry(fIMTFlush);
5034  fIMTZipBytes.store(0);
5035  fIMTTotBytes.store(0);
5036  std::atomic<Int_t> nerrpar(0);
5037  std::atomic<Int_t> nbpar(0);
5038  std::atomic<Int_t> pos(0);
5039 
5040  auto mapFunction = [&]() {
5041  // The branch to process is obtained when the task starts to run.
5042  // This way, since branches are sorted, we make sure that branches
5043  // leading to big tasks are processed first. If we assigned the
5044  // branch at task creation time, the scheduler would not necessarily
5045  // respect our sorting.
5046  Int_t j = pos.fetch_add(1);
5047 
5048  auto branch = fSortedBranches[j].second;
5049  if (R__unlikely(!branch)) { return; }
5050 
5051  if (R__unlikely(gDebug > 0)) {
5052  std::stringstream ss;
5053  ss << std::this_thread::get_id();
5054  Info("FlushBaskets", "[IMT] Thread %s", ss.str().c_str());
5055  Info("FlushBaskets", "[IMT] Running task for branch #%d: %s", j, branch->GetName());
5056  }
5057 
5058  Int_t nbtask = branch->FlushBaskets();
5059 
5060  if (nbtask < 0) { nerrpar++; }
5061  else { nbpar += nbtask; }
5062  };
5063 
5064  ROOT::TThreadExecutor pool;
5065  pool.Foreach(mapFunction, nb);
5066 
5067  fIMTFlush = false;
5068  const_cast<TTree*>(this)->AddTotBytes(fIMTTotBytes);
5069  const_cast<TTree*>(this)->AddZipBytes(fIMTZipBytes);
5070 
5071  return nerrpar ? -1 : nbpar.load();
5072  }
5073 #endif
5074  for (Int_t j = 0; j < nb; j++) {
5075  TBranch* branch = (TBranch*) lb->UncheckedAt(j);
5076  if (branch) {
5077  Int_t nwrite = branch->FlushBaskets();
5078  if (nwrite<0) {
5079  ++nerror;
5080  } else {
5081  nbytes += nwrite;
5082  }
5083  }
5084  }
5085  if (nerror) {
5086  return -1;
5087  } else {
5088  return nbytes;
5089  }
5090 }
5091 
5092 ////////////////////////////////////////////////////////////////////////////////
5093 /// Returns the expanded value of the alias. Search in the friends if any.
5095 const char* TTree::GetAlias(const char* aliasName) const
5096 {
5097  // We already have been visited while recursively looking
5098  // through the friends tree, let's return.
5099  if (kGetAlias & fFriendLockStatus) {
5100  return 0;
5101  }
5102  if (fAliases) {
5103  TObject* alias = fAliases->FindObject(aliasName);
5104  if (alias) {
5105  return alias->GetTitle();
5106  }
5107  }
5108  if (!fFriends) {
5109  return 0;
5110  }
5111  TFriendLock lock(const_cast<TTree*>(this), kGetAlias);
5112  TIter nextf(fFriends);
5113  TFriendElement* fe = 0;
5114  while ((fe = (TFriendElement*) nextf())) {
5115  TTree* t = fe->GetTree();
5116  if (t) {
5117  const char* alias = t->GetAlias(aliasName);
5118  if (alias) {
5119  return alias;
5120  }
5121  const char* subAliasName = strstr(aliasName, fe->GetName());
5122  if (subAliasName && (subAliasName[strlen(fe->GetName())] == '.')) {
5123  alias = t->GetAlias(aliasName + strlen(fe->GetName()) + 1);
5124  if (alias) {
5125  return alias;
5126  }
5127  }
5128  }
5129  }
5130  return 0;
5131 }
5132 
5133 ////////////////////////////////////////////////////////////////////////////////
5134 /// Return pointer to the branch with the given name in this tree or its friends.
5136 TBranch* TTree::GetBranch(const char* name)
5137 {
5138  if (name == 0) return 0;
5139 
5140  // We already have been visited while recursively
5141  // looking through the friends tree, let's return.
5142  if (kGetBranch & fFriendLockStatus) {
5143  return 0;
5144  }
5145 
5146  // Search using branches.
5148  for (Int_t i = 0; i < nb; i++) {
5149  TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
5150  if (!branch) {
5151  continue;
5152  }
5153  if (!strcmp(branch->GetName(), name)) {
5154  return branch;
5155  }
5156  TObjArray* lb = branch->GetListOfBranches();
5157  Int_t nb1 = lb->GetEntriesFast();
5158  for (Int_t j = 0; j < nb1; j++) {
5159  TBranch* b1 = (TBranch*) lb->UncheckedAt(j);
5160  if (!strcmp(b1->GetName(), name)) {
5161  return b1;
5162  }
5163  TObjArray* lb1 = b1->GetListOfBranches();
5164  Int_t nb2 = lb1->GetEntriesFast();
5165  for (Int_t k = 0; k < nb2; k++) {
5166  TBranch* b2 = (TBranch*) lb1->UncheckedAt(k);
5167  if (!strcmp(b2->GetName(), name)) {
5168  return b2;
5169  }
5170  }
5171  }
5172  }
5173 
5174  // Search using leaves.
5175  TObjArray* leaves = GetListOfLeaves();
5176  Int_t nleaves = leaves->GetEntriesFast();
5177  for (Int_t i = 0; i < nleaves; i++) {
5178  TLeaf* leaf = (TLeaf*) leaves->UncheckedAt(i);
5179  TBranch* branch = leaf->GetBranch();
5180  if (!strcmp(branch->GetName(), name)) {
5181  return branch;
5182  }
5183  }
5184 
5185  if (!fFriends) {
5186  return 0;
5187  }
5188 
5189  // Search in list of friends.
5190  TFriendLock lock(this, kGetBranch);
5191  TIter next(fFriends);
5192  TFriendElement* fe = 0;
5193  while ((fe = (TFriendElement*) next())) {
5194  TTree* t = fe->GetTree();
5195  if (t) {
5196  TBranch* branch = t->GetBranch(name);
5197  if (branch) {
5198  return branch;
5199  }
5200  }
5201  }
5202 
5203  // Second pass in the list of friends when
5204  // the branch name is prefixed by the tree name.
5205  next.Reset();
5206  while ((fe = (TFriendElement*) next())) {
5207  TTree* t = fe->GetTree();
5208  if (!t) {
5209  continue;
5210  }
5211  char* subname = (char*) strstr(name, fe->GetName());
5212  if (subname != name) {
5213  continue;
5214  }
5215  Int_t l = strlen(fe->GetName());
5216  subname += l;
5217  if (*subname != '.') {
5218  continue;
5219  }
5220  subname++;
5221  TBranch* branch = t->GetBranch(subname);
5222  if (branch) {
5223  return branch;
5224  }
5225  }
5226  return 0;
5227 }
5228 
5229 ////////////////////////////////////////////////////////////////////////////////
5230 /// Return status of branch with name branchname.
5231 ///
5232 /// - 0 if branch is not activated
5233 /// - 1 if branch is activated
5235 Bool_t TTree::GetBranchStatus(const char* branchname) const
5236 {
5237  TBranch* br = const_cast<TTree*>(this)->GetBranch(branchname);
5238  if (br) {
5239  return br->TestBit(kDoNotProcess) == 0;
5240  }
5241  return 0;
5242 }
5243 
5244 ////////////////////////////////////////////////////////////////////////////////
5245 /// Static function returning the current branch style.
5246 ///
5247 /// - style = 0 old Branch
5248 /// - style = 1 new Bronch
5251 {
5252  return fgBranchStyle;
5253 }
5254 
5255 ////////////////////////////////////////////////////////////////////////////////
5256 /// Used for automatic sizing of the cache.
5257 ///
5258 /// Estimates a suitable size for the tree cache based on AutoFlush.
5259 /// A cache sizing factor is taken from the configuration. If this yields zero
5260 /// and withDefault is true the historical algorithm for default size is used.
5262 Long64_t TTree::GetCacheAutoSize(Bool_t withDefault /* = kFALSE */ ) const
5263 {
5264  const char *stcs;
5265  Double_t cacheFactor = 0.0;
5266  if (!(stcs = gSystem->Getenv("ROOT_TTREECACHE_SIZE")) || !*stcs) {
5267  cacheFactor = gEnv->GetValue("TTreeCache.Size", 1.0);
5268  } else {
5269  cacheFactor = TString(stcs).Atof();
5270  }
5271 
5272  if (cacheFactor < 0.0) {
5273  // ignore negative factors
5274  cacheFactor = 0.0;
5275  }
5276 
5277  Long64_t cacheSize = 0;
5278 
5279  if (fAutoFlush < 0) cacheSize = Long64_t(-cacheFactor*fAutoFlush);
5280  else if (fAutoFlush == 0) cacheSize = 0;
5281  else cacheSize = Long64_t(cacheFactor*1.5*fAutoFlush*GetZipBytes()/(fEntries+1));
5282 
5283  if (cacheSize >= (INT_MAX / 4)) {
5284  cacheSize = INT_MAX / 4;
5285  }
5286 
5287  if (cacheSize < 0) {
5288  cacheSize = 0;
5289  }
5290 
5291  if (cacheSize == 0 && withDefault) {
5292  if (fAutoFlush < 0) cacheSize = -fAutoFlush;
5293  else if (fAutoFlush == 0) cacheSize = 0;
5294  else cacheSize = Long64_t(1.5*fAutoFlush*GetZipBytes()/(fEntries+1));
5295  }
5296 
5297  return cacheSize;
5298 }
5299 
5300 ////////////////////////////////////////////////////////////////////////////////
5301 /// Return an iterator over the cluster of baskets starting at firstentry.
5302 ///
5303 /// This iterator is not yet supported for TChain object.
5304 /// ~~~ {.cpp}
5305 /// TTree::TClusterIterator clusterIter = tree->GetClusterIterator(entry);
5306 /// Long64_t clusterStart;
5307 /// while( (clusterStart = clusterIter()) < tree->GetEntries() ) {
5308 /// printf("The cluster starts at %lld and ends at %lld (inclusive)\n",clusterStart,clusterIter.GetNextEntry()-1);
5309 /// }
5310 /// ~~~
5313 {
5314  // create cache if wanted
5315  if (fCacheDoAutoInit)
5316  SetCacheSizeAux();
5317 
5318  return TClusterIterator(this,firstentry);
5319 }
5320 
5321 ////////////////////////////////////////////////////////////////////////////////
5322 /// Return pointer to the current file.
5325 {
5326  if (!fDirectory || fDirectory==gROOT) {
5327  return 0;
5328  }
5329  return fDirectory->GetFile();
5330 }
5331 
5332 ////////////////////////////////////////////////////////////////////////////////
5333 /// Return the number of entries matching the selection.
5334 /// Return -1 in case of errors.
5335 ///
5336 /// If the selection uses any arrays or containers, we return the number
5337 /// of entries where at least one element match the selection.
5338 /// GetEntries is implemented using the selector class TSelectorEntries,
5339 /// which can be used directly (see code in TTreePlayer::GetEntries) for
5340 /// additional option.
5341 /// If SetEventList was used on the TTree or TChain, only that subset
5342 /// of entries will be considered.
5344 Long64_t TTree::GetEntries(const char *selection)
5345 {
5346  GetPlayer();
5347  if (fPlayer) {
5348  return fPlayer->GetEntries(selection);
5349  }
5350  return -1;
5351 }
5352 
5353 ////////////////////////////////////////////////////////////////////////////////
5354 /// Return pointer to the 1st Leaf named name in any Branch of this Tree or
5355 /// any branch in the list of friend trees.
5358 {
5359  if (fEntries) return fEntries;
5360  if (!fFriends) return 0;
5362  if (!fr) return 0;
5363  TTree *t = fr->GetTree();
5364  if (t==0) return 0;
5365  return t->GetEntriesFriend();
5366 }
5367 
5368 ////////////////////////////////////////////////////////////////////////////////
5369 /// Read all branches of entry and return total number of bytes read.
5370 ///
5371 /// - `getall = 0` : get only active branches
5372 /// - `getall = 1` : get all branches
5373 ///
5374 /// The function returns the number of bytes read from the input buffer.
5375 /// If entry does not exist the function returns 0.
5376 /// If an I/O error occurs, the function returns -1.
5377 ///
5378 /// If the Tree has friends, also read the friends entry.
5379 ///
5380 /// To activate/deactivate one or more branches, use TBranch::SetBranchStatus
5381 /// For example, if you have a Tree with several hundred branches, and you
5382 /// are interested only by branches named "a" and "b", do
5383 /// ~~~ {.cpp}
5384 /// mytree.SetBranchStatus("*",0); //disable all branches
5385 /// mytree.SetBranchStatus("a",1);
5386 /// mytree.SetBranchStatus("b",1);
5387 /// ~~~
5388 /// when calling mytree.GetEntry(i); only branches "a" and "b" will be read.
5389 ///
5390 /// __WARNING!!__
5391 /// If your Tree has been created in split mode with a parent branch "parent.",
5392 /// ~~~ {.cpp}
5393 /// mytree.SetBranchStatus("parent",1);
5394 /// ~~~
5395 /// will not activate the sub-branches of "parent". You should do:
5396 /// ~~~ {.cpp}
5397 /// mytree.SetBranchStatus("parent*",1);
5398 /// ~~~
5399 /// Without the trailing dot in the branch creation you have no choice but to
5400 /// call SetBranchStatus explicitly for each of the sub branches.
5401 ///
5402 /// An alternative is to call directly
5403 /// ~~~ {.cpp}
5404 /// brancha.GetEntry(i)
5405 /// branchb.GetEntry(i);
5406 /// ~~~
5407 /// ## IMPORTANT NOTE
5408 ///
5409 /// By default, GetEntry reuses the space allocated by the previous object
5410 /// for each branch. You can force the previous object to be automatically
5411 /// deleted if you call mybranch.SetAutoDelete(kTRUE) (default is kFALSE).
5412 ///
5413 /// Example:
5414 ///
5415 /// Consider the example in $ROOTSYS/test/Event.h
5416 /// The top level branch in the tree T is declared with:
5417 /// ~~~ {.cpp}
5418 /// Event *event = 0; //event must be null or point to a valid object
5419 /// //it must be initialized
5420 /// T.SetBranchAddress("event",&event);
5421 /// ~~~
5422 /// When reading the Tree, one can choose one of these 3 options:
5423 ///
5424 /// ## OPTION 1
5425 ///
5426 /// ~~~ {.cpp}
5427 /// for (Long64_t i=0;i<nentries;i++) {
5428 /// T.GetEntry(i);
5429 /// // the object event has been filled at this point
5430 /// }
5431 /// ~~~
5432 /// The default (recommended). At the first entry an object of the class
5433 /// Event will be created and pointed by event. At the following entries,
5434 /// event will be overwritten by the new data. All internal members that are
5435 /// TObject* are automatically deleted. It is important that these members
5436 /// be in a valid state when GetEntry is called. Pointers must be correctly
5437 /// initialized. However these internal members will not be deleted if the
5438 /// characters "->" are specified as the first characters in the comment
5439 /// field of the data member declaration.
5440 ///
5441 /// If "->" is specified, the pointer member is read via pointer->Streamer(buf).
5442 /// In this case, it is assumed that the pointer is never null (case of
5443 /// pointer TClonesArray *fTracks in the Event example). If "->" is not
5444 /// specified, the pointer member is read via buf >> pointer. In this case
5445 /// the pointer may be null. Note that the option with "->" is faster to
5446 /// read or write and it also consumes less space in the file.
5447 ///
5448 /// ## OPTION 2
5449 ///
5450 /// The option AutoDelete is set
5451 /// ~~~ {.cpp}
5452 /// TBranch *branch = T.GetBranch("event");
5453 /// branch->SetAddress(&event);
5454 /// branch->SetAutoDelete(kTRUE);
5455 /// for (Long64_t i=0;i<nentries;i++) {
5456 /// T.GetEntry(i);
5457 /// // the object event has been filled at this point
5458 /// }
5459 /// ~~~
5460 /// In this case, at each iteration, the object event is deleted by GetEntry
5461 /// and a new instance of Event is created and filled.
5462 ///
5463 /// ## OPTION 3
5464 ///
5465 /// ~~~ {.cpp}
5466 /// Same as option 1, but you delete yourself the event.
5467 ///
5468 /// for (Long64_t i=0;i<nentries;i++) {
5469 /// delete event;
5470 /// event = 0; // EXTREMELY IMPORTANT
5471 /// T.GetEntry(i);
5472 /// // the object event has been filled at this point
5473 /// }
5474 /// ~~~
5475 /// It is strongly recommended to use the default option 1. It has the
5476 /// additional advantage that functions like TTree::Draw (internally calling
5477 /// TTree::GetEntry) will be functional even when the classes in the file are
5478 /// not available.
5479 ///
5480 /// Note: See the comments in TBranchElement::SetAddress() for the
5481 /// object ownership policy of the underlying (user) data.
5483 Int_t TTree::GetEntry(Long64_t entry, Int_t getall)
5484 {
5485 
5486  // We already have been visited while recursively looking
5487  // through the friends tree, let return
5488  if (kGetEntry & fFriendLockStatus) return 0;
5489 
5490  if (entry < 0 || entry >= fEntries) return 0;
5491  Int_t i;
5492  Int_t nbytes = 0;
5493  fReadEntry = entry;
5494 
5495  // create cache if wanted
5496  if (fCacheDoAutoInit)
5497  SetCacheSizeAux();
5498 
5499  Int_t nbranches = fBranches.GetEntriesUnsafe();
5500  Int_t nb=0;
5501 
5502  auto seqprocessing = [&]() {
5503  TBranch *branch;
5504  for (i=0;i<nbranches;i++) {
5505  branch = (TBranch*)fBranches.UncheckedAt(i);
5506  nb = branch->GetEntry(entry, getall);
5507  if (nb < 0) break;
5508  nbytes += nb;
5509  }
5510  };
5511 
5512 #ifdef R__USE_IMT
5514  if (fSortedBranches.empty())
5515  InitializeBranchLists(true);
5516 
5517  // Count branches are processed first and sequentially
5518  for (auto branch : fSeqBranches) {
5519  nb = branch->GetEntry(entry, getall);
5520  if (nb < 0) break;
5521  nbytes += nb;
5522  }
5523  if (nb < 0) return nb;
5524 
5525  // Enable this IMT use case (activate its locks)
5527 
5528  Int_t errnb = 0;
5529  std::atomic<Int_t> pos(0);
5530  std::atomic<Int_t> nbpar(0);
5531 
5532  auto mapFunction = [&]() {
5533  // The branch to process is obtained when the task starts to run.
5534  // This way, since branches are sorted, we make sure that branches
5535  // leading to big tasks are processed first. If we assigned the
5536  // branch at task creation time, the scheduler would not necessarily
5537  // respect our sorting.
5538  Int_t j = pos.fetch_add(1);
5539 
5540  Int_t nbtask = 0;
5541  auto branch = fSortedBranches[j].second;
5542 
5543  if (gDebug > 0) {
5544  std::stringstream ss;
5545  ss << std::this_thread::get_id();
5546  Info("GetEntry", "[IMT] Thread %s", ss.str().c_str());
5547  Info("GetEntry", "[IMT] Running task for branch #%d: %s", j, branch->GetName());
5548  }
5549 
5550  std::chrono::time_point<std::chrono::system_clock> start, end;
5551 
5552  start = std::chrono::system_clock::now();
5553  nbtask = branch->GetEntry(entry, getall);
5554  end = std::chrono::system_clock::now();
5555 
5556  Long64_t tasktime = (Long64_t)std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
5557  fSortedBranches[j].first += tasktime;
5558 
5559  if (nbtask < 0) errnb = nbtask;
5560  else nbpar += nbtask;
5561  };
5562 
5563  ROOT::TThreadExecutor pool;
5564  pool.Foreach(mapFunction, fSortedBranches.size());
5565 
5566  if (errnb < 0) {
5567  nb = errnb;
5568  }
5569  else {
5570  // Save the number of bytes read by the tasks
5571  nbytes += nbpar;
5572 
5573  // Re-sort branches if necessary
5577  }
5578  }
5579  }
5580  else {
5581  seqprocessing();
5582  }
5583 #else
5584  seqprocessing();
5585 #endif
5586  if (nb < 0) return nb;
5587 
5588  // GetEntry in list of friends
5589  if (!fFriends) return nbytes;
5590  TFriendLock lock(this,kGetEntry);
5591  TIter nextf(fFriends);
5592  TFriendElement *fe;
5593  while ((fe = (TFriendElement*)nextf())) {
5594  TTree *t = fe->GetTree();
5595  if (t) {
5597  nb = t->GetEntry(t->GetReadEntry(),getall);
5598  } else {
5599  if ( t->LoadTreeFriend(entry,this) >= 0 ) {
5600  nb = t->GetEntry(t->GetReadEntry(),getall);
5601  } else nb = 0;
5602  }
5603  if (nb < 0) return nb;
5604  nbytes += nb;
5605  }
5606  }
5607  return nbytes;
5608 }
5609 
5610 
5611 ////////////////////////////////////////////////////////////////////////////////
5612 /// Divides the top-level branches into two vectors: (i) branches to be
5613 /// processed sequentially and (ii) branches to be processed in parallel.
5614 /// Even if IMT is on, some branches might need to be processed first and in a
5615 /// sequential fashion: in the parallelization of GetEntry, those are the
5616 /// branches that store the size of another branch for every entry
5617 /// (e.g. the size of an array branch). If such branches were processed
5618 /// in parallel with the rest, there could be two threads invoking
5619 /// TBranch::GetEntry on one of them at the same time, since a branch that
5620 /// depends on a size (or count) branch will also invoke GetEntry on the latter.
5621 /// This method can be invoked several times during the event loop if the TTree
5622 /// is being written, for example when adding new branches. In these cases, the
5623 /// `checkLeafCount` parameter is false.
5624 /// \param[in] checkLeafCount True if we need to check whether some branches are
5625 /// count leaves.
5627 void TTree::InitializeBranchLists(bool checkLeafCount)
5628 {
5629  Int_t nbranches = fBranches.GetEntriesFast();
5630 
5631  // The special branch fBranchRef needs to be processed sequentially:
5632  // we add it once only.
5633  if (fBranchRef && fBranchRef != fSeqBranches[0]) {
5634  fSeqBranches.push_back(fBranchRef);
5635  }
5636 
5637  // The branches to be processed sequentially are those that are the leaf count of another branch
5638  if (checkLeafCount) {
5639  for (Int_t i = 0; i < nbranches; i++) {
5640  TBranch* branch = (TBranch*)fBranches.UncheckedAt(i);
5641  auto leafCount = ((TLeaf*)branch->GetListOfLeaves()->At(0))->GetLeafCount();
5642  if (leafCount) {
5643  auto countBranch = leafCount->GetBranch();
5644  if (std::find(fSeqBranches.begin(), fSeqBranches.end(), countBranch) == fSeqBranches.end()) {
5645  fSeqBranches.push_back(countBranch);
5646  }
5647  }
5648  }
5649  }
5650 
5651  // Any branch that is not a leaf count can be safely processed in parallel when reading
5652  // We need to reset the vector to make sure we do not re-add several times the same branch.
5653  if (!checkLeafCount) {
5654  fSortedBranches.clear();
5655  }
5656  for (Int_t i = 0; i < nbranches; i++) {
5657  Long64_t bbytes = 0;
5658  TBranch* branch = (TBranch*)fBranches.UncheckedAt(i);
5659  if (std::find(fSeqBranches.begin(), fSeqBranches.end(), branch) == fSeqBranches.end()) {
5660  bbytes = branch->GetTotBytes("*");
5661  fSortedBranches.emplace_back(bbytes, branch);
5662  }
5663  }
5664 
5665  // Initially sort parallel branches by size
5666  std::sort(fSortedBranches.begin(),
5667  fSortedBranches.end(),
5668  [](std::pair<Long64_t,TBranch*> a, std::pair<Long64_t,TBranch*> b) {
5669  return a.first > b.first;
5670  });
5671 
5672  for (size_t i = 0; i < fSortedBranches.size(); i++) {
5673  fSortedBranches[i].first = 0LL;
5674  }
5675 }
5676 
5677 ////////////////////////////////////////////////////////////////////////////////
5678 /// Sorts top-level branches by the last average task time recorded per branch.
5681 {
5682  for (size_t i = 0; i < fSortedBranches.size(); i++) {
5683  fSortedBranches[i].first *= kNEntriesResortInv;
5684  }
5685 
5686  std::sort(fSortedBranches.begin(),
5687  fSortedBranches.end(),
5688  [](std::pair<Long64_t,TBranch*> a, std::pair<Long64_t,TBranch*> b) {
5689  return a.first > b.first;
5690  });
5691 
5692  for (size_t i = 0; i < fSortedBranches.size(); i++) {
5693  fSortedBranches[i].first = 0LL;
5694  }
5695 }
5696 
5697 ////////////////////////////////////////////////////////////////////////////////
5698 ///Returns the entry list assigned to this tree
5701 {
5702  return fEntryList;
5703 }
5704 
5705 ////////////////////////////////////////////////////////////////////////////////
5706 /// Return entry number corresponding to entry.
5707 ///
5708 /// if no TEntryList set returns entry
5709 /// else returns the entry number corresponding to the list index=entry
5712 {
5713  if (!fEntryList) {
5714  return entry;
5715  }
5716 
5717  return fEntryList->GetEntry(entry);
5718 }
5719 
5720 ////////////////////////////////////////////////////////////////////////////////
5721 /// Return entry number corresponding to major and minor number.
5722 /// Note that this function returns only the entry number, not the data
5723 /// To read the data corresponding to an entry number, use TTree::GetEntryWithIndex
5724 /// the BuildIndex function has created a table of Long64_t* of sorted values
5725 /// corresponding to val = major<<31 + minor;
5726 /// The function performs binary search in this sorted table.
5727 /// If it finds a pair that matches val, it returns directly the
5728 /// index in the table.
5729 /// If an entry corresponding to major and minor is not found, the function
5730 /// returns the index of the major,minor pair immediately lower than the
5731 /// requested value, ie it will return -1 if the pair is lower than
5732 /// the first entry in the index.
5733 ///
5734 /// See also GetEntryNumberWithIndex
5737 {
5738  if (!fTreeIndex) {
5739  return -1;
5740  }
5741  return fTreeIndex->GetEntryNumberWithBestIndex(major, minor);
5742 }
5743 
5744 ////////////////////////////////////////////////////////////////////////////////
5745 /// Return entry number corresponding to major and minor number.
5746 /// Note that this function returns only the entry number, not the data
5747 /// To read the data corresponding to an entry number, use TTree::GetEntryWithIndex
5748 /// the BuildIndex function has created a table of Long64_t* of sorted values
5749 /// corresponding to val = major<<31 + minor;
5750 /// The function performs binary search in this sorted table.
5751 /// If it finds a pair that matches val, it returns directly the
5752 /// index in the table, otherwise it returns -1.
5753 ///
5754 /// See also GetEntryNumberWithBestIndex
5757 {
5758  if (!fTreeIndex) {
5759  return -1;
5760  }
5761  return fTreeIndex->GetEntryNumberWithIndex(major, minor);
5762 }
5763 
5764 ////////////////////////////////////////////////////////////////////////////////
5765 /// Read entry corresponding to major and minor number.
5766 ///
5767 /// The function returns the total number of bytes read.
5768 /// If the Tree has friend trees, the corresponding entry with
5769 /// the index values (major,minor) is read. Note that the master Tree
5770 /// and its friend may have different entry serial numbers corresponding
5771