Fork me on GitHub

WorkBook/Tutorials/Student: example.py

File example.py, 2.1 KB (added by mselvaggi, 13 months ago)
Line 
1# example event loop macro
2#!/usr/bin/env python
3
4import sys
5import ROOT
6
7if len(sys.argv) < 2:
8  print " Usage: python macro.py data.root hist_data.root"
9  sys.exit(1)
10
11ROOT.gSystem.Load("libDelphes")
12
13try:
14  ROOT.gInterpreter.Declare('#include "classes/SortableObject.h"')
15  ROOT.gInterpreter.Declare('#include "classes/DelphesClasses.h"')
16  ROOT.gInterpreter.Declare('#include "external/ExRootAnalysis/ExRootTreeReader.h"')
17except:
18  pass
19
20inputFile = sys.argv[1]
21outputFile = sys.argv[2]
22
23# Create chain of root trees
24chain = ROOT.TChain("Delphes")
25chain.Add(inputFile)
26
27# Create object of class ExRootTreeReader
28treeReader = ROOT.ExRootTreeReader(chain)
29numberOfEntries = treeReader.GetEntries()
30
31# Get pointers to branches used in this analysis
32branchMuon = treeReader.UseBranch("Muon")
33branchMissingET = treeReader.UseBranch("MissingET")
34branchElectron = treeReader.UseBranch("Electron")
35
36# Book histograms
37histMuonMultiplicity = ROOT.TH1F("histMuonMultiplicity", "histMuonMultiplicity", 5, -0.5, 4.5)
38histLeadingMuonPt = ROOT.TH1F("histLeadingMuonPt", "histLeadingMuonPt", 50, 0., 100.)
39histMissingET = ROOT.TH1F("histMissingET", "histMissingET", 50, 0., 100.)
40
41# Loop over all events
42for entry in range(0, numberOfEntries):
43    # Load selected branches with data from specified event
44    treeReader.ReadEntry(entry)
45 
46    # loop over lepton collections
47    # store in an array only isolated muons above some pT
48    selected_muons = []
49    for muon in branchMuon:
50         if muon.PT < 15. or abs(muon.Eta)>4.0 or muon.IsolationVar > 0.25:
51             continue
52         selected_muons.append(muon)
53
54    selected_electrons = []
55    for elec in branchElectron:
56         if elec.PT < 15. or abs(elec.Eta)>4.0 or elec.IsolationVar > 0.12:
57             continue
58         selected_electrons.append(elec)
59
60    histMuonMultiplicity.Fill(len(selected_muons))
61   
62    if len(selected_muons) > 0:
63       histLeadingMuonPt.Fill(selected_muons[0].P4().Pt())   
64
65    # fill missing tranvserse energy
66
67    met = branchMissingET[0].MET
68    histMissingET.Fill(met)   
69
70
71out_root = ROOT.TFile(outputFile,"RECREATE")
72
73histMuonMultiplicity.Write()
74histLeadingMuonPt.Write()
75histMissingET.Write()