module has no attribute OOP in python

Question:

I’m very new to OOP in python.

I wrote a python script which takes an input file and plots some data from it. Now I want to make the same plots for several different data files, so I thought I’d make my script into a class.

This class lives in a file called TargetRunAnalyser.py and goes like this so far:

from ROOT import TFile, TCanvas
import os, sys

def tot_event_waveforms(root_file):
    sd = root_file.Get("Target/Waveforms")
    NEvents=0
    for keyname in sd.GetListOfKeys():
        NEvents+=1
    NEvents/=32
    return NEvents

class TargetRunAnalyser:

    x_channels = [8,7,9,6,10,5,11,4,12,3,13,2,14,1,15,0]
    y_channels = [27,28,26,29,25,30,24,31,23,16,22,17,21,18,20,19]
    
    intersection = set(x_channels) & set(y_channels)
    
    if len(x_channels) != 16 or len(y_channels) != 16:
        print(f"x_channel array length: {len(x_channels)}, y_channel array length: {len(y_channels)}")
        raise Exception("Check channel arrays, they should both have 16 elements")
    
    if len(intersection) !=0:
        raise Exception(f"The following channels are in both x and y: {intersection}")
    
    def __init__(self,run_no):
        self.run_no = run_no
        self.file_path=os.path.join("/Users/bethlong/Downloads/", f"Run{self.run_no}_HistoFile.root")
        self.root_file = TFile(self.file_path,"read")
        self.n_event_print = 100

        #check how many events you have waveforms for so that you don't print more
        n_event_tot = tot_event_waveforms(self.root_file)

        if self.n_event_print>n_event_tot:
            self.n_event_print = n_event_tot

    def canvas_creator(self, canv_name):
        my_canvas = TCanvas("mycanv","mycanv",900,700)
        return my_canvas

I call this class in a separate file which goes like this:

import TargetRunAnalyser
import ROOT

def main():
    RunsToAnalyse = [50252,50305]
    Analysers = [TargetRunAnalyser.TargetRunAnalyser(run) for run in RunsToAnalyse]
    print([i_Analyser.n_event_print for i_Analyser in Analysers])

    canvas_x_signals = TargetRunAnalyser.canvas_creator("x_signals")
    
if __name__ == '__main__':
    main()

But I get the error:

    canvas_x_signals = TargetRunAnalyser.canvas_creator("x_signals")
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'TargetRunAnalyser' has no attribute 'canvas_creator'

Can anyone tell me why TargetRunAnalyser.TargetRunAnalyser(run) is fine but TargetRunAnalyser.canvas_creator("x_signals") isn’t?

Asked By: Beth Long

||

Answers:

Can anyone tell me why TargetRunAnalyser.TargetRunAnalyser(run) is fine

This works because the first TargetRunAnalyser refers to the module, and the second TargetRunAnalyser refers to the class that is in the module.

but TargetRunAnalyser.canvas_creator("x_signals") isn’t?

This doesn’t work because TargetRunAnalyser refers only to the module, not the class.

It might be clearer to name the module something else, so the module and the class do not have identical names.

Answered By: John Gordon
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.