How can I retrieve the background data from spectrum

We cannot help with that we need more complete code …
we need to see how are defined s , n , npeaks etc …
a small complete macro reproducing the error is required.

ok. @couet
I am uploading the complete code.
fithisto.C (3.2 KB)
and this is the text file.
eu_gu_1.txt (44.1 KB)

I do not understand what you are trying to do with this char *Background(s, n, 20); declaration in the middle of your code. There is many background examples in the spectrum tutorial’s (see the link I sent before). See how it is used there.

Thanks @couet
If instead of char *Background(s, n, 20);
i use TH1 *hb = s->Background(h,20,“same”); in my code then is it the correct way for estimating the background?

Look how it is done in the tutorials. Try them and you will understand how to use this functionality.

OK @couet
Using the links that you have mentioned above. I have run a auxiliary file i.e. cleanback.C (410 Bytes)
eu_gu_1.C (44.1 KB)
Please look at it once.

void cleanback()
{
   auto g = new TGraph("eu_gu_1.txt");
   auto s = new TSpectrum();

   double sourceX[6000];
   double sourceY[6000];
   int n = g->GetN();

   for (int i = 0; i < n; i++) {
      sourceX[i] = g->GetPointX(i);
      sourceY[i] = g->GetPointY(i);
   }

   s->Background(sourceY, n,
                10, TSpectrum::kBackDecreasingWindow,
                TSpectrum::kBackOrder8, kTRUE,
                TSpectrum::kBackSmoothing5, kTRUE);

   auto b = new TGraph(n, sourceX, sourceY);
   b->SetLineColor(kRed);

   g->Draw("AL");
   b->Draw("L");

}

And you should play a bit with the parameters of the Background function (again see doc and tutorials).

For instance:

void cleanback()
{
   auto g = new TGraph("eu_gu_1.txt");
   auto s = new TSpectrum();

   double sourceX[6000];
   double sourceY[6000];
   int n = g->GetN();

   for (int i = 0; i < n; i++) {
      sourceX[i] = g->GetPointX(i);
      sourceY[i] = g->GetPointY(i);
   }

   s->Background(sourceY, n,
                10, TSpectrum::kBackDecreasingWindow,
                TSpectrum::kBackOrder2, kTRUE,
                TSpectrum::kBackSmoothing3, kTRUE);

   auto b = new TGraph(n, sourceX, sourceY);
   b->SetLineColor(kRed);
   b->SetLineWidth(5);

   g->Draw("AL");
   b->Draw("L");
}

Thanks @couet.
I am getting this error in root terminal
error: no member named ‘GetPointX’ in ‘TGraph’
sourceX[i] = g->GetPointX(i);

error: no member named ‘GetPointY’ in ‘TGraph’
sourceY[i] = g->GetPointY(i);

void cleanback()
{
   auto C = new TCanvas();
   C->SetLogy();

   auto g = new TGraph("eu_gu_1.txt");
   auto s = new TSpectrum();

   double sourceX[6000];
   double sourceY[6000];
   int n = g->GetN();

   double *x = g->GetX();
   double *y = g->GetY();

   for (int i = 0; i < n; i++) {
      sourceX[i] = x[i];
      sourceY[i] = y[i];
   }

   s->Background(sourceY, n,
                10, TSpectrum::kBackDecreasingWindow,
                TSpectrum::kBackOrder2, kTRUE,
                TSpectrum::kBackSmoothing3, kTRUE);

   auto b = new TGraph(n, sourceX, sourceY);
   b->SetLineColor(kRed);
   b->SetLineWidth(5);

   g->Draw("AL");
   b->Draw("L");
}

Thanks @couet. for your help.

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?