7.8.1 PAT Examples: Muon Example



The muon object is rather sophisticate, as information from all CMS detectors are included. The data format was designed to be complete and user-friendly. Reason for that the reco::Muon does not differ to much to the pat::Muon.


It is worthwhile to look at the two data formats themselves:

Note that in 22x the MuonSelectors are directly in the reco::Muon object and hence in the pat::Muon as well. Starting from 31X they are part of the pat::Muon only.

In this tutorial a couple of simple analyses, which show how to access the muon properties, are discussed. One analysis is based on pat::Muon and shows how to handle simulated proton-proton sample, the other illustrates how to access the reco::Muon properties of real cosmic muon data.

How to get the code

this tag works for 2_2_X cycle.

Prepare your release area, then go therein:

cd $CMSSW_BASE/src
cvs co -r V00-00-03 MuonAnalysis/Examples
scramv1 b

The package contains two analyses (which sit in the plugins/ directory):

Here you will not find the detailed description of the code. This to make you usual with C++, and make you able to understand what the code does directly looking at it. The code is well commented (unfortunately, in your life, you will not find so many other examples of such a commented code).


First example: PAT access

Get a PAT sample or produce it

For what concern the first example (MuonAnalysis module), before run the code you have to search for a sample which contains pat::Muons. Please be sure that one of the following conditions have been satisfied when the sample was produced:

  • The pat::Muons have Generated info and Reco Tracks embedded in (i.e., that information has been copied inside the PAT object). This means that the configuration used to produce the sample had:
       process.allLayer1Muons.embedTrack = cms.bool(True)
       process.allLayer1Muons.embedGenMatch = cms.bool(True)

  • The Event content has the explicit collections in (i.e., the information are not copied in the PAT object and the access to it occurs through links). This means that the sample has been produced with the following request:
   RecoMuonPAT = cms.PSet(
         outputCommands = cms.untracked.vstring(
                   'keep recoTracks_standAloneMuons_*_*',   # optional   
                   'keep recoTracks_globalMuons_*_*',           # optional  
                   'keep recoTracks_tevMuons_*_*',                # optional 
                   'keep recoTracks_generalTracks_*_*',   
                   'keep recoMuons_muons_*_*',                     # optional  
                   'keep recoCaloMuons_calomuons_*_*',       # optional   
                   # GEN   
                   'keep *_genParticles_*_*'   

So, if you would use CMS.PhysicsTools/PatAlgos/test/patLayer1_fromAOD_full.cfg.py to produce your home-made sample, it is fine, but you should add to the configuration one of the two blocks above. In MuonAnalysis/Example/test/ there is a copy of patLayer1_fromAOD_full.cfg.py but with the instruction to embed Gen&Track info in. The name of this file is producePATMuons_cfg.py. You can run it to produce a sample.

It runs (on purpose, see exercise below) on sample produced with 22X but not with 21X.

The result of the corresponding job can be find here:

and it is accessible from NAF, of course.

Run the code

Now that we have a sample, run the code is very simple:

cd $CMSSW_BASE/src/MuonAnalysis/Examples/test
cmsRun MuonAnalyzer_cfg.py

The result is a root file, whit the following content:

  • NMuons: Number of muons per event
  • pT: transverse momentum of muons
  • pT_Reso: pT(rec)-pT(sim)/pT(sim)
  • EHCal: Energy deposit in HCAL
  • MuonType: Type of Muons (STA/GLB/TM)
  • DiffPt_STA_TK: pT(TK)-pT(STA)
  • CaloCompatibility: Muon hypothesis probability using calorimeters only
  • SegmentCompatibility: Muon hypothesis using segments only
  • NumMatchedChamber: Number of matched chambers in the muon system
  • MuonIDSelectors: muon id selectors incidence
  • pT_TPFMS: pT(rec) of Tracker Plus First Muon Station Track
  • MuIso03SumPt: isolation using SumPt of the tracker tracks within a cone of Delta(R)=0.3
  • MuIso03CaloComb: isolation using 1.2*ECAL+0.8HCAL within a cone of Delta(R)=0.3
  • InvMass4MuSystem: Invariant mass of the 4 muons system

The (very same) output of the job can be found here:


We can have a look at the file using FWLite. You can set up a .C file with the following lines in:

Then open a .root file:
TFile fMuons("/afs/cern.ch/cms/Physics/muon/MPOG/Tutorial/
TTree *events = (TTree*)f.Get("Events")
Then browse some of the muon quantities:

Second example: cosmic rays analysis

To run the Muon Analysis on cosmic data we do no use the same input, but a sample from the cosmic data taking is used. Please, have a look at the code and the comments therein (especially if you are willing to upgrade your code to 31X).

Here the simple command to run the job:

cmsRun CosmicMuonAnalyzer_cfg.py

The result is a root file with the same content as the first example. You can find an example of it here:

(this file has been produced with a slightly different data set with respect to what chosen for tutorial at NAF. The sample used is anyway commented in the cfg file and it is accessible in DBS). Okay, okay ... here you can find also the exact output of the NAF -job:

Third example: run the visualization

CMS has many type of visualization program. Here an example about how run iguana is given (be sure you logged in with -Y option).

We run a visualization job simply typing iguana file.root or iguana file_cfg.py. In the former approach a default cfg_py is also create and you can use it later as input for iguana itself. Let's see an example and have a direct look at the cosmic rays we analysed earlier:

iguana /store/data/Commissioning08/Cosmics/RECO/CRAFT_V3P_TrackerPointing_v1/
the cfg file created by iguana can be found also in the package you downloaded: MuonAnalysis/Examples/test/visMuon_cfg.py and you can use it instead the command above, if you wish.

Once iguana is ready, click on Event->Next Event ...wait... then click on the eye. Use ctrl+n to move to another event and... enjoy yourself!

How to get more information

A good description of muon software in CMS is available here.


To practice the different steps described in this tutorial, an exercise (with solution) is proposed.
caveat: this is based on sample stored at NAF and the solution may not work out-of-the-box in other sites.

Here what is proposed:

  • produce a PAT sample, out of a Z into mumu sample and using embedded gen info and Tracker track in the pat::Muons
    • Tips:
      • search for it in DBS or using dcls if your site used dCache (e.g. at NAF)
      • modify Muon/Analyser/tes/producePATMuons_cfg.py accordingly

There are known issue to run PAT in 2_2_X on sample produced with 2_1_X. So, if you want to run smoothly on a 2_1_X sample (most likely you would) you need to add a couple of line to the cfg:

         from CMS.PhysicsTools.PatAlgos.tools.cmsswVersionTools import run22XonSummer08AODSIM
         from CMS.PhysicsTools.PatAlgos.tools.trigTools import switchOffTriggerMatchingOld
         switchOffTriggerMatchingOld( process )
The proposed solution uses the above lines (as it runs over a 2_1_X sample... it is not because we are nasty, but in this way you have a broad set of examples). If you are in trouble you can look at the solution.
  • Modify the MuonAnalyser in order to handle 2 muons instead of 4
    • calculate the invariant mass of Z. Pay attention at the charge of the muons!
    • what is the percentage of events in which a good invariant mass of the Z (let say, when it is between 60 and 110 GeV) is given by two muons reconstructed with the same charge sign?
    • Do not forget to change the input of your cfg file, now you have your own produced pat-tuple to look at!
  • Use iguana to look at some Z events.

Searching for the solution?

cvs co -r SV00-00-06 MuonAnalysis/Examples

Muon efficiencies

An example of filling and retrieving "tag-&-probe" efficiencies with the PAT::muon object is available here.

Review status

Reviewer/Editor and Date (copy from screen) Comments
RogerWolf - 13 May 2009 Created the template page
RiccardoBellan - 23 Jun 2009 Modified for Tutorial at DESY

Responsible: RiccardoBellan
Last reviewed by: -- RiccardoBellan - 23 Jun 2009

Topic attachments
I Attachment History Action Size Date Who Comment
JPEGjpg datafmu.jpg r2 r1 manage 465.3 K 2009-06-22 - 18:37 RiccardoBellan  
Edit | Attach | Watch | Print version | History: r18 < r17 < r16 < r15 < r14 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r18 - 2010-08-06 - SudhirMalik
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    CMSPublic All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback