Background Subtraction ShowBackground

Hello, apologies for the toddler question, but how can I subtract the background from a TH1 spectra after using ShowBackground?

Thanks in advance, Ben.

h1->Add(h1_background, -1.);

Nuff said.

Hi there. I’m having trouble implementing this. Can anyone tell me what I’m doing wrong?

TH1D* Background = (TH1D*)Spectrum->ShowBackground(20,"same"); //The above compiles fine, but is evidently wrong as I get segfaults the moment I try to do this:

Spectrum->Add(Background, -1.0);

I know Spectrum is a valid TH1D* as it writes correctly to a root file, but ANY operation on Background (SetName, etc) causes a segfault.

Thanks in advance,

Conor

Alright lad, I’m a root rookie, but I got this working. In a rush so can’t chop it up, but the macro attached is a peakfinder which loops over your spectrum, finds a peak - clones it - fits a background - subtracts the background - outputs integral of the peak.

You’ll need at least version 5.12 (maybe - 5.13 is what I use) to get ShowBackground to work. You can set ‘niter’ - the fitting parameter in the code. I found the default of 20 to be ok.

to do the subtraction you need to call the background a separate spectra. It’s a ‘virtual’ object or some shit so root hides it away when it outputs.

Hopefully you can find some useful bits, tarra.

(oh, don’t use this to get the integral, prob better to do a binwidth-indep gaussian, i.e “gausn”/binwidth)
itch_56.C (5.49 KB)

Thanks for the help, but still no luck.
I tried the following, and I’m still getting a segfault:

TString SpecName = Spectrum->GetName(); TString BackGnd = "_background"; TString BackName = SpecName+BackGnd;
            Spectrum->ShowBackground(20, "BackOrder4");

            Spectrum->Draw();

            std::cout << "notseggedyet" << std::endl;
            TH1D *BackGround = (TH1D*)gDirectory->Get(BackName);
            std::cout << "notseggedyet" << std::endl;
            BackGround->Write();
            std::cout << "segfaultBeforeThisLine" << std::endl;

Specifically, the segfault now tells me:

            Failed to read a valid object file image from memory.

Bear in mind, this is in compiled C++, not an interpreted root macro. I don’t know if that makes a difference though.

This is using ROOT 5.15/07

Bumping this as I need to give my summies presentation this week, and I’m still stuck on what is going wrong 8-[

I hear ya. Mine’s tonight, might see some similarities with my IOP one…

[SOLVED] Although I have no idea how :slight_smile:

I think it had something to do with not having a canvas open- I’m not used to the idea that a canvas might be needed even if it is never displayed.

Thanks jal, Your code was a useful check, especially since I needed to do a similar fit afterwards. Love the variable naming convention by the way!

For those interested, the final code is attached.
overlayhistos.C (4.93 KB)

In the below code we have ROOTNET.Interface.NTH1F it is an interface, where ROOTNET.NTH1F is a class

using ROOTNET.Utility;
using ROOTNET.Interface;

NTH1F g = new ROOTNET.NTH1F("g", "background removal", doubleArrayX.Length - 1,    
    doubleArrayX);

g.SetContent(doubleArrayY);
g.GetXaxis().SetRange(xmin, xmax);

ROOTNET.NTH1F bkg = new ROOTNET.NTH1F(g);
bkg.Reset();

bkg.Add(g.ShowBackground(80, ""));

In the above Im expecting the backgroung removed values to be saved in bkg but bkg contains all zeros, can you please help me in getting background removed values of g into bkg.

Hi

I’d suggest breaking it down a bit. Check out the itch_56.C I wrote (above post).

Your approach is alien to me. Do not explain it.

Definitely save (& check) all the fits (c1->SaveAs(“blah.png”):wink: as showbackground must be closely observed (& adjust via the range and niter parameter, i.e. hist->ShowBackground(15):wink:

First step, in a terminal make sure this works.

rt yourfile.root
TH1F h4;
TH1F h4b;
h4 =(TH1F
)gFile->Get(“yourhist”);
h4->Draw();
h4->GetXaxis()->SetRangeUser(2000,7000);
h4->ShowBackground(20);
h4b =(TH1F
)gFile->Get(“yourhist_background”);
h4->Add(h4b,-1);
h4->Draw();

Then try & automate it (as in itch_56.C)

Hello
How are you Jal
I have followed your subjects and comments with respect to ROOT specially that subjects talk about histograms fitting and background subtraction because this subjects is mean all things about my data while my information is very low in ROOT , C++ and any programming language.
If you don’t mine my dear I want your help to analysis my data
All MY Regards
Nana