Hello,
I’m new to ROOT, my goal is to read a histogram data from my root file. This is my code:
#include <TFile.h>
#include <TTree.h>
#include <TH1F.h>
void histogram() {
TFile *file = TFile::Open("events.root");
TTree *tree = (TTree*)file->Get("Delphes");
TH1F *hist = new TH1F();
tree->SetBranchAddress("GenJet.PT", &hist);
for (Long64_t i = 0; i < tree->GetEntries(); i++) {
tree->GetEntry(i);
hist->Draw();
}
file->Close();
}
When I run my code, an empty histogram is plotted and I get the error:
Error in < TTree::SetBranchAddress>: The pointer type given “TH1F” does not correspond to the type needed “Float_t” (5) by the branch: GenJet.PT
And If I try to adjust the part below as such, I don’t get any error messages but the plotted canvas is empty:
TH1F *hist = new TH1F("GenJet_PT", "GenJet PT", 100, 0, 1000);
Float_t pt;
tree->SetBranchAddress("GenJet.PT", &pt);
for (Long64_t i = 0; i < tree->GetEntries(); i++) {
tree->GetEntry(i);
hist->Fill(pt);
}
hist->Draw();
I’d be happy if someone could help.
Thank you.
ROOT Version: 6.26.10
Platform: Ubuntu 20.04.5