Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:31

0001 from __future__ import print_function
0002 import os
0003 import PhysicsTools.HeppyCore.framework.config as cfg
0004 from PhysicsTools.Heppy.utils.miniAodFiles import miniAodFiles
0005 
0006 # set to True if you want several parallel processes
0007 multi_thread = False
0008 
0009 # input component 
0010 # several input components can be declared,
0011 # and added to the list of selected components
0012 inputSample = cfg.MCComponent(
0013     'test_component',
0014     files = miniAodFiles(),  # returns a list of file for this release
0015     # a list of local or xrootd files can be specified by hand.
0016     )
0017 
0018 if 'RelValZMM' not in inputSample.files[0]:
0019     print('''WARNING: this tutorial is supposed to run on Z->mumu events.
0020 Do not expect meaningful results for this sample:''')
0021     print(inputSample)
0022 
0023 if multi_thread: 
0024     inputSample.splitFactor = len(inputSample.files)
0025 
0026 selectedComponents  = [inputSample]
0027 
0028 # a very simple muon analyzer
0029 # read miniaod muons and wrap them in python muons
0030 from PhysicsTools.Heppy.analyzers.examples.SimpleMuonAnalyzer import SimpleMuonAnalyzer
0031 muons = cfg.Analyzer(
0032     SimpleMuonAnalyzer,
0033     'muons',
0034     )
0035 
0036 from PhysicsTools.Heppy.analyzers.examples.ResonanceBuilder import ResonanceBuilder
0037 dimuons = cfg.Analyzer(
0038     ResonanceBuilder,
0039     'dimuons',
0040     leg_collection = 'muons',
0041     filter_func = lambda x : True, 
0042     pdgid = 23
0043     )
0044 
0045 
0046 # a very simple jet analyzer
0047 # read miniaod jets and wrap them in python jets
0048 from PhysicsTools.Heppy.analyzers.examples.SimpleJetAnalyzer import SimpleJetAnalyzer
0049 all_jets = cfg.Analyzer(
0050     SimpleJetAnalyzer,
0051     'all_jets',
0052     njets = 4, 
0053     filter_func = lambda x : True
0054     )
0055 
0056 # filtering could be done in the SimpleJetAnalyzer above. 
0057 # here, we illustrate the use of the generic Filter module
0058 from PhysicsTools.HeppyCore.analyzers.Filter import Filter
0059 sel_jets = cfg.Analyzer(
0060     Filter,
0061     'jets',
0062     input_objects = 'all_jets',
0063     filter_func = lambda x : x.pt()>30 
0064     )
0065 
0066 
0067 # a simple tree with a Z candidate and the two leading jets (if any)
0068 from PhysicsTools.Heppy.analyzers.examples.ZJetsTreeAnalyzer import ZJetsTreeAnalyzer
0069 tree = cfg.Analyzer(
0070     ZJetsTreeAnalyzer
0071     )
0072 
0073 
0074 # definition of a sequence of analyzers,
0075 # the analyzers will process each event in this order
0076 sequence = cfg.Sequence( [ 
0077         muons,
0078         dimuons,
0079         all_jets,
0080         sel_jets,
0081         tree
0082         ] )
0083 
0084 # finalization of the configuration object. 
0085 from PhysicsTools.HeppyCore.framework.eventsfwlite import Events
0086 config = cfg.Config( components = selectedComponents,
0087                      sequence = sequence, 
0088                      services = [],
0089                      events_class = Events)
0090 
0091 print(config)
0092 
0093 if __name__ == '__main__':
0094     # can either run this configuration through heppy, 
0095     # or directly in python or ipython for easier development. 
0096     # try: 
0097     # 
0098     #   ipython -i simple_example_cfg.py
0099     # 
0100     from PhysicsTools.Heppy.physicsutils.LorentzVectors import LorentzVector
0101 
0102     from PhysicsTools.HeppyCore.framework.looper import Looper 
0103     looper = Looper( 'Loop', config, nPrint = 5, nEvents=100) 
0104     looper.loop()
0105     looper.write()
0106 
0107     # and now, let's play with the contents of the event
0108     print(looper.event)
0109     pz = LorentzVector()
0110     for imu, mu in enumerate(looper.event.muons): 
0111         print('muon1', mu, 'abs iso=', mu.relIso()*mu.pt())
0112         pz += mu.p4()
0113     print('z candidate mass = ', pz.M())
0114 
0115     # you can stay in ipython on a given event 
0116     # and paste more and more code as you need it until 
0117     # your code is correct. 
0118     # then put your code in an analyzer, and loop again. 
0119 
0120     def next():
0121         looper.process(looper.event.iEv+1)