-
Notifications
You must be signed in to change notification settings - Fork 84
Added rntuple_selector.js #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { readHeaderFooter, RBufferReader } from '../../modules/rntuple.mjs'; | ||
import { R__unzip } from '../../modules/io.mjs'; | ||
import { openFile } from 'jsroot'; | ||
|
||
// Read and process the next data cluster from the RNTuple | ||
function readNextCluster(rntuple, selector) { | ||
const builder = rntuple.builder, | ||
clusterSummary = builder.clusterSummaries[selector.currentCluster], | ||
pages = builder.pageLocations[selector.currentCluster][0].pages; | ||
|
||
selector.currentCluster++; | ||
|
||
// Build flat array of [offset, size, offset, size, ...] to read pages | ||
const dataToRead = pages.flatMap(p => [Number(p.locator.offset), Number(p.locator.size)]); | ||
|
||
return rntuple.$file.readBuffer(dataToRead).then(blobsRaw => { | ||
const blobs = Array.isArray(blobsRaw) ? blobsRaw : [blobsRaw], | ||
unzipPromises = blobs.map((blob, idx) => { | ||
const numElements = Number(pages[idx].numElements); | ||
return R__unzip(blob, 8 * numElements); | ||
}); | ||
|
||
// Wait for all pages to be decompressed | ||
return Promise.all(unzipPromises).then(unzipBlobs => { | ||
const totalSize = unzipBlobs.reduce((sum, b) => sum + b.byteLength, 0), | ||
flat = new Uint8Array(totalSize); | ||
|
||
let offset = 0; | ||
for (const blob of unzipBlobs) { | ||
flat.set(new Uint8Array(blob.buffer || blob), offset); | ||
offset += blob.byteLength; | ||
} | ||
|
||
// Create reader and deserialize doubles from the buffer | ||
const reader = new RBufferReader(flat.buffer); | ||
for (let i = 0; i < clusterSummary.numEntries; ++i) { | ||
selector.tgtobj.myDouble = reader.readF64(); | ||
selector.Process(); | ||
} | ||
|
||
selector.End(); | ||
silverweed marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}); | ||
}); | ||
} | ||
|
||
// TODO args can later be used to filter fields, limit entries, etc. | ||
// Create reader and deserialize doubles from the buffer | ||
function rntupleProcess(rntuple, selector, args) { | ||
return readHeaderFooter(rntuple).then(() => { | ||
selector.Begin(); | ||
selector.currentCluster = 0; | ||
return readNextCluster(rntuple, selector, args); | ||
}); | ||
} | ||
|
||
// test block (runs only in node) | ||
if (typeof window === 'undefined') { | ||
// Defining the object selector | ||
const selector = { | ||
tgtobj: {}, | ||
currentCluster: 0, | ||
count: 0, | ||
sum: 0, | ||
|
||
Begin() { | ||
console.log('Begin processing'); | ||
}, | ||
|
||
Process() { | ||
console.log('Entry : ', this.tgtobj); | ||
this.sum += this.tgtobj.myDouble; | ||
this.count++; | ||
}, | ||
|
||
End() { | ||
silverweed marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (this.count === 0) | ||
console.error('No entries processed'); | ||
else | ||
console.log(`Mean = ${(this.sum / this.count).toFixed(4)} from ${this.count} entries`); | ||
} | ||
}; | ||
|
||
openFile('./simple.root') | ||
.then(file => file.readObject('myNtuple')) | ||
.then(rntuple => { | ||
if (!rntuple) throw new Error('myNtuple not found'); | ||
return rntupleProcess(rntuple, selector); | ||
}) | ||
.then(() => console.log('RNTuple::Process finished')) | ||
.catch(err => console.error(err)); | ||
} | ||
|
||
export { rntupleProcess }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.