Adding and subtracting two or more histograms


ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


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

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?

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.

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