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