def generate_sim_histograms(degrader, g4bl, exec_d4_d5):
  print "Starting simulation of", degrader, "g4bl:", g4bl
  if g4bl:
    out_file_name = out_sim_g4bl_file_fmt.format(degrader=degrader)
  else:
    out_file_name = out_sim_file_fmt.format(degrader=degrader)
  
  out_file = TFile(out_file_name, "RECREATE")
  
  charges = {"+":"pos_"+degrader,"-":"neg_"+degrader}
  hists = {c:make_hist(charges[c], **hist_settings) for c in charges}
  hists['combined'] = make_hist("combined_"+degrader,**hist_settings)
  
  if g4bl:
    in_file_name = in_sim_g4bl_file_fmt.format(degrader=degrader)
    tree = get_tree_from_file("truth",in_file_name)
    fill_g4bl_hists (tree, hists["+"], hists["-"], exec_d4_d5)
    hists['combined'].Add(hists['+'], hists['-'])
  else:
    neg_pos_ratio = get_g4bl_decay_ratio(degrader)
    for c in charges:
      in_file_name = in_sim_file_fmt.format(charge=c, degrader=degrader)
      tree = get_tree_from_file("truth",in_file_name)
      fill_sim_hist (tree, hists[c], c, exec_d4_d5)
    hists['combined'].Add(hists['+'], hists['-'], 1.0, neg_pos_ratio)
    # hists['combined'].Add(hists['+'], hists['-'], 1.0, neg_pos_ratio)
    
  save_file(out_file)
Ejemplo n.º 2
0
def run_count_analysis(filename, mu_type):
  print "Counting"
  tree = get_tree_from_file("truth", filename)
  counts  = get_mu_decay_count_from_tree(tree, mu_type)
  res = {}
  for key, n_decays in counts.items():
    res[key] = ValueWithError(n_decays, n_decays**0.5, print_fmt="{: >6.0f} +/- {: <5.0f}")
  return res
Ejemplo n.º 3
0
def init_tree_and_hists(run_info, bins=400):
  filename = file_fmt.format(dir=data_dir, file_id=run_info["file_id"])
  
  tree = get_tree_from_file("Trigger", filename)
  
  for attr, val in run_info.items(): setattr(tree, attr, val)
  
  assign_leaves(tree, channels)
  tree.hists = make_ch_hists(tree, channels, l_bound, u_bound, bins)
  return tree
def main():
    gStyle.SetOptFit()
    res = {'data':{}, 'sec':{}}
    for f_id in file_ids:
        print "Processing", f_id
        filename = file_prefix + f_id + file_suffix
        treename = "ts" if f_id == "448" else "Scaler"
        
        tree = get_tree_from_file(treename, filename)
        print tree.filename, tree
        load_branch(tree, f_id)
        # tfile, tree, branch = get_tfile_tree_and_branch(name, **file_data)
        key = int(f_id)
        res['data'][key] = get_gain_stability_dict(tree)
        res['sec'][key] = get_sec_dict(tree)
        # tfile.Close()
    
    canvas = make_canvas("Gain Stability", 3,2, True)
    # add the histograms to the dictionary
    res.update({'gain_hist':{}, 'count_hist':{}})
    for pad_id, (file_id, data) in enumerate(res['data'].items(), 1):
        gain_hist  = get_gain_stability_hist_for_file(data, file_id)
        # count_hist = get_trigger_count_hist_for_file(data, file_id)
        # potential_hist = get_potential_trigger_count_hist_for_file(data, file_id)
        
        sec_hist = get_sec_count_hist_for_file(res['sec'][int(file_id)], file_id)
        # c1 = make_canvas('foobar', 1, 1, True)
        # c1.cd()
        # sec_hist.Draw()
        # c1.SaveAs("foobar%i.png"%file_id)
        # continue
        
        full_range = max(data.keys())[0] - min(data.keys())[0]
        # Fit the central 80%
        fit_min = full_range * 0.05
        fit_max = full_range * 0.95
        print fit_min, fit_max
        
        # gain_hist.Fit("pol0") # attempt to fit the gain with a flat function
        gain_hist.Fit("pol0", "", "", fit_min, fit_max) # attempt to fit the gain with a flat function
        
        # draw_gain_and_count_hist(gain_hist, count_hist, canvas, pad_id)
        # draw_gain_and_count_hist(gain_hist, potential_hist, canvas, pad_id)
        draw_gain_and_count_hist(gain_hist, sec_hist, canvas, pad_id)
        
        res['gain_hist'][file_id] = gain_hist
        res['count_hist'][file_id] = sec_hist
    
    canvas.SaveAs("images/gain_stability.svg")
    canvas.SaveAs("images/gain_stability.eps")
def generate_data_histograms(run_id):
  # Open the input file & init the tree
  print "Starting run", run_id
  in_file_name = in_data_file_fmt.format(run_id=run_id)
  tree = get_tree_from_file("Trigger", in_file_name)
  assign_leaves(tree, [c[0] for c in channels]) # use comprehension to make a list of just the channels
  
  # Create the file to write to
  out_file_name = out_data_file_fmt.format(run_id=run_id)
  out_file = TFile(out_file_name, "RECREATE")
  
  # Make the histograms & fill them
  hists = {ch[0]:make_hist(ch[0], **hist_settings) for ch in channels}
  fill_data_hists (tree, hists)
  save_file(out_file)
def run_basic_sim_analysis(filename, mu_type, fit_type, bin_width, save_image="", fast=False):
  """
  Gets the tree from file, opens it, creates a histogram of dts
  then fits it and if required, draws it. Returns the integrals
  """
  hist_name = get_file_root(filename) # strip the path and the file type
  print "running", hist_name
  func_fmt, initial_settings = get_fit_func_and_settings_for_muon_type(mu_type,fit_type)
  
  tree = get_tree_from_file("truth", filename)
  hist = create_histogram_from_tree(hist_name, tree, bin_width, mu_type, fast)
  
  fit_options = "LIMER"
  fit_histogram(hist, func_fmt, initial_settings, func_name=hist_name, fit_options=fit_options, img_name=save_image)
  
  sleep(10)
  
  keys = ("f",) if mu_type == "mu+" else ("f", "cu") 
  return get_integrals_from_histogram_for_keys(hist, keys=keys), hist.fit_param
def get_dead_time_from_file(run_id):
  if run_id == 448:
    tree_name, branch_name, leaf_name = "ts", "Scaler", "scaler"
  else:
    run_id = "00{}".format(run_id)
    tree_name, branch_name, leaf_name = "Scaler", "SCLR", "SCLR"
  print_fmt ="Opening file with id:{}, tree:{}, branch:{}, leaf:{}"
  print print_fmt.format(run_id, tree_name, branch_name, leaf_name)
  
  file_name = file_name_fmt.format(run_id=run_id)
  tree = get_tree_from_file(tree_name, file_name)
  assign_leaf(tree, branch_name, leaf_name)
  
  # we need the last entry
  n_entries = tree.GetEntries()
  tree.GetEntry(n_entries - 1)
  
  # U&&!D including & excluding veto and error
  res = {'u_not_d_not_veto':ValueWithError(tree.scaler(1)), 'u_not_d':ValueWithError(tree.scaler(2))}
  for i in res.values():
    i.print_fmt=u"{: >9.0f} +/- {: <4.0f}"
  res['dead_time'] = res['u_not_d_not_veto']/res['u_not_d']
  res['dead_time'].print_fmt = u"{: >5.2f} +/- {: <5.3f}"
  return res