# Extract rms from text files and plot

**URL:** <https://root-forum.cern.ch/t/extract-rms-from-text-files-and-plot/54060>\
**Category:** Newbie\
**Tags:** python, hist, root\
**Created:** [March 16, 2023, 8:50am UTC](https://root-forum.cern.ch/t/extract-rms-from-text-files-and-plot/54060 "2023-03-16T08:50:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdash](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@gdash](https://root-forum.cern.ch/u/gdash)\
**Post date:** [March 16, 2023, 8:50am UTC](https://root-forum.cern.ch/t/extract-rms-from-text-files-and-plot/54060/1 "2023-03-16T08:50:47Z")

</div>

I have 50 files and I’m trying to extract rms from each and plot them with file name. I have the following code.

```auto
import os
import ROOT
from array import array

# Define the directory where the text files are located
dir_path = "/home/ganapati/xray/06march/f1"

# Initialize lists to hold the RMS values and file names
rms_list = []
file_list = []

# Loop over all the files in the directory
for file_name in os.listdir(dir_path):
    # Only consider text files
    if file_name.endswith(".txt"):
        # Construct the full file path
        file_path = os.path.join(dir_path, file_name)
        
        # Open the file and extract the RMS value
        with open(file_path, "r") as f:
            lines = f.readlines()
            # Loop over the lines in the file and find the RMS value
            for line in lines:
                if "RMS" in line:
                    # Extract the RMS value from the line
                    rms_value = float(line.split(":")[1])
                    rms_list.append(rms_value)
                    file_list.append(file_name)
                    break

# Create a TGraph object to plot the RMS values
graph = ROOT.TGraph(len(rms_list), array('d', [float(i) for i in range(len(rms_list))]), array('d', rms_list))
graph.SetTitle("RMS Values")
graph.SetMarkerStyle(20)
graph.SetMarkerSize(0.8)

# Set the x-axis labels to the file names
axis = graph.GetXaxis()
for i, file_name in enumerate(file_list):
    axis.SetBinLabel(i+1, file_name)

# Create a canvas to display the plot
canvas = ROOT.TCanvas("canvas", "RMS Values", 800, 600)
canvas.SetLeftMargin(0.15)
canvas.SetBottomMargin(0.15)
graph.Draw("AP")

# Set the x-axis label orientation and range
axis.SetLabelSize(0.03)
axis.SetLabelOffset(0.01)
axis.SetLabelFont(42)
axis.SetLabelAngle(90)
axis.SetRange(0, len(rms_list)+1)

# Save the plot to a PDF file
canvas.Print("rms_values.pdf")

```

and I’m getting the following erro;-

```auto
File "histo_scope_txt1.py", line 32, in <module>
    graph = ROOT.TGraph(len(rms_list), array('d', [float(i) for i in range(len(rms_list))]), array('d', rms_list))
TypeError: none of the 12 overloaded methods succeeded. Full details:
  TGraph::TGraph(const TGraph& gr) =>
    TypeError: takes at most 1 arguments (3 given)
  TGraph::TGraph(const TVectorT<float>& vx, const TVectorT<float>& vy) =>
    TypeError: takes at most 2 arguments (3 given)
  TGraph::TGraph(const TVectorT<double>& vx, const TVectorT<double>& vy) =>
    TypeError: takes at most 2 arguments (3 given)
  TGraph::TGraph(const TH1* h) =>
    TypeError: takes at most 1 arguments (3 given)
  TGraph::TGraph(const TF1* f, const char* option = "") =>
    TypeError: takes at most 2 arguments (3 given)
  TGraph::TGraph() =>
    TypeError: takes at most 0 arguments (3 given)
  TGraph::TGraph(int n) =>
    TypeError: takes at most 1 arguments (3 given)
  TGraph::TGraph(const char* filename, const char* format = "%lg %lg", const char* option = "") =>
    TypeError: could not convert argument 1 (expected string or Unicode object, int found)
  TGraph::TGraph(int n, const double* y, double start = 0., double step = 1.) =>
    TypeError: could not convert argument 2 (could not convert argument to buffer or nullptr)
  TGraph::TGraph(int n, const int* x, const int* y) =>
    TypeError: could not convert argument 2 (could not convert argument to buffer or nullptr)
  TGraph::TGraph(int n, const float* x, const float* y) =>
    TypeError: could not convert argument 2 (could not convert argument to buffer or nullptr)
  TGraph::TGraph(int n, const double* x, const double* y) =>
    TypeError: could not convert argument 2 (could not convert argument to buffer or nullptr)

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 16, 2023, 9:06am UTC](https://root-forum.cern.ch/t/extract-rms-from-text-files-and-plot/54060/2 "2023-03-16T09:06:26Z")

</div>

> [@gdash](#):
>
> ```auto
> File "histo_scope_txt1.py", line 32, in <module>
> graph = ROOT.TGraph(len(rms_list), array('d', [float(i) for i in range(len(rms_list))]), array('d', rms_list))
> 
> ```

The way you constructed the graph is not valid… I do not know python enough to tell what’s wrong.

---

<div class="post-metadata">

**Author:** ![gdash](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@gdash](https://root-forum.cern.ch/u/gdash)\
**Post date:** [March 16, 2023, 9:21am UTC](https://root-forum.cern.ch/t/extract-rms-from-text-files-and-plot/54060/3 "2023-03-16T09:21:53Z")

</div>

Is there any example macro which can help to extract rms from multiple text files and plot(Say text files two columns where first column is time and second is events)? I searched but didn’t find one.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 16, 2023, 9:36am UTC](https://root-forum.cern.ch/t/extract-rms-from-text-files-and-plot/54060/5 "2023-03-16T09:36:01Z")

</div>

You need to protect your macro against cases when “`len(rms_list)`” is equal to `0`.
