def beginLoop(self, setup):
     super(NJetsAnalyzer, self).beginLoop(setup)
     self.averages.add('NUP', Average('NUP'))
     self.averages.add('NJets', Average('NJets'))
     self.averages.add('NJetWeight', Average('NJetWeight'))
     if self.cfg_comp.isMC:
         self.rootfile = TFile('/'.join([self.dirName, 'NUP.root']),
                               'recreate')
         self.nup = TH1F('nup', 'nup', 20, 0, 20)
         self.njets = TH1F('njets', 'njets', 10, 0, 10)
         self.tree = TreeNumpy('tree', 'test tree for NJetsAnalyzer')
         if self.cfg_ana.fillTree:
             self.tree.var('njets', int)
             self.tree.var('nup', int)
             self.tree.var('weight')
Esempio n. 2
0
 def beginLoop(self):
     super(NJetsAnalyzer,self).beginLoop()        
     self.averages.add('NUP', Average('NUP') )
     self.averages.add('NJets', Average('NJets') )
     self.averages.add('NJetWeight', Average('NJetWeight') )
     if self.cfg_comp.isMC:
         self.rootfile = TFile('/'.join([self.dirName,
                                         'NUP.root']),
                               'recreate')
         self.nup = TH1F('nup', 'nup', 20,0,20)
         self.njets = TH1F('njets', 'njets', 10,0,10)
         self.tree = TreeNumpy('tree', 'test tree for NJetsAnalyzer')
         if self.cfg_ana.fillTree:
             self.tree.var('njets', int)
             self.tree.var('nup', int)
             self.tree.var('weight')
Esempio n. 3
0
class NJetsAnalyzer( Analyzer ):
#class NJetsAnalyzer( GenParticleAnalyzer ):
    '''saves the NUP variable from the LHEEventProduct information.
    
    For the W+jets case:
    NUP = 5 : 0jets
    NUP = 6 : 1jet 
    ...
    
    In case of data, NUP = -1.
    In case of other MCs, the value is saved.
    '''

    def __init__(self, cfg_ana, cfg_comp, looperName):
        super(NJetsAnalyzer,self).__init__(cfg_ana, cfg_comp, looperName)

        cname = self.cfg_comp.name

        # wpat = re.compile('(DY|W)\d?Jet.*') # match DY1Jet, DYJet, W1Jet, WJet, etc.
        # match = wpat.match(self.cfg_comp.name)
        # self.isWJets = not (match is None)

        # if self.isWJets:
        self.applyWeight = False
        if hasattr( self.cfg_comp, 'nevents' ):
            assert( hasattr( self.cfg_comp, 'fractions' ) )
            assert(len(self.cfg_comp.nevents)==len(self.cfg_comp.fractions))  
            self.ninc = self.cfg_comp.nevents[0]
            self.cfg_comp.nevents[0] = 0.
            self.ni = [frac*self.ninc for frac in self.cfg_comp.fractions]
            self.weighti = []
            for ninc, nexc in zip(self.ni, self.cfg_comp.nevents ):
                self.weighti.append( ninc/(ninc+nexc) )
            self.applyWeight = True


        
    def beginLoop(self):
        super(NJetsAnalyzer,self).beginLoop()        
        self.averages.add('NUP', Average('NUP') )
        self.averages.add('NJets', Average('NJets') )
        self.averages.add('NJetWeight', Average('NJetWeight') )
        if self.cfg_comp.isMC:
            self.rootfile = TFile('/'.join([self.dirName,
                                            'NUP.root']),
                                  'recreate')
            self.nup = TH1F('nup', 'nup', 20,0,20)
            self.njets = TH1F('njets', 'njets', 10,0,10)
            self.tree = TreeNumpy('tree', 'test tree for NJetsAnalyzer')
            #import pdb ; pdb.set_trace()
            if self.cfg_ana.fillTree:
                self.tree.var('njets', int)
                self.tree.var('nup', int)
                self.tree.var('weight')
                
            
    def process(self, iEvent, event):
        event.NUP = -1
        event.NJetWeight = 1
        
        if not self.cfg_comp.isMC:
            return True

        if not self.applyWeight:
            return True

        
        self.readCollections( iEvent )
        event.NUP = self.mchandles['source'].product().hepeup().NUP
 
        # removing the 2 incoming partons, a boson,
        # and the 2 partons resulting from the decay of a boson
        njets = event.NUP-5
        event.NJetWeight = self.weighti[njets]
        event.eventWeight *= event.NJetWeight

        if self.cfg_ana.fillTree:
            self.tree.reset()
            self.tree.fill('njets', njets)
            self.tree.fill('nup', event.NUP)
            self.tree.fill('weight', event.NJetWeight)
            self.tree.tree.Fill()
            
        self.averages['NUP'].add( event.NUP )
        self.averages['NJets'].add( njets )
        self.averages['NJetWeight'].add( event.NJetWeight )
        self.nup.Fill(event.NUP)
        self.njets.Fill(njets)

        
        if self.cfg_ana.verbose:
            print 'NUP, njets, weight',event.NUP, njets, event.NJetWeight
        return True
    

    def declareHandles(self):
        '''Reads LHEEventsProduct.'''
        super(NJetsAnalyzer, self).declareHandles()
        self.mchandles['source'] =  AutoHandle(
            'source',
            'LHEEventProduct'
            )
        
    def write(self):
        super(NJetsAnalyzer, self).write()
        if self.cfg_comp.isMC:
            self.rootfile.Write()
            self.rootfile.Close()
class NJetsAnalyzer(Analyzer):
    '''Saves the number of partons and gen HT from the LHEEventProduct 
    information, and reweights the events if according information present
    in the sample configuration.

    Note that unlike before, the number of partons is calculated directly
    and not taken from the NUP variable inside the LHEEventProduct since the
    latter doesn't account for off-shell Z/W bosons anymore.
    '''
    def __init__(self, cfg_ana, cfg_comp, looperName):
        super(NJetsAnalyzer, self).__init__(cfg_ana, cfg_comp, looperName)

        # wpat = re.compile('(DY|W)\d?Jet.*') # match DY1Jet, DYJet, W1Jet, WJet, etc.
        # match = wpat.match(self.cfg_comp.name)
        # self.isWJets = not (match is None)

        # if self.isWJets:
        self.applyWeight = False
        if hasattr(self.cfg_comp, 'nevents') and hasattr(
                self.cfg_comp, 'fractions'):
            assert (len(self.cfg_comp.nevents) == len(self.cfg_comp.fractions))
            self.ninc = self.cfg_comp.nevents[0]
            self.cfg_comp.nevents[0] = 0.
            self.ni = [frac * self.ninc for frac in self.cfg_comp.fractions]
            self.weighti = []
            for ninc, nexc in zip(self.ni, self.cfg_comp.nevents):
                self.weighti.append(ninc / (ninc + nexc))
            self.applyWeight = True

        self.applyWeightFunc = False

        if hasattr(self.cfg_comp, 'weight_func'):
            self.weight_func = self.cfg_comp.weight_func
            self.applyWeightFunc = True

        self.hasMCProduct = True

    def beginLoop(self, setup):
        super(NJetsAnalyzer, self).beginLoop(setup)
        self.averages.add('NUP', Average('NUP'))
        self.averages.add('NJets', Average('NJets'))
        self.averages.add('NJetWeight', Average('NJetWeight'))
        if self.cfg_comp.isMC:
            self.rootfile = TFile('/'.join([self.dirName, 'NUP.root']),
                                  'recreate')
            self.nup = TH1F('nup', 'nup', 20, 0, 20)
            self.njets = TH1F('njets', 'njets', 10, 0, 10)
            self.tree = TreeNumpy('tree', 'test tree for NJetsAnalyzer')
            if self.cfg_ana.fillTree:
                self.tree.var('njets', int)
                self.tree.var('nup', int)
                self.tree.var('weight')

    def process(self, event):
        event.NUP = -1
        event.genPartonHT = 0.
        event.NJetWeight = 1
        event.geninvmass = -999.

        if not self.cfg_comp.isMC:
            return True

        if not self.hasMCProduct:
            return True

        self.readCollections(event.input)

        self.mchandles['source'].ReallyLoad(event.input)
        if not self.mchandles['source'].isValid():
            self.hasMCProduct = False
            print 'WARNING: No LHEEventProduct from externalLHEProducer present in event'
            print '  (fine for sample directly produced in Pythia)'
            return True

        hep = self.mchandles['source'].product().hepeup()

        # For reference: The following worked in run 1, but for run 2 MC,
        # the intermediate boson is not saved if it's too far off shell...
        # removing the 2 incoming partons, a boson,
        # and the 2 partons resulting from the decay of a boson
        # njets = event.NUP - 5

        event.NJetWeight = 1.

        sumpt = 0.
        outgoing = []
        mass = []

        # print [(a, b) for a, b in zip(hep.ISTUP, hep.IDUP)]

        for status, pdg, moth, mom in zip(hep.ISTUP, hep.IDUP, hep.MOTHUP,
                                          hep.PUP):

            if status == 1 and abs(pdg) in [21, 1, 2, 3, 4, 5]:
                sumpt += math.sqrt(mom.x[0]**2 + mom.x[1]**2)
                outgoing.append(pdg)

            if status == 1 and abs(pdg) in [11, 12, 13, 14, 15, 16]:
                l = TLorentzVector(mom.x[0], mom.x[1], mom.x[2], mom.x[3])
                mass.append(l)

        njets = len(outgoing)
        event.NUP = njets

        if len(mass) == 2:
            event.geninvmass = (mass[0] + mass[1]).M()

        event.genPartonHT = sumpt

        if self.applyWeight:
            event.NJetWeight = self.weighti[njets]
            event.eventWeight *= event.NJetWeight

            self.averages['NJetWeight'].add(event.NJetWeight)

            if self.cfg_ana.verbose:
                print 'NUP, njets, weight', event.NUP, njets, event.NJetWeight

        if self.applyWeightFunc:
            event.NJetWeight = self.weight_func(njets, event.geninvmass)

        if self.cfg_ana.fillTree:
            self.tree.reset()
            self.tree.fill('njets', njets)
            self.tree.fill('nup', event.NUP)
            self.tree.fill('weight', event.NJetWeight)
            self.tree.tree.Fill()

        self.averages['NUP'].add(event.NUP)
        self.averages['NJets'].add(njets)

        self.nup.Fill(event.NUP)
        self.njets.Fill(njets)

        return True

    def declareHandles(self):
        '''Reads LHEEventsProduct.'''
        super(NJetsAnalyzer, self).declareHandles()
        self.mchandles['source'] = AutoHandle('externalLHEProducer',
                                              'LHEEventProduct',
                                              mayFail=True)

    def write(self, setup):
        super(NJetsAnalyzer, self).write(setup)
        if self.cfg_comp.isMC:
            self.rootfile.Write()
            self.rootfile.Close()
Esempio n. 5
0
 def write(self, dirName, fileName='RLTInfo.root'):
     f = TFile('/'.join( [dirName, fileName]), 'RECREATE')
     t = Tree('RLTInfo','HLT/Run/Lumi information')
     t.var('run', int )
     t.var('lumi', int )
     t.var('counts', int )
     t.var('trigger', int )
     for rlt, count in self.dict.iteritems():
         t.fill('run', rlt[1])
         t.fill('lumi', rlt[2])
         t.fill( 'counts', count.integer)
         t.tree.Fill()
     f.Write()
     f.Close()
Esempio n. 6
0
 def write(self, dirName, fileName='RLTInfo.root'):
     f = TFile('/'.join([dirName, fileName]), 'RECREATE')
     t = Tree('RLTInfo', 'HLT/Run/Lumi information')
     t.var('run', int)
     t.var('lumi', int)
     t.var('counts', int)
     t.var('trigger', int)
     for rlt, count in self.dict.iteritems():
         t.fill('run', rlt[1])
         t.fill('lumi', rlt[2])
         t.fill('counts', count.integer)
         t.tree.Fill()
     f.Write()
     f.Close()
Esempio n. 7
0
        postfix = int(shift)
    except ValueError:
        print 'arg should be "data" or the shift value (a float)'
        sys.exit(1)


chain = Chain(None, "Prod_Rochester_29Nov/{comp}/ZJetsTreeProducer/ZJetsTreeProducer_tree.root".format(comp=comp))
# chain = Chain(None, 'higgsvbf125.root')

name = "{prefix}_{postfix}".format(prefix=prefix, postfix=postfix)

print shift, name, comp

file = TFile(name + ".root", "recreate")

tree = TreeNumpy(chain.GetName(), ", ".join([chain.GetTitle(), "corrected"]))

tree.copyStructure(chain)

toShift = ["jet1_pt", "jet2_pt"]

shift_factor = shift / 100.0


def correct(tree, entry, jetname, shift):
    eta = getattr(ie, "_".join([jetname, "eta"]))
    pt = getattr(ie, "_".join([jetname, "pt"]))
    corpt = pt
    if shift < 0:
        etabin = h_cor.FindBin(eta)
        shift = h_cor.GetBinContent(etabin) / 100.0
Esempio n. 8
0
class NJetsAnalyzer(Analyzer):
    #class NJetsAnalyzer( GenParticleAnalyzer ):
    '''saves the NUP variable from the LHEEventProduct information.
    
    For the W+jets case:
    NUP = 5 : 0jets
    NUP = 6 : 1jet 
    ...
    
    In case of data, NUP = -1.
    In case of other MCs, the value is saved.
    '''
    def __init__(self, cfg_ana, cfg_comp, looperName):
        super(NJetsAnalyzer, self).__init__(cfg_ana, cfg_comp, looperName)

        cname = self.cfg_comp.name

        # wpat = re.compile('(DY|W)\d?Jet.*') # match DY1Jet, DYJet, W1Jet, WJet, etc.
        # match = wpat.match(self.cfg_comp.name)
        # self.isWJets = not (match is None)

        # if self.isWJets:
        self.applyWeight = False
        if hasattr(self.cfg_comp, 'nevents'):
            assert (hasattr(self.cfg_comp, 'fractions'))
            assert (len(self.cfg_comp.nevents) == len(self.cfg_comp.fractions))
            self.ninc = self.cfg_comp.nevents[0]
            self.cfg_comp.nevents[0] = 0.
            self.ni = [frac * self.ninc for frac in self.cfg_comp.fractions]
            self.weighti = []
            for ninc, nexc in zip(self.ni, self.cfg_comp.nevents):
                self.weighti.append(ninc / (ninc + nexc))
            self.applyWeight = True

    def beginLoop(self):
        super(NJetsAnalyzer, self).beginLoop()
        self.averages.add('NUP', Average('NUP'))
        self.averages.add('NJets', Average('NJets'))
        self.averages.add('NJetWeight', Average('NJetWeight'))
        if self.cfg_comp.isMC:
            self.rootfile = TFile('/'.join([self.dirName, 'NUP.root']),
                                  'recreate')
            self.nup = TH1F('nup', 'nup', 20, 0, 20)
            self.njets = TH1F('njets', 'njets', 10, 0, 10)
            self.tree = TreeNumpy('tree', 'test tree for NJetsAnalyzer')
            if self.cfg_ana.fillTree:
                self.tree.var('njets', int)
                self.tree.var('nup', int)
                self.tree.var('weight')

    def process(self, iEvent, event):
        event.NUP = -1
        event.NJetWeight = 1

        if not self.cfg_comp.isMC:
            return True

        if not self.applyWeight:
            return True

        # JAN: FIXME: No HEP event product in W+Jets extensions, but we now NUP
        # from the file name
        if 'ext' in self.cfg_comp.name:
            event.NUP = int(self.cfg_comp.name[1]) + 5
        else:
            self.readCollections(iEvent)
            event.NUP = self.mchandles['source'].product().hepeup().NUP

        # removing the 2 incoming partons, a boson,
        # and the 2 partons resulting from the decay of a boson
        njets = event.NUP - 5
        event.NJetWeight = self.weighti[njets]
        event.eventWeight *= event.NJetWeight

        if self.cfg_ana.fillTree:
            self.tree.reset()
            self.tree.fill('njets', njets)
            self.tree.fill('nup', event.NUP)
            self.tree.fill('weight', event.NJetWeight)
            self.tree.tree.Fill()

        self.averages['NUP'].add(event.NUP)
        self.averages['NJets'].add(njets)
        self.averages['NJetWeight'].add(event.NJetWeight)
        self.nup.Fill(event.NUP)
        self.njets.Fill(njets)

        if self.cfg_ana.verbose:
            print 'NUP, njets, weight', event.NUP, njets, event.NJetWeight
        return True

    def declareHandles(self):
        '''Reads LHEEventsProduct.'''
        super(NJetsAnalyzer, self).declareHandles()
        self.mchandles['source'] = AutoHandle('source', 'LHEEventProduct')

    def write(self):
        super(NJetsAnalyzer, self).write()
        if self.cfg_comp.isMC:
            self.rootfile.Write()
            self.rootfile.Close()
Esempio n. 9
0
class NJetsAnalyzer(Analyzer):
    # class NJetsAnalyzer( GenParticleAnalyzer ):
    '''saves the NUP variable from the LHEEventProduct information.

    For the W+jets case:
    NUP = 5 : 0jets
    NUP = 6 : 1jet 
    ...

    In case of data, NUP = -1.
    In case of other MCs, the value is saved.
    '''
    def __init__(self, cfg_ana, cfg_comp, looperName):
        super(NJetsAnalyzer, self).__init__(cfg_ana, cfg_comp, looperName)

        # wpat = re.compile('(DY|W)\d?Jet.*') # match DY1Jet, DYJet, W1Jet, WJet, etc.
        # match = wpat.match(self.cfg_comp.name)
        # self.isWJets = not (match is None)

        # if self.isWJets:
        self.applyWeight = False
        if hasattr(self.cfg_comp, 'nevents') and hasattr(
                self.cfg_comp, 'fractions'):
            assert (len(self.cfg_comp.nevents) == len(self.cfg_comp.fractions))
            self.ninc = self.cfg_comp.nevents[0]
            self.cfg_comp.nevents[0] = 0.
            self.ni = [frac * self.ninc for frac in self.cfg_comp.fractions]
            self.weighti = []
            for ninc, nexc in zip(self.ni, self.cfg_comp.nevents):
                self.weighti.append(ninc / (ninc + nexc))
            self.applyWeight = True

        self.hasMCProduct = True

    def beginLoop(self, setup):
        super(NJetsAnalyzer, self).beginLoop(setup)
        self.averages.add('NUP', Average('NUP'))
        self.averages.add('NJets', Average('NJets'))
        self.averages.add('NJetWeight', Average('NJetWeight'))
        if self.cfg_comp.isMC:
            self.rootfile = TFile('/'.join([self.dirName, 'NUP.root']),
                                  'recreate')
            self.nup = TH1F('nup', 'nup', 20, 0, 20)
            self.njets = TH1F('njets', 'njets', 10, 0, 10)
            self.tree = TreeNumpy('tree', 'test tree for NJetsAnalyzer')
            if self.cfg_ana.fillTree:
                self.tree.var('njets', int)
                self.tree.var('nup', int)
                self.tree.var('weight')

    def process(self, event):
        event.NUP = -1
        event.genPartonHT = 0.
        event.NJetWeight = 1

        if not self.cfg_comp.isMC:
            return True

        if not self.hasMCProduct:
            return True

        self.readCollections(event.input)

        self.mchandles['source'].ReallyLoad(event.input)
        if not self.mchandles['source'].isValid():
            self.hasMCProduct = False
            print 'WARNING: No LHEEventProduct from externalLHEProducer present in event'
            print '  (fine for sample directly produced in Pythia)'
            return True

        hep = self.mchandles['source'].product().hepeup()

        # For reference: The following worked in run 1, but for run 2 MC,
        # the intermediate boson is not saved if it's too far off shell...
        # removing the 2 incoming partons, a boson,
        # and the 2 partons resulting from the decay of a boson
        # njets = event.NUP - 5

        event.NJetWeight = 1.

        sumpt = 0.
        outgoing = []

        # print [(a, b) for a, b in zip(hep.ISTUP, hep.IDUP)]

        for status, pdg, moth, mom in zip(hep.ISTUP, hep.IDUP, hep.MOTHUP,
                                          hep.PUP):

            if status == 1 and abs(pdg) in [21, 1, 2, 3, 4, 5]:
                sumpt += math.sqrt(mom.x[0]**2 + mom.x[1]**2)
                outgoing.append(pdg)

        njets = len(outgoing)
        event.NUP = hep.NUP
        event.NUP = njets

        event.genPartonHT = sumpt

        if self.applyWeight:
            event.NJetWeight = self.weighti[njets]
            event.eventWeight *= event.NJetWeight

            self.averages['NJetWeight'].add(event.NJetWeight)

            if self.cfg_ana.verbose:
                print 'NUP, njets, weight', event.NUP, njets, event.NJetWeight

        if self.cfg_ana.fillTree:
            self.tree.reset()
            self.tree.fill('njets', njets)
            self.tree.fill('nup', event.NUP)
            self.tree.fill('weight', event.NJetWeight)
            self.tree.tree.Fill()

        self.averages['NUP'].add(event.NUP)
        self.averages['NJets'].add(njets)

        self.nup.Fill(event.NUP)
        self.njets.Fill(njets)

        return True

    def declareHandles(self):
        '''Reads LHEEventsProduct.'''
        super(NJetsAnalyzer, self).declareHandles()
        self.mchandles['source'] = AutoHandle('externalLHEProducer',
                                              'LHEEventProduct',
                                              mayFail=True)

    def write(self, setup):
        super(NJetsAnalyzer, self).write(setup)
        if self.cfg_comp.isMC:
            self.rootfile.Write()
            self.rootfile.Close()
Esempio n. 10
0
        print 'arg should be "data" or the shift value (a float)'
        sys.exit(1)

chain = Chain(
    None,
    'Prod_Rochester_29Nov/{comp}/ZJetsTreeProducer/ZJetsTreeProducer_tree.root'
    .format(comp=comp))
# chain = Chain(None, 'higgsvbf125.root')

name = '{prefix}_{postfix}'.format(prefix=prefix, postfix=postfix)

print shift, name, comp

file = TFile(name + '.root', 'recreate')

tree = TreeNumpy(chain.GetName(), ', '.join([chain.GetTitle(), 'corrected']))

tree.copyStructure(chain)

toShift = ['jet1_pt', 'jet2_pt']

shift_factor = shift / 100.


def correct(tree, entry, jetname, shift):
    eta = getattr(ie, '_'.join([jetname, 'eta']))
    pt = getattr(ie, '_'.join([jetname, 'pt']))
    corpt = pt
    if shift < 0:
        etabin = h_cor.FindBin(eta)
        shift = h_cor.GetBinContent(etabin) / 100.