Error Using TTree

I am trying to find the invariant mass of the Z boson from the dimuon channel. I generated some events in MadGraph and called it eventgenerate. I then wrote in C++ the following:

#ifdef __CLING__
R__LOAD_LIBRARY(libDelphes)
#include "TFile.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TH1F.h"
#include <iostream>
#include "classes/DelphesClasses.h"
#include "external/ExRootAnalysis/ExRootTreeReader.h"
#include "external/ExRootAnalysis/ExRootResult.h"
#else

class ExRootTreeReader;
class ExRootResult;
using namespace std;
#endif

void readEvents(const char *inputFile)
{

 gSystem->Load("libDelphes");
 
 TChain chain("Delphes");
 chain.Add(inputFile);
 
 
 ExRootTreeReader *treeReader = new ExRootTreeReader(&chain);
 Long64_t numberOfEntries = treeReader->GetEntries();
 
 TTree *tree = new TTree(&chain);
 
 TClonesArray *branchMuon = treeReader->UseBranch("Muon");
 //Long64_t numberOfMuons = branchMuon->GetEntries();

 TClonesArray *branchElectron = treeReader->UseBranch("Electron");
 Long64_t numberOfElectrons = branchElectron->GetEntries();
 

cout << "There are " << numberOfEntries << " entries in your ntuple" << endl;

UInt_t  Muons;
Float_t MuonEta1;
Float_t MuonPhi1;
Float_t MuonPt1;
Float_t MuonEta2;
Float_t MuonPhi2;
Float_t MuonPt2;

tree->SetBranchAddress("MuonEta1", &MuonEta1);
tree->SetBranchAddress("MuonPhi1", &MuonPhi1);
tree->SetBranchAddress("MuonPt1", &MuonPt1);
tree->SetBranchAddress("MuonEta2", &MuonEta2);
tree->SetBranchAddress("MuonPhi2", &MuonPhi2);
tree->SetBranchAddress("MuonPt2", &MuonPt2);

TH1F *histDiMuonMass = new TH1F("mass", "M_{inv}(mu+[1], mu-[2]); M_inv (GeV/c^2); Events", 50, 0.0, 1500);
for(Int_t entry = 0; entry < numberOfEntries; ++entry)
{
 treeReader->ReadEntry(entry);

 Long64_t numberOfMuons = branchMuon->GetEntries();
 
 //Muons += entry;



 //Muon *muon1, *muon2, *muon3, *muon4;
 TLorentzVector muon1;
 TLorentzVector muon2;
 
 muon1.SetPtEtaPhiM(MuonPt1, MuonEta1, MuonPhi1, 0.1);
 muon2.SetPtEtaPhi(MuonPt2, MuonEta2, MuonPhi2, 0.1);
 

if(branchMuon->GetEntries() == 4)
{
 

 TLorentzVector Z = muon1 + muon2;
 histDiMuonMass->Fill(Z.M());
}
}
cout << "There are " << Muons << " entries in your ntuple" << endl;
histDiMuonMass->Draw();
}

In the root terminal, I then wrote: 
.x examples/readEvents.C("../eventgeneration/Events/run_01/tag_1_delphes_events.root")

However, I am getting the following error: 

In file included from input_line_1563:1:
/mnt/c/1/MG5_aMC_v2_6_6/Delphes/examples/readEvents.C:30:20: error: no matching constructor for initialization of 'TTree'
 TTree *tree = new TTree(&chain);
                   ^     ~~~~~~
/home/cucip/builddir/include/TTree.h:295:4: note: candidate constructor not viable: no known conversion from 'TChain *' to 'const TTree' for 1st argument; remove &
   TTree(const TTree& tt) = delete;
   ^
/home/cucip/builddir/include/TTree.h:291:4: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
   TTree();
   ^
/home/cucip/builddir/include/TTree.h:292:4: note: candidate constructor not viable: requires at least 2 arguments, but 1 was provided
   TTree(const char* name, const char* title, Int_t splitlevel = 99, TDirectory* dir = gDirectory);
   ^
In file included from input_line_1563:1:
/mnt/c/1/MG5_aMC_v2_6_6/Delphes/examples/readEvents.C:72:8: error: no member named 'SetPtEtaPhi' in 'TLorentzVector'
 muon2.SetPtEtaPhi(MuonPt2, MuonEta2, MuonPhi2, 0.1);

Does anybody know what it means and how I can fix it?

Many thanks!

Do not try to create a new “tree” (instead of “tree->” use simply “chain.”).

Thank you for the reply. It fixed the error. The only thing now is that I am getting an empty histogram. Entries is non zero but mean and standard deviation is zero.
Do you see what could be the problem?

The error that I am getting is: Error in TChain::SetBranchAddress: unknown branch -> MuonEta1
Error in TChain::SetBranchAddress: unknown branch -> MuonPhi1
Error in TChain::SetBranchAddress: unknown branch -> MuonPt1
Error in TChain::SetBranchAddress: unknown branch -> MuonEta2
Error in TChain::SetBranchAddress: unknown branch -> MuonPhi2
Error in TChain::SetBranchAddress: unknown branch -> MuonPt2

Could it simply be a typo in the branch name? The branch name of the file on disk can be different from the variable name in which you load the data. If you inspect the tree in a TBrowser, does it show MuonEta1 etc. as the branch names?

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