def SaveGraph_2D(FileName, outputFile):
	myfile = TFile( FileName )
	print "\nnow loading [%s]" % FileName

	#########################################
	# -- check the scenario (data or MC) -- #
	#########################################
	Scenario = ""
	GraphName_Base = ""
	if "data_25ns" in FileName:
		Scenario = "Data"
		GraphName_Base = Scenario + "_" + FileName.split("_data_25ns_")[1:][0].split(".root")[:1][0]
		
	elif "mc" in FileName:
		Scenario = "MC"
		GraphName_Base = Scenario + "_" + FileName.split("_mc_")[1:][0].split(".root")[:1][0]

	TreeName = "muonEffs"

	#########################
	# -- sub-directory 1 -- #
	#########################
	Path1 = "%s" % TreeName
	Dir1 = myfile.GetDirectory( Path1 )
	print "now in %s" % Path1

	if Dir1.GetListOfKeys().GetEntries() > 2:
		print "More than 2 directories under %s" % Path1
		sys.exit()

	#########################
	# -- sub-directory 2 -- #
	#########################
	Path2 = "%s/%s/fit_eff_plots" % ( TreeName, Dir1.GetListOfKeys().At(0).GetName() )
	Dir2 = myfile.GetDirectory( Path2 )
	print "now in %s" % Path2

	i_absetabin = 0
	List_Graphs = []
	for key in Dir2.GetListOfKeys():
		if "pt_PLOT_eta_bin" in key.GetName():
			# print key.GetName()

			canvas = key.ReadObj()
			graph = canvas.GetPrimitive("hxy_fit_eff").Clone()

			name_before = canvas.GetName()
			name_after = GraphName_Base + "_abseta%s" % (i_absetabin)
			graph.SetName( name_after )
			i_absetabin += 1

			print "\tgraph: %s -> %s" % (name_before, name_after)
			List_Graphs += [graph]

	outputFile.cd()
	for graph in List_Graphs:
		graph.Write()

	myfile.Close()
示例#2
0
def plotHists(root_filename, topology):
    print(">>> Plotting Hists")
    try:
        test_file = open(root_filename, "r")
    except:
        raise Exception("Error opening .root file. TERMINATING.")

    root_file = TFile(root_filename, "READ")

    for slot_num in range(topology[0]):
        Slot = root_file.GetDirectory("Slot" + str(slot_num))
        #print(Slot)
        if Slot == None:
            pass
        else:
            print("Slot: ", slot_num)
            for channel_num in range(topology[1]):
                print("amp_shape" + str(slot_num) + "_" + str(channel_num))
                plot = False
                try:
                    hist = (TH2F)(
                        root_file.GetDirectory("Slot" + str(slot_num)).
                        GetDirectory("Channel" +
                                     str(channel_num)).Get("amp_shape" +
                                                           str(slot_num) +
                                                           "_" +
                                                           str(channel_num)))
                    plot = True
                except:
                    pass

                if plot:
                    x_bins = hist.GetNbinsX()
                    y_bins = hist.GetNbinsY()
                    #print("xbins: ", x_bins)
                    #print("ybins: ", y_bins)
                    x = []
                    y = []
                    for i in range(x_bins):
                        for j in range(y_bins):
                            if hist.GetBinContent(i, j) > 0:
                                print("Contents bin x: ", i, " y: ", j, " - ",
                                      hist.GetBinContent(i, j))
                            for k in range(int(hist.GetBinContent(i, j))):
                                x.append(i)
                                y.append(j)
                    if x_bins > 0 and y_bins > 0:
                        plt.hist2d(x,
                                   y,
                                   bins=[x_bins, y_bins],
                                   range=[[0, 200], [0.9, 1]])
                        plt.show()

                    del hist

                #input(">>> ")
    root_file.Close()
def PrintFitCanvases(FileName):
	myfile = TFile( FileName )
	print "\nnow loading [%s]" % FileName

	#########################################
	# -- check the scenario (data or MC) -- #
	#########################################
	Scenario = ""
	DirName = ""
	if "data_25ns" in FileName:
		Scenario = "Data"
		DirName = Scenario + "_" + FileName.split("_data_25ns_")[1:][0].split(".root")[:1][0]
		
	elif "mc" in FileName:
		Scenario = "MC"
		DirName = Scenario + "_" + FileName.split("_mc_")[1:][0].split(".root")[:1][0]

	if DirName not in os.listdir("./"):
		os.mkdir( DirName )

	TreeName = "muonEffs"

	#########################
	# -- sub-directory 1 -- #
	#########################
	Path1 = "%s" % TreeName
	Dir1 = myfile.GetDirectory( Path1 );
	print "now in %s" % Path1

	if Dir1.GetListOfKeys().GetEntries() > 2:
		print "More than 2 directories under %s" % Path1
		sys.exit()

	#########################
	# -- sub-directory 2 -- #
	#########################
	Path2 = "%s/%s" % ( TreeName, Dir1.GetListOfKeys().At(0).GetName() )
	Dir2 = myfile.GetDirectory( Path2 )
	print "now in %s" % Path2

	for key in Dir2.GetListOfKeys():
		FolderName = key.GetName()
		
		if "_eff" not in FolderName: 
			Path3 = Path2 + "/%s" % key.GetName()
			Dir3 = myfile.GetDirectory( Path3 )

			for key2 in Dir3.GetListOfKeys():
				if( key2.GetName() == "fit_canvas" ):
					canvas = key2.ReadObj()
					canvas.SaveAs( "./%s/%s.pdf" % (DirName, FolderName ) )

	myfile.Close()
示例#4
0
    def setUp(self):
        """Create dictionaries needed by combiners."""

        dirname = os.path.join(os.path.dirname(__file__), 'fixtures')
        orfdata = TFile(
            os.path.abspath(os.path.join(dirname, 'dqm_data.root')), 'read')
        orfref = TFile(os.path.abspath(os.path.join(dirname, 'dqm_ref.root')),
                       'read')

        # valid ROOT files
        assert (not orfdata.IsZombie())
        assert (not orfref.IsZombie())

        self.tdir = tempfile.mkdtemp()
        self.rfdata = TFile(os.path.join(self.tdir, 'fdata.root'), 'recreate')
        self.rfref = TFile(os.path.join(self.tdir, 'fref.root'), 'recreate')

        hist_recipes = [
            (get_avg_trend, 'Vetra/NoiseMon/ADCCMSuppressed',
             ('RMSNoise_vs_ChipChannel', 'AvgRMSNoise_trend')),
            (get_avg_hist, 'Vetra/NoiseMon/ADCCMSuppressed',
             ('RMSNoise_vs_ChipChannel', 'AvgRMSNoise_all')),
            (get_avg_hist, 'Vetra/NoiseMon/ADCCMSuppressed',
             ('RMSNoise_vs_ChipChannel', 'AvgRMSNoise_R', 'r')),
            (get_avg_hist, 'Vetra/NoiseMon/ADCCMSuppressed',
             ('RMSNoise_vs_ChipChannel', 'AvgRMSNoise_Phi', 'p')),
            # (get_avg_hist, 'Vetra/VeloPedestalSubtractorMoni',
            #  ('Ped_Sub_ADCs_Profile', 'Ped_Sub_ADCs_all'))
        ]
        # histograms: make, save, and cleanup
        for recipe in hist_recipes:
            href = recipe[0](orfref.GetDirectory(recipe[1]), *recipe[2])
            self.rfref.WriteTObject(href)
            del href
            hdata = recipe[0](orfdata.GetDirectory(recipe[1]), *recipe[2])
            self.rfdata.WriteTObject(hdata)
            del hdata
        self.rfref.Close()
        self.rfdata.Close()

        # Write DQ database to temp directory (rather than run directory)
        Config().dq_db_file_path = os.path.join(self.tdir, Config().dq_db)

        configfile = os.path.join(os.path.dirname(__file__),
                                  'analysis_config_test.py')
        with open(configfile, 'r') as inputFile:
            exec(inputFile.read())
        config = AnalysisConfigWrapper(
            (analysis_config_branches, analysis_config_leaves))
        self.mycombiner = config.getTrunk(orfdata.GetName(), orfref.GetName())

        # Results to compare against
        self.results = {"score": Score(70.62594356001006), "lvl": ERROR}
示例#5
0
def make_fractions_single(output_dir,
                          channel,
                          variable,
                          components_dict,
                          category='inclusive',
                          systematic='nominal'):
    '''This funciton aims at producing a root file containing all
    histograms needed for datacards. To do so, provide a dict of components
    (class Component) that have the ROOT histograms to be used.
    Gives also the output directory and the variable name, so that you can
    make datacards for different variables.'''
    rootfilename = '_'.join(['htt', channel, 'for_FF_fractions'])
    rootfile = TFile('{}/{}.root'.format(output_dir, rootfilename), 'UPDATE')
    rootdirname = '_'.join([channels_names[channel], variable, category])
    rootdir = rootfile.GetDirectory(rootdirname)
    if not rootdir:
        rootdir = TDirectoryFile(rootdirname, rootdirname)
    rootdir.cd()
    for key, component in components_dict.iteritems():
        histname = '_'.join([key, systematic])
        if systematic == 'nominal':
            histname = key
        else:
            histname = histname.replace('up', 'Up')
            histname = histname.replace('down', 'Down')
        if rootdir.Get(histname):
            continue
        hist = component.histogram.Clone(histname)
        if histname[:len("fakes_")] == "fakes_":
            hist.Scale(-1)
        hist.SetTitle(key)
        hist.Write()
    rootfile.Close()
示例#6
0
def rewtOneHist(dataset, hwts):
    fileName = condor_dir + "/" + dataset + ".root"
    if not os.path.exists(fileName):
        print "WARNING: didn't find ", fileName
        return
    print "About to reweight histogram in " + fileName
    inFile = TFile(fileName, "UPDATE")
    if inFile.IsZombie() or not inFile.GetNkeys():
        return
    inFile.cd()
    h = inFile.Get(str(arguments.histToBeReWeighted)).Clone()
    if not h:
        print "  Could not find hist named " + arguments.histToBeReWeighted + " in " + inFile.GetName(
        )
        return
    h.SetDirectory(0)
    newName = h.GetName() + str(arguments.suffixRename)
    h.SetName(newName)
    dir = arguments.histToBeReWeighted
    dir = dir[:dir.rfind("/")]
    print "Will write hist to directory " + dir
    inFile.cd(dir)
    tdir = inFile.GetDirectory(dir)
    tdir.Delete(newName + ";*")
    for i in range(1, h.GetNbinsX() + 1):
        val = h.GetBinContent(i)
        err = h.GetBinError(i)
        binCtr = h.GetBinCenter(i)
        wt = hwts.GetBinContent(hwts.FindBin(binCtr))
        h.SetBinContent(i, val * wt)
        h.SetBinError(i, err * wt)
    h.Write()

    inFile.Close()
示例#7
0
def addBranchToDirectory():
    print ">>> Add branch to directory."

    file = TFile("tree.root", 'update')

    # make directory if it does not exist
    dir = file.GetDirectory("dir1")
    if not dir:
        print ">>>   created dir1"
        dir = file.mkdir("dir1")

    # make this directory the current one: everything you write will be saved here
    dir.cd()

    # make new tree with a branch
    tree = TTree("tree2", "tree2")
    n = array('d', [0])
    #n = numpy.zeros(1, dtype=float)
    tree.Branch("normal5", n, 'normal5/D')

    # fill tree
    for i in xrange(10000):
        n[0] = gRandom.Gaus(3, 2)
        tree.Fill()

    file.Write("", TFile.kOverwrite)
    file.Close()
示例#8
0
    def save(self, s_graphs):
        """ Save TGraph in TFile and as *.pdf. """

        graphs = self._get_graphs(s_graphs)

        # Open TFile
        rootfile = TFile(self.s_rootfile, 'UPDATE')

        # Go into directory if it is defined
        if self.directory:
            cwd = getcwd()

            # Change directory in rootfile
            if not rootfile.GetDirectory(self.directory):
                rootfile.mkdir(self.directory)
            rootfile.cd(self.directory)

            # Change directory on filesystem
            if not path.exists(self.directory):
                makedirs(self.directory)
            chdir(self.directory)

        self._canvas.SaveAs('{0}.pdf'.format(self.name))
        for graph in graphs:
            graph.Write()

        # Go back to original working directories
        if self.directory:
            rootfile.cd()
            chdir(cwd)

        # Close TFile
        rootfile.Close()

        self._clear()
示例#9
0
def add_points(graph, directory, layer, usePU):

    ipt = graph.GetN()

    # List runs
    for root, directories, files in os.walk(directory):
        for rundir in sorted(directories):
            if "run_" in rundir:
                # start to process run
                run = rundir[4:]
                #print "processing run ", run

                lumi = 0
                lumi_err = 0

                # Get informations for a given run
                frun = TFile(directory + "/" + rundir +
                             "/withMasking/rootfile/SiStripHitEffHistos_run" +
                             run + ".root")
                fdir = frun.GetDirectory("SiStripHitEff")

                # for efficiency
                hfound = fdir.Get("found")
                htotal = fdir.Get("all")

                if htotal == None:
                    print '  Missing histogram in file ' + frun.GetName()
                    continue

                # lumi
                if usePU == 0: hlumi = fdir.Get("instLumi")
                else: hlumi = fdir.Get("PU")
                if hlumi == None:
                    print '  Missing lumi/pu histogram in file ' + frun.GetName(
                    )
                    continue
                lumi = hlumi.GetMean()
                lumi_err = hlumi.GetRMS()
                #print "lumi (avg+/-rms): ", lumi, "+/-", lumi_err

                # efficiency for a given layer
                found = hfound.GetBinContent(layer)
                total = htotal.GetBinContent(layer)
                if total > 0: eff = found / total
                else: eff = 0
                #print run, eff, lumi, lumi_err

                # remove run without lumi informations
                if lumi > 1:
                    eff_vs_lumi.SetPoint(ipt, lumi, eff)
                    low = TEfficiency.Bayesian(total, found, .683, 1, 1, False)
                    up = TEfficiency.Bayesian(total, found, .683, 1, 1, True)
                    if eff - low > 0.01:
                        print 'large error bar for run', run, 'layer', layer, 'eff:', '{:.4f}'.format(
                            eff), 'err:', '{:.4f}'.format(eff - low)
                    #if lumi_err > lumi/3.: print 'wide lumi range for run', run, 'layer', layer, 'eff:', '{:.4f}'.format(eff), 'lumi/pu:', '{:.4f}'.format(lumi), 'rms:', '{:.4f}'.format(lumi_err)
                    eff_vs_lumi.SetPointError(ipt, lumi_err, lumi_err,
                                              eff - low, up - eff)
                    ipt += 1
                frun.Close()
示例#10
0
    def compare(self, filename1, filename2, st_test):
        if not 'TFile' in globals():
            from ROOT import TFile
        f1 = TFile(filename1)
        f2 = TFile(filename2)

        conn = sqlite3.connect(self.db_name)
        c = conn.cursor()

        ## Create Directory and HistogramComparison structure in the DB
        dir_DQMData = f1.GetDirectory("DQMData")
        dir_Run = None
        for elem in dir_DQMData.GetListOfKeys():
            elem_name = elem.GetName()
            if elem_name.startswith('Run '):
                dir_Run = dir_DQMData.Get(elem_name)

        fid, tid, dir_id = self.walk_through(c, dir_Run, f1, f2, st_test)

        c.execute('''DELETE FROM Directory WHERE from_histogram_id IS NULL
                     AND till_histogram_id IS NULL''')
        c.execute('''INSERT INTO RootFileComparison(filename1, filename2, directory_id)
                     VALUES (?, ?, ?)''', (basename(filename1), basename(filename2), dir_id))
        root_file_comparison_id = c.lastrowid

        conn.commit()
        conn.close()
        f1.Close()
        f2.Close()
        return root_file_comparison_id
示例#11
0
文件: dqm_diff.py 项目: p2l1pfp/cmssw
def get_content(root_file_name):
    """Returns all file (histogram) names, which are found in <root_file_name>."""
    from ROOT import TFile
    root_file = TFile(root_file_name)
    root_directory = root_file.GetDirectory("DQMData")
    filename_set = set()
    collect_directory_filenames(root_directory, filename_set)
    root_file.Close()
    return filename_set
示例#12
0
def skimTree(sample, filename):

    # Unweighted input
    inName = origin + '/' + sample + '/' + filename
    if not os.path.exists(inName):
        print '- WARNING: file', inName, 'does not exist, continuing'
        return True

    # Weighted output
    if not os.path.exists(target + '/' + sample):
        os.mkdir(target + '/' + sample)

    outName = target + '/' + sample + '/' + filename
    if os.path.exists(outName):
        print '- WARNING: weighted file exists, overwriting'
        #return True

    outFile = TFile(outName, 'RECREATE')
    outFile.cd()

    # Open old file
    inFile = TFile(inName, 'READ')

    # Looping over file content
    for key in inFile.GetListOfKeys():
        obj = key.ReadObj()
        # Histograms
        if obj.IsA().InheritsFrom('TH1'):
            if verbose: print ' + TH1:', obj.GetName()
            outFile.cd()
            obj.Write()
        # Tree
        elif obj.IsA().InheritsFrom('TTree'):
            newTree = obj.CopyTree(skimString if obj.GetName() ==
                                   "Events" else "")
            outFile.cd()
            newTree.Write(obj.GetName(), TObject.kOverwrite)
            if verbose: print ' + TTree:', obj.GetName()
        # Directories
        elif obj.IsFolder():
            subdir = obj.GetName()
            if verbose: print ' \ Directory', subdir, ':'
            outFile.mkdir(subdir)
            outFile.cd(subdir)
            for subkey in inFile.GetDirectory(subdir).GetListOfKeys():
                subobj = subkey.ReadObj()
                if subobj.IsA().InheritsFrom('TH1'):
                    if verbose: print '   + TH1:', subobj.GetName()
                    outFile.cd(subdir)
                    subobj.Write()
            outFile.cd('..')

    outFile.Close()
示例#13
0
def saveHist(hist, channel, directory='', addStat=False):
    
    # Blind
#    if BLIND and 'data_obs' in hist and 'SR' in channel:
#        rando = TRandom3()
#        hist['data_obs'].Reset()
#        hist['data_obs'].SetMarkerStyle(21)
#        for i in range(hist['data_obs'].GetNbinsX()):
        #this wiggles the pseudodata with a Poison 
        #-> different from Asimov dataset (-t -1 option), will give different obs/expected limits but thats ok
#            hist['data_obs'].SetBinContent(i+1, rando.Poisson( hist['BkgSum'].GetBinContent(i+1) ))
#            hist['data_obs'].SetBinContent(i+1, hist['BkgSum'].GetBinContent(i+1) )

    # Sanity check
#    smax = max(hist, key=lambda x: hist[x].Integral())
#    for s in hist.keys():
#        for i in range(hist[s].GetNbinsX()):
#            if not hist[s].GetBinContent(i+1)>0.: hist[s].SetBinContent(i+1, 1.e-4) # Sanity check
#            if math.isnan(hist[s].GetBinContent(i+1)) or math.isinf(hist[s].GetBinContent(i+1)): print "WARNING: in channel", channel, "bkg", s, "bin", i+1, "is nan or inf"
#            #print "checking", s, i, hist[s].GetBinContent(i+1)
    
    outFile = TFile("rootfiles_"+options.name+"/"+channel+".root", "RECREATE" if len(directory)==0 else "UPDATE")
    outFile.cd()
    if len(directory) > 0:
        if not outFile.GetDirectory(directory): outFile.mkdir(directory)
        outFile.cd(directory)

    for s in sorted(hist.keys()):
        hist[s].Write(hist[s].GetName().replace('Up', '').replace('Down', ''))

    #outFile.cd("..")
    # Statistical MC uncertainty
    if addStat:
        nbins = hist['data_obs'].GetNbinsX()
        #CMS_stat_Z0b_bin57
        for s in sorted(hist.keys()):
            if 'data' in s: continue
            dirname = channel+"/Sys_"+s
            #if outFile.GetDirectory(dirname): outFile.rmdir(dirname)
            outFile.mkdir(dirname)
            outFile.cd(dirname)
            for k in range(1, nbins+1):
                sysname = "CMS_stat_%s_%s_bin%d" % (channel, s, k)
                histUp = hist[s].Clone(sysname+"Up")
                histUp.SetBinContent(k, histUp.GetBinContent(k) + histUp.GetBinError(k))
                histUp.Write()
                histDown = hist[s].Clone(sysname+"Down")
                histDown.SetBinContent(k, max(histDown.GetBinContent(k) - histDown.GetBinError(k), 1.e-6))
                histDown.Write()
            outFile.cd("..")
    
    outFile.Close()
    print "Histograms saved in file rootfiles_"+options.name+"/"+channel+".root"
def add_points(graph, directory, subdir, layer):

    ipt = graph.GetN()
    labels = []

    # List runs
    for root, directories, files in os.walk(directory):
        for rundir in sorted(directories):
            if "run_" in rundir:
                # start to process run
                run = rundir[4:]
                #print "processing run ", run

                # for efficiency
                frun = TFile(directory + "/" + rundir + "/" + subdir +
                             "/rootfile/SiStripHitEffHistos_run" + run +
                             ".root")
                fdir = frun.GetDirectory("SiStripHitEff")
                hfound = fdir.Get("found")
                htotal = fdir.Get("all")

                if htotal == None:
                    print '  Missing histogram in file ' + frun.GetName()
                    continue

                # efficiency for a given layer
                found = hfound.GetBinContent(int(layer))
                total = htotal.GetBinContent(int(layer))
                if total > 0: eff = found / total
                else: eff = 0
                #print run, eff

                graph.SetPoint(ipt, ipt + 1, eff)
                labels.append(run)
                low = TEfficiency.Bayesian(total, found, .683, 1, 1, False)
                up = TEfficiency.Bayesian(total, found, .683, 1, 1, True)
                #eff_vs_run.SetPointError(ipt, 0, 0, eff-low, up-eff)
                ipt += 1
                frun.Close()

    axis = graph.GetXaxis()
    for i in range(graph.GetN()):
        axis.SetBinLabel(axis.FindBin(i + 1), labels[i])
        #print i, axis.FindBin(i+1), labels[i]
    return labels
示例#15
0
def scaleFiles(inFileName, lumi):
    """docstring for scaleQCD"""

    if os.path.exists(inFileName + '_Scaled.root'):
        os.remove(inFileName + '_Scaled.root')
        outfile = TFile(inFileName + '_Scaled.root', "RECREATE")
    else:
        outfile = TFile(inFileName + '_Scaled.root', "RECREATE")

    infile = TFile(inFileName + '.root', "READ")

    scale = lumi * scaleFactor(inFileName)

    listDir = []
    for k in infile.GetListOfKeys():
        name = k.GetName()
        listDir.append(name)
    for j in listDir:
        if not 'RUNATree' in j:
            outfile.mkdir(j)
            outfile.cd(j)
            dir = infile.GetDirectory(j)
            for q in dir.GetListOfKeys():
                name = q.GetName()
                h = infile.Get(j + '/' + name)
                try:
                    hOut = h.Clone()
                    #hOut.Scale( scale )
                    if not isinstance(h, TTree):
                        hOut.Scale(scale)
                        hOut.Write()
                except AttributeError:
                    pass
            outfile.cd('/')

    outfile.Write()
    outfile.Close()
    infile.Close()
示例#16
0
def addHistToDirectory():
    print ">>> Add histogram to directory."

    # reopen tree file
    file = TFile("tree.root", 'update')

    # make directory if it does not exist
    dir = file.GetDirectory("dir1")
    if not dir:
        print ">>>   created dir1"
        dir = file.mkdir("dir1")

    # make this directory the current one: everything you write will be saved here
    dir.cd()

    # declare histogram
    hist = TH1D("normal4", "Hist in folder", 50, -1, 9)

    # write histogram
    for i in xrange(10000):
        hist.Fill(gRandom.Gaus(4, 3))

    file.Write("", TFile.kOverwrite)
    file.Close()
class make_monitoring_plots:

    def __init__(self):
        self.CANVAS_WIDTH = 1200
        self.CANVAS_HEIGHT = 800

        self.VERBOSE = 1

        # initialize lists used to store data
        self.histlist_filename = None
        self.macrolist_filename = None
        self.hists_to_plot = []
        self.macros_to_run = []

        # name of file that containes the histogram - should get rid of this?
        self.rootfile_name = None
        self.root_file = None       # the actual ROOT file

        self.base_root_dir = ""
        self.output_directory = "."

        # the canvas we plot on
        self.c1  = TCanvas("c1","",self.CANVAS_WIDTH,self.CANVAS_HEIGHT)

    def __del__(self):
        del self.c1
        del self.hists_to_plot
        del self.macros_to_run
        if self.root_file:
            self.root_file.Close()
            #del self.root_file


    ##########################################################
    ## Helper functions
    ##########################################################

    def plot_hist(self, h):
        #c1.Clear()
        h.SetStats(0)
        h.Draw()

    def plot_2dhist(self, h):
        # by request, plot certain histograms with a logarithmic Z scale
        LOGZ_HISTS = [ "cdc_raw_int_vs_n", "cdc_raw_t_vs_n", "cdc_ped_vs_n", "cdc_windata_ped_vs_n" ]
        if h.GetName() in LOGZ_HISTS:
            self.c1.SetLogz(1)
            
        h.SetStats(0)
        h.Draw("COLZ")

    def ClearPad(self, pad):
        #gROOT.SetStyle("Plain")   # reset style
        self.c1.Clear()
        self.c1.SetLogx(0)
        self.c1.SetLogy(0)
        self.c1.SetLogz(0)

    def print_canvas_png(self, fullpath):
        #print "Making plot = " +  self.output_directory + "/" + fullpath.replace("/","_") + ".png" # tmp
        self.c1.Print( self.output_directory + "/" + fullpath.replace("/","_") + ".png" )

    def SumHistContents(self, the_file, hist_path):
        h = the_file.Get(hist_path)
        if(h == None):
            logging.warn("Could not load " + hist_path)
            return -1

        sum = 0
        for bin in range(h.GetNbinsX()+1):
            sum += (bin-1)*h.GetBinContent(bin)
        return sum
             

    ##########################################################
    # These functions are still under development - they work but are slooooow

    # function to see if a histogram "in_hnamepath" matches the path definition given by "hnamepath_def"
    # we make the following assumptions for the path definiton:
    # - if the path does not begin with a '/', then we assume that this specifies a histogram name
    # - if the path does begin with a '/', the we assume that this specifies a full path
    # - we allow the "*" wildcard in the path definition
    # NOTE: apparently this is very slow, need to optimize
    def match_hnamepath(self, hnamepath_def, in_hnamepath):
        if len(hnamepath_def) == 0:
            return False
    
        if hnamepath_def[0] != '/':
            #  match based on histogram name
            return hnamepath_def == in_hnamepath
        else:
            # translate '*' wildcard into regex language
            hnamepath_def.replace('*', '.*')
            result = re.match(hnamepath_def, in_hnamepath)
            if result is None:
                return False
            else:
                return True

    def match_hnamepaths(self, hnamepath_defs, in_hnamepath):
        for hnamepath_def in hnamepath_defs:
            if self.match_hnamepath(hnamepath_def, in_hnamepath):
                return True
        return False

    ############################################################

    # parse file containing items, one on each line, and turn it into a list
    def ParseFile(self, filename):
        data = []
        with open(filename) as f:
            for line in f:
                name = line.strip()
                if name == "":
                    continue
                if name[0] == '#':
                    continue        
                data.append(name)
        return data

    def PlotHistsRecursive(self, the_dir, path, hists_to_plot):
        # loop over all keys in the current directory, ROOT-style
        key_iter = TIter(the_dir.GetListOfKeys())
        key = key_iter()
        while( key ):
            obj = key.ReadObj()
            obj_pathname = path+"/"+key.GetName()

            # if the object is a histogram, then see if we should plot it
            if(isinstance(obj,TH1)):
                if self.VERBOSE>1:
                    logging.info("matching hname = %s, hnamepath = %s" %(obj.GetName(),obj_pathname))
                #if not match_hnamepaths(hists_to_sum, path+"/"+obj.GetName()):
                # match either histogram names or full path names
                if (obj.GetName() not in hists_to_plot) and (obj_pathname not in hists_to_plot):
                    key = key_iter()
                    continue

                # plot histogram
                if(isinstance(obj,TH2)):
                    self.plot_2dhist(obj)
                else:
                    self.plot_hist(obj)

                # save image to disk
                self.print_canvas_png("_"+obj_pathname)  ## name hack for backward compatability
                #print_canvas_pdf()
            
            # if the object is a directory, access what's inside
            if(isinstance(obj, TDirectory)):
                self.PlotHistsRecursive(obj, obj_pathname, hists_to_plot)

            # END OF LOOP - move to next item in the directory
            key = key_iter()
        del key_iter

    def SavePlots(self, root_dir, dir_path, hists_to_plot, macros_to_run):
        # plot individual histograms 
        if(len(hists_to_plot) > 0):
            # since we select histograms by their full path or just their name
            # we have to search through the directory tree, so the 
            # plotting logic is inside this function
            self.PlotHistsRecursive(root_dir,dir_path,hists_to_plot)

        # plot RootSpy macros
        # the basic algorithm is for each macro, first build a clean slate, then execute the macro
        if(len(macros_to_run) > 0):
            for macro_file in macros_to_run: 
                if os.path.isfile(macro_file):
                    if self.VERBOSE>0:
                        logging.info("running macro = " + macro_file)
                    # run the macro inside CINT
                    self.ClearPad(self.c1)
                    root_dir.cd()
                    try:
                        # reset interpreter to handle a succession of unnamed scripts
                        #gROOT.Reset()     ## currently dealing with ROOT6 bug - sdobbs, Feb. 6, 2017
                        #gROOT.ProcessLine(".x " + macro_file)
                        img_fname = macro_file.split('/')[-1]
                        if img_fname[-2:] == ".C":
                            img_fname = img_fname[0:-2]
                        img_fname = self.output_directory + "/" + img_fname.replace("/","_")
                        os.system("python run_single_root_command.py -F %s -O %s %s"%(self.rootfile_name,img_fname,macro_file))
                    except:
                        logging.error("Error processing "+macro_file)
                    # save the canvas - the name depends just on the file name
                    img_fname = macro_file.split('/')[-1]
                    #if img_fname[-2:] == ".C":
                    #    img_fname = img_fname[0:-2]
                    #self.print_canvas_png(img_fname)
                else:
                    logging.info("could not find macro = " + macro_file + " !")
                    
    """
    ### this was needed when we were adding together histograms, but I don't think that we need it anymore
    def extract_macro_hists(macro):
        if VERBOSE>1:
            logging.info("Extracting histograms needed for macros...")
        macro_hists = []
        if not os.path.isfile(macro):
            # this file doesn't exist - should we say something here?
            return macro_hists
        # read through the file and extract the histograms tagged for RootSpy summing
        f = open(macro)
        for line in f:
            tokens = line.strip().split()
            #print str(tokens)
            # histograms are tagged as  "// hnamepath: /path/to/a/hist"
            if (len(tokens) < 3):
                continue
            if ( (tokens[0] == "//") and (tokens[1] == "hnamepath:") ):
                hnamepath = " ".join(tokens[2:])
                if VERBOSE>1:
                    print hnamepath
                hname = hnamepath.split('/')[-1]
                if hname not in macro_hists:
                    macro_hists.append(hname)
        return macro_hists
    """


    def MakePlots(self):
        ## some sanity checks
        if self.rootfile_name is None:
            logging.critical("No ROOT file set!")
            return
        if self.histlist_filename is None and self.macrolist_filename is None:
            logging.critical("No files containing the histograms or macros to plot set!")
            return

        # decide which histograms/macros we should make plots of
        if self.histlist_filename:
            self.hists_to_plot = self.ParseFile(self.histlist_filename)
        else:
            self.hists_to_plot = []
        if self.macrolist_filename:
            self.macros_to_run = self.ParseFile(self.macrolist_filename)
        else:
            self.macros_to_run = []

        ## more sanity checks
        if len(self.hists_to_plot) == 0 and len(self.macros_to_run) == 0:
            logging.critical("No histograms or macro to save!")
            return

        # make the plots
        self.root_file = TFile(self.rootfile_name)
        # should handle bad files better?
        if(self.root_file is None):
            logging.critical("Could not open file: " + self.rootfile_name)
            return

        self.ClearPad(self.c1)
        self.c1.SetCanvasSize(self.CANVAS_WIDTH,self.CANVAS_HEIGHT)
        self.SavePlots(self.root_file.GetDirectory(self.base_root_dir), self.base_root_dir, self.hists_to_plot, self.macros_to_run)
示例#18
0
文件: merger.py 项目: zucchett/SFrame
def merge(files, target):

    hist = {}

    inFile = TFile(files[0][0], "READ")
    inFile.cd()
    for key in inFile.GetListOfKeys():
        obj = key.ReadObj()
        # Copy and rescale histograms
        if obj.IsA().InheritsFrom("TH1"):
            hist[obj.GetName()] = weight(files, obj.GetName())
        elif obj.IsA().InheritsFrom("TTree"):
            print "Skip tree", obj.GetName()
            # do nothing
        # Directories
        elif obj.IsFolder():
            subdir = obj.GetName()
            print "In directory", subdir
            inFile.cd(subdir)
            for subkey in inFile.GetDirectory(subdir).GetListOfKeys():
                subobj = subkey.ReadObj()
                if subobj.IsA().InheritsFrom("TH1"):
                    if 'Eff' in subdir: effh.append(subobj.GetName())
                    elif 'Gen' in subdir: gens.append(subobj.GetName())
                    elif not 'Counter' in subdir: plotHist([subobj])
            inFile.cd("..")
    inFile.Close()

    print "Now plotting efficiency histograms", effh

    for j, h in enumerate(effh):
        name = h.split("_", 1)[0].replace(
            "Eff", "")  #if not 'HEEP_E' in h else h.replace("Eff", "")
        num = TH1F()
        den = TH1F()
        files = {}
        for i, s in enumerate(sign):
            files[s] = TFile(NTUPLEDIR + sample[s]['files'][0] + ".root",
                             "READ")
            files[s].cd()
            tnum = files[s].Get("Eff/" + h)
            tden = files[s].Get("Leptons/" + name)
            tnum.Multiply(tden)
            if i == 0:
                num = tnum.Clone("Numerator")
                den = tden.Clone("Denominator")
            else:
                num.Add(tnum)
                den.Add(tden)
            #files[s].Close()
        num.Divide(den)
        num.SetName(h)
        plotHist([num])

    print "Now plotting gen signal histograms", effh

    for j, h in enumerate(gens):
        files = {}
        sigh = []
        for i, s in enumerate(sigp):
            files[s] = TFile(NTUPLEDIR + sample[s]['files'][0] + ".root",
                             "READ")
            files[s].cd()
            sigh.append(files[s].Get("Gen/" + h))
            sigh[-1].SetTitle(s)
        plotHist(sigh)
示例#19
0
def save_pyunfold_root_file(config,
                            num_groups=4,
                            outfile=None,
                            formatted_df_file=None,
                            res_mat_file=None,
                            res_mat_err_file=None):

    paths = get_paths()

    unfolding_dir = os.path.join(paths.comp_data_dir, config, 'unfolding')
    # Bin Definitions
    binname = 'bin0'
    # ROOT Output
    if outfile is None:
        outfile = os.path.join(
            unfolding_dir, 'pyunfold_input_{}-groups.root'.format(num_groups))
    check_output_dir(outfile)
    if os.path.exists(outfile):
        os.remove(outfile)

    fout = TFile(outfile, 'UPDATE')
    # Check if bin directory exists, quit if so, otherwise create it!
    if not fout.GetDirectory(binname):
        pdir = fout.mkdir(binname, 'Bin number 0')
    else:
        fout.Close()
        print('\n=========================\n')
        raise ValueError('Directory {} already exists!\nEither try another '
                         'bin number or delete {} and start again. '
                         'Exiting...\n'.format(binname, outfile))

    # Go to home of ROOT file
    fout.cd(binname)

    if formatted_df_file is None:
        formatted_df_file = os.path.join(
            unfolding_dir, 'unfolding-df_{}-groups.hdf'.format(num_groups))
    df_flux = pd.read_hdf(formatted_df_file)
    counts = df_flux['counts'].values
    if 'counts_err' in df_flux:
        counts_err = df_flux['counts_err'].values
    else:
        counts_err = None
    efficiencies = df_flux['efficiencies'].values
    efficiencies_err = df_flux['efficiencies_err'].values

    cbins = len(counts) + 1
    carray = np.arange(cbins, dtype=float)

    ebins = len(counts) + 1
    earray = np.arange(ebins, dtype=float)
    cbins -= 1
    ebins -= 1

    # Load response matrix array
    if res_mat_file is None:
        res_mat_file = os.path.join(
            unfolding_dir, 'response_{}-groups.txt'.format(num_groups))
    response_array = np.loadtxt(res_mat_file)
    if res_mat_err_file is None:
        res_mat_err_file = os.path.join(
            unfolding_dir, 'response_err_{}-groups.txt'.format(num_groups))
    response_err_array = np.loadtxt(res_mat_err_file)

    # Measured effects distribution
    ne_meas = TH1F('ne_meas', 'effects histogram', ebins, earray)
    ne_meas.GetXaxis().SetTitle('Effects')
    ne_meas.GetYaxis().SetTitle('Counts')
    ne_meas.SetStats(0)
    ne_meas.Sumw2()

    # Prepare Combined Weighted Histograms - To be Normalized by Model After Filling
    # Isotropic Weights of Causes - For Calculating Combined Species Efficiency
    eff = TH1F('Eff', 'Non-Normed Combined Efficiency', cbins, carray)
    eff.GetXaxis().SetTitle('Causes')
    eff.GetYaxis().SetTitle('Efficiency')
    eff.SetStats(0)
    eff.Sumw2()

    # Isotropic Weighted Mixing Matrix - For Calculating Combined Species MM
    response = TH2F('MM', 'Weighted Combined Mixing Matrix', cbins, carray,
                    ebins, earray)
    response.GetXaxis().SetTitle('Causes')
    response.GetYaxis().SetTitle('Effects')
    response.SetStats(0)
    response.Sumw2()

    for ci in range(0, cbins):

        # Fill measured effects histogram
        ne_meas.SetBinContent(ci + 1, counts[ci])
        if counts_err is None:
            ne_meas.SetBinError(ci + 1, np.sqrt(counts[ci]))
        else:
            ne_meas.SetBinError(ci + 1, counts_err[ci])

        # Fill response matrix entries
        for ek in range(0, ebins):
            response.SetBinContent(ci + 1, ek + 1, response_array[ek][ci])
            response.SetBinError(ci + 1, ek + 1, response_err_array[ek][ci])

        # Fill efficiency histogram from response matrix
        eff.SetBinContent(ci + 1, efficiencies[ci])
        eff.SetBinError(ci + 1, efficiencies_err[ci])

    # Write measured effects histogram to file
    ne_meas.Write()
    # Write the cause and effect arrays to file
    CARRAY = TH1F('CARRAY', 'Cause Array', cbins, carray)
    CARRAY.GetXaxis().SetTitle('Causes')
    EARRAY = TH1F('EARRAY', 'Effect Array', ebins, earray)
    EARRAY.GetXaxis().SetTitle('Effects')
    CARRAY.Write()
    EARRAY.Write()
    # Write efficiencies histogram to file
    eff.Write()
    # Write response matrix to file
    response.Write()

    fout.Write()
    fout.Close()
示例#20
0
        sys.exit()

    data_obs = dataHist.Clone("data_obs")
    rebinnedData = data_obs.Rebin(len(binning) - 1, "", binning)
## input to combine , open root file

if template:

    myfile = TFile("%s%s.root" % (plotDirectory, myfilename), "update")
    # i have to get the nominal histogram from root file first and get the integration value

    if systematics == '':
        myDatahist = rebinnedData.Clone("nominal")
        mydataDir = "%s/data_obs/" % channel

        if myfile.GetDirectory(mydataDir):
            gDirectory.cd(mydataDir)
            gDirectory.Delete("*;*")
            myDatahist.Write()
        else:
            gDirectory.mkdir(mydataDir)
            gDirectory.cd(mydataDir)
            gDirectory.Delete("*;*")
            myDatahist.Write()
    # create directory only if it does not exist
    ### ele channel
    for iprocess in template_category.keys():

        myfile.cd()
        mydir = "%s/%s/" % (channel, iprocess)
        #print "%s/%s/"%(channel,iprocess)
示例#21
0
    def produce(self):
        from ROOT import TFile, TChain, TH1
        from os.path import exists as pathExists
        from os.path import split as splitPath
        outFilePath = "%s/%s.%s.%s.root" % (self.outPath, "".join(
            self.flags), "processed", self.name)
        #if pathExists(outFilePath):
        #    return
        outFile = TFile(
            "%s/%s.%s.%s.root" %
            (self.outPath, "".join(self.flags), "processed", self.name),
            "recreate")
        for section in self.config.sections():
            trees = None
            if section.startswith("dileptonTree:"):
                treeProducerName = self.config.get(section, "treeProducerName")
                trees = self._getDileptonTrees(section)
                treeName = "DileptonTree"
                subDirName = "%s%s" % (section.split("dileptonTree:")[1],
                                       treeProducerName)

            if not trees == None:
                outDir = None
                srcTree = {}
                for object in trees:
                    srcTree[object] = TChain("%s%s" % (object, treeName))
                    processors = self.config.get(section, "%sProcessors" %
                                                 object).split()
                    filter = " and ".join(processors)
                    if self.config.has_option(section, "%sFilter" % object):
                        filter = self.config.get(section, "%sFilter" % object)

                    ### Quick and dirty workaround to prefer certain primary datasets (double muon > double electron > emu)
                    ### To be in synch with ETH group
                    ### Remove duplication of loops and conditions on path if this is to be removed again

                    for treePath in trees[object]:
                        ### Use events from dimuon primary dataset first
                        if not "DoubleMuon" in treePath:
                            continue
                        #srcFile = TFile(filePath,"r")
                        #srcTree = srcFile.Get(treePath)
                        filePath = "%s.root" % treePath.split(".root")[0]
                        inFile = TFile(filePath, "READ")
                        makeCounterSum = eval(
                            self.config.get("general", "counterSum"))
                        print "Add counter sum: %s" % makeCounterSum
                        if not self.counterSum and makeCounterSum:
                            if not outFile.GetDirectory(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1]):
                                outFile.mkdir(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1])
                            outFile.cd("%sCounters" %
                                       section.split("dileptonTree:")[1])
                            task = None

                            if "vtxWeighter" in processors:
                                t = self.config.get("general", "tasks")

                                if ".%s." % t in splitPath(filePath)[1]:
                                    assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                        filePath, task, t)
                                    task = t
                                else:
                                    task = t
                            else:
                                for t in self.tasks:
                                    if ".%s." % t in splitPath(filePath)[1]:
                                        assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                            filePath, task, t)
                                        task = t
                            self.counterSum = inFile.Get(
                                "%sCounters/analysis paths" % task).Clone()

                        else:
                            pass

                        inFile.Close()
                        srcTree[object].Add(treePath)
                        print "adding", treePath

                    for treePath in trees[object]:
                        ### Then take ee primary dataset
                        if not "DoubleElectron" in treePath:
                            continue
                        #srcFile = TFile(filePath,"r")
                        #srcTree = srcFile.Get(treePath)
                        filePath = "%s.root" % treePath.split(".root")[0]
                        inFile = TFile(filePath, "READ")
                        makeCounterSum = eval(
                            self.config.get("general", "counterSum"))
                        print "Add counter sum: %s" % makeCounterSum
                        if not self.counterSum and makeCounterSum:
                            if not outFile.GetDirectory(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1]):
                                outFile.mkdir(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1])
                            outFile.cd("%sCounters" %
                                       section.split("dileptonTree:")[1])
                            task = None

                            if "vtxWeighter" in processors:
                                t = self.config.get("general", "tasks")

                                if ".%s." % t in splitPath(filePath)[1]:
                                    assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                        filePath, task, t)
                                    task = t
                                else:
                                    task = t
                            else:
                                for t in self.tasks:
                                    if ".%s." % t in splitPath(filePath)[1]:
                                        assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                            filePath, task, t)
                                        task = t
                            self.counterSum = inFile.Get(
                                "%sCounters/analysis paths" % task).Clone()

                        else:
                            pass

                        inFile.Close()
                        srcTree[object].Add(treePath)
                        print "adding", treePath

                    for treePath in trees[object]:
                        ### Now the rest, but avoid taking a tree twice
                        if "DoubleElectron" in treePath or "DoubleMuon" in treePath:
                            continue
                        #srcFile = TFile(filePath,"r")
                        #srcTree = srcFile.Get(treePath)
                        filePath = "%s.root" % treePath.split(".root")[0]
                        inFile = TFile(filePath, "READ")
                        makeCounterSum = eval(
                            self.config.get("general", "counterSum"))
                        print "Add counter sum: %s" % makeCounterSum
                        if not self.counterSum and makeCounterSum:
                            if not outFile.GetDirectory(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1]):
                                outFile.mkdir(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1])
                            outFile.cd("%sCounters" %
                                       section.split("dileptonTree:")[1])
                            task = None

                            if "vtxWeighter" in processors:
                                t = self.config.get("general", "tasks")

                                if ".%s." % t in splitPath(filePath)[1]:
                                    assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                        filePath, task, t)
                                    task = t
                                else:
                                    task = t
                            else:
                                for t in self.tasks:
                                    if ".%s." % t in splitPath(filePath)[1]:
                                        assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                            filePath, task, t)
                                        task = t
                            self.counterSum = inFile.Get(
                                "%sCounters/analysis paths" % task).Clone()

                        else:
                            pass

                        inFile.Close()
                        srcTree[object].Add(treePath)
                        print "adding", treePath

                    srcTree[object].SetBranchStatus("*", 1)

                    for processorName in processors:
                        if processorName == "vtxWeighter":
                            srcTree[object].SetBranchStatus("weight", 0)

                        #### old code
                        if (self.treeProcessors[processorName].__class__.
                                __name__ == SimpleSelector.__name__
                                and not self.config.has_option(
                                    section, "%sFilter" % object)):
                            print "Requirements met, applying simple selection boosting =)"
                            expression = self.treeProcessors[
                                processorName].getExpression(object)
                            print "Cutting tree down to: '%s'" % (expression)
                            srcTree[object] = srcTree[object].CopyTree(
                                expression)
                        ######

                        self.treeProcessors[processorName].prepareSrc(
                            srcTree[object], object, self.treeProcessors)
                for object in trees:
                    processors = self.config.get(section, "%sProcessors" %
                                                 object).split()
                    filter = " and ".join(processors)
                    if self.config.has_option(section, "%sFilter" % object):
                        filter = self.config.get(section, "%sFilter" % object)

                    if not outDir:
                        outDir = outFile.mkdir(subDirName)
                    outFile.cd(subDirName)

                    destTree = srcTree[object].CloneTree(0)
                    destTree.SetAutoSave(5000000000)
                    #print processors
                    for processorName in processors:
                        self.treeProcessors[processorName].prepareDest(
                            destTree, object)
                        #~ print "%s: %d" % (str(processorName), self.treeProcessors[processorName].nEntries)
                    endOfLine = 1000
                    for i in srcTree[object]:
                        if endOfLine < 1:
                            pass
                            #continue
                        endOfLine -= 1
                        processingResults = {}

                        ### old code
                        for processorName in processors:
                            processingResults[
                                processorName] = self.treeProcessors[
                                    processorName].processEvent(
                                        srcTree[object], object)
                        if filter == "" or eval(filter, processingResults):
                            destTree.Fill()
                        ####

                    #srcFile.Close()
                    outFile.Write()
                #from pprint import pprint
                #pprint( trees)

        outFile.Purge()
        outFile.Close()
示例#22
0
def saveHist(hist, channel, analysis, addStat=False):

    #if not 'XZh' in channel or not 'monoB' in channel: return True
    # Blind
    #if options.blind: hist['data_obs'] = hist['BkgSum'].Clone("data_obs")

    # Re-normalize signals
    #for s in hist.keys():
    #if 'bbDM' in s or 'ttDM' in s:
    #print "scaling", s, "by", xsections[sample[s]['files'][0][:-3]]
    #hist[s].Scale( 1./xsections[sample[s]['files'][0][:-3]] )
    #if 'bbDM' in s:
    #hist[s].Scale( kfactors4F5F[sample[s]['files'][0][:-3]] )

    # Normalize to lumi
    #for s in hist.keys():
    #    hist[s].Scale(3000/LUMI)

    # Fix name
    #hist['data_obs'].SetName("data_obs")

    # Merge Z->ll and Z->nunu together
    if 'DYJetsToNuNu_HT' in hist:
        if 'DYJetsToLL_HT' in hist:
            hist['DYJets_HT'] = hist['DYJetsToLL_HT'].Clone('DYJets_HT')
            hist['DYJets_HT'].Add(hist['DYJetsToNuNu_HT'])
            #hist['DYJetsToLL_HT'].SetName("DYJets_HT")
        elif 'DYJetsToLL' in hist:
            hist['DYJets'] = hist['DYJetsToLL'].Clone('DYJets')
            hist['DYJets'].Add(hist['DYJetsToNuNu'])
            #hist['DYJetsToLL'].SetName("DYJets")
        #hist.pop('DYJetsToNuNu_HT')

    # Merge Others
    #hist['Others'] = copy.deepcopy( hist['BkgSum'] )
    #hist['Others'].SetFillColor(920)
    #hist['Others'].SetName("Others")
    #hist['Others'].Reset()
    #for i, s in enumerate(othr):
    #    hist['Others'].Add( hist[s] )

    # Sanity check
#    smax = max(hist, key=lambda x: hist[x].Integral())
#    for i in range(hist[smax].GetNbinsX()):#FIXME
    for s in hist.keys():
        if not hist[s].GetBinContent(i + 1) > 0.:
            hist[s].SetBinContent(i + 1, 1.e-6)  # Sanity check

    nbins = hist['data_obs'].GetNbinsX()
    #CMS_stat_Z0b_bin57

    outFile = TFile("rootfiles/" + analysis + "Hist.root", "UPDATE")
    outFile.cd()
    if outFile.GetDirectory(channel): outFile.rmdir(channel)
    outFile.mkdir(channel)
    outFile.cd(channel)
    #hist['data_obs'].Write("data_obs")
    #hist['Others'].Write()
    for s in hist.keys():
        #if not 'DYJetsToLL' in s and not 'DYJetsToNuNu' in s:
        hist[s].Write()
    outFile.cd("..")
    # Statistical MC uncertainty
    if addStat:
        for s in hist.keys():
            if 'data' in s: continue
            dirname = channel + "/Sys_" + s
            if outFile.GetDirectory(dirname): outFile.rmdir(dirname)
            outFile.mkdir(dirname)
            outFile.cd(dirname)
            for k in range(1, nbins + 1):
                sysname = "CMS_stat_%s_%s_bin%d" % (channel, s, k)
                histUp = hist[s].Clone(sysname + "Up")
                histUp.SetBinContent(
                    k,
                    histUp.GetBinContent(k) + histUp.GetBinError(k))
                histUp.Write()
                histDown = hist[s].Clone(sysname + "Down")
                histDown.SetBinContent(
                    k,
                    max(
                        histDown.GetBinContent(k) - histDown.GetBinError(k),
                        1.e-6))
                histDown.Write()
            outFile.cd("..")

    outFile.Close()
    print "Histograms saved in file rootfiles/" + analysis + "Hist.root"
示例#23
0
    def produce(self):
        from ROOT import TFile, TChain, TH1
        from os.path import exists as pathExists
        from os.path import split as splitPath
        outFilePath = "%s/%s.%s.%s.root" % (self.outPath, "".join(
            self.flags), "processed", self.name)
        #if pathExists(outFilePath):
        #    return
        outFile = TFile(
            "%s/%s.%s.%s.root" %
            (self.outPath, "".join(self.flags), "processed", self.name),
            "recreate")
        for section in self.config.sections():
            trees = None
            if section.startswith("dileptonTree:"):
                treeProducerName = self.config.get(section, "treeProducerName")
                trees = self._getDileptonTrees(section)
                treeName = "DileptonTree"
                subDirName = "%s%s" % (section.split("dileptonTree:")[1],
                                       treeProducerName)
                if "PFHTHLT" in subDirName:
                    subDirName = "%sFinalTrees" % subDirName.split(
                        treeProducerName)[0]
            if section.startswith("isoTree:"):
                treeProducerName = self.config.get(section, "treeProducerName")
                trees = self._getIsoTrees(section)
                treeName = "Iso"
                subDirName = "%s%s" % (section.split("isoTree:")[1],
                                       treeProducerName)
            if not trees == None:
                outDir = None
                srcTree = {}
                for object in trees:

                    srcTree[object] = TChain("%s%s" % (object, treeName))
                    processors = self.config.get(section, "%sProcessors" %
                                                 object).split()
                    filter = " and ".join(processors)
                    if self.config.has_option(section, "%sFilter" % object):
                        filter = self.config.get(section, "%sFilter" % object)
                    for treePath in trees[object]:
                        #srcFile = TFile(filePath,"r")
                        #srcTree = srcFile.Get(treePath)
                        filePath = "%s.root" % treePath.split(".root")[0]
                        inFile = TFile(filePath, "READ")
                        makeCounterSum = eval(
                            self.config.get("general", "counterSum"))
                        print "Add counter sum: %s" % makeCounterSum
                        if not self.counterSum and makeCounterSum:
                            if not outFile.GetDirectory(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1]):
                                outFile.mkdir(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1])
                            outFile.cd("%sCounters" %
                                       section.split("dileptonTree:")[1])
                            task = None

                            if "vtxWeighter" in processors:
                                t = self.config.get("general", "tasks")

                                if ".%s." % t in splitPath(filePath)[1]:
                                    assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                        filePath, task, t)
                                    task = t
                                else:
                                    task = t
                            else:
                                for t in self.tasks:
                                    if ".%s." % t in splitPath(filePath)[1]:
                                        assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                            filePath, task, t)
                                        task = t
                            self.counterSum = inFile.Get(
                                "%sCounters/analysis paths" % task).Clone()

                            ## also add 3D weights
                            #outFile.mkdir("%sWeightSummer" % section.split("dileptonTree:")[1])
                            #outFile.cd("%sWeightSummer" % section.split("dileptonTree:")[1])
                            ## task has been defined above
                            #self.weightSum = inFile.Get("%sWeightSummer/Weights"%task).Clone()
                        else:
                            pass
                            #need to cope with different lumis :(
                            #h = inFile.Get("%sCounters/analysis paths"%task)
                            #print inFile, "%sCounters/analysis paths"%task, h
                            #self.counterSum.Add( h,1. )
                        inFile.Close()
                        srcTree[object].Add(treePath)
                        print "adding", treePath
                    srcTree[object].SetBranchStatus("*", 1)

                    ### signal code
                    if (self.treeProcessors[filter].__class__.__name__
                            == SimpleSelector.__name__
                            and self.config.has_option(section,
                                                       "%sFilter" % object)):
                        expression = self.treeProcessors[filter].getExpression(
                            object)
                        print "Cutting tree down to: '%s'" % (expression)
                        outFile.Write()
                        outFile.Close()
                        srcTree[object] = srcTree[object].CopyTree(expression)
                        outFile = TFile(
                            "%s/%s.%s.%s.root" % (self.outPath, "".join(
                                self.flags), "processed", self.name), "UPDATE")
                    #####

                    for processorName in processors:
                        if processorName == "vtxWeighter":
                            srcTree[object].SetBranchStatus("weight", 0)

                        #### default code
                        #~ if (self.treeProcessors[processorName].__class__.__name__ == SimpleSelector.__name__ and not self.config.has_option(section,"%sFilter"%object)):
                        #~ print "Requirements met, applying simple selection boosting =)"
                        #~ expression = self.treeProcessors[processorName].getExpression(object)
                        #~ print "Cutting tree down to: '%s'" % (expression)
                        #~ srcTree[object] = srcTree[object].CopyTree(expression)
                        ######

                        self.treeProcessors[processorName].prepareSrc(
                            srcTree[object], object, self.treeProcessors)
                for object in trees:
                    processors = self.config.get(section, "%sProcessors" %
                                                 object).split()
                    filter = " and ".join(processors)
                    if self.config.has_option(section, "%sFilter" % object):
                        filter = self.config.get(section, "%sFilter" % object)

                    if not outDir:
                        outDir = outFile.mkdir(subDirName)
                    outFile.cd(subDirName)

                    destTree = srcTree[object].CloneTree(0)
                    destTree.SetAutoSave(5000000000)
                    #print processors
                    for processorName in processors:
                        self.treeProcessors[processorName].prepareDest(
                            destTree, object)
                        #~ print "%s: %d" % (str(processorName), self.treeProcessors[processorName].nEntries)
                    endOfLine = 1000
                    for i in srcTree[object]:
                        if endOfLine < 1:
                            pass
                            #continue
                        endOfLine -= 1
                        processingResults = {}

                        ### default code
                        #~ for processorName in processors:
                        #~ processingResults[processorName] = self.treeProcessors[processorName].processEvent(srcTree[object], object)
                        #~ if filter == "" or eval(filter, processingResults):
                        #~ destTree.Fill()
                        ####

                        ### signal code
                        keep = False
                        for processorName in processors:
                            keep = self.treeProcessors[
                                processorName].processEvent(
                                    srcTree[object], object)

                        if keep:
                            destTree.Fill()
                        ####

                    #srcFile.Close()
                    outFile.Write()
                #from pprint import pprint
                #pprint( trees)

        outFile.Purge()
        outFile.Close()
示例#24
0
def SaveGraph_1D(FileName, outputFile):

	myfile = TFile( FileName )
	print "\nnow loading [%s]" % FileName

	#########################################
	# -- check the scenario (data or MC) -- #
	#########################################
	Scenario = ""
	GraphName = ""
	if "data_25ns" in FileName:
		Scenario = "Data"
		GraphName = Scenario + "_" + FileName.split("_data_25ns_")[1:][0].split(".root")[:1][0]
		
	elif "mc" in FileName:
		Scenario = "MC"
		GraphName = Scenario + "_" + FileName.split("_mc_")[1:][0].split(".root")[:1][0]

	#########################
	# -- Make graph name -- #
	#########################
	
	# print "GraphName will be [%s]" % GraphName

	TreeName = "muonEffs"

	#########################
	# -- sub-directory 1 -- #
	#########################
	Path1 = "%s" % TreeName
	Dir1 = myfile.GetDirectory( Path1 );
	print "now in %s" % Path1

	if Dir1.GetListOfKeys().GetEntries() > 2:
		print "More than 2 directories under %s" % Path1
		sys.exit()

	#########################
	# -- sub-directory 2 -- #
	#########################
	Path2 = "%s/%s/fit_eff_plots" % ( TreeName, Dir1.GetListOfKeys().At(0).GetName() )
	Dir2 = myfile.GetDirectory( Path2 )
	print "now in %s" % Path2

	if Dir2.GetListOfKeys().GetEntries() > 1:
		print "More than 1 directory under %s" % Path2
		sys.exit()

	############################
	# -- Get canvas & graph -- #
	############################
	canvas = Dir2.GetListOfKeys().At(0).ReadObj()
	graph = canvas.GetPrimitive("hxy_fit_eff").Clone();
	name_before = canvas.GetName()
	graph.SetName( GraphName )
	print "\tgraph: %s -> %s" % (name_before, GraphName)

	##############
	# -- Save -- #
	##############
	outputFile.cd()
	graph.Write()

	myfile.Close()
示例#25
0
# QCD in SR = TF * (data - nonQCDBkg from CR)
#----------------------------------------
qcdTFDirInFile = "%s/TF"%histDirInFile
qcdShapeDirInFile = "%s/Shape"%histDirInFile
transferFactor = 1.0
if sample =="QCD_DD" and "Semi" in ttbarDecayMode:
        toPrint("Determining QCD Transfer factor from CR", "")
	#transferFactor = getQCDTransFact(year, channel, nBJets, outputFile, qcdTFDirInFile)
	print "Transfer factor = ", transferFactor
        for hist in histogramsToMake:
            if not histogramInfo[hist][5]: continue
            toPrint("Determining QCD shape from CR", "")
	    dataMinusOtherBkg = getShapeFromCR(year, channel, nJetSel, nBJets, histogramInfo[hist], outputFile, qcdShapeDirInFile)
            histograms.append(dataMinusOtherBkg)
	    print histogramInfo[hist][1]
            histograms[-1].Scale(transferFactor)

#-----------------------------------
# Write final histograms in the file
#-----------------------------------
if not outputFile.GetDirectory(histDirInFile):
    outputFile.mkdir(histDirInFile)
outputFile.cd(histDirInFile)
for h in histograms:
    toPrint("Integral of Histogram %s = "%h.GetName(), h.Integral())
    outputFile.cd(histDirInFile)
    gDirectory.Delete("%s;*"%(h.GetName()))
    h.Write()
toPrint("Path of output root file", outFileFullPath)
outputFile.Close()
示例#26
0
class StoreGate(Logger):
    def __init__(self, outputFile, **kw):
        Logger.__init__(self, **kw)
        if not outputFile.endswith('.root'):
            outputFile += '.root'
        from Gaugi.utilities import retrieve_kw
        # Use this property to rebuild the storegate from a root file
        self._restoreStoreGate = retrieve_kw(kw, 'restoreStoreGate', False)
        filterDirs = retrieve_kw(kw, 'filterDirs', None)
        #Create TFile object to hold everything
        from ROOT import TFile
        from Gaugi import expandPath
        outputFile = expandPath(outputFile)
        if self._restoreStoreGate:
            import os.path
            if not os.path.exists(outputFile):
                raise ValueError("File '%s' does not exist" % outputFile)
            self._file = TFile(outputFile, "read")
        else:
            self._file = TFile(outputFile, "recreate")

        self._currentDir = ""
        self._objects = dict()
        self._dirs = list()
        import os
        self._outputFile = os.path.abspath(outputFile)

        if self._restoreStoreGate:
            retrievedObjs = self.__restore(self._file, filterDirs=filterDirs)
            for name, obj in retrievedObjs:
                self._dirs.append(name)
                self._objects[name] = obj

    def local(self):
        return self._outputFile

    #Save objects and delete storegate
    def __del__(self):
        self._dirs = None
        #for val in self._objects.itervalues():
        #  print "deleting", val
        #  val.Delete()
        self._objects = None
        import gc
        gc.collect()
        if not self._restoreStoreGate:
            self._file.Close()

    def write(self):
        self._file.Write()

    #Create a folder
    def mkdir(self, theDir):
        fullpath = (theDir).replace('//', '/')
        if not fullpath in self._dirs:
            self._dirs.append(fullpath)
            self._file.mkdir(fullpath)
            self._file.cd(fullpath)
            self._currentDir = fullpath
            self._logger.verbose('Created directory with name %s', theDir)

    #Go to the pointed directory
    def cd(self, theDir):
        self._currentDir = ''
        self._file.cd()
        fullpath = (theDir).replace('//', '/')
        if fullpath in self._dirs:
            self._currentDir = fullpath
            if self._file.cd(fullpath):
                return True
        self._logger.error("Couldn't cd to folder %s", fullpath)
        return False

    def addHistogram(self, obj):
        feature = obj.GetName()
        fullpath = (self._currentDir + '/' + feature).replace('//', '/')
        if not fullpath.startswith('/'):
            fullpath = '/' + fullpath
        if not fullpath in self._dirs:
            self._dirs.append(fullpath)
            self._objects[fullpath] = obj
            #obj.Write()
            self._logger.debug('Saving object type %s into %s', type(obj),
                               fullpath)

    def addObject(self, obj):
        feature = obj.GetName()
        fullpath = (self._currentDir + '/' + feature).replace('//', '/')
        if not fullpath.startswith('/'):
            fullpath = '/' + fullpath
        if not fullpath in self._dirs:
            self._dirs.append(fullpath)
            self._objects[fullpath] = obj
            obj.Write()
            self._logger.debug('Saving object type %s into %s', type(obj),
                               fullpath)

    def histogram(self, feature):
        fullpath = (feature).replace('//', '/')
        if not fullpath.startswith('/'):
            fullpath = '/' + fullpath
        if fullpath in self._dirs:
            obj = self._objects[fullpath]
            self._logger.verbose('Retrieving object type %s into %s',
                                 type(obj), fullpath)
            return obj
        else:
            #None object if doesnt exist into the store
            self._logger.warning('Object with path %s doesnt exist', fullpath)
            return None

    def getDir(self, path):
        return self._file.GetDirectory(path)

    # Use this to set labels into the histogram
    def setLabels(self, feature, labels):
        histo = self.histogram(feature)
        if not histo is None:
            try:
                if (len(labels) > 0):
                    for i in range(min(len(labels), histo.GetNbinsX())):
                        bin = i + 1
                        histo.GetXaxis().SetBinLabel(bin, labels[i])
                    for i in range(
                            histo.GetNbinsX(),
                            min(len(labels),
                                histo.GetNbinsX() + histo.GetNbinsY())):
                        bin = i + 1 - histo.GetNbinsX()
                        histo.GetYaxis().SetBinLabel(bin, labels[i])
            except:
                self._logger.fatal("Can not set the labels! abort.")
        else:
            self._logger.warning(
                "Can not set the labels because this feature (%s) does not exist into the storage",
                feature)

    def collect(self):
        self._objects.clear()
        self._dirs = list()

    def getObjects(self):
        return self._objects

    def getDirs(self):
        return self._dirs

    def merge(self, sg):
        if isinstance(sg, StoreGate):
            sg = [sg]
        if isinstance(sg, (list, tuple)):
            sg = StoreGateCollection(sg)
        if not isinstance(sg, StoreGateCollection):
            raise TypeError(type(sg))
        from ROOT import TH1, TH2
        for s in sg:
            for path, obj in s.getObjects().iteritems():
                if isinstance(obj, (TH1, TH2)):
                    if path in self._objects:
                        mobj = self.histogram(path)
                        if mobj: mobj.Add(obj)

    # Use this method to retrieve the dirname and root object
    def __restore(self, d, basepath="/", filterDirs=None):
        """
    Generator function to recurse into a ROOT file/dir and yield (path, obj) pairs
    Taken from: https://root.cern.ch/phpBB3/viewtopic.php?t=11049
    """
        try:
            for key in d.GetListOfKeys():
                kname = key.GetName()
                if key.IsFolder():
                    if filterDirs and kname not in filterDirs:
                        continue
                    for i in self.__restore(d.Get(kname),
                                            basepath + kname + "/"):
                        yield i
                else:
                    yield basepath + kname, d.Get(kname)
        except AttributeError as e:
            self._logger.debug("Ignore reading object of type %s.", type(d))
示例#27
0
def processFile(sample_name, verbose=False):
    sample = sample_name.replace(ANALYSIS + ".", "").replace(".root", "")
    isMC = not '2016' in sample

    # Unweighted input
    ref_file_name = origin + '/' + sample_name
    if not os.path.exists(ref_file_name):
        print '  WARNING: file', ref_file_name, 'does not exist, continuing'
        return True

    # Weighted output
    new_file_name = target + '/' + sample + '.root'
    #if os.path.exists(new_file_name):
    #    print '  WARNING: weighted file exists, overwriting'
    #return True

    new_file = TFile(new_file_name, 'RECREATE')
    new_file.cd()

    # Open old file
    ref_file = TFile(ref_file_name, 'READ')
    ref_hist = ref_file.Get('Events')
    try:
        totalEntries = ref_hist.GetBinContent(1)
    except:
        print '  ERROR: nEvents not found in file', sample
        exit(1)

    # Open stitching file
    stitch_file = TFile("stitching.root", 'READ')
    stitch_num = {}
    if sample.startswith('DY'):
        for i in range(len(binsNb)):
            stitch_num[i] = stitch_file.Get("nEvents_%s_nB%d" % (sample, i))
        stitch_den = stitch_file.Get('nEvents')

    # Cross section
    XS = xsection[sample]['xsec'] * xsection[sample]['kfactor'] * xsection[
        sample]['br']
    Leq = LUMI * XS / totalEntries if totalEntries > 0 else 0.

    print sample, ": Leq =", (Leq if isMC else "Data")

    # Variables declaration
    stitchWeight = array('f', [1.0])
    eventWeightLumi = array('f', [1.0])  # global event weight with lumi

    # Looping over file content
    for key in ref_file.GetListOfKeys():
        obj = key.ReadObj()
        # Histograms
        if obj.IsA().InheritsFrom('TH1'):
            if verbose: print ' + TH1:', obj.GetName()
            new_file.cd()
            if isMC: obj.Scale(LUMI * XS / totalEntries)
            obj.Write()
        # Tree
        elif obj.IsA().InheritsFrom('TTree'):
            nev = obj.GetEntriesFast()
            new_file.cd()
            new_tree = obj.CopyTree("")
            # New branches
            stitchWeightBranch = new_tree.Branch('stitchWeight', stitchWeight,
                                                 'stitchWeight/F')
            eventWeightLumiBranch = new_tree.Branch('eventWeightLumi',
                                                    eventWeightLumi,
                                                    'eventWeightLumi/F')

            # looping over events
            for event in range(0, obj.GetEntries()):
                if verbose and (event % 10000 == 0 or event == nev - 1):
                    print ' = TTree:', obj.GetName(
                    ), 'events:', nev, '\t', int(100 * float(event + 1) /
                                                 float(nev)), '%\r',
                #print '.',#*int(20*float(event)/float(nev)),#printProgressBar(event, nev)
                obj.GetEntry(event)
                # Initialize
                eventWeightLumi[0] = stitchWeight[0] = 1.
                # Weights
                if isMC:
                    # MC stitching
                    if sample.startswith(
                            'DY') and 'LL' in sample and not 'BJets' in sample:
                        nB = min(int(obj.LheNb), binsNb[-1])
                        bin = stitch_den.FindBin(
                            min(obj.LheHT, binsHT[-1] - 1),
                            min(obj.LheV_pt, binsV_pt[-1] - 1),
                            min(obj.LheNj, binsNj[-1]))
                        nS = stitch_num[nB].GetBinContent(bin)
                        nT = stitch_den.GetBinContent(bin)
                        stitchWeight[0] = nS / nT

                    # if sample=='DYJetsToLL' or sample=='WJetsToLNu':
                    #     if obj.LheHT > 100.: stitchWeight[0] = 0.
                    #     else: stitchWeight[0] = 0.843
                    eventWeightLumi[0] = obj.eventWeight * obj.bTagWeight
                    eventWeightLumi[0] *= LUMI * XS / totalEntries

                # Fill the branches
                stitchWeightBranch.Fill()
                eventWeightLumiBranch.Fill()

            new_file.cd()
            new_tree.Write()
            if verbose: print ' '

        # Directories
        elif obj.IsFolder():
            subdir = obj.GetName()
            if verbose: print ' \ Directory', subdir, ':'
            new_file.mkdir(subdir)
            new_file.cd(subdir)
            for subkey in ref_file.GetDirectory(subdir).GetListOfKeys():
                subobj = subkey.ReadObj()
                if subobj.IsA().InheritsFrom('TH1'):
                    if verbose: print '   + TH1:', subobj.GetName()
                    new_file.cd(subdir)
                    if isMC: subobj.Scale(LUMI * XS / totalEntries)
                    subobj.Write()
            new_file.cd('..')

    new_file.Close()
示例#28
0
def processFile(sample, origin, target, verbose=False):

    #isSignal = True if "signal" in sample or "Zprime" in sample else False
    isSignal = True if mcType == "signal" else False

    if not os.path.exists(origin):
        print 'Origin directory', origin, 'does not exist, aborting...'
        sys.exit()
    if not os.path.exists(target):
        print 'Target directory', target, 'does not exist, creating it now'
        os.makedirs(target)

    ## Unweighted input
    #ref_file_name = origin + '/' + sample_name
    #if not os.path.exists(ref_file_name):
    #    print '  WARNING: file', ref_file_name, 'does not exist, continuing'
    #    return True

    # first loop over all files to get nGenEvents
    totalEntries = 0.

    file_list = [origin + "/" + sample + ".root"]  ## this is the original line

    ### this here is for big ntuples:
    #file_list = []
    #j = 0

    #while True:
    #    if os.path.exists(origin + "/" + sample + "_flatTuple_{}.root".format(j)):
    #        file_list.append(origin + "/" + sample + "_flatTuple_{}.root".format(j))
    #        j += 1
    #    else:
    #        print "found {} files for sample:".format(j), sample
    #        break
    #if j == 0:
    #    print '  WARNING: files for sample', sample , 'do not exist, continuing'
    #    return True
    ### end big ntuples

    if isMC:
        for infile_name in file_list:
            infile = TFile(infile_name, 'READ')
            try:
                evtHist = infile.Get('Events')
                nEvents = evtHist.GetBinContent(1)
                totalEntries += nEvents
                #print "sample:",sample,"nEvents:", nEvents
            except:
                print '  ERROR: nEvents not found in file', sample
                exit(1)
            infile.Close()
        print "totalEntries =", totalEntries

    # Cross section
    XS = getXsec(sample.replace(year + '_', ''))
    print "XS = ", XS

    Leq = LUMI * XS / totalEntries if totalEntries > 0 else 0.
    #if isSignal:                                                               ## commented out since totalEntries now takes care of btagging weights in case of signal
    #    signName = sample.replace('MC_signal_', 'ZpBB')
    #    if '2016' in signName:
    #        yr = '2016'
    #    elif '2017' in signName:
    #        yr = '2017'
    #    elif '2018' in signName:
    #        yr = '2018'
    #    signName = signName.replace(yr,'')
    #    print "number of generated events:", SAMPLE[signName]['genEvents'][yr]
    #    Leq = LUMI*XS/SAMPLE[signName]['genEvents'][yr]
    print sample, ": Leq =", (Leq if isMC else "Data")

    # Open old files now to plug in weights
    for ref_file_name in file_list:
        print "working on file:", ref_file_name

        # Weighted output
        new_file_name = ref_file_name.replace(origin, target)

        new_file = TFile(new_file_name, 'RECREATE')
        new_file.cd()

        ref_file = TFile(ref_file_name, 'READ')

        # Variables declaration
        eventWeightLumi = array('f', [1.0])  # global event weight with lumi

        # get tree
        tree = ref_file.Get("tree")
        nev = tree.GetEntriesFast()
        new_file.cd()
        new_tree = tree.CopyTree("")
        # New branches
        eventWeightLumiBranch = new_tree.Branch('eventWeightLumi',
                                                eventWeightLumi,
                                                'eventWeightLumi/F')

        # looping over events
        for event in range(0, tree.GetEntries()):
            if verbose and (event % 10000 == 0 or event == nev - 1):
                print ' = TTree:', tree.GetName(), 'events:', nev, '\t', int(
                    100 * float(event + 1) / float(nev)), '%\r',
            tree.GetEntry(event)

            # Initialize
            eventWeightLumi[0] = 1.

            # Weights
            if isMC:
                eventWeightLumi[0] = tree.GenWeight * Leq
                #if isSignal: eventWeightLumi[0] = Leq  ## FIXME FIXME check if this is sufficient!! FIXME FIXME
                if isSignal:
                    eventWeightLumi[0] = tree.BTagAK4Weight_deepJet * Leq

            # Fill the branches
            eventWeightLumiBranch.Fill()
        new_file.cd()
        new_tree.Write("", TObject.kOverwrite)
        if verbose: print ' '

        # Looping over additional file content
        for key in ref_file.GetListOfKeys():
            obj = key.ReadObj()
            # Histograms
            if obj.IsA().InheritsFrom('TH1'):
                if verbose: print ' + TH1:', obj.GetName()
                new_file.cd()
                obj.Write()
            # Tree
            elif obj.IsA().InheritsFrom('TTree'):
                continue
                #nev = obj.GetEntriesFast()
                #new_file.cd()
                #new_tree = obj.CopyTree("")
                ## New branches
                #eventWeightLumiBranch = new_tree.Branch('eventWeightLumi', eventWeightLumi, 'eventWeightLumi/F')

                ## looping over events
                #for event in range(0, obj.GetEntries()):
                #    if verbose and (event%10000==0 or event==nev-1): print ' = TTree:', obj.GetName(), 'events:', nev, '\t', int(100*float(event+1)/float(nev)), '%\r',
                #    obj.GetEntry(event)
                #    # Initialize
                #    eventWeightLumi[0] = 1.

                #    # Weights
                #    if isMC:
                #        eventWeightLumi[0] = obj.GenWeight * Leq
                #        if isSignal: eventWeightLumi[0] = Leq

                #    # Fill the branches
                #    eventWeightLumiBranch.Fill()
                #new_file.cd()
                #new_tree.Write()
                #if verbose: print ' '

            # Directories
            elif obj.IsFolder():
                subdir = obj.GetName()
                if verbose: print ' \ Directory', subdir, ':'
                new_file.mkdir(subdir)
                new_file.cd(subdir)
                for subkey in ref_file.GetDirectory(subdir).GetListOfKeys():
                    subobj = subkey.ReadObj()
                    if subobj.IsA().InheritsFrom('TH1'):
                        if verbose: print '   + TH1:', subobj.GetName()
                        new_file.cd(subdir)
                        subobj.Write()
                new_file.cd('..')

        ref_file.Close()
        new_file.Close()
示例#29
0
def processFile(sample_name, verbose=False):
    sample = sample_name.replace(ANALYSIS + ".", "").replace(".root", "")
    isMC = not '2016' in sample

    # Unweighted input
    ref_file_name = origin + '/' + sample_name
    if not os.path.exists(ref_file_name):
        print '  WARNING: file', ref_file_name, 'does not exist, continuing'
        return True

    # Weighted output
    new_file_name = target + '/' + sample + '.root'
    #if os.path.exists(new_file_name):
    #    print '  WARNING: weighted file exists, overwriting'
    #return True

    new_file = TFile(new_file_name, 'RECREATE')
    new_file.cd()

    # Open old file
    ref_file = TFile(ref_file_name, 'READ')
    ref_hist = ref_file.Get('Events')
    try:
        totalEntries = ref_hist.GetBinContent(1)
    except:
        print '  ERROR: nEvents not found in file', sample
        exit(1)

    old_tree = ref_file.Get("tree")
    #check if the tree already has the lumi weights
    hasLumiWeights = any(True for x in old_tree.GetListOfBranches()
                         if "eventWeightLumi" in x.GetName())

    # Cross section

    XS = xsection[sample]['xsec'] * xsection[sample]['kfactor'] * xsection[
        sample]['br']
    Leq = LUMI * XS / totalEntries if totalEntries > 0 else 0.

    print sample, ": Leq =", (Leq if isMC else "Data")

    for key in ref_file.GetListOfKeys():
        obj = key.ReadObj()
        # Histograms
        if obj.IsA().InheritsFrom('TH1'):
            if verbose: print ' + TH1:', obj.GetName()
            new_file.cd()
            if isMC and not hasLumiWeights: obj.Scale(LUMI * XS / totalEntries)
            obj.Write()
        # Tree
        elif obj.IsA().InheritsFrom('TTree'):
            continue
        # Directories
        elif obj.IsFolder():
            subdir = obj.GetName()
            if verbose: print ' \ Directory', subdir, ':'
            new_file.mkdir(subdir)
            new_file.cd(subdir)
            for subkey in ref_file.GetDirectory(subdir).GetListOfKeys():
                subobj = subkey.ReadObj()
                if subobj.IsA().InheritsFrom('TH1'):
                    if verbose: print '   + TH1:', subobj.GetName()
                    new_file.cd(subdir)
                    if isMC and not hasLumiWeights:
                        subobj.Scale(LUMI * XS / totalEntries)
                    subobj.Write()
            new_file.cd('..')

    # Variables declaration
    #dummy = array('f', [1.0]) #example variable
    mTb_massless = array('f', [1.0])  #example variable
    mTb_mass = array('f', [1.0])  #example variable
    min_DeltaR = array('f', [1.0])
    max_DeltaR = array('f', [1.0])
    minDphiJet1BJet = array('f', [1.0])
    minDphiJet2BJet = array('f', [1.0])
    topdRMass = array('f', [1.0])
    WdRMass = array('f', [1.0])
    Dphi_Wb = array('f', [1.0])
    Dphi_topb = array('f', [1.0])
    Dphi_WMET = array('f', [1.0])
    Dphi_topMET = array('f', [1.0])
    pt_bjet = array('f', [1.0])
    DeltaR_bb = array('f', [1.0])
    cosTheta_bb = array('f', [1.0])
    HT3 = array('f', [1.0])
    sphericity = array('f', [1.0])
    aplanarity = array('f', [1.0])
    circularity = array('f', [1.0])
    isotropy = array('f', [1.0])
    C = array('f', [1.0])
    D = array('f', [1.0])

    stitchWeight = array('f', [1.0])
    eventWeightLumi = array('f', [1.0])  # global event weight with lumi

    nev = old_tree.GetEntriesFast()
    new_file.cd()

    #we are not going to copy the entire old tree
    tname = "Ftree"
    new_tree = TTree(tname, tname)
    if copytree:
        tname = "tree"
        new_tree = old_tree.CopyTree("")

    #define new branches you want to add here
    #!!!Attention: if run twice, this will create a new branch with the same name as the old one
    mTb_massBranch = new_tree.Branch('mTb_mass', mTb_mass, 'mTb_mass/F')
    mTb_masslessBranch = new_tree.Branch('mTb_massless', mTb_massless,
                                         'mTb_massless/F')

    minDphiJet1BJetBranch = new_tree.Branch('minDphiJet1BJet', minDphiJet1BJet,
                                            'minDphiJet1BJet/F')
    minDphiJet2BJetBranch = new_tree.Branch('minDphiJet2BJet', minDphiJet2BJet,
                                            'minDphiJet2BJet/F')
    min_DeltaRBranch = new_tree.Branch('min_DeltaR', min_DeltaR,
                                       'min_DeltaR/F')
    max_DeltaRBranch = new_tree.Branch('max_DeltaR', max_DeltaR,
                                       'max_DeltaR/F')
    topdRMassBranch = new_tree.Branch('topdRMass', topdRMass, 'topdRMass/F')
    WdRMassBranch = new_tree.Branch('WdRMass', WdRMass, 'WdRMass/F')
    Dphi_WbBranch = new_tree.Branch('Dphi_Wb', Dphi_Wb, 'Dphi_Wb/F')
    Dphi_topbBranch = new_tree.Branch('Dphi_topb', Dphi_topb, 'Dphi_topb/F')
    Dphi_WMETBranch = new_tree.Branch('Dphi_WMET', Dphi_WMET, 'Dphi_WMET/F')
    Dphi_topMETBranch = new_tree.Branch('Dphi_topMET', Dphi_topMET,
                                        'Dphi_topMET/F')
    pt_bjetBranch = new_tree.Branch('pt_bjet', pt_bjet, 'pt_bjet/F')
    DeltaR_bbBranch = new_tree.Branch('DeltaR_bb', DeltaR_bb, 'DeltaR_bb/F')
    cosTheta_bbBranch = new_tree.Branch('cosTheta_bb', cosTheta_bb,
                                        'cosTheta_bb/F')
    HT3Branch = new_tree.Branch('HT3', HT3, 'HT3/F')

    sphericityBranch = new_tree.Branch('sphericityFT', sphericity,
                                       'sphericityFT/F')
    aplanarityBranch = new_tree.Branch('aplanarityFT', aplanarity,
                                       'aplanarityFT/F')
    circularityBranch = new_tree.Branch('circularityFT', circularity,
                                        'circularityFT/F')
    isotropyBranch = new_tree.Branch('isotropyFT', isotropy, 'isotropyFT/F')
    CBranch = new_tree.Branch('CFT', C, 'CFT/F')
    DBranch = new_tree.Branch('DFT', D, 'DFT/F')

    if not (hasLumiWeights
            ):  #only do lumi branches if they don't exist already
        stitchWeightBranch = new_tree.Branch('stitchWeight', stitchWeight,
                                             'stitchWeight/F')
        eventWeightLumiBranch = new_tree.Branch('eventWeightLumi',
                                                eventWeightLumi,
                                                'eventWeightLumi/F')

    # looping over events
    for event in range(0, old_tree.GetEntries()):
        if verbose and (event % 10000 == 0 or event == nev - 1):
            print ' = TTree:', old_tree.GetName(), 'events:', nev, '\t', int(
                100 * float(event + 1) / float(nev)), '%\r',
        old_tree.GetEntry(event)

        #not necessarily elegant but should do the trick, if I have a lot of time fix to use 4-vector array
        csv = [
            old_tree.Jet1_csv, old_tree.Jet2_csv, old_tree.Jet3_csv,
            old_tree.Jet4_csv
        ]
        id_csv = csv.index(max(csv))
        id2_csv = heapq.nlargest(2, xrange(len(csv)), key=csv.__getitem__)[1]

        jets = [
            (old_tree.Jet1_pt, old_tree.Jet1_eta, old_tree.Jet1_phi,
             old_tree.Jet1_E),
            (old_tree.Jet2_pt, old_tree.Jet2_eta, old_tree.Jet2_phi,
             old_tree.Jet2_E),
            (old_tree.Jet3_pt, old_tree.Jet3_eta, old_tree.Jet3_phi,
             old_tree.Jet3_E),
            (old_tree.Jet4_pt, old_tree.Jet4_eta, old_tree.Jet4_phi,
             old_tree.Jet4_E),
        ]
        #        print jets
        #        print jets[id_csv]

        minDphiJet1BJet[0] = Return_minDphiJetBJet(jets, id_csv)[0]
        minDphiJet2BJet[0] = Return_minDphiJetBJet(jets, id_csv)[1]

        topdRMass[0] = Return_Top(jets, id_csv, old_tree.MET_phi)[0]
        WdRMass[0] = Return_Top(jets, id_csv, old_tree.MET_phi)[1]

        Dphi_Wb[0] = Return_Top(jets, id_csv, old_tree.MET_phi)[2]
        Dphi_topb[0] = Return_Top(jets, id_csv, old_tree.MET_phi)[3]
        Dphi_WMET[0] = Return_Top(jets, id_csv, old_tree.MET_phi)[4]
        Dphi_topMET[0] = Return_Top(jets, id_csv, old_tree.MET_phi)[5]

        jets_EtaPhi = [
            (old_tree.Jet1_eta, old_tree.Jet1_phi),
            (old_tree.Jet2_eta, old_tree.Jet2_phi),
            (old_tree.Jet3_eta, old_tree.Jet3_phi),
            (old_tree.Jet4_eta, old_tree.Jet4_phi),
            (old_tree.Jet5_eta, old_tree.Jet5_phi),
            (old_tree.Jet6_eta, old_tree.Jet6_phi),
        ]

        min_DeltaR[0] = Return_DeltaR(jets_EtaPhi, 1)[0]
        max_DeltaR[0] = Return_DeltaR(jets_EtaPhi, 2)[1]

        HT3[0] = 0
        for i, j in enumerate(jets):
            if (i != 0 and i != 1):
                if (j[0] > 0):
                    HT3[0] += j[0]

        if (csv[id_csv] >= 0 and csv[id2_csv] >= 0):
            DeltaR_bb[0] = math.sqrt(
                math.pow((jets_EtaPhi[id_csv][0] -
                          jets_EtaPhi[id2_csv][0]), 2) +
                math.pow((jets_EtaPhi[id_csv][1] -
                          jets_EtaPhi[id2_csv][1]), 2))
            cosTheta_bb[0] = math.fabs(
                math.tanh(
                    (jets_EtaPhi[id_csv][1] - jets_EtaPhi[id2_csv][1]) / 2))
        else:
            DeltaR_bb[0] = -9
            cosTheta_bb[0] = -9

        if max(csv) >= 0:
            mTbs = Return_mTbs(jets[id_csv], old_tree.MET_pt, old_tree.MET_phi)

            mTb_mass[0] = mTbs['mTb_mass']
            mTb_massless[0] = mTbs['mTb_massless']
            pt_bjet[0] = jets[id_csv][0]
        else:
            mTb_mass[0] = mTb_massless[0] = -99
            pt_bjet[0] = -9

        if not (hasLumiWeights):
            eventWeightLumi[0] = stitchWeight[0] = 1

        #Event shape variables, need 4vectors
        jets4vec = getJet4Vectors(old_tree)
        if len(jets4vec) > 0:
            eventshape = ROOT.EventShapeVariables(jets4vec)
            #actually (r) with r=2 for normal and r=1 infrared safe defintion
            aplanarity[0] = eventshape.aplanarity()
            sphericity[0] = eventshape.sphericity()
            circularity[0] = eventshape.circularity()
            isotropy[0] = eventshape.isotropy()
            C[0] = eventshape.C()
            D[0] = eventshape.D()
        else:
            sphericity[0] = -99
            aplanarity[0] = -99
            sphericity_AZ[0] = -99
            aplanarity_AZ[0] = -99
            circularity[0] = -99
            isotropy[0] = -99
            C[0] = -99
            D[0] = -99

        if isMC:
            # MC stitching
            if sample == 'DYJetsToLL' or sample == 'WJetsToLNu' or sample == 'W1JetsToLNu' or sample == 'W2JetsToLNu' or sample == 'W3JetsToLNu' or sample == 'W4JetsToLNu':
                if old_tree.LheHT > 70.: stitchWeight[0] = 0.
            if not (hasLumiWeights):
                eventWeightLumi[
                    0] = old_tree.eventWeight * old_tree.bTagWeight * old_tree.TopWeight
                eventWeightLumi[0] *= LUMI * XS / totalEntries

            #fill the new branches of the tree
        if not (hasLumiWeights):
            if copytree:
                stitchWeightBranch.Fill()
                eventWeightLumiBranch.Fill()

        if copytree:
            #dummyBranch.Fill()

            mTb_massBranch.Fill()
            mTb_masslessBranch.Fill()
            min_DeltaRBranch.Fill()
            max_DeltaRBranch.Fill()
            minDphiJet1BJetBranch.Fill()
            minDphiJet2BJetBranch.Fill()
            topdRMassBranch.Fill()
            WdRMassBranch.Fill()
            Dphi_WbBranch.Fill()
            Dphi_topbBranch.Fill()
            Dphi_WMETBranch.Fill()
            Dphi_topMETBranch.Fill()
            pt_bjetBranch.Fill()
            DeltaR_bbBranch.Fill()
            cosTheta_bbBranch.Fill()
            HT3Branch.Fill()
            sphericityBranch.Fill()
            aplanarityBranch.Fill()
            circularityBranch.Fill()
            isotropyBranch.Fill()
            CBranch.Fill()
            DBranch.Fill()

        else:
            new_tree.Fill()

    new_file.cd()
    new_tree.Write(tname, TObject.kOverwrite)
    if verbose: print ' '
    new_file.Close()
示例#30
0
def skimTree(sample, filename):

    # Unweighted input
    inName = origin + '/' + sample + '/' + filename
    if not os.path.exists(inName):
        print '- WARNING: file', inName, 'does not exist, continuing'
        return True

    # Weighted output
    if not os.path.exists(target + '/' + sample):
        os.mkdir(target + '/' + sample)

    outName = target + '/' + sample + '/' + filename
    if os.path.exists(outName):
        print '- WARNING: weighted file exists, overwriting'
        #return True

    outFile = TFile(outName, 'RECREATE')
    outFile.cd()

    # Open old file
    inFile = TFile(inName, 'READ')

    # Looping over file content
    for key in inFile.GetListOfKeys():
        obj = key.ReadObj()
        # Histograms
        if obj.IsA().InheritsFrom('TH1'):
            if verbose: print ' + TH1:', obj.GetName()
            outFile.cd()
            obj.Write()
        # Tree
        elif obj.IsA().InheritsFrom('TTree'):
            outFile.cd()
            newTree = obj.CopyTree(skimString if obj.GetName() ==
                                   "Events" else "")
            ###
            absPhoton_eta = array('f', [1.0])
            absPhoton_etaBranch = newTree.Branch('absPhoton_eta',
                                                 absPhoton_eta,
                                                 'absPhoton_eta/F')
            for event in range(0, obj.GetEntries()):
                obj.GetEntry(event)
                absPhoton_eta[0] = abs(obj.Photon_eta[obj.iPhoton]) if hasattr(
                    obj, "Photon_eta") else -1.
                absPhoton_etaBranch.Fill()
            ###
            newTree.Write(obj.GetName(), TObject.kOverwrite)
            if verbose: print ' + TTree:', obj.GetName()
        # Directories
        elif obj.IsFolder():
            subdir = obj.GetName()
            if verbose: print ' \ Directory', subdir, ':'
            outFile.mkdir(subdir)
            outFile.cd(subdir)
            for subkey in inFile.GetDirectory(subdir).GetListOfKeys():
                subobj = subkey.ReadObj()
                if subobj.IsA().InheritsFrom('TH1'):
                    if verbose: print '   + TH1:', subobj.GetName()
                    outFile.cd(subdir)
                    subobj.Write()
            outFile.cd('..')

    outFile.Close()