# Adding and subtracting two or more histograms

**URL:** <https://root-forum.cern.ch/t/adding-and-subtracting-two-or-more-histograms/31434>\
**Category:** ROOT\
**Tags:** tree, roofit, root, hist\
**Created:** [November 3, 2018, 10:12am UTC](https://root-forum.cern.ch/t/adding-and-subtracting-two-or-more-histograms/31434 "2018-11-03T10:12:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![souvik](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@souvik](https://root-forum.cern.ch/u/souvik)\
**Post date:** [November 3, 2018, 10:12am UTC](https://root-forum.cern.ch/t/adding-and-subtracting-two-or-more-histograms/31434/1 "2018-11-03T10:12:00Z")

</div>

* * *

_ROOT Version:_ Not Provided  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

```auto
void lfv()
{

  TChain *t1 = new TChain();
  t1->Add("/home/souvik/root_work/lfv1.root/vpho");

  Float_t pd1;
  t1->SetBranchAddress("vpho_e0_P4", &pd1);

  Float_t pd2;
  t1->SetBranchAddress("vpho_e1_P4", &pd2);

  TH1F *hist1 = new TH1F("", " ", 50, 2.0, 2.5);
  TCanvas* c3 = new TCanvas();
  c3->cd();

  for (Int_t i = 0; i < t1->GetEntries(); ++i)

    {
      t1->GetEntry(i);
      Float_t pd = pd1+pd2;

        {
          hist1->Fill(pd);

        }
    }
TChain *t2 = new TChain();
  t2->Add("/home/souvik/root_work/lfv1.root/dklfv");

  Float_t pd3;
  t2->SetBranchAddress("D0_P4", &pd3);

  TH1F *hist2 = new TH1F("", " ", 50, -10,10);
  TCanvas* c2 = new TCanvas();
  c2->cd();

  for (Int_t i = 0; i < t2->GetEntries(); ++i)

    {
      t2->GetEntry(i);

        {
          hist2->Fill(pd3);

        }
    }
TChain *t3 = new TChain();
  t3->Add("/home/souvik/root_work/lfv1.root/pion");

  Float_t pd4;
  t3->SetBranchAddress("pi_P4", &pd4);

  TH1F *hist3 = new TH1F("", " ", 50, 0, 10);
  //hist3 = (hist1 + hist2);                                                                                                                                                                                       

  for (Int_t i = 0; i < t3->GetEntries(); ++i)

    {
      t3->GetEntry(i);

      {
        hist3->Fill(pd4);

      }
    }
TChain *t4 = new TChain();
  t4->Add("/home/souvik/root_work/lfv1.root/kshort");

  Float_t pd5;
  t4->SetBranchAddress("K_S0_P4", &pd5);

  TH1F *hist4 = new TH1F("", " ", 50, -3.5, 4.5);
  //hist3 = (hist1 + hist2);                                                                                                                                                                                       

  for (Int_t i = 0; i < t4->GetEntries(); ++i)

    {
      t4->GetEntry(i);

      {
        hist4->Fill(pd5);

      }
    }
TChain *t5 = new TChain();
  t5->Add("/home/souvik/root_work/lfv1.root/phot");

  Float_t pd6;
  t5->SetBranchAddress("gamma_P4", &pd5);

  TH1F *hist5 = new TH1F("", " ", 50, -0.2, 0.2);
  //hist3 = (hist1 + hist2);                                                                                                                                                                                       

  for (Int_t i = 0; i < t5->GetEntries(); ++i)

    {
      t5->GetEntry(i);

      {
        hist5->Fill(pd6);

      }
    }

  TH1F *hist6 = new TH1F("", " ", 50, 2.0, 2.5);
                                                                                                                                                                                                                                                                                                                                                                      
  hist6 = (TH1F*) hist1->Clone("hist6");
  hist6->Add(hist2, -1);
  hist6->Add(hist3, -1);
  hist6->Add(hist4, -1);
  hist6->Add(hist5, -1);

  hist6->Draw();

}

```

Hi !!  
I have written a code where in the fast chain(t1) I have added two different variables from the same tree. Now in the last four trees t2 t3 t4 t5 I have defined four different trees. In this problem I want to subtract last four trees from the first tree(where I have added two variables from the same tree). Each and every variable has different range. If I execute this program I am getting this type of error.  
Error in : Trying to access a pointer that points to an invalid memory address…  
Execution of your code was aborted.invalid memory pointer passed to a callee:  
for (Int\_t i = 0; i \< t2-\>GetEntries(); ++i)

Can anyone please modify the code or instruct me a good code for it.  
Is the last part of the code okay for subtracting histograms ?

Thanks in Advance

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 5, 2018, 8:57am UTC](https://root-forum.cern.ch/t/adding-and-subtracting-two-or-more-histograms/31434/2 "2018-11-05T08:57:08Z")

</div>

The error message means that `t2` is invalid. Given your code I don’t understand why: nothing that should influence `t2` happens between `t2->SetBranchAddress("D0_P4", &pd3);` and `for (Int_t i = 0; i < t2->GetEntries(); ++i)`!

How closely is the code you quote related to the actual code? Can you print the value of `t2` before the loop, and within the first iteration of the loop?

---

<div class="post-metadata">

**Author:** ![souvik](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@souvik](https://root-forum.cern.ch/u/souvik)\
**Post date:** [November 13, 2018, 7:31am UTC](https://root-forum.cern.ch/t/adding-and-subtracting-two-or-more-histograms/31434/3 "2018-11-13T07:31:38Z")

</div>

I printed t2 and it is showing me the number of entries. But as usual I am getting the same error.

Error in : Trying to access a pointer that points to an invalid memory address…  
Execution of your code was aborted.  
warning: invalid memory pointer passed to a callee:  
t2-\>GetEntry(i);  
I tried with each and every loop(commenting out the other loops). I am getting the same error for every loop.

Need help.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [November 27, 2018, 7:31am UTC](https://root-forum.cern.ch/t/adding-and-subtracting-two-or-more-histograms/31434/4 "2018-11-27T07:31:41Z")

</div>

Closed on November 27, 2018, 7:31am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
