Beispiel #1
0
#################

import ROOT, Imports, sys
from Imports import *
sys.path.append('./MassFitting/')
from fittingDict import fittingDict

getData = True  # Load data.
makesWeights = True  # Generate sWeights, write to workspace. Requires getData.
makeFriendTree = True  # create friend tree for simple future sweight plotting. Requires makesWeights.
plotVariable = False  # make an sPlot using sWeights in RooDataSet from workspace.
testFriendTree = True  # test sWeights from friend tree to do an sPlot.

inputdir = "/dcache/bfys/scalo/binned_files/"
outputdir = "/dcache/bfys/cpawley/sWeights/"
cuts = Imports.getDataCuts()

#folders_dict = {"39":["2018_MagDown",2155] , "31":["2017_MagDown", 1843], "40":["2016_MagDown",1859], "41":["2015_MagDown", 579], "42":["2012_MagDown", 1155], "43":["2011_MagDown", 907], "45":["2011_MagUp", 817], "46":["2012_MagUp", 1342], "47":["2015_MagUp", 370], "48":["2016_MagUp", 1771], "49":["2017_MagUp", 1839], "50":["2018_MagUp", 2298] } #a dictionary containing the details of the all the years' data according to joblog.txt

folders_dict = {
    "42": ["2012_MagDown", 1155],
    "46": ["2012_MagUp", 1342]
}  #a dictionary containing the details of the all the years' data according to joblog.txt

y_bins = ["_y2.0-2.5", "_y2.5-3.0", "_y3.0-3.5", "_y3.5-4.0"]
pt_bins = [
    "_pt3000-4000", "_pt4000-5000", "_pt5000-6000", "_pt6000-7000",
    "_pt7000-8000", "_pt8000-10000", "_pt10000-20000"
]
particle_types = ["Lc", "Xic"]
Beispiel #2
0
def main():
	#a dictionary containing the details of the all the years' data according to joblog.txt
	#Run 1 is automatically Lc, and Run 2 has particle specified.
	folders_dict = DATA_jobs_Dict

	path = [TUPLE_PATH_NOTRIG,TUPLE_PATH]
	
	for PATH in path:
		if PATH == TUPLE_PATH:
			trig = True
		else:
			trig = False
		
		#vestige of using turbo output for run 2 data. Still present in case we want to switch back to using Turbo line.
		withTurbo = False
		
		for element in folders_dict:
			if int(element) > 41 and int(element) < 47:
			   extra_variables = [
					"lcplus_Hlt1TrackAllL0Decision_TOS", 
					"lcplus_Hlt2CharmHadD2HHHDecision_TOS",
					"lcplus_L0HadronDecision_TOS",
					]
			   cutsRun = 1
			   particle = "Lc"
			else:
			   extra_variables = [
					"nSPDHits", 
					"nTracks",
			   		"lcplus_L0HadronDecision_TOS",
					"lcplus_Hlt1TrackMVADecision_TOS",
					"lcplus_Hlt2CharmHadXicpToPpKmPipTurboDecision_TOS",
					"lcplus_Hlt2CharmHadLcpToPpKmPipTurboDecision_TOS",]
			   particle = "Lc"
			   cutsRun = 2
			   
			name = folders_dict[element][0]
			subjobs = folders_dict[element][1]
			saving_directory = PATH + name + "_clusters/"
			
			cuts = Imports.getDataCuts(cutsRun, trig)
			
			if not os.path.exists(saving_directory):
			   os.makedirs(saving_directory)
			   
			file_directory = RAW_TUPLE_PATH + element
			
			print ("\nStarting process for " + name)
				
			step = subjobs//20 #carry out the process in 20 clusters of datafiles to avoid memory overflow
			Max = step
			Min = 0

		# Loop used to apply global cuts on the data
			print("Creation of Clusters")
			n = 20
			i = 0
			while (Max <= subjobs):
				#FOR THE PROGRASSION BAR
				if i < n:
					j = (i + 1) / n
					sys.stdout.write('\r')
					sys.stdout.write("[%-20s] %d%%" % ('='*int(20*j), 100*j))
					sys.stdout.flush()
					i += 1
				
				if Max == Min:
					break
				strip_n_save(Min, Max, cuts, file_directory, saving_directory, extra_variables, particle)
				temp = Max
				if (Max+step > subjobs):
					Max = subjobs
				else:
					Max += step
				Min = temp

			clusters = os.listdir(saving_directory)
		
			print("\n\nTChaining the clusters")
			final_chain = TChain("DecayTree")
			n = len(clusters)
			i = 0
			for element in clusters:
				if i < n:
					j = (i + 1) / n
					sys.stdout.write('\r')
					sys.stdout.write("[%-20s] %d%%" % ('='*int(20*j), 100*j))
					sys.stdout.flush()
					i += 1
				final_chain.Add(saving_directory + element)

			
			if not os.path.exists(PATH + name + "/bins"):
			   os.makedirs(PATH + name + "/bins")
			saving_dir = PATH + name + "/bins/"
			print("\n\nCreating the final files")
			split_in_bins_n_save(final_chain, saving_dir, withTurbo, particle) # split the datafile into mass-y-pt bins

			print ("\nProcess completed for " + name)
	
		#CREATION OF THE TOTAL YEAR DATA FILES (e.g. 2012_MagUp/Xic_Total.root)
		print("\Creation of the Total Year data files")
		mother_particle = ["Xic", "Lc"]
		BASE_PATH = PATH
		
		n = len(os.listdir(BASE_PATH))
		p = 0
		for i in os.listdir(BASE_PATH):
			if p < n:
					j = (p + 1) / n
					sys.stdout.write('\r')
					sys.stdout.write("[%-20s] %d%%" % ('='*int(20*j), 100*j))
					sys.stdout.flush()
					p += 1
					
			if "cluster" in i:
				continue
		
			for part in mother_particle:
				totfile = ROOT.TFile.Open(BASE_PATH + i + "/{}_total.root".format(part),"RECREATE")
				totfile.cd()
				tree = TChain("DecayTree")

				for j in os.listdir(BASE_PATH + i +"/bins/ybins"):
						if part in j:
								tree.Add(BASE_PATH + i +"/bins/ybins/"+j)
				tree.Write()
				totfile.Close()
				del totfile
		
		print("\nDeleting Clusters")
		os.system("rm -rf {}*_clusters".format(BASE_PATH))
		
		print("\nNTuple Preparation is done, happy analysis!")
def main(argv):

    ROOT.gROOT.SetBatch(True)  #STOP SHOWING THE GRAPH FOR ROOT

    try:
        opts, args = getopt.getopt(argv, "hstp")
    except getopt.GetoptError:
        print("The arguments are wrong")
        sys.exit(2)

    options = []
    arguments = []

    for opt, arg in opts:
        options.append(opt)
        arguments.append(arg)

    if not options:
        options = ["-s", "-t", "-p"]

    if "-h" in options:
        print(
            textwrap.dedent("""\

			Welcome to the efficiencies.py script.

			The parameters are
				-h : help
				-s : Selection
				-t : Trigger
				-p : PID

			Running with no parameter will output all the data at once.
			"""))

        sys.exit()

    for opt in options:

        if opt == "-s":

            selecEffDict = {}
            years = []

            print("\nCreation of the selection efficiency files")

            n = len(MC_jobs_Dict)
            i = 0

            for job in MC_jobs_Dict:
                #FOR THE PROGRESSION BAR
                if i < n:
                    j = (i + 1) / n
                    sys.stdout.write('\r')
                    sys.stdout.write("[%-20s] %d%%" %
                                     ('=' * int(20 * j), 100 * j))
                    sys.stdout.flush()
                    i += 1
                if job == "NA":
                    continue

                particle = MC_jobs_Dict[job][3]
                year = MC_jobs_Dict[job][0]
                pol = MC_jobs_Dict[job][1]
                subjobs = MC_jobs_Dict[job][2]
                identifier = MC_jobs_Dict[job][4]

                if year not in years:
                    years.append(year)

                filename = "MC_Lc2pKpiTuple_" + identifier + ".root"

                if int(year) <= 2012:
                    run = 1
                    cuts = Imports.getDataCuts(run)
                else:
                    run = 2
                    cuts = Imports.getDataCuts(run)

                Lc_MC_tree = TChain(
                    "tuple_Lc2pKpi/DecayTree"
                )  # !!! QUESTION : NOT BETTER ISTEAD OF CHAIN; JUST GETENTRIES FROM EACH ONE BY ONE, ONCE WITHOUT CUT AND ONCE WITH?

                for subjob in os.listdir(RAW_TUPLE_PATH + job):
                    Lc_MC_tree.Add(RAW_TUPLE_PATH + job + "/" + subjob + "/" +
                                   filename)

                N = float(Lc_MC_tree.GetEntries()
                          )  #WHY DID SIMON USE A HARDCODED NUMBER OF ENTRIES??
                #k = float(Lc_MC_tree.GetEntries(cuts + " && " + turbo)) SIMON VERSION
                k = float(Lc_MC_tree.GetEntries(cuts))
                eff = float(k / N)
                binom_error = (1 / N) * ((k * (1 - k / N))**(0.5))

                selecEffDict[particle + "_" + str(year) + "_" + pol] = {
                    'val': eff,
                    'err': binom_error
                }

            print("\nSelection efficiency calculations are done!")

            latexTable(selecEffDict, years, "Selection")

            prettyEffDict = pprint.pformat(selecEffDict)
            dictF = open(dict_path + "Selection_Eff_Dict.py", "w")
            dictF.write("selDict = " + str(prettyEffDict))
            dictF.close()

        elif opt == "-t":

            print("\nCreation of the trigger efficiency files")

            trigEffDict = {}
            years = []

            n = len(MC_jobs_Dict)
            i = 0

            for job in MC_jobs_Dict:
                #FOR THE PROGRESSION BAR
                if i < n:
                    j = (i + 1) / n
                    sys.stdout.write('\r')
                    sys.stdout.write("[%-20s] %d%%" %
                                     ('=' * int(20 * j), 100 * j))
                    sys.stdout.flush()
                    i += 1
                if job == "NA":
                    continue

                particle = MC_jobs_Dict[job][3]
                year = MC_jobs_Dict[job][0]
                pol = MC_jobs_Dict[job][1]
                subjobs = MC_jobs_Dict[job][2]
                identifier = MC_jobs_Dict[job][4]

                if year not in years:
                    years.append(year)

                filename = "MC_Lc2pKpiTuple_" + identifier + ".root"

                if int(year) <= 2012:
                    run = 1
                    cuts = "lcplus_L0HadronDecision_TOS == 1 && lcplus_Hlt1TrackAllL0Decision_TOS == 1"
                    turbo = "lcplus_Hlt2CharmHadD2HHHDecision_TOS==1"
                else:
                    run = 2
                    cuts = Imports.getDataCuts(run)
                    turbo = "lcplus_Hlt1TrackMVADecision_TOS==1"

                #WHAT DO I NEED FOR TRIGGER CUTS?!!

                Lc_MC_tree = TChain(
                    "tuple_Lc2pKpi/DecayTree"
                )  # !!! QUESTION : NOT BETTER ISTEAD OF CHAIN; JUST GETENTRIES FROM EACH ONE BY ONE, ONCE WITHOUT CUT AND ONCE WITH?

                for subjob in os.listdir(RAW_TUPLE_PATH + job):
                    Lc_MC_tree.Add(RAW_TUPLE_PATH + job + "/" + subjob + "/" +
                                   filename)

                N = float(
                    Lc_MC_tree.GetEntries(
                        turbo + " && lcplus_L0HadronDecision_TOS==1"))
                k = float(Lc_MC_tree.GetEntries(cuts + " && " + turbo))
                eff = float(k / N)
                binom_error = (1 / N) * ((k * (1 - k / N))**(0.5))

                trigEffDict[particle + "_" + str(year) + "_" + pol] = {
                    'val': eff,
                    'err': binom_error
                }

            print("\nTrigger efficiency calculations are done!")

            latexTable(trigEffDict, years, "Trigger")

            prettyEffDict = pprint.pformat(trigEffDict)
            dictF = open(dict_path + "Trigger_Eff_Dict.py", "w")
            dictF.write("trigDict = " + str(prettyEffDict))
            dictF.close()

        elif opt == "-p":
            pass

        else:
            sys.exit()
Beispiel #4
0
def main(script_run):
    print ("Starting main")

    #If you want to test on a small portion of data, then enable it here and add the data to the dictionary below
    TESTING = True

    if(TESTING):
        folders_dict = {"115":["2016_MagDown",186,"Xic"]}
    else:
        #Dictionary for all the data
        folders_dict = DATA_jobs_Dict
    
    #Path to save the tuples
    PATH = TUPLE_PATH+script_run+"/"

    if not os.path.exists(PATH):
        os.makedirs(PATH)
    
    blind_data = True

    makeFile(PATH, script_run, dictionary = folders_dict, blinded = blind_data)

    useful_variables = []
    
    if(blind_data):

        variables = ["lcplus_MM", 
                     "lcplus_P", 
                     "lcplus_PT", 
                     "lcplus_IPCHI2_OWNPV", 
                     "lcplus_OWNPV_CHI2", 
                     "lcplus_IP_OWNPV",
                     "lcplus_L0HadronDecision_TOS", 
                     "lcplus_FD_OWNPV",
                     "lcplus_ENDVERTEX_CHI2",
                     "lcplus_ENDVERTEX_NDOF",
                     "lcplus_RAPIDITY",
                     "piplus_RAPIDITY",
                     "pplus_RAPIDITY",
                     "kminus_RAPIDITY",
                     "pplus_M", 
                     "pplus_P", 
                     "pplus_PT",
                     "pplus_ProbNNp",
                     "pplus_OWNPV_CHI2",
                     "kminus_OWNPV_CHI2",
                     "piplus_OWNPV_CHI2",
                     "piplus_M",
                     "piplus_P", 
                     "piplus_PT", 
                     "piplus_PIDK",
                     "piplus_PIDp",
                     "piplus_ProbNNpi",
                     "piplus_IP_OWNPV",
                     "pplus_PIDp",
                     "pplus_PIDK",
                     "kminus_M",
                     "kminus_P", 
                     "kminus_PT", 
                     "kminus_ProbNNk", 
                     "kminus_PIDK", 
                     "kminus_PIDp",
                     "PVNTRACKS",
                     "pplus_IP_OWNPV",
                     "kminus_IP_OWNPV",
                     "kminus_IPCHI2_OWNPV",
                     "piplus_IPCHI2_OWNPV",
                     "pplus_IPCHI2_OWNPV",
                     "pplus_TRACK_PCHI2",
                     "piplus_TRACK_PCHI2",
                     "kminus_TRACK_PCHI2",
                     "pplus_MC15TuneV1_ProbNNp",
                     "pplus_MC15TuneV1_ProbNNk",
                     "pplus_MC15TuneV1_ProbNNpi",
                     "pplus_MC15TuneV1_ProbNNghost",
                     "kminus_MC15TuneV1_ProbNNp",
                     "kminus_MC15TuneV1_ProbNNk",
                     "kminus_MC15TuneV1_ProbNNpi",
                     "kminus_MC15TuneV1_ProbNNghost",
                     "piplus_MC15TuneV1_ProbNNp",
                     "piplus_MC15TuneV1_ProbNNk",
                     "piplus_MC15TuneV1_ProbNNpi",
                     "piplus_MC15TuneV1_ProbNNghost"]

        useful_variables = variables
        
    else:
        variables = ["lcplus_MM", 
                       "lcplus_P", 
                       "lcplus_PT", 
                       "lcplus_ETA",
                       "lcplus_RAPIDITY", 
                       "lcplus_TIP", 
                       "lcplus_IPCHI2_OWNPV", 
                       "lcplus_OWNPV_CHI2", 
                       "lcplus_TAU",
                       "lcplus_L0HadronDecision_TOS", 
                       "lcplus_FD_OWNPV",
                       "pplus_M", 
                       "pplus_P", 
                       "pplus_PT",
                       "pplus_RAPIDITY", 
                       "pplus_ETA",
                       "pplus_ProbNNp",
                       "piplus_M",
                       "piplus_P", 
                       "piplus_PT", 
                       "piplus_RAPIDITY",
                       "piplus_ETA",
                       "piplus_ProbNNpi",
                       "pplus_PIDp",
                       "kminus_M",
                       "kminus_P", 
                       "kminus_PT", 
                       "kminus_RAPIDITY",
                       "piplus_IP_OWNPV",
                       "kminus_ETA",
                       "kminus_ProbNNk", 
                       "kminus_PIDK",
                       "PVNTRACKS", 
                       "piplus_PX", 
                       "pplus_PX", 
                       "kminus_PX", 
                       "piplus_PY",
                       "pplus_PY", 
                       "kminus_PY", 
                       "piplus_PZ",
                       "pplus_PZ", 
                       "kminus_PZ",
                       "pplus_IP_OWNPV",
                       "kminus_IP_OWNPV",
                       "kminus_IPCHI2_OWNPV",
                       "piplus_IPCHI2_OWNPV",
                       "pplus_IPCHI2_OWNPV"]

        useful_variables = variables
        

    appendVars(PATH, script_run, variables = useful_variables)
    
    for element in folders_dict:
        if int(element) > 41 and int(element) < 47:
            extra_variables = ["lcplus_Hlt1TrackAllL0Decision_TOS", "lcplus_Hlt2CharmHadD2HHHDecision_TOS","*L0*","*Hlt*","*HLT*"]
            run = 1
            particle = "Lc"
            
        else:
            extra_variables = ["nSPDHits", "nTracks", "lcplus_Hlt1TrackMVADecision_TOS"]
            particle = folders_dict[element][2]
            run = 2

        for extra_variable in extra_variables:
            if not (extra_variable == ""):
                #If an extra variable is needed, it will be appended
                useful_variables.append(extra_variable)
        
        
        subjobs = folders_dict[element][1]
        
        if (blind_data):
             name = folders_dict[element][0]+"_blinded"
            
        else:
             name = folders_dict[element][0]
        
        saving_directory = PATH+name+"_clusters/"

        cuts = Imports.getDataCuts(run, blinded = blind_data)
        
        if not os.path.exists(saving_directory):
            os.makedirs(saving_directory)
        
        file_directory = RAW_TUPLE_PATH+element
        
        print("\nStarting process for "+name)
        
        #Carries out the process in steps of 20
        step = subjobs//20
        Max = step
        Min = 0
        
        #Clusters are created here
        print("Creation of clusters")
        n = 20 
        i = 0 
        
        while (Max<=subjobs):
            #Progress bar
            if (i<n):
                j = (i+1)/n
                sys.stdout.write("\r")
                sys.stdout.write("[%-20s] %d%%" % ('='*int(20*j), 100*j))
                sys.stdout.flush()
                i+=1
                
            if (Max == Min):
                break
                
            strip_and_save(Min, Max, cuts, file_directory, saving_directory, useful_variables, particle)
            
            temp = Max
            
            if (Max+step > subjobs):
                Max = subjobs
            
            else:
                Max+=step
                
            Min = temp
            
        clusters = os.listdir(saving_directory)
        
        print("\n\nTChaining the clusters")
        
        final_chain = TChain("DecayTree")
        
        n = len(clusters)
        i = 0
        
        for element in clusters:
            if (i<n):
                j = (i+1)/n 
                sys.stdout.write('\r')
                sys.stdout.write("[%-20s] %d%%" % ('='*int(20*j), 100*j))
                sys.stdout.flush()
                i += 1
            final_chain.Add(saving_directory+element)
            
        if not os.path.exists(PATH+name+"/bins"):
            os.makedirs(PATH+name+"/bins")
            
        saving_directory = PATH+name+"/bins/"
        
        print("\n\nCreating the final files")
        
        split_in_bins_and_save(final_chain, saving_directory, run, useful_variables, particle)
        
        print("\nProcess completed for "+name)
        
    #Creation of the total Year Data files 
    print("\nCreation of the total year data files")
    
    mother_particle = ["Xic","Lc"]
    
    BASE_PATH = TUPLE_PATH+script_run+"/"
    
    n = len(os.listdir(BASE_PATH))
    p = 0 
    
    for i in os.listdir(BASE_PATH):

        if (p < n):
            j = (p + 1) / n
            sys.stdout.write('\r')
            sys.stdout.write("[%-20s] %d%%" % ('='*int(20*j), 100*j))
            sys.stdout.flush()
            p += 1

        if "description" in i:
            continue
            
        if "cluster" in i:
            continue
            
        for particle in mother_particle:
                
            totfile = ROOT.TFile.Open(BASE_PATH+i+"/{}_total.root".format(particle),"RECREATE")
            totfile.cd()
            
            tree = TChain("DecayTree")
                
            for j in os.listdir(BASE_PATH+i+"/bins/ybins"):
                if particle in j:
                    tree.Add(BASE_PATH+i+"/bins/ybins/"+j)
            
            tree.Write()
            totfile.Close()
            
            del totfile

    print("\nDeleting clusters")

    os.system("rm -rf {}*_clusters".format(BASE_PATH))
            
    print("\nNTuple preparation is done")