|
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'; |
3 | 3 |
|
4 | | -let selector = new TSelector(); |
| 4 | +class TSelectorExample extends TSelector { |
5 | 5 |
|
6 | | -selector.addBranch('px'); |
7 | | -selector.addBranch('py'); |
| 6 | + constructor() { |
| 7 | + super(); |
| 8 | + this.addBranch('px'); |
| 9 | + this.addBranch('py'); |
8 | 10 |
|
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 | + } |
21 | 13 |
|
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'); |
29 | 17 | } |
30 | | -} |
31 | 18 |
|
| 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 | + } |
32 | 25 |
|
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 | + } |
34 | 36 |
|
35 | | -let tree = await file.readObject('ntuple;1'); |
| 37 | +} |
36 | 38 |
|
37 | | -await treeProcess(tree, selector); |
| 39 | +const file = await openFile('https://root.cern/js/files/hsimple.root'), |
| 40 | + tree = await file.readObject('ntuple;1'); |
38 | 41 |
|
39 | | -console.log('TTree::Process finished'); |
| 42 | +await treeProcess(tree, new TSelectorExample); |
0 commit comments