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) #newTree.Branch( JetName, Jets1, forBDT.branchLeafStr+':J1isCaloTag/F' ) 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!!!! if not lola_on: attr = forBDT.preList + forBDT.attrTypeList else: attr_out = forLola.attrTypeList startTemp = 0 #theweight = oldTree.GetWeight() #def FillTrees(i): if lola_on == 0: 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 >= 0 and k < num_of_jets): continue if cut_on == 1: if not eval(condition_str): continue """ for stri in attr: strTemp = 'J'+str(k+1)+''+stri #strTemp = 'J1'+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+1)+''+stri strTemp = 'J1' + stri if stri != 'met': fillingValue = getattr(oldTree.Jets[k], stri) setattr(Jets1, strTemp, fillingValue) else: fillingValue = oldTree.GetBranch('MEt').GetLeaf( 'pt').GetValue() Jets1.J1met = fillingValue if passB == 1: newTree.Fill() showTimeLeft(ii=i, mode='e', startTime=start, numOfJobs=numOfEntriesToScan_local) elif lola_on == 1: loop_depth = pan.shape[0] print '>>>>>>>>>>>>>>>>>>>>> # out events: ', loop_depth for i in xrange(0, loop_depth): start = showTimeLeft(ii=i, mode='s', startTime=startTemp) startTemp = start 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, pan[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_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()
weight_SF_ID_down[0]) / weight_SF_ID[0], "%" print "total weight ", weight_total[0], " +", 100 * ( weight_total_up[0] - weight_total[0]) / weight_total[0], "% / -", 100 * ( weight_total[0] - weight_total_down[0]) / weight_total[0], "%" #if evt == 1000 and options.debug: # break if options.isGen and "Tau" not in samplekey and "OniaAndX" not in samplekey: genParticle_pt[0] = intree.genParticle_pt[0] genParticle_eta[0] = intree.genParticle_eta[0] genParticle_phi[0] = intree.genParticle_phi[0] genParticle_pdgId[0] = intree.genParticle_pdgId[0] genParticle_status[0] = intree.genParticle_status[0] JpsiGen[0] = intree.JpsiGen genBc_pt[0] = intree.genBc_pt if "OniaAndX" in samplekey and options.isGen: isBplusJpsiKplus[0] = intree.isBplusJpsiKplus isBplusJpsiPiplus[0] = intree.isBplusJpsiPiplus isBplusJpsiKPiPiplus[0] = intree.isBplusJpsiKPiPiplus isBplusJpsi3Kplus[0] = intree.isBplusJpsi3Kplus isBplusJpsiPhiKplus[0] = intree.isBplusJpsiPhiKplus isBplusJpsiK0Piplus[0] = intree.isBplusJpsiK0Piplus evtid += 1 otree.Fill() otree.Write() outputfile.Close()
sumL7 += weightL7[0] sumL10 += weightL10[0] sumL12p5 += weightL12p5[0] sumL15 += weightL15[0] sumL20 += weightL20[0] sumLm1 += weightLm1[0] sumLm2p4 += weightLm2p4[0] sumLm3 += weightLm3[0] sumLm4 += weightLm4[0] sumLm5 += weightLm5[0] sumLm7 += weightLm7[0] sumLm10 += weightLm10[0] sumLm12p5 += weightLm12p5[0] sumLm15 += weightLm15[0] sumLm20 += weightLm20[0] treeout.Fill() # make histogram to test histmhhReA.Fill(Genmhh[0], weightSMA[0]) histmhhRe.Fill(Genmhh[0], weightSM[0]) histmhhRecoReA.Fill(Genmhh[0], weightSMA[0]) histmhhRecoRe.Fill(Genmhh[0], weightSM[0]) histmhhBoxA.Fill(Genmhh[0], weightL0[0]) histmhhBench1.Fill(Genmhh[0], weight1[0]) histmhhBench2.Fill(Genmhh[0], weight2[0]) histmhhBench6.Fill(Genmhh[0], weight6[0]) histmhhL15.Fill(Genmhh[0], weightL15[0]) histmhhL2p4.Fill(Genmhh[0], weightL2p4[0]) if ifile == 0:
# Read the header, this is either # EHDR -> finish event # C00x -> read the data # "" -> end of file header = f.read(4) # Handle next board if header.startswith(b"B#"): current_board = current_board + 1 tcell = unpack(b'H', f.read(4)[2:])[0] continue # End of Event elif header == b"EHDR": # Fill previous event outtree.Fill() is_new_event = True # Read and store data elif header.startswith(b"C"): # the voltage info is 1024 floats with 2-byte precision chn_i = int(header.decode('ascii')[-1]) + current_board * 4 scaler = unpack('I', f.read(4)) voltage_ints = unpack(b'H' * 1024, f.read(2 * 1024)) """ Calculate precise timing using the time bins and trigger cell see p. 24 of the DRS4 manual for the explanation the following lines sum up the times of all cells starting from the trigger cell to the i_th cell and select only even members, because the amplitude of the adjacent cells are averaged. The width of the bins 1024-2047 is identical to the bins 0-1023, that is why the arrays are simply extended before performing the cumsum operation
DigitizerPulseAmplitude[ch+4][nn] = thisDigi2[ch][DigitizerPulseAmplitudeTimeBin[ch+4][nn]] DigitizerNumberOfPulses[ch+4] = nPulses #save waveforms if needed if SaveWaveforms == 1: waveformname = waveformname_head + str(int(evtkey)) + "_trigFlag_" + str(TrigFlag_Multi) + str(TrigFlag_Hodo) + str(TrigFlag_Led) + "_ch_" + str(ch+4) + "_nPulses_" + str(nPulses) waveformtitle = waveformname + "_mean_" + str(digi2_ped_mean[ch]) + "_sigma_" + str(sigma2) for nn in range(nPulses): waveformtitle = waveformtitle + "_" + str(peakxL[nn]) + "_" + str(peakxR[nn]) hist = TH1F(waveformname,waveformtitle,2560,-0.5,2560-0.5) for bin in range(0,2560,1): hist.SetBinContent(bin+1,thisDigi2[ch][bin]) waveformsDir.cd() hist.Write() del hist #done mytree.Fill() #if int(evtkey) >100: break # average waveforms if SaveWaveforms == 0 or SaveWaveforms == 1: waveformsAvgDir.cd() for channel in range(0,8): for trigFlag in range(0,3): #print "AvgWaveforms: ", channelname[channel], trigFlagname[trigFlag], totalwave_cnts[channel*3+trigFlag] name = hWaveforms_avg[channel*3+trigFlag].GetName() name = name + "_NbOfTriggers_" + str(totalwave_cnts[channel*3+trigFlag]) hWaveforms_avg[channel*3+trigFlag].SetTitle(name) if totalwave_cnts[channel*3+trigFlag] == 0: print "caution 0 trigger: ch ", channelname[channel], "trigType ", trigFlagname[trigFlag] continue for bin in range(0,2560): hWaveforms_avg[channel*3+trigFlag].SetBinContent(bin+1,totalwave[channel*3+trigFlag][bin]/totalwave_cnts[channel*3+trigFlag])
def create_weights(self, discrList, filename): self._logger.info('Getting weights...') from TuningTools import TuningToolCores modelDict = {} modelDict['__version__'] = self._version modelDict['__type__'] = 'Fex' modelDict['__core__'] = TuningToolCores.FastNet modelDict['metadata'] = { 'UseEtaVar': self._useEtaVar, 'UseLumiVar': self._useLumiVar, 'UseCaloRings': True, 'UseTrack': False, 'UseShowerShape': False, 'RemoveOutputTansigTF': self._removeOutputTansigTF, } modelDict['tuning'] = [] def tolist(a): if isinstance(a, list): return a elif isinstance(a, tuple): return a else: return a.tolist() from keras.models import model_from_json from keras.layers import Conv2D, Dense, Activation import json from copy import deepcopy for model in discrList: etBinIdx = model['etBinIdx'] etaBinIdx = model['etaBinIdx'] ## Discriminator configuration discrData = {} discrData['etBin'] = tolist(model['etBin']) discrData['etaBin'] = tolist(model['etaBin']) discrData['muBin'] = tolist(model['muBin']) if self._toPickle: discrData['model'] = deepcopy(model['discriminator']['model']) discrData['weights'] = deepcopy( model['discriminator']['weights']) else: keras_model = model_from_json( json.dumps(model['discriminator']['model'], separators=(',', ':'))) keras_model.set_weights(model['discriminator']['weights']) ### Extract and reshape Keras weights dense_weights = [] dense_bias = [] dense_nodes = [] dense_tfnames = [] conv_kernel = [] conv_kernel_i = [] conv_kernel_j = [] conv_bias = [] conv_tfnames = [] conv_nodes = [] useConvLayer = False ### Loop over layers for idx, obj in enumerate(keras_model.layers): dobj = model['discriminator']['model']['config'][idx][ 'config'] if type(obj) is Conv2D: useConvLayer = True conv_nodes.append(dobj['filters']) conv_tfnames.append(str(dobj['activation'])) w, b = obj.get_weights() for wn in w.T: for wu in wn: conv_kernel.extend( wu.T.reshape( (dobj['kernel_size'][0] * dobj['kernel_size'][1], )).tolist()) conv_bias.extend(b.tolist()) conv_kernel_i.append(dobj['kernel_size'][0]) conv_kernel_j.append(dobj['kernel_size'][1]) elif type(obj) is Dense: dense_nodes.append(dobj['units']) dense_tfnames.append(str(dobj['activation'])) w, b = obj.get_weights() dense_weights.extend(w.reshape(-1, order='F')) dense_bias.extend(b.reshape(-1, order='F')) # TODO: Need to implement something smart to tread this case elif type(obj) is Activation: dense_tfnames.pop() dense_tfnames.append(str(dobj['activation'])) else: continue discrData['dense_nodes'] = tolist(dense_nodes) discrData['dense_bias'] = tolist(dense_bias) discrData['dense_weights'] = tolist(dense_weights) discrData['dense_tfnames'] = tolist(dense_tfnames) discrData['useConvLayer'] = [useConvLayer] # Convolutional neural network if useConvLayer: discrData['conv_nodes'] = tolist(conv_nodes) discrData['conv_tfnames'] = tolist(conv_tfnames) discrData['conv_kernel_i'] = tolist(conv_kernel_i) discrData['conv_kernel_j'] = tolist(conv_kernel_j) discrData['conv_kernel'] = tolist(conv_kernel) discrData['conv_bias'] = tolist(conv_bias) discrData['conv_input_i'] = [ model['discriminator']['model']['config'][0]['config'] ['batch_input_shape'][1] ] discrData['conv_input_j'] = [ model['discriminator']['model']['config'][0]['config'] ['batch_input_shape'][2] ] i = discrData['conv_input_i'][0] - (sum(conv_kernel_i) - len(conv_kernel_i)) j = discrData['conv_input_j'][0] - (sum(conv_kernel_j) - len(conv_kernel_j)) input_layer = i * j * discrData['conv_nodes'][-1] discrData['dense_nodes'] = [input_layer ] + discrData['dense_nodes'] ### Attach modelDict['tuning'].append({'discriminator': discrData}) if self._toPickle: self._logger.info('Export weights to pickle format...') modelDict['__version__'] = self._version modelDict['__core__'] = TuningToolCores.keras from RingerCore import save save(modelDict, filename) from ROOT import TFile, TTree from ROOT import std self._logger.info('Export weights to root format...') ### Create the discriminator root object fdiscr = TFile(appendToFileName(filename, '.root', separator=''), 'recreate') self.__createRootParameter('int', '__version__', self._version).Write() self.__createRootParameter('int', '__core__', TuningToolCores.FastNet).Write() fdiscr.mkdir('tuning') fdiscr.cd('tuning') tdiscr = TTree('discriminators', '') for idx, b in enumerate(self._discrBranches): b[2] = std.vector(b[0])() tdiscr.Branch(b[1], 'vector<%s>' % b[0], b[2]) for t in modelDict['tuning']: for idx, b in enumerate(self._discrBranches): self.__attachToVector(t['discriminator'][b[1]], b[2]) tdiscr.Fill() tdiscr.Write() ### Create the thresholds root object fdiscr.mkdir('metadata') fdiscr.cd('metadata') for key, value in modelDict['metadata'].iteritems(): self._logger.info('Saving metadata %s as %s', key, value) self.__createRootParameter('int' if type(value) is int else 'bool', key, value).Write() fdiscr.Close()
class TrigNtupleHandler: def __init__(self): self.fileName = 'lumi.root' self.treeName = 'lumiData' self.file = None self.tree = None self.updatemode = False # Flag showing whether BCID data is initialized self.bcidData = False # Flag showing whether L1 trigger counts are initialized self.l1TrigData = True def open(self, update=True): print 'NtupleHandler.open() called' if os.path.exists(self.fileName) and update: print 'Opening %s for updating' % self.fileName self.updatemode = True self.file = TFile(self.fileName, 'update') self.tree = self.file.Get(self.treeName) else: print 'Creating %s for writing' % self.fileName self.updatemode = False self.file = TFile(self.fileName, 'create') self.tree = TTree(self.treeName, self.treeName) def close(self): print 'ScanNtupleHandler.close() called' self.tree.Write('', TObject.kOverwrite) self.file.Close() def init(self): print 'ScanNtupleHandler.init() called' self.initLBData() self.initBCIDData() def save(self): self.tree.Fill() def readLastEntry(self): entries = self.tree.GetEntries() self.tree.GetEntry(entries - 1) # Fill information from LumiLBData object def fillLumi(self, lumi): # Erase any old data self.clear() # Time in COOL format (ns) self.lbData.coolStartTime = lumi.startTime.timerunlb() self.lbData.coolEndTime = lumi.endTime.timerunlb() # Time in seconds self.lbData.startTime = lumi.startTime.timerunlb() / 1.E9 self.lbData.endTime = lumi.endTime.timerunlb() / 1.E9 # LB duration in seconds self.lbData.lbTime = (lumi.endTime.timerunlb() - lumi.startTime.timerunlb()) / 1.E9 self.lbData.run = lumi.runLB.run self.lbData.lb = lumi.runLB.lb # Need to fill these elsewhere # self.lbData.fill = 0 # self.lbData.eBeam = 0. # self.lbData.stable = False # self.lbData.ready = False # self.lbData.physics = False # if lumi.onlEvtsPerBX > 0.: # self.lbData.muToLumi = lumi.onlInstLumi/lumi.onlEvtsPerBX self.lbData.onlInstLum = lumi.onlInstLumi self.lbData.onlInstLumAll = lumi.onlInstLumiAll self.lbData.onlEvtsPerBX = lumi.onlEvtsPerBX self.lbData.onlPrefChan = lumi.onlPrefChan self.lbData.onlValid = lumi.onlValid self.lbData.olcValid = lumi.olcValid self.lbData.nColl = lumi.bcid.nbcol() self.lbData.nBeam1 = lumi.bcid.nb1() self.lbData.nBeam2 = lumi.bcid.nb2() self.lbData.qBeam1Col = lumi.IBeam1 self.lbData.qBeam2Col = lumi.IBeam2 self.lbData.qBeam1All = lumi.IBeam1All self.lbData.qBeam2All = lumi.IBeam2All self.lbData.specLumi = lumi.specLumi self.lbData.geomLumi = lumi.geomLumi self.lbData.maxEvtsPerBX = lumi.maxEvtsPerBX self.lbData.l1LiveFrac = lumi.l1Livefrac # Get this from the veto folders # self.lbData.avgLiveFrac = -1. # self.lbData.lumiWtLiveFrac = -1. self.lbData.matched = lumi.matched if not self.bcidData: return # And fill the per-BCID values for (bcid, caliLumi) in lumi.bcid.caliLumi.iteritems(): self.lumiDel[int(bcid)] = caliLumi for (bcid, q) in lumi.bcid.b1Curr.iteritems(): self.qBeam1[int(bcid)] = q for (bcid, q) in lumi.bcid.b2Curr.iteritems(): self.qBeam2[int(bcid)] = q i = 0 for bcid in sorted(list(lumi.bcid.b1BCID)): self.b1BCID[i] = bcid i += 1 i = 0 for bcid in sorted(list(lumi.bcid.b2BCID)): self.b2BCID[i] = bcid i += 1 i = 0 for bcid in sorted(list(lumi.bcid.colBCID)): self.colBCID[i] = bcid i += 1 # Still need live fraction # Pass TriggerL1Data object for lumi block filled by TriggerHandler # Also need mapping of channel names to channel values def fillL1Trig(self, trigData, trigChan): for (trig, chan) in trigChan.iteritems(): self.l1TBP[chan] = trigData.TBP[trig] self.l1TAP[chan] = trigData.TAP[trig] self.l1TAV[chan] = trigData.TAV[trig] def defineBranch(self, name, type): self.tree.Branch(name, AddressOf(self.lbData, name), name + '/' + type) def loadBranch(self, name): branch = self.tree.GetBranch(name) branch.SetAddress(AddressOf(self.lbData, name)) def initLBData(self): # Define the main lumiblock data # Time is in ns # ULong64_t startTime;\ # ULong64_t endTime;\ LBDataStructStr = "struct LBDataStruct {\ ULong64_t coolStartTime;\ ULong64_t coolEndTime;\ Double_t startTime;\ Double_t endTime;\ Float_t lbTime;\ UInt_t fill;\ UInt_t run;\ UInt_t lb;\ Float_t eBeam;\ Bool_t stable;\ Bool_t ready;\ Bool_t physics;\ Bool_t larVeto;\ \ UInt_t onlValid;\ UInt_t olcValid;\ UInt_t onlPrefChan;\ Float_t muToLumi;\ Float_t onlInstLum;\ Float_t onlInstLumAll;\ Float_t onlEvtsPerBX;\ \ UInt_t nColl;\ UInt_t nBeam1;\ UInt_t nBeam2;\ Float_t qBeam1Col;\ Float_t qBeam2Col;\ Float_t qBeam1All;\ Float_t qBeam2All;\ \ Float_t specLumi;\ Float_t geomLumi;\ Float_t maxEvtsPerBX;\ \ Float_t l1LiveFrac;\ Float_t avgLiveFrac;\ Float_t lumiWtLiveFrac;\ \ UInt_t matched;\ };" # Replace sizes if needed gROOT.ProcessLine(LBDataStructStr) from ROOT import LBDataStruct self.lbData = LBDataStruct() self.varList = [] self.varList.append(('startTime', 'D')) self.varList.append(('endTime', 'D')) self.varList.append(('coolStartTime', 'l')) self.varList.append(('coolEndTime', 'l')) self.varList.append(('lbTime', 'F')) self.varList.append(('fill', 'i')) self.varList.append(('run', 'i')) self.varList.append(('lb', 'i')) self.varList.append(('eBeam', 'F')) # Boolean status flags self.varList.append(('stable', 'O')) self.varList.append(('ready', 'O')) self.varList.append(('physics', 'O')) self.varList.append(('larVeto', 'O')) # Luminosity information self.varList.append(('onlPrefChan', 'i')) self.varList.append(('muToLumi', 'F')) self.varList.append(('onlInstLum', 'F')) self.varList.append(('onlInstLumAll', 'F')) self.varList.append(('onlEvtsPerBX', 'F')) self.varList.append(('onlValid', 'i')) # From LBLESTONL & 0x3FF self.varList.append(('olcValid', 'i')) # From LUMINOSITY # Total bunch information self.varList.append(('nColl', 'i')) self.varList.append(('nBeam1', 'i')) self.varList.append(('nBeam2', 'i')) self.varList.append(('qBeam1Col', 'F')) # Total charge colliding self.varList.append(('qBeam2Col', 'F')) self.varList.append(('qBeam1All', 'F')) # Total charge in all BCIDs self.varList.append(('qBeam2All', 'F')) self.varList.append(('specLumi', 'F')) self.varList.append(('geomLumi', 'F')) self.varList.append(('maxEvtsPerBX', 'F')) # Livetime information self.varList.append(('l1LiveFrac', 'F')) self.varList.append(('avgLiveFrac', 'F')) self.varList.append( ('lumiWtLiveFrac', 'F')) # lumi-weighted per-BCID livefraction # Where the lumi information came from self.varList.append(('matched', 'i')) for (var, type) in self.varList: if self.updatemode: self.loadBranch(var) else: self.defineBranch(var, type) def initBCIDData(self): self.bcidData = True # Delivered luminosity self.lumiDel = array.array('f', (0., ) * 3564) self.qBeam1 = array.array('f', (0., ) * 3564) # Charge per BCID self.qBeam2 = array.array('f', (0., ) * 3564) self.liveFrac = array.array('f', (0., ) * 3564) # Deadtime if self.updatemode: self.tree.GetBranch('lumiDel').SetAddress(self.lumiDel) self.tree.GetBranch('qBeam1').SetAddress(self.qBeam1) self.tree.GetBranch('qBeam2').SetAddress(self.qBeam2) self.tree.GetBranch('liveFrac').SetAddress(self.liveFrac) else: self.tree.Branch('lumiDel', self.lumiDel, 'lumiDel[3564]/F') self.tree.Branch('qBeam1', self.qBeam1, 'qBeam1[3564]/F') self.tree.Branch('qBeam2', self.qBeam2, 'qBeam2[3564]/F') self.tree.Branch( 'liveFrac', self.liveFrac, 'liveFrac[3564]/F') # Per-BCID livetime from lumi counters # BCID arrays (unsigned shorts) self.b1BCID = array.array('H', (0, ) * 3564) self.b2BCID = array.array('H', (0, ) * 3564) self.colBCID = array.array('H', (0, ) * 3564) if self.updatemode: self.tree.GetBranch('b1BCID').SetAddress(self.b1BCID) self.tree.GetBranch('b2BCID').SetAddress(self.b2BCID) self.tree.GetBranch('colBCID').SetAddress(self.colBCID) else: self.tree.Branch('b1BCID', self.b1BCID, 'b1BCID[nBeam1]/s') # Unsigned short self.tree.Branch('b2BCID', self.b2BCID, 'b2BCID[nBeam2]/s') # Unsigned short self.tree.Branch('colBCID', self.colBCID, 'colBCID[nColl]/s') # Unsigned short def initL1TrigData(self): self.l1TrigData = True # Counts by channel ID self.l1TBP = array.array('I', (0, ) * 256) self.l1TAP = array.array('I', (0, ) * 256) self.l1TAV = array.array('I', (0, ) * 256) if self.updatemode: self.tree.GetBranch('l1TBP').SetAddress(self.l1TBP) self.tree.GetBranch('l1TAP').SetAddress(self.l1TAP) self.tree.GetBranch('l1TAV').SetAddress(self.l1TAV) else: self.tree.Branch('l1TBP', self.l1TBP, 'l1TBP[256]/i') self.tree.Branch('l1TAP', self.l1TAP, 'l1TAP[256]/i') self.tree.Branch('l1TAV', self.l1TAV, 'l1TAV[256]/i') # Set all ntuple variables to default values def clear(self): self.lbData.startTime = 0 self.lbData.endTime = 0 self.lbData.lbTime = 0. self.lbData.fill = 0 self.lbData.run = 0 self.lbData.lb = 0 self.lbData.eBeam = 0. self.lbData.stable = False self.lbData.ready = False self.lbData.physics = False self.lbData.larVeto = False self.lbData.onlPrefChan = 0 self.lbData.muToLumi = 0. self.lbData.onlInstLum = -1. self.lbData.onlInstLumAll = -1. self.lbData.onlEvtsPerBX = -1. self.lbData.onlValid = 0xFFFFFF self.lbData.olcValid = 0xFFFFFF self.lbData.nColl = 0 self.lbData.nBeam1 = 0 self.lbData.nBeam2 = 0 self.lbData.qBeam1Col = -1. self.lbData.qBeam2Col = -1. self.lbData.qBeam1All = -1. self.lbData.qBeam2All = -1. self.lbData.specLumi = -1. self.lbData.geomLumi = -1. self.lbData.maxEvtsPerBX = -1. self.lbData.l1LiveFrac = -1. self.lbData.avgLiveFrac = -1. self.lbData.lumiWtLiveFrac = -1. self.lbData.matched = 0 if self.bcidData: # Per-BCID arrays for i in range(3564): self.lumiDel[i] = 0. self.qBeam1[i] = 0. self.qBeam2[i] = 0. self.liveFrac[i] = 0. self.b1BCID[i] = 0 self.b2BCID[i] = 0 self.colBCID[i] = 0 if self.l1TrigData: # L1 trigger counts for i in range(256): self.l1TBP[i] = 0 self.l1TAP[i] = 0 self.l1TAV[i] = 0
tob_event = event_from_tob(my_tree, tob) run3_et[0] = tob_event.reco_et else: run3_et[0] = -1 po_3x3_cells_to_array(l0_cells, [-1] * 9) po_12x3_cells_to_array(l1_cells, [-1] * 36) po_12x3_cells_to_array(l2_cells, [-1] * 36) po_3x3_cells_to_array(l3_cells, [-1] * 9) po_3x3_cells_to_array(had_cells, [-1] * 9) true_pt[0] = truePts[tob_num] / 1000. true_eta[0] = trueEta[tob_num] reco_pt[0] = recoPts[tob_num] / 1000. if recoPts != -1 else -1 reco_eta[0] = recoEta[tob_num] t_out.Fill() if t_out.GetEntries() % 1000 == 0: print 'Entries filled: ', t_out.GetEntries() continue # For background, fill only highest-Et in event and no eta cut because we care about overall rate else: tob_event = event_from_tob(my_tree, tob) # Only consider those that pass Run3 seed cut if not isCentralTowerSeed(tob_event): continue if tob_event.reco_et > event_max_et:
def make_ew_trees(inlist, outfile): #input tree = TChain("DetectorTree") for i in inlist: tree.Add(i) #output out = TFile(outfile, "recreate") gROOT.ProcessLine("struct EntryF {Float_t v;};") gROOT.ProcessLine("struct EntryB {Bool_t v;};") #hits by primary photons x = rt.EntryF() y = rt.EntryF() z = rt.EntryF() en = rt.EntryF() otree = TTree("prim_tree", "prim_tree") otree.Branch("x", addressof(x, "v"), "x/F") otree.Branch("y", addressof(y, "v"), "y/F") otree.Branch("z", addressof(z, "v"), "z/F") otree.Branch("en", addressof(en, "v"), "en/F") #conversions gen_en = rt.EntryF() conv = rt.EntryB() clean = rt.EntryB() adep = rt.EntryF() conv_tree = TTree("conv_tree", "conv_tree") conv_tree.Branch("gen_en", addressof(gen_en, "v"), "gen_en/F") conv_tree.Branch("conv", addressof(conv, "v"), "conv/O") conv_tree.Branch("clean", addressof(clean, "v"), "clean/O") conv_tree.Branch("adep", addressof(adep, "v"), "adep/F") #input hits hits = ew_hits("ew", tree) #input generated particles inp_pdg = std.vector(int)() inp_en = std.vector(float)() tree.SetBranchAddress("gen_pdg", inp_pdg) tree.SetBranchAddress("gen_en", inp_en) #number of events with primary photon nprim = 0 #event loop nev = tree.GetEntries() for ievt in range(nev): #for ievt in range(12): tree.GetEntry(ievt) #print("Next event") #generated photon energy gen_en.v = -1. for imc in range(inp_pdg.size()): if inp_pdg.at(imc) == 22: gen_en.v = inp_en.at(imc) #primary photon was_prim = False #conversion in the event conv.v = False #all secondaries and deposited energy in event asec = 0 adep.v = 0. #hit loop for ihit in range(hits.get_n()): hit = hits.get_hit(ihit) hit.global_to_zpos(-18500) # mm #hit by primary photon if hit.prim != 0 and hit.pdg == 22 and was_prim != True: was_prim = True x.v = hit.x y.v = hit.y z.v = hit.z en.v = hit.en otree.Fill() #conversion in the event if hit.prim == 1 and hit.conv == 1: conv.v = True #all secondaries and deposited energy asec += hit.nsec adep.v += hit.edep #hit loop #increment the photon count if was_prim is True: nprim += 1 #evaluate clean conversion clean.v = False if conv.v and asec == 2: clean.v = True conv_tree.Fill() #event loop print("All events: ", nev) print("Primary photons:", nprim) otree.Write() conv_tree.Write() out.Close()
def w_mass_hadronic(tree, ecm, m_w, histdict, reconstruction, reco_level, reco_type=''): '''Run over the hadronic events and save the histograms''' f1 = TFile('signal_and_bkg_cut_240GeV.root', "update") treecut = TTree(treename, "tree title") y34 = array('f', [0]) reco_jet1_e = array('f', [0]) reco_jet2_e = array('f', [0]) reco_jet3_e = array('f', [0]) reco_jet4_e = array('f', [0]) reco_jet1_px = array('f', [0]) reco_jet2_px = array('f', [0]) reco_jet3_px = array('f', [0]) reco_jet4_px = array('f', [0]) reco_jet1_py = array('f', [0]) reco_jet2_py = array('f', [0]) reco_jet3_py = array('f', [0]) reco_jet4_py = array('f', [0]) reco_jet1_pz = array('f', [0]) reco_jet2_pz = array('f', [0]) reco_jet3_pz = array('f', [0]) reco_jet4_pz = array('f', [0]) reco_jet_size = array('f', [0]) file_type = array('f', [0]) quark_size = array('f', [0]) quark1_e = array('f', [0]) quark2_e = array('f', [0]) quark3_e = array('f', [0]) quark4_e = array('f', [0]) quark1_px = array('f', [0]) quark2_px = array('f', [0]) quark3_px = array('f', [0]) quark4_px = array('f', [0]) quark1_py = array('f', [0]) quark2_py = array('f', [0]) quark3_py = array('f', [0]) quark4_py = array('f', [0]) quark1_pz = array('f', [0]) quark2_pz = array('f', [0]) quark3_pz = array('f', [0]) quark4_pz = array('f', [0]) gen_jet_size = array('f', [0]) gen_jet1_e = array('f', [0]) gen_jet2_e = array('f', [0]) gen_jet3_e = array('f', [0]) gen_jet4_e = array('f', [0]) gen_jet1_px = array('f', [0]) gen_jet2_px = array('f', [0]) gen_jet3_px = array('f', [0]) gen_jet4_px = array('f', [0]) gen_jet1_py = array('f', [0]) gen_jet2_py = array('f', [0]) gen_jet3_py = array('f', [0]) gen_jet4_py = array('f', [0]) gen_jet1_pz = array('f', [0]) gen_jet2_pz = array('f', [0]) gen_jet3_pz = array('f', [0]) gen_jet4_pz = array('f', [0]) treecut.Branch("y34", y34, 'y34/F') treecut.Branch("reco_jet1_e", reco_jet1_e, 'reco_jet1_e/F') treecut.Branch("reco_jet2_e", reco_jet2_e, 'reco_jet2_e/F') treecut.Branch("reco_jet3_e", reco_jet3_e, 'reco_jet3_e/F') treecut.Branch("reco_jet4_e", reco_jet4_e, 'reco_jet4_e/F') treecut.Branch("reco_jet1_px", reco_jet1_px, 'reco_jet1_px/F') treecut.Branch("reco_jet2_px", reco_jet2_px, 'reco_jet2_px/F') treecut.Branch("reco_jet3_px", reco_jet3_px, 'reco_jet3_px/F') treecut.Branch("reco_jet4_px", reco_jet4_px, 'reco_jet4_px/F') treecut.Branch("reco_jet1_py", reco_jet1_py, 'reco_jet1_py/F') treecut.Branch("reco_jet2_py", reco_jet2_py, 'reco_jet2_py/F') treecut.Branch("reco_jet3_py", reco_jet3_py, 'reco_jet3_py/F') treecut.Branch("reco_jet4_py", reco_jet4_py, 'reco_jet4_py/F') treecut.Branch("reco_jet1_pz", reco_jet1_pz, 'reco_jet1_pz/F') treecut.Branch("reco_jet2_pz", reco_jet2_pz, 'reco_jet2_pz/F') treecut.Branch("reco_jet3_pz", reco_jet3_pz, 'reco_jet3_pz/F') treecut.Branch("reco_jet4_pz", reco_jet4_pz, 'reco_jet4_pz/F') treecut.Branch("reco_jet_size", reco_jet_size, 'reco_jet_size/F') treecut.Branch("file_type", file_type, 'file_type/F') treecut.Branch("quark_size", quark_size, 'quark_size/F') treecut.Branch("quark1_e", quark1_e, 'quark1_e/F') treecut.Branch("quark2_e", quark2_e, 'quark2_e/F') treecut.Branch("quark3_e", quark3_e, 'quark3_e/F') treecut.Branch("quark4_e", quark4_e, 'quark4_e/F') treecut.Branch("quark1_px", quark1_px, 'quark1_px/F') treecut.Branch("quark2_px", quark2_px, 'quark2_px/F') treecut.Branch("quark3_px", quark3_px, 'quark3_px/F') treecut.Branch("quark4_px", quark4_px, 'quark4_px/F') treecut.Branch("quark1_py", quark1_py, 'quark1_py/F') treecut.Branch("quark2_py", quark2_py, 'quark2_py/F') treecut.Branch("quark3_py", quark3_py, 'quark3_py/F') treecut.Branch("quark4_py", quark4_py, 'quark4_py/F') treecut.Branch("quark1_pz", quark1_pz, 'quark1_pz/F') treecut.Branch("quark2_pz", quark2_pz, 'quark2_pz/F') treecut.Branch("quark3_pz", quark3_pz, 'quark3_pz/F') treecut.Branch("quark4_pz", quark4_pz, 'quark4_pz/F') treecut.Branch("gen_jet_size", gen_jet_size, 'gen_jet_size/F') treecut.Branch("gen_jet1_e", gen_jet1_e, 'gen_jet1_e/F') treecut.Branch("gen_jet2_e", gen_jet2_e, 'gen_jet2_e/F') treecut.Branch("gen_jet3_e", gen_jet3_e, 'gen_jet3_e/F') treecut.Branch("gen_jet4_e", gen_jet4_e, 'gen_jet4_e/F') treecut.Branch("gen_jet1_px", gen_jet1_px, 'gen_jet1_px/F') treecut.Branch("gen_jet2_px", gen_jet2_px, 'gen_jet2_px/F') treecut.Branch("gen_jet3_px", gen_jet3_px, 'gen_jet3_px/F') treecut.Branch("gen_jet4_px", gen_jet4_px, 'gen_jet4_px/F') treecut.Branch("gen_jet1_py", gen_jet1_py, 'gen_jet1_py/F') treecut.Branch("gen_jet2_py", gen_jet2_py, 'gen_jet2_py/F') treecut.Branch("gen_jet3_py", gen_jet3_py, 'gen_jet3_py/F') treecut.Branch("gen_jet4_py", gen_jet4_py, 'gen_jet4_py/F') treecut.Branch("gen_jet1_pz", gen_jet1_pz, 'gen_jet1_pz/F') treecut.Branch("gen_jet2_pz", gen_jet2_pz, 'gen_jet2_pz/F') treecut.Branch("gen_jet3_pz", gen_jet3_pz, 'gen_jet3_pz/F') treecut.Branch("gen_jet4_pz", gen_jet4_pz, 'gen_jet4_pz/F') from_tree = TreeInfo() sign = {} #eeqq bkg1 = {} #ZZqqll bkg2 = {} #ZZqqqq bkg3 = {} #WWqqll bkg4 = {} purete = [] efficacite = [] x = [] #cut y34 # for i in range(0,101): # j=i*0.001 # x.append(j) # print x # for index, value in enumerate(x): # print value #cut ejetcut # for i in range(0,2): # j=i*0.001 # x.append(j) # print x # print len(x) #boucle sur les valeurs de cut for iev, evt in enumerate(tree): if iev % 5000 == 0: print("Processing events {} on {} ".format(iev, tree.GetEntries())) # Object from_tree to access the rootfile information from_tree.set_evt(evt, reco_level, reco_type) # Discard event where one jet is containing only photons and/or no clustered particles if from_tree.get_dim("jet") != 4: continue if not check_content(from_tree): continue #cut masses "similaires" des 2 W #if abs(m_large-m_small)<13: # continue # cut y34 if from_tree.get_y34() <= 0.001: continue #cut energy part chargees < 0.96 * energy jet energyjet1 = from_tree.get_reco_jet1_e() energyjet2 = from_tree.get_reco_jet2_e() energyjet3 = from_tree.get_reco_jet3_e() energyjet4 = from_tree.get_reco_jet4_e() # partchargees=['11','13','211'] energypartchargeesjet1 = from_tree.get_reco_jet1_11_e( ) + from_tree.get_reco_jet1_13_e() + from_tree.get_reco_jet1_211_e() energypartchargeesjet2 = from_tree.get_reco_jet2_11_e( ) + from_tree.get_reco_jet2_13_e() + from_tree.get_reco_jet2_211_e() energypartchargeesjet3 = from_tree.get_reco_jet3_11_e( ) + from_tree.get_reco_jet3_13_e() + from_tree.get_reco_jet3_211_e() energypartchargeesjet4 = from_tree.get_reco_jet4_11_e( ) + from_tree.get_reco_jet4_13_e() + from_tree.get_reco_jet4_211_e() if energypartchargeesjet1 > 0.996 * energyjet1 or energypartchargeesjet2 > 0.996 * energyjet2 or energypartchargeesjet3 > 0.996 * energyjet3 or energypartchargeesjet4 > 0.996 * energyjet4: continue # Jets pvec = [] for part in range(4): njet = str("jet" + str(part + 1)) pvec.append(from_tree.get_p4(njet)) jets = [Jet() for i in range(from_tree.get_dim("jet"))] # Direct reconstruction if reconstruction == "direct": for jid, jet in enumerate(jets): jet.set_id(str("jet" + str(jid + 1))) jet.set_mref(m_w) jet.set_p4(pvec[jid]) # Energy Rescaling else: beta = get_beta_matrix(pvec) energy = energy_from_direction(ecm, beta, from_tree.get_dim("jet")) # discarded events with energy < 0 solutions (no physics) if any(energy < 0): continue # Need to rescale the p4 matrix p4_resc = rescaling_factors.rescale( pvec, rescaling_factors.factor(energy, pvec)) for jid, jet in enumerate(jets): jet.set_id(str("jet" + str(jid + 1))) jet.set_mref(m_w) jet.set_p4(p4_resc[jid]) dijet = get_dijets_pairs(jets) # If the pairing conditions are not full if not dijet: continue #cut energie dijets similaires #diffedjets = return_edijets(dijet) #if diffedjets>=30: continue openingangledj1, openingangledj2 = return_opening_angle(dijet) if openingangledj1 <= 60 or openingangledj2 <= 60 or openingangledj1 >= 145 or openingangledj2 >= 145: continue m_small, m_large = return_mass(dijet) #cut sur la masse de mW1 small mW2 large if m_large <= 70: #or m_small<40: continue #fonction qui rempli les histos fill_histograms(dijet, histdict) #y34=from_tree.get_y34() #treecuts.Fill() y34[0] = from_tree.get_y34() reco_jet1_e[0] = from_tree.get_reco_jet1_e() reco_jet2_e[0] = from_tree.get_reco_jet2_e() reco_jet3_e[0] = from_tree.get_reco_jet3_e() reco_jet4_e[0] = from_tree.get_reco_jet4_e() reco_jet1_px[0] = from_tree.get_reco_jet1_px() reco_jet2_px[0] = from_tree.get_reco_jet2_px() reco_jet3_px[0] = from_tree.get_reco_jet3_px() reco_jet4_px[0] = from_tree.get_reco_jet4_px() reco_jet1_py[0] = from_tree.get_reco_jet1_py() reco_jet2_py[0] = from_tree.get_reco_jet2_py() reco_jet3_py[0] = from_tree.get_reco_jet3_py() reco_jet4_py[0] = from_tree.get_reco_jet4_py() reco_jet1_pz[0] = from_tree.get_reco_jet1_pz() reco_jet2_pz[0] = from_tree.get_reco_jet2_pz() reco_jet3_pz[0] = from_tree.get_reco_jet3_pz() reco_jet4_pz[0] = from_tree.get_reco_jet4_pz() reco_jet_size[0] = from_tree.get_reco_jet_size() quark_size[0] = from_tree.get_quark_size() file_type[0] = float(fileoutput1) quark1_e[0] = from_tree.get_quark1_e() quark2_e[0] = from_tree.get_quark2_e() quark3_e[0] = from_tree.get_quark3_e() quark4_e[0] = from_tree.get_quark4_e() quark1_px[0] = from_tree.get_quark1_px() quark2_px[0] = from_tree.get_quark2_px() quark3_px[0] = from_tree.get_quark3_px() quark4_px[0] = from_tree.get_quark4_px() quark1_py[0] = from_tree.get_quark1_py() quark2_py[0] = from_tree.get_quark2_py() quark3_py[0] = from_tree.get_quark3_py() quark4_py[0] = from_tree.get_quark4_py() quark1_pz[0] = from_tree.get_quark1_pz() quark2_pz[0] = from_tree.get_quark2_pz() quark3_pz[0] = from_tree.get_quark3_pz() quark4_pz[0] = from_tree.get_quark4_pz() gen_jet_size[0] = from_tree.get_gen_jet_size() gen_jet1_e[0] = from_tree.get_gen_jet1_e() gen_jet2_e[0] = from_tree.get_gen_jet2_e() gen_jet3_e[0] = from_tree.get_gen_jet3_e() gen_jet4_e[0] = from_tree.get_gen_jet4_e() gen_jet1_px[0] = from_tree.get_gen_jet1_px() gen_jet2_px[0] = from_tree.get_gen_jet2_px() gen_jet3_px[0] = from_tree.get_gen_jet3_px() gen_jet4_px[0] = from_tree.get_gen_jet4_px() gen_jet1_py[0] = from_tree.get_gen_jet1_py() gen_jet2_py[0] = from_tree.get_gen_jet2_py() gen_jet3_py[0] = from_tree.get_gen_jet3_py() gen_jet4_py[0] = from_tree.get_gen_jet4_py() gen_jet1_pz[0] = from_tree.get_gen_jet1_pz() gen_jet2_pz[0] = from_tree.get_gen_jet2_pz() gen_jet3_pz[0] = from_tree.get_gen_jet3_pz() gen_jet4_pz[0] = from_tree.get_gen_jet4_pz() #print str(quark4_pz[0]) treecut.Fill() f1.cd() f1.Write() f1.Close()
def runbbdm(txtfile): infile_=[] outfilename="" prefix="Skimmed_" ikey_ = "" if not runInteractive: print "running for ", txtfile infile_ = TextToList(txtfile) outfile = txtfile.split('/')[-1].replace('.txt','.root') #key_=txtfile[1] ''' old prefix="Skimmed_" outfilename= prefix+infile_.split("/")[-1] ''' outfilename= outfile#prefix+key_+".root" print "outfilename", outfilename if runInteractive: #infile_=txtfile #print "infile_", infile_ #ikey_ = txtfile[0].split("/")[-5] ## after the crabConfig bug fix this will become -4 #print "ikey_", ikey_ #outfilename=prefix+ikey_+".root" infile_=TextToList(txtfile) #print "running code for ",infile_ prefix_ = '' #'/eos/cms/store/group/phys_exotica/bbMET/2017_skimmedFiles/locallygenerated/' if outputdir!='.': prefix_ = outputdir+'/' #print "prefix_", prefix_ outfilename = prefix_+txtfile.split('/')[-1].replace('.txt','.root')#"SkimmedTree.root" print 'outfilename', outfilename samplename = whichsample(outfilename) #outputfilename = args.outputfile h_total = TH1F('h_total','h_total',2,0,2) h_total_mcweight = TH1F('h_total_mcweight','h_total_mcweight',2,0,2) triglist = trig.trigger2016 passfilename = open("configs/outfilename.txt","w") passfilename.write(outfilename) passfilename.close() ## this will give some warning, but that is safe, from outputTree import * ## following can be moved to outputtree.py if we manage to change the name of output root file. outfilenameis = outfilename outfile = TFile(outfilenameis,'RECREATE') outTree = TTree( 'outTree', 'tree branches' ) outTree.Branch( 'st_runId', st_runId , 'st_runId/L') outTree.Branch( 'st_lumiSection', st_lumiSection , 'st_lumiSection/L') outTree.Branch( 'st_eventId', st_eventId, 'st_eventId/L') outTree.Branch( 'st_pfMetCorrPt', st_pfMetCorrPt , 'st_pfMetCorrPt/F') outTree.Branch( 'st_pfMetCorrPhi', st_pfMetCorrPhi , 'st_pfMetCorrPhi/F') outTree.Branch( 'st_pfMetUncJetResUp', st_pfMetUncJetResUp) outTree.Branch( 'st_pfMetUncJetResDown', st_pfMetUncJetResDown) outTree.Branch( 'st_pfMetUncJetEnUp', st_pfMetUncJetEnUp ) outTree.Branch( 'st_pfMetUncJetEnDown', st_pfMetUncJetEnDown) outTree.Branch( 'st_isData', st_isData , 'st_isData/O') outTree.Branch( 'st_THINnJet',st_THINnJet, 'st_THINnJet/L' ) outTree.Branch( 'st_THINjetPx', st_THINjetPx ) outTree.Branch( 'st_THINjetPy' , st_THINjetPy ) outTree.Branch( 'st_THINjetPz', st_THINjetPz ) outTree.Branch( 'st_THINjetEnergy', st_THINjetEnergy ) outTree.Branch( 'st_THINjetDeepCSV',st_THINjetDeepCSV ) outTree.Branch( 'st_THINjetHadronFlavor',st_THINjetHadronFlavor ) outTree.Branch( 'st_THINjetNHadEF',st_THINjetNHadEF ) outTree.Branch( 'st_THINjetCHadEF',st_THINjetCHadEF ) outTree.Branch( 'st_THINjetCEmEF',st_THINjetCEmEF ) outTree.Branch( 'st_THINjetPhoEF',st_THINjetPhoEF ) outTree.Branch( 'st_THINjetEleEF',st_THINjetEleEF ) outTree.Branch( 'st_THINjetMuoEF',st_THINjetMuoEF ) outTree.Branch('st_THINjetCorrUnc', st_THINjetCorrUnc) # jet mass branches added outTree.Branch( 'st_THINjetCanMass', st_THINjetCanMass) ################## outTree.Branch( 'st_nfjet',st_nfjet,'st_nfjet/L') outTree.Branch( 'st_fjetPx',st_fjetPx) outTree.Branch( 'st_fjetPy',st_fjetPy) outTree.Branch( 'st_fjetPz',st_fjetPz) outTree.Branch( 'st_fjetEnergy',st_fjetEnergy) outTree.Branch( 'st_fjetDoubleSV',st_fjetDoubleSV) outTree.Branch( 'st_fjetProbQCDb',st_fjetProbQCDb) outTree.Branch( 'st_fjetProbHbb',st_fjetProbHbb) outTree.Branch( 'st_fjetProbQCDc',st_fjetProbQCDc) outTree.Branch( 'st_fjetProbHcc',st_fjetProbHcc) outTree.Branch( 'st_fjetProbHbbc',st_fjetProbHbbc) outTree.Branch( 'st_fjetProbbbvsLight',st_fjetProbbbvsLight) outTree.Branch( 'st_fjetProbccvsLight',st_fjetProbccvsLight) outTree.Branch( 'st_fjetProbTvsQCD',st_fjetProbTvsQCD) outTree.Branch( 'st_fjetProbWvsQCD',st_fjetProbWvsQCD) outTree.Branch( 'st_fjetProbZHbbvsQCD',st_fjetProbZHbbvsQCD) outTree.Branch( 'st_fjetSDMass',st_fjetSDMass) outTree.Branch( 'st_fjetN2b1',st_fjetN2b1) outTree.Branch( 'st_fjetN2b2',st_fjetN2b2) outTree.Branch( 'st_fjetCHSPRMass',st_fjetCHSPRMass) outTree.Branch( 'st_fjetCHSSDMass',st_fjetCHSSDMass) outTree.Branch( 'st_nEle',st_nEle , 'st_nEle/L') outTree.Branch( 'st_elePx', st_elePx ) outTree.Branch( 'st_elePy' , st_elePy ) outTree.Branch( 'st_elePz', st_elePz ) outTree.Branch( 'st_eleEnergy', st_eleEnergy ) outTree.Branch( 'st_eleIsPassTight', st_eleIsPassTight)#, 'st_eleIsPassTight/O' ) outTree.Branch( 'st_eleIsPassLoose', st_eleIsPassLoose)#, 'st_eleIsPassLoose/O' ) outTree.Branch( 'st_nPho',st_nPho , 'st_nPho/L') outTree.Branch( 'st_phoIsPassTight', st_phoIsPassTight)#, 'st_phoIsPassTight/O' ) outTree.Branch( 'st_phoPx', st_phoPx ) outTree.Branch( 'st_phoPy' , st_phoPy ) outTree.Branch( 'st_phoPz', st_phoPz ) outTree.Branch( 'st_phoEnergy', st_phoEnergy ) outTree.Branch( 'st_nMu',st_nMu , 'st_nMu/L') outTree.Branch( 'st_muPx', st_muPx) outTree.Branch( 'st_muPy' , st_muPy) outTree.Branch( 'st_muPz', st_muPz) outTree.Branch( 'st_muEnergy', st_muEnergy) outTree.Branch( 'st_isTightMuon', st_isTightMuon)#, 'st_isTightMuon/O' ) #outTree.Branch( 'st_muIso', st_muIso)#, 'st_muIso/F') #outTree.Branch( 'st_HPSTau_n', st_HPSTau_n, 'st_HPSTau_n/L') outTree.Branch( 'st_nTau_DRBased_EleMuVeto',st_nTau_DRBased_EleMuVeto,'st_nTau_DRBased_EleMuVeto/L') outTree.Branch( 'st_nTau_discBased_looseElelooseMuVeto',st_nTau_discBased_looseElelooseMuVeto,'st_nTau_discBased_looseElelooseMuVeto/L') outTree.Branch( 'st_nTau_discBased_looseEleTightMuVeto',st_nTau_discBased_looseEleTightMuVeto,'st_nTau_discBased_looseEleTightMuVeto/L') outTree.Branch( 'st_nTau_discBased_mediumElelooseMuVeto',st_nTau_discBased_mediumElelooseMuVeto,'st_nTau_discBased_mediumElelooseMuVeto/L') outTree.Branch( 'st_nTau_discBased_TightEleTightMuVeto',st_nTau_discBased_TightEleTightMuVeto,'st_nTau_discBased_TightEleTightMuVeto/L') ''' outTree.Branch( 'st_Taudisc_againstLooseMuon', st_Taudisc_againstLooseMuon) outTree.Branch( 'st_Taudisc_againstTightMuon', st_Taudisc_againstTightMuon) outTree.Branch( 'st_Taudisc_againstLooseElectron', st_Taudisc_againstLooseElectron) outTree.Branch( 'st_Taudisc_againstMediumElectron', st_Taudisc_againstMediumElectron) outTree.Branch( 'st_tau_isoLoose', st_tau_isoLoose) outTree.Branch( 'st_tau_isoMedium', st_tau_isoMedium) outTree.Branch( 'st_tau_isoTight', st_tau_isoTight) outTree.Branch('st_tau_dm',st_tau_dm) ''' outTree.Branch( 'st_pu_nTrueInt', st_pu_nTrueInt, 'st_pu_nTrueInt/F') outTree.Branch( 'st_pu_nPUVert', st_pu_nPUVert, 'st_pu_nPUVert/F') outTree.Branch( 'st_THINjetNPV', st_THINjetNPV, 'st_THINjetNPV/F') outTree.Branch( 'mcweight', mcweight, 'mcweight/F') # outTree.Branch( 'st_nGenPar',st_nGenPar,'st_nGenPar/L' ) #nGenPar/I # outTree.Branch( 'st_genParId',st_genParId ) #vector<int> # outTree.Branch( 'st_genMomParId',st_genMomParId ) # outTree.Branch( 'st_genParSt',st_genParSt ) # outTree.Branch( 'st_genParPx', st_genParPx ) # outTree.Branch( 'st_genParPy' , st_genParPy ) # outTree.Branch( 'st_genParPz', st_genParPz ) # outTree.Branch( 'st_genParEnergy', st_genParEnergy ) outTree.Branch( 'st_genParPt', st_genParPt, ) outTree.Branch( 'st_genParSample', st_genParSample ) ''' outTree.Branch( 'WenuRecoil', WenuRecoil, 'WenuRecoil/F') outTree.Branch( 'Wenumass', Wenumass, 'Wenumass/F') outTree.Branch( 'WenuPhi', WenuPhi, 'WenuPhi/F') outTree.Branch( 'WmunuRecoil', WmunuRecoil, 'WmunuRecoil/F') outTree.Branch( 'Wmunumass', Wmunumass, 'Wmunumass/F') outTree.Branch( 'WmunuPhi', WmunuPhi, 'WmunuPhi/F') outTree.Branch( 'ZeeRecoil', ZeeRecoil, 'ZeeRecoil/F') outTree.Branch( 'ZeeMass', ZeeMass, 'ZeeMass/F') outTree.Branch( 'ZeePhi', ZeePhi, 'ZeePhi/F') outTree.Branch( 'ZmumuRecoil', ZmumuRecoil, 'ZmumuRecoil/F') outTree.Branch( 'ZmumuMass', ZmumuMass, 'ZmumuMass/F') outTree.Branch( 'ZmumuPhi', ZmumuPhi, 'ZmumuPhi/F') outTree.Branch( 'GammaRecoil', GammaRecoil, 'GammaRecoil/F') outTree.Branch( 'GammaPhi', GammaPhi, 'GammaPhi/F') ''' # trigger status branches outTree.Branch( 'st_eletrigdecision', st_eletrigdecision , 'st_eletrigdecision/O') outTree.Branch( 'st_mutrigdecision', st_mutrigdecision , 'st_mutrigdecision/O') outTree.Branch( 'st_mettrigdecision', st_mettrigdecision , 'st_mettrigdecision/O') outTree.Branch( 'st_photrigdecision', st_photrigdecision , 'st_photrigdecision/O') ## following can be moved to outputtree.py if we manage to change the name of output root file. jetvariables = branches.allvars2017 filename = infile_ ieve = 0;icount = 0 #print "running on", filename for df in read_root(filename, 'tree/treeMaker', columns=jetvariables, chunksize=125000): for run,lumi,event,isData,mcWeight_,\ pu_nTrueInt_,pu_nPUVert_,\ trigName_,trigResult_,filterName,filterResult,\ met_,metphi_,metUnc_,\ nele_,elepx_,elepy_,elepz_,elee_,elevetoid_, elelooseid_,eletightid_,\ eleCharge_, npho_,phopx_,phopy_,phopz_,phoe_,pholooseid_,photightID_,\ nmu_,mupx_,mupy_,mupz_,mue_,mulooseid_,mutightid_,muisoloose, muisomedium, muisotight, muisovtight, muCharge_,\ nTau_,tau_px_,tau_py_,tau_pz_,tau_e_,tau_dm_,tau_isLoose_,tau_isoMedium_,tau_isoTight_,\ Taudisc_againstLooseMuon,Taudisc_againstTightMuon,Taudisc_againstLooseElectron,Taudisc_againstMediumElectron,Taudisc_againstTightElectron,\ nGenPar_,genParId_,genMomParId_,genParSt_,genpx_,genpy_,genpz_,gene_,\ nak4jet_,ak4px_,ak4py_,ak4pz_,ak4e_,\ ak4TightID_,ak4deepcsv_,ak4flavor_,ak4NHEF_,ak4CHEF_,\ ak4CEmEF_,ak4PhEF_,ak4EleEF_,ak4MuEF_, ak4JEC_, ak4NPV_,\ fatnJet, fatjetPx, fatjetPy, fatjetPz, fatjetEnergy,fatjetTightID,\ fatjet_DoubleSV, fatjet_probQCDb, fatjet_probHbb, fatjet_probQCDc, fatjet_probHcc, fatjet_probHbbc,\ fatjet_prob_bbvsLight, fatjet_prob_ccvsLight, fatjet_prob_TvsQCD, fatjet_prob_WvsQCD, fatjet_prob_ZHbbvsQCD,\ fatjetSDmass, fatN2_Beta1_, fatN2_Beta2_, fatjetCHSPRmassL2L3Corr, fatjetCHSSDmassL2L3Corr\ in zip(df.runId,df.lumiSection,df.eventId,df.isData,df.mcWeight,\ df.pu_nTrueInt,df.pu_nPUVert,\ df.hlt_trigName,df.hlt_trigResult,df.hlt_filterName,df.hlt_filterResult,\ df.pfMetCorrPt,df.pfMetCorrPhi,df.pfMetCorrUnc,\ df.nEle,df.elePx,df.elePy,df.elePz,df.eleEnergy,df.eleIsPassVeto, df.eleIsPassLoose,df.eleIsPassTight,\ df.eleCharge,df.nPho,df.phoPx,df.phoPy,df.phoPz,df.phoEnergy,df.phoIsPassLoose,df.phoIsPassTight,\ df.nMu,df.muPx,df.muPy,df.muPz,df.muEnergy,df.isLooseMuon,df.isTightMuon,df.PFIsoLoose, df.PFIsoMedium, df.PFIsoTight, df.PFIsoVeryTight, df.muCharge,\ df.HPSTau_n,df.HPSTau_Px,df.HPSTau_Py,df.HPSTau_Pz,df.HPSTau_Energy,df.disc_decayModeFinding,df.disc_byLooseIsolationMVArun2017v2DBoldDMwLT2017,df.disc_byMediumIsolationMVArun2017v2DBoldDMwLT2017,df.disc_byTightIsolationMVArun2017v2DBoldDMwLT2017,\ df.disc_againstMuonLoose3,df.disc_againstMuonTight3,df.disc_againstElectronLooseMVA6,df.disc_againstElectronMediumMVA6,df.disc_againstElectronTightMVA6,\ df.nGenPar,df.genParId,df.genMomParId,df.genParSt,df.genParPx,df.genParPy,df.genParPz,df.genParE,\ df.THINnJet,df.THINjetPx,df.THINjetPy,df.THINjetPz,df.THINjetEnergy,\ df.THINjetPassIDTight,df.THINjetDeepCSV_b,df.THINjetHadronFlavor,df.THINjetNHadEF,df.THINjetCHadEF,\ df.THINjetCEmEF,df.THINjetPhoEF,df.THINjetEleEF,df.THINjetMuoEF,df.THINjetCorrUncUp,df.THINjetNPV, \ df.FATnJet, df.FATjetPx, df.FATjetPy, df.FATjetPz, df.FATjetEnergy, df.FATjetPassIDTight,\ df.FATjet_DoubleSV, df.FATjet_probQCDb, df.FATjet_probHbb, df.FATjet_probQCDc, df.FATjet_probHcc, df.FATjet_probHbbc,\ df.FATjet_prob_bbvsLight, df.FATjet_prob_ccvsLight, df.FATjet_prob_TvsQCD, df.FATjet_prob_WvsQCD, df.FATjet_prob_ZHbbvsQCD,\ df.FATjetSDmass, df.FATN2_Beta1_, df.FATN2_Beta2_, df.FATjetCHSPRmassL2L3Corr, df.FATjetCHSSDmassL2L3Corr ): if debug_: print len(trigName_),len(trigResult_),len(filterName),len(filterResult),len(metUnc_), len(elepx_), len(elepy_), len(elepz_), len(elee_), len(elevetoid_), len(elelooseid_), len(eletightid_), len(eleCharge_), npho_,len(phopx_), len(phopy_), len(phopz_), len(phoe_), len(pholooseid_), len(photightID_), nmu_, len(mupx_), len(mupy_), len(mupz_), len(mue_), len(mulooseid_), len(mutightid_), len(muisoloose), len(muisomedium), len(muisotight), len(muisovtight), len(muCharge_), nTau_, len(tau_px_), len(tau_py_), len(tau_pz_), len(tau_e_), len(tau_dm_), len(tau_isLoose_), len(genParId_), len(genMomParId_), len(genParSt_), len(genpx_), len(genpy_), len(genpz_), len(gene_), len(ak4px_), len(ak4py_), len(ak4pz_), len(ak4e_), len(ak4TightID_), len(ak4deepcsv_), len(ak4flavor_), len(ak4NHEF_), len(ak4CHEF_), len(ak4CEmEF_), len(ak4PhEF_), len(ak4EleEF_), len(ak4MuEF_), len(ak4JEC_), len(fatjetPx), len(fatjetPy), len(fatjetPz), len(fatjetEnergy), len(fatjetTightID), len(fatjet_DoubleSV), len(fatjet_probQCDb), len(fatjet_probHbb), len(fatjet_probQCDc), len(fatjet_probHcc), len(fatjet_probHbbc), len(fatjet_prob_bbvsLight), len(fatjet_prob_ccvsLight), len(fatjet_prob_TvsQCD), len(fatjet_prob_WvsQCD), len(fatjet_prob_ZHbbvsQCD), len(fatjetSDmass), len(fatN2_Beta1_), len(fatN2_Beta2_), len(fatjetCHSPRmassL2L3Corr), len(fatjetCHSSDmassL2L3Corr) if ieve%1000==0: print "Processed",ieve,"Events" ieve = ieve + 1 # ------------------------------------------------- # MC Weights # ------------------------------------------------- mcweight[0] = 0.0 if isData==1: mcweight[0] = 1.0 if not isData : if mcWeight_<0: mcweight[0] = -1.0 if mcWeight_>0: mcweight[0] = 1.0 h_total.Fill(1.); h_total_mcweight.Fill(1.,mcweight[0]); # ------------------------------------------------- ## Trigger selection # ------------------------------------------------- eletrigdecision=False mudecision=False metdecision=False phodecision=False eletrigstatus = [( anautil.CheckFilter(trigName_, trigResult_, trig.Electrontrigger2017[itrig] ) ) for itrig in range(len(trig.Electrontrigger2017))] mutrigstatus = [( anautil.CheckFilter(trigName_, trigResult_, trig.Muontrigger2017[itrig] ) ) for itrig in range(len(trig.Muontrigger2017)) ] mettrigstatus = [( anautil.CheckFilter(trigName_, trigResult_, trig.METtrigger2017[itrig] ) ) for itrig in range(len(trig.METtrigger2017)) ] photrigstatus = [( anautil.CheckFilter(trigName_, trigResult_, trig.Photontrigger2017[itrig] ) ) for itrig in range(len(trig.Photontrigger2017)) ] eletrigdecision = boolutil.logical_OR(eletrigstatus) mutrigdecision = boolutil.logical_OR(mutrigstatus) mettrigdecision = boolutil.logical_OR(mettrigstatus) photrigdecision = boolutil.logical_OR(photrigstatus) if not isData: eletrigdecision = True mutrigdecision = True mettrigdecision = True photrigdecision = True # ------------------------------------------------------ ## Filter selection # ------------------------------------------------------ filterdecision=False filterstatus = [False for ifilter in range(len(filters.filters2017)) ] filterstatus = [anautil.CheckFilter(filterName, filterResult, filters.filters2017[ifilter]) for ifilter in range(len(filters.filters2017)) ] if not isData: filterdecision = True if isData: filterdecision = boolutil.logical_AND(filterstatus) if filterdecision == False: continue # ------------------------------------------------------ ## PFMET Selection # -------------------------------------------------------- pfmetstatus = ( met_ > 170.0 ) ''' ******* * * ****** * * * * * * ******* ******** * * * * * * * * * * ****** ''' phopt = [getPt(phopx_[ip], phopy_[ip]) for ip in range(npho_)] phoeta = [getEta(phopx_[ip], phopy_[ip], phopz_[ip]) for ip in range(npho_)] pho_pt15_eta2p5_looseID = [ (phopt[ip] > 15.0) and (abs(phoeta[ip]) < 2.5) and (pholooseid_[ip]) for ip in range(npho_)] pass_pho_index = boolutil.WhereIsTrue(pho_pt15_eta2p5_looseID) ''' **** * **** * * * *** * *** * * * **** **** **** ''' elept = [getPt(elepx_[ie], elepy_[ie]) for ie in range(nele_)] eleeta = [getEta(elepx_[ie], elepy_[ie], elepz_[ie]) for ie in range(nele_)] elephi = [getPhi(elepx_[ie], elepy_[ie]) for ie in range(nele_)] ele_pt10_eta2p5_vetoID = [(elept[ie] > 10.0) and (elevetoid_[ie]) and (((abs(eleeta[ie]) > 1.566 or abs(eleeta[ie]) < 1.4442) and (abs(eleeta[ie]) < 2.5))) for ie in range(nele_)] ele_pt10_eta2p5_looseID = [(elept[ie] > 10.0) and (elelooseid_[ie]) and (((abs(eleeta[ie]) > 1.566 or abs(eleeta[ie]) < 1.4442) and (abs(eleeta[ie]) < 2.5))) for ie in range(nele_)] ele_pt30_eta2p5_tightID = [(elept[ie] > 30.0) and (eletightid_[ie]) and (((abs(eleeta[ie]) > 1.566 or abs(eleeta[ie]) < 1.4442) and (abs(eleeta[ie]) < 2.5))) for ie in range(nele_)] pass_ele_veto_index = boolutil.WhereIsTrue(ele_pt10_eta2p5_vetoID) ''' ** * * * * * * * * * * * * * * * * * * * * ***** ''' mupt = [getPt(mupx_[imu], mupy_[imu]) for imu in range(nmu_)] mueta = [getEta(mupx_[imu], mupy_[imu], mupz_[imu]) for imu in range(nmu_)] muphi = [getPhi(mupx_[imu], mupy_[imu]) for imu in range(nmu_)] mu_pt10_eta2p4_looseID_looseISO = [ ( (mupt[imu] > 10.0) and (abs(mueta[imu]) < 2.4 ) and (mulooseid_[imu]) and (muisoloose[imu]) ) for imu in range(nmu_) ] mu_pt30_eta2p4_tightID_tightISO = [ ( (mupt[imu] > 30.0) and (abs(mueta[imu]) < 2.4 ) and (mutightid_[imu]) and (muisotight[imu]) ) for imu in range(nmu_) ] pass_mu_index = boolutil.WhereIsTrue(mu_pt10_eta2p4_looseID_looseISO) ''' ******* ***** ******* * * * * **** * * * * *** ***** * ''' ak4pt = [getPt(ak4px_[ij], ak4py_[ij]) for ij in range(nak4jet_)] ak4eta = [getEta(ak4px_[ij], ak4py_[ij], ak4pz_[ij]) for ij in range(nak4jet_)] ak4phi = [getPhi(ak4px_[ij], ak4py_[ij]) for ij in range(nak4jet_)] ak4_pt30_eta4p5_IDT = [ ( (ak4pt[ij] > 30.0) and (abs(ak4eta[ij]) < 4.5) and (ak4TightID_[ij] ) ) for ij in range(nak4jet_)] ##--- jet cleaning jetCleanAgainstEle = [] jetCleanAgainstMu = [] pass_jet_index_cleaned = [] if len(ak4_pt30_eta4p5_IDT) > 0: DRCut = 0.4 jetCleanAgainstEle = anautil.jetcleaning(ak4_pt30_eta4p5_IDT, ele_pt10_eta2p5_vetoID, ak4eta, eleeta, ak4phi, elephi, DRCut) jetCleanAgainstMu = anautil.jetcleaning(ak4_pt30_eta4p5_IDT, mu_pt10_eta2p4_looseID_looseISO, ak4eta, mueta, ak4phi, muphi, DRCut) jetCleaned = boolutil.logical_AND_List3(ak4_pt30_eta4p5_IDT,jetCleanAgainstEle, jetCleanAgainstMu) pass_jet_index_cleaned = boolutil.WhereIsTrue(jetCleaned) if debug_:print "pass_jet_index_cleaned = ", pass_jet_index_cleaned,"nJets= ",len(ak4px_) ''' ****** ******* ***** ******* * * * * ***** ---- * **** * * * * * * *** ***** * ''' fatjetpt = [getPt(fatjetPx[ij], fatjetPy[ij]) for ij in range(fatnJet)] fatjeteta = [getEta(fatjetPx[ij], fatjetPy[ij], fatjetPz[ij]) for ij in range(fatnJet)] fatjetphi = [getPhi(fatjetPx[ij], fatjetPy[ij]) for ij in range(fatnJet)] fatjet_pt200_eta2p5_IDT = [ ( (fatjetpt[ij] > 200.0) and (abs(fatjeteta[ij]) < 2.5) and (fatjetTightID[ij] ) ) for ij in range(fatnJet)] ##--- fat jet cleaning fatjetCleanAgainstEle = [] fatjetCleanAgainstMu = [] pass_fatjet_index_cleaned = [] if len(fatjet_pt200_eta2p5_IDT) > 0: fatjetCleanAgainstEle = anautil.jetcleaning(fatjet_pt200_eta2p5_IDT, ele_pt10_eta2p5_vetoID, fatjeteta, eleeta, fatjetphi, elephi, DRCut) fatjetCleanAgainstMu = anautil.jetcleaning(fatjet_pt200_eta2p5_IDT, mu_pt10_eta2p4_looseID_looseISO, fatjeteta, mueta, fatjetphi, muphi, DRCut) fatjetCleaned = boolutil.logical_AND_List3(fatjet_pt200_eta2p5_IDT, fatjetCleanAgainstEle, fatjetCleanAgainstMu) pass_fatjet_index_cleaned = boolutil.WhereIsTrue(fatjetCleaned) if debug_:print "pass_fatjet_index_cleaned = ", pass_fatjet_index_cleaned," nJets = ",len(fatjetpx) ''' ******** * * * * * * * * * * * * * * ******** * * * * * * * * * * ******* ''' taupt = [getPt(tau_px_[itau], tau_py_[itau]) for itau in range(nTau_)] taueta = [getEta(tau_px_[itau], tau_py_[itau], tau_pz_[itau]) for itau in range(nTau_)] tauphi = [getPhi(tau_px_[itau], tau_py_[itau]) for itau in range(nTau_)] tau_eta2p3_iDLdm_pt18 = [ ( (taupt[itau] > 18.0) and (abs(taueta[itau]) < 2.3) and (tau_isLoose_[itau]) and (tau_dm_[itau]) ) for itau in range(nTau_)] if debug_:print "tau_eta2p3_iDLdm_pt18 = ", tau_eta2p3_iDLdm_pt18 tau_eta2p3_iDLdm_pt18_looseEleVeto_looseMuVeto = [ ( (taupt[itau] > 18.0) and (abs(taueta[itau]) < 2.3) and (tau_isLoose_[itau]) and (tau_dm_[itau]) and (Taudisc_againstLooseElectron[itau]) and (Taudisc_againstLooseMuon[itau]) ) for itau in range(nTau_)] tau_eta2p3_iDLdm_pt18_looseEleVeto_tightMuVeto = [ ( (taupt[itau] > 18.0) and (abs(taueta[itau]) < 2.3) and (tau_isLoose_[itau]) and (tau_dm_[itau]) and (Taudisc_againstLooseElectron[itau]) and (Taudisc_againstTightMuon[itau]) ) for itau in range(nTau_)] tau_eta2p3_iDLdm_pt18_mediumEleVeto_looseMuVeto = [ ( (taupt[itau] > 18.0) and (abs(taueta[itau]) < 2.3) and (tau_isLoose_[itau]) and (tau_dm_[itau]) and (Taudisc_againstMediumElectron[itau]) and (Taudisc_againstLooseMuon[itau])) for itau in range(nTau_)] tau_eta2p3_iDLdm_pt18_tightEleVeto_looseMuVeto = [ ( (taupt[itau] > 18.0) and (abs(taueta[itau]) < 2.3) and (tau_isLoose_[itau]) and (tau_dm_[itau]) and (Taudisc_againstTightElectron[itau]) and (Taudisc_againstLooseMuon[itau])) for itau in range(nTau_)] tau_eta2p3_iDLdm_pt18_looseEleVeto_looseMuVeto_index = boolutil.WhereIsTrue(tau_eta2p3_iDLdm_pt18_looseEleVeto_looseMuVeto) tau_eta2p3_iDLdm_pt18_looseEleVeto_tightMuVeto_index = boolutil.WhereIsTrue(tau_eta2p3_iDLdm_pt18_looseEleVeto_tightMuVeto) tau_eta2p3_iDLdm_pt18_mediumEleVeto_looseMuVeto_index = boolutil.WhereIsTrue(tau_eta2p3_iDLdm_pt18_mediumEleVeto_looseMuVeto) tau_eta2p3_iDLdm_pt18_tightEleVeto_looseMuVeto_index = boolutil.WhereIsTrue(tau_eta2p3_iDLdm_pt18_tightEleVeto_looseMuVeto) ''' print 'tau_eta2p3_iDLdm_pt18_looseEleVeto_looseMuVeto_index', tau_eta2p3_iDLdm_pt18_looseEleVeto_looseMuVeto_index, 'tau_eta2p3_iDLdm_pt18_looseEleVeto_looseMuVeto', tau_eta2p3_iDLdm_pt18_looseEleVeto_looseMuVeto print 'tau_eta2p3_iDLdm_pt18_looseEleVeto_tightMuVeto_index', tau_eta2p3_iDLdm_pt18_looseEleVeto_tightMuVeto_index, 'tau_eta2p3_iDLdm_pt18_looseEleVeto_tightMuVeto',tau_eta2p3_iDLdm_pt18_looseEleVeto_tightMuVeto print 'tau_eta2p3_iDLdm_pt18_mediumEleVeto_looseMuVeto_index',tau_eta2p3_iDLdm_pt18_mediumEleVeto_looseMuVeto_index,'tau_eta2p3_iDLdm_pt18_mediumEleVeto_looseMuVeto',tau_eta2p3_iDLdm_pt18_mediumEleVeto_looseMuVeto print 'tau_eta2p3_iDLdm_pt18_tightEleVeto_looseMuVeto_index',tau_eta2p3_iDLdm_pt18_tightEleVeto_looseMuVeto_index,'tau_eta2p3_iDLdm_pt18_tightEleVeto_looseMuVeto',tau_eta2p3_iDLdm_pt18_tightEleVeto_looseMuVeto ''' tauCleanAgainstEle = [] tauCleanAgainstMu = [] pass_tau_index_cleaned_DRBased = [] if len(tau_eta2p3_iDLdm_pt18)>0: tauCleanAgainstEle = anautil.jetcleaning(tau_eta2p3_iDLdm_pt18, ele_pt10_eta2p5_looseID, taueta, eleeta, tauphi, elephi, DRCut) tauCleanAgainstMu = anautil.jetcleaning(tau_eta2p3_iDLdm_pt18, mu_pt10_eta2p4_looseID_looseISO, taueta, mueta, tauphi, muphi, DRCut) tauCleaned = boolutil.logical_AND_List3(tau_eta2p3_iDLdm_pt18 , tauCleanAgainstEle, tauCleanAgainstMu) pass_tau_index_cleaned_DRBased = boolutil.WhereIsTrue(tauCleaned) if debug_:print "pass_tau_index_cleaned_DRBased",pass_tau_index_cleaned_DRBased # ------------------------------------------------------------- st_runId[0] = long(run) st_lumiSection[0] = lumi st_eventId[0] = event st_isData[0] = isData st_eletrigdecision[0] = eletrigdecision st_mutrigdecision[0] = mutrigdecision st_mettrigdecision[0] = mettrigdecision st_photrigdecision[0] = photrigdecision st_pfMetCorrPt[0] = met_ st_pfMetCorrPhi[0] = metphi_ st_pfMetUncJetResUp.clear() st_pfMetUncJetResDown.clear() st_pfMetUncJetEnUp.clear() st_pfMetUncJetEnDown.clear() st_THINjetPx.clear() st_THINjetPy.clear() st_THINjetPz.clear() st_THINjetEnergy.clear() st_THINjetDeepCSV.clear() st_THINjetHadronFlavor.clear() st_THINjetNHadEF.clear() st_THINjetCHadEF.clear() st_THINjetCEmEF.clear() st_THINjetPhoEF.clear() st_THINjetEleEF.clear() st_THINjetMuoEF.clear() st_THINjetCorrUnc.clear() st_THINjetCanMass.clear() #added st_fjetPx.clear() st_fjetPy.clear() st_fjetPz.clear() st_fjetEnergy.clear() st_fjetDoubleSV.clear() st_fjetProbQCDb.clear() st_fjetProbHbb.clear() st_fjetProbQCDc.clear() st_fjetProbHcc.clear() st_fjetProbHbbc.clear() st_fjetProbbbvsLight.clear() st_fjetProbccvsLight.clear() st_fjetProbTvsQCD.clear() st_fjetProbWvsQCD.clear() st_fjetProbZHbbvsQCD.clear() st_fjetSDMass.clear() st_fjetN2b1.clear() st_fjetN2b2.clear() st_fjetCHSPRMass.clear() st_fjetCHSSDMass.clear() ''' st_Taudisc_againstLooseMuon.clear() st_Taudisc_againstTightMuon.clear() st_Taudisc_againstLooseElectron.clear() st_Taudisc_againstMediumElectron.clear() st_tau_isoLoose.clear() st_tau_isoMedium.clear() st_tau_isoTight.clear() st_tau_dm.clear() ''' st_elePx.clear() st_elePy.clear() st_elePz.clear() st_eleEnergy.clear() st_eleIsPassTight.clear() st_eleIsPassLoose.clear() st_muPx.clear() st_muPy.clear() st_muPz.clear() st_muEnergy.clear() st_isTightMuon.clear() #st_muIso.clear() st_phoPx.clear() st_phoPy.clear() st_phoPz.clear() st_phoEnergy.clear() st_phoIsPassTight.clear() # st_genParId.clear() # st_genMomParId.clear() # st_genParSt.clear() # st_genParPx.clear() # st_genParPy.clear() # st_genParPz.clear() # st_genParEnergy.clear() st_genParPt.clear() st_genParSample.clear() st_THINnJet[0] = len(pass_jet_index_cleaned) for ithinjet in pass_jet_index_cleaned: st_THINjetPx.push_back(ak4px_[ithinjet]) st_THINjetPy.push_back(ak4py_[ithinjet]) st_THINjetPz.push_back(ak4pz_[ithinjet]) st_THINjetEnergy.push_back(ak4e_[ithinjet]) st_THINjetDeepCSV.push_back(ak4deepcsv_[ithinjet]) st_THINjetHadronFlavor.push_back(int(ak4flavor_[ithinjet])) st_THINjetNHadEF.push_back(ak4NHEF_[ithinjet]) st_THINjetCHadEF.push_back(ak4CHEF_[ithinjet]) st_THINjetCEmEF.push_back(ak4CEmEF_[ithinjet]) st_THINjetPhoEF.push_back(ak4PhEF_[ithinjet]) st_THINjetEleEF.push_back(ak4EleEF_[ithinjet]) st_THINjetMuoEF.push_back(ak4MuEF_[ithinjet]) st_THINjetCorrUnc.push_back(ak4JEC_[ithinjet]) #try to sum 2 jet n = len(pass_jet_index_cleaned) for a in range(n): if n == 1: continue else: jet1 = pass_jet_index_cleaned[a] jet1_P4 = TLorentzVector(ak4px_[jet1],ak4py_[jet1],ak4pz_[jet1],ak4e_[jet1]) for b in range(n): if b > a: jet2 = pass_jet_index_cleaned[b] jet2_P4 = TLorentzVector(ak4px_[jet2],ak4py_[jet2],ak4pz_[jet2],ak4e_[jet2]) jetsInvMass = (jet1_P4+jet2_P4).M() st_THINjetCanMass.push_back(jetsInvMass) else: continue ################ if debug_:print 'njets: ',len(pass_jet_index_cleaned) st_nfjet[0] = len(pass_fatjet_index_cleaned) for ifjet in pass_fatjet_index_cleaned: st_fjetPx.push_back(fatjetPx[ifjet]) st_fjetPy.push_back(fatjetPy[ifjet]) st_fjetPz.push_back(fatjetPz[ifjet]) st_fjetEnergy.push_back(fatjetEnergy[ifjet]) st_fjetDoubleSV.push_back(fatjet_DoubleSV[ifjet]) st_fjetProbQCDb.push_back(fatjet_probQCDb[ifjet]) st_fjetProbHbb.push_back(fatjet_probHbb[ifjet]) st_fjetProbQCDc.push_back(fatjet_probQCDc[ifjet]) st_fjetProbHcc.push_back(fatjet_probHcc[ifjet]) st_fjetProbHbbc.push_back(fatjet_probHbbc[ifjet]) st_fjetProbbbvsLight.push_back(fatjet_prob_bbvsLight[ifjet]) st_fjetProbccvsLight.push_back(fatjet_prob_ccvsLight[ifjet]) st_fjetProbTvsQCD.push_back(fatjet_prob_TvsQCD[ifjet]) st_fjetProbWvsQCD.push_back(fatjet_prob_WvsQCD[ifjet]) st_fjetProbZHbbvsQCD.push_back(fatjet_prob_ZHbbvsQCD[ifjet]) st_fjetSDMass.push_back(fatjetSDmass[ifjet]) st_fjetN2b1.push_back(fatN2_Beta1_[ifjet]) st_fjetN2b2.push_back(fatN2_Beta2_[ifjet]) st_fjetCHSPRMass.push_back(fatjetCHSPRmassL2L3Corr[ifjet]) st_fjetCHSSDMass.push_back(fatjetCHSSDmassL2L3Corr[ifjet]) #print ("fatN2_Beta1_",fatN2_Beta1_[ifjet],"fatN2_Beta2_",fatN2_Beta2_[ifjet]) st_nEle[0] = len(pass_ele_veto_index) for iele in pass_ele_veto_index: st_elePx.push_back(elepx_[iele]) st_elePy.push_back(elepy_[iele]) st_elePz.push_back(elepz_[iele]) st_eleEnergy.push_back(elee_[iele]) st_eleIsPassTight.push_back(bool(eletightid_[iele])) if debug_:print 'nEle: ',len(pass_ele_veto_index) st_nMu[0] = len(pass_mu_index) for imu in pass_mu_index: st_muPx.push_back(mupx_[imu]) st_muPy.push_back(mupy_[imu]) st_muPz.push_back(mupz_[imu]) st_muEnergy.push_back(mue_[imu]) st_isTightMuon.push_back(bool(mutightid_[imu])) #st_muIso.push_back(muIso_[imu]) if debug_:print 'nMu: ',len(pass_mu_index) st_nTau_DRBased_EleMuVeto[0] = len(pass_tau_index_cleaned_DRBased) st_nTau_discBased_looseElelooseMuVeto[0] = len(tau_eta2p3_iDLdm_pt18_looseEleVeto_looseMuVeto_index) st_nTau_discBased_looseEleTightMuVeto[0] = len(tau_eta2p3_iDLdm_pt18_looseEleVeto_tightMuVeto_index) st_nTau_discBased_mediumElelooseMuVeto[0] = len(tau_eta2p3_iDLdm_pt18_mediumEleVeto_looseMuVeto_index) st_nTau_discBased_TightEleTightMuVeto[0] = len(tau_eta2p3_iDLdm_pt18_tightEleVeto_looseMuVeto_index) if debug_:print 'nTau: ',len(pass_tau_index_cleaned_DRBased) #print 'nTau: ',len(pass_tau_index_cleaned_DRBased),'event',event ''' for itau in pass_tau_index_cleaned: st_Taudisc_againstLooseMuon.push_back(bool(Taudisc_againstLooseMuon[itau])) st_Taudisc_againstTightMuon.push_back(bool(Taudisc_againstTightMuon[itau])) st_Taudisc_againstLooseElectron.push_back(bool(Taudisc_againstLooseElectron[itau])) st_Taudisc_againstMediumElectron.push_back(bool(Taudisc_againstMediumElectron[itau])) st_tau_isoLoose.push_back(bool(tau_isLoose_[itau])) st_tau_isoMedium.push_back(bool(tau_isoMedium_[itau])) st_tau_isoTight.push_back(bool(tau_isoTight_[itau])) st_tau_dm.push_back(bool(tau_dm_[itau])) ''' st_nPho[0]=len(pass_pho_index) for ipho in pass_pho_index: st_phoPx.push_back(phopx_[ipho]) st_phoPy.push_back(phopy_[ipho]) st_phoPz.push_back(phopz_[ipho]) st_phoEnergy.push_back(phoe_[ipho]) st_phoIsPassTight.push_back(bool(photightID_[ipho])) if debug_:print 'nPho: ',len(pass_pho_index) st_pu_nTrueInt[0] = pu_nTrueInt_ st_pu_nPUVert[0] = pu_nPUVert_ st_THINjetNPV[0] = ak4NPV_ #st_nGenPar[0] = nGenPar_ genpar_pt = GenPtProd.GenPtProducer(samplename, nGenPar_, genParId_, genMomParId_, genParSt_,genpx_,genpy_) for i in range(len(genpar_pt)): st_genParPt.push_back(genpar_pt[i]) st_genParSample.push_back(samplename) # for igp in range(nGenPar_): # st_genParId.push_back(int(genParId_[igp])) # st_genMomParId.push_back(int(genMomParId_[igp])) # st_genParSt.push_back(int(genParSt_[igp])) # st_genParPx.push_back(genpx_[igp]) # st_genParPy.push_back(genpy_[igp]) # st_genParPz.push_back(genpz_[igp]) # st_genParEnergy.push_back(gene_[igp]) if debug_: print 'nGen: ',nGenPar_ st_pfMetUncJetResUp.push_back(metUnc_[0]) st_pfMetUncJetResDown.push_back(metUnc_[1]) st_pfMetUncJetEnUp.push_back(metUnc_[2]) st_pfMetUncJetEnDown.push_back(metUnc_[3]) ## Fill variables for the CRs. WenuRecoil[0] = -1.0 Wenumass[0] = -1.0 WenuPhi[0] = -10. WmunuRecoil[0] = -1.0 Wmunumass[0] = -1.0 WmunuPhi[0] = -10. ZeeMass[0] = -1.0 ZeeRecoil[0] = -1.0 ZeePhi[0] = -10. ZmumuMass[0] = -1.0 ZmumuRecoil[0] = -1.0 ZmumuPhi[0] = -10. GammaRecoil[0] = -1.0 GammaPhi[0] = -10. if debug_: print 'Reached Fill variables' # ------------------ # Z CR # ------------------ ## for dielectron if len(pass_ele_veto_index) == 2: iele1=pass_ele_veto_index[0] iele2=pass_ele_veto_index[1] if eleCharge_[iele1]*eleCharge_[iele2]<0: ee_mass = InvMass(elepx_[iele1],elepy_[iele1],elepz_[iele1],elee_[iele1],elepx_[iele2],elepy_[iele2],elepz_[iele2],elee_[iele2]) zeeRecoilPx = -( met_*math.cos(metphi_) + elepx_[iele1] + elepx_[iele2]) zeeRecoilPy = -( met_*math.sin(metphi_) + elepy_[iele1] + elepy_[iele2]) ZeeRecoilPt = math.sqrt(zeeRecoilPx**2 + zeeRecoilPy**2) if ee_mass > 60.0 and ee_mass < 110.0 and ZeeRecoilPt > 170.: ZeeRecoil[0] = ZeeRecoilPt ZeeMass[0] = ee_mass ZeePhi[0] = mathutil.ep_arctan(zeeRecoilPx,zeeRecoilPy) ## for dimu if len(pass_mu_index) ==2: imu1=pass_mu_index[0] imu2=pass_mu_index[1] if muCharge_[imu1]*muCharge_[imu2]<0: mumu_mass = InvMass(mupx_[imu1],mupy_[imu1],mupz_[imu1],mue_[imu1],mupx_[imu2],mupy_[imu2],mupz_[imu2],mue_[imu2] ) zmumuRecoilPx = -( met_*math.cos(metphi_) + mupx_[imu1] + mupx_[imu2]) zmumuRecoilPy = -( met_*math.sin(metphi_) + mupy_[imu1] + mupy_[imu2]) ZmumuRecoilPt = math.sqrt(zmumuRecoilPx**2 + zmumuRecoilPy**2) if mumu_mass > 60.0 and mumu_mass < 110.0 and ZmumuRecoilPt > 170.: ZmumuRecoil[0] = ZmumuRecoilPt ZmumuMass[0] = mumu_mass ZmumuPhi[0] = mathutil.ep_arctan(zmumuRecoilPx,zmumuRecoilPy) if len(pass_ele_veto_index) == 2: ZRecoilstatus =(ZeeRecoil[0] > 170) elif len(pass_mu_index) == 2: ZRecoilstatus =(ZmumuRecoil[0] > 170) else: ZRecoilstatus=False if debug_: print 'Reached Z CR' # ------------------ # W CR # ------------------ ## for Single electron if len(pass_ele_veto_index) == 1: ele1 = pass_ele_veto_index[0] e_mass = MT(elept[ele1],met_, DeltaPhi(elephi[ele1],metphi_)) #transverse mass defined as sqrt{2pT*MET*(1-cos(dphi)} WenuRecoilPx = -( met_*math.cos(metphi_) + elepx_[ele1]) WenuRecoilPy = -( met_*math.sin(metphi_) + elepy_[ele1]) WenuRecoilPt = math.sqrt(WenuRecoilPx**2 + WenuRecoilPy**2) if WenuRecoilPt > 170.: WenuRecoil[0] = WenuRecoilPt Wenumass[0] = e_mass WenuPhi[0] = mathutil.ep_arctan(WenuRecoilPx,WenuRecoilPy) ## for Single muon if len(pass_mu_index) == 1: mu1 = pass_mu_index[0] mu_mass = MT(mupt[mu1],met_, DeltaPhi(muphi[mu1],metphi_)) #transverse mass defined as sqrt{2pT*MET*(1-cos(dphi)} WmunuRecoilPx = -( met_*math.cos(metphi_) + mupx_[mu1]) WmunuRecoilPy = -( met_*math.sin(metphi_) + mupy_[mu1]) WmunuRecoilPt = math.sqrt(WmunuRecoilPx**2 + WmunuRecoilPy**2) if WmunuRecoilPt > 170.: WmunuRecoil[0] = WmunuRecoilPt Wmunumass[0] = mu_mass WmunuPhi[0] = mathutil.ep_arctan(WmunuRecoilPx,WmunuRecoilPy) if len(pass_ele_veto_index) == 1: WRecoilstatus =(WenuRecoil[0] > 170) elif len(pass_mu_index) == 1: WRecoilstatus =(WmunuRecoil[0] > 170) else: WRecoilstatus=False if debug_: print 'Reached W CR' # ------------------ # Gamma CR # ------------------ ## for Single photon if len(pass_pho_index) >= 1: pho1 = pass_pho_index[0] GammaRecoilPx = -( met_*math.cos(metphi_) + phopx_[pho1]) GammaRecoilPy = -( met_*math.sin(metphi_) + phopy_[pho1]) GammaRecoilPt = math.sqrt(GammaRecoilPx**2 + GammaRecoilPy**2) if GammaRecoilPt > 170.: GammaRecoil[0] = GammaRecoilPt GammaPhi[0] = mathutil.ep_arctan(GammaRecoilPx,GammaRecoilPy) GammaRecoilStatus = (GammaRecoil[0] > 170) if debug_: print 'Reached Gamma CR' #if pfmetstatus==False and ZRecoilstatus==False and WRecoilstatus==False and GammaRecoilStatus==False: continue # ------------------ # Get bjets and higgs mass #------------------- # for JET #if sample = ak4_pt30_eta4p5_IDT # nbjet = [] # for i in range(len(nGenPar_)): # JID = genParId_[i] # momJID = genMomParId_[i] # if ( abs(JID) == 5): # #find out deltaR < 0.4 then append it # thisbjetpt = getPt[ak4_pt30_eta4p5_IDT[i]] outTree.Fill() #outfile = TFile(outfilenameis,'RECREATE') outfile.cd() h_total_mcweight.Write() h_total.Write() outfile.Write() print "output written to ", outfilename end = time.clock() print "%.4gs" % (end-start)
(daughter, decaymode) = tauDecayMode(tau) decay_type[0] = decaydict[decaymode] visible_pt = p4sumvis(finDaughters).pt() #print decaymode, 'vis pt = ', visible_pt , 'tau pt = ', tau.pt() if (visible_pt > tau.pt()): print 'Warning! visible_pt > tau.pt()' decay_vispt[0] = visible_pt decaytype.append(decaymode) # save muons and hadronic taus if decaymode == "muon": muons.append(daughter) elif decaymode == "tau": taush.append(tau) decay_tree.Fill() if (len(pair) == 2): inv = (pair[0] + pair[1]).mass() mother_inv[0] = inv mother_dphi_ll[0] = deltaPhi(pair[0].phi(), pair[1].phi()) mother_deta_ll[0] = pair[0].eta() - pair[1].eta() mother_dr_ll[0] = deltaR(pair[0].eta(), pair[0].phi(), pair[1].eta(), pair[1].phi()) if (len(muons) == 1 and len(taush) == 1): mother_dr_tm[0] = deltaR(taush[0].eta(), taush[0].phi(), muons[0].eta(), muons[0].phi()) else: mother_dr_tm[0] = -9 else:
hist_target_train = TH1F('TrainData','TrainData',100,0,300) hist_target_test = TH1F('TestData','TestData',100,0,300) hist_output_train = TH1F('OutputDataTrain','OutputDataTrain',100,0,300) hist_output_test = TH1F('OutputDataTest','OutputDataTest',100,0,300) tree_train = TTree('tree_train','tree_train') Ttrain = np.zeros(1, dtype=float) Otrain = np.zeros(1, dtype=float) tree_train.Branch('target_train',Ttrain,'target_train/D') tree_train.Branch('output_train',Otrain,'output_train/D') for ij1 in range(upper_limit): Ttrain[0] = target_train[ij1] Otrain[0] = predict_train[ij1] tree_train.Fill() tree_test = TTree('tree_test','tree_test') Ttest = np.zeros(1, dtype=float) Otest = np.zeros(1, dtype=float) tree_test.Branch('target_test',Ttest,'target_test/D') tree_test.Branch('output_test',Otest,'output_test/D') for ij2 in range(ARRAY.shape[0]-upper_limit-1): Ttest[0] = target_test[ij2] Otest[0] = predict_test[ij2] tree_test.Fill() #Plots: projection selon l'axe X (premiere variable) fill_hist(hist_target_train, target_train) fill_hist(hist_target_test, target_test)
v4 = numpy.zeros(1, dtype=float) v5 = numpy.zeros(1, dtype=float) v6 = numpy.zeros(1, dtype=float) v7 = numpy.zeros(1, dtype=float) v8 = numpy.zeros(1, dtype=float) d = numpy.zeros(1, dtype=float) tree2.Branch('v1', v1, 'v1/D') tree2.Branch('v2', v2, 'v2/D') tree2.Branch('v3', v3, 'v3/D') tree2.Branch('v4', v4, 'v4/D') tree2.Branch('v5', v5, 'v5/D') tree2.Branch('v6', v6, 'v6/D') tree2.Branch('v7', v7, 'v7/D') tree2.Branch('v8', v8, 'v8/D') tree2.Branch('disc', d, 'disc/D') for i in range(len(Y)): d[0] = predictions[i] v1[0] = X[i][0] v2[0] = X[i][1] v3[0] = X[i][2] v4[0] = X[i][3] v5[0] = X[i][4] v6[0] = X[i][5] v7[0] = X[i][6] v8[0] = X[i][7] tree2.Fill() f2.Write()
Py = rp.Unit().Y()*P0 Pz = rp.Unit().Z()*P0 E0 = ROOT.TMath.Sqrt(P0*P0+me2) p4 = TLorentzVector() p4.SetXYZM(Px,Py,Pz,me) wgt0 = 1 pdgId0 = 11 if(rnd.Uniform()>0.5) else -11 ### fill output vectors wgt.push_back(wgt0) pdgId.push_back(pdgId0) vx.push_back(vx0) vy.push_back(vy0) vz.push_back(vz0) px.push_back(p4.Px()) py.push_back(p4.Py()) pz.push_back(p4.Pz()) E.push_back(p4.E()) if(n%100==0): print("done %g out of %g" % (n,Nevt)) tt.Fill() tf.Write() tf.Write() tf.Close()
channel_dict_1["channel_counts_2_1"][0] = simpleDAQ.channel_counts_2 channel_dict_1["channel_counts_3_1"][0] = simpleDAQ.channel_counts_3 channel_dict_1["channel_counts_4_1"][0] = simpleDAQ.channel_counts_4 channel_dict_1["channel_counts_5_1"][0] = simpleDAQ.channel_counts_5 channel_dict_1["channel_counts_6_1"][0] = simpleDAQ.channel_counts_6 channel_dict_1["channel_counts_7_1"][0] = simpleDAQ.channel_counts_7 channel_dict_1["channel_counts_8_1"][0] = simpleDAQ.channel_counts_8 channel_dict_1["channel_counts_9_1"][0] = simpleDAQ.channel_counts_9 channel_dict_1["channel_counts_10_1"][0] = simpleDAQ.channel_counts_10 channel_dict_1["channel_counts_11_1"][0] = simpleDAQ.channel_counts_11 channel_dict_1["channel_counts_12_1"][0] = simpleDAQ.channel_counts_12 channel_dict_1["channel_counts_13_1"][0] = simpleDAQ.channel_counts_13 channel_dict_1["channel_counts_14_1"][0] = simpleDAQ.channel_counts_14 channel_dict_1["channel_counts_15_1"][0] = simpleDAQ.channel_counts_15 channel_dict_1["channel_counts_16_1"][0] = simpleDAQ.channel_counts_16 splitDAQ.Fill() elif (k == 2): channel_dict_2["channel_counts_1_2"][0] = simpleDAQ.channel_counts_1 channel_dict_2["channel_counts_2_2"][0] = simpleDAQ.channel_counts_2 channel_dict_2["channel_counts_3_2"][0] = simpleDAQ.channel_counts_3 channel_dict_2["channel_counts_4_2"][0] = simpleDAQ.channel_counts_4 channel_dict_2["channel_counts_5_2"][0] = simpleDAQ.channel_counts_5 channel_dict_2["channel_counts_6_2"][0] = simpleDAQ.channel_counts_6 channel_dict_2["channel_counts_7_2"][0] = simpleDAQ.channel_counts_7 channel_dict_2["channel_counts_8_2"][0] = simpleDAQ.channel_counts_8 channel_dict_2["channel_counts_9_2"][0] = simpleDAQ.channel_counts_9 channel_dict_2["channel_counts_10_2"][0] = simpleDAQ.channel_counts_10 channel_dict_2["channel_counts_11_2"][0] = simpleDAQ.channel_counts_11 channel_dict_2["channel_counts_12_2"][0] = simpleDAQ.channel_counts_12 channel_dict_2["channel_counts_13_2"][0] = simpleDAQ.channel_counts_13 channel_dict_2["channel_counts_14_2"][0] = simpleDAQ.channel_counts_14
class HECNoiseD3PDMaker(PyAthena.Alg): def __init__(self, name, **kw): PyAthena.Alg.__init__(self, name) self.name = name self.det = None self.ped = None ## cell cuts self.MinDigitADC = kw.get('MinDigitADC', 20) self.MaxDeltaT = kw.get('MaxDeltaT', 5) self.NtupleFileName = kw.get('NtupleFileName', 'HECNoiseD3PD.root') self.TriggerLines = kw.get('TriggerLines', [ 'L1_J5', 'L1_J10', 'L1_J12', 'L1_J30', 'L1_TAU5', 'L1_TAU8', 'L1_J5_EMPTY', 'L1_J10_EMPTY', 'L1_J12_EMPTY', 'L1_J30_EMPTY', 'L1_TAU5_EMPTY', 'L1_TAU8_EMPTY', 'L1_J5_FIRSTEMPTY', 'L1_J10_FIRSTEMPTY', 'L1_J12_FIRSTEMPTY', 'L1_J30_FIRSTEMPTY', 'L1_TAU5_FIRSTEMPTY', 'L1_TAU8_FIRSTEMPTY' ]) def initialize(self): print("==> initializing ", self.name) print("MinDigitADC: ", self.MinDigitADC) print("MaxDeltaT: ", self.MaxDeltaT) print("NtupleFileName: ", self.NtupleFileName) print("TriggerLines: ", self.TriggerLines) # self.sg = PyAthena.py_svc("StoreGateSvc") self.det = PyAthena.StoreGate.pointer("DetectorStore") self.LArOID = self.det.retrieve("LArOnlineID", "LArOnlineID") self.lcs = PyAthena.py_tool('LArCablingService') self.cdd = PyAthena.CaloDetDescrManager.instance() self.cid = self.cdd.getCaloCell_ID() self.tdt = PyAthena.py_tool('Trig::TrigDecisionTool/TrigDecisionTool') self.ntfile = TFile(self.NtupleFileName, "RECREATE") self.hectree = TTree("HECNoise", "HECNoise") self.iRun = array('i', [0]) self.iEvent = array('L', [0]) self.iEventCount = array('i', [0]) self.fPrescale = {} self.iTrigger = {} for tl in self.TriggerLines: self.fPrescale[tl] = array('f', [0.0]) self.iTrigger[tl] = array('i', [0]) pass self.iTime = array('i', [0]) self.iLB = array('i', [0]) self.iBCID = array('i', [0]) self.avgMu = array('f', [0.0]) self.actMu = array('f', [0.0]) self.iGain = array('i', [0]) self.iOID = array('L', [0]) self.iSide = array('i', [0]) self.iSamp = array('i', [0]) self.iReg = array('i', [0]) self.iEta = array('i', [0]) self.iPhi = array('i', [0]) self.iQuality = array('i', [0]) self.e = array('f', [0.0]) self.t = array('f', [0.0]) self.eta = array('f', [0.0]) self.phi = array('f', [0.0]) self.z = array('f', [0.0]) self.r = array('f', [0.0]) self.Ped = array('f', [0.0]) self.PedRMS = array('f', [0.0]) self.iDigi = array('i', 32 * [0]) self.iMax = array('i', [0]) self.iMin = array('i', [0]) # self.hectree.Branch("iRun", self.iRun, "iRun/I") self.hectree.Branch("iEvent", self.iEvent, "iEvent/I") self.hectree.Branch("iEventCount", self.iEventCount, "iEventCount/I") for tl in self.TriggerLines: self.hectree.Branch(tl + "_Prescale", self.fPrescale[tl], tl + "_Prescale/F") self.hectree.Branch(tl + "_Trigger", self.iTrigger[tl], tl + "_Trigger/I") pass self.hectree.Branch("iTime", self.iTime, "iTime/I") self.hectree.Branch("iLB", self.iLB, "iLB/I") self.hectree.Branch("iBCID", self.iBCID, "iBCID/I") self.hectree.Branch("avgMu", self.avgMu, "avgMu/F") self.hectree.Branch("actMu", self.actMu, "actMu/F") self.hectree.Branch("iGain", self.iGain, "iGain/I") self.hectree.Branch("iOID", self.iOID, "iOID/l") self.hectree.Branch("iSide", self.iSide, "iSide/I") self.hectree.Branch("iSamp", self.iSamp, "iSamp/I") self.hectree.Branch("iReg", self.iReg, "iReg/I") self.hectree.Branch("iEta", self.iEta, "iEta/I") self.hectree.Branch("iPhi", self.iPhi, "iPhi/I") self.hectree.Branch("iQuality", self.iQuality, "iQuality/I") self.hectree.Branch("e", self.e, "e/F") self.hectree.Branch("t", self.t, "t/F") self.hectree.Branch("eta", self.eta, "eta/F") self.hectree.Branch("phi", self.phi, "phi/F") self.hectree.Branch("z", self.z, "z/F") self.hectree.Branch("r", self.r, "r/F") self.hectree.Branch("Ped", self.Ped, "Ped/F") self.hectree.Branch("PedRMS", self.PedRMS, "PedRMS/F") self.hectree.Branch("iDigi", self.iDigi, "iDigi[32]/I") self.hectree.Branch("iMax", self.iMax, "iMax/I") self.hectree.Branch("iMin", self.iMin, "iMin/I") # return True def execute(self): #for some obscure reason, we need run dump before we can retrieve the flat objects using their abstract interface garbagedump = open(os.devnull, 'w') self.det.dump(garbagedump) garbagedump.close() self.ped = self.det.retrieve("ILArPedestal", "Pedestal") # filter low gain cells passedTrigger = False foundLowCell = False for tl in self.TriggerLines: self.fPrescale[tl][0] = self.tdt.getPrescale(tl) if self.tdt.isPassed(tl): passedTrigger = True self.iTrigger[tl][0] = 1 pass else: self.iTrigger[tl][0] = 0 pass pass if passedTrigger or not passedTrigger: # take all events with LG Cells self.iEventCount[0] = 0 ldc = self.sg.retrieve("LArDigitContainer", "LArDigitContainer_Thinned") for ld in ldc: hid = ld.hardwareID() if self.LArOID.isHECchannel(hid): # and ld.gain() == 2: sigmax = 0 sigmin = 0 imax = 0 imin = 0 isamp = 0 samp0 = ld.samples()[0] for samp in ld.samples(): if isamp < 32: self.iDigi[isamp] = samp pass if samp - samp0 > sigmax: sigmax = samp - samp0 imax = isamp pass if samp - samp0 < sigmin: sigmin = samp - samp0 imin = isamp pass isamp = isamp + 1 pass if sigmax > self.MinDigitADC and sigmin < -self.MinDigitADC and ( (imin - imax) < self.MaxDeltaT or imin < imax): foundLowCell = True ei = self.sg.retrieve("EventInfo", "ByteStreamEventInfo") cc = self.sg.retrieve("CaloCellContainer", "AllCalo") self.iRun[0] = ei.event_ID().run_number() self.iEvent[0] = ei.event_ID().event_number() self.iEventCount[0] = self.iEventCount[0] + 1 self.iTime[0] = ei.event_ID().time_stamp() self.iLB[0] = ei.event_ID().lumi_block() self.iBCID[0] = ei.event_ID().bunch_crossing_id() self.avgMu[0] = ei.averageInteractionsPerCrossing() self.actMu[0] = ei.actualInteractionsPerCrossing() self.iGain[0] = ld.gain() oid = self.lcs.cnvToIdentifier(hid) self.iOID[0] = ld.channelID().get_compact() self.Ped[0] = self.ped.pedestal( ld.channelID(), ld.gain()) self.PedRMS[0] = self.ped.pedestalRMS( ld.channelID(), ld.gain()) self.iSide[0] = self.cid.pos_neg(oid) self.iSamp[0] = self.cid.sampling(oid) self.iReg[0] = self.cid.region(oid) self.iEta[0] = self.cid.eta(oid) self.iPhi[0] = self.cid.phi(oid) self.iMax[0] = imax self.iMin[0] = imin ihash = self.cid.calo_cell_hash(oid) rcell = cc.findCell(ihash) self.e[0] = 0.0 self.t[0] = 0.0 self.iQuality[0] = 0 if rcell.ID() != oid: print("Cell iHash does not match ...") pass else: self.e[0] = rcell.e() self.t[0] = rcell.time() self.iQuality[0] = rcell.quality() pass cdde = self.cdd.get_element(oid) self.eta[0] = cdde.eta() self.phi[0] = cdde.phi() self.z[0] = cdde.z() self.r[0] = cdde.r() self.hectree.Fill() self.setFilterPassed(True) pass pass pass if foundLowCell: print('Event passed HECNoise Filter') return True pass print('Event failed HECNoise Filter') self.setFilterPassed(False) return True def finalize(self): self.ntfile.Write() self.ntfile.Close() return True
def create_thresholds(self, discrList, filename): self._logger.info('Getting thresholds...') def tolist(a): if isinstance(a, list): return a elif isinstance(a, tuple): return a else: return a.tolist() from TuningTools import TuningToolCores modelDict = {} modelDict['__version__'] = self._version modelDict['__type__'] = 'Hypo' modelDict['__core__'] = TuningToolCores.FastNet modelDict['metadata'] = { 'RemoveOutputTansigTF': self._removeOutputTansigTF, 'DoPileupCorrection': self._doPileupCorrection, 'LumiCut': self._maxPileupLinearCorrectionValue, } modelDict['tuning'] = [] for model in discrList: thresData = {} etBinIdx = model['etBinIdx'] etaBinIdx = model['etaBinIdx'] thresData['etBin'] = tolist(model['etBin']) thresData['etaBin'] = tolist(model['etaBin']) thresData['muBin'] = tolist(model['muBin']) thresData['thresholds'] = model['threshold'] modelDict['tuning'].append(thresData) if self._toPickle: self._logger.info('Export Thresholds to pickle format...') modelDict['__version__'] = self._version modelDict['__core__'] = TuningToolCores.keras from RingerCore import save save(modelDict, filename) self._logger.info('Export Thresholds to root format...') from ROOT import TFile, TTree from ROOT import std ### Create the thresholds root object fthres = TFile(appendToFileName(filename, '.root', separator=''), 'recreate') self.__createRootParameter('int', '__version__', self._version).Write() self.__createRootParameter('int', '__core__', TuningToolCores.FastNet).Write() fthres.mkdir('tuning') fthres.cd('tuning') tthres = TTree('thresholds', '') for idx, b in enumerate(self._thresBranches): b[2] = std.vector(b[0])() tthres.Branch(b[1], 'vector<%s>' % b[0], b[2]) for t in modelDict['tuning']: for idx, b in enumerate(self._thresBranches): self.__attachToVector(t[b[1]], b[2]) tthres.Fill() tthres.Write() fthres.mkdir('metadata') fthres.cd('metadata') for key, value in modelDict['metadata'].iteritems(): self._logger.info('Saving metadata %s as %s', key, value) self.__createRootParameter('int' if type(value) is int else 'bool', key, value).Write() fthres.Close()
def SplitTree(self): FileNameList = read_file_name_root(self._infile) BranchListAll = self.get_branch_list_all() BranchListEachTree = self.get_branch_list_each_tree() DicNumpyArray_branch = {} for numpyarray in BranchListAll: a = numpy.array([0], 'd') DicNumpyArray_branch[numpyarray] = a DicNumpyArray_branch = collections.OrderedDict( sorted(DicNumpyArray_branch.items())) print(DicNumpyArray_branch) DicNumpyArray_branch_w = {} for numpyarray_w in BranchListAll: a_w = numpy.array([0], 'd') DicNumpyArray_branch_w[numpyarray_w] = a_w DicNumpyArray_branch_w = collections.OrderedDict( sorted(DicNumpyArray_branch_w.items())) print(DicNumpyArray_branch_w) gBenchmark.Start("Regerating tree root") f = TFile(FileNameList[2], "READ") dirlist = f.GetListOfKeys() ITER = dirlist.MakeIterator() key = ITER.Next() outfileName = os.getcwd( ) + "/" + FileNameList[0] + "_cut_TTTL.root" #FIXME print("CREATING... :", outfileName) outfile = TFile(outfileName, "RECREATE") ijk = 0 break_flag = 0 while key: if (break_flag == 1): break break_flag += 1 tree = key.ReadObj() tree_f = TTree(tree.GetName() + "", tree.GetName() + "") ENTRY = tree.GetEntries() #print(ENTRY) for i in range(len(DicNumpyArray_branch)): if (list(DicNumpyArray_branch.keys())[i] in BranchListEachTree[tree.GetName()]): tree.SetBranchAddress( list(DicNumpyArray_branch.keys())[i], list(DicNumpyArray_branch.values())[i]) tree_f.Branch( list(DicNumpyArray_branch_w.keys())[i], list(DicNumpyArray_branch_w.values())[i], list(DicNumpyArray_branch_w.keys())[i] + "/D") else: continue print("for tree", tree.GetName()) for j in range(ENTRY): tree.GetEntry(j) if (j % 5000 == 0): print("now looping", j, "th Events, total of ", ENTRY, "events") for k in range(len(DicNumpyArray_branch)): if (list(DicNumpyArray_branch.keys())[k] in BranchListEachTree[tree.GetName()] ): ### FIXED MAYBE not correct.... pass else: continue list(DicNumpyArray_branch_w.values())[k][0] = list( DicNumpyArray_branch.values())[k][0] if (True #& (list(DicNumpyArray_branch.values())[0][0] == 1) # LL #FIXME & (list(DicNumpyArray_branch.values())[0][0] == 0 ) # TTTL #FIXME ): ijk = ijk + 1 tree_f.Fill() else: continue print(" Event left after filtering : ", ijk, "!!!!!!!!!") print("\n") ijk = 0 if (tree_f.GetEntries() == 0): print("!!!!!!! ", tree_f.GetName(), " is Empty, would not be written !!!!!") else: tree_f.Write() key = ITER.Next() print("") print("////////////////////////////////////////////////") print("outputfile : ") print(outfileName) print("////////////////////////////////////////////////") print("") outfile.Close() f.Close() print( "*********************************************************************************************" ) gBenchmark.Show("Regerating tree root") print( "*********************************************************************************************" ) return outfileName
def AnalyzeDataSet(): NEntries = ntuple.GetEntries() outfile = TFile(outfilename, 'RECREATE') outTree = TTree('outTree', 'tree branches') z_ratio = array('f', [0]) SubJet_csv_min = array('f', [0]) SubJet_csv_1 = array('f', [0]) SubJet_csv_2 = array('f', [0]) fn_csv = array('f', [0]) trackSipdSig_3 = array('f', [0]) trackSipdSig_2 = array('f', [0]) trackSipdSig_1 = array('f', [0]) trackSipdSig_0 = array('f', [0]) trackSipdSig_1_0 = array('f', [0]) trackSipdSig_0_0 = array('f', [0]) trackSipdSig_1_1 = array('f', [0]) trackSipdSig_0_1 = array('f', [0]) trackSip2dSigAboveCharm_0 = array('f', [0]) trackSip2dSigAboveBottom_0 = array('f', [0]) trackSip2dSigAboveBottom_1 = array('f', [0]) tau1_trackEtaRel_0 = array('f', [0]) tau1_trackEtaRel_1 = array('f', [0]) tau1_trackEtaRel_2 = array('f', [0]) tau0_trackEtaRel_0 = array('f', [0]) tau0_trackEtaRel_1 = array('f', [0]) tau0_trackEtaRel_2 = array('f', [0]) tau_vertexMass_0 = array('f', [0]) tau_vertexEnergyRatio_0 = array('f', [0]) tau_vertexDeltaR_0 = array('f', [0]) tau_flightDistance2dSig_0 = array('f', [0]) tau_vertexMass_1 = array('f', [0]) tau_vertexEnergyRatio_1 = array('f', [0]) tau_flightDistance2dSig_1 = array('f', [0]) jetNTracks = array('f', [0]) nSV = array('f', [0]) massPruned = array('f', [0]) flavour = array('f', [0]) nbHadrons = array('f', [0]) ptPruned = array('f', [0]) etaPruned = array('f', [0]) nEle = array('L', [0]) nMu = array('L', [0]) nTau = array('L', [0]) nPho = array('L', [0]) outTree.Branch('z_ratio', z_ratio, 'z_ratio/F') outTree.Branch('SubJet_csv_min', SubJet_csv_min, 'SubJet_csv_min/F') outTree.Branch('SubJet_csv_1', SubJet_csv_1, 'SubJet_csv_1/F') outTree.Branch('SubJet_csv_2', SubJet_csv_2, 'SubJet_csv_2/F') outTree.Branch('fn_csv', fn_csv, 'fn_csv/F') outTree.Branch('trackSipdSig_3', trackSipdSig_3, 'trackSipdSig_3/F') outTree.Branch('trackSipdSig_2', trackSipdSig_2, 'trackSipdSig_2/F') outTree.Branch('trackSipdSig_1', trackSipdSig_1, 'trackSipdSig_1/F') outTree.Branch('trackSipdSig_0', trackSipdSig_0, 'trackSipdSig_0/F') outTree.Branch('trackSipdSig_1_0', trackSipdSig_1_0, 'trackSipdSig_1_0/F') outTree.Branch('trackSipdSig_0_0', trackSipdSig_0_0, 'trackSipdSig_0_0/F') outTree.Branch('trackSipdSig_1_1', trackSipdSig_1_1, 'trackSipdSig_1_1/F') outTree.Branch('trackSipdSig_0_1', trackSipdSig_0_1, 'trackSipdSig_0_1/F') outTree.Branch('trackSip2dSigAboveCharm_0', trackSip2dSigAboveCharm_0, 'trackSip2dSigAboveCharm_0/F') outTree.Branch('trackSip2dSigAboveBottom_0', trackSip2dSigAboveBottom_0, 'trackSip2dSigAboveBottom_0/F') outTree.Branch('trackSip2dSigAboveBottom_1', trackSip2dSigAboveBottom_1, 'trackSip2dSigAboveBottom_1/F') outTree.Branch('tau1_trackEtaRel_0', tau1_trackEtaRel_0, 'tau1_trackEtaRel_0/F') outTree.Branch('tau1_trackEtaRel_1', tau1_trackEtaRel_1, 'tau1_trackEtaRel_1/F') outTree.Branch('tau1_trackEtaRel_2', tau1_trackEtaRel_2, 'tau1_trackEtaRel_2/F') outTree.Branch('tau0_trackEtaRel_0', tau0_trackEtaRel_0, 'tau0_trackEtaRel_0/F') outTree.Branch('tau0_trackEtaRel_1', tau0_trackEtaRel_1, 'tau0_trackEtaRel_1/F') outTree.Branch('tau0_trackEtaRel_2', tau0_trackEtaRel_2, 'tau0_trackEtaRel_2/F') outTree.Branch('tau_vertexMass_0', tau_vertexMass_0, 'tau_vertexMass_0/F') outTree.Branch('tau_vertexEnergyRatio_0', tau_vertexEnergyRatio_0, 'tau_vertexEnergyRatio_0/F') outTree.Branch('tau_vertexDeltaR_0', tau_vertexDeltaR_0, 'tau_vertexDeltaR_0/F') outTree.Branch('tau_flightDistance2dSig_0', tau_flightDistance2dSig_0, 'tau_flightDistance2dSig_0/F') outTree.Branch('tau_vertexMass_1', tau_vertexMass_1, 'tau_vertexMass_1/F') outTree.Branch('tau_vertexEnergyRatio_1', tau_vertexEnergyRatio_1, 'tau_vertexEnergyRatio_1/F') outTree.Branch('tau_flightDistance2dSig_1', tau_flightDistance2dSig_1, 'tau_flightDistance2dSig_1/F') outTree.Branch('jetNTracks', jetNTracks, 'jetNTracks/F') outTree.Branch('nSV', nSV, 'nSV/F') outTree.Branch('massPruned', massPruned, 'massPruned/F') outTree.Branch('flavour', flavour, 'flavour/F') outTree.Branch('nbHadrons', nbHadrons, 'nbHadrons/F') outTree.Branch('ptPruned', ptPruned, 'ptPruned/F') outTree.Branch('etaPruned', etaPruned, 'etaPruned/F') outTree.Branch('nEle', nEle, 'nEle/L') outTree.Branch('nMu', nMu, 'nMu/L') outTree.Branch('nTau', nTau, 'nTau/L') outTree.Branch('nPho', nPho, 'nPho/L') if len(sys.argv) > 2: NEntries = int(sys.argv[2]) print "WARNING: Running in TEST MODE" for ievent in range(NEntries): if ievent % 100 == 0: print "Processed %d of %d events..." % (ievent, NEntries) ntuple.GetEntry(ievent) #doublebtag = ntuple.__getattr__('CA15Puppi_doublebtag') CA15Puppi_z_ratio = ntuple.__getattr__('CA15Puppi_z_ratio') CA15PuppisubjetCSV = ntuple.__getattr__('CA15PuppisubjetSDCSV') CA15Puppi_SubJet_csv = ntuple.__getattr__('CA15Puppi_SubJet_csv') CA15Puppi_trackSipdSig_3 = ntuple.__getattr__( 'CA15Puppi_trackSipdSig_3') CA15Puppi_trackSipdSig_2 = ntuple.__getattr__( 'CA15Puppi_trackSipdSig_2') CA15Puppi_trackSipdSig_1 = ntuple.__getattr__( 'CA15Puppi_trackSipdSig_1') CA15Puppi_trackSipdSig_0 = ntuple.__getattr__( 'CA15Puppi_trackSipdSig_0') CA15Puppi_trackSipdSig_1_0 = ntuple.__getattr__( 'CA15Puppi_trackSipdSig_1_0') CA15Puppi_trackSipdSig_0_0 = ntuple.__getattr__( 'CA15Puppi_trackSipdSig_0_0') CA15Puppi_trackSipdSig_1_1 = ntuple.__getattr__( 'CA15Puppi_trackSipdSig_1_1') CA15Puppi_trackSipdSig_0_1 = ntuple.__getattr__( 'CA15Puppi_trackSipdSig_0_1') CA15Puppi_trackSip2dSigAboveCharm_0 = ntuple.__getattr__( 'CA15Puppi_trackSip2dSigAboveCharm_0') CA15Puppi_trackSip2dSigAboveBottom_0 = ntuple.__getattr__( 'CA15Puppi_trackSip2dSigAboveBottom_0') CA15Puppi_trackSip2dSigAboveBottom_1 = ntuple.__getattr__( 'CA15Puppi_trackSip2dSigAboveBottom_1') CA15Puppi_tau1_trackEtaRel_0 = ntuple.__getattr__( 'CA15Puppi_tau1_trackEtaRel_0') CA15Puppi_tau1_trackEtaRel_1 = ntuple.__getattr__( 'CA15Puppi_tau1_trackEtaRel_1') CA15Puppi_tau1_trackEtaRel_2 = ntuple.__getattr__( 'CA15Puppi_tau1_trackEtaRel_2') CA15Puppi_tau0_trackEtaRel_0 = ntuple.__getattr__( 'CA15Puppi_tau0_trackEtaRel_0') CA15Puppi_tau0_trackEtaRel_1 = ntuple.__getattr__( 'CA15Puppi_tau0_trackEtaRel_1') CA15Puppi_tau0_trackEtaRel_2 = ntuple.__getattr__( 'CA15Puppi_tau0_trackEtaRel_2') CA15Puppi_tau_vertexMass_0 = ntuple.__getattr__( 'CA15Puppi_tau_vertexMass_0') CA15Puppi_tau_vertexEnergyRatio_0 = ntuple.__getattr__( 'CA15Puppi_tau_vertexEnergyRatio_0') CA15Puppi_tau_vertexDeltaR_0 = ntuple.__getattr__( 'CA15Puppi_tau_vertexDeltaR_0') CA15Puppi_tau_flightDistance2dSig_0 = ntuple.__getattr__( 'CA15Puppi_tau_flightDistance2dSig_0') CA15Puppi_tau_vertexMass_1 = ntuple.__getattr__( 'CA15Puppi_tau_vertexMass_1') CA15Puppi_tau_vertexEnergyRatio_1 = ntuple.__getattr__( 'CA15Puppi_tau_vertexEnergyRatio_1') CA15Puppi_tau_flightDistance2dSig_1 = ntuple.__getattr__( 'CA15Puppi_tau_flightDistance2dSig_1') CA15Puppi_jetNTracks = ntuple.__getattr__('CA15Puppi_jetNTracks') CA15Puppi_nSV_ = ntuple.__getattr__('CA15Puppi_nSV_') CA15Puppi_massPruned = ntuple.__getattr__('CA15Puppi_massPruned') CA15Puppi_flavour = ntuple.__getattr__('CA15Puppi_flavour') CA15Puppi_nbHadrons = ntuple.__getattr__('CA15Puppi_nbHadrons') CA15Puppi_ptPruned = ntuple.__getattr__('CA15Puppi_ptPruned') CA15Puppi_etaPruned = ntuple.__getattr__('CA15Puppi_etaPruned') #Other variables eleIsPassLoose = ntuple.__getattr__('eleIsPassLoose') nElectron = ntuple.__getattr__('nEle') eleP4 = ntuple.__getattr__('eleP4') nPho_ = ntuple.__getattr__('nPho') phoP4 = ntuple.__getattr__('phoP4') phoIsPassLoose = ntuple.__getattr__('phoIsPassLoose') nTau_ = ntuple.__getattr__('HPSTau_n') tauP4 = ntuple.__getattr__('HPSTau_4Momentum') isDecayModeFinding = ntuple.__getattr__('disc_decayModeFinding') passLooseTauIso = ntuple.__getattr__( 'disc_byLooseIsolationMVA3oldDMwLT') nMu_ = ntuple.__getattr__('nMu') muP4 = ntuple.__getattr__('muP4') isLooseMuon = ntuple.__getattr__('isLooseMuon') muChHadIso = ntuple.__getattr__('muChHadIso') muNeHadIso = ntuple.__getattr__('muNeHadIso') muGamIso = ntuple.__getattr__('muGamIso') muPUPt = ntuple.__getattr__('muPUPt') muCharge = ntuple.__getattr__('muCharge') CA15jetcond = False # if len(CA15Puppi_ptPruned) > 0 & (170 < CA15Puppi_ptPruned < 1000) & (50 < CA15Puppi_massPruned < 200): # Subjets_0 = CA15PuppisubjetCSV[0] # if len(Subjets_0)==2: CA15jetcond = True # else:CA15jetcond = False if len(CA15Puppi_ptPruned) > 0 & (170 < CA15Puppi_ptPruned < 3000) & ( 50 < CA15Puppi_massPruned < 200): Subjets_0 = CA15PuppisubjetCSV[0] try: if len(Subjets_0 ) == 2: # & (Subjets_0[0] > 0) & (Subjets_0[1] > 0): print "subjets", len(Subjets_0) print("csv: ", Subjets_0[0], "and", Subjets_0[1]) new_csv = math.tanh( math.atanh(Subjets_0[0]) + math.atanh(Subjets_0[1])) print("new csv: ", new_csv) z_ratio[0] = CA15Puppi_z_ratio[0] SubJet_csv_min[0] = CA15Puppi_SubJet_csv[0] SubJet_csv_1[0] = Subjets_0[0] SubJet_csv_2[0] = Subjets_0[1] fn_csv[0] = new_csv trackSipdSig_3[0] = CA15Puppi_trackSipdSig_3[0] trackSipdSig_2[0] = CA15Puppi_trackSipdSig_2[0] trackSipdSig_1[0] = CA15Puppi_trackSipdSig_1[0] trackSipdSig_0[0] = CA15Puppi_trackSipdSig_0[0] trackSipdSig_1_0[0] = CA15Puppi_trackSipdSig_1_0[0] trackSipdSig_0_0[0] = CA15Puppi_trackSipdSig_0_0[0] trackSipdSig_1_1[0] = CA15Puppi_trackSipdSig_1_1[0] trackSipdSig_0_1[0] = CA15Puppi_trackSipdSig_0_1[0] trackSip2dSigAboveCharm_0[ 0] = CA15Puppi_trackSip2dSigAboveCharm_0[0] trackSip2dSigAboveBottom_0[ 0] = CA15Puppi_trackSip2dSigAboveBottom_0[0] trackSip2dSigAboveBottom_1[ 0] = CA15Puppi_trackSip2dSigAboveBottom_1[0] tau1_trackEtaRel_0[0] = CA15Puppi_tau1_trackEtaRel_0[0] tau1_trackEtaRel_1[0] = CA15Puppi_tau1_trackEtaRel_1[0] tau1_trackEtaRel_2[0] = CA15Puppi_tau1_trackEtaRel_2[0] tau0_trackEtaRel_0[0] = CA15Puppi_tau0_trackEtaRel_0[0] tau0_trackEtaRel_1[0] = CA15Puppi_tau0_trackEtaRel_1[0] tau0_trackEtaRel_2[0] = CA15Puppi_tau0_trackEtaRel_2[0] tau_vertexMass_0[0] = CA15Puppi_tau_vertexMass_0[0] tau_vertexEnergyRatio_0[ 0] = CA15Puppi_tau_vertexEnergyRatio_0[0] tau_vertexDeltaR_0[0] = CA15Puppi_tau_vertexDeltaR_0[0] tau_flightDistance2dSig_0[ 0] = CA15Puppi_tau_flightDistance2dSig_0[0] tau_vertexMass_1[0] = CA15Puppi_tau_vertexMass_1[0] tau_vertexEnergyRatio_1[ 0] = CA15Puppi_tau_vertexEnergyRatio_1[0] tau_flightDistance2dSig_1[ 0] = CA15Puppi_tau_flightDistance2dSig_1[0] jetNTracks[0] = CA15Puppi_jetNTracks[0] nSV[0] = CA15Puppi_nSV_[0] massPruned[0] = CA15Puppi_massPruned[0] flavour[0] = CA15Puppi_flavour[0] nbHadrons[0] = CA15Puppi_nbHadrons[0] ptPruned[0] = CA15Puppi_ptPruned[0] etaPruned[0] = CA15Puppi_etaPruned[0] myEles = [] for iele in range(nElectron): if (eleP4[iele].Pt() > 10.) & (abs( eleP4[iele].Eta()) < 2.5) & (bool( eleIsPassLoose[iele]) == True): myEles.append(iele) # print len(myEles) myMuos = [] for imu in range(nMu_): if (muP4[imu].Pt() > 10.) & (abs( muP4[imu].Eta()) < 2.4) & (bool( isLooseMuon[imu]) == True): relPFIso = (muChHadIso[imu] + max( 0., muNeHadIso[imu] + muGamIso[imu] - 0.5 * muPUPt[imu])) / muP4[imu].Pt() if relPFIso < 0.25: myMuos.append(imu) #print len(myMuos) myTaus = [] for itau in range(nTau_): if (tauP4[itau].Pt() > 18.) & (abs( tauP4[itau].Eta()) < 2.3) & (bool( isDecayModeFinding[itau]) == True) & (bool( passLooseTauIso[itau]) == True): myTaus.append(itau) #print len(myTaus) myPhos = [] for ipho in range(nPho_): if (phoP4[ipho].Pt() > 15.) & (abs( phoP4[ipho].Eta()) < 2.5) & (bool( phoIsPassLoose[ipho]) == True): myPhos.append(ipho) # print len(myPhos) #other variables nEle[0] = len(myEles) nMu[0] = len(myMuos) nTau[0] = len(myTaus) nPho[0] = len(myPhos) except: print "There is problem in csv value , skipping event" outTree.Fill() outfile.Write()
def main(): emin = 1. #infile = "/home/jaroslav/sim/lmon/data/luminosity/lm1ax2/lmon.root" #outfile = "/home/jaroslav/sim/lmon/data/luminosity/lm1ax2/hits.root" #infile = "/home/jaroslav/sim/lmon/data/luminosity/lm1b/lmon.root" #outfile = "/home/jaroslav/sim/lmon/data/luminosity/lm1b/hits.root" #infile = "/home/jaroslav/sim/lmon/data/luminosity/lm1c/lmon.root" #outfile = "/home/jaroslav/sim/lmon/data/luminosity/lm1c/hits.root" #input inp = TFile.Open(infile) tree = inp.Get("DetectorTree") #number of events, negative for all nev = -12 #input generated particles pdg = std.vector(int)() en = std.vector(float)() tree.SetBranchAddress("gen_pdg", pdg) tree.SetBranchAddress("gen_en", en) #spectrometer hits up_hits = ParticleCounterHits("up", tree) up_hits.ypos = 163.524 # mm down_hits = ParticleCounterHits("down", tree) down_hits.ypos = -163.524 # mm #photon detector hits phot_hits = ParticleCounterHits("phot", tree) #outputs out = TFile(outfile, "recreate") #interaction tree otree = TTree("event", "event") gen_en = c_double(0) up_en = c_double(0) down_en = c_double(0) is_spect = c_bool(0) phot_en = c_double(0) otree.Branch("gen_en", gen_en, "gen_en/D") otree.Branch("up_en", up_en, "up_en/D") otree.Branch("down_en", down_en, "down_en/D") otree.Branch("is_spect", is_spect, "is_spect/O") otree.Branch("phot_en", phot_en, "phot_en/D") #hit trees up_hits.CreateOutput("up") down_hits.CreateOutput("down") phot_hits.CreateOutput("phot") phot_hits.zpos = -37175 # mm #bunch crossing tree btree = TTree("bunch", "bunch") bun_ni = c_int(0) bun_up_en = c_double(0) bun_down_en = c_double(0) bun_phot_en = c_double(0) btree.Branch("bun_ni", bun_ni, "bun_ni/I") btree.Branch("bun_up_en", bun_up_en, "bun_up_en/D") btree.Branch("bun_down_en", bun_down_en, "bun_down_en/D") btree.Branch("bun_phot_en", bun_phot_en, "bun_phot_en/D") #Poisson distribution for bunch crossings lam = get_scale(1)["lambda"] print("Lambda:", lam) fPois = TF1( "Pois", "TMath::Power([0], Int_t(TMath::Floor(x)) )*TMath::Exp(-[0])/TMath::Factorial( Int_t(TMath::Floor(x)) )", 0, 12. * lam) fPois.SetParameter(0, lam) #print("Pois:", fPois.GetRandom()) #number of interactions in bunch crossing nI = int(TMath.Floor(fPois.GetRandom())) bun_ni.value = nI #interaction loop if nev < 0: nev = tree.GetEntries() iprint = int(nev / 12) for ievt in range(nev): tree.GetEntry(ievt) if ievt % iprint == 0 and ievt > 0: print("{0:.1f} %".format(100. * ievt / nev)) stdout.flush() gen_en.value = 0. up_en.value = 0. down_en.value = 0. is_spect.value = 0 phot_en.value = 0. #generated photon energy for imc in range(pdg.size()): if pdg.at(imc) == 22: gen_en.value = en.at(imc) #print(gen_en.value) #spectrometer hits for i in range(up_hits.GetN()): hit = up_hits.GetHit(i) hit.LocalY() up_en.value += hit.en up_hits.FillOutput() for i in range(down_hits.GetN()): hit = down_hits.GetHit(i) hit.LocalY() down_en.value += hit.en down_hits.FillOutput() #coincidence selection if up_en.value > emin and down_en.value > emin: is_spect.value = 1 #photon hits for i in range(phot_hits.GetN()): hit = phot_hits.GetHit(i) hit.GlobalToLocal() phot_en.value += hit.en phot_hits.FillOutput() otree.Fill() #bunch crossing if nI == 0: btree.Fill() nI = int(TMath.Floor(fPois.GetRandom())) bun_ni.value = nI bun_up_en.value = 0. bun_down_en.value = 0. bun_phot_en.value = 0. else: nI -= 1 bun_up_en.value += up_en.value bun_down_en.value += down_en.value bun_phot_en.value += phot_en.value #interaction loop otree.Write() up_hits.otree.Write() down_hits.otree.Write() phot_hits.otree.Write() btree.Write() out.Close() print("Hit analysis done")
tsig_in.GetEntry(i) pi_pT_array[0] = _pi_pT gamma_eT_array[0] = _gamma_eT nBjets_25_array[0] = _nBjets_25 lep_pT_array[0] = _lep_pT piRelIso_05_ch_array[0] = _piRelIso_05_ch MET_array[0] = _MET weight_array[0] = _weight Wmass_array[0] = _Wmass if isMuon: _BDT_output[0] = reader.EvaluateMVA("BDT_mu") else: _BDT_output[0] = reader.EvaluateMVA("BDT_ele") tsig_out.Fill() # Background: for i in range(0,tbkg_in.GetEntries()): tbkg_in.GetEntry(i) pi_pT_array[0] = _pi_pT gamma_eT_array[0] = _gamma_eT nBjets_25_array[0] = _nBjets_25 lep_pT_array[0] = _lep_pT piRelIso_05_ch_array[0] = _piRelIso_05_ch MET_array[0] = _MET weight_array[0] = _weight Wmass_array[0] = _Wmass if isMuon: _BDT_output[0] = reader.EvaluateMVA("BDT_mu") else:
def createsummaryplots(path=None, folder=None): if path == None: house = raw_input("Data in bb5 or Gif? ") folder = raw_input("Insert folder to study: ") user = raw_input("Who is it? (type Lorenzo, Natalia or bb5) ") if user == "Lorenzo": path = "/Users/lorenzo/Data_" + str( house ) + "/" + folder + "/HV/" #Changed folder: files in Data_bb5 were in DataBB5 2/5/2019 elif user == "Natalia": path = "/home/est/Escritorio/CERN/Data_" + str( house) + "/" + folder + "/HV/" #path = "/home/est/Escritorio/CERN/oldData/Data_"+str(house)+"/"+folder+"/HV/" elif user == "bb5": path = "bb5 path" else: print "Name not found" ID = folder[0:18] timeslot = folder[19:len(folder)] #rootfile = TFile("/Users/lorenzo/Desktop/MMresults/"+folder+".root","RECREATE") rootfile = TFile(folder + ".root", "RECREATE") #create root file in same directory as pdf dir_L1 = rootfile.mkdir("Layer1/") dir_L2 = rootfile.mkdir("Layer2/") dir_L3 = rootfile.mkdir("Layer3/") dir_L4 = rootfile.mkdir("Layer4/") dir_summary = rootfile.mkdir("Summary/") global directories directories = {"L1": dir_L1, "L2": dir_L2, "L3": dir_L3, "L4": dir_L4} chambertype = folder[0:3] defaultlayersSM1 = ["L1S", "L2S", "L3S", "L4S"] defaultlayersSM2 = ["L1S", "L2S", "L3S", "L4S"] if chambertype == "SM1": defaultlayers = defaultlayersSM1 if chambertype == "SM2": defaultlayers = defaultlayersSM2 if chambertype == "LM1": defaultlayers = defaultlayersSM1 if chambertype == "LM2": defaultlayers = defaultlayersSM2 #for summary plots spikenames = [] sectorscurrents = [] sectorsvoltages = [] meancurrents = [] meanvoltages = [] newspikeseconds = [] graphscurrent = [] for dat_file in glob.iglob(path + '*.dat'): print "Analyzing: " + dat_file[len(path):len(dat_file)] + " \n" filename = dat_file[len(path):len(dat_file) - 4] layer = filename[5:7] if "D" not in layer and "HO" not in layer: rootdirectory = directories[layer] graphcurrent, spikeslayer, duration, sectorsvoltage, meanvoltage, sectorscurrent, meancurrent, spikeseconds = createplot( dat_file, dat_file[len(path):len(dat_file) - 4]) if spikeseconds != None: newspikeseconds = newspikeseconds + spikeseconds if spikeslayer != None: spikenames = spikenames + spikeslayer if duration != None: deltatime = duration if sectorscurrent != None: sectorscurrents.append(sectorscurrent) if meancurrent != None: meancurrents.append(meancurrent) if sectorsvoltage != None: sectorsvoltages.append(sectorsvoltage) if meanvoltage != None: meanvoltages.append(meanvoltage) if graphcurrent != None: graphscurrent.append(graphcurrent) #tools.write_roothistogram(newspikeseconds, "Spike time distribution", "t (s)", "Entries", dir_summary) tools.write_rootgraph( range(len(meancurrents)), meancurrents, "i " + str(round(float(deltatime) / float(3600), 2)) + " hours", "sector", "i", sectorscurrents, dir_summary) tools.write_rootgraph( range(len(meanvoltages)), meanvoltages, "HV " + str(round(float(deltatime) / float(3600), 2)) + " hours", "sector", "v", sectorsvoltages, dir_summary) tools.write_spikeroothistogram(spikenames, "spikes", "spikes/min", dir_summary, deltatime) vectorspikes = [x[5:len(x)] for x in spikenames] spikerate = [] spikelayers = list(set(vectorspikes)) for i in spikelayers: spikerate.append(vectorspikes.count(i)) for sector in sectorsvoltages: if sector not in spikelayers: spikelayers.append(sector) spikerate.append(0) orderedspikerate = [] for sector in sectorsvoltages: orderedspikerate.append(spikerate[spikelayers.index(sector)]) orderedspikerate = [ float(x / float((deltatime / 60.))) for x in orderedspikerate ] orderedsectorsvoltages = [] orderedsmeanvoltages = [] ordered_orderedspikerate = [] ordered_graphscurrent = [] print sectorsvoltages for layer in defaultlayers: #put in order voltages index = sectorsvoltages.index(layer) orderedsectorsvoltages.append(sectorsvoltages[index]) orderedsmeanvoltages.append(meanvoltages[index]) ordered_orderedspikerate.append(orderedspikerate[index]) graph_current_names = [] for graph in graphscurrent: graph_current_names.append(graph.name) for layer in defaultlayers: index = graph_current_names.index(layer) ordered_graphscurrent.append(graphscurrent[index]) for counter, graph in enumerate( ordered_graphscurrent ): #+" "+str(int(orderedsmeanvoltages[counter]))# tools.write_orderedrootdategraph_datadw( graph.rootdates, graph.newvalues, graph.filename + " " + str(int(orderedsmeanvoltages[counter])), "time (s)", graph.filename[0], rootdirectory) #create trees for current values for counter, graph in enumerate(ordered_graphscurrent): tree = TTree(ordered_graphscurrent[counter].filename, "tree") newvalue = array('f', [0]) branch = tree.Branch(ordered_graphscurrent[counter].filename, newvalue, "newvalue/F") for i in range(len(graph.newvalues)): newvalue[0] = ordered_graphscurrent[counter].newvalues[i] tree.Fill() tree.Write() eff, layers_eff, total_eff = efficiency.efficiency_values( orderedsmeanvoltages, chambertype) return orderedsectorsvoltages, orderedsmeanvoltages, ordered_orderedspikerate, ID, timeslot, deltatime, eff, layers_eff, total_eff
def convertGENSIM(infiles, outfilename, Nmax=-1, isPythia=False): """Loop over GENSIM events and save custom trees.""" start1 = time.time() lqids = [46] if isPythia else [9000002, 9000006] print ">>> loading files..." events = Events(infiles) outfile = TFile(outfilename, 'RECREATE') print ">>> creating trees and branches..." tree_event = TTree('event', 'event') tree_jet = TTree('jet', 'jet') tree_mother = TTree('mother', 'mother') tree_decay = TTree('decay', 'decay') tree_assoc = TTree('assoc', 'assoc') # EVENT tree_event.addBranch('nbgen', 'i') tree_event.addBranch('nbcut', 'i') tree_event.addBranch('ntgen', 'i') tree_event.addBranch('njet', 'i') tree_event.addBranch('nlepton', 'i') tree_event.addBranch('ntau', 'i') tree_event.addBranch('ntaucut', 'i') tree_event.addBranch('nnu', 'i') tree_event.addBranch('nlq', 'i') tree_event.addBranch('ntau_assoc', 'i') tree_event.addBranch('ntau_decay', 'i') tree_event.addBranch('nbgen_decay', 'i') tree_event.addBranch('met', 'f') tree_event.addBranch('jpt1', 'f') tree_event.addBranch('jpt2', 'f') tree_event.addBranch('sumjet', 'f') tree_event.addBranch('dphi_jj', 'f') tree_event.addBranch('deta_jj', 'f') tree_event.addBranch('dr_jj', 'f') tree_event.addBranch('ncentral', 'i') tree_event.addBranch('mjj', 'f') tree_event.addBranch('lq1_mass', 'f') tree_event.addBranch('lq2_mass', 'f') tree_event.addBranch('lq1_pt', 'f') tree_event.addBranch('lq2_pt', 'f') tree_event.addBranch('tau1_pt', 'f') tree_event.addBranch('tau1_eta', 'f') tree_event.addBranch('tau2_pt', 'f') tree_event.addBranch('tau2_eta', 'f') tree_event.addBranch('st', 'f') # scalar sum pT tree_event.addBranch('st_met', 'f') # scalar sum pT with MET tree_event.addBranch('weight', 'f') # LQ DECAY tree_mother.addBranch('pid', 'i') tree_mother.addBranch('moth', 'i') tree_mother.addBranch('status', 'i') tree_mother.addBranch('pt', 'f') tree_mother.addBranch('eta', 'f') tree_mother.addBranch('phi', 'f') tree_mother.addBranch('mass', 'f') tree_mother.addBranch('inv', 'f') tree_mother.addBranch('ndau', 'i') tree_mother.addBranch('dau', 'i') tree_mother.addBranch('dphi_ll', 'f') tree_mother.addBranch('deta_ll', 'f') tree_mother.addBranch('dr_ll', 'f') tree_mother.addBranch('st', 'f') # scalar sum pT tree_mother.addBranch('st_met', 'f') # scalar sum pT with MET tree_mother.addBranch('weight', 'f') # FROM LQ DECAY tree_decay.addBranch('pid', 'i') tree_decay.addBranch('pt', 'f') tree_decay.addBranch('eta', 'f') tree_decay.addBranch('phi', 'f') tree_decay.addBranch('lq_mass', 'f') tree_decay.addBranch('ptvis', 'f') tree_decay.addBranch('type', 'i') tree_decay.addBranch('isBrem', 'i') tree_decay.addBranch('weight', 'f') # NOT FROM LQ DECAY (ASSOCIATED) tree_assoc.addBranch('pid', 'i') tree_assoc.addBranch('moth', 'i') tree_assoc.addBranch('pt', 'f') tree_assoc.addBranch('ptvis', 'f') tree_assoc.addBranch('eta', 'f') tree_assoc.addBranch('phi', 'f') tree_assoc.addBranch('weight', 'f') # JETS tree_jet.addBranch('pt', 'f') tree_jet.addBranch('eta', 'f') tree_jet.addBranch('phi', 'f') tree_jet.addBranch('weight', 'f') hist_LQ_decay = TH1F('LQ_decay', "LQ decay", 60, -30, 30) handle_gps, label_gps = Handle( 'std::vector<reco::GenParticle>'), 'genParticles' handle_jets, label_jets = Handle('std::vector<reco::GenJet>'), 'ak4GenJets' handle_met, label_met = Handle('vector<reco::GenMET>'), 'genMetTrue' handle_weight, label_weight = Handle('GenEventInfoProduct'), 'generator' evtid = 0 sec_per_evt = 0.023 # seconds per event Ntot = Nmax if Nmax > 0 else events.size() step = stepsize(Ntot) print ">>> start processing %d events, ETA %s..." % ( Ntot, formatTimeShort(sec_per_evt * Ntot)) start_proc = time.time() # LOOP OVER EVENTS for event in events: #print '='*30 #print evtid if Nmax > 0 and evtid >= Nmax: break if evtid > 0 and evtid % step == 0: print ">>> processed %4s/%d events, ETA %s" % ( evtid, Ntot, ETA(start_proc, evtid + 1, Ntot)) evtid += 1 event.getByLabel(label_gps, handle_gps) gps = handle_gps.product() event.getByLabel(label_jets, handle_jets) jets = handle_jets.product() event.getByLabel(label_met, handle_met) met = handle_met.product() event.getByLabel(label_weight, handle_weight) gweight = handle_weight.product() weight = gweight.weight() # GEN PARTICLES gps_mother = [p for p in gps if isFinal(p) and abs(p.pdgId()) in [42]] gps_final = [ p for p in gps if isFinal(p) and abs(p.pdgId()) in [5, 6, 15, 16] + lqids ] gps_mother = [ p for p in gps_final if abs(p.pdgId()) in lqids and p.status() > 60 ] #not(moth.numberOfDaughters()==2 and abs(moth.daughter(0).pdgId()) in lqids) gps_bgen = [ p for p in gps_final if abs(p.pdgId()) == 5 and p.status() == 71 ] gps_bcut = [p for p in gps_bgen if p.pt() > 20 and abs(p.eta()) < 2.5] gps_tgen = [p for p in gps_final if abs(p.pdgId()) == 6] #[-1:] gps_nugen = [p for p in gps_final if abs(p.pdgId()) == 16] gps_tau = [ p for p in gps_final if abs(p.pdgId()) == 15 and p.status() == 2 ] gps_tau.sort(key=lambda p: p.pt(), reverse=True) gps_taucut = [p for p in gps_tau if p.pt() > 20 and abs(p.eta()) < 2.5] #print '-'*10 #for p in gps_tgen: # printParticle(p) #if gps_tgen: # print "has top" #for p in gps_nugen: # printParticle(p) # REMOVE TOP QUARK if its final daughter is also in the list for top in gps_tgen[:]: dau = top while abs(dau.daughter(0).pdgId()) == 6: dau = dau.daughter(0) if dau != top and dau in gps_tgen: gps_tgen.remove(top) # REMOVE JET-LEPTON OVERLAP jets, dummy = cleanObjectCollection(jets, gps_tau, dRmin=0.5) njets = 0 sumjet = 0 jets30 = [] for jet in jets: if jet.pt() > 30 and abs(jet.eta()) < 5: sumjet += jet.pt() njets += 1 tree_jet.pt[0] = jet.pt() tree_jet.eta[0] = jet.eta() tree_jet.phi[0] = jet.phi() tree_jet.weight[0] = weight tree_jet.Fill() jets30.append(jet) # MULTIPLICITIES tree_event.nlq[0] = len(gps_mother) tree_event.nbcut[0] = len(gps_bcut) tree_event.nbgen[0] = len(gps_bgen) tree_event.ntgen[0] = len(gps_tgen) tree_event.njet[0] = njets tree_event.nlepton[0] = len(gps_tau) tree_event.ntau[0] = len(gps_tau) tree_event.ntaucut[0] = len(gps_taucut) tree_event.nnu[0] = len(gps_nugen) # JETS tree_event.met[0] = met[0].pt() tree_event.sumjet[0] = sumjet if len(jets30) >= 2: centrajpt1s = findCentrajpt1s(jets30[:2], jets30[2:]) tree_event.ncentral[0] = len(centrajpt1s) else: tree_event.ncentral[0] = -9 if (len(jets30) >= 2): tree_event.jpt1[0] = jets30[0].pt() tree_event.jpt2[0] = jets30[1].pt() tree_event.dphi_jj[0] = deltaPhi(jets30[0].phi(), jets30[1].phi()) tree_event.deta_jj[0] = jets30[0].eta() - jets30[1].eta() tree_event.dr_jj[0] = deltaR(jets30[0].eta(), jets30[0].phi(), jets30[1].eta(), jets30[1].phi()) dijetp4 = jets30[0].p4() + jets30[1].p4() tree_event.mjj[0] = dijetp4.M() elif (len(jets30) == 1): tree_event.jpt1[0] = jets30[0].pt() tree_event.jpt2[0] = -1 tree_event.dphi_jj[0] = -9 tree_event.deta_jj[0] = -9 tree_event.dr_jj[0] = -1 tree_event.mjj[0] = -1 else: tree_event.jpt1[0] = -1 tree_event.jpt2[0] = -1 tree_event.dphi_jj[0] = -9 tree_event.deta_jj[0] = -9 tree_event.dr_jj[0] = -1 tree_event.mjj[0] = -1 # SCALAR SUM PT if len(gps_taucut) >= 2 and len(gps_bcut) >= 1: st = 0 #gps_taucut.sort(key=lambda p: p.pt(), reverse=True) gps_bcut.sort(key=lambda p: p.pt(), reverse=True) #taus_assoc.sort(key=lambda p: p.pt(), reverse=True) #taus_decay.sort(key=lambda p: p.pt(), reverse=True) #bgen_decay.sort(key=lambda p: p.pt(), reverse=True) for part in gps_taucut[2:] + gps_bcut[1:]: st += part.pt() stmet = st + met[0].pt() else: st = -1 stmet = -1 tree_event.tau1_pt[0] = gps_tau[0].pt() tree_event.tau1_eta[0] = gps_tau[0].eta() tree_event.tau2_pt[0] = gps_tau[1].pt() tree_event.tau2_eta[0] = gps_tau[1].eta() tree_event.st[0] = st tree_event.st_met[0] = stmet tree_mother.st[0] = st tree_mother.st_met[0] = stmet tree_event.weight[0] = weight #print 'len, gps_mother = ', len(gps_mother) #if len(gps_mother)==1: # print gps_mother[0].pdgId(), gps_mother[0].status(), gps_mother[0].pt(), gps_mother[0].eta(), gps_mother[0].phi() # print '1 (ndaughter, daughter pdgid) =', gps_mother[0].numberOfDaughters(), gps_mother[0].daughter(0).pdgId(), '(pdgId, status, pt, eta, phi) = ', gps_mother[0].pdgId(), gps_mother[0].status(), gps_mother[0].pt(), gps_mother[0].eta(), gps_mother[0].phi() #if len(gps_mother)>=2: # print '2 (ndaughter, daughter 1/2 pdgid) =', gps_mother[0].numberOfDaughters(), gps_mother[0].daughter(0).pdgId(), gps_mother[0].daughter(1).pdgId(), '(pdgId, status, pt, eta, phi) = ', gps_mother[0].pdgId(), gps_mother[0].status(), gps_mother[0].pt(), gps_mother[0].eta(), gps_mother[0].phi() # print '2 (ndaughter, daughter 1/2 pdgid) =', gps_mother[1].numberOfDaughters(), gps_mother[1].daughter(0).pdgId(), gps_mother[1].daughter(1).pdgId(), '(pdgId, status, pt, eta, phi) = ', gps_mother[1].pdgId(), gps_mother[1].status(), gps_mother[1].pt(), gps_mother[1].eta(), gps_mother[1].phi() # TAU taus_assoc = [] for gentau in gps_tau: while gentau.status() != 2: gentau = gentau.daughter(0) genfinDaughters = finalDaughters(gentau, []) genptvis = p4sumvis(genfinDaughters).pt() # CHECK MOTHER taumoth = gentau.mother(0) mothpid = abs(taumoth.pdgId()) from_LQ = False #from_had = False # from hadron decay #print '-'*30 while mothpid != 2212: #print taumoth.pdgId() if mothpid in lqids: from_LQ = True break elif 100 < mothpid < 10000: #and mothpid!=2212: #from_had = True break taumoth = taumoth.mother(0) mothpid = abs(taumoth.pdgId()) # ASSOC if not from_LQ: tree_assoc.pt[0] = gentau.pt() tree_assoc.ptvis[0] = genptvis tree_assoc.eta[0] = gentau.eta() tree_assoc.phi[0] = gentau.phi() tree_assoc.pid[0] = gentau.pdgId() tree_assoc.moth[0] = taumoth.pdgId() tree_assoc.weight[0] = weight tree_assoc.Fill() #if not from_had: taus_assoc.append(gentau) # B QUARK for genb in gps_bgen: bmoth = genb.mother(0) mothpid = abs(bmoth.pdgId()) from_LQ = False while mothpid != 2212: if mothpid in lqids: from_LQ = True break bmoth = bmoth.mother(0) mothpid = abs(bmoth.pdgId()) if not from_LQ: tree_assoc.pt[0] = genb.pt() tree_assoc.ptvis[0] = -1 tree_assoc.eta[0] = genb.eta() tree_assoc.phi[0] = genb.phi() tree_assoc.pid[0] = genb.pdgId() tree_assoc.moth[0] = bmoth.pdgId() tree_assoc.weight[0] = weight tree_assoc.Fill() # MOTHER LQ #print '-'*80 taus_decay = [] bgen_decay = [] gps_mother.sort(key=lambda p: p.pt(), reverse=True) for moth in gps_mother: dau_pid = 0 pair = [] if moth.numberOfDaughters() == 2: if moth.daughter(0).pdgId() in [ 21, 22 ] or moth.daughter(1).pdgId() in [21, 22]: continue if abs(moth.daughter(0).pdgId() ) in lqids: # single production with t-channel LQ continue lq_moth = moth.mother(0) while abs(lq_moth.pdgId()) in lqids: lq_moth = lq_moth.mother(0) for i in range(moth.numberOfDaughters()): #print '\t', dau.pdgId() dau = moth.daughter(i) # TAU isBrem = False if abs(dau.pdgId()) == 15: while dau.status() != 2: dau = dau.daughter(0) if dau.numberOfDaughters() == 2 and abs( dau.daughter(0).pdgId()) == 15 and dau.daughter( 1).pdgId() == 22: #print "This is brems !?!" isBrem = True else: taus_decay.append(dau) # BOTTOM QUARK elif abs(dau.pdgId()) == 5: dau_pid = dau.pdgId() bgen_decay.append(dau) # TOP QUARK elif abs(dau.pdgId()) == 6: dau_pid = dau.pdgId() newdau = dau while abs(newdau.daughter(0).pdgId()) == 6: newdau = newdau.daughter(0) if isFinal(newdau): dau = newdau pair.append(dau.p4()) tree_decay.lq_mass[0] = moth.mass() tree_decay.pid[0] = dau.pdgId() tree_decay.pt[0] = dau.pt() tree_decay.eta[0] = dau.eta() tree_decay.phi[0] = dau.phi() tree_decay.isBrem[0] = isBrem if abs(dau.pdgId()) == 15: finDaughters = finalDaughters(dau, []) ptvis = p4sumvis(finDaughters).pt() tree_decay.ptvis[0] = ptvis decaymode = tauDecayMode(dau) tree_decay.type[0] = decaydict[decaymode] #print decaymode, 'vis pt = ', ptvis , 'tau pt = ', dau.pt() if ptvis > dau.pt(): print "%s, vis pt = %s, tau pt = %s " % ( decaymode, ptvis, dau.pt()) + '!' * 30 else: tree_decay.ptvis[0] = dau.pt() tree_decay.type[0] = -1 tree_decay.weight[0] = weight tree_decay.Fill() if abs(moth.pdgId()) in lqids: hist_LQ_decay.Fill(dau.pdgId()) if len(pair) == 2: tree_mother.inv[0] = (pair[0] + pair[1]).mass() tree_mother.dphi_ll[0] = deltaPhi(pair[0].phi(), pair[1].phi()) tree_mother.deta_ll[0] = pair[0].eta() - pair[1].eta() tree_mother.dr_ll[0] = deltaR(pair[0].eta(), pair[0].phi(), pair[1].eta(), pair[1].phi()) else: tree_mother.inv[0] = -1 tree_mother.dphi_ll[0] = -99 tree_mother.deta_ll[0] = -99 tree_mother.dr_ll[0] = -99 tree_mother.pid[0] = moth.pdgId() tree_mother.moth[0] = lq_moth.pdgId() tree_mother.status[0] = moth.status() tree_mother.mass[0] = moth.mass() tree_mother.pt[0] = moth.pt() tree_mother.eta[0] = moth.eta() tree_mother.phi[0] = moth.phi() tree_mother.ndau[0] = len(pair) tree_mother.dau[0] = dau_pid # save PDG ID for quark daughter tree_mother.weight[0] = weight tree_mother.Fill() if len(gps_mother) == 1: tree_event.lq1_mass[0] = gps_mother[0].mass() tree_event.lq1_pt[0] = gps_mother[0].pt() tree_event.lq2_mass[0] = -1 tree_event.lq2_pt[0] = -1 elif len(gps_mother) >= 2: tree_event.lq1_mass[0] = gps_mother[0].mass() tree_event.lq1_pt[0] = gps_mother[0].pt() tree_event.lq2_mass[0] = gps_mother[1].mass() tree_event.lq2_pt[0] = gps_mother[1].pt() else: tree_event.lq1_mass[0] = -1 tree_event.lq1_pt[0] = -1 tree_event.lq2_mass[0] = -1 tree_event.lq2_pt[0] = -1 tree_event.ntau_assoc[0] = len(taus_assoc) tree_event.ntau_decay[0] = len(taus_decay) tree_event.nbgen_decay[0] = len(bgen_decay) tree_event.Fill() print ">>> processed %4s events in %s" % ( evtid, formatTime(time.time() - start_proc)) print ">>> writing to output file %s..." % (outfilename) outfile.Write() outfile.Close() print ">>> done in in %s" % (formatTime(time.time() - start1))
def createplot(file, filename): layer = filename[5:7] if "D" not in layer and "HO" not in layer: rootdirectory = directories[layer] #to check times = [x.split(' ')[0] for x in open(file, "r").readlines()] if len(times) == 1: print "Exception -----> Only one data in " + str(filename) + ".dat \n" return None, None, None, None, None, None, None, None if not times: print "Exception -----> File empty: " + str(filename) + ".dat \n" return None, None, None, None, None, None, None, None times = [x.replace(':', ' ') for x in times] times = [x.replace('/', ' ') for x in times] times = [x.replace('_', ' ') for x in times] times = [dt.strptime(x, '%m %d %Y %H %M %S') for x in times] starttime = times[0] dates = [starttime] times = [int((x - starttime).total_seconds()) for x in times] values = [float(x.split(' ')[1]) for x in open(file, "r").readlines()] newtimes = range(times[len(times) - 1]) newvalues = [None] * len(newtimes) for counter, value in enumerate(newvalues): if counter in times: newvalues[counter] = values[times.index(counter)] else: newvalues[counter] = newvalues[counter - 1] for counter in range(len(newtimes) - 1): dates.append(dates[counter] + td(seconds=1)) rootdates = [ TDatime(x.year, x.month, x.day, x.hour, x.minute, x.second) for x in dates ] #Identify drops valuesdeltas = np.diff(newvalues) valuesdeltas = [0] + valuesdeltas sectorscurrent = None sectorsvoltage = None meancurrent = None nospike_meancurrent = None meanvoltage = None notrips_meanvoltage = None if "i" in filename: #it's a current file #search.findrisingedges(valuesdeltas, dates) #search.findfallingedges(valuesdeltas, dates) sectorscurrent = filename[5:9] meancurrent = np.mean(newvalues) #remove spikes in current files copynewvalues = copy.copy( newvalues) #need to copy it to pass to function below nospike_newvalues = search.removespikes(valuesdeltas, copynewvalues) nospike_meancurrent = np.mean( nospike_newvalues) #used to have real baseline of the current #or meancurrent spikecounter, filename, spikedates, spikeseconds, spikenames = search.findspikes_50na( newvalues, meancurrent, dates, newtimes, filename) #spikecounter, filename, spikedates, spikeseconds, spikenames = search.findspikes(valuesdeltas, dates, newtimes, filename) #old one if "D" in filename: sectorscurrent = None nospike_meancurrent = None if "HO" in filename: sectorscurrent = None nospike_meancurrent = None if "v" in filename: #it's a voltage file sectorsvoltage = filename[5:9] meanvoltage = np.mean(newvalues) copynewvalues = copy.copy(newvalues) notrips_newvalues = search.removetrips(valuesdeltas, copynewvalues) notrips_meanvoltage = np.mean(notrips_newvalues) if "D" in filename: sectorsvoltage = None notrips_meanvoltage = None if "HO" in filename: sectorscurrent = None nospike_meancurrent = None #tools.write_roothistogram(newvalues, filename, filename[0], "Entries", rootdirectory) #if want additional histograms if "D" not in filename and "HO" not in filename: tools.write_rootdategraph(rootdates, newvalues, filename, "time (s)", filename[0], rootdirectory) currentgraph = classes.currentgraph(filename[5:9], rootdates, newvalues, filename, "time (s)", filename[0], rootdirectory) #create trees for voltages branch if "v" in filename: tree = TTree(filename, "tree") newvalue = array('f', [0]) branch = tree.Branch(filename, newvalue, "newvalue/F") for i in range(len(newvalues)): newvalue[0] = newvalues[i] tree.Fill() tree.Write() duration = len(newtimes) #total seconds from start to stop if "i" not in filename or "D" in filename or "HO" in filename: spikenames = None duration = None spikeseconds = None currentgraph = None return currentgraph, spikenames, duration, sectorsvoltage, notrips_meanvoltage, sectorscurrent, nospike_meancurrent, spikeseconds
noise = np.random.normal(0, 1, size=[batch, rand_dim]) y = np.ones(batch) y = y.astype(int) GAN.fit(noise, y, nb_epoch=12, batch_size=int(batch / 10), verbose=1) #g_loss = GAN.train_on_batch(x=noise, y=y) #print 'Generator_loss :', g_loss, ', Discriminator_loss :', d_loss print 'Training done ................' sample_size = 10000 print 'Making noise for sampling ................' noise = np.random.normal(0, 1, size=[sample_size, rand_dim]) print '. ................' sample = Gen.predict_on_batch(noise) print 'done .................' for i in range(len(sample)): ang = sample[i] Theta1[0] = float(ang[0]) Theta2[0] = float(ang[1]) ThetaStar[0] = float(ang[2]) Phi[0] = float(ang[3]) Phi1[0] = float(ang[4]) tr.Fill() f.Write() f.Close()
def wd2tree(fndata="wave0.txt", fnout=None, isASCII=True, hasHeader=True, rLen=1024, dType='H'): # open parser pWD = WDParser(fndata, isASCII, hasHeader, rLen, dType) # open output TFile if not fnout: fnout = fndata[:fndata.index('.')] + '.root' froot = TFile(fnout, "RECREATE") ## Get one event, parser length of waveform event = pWD.get_one_event() rLen = event[0][0] ## TTree Structure tree = TTree("wavedump", "WaveDump pulses") # header pLen = array('L', [0]) pBoardID = array('L', [0]) pChannel = array('L', [0]) pEventID = array('L', [0]) pPattern = array('L', [0]) pTimeStamp = array('L', [0]) pDCoffset = array('L', [0]) # data pLen[0] = rLen pData = array('H', pLen[0] * [0]) # branchs tree.Branch("nLen", pLen, "nLen/i") tree.Branch("nBoardID", pBoardID, "nBoardID/i") tree.Branch("nChannel", pChannel, "nChannel/i") tree.Branch("nPattern", pPattern, "nPattern/i") tree.Branch("nEventID", pEventID, "nEventID/i") tree.Branch("nTimeStamp", pTimeStamp, "nTimeStamp/i") tree.Branch("nDAC", pDCoffset, "nDAC/i") tree.Branch("WAVE", pData, "WAVE[nLen]/s") ## Fill while event: # header pLen[0] = event[0][0] pBoardID[0] = event[0][1] pChannel[0] = event[0][2] pPattern[0] = event[0][3] pEventID[0] = event[0][4] pTimeStamp[0] = event[0][5] pDCoffset[0] = event[0][6] for i in range(event[0][0]): pData[i] = event[1][i] tree.Fill() # get next event event = pWD.get_one_event() ## pWD.close() froot.Write() froot.Close()
def mix(path_in, path_out, mode, ecms, patch, sample): omega = array('d', 3 * [999.]) D1_2420_path = './txts/xs_D1_2420_' + patch + '.txt' with open(D1_2420_path, 'r') as f: for line in f.readlines(): if '#' in line: line = line.strip('#') try: fargs = map(float, line.strip().strip('\n').split()) if fargs[0] == ecms: omega[0] = fargs[4] except: ''' ''' DDPIPI_path = './txts/xs_DDPIPI_' + patch + '.txt' with open(DDPIPI_path, 'r') as f: for line in f.readlines(): if '#' in line: line = line.strip('#') try: fargs = map(float, line.strip().strip('\n').split()) if fargs[0] == ecms: omega[1] = fargs[4] except: ''' ''' psipp_path = './txts/xs_psipp_' + patch + '.txt' with open(psipp_path, 'r') as f: for line in f.readlines(): if '#' in line: line = line.strip('#') try: fargs = map(float, line.strip().strip('\n').split()) if fargs[0] == ecms: omega[2] = fargs[4] except: ''' ''' tot = 0 for i in xrange(len(omega)): tot += omega[i] for i in xrange(len(omega)): omega[i] = omega[i] / tot n = 0 f_out = TFile(path_out[0], 'RECREATE') t_out = TTree('save', 'save') if sample == 'raw': m_rawm_D = array('d', [999.]) if sample == 'raw': t_out.Branch('rawm_D', m_rawm_D, 'm_rawm_D/D') for i in xrange(len(path_in)): try: f_in = TFile(path_in[i]) t_in = f_in.Get('save') entries = t_in.GetEntries() logging.info(mode[i] + ' entries :' + str(entries)) except: logging.error(path_in[i] + ' is invalid!') sys.exit() print '--> Begin to process file: ' + path_in[i] nentries = t_in.GetEntries() for ientry in xrange(int(nentries * omega[n])): t_in.GetEntry(ientry) if t_in.m_rawm_D > 0.: if sample == 'raw': m_rawm_D[0] = t_in.m_rawm_D t_out.Fill() n += 1 f_out.cd() t_out.Write() f_out.Close() print '--> End of processing file: ' + path_out[0]
class ComputeMVA(object): """Perform a multivariate analysis based on a simple algorithm. This MVA algorithm, thanks to its simplicity, provides you with a quite fast MVA which doesn't suffer of overtraining. The algorithm needs a background and a signal tree, and a list of variables that are going to be used in the computation. Basically the program calculates, for each event (both signal and background), the normalized number of background events that have all the values of the varaibles larger or equal than the event (that is the number of background events that look like signal more than the considered event). This is the generalization of the p-value in a given number of dimensions, wrt to the background distribution. This variable is the discriminating variable between signal and background, and can be transformed in a flat variable for bg and a variable peaked at 0 for signal. You can find a few examples in the __main__ part. """ def __init__(self, variables_list, background_tree_training, signal_tree_training, n_max=1e10, verbosity=0): self.variables_list = variables_list self.background_tree_training = background_tree_training self.signal_tree_training = signal_tree_training self.n_max = n_max self.verbosity = verbosity if self.verbosity >= 1: print "### INIT ###" print print "background entries", self.background_tree_training.GetEntries( ) print "signal entries", self.signal_tree_training.GetEntries() print self.variables_signs = [] def get_mean(tree, variable): tree.Draw(variable + ">>" + variable) h = gDirectory.Get(variable) return h.GetMean() if self.verbosity >= 1: print print "deciding sign looking at the means" print for variable in self.variables_list: mean_background = get_mean(self.background_tree_training, variable) mean_signal = get_mean(self.signal_tree_training, variable) if mean_background <= mean_signal: self.variables_signs.append(1) else: self.variables_signs.append(-1) if self.verbosity >= 1: print variable print "mean_background", mean_background print "mean_signal", mean_signal print "sign", self.variables_signs[-1] self.background_histograms = [] self.signal_histograms = [] self.graphs = [] self.canvases = [] self.legendes = [] TopMassStyle() def ComputePvalue(self, point): string_list = [] for variable, variable_sign, point_component in zip( self.variables_list, self.variables_signs, point): if variable_sign == 1: sign = ">=" elif variable_sign == -1: sign = "<=" string_list.append(variable + sign + str(point_component)) join_cuts = " && " cut_string = join_cuts.join(string_list) if self.verbosity >= 2: print print "final cut_string" print cut_string print pvalue = float( self.background_tree_training.GetEntries(cut_string) )\ / self.background_tree_training.GetEntries() if self.verbosity >= 1: print pvalue return pvalue def Training(self): self.discriminating_tree = TTree("discriminating_tree", "discriminating_tree") self.event_type = numpy.zeros( 1, dtype=int, ) self.discriminating_tree.Branch('event_type', self.event_type, 'event_type/I') self.value = numpy.zeros( 1, dtype=float, ) self.discriminating_tree.Branch('value', self.value, 'value/D') for i, event in enumerate(self.signal_tree_training): if i > self.n_max: continue self.event_type[0] = 1 signal_values = [] for variable in self.variables_list: signal_values.append(getattr(event, variable)) self.value[0] = self.ComputePvalue(signal_values) self.discriminating_tree.Fill() for i, event in enumerate(self.background_tree_training): if i > self.n_max: continue self.event_type[0] = 0 background_values = [] for variable in self.variables_list: background_values.append(getattr(event, variable)) self.value[0] = self.ComputePvalue(background_values) self.discriminating_tree.Fill() def Transform(self): self.transformed_discriminating_tree = TTree( "transformed_discriminating_tree", "transformed_discriminating_tree") self.transformed_event_type = numpy.zeros( 1, dtype=int, ) self.transformed_discriminating_tree.Branch( 'event_type', self.transformed_event_type, 'event_type/I') self.transformed_value = numpy.zeros( 1, dtype=float, ) self.transformed_discriminating_tree.Branch('value', self.transformed_value, 'value/D') for i, event in enumerate(self.discriminating_tree): if i > self.n_max: continue if getattr(event, "event_type") == 1: self.transformed_event_type[0] = 1 else: self.transformed_event_type[0] = 0 cut_string = "event_type == 0 && " + "value <= {}".format( getattr(event, "value")) self.transformed_value[0] = float( self.discriminating_tree.GetEntries(cut_string) ) / self.discriminating_tree.GetEntries("event_type == 0") self.transformed_discriminating_tree.Fill() def RocCurve(self, tree, name, title, n_points=100): graph = TGraph() graph.SetNameTitle("g_roc_" + name, "ROC curve " + title) cuts = numpy.linspace(0, 20, n_points) for i, cut_value in enumerate(cuts): cut_string = "value <= {cv}".format(cv=10**(-cut_value)) eff_signal = float( tree.GetEntries("event_type == 1 && " + cut_string) ) / tree.GetEntries("event_type == 1") eff_bg = float(tree.GetEntries("event_type == 0 && " + cut_string) ) / tree.GetEntries("event_type == 0") graph.SetPoint(i, eff_signal, 1. - eff_bg) graph.GetXaxis().SetTitle("Signal efficiency") graph.GetYaxis().SetTitle("1 - Background efficiency") graph.SetMarkerStyle(3) graph.SetMarkerColor(2) canvas = TCanvas("canvas_roc_" + name, "Canvas ROC " + title, 800, 600) canvas.SetGrid() canvas.cd() graph.Draw("alp") self.graphs.append(graph) self.canvases.append(canvas) def Draw(self, tree, name, title, xtitle, n_bin=100): h_background = TH1D("h_background_" + name, title, n_bin, 0., 1.001) tree.Project("h_background_" + name, "value", "event_type == 0") h_background.SetLineColor(2) h_background.SetStats(kFALSE) h_signal = TH1D("h_signal_" + name, title, n_bin, 0., 1.001) tree.Project("h_signal_" + name, "value", "event_type == 1") h_signal.SetLineColor(4) h_signal.SetStats(kFALSE) h_signal.GetXaxis().SetTitle(xtitle) h_signal.GetYaxis().SetTitle("Normalized entries / {:3.2f} ".format( h_signal.GetBinWidth(1))) h_background.Scale(1. / h_background.Integral()) h_signal.Scale(1. / h_signal.Integral()) legend = TLegend(0.7, 0.6, 0.9, 0.8) legend.AddEntry(h_background, "background", "l") legend.AddEntry(h_signal, "signal", "l") canvas = TCanvas("canvas_" + name, title, 800, 600) canvas.SetGrid() canvas.cd() h_signal.Draw() h_background.Draw("same") legend.Draw("same") self.background_histograms.append(h_background) self.signal_histograms.append(h_signal) self.legendes.append(legend) self.canvases.append(canvas) def DrawRawVariables(self): self.raw_variables_canvases = [] self.raw_var_background_histo = [] self.raw_var_signal_histo = [] self.raw_var_legendes = [] for variable in self.variables_list: raw_variable_canvas = TCanvas("c_" + variable, variable, 800, 600) raw_variable_canvas.cd() raw_variable_canvas.SetGrid() self.raw_variables_canvases.append(raw_variable_canvas) h_background = TH1D("h_background_" + variable, variable, 30, 0., 15) self.background_tree_training.Project("h_background_" + variable, variable) h_background.SetLineColor(2) h_background.SetStats(kFALSE) h_signal = TH1D("h_signal_" + variable, variable, 30, 0., 15) self.signal_tree_training.Project("h_signal_" + variable, variable) h_signal.SetLineColor(4) h_signal.SetStats(kFALSE) h_signal.GetXaxis().SetTitle(variable) h_signal.GetYaxis().SetTitle( "Normalized entries / {:3.2f} ".format( h_signal.GetBinWidth(1))) h_background.Scale(1. / h_background.Integral()) h_signal.Scale(1. / h_signal.Integral()) legend = TLegend(0.7, 0.6, 0.9, 0.8) legend.AddEntry(h_background, "background", "l") legend.AddEntry(h_signal, "signal", "l") h_signal.Draw() h_background.Draw("same") legend.Draw("same") self.raw_var_background_histo.append(h_background) self.raw_var_signal_histo.append(h_signal) self.raw_var_legendes.append(legend) def Perform(self): self.Training() self.Draw(self.discriminating_tree, "before_transf", "Separation of the two classes before transforming", "combined variable", 100) #self.RocCurve(self.discriminating_tree, "before_transf", "before transforming") self.Transform() self.Draw(self.transformed_discriminating_tree, "after_transf", "Separation of the two classes after transforming", "combined variable", 100) self.RocCurve(self.transformed_discriminating_tree, "after_transf", "")