CMS 3D CMS Logo

patZpeak.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
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 # use single file name
22 #events = Events ("root://cmsxrootd.fnal.gov///store/mc/RunIISummer20UL17MiniAODv2/DYJetsToMuMu_M-50_TuneCP5_13TeV-madgraphMLM-pythia8/MINIAODSIM/PUForMUOVal_106X_mc2017_realistic_v9-v2/100000/002FD620-0EF8-F044-9D21-7303C48FF2A0.root")
23 
24 # create handle outside of loop
25 handle = Handle ("std::vector<pat::Muon>")
26 
27 # for now, label is just a tuple of strings that is initialized just
28 # like and edm::InputTag
29 label = ("slimmedMuons")
30 
31 # Create histograms, etc.
32 ROOT.gROOT.SetBatch() # don't pop up canvases
33 ROOT.gROOT.SetStyle('Plain') # white background
34 zmassHist = ROOT.TH1F ("zmass", "Z Candidate Mass", 50, 20, 220)
35 
36 # loop over events
37 for event in events:
38  # use getByLabel, just like in cmsRun
39  event.getByLabel (label, handle)
40  # get the product
41  muons = handle.product()
42  # use muons to make Z peak
43  numMuons = len (muons)
44  if numMuons < 2: continue
45  for outer in range (numMuons - 1):
46  outerMuon = muons[outer]
47  for inner in range (outer + 1, numMuons):
48  innerMuon = muons[inner]
49  if outerMuon.charge() * innerMuon.charge() >= 0:
50  continue
51  inner4v = ROOT.TLorentzVector (innerMuon.px(), innerMuon.py(),
52  innerMuon.pz(), innerMuon.energy())
53  outer4v = ROOT.TLorentzVector (outerMuon.px(), outerMuon.py(),
54  outerMuon.pz(), outerMuon.energy())
55  zmassHist.Fill( (inner4v + outer4v).M() )
56 
57 # make a canvas, draw, and save it
58 c1 = ROOT.TCanvas()
59 zmassHist.Draw()
60 c1.Print ("zmass_py.png")
61