# Trouble with read ROOT file in React

**URL:** <https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080>\
**Category:** Newbie\
**Tags:** jsroot\
**Created:** [February 6, 2024, 6:27pm UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080 "2024-02-06T18:27:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fljs825](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@fljs825](https://root-forum.cern.ch/u/fljs825)\
**Post date:** [February 6, 2024, 6:27pm UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080/1 "2024-02-06T18:27:23Z")

</div>

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](https://jsroot.gsi.de/latest/api.htm)_ . Below is the code snippet I used, which closely resembles the reference:

```auto
<!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:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/f/8/f8715f52b7687520b9a92436f46d2ad6ccddb2b2.png)

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:

```auto
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](https://root-forum.cern.ch/uploads/short-url/hj3GzYePlj4CD7xtWeK9M70jvTV.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?

* * *

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [February 7, 2024, 5:01am UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080/2 "2024-02-07T05:01:27Z")

</div>

Hi,

I add @linev in the loop, the expert of JS graphics.

Best,  
Danilo

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [February 7, 2024, 7:38am UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080/3 "2024-02-07T07:38:40Z")

</div>

Hi,

First of all - are you sure that path `"./histo_dt_4000.root"` to your file correct?  
Try to open browser console and check status of http requests to your file.  
It should looks like:

 ![RootFileReading](https://root-forum.cern.ch/uploads/default/original/3X/1/8/18fb57b8f0c492a83008e1ecf39432265ad42110.png)

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![fljs825](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@fljs825](https://root-forum.cern.ch/u/fljs825)\
**Post date:** [February 7, 2024, 9:31am UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080/4 "2024-02-07T09:31:37Z")

</div>

Thank you all. The following is my directory structure. Since App.js and my root file (histo\_dt\_4000.root) are in the same directory, I believe the path is correct.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/4/a/4ae7663ff375ad3bee1019592a4c25ab90ca1a6b.png)  
When I check the status of HTTP requests, it seems that the file I’m trying to load does not exist.  
 ![Screenshot 2024-02-07 at 6.25.23 PM](https://root-forum.cern.ch/uploads/default/original/3X/3/0/30362bcf6fca55cece56c6b99f28be5db36cc269.png)  
Even though I imported ‘openFile’ and ‘draw’ from jsroot/modules/main.mjs, I cannot see the screen as you shared. Is there something that I’m missing?

```auto
// App.js
/* eslint-disable */

import React, { useState, useEffect } from 'react';
import './App.css';
import { openFile, draw } from '../node_modules/jsroot/modules/main.mjs';

```

Thanks!

---

<div class="post-metadata">

**Author:** ![fljs825](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@fljs825](https://root-forum.cern.ch/u/fljs825)\
**Post date:** [February 7, 2024, 9:37am UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080/5 "2024-02-07T09:37:01Z")

</div>

Here is my app.js:

```auto
/* eslint-disable */

import React, { useState, useEffect } from 'react';
import './App.css';
import { openFile, draw } from '../node_modules/jsroot/modules/main.mjs';

function App() {
    
    let [title, title_ch] = useState(['blah1', 'blah2', 'blah3']);
    let mainfont = { color : 'white', fontSize : '30px' };
    let [compo, compo_ch] = useState(false);
    let [detail1, setdetail1] = useState('detail');
    
    let [like_num, like_num_ch] = useState(0);

    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)
      };
    }, []);

    function section_ch(){
        var newArr = [...title];
        newArr[0] = 'blah_extra';
        title_ch( newArr );
    }
    
    function section_pre(){
        var newArr2 = [...title];
        newArr2[0] = 'blah1';
        title_ch( newArr2 );
    }

  return (
    <div className="App">
        <div className="black-nav">
          <div style={ mainfont }><a href="index.html" className="homepage">qwerty</a></div>
        </div>
          
          <nav>
              <h2>Welcome!</h2>
              <h3>Contents</h3>
              <ul>
                  <li>
                      <a href="index.html"><p>Basic Calculator</p></a>
          
                      <a href="index.html"><p>Matrix Mutlplier</p></a>
                </li>
            </ul>
        </nav>
        
        <div className="mainbody">
          <button onClick={ section_pre }>Previous</button>
          <button onClick={ section_ch }>Next</button>
        <div className="list">
          <h3>My first react proejct</h3>
          <p>Feb 7, 2024</p>
          <hr/>
        </div>
        
        <div className="list">
          <h3> { title[0] } <span onClick={ ()=>{ like_num_ch(like_num + 1) } }>🧡</span> { like_num } </h3>
          <p>Feb 7, 2024</p>
          <hr/>
        </div>
          
        <div className="list">
          <h3>{ title[1] } <span onClick= { ()=> { compo_ch(!compo)}}> { compo ? "hold up" : "details" }</span>

              </h3>
          <p>Feb 7, 2024</p>
          <hr/>

        </div>
          {compo && <Modal onClose={()=>compo_ch(false)} />}
          </div>
    </div>
  );
}

function Modal({ onClose }){
    return (
            <div className="modal">
              <h2>Title</h2>
              <p>Date : </p>
              <p>Details : </p>
            <button onClick={ onClose }>Hold up</button>
            </div>
            );
}

export default App;

```

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [February 7, 2024, 10:17am UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080/6 "2024-02-07T10:17:06Z")

</div>

> [@fljs825](#):
>
> Even though I imported ‘openFile’ and ‘draw’ from jsroot/modules/main.mjs, I cannot see the screen as you shared. Is there something that I’m missing?

My screen may look different as yours. I am using Firefox browser, your browser most probably Safari.

Please try to find in network logs request to “histo\_dt\_4000.root” file and check that are return codes from the server. Either it is 404 (file is missing) or may be some other problem. I just want to see which kind of errors are reported in network logs.

You also can try to see browser console for any suspicious messages there.

---

<div class="post-metadata">

**Author:** ![fljs825](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@fljs825](https://root-forum.cern.ch/u/fljs825)\
**Post date:** [February 7, 2024, 10:37am UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080/7 "2024-02-07T10:37:13Z")

</div>

This shows the error message.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/a/8ad9b44824001cdfb9541a3ee9f7421512bb8e6a.png)

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/1/4/14ed3e4e228ea8e442f5828ab0168490b28aaa64.png)

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [February 7, 2024, 11:26am UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080/8 "2024-02-07T11:26:15Z")

</div>

Please check network console once again. It should include requests to “histo\_dt\_4000.root” file.

If you cannot find - can you provide me a code that I can test myself.

---

<div class="post-metadata">

**Author:** ![fljs825](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@fljs825](https://root-forum.cern.ch/u/fljs825)\
**Post date:** [February 7, 2024, 12:31pm UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080/9 "2024-02-07T12:31:39Z")

</div>

As I added the `histo_dt_4000.root` file into the `public` directory, then I can see the response. I attached the screenshot of it. Is this the correct action you were referring to? In my view, it seems that the file was read successfully, but the error message still says `Not a ROOT file ./histo_dt_4000.root`.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/c/6/c6d2f06c3f51b9bff61719dc4df2d93827239f35.jpeg)

I’m not sure that I’m providing the correct response, so I attached the file.

> **[myweb.zip](https://drive.google.com/file/d/1IVHK3q_aQt7hMzEkYeK9QjZ3gqkydNHB/view?usp=sharing)**
>
> Google Drive file.

Or  
[myweb.zip](https://root-forum.cern.ch/uploads/short-url/rXq5yhadqAPpNHLT7JRsNU3ev9H.zip) (287.7 KB)  
which is not including `node_modules` directory. (By running `create-react-app`, react automatically generated this directory.)

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [February 7, 2024, 12:38pm UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080/10 "2024-02-07T12:38:10Z")

</div>

Hi,

I can see that content range of your file is 0-1023/1950.  
It does not look like valid size of ROOT file - it must be at least 5K.

Maybe server does not like extra url “stamp” parameter which is submitted with http query.  
This is special JSROOT feature to avoid nasty browser caching when reading same file many times.  
But one can disable this feature. Just add following code:

```auto
import { openFile, draw, settings } from '../node_modules/jsroot/modules/main.mjs';

settings.UseStamp = false;

```

Maybe this helps.

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![fljs825](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@fljs825](https://root-forum.cern.ch/u/fljs825)\
**Post date:** [February 7, 2024, 12:46pm UTC](https://root-forum.cern.ch/t/trouble-with-read-root-file-in-react/58080/11 "2024-02-07T12:46:33Z")

</div>

Oh, Now it works!!! Thank you !!!
