Search code examples
c++histogramroot-framework

Error in Using Root/C++: No matching constructor for initialization of 'TTree'


I am trying to plot the invariant mass of the Z boson from two decayed muons. I am using MadGraph and Root. MadGraph simulates the events (p p > Z > mu+ and mu-) and creates a .root file that contains the events. I called the simulation eventgeneration.

To use Root to analyze the data and draw the histogram, I have to write my code in C++. Here is the code:

#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();
}

Then in the root terminal I type: \

.x examples/readEvents.C("../eventgeneration/Events/run_01/tag_1_delphes_events.root")

However, I get 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 see what could cause this error and how I can fix it?

Many thanks!


Solution

  • By calling new TTree(&chain) you're trying to call a constructor for a TTree and you provide it with a TChain * (the type of chain is TChain, so &chain is a pointer to a TChain).

    The constructors for TTree https://root.cern/doc/v618/classTTree.html take either no argument or two const char* (usually string literals or .Data() of a TString or .c_str() of a std::string or the output of ROOT's Form …).

    It looks like you try to call the deleted constructor TTree(const TTree&tt)=delete, which doesn't work for several reasons:

    • you're providing a pointer to a TTree instead of a TTree by reference (not even sure from the top of my head if casting from TChain to TTree would work if the & wasn't there.
    • the constructor is deleted, so it doesn't actually exist and cannot be called.

    However, from the looks of it, you want to use your TChain as a TTree*, which doesn't require any code from your side to begin with because a TChain already is a TTree by inheritance. So, you can use chain. instead of tree->, or (if having a pointer variable is really more desirable for reasons that I don't see in the example) create a TTree* (i.e. create only the pointer to a TTree, instead of creating the actual TTree on the heap and get a pointer to it from the new operator) by doing something like

    TTree* tree = &chain;