CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
patZpeak.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 from builtins import range
4 import ROOT
5 import sys
6 from DataFormats.FWLite import Events, Handle
7 
8 # Make VarParsing object
9 # https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideAboutPythonConfigFile#VarParsing_Example
10 from FWCore.ParameterSet.VarParsing import VarParsing
11 options = VarParsing ('python')
12 options.parseArguments()
13 
14 # Events takes either
15 # - single file name
16 # - list of file names
17 # - VarParsing options
18 
19 # use Varparsing object
20 events = Events (options)
21 
22 # create handle outside of loop
23 handle = Handle ("std::vector<pat::Muon>")
24 
25 # for now, label is just a tuple of strings that is initialized just
26 # like and edm::InputTag
27 label = ("selectedLayer1Muons")
28 
29 # Create histograms, etc.
30 ROOT.gROOT.SetBatch() # don't pop up canvases
31 ROOT.gROOT.SetStyle('Plain') # white background
32 zmassHist = ROOT.TH1F ("zmass", "Z Candidate Mass", 50, 20, 220)
33 
34 # loop over events
35 for event in events:
36  # use getByLabel, just like in cmsRun
37  event.getByLabel (label, handle)
38  # get the product
39  muons = handle.product()
40  # use muons to make Z peak
41  numMuons = len (muons)
42  if muons < 2: continue
43  for outer in range (numMuons - 1):
44  outerMuon = muons[outer]
45  for inner in range (outer + 1, numMuons):
46  innerMuon = muons[inner]
47  if outerMuon.charge() * innerMuon.charge() >= 0:
48  continue
49  inner4v = ROOT.TLorentzVector (innerMuon.px(), innerMuon.py(),
50  innerMuon.pz(), innerMuon.energy())
51  outer4v = ROOT.TLorentzVector (outerMuon.px(), outerMuon.py(),
52  outerMuon.pz(), outerMuon.energy())
53  zmassHist.Fill( (inner4v + outer4v).M() )
54 
55 # make a canvas, draw, and save it
56 c1 = ROOT.TCanvas()
57 zmassHist.Draw()
58 c1.Print ("zmass_py.png")