Problem in filling a tree branches

Dear developers,

I am running a macro but it is not getting entries from the tree. I did the same work in another macro twodim.cc but it works. Can you please help me to resolve this problem.

#include <TFile.h>
#include <TTree.h>
#include <TROOT.h>
#include <TH1.h>
#include <TH2.h>
#include <TSystem.h>
#include <vector>
#include <TLorentzVector.h>
#include <math.h>
#include "TMath.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <map>
#include <TFileMerger.h>

void twodim_new()
{
   TFile *f = new TFile("output1_ajeeb_Veto.root");
   TTree *t = (TTree*)f->Get("AnaTree");
   TH1F *h1 = new TH1F("h1","Z1_mass",100,0,200);
   TH1F *h2 = new TH1F("h2","Z2_mass",100,0,200);
   TH1F *h3 = new TH1F("h3","Z3_mass",100,0,200);
using namespace std;

Float_t VetoMu_px[50], VetoMu_py[50], VetoMu_pz[50], VetoMu_en[50], VetoMu_pt[50], VetoMu_eta[50], VetoMu_phi[50], VetoMu_iso[50];
Float_t VetoMu_ismuon[50], VetoMu_idTight[50];
Int_t VetoMu, VetoMu_charge[50];
          t->SetBranchAddress("VetoMu_px",VetoMu_px);
          t->SetBranchAddress("VetoMu_py",VetoMu_py);
          t->SetBranchAddress("VetoMu_pz",VetoMu_pz);
          t->SetBranchAddress("VetoMu_en",VetoMu_en);
          t->SetBranchAddress("VetoMu",&VetoMu);
          t->SetBranchAddress("VetoMu_charge",&VetoMu_charge);
          t->SetBranchAddress("VetoMu_pt",&VetoMu_pt);
          t->SetBranchAddress("VetoMu_eta",&VetoMu_eta);
          t->SetBranchAddress("VetoMu_phi",&VetoMu_phi);
          t->SetBranchAddress("VetoMu_idTight",&VetoMu_idTight);
          t->SetBranchAddress("VetoMu_ismuon",&VetoMu_ismuon);
          t->SetBranchAddress("VetoMu_iso",&VetoMu_iso);

TLorentzVector tmp(0,0,0,0), tmp1(0,0,0,0), tmp2(0,0,0,0), tmp3(0,0,0,0);

//int n1 = t->GetEntries();
 for (int i=0;i<10000;i++)   {

 TLorentzVector tmp1, tmp2, tmp3;

         t->GetEntry(i);

 for(int m=0; m<VetoMu;m++)   {

          if(VetoMu<2)continue;

int m1=0, m2=1, m3=2;

if(m1==0) tmp1.SetPxPyPzE(VetoMu_px[m1], VetoMu_py[m1], VetoMu_pz[m1], VetoMu_en[m1]);
if(m2==1) tmp2.SetPxPyPzE(VetoMu_px[m2], VetoMu_py[m2], VetoMu_pz[m2], VetoMu_en[m2]);
if(m3==2) tmp3.SetPxPyPzE(VetoMu_px[m3], VetoMu_py[m3], VetoMu_pz[m3], VetoMu_en[m3]);

if (VetoMu_charge[m1]*VetoMu_charge[m2] < 0.) {

        double z1_mass = (tmp1 + tmp2).M();
}
if (VetoMu_charge[m2]*VetoMu_charge[m3] < 0.) {

        double z2_mass = (tmp2 + tmp3).M();
}
if (VetoMu_charge[m3]*VetoMu_charge[m1] < 0.) {

        double z3_mass = (tmp1 + tmp3).M();
}
//         t->Fill();
         t->GetEntry(i);
         h1->Fill(z1_mass);
         h2->Fill(z2_mass);
         h3->Fill(z3_mass);
}
}

        TCanvas *c1 = new TCanvas("c1","c1",800,600) ;
        c1->Divide(2,2);
        c1->cd(1);
        h1->Draw();
        h1->GetXaxis()->SetTitle("Z_12_mass");

        c1->cd(2);
        h2->Draw();
        h2->GetXaxis()->SetTitle("Z_32_mass");
        c1->cd(3);
        h3->Draw();
        h3->GetXaxis()->SetTitle("Z_31_mass");
}

Cheers,
Nab
output1_ajeeb_Veto.root (28.2 KB)
twodim.cc (6.2 KB)

Try to run it through ACLiC (and fix reported errors):
root [0] .x twodim_new.cc++

For example, I can see that you need to move some definitions to outside of “if statements”:
double z1_mass, z2_mass, z3_mass;

BTW. You should really start with a ROOT generated “analysis skeleton”. The simplest thing to try could be (and then see how your tree branches should be defined):
AnaTree->MakeClass();

Dear Wile_E_Coyote,

I removed all errors but still i am not getting a distribution.

If you execute the line below, your will see that no branch in your tree is an array (and your current code assumes that they are for almost all of them):
AnaTree->Print();

You should really start with a ROOT generated “analysis skeleton”.

How can I make may tree branches as an array. I will be more easier for me as I have a code in which I am creating the output1_ajeeb_Vet0.root file

I am creating AnaTree using this macro
new_macro.cc (10.5 KB)

See, for example:

  1. Adding a Branch with a Fixed Length Array
  2. Adding a Branch with a Variable Length Array
  3. Search for “array” in: TBranch::TBranch
  4. See also the description of Branches and Streamers
  5. ${ROOTSYS}/tutorials/tree/tree2.C
  6. ${ROOTSYS}/tutorials/tree/tree2a.C
  7. ${ROOTSYS}/tutorials/tree/tree3.C

Thank you Wile_E_Coyote for your guidance.

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