使用根/C++时出错:没有匹配的构造函数来初始化'TTree'

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

本文关键字:构造函数 初始化 TTree C++ 出错      更新时间:2023-10-16

我试图从两个衰变的μ介子绘制Z玻色子的不变质量。我正在使用MadGraph和Root。MadGraph 模拟事件(p p> Z> mu+ 和 mu-(,并创建一个包含事件的 .root 文件。我调用了模拟事件生成。

要使用 Root 分析数据并绘制直方图,我必须用 C++ 编写代码。这是代码:

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

然后在根终端中键入:\

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

但是,我收到以下错误:

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);

有没有人看到可能导致此错误的原因以及我如何解决它?

非常感谢!

通过调用new TTree(&chain),您尝试调用TTree的构造函数,并为其提供TChain *(chain的类型是TChain,因此&chain是指向TChain的指针(。

TTreehttps://root.cern/doc/v618/classTTree.html 的构造函数要么不带参数,要么不带两个参数const char*(通常是字符串文字或TStringstd::string.c_str().Data()或 ROOTForm的输出......

看起来您尝试调用已删除的构造函数TTree(const TTree&tt)=delete,由于以下几个原因,它不起作用:

  • 您通过引用提供指向TTree而不是TTree的指针(甚至不确定如果&不存在,从我的头顶上从TChain转换为TTree是否有效。
  • 构造函数被删除,因此它实际上不存在,也无法调用。

但是,从外观上看,您希望将TChain用作TTree*,这不需要您身边的任何代码,因为TChain已经是继承TTree。因此,您可以使用chain.而不是tree->,或者(如果由于我在示例中没有看到的原因而真正更需要指针变量(创建一个TTree*(即仅创建指向TTree的指针,而不是在堆上创建实际的TTree并从new运算符获取指向它的指针(通过执行以下操作

TTree* tree = &chain;