CMS 3D CMS Logo

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