def getEfficiencyForPtCut(listMatchedPairs, ptCut): global efficiencyCounter efficiencyCounter += 1 effObj = TEfficiency("efficiency" + str(efficiencyCounter), "Efficiency for p_{T,cut} = " + str(ptCut) + " GeV", 251, -0.5, 250.5) ROOT.SetOwnership(effObj, False) truePtValues = range(0, 200) countPassed = [0] * 200 countTotal = [0] * 200 yVal = [0] * 200 xErr = [0.5] * 200 yErr = [0] * 200 for event in listMatchedPairs: for pair in event: effObj.Fill(pair[0].pt >= ptCut, pair[1].pt) countTotal[int(pair[1].pt)] += 1 if (pair[0].pt >= ptCut): countPassed[int(pair[1].pt)] += 1 for i, val in enumerate(countPassed): if (countTotal[i] > 0): yVal[i] = float(countPassed[i] / countTotal[i]) yErr[i] = float(math.sqrt(yVal[i] * (1 - yVal[i]) / countTotal[i])) #print countPassed #print countTotal c = TCanvas("can", "vas", 1200, 1200) #effObj.Draw() return c, effObj
def trackmatch(): garanainit.init() #ROOT.gROOT.SetBatch(True) # run in batch mode (don't display plots) tm = ROOT.TreeManager("./test.root") #structuredtree.root") # truth matching utility bt = ROOT.Backtracker(tm) print("constructed treemanager") #tree accessors #gen = tm.GetGenTree() g4 = tm.GetG4Tree() rec = tm.GetRecoTree() nmatch = 0 # number of reco tracks matched to a G4Particle ntrack = 0 # number of reco tracks nprimary = 0 # true number of primaries nprimary_match = 0 #number of primaries matched to at least one track hg4_nmatch = TH1F('hg4_nmatch', 'G4Particle -> Track matching;N^{0} matches', 10, 0, 10) hfrac = TH1F("hfrac", "particle contributing most energy;fraction of total", 50, 0, 1.1) hncont = TH1F("hncont", "particles contributing energy;N^{0} particles", 10, 0, 10) hncontfrac = TH2F( 'hncontfrac', 'particles contributing energy;N^{0} particles;max fraction of total', 10, 0, 25, 0, 1.1) eff_g4match = TEfficiency( 'eff_g4match', 'G4Particle -> Track matching; initial energy [GeV]; efficiency', 10, 0, 5) # loop over events for ientry in range(rec.NEntries()): if ientry % 100 == 0: print('processing event ' + str(ientry)) tm.GetEntry(ientry) bt.FillMaps() #load associations for this entry #nprimary += g4.NPrimary() for ig4 in range(g4.NSim()): if not g4.IsPrimary(ig4): continue # only consider primaries if not abs(g4.PDG(ig4)) == 2212: continue # muons only nprimary += 1 if bt.G4ParticleToTracks(ig4).size() > 0: nprimary_match += 1 # matched to something? hg4_nmatch.Fill(bt.G4ParticleToTracks(ig4).size()) eff_g4match.Fill( bt.G4ParticleToTracks(ig4).size() > 0, g4.SimMomEnter(ig4, 0).E()) ntrack += rec.NTrack() for itrack in range(rec.NTrack()): imatch = bt.TrackToG4Particle(itrack) if imatch < g4.NSim(): nmatch += 1 #imatches = bt.TrackToG4Particles(itrack) hncont.Fill(rec.TrackNTrueTrack(itrack)) #len(imatches)) for ip in range(rec.TrackNTrueTrack(itrack)): hfrac.Fill(rec.TrackMaxDepositFrac(itrack)) hncontfrac.Fill(rec.TrackNTrueTrack(itrack), rec.TrackMaxDepositFrac(itrack)) if nprimary != 0: print('number of primaries found: ', nprimary) print('number of matched primaries: ', nprimary_match) print('g4particle -> track matching efficiency: ', 1.0 * nprimary_match / nprimary) else: print('no G4particles found!') if ntrack != 0: print('track -> g4particle matching efficiency: ', 1.0 * nmatch / ntrack) else: print('no tracks found!') print('hncont size is ', hncont.Integral()) print('hfrac size is ', hfrac.Integral()) cg4_nmatch = TCanvas() hg4_nmatch.Draw() cg4_nmatch.SaveAs('g4_nmatch.png') cncont = TCanvas() hncont.Draw() cncont.SaveAs('ncont.png') cfrac = TCanvas() hfrac.Draw() cfrac.SaveAs('frac.png') cncontfrac = TCanvas() hncontfrac.Draw() cncontfrac.SaveAs('ncont_v_frac.png') ceff_g4match = TCanvas() eff_g4match.Draw() ceff_g4match.SaveAs('g4match_eff.png') # ask for user input as a way to keep the program from ending so we can # see the histogram. # python doesn't like empty input strings so I abuse exception handling try: null = input("press <Enter> to close canvas and exit program.") except: null = 'null'
def makePlots(f_ntuple, f_results, triggerpath): t = f_ntuple.Get("tree") print "Tree extracted..." h_csv_inc_4Calo = TH1F("h_csv_inc_4Calo", "h_csv_inc_4Calo", 50, 0, 1) h_csv_inc_4PF = TH1F("h_csv_inc_4PF", "h_csv_inc_4PF", 50, 0, 1) h_csv_afcalo = TH1F("h_csv_afcalo", "h_csv_afcalo", 50, 0, 1) h_csv_afpf = TH1F("h_csv_afpf", "h_csv_afpf", 50, 0, 1) h_csv_inc_log_4Calo = TH1F("h_csv_inc_log_4Calo", "h_csv_inc_log_4Calo", 50, 0, 7) h_csv_inc_log_4PF = TH1F("h_csv_inc_log_4PF", "h_csv_inc_log_4PF", 50, 0, 7) h_csv_afcalo_log = TH1F("h_csv_afcalo_log", "h_csv_afcalo_log", 50, 0, 7) h_csv_afpf_log = TH1F("h_csv_afpf_log", "h_csv_afpf_log", 50, 0, 7) h_csv_inc_calo = TH1F("h_csv_inc_calo", "h_csv_inc_calo", 50, 0, 1) h_csv_inc_pf = TH1F("h_csv_inc_pf", "h_csv_inc_pf", 50, 0, 1) h_csv_calo_pf = TH1F("h_csv_calo_pf", "h_csv_calo_pf", 50, 0, 1) h_csv_inc_calo_log = TH1F("h_csv_inc_calo_log", "h_csv_inc_calo_log", 50, 0, 7) h_csv_inc_pf_log = TH1F("h_csv_inc_pf_log", "h_csv_inc_pf_log", 50, 0, 7) h_csv_calo_pf_log = TH1F("h_csv_calo_pf_log", "h_csv_calog_pf_log", 50, 0, 7) eff_csv_inc_calo = TEfficiency("eff_csv_inc_calo", "eff_csv_inc_calo", 50, 0, 1) eff_csv_inc_pf = TEfficiency("eff_csv_inc_pf", "eff_csv_inc_pf", 50, 0, 1) eff_csv_calo_pf = TEfficiency("eff_csv_calo_pf", "eff_csv_calo_pf", 50, 0, 1) eff_csv_inc_calo_log = TEfficiency("eff_csv_inc_calo_log", "eff_csv_inc_calo_log", 50, 0, 7) eff_csv_inc_pf_log = TEfficiency("eff_csv_inc_pf_log", "eff_csv_inc_pf_log", 50, 0, 7) eff_csv_calo_pf_log = TEfficiency("eff_csv_calo_pf_log", "eff_csv_calog_pf_log", 50, 0, 7) i = 0 for event in t: print print "New event --------" print if not SelectTrigger(event, triggerpath, True): continue print "CALO JET SECTION : # calo jet = ", event.caloJet_num for i in range(0, event.caloJet_num - 1): if event.caloJet_offmatch[i] >= 0: print "calo jet[", i, "]" print "event.caloJet_offmatch[i]: ", event.caloJet_offmatch[ i], " event.offJet_num: ", event.offJet_num print "calo pt: ", event.caloJet_pt[ i], " offline pt: ", event.offJet_pt[ event.caloJet_offmatch[i]] print "calo eta: ", event.caloJet_eta[ i], " offline eta: ", event.offJet_eta[ event.caloJet_offmatch[i]] print "calo phi: ", event.caloJet_phi[ i], " offline phi: ", event.offJet_phi[ event.caloJet_offmatch[i]] print "offline csv: ", event.offJet_csv[ event.caloJet_offmatch[i]] if event.offJet_pt[event.caloJet_offmatch[i]] < 30: continue passed = False h_csv_inc_4Calo.Fill( event.offJet_csv[event.caloJet_offmatch[i]]) if event.offJet_csv[event.caloJet_offmatch[i]] < 1.: h_csv_inc_log_4Calo.Fill(-math.log( 1 - event.offJet_csv[event.caloJet_offmatch[i]])) if SelectTrigger(event, triggerpath, False, "Calo", i): passed = True print "passes calo filter", event.offJet_csv[ event.caloJet_offmatch[i]] h_csv_afcalo.Fill( event.offJet_csv[event.caloJet_offmatch[i]]) h_csv_inc_calo.Fill( event.offJet_csv[event.caloJet_offmatch[i]]) if event.offJet_csv[event.caloJet_offmatch[i]] != 1.: h_csv_afcalo_log.Fill(-math.log( 1 - event.offJet_csv[event.caloJet_offmatch[i]])) h_csv_inc_calo_log.Fill(-math.log( 1 - event.offJet_csv[event.caloJet_offmatch[i]])) eff_csv_inc_calo.Fill( passed, event.offJet_csv[event.caloJet_offmatch[i]]) print "PF JET SECTION : # pf jet = ", event.pfJet_num for i in range(0, event.pfJet_num - 1): if event.pfJet_offmatch[i] >= 0: print "pf jet[", i, "]" print "event.pfJet_offmatch[i]: ", event.pfJet_offmatch[ i], " event.offJet_num: ", event.offJet_num print "pf pt: ", event.pfJet_pt[ i], " offline pt: ", event.offJet_pt[ event.pfJet_offmatch[i]] print "pf eta: ", event.pfJet_eta[ i], " offline eta: ", event.offJet_eta[ event.pfJet_offmatch[i]] print "pf phi: ", event.pfJet_phi[ i], " offline phi: ", event.offJet_phi[ event.pfJet_offmatch[i]] print "offline csv: ", event.offJet_csv[ event.pfJet_offmatch[i]] if event.offJet_pt[event.pfJet_offmatch[i]] < 30: continue passed = False h_csv_inc_4PF.Fill(event.offJet_csv[event.pfJet_offmatch[i]]) if event.offJet_csv[event.pfJet_offmatch[i]] < 1.: h_csv_inc_log_4PF.Fill(-math.log( 1 - event.offJet_csv[event.pfJet_offmatch[i]])) if SelectTrigger(event, triggerpath, False, "PF", i): passed = True print "passes pf filter", event.offJet_csv[ event.pfJet_offmatch[i]] h_csv_afpf.Fill(event.offJet_csv[event.pfJet_offmatch[i]]) h_csv_inc_pf.Fill( event.offJet_csv[event.pfJet_offmatch[i]]) h_csv_calo_pf.Fill( event.offJet_csv[event.pfJet_offmatch[i]]) if event.offJet_csv[event.pfJet_offmatch[i]] < 1.: h_csv_afpf_log.Fill(-math.log( 1 - event.offJet_csv[event.pfJet_offmatch[i]])) h_csv_inc_pf_log.Fill(-math.log( 1 - event.offJet_csv[event.pfJet_offmatch[i]])) h_csv_calo_pf_log.Fill(-math.log( 1 - event.offJet_csv[event.pfJet_offmatch[i]])) eff_csv_inc_pf.Fill(passed, event.offJet_csv[event.pfJet_offmatch[i]]) if event.offJet_csv[event.pfJet_offmatch[i]] < 1.: eff_csv_inc_pf_log.Fill( passed, -math.log(1 - event.offJet_csv[event.pfJet_offmatch[i]])) f_results.Write()
def makePlots(f_ntuple, f_results): t = f_ntuple.Get("tree") print "Tree extracted..." h_csv_inc_4PF = TH1F("h_csv_inc_4PF", "h_csv_inc_4PF", 50, 0, 1) h_csv_inc_log_4PF = TH1F("h_csv_inc_log_4PF", "h_csv_inc_log_4PF", 50, 0, 7) h_csv_inc_pf = TH1F("h_csv_inc_pf", "h_csv_inc_pf", 50, 0, 1) h_csv_inc_pf_log = TH1F("h_csv_inc_pf_log", "h_csv_inc_pf_log", 50, 0, 7) eff_csv_inc_pf = TEfficiency("eff_csv_inc_pf", "eff_csv_inc_pf", 50, 0, 1) eff_csv_inc_pf_log = TEfficiency("eff_csv_inc_pf_log", "eff_csv_inc_pf_log", 50, 0, 7) i = 0 for event in t: print print "New event --------" print if not event.HLT_PFHT430_SixPFJet40: continue print "PF JET SECTION : # pf jet = ", event.pfJet_num for i in range(0, event.pfJet_num - 1): if event.pfJet_offmatch[i] >= 0: print "pf jet[", i, "]" print "event.pfJet_offmatch[i]: ", event.pfJet_offmatch[ i], " event.offJet_num: ", event.offJet_num print "pf pt: ", event.pfJet_pt[ i], " offline pt: ", event.offJet_pt[ event.pfJet_offmatch[i]] print "pf eta: ", event.pfJet_eta[ i], " offline eta: ", event.offJet_eta[ event.pfJet_offmatch[i]] print "pf phi: ", event.pfJet_phi[ i], " offline phi: ", event.offJet_phi[ event.pfJet_offmatch[i]] print "offline csv: ", event.offJet_csv[ event.pfJet_offmatch[i]] if event.offJet_pt[event.pfJet_offmatch[i]] < 30: continue passed = False h_csv_inc_4PF.Fill(event.offJet_csv[event.pfJet_offmatch[i]]) if event.offJet_csv[event.pfJet_offmatch[i]] < 1.: h_csv_inc_log_4PF.Fill(-math.log( 1 - event.offJet_csv[event.pfJet_offmatch[i]])) if event.pfJet_hltBTagPFCSVp080Single[ i]: # from HLT_PFHT430_SixPFJet40_PFBTagCSV_1p5 passed = True print "passes pf filter", event.offJet_csv[ event.pfJet_offmatch[i]] h_csv_inc_pf.Fill( event.offJet_csv[event.pfJet_offmatch[i]]) if event.offJet_csv[event.pfJet_offmatch[i]] < 1.: h_csv_inc_pf_log.Fill(-math.log( 1 - event.offJet_csv[event.pfJet_offmatch[i]])) eff_csv_inc_pf.Fill(passed, event.offJet_csv[event.pfJet_offmatch[i]]) if event.offJet_csv[event.pfJet_offmatch[i]] < 1.: eff_csv_inc_pf_log.Fill( passed, -math.log(1 - event.offJet_csv[event.pfJet_offmatch[i]])) f_results.Write()
def RunAnalysis(input_name, output_name=""): # Check output name, set by default if not passed to the function if not output_name: output_name = input_name.split("_")[0] + "_analysed.root" print("INPUT:", input_name, "\nOUTPUT:", output_name) # Configuring thresholds (thr_start, thr_end, thr_step) = (0.3, 8, 0.1) thr_range = np.arange(thr_start, thr_end + thr_step, thr_step) n_thr = len(thr_range) # Get hit dictionary hit_dict = GetHitDict(input_name) # Open root file to write the results into write_file = TFile("data/" + output_name, "recreate") write_file.cd() # Initialize efficiency and cluster size objects eff = TEfficiency("Efficiency", "Efficiency;Threshold [fC];Efficiency", n_thr, thr_start, thr_end) clus_graph = TGraphErrors(n_thr) clus_graph.SetNameTitle("Average_cluster_size", "Average_cluster_size") clus_graph.GetXaxis().SetTitle("Threshold [fC]") clus_graph.GetYaxis().SetTitle("Average cluster size") # Perform threshold scanning for thr in thr_range: thrE = thr * 6242.2 print("Threshold scanning:", round(thr / thr_end * 100, 1), "%", end="\r") # Scan a threshold and obtain a list of clusters cluster_list = ScanThreshold(hit_dict, thrE) # Analyze cluster list and fill TEfficiency object for cluster in cluster_list: eff.Fill(bool(cluster), thr) # Replace zeros with NaN to obtain proper average later cluster_list = [ np.nan if cluster == 0 else cluster for cluster in cluster_list ] try: # Calculate average cluster size and add points to clus_graph cluster_size = np.nanmean(cluster_list) N = np.count_nonzero(~np.isnan(cluster_list)) err = np.nanstd(cluster_list) / sqrt(N - 1) clus_graph.SetPoint(int(np.where(thr_range == thr)[0][0]), thr, cluster_size) clus_graph.SetPointError(int(np.where(thr_range == thr)[0][0]), ex=0, ey=err) except RuntimeWarning: pass print("\nDone.") # Efficiency fit fit_form = "0.5*[0]*TMath::Erfc((x-[1])/(TMath::Sqrt(2)*[2])*(1-0.6*TMath::TanH([3]*(x-[1])/TMath::Sqrt(2)*[2])))" fit_func = TF1("Efficiency_fit", fit_form, 0, 8) # Set initial parameter values and limits fit_func.SetParameters(1, 4, 1, 1, 0.5) fit_func.SetParLimits(1, 0, 5) fit_func.SetParLimits(2, 0, 2) fit_func.SetParLimits(3, 0, 2) fit_func.SetParLimits(4, 0.5, 0.7) # Perform fit eff.Fit(fit_func, "R") # Write outputs to file eff.Write() clus_graph.Write() # Collect info source = "allpix" angle = input_name.split("-")[0] descr = input_name.split("_")[0].strip("0deg-") n_events = str(int(eff.GetTotalHistogram().GetEntries() / n_thr)) title = source + "," + angle + "," + descr + "(" + str(n_events) + "ev)" vt50 = str(fit_func.GetParameter(1)) vt50_err = str(fit_func.GetParError(1)) thr_range = str(thr_start) + ":" + str(thr_end) + ":" + str(thr_step) # Write info to Info directory info_dir = write_file.mkdir("Info") info_dir.cd() info_dir.WriteObject(TString(source), "source") info_dir.WriteObject(TString(angle), "angle") info_dir.WriteObject(TString(descr), "descr") info_dir.WriteObject(TString(n_events), "n_events") info_dir.WriteObject(TString(title), "title") info_dir.WriteObject(TString(vt50), "vt50") info_dir.WriteObject(TString(vt50), "vt50_err") info_dir.WriteObject(TString(thr_range), "thr_range") write_file.Close()