Skip to content

Commit 63ef302

Browse files
authored
[tree] Allow skipping longdoubles when building tree index
Fixes #19560
1 parent 1083c87 commit 63ef302

File tree

12 files changed

+129
-20
lines changed

12 files changed

+129
-20
lines changed

roottest/root/tree/index/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# This is a template for all makefiles.
22

33
#Set the list of files to be deleted by clean (Targets can also be specified).:
4-
CLEAN_TARGETS += $(ALL_LIBRARIES) *.log *.clog newTestFile.root index64.root
4+
CLEAN_TARGETS += $(ALL_LIBRARIES) *.log *.clog newTestFile.root index64.root indexl64.root
55

66
# Set the list of target to make while testing. By default, mytest is the
77
# only target added. If the name of the target is changed in the rules then
88
# the name should be changed accordingly in this list.
99

10-
TEST_TARGETS += chain mytest index64
10+
TEST_TARGETS += chain mytest index64 indexl64
1111

1212
# Search for Rules.mk in roottest/scripts
1313
# Algorithm: Find the current working directory and remove everything after
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Processing runindexl64.C...
3+
Tree BuildIndex returns 9
4+
Entries in chain: 9
5+
BuildIndex returns 9
6+
Try to find the position of run=0, event=500 in the chain, as it does not exist, this should return a -1:
7+
-1
8+
Try to find the position of run=5, event=bigval in the chain, which was inserted in position 4:
9+
4
10+
Entries in chain: 9
11+
BuildIndex returns 1
12+
Try to find the position of run=0, event=500 in the chain, as it does not exist, this should return a -1:
13+
-1
14+
Try to find the position of run=5, event=bigval in the chain, which was inserted in position 4:
15+
4
16+
(int) 0
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "TFile.h"
2+
#include "TChain.h"
3+
#include "Riostream.h"
4+
5+
bool test(TTree *);
6+
7+
const char *fname = "indexl64.root";
8+
https : // github.com/root-project/root/pull/19561
9+
const Long64_t bigval =
10+
0x0FFFFFFFFFFFFFFF; // here we skip long double, so we can go higher than with runindex64.C
11+
12+
int runindexl64()
13+
{
14+
15+
///////////////////////////////////////////////////
16+
// Make a tree and a file and write them to disk //
17+
///////////////////////////////////////////////////
18+
19+
TFile file(fname, "recreate");
20+
TTree *tree = new TTree("testTree", "my test tree");
21+
ULong64_t run, event;
22+
// ULong64 is "l"
23+
tree->Branch("run", &run, "run/l");
24+
tree->Branch("event", &event, "event/l");
25+
26+
// clang-format off
27+
ULong64_t runs[] = { 8,5,5,5, 5, 0, 4, 6, bigval};
28+
ULong64_t events[] = { 0,1,3,2, bigval, 5, bigval, 3, bigval};
29+
// clang-format on
30+
for (size_t i = 0; i < sizeof(events) / sizeof(*events); i++) {
31+
run = runs[i];
32+
event = events[i];
33+
tree->Fill();
34+
}
35+
tree->Write();
36+
37+
bool pass = true;
38+
cout << "Tree BuildIndex returns " << tree->BuildIndex("run", "event", true, true) << endl;
39+
for (size_t i = 0; i < sizeof(events) / sizeof(*events); i++) {
40+
run = runs[i];
41+
event = events[i];
42+
pass &= (tree->GetEntryNumberWithIndex(run, event) == i);
43+
}
44+
if (!pass) {
45+
tree->Scan("run:event", "", "colsize=30");
46+
for (size_t i = 0; i < sizeof(events) / sizeof(*events); i++) {
47+
run = runs[i];
48+
event = events[i];
49+
cout << i << ": Run " << run << ", Event " << event
50+
<< " found at entry number: " << tree->GetEntryNumberWithIndex(run, event) << endl;
51+
}
52+
}
53+
54+
test(tree);
55+
file.Close();
56+
57+
////////////////////////////////////////////////////
58+
// Try loading back in as a TChain //
59+
////////////////////////////////////////////////////
60+
TChain *chain = new TChain("testTree");
61+
chain->Add(fname);
62+
bool result = !test(chain);
63+
64+
delete chain;
65+
66+
return result;
67+
}
68+
69+
bool test(TTree *chain)
70+
{
71+
cout << "Entries in chain: " << chain->GetEntries() << endl;
72+
cout << "BuildIndex returns " << chain->BuildIndex("run", "event", true, true) << endl;
73+
cout << "Try to find the position of run=0, event=500 in the chain, as it does not exist, this should return a -1:"
74+
<< endl;
75+
cout << chain->GetEntryWithIndex(500) << endl;
76+
cout << "Try to find the position of run=5, event=bigval in the chain, which was inserted in position 4:" << endl;
77+
cout << chain->GetEntryNumberWithIndex(5, bigval) << endl;
78+
return (chain->GetEntryNumberWithIndex(500) == -1) && (chain->GetEntryNumberWithIndex(5, bigval) == 4);
79+
}

tree/tree/inc/TTree.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,10 @@ class TTree : public TNamed, public TAttLine, public TAttFill, public TAttMarker
451451
virtual TBranch *BranchOld(const char* name, const char* classname, void* addobj, Int_t bufsize = 32000, Int_t splitlevel = 1);
452452
virtual TBranch *BranchRef();
453453
void Browse(TBrowser*) override;
454-
virtual Int_t BuildIndex(const char *majorname, const char *minorname = "0");
454+
virtual Int_t BuildIndex(const char *majorname, const char *minorname = "0", bool long64major = false, bool long64minor = false);
455+
/// Build index with only a major formula. Minor formula will be set to "0" ie skip.
456+
/// \see TTree::BuildIndex(const char *, const char *, bool, bool)
457+
Int_t BuildIndex(const char *majorname, bool long64major) { return BuildIndex(majorname, "0", long64major, false); }
455458
TStreamerInfo *BuildStreamerInfo(TClass* cl, void *pointer = nullptr, bool canOptimize = true);
456459
virtual TFile *ChangeFile(TFile* file);
457460
virtual TTree *CloneTree(Long64_t nentries = -1, Option_t* option = "");

tree/tree/inc/TVirtualTreePlayer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class TVirtualTreePlayer : public TObject {
4646

4747
TVirtualTreePlayer() { }
4848
~TVirtualTreePlayer() override;
49-
virtual TVirtualIndex *BuildIndex(const TTree *T, const char *majorname, const char *minorname) = 0;
49+
virtual TVirtualIndex *BuildIndex(const TTree *T, const char *majorname, const char *minorname, bool long64major = false, bool long64minor = false) = 0;
5050
virtual TTree *CopyTree(const char *selection, Option_t *option=""
5151
,Long64_t nentries=kMaxEntries, Long64_t firstentry=0) = 0;
5252
virtual Long64_t DrawScript(const char *wrapperPrefix,

tree/tree/src/TTree.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,9 +2666,9 @@ void TTree::Browse(TBrowser* b)
26662666
/// assigned to the TTree via the TTree::SetTreeIndex() method.
26672667
/// \see TTree::SetTreeIndex()
26682668

2669-
Int_t TTree::BuildIndex(const char* majorname, const char* minorname /* = "0" */)
2669+
Int_t TTree::BuildIndex(const char* majorname, const char* minorname /* = "0" */, bool long64major, bool long64minor)
26702670
{
2671-
fTreeIndex = GetPlayer()->BuildIndex(this, majorname, minorname);
2671+
fTreeIndex = GetPlayer()->BuildIndex(this, majorname, minorname, long64major, long64minor);
26722672
if (fTreeIndex->IsZombie()) {
26732673
delete fTreeIndex;
26742674
fTreeIndex = nullptr;

tree/treeplayer/inc/TChainIndex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class TChainIndex : public TVirtualIndex {
7272
TString fMinorName; // Index minor name
7373
TTreeFormula *fMajorFormulaParent; //! Pointer to major TreeFormula in Parent tree (if any)
7474
TTreeFormula *fMinorFormulaParent; //! Pointer to minor TreeFormula in Parent tree (if any)
75-
std::vector<TChainIndexEntry> fEntries; // descriptions of indices of trees in the chain.
75+
std::vector<TChainIndexEntry> fEntries; // descriptions of indices of trees in the chain.
7676

7777
std::pair<TVirtualIndex*, Int_t> GetSubTreeIndex(Long64_t major, Long64_t minor) const;
7878
void ReleaseSubTreeIndex(TVirtualIndex* index, Int_t treeNo) const;
@@ -83,7 +83,7 @@ class TChainIndex : public TVirtualIndex {
8383

8484
public:
8585
TChainIndex();
86-
TChainIndex(const TTree *T, const char *majorname, const char *minorname);
86+
TChainIndex(const TTree *T, const char *majorname, const char *minorname, bool long64major = false, bool long64minor = false);
8787
~TChainIndex() override;
8888
void Append(const TVirtualIndex *, bool delaySort = false) override;
8989
Long64_t GetEntryNumberFriend(const TTree *parent) override;

tree/treeplayer/inc/TTreeIndex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class TTreeIndex : public TVirtualIndex {
4848

4949
public:
5050
TTreeIndex();
51-
TTreeIndex(const TTree *T, const char *majorname, const char *minorname);
51+
TTreeIndex(const TTree *T, const char *majorname, const char *minorname, bool long64major = false, bool long64minor = false);
5252
~TTreeIndex() override;
5353
void Append(const TVirtualIndex *,bool delaySort = false) override;
5454
bool ConvertOldToNew();

tree/treeplayer/inc/TTreePlayer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class TTreePlayer : public TVirtualTreePlayer {
6161
public:
6262
TTreePlayer();
6363
~TTreePlayer() override;
64-
TVirtualIndex *BuildIndex(const TTree *T, const char *majorname, const char *minorname) override;
64+
TVirtualIndex *BuildIndex(const TTree *T, const char *majorname, const char *minorname, bool long64major = false, bool long64minor = false) override;
6565
TTree *CopyTree(const char *selection, Option_t *option
6666
,Long64_t nentries, Long64_t firstentry) override;
6767
Long64_t DrawScript(const char* wrapperPrefix,

tree/treeplayer/src/TChainIndex.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ TChainIndex::TChainIndex(): TVirtualIndex()
8585
/// ignore this warning except for special cases with prior knowledge that sorting the files and/or entries is actually
8686
/// more expensive, or just not possible.
8787

88-
TChainIndex::TChainIndex(const TTree *T, const char *majorname, const char *minorname)
88+
TChainIndex::TChainIndex(const TTree *T, const char *majorname, const char *minorname, bool long64major, bool long64minor)
8989
: TVirtualIndex()
9090
{
9191
fTree = nullptr;
@@ -122,7 +122,7 @@ TChainIndex::TChainIndex(const TTree *T, const char *majorname, const char *mino
122122
}
123123
}
124124
if (!index) {
125-
chain->GetTree()->BuildIndex(majorname, minorname);
125+
chain->GetTree()->BuildIndex(majorname, minorname, long64major, long64minor);
126126
index = chain->GetTree()->GetTreeIndex();
127127
chain->GetTree()->SetTreeIndex(nullptr);
128128
entry.fTreeIndex = index;

0 commit comments

Comments
 (0)