Hello there. I have a root file with a list of particles and I am trying to select a specific particle and fill histogram out of it. I believe I made an error on the filling part as the histogram is showing empty. I have attached the ROOT file structure in the comments section.
void KEHist()
5 {
6 TFile* file = new TFile("TargetSim_POT100.root", "READ"); //READ
7 TTree* tree = (TTree*)file->Get("DUNETargetSim");
8
9 Double_t KEnergy;
10 Char_t pid[16]; //List of particles
11
12 tree->SetBranchAddress("kineticEnergy", &KEnergy);
13 tree->SetBranchAddress("pid", &pid);
14
15 Int_t entries = tree->GetEntries("kineticEnergy");
16
17 TH1D *KEHist = new TH1D("KEHist", "Entries/POT100 - Kinetic Energy; KE [GeV]; Entries/OT100", 1000, 0, 125);
18
19 for(Int_t i = 0; i < entries; i++)
20 {
21 if (strcmp (pid, "gamma") != 0) //Selecting specific particles
22 {
23 continue;
24 }
25 else {
26 tree->GetEntry(i);
27 KEHist->Fill(KEnergy);
28 }
29 }
30
31 KEHist->Draw();
32 KEHist->SetLineColor(kRed);
33 }
I am trying to make a kinetic energy histogram for gamma particles only. Particle identifications are in the leaf “pid”.
Attach the output from: DUNETargetSim->Print();
void KEHist() {
TFile *file = TFile::Open("TargetSim_POT100.root", "READ");
if ((!file) || file->IsZombie()) { delete file; return; } // just a precaution
TTree *tree; file->GetObject("DUNETargetSim", tree);
if (!tree) { delete file; return; } // just a precaution
Double_t KEnergy;
Char_t pid;
tree->SetBranchStatus("*", 0); // disable all branches
tree->SetBranchAddress("kineticEnergy", &KEnergy);
tree->SetBranchStatus("kineticEnergy", 1); // activate this branch
tree->SetBranchAddress("pid", &pid);
tree->SetBranchStatus("pid", 1); // activate this branch
Long64_t entries = tree->GetEntries();
gROOT->cd(); // newly created histograms should go here
TH1D *KEHist = new TH1D("KEHist", "Entries/POT100 - Kinetic Energy; KE [GeV]; Entries/OT100", 1000, 0., 125.);
for(Long64_t i = 0; i < entries; i++) {
tree->GetEntry(i); // read activated (enabled) branches
if (pid != 'g') continue; // check ... DUNETargetSim->Scan("pid")
KEHist->Fill(KEnergy);
}
KEHist->SetLineColor(kRed);
KEHist->Draw();
delete file; // automatically deletes "tree", too
}
1 Like
Thank you very much for improving the code.
First, I figured out the error in my code.
for(Int_t i = 0; i < entries; i++)
{ tree->GetEntry(i); //This one was below the if statement
if (strcmp (pid, "gamma") != 0) //Selecting specific particle
{
continue;
}
else {
KEHist->Fill(KEnergy);
}
}
Second, I tried to run your code which showed this error message.
I think it could be because there is no particle “g” when I scanned the “pid”.
if (pid != "g") continue; // check ... DUNETargetSim->Scan("pid")
KEHist->Fill(KEnergy);
When I changed this to “gamma”, it asked me to use strncmp function.
I got my solution. However, if you have time can you please explain to me the second error part and how to deal with it.
Thank you very much!
Check what values the “pid
” contains: DUNETargetSim->Scan("pid");
@pcanal That’s a bit strange because the output of “DUNETargetSim->Print();
” suggested “pid
” was a single “Char_t
” value.
@TheScientist Try with:
// ...
Char_t pid[32];
// ...
if (strcmp(pid, "gamma")) continue;
// ...
1 Like
pcanal
July 29, 2021, 9:31pm
10
Indeed, the output of Print
seems incomplete/incorrect for this case.