def skim(name): oldFile = TFile(name, "READ") oldTree = oldFile.Get("ntuple/tree") oldTree.SetBranchAddress("Lepton1", AddressOf(Lepton1, "pt")) oldTree.SetBranchAddress("Lepton2", AddressOf(Lepton2, "pt")) oldTree.SetBranchAddress("FatJet1", AddressOf(FatJet1, "pt")) oldTree.SetBranchAddress("V", AddressOf(V, "pt")) oldTree.SetBranchAddress("X", AddressOf(X, "pt")) oldTree.SetBranchAddress("MEt", AddressOf(MEt, "pt")) print 'skimming file', oldFile.GetName(), '\tevents =', oldTree.GetEntries( ), '\tweight =', oldTree.GetWeight() newFile = TFile("Skim/" + name, "RECREATE") newFile.cd() newTree = TTree("alpha", "alpha") EventNumberBranch = newTree.Branch('EventNumber', EventNumber, 'EventNumber/F') EventWeightBranch = newTree.Branch('EventWeight', EventWeight, 'EventWeight/F') RunNumberBranch = newTree.Branch('RunNumber', RunNumber, 'RunNumber/F') LumiNumberBranch = newTree.Branch('LumiNumber', LumiNumber, 'LumiNumber/F') isZtoEEBranch = newTree.Branch('isZtoEE', isZtoEE, 'isZtoEE/O') isZtoMMBranch = newTree.Branch('isZtoMM', isZtoMM, 'isZtoMM/O') isZtoNNBranch = newTree.Branch('isZtoNN', isZtoNN, 'isZtoNN/O') isMCBranch = newTree.Branch('isMC', isMC, 'isMC/O') FatJet1_ptBranch = newTree.Branch('FatJet1_pt', FatJet1_pt, 'FatJet1_pt/F') FatJet1_softdropPuppiMassBranch = newTree.Branch( 'FatJet1_softdropPuppiMass', FatJet1_softdropPuppiMass, 'FatJet1_softdropPuppiMass/F') FatJet1_softdropPuppiMassCorrBranch = newTree.Branch( 'FatJet1_softdropPuppiMassCorr', FatJet1_softdropPuppiMassCorr, 'FatJet1_softdropPuppiMassCorr/F') FatJet1_softdropPuppiMassCorrNotSmearedBranch = newTree.Branch( 'FatJet1_softdropPuppiMassCorrNotSmeared', FatJet1_softdropPuppiMassCorrNotSmeared, 'FatJet1_softdropPuppiMassCorrNotSmeared/F') FatJet1_puppiTau21Branch = newTree.Branch('FatJet1_puppiTau21', FatJet1_puppiTau21, 'FatJet1_puppiTau21/F') FatJet1_ddtTau21Branch = newTree.Branch('FatJet1_ddtTau21', FatJet1_ddtTau21, 'FatJet1_ddtTau21/F') FatJet1_CSVBranch = newTree.Branch('FatJet1_CSV', FatJet1_CSV, 'FatJet1_CSV/F') FatJet1_CSVRBranch = newTree.Branch('FatJet1_CSVR', FatJet1_CSVR, 'FatJet1_CSVR/F') FatJet1_CSV1Branch = newTree.Branch('FatJet1_CSV1', FatJet1_CSV1, 'FatJet1_CSV1/F') FatJet1_CSV2Branch = newTree.Branch('FatJet1_CSV2', FatJet1_CSV2, 'FatJet1_CSV2/F') FatJet1_CSVR1Branch = newTree.Branch('FatJet1_CSVR1', FatJet1_CSVR1, 'FatJet1_CSVR1/F') FatJet1_CSVR2Branch = newTree.Branch('FatJet1_CSVR2', FatJet1_CSVR2, 'FatJet1_CSVR2/F') V_massBranch = newTree.Branch('V_mass', V_mass, 'V_mass/F') V_ptBranch = newTree.Branch('V_pt', V_pt, 'V_pt/F') MEt_ptBranch = newTree.Branch('MEt_pt', MEt_pt, 'MEt_pt/F') X_massBranch = newTree.Branch('X_mass', X_mass, 'X_mass/F') X_tmassBranch = newTree.Branch('X_tmass', X_tmass, 'X_tmass/F') theweight = oldTree.GetWeight() for event in range(0, oldTree.GetEntries() - 1): oldTree.GetEntry(event) # Alpha selections # Channel if not oldTree.isZtoMM and not oldTree.isZtoEE and not oldTree.isZtoNN: continue # Trigger if not oldTree.isMC: if oldTree.isZtoMM: #if not ( oldTree.HLT_TkMu50_v or oldTree.HLT_Mu50_v ): continue if not (oldTree.HLT_Mu45_eta2p1_v): continue elif oldTree.isZtoEE: if not (oldTree.HLT_Ele105_CaloIdVT_GsfTrkIdT_v or oldTree.HLT_Ele115_CaloIdVT_GsfTrkIdT_v): continue elif oldTree.isZtoNN: if not (oldTree.HLT_PFMETNoMu90_PFMHTNoMu90_IDTight_v or oldTree.HLT_PFMETNoMu110_PFMHTNoMu110_IDTight_v or oldTree.HLT_PFMETNoMu120_PFMHTNoMu120_IDTight_v or oldTree.HLT_PFMET170_NoiseCleaned_v or oldTree.HLT_PFMET170_JetIdCleaned_v or oldTree.HLT_PFMET170_HBHECleaned_v): continue else: continue # Leptons if oldTree.isZtoMM and not ( ((Lepton1.isHighPt and Lepton2.isHighPt) or (Lepton1.isTrackerHighPt and Lepton2.isHighPt) or (Lepton1.isHighPt and Lepton2.isTrackerHighPt)) and Lepton1.pt > 55 and Lepton2.pt > 20 and abs(Lepton1.eta) < 2.1 and abs(Lepton2.eta) < 2.1 and not (Lepton1.pt > 500 and abs(Lepton1.eta) > 1.2) and not (Lepton2.pt > 500 and abs(Lepton2.eta) > 1.2) and Lepton1.trkIso < 0.1 and Lepton2.trkIso < 0.1): continue if oldTree.isZtoEE and not (Lepton1.pt > 135 and Lepton2.pt > 35 and Lepton1.isLoose and Lepton2.isLoose): continue # No Leptons if oldTree.isZtoNN and not ( MEt.pt > 200 and oldTree.nLooseMuons == 0 and oldTree.nVetoElectrons == 0 and oldTree.nPhotons == 0 and oldTree.nTaus == 0 and oldTree.MinJetMetDPhi > 0.5 and oldTree.Flag_EcalDeadCellTriggerPrimitiveFilter and oldTree.Flag_HBHENoiseFilter and oldTree.Flag_HBHENoiseIsoFilter and oldTree.Flag_globalTightHalo2016Filter and oldTree.Flag_goodVertices and oldTree.Flag_BadPFMuon and oldTree.Flag_BadChCand): continue if not oldTree.isMC: if oldTree.isZtoNN and not oldTree.Flag_eeBadScFilter: continue # Boost and Z if (oldTree.isZtoEE or oldTree.isZtoMM) and not (V.pt > 170 and FatJet1.pt > 170 and V.mass > 70 and V.mass < 110): continue # Boost and Cleaning for Z invisible if oldTree.isZtoNN and not (FatJet1.pt > 200 and FatJet1.isTight and FatJet1.nhf < 0.8 and FatJet1.chf > 0.2 and oldTree.MaxJetBTag < 0.5426 and X.dPhi > 2): continue # Grooming if not (FatJet1.softdropPuppiMassCorr > 30): continue # Copy relevant variables EventNumber[0] = oldTree.EventNumber EventWeight[0] = oldTree.EventWeight * theweight RunNumber[0] = oldTree.RunNumber LumiNumber[0] = oldTree.LumiNumber isZtoEE[0] = oldTree.isZtoEE isZtoMM[0] = oldTree.isZtoMM isZtoNN[0] = oldTree.isZtoNN isMC[0] = oldTree.isMC FatJet1_pt[0] = FatJet1.pt FatJet1_softdropPuppiMass[0] = FatJet1.softdropPuppiMass FatJet1_softdropPuppiMassCorr[0] = FatJet1.softdropPuppiMassCorr FatJet1_softdropPuppiMassCorrNotSmeared[ 0] = FatJet1.softdropPuppiMassCorrNotSmeared FatJet1_puppiTau21[0] = FatJet1.puppiTau21 FatJet1_ddtTau21[0] = FatJet1.ddtTau21 FatJet1_CSV[0] = FatJet1.CSV FatJet1_CSVR[0] = FatJet1.CSVR FatJet1_CSV1[0] = FatJet1.CSV1 FatJet1_CSV2[0] = FatJet1.CSV2 FatJet1_CSVR1[0] = FatJet1.CSVR1 FatJet1_CSVR2[0] = FatJet1.CSVR2 V_mass[0] = V.mass V_pt[0] = V.pt MEt_pt[0] = MEt.pt X_mass[0] = X.mass X_tmass[0] = X.tmass newTree.Fill() print 'produced skimmed file', newFile.GetName( ), '\tevents =', newTree.GetEntries(), '\tweight =', newTree.GetWeight() newFile.cd() newTree.Write() newFile.Close() oldFile.Close()
for strO in attr_out: tempAttrStrO = 'pfc' + str(ii + 1) + '_' + strO tempAttrStrI = forLola.attrNameDic[strO] + '_' + str(ii + 1) setattr(Jets1, tempAttrStrO, jet1[tempAttrStrI][i]) #!!!!!!!!!!!!!!!!!!!!deal with the empty value situation!!!!!!!!!!!!!!!!!!!!!! newTree.Fill() #showTimeLeft(ii=i,mode='e',startTime=start,numOfJobs=numOfEntriesToScan_local) #set up parallel pool pool = mp.Pool(processes=80) pool.map(FillTrees, xrange(0, numOfEntriesToScan_local)) print '\nproduced skimmed file', newFile.GetName( ), '\tevents =', newTree.GetEntries(), '\tweight =', newTree.GetWeight() newFile.cd() newFile.Write() newFile.Close() #----------------------------------------------------------------------------------------------------------- #_____________________________________________old lines: """ for j in xrange(num_of_jets): if 'QCD' in name: #oldTree.SetBranchAddress( 'Jets' , AddressOf(Jet_old_dict[j+1], 'pt') ) #oldTree.SetBranchAddress( 'MatchedCHSJet' + str(j+1) , AddressOf(Jet_old_dict[j+1], 'pt') ) elif 'ctauS' in name: #oldTree.SetBranchAddress( 'MatchedCHSJet' + str(j+1) , AddressOf(Jet_old_dict[j+1], 'pt') ) #oldTree.SetBranchAddress( 'Jets' , AddressOf(Jet_old_dict[j+1], 'pt') ) """
def skim(name): oldFile = TFile(name, "READ") oldTree = oldFile.Get("ntuple/tree") oldTree.SetBranchAddress("Lepton1", AddressOf(Lepton1, "pt")) oldTree.SetBranchAddress("Lepton2", AddressOf(Lepton2, "pt")) oldTree.SetBranchAddress("FatJet1", AddressOf(FatJet1, "pt")) oldTree.SetBranchAddress("V", AddressOf(V, "pt")) oldTree.SetBranchAddress("X", AddressOf(X, "pt")) print 'skimming file', oldFile.GetName(), '\tevents =', oldTree.GetEntries( ), '\tweight =', oldTree.GetWeight() newFile = TFile("Skim/" + name, "RECREATE") newFile.cd() newTree = TTree("alpha", "alpha") EventNumberBranch = newTree.Branch('EventNumber', EventNumber, 'EventNumber/F') EventWeightBranch = newTree.Branch('EventWeight', EventWeight, 'EventWeight/F') RunNumberBranch = newTree.Branch('RunNumber', RunNumber, 'RunNumber/F') LumiNumberBranch = newTree.Branch('LumiNumber', LumiNumber, 'LumiNumber/F') isZtoEEBranch = newTree.Branch('isZtoEE', isZtoEE, 'isZtoEE/O') isZtoMMBranch = newTree.Branch('isZtoMM', isZtoMM, 'isZtoMM/O') isMCBranch = newTree.Branch('isMC', isMC, 'isMC/O') FatJet1_ptBranch = newTree.Branch('FatJet1_pt', FatJet1_pt, 'FatJet1_pt/F') FatJet1_softdropPuppiMassBranch = newTree.Branch( 'FatJet1_softdropPuppiMass', FatJet1_softdropPuppiMass, 'FatJet1_softdropPuppiMass/F') FatJet1_softdropPuppiMassCorrBranch = newTree.Branch( 'FatJet1_softdropPuppiMassCorr', FatJet1_softdropPuppiMassCorr, 'FatJet1_softdropPuppiMassCorr/F') FatJet1_puppiTau21Branch = newTree.Branch('FatJet1_puppiTau21', FatJet1_puppiTau21, 'FatJet1_puppiTau21/F') FatJet1_ddtTau21Branch = newTree.Branch('FatJet1_ddtTau21', FatJet1_ddtTau21, 'FatJet1_ddtTau21/F') V_massBranch = newTree.Branch('V_mass', V_mass, 'V_mass/F') V_ptBranch = newTree.Branch('V_pt', V_pt, 'V_pt/F') X_massBranch = newTree.Branch('X_mass', X_mass, 'X_mass/F') theweight = oldTree.GetWeight() for event in range(0, oldTree.GetEntries() - 1): oldTree.GetEntry(event) # Alpha selections # Channel if not oldTree.isZtoMM and not oldTree.isZtoEE: continue # Trigger if not oldTree.isMC: if oldTree.isZtoMM: #if not ( oldTree.HLT_TkMu50_v or oldTree.HLT_Mu50_v ): continue if not (oldTree.HLT_Mu45_eta2p1_v): continue elif oldTree.isZtoEE: if not (oldTree.HLT_Ele105_CaloIdVT_GsfTrkIdT_v or oldTree.HLT_Ele115_CaloIdVT_GsfTrkIdT_v): continue else: continue # Leptons if oldTree.isZtoMM and not ( ((Lepton1.isHighPt and Lepton2.isHighPt) or (Lepton1.isTrackerHighPt and Lepton2.isHighPt) or (Lepton1.isHighPt and Lepton2.isTrackerHighPt)) and Lepton1.pt > 55 and Lepton2.pt > 20 and abs(Lepton1.eta) < 2.1 and abs(Lepton2.eta) < 2.1 and not (Lepton1.pt > 500 and abs(Lepton1.eta) > 1.2) and not (Lepton2.pt > 500 and abs(Lepton2.eta) > 1.2) and Lepton1.trkIso < 0.1 and Lepton2.trkIso < 0.1): continue if oldTree.isZtoEE and not (Lepton1.pt > 135 and Lepton2.pt > 35 and Lepton1.isLoose and Lepton2.isLoose): continue # Boost and Z if not (V.pt > 170 and FatJet1.pt > 170 and V.mass > 70 and V.mass < 110): continue # Grooming if not (FatJet1.softdropPuppiMassCorr > 30): continue # Copy relevant variables EventNumber[0] = oldTree.EventNumber EventWeight[0] = oldTree.EventWeight * theweight RunNumber[0] = oldTree.RunNumber LumiNumber[0] = oldTree.LumiNumber isZtoEE[0] = oldTree.isZtoEE isZtoMM[0] = oldTree.isZtoMM isMC[0] = oldTree.isMC FatJet1_pt[0] = FatJet1.pt FatJet1_softdropPuppiMass[0] = FatJet1.softdropPuppiMass FatJet1_softdropPuppiMassCorr[0] = FatJet1.softdropPuppiMassCorr FatJet1_puppiTau21[0] = FatJet1.puppiTau21 FatJet1_ddtTau21[0] = FatJet1.ddtTau21 V_mass[0] = V.mass V_pt[0] = V.pt X_mass[0] = X.mass newTree.Fill() print 'produced skimmed file', newFile.GetName( ), '\tevents =', newTree.GetEntries(), '\tweight =', newTree.GetWeight() newFile.cd() newTree.Write() newFile.Close() oldFile.Close()
def skim_c(name, newFileName): oldFile = TFile(name, "READ") oldTree = oldFile.Get("ntuple/tree") NofEntries = oldTree.GetEntriesFast() numOfEntriesToScan_local = NofEntries if NofEntries > nLimit: numOfEntriesToScan_local = nLimit if testOn == 1: numOfEntriesToScan_local = numOfEntriesToScan #locate and register the Jet branches of the old ttree print '\nskimming file', oldFile.GetName( ), '\tevents =', oldTree.GetEntries(), '\tweight =', oldTree.GetWeight( ), '\n' newFile = TFile(newFileName, "RECREATE") #('Skim/' + newFileName, "RECREATE") newFile.cd() newTree = TTree(treeName, treeName) if lola_on == 0: forBDT.branchLeafStrGen() newTree.Branch(JetName, Jets1, forBDT.branchLeafStr) elif lola_on == 1: forLola.branchLeafStrGen() newTree.Branch(JetName, Jets1, forLola.branchLeafStr) # this attribute list must exactly match (the order of) the features in the header file!!!! attr = forBDT.preList + forBDT.attrTypeList attr_out = forLola.attrTypeList startTemp = 0 #theweight = oldTree.GetWeight() #def FillTrees(i): for i in xrange(0, numOfEntriesToScan_local): start = showTimeLeft(ii=i, mode='s', startTime=startTemp) startTemp = start oldTree.GetEntry(i) # selections if lola_on == 0: passB = 0 for k in xrange(oldTree.Jets.size()): if num_of_jets == 1: if not eval(whichJetStr): continue else: if not (k >= 1 and k <= num_of_jets): continue if cut_on == 1: if not eval(condition_str): for stri in attr: strTemp = 'J' + str(k) + '' + stri if stri == 'eta' or stri == 'phi': dfv = -9. else: dfv = -1. setattr(Jets1, strTemp, dfv) continue passB = 1 for stri in attr: strTemp = 'J' + str(k) + '' + stri setattr(Jets1, strTemp, fillingValue) if passB == 1: newTree.Fill() elif lola_on == 1: for ii in xrange(forLola.nConstit): for strO in attr_out: tempAttrStrO = 'pfc' + str(ii + 1) + '_' + strO tempAttrStrI = forLola.attrNameDic[strO] + '_' + str(ii + 1) setattr(Jets1, tempAttrStrO, jet1[tempAttrStrI][i]) #!!!!!!!!!!!!!!!!!!!!deal with the empty value situation!!!!!!!!!!!!!!!!!!!!!! newTree.Fill() showTimeLeft(ii=i, mode='e', startTime=start, numOfJobs=numOfEntriesToScan_local) #set up parallel pool #pool = mp.Pool(processes=80) #pool.map(FillTrees, xrange(0, numOfEntriesToScan_local)) print '\nproduced skimmed file', newFile.GetName( ), '\tevents =', newTree.GetEntries(), '\tweight =', newTree.GetWeight() newFile.cd() newFile.Write() newFile.Close()
def skim(name): print 'filename:', name oldFile = TFile(name, "READ") oldTree = oldFile.Get("reconstruction/tree") # locate and register the Jet branches of the old ttree oldTree.SetBranchAddress("Jet1", AddressOf(Jet1o, 'pt') ); oldTree.SetBranchAddress("Jet2", AddressOf(Jet2o, 'pt') ); oldTree.SetBranchAddress("Jet3", AddressOf(Jet3o, 'pt') ); oldTree.SetBranchAddress("Jet4", AddressOf(Jet4o, 'pt') ); print 'skimming file',oldFile.GetName(),'\tevents =',oldTree.GetEntries(),'\tweight =',oldTree.GetWeight() newFile = TFile("Skim/"+name, "RECREATE") newFile.cd() newTree = TTree("tree44", "tree44") newTree.Branch( 'Jet1s', Jets1, 'pt/F:eta/F:chf/F:nhf/F:phf/F:elf/F:muf/F:chm/I:cm/I:nm/I:dR_q1/F:dR_q2/F:dR_q3/F:dR_q4/F' ) newTree.Branch( 'Jet2s', Jets2, 'pt/F:eta/F:chf/F:nhf/F:phf/F:elf/F:muf/F:chm/I:cm/I:nm/I:dR_q1/F:dR_q2/F:dR_q3/F:dR_q4/F' ) newTree.Branch( 'Jet3s', Jets3, 'pt/F:eta/F:chf/F:nhf/F:phf/F:elf/F:muf/F:chm/I:cm/I:nm/I:dR_q1/F:dR_q2/F:dR_q3/F:dR_q4/F' ) newTree.Branch( 'Jet4s', Jets4, 'pt/F:eta/F:chf/F:nhf/F:phf/F:elf/F:muf/F:chm/I:cm/I:nm/I:dR_q1/F:dR_q2/F:dR_q3/F:dR_q4/F' ) # this attribute list must exactly match (the order of) the features in the header file!!!! #theweight = oldTree.GetWeight() for i in range( 0 , oldTree.GetEntries() ): # why -1? oldTree.GetEntry(i) # selections # Trigger if Jet1o.pt>15 \ and \ (Jet1o.eta>-2.4 or Jet1o.eta<2.4) \ and \ ( \ (Jet1o.dR_q1<0.4 and Jet1o.dR_q2>0.4 and Jet1o.dR_q3>0.4 and Jet1o.dR_q4>0.4) \ or \ (Jet1o.dR_q2<0.4 and Jet1o.dR_q1>0.4 and Jet1o.dR_q3>0.4 and Jet1o.dR_q4>0.4) \ or \ (Jet1o.dR_q3<0.4 and Jet1o.dR_q2>0.4 and Jet1o.dR_q1>0.4 and Jet1o.dR_q4>0.4) \ or \ (Jet1o.dR_q4<0.4 and Jet1o.dR_q2>0.4 and Jet1o.dR_q3>0.4 and Jet1o.dR_q1>0.4) \ ) \ : # set new leaf values to old ones # this attribute list must exactly match (the order of) the features in the header file!!!! Jets1.pt = Jet1o.pt Jets1.eta = Jet1o.eta Jets1.phi = Jet1o.phi Jets1.chf = Jet1o.chf Jets1.nhf = Jet1o.nhf Jets1.phf = Jet1o.phf Jets1.elf = Jet1o.elf Jets1.muf = Jet1o.muf Jets1.chm = Jet1o.chm Jets1.cm = Jet1o.cm Jets1.nm = Jet1o.nm Jets1.dR_q1 = Jet1o.dR_q1 Jets1.dR_q2 = Jet1o.dR_q2 Jets1.dR_q3 = Jet1o.dR_q3 Jets1.dR_q4 = Jet1o.dR_q4 if Jet2o.pt>15 \ and \ (Jet2o.eta>-2.4 or Jet2o.eta<2.4) \ and \ ( \ (Jet2o.dR_q1<0.4 and Jet2o.dR_q2>0.4 and Jet2o.dR_q3>0.4 and Jet2o.dR_q4>0.4) \ or \ (Jet2o.dR_q2<0.4 and Jet2o.dR_q1>0.4 and Jet2o.dR_q3>0.4 and Jet2o.dR_q4>0.4) \ or \ (Jet2o.dR_q3<0.4 and Jet2o.dR_q2>0.4 and Jet2o.dR_q1>0.4 and Jet2o.dR_q4>0.4) \ or \ (Jet2o.dR_q4<0.4 and Jet2o.dR_q2>0.4 and Jet2o.dR_q3>0.4 and Jet2o.dR_q1>0.4) \ ) \ : Jets2.pt = Jet2o.pt Jets2.eta = Jet2o.eta Jets2.phi = Jet2o.phi Jets2.chf = Jet2o.chf Jets2.nhf = Jet2o.nhf Jets2.phf = Jet2o.phf Jets2.elf = Jet2o.elf Jets2.muf = Jet2o.muf Jets2.chm = Jet2o.chm Jets2.cm = Jet2o.cm Jets2.nm = Jet2o.nm Jets2.dR_q1 = Jet2o.dR_q1 Jets2.dR_q2 = Jet2o.dR_q2 Jets2.dR_q3 = Jet2o.dR_q3 Jets2.dR_q4 = Jet2o.dR_q4 if Jet3o.pt>15 \ and \ (Jet3o.eta>-2.4 or Jet3o.eta<2.4) \ and \ ( \ (Jet3o.dR_q1<0.4 and Jet3o.dR_q2>0.4 and Jet3o.dR_q3>0.4 and Jet3o.dR_q4>0.4) \ or \ (Jet3o.dR_q2<0.4 and Jet3o.dR_q1>0.4 and Jet3o.dR_q3>0.4 and Jet3o.dR_q4>0.4) \ or \ (Jet3o.dR_q3<0.4 and Jet3o.dR_q2>0.4 and Jet3o.dR_q1>0.4 and Jet3o.dR_q4>0.4) \ or \ (Jet3o.dR_q4<0.4 and Jet3o.dR_q2>0.4 and Jet3o.dR_q3>0.4 and Jet3o.dR_q1>0.4) \ ) \ : Jets3.pt = Jet3o.pt Jets3.eta = Jet3o.eta Jets3.phi = Jet3o.phi Jets3.chf = Jet3o.chf Jets3.nhf = Jet3o.nhf Jets3.phf = Jet3o.phf Jets3.elf = Jet3o.elf Jets3.muf = Jet3o.muf Jets3.chm = Jet3o.chm Jets3.cm = Jet3o.cm Jets3.nm = Jet3o.nm Jets3.dR_q1 = Jet3o.dR_q1 Jets3.dR_q2 = Jet3o.dR_q2 Jets3.dR_q3 = Jet3o.dR_q3 Jets3.dR_q4 = Jet3o.dR_q4 if Jet4o.pt>15 \ and \ (Jet4o.eta>-2.4 or Jet4o.eta<2.4) \ and \ ( \ (Jet4o.dR_q1<0.4 and Jet4o.dR_q2>0.4 and Jet4o.dR_q3>0.4 and Jet4o.dR_q4>0.4) \ or \ (Jet4o.dR_q2<0.4 and Jet4o.dR_q1>0.4 and Jet4o.dR_q3>0.4 and Jet4o.dR_q4>0.4) \ or \ (Jet4o.dR_q3<0.4 and Jet4o.dR_q2>0.4 and Jet4o.dR_q1>0.4 and Jet4o.dR_q4>0.4) \ or \ (Jet4o.dR_q4<0.4 and Jet4o.dR_q2>0.4 and Jet4o.dR_q3>0.4 and Jet4o.dR_q1>0.4) \ ) \ : Jets4.pt = Jet4o.pt Jets4.eta = Jet4o.eta Jets4.phi = Jet4o.phi Jets4.chf = Jet4o.chf Jets4.nhf = Jet4o.nhf Jets4.phf = Jet4o.phf Jets4.elf = Jet4o.elf Jets4.muf = Jet4o.muf Jets4.chm = Jet4o.chm Jets4.cm = Jet4o.cm Jets4.nm = Jet4o.nm Jets4.dR_q1 = Jet4o.dR_q1 Jets4.dR_q2 = Jet4o.dR_q2 Jets4.dR_q3 = Jet4o.dR_q3 Jets4.dR_q4 = Jet4o.dR_q4 newTree.Fill() print 'produced skimmed file',newFile.GetName(),'\tevents =',newTree.GetEntries(),'\tweight =',newTree.GetWeight() newFile.cd() newFile.Write() newFile.Close() oldFile.Close()
def skim_c(name, newFileName): #-------------------------------- Jet_old_dict = {} for j in range(num_of_jets): #if 'ctauS' in name: Jet_old_dict[j + 1] = JetType() #if 'ctauS' in name: if NJT == 1: Jet_old_dict[j + 1] = JetTypeSgn() #-------------------------------- print 'filename:', name oldFile = TFile(name, "READ") oldTree = oldFile.Get("reconstruction/tree") #locate and register the Jet branches of the old ttree #oldTree.SetBranchAddress("Jet1", AddressOf(Jet1o, 'pt') ); for j in range(num_of_jets): if 'QCD' in name: oldTree.SetBranchAddress('Jet' + str(j + 1), AddressOf(Jet_old_dict[j + 1], 'pt')) elif 'ctauS' in name: oldTree.SetBranchAddress('MatchedJet' + str(j + 1), AddressOf(Jet_old_dict[j + 1], 'pt')) print 'skimming file', oldFile.GetName(), '\tevents =', oldTree.GetEntries( ), '\tweight =', oldTree.GetWeight() newFile = TFile('Skim/' + newFileName, "RECREATE") newFile.cd() newTree = TTree("tree44", "tree44") newTree.Branch( 'Jet1s', Jets1, 'pt/F:eta/F:chf/F:nhf/F:phf/F:elf/F:muf/F:chm/I:cm/I:nm/I:dR_q1/F:dR_q2/F:dR_q3/F:dR_q4/F' ) #newTree.Branch( 'Jet2s', Jets2, 'pt/F:eta/F:chf/F:nhf/F:phf/F:elf/F:muf/F:chm/I:cm/I:nm/I:dR_q1/F:dR_q2/F:dR_q3/F:dR_q4/F' ) # this attribute list must exactly match (the order of) the features in the header file!!!! ti = 80000 #theweight = oldTree.GetWeight() for i in range(0, oldTree.GetEntries()): # why -1? if i == 0: start = timer() elif i % ti == 2: start = timer() oldTree.GetEntry(i) # selections # Trigger """ if Jet1o.pt>15 \ and \ (Jet1o.eta>-2.4 or Jet1o.eta<2.4) \ and \ ( \ (Jet1o.dR_q1<0.4 and Jet1o.dR_q2>0.4 and Jet1o.dR_q3>0.4 and Jet1o.dR_q4>0.4) \ or \ (Jet1o.dR_q2<0.4 and Jet1o.dR_q1>0.4 and Jet1o.dR_q3>0.4 and Jet1o.dR_q4>0.4) \ or \ (Jet1o.dR_q3<0.4 and Jet1o.dR_q2>0.4 and Jet1o.dR_q1>0.4 and Jet1o.dR_q4>0.4) \ or \ (Jet1o.dR_q4<0.4 and Jet1o.dR_q2>0.4 and Jet1o.dR_q3>0.4 and Jet1o.dR_q1>0.4) \ ) \ : # set new leaf values to old ones # this attribute list must exactly match (the order of) the features in the header file!!!! Jets1.pt = Jet1o.pt Jets1.eta = Jet1o.eta Jets1.phi = Jet1o.phi Jets1.chf = Jet1o.chf Jets1.nhf = Jet1o.nhf Jets1.phf = Jet1o.phf Jets1.elf = Jet1o.elf Jets1.muf = Jet1o.muf Jets1.chm = Jet1o.chm Jets1.cm = Jet1o.cm Jets1.nm = Jet1o.nm Jets1.dR_q1 = Jet1o.dR_q1 Jets1.dR_q2 = Jet1o.dR_q2 Jets1.dR_q3 = Jet1o.dR_q3 Jets1.dR_q4 = Jet1o.dR_q4 newTree.Fill() """ for j in range(num_of_jets): if cut_on == 0: condition_str_dict[j + 1] = '1' if eval(condition_str_dict[j + 1]): Jets1.pt = Jet_old_dict[j + 1].pt Jets1.eta = Jet_old_dict[j + 1].eta Jets1.phi = Jet_old_dict[j + 1].phi Jets1.chf = Jet_old_dict[j + 1].chf Jets1.nhf = Jet_old_dict[j + 1].nhf Jets1.phf = Jet_old_dict[j + 1].phf Jets1.elf = Jet_old_dict[j + 1].elf Jets1.muf = Jet_old_dict[j + 1].muf Jets1.chm = Jet_old_dict[j + 1].chm Jets1.cm = Jet_old_dict[j + 1].cm Jets1.nm = Jet_old_dict[j + 1].nm Jets1.dR_q1 = Jet_old_dict[j + 1].dR_q1 Jets1.dR_q2 = Jet_old_dict[j + 1].dR_q2 Jets1.dR_q3 = Jet_old_dict[j + 1].dR_q3 Jets1.dR_q4 = Jet_old_dict[j + 1].dR_q4 newTree.Fill() ######################################################### if i % 2 == 0: ss = '.' elif i % 2 == 1: ss = 'o' if i % ti == 1 and i > ti: end = timer() dt = end - start tl = int(((oldTree.GetEntries() - i) / ti) * dt) if tl > 60: sys.stdout.write("\r" + 'time left: ' + str(tl / 60) + 'min' + ss) sys.stdout.flush() else: sys.stdout.write("\r" + 'time left: ' + str(tl / 60) + 's') sys.stdout.flush() ######################################################### print 'produced skimmed file', newFile.GetName( ), '\tevents =', newTree.GetEntries(), '\tweight =', newTree.GetWeight() newFile.cd() newFile.Write() newFile.Close() oldFile.Close()