Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #! /usr/bin/env python
0002 # This example shows how to have multiple Tree either in the same file on in different file.
0003 # In particular here we create a second tree producer containing only information and then, 
0004 # cloning it in two copies, we store it both in the same file as the main tree and in separate file
0005 
0006 from __future__ import print_function
0007 import ROOT
0008 import PhysicsTools.HeppyCore.framework.config as cfg
0009 # avoid creating subdirs, in case subdirs are wanted the treeProducer should have different names (set name="blabla" in the config)
0010 cfg.Analyzer.nosubdir=True
0011 
0012 # The content of the output tree is defined here
0013 # the definitions of the NtupleObjects are located under PhysicsTools/Heppy/pythonanalyzers/objects/autophobj.py
0014  
0015 from PhysicsTools.Heppy.analyzers.core.AutoFillTreeProducer  import * 
0016 treeProducer= cfg.Analyzer(
0017     class_object=AutoFillTreeProducer, 
0018     verbose=False, 
0019     vectorTree = True,
0020         #here the list of simple event variables (floats, int) can be specified
0021         globalVariables = [
0022              NTupleVariable("rho",  lambda ev: ev.rho, float, help="jets rho"),
0023         ],
0024         #here one can specify compound objects 
0025         globalObjects = {
0026           "met"    : NTupleObject("met",     metType, help="PF E_{T}^{miss}, after default type 1 corrections"),
0027         },
0028     collections = {
0029         #The following would just store the electrons and muons from miniaod without any selection or cleaning
0030                 # only the basice particle information is saved
0031         #"slimmedMuons" : ( AutoHandle( ("slimmedMuons",), "std::vector<pat::Muon>" ),
0032                 #           NTupleCollection("mu", particleType, 4, help="patMuons, directly from MINIAOD") ),
0033                 #"slimmedElectron" : ( AutoHandle( ("slimmedElectrons",), "std::vector<pat::Electron>" ),
0034                 #           NTupleCollection("ele", particleType, 4, help="patElectron, directly from MINIAOD") ),
0035 
0036         #standard dumping of objects
0037             "selectedLeptons" : NTupleCollection("leptons", leptonType, 8, help="Leptons after the preselection"),
0038                 "selectedTaus"    : NTupleCollection("TauGood", tauType, 3, help="Taus after the preselection"),
0039             "cleanJets"       : NTupleCollection("Jet",     jetType, 8, help="Cental jets after full selection and cleaning, sorted by b-tag"),
0040         #dump of gen objects
0041                 "gentopquarks"    : NTupleCollection("GenTop",     genParticleType, 2, help="Generated top quarks from hard scattering"),
0042                 "genbquarks"      : NTupleCollection("GenBQuark",  genParticleType, 2, help="Generated bottom quarks from top quark decays"),
0043                 "genwzquarks"     : NTupleCollection("GenQuark",   genParticleType, 6, help="Generated quarks from W/Z decays"),
0044                 "genleps"         : NTupleCollection("GenLep",     genParticleType, 6, help="Generated leptons from W/Z decays"),
0045                 "gentauleps"      : NTupleCollection("GenLepFromTau", genParticleType, 6, help="Generated leptons from decays of taus from W/Z/h decays"),
0046 
0047     }
0048     )
0049 
0050 #make a light weight dump containing only generator information
0051 treeProducer2= cfg.Analyzer(
0052     treename="genonly",
0053     ignoreAnalyzerBookings=True, #we do not want trigger bits here or any other central booking
0054         class_object=AutoFillTreeProducer,
0055         verbose=False,
0056         vectorTree = True,
0057         collections = {
0058                 #dump of gen objects
0059                 "gentopquarks"    : NTupleCollection("GenTop",     genParticleType, 2, help="Generated top quarks from hard scattering"),
0060                 "genbquarks"      : NTupleCollection("GenBQuark",  genParticleType, 2, help="Generated bottom quarks from top quark decays"),
0061                 "genwzquarks"     : NTupleCollection("GenQuark",   genParticleType, 6, help="Generated quarks from W/Z decays"),
0062                 "genleps"         : NTupleCollection("GenLep",     genParticleType, 6, help="Generated leptons from W/Z decays"),
0063                 "gentauleps"      : NTupleCollection("GenLepFromTau", genParticleType, 6, help="Generated leptons from decays of taus from W/Z/h decays"),
0064 
0065         }
0066         )
0067 
0068 #create a copy of tree producer with the difference that it stores it in a separate file
0069 from copy import deepcopy 
0070 treeProducer3 = deepcopy(treeProducer2)
0071 treeProducer3.filter = lambda ev : len(getattr(ev,"genbquarks",[])) > 0 # select only events with b-quarks
0072 treeProducer3.outservicename="genonlyfile"
0073 
0074 
0075 
0076 # Import standard analyzers and take their default config
0077 from PhysicsTools.Heppy.analyzers.objects.LeptonAnalyzer import LeptonAnalyzer
0078 LepAna = LeptonAnalyzer.defaultConfig
0079 from PhysicsTools.Heppy.analyzers.objects.VertexAnalyzer import VertexAnalyzer
0080 VertexAna = VertexAnalyzer.defaultConfig
0081 from PhysicsTools.Heppy.analyzers.objects.PhotonAnalyzer import PhotonAnalyzer
0082 PhoAna = PhotonAnalyzer.defaultConfig
0083 from PhysicsTools.Heppy.analyzers.objects.TauAnalyzer import TauAnalyzer
0084 TauAna = TauAnalyzer.defaultConfig
0085 from PhysicsTools.Heppy.analyzers.objects.JetAnalyzer import JetAnalyzer
0086 JetAna = JetAnalyzer.defaultConfig
0087 from PhysicsTools.Heppy.analyzers.gen.LHEAnalyzer import LHEAnalyzer 
0088 LHEAna = LHEAnalyzer.defaultConfig
0089 from PhysicsTools.Heppy.analyzers.gen.GeneratorAnalyzer import GeneratorAnalyzer 
0090 GenAna = GeneratorAnalyzer.defaultConfig
0091 from PhysicsTools.Heppy.analyzers.objects.METAnalyzer import METAnalyzer
0092 METAna = METAnalyzer.defaultConfig
0093 from PhysicsTools.Heppy.analyzers.core.PileUpAnalyzer import PileUpAnalyzer
0094 PUAna = PileUpAnalyzer.defaultConfig
0095 from PhysicsTools.Heppy.analyzers.core.TriggerBitAnalyzer import TriggerBitAnalyzer
0096 FlagsAna = TriggerBitAnalyzer.defaultEventFlagsConfig
0097 
0098 # Configure trigger bit analyzer
0099 from PhysicsTools.Heppy.analyzers.core.TriggerBitAnalyzer import TriggerBitAnalyzer
0100 TrigAna= cfg.Analyzer(
0101     verbose=False,
0102     class_object=TriggerBitAnalyzer,
0103     #grouping several paths into a single flag
0104     # v* can be used to ignore the version of a path
0105     triggerBits={
0106     'ELE':["HLT_Ele23_Ele12_CaloId_TrackId_Iso_v*","HLT_Ele32_eta2p1_WP85_Gsf_v*","HLT_Ele32_eta2p1_WP85_Gsf_v*"],
0107     'MU': ["HLT_Mu17_TrkIsoVVL_TkMu8_TrkIsoVVL_v*","HLT_Mu17_TrkIsoVVL_Mu8_TrkIsoVVL_v*","HLT_IsoTkMu24_eta2p1_IterTrk02_v*","HLT_IsoTkMu24_IterTrk02_v*"],
0108     },
0109 #   processName='HLT',
0110 #   outprefix='HLT'
0111     #setting 'unrollbits' to true will not only store the OR for each set of trigger bits but also the individual bits
0112     #caveat: this does not unroll the version numbers
0113     unrollbits=True 
0114     )
0115 
0116 
0117 
0118 #replace some parameters
0119 LepAna.loose_muon_pt = 10
0120 
0121 sequence = [LHEAna,FlagsAna, GenAna, PUAna,TrigAna,VertexAna,LepAna,TauAna,PhoAna,JetAna,METAna,treeProducer,treeProducer2,treeProducer3]
0122 
0123 #use tfile service to provide a single TFile to all modules where they
0124 #can write any root object. If the name is 'outputfile' or the one specified in treeProducer
0125 #also the treeProducer uses this file
0126 from PhysicsTools.HeppyCore.framework.services.tfile import TFileService 
0127 output_service = cfg.Service(
0128       TFileService,
0129       'outputfile',
0130       name="outputfile",
0131       fname='tree.root',
0132       option='recreate'
0133     )
0134 output_service2= cfg.Service(
0135       TFileService,
0136       'genonlyfile',
0137       name="genonlyfile",
0138       fname='treegen.root',
0139       option='recreate'
0140     )
0141 
0142 
0143 # the following two lines are just for automatic testing
0144 # they are not needed for running on your own samples
0145 from PhysicsTools.Heppy.utils.miniAodFiles import miniAodFiles
0146 testfiles=miniAodFiles()
0147 print("Running on test file %s" % testfiles)
0148 
0149 sample = cfg.MCComponent(
0150 #specify the file you want to run on
0151 #    files = ["/scratch/arizzi/Hbb/CMSSW_7_2_2_patch2/src/VHbbAnalysis/Heppy/test/ZLL-8A345C56-6665-E411-9C25-1CC1DE04DF20.root"],
0152     files = testfiles,
0153     name="SingleSample", isMC=True,isEmbed=False
0154     )
0155 
0156 # the following is declared in case this cfg is used in input to the heppy.py script
0157 from PhysicsTools.HeppyCore.framework.eventsfwlite import Events
0158 selectedComponents = [sample]
0159 config = cfg.Config( components = selectedComponents,
0160                      sequence = sequence,
0161                      services = [output_service,output_service2],  
0162                      events_class = Events)
0163 
0164 # and the following runs the process directly if running as with python filename.py  
0165 if __name__ == '__main__':
0166     from PhysicsTools.HeppyCore.framework.looper import Looper 
0167     looper = Looper( 'Loop', config, nPrint = 5,nEvents=300) 
0168     looper.loop()
0169     looper.write()