Extract rms from text files and plot

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

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;-

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)

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

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.

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