Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:50

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