使用Root/C++时出错:“TTree”的初始化没有匹配的构造函数
我试图绘制两个衰变μ子的Z玻色子的不变质量。我正在使用MadGraph和Root。MadGraph模拟事件p>Z>mu+和mu-并创建包含事件的.root文件。我称之为模拟事件生成 <>使用根分析数据并绘制直方图,我必须用C++编写代码。代码如下:使用Root/C++时出错:“TTree”的初始化没有匹配的构造函数,c++,histogram,root-framework,C++,Histogram,Root Framework,我试图绘制两个衰变μ子的Z玻色子的不变质量。我正在使用MadGraph和Root。MadGraph模拟事件p>Z>mu+和mu-并创建包含事件的.root文件。我称之为模拟事件生成 使用根分析数据并绘制直方图,我必须用C++编写代码。代码如下: #ifdef __CLING__ R__LOAD_LIBRARY(libDelphes) #include "TFile.h" #include "TTree.h" #include "TCanvas.h" #include "TH1F.h" #incl
#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 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);
有人知道是什么导致了这个错误吗?我如何修复它
非常感谢 通过调用新的TTree&chain,您试图调用TTree的构造函数,并为其提供一个TChain*链的类型是TChain,因此&chain是指向TChain的指针
TTree的构造函数要么没有参数,要么有两个常量字符*通常是字符串文字,要么是TString的.Data,要么是std::string的.c_str,要么是ROOT的表单输出
您似乎试图调用已删除的构造函数TTreeconst TTree&tt=delete,但由于以下几个原因,该构造函数不起作用:
你提供了一个指向TTree的指针,而不是一个TTree的引用,甚至从我的头顶上都不确定如果没有&的话,从TChain到TTree的转换是否有效。
构造函数被删除,因此它实际上不存在,无法调用。
但是,从外观上看,您希望将TChain用作TTree*,这不需要您这边的任何代码,因为TChain通过继承已经是TTree了。所以,你可以用链子。而不是tree->,或者如果由于我在示例中没有看到的原因,更需要使用指针变量,则创建一个TTree*,即只创建指向TTree的指针,而不是在堆上创建实际的TTree,并通过执行以下操作从新操作符获取指向它的指针
TTree*tree=&chain;
然而,我得到的新错误是TChain::SetBranchAddress:未知分支->TChain::SetBranchAddress:未知分支->TChain::SetBranchAddress:未知分支->MuonPhi1错误TChain::SetBranchAddress:未知分支->TChain::SetBranchAddress:未知分支->MuonEta2错误TChain::SetBranchAddress:未知分支->TChain::SetBranchAddress:unknown branch->MuonPt2中的MuonPhi2错误可能来自这里,这不是代码本身的问题,但意味着您的代码与您试图访问的输入文件不匹配,而这些数据并不存在。如果不访问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);