# How can I retrieve the background data from spectrum

**URL:** <https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437>\
**Category:** ROOT\
**Tags:** hist, root\
**Created:** [March 17, 2020, 4:00pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437 "2020-03-17T16:00:58Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 20, 2020, 3:02pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/22 "2020-03-20T15:02:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 20, 2020, 3:08pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/23 "2020-03-20T15:08:54Z")

</div>

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:

```auto
g->Draw("AL");

```

and the background by the command:

```auto
b->Draw("L");

```

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 20, 2020, 3:12pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/24 "2020-03-20T15:12:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 20, 2020, 3:20pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/25 "2020-03-20T15:20:07Z")

</div>

Yes with histograms your macro needs to be modified for sure. Indeed it will be very similar to [this example](https://root.cern/doc/master/Background__decr_8C.html). 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.

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 20, 2020, 3:50pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/26 "2020-03-20T15:50:11Z")

</div>

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](https://root-forum.cern.ch/uploads/short-url/bNGPYdgfikWoky5bTfWOD4Sc7Vl.C) (4.3 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 20, 2020, 4:12pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/28 "2020-03-20T16:12:08Z")

</div>

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
  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 ?

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 20, 2020, 4:38pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/29 "2020-03-20T16:38:39Z")

</div>

> [@couet](#):
>
> `back1` is the histogram used by this example… It has nothing to do with your data.

I thought it was used for defining the background histogram.

> [@couet](#):
>
> Can you clean up your macro a little bit and make it more clear ?

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 20, 2020, 4:39pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/30 "2020-03-20T16:39:57Z")

</div>

> [@robertfroast](#):
>
> Anyways, I will do it. Just give me some time.

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

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 20, 2020, 5:21pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/31 "2020-03-20T17:21:25Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 20, 2020, 9:31pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/32 "2020-03-20T21:31:40Z")

</div>

`h->GetArray() + 1`

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 21, 2020, 2:50am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/33 "2020-03-21T02:50:21Z")

</div>

Thanks @Wile_E_Coyote.

 ![backhisto](https://root-forum.cern.ch/uploads/default/original/3X/d/5/d5ad03dc1cda015912db1f05ac6d18de975f5676.png)  
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](https://root-forum.cern.ch/uploads/short-url/7OcGjaaN02DhVvWWkuXql5tX3Tj.C) (4.5 KB)

 ![backhisto2](https://root-forum.cern.ch/uploads/default/original/3X/5/5/55819c7d5598cb55093071edbac2d09b28575559.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 21, 2020, 7:59am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/34 "2020-03-21T07:59:18Z")

</div>

Compare the drawing options in the [TGraphPainter](https://root.cern/doc/master/classTGraphPainter.html) with the ones in the [THistPainter](https://root.cern/doc/master/classTHistPainter.html).

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 21, 2020, 8:27am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/35 "2020-03-21T08:27:10Z")

</div>

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”);

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 21, 2020, 8:39am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/36 "2020-03-21T08:39:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 21, 2020, 8:56am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/37 "2020-03-21T08:56:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 21, 2020, 9:00am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/38 "2020-03-21T09:00:37Z")

</div>

After drawing: `gPad->SetLogy(1);`

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 21, 2020, 3:35pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/39 "2020-03-21T15:35:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 22, 2020, 8:11am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/40 "2020-03-22T08:11:12Z")

</div>

Do I get it right that you `gPad->SetLogy(1);` and `h->SetMinimum(0.);`? Maybe you should [try some math handbook](https://en.wikipedia.org/wiki/Logarithm) (should teach you how much is “log(0)”).

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 22, 2020, 8:14am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/41 "2020-03-22T08:14:13Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 22, 2020, 8:17am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/42 "2020-03-22T08:17:37Z")

</div>

[ROOT HowTo -\> How to set ranges on axis?](https://root.cern.ch/how/how-set-ranges-axis)

[Previous page](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437.md?page=1)

[Next page](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437.md?page=3)
