def _project(tree, var, selection='', weight=1.0, bins=None, includeover=False): h = None if var.count(':') == 0: ## Hist (1D) if bins: if isinstance(bins, tuple): assert len(bins) == 3 h = Hist(*bins) elif isinstance(bins, list): h = Hist(bins) else: assert False else: assert False elif var.count(':') == 1: ## Hist2D ## like rootpy, we use a convention where var=x:y, unlike ROOT varx, vary = var.split(':') var = ':'.join([vary, varx]) if bins: if isinstance(bins, tuple): assert len(bins) == 6 h = Hist2D(*bins) elif isinstance(bins, list): ## TODO: support variable bins for Hist2D h = Hist2D(*bins) #assert False else: assert False else: assert False else: assert False assert h # kwargs['hist'] = h ## add the weight to the selection via a TCut weighted_selection = str(selection) if weight and weight != 1.0: weighted_selection = Cut(weighted_selection) weighted_selection = weighted_selection * weight weighted_selection = str(weighted_selection) tree.Draw('%s>>%s' % (var, h.GetName()), weighted_selection) ## print tree.GetSelectedRows() ## debuging # for event in t: # x = getattr(event, var) # h.fill(x) if h: h.SetDirectory(0) # elist = ROOT.gDirectory.Get('elist') # elist.Print('all') if var.count(':') == 0 and includeover: ## include overflow for Hist (1D) nbins = h.GetNbinsX() c1 = h.GetBinContent(nbins) c2 = h.GetBinContent(nbins + 1) e1 = h.GetBinError(nbins) e2 = h.GetBinError(nbins + 1) h.SetBinContent(nbins, c1 + c2) h.SetBinError( nbins, math.sqrt((c1 * e1 * e1 + c2 * e2 * e2) / (c1 + c2)) if c1 + c2 != 0.0 else 0.0) h.SetBinContent(nbins + 1, 0.0) h.SetBinError(nbins + 1, 0.0) return h
trigger_hist = Hist(nbins, 0, sci_trigger_r_obj.gps_time_length, type='D', name='trigger_hist', title='trigger: { ' + gps_time_span + ' }') modules_hist = [ Hist(nbins, 0, sci_trigger_r_obj.gps_time_length, type='D', name='module_hist_' + str(i), title='module CT_' + str(i) + ': { ' + gps_time_span + ' }') for i in xrange(1, 26) ] trigger_hist.SetDirectory(None) trigger_hist.SetMinimum(0) trigger_hist.color = 'red' trigger_hist.markerstyle = 3 for i in xrange(25): modules_hist[i].SetDirectory(None) modules_hist[i].SetMinimum(0) modules_hist[i].color = 'blue' modules_hist[i].markerstyle = 3 print ' - reading data ... ' for i in tqdm( xrange(sci_trigger_r_obj.begin_entry, sci_trigger_r_obj.end_entry)): sci_trigger_r_obj.t_trigger.get_entry(i) if not sci_trigger_r_obj.t_trigger.abs_gps_valid: continue trigger_hist.fill((sci_trigger_r_obj.t_trigger.abs_gps_week -
tree = Tree("test") branches = {'x': 'F', 'y': 'F', 'z': 'F', 'i': 'I'} tree.create_branches(branches) for i in range(10000): tree.x = gauss(.5, 1.) tree.y = gauss(.3, 2.) tree.z = gauss(13., 42.) tree.i = i tree.fill() # Make a histogram of x when y > 1 hist1 = Hist(100, -10, 10, name='hist1') tree.Draw('x', 'y > 1', hist=hist1) hist1.SetDirectory(0) # memory resident print("The first tree has {0:f} entries where y > 1".format(hist1.Integral())) tree.write() f.close() print("Creating test tree in chaintest2.root") f = root_open("chaintest2.root", "recreate") tree = Tree("test") tree.create_branches(branches) for i in range(10000): tree.x = gauss(.5, 1.) tree.y = gauss(.3, 2.) tree.z = gauss(13., 42.)
def do_analysis(chain, analyze_this, outfile, eventtype, dt_this): # get trees from files #t = f.Get("Delphes") # get number of entries nentries = chain.GetEntries() # initialize everything here before filling histograms if (analyze_this['Jet.PT']): print "\nInitializing Jet.PT...\n" # create the histograms numJets = Hist(NBINS, NLO, NHI, title='numJets ' + eventtype, legendstyle='L') numJets.SetDirectory(0) # interesting values to plot max_jetpt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Max JetPT/Event ' + eventtype, legendstyle='L') max_jetpt_per_event.SetDirectory(0) min_jetpt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Min JetPT/Event ' + eventtype, legendstyle='L') min_jetpt_per_event.SetDirectory(0) else: print "Skipped Jet.PT" if (analyze_this['Jet.BTag']): print "Initializing Jet.BTag...\n" # create the histograms loose = Hist(NBINS, NLO, NHI, title='loose ' + eventtype, legendstyle='L') loose.SetDirectory(0) medium = Hist(NBINS, NLO, NHI, title='medium ' + eventtype, legendstyle='L') medium.SetDirectory(0) tight = Hist(NBINS, NLO, NHI, title='tight ' + eventtype, legendstyle='L') tight.SetDirectory(0) else: print "Skipped Jet.BTag" if (analyze_this['Electron.PT']): print "Initializing Electron.PT...\n" # create the histograms numElectrons = Hist(NBINS, NLO, NHI, title='numElectrons ' + eventtype, legendstyle='L') numElectrons.SetDirectory(0) # interesting values to plot max_ept_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Max ElectronPT/Event ' + eventtype, legendstyle='L') max_ept_per_event.SetDirectory(0) min_ept_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Min ElectronPT/Event ' + eventtype, legendstyle='L') min_ept_per_event.SetDirectory(0) # initialize any variable-specific constants here: # counter for no electrons noeleaf = 0 else: print "Skipped Electron.PT" if (analyze_this['MuonTight.PT']): print "Initializing MuonTight.PT...\n" # create the histograms numMuons = Hist(NBINS, NLO, NHI, title='numMuons ' + eventtype, legendstyle='L') numMuons.SetDirectory(0) # interesting values to plot max_upt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Max MuonPT/Event ' + eventtype, legendstyle='L') max_upt_per_event.SetDirectory(0) min_upt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Min MuonPT/Event ' + eventtype, legendstyle='L') min_upt_per_event.SetDirectory(0) # initialize any variable-specific constants here: # counter for no electrons nouleaf = 0 else: print "Skipped MuonTight.PT" if (analyze_this['MissingET.MET']): print "Initializing MissingET.MET...\n" # create the histograms MET = Hist(MET_NBINS, MET_NLO, MET_NHI, title='MET ' + eventtype, legendstyle='L') MET.SetDirectory(0) else: print "Skipped MissingET.MET" if (analyze_this['MT (NON-LEAF)']): print "Initializing MT...\n" # create the histograms MT = Hist(MT_NBINS, MT_NLO, MT_NHI, title='MT ' + eventtype, legendstyle='L') MT.SetDirectory(0) else: print "Skipped HT" if (analyze_this['HT (NON-LEAF)']): print "Initializing HT...\n" # create the histograms HT = Hist(HT_NBINS, HT_NLO, HT_NHI, title='HT ' + eventtype, legendstyle='L') HT.SetDirectory(0) else: print "Skipped HT" if (analyze_this['DELTA PHI (NON-LEAF)']): print "Initializing Delta Phi...\n" # create the histograms DPHI_metlep = Hist(30, -1 * np.pi, np.pi, title='Delta Phi (MET-lep) ' + eventtype, legendstyle='L') DPHI_metlep.SetDirectory(0) DPHI_metjet = Hist(30, -1 * np.pi, np.pi, title='Delta Phi (MET-jet)' + eventtype, legendstyle='L') DPHI_metjet.SetDirectory(0) else: print "Skipped DELTA PHI" if (analyze_this['DELTA R (NON-LEAF)']): print "Initializing Delta R...\n" #create histograms DR = Hist(50, 0, 2 * np.pi, title='DELTA R ' + eventtype, legendstyle='L') DR.SetDirectory(0) else: print "Skipped DELTA R" # Now fill histograms print "\nFilling histograms...\n" # get float version of num entries to normalize below; subtract 1 to get # actual integral value of hist norm = float(nentries - 1) for e in range(nentries): entry = chain.GetEntry(e) # to check whether each entry is electron or muon is_electron = False is_muon = False e_maxpt = 0 e_maxpt_phi = 0 u_maxpt = 0 u_maxpt_phi = 0 jet_maxpt = 0 jet_maxpt_phi = 0 jet_maxpt_eta = 0 met = 0 met_phi = 0 met_eta = 0 lepton_vec = [] if (analyze_this['Electron.PT']): # define leaves var = "Electron.PT" leaf = chain.GetLeaf(var) phileaf = chain.GetLeaf('Electron.Phi') etaleaf = chain.GetLeaf('Electron.Eta') # returns phi of max pt for entry (e_maxpt, e_maxpt_phi, e_maxpt_eta, e_nume) = fill_Electron_hist(chain, leaf, entry, numElectrons, min_ept_per_event, max_ept_per_event, phileaf, etaleaf, lepton_vec) #print lepton_vec if (leaf.GetLen() == 0): noeleaf += 1 e_maxpt = INVALID e_maxpt_phi = INVALID e_maxpt_eta = INVALID else: is_electron = True if (analyze_this['MuonTight.PT']): # define leaves var = "MuonTight.PT" leaf = chain.GetLeaf(var) phileaf = chain.GetLeaf('MuonTight.Phi') etaleaf = chain.GetLeaf('MuonTight.Eta') (u_maxpt, u_maxpt_phi, u_maxpt_eta, u_numu) = fill_Muon_hist(chain, leaf, entry, numMuons, min_upt_per_event, max_upt_per_event, phileaf, etaleaf, lepton_vec) if leaf.GetLen() == 0: nouleaf += 1 u_maxpt = INVALID u_maxpt_phi = INVALID u_maxpt_eta = INVALID else: is_muon = True # Get preferred lepton for future calcs if e_maxpt >= u_maxpt: lpt = e_maxpt lphi = e_maxpt_phi leta = e_maxpt_eta else: lpt = u_maxpt lphi = u_maxpt_phi leta = u_maxpt_eta # fill dt values for leptons dt_this['numLeptons'].append(e_nume + u_numu) dt_this['maxlpt'].append(lpt) if (analyze_this['Jet.PT']): # define leaves var = 'Jet.PT' leaf = chain.GetLeaf(var) phileaf = chain.GetLeaf('Jet.Phi') etaleaf = chain.GetLeaf('Jet.Eta') # analyze with Jet.PT because HT is sum of Jet.PTs if (analyze_this['HT (NON-LEAF)']): HTfill = True else: HTfill = False # returns phi of max pt for entry (jet_maxpt, jet_maxpt_phi, jet_maxpt_eta, jet_minpt, jet_numjets, jet_ht) = fill_JetPT_hist(chain, leaf, entry, numJets, min_jetpt_per_event, max_jetpt_per_event, HTfill, HT, phileaf, etaleaf, lepton_vec) if (leaf.GetLen() == 0): jet_maxpt = INVALID jet_maxpt_phi = INVALID jet_maxpt_eta = INVALID dt_this['numJets'].append(jet_numjets) dt_this['maxjetpt'].append(jet_maxpt) dt_this['minjetpt'].append(jet_minpt) dt_this['HT'].append(jet_ht) if (analyze_this['Jet.BTag']): # define leaves var = "Jet.BTag" leaf = chain.GetLeaf(var) btag_medium = fill_JetBTag_hist(chain, leaf, entry, loose, medium, tight) dt_this['medium'].append(btag_medium) if (analyze_this['MissingET.MET']): # define leaves var = "MissingET.MET" leaf = chain.GetLeaf(var) phileaf = chain.GetLeaf('MissingET.Phi') etaleaf = chain.GetLeaf('MissingET.Eta') (met, metphi, meteta) = fill_MET_hist(chain, leaf, entry, MET, phileaf, etaleaf) dt_this['MET'].append(met) if (analyze_this['MT (NON-LEAF)']): #print "ok got here" #print "here ",e_maxpt, e_maxpt_phi, u_maxpt, u_maxpt_phi, met, metphi if (not (is_muon or is_electron)): mt_val = 0 else: #print e_maxpt, e_maxpt_phi, u_maxpt, u_maxpt_phi, met, metphi #print is_electron, is_muon mt_val = get_mt(lpt, lphi, met, metphi) if mt_val == 0: mtfin = MT.Fill(INVALID) else: mtfin = MT.Fill(mt_val) dt_this['MT'].append(mtfin) if (analyze_this['DELTA PHI (NON-LEAF)']): dphi_metlep_val = delta_phi(metphi, lphi) dphi_metjet_val = delta_phi(metphi, jet_maxpt_phi) DPHI_metlep.Fill(dphi_metlep_val) DPHI_metjet.Fill(dphi_metjet_val) dt_this['DPHI_metlep'].append(dphi_metlep_val) dt_this['DPHI_metjet'].append(dphi_metjet_val) if (analyze_this['DELTA R (NON-LEAF)']): dr_val = delta_R(jet_maxpt_phi, lphi, jet_maxpt_eta, leta) if dr_val == 0: dr_val = INVALID #elif dr_val > 3.0 and dr_val < 3.3: #print jet_maxpt_phi, lphi, jet_maxpt_eta, leta DR.Fill(dr_val) dt_this['DR_lepjet'].append(dr_val) if (analyze_this['Jet.PT']): # normalize numJets.Scale(1 / (numJets.Integral())) max_jetpt_per_event.Scale(1 / (max_jetpt_per_event.Integral())) min_jetpt_per_event.Scale(1 / (min_jetpt_per_event.Integral())) if (analyze_this['Jet.BTag']): # normalize tight.Scale(1 / (tight.Integral())) medium.Scale(1 / (medium.Integral())) loose.Scale(1 / (loose.Integral())) if (analyze_this['Electron.PT']): # normalize numElectrons.Scale(1 / (numElectrons.Integral())) max_ept_per_event.Scale(1 / (max_ept_per_event.Integral())) min_ept_per_event.Scale(1 / (min_ept_per_event.Integral())) print "\nentries: " + str(nentries) + " noeleaf number: " + str( noeleaf) if (analyze_this['MuonTight.PT']): # normalize numMuons.Scale(1 / (numMuons.Integral())) max_upt_per_event.Scale(1 / (max_upt_per_event.Integral())) min_upt_per_event.Scale(1 / (min_upt_per_event.Integral())) print "\nentries: " + str(nentries) + " nouleaf number: " + str( nouleaf) if (analyze_this['MissingET.MET']): # normalize MET.Scale(1 / (MET.Integral())) if (analyze_this['MT (NON-LEAF)']): #normalize MT.Scale(1 / (MT.Integral())) if (analyze_this['HT (NON-LEAF)']): #normalize HT.Scale(1 / (HT.Integral())) if (analyze_this['DELTA PHI (NON-LEAF)']): #normalize DPHI_metlep.Scale(1 / (DPHI_metlep.Integral())) DPHI_metjet.Scale(1 / (DPHI_metjet.Integral())) if (analyze_this['DELTA R (NON-LEAF)']): #normalize DR.Scale(1 / (DR.Integral())) print "" print "\nDone!\n" numJets.Write(eventtype + "numJets") max_jetpt_per_event.Write(eventtype + "max_jetpt_per_event") min_jetpt_per_event.Write(eventtype + "min_jetpt_per_event") loose.Write(eventtype + "loose") medium.Write(eventtype + "medium") tight.Write(eventtype + "tight") numElectrons.Write(eventtype + "numElectrons") max_ept_per_event.Write(eventtype + "max_ept_per_event") min_ept_per_event.Write(eventtype + "min_ept_per_event") numMuons.Write(eventtype + "numMuons") max_upt_per_event.Write(eventtype + "max_upt_per_event") min_upt_per_event.Write(eventtype + "min_upt_per_event") MET.Write(eventtype + "MET") MT.Write(eventtype + "MT") HT.Write(eventtype + "HT") DPHI_metlep.Write(eventtype + "dphi_metlep") DPHI_metjet.Write(eventtype + "dphi_metjet") DR.Write(eventtype + "deltaR")