space = RooWorkspace('space', False) # new parameters space.factory('bsMass[5.25,5.50]') space.factory('bdMass[5.10,5.50]') space.factory('bdbarMass[5.10,5.50]') space.factory('lbtkMass [5.4,5.9]') space.factory('lbtkbarMass[5.4,5.9]') space.factory('lbtkPt[0.,200.]') space.factory('kkMass[0.8,10.0]') space.factory('kpiMass[0.8,10.0]') space.factory('kpibarMass[0.8,10.0]') ########## load workspace #################### workspaceFile1 = TFile.Open('store_root/keepMCevent_1stStep_MCShape.root') space1st = workspaceFile1.Get('space') space1st.SetName('space1st') workspaceFile2 = TFile.Open('store_root/keepMCevent_2ndStep_dataFit.root') space2nd = workspaceFile2.Get('space') space2nd.SetName('space2nd') #workspaceFileE=TFile.Open('store_root/workspace_extraStep_1st_noSelectionLbFit.root') workspaceFileE = TFile.Open( 'store_root/keepMCevent_shortRangeLbFit_noKinematicCut.{0}.root'.format( ptRange)) #workspaceFileE=TFile.Open('store_root/workspace_extraStep_1st_shortRangeLbFit_withKinematicCut.root') spaceExt = workspaceFileE.Get('space') spaceExt.SetName('spaceExt') load2016Data = True if load2016Data:
sigObj = cfg.get(section,'signal_model').split(':')[1] print sigFile, sigObj # workspaceName = cfg.get('Global','workspace') # ws = RooWorkspace(workspaceName) pwd = ROOT.gDirectory.GetPath() sigFile = cfg.get(section,'signal_model').split(':')[0] sigObj = cfg.get(section,'signal_model').split(':')[1] print ' sigFile: ', sigFile, ' obj: ', sigObj sigFile = TFile.Open(sigFile) ROOT.gDirectory.cd(pwd) sigObj = sigFile.Get(sigObj) if isinstance(sigObj,ROOT.TTree): sigObj = sigObj.CloneTree() else: print 'Signal model must be a TTree (for now)' exit(1) sigFile.Close() ROOT.gDirectory.cd(pwd) bins = [float(i) for i in cfg.get(section,'obsBins').split(',')] nObsBins = len(bins)-1 weightvar = cfg.get('Global','signal_weight_var')
addHist.SetDirectory(0) f.Close() print "input file:", o.inFile f = TFile(o.inFile, "READ") f_syst = {} if o.jetSyst: for syst in NPs: for direction in syst: systFileName = o.inFile.replace("/hists.root", "_" + direction + "/hists.root") print "input file:", systFileName f_syst[direction] = TFile(systFileName, "READ") if path.exists(o.outFile): out = TFile.Open(o.outFile, "UPDATE") else: out = TFile.Open(o.outFile, "RECREATE") def getAndStore(var, channel, histName, suffix='', jetSyst=False, function=''): #h={} #for region in regions: if not o.TDirectory: h = get(f, o.cut + "/" + o.tag + "Tag/mainView/" + o.region + "/" + var) else: h = get(f, o.TDirectory + "/" + var) if rebin: if type(rebin) is list: h, _ = do_variable_rebinning(h, rebin, scaleByBinWidth=False)
def select_evts_2P2F_3P1F_multiquartets( tree, infile_fakerates, genwgts_dct, dct_xs, outfile_root=None, outfile_json=None, name="", int_lumi=-1, start_at_evt=0, break_at_evt=-1, fill_hists=False, explain_skipevent=False, verbose=False, print_every=50000, smartcut_ZapassesZ1sel=False, overwrite=False, skip_mass4l_lessthan0=False, match_lep_Hindex=False, recalc_masses=False, skip_passedFullSelection=True, stop_when_found_3p1f=True, keep_one_quartet=False, use_multiquart_sel=True, sync_with_xBFAna=False, ): """Apply RedBkg multi-lepton quartet selection to all events in tree. NOTE: A "quartet" is a combination of 4-leptons. Each event may have multiple leptons quartets. If an event has both a 3P1F quartet and a 2P2F quartet, then the 3P1F one takes priority. The 2P2F quartet will be skipped. TODO Update below: Select events with: - Exactly 3 leptons passing tight selection and at least 1 failing. - Exactly 2 leptons passing tight selection and at least 2 failing. - When an event has >4 leptons, then multiple combinations of 2P2F/3P1F are possible. Suppose you have an event with 5 leptons, 3 of which pass tight selection and 2 fail tight selection. Then we have two different ways to make a 3P1F combo (two 3P1F subevents). - Does NOT select the BEST ZZ candidate per event. If there are >1 ZZ candidates that pass ZZ selections, then each ZZ cand is saved as a separate entry in the TTree. This means a single event can show up multiple times in a TTree, if there are multiple valid 4-lepton combinations ("quartets"). Args: outfile_root (str): Path to store root file. TTree of selected events will be made. If `fill_hists` is True, then will store histograms. smartcut_ZapassesZ1sel (bool, optional): In the smart cut, the literature essentially says that if a Za looks like a more on-shell Z boson than the Z1 AND if the Zb is a low mass di-lep resonance, then veto the whole ZZ candidate. However, literature doesn't check for Za passing Z1 selections! Set this to True if you require Za to pass Z1 selections. Default is False. skip_mass4l_lessthan0 (bool, optional): If True, then skip events whose tree.mass4l <= 0. This is useful when you need to use the values that are already stored in the BBF NTuple. Default is False. match_lep_Hindex (bool, optional): Useful for reproducing results from the xBF Analyzer. If True, then only save events in which the selected quartet has: Z1 lepton indices that match: lep_Hindex[0], lep_Hindex[1] (any order) Z2 lepton indices that match: lep_Hindex[2], lep_Hindex[3] (any order) Since all quartets are automatically built (not necessarily saved) then this will find all the same events that xBF finds. It is "cheating" since we know the answer (lep_Hindex) already! Default is False. recalc_masses (bool, optional): If True, recalculate mass4l of selected lepton quartet and corresponding massZ1 and massZ2 values. stop_when_found_3p1f (bool, optional): If True, if at least one valid 3P1F ZZ candidate was found, do not build any 2P2F candidates. Defaults to True. use_multiquart_sel (bool, optional): If True, use the updated reducible background event selection logic ("multi-quartet"). """ assert not (use_multiquart_sel and sync_with_xBFAna), ( f"Can't have use_multiquart_sel=True and sync_with_xBFAna=True.") if use_multiquart_sel: msg = f"use_multiquart_sel = True. Forcing these bools:" announce(msg, pad_char='#') print(f" stop_when_found_3p1f = True\n" f" match_lep_Hindex = False\n" f" keep_one_quartet = False\n" f" recalc_masses = True\n" f" skip_mass4l_lessthan0 = False\n" f" skip_passedFullSelection = True\n") stop_when_found_3p1f = True match_lep_Hindex = False keep_one_quartet = False recalc_masses = True skip_mass4l_lessthan0 = False skip_passedFullSelection = True if sync_with_xBFAna: msg = f"sync_with_xBFAna = True. Forcing these bools:" announce(msg, pad_char='#') print(f" stop_when_found_3p1f = False\n" f" match_lep_Hindex = True\n" f" keep_one_quartet = True\n" f" skip_mass4l_lessthan0 = True\n" f" skip_passedFullSelection = True\n" f" recalc_masses = False\n") stop_when_found_3p1f = False match_lep_Hindex = True keep_one_quartet = True skip_mass4l_lessthan0 = True skip_passedFullSelection = True recalc_masses = False if keep_one_quartet and not recalc_masses: warnings.warn(f"!!! You are keeping one quartet per event " f"(keep_one_quartet=True) " f"without recalculating masses(recalc_masses=False).\n" f"!!! Therefore, mass info (mass4l, massZ1, massZ2) " f"in tree may not correspond to saved quartet!") if stop_when_found_3p1f: print("A valid 3P1F quartet will ignore all 2P2F quartets.") if keep_one_quartet: announce("Saving only one quartet per event.") if match_lep_Hindex: print(f" Since match_lep_Hindex=True, the one quartet saved\n" f" must have lepton indices from Z1 and Z2 agree with\n" f" those in lep_Hindex.") if not stop_when_found_3p1f: print(" A valid 2P2F quartet can be chosen over valid 3P1F.") else: print(f" Since match_lep_Hindex=False, " f"not sure which quartet to save!\n" f" Saving the first valid ZZ cand arbitrarily!.") if fill_hists: raise ValueError(f"fill_hists = True, but must review code first.") # Prep histograms. d_hists = { "Data": { "2p2f": { "mass4l": h1_data_2p2f_m4l, "n_quartets": h1_data_n2p2f_combos, }, "3p1f": { "mass4l": h1_data_3p1f_m4l, "n_quartets": h1_data_n3p1f_combos, } }, "ZZ": { "2p2f": { "mass4l": h1_zz_2p2f_m4l, "n_quartets": h1_zz_n2p2f_combos, }, "3p1f": { "mass4l": h1_zz_3p1f_m4l, "n_quartets": h1_zz_n3p1f_combos, } }, } evt_info_d = make_evt_info_d() # Info for printing. evt_info_2p2f_3p1f_d = {} # Info for json file. assert infile_fakerates is not None h_FRe_bar, h_FRe_end, h_FRmu_bar, h_FRmu_end = \ retrieve_FR_hists(infile_fakerates) if outfile_json is not None: check_overwrite(outfile_json, overwrite=overwrite) # Make pointers to store new values. ptr_finalState = np.array([0], dtype=int) ptr_nZXCRFailedLeptons = np.array([0], dtype=int) ptr_is2P2F = np.array([0], dtype=int) # Close enough to bool lol. ptr_is3P1F = np.array([0], dtype=int) ptr_isData = np.array([0], dtype=int) ptr_isMCzz = np.array([0], dtype=int) ptr_fr2_down = array('f', [0.]) ptr_fr2 = array('f', [0.]) ptr_fr2_up = array('f', [0.]) ptr_fr3_down = array('f', [0.]) ptr_fr3 = array('f', [0.]) ptr_fr3_up = array('f', [0.]) ptr_eventWeightFR_down = array('f', [0.]) ptr_eventWeightFR = array('f', [0.]) ptr_eventWeightFR_up = array('f', [0.]) ptr_lep_RedBkgindex = array('i', [0, 0, 0, 0]) ptr_mass4l = array('f', [0.]) ptr_massZ1 = array('f', [0.]) ptr_massZ2 = array('f', [0.]) # ptr_mass4l_vtxFSR_BS = array('f', [0.]) # ptr_eventWeight = array('f', [0.]) if outfile_root is not None: check_overwrite(outfile_root, overwrite=overwrite) new_file = TFile.Open(outfile_root, "recreate") print("Cloning TTree.") new_tree = tree.CloneTree(0) # Clone 0 events. # Make new corresponding branches in the TTree. new_tree.Branch("is2P2F", ptr_is2P2F, "is2P2F/I") new_tree.Branch("is3P1F", ptr_is3P1F, "is3P1F/I") new_tree.Branch("isData", ptr_isData, "isData/I") new_tree.Branch("isMCzz", ptr_isMCzz, "isMCzz/I") new_tree.Branch("fr2_down", ptr_fr2_down, "fr2_down/F") new_tree.Branch("fr2", ptr_fr2, "fr2/F") new_tree.Branch("fr2_up", ptr_fr2_up, "fr2_up/F") new_tree.Branch("fr3_down", ptr_fr3_down, "fr3_down/F") new_tree.Branch("fr3", ptr_fr3, "fr3/F") new_tree.Branch("fr3_up", ptr_fr3_up, "fr3_up/F") new_tree.Branch("eventWeightFR_down", ptr_eventWeightFR_down, "eventWeightFR_down/F") new_tree.Branch("eventWeightFR", ptr_eventWeightFR, "eventWeightFR/F") new_tree.Branch("eventWeightFR_up", ptr_eventWeightFR_up, "eventWeightFR_up/F") # Record the indices of the leptons in passing quartet. new_tree.Branch("lep_RedBkgindex", ptr_lep_RedBkgindex, "lep_RedBkgindex[4]/I") # Modify existing values of branches. new_tree.SetBranchAddress("finalState", ptr_finalState) new_tree.SetBranchAddress("nZXCRFailedLeptons", ptr_nZXCRFailedLeptons) if recalc_masses: new_tree.SetBranchAddress("mass4l", ptr_mass4l) new_tree.SetBranchAddress("massZ1", ptr_massZ1) new_tree.SetBranchAddress("massZ2", ptr_massZ2) n_tot = tree.GetEntries() print(f"Total number of events: {n_tot}\n" f"Looking for >=4 leptons per event...") #################### #=== Event Loop ===# #################### isMCzz = 1 if name in "ZZ" else 0 isData = 1 if name in "Data" else 0 for evt_num in range(start_at_evt, n_tot): if evt_num == break_at_evt: break print_periodic_evtnum(evt_num, n_tot, print_every=print_every) tree.GetEntry(evt_num) run = tree.Run lumi = tree.LumiSect event = tree.Event evt_id = f"{run} : {lumi} : {event}" ################################### #=== Initial event selections. ===# ################################### try: if not tree.passedTrig: continue except AttributeError: # Branch 'passedTrig' doesn't exist. warnings.warn(f"Branch passedTrig probably doesn't exist!\n" f"Ignoring passedTrig==1 criterion.") if skip_passedFullSelection and tree.passedFullSelection: if explain_skipevent: print_skipevent_msg("passedFullSelection == 1", evt_num, run, lumi, event) evt_info_d["n_evts_passedFullSelection"] += 1 continue if skip_mass4l_lessthan0 and (tree.mass4l <= 0): evt_info_d["n_evts_skip_mass4l_le0"] += 1 continue # Check the number of leptons in this event. n_tot_leps = len(tree.lep_pt) if verbose: print(f" Total number of leptons found: {n_tot_leps}") # Ensure at least 4 leptons in event: if n_tot_leps < 4: if explain_skipevent: print_skipevent_msg("n_leps < 4", evt_num, run, lumi, event) evt_info_d["n_evts_lt4_leps"] += 1 continue # Initialize ALL leptons (possibly >=4 leptons). mylep_ls = make_filled_mylep_ls(tree) n_leps_passing = get_n_myleps_passing(mylep_ls) n_leps_failing = get_n_myleps_failing(mylep_ls) if verbose: print(f" Num leptons passing tight sel: {n_leps_passing}\n" f" Num leptons failing tight sel: {n_leps_failing}") for mylep in mylep_ls: mylep.print_info(oneline=True) if n_leps_passing < 2: evt_info_d["n_evts_lt2tightleps"] += 1 if explain_skipevent: msg = f" Contains {n_leps_passing} (< 2) tight leps." print_skipevent_msg(msg, evt_num, run, lumi, event) continue #################################################### #=== Find best ZZ cand for each lepton quartet. ===# #################################################### qtcat = QuartetCategorizer( mylep_ls, verbose=verbose, explain_skipevent=explain_skipevent, smartcut_ZapassesZ1sel=smartcut_ZapassesZ1sel, run=run, lumi=lumi, event=event, entry=evt_num, stop_when_found_3p1f=stop_when_found_3p1f) ls_valid_ZZcands_OS = qtcat.ls_valid_ZZcands_OS_3p1f + \ qtcat.ls_valid_ZZcands_OS_2p2f n_valid_ZZcands_OS = qtcat.n_valid_ZZcands_OS_3p1f + \ qtcat.n_valid_ZZcands_OS_2p2f if n_valid_ZZcands_OS == 0: if explain_skipevent: print_skipevent_msg(" No valid 2P2F or 3P1F ZZ candidates.", evt_num, run, lumi, event) evt_info_d["n_evts_novalid2P2For3P1F_ZZcands"] += 1 continue if verbose: print(f" Num valid OS ZZ cands 3P1F: " f"{qtcat.n_valid_ZZcands_OS_3p1f}\n" f" Num valid OS ZZ cands 2P2F: " f"{qtcat.n_valid_ZZcands_OS_2p2f}") ############################################################## #=== Record each valid ZZ cand per quartet in this event. ===# ############################################################## n_saved_quartets_3p1f = 0 n_saved_quartets_2p2f = 0 found_matching_lep_Hindex = False overall_evt_label = '' # Either '3P1F' or '2P2F'. for ndx_zzcand, zzcand in enumerate(ls_valid_ZZcands_OS, 1): cr_str = zzcand.get_str_cr_os_method().upper() # Sanity checks. if cr_str == '3P1F': assert zzcand.check_valid_cand_os_3p1f() elif cr_str == '2P2F': assert zzcand.check_valid_cand_os_2p2f() else: raise ValueError(f"cr_str is not in ('3P1F', '2P2F').") assert zzcand.check_valid_cand_os_3p1f() \ ^ zzcand.check_valid_cand_os_2p2f() # xor. if match_lep_Hindex: # Only save quartet if the lepton indices match lep_Hindex. # E.g., the three lists below are the same ZZ cand: # quartet_1 = [2, 3, 1, 4] # quartet_2 = [3, 2, 1, 4] # quartet_3 = [3, 2, 4, 1] lep_Hindex_ls = list(tree.lep_Hindex) try: assert len(lep_Hindex_ls) == 4 except AssertionError: print(f'lep_Hindex_ls has weird vals: {lep_Hindex_ls}') AssertionError idcs_lepHindex_z1 = lep_Hindex_ls[:2] idcs_lepHindex_z2 = lep_Hindex_ls[2:] idcs_myz1 = zzcand.z_fir.get_mylep_indices() idcs_myz2 = zzcand.z_sec.get_mylep_indices() # When checking if two sets are equal, order doesn't matter! same_z1 = (set(idcs_lepHindex_z1) == set(idcs_myz1)) same_z2 = (set(idcs_lepHindex_z2) == set(idcs_myz2)) if same_z1 and same_z2: found_matching_lep_Hindex = True else: evt_info_d["n_quartets_skip_lep_Hindex_mismatch"] += 1 continue # Event weight calculation. n_dataset_tot = float(genwgts_dct[name]) evt_weight_calcd = get_evt_weight(dct_xs=dct_xs, Nickname=name, lumi=int_lumi, event=tree, n_dataset_tot=n_dataset_tot, orig_evt_weight=tree.eventWeight) # See which leptons from Z2 failed. n_fail_code = check_which_Z2_leps_failed(zzcand) mylep1_fromz1 = zzcand.z_fir.mylep1 mylep2_fromz1 = zzcand.z_fir.mylep2 mylep1_fromz2 = zzcand.z_sec.mylep1 mylep2_fromz2 = zzcand.z_sec.mylep2 #=== 3P1F quartet. ===# if n_fail_code == 2: # First lep from Z2 failed. # NOTE: Turns out this will never trigger for OS Method! # This is due to the way that the 3P1F quartets are built. # The 1 failing lepton is always placed as the 4th lepton. # Therefore, fr3 (the 4th lepton) will always be != 0. assert cr_str == '3P1F' assert mylep1_fromz2.is_loose fr2, fr2_err = get_fakerate_and_error_mylep(mylep1_fromz2, h_FRe_bar, h_FRe_end, h_FRmu_bar, h_FRmu_end, verbose=verbose) fr2_down = calc_fakerate_down(fr2, fr2_err) # Scale down. fr2_up = calc_fakerate_up(fr2, fr2_err) # Scale up. fr3 = 0 fr3_err = 0 fr3_down = 0 fr3_up = 0 # Use fake rates to calculate new event weight. new_weight_down = (fr2_down / (1 - fr2_down)) * evt_weight_calcd new_weight = (fr2 / (1 - fr2)) * evt_weight_calcd new_weight_up = (fr2_up / (1 - fr2_up)) * evt_weight_calcd elif n_fail_code == 3: # Second lep from Z2 failed. assert cr_str == '3P1F' assert mylep2_fromz2.is_loose fr2 = 0 fr2_err = 0 fr2_down = 0 fr2_up = 0 fr3, fr3_err = get_fakerate_and_error_mylep(mylep2_fromz2, h_FRe_bar, h_FRe_end, h_FRmu_bar, h_FRmu_end, verbose=verbose) fr3_down = calc_fakerate_down(fr3, fr3_err) # Scale down. fr3_up = calc_fakerate_up(fr3, fr3_err) # Scale up. # Use fake rates to calculate new event weight. new_weight_down = (fr3_down / (1 - fr3_down)) * evt_weight_calcd new_weight = (fr3 / (1 - fr3)) * evt_weight_calcd new_weight_up = (fr3_up / (1 - fr3_up)) * evt_weight_calcd #=== 2P2F quartet. ===# elif n_fail_code == 5: # Both leps failed. assert cr_str == '2P2F' fr2, fr2_err = get_fakerate_and_error_mylep(mylep1_fromz2, h_FRe_bar, h_FRe_end, h_FRmu_bar, h_FRmu_end, verbose=verbose) fr3, fr3_err = get_fakerate_and_error_mylep(mylep2_fromz2, h_FRe_bar, h_FRe_end, h_FRmu_bar, h_FRmu_end, verbose=verbose) fr2_down = calc_fakerate_down(fr2, fr2_err) # Scale down. fr2_up = calc_fakerate_up(fr2, fr2_err) # Scale up. fr3_down = calc_fakerate_down(fr3, fr3_err) # Scale down. fr3_up = calc_fakerate_up(fr3, fr3_err) # Scale up. # Use fake rates to calculate new event weight. new_weight_down = (fr2_down / (1 - fr2_down)) * ( fr3_down / (1 - fr3_down)) * evt_weight_calcd new_weight = (fr2 / (1 - fr2)) * (fr3 / (1 - fr3)) * evt_weight_calcd new_weight_up = (fr2_up / (1 - fr2_up)) * ( fr3_up / (1 - fr3_up)) * evt_weight_calcd if verbose: announce(f"Valid ZZ cand is {cr_str}") print(f" This fail code: {n_fail_code}\n" f" Z2 lep codes FOR DEBUGGING:\n" f" ===========================\n" f" 0, if neither lep from Z2 failed\n" f" 2, if first lep from Z2 failed\n" f" 3, if second lep from Z2 failed\n" f" 5, if both leps from Z2 failed\n" f" ===========================\n" f" fr2_down={fr2_down:.6f}, fr3_down={fr3_down:.6f}\n" f" fr2={fr2:.6f}, fr3={fr3:.6f}\n" f" fr2_up={fr2_up:.6f}, fr3_up={fr3_up:.6f}\n" f" tree.eventWeight = {tree.eventWeight:.6f}\n" f" evt_weight_calcd = {evt_weight_calcd:.6f}\n" f" new_weight_down = {new_weight_down:.6f}\n" f" new_weight = {new_weight:.6f}\n" f" new_weight_up = {new_weight_up:.6f}") zzname = f"SELECTED ZZ CAND ({ndx_zzcand}/{n_valid_ZZcands_OS})" zzcand.print_info(name=zzname) # Save this quartet in TTree. Fill branches. lep_idcs = [ mylep1_fromz1.ndx_lepvec, mylep2_fromz1.ndx_lepvec, mylep1_fromz2.ndx_lepvec, mylep2_fromz2.ndx_lepvec, ] ptr_finalState[0] = dct_finalstates_str2int[ zzcand.get_finalstate()] ptr_nZXCRFailedLeptons[0] = zzcand.get_num_failing_leps() ptr_is3P1F[0] = zzcand.check_valid_cand_os_3p1f() ptr_is2P2F[0] = zzcand.check_valid_cand_os_2p2f() ptr_isData[0] = isData ptr_isMCzz[0] = isMCzz ptr_fr2_down[0] = fr2_down ptr_fr2[0] = fr2 ptr_fr2_up[0] = fr2_up ptr_fr3_down[0] = fr3_down ptr_fr3[0] = fr3 ptr_fr3_up[0] = fr3_up ptr_eventWeightFR_down[0] = new_weight_down ptr_eventWeightFR[0] = new_weight ptr_eventWeightFR_up[0] = new_weight_up ptr_lep_RedBkgindex[0] = lep_idcs[0] ptr_lep_RedBkgindex[1] = lep_idcs[1] ptr_lep_RedBkgindex[2] = lep_idcs[2] ptr_lep_RedBkgindex[3] = lep_idcs[3] # Label entire event as either 3P1F or 2P2F. # The new RedBkg logic gives 3P1F priority over 2P2F, # i.e. if there are 3P1F and 2P2F quartets, label event 3P1F. #### However when finding the quartet whose lep indices match #### lep_Hindex (i.e., when syncing with the xBF Analyzer), #### then choose the quartet (ZZ) with the highest D_kin_bkg. #### In this case, 2P2F may override 3P1F. if stop_when_found_3p1f and (overall_evt_label == '3P1F') \ and (cr_str == '2P2F'): # DON'T label as 2P2F! pass else: overall_evt_label = cr_str n_saved_quartets_3p1f += zzcand.check_valid_cand_os_3p1f() n_saved_quartets_2p2f += zzcand.check_valid_cand_os_2p2f() if recalc_masses: ptr_massZ1[0] = zzcand.z_fir.get_mass() ptr_massZ2[0] = zzcand.z_sec.get_mass() ptr_mass4l[0] = zzcand.get_m4l() # Getting an IndexError since there is a discrepancy in the length # of vectors, like `lep_pt` and `vtxLepFSR_BS_pt`. # ptr_mass4l[0] = calc_mass4l_from_idcs( # tree, lep_idcs, kind="lepFSR" # ) # ptr_mass4l_vtxFSR_BS[0] = calc_mass4l_from_idcs( # tree, lep_idcs, kind="vtxLepFSR_BS" # ) # Save each quartet (ZZcand) as a separate entry in TTree. if outfile_root is not None: new_tree.Fill() if verbose: print(f"** ZZ cand {ndx_zzcand} passed OS Method Sel: **\n" f" CR {cr_str}: {evt_id}, (row {evt_num})") if found_matching_lep_Hindex: # Matching quartet info has been saved. Done with event. # Current zzcand is retained for further analysis. break if keep_one_quartet and not match_lep_Hindex: # Save first valid ZZ cand. break # End loop over quartets. # Final counts. evt_info_d["n_sel_redbkg_evts"] += 1 evt_info_d["n_saved_quartets_3p1f"] += n_saved_quartets_3p1f evt_info_d["n_saved_quartets_2p2f"] += n_saved_quartets_2p2f evt_info_d["n_saved_evts_3p1f"] += (overall_evt_label == '3P1F') evt_info_d["n_saved_evts_2p2f"] += (overall_evt_label == '2P2F') # if fill_hists: # d_hists[name][cr]["mass4l"].Fill(tree.mass4l, new_weight) # d_hists[name][cr]["n_quartets"].Fill(n_valid_ZZcands_OS, 1) # if evt_is_2p2plusf: # cr = "2p2f" # n_quart_3p1f = 0 # n_quart_2p2f = n_valid_ZZcands_OS # evt_info_d["n_good_2p2f_evts"] += 1 evt_info_2p2f_3p1f_d[evt_id] = { "num_combos_2p2f": n_saved_quartets_2p2f, "num_combos_3p1f": n_saved_quartets_3p1f, } # if fill_hists: # h2_n3p1fcombos_n2p2fcombos.Fill(n_quart_2p2f, n_quart_3p1f, 1) print("End loop over events.") print("Event info:") pretty_print_dict(evt_info_d) if outfile_root is not None: print(f"Writing tree to root file:\n{outfile_root}") new_tree.Write() if fill_hists: print(f"Writing hists to root file:\n{outfile_root}") for d_name in d_hists.values(): for d_cr in d_name.values(): for h in d_cr.values(): h.Write() h2_n3p1fcombos_n2p2fcombos.Write() new_file.Close() if outfile_json is not None: save_to_json(evt_info_2p2f_3p1f_d, outfile_json, overwrite=overwrite, sort_keys=False)
'hz', 'tt_semilep', 'ww', 'tt_dilep', 'tt_allhad', 'zz', ] filenames = [] files = [] trees = [] datasets = [] distributions = [] for sample_name in sample_names: aux_filename = filename_template.format(sample_name) aux_file = TFile.Open(aux_filename) aux_tree = aux_file.Get("events") aux_dataset = DataSet.DataSet(aux_tree, sample_name, legend_names[sample_name], cross_sections[sample_name], luminosity, generators[sample_name]) filenames.append(aux_filename) files.append(aux_file) trees.append(aux_tree) datasets.append(aux_dataset) #lepton lep1_e = DistributionPlotter.DistributionPlotter( "lep1_e", "",
gStyle.SetOptStat('emr') gStyle.SetTitleAlign(23) gStyle.SetPadLeftMargin(0.16) gStyle.SetPadBottomMargin(0.16) TGaxis.SetMaxDigits(3) namefiles = options.input.split(',') nfiles = len(namefiles) files = [] print 'number of input files is ' + str(nfiles) for i in range(0, nfiles): print 'opening file ' + str(i) + ': ' + namefiles[i] files.append(TFile.Open(namefiles[i], 'r')) pt_bins = [0, 15, 16, 17, 18, 20, 22, 25, 30, 40, 50, 60, 80, 100] eta_bins = [ -2.4, -2.1, -1.6, -1.2, -0.9, -0.3, -0.2, 0.2, 0.3, 0.9, 1.2, 1.6, 2.1, 2.4 ] lxy_bins = [0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 30, 100] colorlist = [ ROOT.kBlack, ROOT.kRed, ROOT.kGreen + 2, ROOT.kAzure + 1, ROOT.kViolet, ROOT.kOrange, ROOT.kBlue, ROOT.kGray, ROOT.kMagenta + 3, ROOT.kOrange + 2, ROOT.kYellow ] sel = ''
leg = TLegend(xLegLimits[0], yLegLimits[0], xLegLimits[1], yLegLimits[1]) leg.SetFillStyle(0) leg.SetTextSize(legTextSize) leg.SetNColumns(ncolumns) if legHeader is not None: leg.SetHeader(legHeader, 'C') hToCompare, hRatioToCompare, hUncToCompare = [], [], [] for iFile, (inFileName, objName, objType, scale, lambdaParam, normalize, color, marker, fillstyle, fillalpha) in \ enumerate( zip(inFileNames, objNames, objTypes, scales, lambdaParams, normalizes, colors, markers, fillstyles, fillalphas) ): if inDirName: inFileName = join(inDirName, inFileName) inFile = TFile.Open(inFileName) if inFile == None: print(f"ERROR: cannot open {inFileName}. Check your config. Exit!") sys.exit() objToCompare = GetObjectFromFile(inFile, objName) if objToCompare == None: print( f"ERROR: couldn't load the histogram \'{objName}\' in \'{inFileName}\'. Check your config. Exit! " ) sys.exit() hToCompare.append(objToCompare) if 'TH' in objType: hToCompare[iFile].SetName(f'h{iFile}') hToCompare[iFile].SetStats(0) else: hToCompare[iFile].SetName(f'g{iFile}')
import plotTools if __name__ == '__main__': outputdir = "./" basedir = "./pileup/" inputfile_data = basedir + "data_pileup.root" inputfile_bg = basedir + "mc_pileup.root" inputfile_sig = basedir + "sig53X_pileup.root" inputfile_weight = basedir + "pileup_weights.root" if not os.path.isdir(outputdir): os.mkdir(outputdir) outfile = TFile.Open(outputdir + "/pileup_plots.root", "RECREATE") infile_data = TFile.Open(inputfile_data) infile_bg = TFile.Open(inputfile_bg) infile_sig = TFile.Open(inputfile_sig) infile_weight = TFile.Open(inputfile_weight) # Integrated luminosity in fb-1s intlumi = 19.712 # ABCD # set root styles plotTools.SetBoostStyle() ################################################################## leg = plotTools.ConstructLDict(0.6, 0.9, 0.6, 0.87)
def GetNamedHisto(self, name, pr='', rebin=-1): ''' Load an histo from process pr ''' if pr != '': self.SetProcess(pr) if rebin != -1: self.rebin = rebin pr = self.process if ',' in self.process: self.process = self.process.replace(' ', '').split(',') if isinstance(self.process, list): listOfProcess = self.process h = self.GetNamedHisto(name, listOfProcess[0], rebin) for p in listOfProcess[1:]: while p[0] == ' ': p = p[1:] while p[-1] == ' ': p = p[:-1] h.Add(self.GetNamedHisto(name, p, rebin)) return h if ',' in name: listOfNames = name.replace(' ', '').split(',') return self.GetNamedHisto(listOfNames, pr, rebin) elif isinstance(name, list): listOfNames = name h = self.GetNamedHisto(listOfNames[0], pr, rebin) for n in listOfNames[1:]: while n[0] == ' ': n = n[1:] while n[-1] == ' ': n = n[:-1] h.Add(self.GetNamedHisto(n, pr, rebin)) return h filename = self.path + self.fileprefix + pr + '.root' if self.verbose: print ' >> Opening file: ' + filename f = TFile.Open(filename) if self.verbose: print ' >> Looking for histo: ' + name if not hasattr(f, name): if name == self.GetHistoName() and self.syst != '': hnosyst = self.var + '_' + self.chan + '_' + self.level if hasattr(f, hnosyst): #print 'WARNING: no systematic %s for histo %s in process %s!! Returning nominal...'%(self.syst, hnosyst, pr) return else: print 'ERROR: not found histogram ' + name + ' in file: ' + filename return False if self.syst != '': hsyst = name + '_' + self.syst if hasattr(f, hsyst): name = hsyst #else: print 'WARNING: not found systematic %s for histogram %s in sample %s'%(self.syst, name, filename) h = f.Get(name) h.Rebin(self.rebin) nb = h.GetNbinsX() if self.doStackOverflow: h.SetBinContent(nb, h.GetBinContent(nb) + h.GetBinContent(nb + 2)) h.SetBinContent(nb + 2, 0) if not self.IsData: h.Scale(self.lumi) h.SetLineColor(1) h.SetFillStyle(0) h.SetLineWidth(2) h.SetStats(0) h.SetTitle('') h.GetXaxis().SetTitle('') h.GetYaxis().SetTitle('') h.SetDirectory(0) ### This is a patch, the bin-by-bin stat unc are (whatever the reason) not well calculated... ### This is a temporary solution nbins = h.GetNbinsX() integral = h.Integral() if h.Integral() > 0 else 0 entries = h.GetEntries() if self.ReComputeStatUnc: for i in range(1, nbins + 1): bc = h.GetBinContent(i) if h.GetBinContent(i) > 0 else 0 h.SetBinError( i, sqrt(bc * integral / entries) if entries != 0 else 0) if self.doNormalize: h.Scale(1. / (integral if integral != 0 else 1)) return h
def compareEventsToDAS(inputarg, dasname, histname='cutflow', treename=""): """Compare a number of processed events in an output file to the available number of events in DAS.""" dasname = dasname.replace('__', '/') if dasname[0] != '/': dasname = '/' + dasname if args.verbose: print "compareEventsToDAS: %s, %s" % (inputarg, dasname) #start = time.time() # COUNT EVENTS nfiles = "" total_processed = 0 if isinstance(inputarg, list) or isinstance(inputarg, str): filenames = inputarg if isinstance(filenames, str): filenames = [filenames] nfiles = len(filenames) for filename in filenames: file = TFile.Open(filename, 'READ') events_processed = 0 if file == None: continue elif file.IsZombie(): file.Close() continue elif treename: tree = file.Get(treename) if tree: events_processed = tree.GetEntries() else: hist = file.Get(histname) if hist: events_processed = hist.GetBinContent(1) if args.verbose: print "%12d events processed in %s " % (events_processed, filename) total_processed += events_processed file.Close() nfiles = ", %d files" % (nfiles) if nfiles > 1 else "" elif isinstance(inputarg, long) or isinstance(inputarg, int) or isinstance( inputarg, float): total_processed = inputarg else: print bcolors.BOLD + bcolors.FAIL + ' [NG] Did recognize input for "%s": %s' % ( dasname, inputarg) + bcolors.ENDC instance = 'prod/phys03' if 'USER' in dasname else 'prod/global' dascmd = 'das_client --limit=0 --query=\"summary dataset=%s instance=%s\"' % ( dasname, instance) if args.verbose: print dascmd dasargs = shlex.split(dascmd) output, error = subprocess.Popen(dasargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() if not "nevents" in output: print bcolors.BOLD + bcolors.FAIL + ' [NG] Did not find nevents for "%s" in DAS. Return message:' % ( dasname) + bcolors.ENDC print bcolors.FAIL + ' ' + output + bcolors.ENDC return False total_das = Double(output.split('"nevents":')[1].split(',')[0]) fraction = total_processed / total_das if fraction > 1.001: print bcolors.BOLD + bcolors.FAIL + ' [NG] DAS entries = %d, Processed in tree = %d (frac = %.2f > 1%s)' % ( total_das, total_processed, fraction, nfiles) + bcolors.ENDC elif fraction > 0.8: print bcolors.BOLD + bcolors.OKBLUE + ' [OK] DAS entries = %d, Processed in tree = %d (frac = %.2f%s)' % ( total_das, total_processed, fraction, nfiles) + bcolors.ENDC else: print bcolors.BOLD + bcolors.FAIL + ' [NG] DAS entries = %d, Processed in tree = %d (frac = %.2f < 0.8%s)' % ( total_das, total_processed, fraction, nfiles) + bcolors.ENDC return True
label2.SetTextSize(0.03) label2.SetTextAlign(32) l = TLegend(0.15, 0.8, 0.89, 0.87) l.SetNColumns(2) l.SetTextSize(0.03) l.SetLineColor(0) l.SetFillColor(0) ##all histos with event selection! ########################################################### for ch in ['Hct', 'Hut']: for jetcat in ['j3b2', 'j3b3', 'j4b2', 'j4b3', 'j4b4']: for i in ['01']: ver = str(i) out = TFile.Open(era + '/final_' + ch + '_' + jetcat + '_' + ver + '/output_' + ch + '_' + jetcat + '.root') if not os.path.exists(era + '/bdt_plots/' + i): os.makedirs(era + '/bdt_plots/' + i) if era == "2017": tmp = '2017/' elif era == "2018": tmp = '2018/' for method in ['BDT']: trainS = out.Get(tmp + 'final_' + ch + '_' + jetcat + '_' + ver + '/Method_' + method + '/' + method + '/MVA_' + method + '_Train_S') trainB = out.Get(tmp + 'final_' + ch + '_' + jetcat + '_' + ver + '/Method_' + method + '/' + method + '/MVA_' + method + '_Train_B') testS = out.Get(tmp + 'final_' + ch + '_' + jetcat + '_' + ver + '/Method_' + method + '/' + method +
def checkFiles(filelist, directory, clean=False, force=False, cleanBug=False, treename='tree', tag=""): """Check if the file is valid.""" if args.verbose: print "checkFiles: %s, %s" % (filelist, directory) if isinstance(filelist, str): filelist = [filelist] badfiles = [] bugfiles = [] ifound = [] nfiles = len(filelist) #total_processed = 0 indexpattern = re.compile(r".*_(\d+)_[a-z]+%s\.root" % tag) #(?:_[A-Z]+\dp\d+)?(?:_Zmass)? for filename in filelist: match = indexpattern.search(filename) if match: ifound.append(int(match.group(1))) file = TFile.Open(filename, 'READ') isbad = False if file == None: print bcolors.FAIL + '[NG] file %s is None' % ( filename) + bcolors.ENDC badfiles.append(filename) continue elif file.IsZombie(): print bcolors.FAIL + '[NG] file %s is a zombie' % ( filename) + bcolors.ENDC badfiles.append(filename) else: tree = file.Get(treename) if not isinstance(tree, TTree): print bcolors.FAIL + '[NG] no tree found in ' + filename + bcolors.ENDC badfiles.append(filename) elif not isinstance(file.Get('cutflow'), TH1): print bcolors.FAIL + '[NG] no cutflow found in ' + filename + bcolors.ENDC badfiles.append(filename) elif any(s in filename for s in ['DYJets', 'WJets' ]) and tree.GetMaximum('LHE_Njets') > 10: print bcolors.BOLD + bcolors.WARNING + '[WN] %d/%d events have LHE_Njets = %d > 10 in %s' % ( tree.GetEntries(), tree.GetEntries("LHE_Njets>10"), tree.GetMaximum('LHE_Njets'), filename) + bcolors.ENDC bugfiles.append(filename) #if isinstance(tree,TTree): # total_processed += tree.GetEntries() file.Close() if len(badfiles) > 0: print bcolors.BOLD + bcolors.FAIL + "[NG] %s: %d out of %d files %s no tree or cutflow!" % ( directory, len(badfiles), len(filelist), "have" if len(badfiles) > 1 else "has") + bcolors.ENDC for cleanlist, cleanflag in [(badfiles, clean), (bugfiles, cleanBug)]: if len(cleanlist) > 0 and cleanflag: if force: print bcolors.WARNING + ' [WN] removing bad files:' for filename in cleanlist: print " %s" % filename print bcolors.ENDC for filename in cleanlist: os.system("rm %s" % filename) filelist.remove(filename) else: print "\n Bad files:" for filename in cleanlist: print " %s" % filename remove = raw_input( " Do you really want to remove these? [y/n] ") if remove.lower() == 'force': remove = 'y' force = True if remove.lower() == 'quit': exit(0) if remove.lower() == 'y': for filename in cleanlist: print "removing %s..." % filename os.system("rm %s" % filename) filelist.remove(filename) print # TODO: check all chunks (those>imax) if ifound: imax = max(ifound) + 1 if nfiles < imax: imiss = [i for i in range(0, max(ifound)) if i not in ifound] chunktext = ('chunks ' if len(imiss) > 1 else 'chunk ') + ', '.join(str(i) for i in imiss) print bcolors.BOLD + bcolors.WARNING + "[WN] %s missing %d/%d files (%s) ?" % ( directory, len(imiss), nfiles, chunktext) + bcolors.ENDC return False else: print bcolors.BOLD + bcolors.WARNING + "[WN] %s did not find any valid chunk pattern in file list ?" % ( directory) + bcolors.ENDC return len(badfiles) == 0
## \date 2017 ## \author TMVA Team from ROOT import TMVA, TFile, TTree, TCut from subprocess import call from os.path import isfile from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Activation from tensorflow.keras.optimizers import SGD # Setup TMVA TMVA.Tools.Instance() TMVA.PyMethodBase.PyInitialize() output = TFile.Open('TMVA.root', 'RECREATE') factory = TMVA.Factory( 'TMVAClassification', output, '!V:!Silent:Color:DrawProgressBar:Transformations=D,G:AnalysisType=Classification' ) # Load data if not isfile('tmva_class_example.root'): call([ 'curl', '-L', '-O', 'http://root.cern.ch/files/tmva_class_example.root' ]) data = TFile.Open('tmva_class_example.root') signal = data.Get('TreeS') background = data.Get('TreeB')
p.close() p.join() outfiles = [] for mass in args.masses: signame = "SVJ_mZprime{}_mDark20_rinv03_alphapeak".format(mass) mname = "ManualCLs" if args.manualCLs and not "-A" in args.extra else "AsymptoticLimits" sname = "StepA" if args.manualCLs and "-A" in args.extra else "" fname = signame + "/higgsCombine" + sname + cname + "." + mname + ".mH120.ana" + combo + ".root" append = False if args.dry_run: append = True else: # check if limit converged from ROOT import TFile, TTree f = TFile.Open(fname) if f == None: continue t = f.Get("limit") if t == None: continue # 5 expected + 1 observed (+ prefit sometimes) append = t.GetEntries() >= 6 if append: outfiles.append(fname) else: fprint( "Warning: {} limit for mZprime = {} did not converge".format( combo, mass)) # combine outfiles if not args.no_hadd: os.chdir(pwd) outname = "limit_" + combo + cname[4:] + ".root"
def merge_hist(inhist_0="", inhist_list=[], m_hname=""): print 'merge_hist: ===== info === ' print 'final merged hist: ', inhist_0 print 'source hist : ', inhist_list #if os.path.isfile(inhist_0): # tfin_0=TFile.Open(inhist_0, "UPDATE") #else: tfin_0=TFile.Open(inhist_0, "RECREATE") hist_dict_var = {} hist_dict_nom = {} for ih, fhist in enumerate(inhist_list): print "Look at file: ", fhist tfin=TFile.Open(fhist, "READ") ## loop inputs list_hist=tfin.GetListOfKeys() next=TIter(tfin.GetListOfKeys()) for key in list_hist: obj=key.ReadObj() if obj.IsA().InheritsFrom(TH1.Class()): hname=obj.GetName() hist=obj.Clone() hist.SetDirectory(0) dbin14=[0, 75, 100, 120, 130, 150, 180, 200, 225, 255, 290, 325, 370, 440, 540, 680, 1200, 2000] if m_hname=='m4l': hist= hist.Rebin(17, hname, array('d',dbin14)) #if m_hname=='m4l': hist.Rebin(17, hname, dbin14) elif m_hname=='MEM': hist.Rebin(2) ## check hist type cat, sys, var='', '', '' if '-' in hname: cat=hname.split('-')[-2] # histogram name (m4l/MEM) with different channel var=hname.split('-')[1] # variation name sys=hname.split('-')[-1] # up/down else: cat='_'.join(hname.split('_')[1:]) var='Nominal' #if hist.GetBinContent(250)==0: print cat, var, sys #print "Looking at=> ", cat, var, sys if var=='Nominal': if cat not in hist_dict_nom.keys(): hist_dict_nom[cat]=hist hist_dict_nom[cat].SetDirectory(0) else: hist_dict_nom[cat].Add(hist) else: if cat not in hist_dict_var.keys(): hist_dict_var[cat]={} if var not in hist_dict_var[cat].keys(): hist_dict_var[cat][var]={} if sys not in hist_dict_var[cat][var].keys(): hist_dict_var[cat][var][sys]=hist hist_dict_var[cat][var][sys].SetDirectory(0) else: hist_dict_var[cat][var][sys].Add(hist) tfin.Close() hist_dict_ratio = {} for cat in hist_dict_var.keys(): Nbin = hist_dict_nom[cat].GetNbinsX() print "Nbin: ", Nbin ## Define histogram to save error ratio in different cat hname="%s_%s" % (m_hname, cat) hist=hist_dict_nom[cat].Clone(hname) hist.Reset() hist.SetDirectory(tfin_0) if cat not in hist_dict_ratio.keys(): hist_dict_ratio[cat]=hist ## Then loop bin by bin to calculate the ratio in each bin and save into hists for ibin in range(Nbin): y_nom = hist_dict_nom[cat].GetBinContent(ibin+1) sum_ratio = 0 for var in hist_dict_var[cat].keys(): if var=='HOQCD_scale_gg_syst': continue max_error = -999. for sys in hist_dict_var[cat][var].keys(): delta_error = fabs(hist_dict_var[cat][var][sys].GetBinContent(ibin+1)-y_nom) if max_error<delta_error: max_error = delta_error if y_nom==0 or y_nom==max_error: ratio = 0 else: ratio = max_error/y_nom #if ibin>=58 and cat=='Incl_incl_M4lAll_13TeV' and ratio>=0.9 or ratio<-0.9: # print "Bin: ", ibin, "y_nom: ", y_nom, "sys: ", "max_error: ", max_error, "Ratio: ", ratio, "var: ", var sum_ratio += pow(ratio,2) sqrt_ratio = sqrt(sum_ratio) #if cat=='Incl_incl_M4lAll_13TeV': print "Sum_ratio: ", sum_ratio hist_dict_ratio[cat].SetBinContent(ibin+1,sqrt_ratio) tfin_0.cd() for cat in hist_dict_var.keys(): hist_dict_ratio[cat].Write() tfin_0.Close()
##################### ZZTree = False CRZLTree = False ZTree = True ######################## # Data periods options # ######################## #period = "data2016" period = "data2017" #period = "data2018" if(period == "data2016"): data = TFile.Open("root://lxcms03//data3/Higgs/190617/Data_2016/AllData/ZZ4lAnalysis.root") inputTXT = "JSON_calc/SplittedBlocks_2016data_0p5_new.txt" saveAs = "2016data_0p5_reduced" elif(period == "data2017"): data = TFile.Open("root://lxcms03//data3/Higgs/190617/Data_2017/AllData/ZZ4lAnalysis.root") inputTXT = "JSON_calc/SplittedBlocks_2017data_0p5_new.txt" saveAs = "2017data_0p5_reduced" elif(period == "data2018"): data = TFile.Open("root://lxcms03//data3/Higgs/190617/Data_2018/AllData/ZZ4lAnalysis.root") inputTXT = "JSON_calc/SplittedBlocks_2018data_0p5_new.txt" saveAs = "2018data_0p5_reduced" else: print ("Error, wrong period chosen!!!")
return fprompt #*************************************************************************** # Main function cfgFileName = sys.argv[1] outFileName = sys.argv[2] with open(cfgFileName, 'r') as ymlCfgFile: if six.PY2: inputCfg = yaml.load(ymlCfgFile) else: inputCfg = yaml.safe_load(ymlCfgFile) if inputCfg['bkgConfiguration']['getbkgfromMC'] is False: infileDataLowPt = TFile.Open(inputCfg['fileDataLowPt']['filename']) indirDataLowPt = infileDataLowPt.Get(inputCfg['fileDataLowPt']['dirname']) inlistDataLowPt = indirDataLowPt.Get(inputCfg['fileDataLowPt']['listname']) sMassPtCutVarsLowPt = inlistDataLowPt.FindObject(inputCfg['fileDataLowPt']['sparsenameAll']) hEvLowPt = inlistDataLowPt.FindObject(inputCfg['fileDataLowPt']['histoevname']) infileDataHighPt = TFile.Open(inputCfg['fileDataHighPt']['filename']) indirDataHighPt = infileDataHighPt.Get(inputCfg['fileDataHighPt']['dirname']) inlistDataHighPt = indirDataHighPt.Get(inputCfg['fileDataHighPt']['listname']) sMassPtCutVarsHighPt = inlistDataHighPt.FindObject(inputCfg['fileDataHighPt']['sparsenameAll']) hEvHighPt = inlistDataHighPt.FindObject(inputCfg['fileDataHighPt']['histoevname']) infileMCLowPt = TFile(inputCfg['fileMCLowPt']['filename']) indirMCLowPt = infileMCLowPt.Get(inputCfg['fileMCLowPt']['dirname']) inlistMCLowPt = indirMCLowPt.Get(inputCfg['fileMCLowPt']['listname']) sMassPtCutVarsPromptLowPt = inlistMCLowPt.FindObject(inputCfg['fileMCLowPt']['sparsenamePrompt'])
testWJets1200 = [] testWJets2500 = [] ## Open ROOT files print 'Opening files...' eosdir = "root://cmseos.fnal.gov//store/user/jmanagan/MVAtraining_2016_Jan2021/" ## Choosing valid events with appropriate characteristics and cutting the rest seltrain = "isValidTTDecayMode_DeepAK8 == 0 && Tprime2_DeepAK8_Mass < 0 && NJetsAK8_JetSubCalc > 2" seltest = "isValidTTDecayMode_DeepAK8 == 0 && Tprime2_DeepAK8_Mass >= 0" treeVars = vars ## Getting values from trees for each parent particle and either keeping them in an array or adding them together fileTTToSemiLepT = TFile.Open( eosdir + "TTJets_SingleLeptFromT_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_hadd.root", "READ") treeTTToSemiLepT = fileTTToSemiLepT.Get("ljmet") trainTTToSemiLepT = tree2array(treeTTToSemiLepT, treeVars, seltrain) testTTToSemiLepT = tree2array(treeTTToSemiLepT, treeVars, seltest) ## Selection with Single Lept from TBar fileTTToSemiLepTb = TFile.Open( eosdir + "TTJets_SingleLeptFromTbar_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_hadd.root", "READ") treeTTToSemiLepTb = fileTTToSemiLepTb.Get("ljmet") trainTTToSemiLepTb = tree2array(treeTTToSemiLepTb, treeVars, seltrain) testTTToSemiLepTb = tree2array(treeTTToSemiLepTb, treeVars, seltest) ## Selection with signals
def hvt(benchmark = ['B3', 'A1']): hxs = {} hw = {} gxs = {} gw = {} mg = TMultiGraph() for m in massPoints: hxs[m] = TH2F("hxs_M%d" % m, ";;", 50, -0.04, 3.96, 100, 0., 2.) hw[m] = TH2F("hw_M%d" % m, ";;", 50, -0.04, 3.96, 50, 0., 2.) for m in massPoints: file = TFile.Open("HVT/scanHVT_M%s.root" % m, "READ") tree = file.Get("tree") for entry in range(tree.GetEntries()): # Fill mass points only if NOT excluded tree.GetEntry(entry) gH, gF = tree.gv*tree.ch, tree.g*tree.g*tree.cq/tree.gv XsBr = tree.CX0 * tree.BRbb * 1000. # in fb if XsBr < observed[m]: hxs[m].Fill(gH, gF) if tree.total_widthV0/float(m) < width: hw[m].Fill(gH, gF) for b in range(hxs[m].GetNbinsX()*hxs[m].GetNbinsY()): hxs[m].SetBinContent(b, 1. if hxs[m].GetBinContent(b)>0. else 0.) hw[m].SetBinContent(b, 1. if hw[m].GetBinContent(b)>0. else 0.) #hxs[m].Smooth(20) #hw[m].Smooth(20) gxs[m] = getCurve(hxs[m]) for i, g in enumerate(gxs[m]): g.SetLineColor(massColors[m]) g.SetFillColor(massColors[m]) g.SetFillStyle(massFill[m]) #(3345 if i>1 else 3354) g.SetLineWidth(503*(1 if i<2 else -1)) mg.Add(g) if m==3000: gw[m] = getCurve(hw[m]) for i, g in enumerate(gw[m]): g.SetPoint(0, 0., g.GetY()[0]) g.SetLineWidth(501*(1 if i<2 else -1)) g.SetLineColor(920+2) g.SetFillColor(920+1) g.SetFillStyle(3003) mg.Add(g) if options.root: outFile = TFile("plotsLimit/Model.root", "RECREATE") outFile.cd() for m in massPoints: mg[m].Write("X_M%d" % m) mgW.Write("width") outFile.Close() print "Saved histogram in file plotsLimit/Model.root, exiting..." exit() ### plot ### c1 = TCanvas("c1", "HVT Exclusion Limits", 800, 600) c1.cd() c1.GetPad(0).SetTopMargin(0.06) c1.GetPad(0).SetRightMargin(0.05) c1.GetPad(0).SetTicks(1, 1) mg.Draw("AC") #mg.GetXaxis().SetTitle("g_{V} c_{H}") mg.GetXaxis().SetTitle("Higgs and vector boson coupling g_{H}") mg.GetXaxis().SetRangeUser(-3.,3.) mg.GetXaxis().SetLabelSize(0.045) mg.GetXaxis().SetTitleSize(0.045) mg.GetXaxis().SetTitleOffset(1.) #mg.GetYaxis().SetTitle("g^{2} c_{F} / g_{V}") mg.GetYaxis().SetTitle("Fermion coupling g_{F}") mg.GetYaxis().SetLabelSize(0.045) mg.GetYaxis().SetTitleSize(0.045) mg.GetYaxis().SetTitleOffset(1.) mg.GetYaxis().SetRangeUser(-1., 1.) mg.GetYaxis().SetNdivisions(505) # hxs[3500].Draw("CONTZ") drawCMS(LUMI, "", False) #Preliminary # drawAnalysis("XVH"+category, False) # latex = TLatex() # latex.SetNDC() # latex.SetTextFont(62) # latex.SetTextSize(0.06) # latex.DrawLatex(0.10, 0.925, "CMS") # model B g_model = {} for i, b in enumerate(benchmark): g_model[i] = TGraph(1) g_model[i].SetTitle(models_name[b]) g_model[i].SetPoint(0, models_point[b][0], models_point[b][1]) g_model[i].SetMarkerStyle(models_style[b]) g_model[i].SetMarkerColor(models_color[b]) g_model[i].SetMarkerSize(1.5) g_model[i].Draw("PSAME") # text latex = TLatex() latex.SetTextSize(0.045) latex.SetTextFont(42) latex.SetTextColor(630) # for b in benchmark: latex.DrawLatex(models_point[b][0]+0.02, models_point[b][1]+0.02, models_name[b]) latex.SetTextColor(920+2) latex.DrawLatex(-2.8, -0.875, "#frac{#Gamma_{Z'}}{m_{Z'}} > %.0f%%" % (width*100, )) leg = TLegend(0.68, 0.60, 0.95, 0.94) leg.SetBorderSize(1) leg.SetFillStyle(1001) leg.SetFillColor(0) for m in massPoints: leg.AddEntry(gxs[m][0], "m_{Z'} = %.1f TeV" % (m/1000.), "fl") for i, b in enumerate(benchmark): leg.AddEntry(g_model[i], g_model[i].GetTitle(), "P") leg.SetY1(leg.GetY2()-leg.GetNRows()*0.050) leg.SetMargin(0.35) leg.Draw() gxs_ = gxs[massPoints[0]][0].Clone("gxs_") gxs_.SetLineColor(1) # gxs_.SetFillColor(1) latex.SetNDC() latex.SetTextColor(1) latex.SetTextSize(0.04) latex.SetTextFont(52) latex.DrawLatex(0.15, 0.95, "q#bar{q} #rightarrow Z' #rightarrow b#bar{b}") c1.Print("plots/model/HVT.png") c1.Print("plots/model/HVT.pdf") c1.Print("plots/model/HVT.root") c1.Print("plots/model/HVT.C") #g = 0.646879, cH = 0.976246, cF = 1.02433 print "model B = [", 3*0.976246, ",", 0.646879*0.646879*1.02433/3, "]" if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
def runJob(): TMVA.Tools.Instance() TMVA.PyMethodBase.PyInitialize() output = TFile.Open('TMVA_SSSF.root', 'RECREATE') factory = TMVA.Factory( 'TMVAClassification', output, '!V:!Silent:Color:DrawProgressBar:AnalysisType=Classification') # factory = TMVA.Factory('TMVAClassification', output, '!V:!Silent:Color:DrawProgressBar:Transformations=D,G:AnalysisType=Classification') dataloader = TMVA.DataLoader('datasetSSSF04Feb') for br in config.mvaVariables: dataloader.AddVariable(br) for sampleName, sample in config.samples.items(): if config.structure[sampleName]['isData'] == 1: continue print sampleName sample['tree'] = TChain("Events") for f in sample['name']: sample['tree'].Add(f) if config.structure[sampleName]['isSignal'] == 1: dataloader.AddSignalTree(sample['tree'], 1.0) else: dataloader.AddBackgroundTree(sample['tree'], 1.0) # output_dim += 1 dataloader.PrepareTrainingAndTestTree( TCut(config.cut), 'SplitMode=Random:NormMode=NumEvents:!V') factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG0", "!H:!V:NTrees=400:MinNodeSize=1.0%:BoostType=Grad:Shrinkage=0.03:UseBaggedBoost:GradBaggingFraction=0.4:nCuts=100:MaxDepth=1" ) factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG1", "!H:!V:NTrees=400:MinNodeSize=1.0%:BoostType=Grad:Shrinkage=0.03:UseBaggedBoost:GradBaggingFraction=0.4:nCuts=500:MaxDepth=2" ) factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG4D3", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.5:nCuts=500:MaxDepth=3" ) factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG4C3", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.5:nCuts=300:MaxDepth=2" ) factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG4SK01", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.01:UseBaggedBoost:GradBaggingFraction=0.5:nCuts=500:MaxDepth=2" ) factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG4F07", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.7:nCuts=500:MaxDepth=2" ) factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG4F07NT", "!H:!V:NTrees=1000:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.7:nCuts=500:MaxDepth=2" ) factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG4F07NC", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.7:nCuts=1000:MaxDepth=2" ) # Run training, test and evaluation factory.TrainAllMethods() factory.TestAllMethods() factory.EvaluateAllMethods() output.Close()
from string import * import ROOT as rt from ROOT import TFile import plotTools if __name__ == '__main__': outputdir = "./plots_noTopPt/" inputdir = "./results_20140917_summary2/" analyzer = "rzrBoostMC" if not os.path.isdir(outputdir): os.mkdir(outputdir) outfile = TFile.Open(outputdir+"/plots.root","RECREATE") # Integrated luminosity in fb-1s intlumi = 19.7 # ABCD plotTools.SetBoostStyle() print "Will make plots for integrated luminosity of %.3f fb-1" % (intlumi) # define all the datasets we want to plot, and their colors # backgrounds #mc_datasets = ["QCD","TTJets","WJetsToLNu","Wbb","Top","TTX","ZJetsToNuNu","DYJetsToLL","DYToBB","DYToCC","VV","VVV"] #mc_colors = [rt.kMagenta,rt.kRed,rt.kGreen+1,rt.kGreen+3,rt.kCyan,rt.kCyan+2,rt.kOrange,rt.kOrange+2,rt.kOrange+7,rt.kOrange+9,rt.kBlue+1,rt.kBlue-3] mc_datasets = ["QCD" ,"TTJets_noTopPt" ,"WJets" ,"Top" ,"ZJetsToNuNu" ,"DY" ,"rare"] mc_colors = [rt.kMagenta+1,rt.kRed ,rt.kGreen+1,rt.kCyan ,rt.kOrange ,rt.kOrange+2,rt.kBlue+1] mc_titles = [" multijet" ," t#bar{t}"," W+jets" ," single top"," Z#rightarrow#nu#nu+jets"," Drell-Yan" ," VV(V) + t#bar{t}V(V)"]
def main(options, args): gROOT.Reset() gROOT.Reset() #setup integration intConf = ROOT.RooAbsReal.defaultIntegratorConfig() #intConf.Print('v') #intConf.method1D().setLabel('RooAdaptiveGaussKronrodIntegrator1D') #for i in range(1000): # print unitarityTriplet() for f in args: output = TFile.Open(f, 'UPDATE') output.cd() print f[:f.find('.root')].split('-')[0] ws = output.Get(f[:f.find('.root')].split('-')[0]) for rap_bin in range(1, len(jpsi.pTRange)): for pt_bin in range(len(jpsi.pTRange[rap_bin])): if options.testBin is not None: if rap_bin != int(options.testBin[0]) or pt_bin + 1 != int( options.testBin[1]): continue ws.loadSnapshot('pol_snapshot_rap' + str(rap_bin) + '_pt' + str(pt_bin + 1)) PinP = ws.var('fPinP_actual_rap' + str(rap_bin) + '_pt' + str(pt_bin + 1)).getVal() NPinP = ws.var('fNPinP_actual_rap' + str(rap_bin) + '_pt' + str(pt_bin + 1)).getVal() BGinP = ws.var('fBkginP_actual_rap' + str(rap_bin) + '_pt' + str(pt_bin + 1)).getVal() PinNP = ws.var('fPinNP_actual_rap' + str(rap_bin) + '_pt' + str(pt_bin + 1)).getVal() NPinNP = ws.var('fNPinNP_actual_rap' + str(rap_bin) + '_pt' + str(pt_bin + 1)).getVal() BGinNP = ws.var('fBkginNP_actual_rap' + str(rap_bin) + '_pt' + str(pt_bin + 1)).getVal() nPinP = ws.var('nPromptSignal').getVal() * PinP nNPinP = ws.var('nNonPromptSignal').getVal() * NPinP nBGinP = ws.var('nBackgroundSignal').getVal() * BGinP nPinPerr = ws.var('nPromptSignal').getError() * PinP nNPinPerr = ws.var('nNonPromptSignal').getError() * NPinP nBGinPerr = ws.var('nBackgroundSignal').getError() * BGinP nPinNP = ws.var('nPromptSignal').getVal() * PinNP nNPinNP = ws.var('nNonPromptSignal').getVal() * NPinNP nBGinNP = ws.var('nBackgroundSignal').getVal() * BGinNP nPinNPerr = ws.var('nPromptSignal').getError() * PinNP nNPinNPerr = ws.var('nNonPromptSignal').getError() * NPinNP nBGinNPerr = ws.var('nBackgroundSignal').getError() * BGinNP errPinP = ( (1 / (nPinP + nNPinP + nBGinP) + nPinP / pow(nPinP + nNPinP + nBGinP, 2)) * nPinPerr + nPinP / pow(nPinP + nNPinP + nBGinP, 2) * nNPinPerr + nPinP / pow(nPinP + nNPinP + nBGinP, 2) * nBGinPerr) alphaPinP = 1 #PinP*((PinP*(1-PinP))/errPinP/errPinP - 1) betaPinP = 1 #(alphaPinP*(1-PinP) + 2*(PinP-.5))/PinP errNPinP = ( (1 / (nPinP + nNPinP + nBGinP) + nNPinP / pow(nPinP + nNPinP + nBGinP, 2)) * nNPinPerr + nNPinP / pow(nPinP + nNPinP + nBGinP, 2) * nPinPerr + nNPinP / pow(nPinP + nNPinP + nBGinP, 2) * nBGinPerr) alphaNPinP = 1 #NPinP*((NPinP*(1-NPinP))/errNPinP/errNPinP - 1) betaNPinP = 1 #(alphaNPinP*(1-NPinP) + 2*(NPinP-.5))/NPinP errBGinP = ( (1 / (nPinP + nNPinP + nBGinP) + nBGinP / pow(nPinP + nNPinP + nBGinP, 2)) * nBGinPerr + nBGinP / pow(nPinP + nNPinP + nBGinP, 2) * nPinPerr + nBGinP / pow(nPinP + nNPinP + nBGinP, 2) * nNPinPerr) alphaBGinP = 1 #BGinP*((BGinP*(1-BGinP))/errBGinP/errBGinP - 1) betaBGinP = 1 #(alphaBGinP*(1-BGinP) + 2*(BGinP-.5))/BGinP errPinNP = ( (1 / (nPinNP + nNPinNP + nBGinNP) + nPinNP / pow(nPinNP + nNPinNP + nBGinNP, 2)) * nPinNPerr + nPinNP / pow(nPinNP + nNPinNP + nBGinNP, 2) * nNPinNPerr + nPinNP / pow(nPinNP + nNPinNP + nBGinNP, 2) * nBGinNPerr) alphaPinNP = 1 #PinNP*((PinNP*(1-PinNP))/errPinNP/errPinNP - 1) betaPinNP = 1 #(alphaPinNP*(1-PinNP) + 2*(PinNP-.5))/PinNP errNPinNP = ( (1 / (nPinNP + nNPinNP + nBGinNP) + nNPinNP / pow(nPinNP + nNPinNP + nBGinNP, 2)) * nNPinNPerr + nNPinNP / pow(nPinNP + nNPinNP + nBGinNP, 2) * nPinNPerr + nNPinNP / pow(nPinNP + nNPinNP + nBGinNP, 2) * nBGinNPerr) alphaNPinNP = 1 #NPinNP*((NPinNP*(1-NPinNP))/errNPinNP/errNPinNP - 1) betaNPinNP = 1 #(1-NPinNP)*((NPinNP*(1-NPinNP))/errNPinNP/errNPinNP - 1) #if NPinNP*(1-NPinNP)/errNPinNP/errNPinNP <= 1 and NPinNP < .1: # # alphaNPinNP = 1 # betaNPinNP = (1-NPinNP)/NPinNP #elif NPinNP*(1-NPinNP)/errNPinNP/errNPinNP <= 1 and NPinNP > .9: # # betaNPinNP = 1 # alphaNPinNP = NPinNP/(1-NPinNP) errBGinNP = ( (1 / (nPinNP + nNPinNP + nBGinNP) + nBGinNP / pow(nPinNP + nNPinNP + nBGinNP, 2)) * nBGinNPerr + nBGinNP / pow(nPinNP + nNPinNP + nBGinNP, 2) * nNPinNPerr + nBGinNP / pow(nPinNP + nNPinNP + nBGinNP, 2) * nPinNPerr) alphaBGinNP = 1 #BGinNP*((BGinNP*(1-BGinNP))/errBGinNP/errBGinNP - 1) betaBGinNP = 1 #(alphaBGinNP*(1-BGinNP) + 2*(BGinNP-.5))/BGinNP print 'rap = ', rap_bin, ' pt = ', pt_bin + 1 print 'fPinP = ', PinP, ' +- ', errPinP print 'fNPinP = ', NPinP, ' +- ', errNPinP print 'fBGinP = ', BGinP, ' +- ', errBGinP print 'fPinNP = ', PinNP, ' +- ', errPinNP print 'fNPinNP = ', NPinNP, ' +- ', errNPinNP print 'fBGinNP = ', BGinNP, ' +- ', errBGinNP print print ' P in P alpha = ', alphaPinP, ' beta = ', betaPinP print 'NP in P alpha = ', alphaNPinP, ' beta = ', betaNPinP print 'BG in P alpha = ', alphaBGinP, ' beta = ', betaBGinP print ' P in NP alpha = ', alphaPinNP, ' beta = ', betaPinNP print 'NP in NP alpha = ', alphaNPinNP, ' beta = ', betaNPinNP print 'BG in NP alpha = ', alphaBGinNP, ' beta = ', betaBGinNP print d = ws.data('data_rap' + str(rap_bin) + '_pt' + str(pt_bin + 1)) dpr = d.reduce('mlRegion == mlRegion::promptSignal') dnp = d.reduce('mlRegion == mlRegion::nonPromptSignal') print 'N_Events (l < .1 mm): ', dpr.sumEntries() print 'N_Events (l > .1 mm): ', dnp.sumEntries() output.Close()
def runPlotter(inDir, options): """ Loop over the inputs and launch jobs """ from ROOT import TFile jsonFile = open(options.json, 'r') procList = json.load(jsonFile, encoding='utf-8').items() # Make a survey of *all* existing plots plots = [] xsecweights = {} tot_ngen = {} missing_files = [] baseRootFile = None if inDir.endswith('.root'): baseRootFile = TFile.Open(inDir) plots = list(set(getAllPlotsFrom(tdir=baseRootFile, chopPrefix=True))) else: for proc_tag in procList: for desc in proc_tag[1]: data = desc['data'] isData = getByLabel(desc, 'isdata', False) mctruthmode = getByLabel(desc, 'mctruthmode') for process in data: dtag = getByLabel(process, 'dtag', '') split = getByLabel(process, 'split', 1) dset = getByLabel(process, 'dset', dtag) try: ngen = tot_ngen[dset] except KeyError: ngen = 0 for segment in range(0, split): eventsFile = dtag if split > 1: eventsFile = dtag + '_' + str(segment) if mctruthmode: eventsFile += '_filt%d' % mctruthmode rootFileUrl = inDir + '/' + eventsFile + '.root' rootFile = openTFile(rootFileUrl) if rootFile is None: missing_files.append(eventsFile + '.root') continue iplots = getAllPlotsFrom(tdir=rootFile) if not isData: ngen_seg = getNormalization(rootFile) ngen += ngen_seg rootFile.Close() plots = list(set(plots + iplots)) tot_ngen[dset] = ngen # Calculate weights: for process in data: dtag = getByLabel(process, 'dtag', '') dset = getByLabel(process, 'dset', dtag) brratio = getByLabel(process, 'br', [1]) xsec = getByLabel(process, 'xsec', 1) if dtag not in xsecweights.keys(): try: ngen = tot_ngen[dset] xsecweights[str(dtag)] = brratio[0] * xsec / ngen except ZeroDivisionError: if isData: xsecweights[str(dtag)] = 1.0 else: print "ngen not properly set for", dtag if len(missing_files) and options.verbose > 0: print 20 * '-' print "WARNING: Missing the following files:" for filename in missing_files: print filename print 20 * '-' # Apply mask: if len(options.plotMask) > 0: masked_plots = [_ for _ in plots if options.plotMask in _] plots = masked_plots plots.sort() # Now plot them if options.jobs == 0: for plot in plots: makePlot(plot, inDir, procList, xsecweights, options) else: from multiprocessing import Pool pool = Pool(options.jobs) tasklist = [(p, inDir, procList, xsecweights, options) for p in plots] pool.map(makePlotPacked, tasklist) if baseRootFile is not None: baseRootFile.Close()
quantity = [ 'nJet', 'nBJet', '0JetpT', 'RawMET', 'HT', 'mT', 'mT2W', 'Central', 'dPhi' ] cutstart = [0, 0, 0, 0, 0, 0, 0, 0, 0] #cutend = [1,1,1,1,1,1,1,1,1] cutend = [10, 10, 1000, 2500, 2000, 500, 500, 20, 100] cutstep = [1, 1, 20, 50, 50, 20, 20, 1, 4] binsize = [1, 1, 10, 20, 20, 10, 10, 0.01, 0.04] evsel = [3, 5, 7, 6, 11, 11, 13, 15, 18] #evsel=[16,17,18,19,19,19,19,19,19] #evsel=[3,3,3,3,3,3] cuts = ["bjets == 1/2"] cutsShort = ["nBJeteq12"] file_TTbar = TFile.Open(base + pu + '_TTbar_his.root') file_BosonJets = TFile.Open(base + pu + '_BosonJets_his.root') file_TopJets = TFile.Open(base + pu + '_TopJets_his.root') file_DiBoson = TFile.Open(base + pu + '_DiBoson_his.root') file_STC = TFile.Open(base + pu + '_STCfirst_his.root') file_NM1 = TFile.Open(base + pu + '_NM1_his.root') file_NM2 = TFile.Open(base + pu + '_NM2_his.root') file_NM3 = TFile.Open(base + pu + '_NM3_his.root') file_NM3_stoponly = TFile.Open(base + pu + '_NM3_his_stoponly.root') file_STC_stoponly = TFile.Open(base + pu + '_STCfirst_his_stoponly.root') file_NM1_stoponly = TFile.Open(base + pu + '_NM1_his_stoponly.root') file_NM2_stoponly = TFile.Open(base + pu + '_NM2_his_stoponly.root') i = 0
if mode == 'BDT': #input = '/mnt/t3nfs01/data01/shome/gaperrin/VHbb/CMSSW_7_4_7/src/HiggsAnalysis/CombinedLimit/V24/SCAHINGDC_v5_CSV_6/' #input_ = '/mnt/t3nfs01/data01/shome/gaperrin/VHbb/CMSSW_7_4_3/src/Xbb/python/logs_v25/MSCACHINGSPLIT_eval_v8_sigPS_All_rmvbr_VH_7/Limits//' #input_ = '/mnt/t3nfs01/data01/shome/vlohanes/CMSSW_10_1_0/src/Xbb/python/logs_Zll2016Nano_V4/rundc_fixednoPU-v3/Limits' input_ = '%s/vhbb_TH_%s.root' % (path_, channel) print 'input_ is', input_ input_ = '%s/vhbb_TH_%s.root' % (path_, channel) print 'input_ is', input_ #'ZeeMass_lowpt':'ZeeMass_lowpt',if not 'ZeeBDT_lowpt' in input_: continue #if not ('ZeeMassVV_highpt' in input_ or 'ZeeMass_highpt' in input_): continue #if not ('ZuuMassVV_highpt' in input_ or 'ZuuMass_highpt' in input_): continue input = TFile.Open(input_, 'read') print 'open', input if mode == 'Mjj': input = TFile.Open( path + '/vhbb_TH_Mjj_' + Abin + '_M' + mass + '_' + channel + '.root', 'read') print 'open', path + '/vhbb_TH_Mjj_' + Abin + '_M' + mass + '_' + channel + '.root' print 'input:', input print 'The MCs are', MCs for MC in MCs: #if not 'Top' in MC: continue print MC print 'The systs are' print systs for syst in systs:
#!/usr/bin/env python import ROOT from ROOT import TFile, TCanvas ## Run by calling in the shell: ## python controlPlots.py ## Plot b-tagged jet transverse momentum for data # Open the file fiData = TFile.Open( " root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/LONG_EXERCISES/MtopFromEbPeak/Data13TeV_MuonEG_2015D_v4.root", "READ") # Get the tree treeData = fiData.Get("data") # Make a new TCanvas c1 = TCanvas("canvas 1", "c1", 600, 600) c1.cd() # Draw the variable treeData.Draw("Jet_pt", "Jet_CombIVF>0.605") # Save the plot c1.SaveAs("plotData.png") ## Plot b jet generated transverse momentumm for MC # Open the file fiMC = TFile.Open( " root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/LONG_EXERCISES/MtopFromEbPeak/MC13TeV_TTJets.root", "READ") # Get the tree
from matplotlib.offsetbox import OffsetImage, AnnotationBbox from matplotlib.cbook import get_sample_data from matplotlib._png import read_png import subprocess def round_sig(x, sig=2): return round(x, sig - int(floor(log10(abs(x)))) - 1) selectDirNames = ("-50 < <y> < -30", "-30 < <y> < -10", "-10 < <y> < 10", "10 < <y> < 30") wireGroups = ("S0-S15", "S16-S31") colour = ("green", "purple", "orange") f = TFile.Open('TrackerAlignment.root') if f: print("is open") else: print("Not found") ticks = [] yMin = -1 yMax = 3 plt.figure(1) axes = plt.gca() for i_wireGroup in range(0, len(wireGroups)): colourDirs = colour[i_wireGroup] wireGroupName = wireGroups[i_wireGroup] for i_dirNames in range(0, len(selectDirNames)):
def main(argv): #Usage controls from OptionParser parser_usage = "outputfile.root filename1.root ... filenameN.root" parser = OptionParser(usage = parser_usage) (options, args) = parser.parse_args(argv) if(len(args) == 0): parser.print_help() return if(SCAN_OVER_ENERGY and SCAN_OVER_THETA): print "ERROR: both scan flags turned on! Please select either energy or theta to scan over..." return if(not SCAN_OVER_ENERGY and not SCAN_OVER_THETA): print "ERROR: neither scan flags turned on! Please select either energy or theta to scan over..." return c1 = TCanvas("c1","c1",1600,900) file_list = [] curr_P_val = 0.1 for i in range(1,len(args)): file_list.append(TFile.Open(argv[i],'read')) scan_val_arr = array('d',[]) scan_val_arr_arr_err = array('d',[]) effic_gauscore_arr = array('d',[]) effic_gauscore_err_arr = array('d',[]) effic_anyquality_arr = array('d',[]) effic_anyquality_err_arr = array('d',[]) effic_gauscore_1show_arr = array('d',[]) effic_gauscore_1show_err_arr = array('d',[]) effic_anyquality_1show_arr = array('d',[]) effic_anyquality_1show_err_arr = array('d',[]) # curr_P_val = 0.1 for i in range(0,len(file_list)): print "Current file: " + argv[i+1] #For normalization # h_curr_showers = file_list[i].Get("pi_pm_gun_hists/h_NFCALShowers") h_ThrownPionP_curr = TH1F() h_ThrownPhotonTheta_curr = TH1F() h_ThrownPionP_curr = file_list[i].Get("pi_pm_gun_hists/h_ThrownPionP") h_ThrownPhotonTheta_curr = file_list[i].Get("pi_pm_gun_hists/h_ThrownPionTheta") norm_count = h_ThrownPionP_curr.GetEntries() if(norm_count < MIN_EVENTS): continue # atleast_one_shower = norm_count-h_curr_showers.GetBinContent(1) #Bin 1 corresponds to 0 showers # curr_P_val = my_gaus_fit.GetParameter(1) curr_P_val = h_ThrownPionP_curr.GetBinLowEdge( h_ThrownPionP_curr.GetMaximumBin()+1 ) curr_theta_val = h_ThrownPhotonTheta_curr.GetBinLowEdge( h_ThrownPhotonTheta_curr.GetMaximumBin()+1 ) print "Current P: " + str(curr_P_val) print "Current theta: " + str(curr_theta_val) + "\n" BELOW_MEAN_TO_FIT = E_BELOWTHROWN_TOFIT*curr_P_val/2 ABOVE_MEAN_TO_FIT = E_BELOWTHROWN_TOFIT*curr_P_val/2 h_curr = file_list[i].Get("pi_pm_gun_hists/h_ChargedTrackHypothesis_P") my_gaus_fit = TF1("my_gaus_fit","gausn",0.001,12.) my_gaus_fit.SetParLimits(0,0,100000) my_gaus_fit.SetParLimits(1,curr_P_val-0.2,curr_P_val+0.1) my_gaus_fit.SetParLimits(2,0.005,0.4) my_gaus_fit.SetNpx(1000); h_curr.GetXaxis().SetRangeUser(curr_P_val-3,curr_P_val+3.0) h_curr.Fit(my_gaus_fit,"Q","",curr_P_val-BELOW_MEAN_TO_FIT,curr_P_val+ABOVE_MEAN_TO_FIT) h_curr.Fit(my_gaus_fit,"QL","",curr_P_val-BELOW_MEAN_TO_FIT,curr_P_val+ABOVE_MEAN_TO_FIT) if(POLY_ORDER>=1): gaus_fit_amplitude = my_gaus_fit.GetParameter(0) gaus_fit_mean = my_gaus_fit.GetParameter(1) gaus_fit_sigma = my_gaus_fit.GetParameter(2) gaus_plus_poly_fit = TF1("gaus_plus_poly_fit","gausn+pol"+str(POLY_ORDER)+"(3)",0.001,12.) gaus_plus_poly_fit.SetParameter(0,gaus_fit_amplitude) gaus_plus_poly_fit.SetParameter(1,gaus_fit_mean) gaus_plus_poly_fit.SetParameter(2,gaus_fit_sigma) gaus_plus_poly_fit.SetParLimits(0,0,100000) gaus_plus_poly_fit.SetParLimits(1,curr_P_val-0.2,curr_P_val+0.1) gaus_plus_poly_fit.SetParLimits(2,0.005,0.4) h_curr.Fit(gaus_plus_poly_fit,"Q","",curr_P_val-BELOW_MEAN_TO_FIT,curr_P_val+ABOVE_MEAN_TO_FIT) h_curr.Fit(gaus_plus_poly_fit,"QL","",curr_P_val-BELOW_MEAN_TO_FIT,curr_P_val+ABOVE_MEAN_TO_FIT) twogaus_plus_poly_fit = TF1("twogaus_plus_poly_fit","gausn+gausn(3)+pol"+str(POLY_ORDER)+"(6)",0.001,12.) twogaus_plus_poly_fit.SetParameter(0,gaus_plus_poly_fit.GetParameter(0)) twogaus_plus_poly_fit.SetParameter(1,gaus_plus_poly_fit.GetParameter(1)) twogaus_plus_poly_fit.SetParameter(2,gaus_plus_poly_fit.GetParameter(2)) twogaus_plus_poly_fit.SetParameter(6,gaus_plus_poly_fit.GetParameter(3)) twogaus_plus_poly_fit.SetParameter(7,gaus_plus_poly_fit.GetParameter(4)) twogaus_plus_poly_fit.SetParameter(8,gaus_plus_poly_fit.GetParameter(5)) twogaus_plus_poly_fit.SetParLimits(0,0,100000) twogaus_plus_poly_fit.SetParLimits(1,curr_P_val-0.2,curr_P_val+0.1) twogaus_plus_poly_fit.SetParLimits(2,0.005,0.4) twogaus_plus_poly_fit.SetParLimits(3,0,100000) twogaus_plus_poly_fit.SetParLimits(4,curr_P_val-0.2,curr_P_val+0.1) twogaus_plus_poly_fit.SetParLimits(5,0.005,0.4) h_curr.Fit(twogaus_plus_poly_fit,"Q","",curr_P_val-BELOW_MEAN_TO_FIT,curr_P_val+ABOVE_MEAN_TO_FIT) h_curr.Fit(twogaus_plus_poly_fit,"QL","",curr_P_val-BELOW_MEAN_TO_FIT,curr_P_val+ABOVE_MEAN_TO_FIT) c1.SaveAs(".plots/FitE_"+str(curr_P_val)+".png") effic_gauscore_arr.append((my_gaus_fit.GetParameter(0)/h_curr.GetBinWidth(0))/norm_count) effic_gauscore_err_arr.append(my_gaus_fit.GetParError(0)/h_curr.GetBinWidth(0)/norm_count) # effic_anyquality_arr.append(atleast_one_shower/norm_count) # effic_anyquality_err_arr.append(0) if(SCAN_OVER_ENERGY): scan_val_arr.append(curr_P_val) if(SCAN_OVER_THETA): scan_val_arr.append(curr_theta_val) scan_val_arr_arr_err.append(0) # curr_P_val+=0.05 gr_gauscore_effic = TGraphErrors( len(file_list), scan_val_arr, effic_gauscore_arr, scan_val_arr_arr_err, effic_gauscore_err_arr) gr_gauscore_effic.SetMarkerStyle(15) gr_gauscore_effic.SetMarkerSize(1.2) gr_gauscore_effic.SetMarkerColor(kBlue) gr_gauscore_effic.SetName("gr_gauscore_effic") gr_gauscore_effic.SetTitle("Efficiency at 1 GeV") gr_gauscore_effic.GetXaxis().SetTitle("Photon #theta (degrees)") gr_gauscore_effic.GetYaxis().SetTitle("Efficiency") # gr_gauscore_effic.GetXaxis().SetRangeUser(0,12.) gr_gauscore_effic.Draw("AP") c1.SaveAs("GaussianCoreEfficiency.png") # gr_anyquality_effic.Draw("AP") # c1.SaveAs("AnyQualityEfficiency.png") f_out = TFile(argv[0],"RECREATE") f_out.cd() gr_gauscore_effic.Write() # gr_anyquality_effic.Write() # gr_gauscore_1show_effic.Write() # gr_anyquality_1show_effic.Write() f_out.Close() print("Done ")
def main(args): # Measures execution time start_time = time.time() # Take the input file as a 1st command line argument input_file = TFile.Open(args.path_to_file, "READ") input_tree = input_file.apv_reco # Create output root file. # Create output root file before the tree!!! It prevents memory leakage output_file = TFile('./' + args.path_to_file + '_TRANSFORMED.root', "RECREATE") # Create output tree output_tree = TTree('data', 'Extracted Data') # Create variables that will be written in the output tree n_hits = array.array('i', [0]) pad = array.array('i', [0] * 64 * 4 * 8) sector = array.array('i', [0] * 64 * 4 * 8) layer = array.array('i', [0] * 64 * 4 * 8) energy = array.array('f', [0.0] * 64 * 4 * 8) # Create branches in the output tree for these variables output_tree.Branch('n_hits', n_hits, 'n_hits/I') output_tree.Branch('pad', pad, 'pad[n_hits]/I') output_tree.Branch('sector', sector, 'sector[n_hits]/I') output_tree.Branch('layer', layer, 'layer[n_hits]/I') output_tree.Branch('energy', energy, 'energy[n_hits]/F') n_events = input_tree.GetEntries() # Loop through all events in the input tree for idx, event in enumerate(input_tree): # This for debuging. Check on 10k events that everything works. # Then launch the whole file # if idx == 10000: # break # Prints to estimate the end and to be sure its not dead. if idx % (1000) == 0: time_min = (time.time() - start_time) // 60 time_sec = (time.time() - start_time) % 60 print('Event: {} out of {};'.format(idx, n_events), end=' ') print('{} min {} sec'.format(time_min, time_sec)) j = 0 # Loop through all arrays simultaniously in this event. # These are arrays with n_signals size. for apv_id, apv_ch, signal, nn, tau, t0, t1 in zip( event.apv_id, event.apv_ch, event.apv_signal_maxfit, event.apv_nn_output, event.apv_fit_tau, event.apv_fit_t0, event.apv_bint1): # If signal is bad -- go next if (tau < 1 or tau > 3 or signal < 0. or signal > 2000. or t0 < (t1 - 2.7) or t0 > (t1 - 0.5) or nn < args.nn): continue # Choose APV channel Maps based on apv_id if apv_id < 4: apv_map = ApvMaps.tb15_slave if apv_id % 2 == 1 else ApvMaps.tb15_master elif 4 <= apv_id < 14: apv_map = ApvMaps.tb16_slave_divider if apv_id % 2 == 1 else ApvMaps.tb16_master_divider elif apv_id == 14: apv_map = ApvMaps.tb16_master_tab_divider elif apv_id == 15: apv_map = ApvMaps.tb16_slave_tab_divider # Write sector, pad, layer of the signal sector[j] = apv_map[apv_ch] // 64 pad[j] = apv_map[apv_ch] % 64 layer[j] = apv_id // 2 if (pad[j] < 20 or sector[j] == 0 or sector[j] == 3 or layer[j] == 7 or bad_pad(sector[j], pad[j], layer[j])): continue # Return hit's energy in MIP energy[j] = calib_graphs[apv_id].Eval( signal if signal < 1450. else 1450.) j += 1 n_hits[0] = j output_tree.Fill() output_tree.Write() output_file.Close() print("Hooray, extracted tree file is ready, take it :3")
'jet_pt_0b_loose_not_tight_Wtagged', 'jet_pt_0b_tight_Wtagged', 'jet_pt_0b_not_loose_Wtagged', 'jet_pt_2b_not_loose_Wtagged', 'jet_pt_2b_tight_Wtagged', 'jet_pt_2b_loose_not_tight_Wtagged' ] jet_pt_var_bin = np.array((350., 400., 500., 600., 700., 800., 1200.)) ljet_pt = [ 'ljet_pt_0b_loose_not_tight_Wtagged', 'ljet_pt_0b_tight_Wtagged', 'ljet_pt_0b_not_loose_Wtagged', 'ljet_pt_2b_not_loose_Wtagged', 'ljet_pt_2b_tight_Wtagged', 'ljet_pt_2b_loose_not_tight_Wtagged' ] ljet_pt_var_bin = np.array( (500., 550., 600., 650., 700., 750., 800., 850., 900., 1400.)) #Data & background samples f1 = TFile.Open('/cephfs/user/s6marahm/output/abcd_data.root', "update") for p in jet_pt: n_bincontent_one = f1.Get(p) n_bincontent_one.SetName(p + '_old') n_bincontent_one.Write() temp = ROOT.TH1F(p, p, (len(jet_pt_var_bin) - 1), jet_pt_var_bin) temp.Sumw2() temp = n_bincontent_one.Rebin((len(jet_pt_var_bin) - 1), p, jet_pt_var_bin) temp.Write() for p in ljet_pt: n_bincontent_one = f1.Get(p) n_bincontent_one.SetName(p + '_old') n_bincontent_one.Write() temp = ROOT.TH1F(p, p, (len(ljet_pt_var_bin) - 1), ljet_pt_var_bin) temp.Sumw2() temp = n_bincontent_one.Rebin((len(ljet_pt_var_bin) - 1), p,