Problem in converting TSpectrum Background Class functions to pyROOT

Hello everyone!
I am trying to implement the background function in TSpectrum class by following the tutorials given in the website.
The C++ (ROOT) Code provided in the tutorial is given below:

void Background_width() {

Int_t i;

const Int_t nbins = 1024;

Double_t xmin = 0;

Double_t xmax = nbins;

Double_t source[nbins];

gROOT->ForceStyle();

TString dir = gROOT->GetTutorialDir();

TString file = dir+"/spectrum/TSpectrum.root";

TFile *f = new TFile(file.Data());

TH1F *back = (TH1F*) f->Get("back1");

TH1F *d1 = new TH1F("d1","",nbins,xmin,xmax);

TH1F *d2 = new TH1F("d2","",nbins,xmin,xmax);

TH1F *d3 = new TH1F("d3","",nbins,xmin,xmax);

back->GetXaxis()->SetRange(1,nbins);

back->SetTitle("Influence of clipping window width on the estimated background");

back->Draw("L");

TSpectrum *s = new TSpectrum();

for (i = 0; i < nbins; i++) source[i]=back->GetBinContent(i + 1);

s->Background(source,nbins,4,TSpectrum::kBackDecreasingWindow,

TSpectrum::kBackOrder2,kFALSE,

TSpectrum::kBackSmoothing3,kFALSE);

for (i = 0; i < nbins; i++) d1->SetBinContent(i + 1,source[i]);

d1->SetLineColor(kRed);

d1->Draw("SAME L");

for (i = 0; i < nbins; i++) source[i]=back->GetBinContent(i + 1);

s->Background(source,nbins,6,TSpectrum::kBackDecreasingWindow,

TSpectrum::kBackOrder2,kFALSE,

TSpectrum::kBackSmoothing3,kFALSE);

for (i = 0; i < nbins; i++) d2->SetBinContent(i + 1,source[i]);

d2->SetLineColor(kOrange);

d2->Draw("SAME L");

for (i = 0; i < nbins; i++) source[i]=back->GetBinContent(i + 1);

s->Background(source,nbins,8,TSpectrum::kBackDecreasingWindow,

TSpectrum::kBackOrder2,kFALSE,

TSpectrum::kBackSmoothing3,kFALSE);

for (i = 0; i < nbins; i++) d3->SetBinContent(i + 1,source[i]);

d3->SetLineColor(kGreen);

d3->Draw("SAME L");

}

In pyROOT, the code should look like this:

import ROOT

def Background_width():
  """
  Illustrates the influence of the clipping window width on the estimated background.
  """

  nbins = 1024
  xmin = 0
  xmax = nbins

  # Get the tutorial directory
  dir = ROOT.gROOT.GetTutorialDir()
  file_path = dir + "/spectrum/TSpectrum.root"

  # Open the ROOT file
  f = ROOT.TFile.Open(file_path)

  # Get the background histogram
  back = f.Get("back1")

  # Create histograms for different window widths
  d1 = ROOT.TH1F("d1", "", nbins, xmin, xmax)
  d2 = ROOT.TH1F("d2", "", nbins, xmin, xmax)
  d3 = ROOT.TH1F("d3", "", nbins, xmin, xmax)

  # Set the clipping window range for the background histogram
  back.GetXaxis().SetRange(1, nbins)
  back.SetTitle("Influence of clipping window width on the estimated background")

  # Draw the background histogram
  back.Draw("L")

  # Create a TSpectrum object
  spectrum = ROOT.TSpectrum()

  # Get the bin contents of the background histogram
  source = [back.GetBinContent(i + 1) for i in range(nbins)]

  # Estimate background with window width 4
  spectrum.Background(source, nbins, 4, ROOT.TSpectrum.kBackDecreasingWindow,
                      ROOT.TSpectrum.kBackOrder2, False, ROOT.TSpectrum.kBackSmoothing3, False)

  # Fill the first histogram
  for i in range(nbins):
    d1.SetBinContent(i + 1, source[i])

  d1.SetLineColor(ROOT.kRed)
  d1.Draw("SAME L")

  # Estimate background with window width 6
  spectrum.Background(source, nbins, 6, ROOT.TSpectrum.kBackDecreasingWindow,
                      ROOT.TSpectrum.kBackOrder2, False, ROOT.TSpectrum.kBackSmoothing3, False)

  # Fill the second histogram
  for i in range(nbins):
    d2.SetBinContent(i + 1, source[i])

  d2.SetLineColor(ROOT.kOrange)
  d2.Draw("SAME L")

  # Estimate background with window width 8
  spectrum.Background(source, nbins, 8, ROOT.TSpectrum.kBackDecreasingWindow,
                      ROOT.TSpectrum.kBackOrder2, False, ROOT.TSpectrum.kBackSmoothing3, False)

  # Fill the third histogram
  for i in range(nbins):
    d3.SetBinContent(i + 1, source[i])

  d3.SetLineColor(ROOT.kGreen)
  d3.Draw("SAME L")

# Call the function to execute the code
Background_width()

However, I am getting errors due to the following line:

spectrum.Background(source, nbins, 8, ROOT.TSpectrum.kBackDecreasingWindow,
                      ROOT.TSpectrum.kBackOrder2, False, ROOT.TSpectrum.kBackSmoothing3, False)

I think there is a mistake in ‘ROOT.TSpectrum.kBackOrder2’ and similar syntax.
Can you please help me how to rectify this?
Thanks in advance

Hi,

Thanks for the post.
Could you please post also the errors you are seeing, as well as the ROOT version you are using? Slimmig down the example to a few lines would also help a speedy feedback.

Cheers,
Danilo