Skip to content

Commit e49a058

Browse files
committed
elsint of tree demos
1 parent 0809c94 commit e49a058

File tree

4 files changed

+52
-49
lines changed

4 files changed

+52
-49
lines changed

demo/node/tree_draw.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@ let arg = 'https://root.cern/js/files/hsimple.root';
1010
if (process.argv && (process.argv[2] == 'buf'))
1111
arg = await httpRequest(arg, 'buf');
1212

13-
const file = await openFile(arg);
14-
1513
// now read ntuple, perform Draw operation, create SVG file and save to the disk
16-
const ntuple = await file.readObject('ntuple');
17-
const hist = await treeDraw(ntuple, 'px:py::pz>5');
14+
const file = await openFile(arg),
15+
ntuple = await file.readObject('ntuple'),
16+
hist = await treeDraw(ntuple, 'px:py::pz>5');
1817
hist.fTitle = 'Example of TTree::Draw';
1918
const svg = await makeSVG({ object: hist, width: 1200, height: 800 });
2019
writeFileSync('tree_draw1.svg', svg);
2120
console.log(`Create tree_draw1.svg size ${svg.length}`);
2221

2322
// extract entries list which corresponds to cut expression
24-
const elist = await treeDraw(ntuple, '::pz>5>>elist');
25-
// apply entries list for draw expression
26-
const hist2 = await treeDraw(ntuple, { expr: 'px:py', elist });
23+
// and apply entries list for draw expression
24+
const elist = await treeDraw(ntuple, '::pz>5>>elist'),
25+
hist2 = await treeDraw(ntuple, { expr: 'px:py', elist });
2726
hist2.fTitle = 'Example of TTree::Draw';
2827
const svg2 = await makeSVG({ object: hist2, width: 1200, height: 800 });
2928
writeFileSync('tree_draw2.svg', svg2);
@@ -46,4 +45,4 @@ if (svg !== svg3)
4645
console.error('FAILURE: svg and svg3 do not match');
4746

4847
if ((svg === svg2) && (svg2 === svg3))
49-
console.log('OK: all svg files match!')
48+
console.log('OK: all svg files match!');

demo/node/tree_dump.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { openFile, treeDraw } from 'jsroot';
22

3-
let f = await openFile('https://root.cern/js/files/hsimple.root');
4-
let tree = await f.readObject('ntuple;1');
5-
let dump = await treeDraw(tree, { expr: 'px:py:pz', dump: true, numentries: 1000 });
3+
const f = await openFile('https://root.cern/js/files/hsimple.root'),
4+
tree = await f.readObject('ntuple;1'),
5+
dump = await treeDraw(tree, { expr: 'px:py:pz', dump: true, numentries: 1000 });
66

77
console.log(`NumEntries ${dump.length}`);
88
let sumx = 0, sumy = 0, sumz = 0;
@@ -13,6 +13,6 @@ dump.forEach(entry => {
1313
sumz += entry.z;
1414
});
1515

16-
console.log(`Mean x = ${sumx/dump.length}`);
17-
console.log(`Mean y = ${sumy/dump.length}`);
18-
console.log(`Mean z = ${sumz/dump.length}`);
16+
console.log(`Mean x = ${sumx / dump.length}`);
17+
console.log(`Mean y = ${sumy / dump.length}`);
18+
console.log(`Mean z = ${sumz / dump.length}`);

demo/node/tree_selector.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
1-
import { openFile } from 'jsroot/io';
2-
import { TSelector, treeProcess } from 'jsroot/tree';
1+
import { openFile } from 'jsroot/io';
2+
import { TSelector, treeProcess } from 'jsroot/tree';
33

4-
let selector = new TSelector();
4+
class TSelectorExample extends TSelector {
55

6-
selector.addBranch('px');
7-
selector.addBranch('py');
6+
constructor() {
7+
super();
8+
this.addBranch('px');
9+
this.addBranch('py');
810

9-
let cnt = 0, sumpx = 0, sumpy = 0;
10-
11-
selector.Begin = function() {
12-
// function called before reading of TTree starts
13-
}
14-
15-
selector.Process = function() {
16-
// function called for every entry
17-
sumpx += this.tgtobj.px;
18-
sumpy += this.tgtobj.py;
19-
cnt++;
20-
}
11+
this.cnt = this.sumpx = this.sumpy = 0;
12+
}
2113

22-
selector.Terminate = function(res) {
23-
// function called when processing finishes
24-
if (!res || (cnt === 0)) {
25-
console.error('Fail to process TTree');
26-
} else {
27-
let meanpx = sumpx/cnt, meanpy = sumpy/cnt;
28-
console.log(`MeanPX = ${meanpx.toFixed(4)} MeanPY = ${meanpy.toFixed(4)}`);
14+
Begin() {
15+
// function called before reading of TTree starts
16+
console.log('TTree::Process started');
2917
}
30-
}
3118

19+
Process(/* entry */) {
20+
// function called for every entry
21+
this.sumpx += this.tgtobj.px;
22+
this.sumpy += this.tgtobj.py;
23+
this.cnt++;
24+
}
3225

33-
let file = await openFile('https://root.cern/js/files/hsimple.root');
26+
Terminate(res) {
27+
// function called when processing finishes
28+
if (!res || !this.cnt)
29+
console.error('Fail to process TTree');
30+
else {
31+
const meanpx = this.sumpx / this.cnt, meanpy = this.sumpy / this.cnt;
32+
console.log(`MeanPX = ${meanpx.toFixed(4)} MeanPY = ${meanpy.toFixed(4)}`);
33+
}
34+
console.log('TTree::Process finished');
35+
}
3436

35-
let tree = await file.readObject('ntuple;1');
37+
}
3638

37-
await treeProcess(tree, selector);
39+
const file = await openFile('https://root.cern/js/files/hsimple.root'),
40+
tree = await file.readObject('ntuple;1');
3841

39-
console.log('TTree::Process finished');
42+
await treeProcess(tree, new TSelectorExample);

demo/node/tree_staged.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { version, openFile, treeDraw, treeProcess, TSelector } from 'jsroot';
22

3+
34
console.log(`JSROOT version ${version}`);
45

56
// Macro demonstrates three different methods to produce entries list which match
@@ -18,11 +19,11 @@ function compareArrays(arr1, arr2) {
1819
return true;
1920
}
2021

21-
// open file
22-
const file = await openFile('https://root.cern/js/files/hsimple.root');
22+
// open file and read ntuple object
23+
const file = await openFile('https://root.cern/js/files/hsimple.root'),
24+
ntuple = await file.readObject('ntuple');
2325

24-
// read ntuple
25-
const ntuple = await file.readObject('ntuple');
26+
console.log(`Read ${ntuple.fName} of type ${ntuple._typename}`);
2627

2728
// use treeDraw to extract array of entries which match condition 'pz>5'
2829
const entries1 = await treeDraw(ntuple, '::pz>5>>elist');
@@ -37,7 +38,7 @@ selector.addBranch('pz');
3738
selector.Process = function(entry) {
3839
if (this.tgtobj.pz > 5)
3940
entries3.push(entry);
40-
}
41+
};
4142
await treeProcess(ntuple, selector);
4243
console.log('entries3', entries3.length);
4344

@@ -70,7 +71,7 @@ const selector2 = new TSelector, pxarr3 = [];
7071
selector2.addBranch('px');
7172
selector2.Process = function() {
7273
pxarr3.push(this.tgtobj.px);
73-
}
74+
};
7475
await treeProcess(ntuple, selector2, { elist: entries3 });
7576
console.log('pxarr3', pxarr3.length);
7677

0 commit comments

Comments
 (0)