How can I retrieve the background data from spectrum

One important question @couet is that in the above graph what does this red line signifies?
Is it background fitting? if not then, what else?

You asked to have on the same plot the original graph and the one representing the background. The original one is the black one and the black is the red one. The original one is drawn by the command:

g->Draw("AL");

and the background by the command:

b->Draw("L");

Ok. @couet. If instead of graph i use histogram. How does the above code gets modified?

Yes with histograms your macro needs to be modified for sure. Indeed it will be very similar to this example. Look at it and see how the orignal histogram is copied to the source vector, and how the result is copied back to an histogram. The structure is very similar to the macro I sent you.

For histogram
When i use @couet

root [0] 
Processing fithisto.C...
#0  0x00007f1b5e77f687 in __GI___waitpid (pid=6694, stat_loc=stat_loc
entry=0x7fff7e35a418, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:30
#1  0x00007f1b5e6ea067 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:149
#2  0x00007f1b5f393b23 in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x55866667c7c0) at /home/robert/Downloads/root6/core/unix/src/TUnixSystem.cxx:2119
#3  TUnixSystem::StackTrace (this=0x55866667c7c0) at /home/gourav/Downloads/root6/core/unix/src/TUnixSystem.cxx:2413
#4  0x00007f1b59b8b005 in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /home/robert/Downloads/MyRoot/lib/libCling.so
#5  0x00007f1b59b8aa0b in cling_runtime_internal_throwIfInvalidPointer () from /home/robert/Downloads/MyRoot/lib/libCling.so
#6  0x00007f1b5fb40921 in ?? ()
#7  0x0000000000000004 in ?? ()
#8  0x000000000000001b in ?? ()
#9  0x0000558600000002 in ?? ()
#10 0x9d767539bf7b5a00 in ?? ()
#11 0x0000558600000002 in ?? ()
#12 0x0000558666994e58 in ?? ()
#13 0x00007fff7e35ca10 in ?? ()
#14 0x00007f1b5ba116d5 in (anonymous namespace)::StmtProfiler::VisitStmt(clang::Stmt const*) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#15 0x00007f1b5ba0fd1e in clang::StmtVisitorBase<clang::make_const_ptr, (anonymous namespace)::StmtProfiler, void>::Visit(clang::Stmt const*) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#16 0x00007f1b5ba130eb in clang::Stmt::Profile(llvm::FoldingSetNodeID&, clang::ASTContext const&, bool) const () from /home/robert/Downloads/MyRoot/lib/libCling.so
#17 0x00007f1b5ba144ac in clang::TemplateArgument::Profile(llvm::FoldingSetNodeID&, clang::ASTContext const&) const () from /home/robert/Downloads/MyRoot/lib/libCling.so
#18 0x00007f1b5ba1dbd2 in clang::TemplateSpecializationType::Profile(llvm::FoldingSetNodeID&, clang::TemplateName, llvm::ArrayRef<clang::TemplateArgument>, clang::ASTContext const&) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#19 0x00007f1b5b81f9e5 in llvm::ContextualFoldingSet<clang::TemplateSpecializationType, clang::ASTContext&>::NodeEquals(llvm::FoldingSetBase::Node*, llvm::FoldingSetNodeID const&, unsigned int, llvm::FoldingSetNodeID&) const () from /home/robert/Downloads/MyRoot/lib/libCling.so
#20 0x00007f1b5bd8edcd in llvm::FoldingSetBase::FindNodeOrInsertPos(llvm::FoldingSetNodeID const&, void*&) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#21 0x00007f1b5b830656 in clang::ASTContext::getCanonicalTemplateSpecializationType(clang::TemplateName, llvm::ArrayRef<clang::TemplateArgument>) const () from /home/robert/Downloads/MyRoot/lib/libCling.so
#22 0x00007f1b5a18d797 in clang::ASTReader::readTypeRecord(unsigned int) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#23 0x00007fff7e35d290 in ?? ()
#24 0x00007fff7e35d3a0 in ?? ()
#25 0x00007f1b5bd8e49f in llvm::FoldingSetNodeID::operator==(llvm::FoldingSetNodeID const&) const () from /home/robert/Downloads/MyRoot/lib/libCling.so
#26 0x00007f1b5bd8edcd in llvm::FoldingSetBase::FindNodeOrInsertPos(llvm::FoldingSetNodeID const&, void*&) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#27 0x00007f1b5b829495 in clang::ASTContext::getTemplateTypeParmType(unsigned int, unsigned int, bool, clang::TemplateTypeParmDecl*) const () from /home/robert/Downloads/MyRoot/lib/libCling.so
#28 0x00007f1b5a18d797 in clang::ASTReader::readTypeRecord(unsigned int) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#29 0x00007fff7e35e080 in ?? ()
#30 0x0000000000000080 in ?? ()
#31 0x00007f1b5bda1285 in llvm::SmallVectorBase::grow_pod(void*, unsigned long, unsigned long) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#32 0x00007f1b5bd8de73 in llvm::FoldingSetNodeID::AddPointer(void const*) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#33 0x00007f1b5ba1274e in (anonymous namespace)::StmtProfiler::VisitTemplateArgument(clang::TemplateArgument const&) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#34 0x00007f1b5ba1103a in clang::StmtVisitorBase<clang::make_const_ptr, (anonymous namespace)::StmtProfiler, void>::Visit(clang::Stmt const*) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#35 0x00007f1b5ba11791 in (anonymous namespace)::StmtProfiler::VisitStmt(clang::Stmt const*) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#36 0x00007f1b5ba10254 in clang::StmtVisitorBase<clang::make_const_ptr, (anonymous namespace)::StmtProfiler, void>::Visit(clang::Stmt const*) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#37 0x00007f1b5ba11791 in (anonymous namespace)::StmtProfiler::VisitStmt(clang::Stmt const*) () from /home/robert/Downloads/MyRoot/lib/libCling.so
#38 0x00007fff7e35dd38 in ?? ()
#39 0x00007fff7e35dd38 in ?? ()
#40 0x00007fff7e35dd48 in ?? ()
#41 0x00007fff7e35ddb8 in ?? ()
#42 0x0000000000000004 in ?? ()
#43 0x0000558666b24450 in ?? ()
#44 0x9d767539bf7b5a00 in ?? ()
#45 0x00007fff7e35dd68 in ?? ()
#46 0x00007fff7e35ddc0 in ?? ()
#47 0x0000000000000000 in ?? ()
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
In file included from input_line_10:1:
/home/robert/Downloads/MyRoot/macros/fithisto.C:70:3: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
  back->SetTitle("Estimation of background with decreasing window");
  ^~~~

I am getting this error when i run fithisto.C (4.3 KB)

I am sorry but you blindly copy/pasted the macro I pointed you as example. It makes no sense as you did it . In particular you do something like:

  auto f = TFile::Open("basic.root","RECREATE");
  TH1F *back = (TH1F*) f->Get("back1");

This code comes directly from the the example I pointed. back1 is the histogram used by this example… It has nothing to do with your data. I started to modify your macro but I stopped at some point because it is not possible to understand what you want to do … Can you clean up your macro a little bit and make it more clear ?

1 Like

I thought it was used for defining the background histogram.

Anyways, I will do it. Just give me some time.

Sure … take time to read a bit the examples and the doc.

@couet
This is a rough code. I am still trying.
TH1D *h = new TH1D(“h”, “spectrum;x-value(Channels);#counts”, n, x);
TSpectrum *s = new TSpectrum();
s->Background(h->GetY(), n,
10, TSpectrum::kBackDecreasingWindow,
TSpectrum::kBackOrder8, kTRUE,
TSpectrum::kBackSmoothing5, kTRUE);
h->Draw(“AL”);``
The code in bold i.e. h->GetY() is not correct.
What should i use for that?

h->GetArray() + 1

Thanks @Wile_E_Coyote.


The scale on x-axis and y-axis is not shown.

when i add the existing code with fitted peaks code. i.e.fithisto.C (4.5 KB)

Compare the drawing options in the TGraphPainter with the ones in the THistPainter.

Hi, @Wile_E_Coyote
After looking at the above mentioned links. I was only able to figure out that when i use command for graph i.e. g->Draw(“AL”); it draws scale on the respective axis.
but at the moment for histogram it is not possible for me to distinguish between the two i.e.
h->Draw(“AL”);
& h->Draw(“AH”);

ok @Wile_E_Coyote.
I have got when i use scatter plot.
i.e. h->Draw(" ");

@Wile_E_Coyote
How should i set the log scale of y-axis in the spectrum or histogram?

After drawing: gPad->SetLogy(1);

Thanks @Wile_E_Coyote.
One more thing how do i set a maximum and minimum limits on scale of y-axis.
I tried
gPad->SetMaximum(3000.);//along Y
gPad->SetMinimum(0.);

& h->SetMaximum(3000.);//along Y
h->SetMinimum(0.);
but not working.

Do I get it right that you gPad->SetLogy(1); and h->SetMinimum(0.);? Maybe you should try some math handbook (should teach you how much is “log(0)”).

@Wile_E_Coyote No, gpad is fine but i want to only set limits for y axis.
i have commented gpad and then i was trying for the limits of y.
i was asking when I don’t want to use logy then how can i set limits of y?

ROOT HowTo -> How to set ranges on axis?