Access values of a variable in events that pass a cut

I want to access the values of a variable in events that pass a particular cut selection. That means that I want to access the actual numbers in a list.

Selection cuts can be applied using the Draw function of a TTree. For a histogram of the values of the variable el_pt in events that contain the flag variable e_jets (indicating that the event has passed some prior selection) the procedure could be something like the following:

from ROOT import *

ROOTFile1Name     = "output_13TeV.root"
variablesTreeName = "nominal"
variable          = "el_pt"
selection         = "e_jets"

ROOTFile1         = TFile(ROOTFile1Name)
variablesTree1    = ROOTFile1.Get(variablesTreeName)
histogram1        = TH1F(
    "histogram1",
    "histogram1",
    100,
    0,
    300000
)
variablesTree1.Project("histogram1", variable, selection)
histogram1.Draw()

Rather than drawing a histogram of the values of the variable, I want to access the values themselves, likely as a simple list of numbers. This could be used in a function that recommends a certain histogram binning, for example. What I have so far is the following:

from ROOT import *

ROOTFile1Name     = "output_13TeV.root"
variablesTreeName = "nominal"
variable          = "el_pt"
selection         = "e_jets"

ROOTFile1         = TFile(ROOTFile1Name)
variablesTree1    = ROOTFile1.Get(variablesTreeName)

# Make a list of all of the event numbers that pass the selection.
variablesTree1.Draw(">>eventNumberList1", variable, selection)
# Convert the list of event numbers to a list of events.
eventList1 = variablesTree1.SetEventList(eventNumberList1)

How could I proceed now with a view to creating a list that contains the values of the variable el_pt?

Hi,

Wim will reply next week. Conferences, conferences…

Axel.

Thanks, I’d appreciate any hints on this that you might have. :slight_smile:

Hi,

how about:l = [variablesTree1.GetEntry(eventNumberList1.GetEntry(ievent)) and variablesTree1.e_pt for ievent in xrange(eventNumberList1.GetN())]
Cheers,
Wim

Hi Wim

Thank you very much for your help on that. The use of the event number list with the tree is much clearer to me now. With a slight modification to your suggestion, I have the following solution that gives me a list of the actual values – I’m dealing with a variable whose values are stored in a vector:

l = [variablesTree1.GetEntry(eventNumberList1.GetEntry(ievent)) and getattr(variablesTree1, variable)[0] for ievent in xrange(eventNumberList1.GetN())]