Dear all,
I’m currently struggling to create a webpage that can handle ROOT files. Utilizing JSROOT, I managed to successfully read ROOT files and draw histograms on my webpage, thanks to the documentation available at JavaScript ROOT API examples . Below is the code snippet I used, which closely resembles the reference:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Reading object from the ROOT file</title>
</head>
<body>
<h1>Hi</h1>
<div id="drawing" style="width:800px; height:600px"></div>
</body>
<script type='module'>
import { openFile, draw } from './modules/main.mjs';
let file = await openFile('./histo_dt_4000.root');
let obj = await file.readObject('h2;1');
draw('drawing', obj, 'colz');
</script>
</html>
The rendered output appears as shown in the image below:
Now, my objective is to achieve the same functionality within a React environment since I’ve already designed my webpage using React. I attempted to adapt the aforementioned process to React, but I encountered some discrepancies in the syntax between JavaScript and JSX. Here’s where I currently stand:
import { openFile, draw } from '../node_modules/jsroot/modules/main.mjs';
import React, { useState, useEffect } from 'react';
function App() {
async function fetchData() {
try {
let file = await openFile("./histo_dt_4000.root");
let obj = await file.readObject('h2;1');
draw('drawing', obj, 'colz');
} catch (error) {
console.error('Error fetching data:', error);
}
}
useEffect(() => {
fetchData();
// Cleanup function (optional)
return () => {
// Cleanup logic (if needed)
};
}, []);
return ( /*other codes display the webpage*/
);
}
export default App;
However, I encountered an error stating: “Error: Not a ROOT file ./histo_dt_4000.root.” Despite verifying the correctness of my file path multiple times and confirming that my ROOT file is functional, the error persists. I’ve attached my ROOT file containing two histograms for reference:
histo_dt_4000.root (49.9 KB)
I’ve exhaustively searched for solutions to my JSROOT-related issues by referring to other documentation, but finding resources specifically addressing JSROOT in React has proven to be challenging. Therefore, I’ve decided to seek assistance here. Any insights or guidance would be greatly appreciated.
Thank you in advance!
ROOT Version: 6.30.02
Platform: macOS Sonoma 14.2.1
Compiler: iTerm?