|
| 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 | +} |
0 commit comments