|
1 | 1 | //
|
2 | 2 | // View a region of a RAM file.
|
3 | 3 | //
|
4 |
| -// Author: Jose Javier Gonzalez Ortiz, 5/7/2017 |
| 4 | +// Author: Fons Rademakers, 7/12/2017 |
5 | 5 | //
|
6 | 6 |
|
7 | 7 | #include <iostream>
|
8 |
| -#include <string> |
| 8 | +#include <cstring> |
9 | 9 |
|
10 | 10 | #include <TBranch.h>
|
11 | 11 | #include <TTree.h>
|
12 | 12 | #include <TFile.h>
|
13 | 13 | #include <TStopwatch.h>
|
| 14 | +#include <TString.h> |
| 15 | +#include <TTreeIndex.h> |
14 | 16 | #include <TTreePerfStats.h>
|
15 | 17 |
|
| 18 | +#include "utils.h" |
| 19 | + |
16 | 20 | #include "ramrecord.C"
|
17 | 21 |
|
18 | 22 |
|
19 |
| -void ramview(const char *file, const char *query, bool cache = false, bool perfstats = false, |
| 23 | +void ramview(const char *file, const char *query, bool cache = true, bool perfstats = false, |
20 | 24 | const char *perfstatsfilename = "perf.root")
|
21 | 25 | {
|
22 | 26 | TStopwatch stopwatch;
|
23 | 27 | stopwatch.Start();
|
24 | 28 |
|
25 | 29 | // Open the file and load tree and reader
|
26 | 30 | auto f = TFile::Open(file);
|
| 31 | + if (!f) { |
| 32 | + printf("ramview: failed to open file %s\n", file); |
| 33 | + return; |
| 34 | + } |
27 | 35 | auto t = RAMRecord::GetTree(f);
|
28 | 36 |
|
29 | 37 | RAMRecord *r = 0;
|
30 | 38 |
|
31 |
| - if (!cache) { |
| 39 | + if (!cache) |
32 | 40 | t->SetCacheSize(0);
|
33 |
| - } |
34 | 41 |
|
35 | 42 | TTreePerfStats *ps = 0;
|
36 |
| - |
37 |
| - if (perfstats) { |
| 43 | + if (perfstats) |
38 | 44 | ps = new TTreePerfStats("ioperf", t);
|
39 |
| - } |
40 | 45 |
|
41 | 46 | t->SetBranchAddress("RAMRecord.", &r);
|
42 | 47 |
|
43 | 48 | TBranch *b = t->GetBranch("RAMRecord.");
|
44 | 49 |
|
45 |
| - // Parse queried region string |
| 50 | + // Parse queried region string (rname:pos1-pos2): chr1:5000-6000 |
46 | 51 | std::string region = query;
|
47 | 52 | int chrDelimiterPos = region.find(":");
|
48 | 53 | TString rname = region.substr(0, chrDelimiterPos);
|
49 | 54 |
|
50 | 55 | int rangeDelimiterPos = region.find("-");
|
51 | 56 |
|
52 |
| - UInt_t rangeStart = std::stoi(region.substr(chrDelimiterPos + 1, rangeDelimiterPos - chrDelimiterPos)); |
53 |
| - UInt_t rangeEnd = std::stoi(region.substr(rangeDelimiterPos + 1, region.size() - rangeDelimiterPos)); |
| 57 | + Int_t range_start = std::stoi(region.substr(chrDelimiterPos + 1, rangeDelimiterPos - chrDelimiterPos)); |
| 58 | + Int_t range_end = std::stoi(region.substr(rangeDelimiterPos + 1, region.size() - rangeDelimiterPos)); |
54 | 59 |
|
55 |
| - // Default values to ensure correctness |
56 |
| - int rnameStart = -1; |
57 |
| - int posStart = -1; |
| 60 | + // Convert rname to refid |
| 61 | + auto refid = RAMRecord::GetRnameRefs()->GetRefId(rname); |
58 | 62 |
|
59 |
| - // Assume RNAME are chunked together |
60 |
| - // We look only at the RNAME column |
61 |
| - // We can only do this when there are columns |
62 |
| - if (b->GetSplitLevel() > 0) { |
| 63 | + // Find starting row in index |
| 64 | + auto start_entry = RAMRecord::GetIndex()->GetRow(refid, range_start); |
| 65 | + auto end_entry = RAMRecord::GetIndex()->GetRow(refid, range_end); |
| 66 | + |
| 67 | + printf("ramview: %s:%d (%lld) - %d (%lld)\n", rname.Data(), range_start, start_entry, |
| 68 | + range_end, end_entry); |
| 69 | + |
| 70 | + if (b->GetSplitLevel() > 0) |
63 | 71 | t->SetBranchStatus("RAMRecord.*", 0);
|
64 |
| - t->SetBranchStatus("RAMRecord.v_rname", 1); |
| 72 | + |
| 73 | + if (b->GetSplitLevel() > 0) { |
| 74 | + t->SetBranchStatus("RAMRecord.v_refid", 1); |
| 75 | + t->SetBranchStatus("RAMRecord.v_pos", 1); |
| 76 | + t->SetBranchStatus("RAMRecord.v_lseq", 1); |
65 | 77 | }
|
66 | 78 |
|
67 |
| - for (int i = 0; i < t->GetEntries(); i++) { |
68 |
| - t->GetEntry(i); |
69 |
| - if (rname.EqualTo(r->GetRNAME())) { |
70 |
| - rnameStart = i; |
| 79 | + for (; start_entry < end_entry; start_entry++) { |
| 80 | + t->GetEntry(start_entry); |
| 81 | + if (r->GetPOS() + r->GetSEQLEN() > range_start) { |
| 82 | + // First valid position for printing |
71 | 83 | break;
|
72 | 84 | }
|
73 | 85 | }
|
74 | 86 |
|
75 |
| - // If the RNAME was found |
76 |
| - if (rnameStart >= 0) { |
77 |
| - |
78 |
| - // We need to look both at the leftmost position (v_pos) |
79 |
| - // as well as the length of sequence (v_lseq) |
80 |
| - if (b->GetSplitLevel() > 0) { |
81 |
| - t->SetBranchStatus("RAMRecord.v_pos", 1); |
82 |
| - t->SetBranchStatus("RAMRecord.v_lseq", 1); |
83 |
| - } |
| 87 | + if (b->GetSplitLevel() > 0) |
| 88 | + t->SetBranchStatus("RAMRecord.*", 1); |
84 | 89 |
|
85 |
| - for (int i = rnameStart; i < t->GetEntries(); i++) { |
86 |
| - t->GetEntry(i); |
87 |
| - |
88 |
| - // If the RNAME region ends |
89 |
| - if (!rname.EqualTo(r->GetRNAME())) { |
90 |
| - break; |
91 |
| - } else { |
92 |
| - if (r->GetPOS() + r->GetSEQLEN() > rangeStart) { |
93 |
| - // Register first valid position for printing |
94 |
| - posStart = i; |
95 |
| - break; |
96 |
| - } |
97 |
| - } |
98 |
| - } |
| 90 | + Long64_t j; |
| 91 | + for (j = start_entry; j < end_entry; j++) { |
| 92 | + t->GetEntry(j); |
| 93 | +// r->Print(); |
| 94 | + } |
99 | 95 |
|
100 |
| - // If the position was found |
101 |
| - if (posStart >= 0) { |
102 |
| - |
103 |
| - // Enable all fields for printing |
104 |
| - if (b->GetSplitLevel() > 0) { |
105 |
| - t->SetBranchStatus("RAMRecord.*", 1); |
106 |
| - } |
107 |
| - for (int i = posStart; i < t->GetEntries(); i++) { |
108 |
| - t->GetEntry(i); |
109 |
| - |
110 |
| - // If the RNAME region ends |
111 |
| - if (!rname.EqualTo(r->GetRNAME())) { |
112 |
| - break; |
113 |
| - } else { |
114 |
| - // Within the region |
115 |
| - if (r->GetPOS() <= rangeEnd) { |
116 |
| - r->Print(); |
117 |
| - } else { |
118 |
| - break; |
119 |
| - } |
120 |
| - } |
121 |
| - } |
122 |
| - } |
| 96 | + t->GetEntry(j); |
| 97 | + while (r->GetPOS() < range_end) { |
| 98 | +// r->Print(); |
| 99 | + j++; |
| 100 | + t->GetEntry(j); |
123 | 101 | }
|
124 | 102 |
|
125 | 103 | stopwatch.Print();
|
126 | 104 |
|
127 | 105 | if (perfstats) {
|
128 | 106 | ps->SaveAs(perfstatsfilename);
|
129 | 107 | delete ps;
|
130 |
| - printf("Reading %lld bytes in %d transactions\n", f->GetBytesRead(), f->GetReadCalls()); |
131 | 108 | }
|
132 | 109 | }
|
0 commit comments