# Selecting Particles and Filling the Histogram

**URL:** <https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078>\
**Category:** Newbie\
**Created:** [July 29, 2021, 6:21pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078 "2021-07-29T18:21:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [July 29, 2021, 6:21pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078/1 "2021-07-29T18:21:49Z")

</div>

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.

```auto
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 }

```

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [July 29, 2021, 6:23pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078/2 "2021-07-29T18:23:15Z")

</div>

![Capture](https://root-forum.cern.ch/uploads/default/original/3X/1/9/1957771956f79f96a5c8a680c8843810f1383d39.png)

I am trying to make a kinetic energy histogram for gamma particles only. Particle identifications are in the leaf “pid”.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 29, 2021, 6:41pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078/3 "2021-07-29T18:41:46Z")

</div>

Attach the output from: `DUNETargetSim->Print();`

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [July 29, 2021, 6:48pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078/4 "2021-07-29T18:48:09Z")

</div>

Here you go. Thank you.

 ![2](https://root-forum.cern.ch/uploads/default/original/3X/f/d/fd6db9d0aef741cb9c8fb189665266340fb92563.png)  
 ![3](https://root-forum.cern.ch/uploads/default/original/3X/1/f/1fbcf1bc5678b3c430beffebed7f12e84f098e0b.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 29, 2021, 7:01pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078/5 "2021-07-29T19:01:38Z")

</div>

```cpp
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
}

```

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [July 29, 2021, 7:29pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078/6 "2021-07-29T19:29:00Z")

</div>

Thank you very much for improving the code.

First, I figured out the error in my code.

```auto
        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.

 ![4](https://root-forum.cern.ch/uploads/default/original/3X/6/8/6839c0032ad54d5f5635d233129ffe9c1340d3f2.png)  
I think it could be because there is no particle “g” when I scanned the “pid”.

```auto
if (pid != "g") continue; // check ... DUNETargetSim->Scan("pid")
    KEHist->Fill(KEnergy);

```

When I changed this to “gamma”, it asked me to use strncmp function.

 ![5](https://root-forum.cern.ch/uploads/default/original/3X/a/f/af72ab2e9a09cc17a3508a7ce3ff583e686d8546.png)

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!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 29, 2021, 7:33pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078/7 "2021-07-29T19:33:46Z")

</div>

Check what values the “`pid`” contains: `DUNETargetSim->Scan("pid");`

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [July 29, 2021, 7:39pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078/8 "2021-07-29T19:39:21Z")

</div>

This is what I got.

 ![6](https://root-forum.cern.ch/uploads/default/original/3X/7/7/7718e7e99a2a1d82740fe508e386de9050c5c13c.png)  
 ![7](https://root-forum.cern.ch/uploads/default/original/3X/d/7/d7eca3feca99d49aff4b0e57446cf84c82d4675c.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 29, 2021, 7:51pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078/9 "2021-07-29T19:51:25Z")

</div>

@pcanal That’s a bit strange because the output of “`DUNETargetSim->Print();`” suggested “`pid`” was a single “`Char_t`” value.

@TheScientist Try with:

```cpp
  // ...
  Char_t pid[32];
  // ...
    if (strcmp(pid, "gamma")) continue;
  // ...
```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [July 29, 2021, 9:31pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078/10 "2021-07-29T21:31:34Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> @pcanal That’s a bit strange because the output of “`DUNETargetSim->Print();`” suggested “`pid`” was a single “`Char_t`” value.

Indeed, the output of `Print` seems incomplete/incorrect for this case.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 29, 2021, 9:39pm UTC](https://root-forum.cern.ch/t/selecting-particles-and-filling-the-histogram/46078/11 "2021-07-29T21:39:29Z")

</div>

Maybe that’s [another Geant4 bug related to ROOT](https://root-forum.cern.ch/t/opening-a-file-in-update-mode-causes-error-in-tbasket-streamer-the-value-of-fnbytes-is-incorrect/43340/15)?
