Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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