Logo ROOT   6.12/07
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 "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
416 
417 constexpr Int_t kNEntriesResort = 100;
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 ////////////////////////////////////////////////////////////////////////////////
428 
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.
469 
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) {
477  fPrevious = fTree->fFriendLockStatus & fMethodBit;
478  fTree->fFriendLockStatus |= fMethodBit;
479  } else {
480  fPrevious = 0;
481  }
482 }
483 
484 ////////////////////////////////////////////////////////////////////////////////
485 /// Copy constructor.
486 
488  fTree(tfl.fTree),
489  fMethodBit(tfl.fMethodBit),
490  fPrevious(tfl.fPrevious)
491 {
492 }
493 
494 ////////////////////////////////////////////////////////////////////////////////
495 /// Assignment operator.
496 
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.
509 
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.
526 
527 TTree::TClusterIterator::TClusterIterator(TTree *tree, Long64_t firstEntry) : fTree(tree), fClusterRange(0), fStartEntry(0), fNextEntry(0)
528 {
529  if ( fTree->GetAutoFlush() <= 0 ) {
530  // Case of old files before November 9 2009
531  fStartEntry = firstEntry;
532  } else if (fTree->fNClusterRange) {
533  // Find the correct cluster range.
534  //
535  // Since fClusterRangeEnd contains the inclusive upper end of the range, we need to search for the
536  // range that was containing the previous entry and add 1 (because BinarySearch consider the values
537  // to be the inclusive start of the bucket).
538  fClusterRange = TMath::BinarySearch(fTree->fNClusterRange, fTree->fClusterRangeEnd, firstEntry - 1) + 1;
539 
540  Long64_t entryInRange;
541  Long64_t pedestal;
542  if (fClusterRange == 0) {
543  pedestal = 0;
544  entryInRange = firstEntry;
545  } else {
546  pedestal = fTree->fClusterRangeEnd[fClusterRange-1] + 1;
547  entryInRange = firstEntry - pedestal;
548  }
549  Long64_t autoflush;
550  if (fClusterRange == fTree->fNClusterRange) {
551  autoflush = fTree->fAutoFlush;
552  } else {
553  autoflush = fTree->fClusterSize[fClusterRange];
554  }
555  if (autoflush == 0) {
556  autoflush = GetEstimatedClusterSize();
557  }
558  fStartEntry = pedestal + entryInRange - entryInRange%autoflush;
559  } else {
560  fStartEntry = firstEntry - firstEntry%fTree->GetAutoFlush();
561  }
562  fNextEntry = fStartEntry; // Position correctly for the first call to Next()
563 }
564 
565 ////////////////////////////////////////////////////////////////////////////////
566 /// In the case where the cluster size was not fixed (old files and
567 /// case where autoflush was explicitly set to zero, we need estimate
568 /// a cluster size in relation to the size of the cache.
569 
571 {
572  Long64_t zipBytes = fTree->GetZipBytes();
573  if (zipBytes == 0) {
574  return fTree->GetEntries() - 1;
575  } else {
576  Long64_t clusterEstimate = 1;
577  Long64_t cacheSize = fTree->GetCacheSize();
578  if (cacheSize == 0) {
579  // Humm ... let's double check on the file.
581  if (file) {
582  TFileCacheRead *cache = file->GetCacheRead(fTree);
583  if (cache) {
584  cacheSize = cache->GetBufferSize();
585  }
586  }
587  }
588  if (cacheSize > 0) {
589  clusterEstimate = fTree->GetEntries() * cacheSize / zipBytes;
590  if (clusterEstimate == 0)
591  clusterEstimate = 1;
592  }
593  return clusterEstimate;
594  }
595 }
596 
597 ////////////////////////////////////////////////////////////////////////////////
598 /// Move on to the next cluster and return the starting entry
599 /// of this next cluster
600 
602 {
603  fStartEntry = fNextEntry;
604  if ( fTree->GetAutoFlush() <= 0 ) {
605  // Case of old files before November 9 2009
606  Long64_t clusterEstimate = GetEstimatedClusterSize();
607  fNextEntry = fStartEntry + clusterEstimate;
608  } else {
609  if (fClusterRange == fTree->fNClusterRange) {
610  // We are looking at a range which size
611  // is defined by AutoFlush itself and goes to the GetEntries.
612  fNextEntry += fTree->GetAutoFlush();
613  } else {
614  if (fStartEntry > fTree->fClusterRangeEnd[fClusterRange]) {
615  ++fClusterRange;
616  }
617  if (fClusterRange == fTree->fNClusterRange) {
618  // We are looking at the last range which size
619  // is defined by AutoFlush itself and goes to the GetEntries.
620  fNextEntry += fTree->GetAutoFlush();
621  } else {
622  Long64_t clusterSize = fTree->fClusterSize[fClusterRange];
623  if (clusterSize == 0) {
624  clusterSize = GetEstimatedClusterSize();
625  }
626  fNextEntry += clusterSize;
627  if (fNextEntry > fTree->fClusterRangeEnd[fClusterRange]) {
628  // The last cluster of the range was a partial cluster,
629  // so the next cluster starts at the beginning of the
630  // next range.
631  fNextEntry = fTree->fClusterRangeEnd[fClusterRange] + 1;
632  }
633  }
634  }
635  }
636  if (fNextEntry > fTree->GetEntries()) {
637  fNextEntry = fTree->GetEntries();
638  }
639  return fStartEntry;
640 }
641 
642 ////////////////////////////////////////////////////////////////////////////////
643 /// Move on to the previous cluster and return the starting entry
644 /// of this previous cluster
645 
647 {
648  fNextEntry = fStartEntry;
649  if (fTree->GetAutoFlush() <= 0) {
650  // Case of old files before November 9 2009
651  Long64_t clusterEstimate = GetEstimatedClusterSize();
652  fStartEntry = fNextEntry - clusterEstimate;
653  } else {
654  if (fClusterRange == 0 || fTree->fNClusterRange == 0) {
655  // We are looking at a range which size
656  // is defined by AutoFlush itself.
657  fStartEntry -= fTree->GetAutoFlush();
658  } else {
659  if (fNextEntry <= fTree->fClusterRangeEnd[fClusterRange]) {
660  --fClusterRange;
661  }
662  if (fClusterRange == 0) {
663  // We are looking at the first range.
664  fStartEntry = 0;
665  } else {
666  Long64_t clusterSize = fTree->fClusterSize[fClusterRange];
667  if (clusterSize == 0) {
668  clusterSize = GetEstimatedClusterSize();
669  }
670  fStartEntry -= clusterSize;
671  }
672  }
673  }
674  if (fStartEntry < 0) {
675  fStartEntry = 0;
676  }
677  return fStartEntry;
678 }
679 
680 ////////////////////////////////////////////////////////////////////////////////
681 ////////////////////////////////////////////////////////////////////////////////
682 ////////////////////////////////////////////////////////////////////////////////
683 
684 ////////////////////////////////////////////////////////////////////////////////
685 /// Default constructor and I/O constructor.
686 ///
687 /// Note: We do *not* insert ourself into the current directory.
688 ///
689 
690 TTree::TTree()
692 , TAttLine()
693 , TAttFill()
694 , TAttMarker()
695 , fEntries(0)
696 , fTotBytes(0)
697 , fZipBytes(0)
698 , fSavedBytes(0)
699 , fFlushedBytes(0)
700 , fWeight(1)
701 , fTimerInterval(0)
702 , fScanField(25)
703 , fUpdate(0)
705 , fNClusterRange(0)
706 , fMaxClusterRange(0)
707 , fMaxEntries(0)
708 , fMaxEntryLoop(0)
709 , fMaxVirtualSize(0)
710 , fAutoSave( -300000000)
711 , fAutoFlush(-30000000)
712 , fEstimate(1000000)
713 , fClusterRangeEnd(0)
714 , fClusterSize(0)
715 , fCacheSize(0)
716 , fChainOffset(0)
717 , fReadEntry(-1)
718 , fTotalBuffers(0)
719 , fPacketSize(100)
720 , fNfill(0)
721 , fDebug(0)
722 , fDebugMin(0)
723 , fDebugMax(9999999)
724 , fMakeClass(0)
725 , fFileNumber(0)
726 , fNotify(0)
727 , fDirectory(0)
728 , fBranches()
729 , fLeaves()
730 , fAliases(0)
731 , fEventList(0)
732 , fEntryList(0)
733 , fIndexValues()
734 , fIndex()
735 , fTreeIndex(0)
736 , fFriends(0)
737 , fPerfStats(0)
738 , fUserInfo(0)
739 , fPlayer(0)
740 , fClones(0)
741 , fBranchRef(0)
743 , fTransientBuffer(0)
749 {
750  fMaxEntries = 1000000000;
751  fMaxEntries *= 1000;
752 
753  fMaxEntryLoop = 1000000000;
754  fMaxEntryLoop *= 1000;
755 
757 }
758 
759 ////////////////////////////////////////////////////////////////////////////////
760 /// Normal tree constructor.
761 ///
762 /// The tree is created in the current directory.
763 /// Use the various functions Branch below to add branches to this tree.
764 ///
765 /// If the first character of title is a "/", the function assumes a folder name.
766 /// In this case, it creates automatically branches following the folder hierarchy.
767 /// splitlevel may be used in this case to control the split level.
768 
769 TTree::TTree(const char* name, const char* title, Int_t splitlevel /* = 99 */,
770  TDirectory* dir /* = gDirectory*/)
771 : TNamed(name, title)
772 , TAttLine()
773 , TAttFill()
774 , TAttMarker()
775 , fEntries(0)
776 , fTotBytes(0)
777 , fZipBytes(0)
778 , fSavedBytes(0)
779 , fFlushedBytes(0)
780 , fWeight(1)
781 , fTimerInterval(0)
782 , fScanField(25)
783 , fUpdate(0)
785 , fNClusterRange(0)
786 , fMaxClusterRange(0)
787 , fMaxEntries(0)
788 , fMaxEntryLoop(0)
789 , fMaxVirtualSize(0)
790 , fAutoSave( -300000000)
791 , fAutoFlush(-30000000)
792 , fEstimate(1000000)
793 , fClusterRangeEnd(0)
794 , fClusterSize(0)
795 , fCacheSize(0)
796 , fChainOffset(0)
797 , fReadEntry(-1)
798 , fTotalBuffers(0)
799 , fPacketSize(100)
800 , fNfill(0)
801 , fDebug(0)
802 , fDebugMin(0)
803 , fDebugMax(9999999)
804 , fMakeClass(0)
805 , fFileNumber(0)
806 , fNotify(0)
807 , fDirectory(dir)
808 , fBranches()
809 , fLeaves()
810 , fAliases(0)
811 , fEventList(0)
812 , fEntryList(0)
813 , fIndexValues()
814 , fIndex()
815 , fTreeIndex(0)
816 , fFriends(0)
817 , fPerfStats(0)
818 , fUserInfo(0)
819 , fPlayer(0)
820 , fClones(0)
821 , fBranchRef(0)
823 , fTransientBuffer(0)
829 {
830  // TAttLine state.
834 
835  // TAttFill state.
838 
839  // TAttMarkerState.
843 
844  fMaxEntries = 1000000000;
845  fMaxEntries *= 1000;
846 
847  fMaxEntryLoop = 1000000000;
848  fMaxEntryLoop *= 1000;
849 
850  // Insert ourself into the current directory.
851  // FIXME: This is very annoying behaviour, we should
852  // be able to choose to not do this like we
853  // can with a histogram.
854  if (fDirectory) fDirectory->Append(this);
855 
857 
858  // If title starts with "/" and is a valid folder name, a superbranch
859  // is created.
860  // FIXME: Why?
861  if (strlen(title) > 2) {
862  if (title[0] == '/') {
863  Branch(title+1,32000,splitlevel);
864  }
865  }
866 }
867 
868 ////////////////////////////////////////////////////////////////////////////////
869 /// Destructor.
870 
872 {
873  if (fDirectory) {
874  // We are in a directory, which may possibly be a file.
875  if (fDirectory->GetList()) {
876  // Remove us from the directory listing.
877  fDirectory->Remove(this);
878  }
879  //delete the file cache if it points to this Tree
881  MoveReadCache(file,0);
882  }
883  // We don't own the leaves in fLeaves, the branches do.
884  fLeaves.Clear();
885  // I'm ready to destroy any objects allocated by
886  // SetAddress() by my branches. If I have clones,
887  // tell them to zero their pointers to this shared
888  // memory.
889  if (fClones && fClones->GetEntries()) {
890  // I have clones.
891  // I am about to delete the objects created by
892  // SetAddress() which we are sharing, so tell
893  // the clones to release their pointers to them.
894  for (TObjLink* lnk = fClones->FirstLink(); lnk; lnk = lnk->Next()) {
895  TTree* clone = (TTree*) lnk->GetObject();
896  // clone->ResetBranchAddresses();
897 
898  // Reset only the branch we have set the address of.
899  CopyAddresses(clone,kTRUE);
900  }
901  }
902  // Get rid of our branches, note that this will also release
903  // any memory allocated by TBranchElement::SetAddress().
904  fBranches.Delete();
905  // FIXME: We must consider what to do with the reset of these if we are a clone.
906  delete fPlayer;
907  fPlayer = 0;
908  if (fFriends) {
909  fFriends->Delete();
910  delete fFriends;
911  fFriends = 0;
912  }
913  if (fAliases) {
914  fAliases->Delete();
915  delete fAliases;
916  fAliases = 0;
917  }
918  if (fUserInfo) {
919  fUserInfo->Delete();
920  delete fUserInfo;
921  fUserInfo = 0;
922  }
923  if (fClones) {
924  // Clone trees should no longer be removed from fClones when they are deleted.
925  {
927  gROOT->GetListOfCleanups()->Remove(fClones);
928  }
929  // Note: fClones does not own its content.
930  delete fClones;
931  fClones = 0;
932  }
933  if (fEntryList) {
935  // Delete the entry list if it is marked to be deleted and it is not also
936  // owned by a directory. (Otherwise we would need to make sure that a
937  // TDirectoryFile that has a TTree in it does a 'slow' TList::Delete.
938  delete fEntryList;
939  fEntryList=0;
940  }
941  }
942  delete fTreeIndex;
943  fTreeIndex = 0;
944  delete fBranchRef;
945  fBranchRef = 0;
946  delete [] fClusterRangeEnd;
947  fClusterRangeEnd = 0;
948  delete [] fClusterSize;
949  fClusterSize = 0;
950  // Must be done after the destruction of friends.
951  // Note: We do *not* own our directory.
952  fDirectory = 0;
953 
954  if (fTransientBuffer) {
955  delete fTransientBuffer;
956  fTransientBuffer = 0;
957  }
958 }
959 
960 ////////////////////////////////////////////////////////////////////////////////
961 /// Returns the transient buffer currently used by this TTree for reading/writing baskets.
962 
964 {
965  if (fTransientBuffer) {
966  if (fTransientBuffer->BufferSize() < size) {
967  fTransientBuffer->Expand(size);
968  }
969  return fTransientBuffer;
970  }
972  return fTransientBuffer;
973 }
974 
975 ////////////////////////////////////////////////////////////////////////////////
976 /// Add branch with name bname to the Tree cache.
977 /// If bname="*" all branches are added to the cache.
978 /// if subbranches is true all the branches of the subbranches are
979 /// also put to the cache.
980 ///
981 /// Returns:
982 /// - 0 branch added or already included
983 /// - -1 on error
984 
985 Int_t TTree::AddBranchToCache(const char*bname, Bool_t subbranches)
986 {
987  if (!GetTree()) {
988  if (LoadTree(0)<0) {
989  Error("AddBranchToCache","Could not load a tree");
990  return -1;
991  }
992  }
993  if (GetTree()) {
994  if (GetTree() != this) {
995  return GetTree()->AddBranchToCache(bname, subbranches);
996  }
997  } else {
998  Error("AddBranchToCache", "No tree is available. Branch was not added to the cache");
999  return -1;
1000  }
1001 
1002  TFile *f = GetCurrentFile();
1003  if (!f) {
1004  Error("AddBranchToCache", "No file is available. Branch was not added to the cache");
1005  return -1;
1006  }
1007  TTreeCache *tc = GetReadCache(f,kTRUE);
1008  if (!tc) {
1009  Error("AddBranchToCache", "No cache is available, branch not added");
1010  return -1;
1011  }
1012  return tc->AddBranch(bname,subbranches);
1013 }
1014 
1015 ////////////////////////////////////////////////////////////////////////////////
1016 /// Add branch b to the Tree cache.
1017 /// if subbranches is true all the branches of the subbranches are
1018 /// also put to the cache.
1019 ///
1020 /// Returns:
1021 /// - 0 branch added or already included
1022 /// - -1 on error
1023 
1026  if (!GetTree()) {
1027  if (LoadTree(0)<0) {
1028  Error("AddBranchToCache","Could not load a tree");
1029  return -1;
1030  }
1031  }
1032  if (GetTree()) {
1033  if (GetTree() != this) {
1034  Int_t res = GetTree()->AddBranchToCache(b, subbranches);
1035  if (res<0) {
1036  Error("AddBranchToCache", "Error adding branch");
1037  }
1038  return res;
1039  }
1040  } else {
1041  Error("AddBranchToCache", "No tree is available. Branch was not added to the cache");
1042  return -1;
1043  }
1044 
1045  TFile *f = GetCurrentFile();
1046  if (!f) {
1047  Error("AddBranchToCache", "No file is available. Branch was not added to the cache");
1048  return -1;
1049  }
1050  TTreeCache *tc = GetReadCache(f,kTRUE);
1051  if (!tc) {
1052  Error("AddBranchToCache", "No cache is available, branch not added");
1053  return -1;
1054  }
1055  return tc->AddBranch(b,subbranches);
1056 }
1057 
1058 ////////////////////////////////////////////////////////////////////////////////
1059 /// Remove the branch with name 'bname' from the Tree cache.
1060 /// If bname="*" all branches are removed from the cache.
1061 /// if subbranches is true all the branches of the subbranches are
1062 /// also removed from the cache.
1063 ///
1064 /// Returns:
1065 /// - 0 branch dropped or not in cache
1066 /// - -1 on error
1067 
1068 Int_t TTree::DropBranchFromCache(const char*bname, Bool_t subbranches)
1070  if (!GetTree()) {
1071  if (LoadTree(0)<0) {
1072  Error("DropBranchFromCache","Could not load a tree");
1073  return -1;
1074  }
1075  }
1076  if (GetTree()) {
1077  if (GetTree() != this) {
1078  return GetTree()->DropBranchFromCache(bname, subbranches);
1079  }
1080  } else {
1081  Error("DropBranchFromCache", "No tree is available. Branch was not dropped from the cache");
1082  return -1;
1083  }
1084 
1085  TFile *f = GetCurrentFile();
1086  if (!f) {
1087  Error("DropBranchFromCache", "No file is available. Branch was not dropped from the cache");
1088  return -1;
1089  }
1090  TTreeCache *tc = GetReadCache(f,kTRUE);
1091  if (!tc) {
1092  Error("DropBranchFromCache", "No cache is available, branch not dropped");
1093  return -1;
1094  }
1095  return tc->DropBranch(bname,subbranches);
1096 }
1097 
1098 ////////////////////////////////////////////////////////////////////////////////
1099 /// Remove the branch b from the Tree cache.
1100 /// if subbranches is true all the branches of the subbranches are
1101 /// also removed from the cache.
1102 ///
1103 /// Returns:
1104 /// - 0 branch dropped or not in cache
1105 /// - -1 on error
1106 
1109  if (!GetTree()) {
1110  if (LoadTree(0)<0) {
1111  Error("DropBranchFromCache","Could not load a tree");
1112  return -1;
1113  }
1114  }
1115  if (GetTree()) {
1116  if (GetTree() != this) {
1117  Int_t res = GetTree()->DropBranchFromCache(b, subbranches);
1118  if (res<0) {
1119  Error("DropBranchFromCache", "Error dropping branch");
1120  }
1121  return res;
1122  }
1123  } else {
1124  Error("DropBranchFromCache", "No tree is available. Branch was not dropped from the cache");
1125  return -1;
1126  }
1127 
1128  TFile *f = GetCurrentFile();
1129  if (!f) {
1130  Error("DropBranchFromCache", "No file is available. Branch was not dropped from the cache");
1131  return -1;
1132  }
1133  TTreeCache *tc = GetReadCache(f,kTRUE);
1134  if (!tc) {
1135  Error("DropBranchFromCache", "No cache is available, branch not dropped");
1136  return -1;
1137  }
1138  return tc->DropBranch(b,subbranches);
1139 }
1140 
1141 ////////////////////////////////////////////////////////////////////////////////
1142 /// Add a cloned tree to our list of trees to be notified whenever we change
1143 /// our branch addresses or when we are deleted.
1144 
1145 void TTree::AddClone(TTree* clone)
1147  if (!fClones) {
1148  fClones = new TList();
1149  fClones->SetOwner(false);
1150  // So that the clones are automatically removed from the list when
1151  // they are deleted.
1152  {
1154  gROOT->GetListOfCleanups()->Add(fClones);
1155  }
1156  }
1157  if (!fClones->FindObject(clone)) {
1158  fClones->Add(clone);
1159  }
1160 }
1161 
1162 ////////////////////////////////////////////////////////////////////////////////
1163 /// Add a TFriendElement to the list of friends.
1164 ///
1165 /// This function:
1166 /// - opens a file if filename is specified
1167 /// - reads a Tree with name treename from the file (current directory)
1168 /// - adds the Tree to the list of friends
1169 /// see other AddFriend functions
1170 ///
1171 /// A TFriendElement TF describes a TTree object TF in a file.
1172 /// When a TFriendElement TF is added to the the list of friends of an
1173 /// existing TTree T, any variable from TF can be referenced in a query
1174 /// to T.
1175 ///
1176 /// A tree keeps a list of friends. In the context of a tree (or a chain),
1177 /// friendship means unrestricted access to the friends data. In this way
1178 /// it is much like adding another branch to the tree without taking the risk
1179 /// of damaging it. To add a friend to the list, you can use the TTree::AddFriend
1180 /// method. The tree in the diagram below has two friends (friend_tree1 and
1181 /// friend_tree2) and now has access to the variables a,b,c,i,j,k,l and m.
1182 ///
1183 /// \image html ttree_friend1.png
1184 ///
1185 /// The AddFriend method has two parameters, the first is the tree name and the
1186 /// second is the name of the ROOT file where the friend tree is saved.
1187 /// AddFriend automatically opens the friend file. If no file name is given,
1188 /// the tree called ft1 is assumed to be in the same file as the original tree.
1189 ///
1190 /// tree.AddFriend("ft1","friendfile1.root");
1191 /// If the friend tree has the same name as the original tree, you can give it
1192 /// an alias in the context of the friendship:
1193 ///
1194 /// tree.AddFriend("tree1 = tree","friendfile1.root");
1195 /// Once the tree has friends, we can use TTree::Draw as if the friend's
1196 /// variables were in the original tree. To specify which tree to use in
1197 /// the Draw method, use the syntax:
1198 /// ~~~ {.cpp}
1199 /// <treeName>.<branchname>.<varname>
1200 /// ~~~
1201 /// If the variablename is enough to uniquely identify the variable, you can
1202 /// leave out the tree and/or branch name.
1203 /// For example, these commands generate a 3-d scatter plot of variable "var"
1204 /// in the TTree tree versus variable v1 in TTree ft1 versus variable v2 in
1205 /// TTree ft2.
1206 /// ~~~ {.cpp}
1207 /// tree.AddFriend("ft1","friendfile1.root");
1208 /// tree.AddFriend("ft2","friendfile2.root");
1209 /// tree.Draw("var:ft1.v1:ft2.v2");
1210 /// ~~~
1211 /// \image html ttree_friend2.png
1212 ///
1213 /// The picture illustrates the access of the tree and its friends with a
1214 /// Draw command.
1215 /// When AddFriend is called, the ROOT file is automatically opened and the
1216 /// friend tree (ft1) is read into memory. The new friend (ft1) is added to
1217 /// the list of friends of tree.
1218 /// The number of entries in the friend must be equal or greater to the number
1219 /// of entries of the original tree. If the friend tree has fewer entries a
1220 /// warning is given and the missing entries are not included in the histogram.
1221 /// To retrieve the list of friends from a tree use TTree::GetListOfFriends.
1222 /// When the tree is written to file (TTree::Write), the friends list is saved
1223 /// with it. And when the tree is retrieved, the trees on the friends list are
1224 /// also retrieved and the friendship restored.
1225 /// When a tree is deleted, the elements of the friend list are also deleted.
1226 /// It is possible to declare a friend tree that has the same internal
1227 /// structure (same branches and leaves) as the original tree, and compare the
1228 /// same values by specifying the tree.
1229 /// ~~~ {.cpp}
1230 /// tree.Draw("var:ft1.var:ft2.var")
1231 /// ~~~
1232 
1233 TFriendElement* TTree::AddFriend(const char* treename, const char* filename)
1235  if (!fFriends) {
1236  fFriends = new TList();
1237  }
1238  TFriendElement* fe = new TFriendElement(this, treename, filename);
1239 
1240  fFriends->Add(fe);
1241  TTree* t = fe->GetTree();
1242  if (t) {
1243  if (!t->GetTreeIndex() && (t->GetEntries() < fEntries)) {
1244  Warning("AddFriend", "FriendElement %s in file %s has less entries %lld than its parent Tree: %lld", treename, filename, t->GetEntries(), fEntries);
1245  }
1246  } else {
1247  Warning("AddFriend", "Cannot add FriendElement %s in file %s", treename, filename);
1248  }
1249  return fe;
1250 }
1251 
1252 ////////////////////////////////////////////////////////////////////////////////
1253 /// Add a TFriendElement to the list of friends.
1254 ///
1255 /// The TFile is managed by the user (e.g. the user must delete the file).
1256 /// For complete description see AddFriend(const char *, const char *).
1257 /// This function:
1258 /// - reads a Tree with name treename from the file
1259 /// - adds the Tree to the list of friends
1260 
1261 TFriendElement* TTree::AddFriend(const char* treename, TFile* file)
1263  if (!fFriends) {
1264  fFriends = new TList();
1265  }
1266  TFriendElement *fe = new TFriendElement(this, treename, file);
1267  R__ASSERT(fe);
1268  fFriends->Add(fe);
1269  TTree *t = fe->GetTree();
1270  if (t) {
1271  if (!t->GetTreeIndex() && (t->GetEntries() < fEntries)) {
1272  Warning("AddFriend", "FriendElement %s in file %s has less entries %lld than its parent tree: %lld", treename, file->GetName(), t->GetEntries(), fEntries);
1273  }
1274  } else {
1275  Warning("AddFriend", "unknown tree '%s' in file '%s'", treename, file->GetName());
1276  }
1277  return fe;
1278 }
1279 
1280 ////////////////////////////////////////////////////////////////////////////////
1281 /// Add a TFriendElement to the list of friends.
1282 ///
1283 /// The TTree is managed by the user (e.g., the user must delete the file).
1284 /// For a complete description see AddFriend(const char *, const char *).
1285 
1286 TFriendElement* TTree::AddFriend(TTree* tree, const char* alias, Bool_t warn)
1288  if (!tree) {
1289  return 0;
1290  }
1291  if (!fFriends) {
1292  fFriends = new TList();
1293  }
1294  TFriendElement* fe = new TFriendElement(this, tree, alias);
1295  R__ASSERT(fe); // this assert is for historical reasons. Don't remove it unless you understand all the consequences.
1296  fFriends->Add(fe);
1297  TTree* t = fe->GetTree();
1298  if (warn && (t->GetEntries() < fEntries)) {
1299  Warning("AddFriend", "FriendElement '%s' in file '%s' has less entries %lld than its parent tree: %lld",
1300  tree->GetName(), fe->GetFile() ? fe->GetFile()->GetName() : "(memory resident)", t->GetEntries(), fEntries);
1301  }
1302  return fe;
1303 }
1304 
1305 ////////////////////////////////////////////////////////////////////////////////
1306 /// AutoSave tree header every fAutoSave bytes.
1307 ///
1308 /// When large Trees are produced, it is safe to activate the AutoSave
1309 /// procedure. Some branches may have buffers holding many entries.
1310 /// If fAutoSave is negative, AutoSave is automatically called by
1311 /// TTree::Fill when the number of bytes generated since the previous
1312 /// AutoSave is greater than -fAutoSave bytes.
1313 /// If fAutoSave is positive, AutoSave is automatically called by
1314 /// TTree::Fill every N entries.
1315 /// This function may also be invoked by the user.
1316 /// Each AutoSave generates a new key on the file.
1317 /// Once the key with the tree header has been written, the previous cycle
1318 /// (if any) is deleted.
1319 ///
1320 /// Note that calling TTree::AutoSave too frequently (or similarly calling
1321 /// TTree::SetAutoSave with a small value) is an expensive operation.
1322 /// You should make tests for your own application to find a compromise
1323 /// between speed and the quantity of information you may loose in case of
1324 /// a job crash.
1325 ///
1326 /// In case your program crashes before closing the file holding this tree,
1327 /// the file will be automatically recovered when you will connect the file
1328 /// in UPDATE mode.
1329 /// The Tree will be recovered at the status corresponding to the last AutoSave.
1330 ///
1331 /// if option contains "SaveSelf", gDirectory->SaveSelf() is called.
1332 /// This allows another process to analyze the Tree while the Tree is being filled.
1333 ///
1334 /// if option contains "FlushBaskets", TTree::FlushBaskets is called and all
1335 /// the current basket are closed-out and written to disk individually.
1336 ///
1337 /// By default the previous header is deleted after having written the new header.
1338 /// if option contains "Overwrite", the previous Tree header is deleted
1339 /// before written the new header. This option is slightly faster, but
1340 /// the default option is safer in case of a problem (disk quota exceeded)
1341 /// when writing the new header.
1342 ///
1343 /// The function returns the number of bytes written to the file.
1344 /// if the number of bytes is null, an error has occurred while writing
1345 /// the header to the file.
1346 ///
1347 /// ## How to write a Tree in one process and view it from another process
1348 ///
1349 /// The following two scripts illustrate how to do this.
1350 /// The script treew.C is executed by process1, treer.C by process2
1351 ///
1352 /// script treew.C:
1353 /// ~~~ {.cpp}
1354 /// void treew() {
1355 /// TFile f("test.root","recreate");
1356 /// TNtuple *ntuple = new TNtuple("ntuple","Demo","px:py:pz:random:i");
1357 /// Float_t px, py, pz;
1358 /// for ( Int_t i=0; i<10000000; i++) {
1359 /// gRandom->Rannor(px,py);
1360 /// pz = px*px + py*py;
1361 /// Float_t random = gRandom->Rndm(1);
1362 /// ntuple->Fill(px,py,pz,random,i);
1363 /// if (i%1000 == 1) ntuple->AutoSave("SaveSelf");
1364 /// }
1365 /// }
1366 /// ~~~
1367 /// script treer.C:
1368 /// ~~~ {.cpp}
1369 /// void treer() {
1370 /// TFile f("test.root");
1371 /// TTree *ntuple = (TTree*)f.Get("ntuple");
1372 /// TCanvas c1;
1373 /// Int_t first = 0;
1374 /// while(1) {
1375 /// if (first == 0) ntuple->Draw("px>>hpx", "","",10000000,first);
1376 /// else ntuple->Draw("px>>+hpx","","",10000000,first);
1377 /// first = (Int_t)ntuple->GetEntries();
1378 /// c1.Update();
1379 /// gSystem->Sleep(1000); //sleep 1 second
1380 /// ntuple->Refresh();
1381 /// }
1382 /// }
1383 /// ~~~
1384 
1387  if (!fDirectory || fDirectory == gROOT || !fDirectory->IsWritable()) return 0;
1388  if (gDebug > 0) {
1389  Info("AutoSave", "Tree:%s after %lld bytes written\n",GetName(),GetTotBytes());
1390  }
1391  TString opt = option;
1392  opt.ToLower();
1393 
1394  if (opt.Contains("flushbaskets")) {
1395  if (gDebug > 0) Info("AutoSave", "calling FlushBaskets \n");
1396  FlushBaskets();
1397  }
1398 
1400 
1402  Long64_t nbytes;
1403  if (opt.Contains("overwrite")) {
1404  nbytes = fDirectory->WriteTObject(this,"","overwrite");
1405  } else {
1406  nbytes = fDirectory->WriteTObject(this); //nbytes will be 0 if Write failed (disk space exceeded)
1407  if (nbytes && key) {
1408  key->Delete();
1409  delete key;
1410  }
1411  }
1412  // save StreamerInfo
1413  TFile *file = fDirectory->GetFile();
1414  if (file) file->WriteStreamerInfo();
1415 
1416  if (opt.Contains("saveself")) {
1417  fDirectory->SaveSelf();
1418  //the following line is required in case GetUserInfo contains a user class
1419  //for which the StreamerInfo must be written. One could probably be a bit faster (Rene)
1420  if (file) file->WriteHeader();
1421  }
1422 
1423  return nbytes;
1424 }
1425 
1426 namespace {
1427  // This error message is repeated several times in the code. We write it once.
1428  const char* writeStlWithoutProxyMsg = "The class requested (%s) for the branch \"%s\""
1429  " is an instance of an stl collection and does not have a compiled CollectionProxy."
1430  " Please generate the dictionary for this collection (%s) to avoid to write corrupted data.";
1431 }
1432 
1433 ////////////////////////////////////////////////////////////////////////////////
1434 /// Same as TTree::Branch() with added check that addobj matches className.
1435 ///
1436 /// See TTree::Branch() for other details.
1437 ///
1438 
1439 TBranch* TTree::BranchImp(const char* branchname, const char* classname, TClass* ptrClass, void* addobj, Int_t bufsize, Int_t splitlevel)
1441  TClass* claim = TClass::GetClass(classname);
1442  if (!ptrClass) {
1443  if (claim && claim->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(claim->GetCollectionProxy())) {
1444  Error("Branch", writeStlWithoutProxyMsg,
1445  claim->GetName(), branchname, claim->GetName());
1446  return 0;
1447  }
1448  return Branch(branchname, classname, (void*) addobj, bufsize, splitlevel);
1449  }
1450  TClass* actualClass = 0;
1451  void** addr = (void**) addobj;
1452  if (addr) {
1453  actualClass = ptrClass->GetActualClass(*addr);
1454  }
1455  if (ptrClass && claim) {
1456  if (!(claim->InheritsFrom(ptrClass) || ptrClass->InheritsFrom(claim))) {
1457  // Note we currently do not warn in case of splicing or over-expectation).
1458  if (claim->IsLoaded() && ptrClass->IsLoaded() && strcmp( claim->GetTypeInfo()->name(), ptrClass->GetTypeInfo()->name() ) == 0) {
1459  // The type is the same according to the C++ type_info, we must be in the case of
1460  // a template of Double32_t. This is actually a correct case.
1461  } else {
1462  Error("Branch", "The class requested (%s) for \"%s\" is different from the type of the pointer passed (%s)",
1463  claim->GetName(), branchname, ptrClass->GetName());
1464  }
1465  } else if (actualClass && (claim != actualClass) && !actualClass->InheritsFrom(claim)) {
1466  if (claim->IsLoaded() && actualClass->IsLoaded() && strcmp( claim->GetTypeInfo()->name(), actualClass->GetTypeInfo()->name() ) == 0) {
1467  // The type is the same according to the C++ type_info, we must be in the case of
1468  // a template of Double32_t. This is actually a correct case.
1469  } else {
1470  Error("Branch", "The actual class (%s) of the object provided for the definition of the branch \"%s\" does not inherit from %s",
1471  actualClass->GetName(), branchname, claim->GetName());
1472  }
1473  }
1474  }
1475  if (claim && claim->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(claim->GetCollectionProxy())) {
1476  Error("Branch", writeStlWithoutProxyMsg,
1477  claim->GetName(), branchname, claim->GetName());
1478  return 0;
1479  }
1480  return Branch(branchname, classname, (void*) addobj, bufsize, splitlevel);
1481 }
1482 
1483 ////////////////////////////////////////////////////////////////////////////////
1484 /// Same as TTree::Branch but automatic detection of the class name.
1485 /// See TTree::Branch for other details.
1486 
1487 TBranch* TTree::BranchImp(const char* branchname, TClass* ptrClass, void* addobj, Int_t bufsize, Int_t splitlevel)
1489  if (!ptrClass) {
1490  Error("Branch", "The pointer specified for %s is not of a class known to ROOT", branchname);
1491  return 0;
1492  }
1493  TClass* actualClass = 0;
1494  void** addr = (void**) addobj;
1495  if (addr && *addr) {
1496  actualClass = ptrClass->GetActualClass(*addr);
1497  if (!actualClass) {
1498  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",
1499  branchname, ptrClass->GetName());
1500  actualClass = ptrClass;
1501  } else if ((ptrClass != actualClass) && !actualClass->InheritsFrom(ptrClass)) {
1502  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());
1503  return 0;
1504  }
1505  } else {
1506  actualClass = ptrClass;
1507  }
1508  if (actualClass && actualClass->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(actualClass->GetCollectionProxy())) {
1509  Error("Branch", writeStlWithoutProxyMsg,
1510  actualClass->GetName(), branchname, actualClass->GetName());
1511  return 0;
1512  }
1513  return Branch(branchname, actualClass->GetName(), (void*) addobj, bufsize, splitlevel);
1514 }
1515 
1516 ////////////////////////////////////////////////////////////////////////////////
1517 /// Same as TTree::Branch but automatic detection of the class name.
1518 /// See TTree::Branch for other details.
1519 
1520 TBranch* TTree::BranchImpRef(const char* branchname, const char *classname, TClass* ptrClass, void *addobj, Int_t bufsize, Int_t splitlevel)
1522  TClass* claim = TClass::GetClass(classname);
1523  if (!ptrClass) {
1524  if (claim && claim->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(claim->GetCollectionProxy())) {
1525  Error("Branch", writeStlWithoutProxyMsg,
1526  claim->GetName(), branchname, claim->GetName());
1527  return 0;
1528  } else if (claim == 0) {
1529  Error("Branch", "The pointer specified for %s is not of a class known to ROOT and %s is not a known class", branchname, classname);
1530  return 0;
1531  }
1532  ptrClass = claim;
1533  }
1534  TClass* actualClass = 0;
1535  if (!addobj) {
1536  Error("Branch", "Reference interface requires a valid object (for branch: %s)!", branchname);
1537  return 0;
1538  }
1539  actualClass = ptrClass->GetActualClass(addobj);
1540  if (ptrClass && claim) {
1541  if (!(claim->InheritsFrom(ptrClass) || ptrClass->InheritsFrom(claim))) {
1542  // Note we currently do not warn in case of splicing or over-expectation).
1543  if (claim->IsLoaded() && ptrClass->IsLoaded() && strcmp( claim->GetTypeInfo()->name(), ptrClass->GetTypeInfo()->name() ) == 0) {
1544  // The type is the same according to the C++ type_info, we must be in the case of
1545  // a template of Double32_t. This is actually a correct case.
1546  } else {
1547  Error("Branch", "The class requested (%s) for \"%s\" is different from the type of the object passed (%s)",
1548  claim->GetName(), branchname, ptrClass->GetName());
1549  }
1550  } else if (actualClass && (claim != actualClass) && !actualClass->InheritsFrom(claim)) {
1551  if (claim->IsLoaded() && actualClass->IsLoaded() && strcmp( claim->GetTypeInfo()->name(), actualClass->GetTypeInfo()->name() ) == 0) {
1552  // The type is the same according to the C++ type_info, we must be in the case of
1553  // a template of Double32_t. This is actually a correct case.
1554  } else {
1555  Error("Branch", "The actual class (%s) of the object provided for the definition of the branch \"%s\" does not inherit from %s",
1556  actualClass->GetName(), branchname, claim->GetName());
1557  }
1558  }
1559  }
1560  if (!actualClass) {
1561  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",
1562  branchname, ptrClass->GetName());
1563  actualClass = ptrClass;
1564  } else if ((ptrClass != actualClass) && !actualClass->InheritsFrom(ptrClass)) {
1565  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());
1566  return 0;
1567  }
1568  if (actualClass && actualClass->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(actualClass->GetCollectionProxy())) {
1569  Error("Branch", writeStlWithoutProxyMsg,
1570  actualClass->GetName(), branchname, actualClass->GetName());
1571  return 0;
1572  }
1573  return BronchExec(branchname, actualClass->GetName(), (void*) addobj, kFALSE, bufsize, splitlevel);
1574 }
1575 
1576 ////////////////////////////////////////////////////////////////////////////////
1577 /// Same as TTree::Branch but automatic detection of the class name.
1578 /// See TTree::Branch for other details.
1579 
1580 TBranch* TTree::BranchImpRef(const char* branchname, TClass* ptrClass, EDataType datatype, void* addobj, Int_t bufsize, Int_t splitlevel)
1582  if (!ptrClass) {
1583  if (datatype == kOther_t || datatype == kNoType_t) {
1584  Error("Branch", "The pointer specified for %s is not of a class or type known to ROOT", branchname);
1585  } else {
1586  TString varname; varname.Form("%s/%c",branchname,DataTypeToChar(datatype));
1587  return Branch(branchname,addobj,varname.Data(),bufsize);
1588  }
1589  return 0;
1590  }
1591  TClass* actualClass = 0;
1592  if (!addobj) {
1593  Error("Branch", "Reference interface requires a valid object (for branch: %s)!", branchname);
1594  return 0;
1595  }
1596  actualClass = ptrClass->GetActualClass(addobj);
1597  if (!actualClass) {
1598  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",
1599  branchname, ptrClass->GetName());
1600  actualClass = ptrClass;
1601  } else if ((ptrClass != actualClass) && !actualClass->InheritsFrom(ptrClass)) {
1602  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());
1603  return 0;
1604  }
1605  if (actualClass && actualClass->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(actualClass->GetCollectionProxy())) {
1606  Error("Branch", writeStlWithoutProxyMsg,
1607  actualClass->GetName(), branchname, actualClass->GetName());
1608  return 0;
1609  }
1610  return BronchExec(branchname, actualClass->GetName(), (void*) addobj, kFALSE, bufsize, splitlevel);
1611 }
1612 
1613 ////////////////////////////////////////////////////////////////////////////////
1614 /// Deprecated function. Use next function instead.
1615 
1616 Int_t TTree::Branch(TList* li, Int_t bufsize /* = 32000 */ , Int_t splitlevel /* = 99 */)
1618  return Branch((TCollection*) li, bufsize, splitlevel);
1619 }
1620 
1621 ////////////////////////////////////////////////////////////////////////////////
1622 /// Create one branch for each element in the collection.
1623 ///
1624 /// Each entry in the collection becomes a top level branch if the
1625 /// corresponding class is not a collection. If it is a collection, the entry
1626 /// in the collection becomes in turn top level branches, etc.
1627 /// The splitlevel is decreased by 1 every time a new collection is found.
1628 /// For example if list is a TObjArray*
1629 /// - if splitlevel = 1, one top level branch is created for each element
1630 /// of the TObjArray.
1631 /// - if splitlevel = 2, one top level branch is created for each array element.
1632 /// if, in turn, one of the array elements is a TCollection, one top level
1633 /// branch will be created for each element of this collection.
1634 ///
1635 /// In case a collection element is a TClonesArray, the special Tree constructor
1636 /// for TClonesArray is called.
1637 /// The collection itself cannot be a TClonesArray.
1638 ///
1639 /// The function returns the total number of branches created.
1640 ///
1641 /// If name is given, all branch names will be prefixed with name_.
1642 ///
1643 /// IMPORTANT NOTE1: This function should not be called with splitlevel < 1.
1644 ///
1645 /// IMPORTANT NOTE2: The branches created by this function will have names
1646 /// corresponding to the collection or object names. It is important
1647 /// to give names to collections to avoid misleading branch names or
1648 /// identical branch names. By default collections have a name equal to
1649 /// the corresponding class name, e.g. the default name for a TList is "TList".
1650 ///
1651 /// And in general in any cases two or more master branches contain subbranches
1652 /// with identical names, one must add a "." (dot) character at the end
1653 /// of the master branch name. This will force the name of the subbranch
1654 /// to be master.subbranch instead of simply subbranch.
1655 /// This situation happens when the top level object (say event)
1656 /// has two or more members referencing the same class.
1657 /// For example, if a Tree has two branches B1 and B2 corresponding
1658 /// to objects of the same class MyClass, one can do:
1659 /// ~~~ {.cpp}
1660 /// tree.Branch("B1.","MyClass",&b1,8000,1);
1661 /// tree.Branch("B2.","MyClass",&b2,8000,1);
1662 /// ~~~
1663 /// if MyClass has 3 members a,b,c, the two instructions above will generate
1664 /// subbranches called B1.a, B1.b ,B1.c, B2.a, B2.b, B2.c
1665 ///
1666 /// Example:
1667 /// ~~~ {.cpp}
1668 /// {
1669 /// TTree T("T","test list");
1670 /// TList *list = new TList();
1671 ///
1672 /// TObjArray *a1 = new TObjArray();
1673 /// a1->SetName("a1");
1674 /// list->Add(a1);
1675 /// TH1F *ha1a = new TH1F("ha1a","ha1",100,0,1);
1676 /// TH1F *ha1b = new TH1F("ha1b","ha1",100,0,1);
1677 /// a1->Add(ha1a);
1678 /// a1->Add(ha1b);
1679 /// TObjArray *b1 = new TObjArray();
1680 /// b1->SetName("b1");
1681 /// list->Add(b1);
1682 /// TH1F *hb1a = new TH1F("hb1a","hb1",100,0,1);
1683 /// TH1F *hb1b = new TH1F("hb1b","hb1",100,0,1);
1684 /// b1->Add(hb1a);
1685 /// b1->Add(hb1b);
1686 ///
1687 /// TObjArray *a2 = new TObjArray();
1688 /// a2->SetName("a2");
1689 /// list->Add(a2);
1690 /// TH1S *ha2a = new TH1S("ha2a","ha2",100,0,1);
1691 /// TH1S *ha2b = new TH1S("ha2b","ha2",100,0,1);
1692 /// a2->Add(ha2a);
1693 /// a2->Add(ha2b);
1694 ///
1695 /// T.Branch(list,16000,2);
1696 /// T.Print();
1697 /// }
1698 /// ~~~
1699 
1700 Int_t TTree::Branch(TCollection* li, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 99 */, const char* name /* = "" */)
1702 
1703  if (!li) {
1704  return 0;
1705  }
1706  TObject* obj = 0;
1707  Int_t nbranches = GetListOfBranches()->GetEntries();
1708  if (li->InheritsFrom(TClonesArray::Class())) {
1709  Error("Branch", "Cannot call this constructor for a TClonesArray");
1710  return 0;
1711  }
1712  Int_t nch = strlen(name);
1713  TString branchname;
1714  TIter next(li);
1715  while ((obj = next())) {
1716  if ((splitlevel > 1) && obj->InheritsFrom(TCollection::Class()) && !obj->InheritsFrom(TClonesArray::Class())) {
1717  TCollection* col = (TCollection*) obj;
1718  if (nch) {
1719  branchname.Form("%s_%s_", name, col->GetName());
1720  } else {
1721  branchname.Form("%s_", col->GetName());
1722  }
1723  Branch(col, bufsize, splitlevel - 1, branchname);
1724  } else {
1725  if (nch && (name[nch-1] == '_')) {
1726  branchname.Form("%s%s", name, obj->GetName());
1727  } else {
1728  if (nch) {
1729  branchname.Form("%s_%s", name, obj->GetName());
1730  } else {
1731  branchname.Form("%s", obj->GetName());
1732  }
1733  }
1734  if (splitlevel > 99) {
1735  branchname += ".";
1736  }
1737  Bronch(branchname, obj->ClassName(), li->GetObjectRef(obj), bufsize, splitlevel - 1);
1738  }
1739  }
1740  return GetListOfBranches()->GetEntries() - nbranches;
1741 }
1742 
1743 ////////////////////////////////////////////////////////////////////////////////
1744 /// Create one branch for each element in the folder.
1745 /// Returns the total number of branches created.
1746 
1747 Int_t TTree::Branch(const char* foldername, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 99 */)
1749  TObject* ob = gROOT->FindObjectAny(foldername);
1750  if (!ob) {
1751  return 0;
1752  }
1753  if (ob->IsA() != TFolder::Class()) {
1754  return 0;
1755  }
1756  Int_t nbranches = GetListOfBranches()->GetEntries();
1757  TFolder* folder = (TFolder*) ob;
1758  TIter next(folder->GetListOfFolders());
1759  TObject* obj = 0;
1760  char* curname = new char[1000];
1761  char occur[20];
1762  while ((obj = next())) {
1763  snprintf(curname,1000, "%s/%s", foldername, obj->GetName());
1764  if (obj->IsA() == TFolder::Class()) {
1765  Branch(curname, bufsize, splitlevel - 1);
1766  } else {
1767  void* add = (void*) folder->GetListOfFolders()->GetObjectRef(obj);
1768  for (Int_t i = 0; i < 1000; ++i) {
1769  if (curname[i] == 0) {
1770  break;
1771  }
1772  if (curname[i] == '/') {
1773  curname[i] = '.';
1774  }
1775  }
1776  Int_t noccur = folder->Occurence(obj);
1777  if (noccur > 0) {
1778  snprintf(occur,20, "_%d", noccur);
1779  strlcat(curname, occur,1000);
1780  }
1781  TBranchElement* br = (TBranchElement*) Bronch(curname, obj->ClassName(), add, bufsize, splitlevel - 1);
1782  if (br) br->SetBranchFolder();
1783  }
1784  }
1785  delete[] curname;
1786  return GetListOfBranches()->GetEntries() - nbranches;
1787 }
1788 
1789 ////////////////////////////////////////////////////////////////////////////////
1790 /// Create a new TTree Branch.
1791 ///
1792 /// This Branch constructor is provided to support non-objects in
1793 /// a Tree. The variables described in leaflist may be simple
1794 /// variables or structures. // See the two following
1795 /// constructors for writing objects in a Tree.
1796 ///
1797 /// By default the branch buffers are stored in the same file as the Tree.
1798 /// use TBranch::SetFile to specify a different file
1799 ///
1800 /// * address is the address of the first item of a structure.
1801 /// * leaflist is the concatenation of all the variable names and types
1802 /// separated by a colon character :
1803 /// The variable name and the variable type are separated by a slash (/).
1804 /// The variable type may be 0,1 or 2 characters. If no type is given,
1805 /// the type of the variable is assumed to be the same as the previous
1806 /// variable. If the first variable does not have a type, it is assumed
1807 /// of type F by default. The list of currently supported types is given below:
1808 /// - `C` : a character string terminated by the 0 character
1809 /// - `B` : an 8 bit signed integer (`Char_t`)
1810 /// - `b` : an 8 bit unsigned integer (`UChar_t`)
1811 /// - `S` : a 16 bit signed integer (`Short_t`)
1812 /// - `s` : a 16 bit unsigned integer (`UShort_t`)
1813 /// - `I` : a 32 bit signed integer (`Int_t`)
1814 /// - `i` : a 32 bit unsigned integer (`UInt_t`)
1815 /// - `F` : a 32 bit floating point (`Float_t`)
1816 /// - `D` : a 64 bit floating point (`Double_t`)
1817 /// - `L` : a 64 bit signed integer (`Long64_t`)
1818 /// - `l` : a 64 bit unsigned integer (`ULong64_t`)
1819 /// - `O` : [the letter `o`, not a zero] a boolean (`Bool_t`)
1820 ///
1821 /// Arrays of values are supported with the following syntax:
1822 /// - If leaf name has the form var[nelem], where nelem is alphanumeric, then
1823 /// if nelem is a leaf name, it is used as the variable size of the array,
1824 /// otherwise return 0.
1825 /// - If leaf name has the form var[nelem], where nelem is a non-negative integer, then
1826 /// it is used as the fixed size of the array.
1827 /// - If leaf name has the form of a multi-dimensional array (e.g. var[nelem][nelem2])
1828 /// where nelem and nelem2 are non-negative integer) then
1829 /// it is used as a 2 dimensional array of fixed size.
1830 ///
1831 /// Any of other form is not supported.
1832 ///
1833 /// Note that the TTree will assume that all the item are contiguous in memory.
1834 /// On some platform, this is not always true of the member of a struct or a class,
1835 /// due to padding and alignment. Sorting your data member in order of decreasing
1836 /// sizeof usually leads to their being contiguous in memory.
1837 ///
1838 /// * bufsize is the buffer size in bytes for this branch
1839 /// The default value is 32000 bytes and should be ok for most cases.
1840 /// You can specify a larger value (e.g. 256000) if your Tree is not split
1841 /// and each entry is large (Megabytes)
1842 /// A small value for bufsize is optimum if you intend to access
1843 /// the entries in the Tree randomly and your Tree is in split mode.
1844 
1845 TBranch* TTree::Branch(const char* name, void* address, const char* leaflist, Int_t bufsize /* = 32000 */)
1847  TBranch* branch = new TBranch(this, name, address, leaflist, bufsize);
1848  if (branch->IsZombie()) {
1849  delete branch;
1850  branch = 0;
1851  return 0;
1852  }
1853  fBranches.Add(branch);
1854  return branch;
1855 }
1856 
1857 ////////////////////////////////////////////////////////////////////////////////
1858 /// Create a new branch with the object of class classname at address addobj.
1859 ///
1860 /// WARNING:
1861 ///
1862 /// Starting with Root version 3.01, the Branch function uses the new style
1863 /// branches (TBranchElement). To get the old behaviour, you can:
1864 /// - call BranchOld or
1865 /// - call TTree::SetBranchStyle(0)
1866 ///
1867 /// Note that with the new style, classname does not need to derive from TObject.
1868 /// It must derived from TObject if the branch style has been set to 0 (old)
1869 ///
1870 /// Note: See the comments in TBranchElement::SetAddress() for a more
1871 /// detailed discussion of the meaning of the addobj parameter in
1872 /// the case of new-style branches.
1873 ///
1874 /// Use splitlevel < 0 instead of splitlevel=0 when the class
1875 /// has a custom Streamer
1876 ///
1877 /// Note: if the split level is set to the default (99), TTree::Branch will
1878 /// not issue a warning if the class can not be split.
1879 
1880 TBranch* TTree::Branch(const char* name, const char* classname, void* addobj, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 99 */)
1882  if (fgBranchStyle == 1) {
1883  return Bronch(name, classname, addobj, bufsize, splitlevel);
1884  } else {
1885  if (splitlevel < 0) {
1886  splitlevel = 0;
1887  }
1888  return BranchOld(name, classname, addobj, bufsize, splitlevel);
1889  }
1890 }
1891 
1892 ////////////////////////////////////////////////////////////////////////////////
1893 /// Create a new TTree BranchObject.
1894 ///
1895 /// Build a TBranchObject for an object of class classname.
1896 /// addobj is the address of a pointer to an object of class classname.
1897 /// IMPORTANT: classname must derive from TObject.
1898 /// The class dictionary must be available (ClassDef in class header).
1899 ///
1900 /// This option requires access to the library where the corresponding class
1901 /// is defined. Accessing one single data member in the object implies
1902 /// reading the full object.
1903 /// See the next Branch constructor for a more efficient storage
1904 /// in case the entry consists of arrays of identical objects.
1905 ///
1906 /// By default the branch buffers are stored in the same file as the Tree.
1907 /// use TBranch::SetFile to specify a different file
1908 ///
1909 /// IMPORTANT NOTE about branch names:
1910 ///
1911 /// In case two or more master branches contain subbranches with
1912 /// identical names, one must add a "." (dot) character at the end
1913 /// of the master branch name. This will force the name of the subbranch
1914 /// to be master.subbranch instead of simply subbranch.
1915 /// This situation happens when the top level object (say event)
1916 /// has two or more members referencing the same class.
1917 /// For example, if a Tree has two branches B1 and B2 corresponding
1918 /// to objects of the same class MyClass, one can do:
1919 /// ~~~ {.cpp}
1920 /// tree.Branch("B1.","MyClass",&b1,8000,1);
1921 /// tree.Branch("B2.","MyClass",&b2,8000,1);
1922 /// ~~~
1923 /// if MyClass has 3 members a,b,c, the two instructions above will generate
1924 /// subbranches called B1.a, B1.b ,B1.c, B2.a, B2.b, B2.c
1925 ///
1926 /// bufsize is the buffer size in bytes for this branch
1927 /// The default value is 32000 bytes and should be ok for most cases.
1928 /// You can specify a larger value (e.g. 256000) if your Tree is not split
1929 /// and each entry is large (Megabytes)
1930 /// A small value for bufsize is optimum if you intend to access
1931 /// the entries in the Tree randomly and your Tree is in split mode.
1932 
1933 TBranch* TTree::BranchOld(const char* name, const char* classname, void* addobj, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 1 */)
1935  TClass* cl = TClass::GetClass(classname);
1936  if (!cl) {
1937  Error("BranchOld", "Cannot find class: '%s'", classname);
1938  return 0;
1939  }
1940  if (!cl->IsTObject()) {
1941  if (fgBranchStyle == 0) {
1942  Fatal("BranchOld", "The requested class ('%s') does not inherit from TObject.\n"
1943  "\tfgBranchStyle is set to zero requesting by default to use BranchOld.\n"
1944  "\tIf this is intentional use Bronch instead of Branch or BranchOld.", classname);
1945  } else {
1946  Fatal("BranchOld", "The requested class ('%s') does not inherit from TObject.\n"
1947  "\tYou can not use BranchOld to store objects of this type.",classname);
1948  }
1949  return 0;
1950  }
1951  TBranch* branch = new TBranchObject(this, name, classname, addobj, bufsize, splitlevel);
1952  fBranches.Add(branch);
1953  if (!splitlevel) {
1954  return branch;
1955  }
1956  // We are going to fully split the class now.
1957  TObjArray* blist = branch->GetListOfBranches();
1958  const char* rdname = 0;
1959  const char* dname = 0;
1960  TString branchname;
1961  char** apointer = (char**) addobj;
1962  TObject* obj = (TObject*) *apointer;
1963  Bool_t delobj = kFALSE;
1964  if (!obj) {
1965  obj = (TObject*) cl->New();
1966  delobj = kTRUE;
1967  }
1968  // Build the StreamerInfo if first time for the class.
1969  BuildStreamerInfo(cl, obj);
1970  // Loop on all public data members of the class and its base classes.
1971  Int_t lenName = strlen(name);
1972  Int_t isDot = 0;
1973  if (name[lenName-1] == '.') {
1974  isDot = 1;
1975  }
1976  TBranch* branch1 = 0;
1977  TRealData* rd = 0;
1978  TRealData* rdi = 0;
1979  TIter nexti(cl->GetListOfRealData());
1980  TIter next(cl->GetListOfRealData());
1981  // Note: This loop results in a full split because the
1982  // real data list includes all data members of
1983  // data members.
1984  while ((rd = (TRealData*) next())) {
1985  if (rd->TestBit(TRealData::kTransient)) continue;
1986 
1987  // Loop over all data members creating branches for each one.
1988  TDataMember* dm = rd->GetDataMember();
1989  if (!dm->IsPersistent()) {
1990  // Do not process members with an "!" as the first character in the comment field.
1991  continue;
1992  }
1993  if (rd->IsObject()) {
1994  // We skip data members of class type.
1995  // But we do build their real data, their
1996  // streamer info, and write their streamer
1997  // info to the current directory's file.
1998  // Oh yes, and we also do this for all of
1999  // their base classes.
2000  TClass* clm = TClass::GetClass(dm->GetFullTypeName());
2001  if (clm) {
2002  BuildStreamerInfo(clm, (char*) obj + rd->GetThisOffset());
2003  }
2004  continue;
2005  }
2006  rdname = rd->GetName();
2007  dname = dm->GetName();
2008  if (cl->CanIgnoreTObjectStreamer()) {
2009  // Skip the TObject base class data members.
2010  // FIXME: This prevents a user from ever
2011  // using these names themself!
2012  if (!strcmp(dname, "fBits")) {
2013  continue;
2014  }
2015  if (!strcmp(dname, "fUniqueID")) {
2016  continue;
2017  }
2018  }
2019  TDataType* dtype = dm->GetDataType();
2020  Int_t code = 0;
2021  if (dtype) {
2022  code = dm->GetDataType()->GetType();
2023  }
2024  // Encode branch name. Use real data member name
2025  branchname = rdname;
2026  if (isDot) {
2027  if (dm->IsaPointer()) {
2028  // FIXME: This is wrong! The asterisk is not usually in the front!
2029  branchname.Form("%s%s", name, &rdname[1]);
2030  } else {
2031  branchname.Form("%s%s", name, &rdname[0]);
2032  }
2033  }
2034  // FIXME: Change this to a string stream.
2035  TString leaflist;
2036  Int_t offset = rd->GetThisOffset();
2037  char* pointer = ((char*) obj) + offset;
2038  if (dm->IsaPointer()) {
2039  // We have a pointer to an object or a pointer to an array of basic types.
2040  TClass* clobj = 0;
2041  if (!dm->IsBasic()) {
2042  clobj = TClass::GetClass(dm->GetTypeName());
2043  }
2044  if (clobj && clobj->InheritsFrom(TClonesArray::Class())) {
2045  // We have a pointer to a clones array.
2046  char* cpointer = (char*) pointer;
2047  char** ppointer = (char**) cpointer;
2048  TClonesArray* li = (TClonesArray*) *ppointer;
2049  if (splitlevel != 2) {
2050  if (isDot) {
2051  branch1 = new TBranchClones(branch,branchname, pointer, bufsize);
2052  } else {
2053  // FIXME: This is wrong! The asterisk is not usually in the front!
2054  branch1 = new TBranchClones(branch,&branchname.Data()[1], pointer, bufsize);
2055  }
2056  blist->Add(branch1);
2057  } else {
2058  if (isDot) {
2059  branch1 = new TBranchObject(branch, branchname, li->ClassName(), pointer, bufsize);
2060  } else {
2061  // FIXME: This is wrong! The asterisk is not usually in the front!
2062  branch1 = new TBranchObject(branch, &branchname.Data()[1], li->ClassName(), pointer, bufsize);
2063  }
2064  blist->Add(branch1);
2065  }
2066  } else if (clobj) {
2067  // We have a pointer to an object.
2068  //
2069  // It must be a TObject object.
2070  if (!clobj->IsTObject()) {
2071  continue;
2072  }
2073  branch1 = new TBranchObject(branch, dname, clobj->GetName(), pointer, bufsize, 0);
2074  if (isDot) {
2075  branch1->SetName(branchname);
2076  } else {
2077  // FIXME: This is wrong! The asterisk is not usually in the front!
2078  // Do not use the first character (*).
2079  branch1->SetName(&branchname.Data()[1]);
2080  }
2081  blist->Add(branch1);
2082  } else {
2083  // We have a pointer to an array of basic types.
2084  //
2085  // Check the comments in the text of the code for an index specification.
2086  const char* index = dm->GetArrayIndex();
2087  if (index[0]) {
2088  // We are a pointer to a varying length array of basic types.
2089  //check that index is a valid data member name
2090  //if member is part of an object (e.g. fA and index=fN)
2091  //index must be changed from fN to fA.fN
2092  TString aindex (rd->GetName());
2093  Ssiz_t rdot = aindex.Last('.');
2094  if (rdot>=0) {
2095  aindex.Remove(rdot+1);
2096  aindex.Append(index);
2097  }
2098  nexti.Reset();
2099  while ((rdi = (TRealData*) nexti())) {
2100  if (rdi->TestBit(TRealData::kTransient)) continue;
2101 
2102  if (!strcmp(rdi->GetName(), index)) {
2103  break;
2104  }
2105  if (!strcmp(rdi->GetName(), aindex)) {
2106  index = rdi->GetName();
2107  break;
2108  }
2109  }
2110 
2111  char vcode = DataTypeToChar((EDataType)code);
2112  // Note that we differentiate between strings and
2113  // char array by the fact that there is NO specified
2114  // size for a string (see next if (code == 1)
2115 
2116  if (vcode) {
2117  leaflist.Form("%s[%s]/%c", &rdname[0], index, vcode);
2118  } else {
2119  Error("BranchOld", "Cannot create branch for rdname: %s code: %d", branchname.Data(), code);
2120  leaflist = "";
2121  }
2122  } else {
2123  // We are possibly a character string.
2124  if (code == 1) {
2125  // We are a character string.
2126  leaflist.Form("%s/%s", dname, "C");
2127  } else {
2128  // Invalid array specification.
2129  // FIXME: We need an error message here.
2130  continue;
2131  }
2132  }
2133  // There are '*' in both the branchname and leaflist, remove them.
2134  TString bname( branchname );
2135  bname.ReplaceAll("*","");
2136  leaflist.ReplaceAll("*","");
2137  // Add the branch to the tree and indicate that the address
2138  // is that of a pointer to be dereferenced before using.
2139  branch1 = new TBranch(branch, bname, *((void**) pointer), leaflist, bufsize);
2140  TLeaf* leaf = (TLeaf*) branch1->GetListOfLeaves()->At(0);
2142  leaf->SetAddress((void**) pointer);
2143  blist->Add(branch1);
2144  }
2145  } else if (dm->IsBasic()) {
2146  // We have a basic type.
2147 
2148  char vcode = DataTypeToChar((EDataType)code);
2149  if (vcode) {
2150  leaflist.Form("%s/%c", rdname, vcode);
2151  } else {
2152  Error("BranchOld", "Cannot create branch for rdname: %s code: %d", branchname.Data(), code);
2153  leaflist = "";
2154  }
2155  branch1 = new TBranch(branch, branchname, pointer, leaflist, bufsize);
2156  branch1->SetTitle(rdname);
2157  blist->Add(branch1);
2158  } else {
2159  // We have a class type.
2160  // Note: This cannot happen due to the rd->IsObject() test above.
2161  // FIXME: Put an error message here just in case.
2162  }
2163  if (branch1) {
2164  branch1->SetOffset(offset);
2165  } else {
2166  Warning("BranchOld", "Cannot process member: '%s'", rdname);
2167  }
2168  }
2169  if (delobj) {
2170  delete obj;
2171  obj = 0;
2172  }
2173  return branch;
2174 }
2175 
2176 ////////////////////////////////////////////////////////////////////////////////
2177 /// Build the optional branch supporting the TRefTable.
2178 /// This branch will keep all the information to find the branches
2179 /// containing referenced objects.
2180 ///
2181 /// At each Tree::Fill, the branch numbers containing the
2182 /// referenced objects are saved to the TBranchRef basket.
2183 /// When the Tree header is saved (via TTree::Write), the branch
2184 /// is saved keeping the information with the pointers to the branches
2185 /// having referenced objects.
2186 
2189  if (!fBranchRef) {
2190  fBranchRef = new TBranchRef(this);
2191  }
2192  return fBranchRef;
2193 }
2194 
2195 ////////////////////////////////////////////////////////////////////////////////
2196 /// Create a new TTree BranchElement.
2197 ///
2198 /// ## WARNING about this new function
2199 ///
2200 /// This function is designed to replace the internal
2201 /// implementation of the old TTree::Branch (whose implementation
2202 /// has been moved to BranchOld).
2203 ///
2204 /// NOTE: The 'Bronch' method supports only one possible calls
2205 /// signature (where the object type has to be specified
2206 /// explicitly and the address must be the address of a pointer).
2207 /// For more flexibility use 'Branch'. Use Bronch only in (rare)
2208 /// cases (likely to be legacy cases) where both the new and old
2209 /// implementation of Branch needs to be used at the same time.
2210 ///
2211 /// This function is far more powerful than the old Branch
2212 /// function. It supports the full C++, including STL and has
2213 /// the same behaviour in split or non-split mode. classname does
2214 /// not have to derive from TObject. The function is based on
2215 /// the new TStreamerInfo.
2216 ///
2217 /// Build a TBranchElement for an object of class classname.
2218 ///
2219 /// addr is the address of a pointer to an object of class
2220 /// classname. The class dictionary must be available (ClassDef
2221 /// in class header).
2222 ///
2223 /// Note: See the comments in TBranchElement::SetAddress() for a more
2224 /// detailed discussion of the meaning of the addr parameter.
2225 ///
2226 /// This option requires access to the library where the
2227 /// corresponding class is defined. Accessing one single data
2228 /// member in the object implies reading the full object.
2229 ///
2230 /// By default the branch buffers are stored in the same file as the Tree.
2231 /// use TBranch::SetFile to specify a different file
2232 ///
2233 /// IMPORTANT NOTE about branch names:
2234 ///
2235 /// In case two or more master branches contain subbranches with
2236 /// identical names, one must add a "." (dot) character at the end
2237 /// of the master branch name. This will force the name of the subbranch
2238 /// to be master.subbranch instead of simply subbranch.
2239 /// This situation happens when the top level object (say event)
2240 /// has two or more members referencing the same class.
2241 /// For example, if a Tree has two branches B1 and B2 corresponding
2242 /// to objects of the same class MyClass, one can do:
2243 /// ~~~ {.cpp}
2244 /// tree.Branch("B1.","MyClass",&b1,8000,1);
2245 /// tree.Branch("B2.","MyClass",&b2,8000,1);
2246 /// ~~~
2247 /// if MyClass has 3 members a,b,c, the two instructions above will generate
2248 /// subbranches called B1.a, B1.b ,B1.c, B2.a, B2.b, B2.c
2249 ///
2250 /// bufsize is the buffer size in bytes for this branch
2251 /// The default value is 32000 bytes and should be ok for most cases.
2252 /// You can specify a larger value (e.g. 256000) if your Tree is not split
2253 /// and each entry is large (Megabytes)
2254 /// A small value for bufsize is optimum if you intend to access
2255 /// the entries in the Tree randomly and your Tree is in split mode.
2256 ///
2257 /// Use splitlevel < 0 instead of splitlevel=0 when the class
2258 /// has a custom Streamer
2259 ///
2260 /// Note: if the split level is set to the default (99), TTree::Branch will
2261 /// not issue a warning if the class can not be split.
2262 
2263 TBranch* TTree::Bronch(const char* name, const char* classname, void* addr, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 99 */)
2265  return BronchExec(name, classname, addr, kTRUE, bufsize, splitlevel);
2266 }
2267 
2268 ////////////////////////////////////////////////////////////////////////////////
2269 /// Helper function implementing TTree::Bronch and TTree::Branch(const char *name, T &obj);
2270 
2271 TBranch* TTree::BronchExec(const char* name, const char* classname, void* addr, Bool_t isptrptr, Int_t bufsize /* = 32000 */, Int_t splitlevel /* = 99 */)
2273  TClass* cl = TClass::GetClass(classname);
2274  if (!cl) {
2275  Error("Bronch", "Cannot find class:%s", classname);
2276  return 0;
2277  }
2278 
2279  //if splitlevel <= 0 and class has a custom Streamer, we must create
2280  //a TBranchObject. We cannot assume that TClass::ReadBuffer is consistent
2281  //with the custom Streamer. The penalty is that one cannot process
2282  //this Tree without the class library containing the class.
2283 
2284  char* objptr = 0;
2285  if (!isptrptr) {
2286  objptr = (char*)addr;
2287  } else if (addr) {
2288  objptr = *((char**) addr);
2289  }
2290 
2291  if (cl == TClonesArray::Class()) {
2292  TClonesArray* clones = (TClonesArray*) objptr;
2293  if (!clones) {
2294  Error("Bronch", "Pointer to TClonesArray is null");
2295  return 0;
2296  }
2297  if (!clones->GetClass()) {
2298  Error("Bronch", "TClonesArray with no class defined in branch: %s", name);
2299  return 0;
2300  }
2301  if (!clones->GetClass()->HasDataMemberInfo()) {
2302  Error("Bronch", "TClonesArray with no dictionary defined in branch: %s", name);
2303  return 0;
2304  }
2305  bool hasCustomStreamer = clones->GetClass()->TestBit(TClass::kHasCustomStreamerMember);
2306  if (splitlevel > 0) {
2307  if (hasCustomStreamer)
2308  Warning("Bronch", "Using split mode on a class: %s with a custom Streamer", clones->GetClass()->GetName());
2309  } else {
2310  if (hasCustomStreamer) clones->BypassStreamer(kFALSE);
2311  TBranchObject *branch = new TBranchObject(this,name,classname,addr,bufsize,0,/*compress=*/ -1,isptrptr);
2312  fBranches.Add(branch);
2313  return branch;
2314  }
2315  }
2316 
2317  if (cl->GetCollectionProxy()) {
2318  TVirtualCollectionProxy* collProxy = cl->GetCollectionProxy();
2319  //if (!collProxy) {
2320  // Error("Bronch", "%s is missing its CollectionProxy (for branch %s)", classname, name);
2321  //}
2322  TClass* inklass = collProxy->GetValueClass();
2323  if (!inklass && (collProxy->GetType() == 0)) {
2324  Error("Bronch", "%s with no class defined in branch: %s", classname, name);
2325  return 0;
2326  }
2327  if ((splitlevel > 0) && inklass && (inklass->GetCollectionProxy() == 0)) {
2328  ROOT::ESTLType stl = cl->GetCollectionType();
2329  if ((stl != ROOT::kSTLmap) && (stl != ROOT::kSTLmultimap)) {
2330  if (!inklass->HasDataMemberInfo()) {
2331  Error("Bronch", "Container with no dictionary defined in branch: %s", name);
2332  return 0;
2333  }
2334  if (inklass->TestBit(TClass::kHasCustomStreamerMember)) {
2335  Warning("Bronch", "Using split mode on a class: %s with a custom Streamer", inklass->GetName());
2336  }
2337  }
2338  }
2339  //-------------------------------------------------------------------------
2340  // If the splitting switch is enabled, the split level is big enough and
2341  // the collection contains pointers we can split it
2342  //////////////////////////////////////////////////////////////////////////
2343 
2344  TBranch *branch;
2345  if( splitlevel > kSplitCollectionOfPointers && collProxy->HasPointers() )
2346  branch = new TBranchSTL( this, name, collProxy, bufsize, splitlevel );
2347  else
2348  branch = new TBranchElement(this, name, collProxy, bufsize, splitlevel);
2349  fBranches.Add(branch);
2350  if (isptrptr) {
2351  branch->SetAddress(addr);
2352  } else {
2353  branch->SetObject(addr);
2354  }
2355  return branch;
2356  }
2357 
2358  Bool_t hasCustomStreamer = kFALSE;
2359  if (!cl->HasDataMemberInfo() && !cl->GetCollectionProxy()) {
2360  Error("Bronch", "Cannot find dictionary for class: %s", classname);
2361  return 0;
2362  }
2363 
2365  // Not an STL container and the linkdef file had a "-" after the class name.
2366  hasCustomStreamer = kTRUE;
2367  }
2368 
2369  if (splitlevel < 0 || ((splitlevel == 0) && hasCustomStreamer && cl->IsTObject())) {
2370  TBranchObject* branch = new TBranchObject(this, name, classname, addr, bufsize, 0, /*compress=*/ -1, isptrptr);
2371  fBranches.Add(branch);
2372  return branch;
2373  }
2374 
2375  if (cl == TClonesArray::Class()) {
2376  // Special case of TClonesArray.
2377  // No dummy object is created.
2378  // The streamer info is not rebuilt unoptimized.
2379  // No dummy top-level branch is created.
2380  // No splitting is attempted.
2381  TBranchElement* branch = new TBranchElement(this, name, (TClonesArray*) objptr, bufsize, splitlevel%kSplitCollectionOfPointers);
2382  fBranches.Add(branch);
2383  if (isptrptr) {
2384  branch->SetAddress(addr);
2385  } else {
2386  branch->SetObject(addr);
2387  }
2388  return branch;
2389  }
2390 
2391  //
2392  // If we are not given an object to use as an i/o buffer
2393  // then create a temporary one which we will delete just
2394  // before returning.
2395  //
2396 
2397  Bool_t delobj = kFALSE;
2398 
2399  if (!objptr) {
2400  objptr = (char*) cl->New();
2401  delobj = kTRUE;
2402  }
2403 
2404  //
2405  // Avoid splitting unsplittable classes.
2406  //
2407 
2408  if ((splitlevel > 0) && !cl->CanSplit()) {
2409  if (splitlevel != 99) {
2410  Warning("Bronch", "%s cannot be split, resetting splitlevel to 0", cl->GetName());
2411  }
2412  splitlevel = 0;
2413  }
2414 
2415  //
2416  // Make sure the streamer info is built and fetch it.
2417  //
2418  // If we are splitting, then make sure the streamer info
2419  // is built unoptimized (data members are not combined).
2420  //
2421 
2422  TStreamerInfo* sinfo = BuildStreamerInfo(cl, objptr, splitlevel==0);
2423  if (!sinfo) {
2424  Error("Bronch", "Cannot build the StreamerInfo for class: %s", cl->GetName());
2425  return 0;
2426  }
2427 
2428  //
2429  // Create a dummy top level branch object.
2430  //
2431 
2432  Int_t id = -1;
2433  if (splitlevel > 0) {
2434  id = -2;
2435  }
2436  TBranchElement* branch = new TBranchElement(this, name, sinfo, id, objptr, bufsize, splitlevel);
2437  fBranches.Add(branch);
2438 
2439  //
2440  // Do splitting, if requested.
2441  //
2442 
2443  if (splitlevel%kSplitCollectionOfPointers > 0) {
2444  branch->Unroll(name, cl, sinfo, objptr, bufsize, splitlevel);
2445  }
2446 
2447  //
2448  // Setup our offsets into the user's i/o buffer.
2449  //
2450 
2451  if (isptrptr) {
2452  branch->SetAddress(addr);
2453  } else {
2454  branch->SetObject(addr);
2455  }
2456 
2457  if (delobj) {
2458  cl->Destructor(objptr);
2459  objptr = 0;
2460  }
2461 
2462  return branch;
2463 }
2464 
2465 ////////////////////////////////////////////////////////////////////////////////
2466 /// Browse content of the TTree.
2467 
2468 void TTree::Browse(TBrowser* b)
2470  fBranches.Browse(b);
2471  if (fUserInfo) {
2472  if (strcmp("TList",fUserInfo->GetName())==0) {
2473  fUserInfo->SetName("UserInfo");
2474  b->Add(fUserInfo);
2475  fUserInfo->SetName("TList");
2476  } else {
2477  b->Add(fUserInfo);
2478  }
2479  }
2480 }
2481 
2482 ////////////////////////////////////////////////////////////////////////////////
2483 /// Build a Tree Index (default is TTreeIndex).
2484 /// See a description of the parameters and functionality in
2485 /// TTreeIndex::TTreeIndex().
2486 ///
2487 /// The return value is the number of entries in the Index (< 0 indicates failure).
2488 ///
2489 /// A TTreeIndex object pointed by fTreeIndex is created.
2490 /// This object will be automatically deleted by the TTree destructor.
2491 /// See also comments in TTree::SetTreeIndex().
2492 
2493 Int_t TTree::BuildIndex(const char* majorname, const char* minorname /* = "0" */)
2495  fTreeIndex = GetPlayer()->BuildIndex(this, majorname, minorname);
2496  if (fTreeIndex->IsZombie()) {
2497  delete fTreeIndex;
2498  fTreeIndex = 0;
2499  return 0;
2500  }
2501  return fTreeIndex->GetN();
2502 }
2503 
2504 ////////////////////////////////////////////////////////////////////////////////
2505 /// Build StreamerInfo for class cl.
2506 /// pointer is an optional argument that may contain a pointer to an object of cl.
2507 
2508 TStreamerInfo* TTree::BuildStreamerInfo(TClass* cl, void* pointer /* = 0 */, Bool_t canOptimize /* = kTRUE */ )
2510  if (!cl) {
2511  return 0;
2512  }
2513  cl->BuildRealData(pointer);
2515 
2516  // Create StreamerInfo for all base classes.
2517  TBaseClass* base = 0;
2518  TIter nextb(cl->GetListOfBases());
2519  while((base = (TBaseClass*) nextb())) {
2520  if (base->IsSTLContainer()) {
2521  continue;
2522  }
2523  TClass* clm = TClass::GetClass(base->GetName());
2524  BuildStreamerInfo(clm, pointer, canOptimize);
2525  }
2526  if (sinfo && fDirectory) {
2527  sinfo->ForceWriteInfo(fDirectory->GetFile());
2528  }
2529  return sinfo;
2530 }
2531 
2532 ////////////////////////////////////////////////////////////////////////////////
2533 /// Called by TTree::Fill() when file has reached its maximum fgMaxTreeSize.
2534 /// Create a new file. If the original file is named "myfile.root",
2535 /// subsequent files are named "myfile_1.root", "myfile_2.root", etc.
2536 ///
2537 /// Returns a pointer to the new file.
2538 ///
2539 /// Currently, the automatic change of file is restricted
2540 /// to the case where the tree is in the top level directory.
2541 /// The file should not contain sub-directories.
2542 ///
2543 /// Before switching to a new file, the tree header is written
2544 /// to the current file, then the current file is closed.
2545 ///
2546 /// To process the multiple files created by ChangeFile, one must use
2547 /// a TChain.
2548 ///
2549 /// The new file name has a suffix "_N" where N is equal to fFileNumber+1.
2550 /// By default a Root session starts with fFileNumber=0. One can set
2551 /// fFileNumber to a different value via TTree::SetFileNumber.
2552 /// In case a file named "_N" already exists, the function will try
2553 /// a file named "__N", then "___N", etc.
2554 ///
2555 /// fgMaxTreeSize can be set via the static function TTree::SetMaxTreeSize.
2556 /// The default value of fgMaxTreeSize is 100 Gigabytes.
2557 ///
2558 /// If the current file contains other objects like TH1 and TTree,
2559 /// these objects are automatically moved to the new file.
2560 ///
2561 /// IMPORTANT NOTE:
2562 ///
2563 /// Be careful when writing the final Tree header to the file!
2564 ///
2565 /// Don't do:
2566 /// ~~~ {.cpp}
2567 /// TFile *file = new TFile("myfile.root","recreate");
2568 /// TTree *T = new TTree("T","title");
2569 /// T->Fill(); //loop
2570 /// file->Write();
2571 /// file->Close();
2572 /// ~~~
2573 /// but do the following:
2574 /// ~~~ {.cpp}
2575 /// TFile *file = new TFile("myfile.root","recreate");
2576 /// TTree *T = new TTree("T","title");
2577 /// T->Fill(); //loop
2578 /// file = T->GetCurrentFile(); //to get the pointer to the current file
2579 /// file->Write();
2580 /// file->Close();
2581 /// ~~~
2582 
2585  file->cd();
2586  Write();
2587  Reset();
2588  char* fname = new char[2000];
2589  ++fFileNumber;
2590  char uscore[10];
2591  for (Int_t i = 0; i < 10; ++i) {
2592  uscore[i] = 0;
2593  }
2594  Int_t nus = 0;
2595  // Try to find a suitable file name that does not already exist.
2596  while (nus < 10) {
2597  uscore[nus] = '_';
2598  fname[0] = 0;
2599  strlcpy(fname, file->GetName(),2000);
2600 
2601  if (fFileNumber > 1) {
2602  char* cunder = strrchr(fname, '_');
2603  if (cunder) {
2604  snprintf(cunder,2000-Int_t(cunder-fname), "%s%d", uscore, fFileNumber);
2605  const char* cdot = strrchr(file->GetName(), '.');
2606  if (cdot) {
2607  strlcat(fname, cdot,2000);
2608  }
2609  } else {
2610  char fcount[10];
2611  snprintf(fcount,10, "%s%d", uscore, fFileNumber);
2612  strlcat(fname, fcount,2000);
2613  }
2614  } else {
2615  char* cdot = strrchr(fname, '.');
2616  if (cdot) {
2617  snprintf(cdot,2000-Int_t(fname-cdot), "%s%d", uscore, fFileNumber);
2618  strlcat(fname, strrchr(file->GetName(), '.'),2000);
2619  } else {
2620  char fcount[10];
2621  snprintf(fcount,10, "%s%d", uscore, fFileNumber);
2622  strlcat(fname, fcount,2000);
2623  }
2624  }
2625  if (gSystem->AccessPathName(fname)) {
2626  break;
2627  }
2628  ++nus;
2629  Warning("ChangeFile", "file %s already exist, trying with %d underscores", fname, nus+1);
2630  }
2631  Int_t compress = file->GetCompressionSettings();
2632  TFile* newfile = TFile::Open(fname, "recreate", "chain files", compress);
2633  if (newfile == 0) {
2634  Error("Fill","Failed to open new file %s, continuing as a memory tree.",fname);
2635  } else {
2636  Printf("Fill: Switching to new file: %s", fname);
2637  }
2638  // The current directory may contain histograms and trees.
2639  // These objects must be moved to the new file.
2640  TBranch* branch = 0;
2641  TObject* obj = 0;
2642  while ((obj = file->GetList()->First())) {
2643  file->Remove(obj);
2644  // Histogram: just change the directory.
2645  if (obj->InheritsFrom("TH1")) {
2646  gROOT->ProcessLine(TString::Format("((%s*)0x%lx)->SetDirectory((TDirectory*)0x%lx);", obj->ClassName(), (Long_t) obj, (Long_t) newfile));
2647  continue;
2648  }
2649  // Tree: must save all trees in the old file, reset them.
2650  if (obj->InheritsFrom(TTree::Class())) {
2651  TTree* t = (TTree*) obj;
2652  if (t != this) {
2653  t->AutoSave();
2654  t->Reset();
2655  t->fFileNumber = fFileNumber;
2656  }
2657  t->SetDirectory(newfile);
2658  TIter nextb(t->GetListOfBranches());
2659  while ((branch = (TBranch*)nextb())) {
2660  branch->SetFile(newfile);
2661  }
2662  if (t->GetBranchRef()) {
2663  t->GetBranchRef()->SetFile(newfile);
2664  }
2665  continue;
2666  }
2667  // Not a TH1 or a TTree, move object to new file.
2668  if (newfile) newfile->Append(obj);
2669  file->Remove(obj);
2670  }
2671  delete file;
2672  file = 0;
2673  delete[] fname;
2674  fname = 0;
2675  return newfile;
2676 }
2677 
2678 ////////////////////////////////////////////////////////////////////////////////
2679 /// Check whether or not the address described by the last 3 parameters
2680 /// matches the content of the branch. If a Data Model Evolution conversion
2681 /// is involved, reset the fInfo of the branch.
2682 /// The return values are:
2683 //
2684 /// - kMissingBranch (-5) : Missing branch
2685 /// - kInternalError (-4) : Internal error (could not find the type corresponding to a data type number)
2686 /// - kMissingCompiledCollectionProxy (-3) : Missing compiled collection proxy for a compiled collection
2687 /// - kMismatch (-2) : Non-Class Pointer type given does not match the type expected by the branch
2688 /// - kClassMismatch (-1) : Class Pointer type given does not match the type expected by the branch
2689 /// - kMatch (0) : perfect match
2690 /// - kMatchConversion (1) : match with (I/O) conversion
2691 /// - kMatchConversionCollection (2) : match with (I/O) conversion of the content of a collection
2692 /// - kMakeClass (3) : MakeClass mode so we can not check.
2693 /// - kVoidPtr (4) : void* passed so no check was made.
2694 /// - kNoCheck (5) : Underlying TBranch not yet available so no check was made.
2695 
2696 Int_t TTree::CheckBranchAddressType(TBranch* branch, TClass* ptrClass, EDataType datatype, Bool_t isptr)
2698  if (GetMakeClass()) {
2699  // If we are in MakeClass mode so we do not really use classes.
2700  return kMakeClass;
2701  }
2702 
2703  // Let's determine what we need!
2704  TClass* expectedClass = 0;
2705  EDataType expectedType = kOther_t;
2706  if (0 != branch->GetExpectedType(expectedClass,expectedType) ) {
2707  // Something went wrong, the warning message has already be issued.
2708  return kInternalError;
2709  }
2710  if (expectedClass && datatype == kOther_t && ptrClass == 0) {
2711  if (branch->InheritsFrom( TBranchElement::Class() )) {
2712  TBranchElement* bEl = (TBranchElement*)branch;
2713  bEl->SetTargetClass( expectedClass->GetName() );
2714  }
2715  if (expectedClass && expectedClass->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(expectedClass->GetCollectionProxy())) {
2716  Error("SetBranchAddress", "Unable to determine the type given for the address for \"%s\". "
2717  "The class expected (%s) refers to an stl collection and do not have a compiled CollectionProxy. "
2718  "Please generate the dictionary for this class (%s)",
2719  branch->GetName(), expectedClass->GetName(), expectedClass->GetName());
2721  }
2722  if (!expectedClass->IsLoaded()) {
2723  // The originally expected class does not have a dictionary, it is then plausible that the pointer being passed is the right type
2724  // (we really don't know). So let's express that.
2725  Error("SetBranchAddress", "Unable to determine the type given for the address for \"%s\". "
2726  "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."
2727  "Please generate the dictionary for this class (%s)",
2728  branch->GetName(), expectedClass->GetName(), expectedClass->GetName());
2729  } else {
2730  Error("SetBranchAddress", "Unable to determine the type given for the address for \"%s\". "
2731  "This is probably due to a missing dictionary, the original data class for this branch is %s.", branch->GetName(), expectedClass->GetName());
2732  }
2733  return kClassMismatch;
2734  }
2735  if (expectedClass && ptrClass && (branch->GetMother() == branch)) {
2736  // Top Level branch
2737  if (!isptr) {
2738  Error("SetBranchAddress", "The address for \"%s\" should be the address of a pointer!", branch->GetName());
2739  }
2740  }
2741  if (expectedType == kFloat16_t) {
2742  expectedType = kFloat_t;
2743  }
2744  if (expectedType == kDouble32_t) {
2745  expectedType = kDouble_t;
2746  }
2747  if (datatype == kFloat16_t) {
2748  datatype = kFloat_t;
2749  }
2750  if (datatype == kDouble32_t) {
2751  datatype = kDouble_t;
2752  }
2753 
2754  /////////////////////////////////////////////////////////////////////////////
2755  // Deal with the class renaming
2756  /////////////////////////////////////////////////////////////////////////////
2757 
2758  if( expectedClass && ptrClass &&
2759  expectedClass != ptrClass &&
2760  branch->InheritsFrom( TBranchElement::Class() ) &&
2761  ptrClass->GetSchemaRules() &&
2762  ptrClass->GetSchemaRules()->HasRuleWithSourceClass( expectedClass->GetName() ) ) {
2763  TBranchElement* bEl = (TBranchElement*)branch;
2764 
2765  if ( ptrClass->GetCollectionProxy() && expectedClass->GetCollectionProxy() ) {
2766  if (gDebug > 7)
2767  Info("SetBranchAddress", "Matching STL collection (at least according to the SchemaRuleSet when "
2768  "reading a %s into a %s",expectedClass->GetName(),ptrClass->GetName());
2769 
2770  bEl->SetTargetClass( ptrClass->GetName() );
2771  return kMatchConversion;
2772 
2773  } else if ( !ptrClass->GetConversionStreamerInfo( expectedClass, bEl->GetClassVersion() ) &&
2774  !ptrClass->FindConversionStreamerInfo( expectedClass, bEl->GetCheckSum() ) ) {
2775  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());
2776 
2777  bEl->SetTargetClass( expectedClass->GetName() );
2778  return kClassMismatch;
2779  }
2780  else {
2781 
2782  bEl->SetTargetClass( ptrClass->GetName() );
2783  return kMatchConversion;
2784  }
2785 
2786  } else if (expectedClass && ptrClass && !expectedClass->InheritsFrom(ptrClass)) {
2787 
2788  if (expectedClass->GetCollectionProxy() && ptrClass->GetCollectionProxy() &&
2789  branch->InheritsFrom( TBranchElement::Class() ) &&
2790  expectedClass->GetCollectionProxy()->GetValueClass() &&
2791  ptrClass->GetCollectionProxy()->GetValueClass() )
2792  {
2793  // In case of collection, we know how to convert them, if we know how to convert their content.
2794  // NOTE: we need to extend this to std::pair ...
2795 
2796  TClass *onfileValueClass = expectedClass->GetCollectionProxy()->GetValueClass();
2797  TClass *inmemValueClass = ptrClass->GetCollectionProxy()->GetValueClass();
2798 
2799  if (inmemValueClass->GetSchemaRules() &&
2800  inmemValueClass->GetSchemaRules()->HasRuleWithSourceClass(onfileValueClass->GetName() ) )
2801  {
2802  TBranchElement* bEl = (TBranchElement*)branch;
2803  bEl->SetTargetClass( ptrClass->GetName() );
2805  }
2806  }
2807 
2808  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());
2809  if (branch->InheritsFrom( TBranchElement::Class() )) {
2810  TBranchElement* bEl = (TBranchElement*)branch;
2811  bEl->SetTargetClass( expectedClass->GetName() );
2812  }
2813  return kClassMismatch;
2814 
2815  } else if ((expectedType != kOther_t) && (datatype != kOther_t) && (expectedType != kNoType_t) && (datatype != kNoType_t) && (expectedType != datatype)) {
2816  if (datatype != kChar_t) {
2817  // For backward compatibility we assume that (char*) was just a cast and/or a generic address
2818  Error("SetBranchAddress", "The pointer type given \"%s\" (%d) does not correspond to the type needed \"%s\" (%d) by the branch: %s",
2819  TDataType::GetTypeName(datatype), datatype, TDataType::GetTypeName(expectedType), expectedType, branch->GetName());
2820  return kMismatch;
2821  }
2822  } else if ((expectedClass && (datatype != kOther_t && datatype != kNoType_t && datatype != kInt_t)) ||
2823  (ptrClass && (expectedType != kOther_t && expectedType != kNoType_t && datatype != kInt_t)) ) {
2824  // Sometime a null pointer can look an int, avoid complaining in that case.
2825  if (expectedClass) {
2826  Error("SetBranchAddress", "The pointer type given \"%s\" (%d) does not correspond to the type needed \"%s\" by the branch: %s",
2827  TDataType::GetTypeName(datatype), datatype, expectedClass->GetName(), branch->GetName());
2828  if (branch->InheritsFrom( TBranchElement::Class() )) {
2829  TBranchElement* bEl = (TBranchElement*)branch;
2830  bEl->SetTargetClass( expectedClass->GetName() );
2831  }
2832  } else {
2833  // 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
2834  // a struct).
2835  bool found = false;
2836  if (ptrClass->IsLoaded()) {
2837  TIter next(ptrClass->GetListOfRealData());
2838  TRealData *rdm;
2839  while ((rdm = (TRealData*)next())) {
2840  if (rdm->GetThisOffset() == 0) {
2841  TDataType *dmtype = rdm->GetDataMember()->GetDataType();
2842  if (dmtype) {
2843  EDataType etype = (EDataType)dmtype->GetType();
2844  if (etype == expectedType) {
2845  found = true;
2846  }
2847  }
2848  break;
2849  }
2850  }
2851  } else {
2852  TIter next(ptrClass->GetListOfDataMembers());
2853  TDataMember *dm;
2854  while ((dm = (TDataMember*)next())) {
2855  if (dm->GetOffset() == 0) {
2856  TDataType *dmtype = dm->GetDataType();
2857  if (dmtype) {
2858  EDataType etype = (EDataType)dmtype->GetType();
2859  if (etype == expectedType) {
2860  found = true;
2861  }
2862  }
2863  break;
2864  }
2865  }
2866  }
2867  if (found) {
2868  // let's check the size.
2869  TLeaf *last = (TLeaf*)branch->GetListOfLeaves()->Last();
2870  long len = last->GetOffset() + last->GetLenType() * last->GetLen();
2871  if (len <= ptrClass->Size()) {
2872  return kMatch;
2873  }
2874  }
2875  Error("SetBranchAddress", "The pointer type given \"%s\" does not correspond to the type needed \"%s\" (%d) by the branch: %s",
2876  ptrClass->GetName(), TDataType::GetTypeName(expectedType), expectedType, branch->GetName());
2877  }
2878  return kMismatch;
2879  }
2880  if (expectedClass && expectedClass->GetCollectionProxy() && dynamic_cast<TEmulatedCollectionProxy*>(expectedClass->GetCollectionProxy())) {
2881  Error("SetBranchAddress", writeStlWithoutProxyMsg,
2882  expectedClass->GetName(), branch->GetName(), expectedClass->GetName());
2883  if (branch->InheritsFrom( TBranchElement::Class() )) {
2884  TBranchElement* bEl = (TBranchElement*)branch;
2885  bEl->SetTargetClass( expectedClass->GetName() );
2886  }
2888  }
2889  if (expectedClass && branch->InheritsFrom( TBranchElement::Class() )) {
2890  TBranchElement* bEl = (TBranchElement*)branch;
2891  bEl->SetTargetClass( expectedClass->GetName() );
2892  }
2893  return kMatch;
2894 }
2895 
2896 ////////////////////////////////////////////////////////////////////////////////
2897 /// Create a clone of this tree and copy nentries.
2898 ///
2899 /// By default copy all entries.
2900 /// The compression level of the cloned tree is set to the destination
2901 /// file's compression level.
2902 ///
2903 /// NOTE: Only active branches are copied.
2904 /// NOTE: If the TTree is a TChain, the structure of the first TTree
2905 /// is used for the copy.
2906 ///
2907 /// IMPORTANT: The cloned tree stays connected with this tree until
2908 /// this tree is deleted. In particular, any changes in
2909 /// branch addresses in this tree are forwarded to the
2910 /// clone trees, unless a branch in a clone tree has had
2911 /// its address changed, in which case that change stays in
2912 /// effect. When this tree is deleted, all the addresses of
2913 /// the cloned tree are reset to their default values.
2914 ///
2915 /// If 'option' contains the word 'fast' and nentries is -1, the
2916 /// cloning will be done without unzipping or unstreaming the baskets
2917 /// (i.e., a direct copy of the raw bytes on disk).
2918 ///
2919 /// When 'fast' is specified, 'option' can also contain a sorting
2920 /// order for the baskets in the output file.
2921 ///
2922 /// There are currently 3 supported sorting order:
2923 ///
2924 /// - SortBasketsByOffset (the default)
2925 /// - SortBasketsByBranch
2926 /// - SortBasketsByEntry
2927 ///
2928 /// When using SortBasketsByOffset the baskets are written in the
2929 /// output file in the same order as in the original file (i.e. the
2930 /// baskets are sorted by their offset in the original file; Usually
2931 /// this also means that the baskets are sorted by the index/number of
2932 /// the _last_ entry they contain)
2933 ///
2934 /// When using SortBasketsByBranch all the baskets of each individual
2935 /// branches are stored contiguously. This tends to optimize reading
2936 /// speed when reading a small number (1->5) of branches, since all
2937 /// their baskets will be clustered together instead of being spread
2938 /// across the file. However it might decrease the performance when
2939 /// reading more branches (or the full entry).
2940 ///
2941 /// When using SortBasketsByEntry the baskets with the lowest starting
2942 /// entry are written first. (i.e. the baskets are sorted by the
2943 /// index/number of the first entry they contain). This means that on
2944 /// the file the baskets will be in the order in which they will be
2945 /// needed when reading the whole tree sequentially.
2946 ///
2947 /// For examples of CloneTree, see tutorials:
2948 ///
2949 /// - copytree.C:
2950 /// A macro to copy a subset of a TTree to a new TTree.
2951 /// The input file has been generated by the program in
2952 /// $ROOTSYS/test/Event with: Event 1000 1 1 1
2953 ///
2954 /// - copytree2.C:
2955 /// A macro to copy a subset of a TTree to a new TTree.
2956 /// One branch of the new Tree is written to a separate file.
2957 /// The input file has been generated by the program in
2958 /// $ROOTSYS/test/Event with: Event 1000 1 1 1
2959 
2960 TTree* TTree::CloneTree(Long64_t nentries /* = -1 */, Option_t* option /* = "" */)
2962  // Options
2963  Bool_t fastClone = kFALSE;
2964 
2965  TString opt = option;
2966  opt.ToLower();
2967  if (opt.Contains("fast")) {
2968  fastClone = kTRUE;
2969  }
2970 
2971  // If we are a chain, switch to the first tree.
2972  if ((fEntries > 0) && (LoadTree(0) < 0)) {
2973  // FIXME: We need an error message here.
2974  return 0;
2975  }
2976 
2977  // Note: For a tree we get the this pointer, for
2978  // a chain we get the chain's current tree.
2979  TTree* thistree = GetTree();
2980 
2981  // We will use this to override the IO features on the cloned branches.
2982  ROOT::TIOFeatures features = this->GetIOFeatures();
2983  ;
2984 
2985  // Note: For a chain, the returned clone will be
2986  // a clone of the chain's first tree.
2987  TTree* newtree = (TTree*) thistree->Clone();
2988  if (!newtree) {
2989  return 0;
2990  }
2991 
2992  // The clone should not delete any objects allocated by SetAddress().
2993  TObjArray* branches = newtree->GetListOfBranches();
2994  Int_t nb = branches->GetEntriesFast();
2995  for (Int_t i = 0; i < nb; ++i) {
2996  TBranch* br = (TBranch*) branches->UncheckedAt(i);
2997  if (br->InheritsFrom(TBranchElement::Class())) {
2998  ((TBranchElement*) br)->ResetDeleteObject();
2999  }
3000  }
3001 
3002  // Add the new tree to the list of clones so that
3003  // we can later inform it of changes to branch addresses.
3004  thistree->AddClone(newtree);
3005  if (thistree != this) {
3006  // In case this object is a TChain, add the clone
3007  // also to the TChain's list of clones.
3008  AddClone(newtree);
3009  }
3010 
3011  newtree->Reset();
3012 
3013  TDirectory* ndir = newtree->GetDirectory();
3014  TFile* nfile = 0;
3015  if (ndir) {
3016  nfile = ndir->GetFile();
3017  }
3018  Int_t newcomp = -1;
3019  if (nfile) {
3020  newcomp = nfile->GetCompressionSettings();
3021  }
3022 
3023  //
3024  // Delete non-active branches from the clone.
3025  //
3026  // Note: If we are a chain, this does nothing
3027  // since chains have no leaves.
3028  TObjArray* leaves = newtree->GetListOfLeaves();
3029  Int_t nleaves = leaves->GetEntriesFast();
3030  for (Int_t lndx = 0; lndx < nleaves; ++lndx) {
3031  TLeaf* leaf = (TLeaf*) leaves->UncheckedAt(lndx);
3032  if (!leaf) {
3033  continue;
3034  }
3035  TBranch* branch = leaf->GetBranch();
3036  if (branch && (newcomp > -1)) {
3037  branch->SetCompressionSettings(newcomp);
3038  }
3039  if (branch) branch->SetIOFeatures(features);
3040  if (!branch || !branch->TestBit(kDoNotProcess)) {
3041  continue;
3042  }
3043  // size might change at each iteration of the loop over the leaves.
3044  nb = branches->GetEntriesFast();
3045  for (Long64_t i = 0; i < nb; ++i) {
3046  TBranch* br = (TBranch*) branches->UncheckedAt(i);
3047  if (br == branch) {
3048  branches->RemoveAt(i);
3049  delete br;
3050  br = 0;
3051  branches->Compress();
3052  break;
3053  }
3054  TObjArray* lb = br->GetListOfBranches();
3055  Int_t nb1 = lb->GetEntriesFast();
3056  for (Int_t j = 0; j < nb1; ++j) {
3057  TBranch* b1 = (TBranch*) lb->UncheckedAt(j);
3058  if (!b1) {
3059  continue;
3060  }
3061  if (b1 == branch) {
3062  lb->RemoveAt(j);
3063  delete b1;
3064  b1 = 0;
3065  lb->Compress();
3066  break;
3067  }
3068  TObjArray* lb1 = b1->GetListOfBranches();
3069  Int_t nb2 = lb1->GetEntriesFast();
3070  for (Int_t k = 0; k < nb2; ++k) {
3071  TBranch* b2 = (TBranch*) lb1->UncheckedAt(k);
3072  if (!b2) {
3073  continue;
3074  }
3075  if (b2 == branch) {
3076  lb1->RemoveAt(k);
3077  delete b2;
3078  b2 = 0;
3079  lb1->Compress();
3080  break;
3081  }
3082  }
3083  }
3084  }
3085  }
3086  leaves->Compress();
3087 
3088  // Copy MakeClass status.
3089  newtree->SetMakeClass(fMakeClass);
3090 
3091  // Copy branch addresses.
3092  CopyAddresses(newtree);
3093 
3094  //
3095  // Copy entries if requested.
3096  //
3097 
3098  if (nentries != 0) {
3099  if (fastClone && (nentries < 0)) {
3100  if ( newtree->CopyEntries( this, -1, option ) < 0 ) {
3101  // There was a problem!
3102  Error("CloneTTree", "TTree has not been cloned\n");
3103  delete newtree;
3104  newtree = 0;
3105  return 0;
3106  }
3107  } else {
3108  newtree->CopyEntries( this, nentries, option );
3109  }
3110  }
3111 
3112  return newtree;
3113 }
3114 
3115 ////////////////////////////////////////////////////////////////////////////////
3116 /// Set branch addresses of passed tree equal to ours.
3117 /// If undo is true, reset the branch address instead of copying them.
3118 /// This insures 'separation' of a cloned tree from its original
3119 
3120 void TTree::CopyAddresses(TTree* tree, Bool_t undo)
3122  // Copy branch addresses starting from branches.
3124  Int_t nbranches = branches->GetEntriesFast();
3125  for (Int_t i = 0; i < nbranches; ++i) {
3126  TBranch* branch = (TBranch*) branches->UncheckedAt(i);
3127  if (branch->TestBit(kDoNotProcess)) {
3128  continue;
3129  }
3130  if (undo) {
3131  TBranch* br = tree->GetBranch(branch->GetName());
3132  tree->ResetBranchAddress(br);
3133  } else {
3134  char* addr = branch->GetAddress();
3135  if (!addr) {
3136  if (branch->IsA() == TBranch::Class()) {
3137  // If the branch was created using a leaflist, the branch itself may not have
3138  // an address but the leaf might already.
3139  TLeaf *firstleaf = (TLeaf*)branch->GetListOfLeaves()->At(0);
3140  if (!firstleaf || firstleaf->GetValuePointer()) {
3141  // Either there is no leaf (and thus no point in copying the address)
3142  // or the leaf has an address but we can not copy it via the branche
3143  // this will be copied via the next loop (over the leaf).
3144  continue;
3145  }
3146  }
3147  // Note: This may cause an object to be allocated.
3148  branch->SetAddress(0);
3149  addr = branch->GetAddress();
3150  }
3151  // FIXME: The GetBranch() function is braindead and may
3152  // not find the branch!
3153  TBranch* br = tree->GetBranch(branch->GetName());
3154  if (br) {
3155  br->SetAddress(addr);
3156  // The copy does not own any object allocated by SetAddress().
3157  if (br->InheritsFrom(TBranchElement::Class())) {
3158  ((TBranchElement*) br)->ResetDeleteObject();
3159  }
3160  } else {
3161  Warning("CopyAddresses", "Could not find branch named '%s' in tree named '%s'", branch->GetName(), tree->GetName());
3162  }
3163  }
3164  }
3165 
3166  // Copy branch addresses starting from leaves.
3167  TObjArray* tleaves = tree->GetListOfLeaves();
3168  Int_t ntleaves = tleaves->GetEntriesFast();
3169  for (Int_t i = 0; i < ntleaves; ++i) {
3170  TLeaf* tleaf = (TLeaf*) tleaves->UncheckedAt(i);
3171  TBranch* tbranch = tleaf->GetBranch();
3172  TBranch* branch = GetBranch(tbranch->GetName());
3173  if (!branch) {
3174  continue;
3175  }
3176  TLeaf* leaf = branch->GetLeaf(tleaf->GetName());
3177  if (!leaf) {
3178  continue;
3179  }
3180  if (branch->TestBit(kDoNotProcess)) {
3181  continue;
3182  }
3183  if (undo) {
3184  // Now we know whether the address has been transfered
3185  tree->ResetBranchAddress(tbranch);
3186  } else {
3187  TBranchElement *mother = dynamic_cast<TBranchElement*>(leaf->GetBranch()->GetMother());
3188  if (leaf->GetLeafCount() && (leaf->TestBit(TLeaf::kNewValue) || !leaf->GetValuePointer() || (mother && mother->IsObjectOwner())) && tleaf->GetLeafCount())
3189  {
3190  // If it is an array and it was allocated by the leaf itself,
3191  // let's make sure it is large enough for the incoming data.
3192  if (leaf->GetLeafCount()->GetMaximum() < tleaf->GetLeafCount()->GetMaximum()) {
3193  tleaf->IncludeRange( leaf );
3194  if (leaf->GetValuePointer()) {
3195  if (leaf->IsA() == TLeafElement::Class() && mother)
3196  mother->ResetAddress();
3197  else
3198  leaf->SetAddress(nullptr);
3199  }
3200  }
3201  }
3202  if (!branch->GetAddress() && !leaf->GetValuePointer()) {
3203  // We should attempts to set the address of the branch.
3204  // something like:
3205  //(TBranchElement*)branch->GetMother()->SetAddress(0)
3206  //plus a few more subtilities (see TBranchElement::GetEntry).
3207  //but for now we go the simplest route:
3208  //
3209  // Note: This may result in the allocation of an object.
3210  branch->SetupAddresses();
3211  }
3212  if (branch->GetAddress()) {
3213  tree->SetBranchAddress(branch->GetName(), (void*) branch->GetAddress());
3214  TBranch* br = tree->GetBranch(branch->GetName());
3215  if (br) {
3216  // The copy does not own any object allocated by SetAddress().
3217  // FIXME: We do too much here, br may not be a top-level branch.
3218  if (br->InheritsFrom(TBranchElement::Class())) {
3219  ((TBranchElement*) br)->ResetDeleteObject();
3220  }
3221  } else {
3222  Warning("CopyAddresses", "Could not find branch named '%s' in tree named '%s'", branch->GetName(), tree->GetName());
3223  }
3224  } else {
3225  tleaf->SetAddress(leaf->GetValuePointer());
3226  }
3227  }
3228  }
3229 
3230  if (undo &&
3231  ( tree->IsA()->InheritsFrom("TNtuple") || tree->IsA()->InheritsFrom("TNtupleD") )
3232  ) {
3233  tree->ResetBranchAddresses();
3234  }
3235 }
3236 
3237 namespace {
3238 
3239  enum EOnIndexError { kDrop, kKeep, kBuild };
3241  static Bool_t R__HandleIndex(EOnIndexError onIndexError, TTree *newtree, TTree *oldtree)
3242  {
3243  // Return true if we should continue to handle indices, false otherwise.
3244 
3245  Bool_t withIndex = kTRUE;
3246 
3247  if ( newtree->GetTreeIndex() ) {
3248  if ( oldtree->GetTree()->GetTreeIndex() == 0 ) {
3249  switch (onIndexError) {
3250  case kDrop:
3251  delete newtree->GetTreeIndex();
3252  newtree->SetTreeIndex(0);
3253  withIndex = kFALSE;
3254  break;
3255  case kKeep:
3256  // Nothing to do really.
3257  break;
3258  case kBuild:
3259  // Build the index then copy it
3260  if (oldtree->GetTree()->BuildIndex(newtree->GetTreeIndex()->GetMajorName(), newtree->GetTreeIndex()->GetMinorName())) {
3261  newtree->GetTreeIndex()->Append(oldtree->GetTree()->GetTreeIndex(), kTRUE);
3262  // Clean up
3263  delete oldtree->GetTree()->GetTreeIndex();
3264  oldtree->GetTree()->SetTreeIndex(0);
3265  }
3266  break;
3267  }
3268  } else {
3269  newtree->GetTreeIndex()->Append(oldtree->GetTree()->GetTreeIndex(), kTRUE);
3270  }
3271  } else if ( oldtree->GetTree()->GetTreeIndex() != 0 ) {
3272  // We discover the first index in the middle of the chain.
3273  switch (onIndexError) {
3274  case kDrop:
3275  // Nothing to do really.
3276  break;
3277  case kKeep: {
3278  TVirtualIndex *index = (TVirtualIndex*) oldtree->GetTree()->GetTreeIndex()->Clone();
3279  index->SetTree(newtree);
3280  newtree->SetTreeIndex(index);
3281  break;
3282  }
3283  case kBuild:
3284  if (newtree->GetEntries() == 0) {
3285  // Start an index.
3286  TVirtualIndex *index = (TVirtualIndex*) oldtree->GetTree()->GetTreeIndex()->Clone();
3287  index->SetTree(newtree);
3288  newtree->SetTreeIndex(index);
3289  } else {
3290  // Build the index so far.
3291  if (newtree->BuildIndex(oldtree->GetTree()->GetTreeIndex()->GetMajorName(), oldtree->GetTree()->GetTreeIndex()->GetMinorName())) {
3292  newtree->GetTreeIndex()->Append(oldtree->GetTree()->GetTreeIndex(), kTRUE);
3293  }
3294  }
3295  break;
3296  }
3297  } else if ( onIndexError == kDrop ) {
3298  // There is no index on this or on tree->GetTree(), we know we have to ignore any further
3299  // index
3300  withIndex = kFALSE;
3301  }
3302  return withIndex;
3303  }
3304 }
3305 
3306 ////////////////////////////////////////////////////////////////////////////////
3307 /// Copy nentries from given tree to this tree.
3308 /// This routines assumes that the branches that intended to be copied are
3309 /// already connected. The typical case is that this tree was created using
3310 /// tree->CloneTree(0).
3311 ///
3312 /// By default copy all entries.
3313 ///
3314 /// Returns number of bytes copied to this tree.
3315 ///
3316 /// If 'option' contains the word 'fast' and nentries is -1, the cloning will be
3317 /// done without unzipping or unstreaming the baskets (i.e., a direct copy of the
3318 /// raw bytes on disk).
3319 ///
3320 /// When 'fast' is specified, 'option' can also contains a sorting order for the
3321 /// baskets in the output file.
3322 ///
3323 /// There are currently 3 supported sorting order:
3324 ///
3325 /// - SortBasketsByOffset (the default)
3326 /// - SortBasketsByBranch
3327 /// - SortBasketsByEntry
3328 ///
3329 /// See TTree::CloneTree for a detailed explanation of the semantics of these 3 options.
3330 ///
3331 /// If the tree or any of the underlying tree of the chain has an index, that index and any
3332 /// index in the subsequent underlying TTree objects will be merged.
3333 ///
3334 /// There are currently three 'options' to control this merging:
3335 /// - NoIndex : all the TTreeIndex object are dropped.
3336 /// - DropIndexOnError : if any of the underlying TTree object do no have a TTreeIndex,
3337 /// they are all dropped.
3338 /// - AsIsIndexOnError [default]: In case of missing TTreeIndex, the resulting TTree index has gaps.
3339 /// - BuildIndexOnError : If any of the underlying TTree objects do not have a TTreeIndex,
3340 /// all TTreeIndex are 'ignored' and the missing piece are rebuilt.
3341 
3342 Long64_t TTree::CopyEntries(TTree* tree, Long64_t nentries /* = -1 */, Option_t* option /* = "" */)
3344  if (!tree) {
3345  return 0;
3346  }
3347  // Options
3348  TString opt = option;
3349  opt.ToLower();
3350  Bool_t fastClone = opt.Contains("fast");
3351  Bool_t withIndex = !opt.Contains("noindex");
3352  EOnIndexError onIndexError;
3353  if (opt.Contains("asisindex")) {
3354  onIndexError = kKeep;
3355  } else if (opt.Contains("buildindex")) {
3356  onIndexError = kBuild;
3357  } else if (opt.Contains("dropindex")) {
3358  onIndexError = kDrop;
3359  } else {
3360  onIndexError = kBuild;
3361  }
3362  Ssiz_t cacheSizeLoc = opt.Index("cachesize=");
3363  Int_t cacheSize = -1;
3364  if (cacheSizeLoc != TString::kNPOS) {
3365  // If the parse faile, cacheSize stays at -1.
3366  Ssiz_t cacheSizeEnd = opt.Index(" ",cacheSizeLoc+10) - (cacheSizeLoc+10);
3367  TSubString cacheSizeStr( opt(cacheSizeLoc+10,cacheSizeEnd) );
3368  auto parseResult = ROOT::FromHumanReadableSize(cacheSizeStr,cacheSize);
3369  if (parseResult == ROOT::EFromHumanReadableSize::kParseFail) {
3370  Warning("CopyEntries","The cachesize option can not be parsed: %s. The default size will be used.",cacheSizeStr.String().Data());
3371  } else if (parseResult == ROOT::EFromHumanReadableSize::kOverflow) {
3372  double m;
3373  const char *munit = nullptr;
3374  ROOT::ToHumanReadableSize(std::numeric_limits<decltype(cacheSize)>::max(),false,&m,&munit);
3375 
3376  Warning("CopyEntries","The cachesize option is too large: %s (%g%s max). The default size will be used.",cacheSizeStr.String().Data(),m,munit);
3377  }
3378  }
3379  if (gDebug > 0 && cacheSize != -1) Info("CopyEntries","Using Cache size: %d\n",cacheSize);
3380 
3381  Long64_t nbytes = 0;
3382  Long64_t treeEntries = tree->GetEntriesFast();
3383  if (nentries < 0) {
3384  nentries = treeEntries;
3385  } else if (nentries > treeEntries) {
3386  nentries = treeEntries;
3387  }
3388 
3389  if (fastClone && (nentries < 0 || nentries == tree->GetEntriesFast())) {
3390  // Quickly copy the basket without decompression and streaming.
3391  Long64_t totbytes = GetTotBytes();
3392  for (Long64_t i = 0; i < nentries; i += tree->GetTree()->GetEntries()) {
3393  if (tree->LoadTree(i) < 0) {
3394  break;
3395  }
3396  if ( withIndex ) {
3397  withIndex = R__HandleIndex( onIndexError, this, tree );
3398  }
3399  if (this->GetDirectory()) {
3400  TFile* file2 = this->GetDirectory()->GetFile();
3401  if (file2 && (file2->GetEND() > TTree::GetMaxTreeSize())) {
3402  if (this->GetDirectory() == (TDirectory*) file2) {
3403  this->ChangeFile(file2);
3404  }
3405  }
3406  }
3407  TTreeCloner cloner(tree->GetTree(), this, option, TTreeCloner::kNoWarnings);
3408  if (cloner.IsValid()) {
3409  this->SetEntries(this->GetEntries() + tree->GetTree()->GetEntries());
3410  if (cacheSize != -1) cloner.SetCacheSize(cacheSize);
3411  cloner.Exec();
3412  } else {
3413  if (i == 0) {
3414  Warning("CopyEntries","%s",cloner.GetWarning());
3415  // If the first cloning does not work, something is really wrong
3416  // (since apriori the source and target are exactly the same structure!)
3417  return -1;
3418  } else {
3419  if (cloner.NeedConversion()) {
3420  TTree *localtree = tree->GetTree();
3421  Long64_t tentries = localtree->GetEntries();
3422  for (Long64_t ii = 0; ii < tentries; ii++) {
3423  if (localtree->GetEntry(ii) <= 0) {
3424  break;
3425  }
3426  this->Fill();
3427  }
3428  if (this->GetTreeIndex()) {
3429  this->GetTreeIndex()->Append(tree->GetTree()->GetTreeIndex(), kTRUE);
3430  }
3431  } else {
3432  Warning("CopyEntries","%s",cloner.GetWarning());
3433  if (tree->GetDirectory() && tree->GetDirectory()->GetFile()) {
3434  Warning("CopyEntries", "Skipped file %s\n", tree->GetDirectory()->GetFile()->GetName());
3435  } else {
3436  Warning("CopyEntries", "Skipped file number %d\n", tree->GetTreeNumber());
3437  }
3438  }
3439  }
3440  }
3441 
3442  }
3443  if (this->GetTreeIndex()) {
3444  this->GetTreeIndex()->Append(0,kFALSE); // Force the sorting
3445  }
3446  nbytes = GetTotBytes() - totbytes;
3447  } else {
3448  if (nentries < 0) {
3449  nentries = treeEntries;
3450  } else if (nentries > treeEntries) {
3451  nentries = treeEntries;
3452  }
3453  Int_t treenumber = -1;
3454  for (Long64_t i = 0; i < nentries; i++) {
3455  if (tree->LoadTree(i) < 0) {
3456  break;
3457  }
3458  if (treenumber != tree->GetTreeNumber()) {
3459  if ( withIndex ) {
3460  withIndex = R__HandleIndex( onIndexError, this, tree );
3461  }
3462  treenumber = tree->GetTreeNumber();
3463  }
3464  if (tree->GetEntry(i) <= 0) {
3465  break;
3466  }
3467  nbytes += this->Fill();
3468  }
3469  if (this->GetTreeIndex()) {
3470  this->GetTreeIndex()->Append(0,kFALSE); // Force the sorting
3471  }
3472  }
3473  return nbytes;
3474 }
3475 
3476 ////////////////////////////////////////////////////////////////////////////////
3477 /// Copy a tree with selection.
3478 ///
3479 /// ### Important:
3480 ///
3481 /// The returned copied tree stays connected with the original tree
3482 /// until the original tree is deleted. In particular, any changes
3483 /// to the branch addresses in the original tree are also made to
3484 /// the copied tree. Any changes made to the branch addresses of the
3485 /// copied tree are overridden anytime the original tree changes its
3486 /// branch addresses. When the original tree is deleted, all the
3487 /// branch addresses of the copied tree are set to zero.
3488 ///
3489 /// For examples of CopyTree, see the tutorials:
3490 ///
3491 /// - copytree.C:
3492 /// Example macro to copy a subset of a tree to a new tree.
3493 /// The input file was generated by running the program in
3494 /// $ROOTSYS/test/Event in this way:
3495 /// ~~~ {.cpp}
3496 /// ./Event 1000 1 1 1
3497 /// ~~~
3498 /// - copytree2.C
3499 /// Example macro to copy a subset of a tree to a new tree.
3500 /// One branch of the new tree is written to a separate file.
3501 /// The input file was generated by running the program in
3502 /// $ROOTSYS/test/Event in this way:
3503 /// ~~~ {.cpp}
3504 /// ./Event 1000 1 1 1
3505 /// ~~~
3506 /// - copytree3.C
3507 /// Example macro to copy a subset of a tree to a new tree.
3508 /// Only selected entries are copied to the new tree.
3509 /// NOTE that only the active branches are copied.
3510 
3511 TTree* TTree::CopyTree(const char* selection, Option_t* option /* = 0 */, Long64_t nentries /* = TTree::kMaxEntries */, Long64_t firstentry /* = 0 */)
3513  GetPlayer();
3514  if (fPlayer) {
3515  return fPlayer->CopyTree(selection, option, nentries, firstentry);
3516  }
3517  return 0;
3518 }
3519 
3520 ////////////////////////////////////////////////////////////////////////////////
3521 /// Create a basket for this tree and given branch.
3522 
3525  if (!branch) {
3526  return 0;
3527  }
3528  return new TBasket(branch->GetName(), GetName(), branch);
3529 }
3530 
3531 ////////////////////////////////////////////////////////////////////////////////
3532 /// Delete this tree from memory or/and disk.
3533 ///
3534 /// - if option == "all" delete Tree object from memory AND from disk
3535 /// all baskets on disk are deleted. All keys with same name
3536 /// are deleted.
3537 /// - if option =="" only Tree object in memory is deleted.
3538 
3539 void TTree::Delete(Option_t* option /* = "" */)
3541  TFile *file = GetCurrentFile();
3542 
3543  // delete all baskets and header from file
3544  if (file && !strcmp(option,"all")) {
3545  if (!file->IsWritable()) {
3546  Error("Delete","File : %s is not writable, cannot delete Tree:%s", file->GetName(),GetName());
3547  return;
3548  }
3549 
3550  //find key and import Tree header in memory
3551  TKey *key = fDirectory->GetKey(GetName());
3552  if (!key) return;
3553 
3554  TDirectory *dirsav = gDirectory;
3555  file->cd();
3556 
3557  //get list of leaves and loop on all the branches baskets
3558  TIter next(GetListOfLeaves());
3559  TLeaf *leaf;
3560  char header[16];
3561  Int_t ntot = 0;
3562  Int_t nbask = 0;
3563  Int_t nbytes,objlen,keylen;
3564  while ((leaf = (TLeaf*)next())) {
3565  TBranch *branch = leaf->GetBranch();
3566  Int_t nbaskets = branch->GetMaxBaskets();
3567  for (Int_t i=0;i<nbaskets;i++) {
3568  Long64_t pos = branch->GetBasketSeek(i);
3569  if (!pos) continue;
3570  TFile *branchFile = branch->GetFile();
3571  if (!branchFile) continue;
3572  branchFile->GetRecordHeader(header,pos,16,nbytes,objlen,keylen);
3573  if (nbytes <= 0) continue;
3574  branchFile->MakeFree(pos,pos+nbytes-1);
3575  ntot += nbytes;
3576  nbask++;
3577  }
3578  }
3579 
3580  // delete Tree header key and all keys with the same name
3581  // A Tree may have been saved many times. Previous cycles are invalid.
3582  while (key) {
3583  ntot += key->GetNbytes();
3584  key->Delete();
3585  delete key;
3586  key = fDirectory->GetKey(GetName());
3587  }
3588  if (dirsav) dirsav->cd();
3589  if (gDebug) Info("TTree::Delete", "Deleting Tree: %s: %d baskets deleted. Total space freed = %d bytes\n",GetName(),nbask,ntot);
3590  }
3591 
3592  if (fDirectory) {
3593  fDirectory->Remove(this);
3594  //delete the file cache if it points to this Tree
3595  MoveReadCache(file,0);
3596  fDirectory = 0;
3598  }
3599 
3600  // Delete object from CINT symbol table so it can not be used anymore.
3601  gCling->DeleteGlobal(this);
3602 
3603  // Warning: We have intentional invalidated this object while inside a member function!
3604  delete this;
3605 }
3606 
3607  ///////////////////////////////////////////////////////////////////////////////
3608  /// Called by TKey and TObject::Clone to automatically add us to a directory
3609  /// when we are read from a file.
3610 
3613  if (fDirectory == dir) return;
3614  if (fDirectory) {
3615  fDirectory->Remove(this);
3616  // Delete or move the file cache if it points to this Tree
3617  TFile *file = fDirectory->GetFile();
3618  MoveReadCache(file,dir);
3619  }
3620  fDirectory = dir;
3621  TBranch* b = 0;
3622  TIter next(GetListOfBranches());
3623  while((b = (TBranch*) next())) {
3624  b->UpdateFile();
3625  }
3626  if (fBranchRef) {
3628  }
3629  if (fDirectory) fDirectory->Append(this);
3630 }
3631 
3632 ////////////////////////////////////////////////////////////////////////////////
3633 /// Draw expression varexp for specified entries.
3634 ///
3635 /// \return -1 in case of error or number of selected events in case of success.
3636 ///
3637 /// This function accepts TCut objects as arguments.
3638 /// Useful to use the string operator +
3639 ///
3640 /// Example:
3641 ///
3642 /// ~~~ {.cpp}
3643 /// ntuple.Draw("x",cut1+cut2+cut3);
3644 /// ~~~
3645 
3646 
3647 Long64_t TTree::Draw(const char* varexp, const TCut& selection, Option_t* option, Long64_t nentries, Long64_t firstentry)
3649  return TTree::Draw(varexp, selection.GetTitle(), option, nentries, firstentry);
3650 }
3651 
3652 ////////////////////////////////////////////////////////////////////////////////
3653 /// Draw expression varexp for specified entries.
3654 ///
3655 /// \return -1 in case of error or number of selected events in case of success.
3656 ///
3657 /// \param [in] varexp is an expression of the general form
3658 /// - "e1" produces a 1-d histogram (TH1F) of expression "e1"
3659 /// - "e1:e2" produces an unbinned 2-d scatter-plot (TGraph) of "e1"
3660 /// on the y-axis versus "e2" on the x-axis
3661 /// - "e1:e2:e3" produces an unbinned 3-d scatter-plot (TPolyMarker3D) of "e1"
3662 /// vs "e2" vs "e3" on the x-, y-, z-axis, respectively.
3663 /// - "e1:e2:e3:e4" produces an unbinned 3-d scatter-plot (TPolyMarker3D) of "e1"
3664 /// vs "e2" vs "e3" and "e4" mapped on the current color palette.
3665 /// (to create histograms in the 2, 3, and 4 dimensional case,
3666 /// see section "Saving the result of Draw to an histogram")
3667 ///
3668 /// Example:
3669 /// - varexp = x simplest case: draw a 1-Dim distribution of column named x
3670 /// - varexp = sqrt(x) : draw distribution of sqrt(x)
3671 /// - varexp = x*y/z
3672 /// - varexp = y:sqrt(x) 2-Dim distribution of y versus sqrt(x)
3673 /// - varexp = px:py:pz:2.5*E produces a 3-d scatter-plot of px vs py ps pz
3674 /// and the color number of each marker will be 2.5*E.
3675 /// If the color number is negative it is set to 0.
3676 /// If the color number is greater than the current number of colors
3677 /// it is set to the highest color number.The default number of
3678 /// colors is 50. see TStyle::SetPalette for setting a new color palette.
3679 ///
3680 /// Note that the variables e1, e2 or e3 may contain a selection.
3681 /// example, if e1= x*(y<0), the value histogrammed will be x if y<0
3682 /// and will be 0 otherwise.
3683 ///
3684 /// The expressions can use all the operations and build-in functions
3685 /// supported by TFormula (See TFormula::Analyze), including free
3686 /// standing function taking numerical arguments (TMath::Bessel).
3687 /// In addition, you can call member functions taking numerical
3688 /// arguments. For example:
3689 /// ~~~ {.cpp}
3690 /// TMath::BreitWigner(fPx,3,2)
3691 /// event.GetHistogram().GetXaxis().GetXmax()
3692 /// ~~~
3693 /// Note: You can only pass expression that depend on the TTree's data
3694 /// to static functions and you can only call non-static member function
3695 /// with 'fixed' parameters.
3696 ///
3697 /// \param [in] selection is an expression with a combination of the columns.
3698 /// In a selection all the C++ operators are authorized.
3699 /// The value corresponding to the selection expression is used as a weight
3700 /// to fill the histogram.
3701 /// If the expression includes only boolean operations, the result
3702 /// is 0 or 1. If the result is 0, the histogram is not filled.
3703 /// In general, the expression may be of the form:
3704 /// ~~~ {.cpp}
3705 /// value*(boolean expression)
3706 /// ~~~
3707 /// if boolean expression is true, the histogram is filled with
3708 /// a `weight = value`.
3709 /// Examples:
3710 /// - selection1 = "x<y && sqrt(z)>3.2"
3711 /// - selection2 = "(x+y)*(sqrt(z)>3.2)"
3712 /// - selection1 returns a weight = 0 or 1
3713 /// - selection2 returns a weight = x+y if sqrt(z)>3.2
3714 /// returns a weight = 0 otherwise.
3715 ///
3716 /// \param [in] option is the drawing option.
3717 /// - When an histogram is produced it can be any histogram drawing option
3718 /// listed in THistPainter.
3719 /// - when no option is specified:
3720 /// - the default histogram drawing option is used
3721 /// if the expression is of the form "e1".
3722 /// - if the expression is of the form "e1:e2"or "e1:e2:e3" a cloud of
3723 /// unbinned 2D or 3D points is drawn respectively.
3724 /// - if the expression has four fields "e1:e2:e3:e4" a 2D scatter is
3725 /// produced with e1 vs e2 vs e3, and e4 is mapped on the current color
3726 /// palette.
3727 /// - If option COL is specified when varexp has three fields:
3728 /// ~~~ {.cpp}
3729 /// tree.Draw("e1:e2:e3","","col");
3730 /// ~~~
3731 /// a 2D scatter is produced with e1 vs e2, and e3 is mapped on the current
3732 /// color palette. The colors for e3 are evaluated once in linear scale before
3733 /// painting. Therefore changing the pad to log scale along Z as no effect
3734 /// on the colors.
3735 /// - if expression has more than four fields the option "PARA"or "CANDLE"
3736 /// can be used.
3737 /// - If option contains the string "goff", no graphics is generated.
3738 ///
3739 /// \param [in] nentries is the number of entries to process (default is all)
3740 ///
3741 /// \param [in] firstentry is the first entry to process (default is 0)
3742 ///
3743 /// ### Drawing expressions using arrays and array elements
3744 ///
3745 /// Let assumes, a leaf fMatrix, on the branch fEvent, which is a 3 by 3 array,
3746 /// or a TClonesArray.
3747 /// In a TTree::Draw expression you can now access fMatrix using the following
3748 /// syntaxes:
3749 ///
3750 /// | String passed | What is used for each entry of the tree
3751 /// |-----------------|--------------------------------------------------------|
3752 /// | `fMatrix` | the 9 elements of fMatrix |
3753 /// | `fMatrix[][]` | the 9 elements of fMatrix |
3754 /// | `fMatrix[2][2]` | only the elements fMatrix[2][2] |
3755 /// | `fMatrix[1]` | the 3 elements fMatrix[1][0], fMatrix[1][1] and fMatrix[1][2] |
3756 /// | `fMatrix[1][]` | the 3 elements fMatrix[1][0], fMatrix[1][1] and fMatrix[1][2] |
3757 /// | `fMatrix[][0]` | the 3 elements fMatrix[0][0], fMatrix[1][0] and fMatrix[2][0] |
3758 ///
3759 /// "fEvent.fMatrix...." same as "fMatrix..." (unless there is more than one leaf named fMatrix!).
3760 ///
3761 /// In summary, if a specific index is not specified for a dimension, TTree::Draw
3762 /// will loop through all the indices along this dimension. Leaving off the
3763 /// last (right most) dimension of specifying then with the two characters '[]'
3764 /// is equivalent. For variable size arrays (and TClonesArray) the range
3765 /// of the first dimension is recalculated for each entry of the tree.
3766 /// You can also specify the index as an expression of any other variables from the
3767 /// tree.
3768 ///
3769 /// TTree::Draw also now properly handling operations involving 2 or more arrays.
3770 ///
3771 /// Let assume a second matrix fResults[5][2], here are a sample of some
3772 /// of the possible combinations, the number of elements they produce and
3773 /// the loop used:
3774 ///
3775 /// | expression | element(s) | Loop |
3776 /// |----------------------------------|------------|--------------------------|
3777 /// | `fMatrix[2][1] - fResults[5][2]` | one | no loop |
3778 /// | `fMatrix[2][] - fResults[5][2]` | three | on 2nd dim fMatrix |
3779 /// | `fMatrix[2][] - fResults[5][]` | two | on both 2nd dimensions |
3780 /// | `fMatrix[][2] - fResults[][1]` | three | on both 1st dimensions |
3781 /// | `fMatrix[][2] - fResults[][]` | six | on both 1st and 2nd dimensions of fResults |
3782 /// | `fMatrix[][2] - fResults[3][]` | two | on 1st dim of fMatrix and 2nd of fResults (at the same time) |
3783 /// | `fMatrix[][] - fResults[][]` | six | on 1st dim then on 2nd dim |
3784 /// | `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.|
3785 ///
3786 ///
3787 /// In summary, TTree::Draw loops through all unspecified dimensions. To
3788 /// figure out the range of each loop, we match each unspecified dimension
3789 /// from left to right (ignoring ALL dimensions for which an index has been
3790 /// specified), in the equivalent loop matched dimensions use the same index
3791 /// and are restricted to the smallest range (of only the matched dimensions).
3792 /// When involving variable arrays, the range can of course be different
3793 /// for each entry of the tree.
3794 ///
3795 /// So the loop equivalent to "fMatrix[][2] - fResults[3][]" is:
3796 /// ~~~ {.cpp}
3797 /// for (Int_t i0; i < min(3,2); i++) {
3798 /// use the value of (fMatrix[i0][2] - fMatrix[3][i0])
3799 /// }
3800 /// ~~~
3801 /// So the loop equivalent to "fMatrix[][2] - fResults[][]" is:
3802 /// ~~~ {.cpp}
3803 /// for (Int_t i0; i < min(3,5); i++) {
3804 /// for (Int_t i1; i1 < 2; i1++) {
3805 /// use the value of (fMatrix[i0][2] - fMatrix[i0][i1])
3806 /// }
3807 /// }
3808 /// ~~~
3809 /// So the loop equivalent to "fMatrix[][] - fResults[][]" is:
3810 /// ~~~ {.cpp}
3811 /// for (Int_t i0; i < min(3,5); i++) {
3812 /// for (Int_t i1; i1 < min(3,2); i1++) {
3813 /// use the value of (fMatrix[i0][i1] - fMatrix[i0][i1])
3814 /// }
3815 /// }
3816 /// ~~~
3817 /// So the loop equivalent to "fMatrix[][fResults[][]]" is:
3818 /// ~~~ {.cpp}
3819 /// for (Int_t i0; i0 < 3; i0++) {
3820 /// for (Int_t j2; j2 < 5; j2++) {
3821 /// for (Int_t j3; j3 < 2; j3++) {
3822 /// i1 = fResults[j2][j3];
3823 /// use the value of fMatrix[i0][i1]
3824 /// }
3825 /// }
3826 /// ~~~
3827 /// ### Retrieving the result of Draw
3828 ///
3829 /// By default the temporary histogram created is called "htemp", but only in
3830 /// the one dimensional Draw("e1") it contains the TTree's data points. For
3831 /// a two dimensional Draw, the data is filled into a TGraph which is named
3832 /// "Graph". They can be retrieved by calling
3833 /// ~~~ {.cpp}
3834 /// TH1F *htemp = (TH1F*)gPad->GetPrimitive("htemp"); // 1D
3835 /// TGraph *graph = (TGraph*)gPad->GetPrimitive("Graph"); // 2D
3836 /// ~~~
3837 /// For a three and four dimensional Draw the TPolyMarker3D is unnamed, and
3838 /// cannot be retrieved.
3839 ///
3840 /// gPad always contains a TH1 derived object called "htemp" which allows to
3841 /// access the axes:
3842 /// ~~~ {.cpp}
3843 /// TGraph *graph = (TGraph*)gPad->GetPrimitive("Graph"); // 2D
3844 /// TH2F *htemp = (TH2F*)gPad->GetPrimitive("htemp"); // empty, but has axes
3845 /// TAxis *xaxis = htemp->GetXaxis();
3846 /// ~~~
3847 /// ### Saving the result of Draw to an histogram
3848 ///
3849 /// If varexp0 contains >>hnew (following the variable(s) name(s),
3850 /// the new histogram created is called hnew and it is kept in the current
3851 /// directory (and also the current pad). This works for all dimensions.
3852 ///
3853 /// Example:
3854 /// ~~~ {.cpp}
3855 /// tree.Draw("sqrt(x)>>hsqrt","y>0")
3856 /// ~~~
3857 /// will draw `sqrt(x)` and save the histogram as "hsqrt" in the current
3858 /// directory. To retrieve it do:
3859 /// ~~~ {.cpp}
3860 /// TH1F *hsqrt = (TH1F*)gDirectory->Get("hsqrt");
3861 /// ~~~
3862 /// The binning information is taken from the environment variables
3863 /// ~~~ {.cpp}
3864 /// Hist.Binning.?D.?
3865 /// ~~~
3866 /// In addition, the name of the histogram can be followed by up to 9
3867 /// numbers between '(' and ')', where the numbers describe the
3868 /// following:
3869 ///
3870 /// - 1 - bins in x-direction
3871 /// - 2 - lower limit in x-direction
3872 /// - 3 - upper limit in x-direction
3873 /// - 4-6 same for y-direction
3874 /// - 7-9 same for z-direction
3875 ///
3876 /// When a new binning is used the new value will become the default.
3877 /// Values can be skipped.
3878 ///
3879 /// Example:
3880 /// ~~~ {.cpp}
3881 /// tree.Draw("sqrt(x)>>hsqrt(500,10,20)")
3882 /// // plot sqrt(x) between 10 and 20 using 500 bins
3883 /// tree.Draw("sqrt(x):sin(y)>>hsqrt(100,10,60,50,.1,.5)")
3884 /// // plot sqrt(x) against sin(y)
3885 /// // 100 bins in x-direction; lower limit on x-axis is 10; upper limit is 60
3886 /// // 50 bins in y-direction; lower limit on y-axis is .1; upper limit is .5
3887 /// ~~~
3888 /// By default, the specified histogram is reset.
3889 /// To continue to append data to an existing histogram, use "+" in front
3890 /// of the histogram name.
3891 ///
3892 /// A '+' in front of the histogram name is ignored, when the name is followed by
3893 /// binning information as described in the previous paragraph.
3894 /// ~~~ {.cpp}
3895 /// tree.Draw("sqrt(x)>>+hsqrt","y>0")
3896 /// ~~~
3897 /// will not reset `hsqrt`, but will continue filling. This works for 1-D, 2-D
3898 /// and 3-D histograms.
3899 ///
3900 /// ### Accessing collection objects
3901 ///
3902 /// TTree::Draw default's handling of collections is to assume that any
3903 /// request on a collection pertain to it content. For example, if fTracks
3904 /// is a collection of Track objects, the following:
3905 /// ~~~ {.cpp}
3906 /// tree->Draw("event.fTracks.fPx");
3907 /// ~~~
3908 /// will plot the value of fPx for each Track objects inside the collection.
3909 /// Also
3910 /// ~~~ {.cpp}
3911 /// tree->Draw("event.fTracks.size()");
3912 /// ~~~
3913 /// would plot the result of the member function Track::size() for each
3914 /// Track object inside the collection.
3915 /// To access information about the collection itself, TTree::Draw support
3916 /// the '@' notation. If a variable which points to a collection is prefixed
3917 /// or postfixed with '@', the next part of the expression will pertain to
3918 /// the collection object. For example:
3919 /// ~~~ {.cpp}
3920 /// tree->Draw("event.@fTracks.size()");
3921 /// ~~~
3922 /// will plot the size of the collection referred to by `fTracks` (i.e the number
3923 /// of Track objects).
3924 ///
3925 /// ### Drawing 'objects'
3926 ///
3927 /// When a class has a member function named AsDouble or AsString, requesting
3928 /// to directly draw the object will imply a call to one of the 2 functions.
3929 /// If both AsDouble and AsString are present, AsDouble will be used.
3930 /// AsString can return either a char*, a std::string or a TString.s
3931 /// For example, the following
3932 /// ~~~ {.cpp}
3933 /// tree->Draw("event.myTTimeStamp");
3934 /// ~~~
3935 /// will draw the same histogram as
3936 /// ~~~ {.cpp}
3937 /// tree->Draw("event.myTTimeStamp.AsDouble()");
3938 /// ~~~
3939 /// In addition, when the object is a type TString or std::string, TTree::Draw
3940 /// will call respectively `TString::Data` and `std::string::c_str()`
3941 ///
3942 /// If the object is a TBits, the histogram will contain the index of the bit
3943 /// that are turned on.
3944 ///
3945 /// ### Retrieving information about the tree itself.
3946 ///
3947 /// You can refer to the tree (or chain) containing the data by using the
3948 /// string 'This'.
3949 /// You can then could any TTree methods. For example:
3950 /// ~~~ {.cpp}
3951 /// tree->Draw("This->GetReadEntry()");
3952 /// ~~~
3953 /// will display the local entry numbers be read.
3954 /// ~~~ {.cpp}
3955 /// tree->Draw("This->GetUserInfo()->At(0)->GetName()");
3956 /// ~~~
3957 /// will display the name of the first 'user info' object.
3958 ///
3959 /// ### Special functions and variables
3960 ///
3961 /// `Entry$`: A TTree::Draw formula can use the special variable `Entry$`
3962 /// to access the entry number being read. For example to draw every
3963 /// other entry use:
3964 /// ~~~ {.cpp}
3965 /// tree.Draw("myvar","Entry$%2==0");
3966 /// ~~~
3967 /// - `Entry$` : return the current entry number (`== TTree::GetReadEntry()`)
3968 /// - `LocalEntry$` : return the current entry number in the current tree of a
3969 /// chain (`== GetTree()->GetReadEntry()`)
3970 /// - `Entries$` : return the total number of entries (== TTree::GetEntries())
3971 /// - `LocalEntries$` : return the total number of entries in the current tree
3972 /// of a chain (== GetTree()->TTree::GetEntries())
3973 /// - `Length$` : return the total number of element of this formula for this
3974 /// entry (`==TTreeFormula::GetNdata()`)
3975 /// - `Iteration$` : return the current iteration over this formula for this
3976 /// entry (i.e. varies from 0 to `Length$`).
3977 /// - `Length$(formula )` : return the total number of element of the formula
3978 /// given as a parameter.
3979 /// - `Sum$(formula )` : return the sum of the value of the elements of the
3980 /// formula given as a parameter. For example the mean for all the elements in
3981 /// one entry can be calculated with: `Sum$(formula )/Length$(formula )`
3982 /// - `Min$(formula )` : return the minimun (within one TTree entry) of the value of the
3983 /// elements of the formula given as a parameter.
3984 /// - `Max$(formula )` : return the maximum (within one TTree entry) of the value of the
3985 /// elements of the formula given as a parameter.
3986 /// - `MinIf$(formula,condition)`
3987 /// - `MaxIf$(formula,condition)` : return the minimum (maximum) (within one TTree entry)
3988 /// of the value of the elements of the formula given as a parameter
3989 /// if they match the condition. If no element matches the condition,
3990 /// the result is zero. To avoid the resulting peak at zero, use the
3991 /// pattern:
3992 /// ~~~ {.cpp}
3993 /// tree->Draw("MinIf$(formula,condition)","condition");
3994 /// ~~~
3995 /// which will avoid calculation `MinIf$` for the entries that have no match
3996 /// for the condition.
3997 /// - `Alt$(primary,alternate)` : return the value of "primary" if it is available
3998 /// for the current iteration otherwise return the value of "alternate".
3999 /// For example, with arr1[3] and arr2[2]
4000 /// ~~~ {.cpp}
4001 /// tree->Draw("arr1+Alt$(arr2,0)");
4002 /// ~~~
4003 /// will draw arr1[0]+arr2[0] ; arr1[1]+arr2[1] and arr1[2]+0
4004 /// Or with a variable size array arr3
4005 /// ~~~ {.cpp}
4006 /// tree->Draw("Alt$(arr3[0],0)+Alt$(arr3[1],0)+Alt$(arr3[2],0)");
4007 /// ~~~
4008 /// will draw the sum arr3 for the index 0 to min(2,actual_size_of_arr3-1)
4009 /// As a comparison
4010 /// ~~~ {.cpp}
4011 /// tree->Draw("arr3[0]+arr3[1]+arr3[2]");
4012 /// ~~~
4013 /// will draw the sum arr3 for the index 0 to 2 only if the
4014 /// actual_size_of_arr3 is greater or equal to 3.
4015 /// Note that the array in 'primary' is flattened/linearized thus using
4016 /// `Alt$` with multi-dimensional arrays of different dimensions in unlikely
4017 /// to yield the expected results. To visualize a bit more what elements
4018 /// would be matched by TTree::Draw, TTree::Scan can be used:
4019 /// ~~~ {.cpp}
4020 /// tree->Scan("arr1:Alt$(arr2,0)");
4021 /// ~~~
4022 /// will print on one line the value of arr1 and (arr2,0) that will be
4023 /// matched by
4024 /// ~~~ {.cpp}
4025 /// tree->Draw("arr1-Alt$(arr2,0)");
4026 /// ~~~
4027 /// The ternary operator is not directly supported in TTree::Draw however, to plot the
4028 /// equivalent of `var2<20 ? -99 : var1`, you can use:
4029 /// ~~~ {.cpp}
4030 /// tree->Draw("(var2<20)*99+(var2>=20)*var1","");
4031 /// ~~~
4032 ///
4033 /// ### Drawing a user function accessing the TTree data directly
4034 ///
4035 /// If the formula contains a file name, TTree::MakeProxy will be used
4036 /// to load and execute this file. In particular it will draw the
4037 /// result of a function with the same name as the file. The function
4038 /// will be executed in a context where the name of the branches can
4039 /// be used as a C++ variable.
4040 ///
4041 /// For example draw px using the file hsimple.root (generated by the
4042 /// hsimple.C tutorial), we need a file named hsimple.cxx:
4043 /// ~~~ {.cpp}
4044 /// double hsimple() {
4045 /// return px;
4046 /// }
4047 /// ~~~
4048 /// MakeProxy can then be used indirectly via the TTree::Draw interface
4049 /// as follow:
4050 /// ~~~ {.cpp}
4051 /// new TFile("hsimple.root")
4052 /// ntuple->Draw("hsimple.cxx");
4053 /// ~~~
4054 /// A more complete example is available in the tutorials directory:
4055 /// `h1analysisProxy.cxx`, `h1analysProxy.h` and `h1analysisProxyCut.C`
4056 /// which reimplement the selector found in `h1analysis.C`
4057 ///
4058 /// The main features of this facility are:
4059 ///
4060 /// * on-demand loading of branches
4061 /// * ability to use the 'branchname' as if it was a data member
4062 /// * protection against array out-of-bound
4063 /// * ability to use the branch data as object (when the user code is available)
4064 ///
4065 /// See TTree::MakeProxy for more details.
4066 ///
4067 /// ### Making a Profile histogram
4068 ///
4069 /// In case of a 2-Dim expression, one can generate a TProfile histogram
4070 /// instead of a TH2F histogram by specifying option=prof or option=profs
4071 /// or option=profi or option=profg ; the trailing letter select the way
4072 /// the bin error are computed, See TProfile2D::SetErrorOption for
4073 /// details on the differences.
4074 /// The option=prof is automatically selected in case of y:x>>pf
4075 /// where pf is an existing TProfile histogram.
4076 ///
4077 /// ### Making a 2D Profile histogram
4078 ///
4079 /// In case of a 3-Dim expression, one can generate a TProfile2D histogram
4080 /// instead of a TH3F histogram by specifying option=prof or option=profs.
4081 /// or option=profi or option=profg ; the trailing letter select the way
4082 /// the bin error are computed, See TProfile2D::SetErrorOption for
4083 /// details on the differences.
4084 /// The option=prof is automatically selected in case of z:y:x>>pf
4085 /// where pf is an existing TProfile2D histogram.
4086 ///
4087 /// ### Making a 5D plot using GL
4088 ///
4089 /// If option GL5D is specified together with 5 variables, a 5D plot is drawn
4090 /// using OpenGL. See $ROOTSYS/tutorials/tree/staff.C as example.
4091 ///
4092 /// ### Making a parallel coordinates plot
4093 ///
4094 /// In case of a 2-Dim or more expression with the option=para, one can generate
4095 /// a parallel coordinates plot. With that option, the number of dimensions is
4096 /// arbitrary. Giving more than 4 variables without the option=para or
4097 /// option=candle or option=goff will produce an error.
4098 ///
4099 /// ### Making a candle sticks chart
4100 ///
4101 /// In case of a 2-Dim or more expression with the option=candle, one can generate
4102 /// a candle sticks chart. With that option, the number of dimensions is
4103 /// arbitrary. Giving more than 4 variables without the option=para or
4104 /// option=candle or option=goff will produce an error.
4105 ///
4106 /// ### Normalizing the output histogram to 1
4107 ///
4108 /// When option contains "norm" the output histogram is normalized to 1.
4109 ///
4110 /// ### Saving the result of Draw to a TEventList, a TEntryList or a TEntryListArray
4111 ///
4112 /// TTree::Draw can be used to fill a TEventList object (list of entry numbers)
4113 /// instead of histogramming one variable.
4114 /// If varexp0 has the form >>elist , a TEventList object named "elist"
4115 /// is created in the current directory. elist will contain the list
4116 /// of entry numbers satisfying the current selection.
4117 /// If option "entrylist" is used, a TEntryList object is created
4118 /// If the selection contains arrays, vectors or any container class and option
4119 /// "entrylistarray" is used, a TEntryListArray object is created
4120 /// containing also the subentries satisfying the selection, i.e. the indices of
4121 /// the branches which hold containers classes.
4122 /// Example:
4123 /// ~~~ {.cpp}
4124 /// tree.Draw(">>yplus","y>0")
4125 /// ~~~
4126 /// will create a TEventList object named "yplus" in the current directory.
4127 /// In an interactive session, one can type (after TTree::Draw)
4128 /// ~~~ {.cpp}
4129 /// yplus.Print("all")
4130 /// ~~~
4131 /// to print the list of entry numbers in the list.
4132 /// ~~~ {.cpp}
4133 /// tree.Draw(">>yplus", "y>0", "entrylist")
4134 /// ~~~
4135 /// will create a TEntryList object names "yplus" in the current directory
4136 /// ~~~ {.cpp}
4137 /// tree.Draw(">>yplus", "y>0", "entrylistarray")
4138 /// ~~~
4139 /// will create a TEntryListArray object names "yplus" in the current directory
4140 ///
4141 /// By default, the specified entry list is reset.
4142 /// To continue to append data to an existing list, use "+" in front
4143 /// of the list name;
4144 /// ~~~ {.cpp}
4145 /// tree.Draw(">>+yplus","y>0")
4146 /// ~~~
4147 /// will not reset yplus, but will enter the selected entries at the end
4148 /// of the existing list.
4149 ///
4150 /// ### Using a TEventList, TEntryList or TEntryListArray as Input
4151 ///
4152 /// Once a TEventList or a TEntryList object has been generated, it can be used as input
4153 /// for TTree::Draw. Use TTree::SetEventList or TTree::SetEntryList to set the
4154 /// current event list
4155 ///
4156 /// Example 1:
4157 /// ~~~ {.cpp}
4158 /// TEventList *elist = (TEventList*)gDirectory->Get("yplus");
4159 /// tree->SetEventList(elist);
4160 /// tree->Draw("py");
4161 /// ~~~
4162 /// Example 2:
4163 /// ~~~ {.cpp}
4164 /// TEntryList *elist = (TEntryList*)gDirectory->Get("yplus");
4165 /// tree->SetEntryList(elist);
4166 /// tree->Draw("py");
4167 /// ~~~
4168 /// If a TEventList object is used as input, a new TEntryList object is created
4169 /// inside the SetEventList function. In case of a TChain, all tree headers are loaded
4170 /// for this transformation. This new object is owned by the chain and is deleted
4171 /// with it, unless the user extracts it by calling GetEntryList() function.
4172 /// See also comments to SetEventList() function of TTree and TChain.
4173 ///
4174 /// If arrays are used in the selection criteria and TEntryListArray is not used,
4175 /// all the entries that have at least one element of the array that satisfy the selection
4176 /// are entered in the list.
4177 ///
4178 /// Example:
4179 /// ~~~ {.cpp}
4180 /// tree.Draw(">>pyplus","fTracks.fPy>0");
4181 /// tree->SetEventList(pyplus);
4182 /// tree->Draw("fTracks.fPy");
4183 /// ~~~
4184 /// will draw the fPy of ALL tracks in event with at least one track with
4185 /// a positive fPy.
4186 ///
4187 /// To select only the elements that did match the original selection
4188 /// use TEventList::SetReapplyCut or TEntryList::SetReapplyCut.
4189 ///
4190 /// Example:
4191 /// ~~~ {.cpp}
4192 /// tree.Draw(">>pyplus","fTracks.fPy>0");
4193 /// pyplus->SetReapplyCut(kTRUE);
4194 /// tree->SetEventList(pyplus);
4195 /// tree->Draw("fTracks.fPy");
4196 /// ~~~
4197 /// will draw the fPy of only the tracks that have a positive fPy.
4198 ///
4199 /// To draw only the elements that match a selection in case of arrays,
4200 /// you can also use TEntryListArray (faster in case of a more general selection).
4201 ///
4202 /// Example:
4203 /// ~~~ {.cpp}
4204 /// tree.Draw(">>pyplus","fTracks.fPy>0", "entrylistarray");
4205 /// tree->SetEntryList(pyplus);
4206 /// tree->Draw("fTracks.fPy");
4207 /// ~~~
4208 /// will draw the fPy of only the tracks that have a positive fPy,
4209 /// but without redoing the selection.
4210 ///
4211 /// Note: Use tree->SetEventList(0) if you do not want use the list as input.
4212 ///
4213 /// ### How to obtain more info from TTree::Draw
4214 ///
4215 /// Once TTree::Draw has been called, it is possible to access useful
4216 /// information still stored in the TTree object via the following functions:
4217 ///
4218 /// - GetSelectedRows() // return the number of values accepted by the selection expression. In case where no selection was specified, returns the number of values processed.
4219 /// - GetV1() // returns a pointer to the double array of V1
4220 /// - GetV2() // returns a pointer to the double array of V2
4221 /// - GetV3() // returns a pointer to the double array of V3
4222 /// - GetV4() // returns a pointer to the double array of V4
4223 /// - GetW() // returns a pointer to the double array of Weights where weight equal the result of the selection expression.
4224 ///
4225 /// where V1,V2,V3 correspond to the expressions in
4226 /// ~~~ {.cpp}
4227 /// TTree::Draw("V1:V2:V3:V4",selection);
4228 /// ~~~
4229 /// If the expression has more than 4 component use GetVal(index)
4230 ///
4231 /// Example:
4232 /// ~~~ {.cpp}
4233 /// Root > ntuple->Draw("py:px","pz>4");
4234 /// Root > TGraph *gr = new TGraph(ntuple->GetSelectedRows(),
4235 /// ntuple->GetV2(), ntuple->GetV1());
4236 /// Root > gr->Draw("ap"); //draw graph in current pad
4237 /// ~~~
4238 ///
4239 /// A more complete complete tutorial (treegetval.C) shows how to use the
4240 /// GetVal() method.
4241 ///
4242 /// creates a TGraph object with a number of points corresponding to the
4243 /// number of entries selected by the expression "pz>4", the x points of the graph
4244 /// being the px values of the Tree and the y points the py values.
4245 ///
4246 /// Important note: By default TTree::Draw creates the arrays obtained
4247 /// with GetW, GetV1, GetV2, GetV3, GetV4, GetVal with a length corresponding
4248 /// to the parameter fEstimate. The content will be the last `GetSelectedRows() % GetEstimate()`
4249 /// values calculated.
4250 /// By default fEstimate=1000000 and can be modified
4251 /// via TTree::SetEstimate. To keep in memory all the results (in case
4252 /// where there is only one result per entry), use
4253 /// ~~~ {.cpp}
4254 /// tree->SetEstimate(tree->GetEntries()+1); // same as tree->SetEstimate(-1);
4255 /// ~~~
4256 /// You must call SetEstimate if the expected number of selected rows
4257 /// you need to look at is greater than 1000000.
4258 ///
4259 /// You can use the option "goff" to turn off the graphics output
4260 /// of TTree::Draw in the above example.
4261 ///
4262 /// ### Automatic interface to TTree::Draw via the TTreeViewer
4263 ///
4264 /// A complete graphical interface to this function is implemented
4265 /// in the class TTreeViewer.
4266 /// To start the TTreeViewer, three possibilities:
4267 /// - select TTree context menu item "StartViewer"
4268 /// - type the command "TTreeViewer TV(treeName)"
4269 /// - execute statement "tree->StartViewer();"
4270 
4271 Long64_t TTree::Draw(const char* varexp, const char* selection, Option_t* option, Long64_t nentries, Long64_t firstentry)
4273  GetPlayer();
4274  if (fPlayer)
4275  return fPlayer->DrawSelect(varexp,selection,option,nentries,firstentry);
4276  return -1;
4277 }
4278 
4279 ////////////////////////////////////////////////////////////////////////////////
4280 /// Remove some baskets from memory.
4281 
4282 void TTree::DropBaskets()
4284  TBranch* branch = 0;
4286  for (Int_t i = 0; i < nb; ++i) {
4287  branch = (TBranch*) fBranches.UncheckedAt(i);
4288  branch->DropBaskets("all");
4289  }
4290 }
4291 
4292 ////////////////////////////////////////////////////////////////////////////////
4293 /// Drop branch buffers to accommodate nbytes below MaxVirtualsize.
4294 
4297  // Be careful not to remove current read/write buffers.
4298  Int_t ndrop = 0;
4299  Int_t nleaves = fLeaves.GetEntriesFast();
4300  for (Int_t i = 0; i < nleaves; ++i) {
4301  TLeaf* leaf = (TLeaf*) fLeaves.UncheckedAt(i);
4302  TBranch* branch = (TBranch*) leaf->GetBranch();
4303  Int_t nbaskets = branch->GetListOfBaskets()->GetEntries();
4304  for (Int_t j = 0; j < nbaskets - 1; ++j) {
4305  if ((j == branch->GetReadBasket()) || (j == branch->GetWriteBasket())) {
4306  continue;
4307  }
4308  TBasket* basket = (TBasket*)branch->GetListOfBaskets()->UncheckedAt(j);
4309  if (basket) {
4310  ndrop += basket->DropBuffers();
4312  return;
4313  }
4314  }
4315  }
4316  }
4317 }
4318 
4319 ////////////////////////////////////////////////////////////////////////////////
4320 /// Fill all branches.
4321 ///
4322 /// This function loops on all the branches of this tree. For
4323 /// each branch, it copies to the branch buffer (basket) the current
4324 /// values of the leaves data types. If a leaf is a simple data type,
4325 /// a simple conversion to a machine independent format has to be done.
4326 ///
4327 /// This machine independent version of the data is copied into a
4328 /// basket (each branch has its own basket). When a basket is full
4329 /// (32k worth of data by default), it is then optionally compressed
4330 /// and written to disk (this operation is also called committing or
4331 /// 'flushing' the basket). The committed baskets are then
4332 /// immediately removed from memory.
4333 ///
4334 /// The function returns the number of bytes committed to the
4335 /// individual branches.
4336 ///
4337 /// If a write error occurs, the number of bytes returned is -1.
4338 ///
4339 /// If no data are written, because, e.g., the branch is disabled,
4340 /// the number of bytes returned is 0.
4341 ///
4342 /// __The baskets are flushed and the Tree header saved at regular intervals__
4343 ///
4344 /// At regular intervals, when the amount of data written so far is
4345 /// greater than fAutoFlush (see SetAutoFlush) all the baskets are flushed to disk.
4346 /// This makes future reading faster as it guarantees that baskets belonging to nearby
4347 /// entries will be on the same disk region.
4348 /// When the first call to flush the baskets happen, we also take this opportunity
4349 /// to optimize the baskets buffers.
4350 /// We also check if the amount of data written is greater than fAutoSave (see SetAutoSave).
4351 /// In this case we also write the Tree header. This makes the Tree recoverable up to this point
4352 /// in case the program writing the Tree crashes.
4353 /// The decisions to FlushBaskets and Auto Save can be made based either on the number
4354 /// of bytes written (fAutoFlush and fAutoSave negative) or on the number of entries
4355 /// written (fAutoFlush and fAutoSave positive).
4356 /// Note that the user can decide to call FlushBaskets and AutoSave in her event loop
4357 /// base on the number of events written instead of the number of bytes written.
4358 ///
4359 /// Note that calling FlushBaskets too often increases the IO time.
4360 ///
4361 /// Note that calling AutoSave too often increases the IO time and also the file size.
4362 
4365  Int_t nbytes = 0;
4366  Int_t nwrite = 0;
4367  Int_t nerror = 0;
4368  Int_t nbranches = fBranches.GetEntriesFast();
4369 
4370  // Case of one single super branch. Automatically update
4371  // all the branch addresses if a new object was created.
4372  if (nbranches == 1)
4373  ((TBranch *)fBranches.UncheckedAt(0))->UpdateAddress();
4374 
4375  if (fBranchRef)
4376  fBranchRef->Clear();
4377 
4378 #ifdef R__USE_IMT
4380  if (fIMTEnabled) {
4381  fIMTFlush = true;
4382  fIMTZipBytes.store(0);
4383  fIMTTotBytes.store(0);
4384  }
4385 #endif
4386 
4387  for (Int_t i = 0; i < nbranches; ++i) {
4388  // Loop over all branches, filling and accumulating bytes written and error counts.
4389  TBranch *branch = (TBranch *)fBranches.UncheckedAt(i);
4390 
4391  if (branch->TestBit(kDoNotProcess))
4392  continue;
4393 
4394 #ifndef R__USE_IMT
4395  nwrite = branch->FillImpl(nullptr);
4396 #else
4397  nwrite = branch->FillImpl(fIMTEnabled ? &imtHelper : nullptr);
4398 #endif
4399  if (nwrite < 0) {
4400  if (nerror < 2) {
4401  Error("Fill", "Failed filling branch:%s.%s, nbytes=%d, entry=%lld\n"
4402  " This error is symptomatic of a Tree created as a memory-resident Tree\n"
4403  " Instead of doing:\n"
4404  " TTree *T = new TTree(...)\n"
4405  " TFile *f = new TFile(...)\n"
4406  " you should do:\n"
4407  " TFile *f = new TFile(...)\n"
4408  " TTree *T = new TTree(...)\n\n",
4409  GetName(), branch->GetName(), nwrite, fEntries + 1);
4410  } else {
4411  Error("Fill", "Failed filling branch:%s.%s, nbytes=%d, entry=%lld", GetName(), branch->GetName(), nwrite,
4412  fEntries + 1);
4413  }
4414  ++nerror;
4415  } else {
4416  nbytes += nwrite;
4417  }
4418  }
4419 
4420 #ifdef R__USE_IMT
4421  if (fIMTFlush) {
4422  imtHelper.Wait();
4423  fIMTFlush = false;
4424  const_cast<TTree *>(this)->AddTotBytes(fIMTTotBytes);
4425  const_cast<TTree *>(this)->AddZipBytes(fIMTZipBytes);
4426  nbytes += imtHelper.GetNbytes();
4427  nerror += imtHelper.GetNerrors();
4428  }
4429 #endif
4430 
4431  if (fBranchRef)
4432  fBranchRef->Fill();
4433 
4434  ++fEntries;
4435 
4436  if (fEntries > fMaxEntries)
4437  KeepCircular();
4438 
4439  if (gDebug > 0)
4440  Info("TTree::Fill", " - A: %d %lld %lld %lld %lld %lld %lld \n", nbytes, fEntries, fAutoFlush, fAutoSave,
4442 
4443  bool autoFlush = false;
4444  bool autoSave = false;
4445 
4446  if (fAutoFlush != 0 || fAutoSave != 0) {
4447  // Is it time to flush or autosave baskets?
4448  if (fFlushedBytes == 0) {
4449  // If fFlushedBytes == 0, it means we never flushed or saved, so
4450  // we need to check if it's time to do it and recompute the values
4451  // of fAutoFlush and fAutoSave in terms of the number of entries.
4452  // Decision can be based initially either on the number of bytes
4453  // or the number of entries written.
4454  Long64_t zipBytes = GetZipBytes();
4455 
4456  if (fAutoFlush)
4457  autoFlush = fAutoFlush < 0 ? (zipBytes > -fAutoFlush) : fEntries % fAutoFlush == 0;
4458 
4459  if (fAutoSave)
4460  autoSave = fAutoSave < 0 ? (zipBytes > -fAutoSave) : fEntries % fAutoSave == 0;
4461 
4462  if (autoFlush || autoSave) {
4463  // First call FlushBasket to make sure that fTotBytes is up to date.
4464  FlushBaskets();
4465  OptimizeBaskets(GetTotBytes(), 1, "");
4466  autoFlush = false; // avoid auto flushing again later
4467 
4468  if (gDebug > 0)
4469  Info("TTree::Fill", "OptimizeBaskets called at entry %lld, fZipBytes=%lld, fFlushedBytes=%lld\n",
4471 
4473  fAutoFlush = fEntries; // Use test on entries rather than bytes
4474 
4475  // subsequently in run
4476  if (fAutoSave < 0) {
4477  // Set fAutoSave to the largest integer multiple of
4478  // fAutoFlush events such that fAutoSave*fFlushedBytes
4479  // < (minus the input value of fAutoSave)
4480  Long64_t totBytes = GetTotBytes();
4481  if (zipBytes != 0) {
4482  fAutoSave = TMath::Max(fAutoFlush, fEntries * ((-fAutoSave / zipBytes) / fEntries));
4483  } else if (totBytes != 0) {
4484  fAutoSave = TMath::Max(fAutoFlush, fEntries * ((-fAutoSave / totBytes) / fEntries));
4485  } else {
4486  TBufferFile b(TBuffer::kWrite, 10000);
4487  TTree::Class()->WriteBuffer(b, (TTree *)this);
4488  Long64_t total = b.Length();
4490  }
4491  } else if (fAutoSave > 0) {
4493  }
4494 
4495  if (fAutoSave != 0 && fEntries >= fAutoSave)
4496  autoSave = true;
4497 
4498  if (gDebug > 0)
4499  Info("TTree::Fill", "First AutoFlush. fAutoFlush = %lld, fAutoSave = %lld\n", fAutoFlush, fAutoSave);
4500  }
4501  } else {
4502  // Check if we need to auto flush
4503  if (fAutoFlush) {
4504  if (fNClusterRange == 0)
4505  autoFlush = fEntries > 1 && fEntries % fAutoFlush == 0;
4506  else
4507  autoFlush = (fEntries - (fClusterRangeEnd[fNClusterRange - 1] + 1)) % fAutoFlush == 0;
4508  }
4509  // Check if we need to auto save
4510  if (fAutoSave)
4511  autoSave = fEntries % fAutoSave == 0;
4512  }
4513  }
4514 
4515  if (autoFlush) {
4516  FlushBaskets();
4517  if (gDebug > 0)
4518  Info("TTree::Fill", "FlushBaskets() called at entry %lld, fZipBytes=%lld, fFlushedBytes=%lld\n", fEntries,
4521  }
4522 
4523  if (autoSave) {
4524  AutoSave(); // does not call FlushBaskets() again
4525  if (gDebug > 0)
4526  Info("TTree::Fill", "AutoSave called at entry %lld, fZipBytes=%lld, fSavedBytes=%lld\n", fEntries,
4528  }
4529 
4530  // Check that output file is still below the maximum size.
4531  // If above, close the current file and continue on a new file.
4532  // Currently, the automatic change of file is restricted
4533  // to the case where the tree is in the top level directory.
4534  if (fDirectory)
4535  if (TFile *file = fDirectory->GetFile())
4536  if ((TDirectory *)file == fDirectory && (file->GetEND() > fgMaxTreeSize))
4537  ChangeFile(file);
4538 
4539  return nerror == 0 ? nbytes : -1;
4540 }
4541 
4542 ////////////////////////////////////////////////////////////////////////////////
4543 /// Search in the array for a branch matching the branch name,
4544 /// with the branch possibly expressed as a 'full' path name (with dots).
4545 
4546 static TBranch *R__FindBranchHelper(TObjArray *list, const char *branchname) {
4547  if (list==0 || branchname == 0 || branchname[0] == '\0') return 0;
4548 
4549  Int_t nbranches = list->GetEntries();
4550 
4551  UInt_t brlen = strlen(branchname);
4552 
4553  for(Int_t index = 0; index < nbranches; ++index) {
4554  TBranch *where = (TBranch*)list->UncheckedAt(index);
4555 
4556  const char *name = where->GetName();
4557  UInt_t len = strlen(name);
4558  if (len && name[len-1]==']') {
4559  const char *dim = strchr(name,'[');
4560  if (dim) {
4561  len = dim - name;
4562  }
4563  }
4564  if (brlen == len && strncmp(branchname,name,len)==0) {
4565  return where;
4566  }
4567  TBranch *next = 0;
4568  if ((brlen >= len) && (branchname[len] == '.')
4569  && strncmp(name, branchname, len) == 0) {
4570  // The prefix subbranch name match the branch name.
4571 
4572  next = where->FindBranch(branchname);
4573  if (!next) {
4574  next = where->FindBranch(branchname+len+1);
4575  }
4576  if (next) return next;
4577  }
4578  const char *dot = strchr((char*)branchname,'.');
4579  if (dot) {
4580  if (len==(size_t)(dot-branchname) &&
4581  strncmp(branchname,name,dot-branchname)==0 ) {
4582  return R__FindBranchHelper(where->GetListOfBranches(),dot+1);
4583  }
4584  }
4585  }
4586  return 0;
4587 }
4588 
4589 ////////////////////////////////////////////////////////////////////////////////
4590 /// Return the branch that correspond to the path 'branchname', which can
4591 /// include the name of the tree or the omitted name of the parent branches.
4592 /// In case of ambiguity, returns the first match.
4593 
4594 TBranch* TTree::FindBranch(const char* branchname)
4596  // We already have been visited while recursively looking
4597  // through the friends tree, let return
4599  return 0;
4600  }
4601 
4602  TBranch* branch = 0;
4603  // If the first part of the name match the TTree name, look for the right part in the
4604  // list of branches.
4605  // This will allow the branchname to be preceded by
4606  // the name of this tree.
4607  if (strncmp(fName.Data(),branchname,fName.Length())==0 && branchname[fName.Length()]=='.') {
4608  branch = R__FindBranchHelper( GetListOfBranches(), branchname + fName.Length() + 1);
4609  if (branch) return branch;
4610  }
4611  // If we did not find it, let's try to find the full name in the list of branches.
4612  branch = R__FindBranchHelper(GetListOfBranches(), branchname);
4613  if (branch) return branch;
4614 
4615  // If we still did not find, let's try to find it within each branch assuming it does not the branch name.
4616  TIter next(GetListOfBranches());
4617  while ((branch = (TBranch*) next())) {
4618  TBranch* nestedbranch = branch->FindBranch(branchname);
4619  if (nestedbranch) {
4620  return nestedbranch;
4621  }
4622  }
4623 
4624  // Search in list of friends.
4625  if (!fFriends) {
4626  return 0;
4627  }
4628  TFriendLock lock(this, kFindBranch);
4629  TIter nextf(fFriends);
4630  TFriendElement* fe = 0;
4631  while ((fe = (TFriendElement*) nextf())) {
4632  TTree* t = fe->GetTree();
4633  if (!t) {
4634  continue;
4635  }
4636  // If the alias is present replace it with the real name.
4637  const char *subbranch = strstr(branchname, fe->GetName());
4638  if (subbranch != branchname) {
4639  subbranch = 0;
4640  }
4641  if (subbranch) {
4642  subbranch += strlen(fe->GetName());
4643  if (*subbranch != '.') {
4644  subbranch = 0;
4645  } else {
4646  ++subbranch;
4647  }
4648  }
4649  std::ostringstream name;
4650  if (subbranch) {
4651  name << t->GetName() << "." << subbranch;
4652  } else {
4653  name << branchname;
4654  }
4655  branch = t->FindBranch(name.str().c_str());
4656  if (branch) {
4657  return branch;
4658  }
4659  }
4660  return 0;
4661 }
4662 
4663 ////////////////////////////////////////////////////////////////////////////////
4664 /// Find leaf..
4665 
4666 TLeaf* TTree::FindLeaf(const char* searchname)
4668  // We already have been visited while recursively looking
4669  // through the friends tree, let's return.
4670  if (kFindLeaf & fFriendLockStatus) {
4671  return 0;
4672  }
4673 
4674  // This will allow the branchname to be preceded by
4675  // the name of this tree.
4676  char* subsearchname = (char*) strstr(searchname, GetName());
4677  if (subsearchname != searchname) {
4678  subsearchname = 0;
4679  }
4680  if (subsearchname) {
4681  subsearchname += strlen(GetName());
4682  if (*subsearchname != '.') {
4683  subsearchname = 0;
4684  } else {
4685  ++subsearchname;
4686  if (subsearchname[0]==0) {
4687  subsearchname = 0;
4688  }
4689  }
4690  }
4691 
4692  TString leafname;
4693  TString leaftitle;
4694  TString longname;
4695  TString longtitle;
4696 
4697  // For leaves we allow for one level up to be prefixed to the name.
4698  TIter next(GetListOfLeaves());
4699  TLeaf* leaf = 0;
4700  while ((leaf = (TLeaf*) next())) {
4701  leafname = leaf->GetName();
4702  Ssiz_t dim = leafname.First('[');
4703  if (dim >= 0) leafname.Remove(dim);
4704 
4705  if (leafname == searchname) {
4706  return leaf;
4707  }
4708  if (subsearchname && leafname == subsearchname) {
4709  return leaf;
4710  }
4711  // The TLeafElement contains the branch name
4712  // in its name, let's use the title.
4713  leaftitle = leaf->GetTitle();
4714  dim = leaftitle.First('[');
4715  if (dim >= 0) leaftitle.Remove(dim);
4716 
4717  if (leaftitle == searchname) {
4718  return leaf;
4719  }
4720  if (subsearchname && leaftitle == subsearchname) {
4721  return leaf;
4722  }
4723  TBranch* branch = leaf->GetBranch();
4724  if (branch) {
4725  longname.Form("%s.%s",branch->GetName(),leafname.Data());
4726  dim = longname.First('[');
4727  if (dim>=0) longname.Remove(dim);
4728  if (longname == searchname) {
4729  return leaf;
4730  }
4731  if (subsearchname && longname == subsearchname) {
4732  return leaf;
4733  }
4734  longtitle.Form("%s.%s",branch->GetName(),leaftitle.Data());
4735  dim = longtitle.First('[');
4736  if (dim>=0) longtitle.Remove(dim);
4737  if (longtitle == searchname) {
4738  return leaf;
4739  }
4740  if (subsearchname && longtitle == subsearchname) {
4741  return leaf;
4742  }
4743  // The following is for the case where the branch is only
4744  // a sub-branch. Since we do not see it through
4745  // TTree::GetListOfBranches, we need to see it indirectly.
4746  // This is the less sturdy part of this search ... it may
4747  // need refining ...
4748  if (strstr(searchname, ".") && !strcmp(searchname, branch->GetName())) {
4749  return leaf;
4750  }
4751  if (subsearchname && strstr(subsearchname, ".") && !strcmp(subsearchname, branch->GetName())) {
4752  return leaf;
4753  }
4754  }
4755  }
4756  // Search in list of friends.
4757  if (!fFriends) {
4758  return 0;
4759  }
4760  TFriendLock lock(this, kFindLeaf);
4761  TIter nextf(fFriends);
4762  TFriendElement* fe = 0;
4763  while ((fe = (TFriendElement*) nextf())) {
4764  TTree* t = fe->GetTree();
4765  if (!t) {
4766  continue;
4767  }
4768  // If the alias is present replace it with the real name.
4769  subsearchname = (char*) strstr(searchname, fe->GetName());
4770  if (subsearchname != searchname) {
4771  subsearchname = 0;
4772  }
4773  if (subsearchname) {
4774  subsearchname += strlen(fe->GetName());
4775  if (*subsearchname != '.') {
4776  subsearchname = 0;
4777  } else {
4778  ++subsearchname;
4779  }
4780  }
4781  if (subsearchname) {
4782  leafname.Form("%s.%s",t->GetName(),subsearchname);
4783  } else {
4784  leafname = searchname;
4785  }
4786  leaf = t->FindLeaf(leafname);
4787  if (leaf) {
4788  return leaf;
4789  }
4790  }
4791  return 0;
4792 }
4793 
4794 ////////////////////////////////////////////////////////////////////////////////
4795 /// Fit a projected item(s) from a tree.
4796 ///
4797 /// funcname is a TF1 function.
4798 ///
4799 /// See TTree::Draw() for explanations of the other parameters.
4800 ///
4801 /// By default the temporary histogram created is called htemp.
4802 /// If varexp contains >>hnew , the new histogram created is called hnew
4803 /// and it is kept in the current directory.
4804 ///
4805 /// The function returns the number of selected entries.
4806 ///
4807 /// Example:
4808 /// ~~~ {.cpp}
4809 /// tree.Fit(pol4,"sqrt(x)>>hsqrt","y>0")
4810 /// ~~~
4811 /// will fit sqrt(x) and save the histogram as "hsqrt" in the current
4812 /// directory.
4813 ///
4814 /// See also TTree::UnbinnedFit
4815 ///
4816 /// ## Return status
4817 ///
4818 /// The function returns the status of the histogram fit (see TH1::Fit)
4819 /// If no entries were selected, the function returns -1;
4820 /// (i.e. fitResult is null if the fit is OK)
4821 
4822 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)
4824  GetPlayer();
4825  if (fPlayer) {
4826  return fPlayer->Fit(funcname, varexp, selection, option, goption, nentries, firstentry);
4827  }
4828  return -1;
4829 }
4830 
4831 namespace {
4832 struct BoolRAIIToggle {
4833  Bool_t &m_val;
4834 
4835  BoolRAIIToggle(Bool_t &val) : m_val(val) { m_val = true; }
4836  ~BoolRAIIToggle() { m_val = false; }
4837 };
4838 }
4839 
4840 ////////////////////////////////////////////////////////////////////////////////
4841 /// Write to disk all the basket that have not yet been individually written.
4842 ///
4843 /// If ROOT has IMT-mode enabled, this will launch multiple TBB tasks in parallel
4844 /// via TThreadExecutor to do this operation; one per basket compression. If the
4845 /// caller utilizes TBB also, care must be taken to prevent deadlocks.
4846 ///
4847 /// For example, let's say the caller holds mutex A and calls FlushBaskets; while
4848 /// TBB is waiting for the ROOT compression tasks to complete, it may decide to
4849 /// run another one of the user's tasks in this thread. If the second user task
4850 /// tries to acquire A, then a deadlock will occur. The example call sequence
4851 /// looks like this:
4852 ///
4853 /// - User acquires mutex A
4854 /// - User calls FlushBaskets.
4855 /// - ROOT launches N tasks and calls wait.
4856 /// - TBB schedules another user task, T2.
4857 /// - T2 tries to acquire mutex A.
4858 ///
4859 /// At this point, the thread will deadlock: the code may function with IMT-mode
4860 /// disabled if the user assumed the legacy code never would run their own TBB
4861 /// tasks.
4862 ///
4863 /// SO: users of TBB who want to enable IMT-mode should carefully review their
4864 /// locking patterns and make sure they hold no coarse-grained application
4865 /// locks when they invoke ROOT.
4866 ///
4867 /// Return the number of bytes written or -1 in case of write error.
4868 
4869 Int_t TTree::FlushBaskets() const
4871  if (!fDirectory) return 0;
4872  Int_t nbytes = 0;
4873  Int_t nerror = 0;
4874  TObjArray *lb = const_cast<TTree*>(this)->GetListOfBranches();
4875  Int_t nb = lb->GetEntriesFast();
4876 
4877 #ifdef R__USE_IMT
4878  if (fIMTEnabled) {
4879  if (fSortedBranches.empty()) { const_cast<TTree*>(this)->InitializeBranchLists(false); }
4880 
4881  BoolRAIIToggle sentry(fIMTFlush);
4882  fIMTZipBytes.store(0);
4883  fIMTTotBytes.store(0);
4884  std::atomic<Int_t> nerrpar(0);
4885  std::atomic<Int_t> nbpar(0);
4886  std::atomic<Int_t> pos(0);
4887 
4888  auto mapFunction = [&]() {
4889  // The branch to process is obtained when the task starts to run.
4890  // This way, since branches are sorted, we make sure that branches
4891  // leading to big tasks are processed first. If we assigned the
4892  // branch at task creation time, the scheduler would not necessarily
4893  // respect our sorting.
4894  Int_t j = pos.fetch_add(1);
4895 
4896  auto branch = fSortedBranches[j].second;
4897  if (R__unlikely(!branch)) { return; }
4898 
4899  if (R__unlikely(gDebug > 0)) {
4900  std::stringstream ss;
4901  ss << std::this_thread::get_id();
4902  Info("FlushBaskets", "[IMT] Thread %s", ss.str().c_str());
4903  Info("FlushBaskets", "[IMT] Running task for branch #%d: %s", j, branch->GetName());
4904  }
4905 
4906  Int_t nbtask = branch->FlushBaskets();
4907 
4908  if (nbtask < 0) { nerrpar++; }
4909  else { nbpar += nbtask; }
4910  };
4911 
4912  ROOT::TThreadExecutor pool;
4913  pool.Foreach(mapFunction, nb);
4914 
4915  fIMTFlush = false;
4916  const_cast<TTree*>(this)->AddTotBytes(fIMTTotBytes);
4917  const_cast<TTree*>(this)->AddZipBytes(fIMTZipBytes);
4918 
4919  return nerrpar ? -1 : nbpar.load();
4920  }
4921 #endif
4922  for (Int_t j = 0; j < nb; j++) {
4923  TBranch* branch = (TBranch*) lb->UncheckedAt(j);
4924  if (branch) {
4925  Int_t nwrite = branch->FlushBaskets();
4926  if (nwrite<0) {
4927  ++nerror;
4928  } else {
4929  nbytes += nwrite;
4930  }
4931  }
4932  }
4933  if (nerror) {
4934  return -1;
4935  } else {
4936  return nbytes;
4937  }
4938 }
4939 
4940 ////////////////////////////////////////////////////////////////////////////////
4941 /// Returns the expanded value of the alias. Search in the friends if any.
4942 
4943 const char* TTree::GetAlias(const char* aliasName) const
4945  // We already have been visited while recursively looking
4946  // through the friends tree, let's return.
4947  if (kGetAlias & fFriendLockStatus) {
4948  return 0;
4949  }
4950  if (fAliases) {
4951  TObject* alias = fAliases->FindObject(aliasName);
4952  if (alias) {
4953  return alias->GetTitle();
4954  }
4955  }
4956  if (!fFriends) {
4957  return 0;
4958  }
4959  TFriendLock lock(const_cast<TTree*>(this), kGetAlias);
4960  TIter nextf(fFriends);
4961  TFriendElement* fe = 0;
4962  while ((fe = (TFriendElement*) nextf())) {
4963  TTree* t = fe->GetTree();
4964  if (t) {
4965  const char* alias = t->GetAlias(aliasName);
4966  if (alias) {
4967  return alias;
4968  }
4969  const char* subAliasName = strstr(aliasName, fe->GetName());
4970  if (subAliasName && (subAliasName[strlen(fe->GetName())] == '.')) {
4971  alias = t->GetAlias(aliasName + strlen(fe->GetName()) + 1);
4972  if (alias) {
4973  return alias;
4974  }
4975  }
4976  }
4977  }
4978  return 0;
4979 }
4980 
4981 ////////////////////////////////////////////////////////////////////////////////
4982 /// Return pointer to the branch with the given name in this tree or its friends.
4983 
4984 TBranch* TTree::GetBranch(const char* name)
4986  if (name == 0) return 0;
4987 
4988  // We already have been visited while recursively
4989  // looking through the friends tree, let's return.
4990  if (kGetBranch & fFriendLockStatus) {
4991  return 0;
4992  }
4993 
4994  // Search using branches.
4996  for (Int_t i = 0; i < nb; i++) {
4997  TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
4998  if (!branch) {
4999  continue;
5000  }
5001  if (!strcmp(branch->GetName(), name)) {
5002  return branch;
5003  }
5004  TObjArray* lb = branch->GetListOfBranches();
5005  Int_t nb1 = lb->GetEntriesFast();
5006  for (Int_t j = 0; j < nb1; j++) {
5007  TBranch* b1 = (TBranch*) lb->UncheckedAt(j);
5008  if (!strcmp(b1->GetName(), name)) {
5009  return b1;
5010  }
5011  TObjArray* lb1 = b1->GetListOfBranches();
5012  Int_t nb2 = lb1->GetEntriesFast();
5013  for (Int_t k = 0; k < nb2; k++) {
5014  TBranch* b2 = (TBranch*) lb1->UncheckedAt(k);
5015  if (!strcmp(b2->GetName(), name)) {
5016  return b2;
5017  }
5018  }
5019  }
5020  }
5021 
5022  // Search using leaves.
5023  TObjArray* leaves = GetListOfLeaves();
5024  Int_t nleaves = leaves->GetEntriesFast();
5025  for (Int_t i = 0; i < nleaves; i++) {
5026  TLeaf* leaf = (TLeaf*) leaves->UncheckedAt(i);
5027  TBranch* branch = leaf->GetBranch();
5028  if (!strcmp(branch->GetName(), name)) {
5029  return branch;
5030  }
5031  }
5032 
5033  if (!fFriends) {
5034  return 0;
5035  }
5036 
5037  // Search in list of friends.
5038  TFriendLock lock(this, kGetBranch);
5039  TIter next(fFriends);
5040  TFriendElement* fe = 0;
5041  while ((fe = (TFriendElement*) next())) {
5042  TTree* t = fe->GetTree();
5043  if (t) {
5044  TBranch* branch = t->GetBranch(name);
5045  if (branch) {
5046  return branch;
5047  }
5048  }
5049  }
5050 
5051  // Second pass in the list of friends when
5052  // the branch name is prefixed by the tree name.
5053  next.Reset();
5054  while ((fe = (TFriendElement*) next())) {
5055  TTree* t = fe->GetTree();
5056  if (!t) {
5057  continue;
5058  }
5059  char* subname = (char*) strstr(name, fe->GetName());
5060  if (subname != name) {
5061  continue;
5062  }
5063  Int_t l = strlen(fe->GetName());
5064  subname += l;
5065  if (*subname != '.') {
5066  continue;
5067  }
5068  subname++;
5069  TBranch* branch = t->GetBranch(subname);
5070  if (branch) {
5071  return branch;
5072  }
5073  }
5074  return 0;
5075 }
5076 
5077 ////////////////////////////////////////////////////////////////////////////////
5078 /// Return status of branch with name branchname.
5079 ///
5080 /// - 0 if branch is not activated
5081 /// - 1 if branch is activated
5082 
5083 Bool_t TTree::GetBranchStatus(const char* branchname) const
5085  TBranch* br = const_cast<TTree*>(this)->GetBranch(branchname);
5086  if (br) {
5087  return br->TestBit(kDoNotProcess) == 0;
5088  }
5089  return 0;
5090 }
5091 
5092 ////////////////////////////////////////////////////////////////////////////////
5093 /// Static function returning the current branch style.
5094 ///
5095 /// - style = 0 old Branch
5096 /// - style = 1 new Bronch
5097 
5100  return fgBranchStyle;
5101 }
5102 
5103 ////////////////////////////////////////////////////////////////////////////////
5104 /// Used for automatic sizing of the cache.
5105 ///
5106 /// Estimates a suitable size for the tree cache based on AutoFlush.
5107 /// A cache sizing factor is taken from the configuration. If this yields zero
5108 /// and withDefault is true the historical algorithm for default size is used.
5109 
5110 Long64_t TTree::GetCacheAutoSize(Bool_t withDefault /* = kFALSE */ ) const
5112  const char *stcs;
5113  Double_t cacheFactor = 0.0;
5114  if (!(stcs = gSystem->Getenv("ROOT_TTREECACHE_SIZE")) || !*stcs) {
5115  cacheFactor = gEnv->GetValue("TTreeCache.Size", 1.0);
5116  } else {
5117  cacheFactor = TString(stcs).Atof();
5118  }
5119 
5120  if (cacheFactor < 0.0) {
5121  // ignore negative factors
5122  cacheFactor = 0.0;
5123  }
5124 
5125  Long64_t cacheSize = 0;
5126 
5127  if (fAutoFlush < 0) cacheSize = Long64_t(-cacheFactor*fAutoFlush);
5128  else if (fAutoFlush == 0) cacheSize = 0;
5129  else cacheSize = Long64_t(cacheFactor*1.5*fAutoFlush*GetZipBytes()/(fEntries+1));
5130 
5131  if (cacheSize >= (INT_MAX / 4)) {
5132  cacheSize = INT_MAX / 4;
5133  }
5134 
5135  if (cacheSize < 0) {
5136  cacheSize = 0;
5137  }
5138 
5139  if (cacheSize == 0 && withDefault) {
5140  if (fAutoFlush < 0) cacheSize = -fAutoFlush;
5141  else if (fAutoFlush == 0) cacheSize = 0;
5142  else cacheSize = Long64_t(1.5*fAutoFlush*GetZipBytes()/(fEntries+1));
5143  }
5144 
5145  return cacheSize;
5146 }
5147 
5148 ////////////////////////////////////////////////////////////////////////////////
5149 /// Return an iterator over the cluster of baskets starting at firstentry.
5150 ///
5151 /// This iterator is not yet supported for TChain object.
5152 /// ~~~ {.cpp}
5153 /// TTree::TClusterIterator clusterIter = tree->GetClusterIterator(entry);
5154 /// Long64_t clusterStart;
5155 /// while( (clusterStart = clusterIter()) < tree->GetEntries() ) {
5156 /// printf("The cluster starts at %lld and ends at %lld (inclusive)\n",clusterStart,clusterIter.GetNextEntry()-1);
5157 /// }
5158 /// ~~~
5159 
5162  // create cache if wanted
5164 
5165  return TClusterIterator(this,firstentry);
5166 }
5167 
5168 ////////////////////////////////////////////////////////////////////////////////
5169 /// Return pointer to the current file.
5170 
5173  if (!fDirectory || fDirectory==gROOT) {
5174  return 0;
5175  }
5176  return fDirectory->GetFile();
5177 }
5178 
5179 ////////////////////////////////////////////////////////////////////////////////
5180 /// Return the number of entries matching the selection.
5181 /// Return -1 in case of errors.
5182 ///
5183 /// If the selection uses any arrays or containers, we return the number
5184 /// of entries where at least one element match the selection.
5185 /// GetEntries is implemented using the selector class TSelectorEntries,
5186 /// which can be used directly (see code in TTreePlayer::GetEntries) for
5187 /// additional option.
5188 /// If SetEventList was used on the TTree or TChain, only that subset
5189 /// of entries will be considered.
5190 
5191 Long64_t TTree::GetEntries(const char *selection)
5193  GetPlayer();
5194  if (fPlayer) {
5195  return fPlayer->GetEntries(selection);
5196  }
5197  return -1;
5198 }
5199 
5200 ////////////////////////////////////////////////////////////////////////////////
5201 /// Return pointer to the 1st Leaf named name in any Branch of this Tree or
5202 /// any branch in the list of friend trees.
5203 
5206  if (fEntries) return fEntries;
5207  if (!fFriends) return 0;
5209  if (!fr) return 0;
5210  TTree *t = fr->GetTree();
5211  if (t==0) return 0;
5212  return t->GetEntriesFriend();
5213 }
5214 
5215 ////////////////////////////////////////////////////////////////////////////////
5216 /// Read all branches of entry and return total number of bytes read.
5217 ///
5218 /// - `getall = 0` : get only active branches
5219 /// - `getall = 1` : get all branches
5220 ///
5221 /// The function returns the number of bytes read from the input buffer.
5222 /// If entry does not exist the function returns 0.
5223 /// If an I/O error occurs, the function returns -1.
5224 ///
5225 /// If the Tree has friends, also read the friends entry.
5226 ///
5227 /// To activate/deactivate one or more branches, use TBranch::SetBranchStatus
5228 /// For example, if you have a Tree with several hundred branches, and you
5229 /// are interested only by branches named "a" and "b", do
5230 /// ~~~ {.cpp}
5231 /// mytree.SetBranchStatus("*",0); //disable all branches
5232 /// mytree.SetBranchStatus("a",1);
5233 /// mytree.SetBranchStatus("b",1);
5234 /// ~~~
5235 /// when calling mytree.GetEntry(i); only branches "a" and "b" will be read.
5236 ///
5237 /// __WARNING!!__
5238 /// If your Tree has been created in split mode with a parent branch "parent.",
5239 /// ~~~ {.cpp}
5240 /// mytree.SetBranchStatus("parent",1);
5241 /// ~~~
5242 /// will not activate the sub-branches of "parent". You should do:
5243 /// ~~~ {.cpp}
5244 /// mytree.SetBranchStatus("parent*",1);
5245 /// ~~~
5246 /// Without the trailing dot in the branch creation you have no choice but to
5247 /// call SetBranchStatus explicitly for each of the sub branches.
5248 ///
5249 /// An alternative is to call directly
5250 /// ~~~ {.cpp}
5251 /// brancha.GetEntry(i)
5252 /// branchb.GetEntry(i);
5253 /// ~~~
5254 /// ## IMPORTANT NOTE
5255 ///
5256 /// By default, GetEntry reuses the space allocated by the previous object
5257 /// for each branch. You can force the previous object to be automatically
5258 /// deleted if you call mybranch.SetAutoDelete(kTRUE) (default is kFALSE).
5259 ///
5260 /// Example:
5261 ///
5262 /// Consider the example in $ROOTSYS/test/Event.h
5263 /// The top level branch in the tree T is declared with:
5264 /// ~~~ {.cpp}
5265 /// Event *event = 0; //event must be null or point to a valid object
5266 /// //it must be initialized
5267 /// T.SetBranchAddress("event",&event);
5268 /// ~~~
5269 /// When reading the Tree, one can choose one of these 3 options:
5270 ///
5271 /// ## OPTION 1
5272 ///
5273 /// ~~~ {.cpp}
5274 /// for (Long64_t i=0;i<nentries;i++) {
5275 /// T.GetEntry(i);
5276 /// // the object event has been filled at this point
5277 /// }
5278 /// ~~~
5279 /// The default (recommended). At the first entry an object of the class
5280 /// Event will be created and pointed by event. At the following entries,
5281 /// event will be overwritten by the new data. All internal members that are
5282 /// TObject* are automatically deleted. It is important that these members
5283 /// be in a valid state when GetEntry is called. Pointers must be correctly
5284 /// initialized. However these internal members will not be deleted if the
5285 /// characters "->" are specified as the first characters in the comment
5286 /// field of the data member declaration.
5287 ///
5288 /// If "->" is specified, the pointer member is read via pointer->Streamer(buf).
5289 /// In this case, it is assumed that the pointer is never null (case of
5290 /// pointer TClonesArray *fTracks in the Event example). If "->" is not
5291 /// specified, the pointer member is read via buf >> pointer. In this case
5292 /// the pointer may be null. Note that the option with "->" is faster to
5293 /// read or write and it also consumes less space in the file.
5294 ///
5295 /// ## OPTION 2
5296 ///
5297 /// The option AutoDelete is set
5298 /// ~~~ {.cpp}
5299 /// TBranch *branch = T.GetBranch("event");
5300 /// branch->SetAddress(&event);
5301 /// branch->SetAutoDelete(kTRUE);
5302 /// for (Long64_t i=0;i<nentries;i++) {
5303 /// T.GetEntry(i);
5304 /// // the object event has been filled at this point
5305 /// }
5306 /// ~~~
5307 /// In this case, at each iteration, the object event is deleted by GetEntry
5308 /// and a new instance of Event is created and filled.
5309 ///
5310 /// ## OPTION 3
5311 /// ~~~ {.cpp}
5312 /// Same as option 1, but you delete yourself the event.
5313 ///
5314 /// for (Long64_t i=0;i<nentries;i++) {
5315 /// delete event;
5316 /// event = 0; // EXTREMELY IMPORTANT
5317 /// T.GetEntry(i);
5318 /// // the object event has been filled at this point
5319 /// }
5320 /// ~~~
5321 /// It is strongly recommended to use the default option 1. It has the
5322 /// additional advantage that functions like TTree::Draw (internally calling
5323 /// TTree::GetEntry) will be functional even when the classes in the file are
5324 /// not available.
5325 ///
5326 /// Note: See the comments in TBranchElement::SetAddress() for the
5327 /// object ownership policy of the underlying (user) data.
5328 
5329 Int_t TTree::GetEntry(Long64_t entry, Int_t getall)
5331 
5332  // We already have been visited while recursively looking
5333  // through the friends tree, let return
5334  if (kGetEntry & fFriendLockStatus) return 0;
5335 
5336  if (entry < 0 || entry >= fEntries) return 0;
5337  Int_t i;
5338  Int_t nbytes = 0;
5339  fReadEntry = entry;
5340 
5341  // create cache if wanted
5343 
5344  Int_t nbranches = fBranches.GetEntriesFast();
5345  Int_t nb=0;
5346 
5347  auto seqprocessing = [&]() {
5348  TBranch *branch;
5349  for (i=0;i<nbranches;i++) {
5350  branch = (TBranch*)fBranches.UncheckedAt(i);
5351  nb = branch->GetEntry(entry, getall);
5352  if (nb < 0) break;
5353  nbytes += nb;
5354  }
5355  };
5356 
5357 #ifdef R__USE_IMT
5359  if (fSortedBranches.empty()) InitializeBranchLists(true);
5360 
5361  // Count branches are processed first and sequentially
5362  for (auto branch : fSeqBranches) {
5363  nb = branch->GetEntry(entry, getall);
5364  if (nb < 0) break;
5365  nbytes += nb;
5366  }
5367  if (nb < 0) return nb;
5368 
5369  // Enable this IMT use case (activate its locks)
5371 
5372  Int_t errnb = 0;
5373  std::atomic<Int_t> pos(0);
5374  std::atomic<Int_t> nbpar(0);
5375 
5376  auto mapFunction = [&]() {
5377  // The branch to process is obtained when the task starts to run.
5378  // This way, since branches are sorted, we make sure that branches
5379  // leading to big tasks are processed first. If we assigned the
5380  // branch at task creation time, the scheduler would not necessarily
5381  // respect our sorting.
5382  Int_t j = pos.fetch_add(1);
5383 
5384  Int_t nbtask = 0;
5385  auto branch = fSortedBranches[j].second;
5386 
5387  if (gDebug > 0) {
5388  std::stringstream ss;
5389  ss << std::this_thread::get_id();
5390  Info("GetEntry", "[IMT] Thread %s", ss.str().c_str());
5391  Info("GetEntry", "[IMT] Running task for branch #%d: %s", j, branch->GetName());
5392  }
5393 
5394  std::chrono::time_point<std::chrono::system_clock> start, end;
5395 
5396  start = std::chrono::system_clock::now();
5397  nbtask = branch->GetEntry(entry, getall);
5398  end = std::chrono::system_clock::now();
5399 
5400  Long64_t tasktime = (Long64_t)std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
5401  fSortedBranches[j].first += tasktime;
5402 
5403  if (nbtask < 0) errnb = nbtask;
5404  else nbpar += nbtask;
5405  };
5406 
5407  ROOT::TThreadExecutor pool;
5408  pool.Foreach(mapFunction, fSortedBranches.size());
5409 
5410  if (errnb < 0) {
5411  nb = errnb;
5412  }
5413  else {
5414  // Save the number of bytes read by the tasks
5415  nbytes += nbpar;
5416 
5417  // Re-sort branches if necessary
5421  }
5422  }
5423  }
5424  else {
5425  seqprocessing();
5426  }
5427 #else
5428  seqprocessing();
5429 #endif
5430  if (nb < 0) return nb;
5431 
5432  // GetEntry in list of friends
5433  if (!fFriends) return nbytes;
5434  TFriendLock lock(this,kGetEntry);
5435  TIter nextf(fFriends);
5436  TFriendElement *fe;
5437  while ((fe = (TFriendElement*)nextf())) {
5438  TTree *t = fe->GetTree();
5439  if (t) {
5441  nb = t->GetEntry(t->GetReadEntry(),getall);
5442  } else {
5443  if ( t->LoadTreeFriend(entry,this) >= 0 ) {
5444  nb = t->GetEntry(t->GetReadEntry(),getall);
5445  } else nb = 0;
5446  }
5447  if (nb < 0) return nb;
5448  nbytes += nb;
5449  }
5450  }
5451  return nbytes;
5452 }
5453 
5454 
5455 ////////////////////////////////////////////////////////////////////////////////
5456 /// Divides the top-level branches into two vectors: (i) branches to be
5457 /// processed sequentially and (ii) branches to be processed in parallel.
5458 /// Even if IMT is on, some branches might need to be processed first and in a
5459 /// sequential fashion: in the parallelization of GetEntry, those are the
5460 /// branches that store the size of another branch for every entry
5461 /// (e.g. the size of an array branch). If such branches were processed
5462 /// in parallel with the rest, there could be two threads invoking
5463 /// TBranch::GetEntry on one of them at the same time, since a branch that
5464 /// depends on a size (or count) branch will also invoke GetEntry on the latter.
5465 /// \param[in] checkLeafCount True if we need to check whether some branches are
5466 /// count leaves.
5467 
5468 void TTree::InitializeBranchLists(bool checkLeafCount)
5470  Int_t nbranches = fBranches.GetEntriesFast();
5471 
5472  // The branches to be processed sequentially are those that are the leaf count of another branch
5473  if (checkLeafCount) {
5474  for (Int_t i = 0; i < nbranches; i++) {
5475  TBranch* branch = (TBranch*)fBranches.UncheckedAt(i);
5476  auto leafCount = ((TLeaf*)branch->GetListOfLeaves()->At(0))->GetLeafCount();
5477  if (leafCount) {
5478  auto countBranch = leafCount->GetBranch();
5479  if (std::find(fSeqBranches.begin(), fSeqBranches.end(), countBranch) == fSeqBranches.end()) {
5480  fSeqBranches.push_back(countBranch);
5481  }
5482  }
5483  }
5484  }
5485 
5486  // The special branch fBranchRef also needs to be processed sequentially
5487  if (fBranchRef) {
5488  fSeqBranches.push_back(fBranchRef);
5489  }
5490 
5491  // Any branch that is not a leaf count can be safely processed in parallel when reading
5492  for (Int_t i = 0; i < nbranches; i++) {
5493  Long64_t bbytes = 0;
5494  TBranch* branch = (TBranch*)fBranches.UncheckedAt(i);
5495  if (std::find(fSeqBranches.begin(), fSeqBranches.end(), branch) == fSeqBranches.end()) {
5496  bbytes = branch->GetTotBytes("*");
5497  fSortedBranches.emplace_back(bbytes, branch);
5498  }
5499  }
5500 
5501  // Initially sort parallel branches by size
5502  std::sort(fSortedBranches.begin(),
5503  fSortedBranches.end(),
5504  [](std::pair<Long64_t,TBranch*> a, std::pair<Long64_t,TBranch*> b) {
5505  return a.first > b.first;
5506  });
5507 
5508  for (size_t i = 0; i < fSortedBranches.size(); i++) {
5509  fSortedBranches[i].first = 0LL;
5510  }
5511 }
5512 
5513 ////////////////////////////////////////////////////////////////////////////////
5514 /// Sorts top-level branches by the last average task time recorded per branch.
5515 
5518  for (size_t i = 0; i < fSortedBranches.size(); i++) {
5519  fSortedBranches[i].first *= kNEntriesResortInv;
5520  }
5521 
5522  std::sort(fSortedBranches.begin(),
5523  fSortedBranches.end(),
5524  [](std::pair<Long64_t,TBranch*> a, std::pair<Long64_t,TBranch*> b) {
5525  return a.first > b.first;
5526  });
5527 
5528  for (size_t i = 0; i < fSortedBranches.size(); i++) {
5529  fSortedBranches[i].first = 0LL;
5530  }
5531 }
5532 
5533 ////////////////////////////////////////////////////////////////////////////////
5534 ///Returns the entry list, set to this tree
5535 
5538  return fEntryList;
5539 }
5540 
5541 ////////////////////////////////////////////////////////////////////////////////
5542 /// Return entry number corresponding to entry.
5543 ///
5544 /// if no TEntryList set returns entry
5545 /// else returns the entry number corresponding to the list index=entry
5546 
5549  if (!fEntryList) {
5550  return entry;
5551  }
5552 
5553  return fEntryList->GetEntry(entry);
5554 }
5555 
5556 ////////////////////////////////////////////////////////////////////////////////
5557 /// Return entry number corresponding to major and minor number.
5558 /// Note that this function returns only the entry number, not the data
5559 /// To read the data corresponding to an entry number, use TTree::GetEntryWithIndex
5560 /// the BuildIndex function has created a table of Long64_t* of sorted values
5561 /// corresponding to val = major<<31 + minor;
5562 /// The function performs binary search in this sorted table.
5563 /// If it finds a pair that matches val, it returns directly the
5564 /// index in the table.
5565 /// If an entry corresponding to major and minor is not found, the function
5566 /// returns the index of the major,minor pair immediately lower than the
5567 /// requested value, ie it will return -1 if the pair is lower than
5568 /// the first entry in the index.
5569 ///
5570 /// See also GetEntryNumberWithIndex
5571 
5574  if (!fTreeIndex) {
5575  return -1;
5576  }
5577  return fTreeIndex->GetEntryNumberWithBestIndex(major, minor);
5578 }
5579 
5580 ////////////////////////////////////////////////////////////////////////////////
5581 /// Return entry number corresponding to major and minor number.
5582 /// Note that this function returns only the entry number, not the data
5583 /// To read the data corresponding to an entry number, use TTree::GetEntryWithIndex
5584 /// the BuildIndex function has created a table of Long64_t* of sorted values
5585 /// corresponding to val = major<<31 + minor;
5586 /// The function performs binary search in this sorted table.
5587 /// If it finds a pair that matches val, it returns directly the
5588 /// index in the table, otherwise it returns -1.
5589 ///
5590 /// See also GetEntryNumberWithBestIndex
5591 
5594  if (!fTreeIndex) {
5595  return -1;
5596  }
5597  return fTreeIndex->GetEntryNumberWithIndex(major, minor);
5598 }
5599 
5600 ////////////////////////////////////////////////////////////////////////////////
5601 /// Read entry corresponding to major and minor number.
5602 ///
5603 /// The function returns the total number of bytes read.
5604 /// If the Tree has friend trees, the corresponding entry with
5605 /// the index values (major,minor) is read. Note that the master Tree
5606 /// and its friend may have different entry serial numbers corresponding
5607 /// to (major,minor).
5608 
5611  // We already have been visited while recursively looking
5612  // through the friends tree, let's return.
5614  return 0;
5615  }
5616  Long64_t serial = GetEntryNumberWithIndex(major, minor);
5617  if (serial < 0) {
5618  return -1;
5619  }
5620  // create cache if wanted
5622 
5623  Int_t i;
5624  Int_t nbytes = 0;
5625  fReadEntry = serial;
5626  TBranch *branch;
5627  Int_t nbranches = fBranches.GetEntriesFast();
5628  Int_t nb;
5629  for (i = 0; i < nbranches; ++i) {
5630  branch = (TBranch*)fBranches.UncheckedAt(i);
5631  nb = branch->GetEntry(serial);
5632  if (nb < 0) return nb;
5633  nbytes += nb;
5634  }
5635  // GetEntry in list of friends
5636  if (!fFriends) return nbytes;
5637  TFriendLock lock(this,kGetEntryWithIndex);
5638  TIter nextf(fFriends);
5639  TFriendElement* fe = 0;
5640  while ((fe = (TFriendElement*) nextf())) {
5641  TTree *t = fe->GetTree();
5642  if (t) {
5643  serial = t->GetEntryNumberWithIndex(major,minor);
5644  if (serial <0) return -nbytes;
5645  nb = t->GetEntry(serial);
5646  if (nb < 0) return nb;
5647  nbytes += nb;
5648  }
5649  }
5650  return nbytes;
5651 }
5652 
5653 ////////////////////////////////////////////////////////////////////////////////
5654 /// Return a pointer to the TTree friend whose name or alias is 'friendname.
5655 
5656 TTree* TTree::GetFriend(const char *friendname) const
5658 
5659  // We already have been visited while recursively
5660  // looking through the friends tree, let's return.
5661  if (kGetFriend & fFriendLockStatus) {
5662  return 0;
5663  }
5664  if (!fFriends) {
5665  return 0;
5666  }
5667  TFriendLock lock(const_cast<TTree*>(this), kGetFriend);
5668  TIter nextf(fFriends);
5669  TFriendElement* fe = 0;
5670  while ((fe = (TFriendElement*) nextf())) {
5671  if (strcmp(friendname,fe->GetName())==0
5672  || strcmp(friendname,fe->GetTreeName())==0) {
5673  return fe->GetTree();
5674  }
5675  }
5676  // After looking at the first level,
5677  // let's see if it is a friend of friends.
5678  nextf.Reset();
5679  fe = 0;
5680  while ((fe = (TFriendElement*) nextf())) {
5681  TTree *res = fe->GetTree()->GetFriend(friendname);
5682  if (res) {
5683  return res;
5684  }
5685  }
5686  return 0;
5687 }
5688 
5689 ////////////////////////////////////////////////////////////////////////////////
5690 /// If the 'tree' is a friend, this method returns its alias name.
5691 ///
5692 /// This alias is an alternate name for the tree.
5693 ///
5694 /// It can be used in conjunction with a branch or leaf name in a TTreeFormula,
5695 /// to specify in which particular tree the branch or leaf can be found if
5696 /// the friend trees have branches or leaves with the same name as the master
5697 /// tree.
5698 ///
5699 /// It can also be used in conjunction with an alias created using
5700 /// TTree::SetAlias in a TTreeFormula, e.g.:
5701 /// ~~~ {.cpp}
5702 /// maintree->Draw("treealias.fPx - treealias.myAlias");
5703 /// ~~~
5704 /// where fPx is a branch of the friend tree aliased as 'treealias' and 'myAlias'
5705 /// was created using TTree::SetAlias on the friend tree.
5706 ///
5707 /// However, note that 'treealias.myAlias' will be expanded literally,
5708 /// without remembering that it comes from the aliased friend and thus
5709 /// the branch name might not be disambiguated properly, which means
5710 /// that you may not be able to take advantage of this feature.
5711 ///
5712 
5713 const char* TTree::GetFriendAlias(TTree* tree) const
5715  if ((tree == this) || (tree == GetTree())) {
5716  return 0;
5717  }
5718 
5719  // We already have been visited while recursively
5720  // looking through the friends tree, let's return.
5722  return 0;
5723  }
5724  if (!fFriends) {
5725  return 0;
5726  }
5727  TFriendLock lock(const_cast<TTree*>(this), kGetFriendAlias);
5728  TIter nextf(fFriends);
5729  TFriendElement* fe = 0;
5730  while ((fe = (TFriendElement*) nextf())) {
5731  TTree* t = fe->GetTree();
5732  if (t == tree) {
5733  return fe->GetName();
5734  }
5735  // Case of a chain:
5736  if (t && t->GetTree() == tree) {
5737  return fe->GetName();
5738  }
5739  }
5740  // After looking at the first level,
5741  // let's see if it is a friend of friends.
5742  nextf.Reset();
5743  fe = 0;
5744  while ((fe = (TFriendElement*) nextf())) {
5745  const char* res = fe->GetTree()->GetFriendAlias(tree);
5746  if (res) {
5747  return res;
5748  }
5749  }
5750  return 0;
5751 }
5752 
5753 ////////////////////////////////////////////////////////////////////////////////
5754 /// Returns the current set of IO settings
5757  return fIOFeatures;
5758 }
5759 
5760 ////////////////////////////////////////////////////////////////////////////////
5761 /// Creates a new iterator that will go through all the leaves on the tree itself and its friend.
5762 
5765  return new TTreeFriendLeafIter(this, dir);
5766 }
5767 
5768 ////////////////////////////////////////////////////////////////////////////////
5769 /// Return pointer to the 1st Leaf named name in any Branch of this
5770 /// Tree or any branch in the list of friend trees.
5771 ///
5772 /// The leaf name can contain the name of a friend tree with the
5773 /// syntax: friend_dir_and_tree.full_leaf_name
5774 /// the friend_dir_and_tree can be of the form:
5775 /// ~~~ {.cpp}
5776 /// TDirectoryName/TreeName
5777 /// ~~~
5778 
5779 TLeaf* TTree::GetLeafImpl(const char* branchname, const char *leafname)
5781  TLeaf *leaf = 0;
5782  if (branchname) {
5783  TBranch *branch = FindBranch(branchname);
5784  if (branch) {
5785  leaf = branch->GetLeaf(leafname);
5786  if (leaf) {
5787  return leaf;
5788  }
5789  }
5790  }
5791  TIter nextl(GetListOfLeaves());
5792  while ((leaf = (TLeaf*)nextl())) {
5793  if (strcmp(leaf->GetName(),leafname)) continue;
5794  if (branchname) {
5795  UInt_t nbch = strlen(branchname);
5796  TBranch *br = leaf->GetBranch();
5797  const char* brname = br->GetName();
5798  TBranch *mother = br->GetMother();
5799  if (strncmp(brname,branchname,nbch)) {
5800  if (mother != br) {
5801  const char *mothername = mother->GetName();
5802  UInt_t motherlen = strlen(mothername);
5803  if (nbch > motherlen && strncmp(mothername,branchname,motherlen)==0 && (mothername[motherlen-1]=='.' || branchname[motherlen]=='.')) {
5804  // The left part of the requested name match the name of the mother, let's see if the right part match the name of the branch.
5805  if (strncmp(brname,branchname+motherlen+1,nbch-motherlen-1)) {
5806  // No it does not
5807  continue;
5808  } // else we have match so we can proceed.
5809  } else {
5810  // no match
5811  continue;
5812  }
5813  } else {
5814  continue;
5815  }
5816  }
5817  // The start of the branch name is identical to the content
5818  // of 'aname' before the first '/'.
5819  // Let's make sure that it is not longer (we are trying
5820  // to avoid having jet2/value match the branch jet23
5821  if ((strlen(brname) > nbch) && (brname[nbch] != '.') && (brname[nbch] != '[')) {
5822  continue;
5823  }
5824  }
5825  return leaf;
5826  }
5827  if (!fFriends) return 0;
5828  TFriendLock lock(this,kGetLeaf);
5829  TIter next(fFriends);
5830  TFriendElement *fe;
5831  while ((fe = (TFriendElement*)next())) {
5832  TTree *t = fe->GetTree();
5833  if (t) {
5834  leaf = t->GetLeaf(leafname);
5835  if (leaf) return leaf;
5836  }
5837  }
5838 
5839  //second pass in the list of friends when the leaf name
5840  //is prefixed by the tree name
5841  TString strippedArg;
5842  next.Reset();
5843  while ((fe = (TFriendElement*)next())) {
5844  TTree *t = fe->GetTree();
5845  if (t==0) continue;
5846  char *subname = (char*)strstr(leafname,fe->GetName());
5847  if (subname != leafname) continue;
5848  Int_t l = strlen(fe->GetName());
5849  subname += l;
5850  if (*subname != '.') continue;
5851  subname++;
5852  strippedArg += subname;
5853  leaf = t->GetLeaf(branchname,subname);
5854  if (leaf) return leaf;
5855  }
5856  return 0;
5857 }
5858 
5859 ////////////////////////////////////////////////////////////////////////////////
5860 /// Return pointer to the 1st Leaf named name in any Branch of this
5861 /// Tree or any branch in the list of friend trees.
5862 ///
5863 /// The leaf name can contain the name of a friend tree with the
5864 /// syntax: friend_dir_and_tree.full_leaf_name
5865 /// the friend_dir_and_tree can be of the form:
5866 ///
5867 /// TDirectoryName/TreeName
5868 
5869 TLeaf* TTree::GetLeaf(const char* branchname, const char *leafname)
5871  if (leafname == 0) return 0;
5872 
5873  // We already have been visited while recursively looking
5874  // through the friends tree, let return
5875  if (kGetLeaf & fFriendLockStatus) {
5876  return 0;
5877  }
5878 
5879  return GetLeafImpl(branchname,leafname);
5880 }
5881 
5882 ////////////////////////////////////////////////////////////////////////////////
5883 /// Return pointer to the 1st Leaf named name in any Branch of this
5884 /// Tree or any branch in the list of friend trees.
5885 ///
5886 /// aname may be of the form branchname/leafname
5887 
5888 TLeaf* TTree::GetLeaf(const char* aname)
5890  if (aname == 0) return 0;
5891 
5892  // We already have been visited while recursively looking
5893  // through the friends tree, let return
5894  if (kGetLeaf & fFriendLockStatus) {
5895  return 0;
5896  }
5897  char* slash = (char*) strrchr(aname, '/');
5898  char* name = 0;
5899  UInt_t nbch = 0;
5900  if (slash) {
5901  name = slash + 1;
5902  nbch = slash - aname;
5903  TString brname(aname,nbch);
5904  return GetLeafImpl(brname.Data(),name);
5905  } else {
5906  return GetLeafImpl(0,aname);
5907  }
5908 }
5909 
5910 ////////////////////////////////////////////////////////////////////////////////
5911 /// Return maximum of column with name columname.
5912 /// if the Tree has an associated TEventList or TEntryList, the maximum
5913 /// is computed for the entries in this list.
5914 
5915 Double_t TTree::GetMaximum(const char* columname)
5917  TLeaf* leaf = this->GetLeaf(columname);
5918  if (!leaf) {
5919  return 0;
5920  }
5921 
5922  // create cache if wanted
5924 
5925  TBranch* branch = leaf->GetBranch();
5926  Double_t cmax = -DBL_MAX;
5927  for (Long64_t i = 0; i < fEntries; ++i) {
5928  Long64_t entryNumber = this->GetEntryNumber(i);
5929  if (entryNumber < 0) break;
5930  branch->GetEntry(entryNumber);
5931  for (Int_t j = 0; j < leaf->GetLen(); ++j) {
5932  Double_t val = leaf->GetValue(j);
5933  if (val > cmax) {
5934  cmax = val;
5935  }
5936  }
5937  }
5938  return cmax;
5939 }
5940 
5941 ////////////////////////////////////////////////////////////////////////////////
5942 /// Static function which returns the tree file size limit in bytes.
5943 
5946  return fgMaxTreeSize;
5947 }
5948 
5949 ////////////////////////////////////////////////////////////////////////////////
5950 /// Return minimum of column with name columname.
5951 /// if the Tree has an associated TEventList or TEntryList, the minimum
5952 /// is computed for the entries in this list.
5953 
5954 Double_t TTree::GetMinimum(const char* columname)
5956  TLeaf* leaf = this->GetLeaf(columname);
5957  if (!leaf) {
5958  return 0;
5959  }
5960 
5961  // create cache if wanted
5963 
5964  TBranch* branch = leaf->GetBranch();
5965  Double_t cmin = DBL_MAX;
5966  for (Long64_t i = 0; i < fEntries; ++i) {
5967  Long64_t entryNumber = this->GetEntryNumber(i);
5968  if (entryNumber < 0) break;
5969  branch->GetEntry(entryNumber);
5970  for (Int_t j = 0;j < leaf->GetLen(); ++j) {
5971  Double_t val = leaf->GetValue(j);
5972  if (val < cmin) {
5973  cmin = val;
5974  }
5975  }
5976  }
5977  return cmin;
5978 }
5979 
5980 ////////////////////////////////////////////////////////////////////////////////
5981 /// Load the TTreePlayer (if not already done).
5982 
5985  if (fPlayer) {
5986  return fPlayer;
5987  }
5989  return fPlayer;
5990 }
5991 
5992 ////////////////////////////////////////////////////////////////////////////////
5993 /// Find and return the TTreeCache registered with the file and which may
5994 /// contain branches for us. If create is true and there is no cache
5995 /// a new cache is created with default size.
5996 
5997 TTreeCache *TTree::GetReadCache(TFile *file, Bool_t create /* = kFALSE */ )
5999  TTreeCache *pe = dynamic_cast<TTreeCache*>(file->GetCacheRead(this));
6000  if (pe && pe->GetTree() != this) pe = 0;
6001  if (create && !pe) {
6003  pe = dynamic_cast<TTreeCache*>(file->GetCacheRead(this));
6004  if (pe && pe->GetTree() != this) pe = 0;
6005  }
6006  return pe;
6007 }
6008 
6009 ////////////////////////////////////////////////////////////////////////////////
6010 /// Return a pointer to the list containing user objects associated to this tree.
6011 ///
6012 /// The list is automatically created if it does not exist.
6013 ///
6014 /// WARNING: By default the TTree destructor will delete all objects added
6015 /// to this list. If you do not want these objects to be deleted,
6016 /// call:
6017 ///
6018 /// mytree->GetUserInfo()->Clear();
6019 ///
6020 /// before deleting the tree.
6021 
6024  if (!fUserInfo) {
6025  fUserInfo = new TList();
6026  fUserInfo->SetName("UserInfo");
6027  }
6028  return fUserInfo;
6029 }
6030 
6031 ////////////////////////////////////////////////////////////////////////////////
6032 /// Appends the cluster range information stored in 'fromtree' to this tree,
6033 /// including the value of fAutoFlush.
6034 ///
6035 /// This is used when doing a fast cloning (by TTreeCloner).
6036 /// See also fAutoFlush and fAutoSave if needed.
6037 
6038 void TTree::ImportClusterRanges(TTree *fromtree)
6040  Long64_t autoflush = fromtree->GetAutoFlush();
6041  if (fromtree->fNClusterRange == 0 && fromtree->fAutoFlush == fAutoFlush) {
6042  // nothing to do
6043  } else if (fNClusterRange || fromtree->fNClusterRange) {
6044  Int_t newsize = fNClusterRange + 1 + fromtree->fNClusterRange;
6045  if (newsize > fMaxClusterRange) {
6046  if (fMaxClusterRange) {
6048  newsize*sizeof(Long64_t),fMaxClusterRange*sizeof(Long64_t));
6050  newsize*sizeof(Long64_t),fMaxClusterRange*sizeof(Long64_t));
6051  fMaxClusterRange = newsize;
6052  } else {
6053  fMaxClusterRange = newsize;
6056  }
6057  }
6058  if (fEntries) {
6061  ++fNClusterRange;
6062  }
6063  for (Int_t i = 0 ; i < fromtree->fNClusterRange; ++i) {
6065  fClusterSize[fNClusterRange] = fromtree->fClusterSize[i];
6066  ++fNClusterRange;
6067  }
6068  fAutoFlush = autoflush;
6069  } else {
6070  SetAutoFlush( autoflush );
6071  }
6072  Long64_t autosave = GetAutoSave();
6073  if (autoflush > 0 && autosave > 0) {
6074  SetAutoSave( autoflush*(autosave/autoflush) );
6075  }
6076 }
6077 
6078 ////////////////////////////////////////////////////////////////////////////////
6079 /// Keep a maximum of fMaxEntries in memory.
6080 
6081 void TTree::KeepCircular()
6084  Long64_t maxEntries = fMaxEntries - (fMaxEntries / 10);
6085  for (Int_t i = 0; i < nb; ++i) {
6086  TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
6087  branch->KeepCircular(maxEntries);
6088  }
6089  if (fNClusterRange) {
6090  Long64_t entriesOffset = fEntries - maxEntries;
6091  Int_t oldsize = fNClusterRange;
6092  for(Int_t i = 0, j = 0; j < oldsize; ++j) {
6093  if (fClusterRangeEnd[j] > entriesOffset) {
6094  fClusterRangeEnd[i] = fClusterRangeEnd[j] - entriesOffset;
6095  ++i;
6096  } else {
6097  --fNClusterRange;
6098  }
6099  }
6100  }
6101  fEntries = maxEntries;
6102  fReadEntry = -1;
6103 }
6104 
6105 ////////////////////////////////////////////////////////////////////////////////
6106 /// Read in memory all baskets from all branches up to the limit of maxmemory bytes.
6107 ///
6108 /// If maxmemory is non null and positive SetMaxVirtualSize is called
6109 /// with this value. Default for maxmemory is 2000000000 (2 Gigabytes).
6110 /// The function returns the total number of baskets read into memory
6111 /// if negative an error occurred while loading the branches.
6112 /// This method may be called to force branch baskets in memory
6113 /// when random access to branch entries is required.
6114 /// If random access to only a few branches is required, you should
6115 /// call directly TBranch::LoadBaskets.
6116 
6119  if (maxmemory > 0) SetMaxVirtualSize(maxmemory);
6120 
6121  TIter next(GetListOfLeaves());
6122  TLeaf *leaf;
6123  Int_t nimported = 0;
6124  while ((leaf=(TLeaf*)next())) {
6125  nimported += leaf->GetBranch()->LoadBaskets();//break;
6126  }
6127  return nimported;
6128 }
6129 
6130 ////////////////////////////////////////////////////////////////////////////////
6131 /// Set current entry.
6132 ///
6133 /// Returns -2 if entry does not exist (just as TChain::LoadTree()).
6134 ///
6135 /// Note: This function is overloaded in TChain.
6136 ///
6137 
6140  // We already have been visited while recursively looking
6141  // through the friends tree, let return
6142  if (kLoadTree & fFriendLockStatus) {
6143  // We need to return a negative value to avoid a circular list of friend
6144  // to think that there is always an entry somewhere in the list.
6145  return -1;
6146  }
6147 
6148  if (fNotify) {
6149  if (fReadEntry < 0) {
6150  fNotify->Notify();
6151  }
6152  }
6153  fReadEntry = entry;
6154 
6155  Bool_t friendHasEntry = kFALSE;
6156  if (fFriends) {
6157  // Set current entry in friends as well.
6158  //
6159  // An alternative would move this code to each of the
6160  // functions calling LoadTree (and to overload a few more).
6161  Bool_t needUpdate = kFALSE;
6162  {
6163  // This scope is need to insure the lock is released at the right time
6164  TIter nextf(fFriends);
6165  TFriendLock lock(this, kLoadTree);
6166  TFriendElement* fe = 0;
6167  while ((fe = (TFriendElement*) nextf())) {
6169  // This friend element was added by the chain that owns this
6170  // tree, the chain will deal with loading the correct entry.
6171  continue;
6172  }
6173  TTree* friendTree = fe->GetTree();
6174  if (friendTree == 0) {
6175  // Somehow we failed to retrieve the friend TTree.
6176  } else if (friendTree->IsA() == TTree::Class()) {
6177  // Friend is actually a tree.
6178  if (friendTree->LoadTreeFriend(entry, this) >= 0) {
6179  friendHasEntry = kTRUE;
6180  }
6181  } else {
6182  // Friend is actually a chain.
6183  // FIXME: This logic should be in the TChain override.
6184  Int_t oldNumber = friendTree->GetTreeNumber();
6185  if (friendTree->LoadTreeFriend(entry, this) >= 0) {
6186  friendHasEntry = kTRUE;
6187  }
6188  Int_t newNumber = friendTree->GetTreeNumber();
6189  if (oldNumber != newNumber) {
6190  // We can not just compare the tree pointers because they could be reused.
6191  // So we compare the tree number instead.
6192  needUpdate = kTRUE;
6193  }
6194  }
6195  } // for each friend
6196  }
6197  if (needUpdate) {
6198  //update list of leaves in all TTreeFormula of the TTreePlayer (if any)
6199  if (fPlayer) {
6201  }
6202  //Notify user if requested
6203  if (fNotify) {
6204  fNotify->Notify();
6205  }
6206  }
6207  }
6208 
6209  if ((fReadEntry >= fEntries) && !friendHasEntry) {
6210  fReadEntry = -1;
6211  return -2;
6212  }
6213  return fReadEntry;
6214 }
6215 
6216 ////////////////////////////////////////////////////////////////////////////////
6217 /// Load entry on behalf of our master tree, we may use an index.
6218 ///
6219 /// Called by LoadTree() when the masterTree looks for the entry
6220 /// number in a friend tree (us) corresponding to the passed entry
6221 /// number in the masterTree.
6222 ///
6223 /// If we have no index, our entry number and the masterTree entry
6224 /// number are the same.
6225 ///
6226 /// If we *do* have an index, we must find the (major, minor) value pair
6227 /// in masterTree to locate our corresponding entry.
6228 ///
6229 
6230 Long64_t TTree::LoadTreeFriend(Long64_t entry, TTree* masterTree)
6232  if (!fTreeIndex) {
6233  return LoadTree(entry);
6234  }
6235  return LoadTree(fTreeIndex->GetEntryNumberFriend(masterTree));
6236 }
6237 
6238 ////////////////////////////////////////////////////////////////////////////////
6239 /// Generate a skeleton analysis class for this tree.
6240 ///
6241 /// The following files are produced: classname.h and classname.C.
6242 /// If classname is 0, classname will be called "nameoftree".
6243 ///
6244 /// The generated code in classname.h includes the following:
6245 ///
6246 /// - Identification of the original tree and the input file name.
6247 /// - Definition of an analysis class (data members and member functions).
6248 /// - The following member functions:
6249 /// - constructor (by default opening the tree file),
6250 /// - GetEntry(Long64_t entry),
6251 /// - Init(TTree* tree) to initialize a new TTree,
6252 /// - Show(Long64_t entry) to read and dump entry.
6253 ///
6254 /// The generated code in classname.C includes only the main
6255 /// analysis function Loop.
6256 ///
6257 /// To use this function:
6258 ///
6259 /// - Open your tree file (eg: TFile f("myfile.root");)
6260 /// - T->MakeClass("MyClass");
6261 ///
6262 /// where T is the name of the TTree in file myfile.root,
6263 /// and MyClass.h, MyClass.C the name of the files created by this function.
6264 /// In a ROOT session, you can do:
6265 /// ~~~ {.cpp}
6266 /// root > .L MyClass.C
6267 /// root > MyClass* t = new MyClass;
6268 /// root > t->GetEntry(12); // Fill data members of t with entry number 12.
6269 /// root > t->Show(); // Show values of entry 12.
6270 /// root > t->Show(16); // Read and show values of entry 16.
6271 /// root > t->Loop(); // Loop on all entries.
6272 /// ~~~
6273 /// NOTE: Do not use the code generated for a single TTree which is part
6274 /// of a TChain to process that entire TChain. The maximum dimensions
6275 /// calculated for arrays on the basis of a single TTree from the TChain
6276 /// might be (will be!) too small when processing all of the TTrees in
6277 /// the TChain. You must use myChain.MakeClass() to generate the code,
6278 /// not myTree.MakeClass(...).
6279 
6280 Int_t TTree::MakeClass(const char* classname, Option_t* option)
6282  GetPlayer();
6283  if (!fPlayer) {
6284  return 0;
6285  }
6286  return fPlayer->MakeClass(classname, option);
6287 }
6288 
6289 ////////////////////////////////////////////////////////////////////////////////
6290 /// Generate a skeleton function for this tree.
6291 ///
6292 /// The function code is written on filename.
6293 /// If filename is 0, filename will be called nameoftree.C
6294 ///
6295 /// The generated code includes the following:
6296 /// - Identification of the original Tree and Input file name,
6297 /// - Opening the Tree file,
6298 /// - Declaration of Tree variables,
6299 /// - Setting of branches addresses,
6300 /// - A skeleton for the entry loop.
6301 ///
6302 /// To use this function:
6303 ///
6304 /// - Open your Tree file (eg: TFile f("myfile.root");)
6305 /// - T->MakeCode("MyAnalysis.C");
6306 ///
6307 /// where T is the name of the TTree in file myfile.root
6308 /// and MyAnalysis.C the name of the file created by this function.
6309 ///
6310 /// NOTE: Since the implementation of this function, a new and better
6311 /// function TTree::MakeClass() has been developed.
6312 
6313 Int_t TTree::MakeCode(const char* filename)
6315  Warning("MakeCode", "MakeCode is obsolete. Use MakeClass or MakeSelector instead");
6316 
6317  GetPlayer();
6318  if (!fPlayer) return 0;
6319  return fPlayer->MakeCode(filename);
6320 }
6321 
6322 ////////////////////////////////////////////////////////////////////////////////
6323 /// Generate a skeleton analysis class for this Tree using TBranchProxy.
6324 ///
6325 /// TBranchProxy is the base of a class hierarchy implementing an
6326 /// indirect access to the content of the branches of a TTree.
6327 ///
6328 /// "proxyClassname" is expected to be of the form:
6329 /// ~~~ {.cpp}
6330 /// [path/]fileprefix
6331 /// ~~~
6332 /// The skeleton will then be generated in the file:
6333 /// ~~~ {.cpp}
6334 /// fileprefix.h
6335 /// ~~~
6336 /// located in the current directory or in 'path/' if it is specified.
6337 /// The class generated will be named 'fileprefix'
6338 ///
6339 /// "macrofilename" and optionally "cutfilename" are expected to point
6340 /// to source files which will be included by the generated skeleton.
6341 /// Method of the same name as the file(minus the extension and path)
6342 /// will be called by the generated skeleton's Process method as follow:
6343 /// ~~~ {.cpp}
6344 /// [if (cutfilename())] htemp->Fill(macrofilename());
6345 /// ~~~
6346 /// "option" can be used select some of the optional features during
6347 /// the code generation. The possible options are:
6348 ///
6349 /// - nohist : indicates that the generated ProcessFill should not fill the histogram.
6350 ///
6351 /// 'maxUnrolling' controls how deep in the class hierarchy does the
6352 /// system 'unroll' classes that are not split. Unrolling a class
6353 /// allows direct access to its data members (this emulates the behavior
6354 /// of TTreeFormula).
6355 ///
6356 /// The main features of this skeleton are:
6357 ///
6358 /// * on-demand loading of branches
6359 /// * ability to use the 'branchname' as if it was a data member
6360 /// * protection against array out-of-bounds errors
6361 /// * ability to use the branch data as an object (when the user code is available)
6362 ///
6363 /// For example with Event.root, if
6364 /// ~~~ {.cpp}
6365 /// Double_t somePx = fTracks.fPx[2];
6366 /// ~~~
6367 /// is executed by one of the method of the skeleton,
6368 /// somePx will updated with the current value of fPx of the 3rd track.
6369 ///
6370 /// Both macrofilename and the optional cutfilename are expected to be
6371 /// the name of source files which contain at least a free standing
6372 /// function with the signature:
6373 /// ~~~ {.cpp}
6374 /// x_t macrofilename(); // i.e function with the same name as the file
6375 /// ~~~
6376 /// and
6377 /// ~~~ {.cpp}
6378 /// y_t cutfilename(); // i.e function with the same name as the file
6379 /// ~~~
6380 /// x_t and y_t needs to be types that can convert respectively to a double
6381 /// and a bool (because the skeleton uses:
6382 ///
6383 /// if (cutfilename()) htemp->Fill(macrofilename());
6384 ///
6385 /// These two functions are run in a context such that the branch names are
6386 /// available as local variables of the correct (read-only) type.
6387 ///
6388 /// Note that if you use the same 'variable' twice, it is more efficient
6389 /// to 'cache' the value. For example:
6390 /// ~~~ {.cpp}
6391 /// Int_t n = fEventNumber; // Read fEventNumber
6392 /// if (n<10 || n>10) { ... }
6393 /// ~~~
6394 /// is more efficient than
6395 /// ~~~ {.cpp}
6396 /// if (fEventNumber<10 || fEventNumber>10)
6397 /// ~~~
6398 /// Also, optionally, the generated selector will also call methods named
6399 /// macrofilename_methodname in each of 6 main selector methods if the method
6400 /// macrofilename_methodname exist (Where macrofilename is stripped of its
6401 /// extension).
6402 ///
6403 /// Concretely, with the script named h1analysisProxy.C,
6404 ///
6405 /// - The method calls the method (if it exist)
6406 /// - Begin -> void h1analysisProxy_Begin(TTree*);
6407 /// - SlaveBegin -> void h1analysisProxy_SlaveBegin(TTree*);
6408 /// - Notify -> Bool_t h1analysisProxy_Notify();
6409 /// - Process -> Bool_t h1analysisProxy_Process(Long64_t);
6410 /// - SlaveTerminate -> void h1analysisProxy_SlaveTerminate();
6411 /// - Terminate -> void h1analysisProxy_Terminate();
6412 ///
6413 /// If a file name macrofilename.h (or .hh, .hpp, .hxx, .hPP, .hXX) exist
6414 /// it is included before the declaration of the proxy class. This can
6415 /// be used in particular to insure that the include files needed by
6416 /// the macro file are properly loaded.
6417 ///
6418 /// The default histogram is accessible via the variable named 'htemp'.
6419 ///
6420 /// If the library of the classes describing the data in the branch is
6421 /// loaded, the skeleton will add the needed `include` statements and
6422 /// give the ability to access the object stored in the branches.
6423 ///
6424 /// To draw px using the file hsimple.root (generated by the
6425 /// hsimple.C tutorial), we need a file named hsimple.cxx:
6426 /// ~~~ {.cpp}
6427 /// double hsimple() {
6428 /// return px;
6429 /// }
6430 /// ~~~
6431 /// MakeProxy can then be used indirectly via the TTree::Draw interface
6432 /// as follow:
6433 /// ~~~ {.cpp}
6434 /// new TFile("hsimple.root")
6435 /// ntuple->Draw("hsimple.cxx");
6436 /// ~~~
6437 /// A more complete example is available in the tutorials directory:
6438 /// h1analysisProxy.cxx , h1analysProxy.h and h1analysisProxyCut.C
6439 /// which reimplement the selector found in h1analysis.C
6440 
6441 Int_t TTree::MakeProxy(const char* proxyClassname, const char* macrofilename, const char* cutfilename, const char* option, Int_t maxUnrolling)
6443  GetPlayer();
6444  if (!fPlayer) return 0;
6445  return fPlayer->MakeProxy(proxyClassname,macrofilename,cutfilename,option,maxUnrolling);
6446 }
6447 
6448 ////////////////////////////////////////////////////////////////////////////////
6449 /// Generate skeleton selector class for this tree.
6450 ///
6451 /// The following files are produced: selector.h and selector.C.
6452 /// If selector is 0, the selector will be called "nameoftree".
6453 /// The option can be used to specify the branches that will have a data member.
6454 /// - If option is "=legacy", a pre-ROOT6 selector will be generated (data
6455 /// members and branch pointers instead of TTreeReaders).
6456 /// - If option is empty, readers will be generated for each leaf.
6457 /// - If option is "@", readers will be generated for the topmost branches.
6458 /// - Individual branches can also be picked by their name:
6459 /// - "X" generates readers for leaves of X.
6460 /// - "@X" generates a reader for X as a whole.
6461 /// - "@X;Y" generates a reader for X as a whole and also readers for the
6462 /// leaves of Y.
6463 /// - For further examples see the figure below.
6464 ///
6465 /// \image html ttree_makeselector_option_examples.png
6466 ///
6467 /// The generated code in selector.h includes the following:
6468 /// - Identification of the original Tree and Input file name
6469 /// - Definition of selector class (data and functions)
6470 /// - The following class functions:
6471 /// - constructor and destructor
6472 /// - void Begin(TTree *tree)
6473 /// - void SlaveBegin(TTree *tree)
6474 /// - void Init(TTree *tree)
6475 /// - Bool_t Notify()
6476 /// - Bool_t Process(Long64_t entry)
6477 /// - void Terminate()
6478 /// - void SlaveTerminate()
6479 ///
6480 /// The class selector derives from TSelector.
6481 /// The generated code in selector.C includes empty functions defined above.
6482 ///
6483 /// To use this function:
6484 ///
6485 /// - connect your Tree file (eg: `TFile f("myfile.root");`)
6486 /// - `T->MakeSelector("myselect");`
6487 ///
6488 /// where T is the name of the Tree in file myfile.root
6489 /// and myselect.h, myselect.C the name of the files created by this function.
6490 /// In a ROOT session, you can do:
6491 /// ~~~ {.cpp}
6492 /// root > T->Process("myselect.C")
6493 /// ~~~
6494 
6495 Int_t TTree::MakeSelector(const char* selector, Option_t* option)
6497  TString opt(option);
6498  if(opt.EqualTo("=legacy", TString::ECaseCompare::kIgnoreCase)) {
6499  return MakeClass(selector, "selector");
6500  } else {
6501  GetPlayer();
6502  if (!fPlayer) return 0;
6503  return fPlayer->MakeReader(selector, option);
6504  }
6505 }
6506 
6507 ////////////////////////////////////////////////////////////////////////////////
6508 /// Check if adding nbytes to memory we are still below MaxVirtualsize.
6509 
6512  if ((fTotalBuffers + nbytes) < fMaxVirtualSize) {
6513  return kFALSE;
6514  }
6515  return kTRUE;
6516 }
6517 
6518 ////////////////////////////////////////////////////////////////////////////////
6519 /// Static function merging the trees in the TList into a new tree.
6520 ///
6521 /// Trees in the list can be memory or disk-resident trees.
6522 /// The new tree is created in the current directory (memory if gROOT).
6523 
6524 TTree* TTree::MergeTrees(TList* li, Option_t* options)
6526  if (!li) return 0;
6527  TIter next(li);
6528  TTree *newtree = 0;
6529  TObject *obj;
6530 
6531  while ((obj=next())) {
6532  if (!obj->InheritsFrom(TTree::Class())) continue;
6533  TTree *tree = (TTree*)obj;
6534  Long64_t nentries = tree->GetEntries();
6535  if (nentries == 0) continue;
6536  if (!newtree) {
6537  newtree = (TTree*)tree->CloneTree();
6538  if (!newtree) continue;
6539 
6540  // Once the cloning is done, separate the trees,
6541  // to avoid as many side-effects as possible
6542  // The list of clones is guaranteed to exist since we
6543  // just cloned the tree.
6544  tree->GetListOfClones()->Remove(newtree);
6545  tree->ResetBranchAddresses();
6546  newtree->ResetBranchAddresses();
6547  continue;
6548  }
6549 
6550  newtree->CopyAddresses(tree);
6551 
6552  newtree->CopyEntries(tree,-1,options);
6553 
6554  tree->ResetBranchAddresses(); // Disconnect from new tree.
6555  }
6556  if (newtree && newtree->GetTreeIndex()) {
6557  newtree->GetTreeIndex()->Append(0,kFALSE); // Force the sorting
6558  }
6559  return newtree;
6560 }
6561 
6562 ////////////////////////////////////////////////////////////////////////////////
6563 /// Merge the trees in the TList into this tree.
6564 ///
6565 /// Returns the total number of entries in the merged tree.
6566 
6569  if (!li) return 0;
6570  Long64_t storeAutoSave = fAutoSave;
6571  // Disable the autosave as the TFileMerge keeps a list of key and deleting the underlying
6572  // key would invalidate its iteration (or require costly measure to not use the deleted keys).
6573  // Also since this is part of a merging operation, the output file is not as precious as in
6574  // the general case since the input file should still be around.
6575  fAutoSave = 0;
6576  TIter next(li);
6577  TTree *tree;
6578  while ((tree = (TTree*)next())) {
6579  if (tree==this) continue;
6580  if (!tree->InheritsFrom(TTree::Class())) {
6581  Error("Add","Attempt to add object of class: %s to a %s", tree->ClassName(), ClassName());
6582  fAutoSave = storeAutoSave;
6583  return -1;
6584  }
6585 
6586  Long64_t nentries = tree->GetEntries();
6587  if (nentries == 0) continue;
6588 
6589  CopyAddresses(tree);
6590 
6591  CopyEntries(tree,-1,options);
6592 
6593  tree->ResetBranchAddresses();
6594  }
6595  fAutoSave = storeAutoSave;
6596  return GetEntries();
6597 }
6598 
6599 ////////////////////////////////////////////////////////////////////////////////
6600 /// Merge the trees in the TList into this tree.
6601 /// If info->fIsFirst is true, first we clone this TTree info the directory
6602 /// info->fOutputDirectory and then overlay the new TTree information onto
6603 /// this TTree object (so that this TTree object is now the appropriate to
6604 /// use for further merging).
6605 ///
6606 /// Returns the total number of entries in the merged tree.
6607 
6610  const char *options = info ? info->fOptions.Data() : "";
6611  if (info && info->fIsFirst && info->fOutputDirectory && info->fOutputDirectory->GetFile() != GetCurrentFile()) {
6613  TIOFeatures saved_features = fIOFeatures;
6614  if (info->fIOFeatures) {
6615  fIOFeatures = *(info->fIOFeatures);
6616  }
6617  TTree *newtree = CloneTree(-1, options);
6618  fIOFeatures = saved_features;
6619  if (newtree) {
6620  newtree->Write();
6621  delete newtree;
6622  }
6623  // Make sure things are really written out to disk before attempting any reading.
6624  info->fOutputDirectory->GetFile()->Flush();
6625  info->fOutputDirectory->ReadTObject(this,this->GetName());
6626  }
6627  if (!li) return 0;
6628  Long64_t storeAutoSave = fAutoSave;
6629  // Disable the autosave as the TFileMerge keeps a list of key and deleting the underlying
6630  // key would invalidate its iteration (or require costly measure to not use the deleted keys).
6631  // Also since this is part of a merging operation, the output file is not as precious as in
6632  // the general case since the input file should still be around.
6633  fAutoSave = 0;
6634  TIter next(li);
6635  TTree *tree;
6636  while ((tree = (TTree*)next())) {
6637  if (tree==this) continue;
6638  if (!tree->InheritsFrom(TTree::Class())) {
6639  Error("Add","Attempt to add object of class: %s to a %s", tree->ClassName(), ClassName());
6640  fAutoSave = storeAutoSave;
6641  return -1;
6642  }
6643  // Copy MakeClass status.
6644  tree->SetMakeClass(fMakeClass);
6645 
6646  // Copy branch addresses.
6647  CopyAddresses(tree);
6648 
6649  CopyEntries(tree,-1,options);
6650 
6651  tree->ResetBranchAddresses();
6652  }
6653  fAutoSave = storeAutoSave;
6654  return GetEntries();
6655 }
6656 
6657 ////////////////////////////////////////////////////////////////////////////////
6658 /// Move a cache from a file to the current file in dir.
6659 /// if src is null no operation is done, if dir is null or there is no
6660 /// current file the cache is deleted.
6661 
6662 void TTree::MoveReadCache(TFile *src, TDirectory *dir)
6664  if (!src) return;
6665  TFile *dst = (dir && dir != gROOT) ? dir->GetFile() : 0;
6666  if (src == dst) return;
6667 
6668  TTreeCache *pf = GetReadCache(src);
6669  if (dst) {
6670  src->SetCacheRead(0,this);
6671  dst->SetCacheRead(pf, this);
6672  } else {
6673  if (pf) {
6674  pf->WaitFinishPrefetch();
6675  }
6676  src->SetCacheRead(0,this);
6677  delete pf;
6678  }
6679 }
6680 
6681 ////////////////////////////////////////////////////////////////////////////////
6682 /// Function called when loading a new class library.
6683 
6686  TIter next(GetListOfLeaves());
6687  TLeaf* leaf = 0;
6688  while ((leaf = (TLeaf*) next())) {
6689  leaf->Notify();
6690  leaf->GetBranch()->Notify();
6691  }
6692  return kTRUE;
6693 }
6694 
6695 ////////////////////////////////////////////////////////////////////////////////
6696 /// This function may be called after having filled some entries in a Tree.
6697 /// Using the information in the existing branch buffers, it will reassign
6698 /// new branch buffer sizes to optimize time and memory.
6699 ///
6700 /// The function computes the best values for branch buffer sizes such that
6701 /// the total buffer sizes is less than maxMemory and nearby entries written
6702 /// at the same time.
6703 /// In case the branch compression factor for the data written so far is less
6704 /// than compMin, the compression is disabled.
6705 ///
6706 /// if option ="d" an analysis report is printed.
6707 
6708 void TTree::OptimizeBaskets(ULong64_t maxMemory, Float_t minComp, Option_t *option)
6710  //Flush existing baskets if the file is writable
6711  if (this->GetDirectory()->IsWritable()) this->FlushBaskets();
6712 
6713  TString opt( option );
6714  opt.ToLower();
6715  Bool_t pDebug = opt.Contains("d");
6716  TObjArray *leaves = this->GetListOfLeaves();
6717  Int_t nleaves = leaves->GetEntries();
6718  Double_t treeSize = (Double_t)this->GetTotBytes();
6719 
6720  if (nleaves == 0 || treeSize == 0) {
6721  // We're being called too early, we really have nothing to do ...
6722  return;
6723  }
6724  Double_t aveSize = treeSize/nleaves;
6725  UInt_t bmin = 512;
6726  UInt_t bmax = 256000;
6727  Double_t memFactor = 1;
6728  Int_t i, oldMemsize,newMemsize,oldBaskets,newBaskets;
6729  i = oldMemsize = newMemsize = oldBaskets = newBaskets = 0;
6730 
6731  //we make two passes
6732  //one pass to compute the relative branch buffer sizes
6733  //a second pass to compute the absolute values
6734  for (Int_t pass =0;pass<2;pass++) {
6735  oldMemsize = 0; //to count size of baskets in memory with old buffer size
6736  newMemsize = 0; //to count size of baskets in memory with new buffer size
6737  oldBaskets = 0; //to count number of baskets with old buffer size
6738  newBaskets = 0; //to count number of baskets with new buffer size
6739  for (i=0;i<nleaves;i++) {
6740  TLeaf *leaf = (TLeaf*)leaves->At(i);
6741  TBranch *branch = leaf->GetBranch();
6742  Double_t totBytes = (Double_t)branch->GetTotBytes();
6743  Double_t idealFactor = totBytes/aveSize;
6744  UInt_t sizeOfOneEntry;
6745  if (branch->GetEntries() == 0) {
6746  // There is no data, so let's make a guess ...
6747  sizeOfOneEntry = aveSize;
6748  } else {
6749  sizeOfOneEntry = 1+(UInt_t)(totBytes / (Double_t)branch->GetEntries());
6750  }
6751  Int_t oldBsize = branch->GetBasketSize();
6752  oldMemsize += oldBsize;
6753  oldBaskets += 1+Int_t(totBytes/oldBsize);
6754  Int_t nb = branch->GetListOfBranches()->GetEntries();
6755  if (nb > 0) {
6756  newBaskets += 1+Int_t(totBytes/oldBsize);
6757  continue;
6758  }
6759  Double_t bsize = oldBsize*idealFactor*memFactor; //bsize can be very large !
6760  if (bsize < 0) bsize = bmax;
6761  if (bsize > bmax) bsize = bmax;
6762  UInt_t newBsize = UInt_t(bsize);
6763  if (pass) { // only on the second pass so that it doesn't interfere with scaling
6764  newBsize = newBsize + (branch->GetEntries() * sizeof(Int_t) * 2); // make room for meta data
6765  // We used ATLAS fully-split xAOD for testing, which is a rather unbalanced TTree, 10K branches,
6766  // with 8K having baskets smaller than 512 bytes. To achieve good I/O performance ATLAS uses auto-flush 100,
6767  // resulting in the smallest baskets being ~300-400 bytes, so this change increases their memory by about 8k*150B =~ 1MB,
6768  // at the same time it significantly reduces the number of total baskets because it ensures that all 100 entries can be
6769  // stored in a single basket (the old optimization tended to make baskets too small). In a toy example with fixed sized
6770  // structures we found a factor of 2 fewer baskets needed in the new scheme.
6771  // rounds up, increases basket size to ensure all entries fit into single basket as intended
6772  newBsize = newBsize - newBsize%512 + 512;
6773  }
6774  if (newBsize < sizeOfOneEntry) newBsize = sizeOfOneEntry;
6775  if (newBsize < bmin) newBsize = bmin;
6776  if (newBsize > 10000000) newBsize = bmax;
6777  if (pass) {
6778  if (pDebug) Info("OptimizeBaskets", "Changing buffer size from %6d to %6d bytes for %s\n",oldBsize,newBsize,branch->GetName());
6779  branch->SetBasketSize(newBsize);
6780  }
6781  newMemsize += newBsize;
6782  // For this number to be somewhat accurate when newBsize is 'low'
6783  // we do not include any space for meta data in the requested size (newBsize) even-though SetBasketSize will
6784  // not let it be lower than 100+TBranch::fEntryOffsetLen.
6785  newBaskets += 1+Int_t(totBytes/newBsize);
6786  if (pass == 0) continue;
6787  //Reset the compression level in case the compression factor is small
6788  Double_t comp = 1;
6789  if (branch->GetZipBytes() > 0) comp = totBytes/Double_t(branch->GetZipBytes());
6790  if (comp > 1 && comp < minComp) {
6791  if (pDebug) Info("OptimizeBaskets", "Disabling compression for branch : %s\n",branch->GetName());
6792  branch->SetCompressionSettings(0);
6793  }
6794  }
6795  // coverity[divide_by_zero] newMemsize can not be zero as there is at least one leaf
6796  memFactor = Double_t(maxMemory)/Double_t(newMemsize);
6797  if (memFactor > 100) memFactor = 100;
6798  Double_t bmin_new = bmin*memFactor;
6799  Double_t bmax_new = bmax*memFactor;
6800  static const UInt_t hardmax = 1*1024*1024*1024; // Really, really never give more than 1Gb to a single buffer.
6801 
6802  // Really, really never go lower than 8 bytes (we use this number
6803  // so that the calculation of the number of basket is consistent
6804  // but in fact SetBasketSize will not let the size go below
6805  // TBranch::fEntryOffsetLen + (100 + strlen(branch->GetName())
6806  // (The 2nd part being a slight over estimate of the key length.
6807  static const UInt_t hardmin = 8;
6808  bmin = (bmin_new > hardmax) ? hardmax : ( bmin_new < hardmin ? hardmin : (UInt_t)bmin_new );
6809  bmax = (bmax_new > hardmax) ? bmin : (UInt_t)bmax_new;
6810  }
6811  if (pDebug) {
6812  Info("OptimizeBaskets", "oldMemsize = %d, newMemsize = %d\n",oldMemsize, newMemsize);
6813  Info("OptimizeBaskets", "oldBaskets = %d, newBaskets = %d\n",oldBaskets, newBaskets);
6814  }
6815 }
6816 
6817 ////////////////////////////////////////////////////////////////////////////////
6818 /// Interface to the Principal Components Analysis class.
6819 ///
6820 /// Create an instance of TPrincipal
6821 ///
6822 /// Fill it with the selected variables
6823 ///
6824 /// - if option "n" is specified, the TPrincipal object is filled with
6825 /// normalized variables.
6826 /// - If option "p" is specified, compute the principal components
6827 /// - If option "p" and "d" print results of analysis
6828 /// - If option "p" and "h" generate standard histograms
6829 /// - If option "p" and "c" generate code of conversion functions
6830 /// - return a pointer to the TPrincipal object. It is the user responsibility
6831 /// - to delete this object.
6832 /// - The option default value is "np"
6833 ///
6834 /// see TTree::Draw for explanation of the other parameters.
6835 ///
6836 /// The created object is named "principal" and a reference to it
6837 /// is added to the list of specials Root objects.
6838 /// you can retrieve a pointer to the created object via:
6839 /// ~~~ {.cpp}
6840 /// TPrincipal *principal =
6841 /// (TPrincipal*)gROOT->GetListOfSpecials()->FindObject("principal");
6842 /// ~~~
6843 
6844 TPrincipal* TTree::Principal(const char* varexp, const char* selection, Option_t* option, Long64_t nentries, Long64_t firstentry)
6846  GetPlayer();
6847  if (fPlayer) {
6848  return fPlayer->Principal(varexp, selection, option, nentries, firstentry);
6849  }
6850  return 0;
6851 }
6852 
6853 ////////////////////////////////////////////////////////////////////////////////
6854 /// Print a summary of the tree contents.
6855 ///
6856 /// - If option contains "all" friend trees are also printed.
6857 /// - If option contains "toponly" only the top level branches are printed.
6858 /// - If option contains "clusters" information about the cluster of baskets is printed.
6859 ///
6860 /// Wildcarding can be used to print only a subset of the branches, e.g.,
6861 /// `T.Print("Elec*")` will print all branches with name starting with "Elec".
6862 
6863 void TTree::Print(Option_t* option) const
6865  // We already have been visited while recursively looking
6866  // through the friends tree, let's return.
6867  if (kPrint & fFriendLockStatus) {
6868  return;
6869  }
6870  Int_t s = 0;
6871  Int_t skey = 0;
6872  if (fDirectory) {
6873  TKey* key = fDirectory->GetKey(GetName());
6874  if (key) {
6875  skey = key->GetKeylen();
6876  s = key->GetNbytes();
6877  }
6878  }
6879  Long64_t total = skey;
6880  Long64_t zipBytes = GetZipBytes();
6881  if (zipBytes > 0) {
6882  total += GetTotBytes();
6883  }
6884  TBufferFile b(TBuffer::kWrite, 10000);
6885  TTree::Class()->WriteBuffer(b, (TTree*) this);
6886  total += b.Length();
6887  Long64_t file = zipBytes + s;
6888  Float_t cx = 1;
6889  if (zipBytes) {
6890  cx = (GetTotBytes() + 0.00001) / zipBytes;
6891  }
6892  Printf("******************************************************************************");
6893  Printf("*Tree :%-10s: %-54s *", GetName(), GetTitle());
6894  Printf("*Entries : %8lld : Total = %15lld bytes File Size = %10lld *", fEntries, total, file);
6895  Printf("* : : Tree compression factor = %6.2f *", cx);
6896  Printf("******************************************************************************");
6897 
6898  if (strncmp(option,"clusters",strlen("clusters"))==0) {
6899  Printf("%-16s %-16s %-16s %5s",
6900  "Cluster Range #", "Entry Start", "Last Entry", "Size");
6901  Int_t index= 0;
6902  Long64_t clusterRangeStart = 0;
6903  if (fNClusterRange) {
6904  for( ; index < fNClusterRange; ++index) {
6905  Printf("%-16d %-16lld %-16lld %5lld",
6906  index, clusterRangeStart, fClusterRangeEnd[index], fClusterSize[index]);
6907  clusterRangeStart = fClusterRangeEnd[index] + 1;
6908  }
6909  }
6910  Printf("%-16d %-16lld %-16lld %5lld",
6911  index, clusterRangeStart, fEntries - 1, fAutoFlush);
6912  return;
6913  }
6914 
6915  Int_t nl = const_cast<TTree*>(this)->GetListOfLeaves()->GetEntries();
6916  Int_t l;
6917  TBranch* br = 0;
6918  TLeaf* leaf = 0;
6919  if (strstr(option, "toponly")) {
6920  Long64_t *count = new Long64_t[nl];
6921  Int_t keep =0;
6922  for (l=0;l<nl;l++) {
6923  leaf = (TLeaf *)const_cast<TTree*>(this)->GetListOfLeaves()->At(l);
6924  br = leaf->GetBranch();
6925  if (strchr(br->GetName(),'.')) {
6926  count[l] = -1;
6927  count[keep] += br->GetZipBytes();
6928  } else {
6929  keep = l;
6930  count[keep] = br->GetZipBytes();
6931  }
6932  }
6933  for (l=0;l<nl;l++) {
6934  if (count[l] < 0) continue;
6935  leaf = (TLeaf *)const_cast<TTree*>(this)->GetListOfLeaves()->At(l);
6936  br = leaf->GetBranch();
6937  Printf("branch: %-20s %9lld\n",br->GetName(),count[l]);
6938  }
6939  delete [] count;
6940  } else {
6941  TString reg = "*";
6942  if (strlen(option) && strchr(option,'*')) reg = option;
6943  TRegexp re(reg,kTRUE);
6944  TIter next(const_cast<TTree*>(this)->GetListOfBranches());
6946  while ((br= (TBranch*)next())) {
6947  TString st = br->GetName();
6948  st.ReplaceAll("/","_");
6949  if (st.Index(re) == kNPOS) continue;
6950  br->Print(option);
6951  }
6952  }
6953 
6954  //print TRefTable (if one)
6955  if (fBranchRef) fBranchRef->Print(option);
6956 
6957  //print friends if option "all"
6958  if (!fFriends || !strstr(option,"all")) return;
6959  TIter nextf(fFriends);
6960  TFriendLock lock(const_cast<TTree*>(this),kPrint);
6961  TFriendElement *fr;
6962  while ((fr = (TFriendElement*)nextf())) {
6963  TTree * t = fr->GetTree();
6964  if (t) t->Print(option);
6965  }
6966 }
6967 
6968 ////////////////////////////////////////////////////////////////////////////////
6969 /// Print statistics about the TreeCache for this tree.
6970 /// Like:
6971 /// ~~~ {.cpp}
6972 /// ******TreeCache statistics for file: cms2.root ******
6973 /// Reading 73921562 bytes in 716 transactions
6974 /// Average transaction = 103.242405 Kbytes
6975 /// Number of blocks in current cache: 202, total size : 6001193
6976 /// ~~~
6977 /// if option = "a" the list of blocks in the cache is printed
6978 
6979 void TTree::PrintCacheStats(Option_t* option) const
6981  TFile *f = GetCurrentFile();
6982  if (!f) return;
6983  TTreeCache *tc = (TTreeCache*)f->GetCacheRead(const_cast<TTree*>(this));
6984  if (tc) tc->Print(option);
6985 }
6986 
6987 ////////////////////////////////////////////////////////////////////////////////
6988 /// Process this tree executing the TSelector code in the specified filename.
6989 /// The return value is -1 in case of error and TSelector::GetStatus() in
6990 /// in case of success.
6991 ///
6992 /// The code in filename is loaded (interpreted or compiled, see below),
6993 /// filename must contain a valid class implementation derived from TSelector,
6994 /// where TSelector has the following member functions:
6995 ///
6996 /// - `Begin()`: called every time a loop on the tree starts,
6997 /// a convenient place to create your histograms.
6998 /// - `SlaveBegin()`: called after Begin(), when on PROOF called only on the
6999 /// slave servers.
7000 /// - `Process()`: called for each event, in this function you decide what
7001 /// to read and fill your histograms.
7002 /// - `SlaveTerminate`: called at the end of the loop on the tree, when on PROOF
7003 /// called only on the slave servers.
7004 /// - `Terminate()`: called at the end of the loop on the tree,
7005 /// a convenient place to draw/fit your histograms.
7006 ///
7007 /// If filename is of the form file.C, the file will be interpreted.
7008 ///
7009 /// If filename is of the form file.C++, the file file.C will be compiled
7010 /// and dynamically loaded.
7011 ///
7012 /// If filename is of the form file.C+, the file file.C will be compiled
7013 /// and dynamically loaded. At next call, if file.C is older than file.o
7014 /// and file.so, the file.C is not compiled, only file.so is loaded.
7015 ///
7016 /// ## NOTE1
7017 ///
7018 /// It may be more interesting to invoke directly the other Process function
7019 /// accepting a TSelector* as argument.eg
7020 /// ~~~ {.cpp}
7021 /// MySelector *selector = (MySelector*)TSelector::GetSelector(filename);
7022 /// selector->CallSomeFunction(..);
7023 /// mytree.Process(selector,..);
7024 /// ~~~
7025 /// ## NOTE2
7026 //
7027 /// One should not call this function twice with the same selector file
7028 /// in the same script. If this is required, proceed as indicated in NOTE1,
7029 /// by getting a pointer to the corresponding TSelector,eg
7030 ///
7031 /// ### workaround 1
7032 /// ~~~ {.cpp}
7033 /// void stubs1() {
7034 /// TSelector *selector = TSelector::GetSelector("h1test.C");
7035 /// TFile *f1 = new TFile("stubs_nood_le1.root");
7036 /// TTree *h1 = (TTree*)f1->Get("h1");
7037 /// h1->Process(selector);
7038 /// TFile *f2 = new TFile("stubs_nood_le1_coarse.root");
7039 /// TTree *h2 = (TTree*)f2->Get("h1");
7040 /// h2->Process(selector);
7041 /// }
7042 /// ~~~
7043 /// or use ACLIC to compile the selector
7044 ///
7045 /// ### workaround 2
7046 /// ~~~ {.cpp}
7047 /// void stubs2() {
7048 /// TFile *f1 = new TFile("stubs_nood_le1.root");
7049 /// TTree *h1 = (TTree*)f1->Get("h1");
7050 /// h1->Process("h1test.C+");
7051 /// TFile *f2 = new TFile("stubs_nood_le1_coarse.root");
7052 /// TTree *h2 = (TTree*)f2->Get("h1");
7053 /// h2->Process("h1test.C+");
7054 /// }
7055 /// ~~~
7056 
7057 Long64_t TTree::Process(const char* filename, Option_t* option, Long64_t nentries, Long64_t firstentry)
7059  GetPlayer();
7060  if (fPlayer) {
7061  return fPlayer->Process(filename, option, nentries, firstentry);
7062  }
7063  return -1;
7064 }
7065 
7066 ////////////////////////////////////////////////////////////////////////////////
7067 /// Process this tree executing the code in the specified selector.
7068 /// The return value is -1 in case of error and TSelector::GetStatus() in
7069 /// in case of success.
7070 ///
7071 /// The TSelector class has the following member functions:
7072 ///
7073 /// - `Begin()`: called every time a loop on the tree starts,
7074 /// a convenient place to create your histograms.
7075 /// - `SlaveBegin()`: called after Begin(), when on PROOF called only on the
7076 /// slave servers.
7077 /// - `Process()`: called for each event, in this function you decide what
7078 /// to read and fill your histograms.
7079 /// - `SlaveTerminate`: called at the end of the loop on the tree, when on PROOF
7080 /// called only on the slave servers.
7081 /// - `Terminate()`: called at the end of the loop on the tree,
7082 /// a convenient place to draw/fit your histograms.
7083 ///
7084 /// If the Tree (Chain) has an associated EventList, the loop is on the nentries
7085 /// of the EventList, starting at firstentry, otherwise the loop is on the
7086 /// specified Tree entries.
7087 
7088 Long64_t TTree::Process(TSelector* selector, Option_t* option, Long64_t nentries, Long64_t firstentry)
7090  GetPlayer();
7091  if (fPlayer) {
7092  return fPlayer->Process(selector, option, nentries, firstentry);
7093  }
7094  return -1;
7095 }
7096 
7097 ////////////////////////////////////////////////////////////////////////////////
7098 /// Make a projection of a tree using selections.
7099 ///
7100 /// Depending on the value of varexp (described in Draw) a 1-D, 2-D, etc.,
7101 /// projection of the tree will be filled in histogram hname.
7102 /// Note that the dimension of hname must match with the dimension of varexp.
7103 ///
7104 
7105 Long64_t TTree::Project(const char* hname, const char* varexp, const char* selection, Option_t* option, Long64_t nentries, Long64_t firstentry)
7107  TString var;
7108  var.Form("%s>>%s", varexp, hname);
7109  TString opt("goff");
7110  if (option) {
7111  opt.Form("%sgoff", option);
7112  }
7113  Long64_t nsel = Draw(var, selection, opt, nentries, firstentry);
7114  return nsel;
7115 }
7116 
7117 ////////////////////////////////////////////////////////////////////////////////
7118 /// Loop over entries and return a TSQLResult object containing entries following selection.
7119 
7120 TSQLResult* TTree::Query(const char* varexp, const char* selection, Option_t* option, Long64_t nentries, Long64_t firstentry)
7122  GetPlayer();
7123  if (fPlayer) {
7124  return fPlayer->Query(varexp, selection, option, nentries, firstentry);
7125  }
7126  return 0;
7127 }
7128 
7129 ////////////////////////////////////////////////////////////////////////////////
7130 /// Create or simply read branches from filename.
7131 ///
7132 /// if branchDescriptor = "" (default), it is assumed that the Tree descriptor
7133 /// is given in the first line of the file with a syntax like
7134 /// ~~~ {.cpp}
7135 /// A/D:Table[2]/F:Ntracks/I:astring/C
7136 /// ~~~
7137 /// otherwise branchDescriptor must be specified with the above syntax.
7138 ///
7139 /// - If the type of the first variable is not specified, it is assumed to be "/F"
7140 /// - If the type of any other variable is not specified, the type of the previous
7141 /// variable is assumed. eg
7142 /// - `x:y:z` (all variables are assumed of type "F"
7143 /// - `x/D:y:z` (all variables are of type "D"
7144 /// - `x:y/D:z` (x is type "F", y and z of type "D"
7145 ///
7146 /// delimiter allows for the use of another delimiter besides whitespace.
7147 /// This provides support for direct import of common data file formats
7148 /// like csv. If delimiter != ' ' and branchDescriptor == "", then the
7149 /// branch description is taken from the first line in the file, but
7150 /// delimiter is used for the branch names tokenization rather than ':'.
7151 /// Note however that if the values in the first line do not use the
7152 /// /[type] syntax, all variables are assumed to be of type "F".
7153 /// If the filename ends with extensions .csv or .CSV and a delimiter is
7154 /// not specified (besides ' '), the delimiter is automatically set to ','.
7155 ///
7156 /// Lines in the input file starting with "#" are ignored. Leading whitespace
7157 /// for each column data is skipped. Empty lines are skipped.
7158 ///
7159 /// A TBranch object is created for each variable in the expression.
7160 /// The total number of rows read from the file is returned.
7161 ///
7162 /// ## FILLING a TTree WITH MULTIPLE INPUT TEXT FILES
7163 ///
7164 /// To fill a TTree with multiple input text files, proceed as indicated above
7165 /// for the first input file and omit the second argument for subsequent calls
7166 /// ~~~ {.cpp}
7167 /// T.ReadFile("file1.dat","branch descriptor");
7168 /// T.ReadFile("file2.dat");
7169 /// ~~~
7170 
7171 Long64_t TTree::ReadFile(const char* filename, const char* branchDescriptor, char delimiter)
7173  std::ifstream in;
7174  in.open(filename);
7175  if (!in.good()) {
7176  Error("ReadFile","Cannot open file: %s",filename);
7177  return 0;
7178  }
7179  const char* ext = strrchr(filename, '.');
7180  if(ext != NULL && ((strcmp(ext, ".csv") == 0) || (strcmp(ext, ".CSV") == 0)) && delimiter == ' ') {
7181  delimiter = ',';
7182  }
7183  return ReadStream(in, branchDescriptor, delimiter);
7184 }
7185 
7186 ////////////////////////////////////////////////////////////////////////////////
7187 /// Determine which newline this file is using.
7188 /// Return '\\r' for Windows '\\r\\n' as that already terminates.
7189 
7190 char TTree::GetNewlineValue(std::istream &inputStream)
7192  Long_t inPos = inputStream.tellg();
7193  char newline = '\n';
7194  while(1) {
7195  char c = 0;
7196  inputStream.get(c);
7197  if(!inputStream.good()) {
7198  Error("ReadStream","Error reading stream: no newline found.");
7199  return 0;
7200  }
7201  if(c == newline) break;
7202  if(c == '\r') {
7203  newline = '\r';
7204  break;
7205  }
7206  }
7207  inputStream.clear();
7208  inputStream.seekg(inPos);
7209  return newline;
7210 }
7211 
7212 ////////////////////////////////////////////////////////////////////////////////
7213 /// Create or simply read branches from an input stream.
7214 ///
7215 /// See reference information for TTree::ReadFile
7216 
7217 Long64_t TTree::ReadStream(std::istream& inputStream, const char *branchDescriptor, char delimiter)
7219  char newline = 0;
7220  std::stringstream ss;
7221  std::istream *inTemp;
7222  Long_t inPos = inputStream.tellg();
7223  if (!inputStream.good()) {
7224  Error("ReadStream","Error reading stream");
7225  return 0;
7226  }
7227  if (inPos == -1) {
7228  ss << std::cin.rdbuf();
7229  newline = GetNewlineValue(ss);
7230  inTemp = &ss;
7231  } else {
7232  newline = GetNewlineValue(inputStream);
7233  inTemp = &inputStream;
7234  }
7235  std::istream& in = *inTemp;
7236  Long64_t nlines = 0;
7237 
7238  TBranch *branch = 0;
7239  Int_t nbranches = fBranches.GetEntries();
7240  if (nbranches == 0) {
7241  char *bdname = new char[4000];
7242  char *bd = new char[100000];
7243  Int_t nch = 0;
7244  if (branchDescriptor) nch = strlen(branchDescriptor);
7245  // branch Descriptor is null, read its definition from the first line in the file
7246  if (!nch) {
7247  do {
7248  in.getline(bd, 100000, newline);
7249  if (!in.good()) {
7250  delete [] bdname;
7251  delete [] bd;
7252  Error("ReadStream","Error reading stream");
7253  return 0;
7254  }
7255  char *cursor = bd;
7256  while( isspace(*cursor) && *cursor != '\n' && *cursor != '\0') {
7257  ++cursor;
7258  }
7259  if (*cursor != '#' && *cursor != '\n' && *cursor != '\0') {
7260  break;
7261  }
7262  } while (true);
7263  ++nlines;
7264  nch = strlen(bd);
7265  } else {
7266  strlcpy(bd,branchDescriptor,100000);
7267  }
7268 
7269  //parse the branch descriptor and create a branch for each element
7270  //separated by ":"
7271  void *address = &bd[90000];
7272  char *bdcur = bd;
7273  TString desc="", olddesc="F";
7274  char bdelim = ':';
7275  if(delimiter != ' ') {
7276  bdelim = delimiter;
7277  if (strchr(bdcur,bdelim)==0 && strchr(bdcur,':') != 0) {
7278  // revert to the default
7279  bdelim = ':';
7280  }
7281  }
7282  while (bdcur) {
7283  char *colon = strchr(bdcur,bdelim);
7284  if (colon) *colon = 0;
7285  strlcpy(bdname,bdcur,4000);
7286  char *slash = strchr(bdname,'/');
7287  if (slash) {
7288  *slash = 0;
7289  desc = bdcur;
7290  olddesc = slash+1;
7291  } else {
7292  desc.Form("%s/%s",bdname,olddesc.Data());
7293  }
7294  char *bracket = strchr(bdname,'[');
7295  if (bracket) {
7296  *bracket = 0;
7297  }
7298  branch = new TBranch(this,bdname,address,desc.Data(),32000);
7299  if (branch->IsZombie()) {
7300  delete branch;
7301  Warning("ReadStream","Illegal branch definition: %s",bdcur);
7302  } else {
7303  fBranches.Add(branch);
7304  branch->SetAddress(0);
7305  }
7306  if (!colon)break;
7307  bdcur = colon+1;
7308  }
7309  delete [] bdname;
7310  delete [] bd;
7311  }
7312 
7313  nbranches = fBranches.GetEntries();
7314 
7315  if (gDebug > 1) {
7316  Info("ReadStream", "Will use branches:");
7317  for (int i = 0 ; i < nbranches; ++i) {
7318  TBranch* br = (TBranch*) fBranches.At(i);
7319  Info("ReadStream", " %s: %s [%s]", br->GetName(),
7320  br->GetTitle(), br->GetListOfLeaves()->At(0)->IsA()->GetName());
7321  }
7322  if (gDebug > 3) {
7323  Info("ReadStream", "Dumping read tokens, format:");
7324  Info("ReadStream", "LLLLL:BBB:gfbe:GFBE:T");
7325  Info("ReadStream", " L: line number");
7326  Info("ReadStream", " B: branch number");
7327  Info("ReadStream", " gfbe: good / fail / bad / eof of token");
7328  Info("ReadStream", " GFBE: good / fail / bad / eof of file");
7329  Info("ReadStream", " T: Token being read");
7330  }
7331  }
7332 
7333  //loop on all lines in the file
7334  Long64_t nGoodLines = 0;
7335  std::string line;
7336  const char sDelimBuf[2] = { delimiter, 0 };
7337  const char* sDelim = sDelimBuf;
7338  if (delimiter == ' ') {
7339  // ' ' really means whitespace
7340  sDelim = "[ \t]";
7341  }
7342  while(in.good()) {
7343  if (newline == '\r' && in.peek() == '\n') {
7344  // Windows, skip '\n':
7345  in.get();
7346  }
7347  std::getline(in, line, newline);
7348  ++nlines;
7349 
7350  TString sLine(line);
7351  sLine = sLine.Strip(TString::kLeading); // skip leading whitespace
7352  if (sLine.IsNull()) {
7353  if (gDebug > 2) {
7354  Info("ReadStream", "Skipping empty line number %lld", nlines);
7355  }
7356  continue; // silently skip empty lines
7357  }
7358  if (sLine[0] == '#') {
7359  if (gDebug > 2) {
7360  Info("ReadStream", "Skipping comment line number %lld: '%s'",
7361  nlines, line.c_str());
7362  }
7363  continue;
7364  }
7365  if (gDebug > 2) {
7366  Info("ReadStream", "Parsing line number %lld: '%s'",
7367  nlines, line.c_str());
7368  }
7369 
7370  // Loop on branches and read the branch values into their buffer
7371  branch = 0;
7372  TString tok; // one column's data
7373  TString leafData; // leaf data, possibly multiple tokens for e.g. /I[2]
7374  std::stringstream sToken; // string stream feeding leafData into leaves
7375  Ssiz_t pos = 0;
7376  Int_t iBranch = 0;
7377  Bool_t goodLine = kTRUE; // whether the row can be filled into the tree
7378  Int_t remainingLeafLen = 0; // remaining columns for the current leaf
7379  while (goodLine && iBranch < nbranches
7380  && sLine.Tokenize(tok, pos, sDelim)) {
7381  tok = tok.Strip(TString::kLeading); // skip leading whitespace
7382  if (tok.IsNull() && delimiter == ' ') {
7383  // 1 2 should not be interpreted as 1,,,2 but 1, 2.
7384  // Thus continue until we have a non-empty token.
7385  continue;
7386  }
7387 
7388  if (!remainingLeafLen) {
7389  // next branch!
7390  branch = (TBranch*)fBranches.At(iBranch);
7391  }
7392  TLeaf *leaf = (TLeaf*)branch->GetListOfLeaves()->At(0);
7393  if (!remainingLeafLen) {
7394  remainingLeafLen = leaf->GetLen();
7395  if (leaf->GetMaximum() > 0) {
7396  // This is a dynamic leaf length, i.e. most likely a TLeafC's
7397  // string size. This still translates into one token:
7398  remainingLeafLen = 1;
7399  }
7400 
7401  leafData = tok;
7402  } else {
7403  // append token to laf data:
7404  leafData += " ";
7405  leafData += tok;
7406  }
7407  --remainingLeafLen;
7408  if (remainingLeafLen) {
7409  // need more columns for this branch:
7410  continue;
7411  }
7412  ++iBranch;
7413 
7414  // initialize stringstream with token
7415  sToken.clear();
7416  sToken.seekp(0, std::ios_base::beg);
7417  sToken.str(leafData.Data());
7418  sToken.seekg(0, std::ios_base::beg);
7419  leaf->ReadValue(sToken, 0 /* 0 = "all" */);
7420  if (gDebug > 3) {
7421  Info("ReadStream", "%5lld:%3d:%d%d%d%d:%d%d%d%d:%s",
7422  nlines, iBranch,
7423  (int)sToken.good(), (int)sToken.fail(),
7424  (int)sToken.bad(), (int)sToken.eof(),
7425  (int)in.good(), (int)in.fail(),
7426  (int)in.bad(), (int)in.eof(),
7427  sToken.str().c_str());
7428  }
7429 
7430  // Error handling
7431  if (sToken.bad()) {
7432  // How could that happen for a stringstream?
7433  Warning("ReadStream",
7434  "Buffer error while reading data for branch %s on line %lld",
7435  branch->GetName(), nlines);
7436  } else if (!sToken.eof()) {
7437  if (sToken.fail()) {
7438  Warning("ReadStream",
7439  "Couldn't read formatted data in \"%s\" for branch %s on line %lld; ignoring line",
7440  tok.Data(), branch->GetName(), nlines);
7441  goodLine = kFALSE;
7442  } else {
7443  std::string remainder;
7444  std::getline(sToken, remainder, newline);
7445  if (!remainder.empty()) {
7446  Warning("ReadStream",
7447  "Ignoring trailing \"%s\" while reading data for branch %s on line %lld",
7448  remainder.c_str(), branch->GetName(), nlines);
7449  }
7450  }
7451  }
7452  } // tokenizer loop
7453 
7454  if (iBranch < nbranches) {
7455  Warning("ReadStream",
7456  "Read too few columns (%d < %d) in line %lld; ignoring line",
7457  iBranch, nbranches, nlines);
7458  goodLine = kFALSE;
7459  } else if (pos != kNPOS) {
7460  sLine = sLine.Strip(TString::kTrailing);
7461  if (pos < sLine.Length()) {
7462  Warning("ReadStream",
7463  "Ignoring trailing \"%s\" while reading line %lld",
7464  sLine.Data() + pos - 1 /* also print delimiter */,
7465  nlines);
7466  }
7467  }
7468 
7469  //we are now ready to fill the tree
7470  if (goodLine) {
7471  Fill();
7472  ++nGoodLines;
7473  }
7474  }
7475 
7476  return nGoodLines;
7477 }
7478 
7479 ////////////////////////////////////////////////////////////////////////////////
7480 /// Make sure that obj (which is being deleted or will soon be) is no
7481 /// longer referenced by this TTree.
7482 
7485  if (obj == fEventList) {
7486  fEventList = 0;
7487  }
7488  if (obj == fEntryList) {
7489  fEntryList = 0;
7490  }
7491  if (fUserInfo) {
7492  fUserInfo->RecursiveRemove(obj);
7493  }
7494  if (fPlayer == obj) {
7495  fPlayer = 0;
7496  }
7497  if (fTreeIndex == obj) {
7498  fTreeIndex = 0;
7499  }
7500  if (fAliases) {
7501  fAliases->RecursiveRemove(obj);
7502  }
7503  if (fFriends) {
7504  fFriends->RecursiveRemove(obj);
7505  }
7506 }
7507 
7508 ////////////////////////////////////////////////////////////////////////////////
7509 /// Refresh contents of this tree and its branches from the current status on disk.
7510 ///
7511 /// One can call this function in case the tree file is being
7512 /// updated by another process.
7513 
7514 void TTree::Refresh()
7516  if (!fDirectory->GetFile()) {
7517  return;
7518  }
7519  fDirectory->ReadKeys();
7520  fDirectory->Remove(this);
7521  TTree* tree; fDirectory->GetObject(GetName(),tree);
7522  if (!tree) {
7523  return;
7524  }
7525  //copy info from tree header into this Tree
7526  fEntries = 0;
7527  fNClusterRange = 0;
7528  ImportClusterRanges(tree);
7529 
7530  fAutoSave = tree->fAutoSave;
7531  fEntries = tree->fEntries;
7532  fTotBytes = tree->GetTotBytes();
7533  fZipBytes = tree->GetZipBytes();
7534  fSavedBytes = tree->fSavedBytes;
7535  fTotalBuffers = tree->fTotalBuffers.load();
7536 
7537  //loop on all branches and update them
7538  Int_t nleaves = fLeaves.GetEntriesFast();
7539  for (Int_t i = 0; i < nleaves; i++) {
7540  TLeaf* leaf = (TLeaf*) fLeaves.UncheckedAt(i);
7541  TBranch* branch = (TBranch*) leaf->GetBranch();
7542  branch->Refresh(tree->GetBranch(branch->GetName()));
7543  }
7544  fDirectory->Remove(tree);
7545  fDirectory->Append(this);
7546  delete tree;
7547  tree = 0;
7548 }
7549 
7550 ////////////////////////////////////////////////////////////////////////////////
7551 /// Remove a friend from the list of friends.
7552 
7553 void TTree::RemoveFriend(TTree* oldFriend)
7555  // We already have been visited while recursively looking
7556  // through the friends tree, let return
7558  return;
7559  }
7560  if (!fFriends) {
7561  return;
7562  }
7563  TFriendLock lock(this, kRemoveFriend);
7564  TIter nextf(fFriends);
7565  TFriendElement* fe = 0;
7566  while ((fe = (TFriendElement*) nextf())) {
7567  TTree* friend_t = fe->GetTree();
7568  if (friend_t == oldFriend) {
7569  fFriends->Remove(fe);
7570  delete fe;
7571  fe = 0;
7572  }
7573  }
7574 }
7575 
7576 ////////////////////////////////////////////////////////////////////////////////
7577 /// Reset baskets, buffers and entries count in all branches and leaves.
7578 
7579 void TTree::Reset(Option_t* option)
7581  fNotify = 0;
7582  fEntries = 0;
7583  fNClusterRange = 0;
7584  fTotBytes = 0;
7585  fZipBytes = 0;
7586  fFlushedBytes = 0;
7587  fSavedBytes = 0;
7588  fTotalBuffers = 0;
7589  fChainOffset = 0;
7590  fReadEntry = -1;
7591 
7592  delete fTreeIndex;
7593  fTreeIndex = 0;
7594 
7596  for (Int_t i = 0; i < nb; ++i) {
7597  TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
7598  branch->Reset(option);
7599  }
7600 
7601  if (fBranchRef) {
7602  fBranchRef->Reset();
7603  }
7604 }
7605 
7606 ////////////////////////////////////////////////////////////////////////////////
7607 /// Resets the state of this TTree after a merge (keep the customization but
7608 /// forget the data).
7609 
7612  fEntries = 0;
7613  fNClusterRange = 0;
7614  fTotBytes = 0;
7615  fZipBytes = 0;
7616  fSavedBytes = 0;
7617  fFlushedBytes = 0;
7618  fTotalBuffers = 0;
7619  fChainOffset = 0;
7620  fReadEntry = -1;
7621 
7622  delete fTreeIndex;
7623  fTreeIndex = 0;
7624 
7626  for (Int_t i = 0; i < nb; ++i) {
7627  TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
7628  branch->ResetAfterMerge(info);
7629  }
7630 
7631  if (fBranchRef) {
7632  fBranchRef->ResetAfterMerge(info);
7633  }
7634 }
7635 
7636 ////////////////////////////////////////////////////////////////////////////////
7637 /// Tell all of our branches to set their addresses to zero.
7638 ///
7639 /// Note: If any of our branches own any objects, they are deleted.
7640 
7643  if (br && br->GetTree()) {
7644  br->ResetAddress();
7645  }
7646 }
7647 
7648 ////////////////////////////////////////////////////////////////////////////////
7649 /// Tell all of our branches to drop their current objects and allocate new ones.
7650 
7654  Int_t nbranches = branches->GetEntriesFast();
7655  for (Int_t i = 0; i < nbranches; ++i) {
7656  TBranch* branch = (TBranch*) branches->UncheckedAt(i);
7657  branch->ResetAddress();
7658  }
7659 }
7660 
7661 ////////////////////////////////////////////////////////////////////////////////
7662 /// Loop over tree entries and print entries passing selection.
7663 ///
7664 /// - If varexp is 0 (or "") then print only first 8 columns.
7665 /// - If varexp = "*" print all columns.
7666 ///
7667 /// Otherwise a columns selection can be made using "var1:var2:var3".
7668 /// See TTreePlayer::Scan for more information
7669 
7670 Long64_t TTree::Scan(const char* varexp, const char* selection, Option_t* option, Long64_t nentries, Long64_t firstentry)
7672  GetPlayer();
7673  if (fPlayer) {
7674  return fPlayer->Scan(varexp, selection, option, nentries, firstentry);
7675  }
7676  return -1;
7677 }
7678 
7679 ////////////////////////////////////////////////////////////////////////////////
7680 /// Set a tree variable alias.
7681 ///
7682 /// Set an alias for an expression/formula based on the tree 'variables'.
7683 ///
7684 /// The content of 'aliasName' can be used in TTreeFormula (i.e. TTree::Draw,
7685 /// TTree::Scan, TTreeViewer) and will be evaluated as the content of
7686 /// 'aliasFormula'.
7687 ///
7688 /// If the content of 'aliasFormula' only contains symbol names, periods and
7689 /// array index specification (for example event.fTracks[3]), then
7690 /// the content of 'aliasName' can be used as the start of symbol.
7691 ///
7692 /// If the alias 'aliasName' already existed, it is replaced by the new
7693 /// value.
7694 ///
7695 /// When being used, the alias can be preceded by an eventual 'Friend Alias'
7696 /// (see TTree::GetFriendAlias)
7697 ///
7698 /// Return true if it was added properly.
7699 ///
7700 /// For example:
7701 /// ~~~ {.cpp}
7702 /// tree->SetAlias("x1","(tdc1[1]-tdc1[0])/49");
7703 /// tree->SetAlias("y1","(tdc1[3]-tdc1[2])/47");
7704 /// tree->SetAlias("x2","(tdc2[1]-tdc2[0])/49");
7705 /// tree->SetAlias("y2","(tdc2[3]-tdc2[2])/47");
7706 /// tree->Draw("y2-y1:x2-x1");
7707 ///
7708 /// tree->SetAlias("theGoodTrack","event.fTracks[3]");
7709 /// tree->Draw("theGoodTrack.fPx"); // same as "event.fTracks[3].fPx"
7710 /// ~~~
7711 
7712 Bool_t TTree::SetAlias(const char* aliasName, const char* aliasFormula)
7714  if (!aliasName || !aliasFormula) {
7715  return kFALSE;
7716  }
7717  if (!aliasName[0] || !aliasFormula[0]) {
7718  return kFALSE;
7719  }
7720  if (!fAliases) {
7721  fAliases = new TList;
7722  } else {
7723  TNamed* oldHolder = (TNamed*) fAliases->FindObject(aliasName);
7724  if (oldHolder) {
7725  oldHolder->SetTitle(aliasFormula);
7726  return kTRUE;
7727  }
7728  }
7729  TNamed* holder = new TNamed(aliasName, aliasFormula);
7730  fAliases->Add(holder);
7731  return kTRUE;
7732 }
7733 
7734 ////////////////////////////////////////////////////////////////////////////////
7735 /// This function may be called at the start of a program to change
7736 /// the default value for fAutoFlush.
7737 ///
7738 /// ### CASE 1 : autof > 0
7739 ///
7740 /// autof is the number of consecutive entries after which TTree::Fill will
7741 /// flush all branch buffers to disk.
7742 ///
7743 /// ### CASE 2 : autof < 0
7744 ///
7745 /// When filling the Tree the branch buffers will be flushed to disk when
7746 /// more than autof bytes have been written to the file. At the first FlushBaskets
7747 /// TTree::Fill will replace fAutoFlush by the current value of fEntries.
7748 ///
7749 /// Calling this function with autof<0 is interesting when it is hard to estimate
7750 /// the size of one entry. This value is also independent of the Tree.
7751 ///
7752 /// The Tree is initialized with fAutoFlush=-30000000, ie that, by default,
7753 /// the first AutoFlush will be done when 30 MBytes of data are written to the file.
7754 ///
7755 /// ### CASE 3 : autof = 0
7756 ///
7757 /// The AutoFlush mechanism is disabled.
7758 ///
7759 /// Flushing the buffers at regular intervals optimize the location of
7760 /// consecutive entries on the disk by creating clusters of baskets.
7761 ///
7762 /// A cluster of baskets is a set of baskets that contains all
7763 /// the data for a (consecutive) set of entries and that is stored
7764 /// consecutively on the disk. When reading all the branches, this
7765 /// is the minimum set of baskets that the TTreeCache will read.
7766 
7767 void TTree::SetAutoFlush(Long64_t autof /* = -30000000 */ )
7769  // Implementation note:
7770  //
7771  // A positive value of autoflush determines the size (in number of entries) of
7772  // a cluster of baskets.
7773  //
7774  // If the value of autoflush is changed over time (this happens in
7775  // particular when the TTree results from fast merging many trees),
7776  // we record the values of fAutoFlush in the data members:
7777  // fClusterRangeEnd and fClusterSize.
7778  // In the code we refer to a range of entries where the size of the
7779  // cluster of baskets is the same (i.e the value of AutoFlush was
7780  // constant) is called a ClusterRange.
7781  //
7782  // The 2 arrays (fClusterRangeEnd and fClusterSize) have fNClusterRange
7783  // active (used) values and have fMaxClusterRange allocated entries.
7784  //
7785  // fClusterRangeEnd contains the last entries number of a cluster range.
7786  // In particular this means that the 'next' cluster starts at fClusterRangeEnd[]+1
7787  // fClusterSize contains the size in number of entries of all the cluster
7788  // within the given range.
7789  // The last range (and the only one if fNClusterRange is zero) start at
7790  // fNClusterRange[fNClusterRange-1]+1 and ends at the end of the TTree. The
7791  // size of the cluster in this range is given by the value of fAutoFlush.
7792  //
7793  // For example printing the beginning and end of each the ranges can be done by:
7794  //
7795  // Printf("%-16s %-16s %-16s %5s",
7796  // "Cluster Range #", "Entry Start", "Last Entry", "Size");
7797  // Int_t index= 0;
7798  // Long64_t clusterRangeStart = 0;
7799  // if (fNClusterRange) {
7800  // for( ; index < fNClusterRange; ++index) {
7801  // Printf("%-16d %-16lld %-16lld %5lld",
7802  // index, clusterRangeStart, fClusterRangeEnd[index], fClusterSize[index]);
7803  // clusterRangeStart = fClusterRangeEnd[index] + 1;
7804  // }
7805  // }
7806  // Printf("%-16d %-16lld %-16lld %5lld",
7807  // index, prevEntry, fEntries - 1, fAutoFlush);
7808  //
7809 
7810  // Note: We store the entry number corresponding to the end of the cluster
7811  // rather than its start in order to avoid using the array if the cluster
7812  // size never varies (If there is only one value of AutoFlush for the whole TTree).
7813 
7814  if( fAutoFlush != autof) {
7815  if (fAutoFlush > 0 || autof > 0) {
7816  // The mechanism was already enabled, let's record the previous
7817  // cluster if needed.
7818  if (fFlushedBytes && fEntries) {
7819  if ( (fNClusterRange+1) > fMaxClusterRange ) {
7820  if (fMaxClusterRange) {
7821  Int_t newsize = TMath::Max(10,Int_t(2*fMaxClusterRange));
7823  newsize*sizeof(Long64_t),fMaxClusterRange*sizeof(Long64_t));
7825  newsize*sizeof(Long64_t),fMaxClusterRange*sizeof(Long64_t));
7826  fMaxClusterRange = newsize;
7827  } else {
7828  fMaxClusterRange = 2;
7831  }
7832  }
7835  ++fNClusterRange;
7836  }
7837  }
7838  fAutoFlush = autof;
7839  }
7840 }
7841 
7842 ////////////////////////////////////////////////////////////////////////////////
7843 /// This function may be called at the start of a program to change
7844 /// the default value for fAutoSave (and for SetAutoSave) is -300000000, ie 300 MBytes.
7845 /// When filling the Tree the branch buffers as well as the Tree header
7846 /// will be flushed to disk when the watermark is reached.
7847 /// If fAutoSave is positive the watermark is reached when a multiple of fAutoSave
7848 /// entries have been written.
7849 /// If fAutoSave is negative the watermark is reached when -fAutoSave bytes
7850 /// have been written to the file.
7851 /// In case of a program crash, it will be possible to recover the data in the Tree
7852 /// up to the last AutoSave point.
7853 
7854 void TTree::SetAutoSave(Long64_t autos)
7856  fAutoSave = autos;
7857 }
7858 
7859 ////////////////////////////////////////////////////////////////////////////////
7860 /// Set a branch's basket size.
7861 ///
7862 /// bname is the name of a branch.
7863 ///
7864 /// - if bname="*", apply to all branches.
7865 /// - if bname="xxx*", apply to all branches with name starting with xxx
7866 ///
7867 /// see TRegexp for wildcarding options
7868 /// buffsize = branc basket size
7869 
7870 void TTree::SetBasketSize(const char* bname, Int_t buffsize)
7872  Int_t nleaves = fLeaves.GetEntriesFast();
7873  TRegexp re(bname, kTRUE);
7874  Int_t nb = 0;
7875  for (Int_t i = 0; i < nleaves; i++) {
7876  TLeaf* leaf = (TLeaf*) fLeaves.UncheckedAt(i);
7877  TBranch* branch = (TBranch*) leaf->GetBranch();
7878  TString s = branch->GetName();
7879  if (strcmp(bname, branch->GetName()) && (s.Index(re) == kNPOS)) {
7880  continue;
7881  }
7882  nb++;
7883  branch->SetBasketSize(buffsize);
7884  }
7885  if (!nb) {
7886  Error("SetBasketSize", "unknown branch -> '%s'", bname);
7887  }
7888 }
7889 
7890 ////////////////////////////////////////////////////////////////////////////////
7891 /// Change branch address, dealing with clone trees properly.
7892 /// See TTree::CheckBranchAddressType for the semantic of the return value.
7893 ///
7894 /// Note: See the comments in TBranchElement::SetAddress() for the
7895 /// meaning of the addr parameter and the object ownership policy.
7896 
7897 Int_t TTree::SetBranchAddress(const char* bname, void* addr, TBranch** ptr)
7899  TBranch* branch = GetBranch(bname);
7900  if (!branch) {
7901  if (ptr) *ptr = 0;
7902  Error("SetBranchAddress", "unknown branch -> %s", bname);
7903  return kMissingBranch;
7904  }
7905  return SetBranchAddressImp(branch,addr,ptr);
7906 }
7907 
7908 ////////////////////////////////////////////////////////////////////////////////
7909 /// Verify the validity of the type of addr before calling SetBranchAddress.
7910 /// See TTree::CheckBranchAddressType for the semantic of the return value.
7911 ///
7912 /// Note: See the comments in TBranchElement::SetAddress() for the
7913 /// meaning of the addr parameter and the object ownership policy.
7914 
7915 Int_t TTree::SetBranchAddress(const char* bname, void* addr, TClass* ptrClass, EDataType datatype, Bool_t isptr)
7917  return SetBranchAddress(bname, addr, 0, ptrClass, datatype, isptr);
7918 }
7919 
7920 ////////////////////////////////////////////////////////////////////////////////
7921 /// Verify the validity of the type of addr before calling SetBranchAddress.
7922 /// See TTree::CheckBranchAddressType for the semantic of the return value.
7923 ///
7924 /// Note: See the comments in TBranchElement::SetAddress() for the
7925 /// meaning of the addr parameter and the object ownership policy.
7926 
7927 Int_t TTree::SetBranchAddress(const char* bname, void* addr, TBranch** ptr, TClass* ptrClass, EDataType datatype, Bool_t isptr)
7929  TBranch* branch = GetBranch(bname);
7930  if (!branch) {
7931  if (ptr) *ptr = 0;
7932  Error("SetBranchAddress", "unknown branch -> %s", bname);
7933  return kMissingBranch;
7934  }
7935 
7936  Int_t res = CheckBranchAddressType(branch, ptrClass, datatype, isptr);
7937  // This will set the value of *ptr to branch.
7938  if (res >= 0) {
7939  // The check succeeded.
7940  SetBranchAddressImp(branch,addr,ptr);
7941  } else {
7942  if (ptr) *ptr = 0;
7943  }
7944  return res;
7945 }
7946 
7947 ////////////////////////////////////////////////////////////////////////////////
7948 /// Change branch address, dealing with clone trees properly.
7949 /// See TTree::CheckBranchAddressType for the semantic of the return value.
7950 ///
7951 /// Note: See the comments in TBranchElement::SetAddress() for the
7952 /// meaning of the addr parameter and the object ownership policy.
7953 
7954 Int_t TTree::SetBranchAddressImp(TBranch *branch, void* addr, TBranch** ptr)
7956  if (ptr) {
7957  *ptr = branch;
7958  }
7959  if (fClones) {
7960  void* oldAddr = branch->GetAddress();
7961  TIter next(fClones);
7962  TTree* clone = 0;
7963  const char *bname = branch->GetName();
7964  while ((clone = (TTree*) next())) {
7965  TBranch* cloneBr = clone->GetBranch(bname);
7966  if (cloneBr && (cloneBr->GetAddress() == oldAddr)) {
7967  cloneBr->SetAddress(addr);
7968  }
7969  }
7970  }
7971  branch->SetAddress(addr);
7972  return kVoidPtr;
7973 }
7974 
7975 ////////////////////////////////////////////////////////////////////////////////
7976 /// Set branch status to Process or DoNotProcess.
7977 ///
7978 /// When reading a Tree, by default, all branches are read.
7979 /// One can speed up considerably the analysis phase by activating
7980 /// only the branches that hold variables involved in a query.
7981 ///
7982 /// bname is the name of a branch.
7983 ///
7984 /// - if bname="*", apply to all branches.
7985 /// - if bname="xxx*", apply to all branches with name starting with xxx
7986 ///
7987 /// see TRegexp for wildcarding options
7988 ///
7989 /// - status = 1 branch will be processed
7990 /// - = 0 branch will not be processed
7991 ///
7992 /// Example:
7993 ///
7994 /// Assume a tree T with sub-branches a,b,c,d,e,f,g,etc..
7995 /// when doing T.GetEntry(i) all branches are read for entry i.
7996 /// to read only the branches c and e, one can do
7997 /// ~~~ {.cpp}
7998 /// T.SetBranchStatus("*",0); //disable all branches
7999 /// T.SetBranchStatus("c",1);
8000 /// T.setBranchStatus("e",1);
8001 /// T.GetEntry(i);
8002 /// ~~~
8003 /// bname is interpreted as a wildcarded TRegexp (see TRegexp::MakeWildcard).
8004 /// Thus, "a*b" or "a.*b" matches branches starting with "a" and ending with
8005 /// "b", but not any other branch with an "a" followed at some point by a
8006 /// "b". For this second behavior, use "*a*b*". Note that TRegExp does not
8007 /// support '|', and so you cannot select, e.g. track and shower branches
8008 /// with "track|shower".
8009 ///
8010 /// __WARNING! WARNING! WARNING!__
8011 ///
8012 /// SetBranchStatus is matching the branch based on match of the branch
8013 /// 'name' and not on the branch hierarchy! In order to be able to
8014 /// selectively enable a top level object that is 'split' you need to make
8015 /// sure the name of the top level branch is prefixed to the sub-branches'
8016 /// name (by adding a dot ('.') at the end of the Branch creation and use the
8017 /// corresponding bname.
8018 ///
8019 /// I.e If your Tree has been created in split mode with a parent branch "parent."
8020 /// (note the trailing dot).
8021 /// ~~~ {.cpp}
8022 /// T.SetBranchStatus("parent",1);
8023 /// ~~~
8024 /// will not activate the sub-branches of "parent". You should do:
8025 /// ~~~ {.cpp}
8026 /// T.SetBranchStatus("parent*",1);
8027 /// ~~~
8028 /// Without the trailing dot in the branch creation you have no choice but to
8029 /// call SetBranchStatus explicitly for each of the sub branches.
8030 ///
8031 /// An alternative to this function is to read directly and only
8032 /// the interesting branches. Example:
8033 /// ~~~ {.cpp}
8034 /// TBranch *brc = T.GetBranch("c");
8035 /// TBranch *bre = T.GetBranch("e");
8036 /// brc->GetEntry(i);
8037 /// bre->GetEntry(i);
8038 /// ~~~
8039 /// If found is not 0, the number of branch(es) found matching the regular
8040 /// expression is returned in *found AND the error message 'unknown branch'
8041 /// is suppressed.
8042 
8043 void TTree::SetBranchStatus(const char* bname, Bool_t status, UInt_t* found)
8045  // We already have been visited while recursively looking
8046  // through the friends tree, let return
8048  return;
8049  }
8050 
8051  TBranch *branch, *bcount, *bson;
8052  TLeaf *leaf, *leafcount;
8053 
8054  Int_t i,j;
8055  Int_t nleaves = fLeaves.GetEntriesFast();
8056  TRegexp re(bname,kTRUE);
8057  Int_t nb = 0;
8058 
8059  // first pass, loop on all branches
8060  // for leafcount branches activate/deactivate in function of status
8061  for (i=0;i<nleaves;i++) {
8062  leaf = (TLeaf*)fLeaves.UncheckedAt(i);
8063  branch = (TBranch*)leaf->GetBranch();
8064  TString s = branch->GetName();
8065  if (strcmp(bname,"*")) { //Regexp gives wrong result for [] in name
8066  TString longname;
8067  longname.Form("%s.%s",GetName(),branch->GetName());
8068  if (strcmp(bname,branch->GetName())
8069  && longname != bname
8070  && s.Index(re) == kNPOS) continue;
8071  }
8072  nb++;
8073  if (status) branch->ResetBit(kDoNotProcess);
8074  else branch->SetBit(kDoNotProcess);
8075  leafcount = leaf->GetLeafCount();
8076  if (leafcount) {
8077  bcount = leafcount->GetBranch();
8078  if (status) bcount->ResetBit(kDoNotProcess);
8079  else bcount->SetBit(kDoNotProcess);
8080  }
8081  }
8082  if (nb==0 && strchr(bname,'*')==0) {
8083  branch = GetBranch(bname);
8084  if (branch) {
8085  if (status) branch->ResetBit(kDoNotProcess);
8086  else branch->SetBit(kDoNotProcess);
8087  ++nb;
8088  }
8089  }
8090 
8091  //search in list of friends
8092  UInt_t foundInFriend = 0;
8093  if (fFriends) {
8094  TFriendLock lock(this,kSetBranchStatus);
8095  TIter nextf(fFriends);
8096  TFriendElement *fe;
8097  TString name;
8098  while ((fe = (TFriendElement*)nextf())) {
8099  TTree *t = fe->GetTree();
8100  if (t==0) continue;
8101 
8102  // If the alias is present replace it with the real name.
8103  char *subbranch = (char*)strstr(bname,fe->GetName());
8104  if (subbranch!=bname) subbranch = 0;
8105  if (subbranch) {
8106  subbranch += strlen(fe->GetName());
8107  if ( *subbranch != '.' ) subbranch = 0;
8108  else subbranch ++;
8109  }
8110  if (subbranch) {
8111  name.Form("%s.%s",t->GetName(),subbranch);
8112  } else {
8113  name = bname;
8114  }
8115  t->SetBranchStatus(name,status, &foundInFriend);
8116  }
8117  }
8118  if (!nb && !foundInFriend) {
8119  if (found==0) {
8120  if (status) {
8121  if (strchr(bname,'*') != 0)
8122  Error("SetBranchStatus", "No branch name is matching wildcard -> %s", bname);
8123  else
8124  Error("SetBranchStatus", "unknown branch -> %s", bname);
8125  } else {
8126  if (strchr(bname,'*') != 0)
8127  Warning("SetBranchStatus", "No branch name is matching wildcard -> %s", bname);
8128  else
8129  Warning("SetBranchStatus", "unknown branch -> %s", bname);
8130  }
8131  }
8132  return;
8133  }
8134  if (found) *found = nb + foundInFriend;
8135 
8136  // second pass, loop again on all branches
8137  // activate leafcount branches for active branches only
8138  for (i = 0; i < nleaves; i++) {
8139  leaf = (TLeaf*)fLeaves.UncheckedAt(i);
8140  branch = (TBranch*)leaf->GetBranch();
8141  if (!branch->TestBit(kDoNotProcess)) {
8142  leafcount = leaf->GetLeafCount();
8143  if (leafcount) {
8144  bcount = leafcount->GetBranch();
8145  bcount->ResetBit(kDoNotProcess);
8146  }
8147  } else {
8148  //Int_t nbranches = branch->GetListOfBranches()->GetEntriesFast();
8149  Int_t nbranches = branch->GetListOfBranches()->GetEntries();
8150  for (j=0;j<nbranches;j++) {
8151  bson = (TBranch*)branch->GetListOfBranches()->UncheckedAt(j);
8152  if (!bson) continue;
8153  if (!bson->TestBit(kDoNotProcess)) {
8154  if (bson->GetNleaves() <= 0) continue;
8155  branch->ResetBit(kDoNotProcess);
8156  break;
8157  }
8158  }
8159  }
8160  }
8161 }
8162 
8163 ////////////////////////////////////////////////////////////////////////////////
8164 /// Set the current branch style. (static function)
8165 ///
8166 /// - style = 0 old Branch
8167 /// - style = 1 new Bronch
8168 
8171  fgBranchStyle = style;
8172 }
8173 
8174 ////////////////////////////////////////////////////////////////////////////////
8175 /// Set maximum size of the file cache .
8176 //
8177 /// - if cachesize = 0 the existing cache (if any) is deleted.
8178 /// - if cachesize = -1 (default) it is set to the AutoFlush value when writing
8179 /// the Tree (default is 30 MBytes).
8180 ///
8181 /// Returns:
8182 /// - 0 size set, cache was created if possible
8183 /// - -1 on error
8184 
8187  // remember that the user has requested an explicit cache setup
8188  fCacheUserSet = kTRUE;
8189 
8190  return SetCacheSizeAux(kFALSE, cacheSize);
8191 }
8192 
8193 ////////////////////////////////////////////////////////////////////////////////
8194 /// Set the size of the file cache and create it if possible.
8195 ///
8196 /// If autocache is true:
8197 /// this may be an autocreated cache, possibly enlarging an existing
8198 /// autocreated cache. The size is calculated. The value passed in cacheSize:
8199 /// - cacheSize = 0 make cache if default cache creation is enabled
8200 /// - cacheSize = -1 make a default sized cache in any case
8201 ///
8202 /// If autocache is false:
8203 /// this is a user requested cache. cacheSize is used to size the cache.
8204 /// This cache should never be automatically adjusted.
8205 ///
8206 /// Returns:
8207 /// - 0 size set, or existing autosized cache almost large enough.
8208 /// (cache was created if possible)
8209 /// - -1 on error
8210 
8211 Int_t TTree::SetCacheSizeAux(Bool_t autocache /* = kTRUE */, Long64_t cacheSize /* = 0 */ )
8213  if (autocache) {
8214  // used as a once only control for automatic cache setup
8216  }
8217 
8218  if (!autocache) {
8219  // negative size means the user requests the default
8220  if (cacheSize < 0) {
8221  cacheSize = GetCacheAutoSize(kTRUE);
8222  }
8223  } else {
8224  if (cacheSize == 0) {
8225  cacheSize = GetCacheAutoSize();
8226  } else if (cacheSize < 0) {
8227  cacheSize = GetCacheAutoSize(kTRUE);
8228  }
8229  }
8230 
8231  TFile* file = GetCurrentFile();
8232  if (!file || GetTree() != this) {
8233  // if there's no file or we are not a plain tree (e.g. if we're a TChain)
8234  // do not create a cache, only record the size if one was given
8235  if (!autocache) {
8236  fCacheSize = cacheSize;
8237  }
8238  if (GetTree() != this) {
8239  return 0;
8240  }
8241  if (!autocache && cacheSize>0) {
8242  Warning("SetCacheSizeAux", "A TTreeCache could not be created because the TTree has no file");
8243  }
8244  return 0;
8245  }
8246 
8247  // Check for an existing cache
8248  TTreeCache* pf = GetReadCache(file);
8249  if (pf) {
8250  if (autocache) {
8251  // reset our cache status tracking in case existing cache was added
8252  // by the user without using one of the TTree methods
8253  fCacheSize = pf->GetBufferSize();
8254  fCacheUserSet = !pf->IsAutoCreated();
8255 
8256  if (fCacheUserSet) {
8257  // existing cache was created by the user, don't change it
8258  return 0;
8259  }
8260  } else {
8261  // update the cache to ensure it records the user has explicitly
8262  // requested it
8263  pf->SetAutoCreated(kFALSE);
8264  }
8265 
8266  // if we're using an automatically calculated size and the existing
8267  // cache is already almost large enough don't resize
8268  if (autocache && Long64_t(0.80*cacheSize) < fCacheSize) {
8269  // already large enough
8270  return 0;
8271  }
8272 
8273  if (cacheSize == fCacheSize) {
8274  return 0;
8275  }
8276 
8277  if (cacheSize == 0) {
8278  // delete existing cache
8279  pf->WaitFinishPrefetch();
8280  file->SetCacheRead(0,this);
8281  delete pf;
8282  pf = 0;
8283  } else {
8284  // resize
8285  Int_t res = pf->SetBufferSize(cacheSize);
8286  if (res < 0) {
8287  return -1;
8288  }
8289  }
8290  } else {
8291  // no existing cache
8292  if (autocache) {
8293  if (fCacheUserSet) {
8294  // value was already set manually.
8295  if (fCacheSize == 0) return 0;
8296  // Expected a cache should exist; perhaps the user moved it
8297  // Do nothing more here.
8298  if (cacheSize) {
8299  Error("SetCacheSizeAux", "Not setting up an automatically sized TTreeCache because of missing cache previously set");
8300  }
8301  return -1;
8302  }
8303  }
8304  }
8305 
8306  fCacheSize = cacheSize;
8307  if (cacheSize == 0 || pf) {
8308  return 0;
8309  }
8310 
8312  pf = new TTreeCacheUnzip(this, cacheSize);
8313  else
8314  pf = new TTreeCache(this, cacheSize);
8315 
8316  pf->SetAutoCreated(autocache);
8317 
8318  return 0;
8319 }
8320 
8321 ////////////////////////////////////////////////////////////////////////////////
8322 ///interface to TTreeCache to set the cache entry range
8323 ///
8324 /// Returns:
8325 /// - 0 entry range set
8326 /// - -1 on error
8327 
8330  if (!GetTree()) {
8331  if (LoadTree(0)<0) {
8332  Error("SetCacheEntryRange","Could not load a tree");
8333  return -1;
8334  }
8335  }
8336  if (GetTree()) {
8337  if (GetTree() != this) {
8338  return GetTree()->SetCacheEntryRange(first, last);
8339  }
8340  } else {
8341  Error("SetCacheEntryRange", "No tree is available. Could not set cache entry range");
8342  return -1;
8343  }
8344 
8345  TFile *f = GetCurrentFile();
8346  if (!f) {
8347  Error("SetCacheEntryRange", "No file is available. Could not set cache entry range");
8348  return -1;
8349  }
8350  TTreeCache *tc = GetReadCache(f,kTRUE);
8351  if (!tc) {
8352  Error("SetCacheEntryRange", "No cache is available. Could not set entry range");
8353  return -1;
8354  }
8355  tc->SetEntryRange(first,last);
8356  return 0;
8357 }
8358 
8359 ////////////////////////////////////////////////////////////////////////////////
8360 /// Interface to TTreeCache to set the number of entries for the learning phase
8361 
8365 }
8366 
8367 ////////////////////////////////////////////////////////////////////////////////
8368 /// Enable/Disable circularity for this tree.
8369 ///
8370 /// if maxEntries > 0 a maximum of maxEntries is kept in one buffer/basket
8371 /// per branch in memory.
8372 /// Note that when this function is called (maxEntries>0) the Tree
8373 /// must be empty or having only one basket per branch.
8374 /// if maxEntries <= 0 the tree circularity is disabled.
8375 ///
8376 /// #### NOTE 1:
8377 /// Circular Trees are interesting in online real time environments
8378 /// to store the results of the last maxEntries events.
8379 /// #### NOTE 2:
8380 /// Calling SetCircular with maxEntries <= 0 is necessary before
8381 /// merging circular Trees that have been saved on files.
8382 /// #### NOTE 3:
8383 /// SetCircular with maxEntries <= 0 is automatically called
8384 /// by TChain::Merge
8385 /// #### NOTE 4:
8386 /// A circular Tree can still be saved in a file. When read back,
8387 /// it is still a circular Tree and can be filled again.
8388 
8389 void TTree::SetCircular(Long64_t maxEntries)
8391  if (maxEntries <= 0) {
8392  // Disable circularity.
8393  fMaxEntries = 1000000000;
8394  fMaxEntries *= 1000;
8396  //in case the Tree was originally created in gROOT, the branch
8397  //compression level was set to -1. If the Tree is now associated to
8398  //a file, reset the compression level to the file compression level
8399  if (fDirectory) {
8400  TFile* bfile = fDirectory->GetFile();
8401  Int_t compress = 1;
8402  if (bfile) {
8403  compress = bfile->GetCompressionSettings();
8404  }
8406  for (Int_t i = 0; i < nb; i++) {
8407  TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
8408  branch->SetCompressionSettings(compress);
8409  }
8410  }
8411  } else {
8412  // Enable circularity.
8413  fMaxEntries = maxEntries;
8414  SetBit(kCircular);
8415  }
8416 }
8417 
8418 ////////////////////////////////////////////////////////////////////////////////
8419 /// Set the debug level and the debug range.
8420 ///
8421 /// For entries in the debug range, the functions TBranchElement::Fill
8422 /// and TBranchElement::GetEntry will print the number of bytes filled
8423 /// or read for each branch.
8424 
8425 void TTree::SetDebug(Int_t level, Long64_t min, Long64_t max)
8427  fDebug = level;
8428  fDebugMin = min;
8429  fDebugMax = max;
8430 }
8431 
8432 ////////////////////////////////////////////////////////////////////////////////
8433 /// Update the default value for the branch's fEntryOffsetLen.
8434 /// If updateExisting is true, also update all the existing branches.
8435 /// If newdefault is less than 10, the new default value will be 10.
8436 
8437 void TTree::SetDefaultEntryOffsetLen(Int_t newdefault, Bool_t updateExisting)
8439  if (newdefault < 10) {
8440  newdefault = 10;
8441  }
8442  fDefaultEntryOffsetLen = newdefault;
8443  if (updateExisting) {
8444  TIter next( GetListOfBranches() );
8445  TBranch *b;
8446  while ( ( b = (TBranch*)next() ) ) {
8447  b->SetEntryOffsetLen( newdefault, kTRUE );
8448  }
8449  if (fBranchRef) {
8450  fBranchRef->SetEntryOffsetLen( newdefault, kTRUE );
8451  }
8452  }
8453 }
8454 
8455 ////////////////////////////////////////////////////////////////////////////////
8456 /// Change the tree's directory.
8457 ///
8458 /// Remove reference to this tree from current directory and
8459 /// add reference to new directory dir. The dir parameter can
8460 /// be 0 in which case the tree does not belong to any directory.
8461 ///
8462 
8465  if (fDirectory == dir) {
8466  return;
8467  }
8468  if (fDirectory) {
8469  fDirectory->Remove(this);
8470 
8471  // Delete or move the file cache if it points to this Tree
8472  TFile *file = fDirectory->GetFile();
8473  MoveReadCache(file,dir);
8474  }
8475  fDirectory = dir;
8476  if (fDirectory) {
8477  fDirectory->Append(this);
8478  }
8479  TFile* file = 0;
8480  if (fDirectory) {
8481  file = fDirectory->GetFile();
8482  }
8483  if (fBranchRef) {
8484  fBranchRef->SetFile(file);
8485  }
8486  TBranch* b = 0;
8487  TIter next(GetListOfBranches());
8488  while((b = (TBranch*) next())) {
8489  b->SetFile(file);
8490  }
8491 }
8492 
8493 ////////////////////////////////////////////////////////////////////////////////
8494 /// Change number of entries in the tree.
8495 ///
8496 /// If n >= 0, set number of entries in the tree = n.
8497 ///
8498 /// If n < 0, set number of entries in the tree to match the
8499 /// number of entries in each branch. (default for n is -1)
8500 ///
8501 /// This function should be called only when one fills each branch
8502 /// independently via TBranch::Fill without calling TTree::Fill.
8503 /// Calling TTree::SetEntries() make sense only if the number of entries
8504 /// in each branch is identical, a warning is issued otherwise.
8505 /// The function returns the number of entries.
8506 ///
8507 
8510  // case 1 : force number of entries to n
8511  if (n >= 0) {
8512  fEntries = n;
8513  return n;
8514  }
8515 
8516  // case 2; compute the number of entries from the number of entries in the branches
8517  TBranch* b(nullptr), *bMin(nullptr), *bMax(nullptr);
8518  Long64_t nMin = kMaxEntries;
8519  Long64_t nMax = 0;
8520  TIter next(GetListOfBranches());
8521  while((b = (TBranch*) next())){
8522  Long64_t n2 = b->GetEntries();
8523  if (!bMin || n2 < nMin) {
8524  nMin = n2;
8525  bMin = b;
8526  }
8527  if (!bMax || n2 > nMax) {
8528  nMax = n2;
8529  bMax = b;
8530  }
8531  }
8532  if (bMin && nMin != nMax) {
8533  Warning("SetEntries", "Tree branches have different numbers of entries, eg %s has %lld entries while %s has %lld entries.",
8534  bMin->GetName(), nMin, bMax->GetName(), nMax);
8535  }
8536  fEntries = nMax;
8537  return fEntries;
8538 }
8539 
8540 ////////////////////////////////////////////////////////////////////////////////
8541 /// Set an EntryList
8542 
8543 void TTree::SetEntryList(TEntryList *enlist, Option_t * /*opt*/)
8545  if (fEntryList) {
8546  //check if the previous entry list is owned by the tree
8547  if (fEntryList->TestBit(kCanDelete)){
8548  delete fEntryList;
8549  }
8550  }
8551  fEventList = 0;
8552  if (!enlist) {
8553  fEntryList = 0;
8554  return;
8555  }
8556  fEntryList = enlist;
8557  fEntryList->SetTree(this);
8558 
8559 }
8560 
8561 ////////////////////////////////////////////////////////////////////////////////
8562 /// This function transfroms the given TEventList into a TEntryList
8563 /// The new TEntryList is owned by the TTree and gets deleted when the tree
8564 /// is deleted. This TEntryList can be returned by GetEntryList() function.
8565 
8566 void TTree::SetEventList(TEventList *evlist)
8568  fEventList = evlist;
8569  if (fEntryList){
8570  if (fEntryList->TestBit(kCanDelete)) {
8571  TEntryList *tmp = fEntryList;
8572  fEntryList = 0; // Avoid problem with RecursiveRemove.
8573  delete tmp;
8574  } else {
8575  fEntryList = 0;
8576  }
8577  }
8578 
8579  if (!evlist) {
8580  fEntryList = 0;
8581  fEventList = 0;
8582  return;
8583  }
8584 
8585  fEventList = evlist;
8586  char enlistname[100];
8587  snprintf(enlistname,100, "%s_%s", evlist->GetName(), "entrylist");
8588  fEntryList = new TEntryList(enlistname, evlist->GetTitle());
8589  fEntryList->SetDirectory(0); // We own this.
8590  Int_t nsel = evlist->GetN();
8591  fEntryList->SetTree(this);
8592  Long64_t entry;
8593  for (Int_t i=0; i<nsel; i++){
8594  entry = evlist->GetEntry(i);
8595  fEntryList->Enter(entry);
8596  }
8597  fEntryList->SetReapplyCut(evlist->GetReapplyCut());
8598  fEntryList->SetBit(kCanDelete, kTRUE);
8599 }
8600 
8601 ////////////////////////////////////////////////////////////////////////////////
8602 /// Set number of entries to estimate variable limits.
8603 /// If n is -1, the estimate is set to be the current maximum
8604 /// for the tree (i.e. GetEntries() + 1)
8605 /// If n is less than -1, the behavior is undefined.
8606 
8607 void TTree::SetEstimate(Long64_t n /* = 1000000 */)
8609  if (n == 0) {
8610  n = 10000;
8611  } else if (n < 0) {
8612  n = fEntries - n;
8613  }
8614  fEstimate = n;
8615  GetPlayer();
8616  if (fPlayer) {
8617  fPlayer->SetEstimate(n);
8618  }
8619 }
8620 
8621 ////////////////////////////////////////////////////////////////////////////////
8622 /// Provide the end-user with the ability to enable/disable various experimental
8623 /// IO features for this TTree.
8624 ///
8625 /// Returns all the newly-set IO settings.
8626 
8629  // Purposely ignore all unsupported bits; TIOFeatures implementation already warned the user about the
8630  // error of their ways; this is just a safety check.
8631  UChar_t featuresRequested = features.GetFeatures() & static_cast<UChar_t>(TBasket::EIOBits::kSupported);
8632 
8633  UChar_t curFeatures = fIOFeatures.GetFeatures();
8634  UChar_t newFeatures = ~curFeatures & featuresRequested;
8635  curFeatures |= newFeatures;
8636  fIOFeatures.Set(curFeatures);
8637 
8638  ROOT::TIOFeatures newSettings(newFeatures);
8639  return newSettings;
8640 }
8641 
8642 ////////////////////////////////////////////////////////////////////////////////
8643 /// Set fFileNumber to number.
8644 /// fFileNumber is used by TTree::Fill to set the file name
8645 /// for a new file to be created when the current file exceeds fgTreeMaxSize.
8646 /// (see TTree::ChangeFile)
8647 /// if fFileNumber=10, the new file name will have a suffix "_11",
8648 /// ie, fFileNumber is incremented before setting the file name
8649 
8650 void TTree::SetFileNumber(Int_t number)
8652  if (fFileNumber < 0) {
8653  Warning("SetFileNumber", "file number must be positive. Set to 0");
8654  fFileNumber = 0;
8655  return;
8656  }
8657  fFileNumber = number;
8658 }
8659 
8660 ////////////////////////////////////////////////////////////////////////////////
8661 /// Set all the branches in this TTree to be in decomposed object mode
8662 /// (also known as MakeClass mode).
8663 
8666  fMakeClass = make;
8667 
8669  for (Int_t i = 0; i < nb; ++i) {
8670  TBranch* branch = (TBranch*) fBranches.UncheckedAt(i);
8671  branch->SetMakeClass(make);
8672  }
8673 }
8674 
8675 ////////////////////////////////////////////////////////////////////////////////
8676 /// Set the maximum size in bytes of a Tree file (static function).
8677 /// The default size is 100000000000LL, ie 100 Gigabytes.
8678 ///
8679 /// In TTree::Fill, when the file has a size > fgMaxTreeSize,
8680 /// the function closes the current file and starts writing into
8681 /// a new file with a name of the style "file_1.root" if the original
8682 /// requested file name was "file.root".
8683 
8684 void TTree::SetMaxTreeSize(Long64_t maxsize)
8686  fgMaxTreeSize = maxsize;
8687 }
8688 
8689 ////////////////////////////////////////////////////////////////////////////////
8690 /// Change the name of this tree.
8691 
8692 void TTree::SetName(const char* name)
8694  if (gPad) {
8695  gPad->Modified();
8696  }
8697  // Trees are named objects in a THashList.
8698  // We must update hashlists if we change the name.
8699  TFile *file = 0;
8700  TTreeCache *pf = 0;
8701  if (fDirectory) {
8702  fDirectory->Remove(this);
8703  if ((file = GetCurrentFile())) {
8704  pf = GetReadCache(file);
8705  file->SetCacheRead(0,this,TFile::kDoNotDisconnect);
8706  }
8707  }
8708  // This changes our hash value.
8709  fName = name;
8710  if (fDirectory) {
8711  fDirectory->Append(this);
8712  if (pf) {
8713  file->SetCacheRead(pf,this,TFile::kDoNotDisconnect);
8714  }
8715  }
8716 }
8717 
8718 ////////////////////////////////////////////////////////////////////////////////
8719 /// Change the name and title of this tree.
8720 
8721 void TTree::SetObject(const char* name, const char* title)
8723  if (gPad) {
8724  gPad->Modified();
8725  }
8726 
8727  // Trees are named objects in a THashList.
8728  // We must update hashlists if we change the name
8729  TFile *file = 0;
8730  TTreeCache *pf = 0;
8731  if (fDirectory) {
8732  fDirectory->Remove(this);
8733  if ((file = GetCurrentFile())) {
8734  pf = GetReadCache(file);
8735  file->SetCacheRead(0,this,TFile::kDoNotDisconnect);
8736  }
8737  }
8738  // This changes our hash value.
8739  fName = name;
8740  fTitle = title;
8741  if (fDirectory) {
8742  fDirectory->Append(this);
8743  if (pf) {
8744  file->SetCacheRead(pf,this,TFile::kDoNotDisconnect);
8745  }
8746  }
8747 }
8748 
8749 ////////////////////////////////////////////////////////////////////////////////
8750 /// Enable or disable parallel unzipping of Tree buffers.
8751 
8752 void TTree::SetParallelUnzip(Bool_t opt, Float_t RelSize)
8756 
8757  if (RelSize > 0) {
8759  }
8760 
8761 }
8762 
8763 ////////////////////////////////////////////////////////////////////////////////
8764 /// Set perf stats
8765 
8768  fPerfStats = perf;
8769 }
8770 
8771 ////////////////////////////////////////////////////////////////////////////////
8772 /// The current TreeIndex is replaced by the new index.
8773 /// Note that this function does not delete the previous index.
8774 /// This gives the possibility to play with more than one index, e.g.,
8775 /// ~~~ {.cpp}
8776 /// TVirtualIndex* oldIndex = tree.GetTreeIndex();
8777 /// tree.SetTreeIndex(newIndex);
8778 /// tree.Draw();
8779 /// tree.SetTreeIndex(oldIndex);
8780 /// tree.Draw(); etc
8781 /// ~~~
8782 
8785  if (fTreeIndex) {
8786  fTreeIndex->SetTree(0);
8787  }
8788  fTreeIndex = index;
8789 }
8790 
8791 ////////////////////////////////////////////////////////////////////////////////
8792 /// Set tree weight.
8793 ///
8794 /// The weight is used by TTree::Draw to automatically weight each
8795 /// selected entry in the resulting histogram.
8796 ///
8797 /// For example the equivalent of:
8798 /// ~~~ {.cpp}
8799 /// T.Draw("x", "w")
8800 /// ~~~
8801 /// is:
8802 /// ~~~ {.cpp}
8803 /// T.SetWeight(w);
8804 /// T.Draw("x");
8805 /// ~~~
8806 /// This function is redefined by TChain::SetWeight. In case of a
8807 /// TChain, an option "global" may be specified to set the same weight
8808 /// for all trees in the TChain instead of the default behaviour
8809 /// using the weights of each tree in the chain (see TChain::SetWeight).
8810 
8813  fWeight = w;
8814 }
8815 
8816 ////////////////////////////////////////////////////////////////////////////////
8817 /// Print values of all active leaves for entry.
8818 ///
8819 /// - if entry==-1, print current entry (default)
8820 /// - if a leaf is an array, a maximum of lenmax elements is printed.
8821 
8822 void TTree::Show(Long64_t entry, Int_t lenmax)
8824  if (entry != -1) {
8825  Int_t ret = LoadTree(entry);
8826  if (ret == -2) {
8827  Error("Show()", "Cannot read entry %lld (entry does not exist)", entry);
8828  return;
8829  } else if (ret == -1) {
8830  Error("Show()", "Cannot read entry %lld (I/O error)", entry);
8831  return;
8832  }
8833  ret = GetEntry(entry);
8834  if (ret == -1) {
8835  Error("Show()", "Cannot read entry %lld (I/O error)", entry);
8836  return;
8837  } else if (ret == 0) {
8838  Error("Show()", "Cannot read entry %lld (no data read)", entry);
8839  return;
8840  }
8841  }
8842  printf("======> EVENT:%lld\n", fReadEntry);
8843  TObjArray* leaves = GetListOfLeaves();
8844  Int_t nleaves = leaves->GetEntriesFast();
8845  Int_t ltype;
8846  for (Int_t i = 0; i < nleaves; i++) {
8847  TLeaf* leaf = (TLeaf*) leaves->UncheckedAt(i);
8848  TBranch* branch = leaf->GetBranch();
8849  if (branch->TestBit(kDoNotProcess)) {
8850  continue;
8851  }
8852  Int_t len = leaf->GetLen();
8853  if (len <= 0) {
8854  continue;
8855  }
8856  len = TMath::Min(len, lenmax);
8857  if (leaf->IsA() == TLeafElement::Class()) {
8858  leaf->PrintValue(lenmax);
8859  continue;
8860  }
8861  if (branch->GetListOfBranches()->GetEntriesFast() > 0) {
8862  continue;
8863  }
8864  ltype = 10;
8865  if (leaf->IsA() == TLeafF::Class()) {
8866  ltype = 5;
8867  }
8868  if (leaf->IsA() == TLeafD::Class()) {
8869  ltype = 5;
8870  }
8871  if (leaf->IsA() == TLeafC::Class()) {
8872  len = 1;
8873  ltype = 5;
8874  };
8875  printf(" %-15s = ", leaf->GetName());
8876  for (Int_t l = 0; l < len; l++) {
8877  leaf->PrintValue(l);
8878  if (l == (len - 1)) {
8879  printf("\n");
8880  continue;
8881  }
8882  printf(", ");
8883  if ((l % ltype) == 0) {
8884  printf("\n ");
8885  }
8886  }
8887  }
8888 }
8889 
8890 ////////////////////////////////////////////////////////////////////////////////
8891 /// Start the TTreeViewer on this tree.
8892 ///
8893 /// - ww is the width of the canvas in pixels
8894 /// - wh is the height of the canvas in pixels
8895 
8896 void TTree::StartViewer()
8898  GetPlayer();
8899  if (fPlayer) {
8900  fPlayer->StartViewer(600, 400);
8901  }
8902 }
8903 
8904 ////////////////////////////////////////////////////////////////////////////////
8905 /// Stop the cache learning phase
8906 ///
8907 /// Returns:
8908 /// - 0 learning phase stopped or not active
8909 /// - -1 on error
8910 
8913  if (!GetTree()) {
8914  if (LoadTree(0)<0) {
8915  Error("StopCacheLearningPhase","Could not load a tree");
8916  return -1;
8917  }
8918  }
8919  if (GetTree()) {
8920  if (GetTree() != this) {
8921  return GetTree()->StopCacheLearningPhase();
8922  }
8923  } else {
8924  Error("StopCacheLearningPhase", "No tree is available. Could not stop cache learning phase");
8925  return -1;
8926  }
8927 
8928  TFile *f = GetCurrentFile();
8929  if (!f) {
8930  Error("StopCacheLearningPhase", "No file is available. Could not stop cache learning phase");
8931  return -1;
8932  }
8933  TTreeCache *tc = GetReadCache(f,kTRUE);
8934  if (!tc) {
8935  Error("StopCacheLearningPhase", "No cache is available. Could not stop learning phase");
8936  return -1;
8937  }
8938  tc->StopLearningPhase();
8939  return 0;
8940 }
8941 
8942 ////////////////////////////////////////////////////////////////////////////////
8943 /// Set the fTree member for all branches and sub branches.
8944 
8945 static void TBranch__SetTree(TTree *tree, TObjArray &branches)
8947  Int_t nb = branches.GetEntriesFast();
8948  for (Int_t i = 0; i < nb; ++i) {
8949  TBranch* br = (TBranch*) branches.UncheckedAt(i);
8950  br->SetTree(tree);
8951 
8952  Int_t nBaskets = br->GetListOfBaskets()->GetEntries();
8953  Int_t writeBasket = br->GetWriteBasket();
8954  for (Int_t j=writeBasket,n=0;j>=0 && n<nBaskets;--j) {
8955  TBasket *bk = (TBasket*)br->GetListOfBaskets()->UncheckedAt(j);
8956  if (bk) {
8957  tree->IncrementTotalBuffers(bk->GetBufferSize());
8958  ++n;
8959  }
8960  }
8961 
8962  TBranch__SetTree(tree,*br->GetListOfBranches());
8963  }
8964 }
8965 
8966 ////////////////////////////////////////////////////////////////////////////////
8967 /// Set the fTree member for all friend elements.
8968 
8969 void TFriendElement__SetTree(TTree *tree, TList *frlist)
8971  if (frlist) {
8972  TObjLink *lnk = frlist->FirstLink();
8973  while (lnk) {
8974  TFriendElement *elem = (TFriendElement*)lnk->GetObject();
8975  elem->fParentTree = tree;
8976  lnk = lnk->Next();
8977  }
8978  }
8979 }
8980 
8981 ////////////////////////////////////////////////////////////////////////////////
8982 /// Stream a class object.
8983 
8984 void TTree::Streamer(TBuffer& b)
8985 {
8986  if (b.IsReading()) {
8987  UInt_t R__s, R__c;
8988  if (fDirectory) {
8989  fDirectory->Remove(this);
8990  //delete the file cache if it points to this Tree
8991  TFile *file = fDirectory->GetFile();
8992  MoveReadCache(file,0);
8993  }
8994  fDirectory = 0;
8997  Version_t R__v = b.ReadVersion(&R__s, &R__c);
8998  if (R__v > 4) {
8999  b.ReadClassBuffer(TTree::Class(), this, R__v, R__s, R__c);
9000 
9001  fBranches.SetOwner(kTRUE); // True needed only for R__v < 19 and most R__v == 19
9002 
9003  if (fBranchRef) fBranchRef->SetTree(this);
9006 
9007  if (fTreeIndex) {
9008  fTreeIndex->SetTree(this);
9009  }
9010  if (fIndex.fN) {
9011  Warning("Streamer", "Old style index in this tree is deleted. Rebuild the index via TTree::BuildIndex");
9012  fIndex.Set(0);
9013  fIndexValues.Set(0);
9014  }
9015  if (fEstimate <= 10000) {
9016  fEstimate = 1000000;
9017  }
9018 
9019  if (fNClusterRange) {
9020  // The I/O allocated just enough memory to hold the
9021  // current set of ranges.
9023  }
9024  if (GetCacheAutoSize() != 0) {
9025  // a cache will be automatically created.
9026  // No need for TTreePlayer::Process to enable the cache
9027  fCacheSize = 0;
9028  } else if (fAutoFlush < 0) {
9029  // If there is no autoflush set, let's keep the cache completely
9030  // disable by default for now.
9032  } else if (fAutoFlush != 0) {
9033  // Estimate the cluster size.
9034  // This will allow TTree::Process to enable the cache.
9035  Long64_t zipBytes = GetZipBytes();
9036  Long64_t totBytes = GetTotBytes();
9037  if (zipBytes != 0) {
9038  fCacheSize = fAutoFlush*(zipBytes/fEntries);
9039  } else if (totBytes != 0) {
9040  fCacheSize = fAutoFlush*(totBytes/fEntries);
9041  } else {
9042  fCacheSize = 30000000;
9043  }
9044  if (fCacheSize >= (INT_MAX / 4)) {
9045  fCacheSize = INT_MAX / 4;
9046  } else if (fCacheSize == 0) {
9047  fCacheSize = 30000000;
9048  }
9049  } else {
9050  fCacheSize = 0;
9051  }
9053  return;
9054  }
9055  //====process old versions before automatic schema evolution
9056  Stat_t djunk;
9057  Int_t ijunk;
9058  TNamed::Streamer(b);
9059  TAttLine::Streamer(b);
9060  TAttFill::Streamer(b);
9061  TAttMarker::Streamer(b);
9062  b >> fScanField;
9063  b >> ijunk; fMaxEntryLoop = (Long64_t)ijunk;
9064  b >> ijunk; fMaxVirtualSize = (Long64_t)ijunk;
9065  b >> djunk; fEntries = (Long64_t)djunk;
9066  b >> djunk; fTotBytes = (Long64_t)djunk;
9067  b >> djunk; fZipBytes = (Long64_t)djunk;
9068  b >> ijunk; fAutoSave = (Long64_t)ijunk;
9069  b >> ijunk; fEstimate = (Long64_t)ijunk;
9070  if (fEstimate <= 10000) fEstimate = 1000000;
9071  fBranches.Streamer(b);
9072  if (fBranchRef) fBranchRef->SetTree(this);
9074  fLeaves.Streamer(b);
9076  if (R__v > 1) fIndexValues.Streamer(b);
9077  if (R__v > 2) fIndex.Streamer(b);
9078  if (R__v > 3) {
9079  TList OldInfoList;
9080  OldInfoList.Streamer(b);
9081  OldInfoList.Delete();
9082  }
9083  fNClusterRange = 0;
9084  fDefaultEntryOffsetLen = 1000;
9086  b.CheckByteCount(R__s, R__c, TTree::IsA());
9087  //====end of old versions
9088  } else {
9089  if (fBranchRef) {
9090  fBranchRef->Clear();
9091  }
9092  TRefTable *table = TRefTable::GetRefTable();
9093  if (table) TRefTable::SetRefTable(0);
9094 
9095  b.WriteClassBuffer(TTree::Class(), this);
9096 
9097  if (table) TRefTable::SetRefTable(table);
9098  }
9099 }
9100 
9101 ////////////////////////////////////////////////////////////////////////////////
9102 /// Unbinned fit of one or more variable(s) from a tree.
9103 ///
9104 /// funcname is a TF1 function.
9105 ///
9106 /// See TTree::Draw for explanations of the other parameters.
9107 ///
9108 /// Fit the variable varexp using the function funcname using the
9109 /// selection cuts given by selection.
9110 ///
9111 /// The list of fit options is given in parameter option.
9112 ///
9113 /// - option = "Q" Quiet mode (minimum printing)
9114 /// - option = "V" Verbose mode (default is between Q and V)
9115 /// - option = "E" Perform better Errors estimation using Minos technique
9116 /// - option = "M" More. Improve fit results
9117 ///
9118 /// You can specify boundary limits for some or all parameters via
9119 /// ~~~ {.cpp}
9120 /// func->SetParLimits(p_number, parmin, parmax);
9121 /// ~~~
9122 /// if parmin>=parmax, the parameter is fixed
9123 ///
9124 /// Note that you are not forced to fix the limits for all parameters.
9125 /// For example, if you fit a function with 6 parameters, you can do:
9126 /// ~~~ {.cpp}
9127 /// func->SetParameters(0,3.1,1.e-6,0.1,-8,100);
9128 /// func->SetParLimits(4,-10,-4);
9129 /// func->SetParLimits(5, 1,1);
9130 /// ~~~
9131 /// With this setup:
9132 ///
9133 /// - Parameters 0->3 can vary freely
9134 /// - Parameter 4 has boundaries [-10,-4] with initial value -8
9135 /// - Parameter 5 is fixed to 100.
9136 ///
9137 /// For the fit to be meaningful, the function must be self-normalized.
9138 ///
9139 /// i.e. It must have the same integral regardless of the parameter
9140 /// settings. Otherwise the fit will effectively just maximize the
9141 /// area.
9142 ///
9143 /// It is mandatory to have a normalization variable
9144 /// which is fixed for the fit. e.g.
9145 /// ~~~ {.cpp}
9146 /// TF1* f1 = new TF1("f1", "gaus(0)/sqrt(2*3.14159)/[2]", 0, 5);
9147 /// f1->SetParameters(1, 3.1, 0.01);
9148 /// f1->SetParLimits(0, 1, 1); // fix the normalization parameter to 1
9149 /// data->UnbinnedFit("f1", "jpsimass", "jpsipt>3.0");
9150 /// ~~~
9151 /// 1, 2 and 3 Dimensional fits are supported. See also TTree::Fit
9152 ///
9153 /// Return status:
9154 ///
9155 /// - The function return the status of the fit in the following form
9156 /// fitResult = migradResult + 10*minosResult + 100*hesseResult + 1000*improveResult
9157 /// - The fitResult is 0 is the fit is OK.
9158 /// - The fitResult is negative in case of an error not connected with the fit.
9159 /// - The number of entries used in the fit can be obtained via mytree.GetSelectedRows();
9160 /// - If the number of selected entries is null the function returns -1
9161 
9162 Int_t TTree::UnbinnedFit(const char* funcname, const char* varexp, const char* selection, Option_t* option, Long64_t nentries, Long64_t firstentry)
9164  GetPlayer();
9165  if (fPlayer) {
9166  return fPlayer->UnbinnedFit(funcname, varexp, selection, option, nentries, firstentry);
9167  }
9168  return -1;
9169 }
9170 
9171 ////////////////////////////////////////////////////////////////////////////////
9172 /// Replace current attributes by current style.
9173 
9176  if (gStyle->IsReading()) {
9185  } else {
9194  }
9195 }
9196 
9197 ////////////////////////////////////////////////////////////////////////////////
9198 /// Write this object to the current directory. For more see TObject::Write
9199 /// Write calls TTree::FlushBaskets before writing the tree.
9200 
9201 Int_t TTree::Write(const char *name, Int_t option, Int_t bufsize) const
9203  FlushBaskets();
9204  return TObject::Write(name, option, bufsize);
9205 }
9206 
9207 ////////////////////////////////////////////////////////////////////////////////
9208 /// Write this object to the current directory. For more see TObject::Write
9209 /// If option & kFlushBasket, call FlushBasket before writing the tree.
9210 
9211 Int_t TTree::Write(const char *name, Int_t option, Int_t bufsize)
9213  return ((const TTree*)this)->Write(name, option, bufsize);
9214 }
9215 
9216 ////////////////////////////////////////////////////////////////////////////////
9217 /// \class TTreeFriendLeafIter
9218 ///
9219 /// Iterator on all the leaves in a TTree and its friend
9220 
9222 
9223 ////////////////////////////////////////////////////////////////////////////////
9224 /// Create a new iterator. By default the iteration direction
9225 /// is kIterForward. To go backward use kIterBackward.
9226 
9228 : fTree(const_cast<TTree*>(tree))
9229 , fLeafIter(0)
9230 , fTreeIter(0)
9231 , fDirection(dir)
9232 {
9233 }
9234 
9235 ////////////////////////////////////////////////////////////////////////////////
9236 /// Copy constructor. Does NOT copy the 'cursor' location!
9237 
9239 : TIterator(iter)
9240 , fTree(iter.fTree)
9241 , fLeafIter(0)
9242 , fTreeIter(0)
9243 , fDirection(iter.fDirection)
9244 {
9245 }
9246 
9247 ////////////////////////////////////////////////////////////////////////////////
9248 /// Overridden assignment operator. Does NOT copy the 'cursor' location!
9249 
9252  if (this != &rhs && rhs.IsA() == TTreeFriendLeafIter::Class()) {
9253  const TTreeFriendLeafIter &rhs1 = (const TTreeFriendLeafIter &)rhs;
9254  fDirection = rhs1.fDirection;
9255  }
9256  return *this;
9257 }
9258 
9259 ////////////////////////////////////////////////////////////////////////////////
9260 /// Overridden assignment operator. Does NOT copy the 'cursor' location!
9261 
9264  if (this != &rhs) {
9265  fDirection = rhs.fDirection;
9266  }
9267  return *this;
9268 }
9269 
9270 ////////////////////////////////////////////////////////////////////////////////
9271 /// Go the next friend element
9272 
9275  if (!fTree) return 0;
9276 
9277  TObject * next;
9278  TTree * nextTree;
9279 
9280  if (!fLeafIter) {
9281  TObjArray *list = fTree->GetListOfLeaves();
9282  if (!list) return 0; // Can happen with an empty chain.
9283  fLeafIter = list->MakeIterator(fDirection);
9284  if (!fLeafIter) return 0;
9285  }
9286 
9287  next = fLeafIter->Next();
9288  if (!next) {
9289  if (!fTreeIter) {
9290  TCollection * list = fTree->GetListOfFriends();
9291  if (!list) return next;
9292  fTreeIter = list->MakeIterator(fDirection);
9293  if (!fTreeIter) return 0;
9294  }
9295  TFriendElement * nextFriend = (TFriendElement*) fTreeIter->Next();
9296  ///nextTree = (TTree*)fTreeIter->Next();
9297  if (nextFriend) {
9298  nextTree = const_cast<TTree*>(nextFriend->GetTree());
9299  if (!nextTree) return Next();
9300  SafeDelete(fLeafIter);
9301  fLeafIter = nextTree->GetListOfLeaves()->MakeIterator(fDirection);
9302  if (!fLeafIter) return 0;
9303  next = fLeafIter->Next();
9304  }
9305  }
9306  return next;
9307 }
9308 
9309 ////////////////////////////////////////////////////////////////////////////////
9310 /// Returns the object option stored in the list.
9311 
9314  if (fLeafIter) return fLeafIter->GetOption();
9315  return "";
9316 }
A zero length substring is legal.
Definition: TString.h:71
Bool_t HasRuleWithSourceClass(const TString &source) const
Return True if we have any rule whose source class is &#39;source&#39;.
TString fTitle
Definition: TNamed.h:33
TTree * fParentTree
! pointer to the parent TTree
void Add(TObject *obj, const char *name=0, Int_t check=-1)
Add object with name to browser.
Definition: TBrowser.cxx:261
Describe Streamer information for one class version.
Definition: TStreamerInfo.h:43
void Foreach(F func, unsigned nTimes)
Execute func (with no arguments) nTimes in parallel.
virtual Bool_t GetReapplyCut() const
Definition: TEventList.h:57
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
virtual TBranch * FindBranch(const char *name)
Return the branch that correspond to the path &#39;branchname&#39;, which can include the name of the tree or...
Definition: TTree.cxx:4595
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:785
Double_t fWeight
Tree weight (see TTree::SetWeight)
Definition: TTree.h:81
virtual Int_t Occurence(const TObject *obj) const
Return occurence number of object in the list of objects of this folder.
Definition: TFolder.cxx:436
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1276
virtual Int_t MakeProxy(const char *classname, const char *macrofilename=0, const char *cutfilename=0, const char *option=0, Int_t maxUnrolling=3)=0
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:32
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
Int_t Unroll(const char *name, TClass *cltop, TClass *cl, char *ptr, Int_t basketsize, Int_t splitlevel, Int_t btype)
Split class cl into sub-branches of this branch.
Long64_t Previous()
Move on to the previous cluster and return the starting entry of this previous cluster.
Definition: TTree.cxx:647
Bool_t IsReading() const
Definition: TBuffer.h:83
virtual void AddTotBytes(Int_t tot)
Definition: TTree.h:295
TTreeCache * GetReadCache(TFile *file, Bool_t create=kFALSE)
Find and return the TTreeCache registered with the file and which may contain branches for us...
Definition: TTree.cxx:5998
A TFolder object is a collection of objects and folders.
Definition: TFolder.h:30
virtual void ResetAfterMerge(TFileMergeInfo *)
Reset a Branch after a Merge operation (drop data but keep customizations) TRefTable is cleared...
Definition: TBranchRef.cxx:198
virtual TList * GetListOfClones()
Definition: TTree.h:406
Bool_t CanSplit() const
Return true if the data member of this TClass can be saved separately.
Definition: TClass.cxx:2231
TList * GetListOfBases()
Return list containing the TBaseClass(es) of a class.
Definition: TClass.cxx:3507
virtual TTree * CopyTree(const char *selection, Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)=0
ESTLType
Definition: ESTLType.h:28
TStreamerInfo * BuildStreamerInfo(TClass *cl, void *pointer=0, Bool_t canOptimize=kTRUE)
Build StreamerInfo for class cl.
Definition: TTree.cxx:2509
An array of TObjects.
Definition: TObjArray.h:37
static TDataType * GetDataType(EDataType type)
Given a EDataType type, get the TDataType* that represents it.
Definition: TDataType.cxx:440
Long64_t fDebugMin
! First entry number to debug
Definition: TTree.h:103
Principal Components Analysis (PCA)
Definition: TPrincipal.h:20
virtual Int_t FillImpl(ROOT::Internal::TBranchIMTHelper *)
Loop on all leaves of this branch to fill Basket buffer.
Definition: TBranch.cxx:815
virtual TDirectory * GetDirectory() const
Definition: TEntryList.h:74
virtual void Append(const TVirtualIndex *, Bool_t delaySort=kFALSE)=0
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
Bool_t fPrevious
Definition: TTree.h:170
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:150
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:467
virtual void UpdateFile()
Refresh the value of fDirectory (i.e.
Definition: TBranch.cxx:2854
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:854
TString GetTypeName()
Get basic type of typedef, e,g.
Definition: TDataType.cxx:149
virtual TTree * GetTree()
Return pointer to friend TTree.
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket...
Definition: TBufferFile.h:47
virtual void SetAddress(void *add)
Set address of this branch.
Definition: TBranch.cxx:2240
virtual void OptimizeBaskets(ULong64_t maxMemory=10000000, Float_t minComp=1.1, Option_t *option="")
This function may be called after having filled some entries in a Tree.
Definition: TTree.cxx:6709
Bool_t MemoryFull(Int_t nbytes)
Check if adding nbytes to memory we are still below MaxVirtualsize.
Definition: TTree.cxx:6511
virtual TBranch * BranchImpRef(const char *branchname, const char *classname, TClass *ptrClass, void *addobj, Int_t bufsize, Int_t splitlevel)
Same as TTree::Branch but automatic detection of the class name.
Definition: TTree.cxx:1521
virtual Int_t MakeCode(const char *filename=0)
Generate a skeleton function for this tree.
Definition: TTree.cxx:6314
long long Long64_t
Definition: RtypesCore.h:69
Abstract interface for Tree Index.
Definition: TVirtualIndex.h:29
auto * m
Definition: textangle.C:8
virtual Int_t GetBasketSize() const
Definition: TBranch.h:167
virtual Int_t MakeReader(const char *classname, Option_t *option)=0
virtual Long64_t ReadStream(std::istream &inputStream, const char *branchDescriptor="", char delimiter=' ')
Create or simply read branches from an input stream.
Definition: TTree.cxx:7218
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
virtual Int_t StopCacheLearningPhase()
Stop the cache learning phase.
Definition: TTree.cxx:8912
virtual void SetBranchStatus(const char *bname, Bool_t status=1, UInt_t *found=0)
Set branch status to Process or DoNotProcess.
Definition: TTree.cxx:8044
virtual void IncrementTotalBuffers(Int_t nbytes)
Definition: TTree.h:462
short Version_t
Definition: RtypesCore.h:61
virtual void Delete(Option_t *option="")
Delete this tree from memory or/and disk.
Definition: TTree.cxx:3540
Bool_t EqualTo(const char *cs, ECaseCompare cmp=kExact) const
Definition: TString.h:579
A Branch for the case of an object.
Definition: TBranchObject.h:26
const char * GetFullTypeName() const
Get full type description of data member, e,g.: "class TDirectory*".
TObjArray * GetListOfBaskets()
Definition: TBranch.h:193
TLine * line
static Int_t fgBranchStyle
Old/New branch style.
Definition: TTree.h:134
virtual void AddZipBytes(Int_t zip)
Definition: TTree.h:296
virtual void SetTree(const TTree *T)=0
virtual Int_t LoadBaskets(Long64_t maxmemory=2000000000)
Read in memory all baskets from all branches up to the limit of maxmemory bytes.
Definition: TTree.cxx:6118
virtual void Clear(Option_t *option="")
Remove all objects from the array.
Definition: TObjArray.cxx:320
float Float_t
Definition: RtypesCore.h:53
virtual Int_t GetExpectedType(TClass *&clptr, EDataType &type)
Fill expectedClass and expectedType with information on the data type of the object/values contained ...
Definition: TBranch.cxx:1473
TVirtualStreamerInfo * FindConversionStreamerInfo(const char *onfile_classname, UInt_t checksum) const
Return a Conversion StreamerInfo from the class &#39;classname&#39; for the layout represented by &#39;checksum&#39; ...
Definition: TClass.cxx:6854
static Long64_t GetMaxTreeSize()
Static function which returns the tree file size limit in bytes.
Definition: TTree.cxx:5945
virtual void SetParallelUnzip(Bool_t opt=kTRUE, Float_t RelSize=-1)
Enable or disable parallel unzipping of Tree buffers.
Definition: TTree.cxx:8753
TVirtualStreamerInfo * GetConversionStreamerInfo(const char *onfile_classname, Int_t version) const
Return a Conversion StreamerInfo from the class &#39;classname&#39; for version number &#39;version&#39; to this clas...
Definition: TClass.cxx:6757
virtual Int_t MakeCode(const char *filename)=0
Provides the interface for the PROOF internal performance measurement and event tracing.
A cache when reading files over the network.
const char Option_t
Definition: RtypesCore.h:62
virtual Bool_t Notify()
Function called when loading a new class library.
Definition: TTree.cxx:6685
TTree()
Default constructor and I/O constructor.
Definition: TTree.cxx:691
virtual TTree * GetFriend(const char *) const
Return a pointer to the TTree friend whose name or alias is &#39;friendname.
Definition: TTree.cxx:5657
void GetObject(const char *namecycle, T *&ptr)
Definition: TDirectory.h:139
virtual TClass * GetValueClass() const =0
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Definition: TObjArray.cxx:355
All ROOT classes may have RTTI (run time type identification) support added.
Definition: TDataMember.h:31
virtual Long64_t GetEntriesFast() const
Definition: TTree.h:384
const char * Size
Definition: TXMLSetup.cxx:55
TBuffer * fTransientBuffer
! Pointer to the current transient buffer.
Definition: TTree.h:125
virtual void Flush()
Synchronize a file&#39;s in-memory and on-disk states.
Definition: TFile.cxx:1098
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6864
virtual void SetCacheRead(TFileCacheRead *cache, TObject *tree=0, ECacheAction action=kDisconnect)
Set a pointer to the read cache.
Definition: TFile.cxx:2242
Long64_t GetZipBytes(Option_t *option="") const
Return total number of zip bytes in the branch if option ="*" includes all sub-branches of this branc...
Definition: TBranch.cxx:1805
virtual Int_t Fit(const char *funcname, const char *varexp, const char *selection="", Option_t *option="", Option_t *goption="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Fit a projected item(s) from a tree.
Definition: TTree.cxx:4823
const Ssiz_t kNPOS
Definition: RtypesCore.h:111
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:638
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
TList * fFriends
pointer to list of friend elements
Definition: TTree.h:118
void SetHistLineWidth(Width_t width=1)
Definition: TStyle.h:357
TPrincipal * Principal(const char *varexp="", const char *selection="", Option_t *option="np", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Interface to the Principal Components Analysis class.
Definition: TTree.cxx:6845
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4364
Int_t GetMakeClass() const
Definition: TTree.h:414
virtual TEntryList * GetEntryList()
Returns the entry list, set to this tree.
Definition: TTree.cxx:5537
ROOT::TIOFeatures SetIOFeatures(const ROOT::TIOFeatures &)
Provide the end-user with the ability to enable/disable various experimental IO features for this TTr...
Definition: TTree.cxx:8628
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
virtual void DropBaskets(Option_t *option="")
Loop on all branch baskets.
Definition: TBranch.cxx:716
static void SetBranchStyle(Int_t style=1)
Set the current branch style.
Definition: TTree.cxx:8170
A specialized TFileCacheRead object for a TTree.
Definition: TTreeCache.h:30
virtual TLeaf * GetLeaf(const char *branchname, const char *leafname)
Return pointer to the 1st Leaf named name in any Branch of this Tree or any branch in the list of fri...
Definition: TTree.cxx:5870
TTree * fTree
Definition: TTree.h:168
const std::type_info * GetTypeInfo() const
Definition: TClass.h:461
static Int_t SetParallelUnzip(TTreeCacheUnzip::EParUnzipMode option=TTreeCacheUnzip::kEnable)
Static function that (de)activates multithreading unzipping.
static char DataTypeToChar(EDataType datatype)
Definition: TTree.cxx:430
TVirtualStreamerInfo * GetStreamerInfo(Int_t version=0) const
returns a pointer to the TVirtualStreamerInfo object for version If the object does not exist...
Definition: TClass.cxx:4420
virtual void PrintValue(Int_t i=0) const
Definition: TLeaf.h:125
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
const char * GetTypeName() const
Get type of data member, e,g.: "class TDirectory*" -> "TDirectory".
virtual Int_t GetEntries() const
Definition: TCollection.h:177
virtual TLeaf * GetLeafImpl(const char *branchname, const char *leafname)
Return pointer to the 1st Leaf named name in any Branch of this Tree or any branch in the list of fri...
Definition: TTree.cxx:5780
virtual Long64_t GetAutoFlush() const
Definition: TTree.h:366
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
bool Set(EIOFeatures bits)
Set a specific IO feature.
Regular expression class.
Definition: TRegexp.h:31
TDirectory * fDirectory
! Pointer to directory holding this tree
Definition: TTree.h:109
constexpr Float_t kNEntriesResortInv
Definition: TTree.cxx:419
Int_t fMakeClass
! not zero when processing code generated by MakeClass
Definition: TTree.h:106
#define R__ASSERT(e)
Definition: TError.h:96
virtual TBasket * CreateBasket(TBranch *)
Create a basket for this tree and given branch.
Definition: TTree.cxx:3524
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
#define gROOT
Definition: TROOT.h:402
TList * GetListOfDataMembers(Bool_t load=kTRUE)
Return list containing the TDataMembers of a class.
Definition: TClass.cxx:3617
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:585
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
virtual Int_t WriteTObject(const TObject *obj, const char *name=0, Option_t *="", Int_t=0)
See TDirectoryFile::WriteTObject for details.
void ForceWriteInfo(TFile *file, Bool_t force=kFALSE)
Recursively mark streamer infos for writing to a file.
virtual TPrincipal * Principal(const char *varexp="", const char *selection="", Option_t *option="np", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)=0
virtual void SetAutoSave(Long64_t autos=-300000000)
This function may be called at the start of a program to change the default value for fAutoSave (and ...
Definition: TTree.cxx:7855
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5330
virtual Int_t ReadKeys(Bool_t=kTRUE)
Definition: TDirectory.h:174
virtual Int_t GetOffset() const
Definition: TLeaf.h:80
Basic string class.
Definition: TString.h:125
TVirtualCollectionProxy * GetCollectionProxy() const
Return the proxy describing the collection (if any).
Definition: TClass.cxx:2816
virtual void SetAddress(void *addobj)
Point this branch at an object.
virtual Int_t MakeProxy(const char *classname, const char *macrofilename=0, const char *cutfilename=0, const char *option=0, Int_t maxUnrolling=3)
Generate a skeleton analysis class for this Tree using TBranchProxy.
Definition: TTree.cxx:6442
virtual Long64_t GetEntryNumberWithBestIndex(Long64_t major, Long64_t minor) const =0
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:168
virtual void SetTargetClass(const char *name)
Set the name of the class of the in-memory object into which the data will loaded.
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1099
static TBranch * R__FindBranchHelper(TObjArray *list, const char *branchname)
Search in the array for a branch matching the branch name, with the branch possibly expressed as a &#39;f...
Definition: TTree.cxx:4547
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:57
virtual void Browse(TBrowser *)
Browse content of the TTree.
Definition: TTree.cxx:2469
constexpr std::array< decltype(std::declval< F >)(std::declval< int >))), N > make(F f)
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:39
virtual void StopLearningPhase()
This is the counterpart of StartLearningPhase() and can be used to stop the learning phase...
static void SetMaxTreeSize(Long64_t maxsize=100000000000LL)
Set the maximum size in bytes of a Tree file (static function).
Definition: TTree.cxx:8685
TIOFeatures provides the end-user with the ability to change the IO behavior of data written via a TT...
Definition: TIOFeatures.hxx:62
Int_t fScanField
Number of runs before prompting in Scan.
Definition: TTree.h:83
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition: TTree.cxx:2961
TObject * At(Int_t idx) const
Definition: TObjArray.h:165
virtual TTree * CopyTree(const char *selection, Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Copy a tree with selection.
Definition: TTree.cxx:3512
virtual void KeepCircular()
Keep a maximum of fMaxEntries in memory.
Definition: TTree.cxx:6082
TArrayD fIndexValues
Sorted index values.
Definition: TTree.h:115
void SetAutoCreated(Bool_t val)
Definition: TTreeCache.h:97
virtual EDataType GetType() const =0
constexpr Int_t kNEntriesResort
Definition: TTree.cxx:418
virtual Long64_t GetEntries(const char *)=0
Long64_t fMaxEntryLoop
Maximum number of entries to process.
Definition: TTree.h:89
TVirtualTreePlayer * GetPlayer()
Load the TTreePlayer (if not already done).
Definition: TTree.cxx:5984
virtual void SetMaxVirtualSize(Long64_t size=0)
Definition: TTree.h:546
virtual TSQLResult * Query(const char *varexp="", const char *selection="", Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Loop over entries and return a TSQLResult object containing entries following selection.
Definition: TTree.cxx:7121
Streamer around an arbitrary STL like container, which implements basic container functionality...
void ToHumanReadableSize(value_type bytes, Bool_t si, Double_t *coeff, const char **units)
Return the size expressed in &#39;human readable&#39; format.
Definition: StringConv.hxx:38
virtual Int_t AddBranch(TBranch *b, Bool_t subgbranches=kFALSE)
Add a branch to the list of branches to be stored in the cache this function is called by TBranch::Ge...
Definition: TTreeCache.cxx:332
virtual Int_t DeleteGlobal(void *obj)=0
virtual void SetupAddresses()
If the branch address is not set, we set all addresses starting with the top level parent branch...
Definition: TBranch.cxx:2844
virtual void SetTree(const TTree *tree)
If a list for a tree with such name and filename exists, sets it as the current sublist If not...
Iterator abstract base class.
Definition: TIterator.h:30
virtual Width_t GetLineWidth() const
Return the line width.
Definition: TAttLine.h:35
void Reset()
Definition: TCollection.h:250
void BypassStreamer(Bool_t bypass=kTRUE)
When the kBypassStreamer bit is set, the automatically generated Streamer can call directly TClass::W...
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:694
virtual void Reset(Option_t *option="")
Definition: TBranchRef.cxx:187
virtual Int_t Fit(const char *formula, const char *varexp, const char *selection, Option_t *option, Option_t *goption, Long64_t nentries, Long64_t firstentry)=0
virtual TObject * FindObject(const char *name) const
Delete a TObjLink object.
Definition: TList.cxx:574
virtual void Refresh()
Refresh contents of this tree and its branches from the current status on disk.
Definition: TTree.cxx:7515
Width_t GetHistLineWidth() const
Definition: TStyle.h:221
if object in a list can be deleted
Definition: TObject.h:58
virtual void SetFileNumber(Int_t number=0)
Set fFileNumber to number.
Definition: TTree.cxx:8651
virtual Double_t GetMinimum(const char *columname)
Return minimum of column with name columname.
Definition: TTree.cxx:5955
virtual void Print(Option_t *option="") const
Print the TRefTable branch.
Definition: TBranchRef.cxx:159
virtual void DirectoryAutoAdd(TDirectory *)
Called by TKey and TObject::Clone to automatically add us to a directory when we are read from a file...
Definition: TTree.cxx:3612
static Bool_t IsParallelUnzip()
Static function that tells wether the multithreading unzipping is activated.
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3950
Int_t Length() const
Definition: TBuffer.h:96
virtual void SetTree(TTree *tree)
Definition: TBranch.h:234
virtual void SetDirectory(TDirectory *dir)
Add reference to directory dir. dir can be 0.
static void SetRefTable(TRefTable *table)
Static function setting the current TRefTable.
Definition: TRefTable.cxx:383
Bool_t fIMTFlush
! True if we are doing a multithreaded flush.
Definition: TTree.h:139
virtual Style_t GetMarkerStyle() const
Return the marker style.
Definition: TAttMarker.h:32
virtual void StartViewer()
Start the TTreeViewer on this tree.
Definition: TTree.cxx:8897
virtual Long64_t Merge(TCollection *list, Option_t *option="")
Merge the trees in the TList into this tree.
Definition: TTree.cxx:6568
Marker Attributes class.
Definition: TAttMarker.h:19
static TVirtualTreePlayer * TreePlayer(TTree *obj)
Static function returning a pointer to a Tree player.
virtual void SetBranchFolder()
virtual Style_t GetLineStyle() const
Return the line style.
Definition: TAttLine.h:34
virtual Int_t GetEntryWithIndex(Int_t major, Int_t minor=0)
Read entry corresponding to major and minor number.
Definition: TTree.cxx:5610
TList * fAliases
List of aliases for expressions based on the tree branches.
Definition: TTree.h:112
EFromHumanReadableSize FromHumanReadableSize(std::string_view str, T &value)
Convert strings like the following into byte counts 5MB, 5 MB, 5M, 3.7GB, 123b, 456kB, 3.7GiB, 5MiB with some amount of forgiveness baked into the parsing.
Definition: StringConv.hxx:86
Long64_t GetTotBytes(Option_t *option="") const
Return total number of bytes in the branch (excluding current buffer) if option ="*" includes all sub...
Definition: TBranch.cxx:1787
virtual Long64_t DrawSelect(const char *varexp, const char *selection, Option_t *option, Long64_t nentries, Long64_t firstentry)=0
virtual Int_t FlushBaskets() const
Write to disk all the basket that have not yet been individually written.
Definition: TTree.cxx:4870
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
Definition: TObjArray.cxx:505
Bool_t fCacheDoClusterPrefetch
! true if cache is prefetching whole clusters
Definition: TTree.h:127
#define SafeDelete(p)
Definition: RConfig.h:509
virtual Long64_t GetCacheSize() const
Definition: TTree.h:372
Bool_t IsBasic() const
Return true if data member is a basic type, e.g. char, int, long...
virtual TObjArray * GetListOfBranches()
Definition: TTree.h:407
Helper class to iterate over cluster of baskets.
Definition: TTree.h:234
TVirtualTreePlayer * fPlayer
! Pointer to current Tree player
Definition: TTree.h:121
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:128
Fill Area Attributes class.
Definition: TAttFill.h:19
void ImportClusterRanges(TTree *fromtree)
Appends the cluster range information stored in &#39;fromtree&#39; to this tree, including the value of fAuto...
Definition: TTree.cxx:6039
virtual void SetAutoFlush(Long64_t autof=-30000000)
This function may be called at the start of a program to change the default value for fAutoFlush...
Definition: TTree.cxx:7768
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2365
Bool_t IsAutoCreated() const
Definition: TTreeCache.h:85
void Class()
Definition: Class.C:29
virtual Long64_t CopyEntries(TTree *tree, Long64_t nentries=-1, Option_t *option="")
Copy nentries from given tree to this tree.
Definition: TTree.cxx:3343
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7898
void SetHistFillColor(Color_t color=1)
Definition: TStyle.h:353
Int_t fNfill
! Local for EntryLoop
Definition: TTree.h:101
UChar_t GetFeatures() const
virtual Bool_t IncludeRange(TLeaf *)
Definition: TLeaf.h:89
virtual Double_t GetValue(Int_t i=0) const
Definition: TLeaf.h:124
virtual void SetObject(const char *name, const char *title)
Change the name and title of this tree.
Definition: TTree.cxx:8722
virtual Bool_t IsWritable() const
Definition: TDirectory.h:163
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual Long64_t GetEntryNumberWithBestIndex(Long64_t major, Long64_t minor=0) const
Return entry number corresponding to major and minor number.
Definition: TTree.cxx:5573
virtual Bool_t Notify()
This method must be overridden to handle object notification.
Definition: TObject.cxx:506
virtual Long64_t GetReadEntry() const
Definition: TTree.h:426
virtual const char * GetAlias(const char *aliasName) const
Returns the expanded value of the alias. Search in the friends if any.
Definition: TTree.cxx:4944
virtual TLeaf * GetLeaf(const char *name) const
Return pointer to the 1st Leaf named name in thisBranch.
Definition: TBranch.cxx:1636
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition: TTree.cxx:6139
Bool_t fCacheUserSet
! true if the cache setting was explicitly given by user
Definition: TTree.h:128
virtual void SetTreeIndex(TVirtualIndex *index)
The current TreeIndex is replaced by the new index.
Definition: TTree.cxx:8784
virtual Long64_t GetEntry(Int_t index) const
Return value of entry at index in the list.
Definition: TEventList.cxx:222
Int_t fMaxClusterRange
! Memory allocated for the cluster range.
Definition: TTree.h:87
static Int_t GetBranchStyle()
Static function returning the current branch style.
Definition: TTree.cxx:5099
virtual Int_t BuildIndex(const char *majorname, const char *minorname="0")
Build a Tree Index (default is TTreeIndex).
Definition: TTree.cxx:2494
virtual Int_t UnbinnedFit(const char *funcname, const char *varexp, const char *selection="", Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Unbinned fit of one or more variable(s) from a tree.
Definition: TTree.cxx:9163
virtual Bool_t SetMakeClass(Bool_t decomposeObj=kTRUE)
Set the branch in a mode where the object are decomposed (Also known as MakeClass mode)...
Definition: TBranch.cxx:2485
virtual TBranch * BranchOld(const char *name, const char *classname, void *addobj, Int_t bufsize=32000, Int_t splitlevel=1)
Create a new TTree BranchObject.
Definition: TTree.cxx:1934
Bool_t IsLoaded() const
Return true if the shared library of this class is currently in the a process&#39;s memory.
Definition: TClass.cxx:5642
Set if we own the value buffer and so must delete it ourselves.
Definition: TLeaf.h:59
virtual void Show(Long64_t entry=-1, Int_t lenmax=20)
Print values of all active leaves for entry.
Definition: TTree.cxx:8823
virtual TBranch * FindBranch(const char *name)
Find the immediate sub-branch with passed name.
Definition: TBranch.cxx:986
TBranchRef * fBranchRef
Branch supporting the TRefTable (if any)
Definition: TTree.h:123
virtual Int_t MakeClass(const char *classname=0, Option_t *option="")
Generate a skeleton analysis class for this tree.
Definition: TTree.cxx:6281
virtual const char * Getenv(const char *env)
Get environment variable.
Definition: TSystem.cxx:1638
virtual TClusterIterator GetClusterIterator(Long64_t firstentry)
Return an iterator over the cluster of baskets starting at firstentry.
Definition: TTree.cxx:5161
TIOFeatures * fIOFeatures
virtual Int_t GetN() const
Definition: TEventList.h:56
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
Style_t GetHistFillStyle() const
Definition: TStyle.h:219
const Int_t kDoNotProcess
Definition: TBranch.h:47
UInt_t fFriendLockStatus
! Record which method is locking the friend recursion
Definition: TTree.h:124
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
Long_t GetThisOffset() const
Definition: TRealData.h:55
virtual Int_t MakeClass(const char *classname, const char *option)=0
Int_t fTimerInterval
Timer interval in milliseconds.
Definition: TTree.h:82
virtual Size_t GetMarkerSize() const
Return the marker size.
Definition: TAttMarker.h:33
virtual TFriendElement * AddFriend(const char *treename, const char *filename="")
Add a TFriendElement to the list of friends.
Definition: TTree.cxx:1234
TDataType * GetDataType() const
Definition: TDataMember.h:74
virtual Long64_t Scan(const char *varexp="", const char *selection="", Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Loop over tree entries and print entries passing selection.
Definition: TTree.cxx:7671
Long64_t fFlushedBytes
Number of auto-flushed bytes.
Definition: TTree.h:80
virtual Int_t GetLenType() const
Definition: TLeaf.h:76
TObjArray * GetListOfBranches()
Definition: TBranch.h:194
UInt_t fNEntriesSinceSorting
! Number of entries processed since the last re-sorting of branches
Definition: TTree.h:130
Ssiz_t First(char c) const
Find first occurrence of a character c.
Definition: TString.cxx:477
Specialization of TTreeCache for parallel Unzipping.
virtual TList * GetList() const
Definition: TDirectory.h:149
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
virtual void Delete(Option_t *option="")
Delete an object from the file.
Definition: TKey.cxx:534
virtual Double_t GetMaximum(const char *columname)
Return maximum of column with name columname.
Definition: TTree.cxx:5916
void Set(Int_t n)
Set size of this array to n ints.
Definition: TArrayI.cxx:105
virtual void RemoveFriend(TTree *)
Remove a friend from the list of friends.
Definition: TTree.cxx:7554
virtual void SetAddress(void *add=0)
Definition: TLeaf.h:126
TCollection * GetListOfFolders() const
Definition: TFolder.h:55
virtual TList * GetUserInfo()
Return a pointer to the list containing user objects associated to this tree.
Definition: TTree.cxx:6023
std::atomic< Long64_t > fIMTZipBytes
! Zip bytes for the IMT flush baskets.
Definition: TTree.h:141
virtual void SetCacheLearnEntries(Int_t n=10)
Interface to TTreeCache to set the number of entries for the learning phase.
Definition: TTree.cxx:8363
A branch containing and managing a TRefTable for TRef autoloading.
Definition: TBranchRef.h:29
Long64_t fZipBytes
Total number of bytes in all branches after compression.
Definition: TTree.h:78
virtual TFile * GetFile() const
Definition: TDirectory.h:147
Long64_t fDebugMax
! Last entry number to debug
Definition: TTree.h:104
virtual TBranch * BranchImp(const char *branchname, const char *classname, TClass *ptrClass, void *addobj, Int_t bufsize, Int_t splitlevel)
Same as TTree::Branch() with added check that addobj matches className.
Definition: TTree.cxx:1440
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:134
virtual void ResetAddress()
Set branch address to zero and free all allocated memory.
virtual TTree * GetTree() const
Definition: TTree.h:434
virtual void SetEventList(TEventList *list)
This function transfroms the given TEventList into a TEntryList The new TEntryList is owned by the TT...
Definition: TTree.cxx:8567
void Expand(Int_t newsize, Bool_t copy=kTRUE)
Expand (or shrink) the I/O buffer to newsize bytes.
Definition: TBuffer.cxx:211
virtual Int_t GetTreeNumber() const
Definition: TTree.h:436
void UseCurrentStyle()
Replace current attributes by current style.
Definition: TTree.cxx:9175
A specialized string object used for TTree selections.
Definition: TCut.h:25
A doubly linked list.
Definition: TList.h:44
virtual void ResetAfterMerge(TFileMergeInfo *)
Reset a Branch.
Definition: TBranch.cxx:2156
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:4985
Bool_t fCacheDoAutoInit
! true if cache auto creation or resize check is needed
Definition: TTree.h:126
Int_t GetRecordHeader(char *buf, Long64_t first, Int_t maxbytes, Int_t &nbytes, Int_t &objlen, Int_t &keylen)
Read the logical record header starting at a certain postion.
Definition: TFile.cxx:1251
Int_t GetType() const
Definition: TDataType.h:68
Int_t Fill()
Definition: TBranch.h:155
TObjArray fLeaves
Direct pointers to individual branch leaves.
Definition: TTree.h:111
Long64_t * fClusterRangeEnd
[fNClusterRange] Last entry of a cluster range.
Definition: TTree.h:94
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
void BuildRealData(void *pointer=0, Bool_t isTransient=kFALSE)
Build a full list of persistent data members.
Definition: TClass.cxx:1942
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
This class provides a simple interface to execute the same task multiple times in parallel...
Int_t fNClusterRange
Number of Cluster range in addition to the one defined by &#39;AutoFlush&#39;.
Definition: TTree.h:86
virtual Long64_t GetEntryNumber(Long64_t entry) const
Return entry number corresponding to entry.
Definition: TTree.cxx:5548
Int_t fN
Definition: TArray.h:38
TEntryList * fEntryList
! Pointer to event selection list (if one)
Definition: TTree.h:114
virtual Int_t GetLen() const
Return the number of effective elements of this leaf.
Definition: TLeaf.cxx:307
virtual Long64_t GetAutoSave() const
Definition: TTree.h:367
virtual void SetEntryRange(Long64_t emin, Long64_t emax)
Set the minimum and maximum entry number to be processed this information helps to optimize the numbe...
Int_t GetReadBasket() const
Definition: TBranch.h:184
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TTree.cxx:9212
virtual void SetEstimate(Long64_t nentries=1000000)
Set number of entries to estimate variable limits.
Definition: TTree.cxx:8608
virtual void SetOffset(Int_t offset=0)
Definition: TBranch.h:232
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:655
virtual Long64_t Process(const char *filename, Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)=0
virtual void UpdateFormulaLeaves()=0
virtual Int_t DropBranch(TBranch *b, Bool_t subbranches=kFALSE)
Remove a branch to the list of branches to be stored in the cache this function is called by TBranch:...
Definition: TTreeCache.cxx:484
void SetHistFillStyle(Style_t styl=0)
Definition: TStyle.h:355
void SetCompressionSettings(Int_t settings=1)
Set compression settings.
Definition: TBranch.cxx:2363
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
Long64_t fChainOffset
! Offset of 1st entry of this Tree in a TChain
Definition: TTree.h:97
~TFriendLock()
Restore the state of tree the same as before we set the lock.
Definition: TTree.cxx:511
Long64_t * fClusterSize
[fNClusterRange] Number of entries in each cluster for a given range.
Definition: TTree.h:95
virtual void WriteStreamerInfo()
Write the list of TStreamerInfo as a single object in this file The class Streamer description for al...
Definition: TFile.cxx:3653
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
Basic data type descriptor (datatype information is obtained from CINT).
Definition: TDataType.h:44
void TFriendElement__SetTree(TTree *tree, TList *frlist)
Set the fTree member for all friend elements.
Definition: TTree.cxx:8970
auto * a
Definition: textangle.C:12
virtual Long64_t GetEntryNumberWithIndex(Long64_t major, Long64_t minor=0) const
Return entry number corresponding to major and minor number.
Definition: TTree.cxx:5593
TVirtualPerfStats * fPerfStats
! pointer to the current perf stats object
Definition: TTree.h:119
TClass * GetActualClass(const void *object) const
Return a pointer the the real class of the object.
Definition: TClass.cxx:2527
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const =0
TTree * GetTree() const
Definition: TTreeCache.h:84
virtual TObject * RemoveAt(Int_t idx)
Remove object at index idx.
Definition: TObjArray.cxx:678
virtual Int_t ReadTObject(TObject *, const char *)
Definition: TDirectory.h:175
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:818
Long64_t fReadEntry
! Number of the entry being processed
Definition: TTree.h:98
virtual TVirtualIndex * GetTreeIndex() const
Definition: TTree.h:435
virtual Bool_t HasPointers() const =0
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:443
Int_t GetWriteBasket() const
Definition: TBranch.h:186
virtual ~TTree()
Destructor.
Definition: TTree.cxx:872
TDataMember * GetDataMember() const
Definition: TRealData.h:53
Collection abstract base class.
Definition: TCollection.h:63
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition: TClass.cxx:5149
TList * fUserInfo
pointer to a list of user objects associated to this Tree
Definition: TTree.h:120
TObjArray fBranches
List of Branches.
Definition: TTree.h:110
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2343
unsigned int UInt_t
Definition: RtypesCore.h:42
Int_t GetEntriesFast() const
Definition: TObjArray.h:64
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
TFile * GetCurrentFile() const
Return pointer to the current file.
Definition: TTree.cxx:5172
virtual Long64_t GetN() const =0
virtual Long64_t GetEntry(Int_t index)
Return the number of the entry #index of this TEntryList in the TTree or TChain See also Next()...
Definition: TEntryList.cxx:657
Ssiz_t Length() const
Definition: TString.h:386
virtual void MakeFree(Long64_t first, Long64_t last)
Mark unused bytes on the file.
Definition: TFile.cxx:1410
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
Definition: TDirectory.cxx:190
virtual TLeaf * GetLeafCount() const
Definition: TLeaf.h:72
Manages buffers for branches of a Tree.
Definition: TBasket.h:34
virtual void * GetValuePointer() const
Definition: TLeaf.h:81
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition: TString.cxx:1080
Int_t GetMaxBaskets() const
Definition: TBranch.h:196
A TEventList object is a list of selected events (entries) in a TTree.
Definition: TEventList.h:31
virtual TLeaf * FindLeaf(const char *name)
Find leaf..
Definition: TTree.cxx:4667
The TRealData class manages the effective list of all data members for a given class.
Definition: TRealData.h:30
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Definition: TBranch.cxx:1301
Bool_t CanIgnoreTObjectStreamer()
Definition: TClass.h:365
TArrayI fIndex
Index of sorted values.
Definition: TTree.h:116
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:75
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
static void SetLearnEntries(Int_t n=10)
Static function to set the number of entries to be used in learning mode The default value for n is 1...
ROOT::ESTLType GetCollectionType() const
Return the &#39;type&#39; of the STL the TClass is representing.
Definition: TClass.cxx:2805
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:354
Bool_t fIMTEnabled
! true if implicit multi-threading is enabled for this tree
Definition: TTree.h:129
Option_t * GetOption() const
Returns the object option stored in the list.
Definition: TTree.cxx:9313
virtual TBranchRef * GetBranchRef() const
Definition: TTree.h:369
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4688
void SetName(const char *name)
Definition: TCollection.h:202
virtual void ResetBranchAddress(TBranch *)
Tell all of our branches to set their addresses to zero.
Definition: TTree.cxx:7642
ROOT::TIOFeatures GetIOFeatures() const
Returns the current set of IO settings.
Definition: TTree.cxx:5756
Bool_t fDirection
iteration direction
Definition: TTree.h:594
TEventList * fEventList
! Pointer to event selection list (if one)
Definition: TTree.h:113
TString fName
Definition: TNamed.h:32
virtual TBranch * Bronch(const char *name, const char *classname, void *addobj, Int_t bufsize=32000, Int_t splitlevel=99)
Create a new TTree BranchElement.
Definition: TTree.cxx:2264
virtual const char * GetTreeName() const
virtual Bool_t GetBranchStatus(const char *branchname) const
Return status of branch with name branchname.
Definition: TTree.cxx:5084
if object destructor must call RecursiveRemove()
Definition: TObject.h:60
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition: TAttMarker.h:41
Long64_t fMaxEntries
Maximum number of entries in case of circular buffers.
Definition: TTree.h:88
void AddClone(TTree *)
Add a cloned tree to our list of trees to be notified whenever we change our branch addresses or when...
Definition: TTree.cxx:1146
void SetIOFeatures(TIOFeatures &features)
Definition: TBranch.h:230
virtual void KeepCircular(Long64_t maxEntries)
keep a maximum of fMaxEntries in memory
Definition: TBranch.cxx:1850
Each class (see TClass) has a linked list of its base class(es).
Definition: TBaseClass.h:33
char GetNewlineValue(std::istream &inputStream)
Determine which newline this file is using.
Definition: TTree.cxx:7191
virtual TObjLink * FirstLink() const
Definition: TList.h:108
A Branch for the case of an object.
virtual Long64_t GetBasketSeek(Int_t basket) const
Return address of basket in the file.
Definition: TBranch.cxx:1244
#define Printf
Definition: TGeoToOCC.h:18
virtual Int_t DropBranchFromCache(const char *bname, Bool_t subbranches=kFALSE)
Remove the branch with name &#39;bname&#39; from the Tree cache.
Definition: TTree.cxx:1069
void InitializeBranchLists(bool checkLeafCount)
Divides the top-level branches into two vectors: (i) branches to be processed sequentially and (ii) b...
Definition: TTree.cxx:5469
Long64_t fAutoSave
Autosave tree when fAutoSave entries written or -fAutoSave (compressed) bytes produced.
Definition: TTree.h:91
const Bool_t kFALSE
Definition: RtypesCore.h:88
virtual void SaveSelf(Bool_t=kFALSE)
Definition: TDirectory.h:181
virtual void SetBasketSize(Int_t buffsize)
Set the basket size The function makes sure that the basket size is greater than fEntryOffsetlen.
Definition: TBranch.cxx:2287
virtual Int_t CheckBranchAddressType(TBranch *branch, TClass *ptrClass, EDataType datatype, Bool_t ptr)
Check whether or not the address described by the last 3 parameters matches the content of the branch...
Definition: TTree.cxx:2697
virtual Color_t GetLineColor() const
Return the line color.
Definition: TAttLine.h:33
virtual void SetDebug(Int_t level=1, Long64_t min=0, Long64_t max=9999999)
Set the debug level and the debug range.
Definition: TTree.cxx:8426
virtual Long64_t Process(const char *filename, Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Process this tree executing the TSelector code in the specified filename.
Definition: TTree.cxx:7058
static unsigned int total
static const Ssiz_t kNPOS
Definition: TString.h:246
TString & Remove(Ssiz_t pos)
Definition: TString.h:619
long Long_t
Definition: RtypesCore.h:50
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
int Ssiz_t
Definition: RtypesCore.h:63
virtual void DropBuffers(Int_t nbytes)
Drop branch buffers to accommodate nbytes below MaxVirtualsize.
Definition: TTree.cxx:4296
virtual void SetMakeClass(Int_t make)
Set all the branches in this TTree to be in decomposed object mode (also known as MakeClass mode)...
Definition: TTree.cxx:8665
virtual void SetWeight(Double_t w=1, Option_t *option="")
Set tree weight.
Definition: TTree.cxx:8812
Color_t GetHistFillColor() const
Definition: TStyle.h:217
Version_t GetClassVersion() const
Definition: TClass.h:391
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2251
Long64_t Next()
Move on to the next cluster and return the starting entry of this next cluster.
Definition: TTree.cxx:602
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns an array iterator.
Definition: TObjArray.cxx:633
virtual void SetDirectory(TDirectory *dir)
Change the tree&#39;s directory.
Definition: TTree.cxx:8464
virtual Long64_t LoadTreeFriend(Long64_t entry, TTree *T)
Load entry on behalf of our master tree, we may use an index.
Definition: TTree.cxx:6231
virtual void SetEstimate(Long64_t n)=0
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:89
virtual void Print(Option_t *option="") const
Print cache statistics.
Long64_t fMaxVirtualSize
Maximum total size of buffers kept in memory.
Definition: TTree.h:90
virtual TObject * Remove(TObject *)
Remove an object from the in-memory list.
virtual Int_t GetMaximum() const
Definition: TLeaf.h:77
#define ClassImp(name)
Definition: Rtypes.h:359
virtual void StartViewer(Int_t ww, Int_t wh)=0
virtual void SetEntryList(TEntryList *list, Option_t *opt="")
Set an EntryList.
Definition: TTree.cxx:8544
TIterator & operator=(const TIterator &rhs)
Overridden assignment operator. Does NOT copy the &#39;cursor&#39; location!
Definition: TTree.cxx:9251
virtual void ResetAddress()
Reset the address of the branch.
Definition: TBranch.cxx:2209
virtual TKey * GetKey(const char *, Short_t=9999) const
Definition: TDirectory.h:148
double Double_t
Definition: RtypesCore.h:55
virtual void Clear(Option_t *option="")
Clear entries in the TRefTable.
Definition: TBranchRef.cxx:95
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition: TString.cxx:875
static TRefTable * GetRefTable()
Static function returning the current TRefTable.
Definition: TRefTable.cxx:287
Long64_t GetCacheAutoSize(Bool_t withDefault=kFALSE) const
Used for automatic sizing of the cache.
Definition: TTree.cxx:5111
void SetHistLineStyle(Style_t styl=0)
Definition: TStyle.h:356
Long64_t fTotBytes
Total number of bytes in all branches before compression.
Definition: TTree.h:77
virtual void WaitFinishPrefetch()
Data member is a pointer to an array of basic types.
Definition: TLeaf.h:58
Color_t GetHistLineColor() const
Definition: TStyle.h:218
std::vector< TBranch * > fSeqBranches
! Branches to be processed sequentially when IMT is on
Definition: TTree.h:132
Describe directory structure in memory.
Definition: TDirectory.h:34
virtual void SetPerfStats(TVirtualPerfStats *perf)
Set perf stats.
Definition: TTree.cxx:8767
std::vector< std::pair< Long64_t, TBranch * > > fSortedBranches
! Branches to be processed in parallel when IMT is on, sorted by average task time ...
Definition: TTree.h:131
TDirectory * GetDirectory() const
Definition: TTree.h:381
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any. ...
static void TBranch__SetTree(TTree *tree, TObjArray &branches)
Set the fTree member for all branches and sub branches.
Definition: TTree.cxx:8946
void SortBranchesByTime()
Sorts top-level branches by the last average task time recorded per branch.
Definition: TTree.cxx:5517
R__EXTERN TEnv * gEnv
Definition: TEnv.h:171
virtual Int_t GetBufferSize() const
unsigned long long ULong64_t
Definition: RtypesCore.h:70
virtual Long64_t ReadFile(const char *filename, const char *branchDescriptor="", char delimiter=' ')
Create or simply read branches from filename.
Definition: TTree.cxx:7172
TList * GetListOfRealData() const
Definition: TClass.h:418
Bool_t HasDataMemberInfo() const
Definition: TClass.h:378
TNamed()
Definition: TNamed.h:36
Long64_t fAutoFlush
Auto-flush tree when fAutoFlush entries written or -fAutoFlush (compressed) bytes produced...
Definition: TTree.h:92
Int_t GetNbytes() const
Definition: TKey.h:82
virtual void ResetBranchAddresses()
Tell all of our branches to drop their current objects and allocate new ones.
Definition: TTree.cxx:7652
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:355
virtual void SetObject(void *objadd)
Set object this branch is pointing to.
TCanvas * style()
Definition: style.C:1
int nentries
Definition: THbookFile.cxx:89
A TRefTable maintains the association between a referenced object and the parent object supporting th...
Definition: TRefTable.h:35
EDataType
Definition: TDataType.h:28
virtual Color_t GetFillColor() const
Return the fill area color.
Definition: TAttFill.h:30
Int_t fDefaultEntryOffsetLen
Initial Length of fEntryOffset table in the basket buffers.
Definition: TTree.h:85
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:570
static constexpr double s
#define R__LOCKGUARD(mutex)
void Browse(TBrowser *b)
Browse this collection (called by TBrowser).
TObjArray * GetListOfLeaves()
Definition: TBranch.h:195
Long64_t fEstimate
Number of entries to estimate histogram limits.
Definition: TTree.h:93
Int_t BufferSize() const
Definition: TBuffer.h:94
static void SetUnzipRelBufferSize(Float_t relbufferSize)
static function: Sets the unzip relatibe buffer size
UInt_t fMethodBit
Definition: TTree.h:169
Helper class to prevent infinite recursion in the usage of TTree Friends.
Definition: TTree.h:165
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition: TClass.cxx:2887
virtual Int_t SetBufferSize(Int_t buffersize)
Change the underlying buffer size of the cache.
virtual Int_t SetCacheSize(Long64_t cachesize=-1)
Set maximum size of the file cache .
Definition: TTree.cxx:8186
Int_t fDebug
! Debug level
Definition: TTree.h:102
EOnIndexError
Definition: TTree.cxx:3240
TCanvas * slash()
Definition: slash.C:1
virtual Int_t UnbinnedFit(const char *formula, const char *varexp, const char *selection, Option_t *option, Long64_t nentries, Long64_t firstentry)=0
UInt_t GetCheckSum()
Int_t SetCacheSizeAux(Bool_t autocache=kTRUE, Long64_t cacheSize=0)
Set the size of the file cache and create it if possible.
Definition: TTree.cxx:8212
double Stat_t
Definition: RtypesCore.h:73
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition: TAttLine.h:42
virtual Long64_t GetEntries() const
Definition: TTree.h:382
Int_t GetKeylen() const
Definition: TKey.h:80
Bool_t IsPersistent() const
Definition: TDataMember.h:89
Bool_t IsNull() const
Definition: TString.h:383
TClass * GetClass() const
Definition: TClonesArray.h:56
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TNamed.cxx:74
virtual void SetBasketSize(const char *bname, Int_t buffsize=16000)
Set a branch&#39;s basket size.
Definition: TTree.cxx:7871
virtual TFile * GetFile()
Return pointer to TFile containing this friend TTree.
virtual TBranch * BranchRef()
Build the optional branch supporting the TRefTable.
Definition: TTree.cxx:2188
virtual void ResetAfterMerge(TFileMergeInfo *)
Resets the state of this TTree after a merge (keep the customization but forget the data)...
Definition: TTree.cxx:7611
virtual Bool_t SetAlias(const char *aliasName, const char *aliasFormula)
Set a tree variable alias.
Definition: TTree.cxx:7713
Int_t fFileNumber
! current file number (if file extensions)
Definition: TTree.h:107
Mother of all ROOT objects.
Definition: TObject.h:37
Int_t fUpdate
Update frequency for EntryLoop.
Definition: TTree.h:84
virtual void ReadValue(std::istream &, Char_t=' ')
Definition: TLeaf.h:97
Bool_t IsReading() const
Definition: TStyle.h:274
virtual Long64_t GetEND() const
Definition: TFile.h:202
A Branch handling STL collection of pointers (vectors, lists, queues, sets and multisets) while stori...
Definition: TBranchSTL.h:22
static void * ReAlloc(void *vp, size_t size)
Reallocate (i.e.
Definition: TStorage.cxx:183
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1701
TDirectory * fOutputDirectory
virtual void SetEntryOffsetLen(Int_t len, Bool_t updateSubBranches=kFALSE)
Update the default value for the branch&#39;s fEntryOffsetLen if and only if it was already non zero (and...
Definition: TBranch.cxx:2379
virtual Long64_t Scan(const char *varexp, const char *selection, Option_t *option, Long64_t nentries, Long64_t firstentry)=0
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:401
virtual void SetFile(TFile *file=0)
Set file where this branch writes/reads its buffers.
Definition: TBranch.cxx:2421
Long64_t fEntries
Number of entries.
Definition: TTree.h:75
Bool_t IsTObject() const
Return kTRUE is the class inherits from TObject.
Definition: TClass.cxx:5668
TList * fClones
! List of cloned trees which share our addresses
Definition: TTree.h:122
virtual void Refresh(TBranch *b)
Refresh this branch using new information in b This function is called by TTree::Refresh.
Definition: TBranch.cxx:2066
Long64_t GetEntries() const
Definition: TBranch.h:199
Style_t GetHistLineStyle() const
Definition: TStyle.h:220
virtual Int_t MakeSelector(const char *selector=0, Option_t *option="")
Generate skeleton selector class for this tree.
Definition: TTree.cxx:6496
An array of clone (identical) objects.
Definition: TClonesArray.h:32
TBuffer * GetTransientBuffer(Int_t size)
Returns the transient buffer currently used by this TTree for reading/writing baskets.
Definition: TTree.cxx:964
virtual Long64_t GetTotBytes() const
Definition: TTree.h:433
Class implementing or helping the various TTree cloning method.
Definition: TTreeCloner.h:38
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:497
virtual Int_t DropBuffers()
Drop buffers of this basket if it is not the current basket.
Definition: TBasket.cxx:163
ROOT::ESTLType IsSTLContainer()
Return which type (if any) of STL container the data member is.
Definition: TBaseClass.cxx:101
Abstract base class defining the interface for the plugins that implement Draw, Scan, Process, MakeProxy, etc.
A Branch for the case of an array of clone objects.
Definition: TBranchClones.h:29
virtual void Add(TObject *obj)
Definition: TList.h:87
auto * l
Definition: textangle.C:4
#define R__unlikely(expr)
Definition: RConfig.h:554
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition: TROOT.cxx:584
TClusterIterator(TTree *tree, Long64_t firstEntry)
Regular constructor.
Definition: TTree.cxx:528
virtual void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition: TList.cxx:760
Definition: file.py:1
Int_t GetClassVersion()
const char * GetArrayIndex() const
If the data member is pointer and has a valid array size in its comments GetArrayIndex returns a stri...
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:200
virtual void Reset(Option_t *option="")
Reset a Branch.
Definition: TBranch.cxx:2115
A TFriendElement TF describes a TTree object TF in a file.
virtual Long64_t GetZipBytes() const
Definition: TTree.h:461
virtual const char * GetMinorName() const =0
Iterator on all the leaves in a TTree and its friend.
Definition: TTree.h:588
virtual TBranch * BronchExec(const char *name, const char *classname, void *addobj, Bool_t isptrptr, Int_t bufsize, Int_t splitlevel)
Helper function implementing TTree::Bronch and TTree::Branch(const char *name, T &obj);.
Definition: TTree.cxx:2272
virtual void RecursiveRemove(TObject *obj)
Make sure that obj (which is being deleted or will soon be) is no longer referenced by this TTree...
Definition: TTree.cxx:7484
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
virtual Int_t LoadBaskets()
Baskets associated to this branch are forced to be in memory.
Definition: TBranch.cxx:1876
#define snprintf
Definition: civetweb.c:822
TTree * GetTree() const
Definition: TBranch.h:200
#define gPad
Definition: TVirtualPad.h:285
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:522
virtual void SetCircular(Long64_t maxEntries)
Enable/Disable circularity for this tree.
Definition: TTree.cxx:8390
R__EXTERN Int_t gDebug
Definition: Rtypes.h:86
Int_t GetBufferSize() const
Definition: TBasket.h:110
virtual void Reset(Option_t *option="")
Reset baskets, buffers and entries count in all branches and leaves.
Definition: TTree.cxx:7580
virtual const char * GetMajorName() const =0
Definition: tree.py:1
std::atomic< Long64_t > fTotalBuffers
! Total number of bytes in branch buffers
Definition: TTree.h:99
TFileCacheRead * GetCacheRead(TObject *tree=0) const
Return a pointer to the current read cache.
Definition: TFile.cxx:1214
virtual char * GetAddress() const
Definition: TBranch.h:162
TObject * fNotify
! Object to be notified when loading a Tree
Definition: TTree.h:108
virtual Int_t SetCacheEntryRange(Long64_t first, Long64_t last)
interface to TTreeCache to set the cache entry range
Definition: TTree.cxx:8329
void Add(TObject *obj)
Definition: TObjArray.h:73
Double_t Atof() const
Return floating-point value contained in string.
Definition: TString.cxx:2041
A TTree object has a header with a name and a title.
Definition: TTree.h:70
#define gDirectory
Definition: TDirectory.h:213
TVirtualIndex * fTreeIndex
Pointer to the tree Index (if any)
Definition: TTree.h:117
TFriendLock & operator=(const TFriendLock &)
Assignment operator.
Definition: TTree.cxx:498
Int_t SetBranchAddressImp(TBranch *branch, void *addr, TBranch **ptr)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7955
virtual TVirtualIndex * BuildIndex(const TTree *T, const char *majorname, const char *minorname)=0
void ResetBit(UInt_t f)
Definition: TObject.h:171
unsigned char UChar_t
Definition: RtypesCore.h:34
virtual void SetDefaultEntryOffsetLen(Int_t newdefault, Bool_t updateExisting=kFALSE)
Update the default value for the branch&#39;s fEntryOffsetLen.
Definition: TTree.cxx:8438
virtual TObject ** GetObjectRef(const TObject *obj) const =0
const ROOT::Detail::TSchemaRuleSet * GetSchemaRules() const
Return the set of the schema rules if any.
Definition: TClass.cxx:1843
TObject * Next()
Go the next friend element.
Definition: TTree.cxx:9274
Definition: first.py:1
virtual Color_t GetMarkerColor() const
Return the marker color.
Definition: TAttMarker.h:31
virtual void DropBaskets()
Remove some baskets from memory.
Definition: TTree.cxx:4283
Int_t FlushBaskets()
Flush to disk all the baskets of this branch and any of subbranches.
Definition: TBranch.cxx:1087
virtual TSQLResult * Query(const char *varexp, const char *selection, Option_t *option, Long64_t nentries, Long64_t firstentry)=0
virtual TIterator * GetIteratorOnAllLeaves(Bool_t dir=kIterForward)
Creates a new iterator that will go through all the leaves on the tree itself and its friend...
Definition: TTree.cxx:5764
static Long64_t fgMaxTreeSize
Maximum size of a file containing a Tree.
Definition: TTree.h:135
virtual Style_t GetFillStyle() const
Return the fill area style.
Definition: TAttFill.h:31
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition: TObject.cxx:908
virtual void WriteHeader()
Write File Header.
Definition: TFile.cxx:2492
Bool_t IsWritable() const
virtual Long64_t GetEntryNumberFriend(const TTree *)=0
virtual TFile * GetFile(Int_t mode=0)
Return pointer to the file where branch buffers reside, returns 0 in case branch buffers reside in th...
Definition: TBranch.cxx:1492
virtual Long64_t SetEntries(Long64_t n=-1)
Change number of entries in the tree.
Definition: TTree.cxx:8509
R__EXTERN TInterpreter * gCling
Definition: TInterpreter.h:528
TBranch * GetBranch() const
Definition: TLeaf.h:71
virtual void Compress()
Remove empty slots from array.
Definition: TObjArray.cxx:333
virtual TFile * ChangeFile(TFile *file)
Called by TTree::Fill() when file has reached its maximum fgMaxTreeSize.
Definition: TTree.cxx:2584
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
static TTree * MergeTrees(TList *list, Option_t *option="")
Static function merging the trees in the TList into a new tree.
Definition: TTree.cxx:6525
A TTree is a list of TBranches.
Definition: TBranch.h:59
virtual const char * GetName() const
Returns name of object.
Definition: TRealData.h:52
A TSelector object is used by the TTree::Draw, TTree::Scan, TTree::Process to navigate in a TTree and...
Definition: TSelector.h:33
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
void SetHistLineColor(Color_t color=1)
Definition: TStyle.h:354
Bool_t IsaPointer() const
Return true if data member is a pointer.
virtual void Print(Option_t *option="") const
Print TBranch parameters.
Definition: TBranch.cxx:1908
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition: TEnv.cxx:491
A List of entry numbers in a TTree or TChain.
Definition: TEntryList.h:25
virtual Int_t AddBranchToCache(const char *bname, Bool_t subbranches=kFALSE)
Add branch with name bname to the Tree cache.
Definition: TTree.cxx:986
const Bool_t kTRUE
Definition: RtypesCore.h:87
Int_t fPacketSize
! Number of entries in one packet for parallel root
Definition: TTree.h:100
void Set(Int_t n)
Set size of this array to n doubles.
Definition: TArrayD.cxx:106
const Int_t n
Definition: legend1.C:16
TString & String()
Definition: TString.h:110
virtual Long64_t Project(const char *hname, const char *varexp, const char *selection="", Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Make a projection of a tree using selections.
Definition: TTree.cxx:7106
Line Attributes class.
Definition: TAttLine.h:18
virtual Long64_t GetEntryNumberWithIndex(Long64_t major, Long64_t minor) const =0
virtual Bool_t IsObjectOwner() const
Int_t GetCompressionLevel() const
Definition: TFile.h:372
Bool_t IsObject() const
Definition: TRealData.h:56
TIOFeatures fIOFeatures
IO features to define for newly-written baskets and branches.
Definition: TTree.h:105
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Definition: TMath.h:1092
static constexpr Long64_t kMaxEntries
Definition: TTree.h:206
TBranch * GetMother() const
Get our top-level parent branch in the tree.
Definition: TBranch.cxx:1709
char name[80]
Definition: TGX11.cxx:109
TFriendLock(const TFriendLock &)
Copy constructor.
Definition: TTree.cxx:488
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:866
virtual const char * GetName() const
Return name of this collection.
virtual Long64_t GetEntriesFriend() const
Return pointer to the 1st Leaf named name in any Branch of this Tree or any branch in the list of fri...
Definition: TTree.cxx:5205
virtual void SetName(const char *name)
Change the name of this tree.
Definition: TTree.cxx:8693
Int_t GetCompressionSettings() const
Definition: TFile.h:378
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
Long64_t GetEstimatedClusterSize()
In the case where the cluster size was not fixed (old files and case where autoflush was explicitly s...
Definition: TTree.cxx:571
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual void PrintCacheStats(Option_t *option="") const
Print statistics about the TreeCache for this tree.
Definition: TTree.cxx:6980
void MoveReadCache(TFile *src, TDirectory *dir)
Move a cache from a file to the current file in dir.
Definition: TTree.cxx:6663
virtual void CopyAddresses(TTree *, Bool_t undo=kFALSE)
Set branch addresses of passed tree equal to ours.
Definition: TTree.cxx:3121
virtual TObjArray * GetListOfLeaves()
Definition: TTree.h:408
static void ResetCount()
Static function resetting fgCount.
Definition: TBranch.cxx:2232
Long64_t fSavedBytes
Number of autosaved bytes.
Definition: TTree.h:79
Long64_t fCacheSize
! Maximum size of file buffers
Definition: TTree.h:96
virtual const char * GetFriendAlias(TTree *) const
If the &#39;tree&#39; is a friend, this method returns its alias name.
Definition: TTree.cxx:5714
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Return a pointer to a newly allocated object of this class.
Definition: TClass.cxx:4792
Int_t GetNleaves() const
Definition: TBranch.h:197
virtual Long64_t AutoSave(Option_t *option="")
AutoSave tree header every fAutoSave bytes.
Definition: TTree.cxx:1386
const char * Data() const
Definition: TString.h:345
std::atomic< Long64_t > fIMTTotBytes
! Total bytes for the IMT flush baskets
Definition: TTree.h:140