Skip to content

Created a new file rntuple_histogram.js #346

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions demo/node/rntuple_histogram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { rntupleProcess } from '../../modules/rntuple.mjs';
import { openFile, TSelector, createHistogram, makeSVG } from 'jsroot';
import { writeFileSync } from 'fs';

const selector = new TSelector();
selector.hist = null;
selector.minpy = null;
selector.maxpy = null;
selector.sum = 0;
selector.count = 0;

selector.Begin = function() {
this.hist = createHistogram('TH1F', 10, -5, 5);
this.hist.fXaxis.fXmin = -5;
this.hist.fXaxis.fXmax = 5;
this.hist.fFillColor = 2; // Red fill
this.hist.fFillStyle = 1001; // Solid
this.hist.fName = 'h1';
this.hist.fTitle = 'myDouble Distribution';
this.hist.fXaxis.fTitle = 'myDouble';
this.hist.fYaxis.fTitle = 'Entries';
this.hist.fXaxis.fLabelSize = 0.02;
this.hist.fYaxis.fLabelSize = 0.02;
};

selector.Process = function() {
const val = this.tgtobj.myDouble;

if (typeof val === 'number') {
this.hist.Fill(val); // Fill histogram
this.sum += val; // Sum for mean
this.count++; // Count
} else
console.warn('Invalid myDouble:', val);
};

selector.Terminate = async function() {
if (this.count === 0)
console.error('No valid entries processed');

const mean = this.sum / this.count;
console.log(`Mean = ${mean.toFixed(4)} from ${this.count} entries`);

const svg = await makeSVG({
object: this.hist,
width: 800,
height: 600
});

writeFileSync('myDouble_histogram.svg', svg);
console.log('Histogram written to myDouble_histogram.svg');
};

if (typeof window === 'undefined') {
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));
}