# Filling a histogram with ntuple entries with cuts

**URL:** <https://root-forum.cern.ch/t/filling-a-histogram-with-ntuple-entries-with-cuts/42459>\
**Category:** Newbie\
**Created:** [November 25, 2020, 11:45pm UTC](https://root-forum.cern.ch/t/filling-a-histogram-with-ntuple-entries-with-cuts/42459 "2020-11-25T23:45:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ada](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@Ada](https://root-forum.cern.ch/u/Ada)\
**Post date:** [November 25, 2020, 11:45pm UTC](https://root-forum.cern.ch/t/filling-a-histogram-with-ntuple-entries-with-cuts/42459/1 "2020-11-25T23:45:48Z")

</div>

Hi!  
I have basic problems with operating on ntuple. All I can do is to read ntuple from loop made by MakeClass function. Now I’m suppposed to draw a histogram with bins whrere in the first one I have number of all events before preselction cuts, and next ones show number of events after each cut.  
I wrote definotions for all of my cuts and then I have the part in my code form MakeClass:

```auto
   if (fChain == 0) { cout << "fChain is null" << endl; return; }

   Long64_t nentries = fChain->GetEntriesFast();

   Long64_t nbytes = 0, nb = 0;
   for (Long64_t jentry=0; jentry<nentries;jentry++) {
      Long64_t ientry = LoadTree(jentry);
      if (ientry < 0) break;
      nb = fChain->GetEntry(jentry); nbytes += nb;
      // if (Cut(ientry) < 0) continue;

```

And then I have the part that draw me a histogram for the leaf with all events:

```auto
   h->Fill(NOMINAL_pileup_random_run_number);
   }
   int Events = h->GetEntries();
   cout << Events << endl;
//etc...

```

However, I want to put all these entres to just one bin of histogram and then add bins with preselected events. I tried to use something like this:

```auto
if( !m_SR_passTrigger ) return false;
h->Fill(number of bin i guess, weight);

```

But all I get is empty histogram and the Events gives 0. And I have no more ideas on how aproach this problem. Thanks in andvace for any help!

---

<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:** [November 26, 2020, 11:11am UTC](https://root-forum.cern.ch/t/filling-a-histogram-with-ntuple-entries-with-cuts/42459/2 "2020-11-26T11:11:31Z")

</div>

`if( m_SR_passTrigger ) h->Fill(number of bin i guess, weight);`

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [December 8, 2020, 12:08pm UTC](https://root-forum.cern.ch/t/filling-a-histogram-with-ntuple-entries-with-cuts/42459/3 "2020-12-08T12:08:16Z")

</div>

Hi Ada,  
note that there are simpler ways to read ROOT datasets than `MakeClass`.  
With [RDataFrame](https://root.cern.ch/doc/master/classROOT_1_1RDataFrame.html) this is all the code you need:

```c++
#include <ROOT/RDataFrame.hxx>

void macro() {
  ROOT::RDataFrame df("treename", "filename.root");
  auto histo1 = df.Histo1D("NOMINAL_pileup_random_run_number");
  auto histo2 = df.Filter("m_SR_passTrigger").Histo1D("valuecolumn", "weightcolumn");
  /* do what you want with the histograms */
}

```

Hope this helps!  
Cheers,  
Enrico
