Ejemplo n.º 1
0
def parallelTreeWorker(item):
    import random, string, os
    filename, _tree, value, cut, _hist, weight, f = item
    rfile = File(filename)
    tree = rfile.Get(_tree)
    tree = asrootpy(tree)
    _hist = asrootpy(_hist)
    try:
        if weight is None:
            tree.Draw(value, selection=cut, hist=_hist)
        else:
            #_tree.Draw(value,selection="(%s)*(%s)"%(cut,weight),hist=self.hists[f])
            tmpFileName = ''.join(
                random.choice(string.ascii_lowercase) for i in range(4))
            tmpFile = File("/tmp/%s.root" % tmpFileName, "recreate")
            #sel_tree=_tree.copy_tree(selection=cut)
            sel_tree = asrootpy(tree.CopyTree(cut))
            ##print weight
            sel_tree.Draw(value, selection=weight, hist=_hist)
            tmpFile.Close()
            os.remove("/tmp/%s.root" % tmpFileName)
    except Exception as e:
        print(tree, value, cut, _hist, weight, f)
        log_plotlib.info("error:%s" % (e))
        log_plotlib.info("file :%s" % (f))
        log_plotlib.info("Perhaps try this one:")
        for i in tree.glob("*"):
            log_plotlib.info(i)
        raise RuntimeError("Will stop here!")
    rfile.Close()
    del (tree)
    return (_hist, f)
Ejemplo n.º 2
0
def write_to_root_files(parsed_hists, parsed_log_hists, output_filename):
    start = time.time()

    f = TFile(output_filename[0], "RECREATE")

    for var in parsed_hists.keys():

        index = 0

        for mod_hist in parsed_hists[var]:
            hist = copy.deepcopy(mod_hist.hist())
            hist.SetName("{}#{}".format(var, index))
            hist.Write()

            index += 1

    f.Close()

    f = TFile(output_filename[1], "RECREATE")

    for var in parsed_log_hists.keys():

        index = 0

        for mod_hist in parsed_log_hists[var]:
            hist = copy.deepcopy(mod_hist.hist())
            hist.SetName("{}#{}".format(var, index))
            hist.Write()

            index += 1

    f.Close()

    end = time.time()
Ejemplo n.º 3
0
def get_histogram_from_file( histogram_path, input_file ):
    current_btag, found_btag = find_btag(histogram_path)

    root_file = File( input_file )
    get_histogram = root_file.Get


    if not found_btag or not current_btag in sumations.b_tag_summations.keys():
        root_histogram = get_histogram( histogram_path )
        if not is_valid_histogram( root_histogram, histogram_path, input_file ):
            return
    else:
        listOfExclusiveBins = sumations.b_tag_summations[current_btag]
        exclhists = []

        for excbin in listOfExclusiveBins:
            hist = get_histogram( histogram_path.replace( current_btag, excbin ) )
            if not is_valid_histogram( hist, histogram_path.replace( current_btag, excbin ), input_file ):
                return
            exclhists.append( hist )
        root_histogram = exclhists[0].Clone()

        for hist in exclhists[1:]:
            root_histogram.Add( hist )

    gcd()
    histogram = None
    # change from float to double
    if root_histogram.TYPE == 'F':
        histogram = root_histogram.empty_clone(type='D')
        histogram.Add(root_histogram)
    else:
        histogram = root_histogram.Clone()
    root_file.Close()
    return histogram
Ejemplo n.º 4
0
 def setUp(self):
     f = File('test.root', 'recreate')
     tree = create_test_tree()
     h = create_test_hist()
     h.write()
     tree.write()
     f.write()
     f.Close()
def can_open_ROOT_file(filename):
    passesCheck = False
    try:
        openFile = File(filename, 'r')
        if openFile:
            passesCheck = True
            openFile.Close()
    except:
        print "Could not open ROOT file"

    return passesCheck
Ejemplo n.º 6
0
 def getHistFromTree(self,
                     bins,
                     xmin,
                     xmax,
                     xtitle,
                     cut,
                     value,
                     tree,
                     weight=None):
     from rootpy.plotting import Hist
     import random, string, os
     self.clearHists()
     for f in self.files:
         try:
             _tree = self.files[f].Get(tree)
         except AttributeError as e:
             log_plotlib.warning("No %s in %s" % (tree, f))
             log_plotlib.warning(
                 "Will try without %s, and add an empty hist." % f)
             log_plotlib.warning(e)
             self.hists[f] = Hist(binns, xmin, xmax)
             continue
         self.hists[f] = Hist(bins, xmin, xmax)
         self.hists[f].GetXaxis().SetTitle(xtitle)
         try:
             if weight is None:
                 _tree.Draw(value, selection=cut, hist=self.hists[f])
             else:
                 #_tree.Draw(value,selection="(%s)*(%s)"%(cut,weight),hist=self.hists[f])
                 tmpFileName = ''.join(
                     random.choice(string.ascii_lowercase)
                     for i in range(4))
                 tmpFile = File("/tmp/%s.root" % tmpFileName, "recreate")
                 #sel_tree=_tree.copy_tree(selection=cut)
                 sel_tree = asrootpy(_tree.CopyTree(cut))
                 ##print weight
                 sel_tree.Draw(value, selection=weight, hist=self.hists[f])
                 tmpFile.Close()
                 os.remove("/tmp/%s.root" % tmpFileName)
         except Exception as e:
             log_plotlib.info("error:%s" % (e))
             log_plotlib.info("file :%s" % (f))
             log_plotlib.info("Perhaps try this one:")
             for i in _tree.glob("*"):
                 log_plotlib.info(i)
             raise RuntimeError("Will stop here!")
         self.hists[f].Scale(self._getWeight(f))
         self.hists[f].Sumw2()
     if self._joinList is not False:
         self.joinList(self._joinList)
     for hist in self.hists:
         if hist in self.style:
             self.hists[hist].decorate(**self.style[hist])
Ejemplo n.º 7
0
def parse_to_root_file(input_filename, output_filename, hist_templates):

    print "Parsing {} to {}".format(input_filename, output_filename)

    parsed_hists, parsed_log_hists = parse_file(
        input_filename, copy.deepcopy(hist_templates[0]),
        copy.deepcopy(hist_templates[1]))

    f = TFile(output_filename[0], "RECREATE")

    for var in parsed_hists.keys():

        index = 0

        for mod_hist in parsed_hists[var]:
            hist = copy.deepcopy(mod_hist.hist())
            hist.SetName("{}#{}".format(var, index))
            hist.Write()

            index += 1

    f.Close()

    f = TFile(output_filename[1], "RECREATE")

    for var in parsed_log_hists.keys():

        index = 0

        for mod_hist in parsed_log_hists[var]:
            hist = copy.deepcopy(mod_hist.hist())
            hist.SetName("{}#{}".format(var, index))
            hist.Write()

            index += 1

    f.Close()
Ejemplo n.º 8
0
def write_to_root_file(output_filename, parsed_hists):
    f = TFile(output_filename, "RECREATE")

    for var in parsed_hists.keys():

        index = 0

        for mod_hist in parsed_hists[var]:
            hist = copy.deepcopy(mod_hist.hist())
            hist.SetName("{}#{}".format(var, index))
            hist.Write()

            index += 1

    f.Close()
Ejemplo n.º 9
0
def parse_to_root_file(input_filename, output_filename, hist_templates):

    print "Parsing {} to {}".format(input_filename, output_filename)

    parsed_hists = parse_file(input_filename, copy.deepcopy(hist_templates))

    f = TFile(output_filename, "RECREATE")

    for var in parsed_hists.keys():
        mod_hist = parsed_hists[var]
        hist = copy.deepcopy(mod_hist.hist())
        hist.SetName("{}".format(var))
        hist.Write()

    f.Close()
def cleanFile(filename):
    testfile = File(filename, 'update')
    nHistograms = 0
    print "Deleting inclusive bin histograms histograms"
    for folder, emptyThing, histograms in testfile:
        for histogram in histograms:
            currentPath = folder + '/' + histogram
            for btag in btag_bins_inclusive:
                if btag in histogram:
                    nHistograms += 1
                    #                    print 'Deleting:', currentPath
                    testfile.Delete(currentPath + ';*')
    print 'Deleted', nHistograms, 'histograms'
    print 'Closing file', filename
    testfile.Close()
    print 'Closed file'
Ejemplo n.º 11
0
def get_histograms_from_trees(
    trees=[],
    branch='var',
    weightBranch='EventWeight',
    selection='1',
    files={},
    verbose=False,
    nBins=40,
    xMin=0,
    xMax=100,
    ignoreUnderflow=True,
):
    histograms = {}
    nHistograms = 0

    # Setup selection and weight string for ttree draw
    weightAndSelection = '( %s ) * ( %s )' % (weightBranch, selection)

    for sample, input_file in files.iteritems():
        root_file = File(input_file)

        get_tree = root_file.Get
        histograms[sample] = {}

        for tree in trees:

            tempTree = tree
            if 'data' in sample and ('Up' in tempTree or 'Down' in tempTree):
                tempTree = tempTree.replace('_' + tempTree.split('_')[-1], '')

            currentTree = get_tree(tempTree)
            root_histogram = Hist(nBins, xMin, xMax)
            currentTree.Draw(branch, weightAndSelection, hist=root_histogram)
            if not is_valid_histogram(root_histogram, tree, input_file):
                return

            # When a tree is filled with a dummy variable, it will end up in the underflow, so ignore it
            if ignoreUnderflow:
                root_histogram.SetBinContent(0, 0)
                root_histogram.SetBinError(0, 0)

            gcd()
            nHistograms += 1
            histograms[sample][tree] = root_histogram.Clone()

        root_file.Close()
    return histograms
Ejemplo n.º 12
0
def get_histograms_from_files(histogram_paths=[], files={}, verbose=False):
    histograms = {}
    nHistograms = 0
    for sample, input_file in files.iteritems():
        root_file = File(input_file)
        get_histogram = root_file.Get
        histograms[sample] = {}

        for histogram_path in histogram_paths:
            current_btag, found_btag = find_btag(histogram_path)

            root_histogram = None
            if not found_btag or not current_btag in sumations.b_tag_summations.keys(
            ):
                root_histogram = get_histogram(histogram_path)
                if not is_valid_histogram(root_histogram, histogram_path,
                                          input_file):
                    return
            else:
                listOfExclusiveBins = sumations.b_tag_summations[current_btag]
                exclhists = []

                for excbin in listOfExclusiveBins:
                    hist = get_histogram(
                        histogram_path.replace(current_btag, excbin))
                    if not is_valid_histogram(
                            hist, histogram_path.replace(current_btag, excbin),
                            input_file):
                        return
                    exclhists.append(hist)
                root_histogram = exclhists[0].Clone()

                for hist in exclhists[1:]:
                    root_histogram.Add(hist)

            gcd()
            nHistograms += 1
            histograms[sample][histogram_path] = root_histogram.Clone()
            if verbose and nHistograms % 5000 == 0:
                print 'Read', nHistograms, 'histograms'
        root_file.Close()
    return histograms
Ejemplo n.º 13
0
def main():
    "Main Function"
    set_root_defaults()

    parser = OptionParser(
        "Script to check progress of CRAB jobs in creating nTuples. Run as: python check_CRAB_jobs.py -p projectFolder -n numberOfJobs >&check.log &"
    )
    parser.add_option("-p",
                      "--projectFolder",
                      dest="projectFolder",
                      help="specify project")
    parser.add_option("-n",
                      "--numberOfJobs",
                      dest="numberOfJobs",
                      help="specify project")

    (options, _) = parser.parse_args()

    #make sure the project option has been specified
    if not options.projectFolder:
        parser.error(
            'Please enter a project folder as the -p option: /gpfs_phys/storm/cms/user/...'
        )

    #normalise the projectFolder filepath and add a "/" at the end
    projectFolder = os.path.normpath(options.projectFolder) + os.sep

    #list the items in the CRAB output folder on the Bristol Storage Element.
    storageElementList = glob.glob(projectFolder + "*.root")
    if storageElementList:
        pass
    else:
        print "Location Error: Specified project folder does not exist on the Bristol Storage Element, signifying that the CRAB job has probably not started running yet or you forgot to include the full path /gpfs_storm/cms/user/..."
        sys.exit()

    #The following section has been commented out because if it is the first time this script is being run in a session, a grid password will be needed which will cause the script
    #to not be able to finish. Since the only purpose of this following CRAB command is to obtain the number of jobs, for the time being the number of jobs has been entered as an option to
    #the script which should be manually entered by the user.

    #get the status of the crab jobs and extract the number of output files expected on the Bristol Storage Element.


#	projectFolder = options.projectFolder.split("/")[6]
#	status = commands.getstatusoutput("crab -status -c " + projectFolder)
#	statusFormatted = status[1].split("\n")
#	for line in statusFormatted:
#		if "crab:" in line and "Total Jobs" in line:
#			words = line.split()
#			numberOfJobs = int(words[1])

#Now, check that all job root files are present in Bristol Storage Element folder:

    missingOrBrokenTemp = []
    missingOrBroken = []
    goodFilesTemp = []
    goodFiles = []
    presentJobList = []
    duplicatesToDelete = []

    #make list of all the job numbers which should be present.
    jobList = range(1, int(options.numberOfJobs) + 1)

    #try opening all files in Bristol Storage Element folder and add to missing list if they cannot be opened.
    for f in storageElementList:
        #make list of all jobs numbers in the Bristol Storage Element folder
        jobNumber = int((re.split('[\W+,_]', f))[-4])
        presentJobList.append(jobNumber)

        #check if files are corrupt or not
        try:
            rootFile = File(f)
            rootFile.Close()
        except:
            print "Adding Job Number", jobNumber, "to missingOrBroken list because file is corrupted."
            missingOrBrokenTemp.append(jobNumber)
        else:
            goodFilesTemp.append(jobNumber)

    #now add any absent files to the missing list:
    for job in jobList:
        if job not in presentJobList:
            print "Adding Job Number", job, "to missingOrBroken list because it doesn't exist on the Storage Element."
            missingOrBrokenTemp.append(job)

    #Remove any job numbers from missingOrBroken which appear in both goodFiles and missingOrBroken lists
    for job in missingOrBrokenTemp:
        if job not in goodFilesTemp:
            missingOrBroken.append(job)
        else:
            print "Removing", job, "from missingOrBroken list because there is at least one duplicate good output file."

    #Remove any job numbers from goodFiles which appear more than once in goodFiles
    for job in goodFilesTemp:
        if job not in goodFiles:
            goodFiles.append(job)
        else:
            duplicatesToDelete.append(job)

    print "\n The following", len(
        goodFiles
    ), "good output files were found in the Bristol Storage Element folder:"
    print str(goodFiles).replace(" ", "")
    print "\n The following", len(
        duplicatesToDelete
    ), "job numbers have multiple good files on the Bristol Storage Element folder which can be deleted:"
    print str(duplicatesToDelete).replace(" ", "")
    print "\n The following", len(
        missingOrBroken
    ), "job numbers could not be found in the Bristol Storage Element folder:"
    print str(missingOrBroken).replace(" ", "")
Ejemplo n.º 14
0
            for nbin in range(1, ratio_histograms[idx].GetNbinsX() + 1):
                ratio_histograms[idx].SetBinContent(
                    nbin, ratio_histograms[idx].GetBinContent(nbin) + 80.0)
            ratio_histograms[idx].Draw('e3 same')
            legend = TLegend(0.75, 0.3, 0.85, 0.5)
            legend.SetLineColor(0)
            legend.SetFillStyle(0)
            legend.AddEntry(histograms[0][idx], 'DsK', 'l')
            legend.AddEntry(histograms[1][idx], 'Ds#pi', 'l')
            legend.AddEntry(ratio_histograms[idx], 'ratio', 'l')
            legend.Draw()
            gPad.Update()
        else:
            ratio_histograms[idx].Draw('e1')
        if doPrint and (idx == 0 or idx > 6):
            canvas0.Print('plots/DsK_ratio_' + str(idx) + '_' + str(zoom) +
                          '.png')
            canvas0.Print('plots/DsK_ratio_' + str(idx) + '_' + str(zoom) +
                          '.pdf')
    canvas.Update()
    if not zoom:
        dfile = File('data/raw_ratios.root', 'recreate')
        dfile.cd()
        for hist in ratio_histograms:
            dfile.WriteTObject(hist)
        dfile.Close()

    if doPrint:
        canvas.Print('plots/DsK_ratio_grid_' + str(zoom) + '.png')
        canvas.Print('plots/DsK_ratio_grid_' + str(zoom) + '.pdf')
def sumExclusiveHistogramsInFile(filename):
    testfile = File(filename, 'read')
    newHistograms = {}
    histExists = newHistograms.has_key

    nTimes = 1000000000
    nthTime = 0
    nHistograms = 0
    listOfAllHistograms = []
    addToList = listOfAllHistograms.append
    for folder, emptyThing, histograms in testfile:
        for histogram in histograms:
            nHistograms += 1
            currentPath = folder + '/' + histogram
            addToList(currentPath)
    print 'Loading', nHistograms, 'histograms'
    allHistograms = fileReader.getHistogramsFromFiles(listOfAllHistograms,
                                                      {'Test': filename},
                                                      True)['Test']
    print 'Loaded', nHistograms, 'histograms'

    nHistograms = 0
    for histogramName, histogram in allHistograms.iteritems():
        nthTime += 1
        if nthTime >= nTimes:
            continue
        nHistograms += 1
        isBtagBinnedHist = False
        if nHistograms % 5000 == 0:
            print 'Done', nHistograms, 'histograms'
        currentHist = histogramName

        for btag_bin in btag_bins_available:
            if btag_bin in histogramName:
                isBtagBinnedHist = True
                currentHist = histogramName.replace(btag_bin, '')
        if not isBtagBinnedHist:
            continue

        currentPath = currentHist
        if histExists(currentHist):  #already have it
            continue
        inclBinHistograms = {}
        for exclBin in range(len(btag_bins_inclusive)):
            #        print 'Starting with', currentPath + btag_bins_available[exclBin]
            newhist = allHistograms[currentPath + btag_bins_available[exclBin]]
            for availBin in range(exclBin + 1, len(btag_bins_available)):
                #            print '>>>>> Adding:', currentPath + btag_bins_available[availBin]
                addThis = allHistograms[currentPath +
                                        btag_bins_available[availBin]]
                newhist.Add(addThis)
            inclBinHistograms[currentPath +
                              btag_bins_inclusive[exclBin]] = newhist
        newHistograms[currentPath] = inclBinHistograms
    testfile.Close()
    print 'Done', nHistograms, 'histograms'

    rootFile = TFile.Open(filename, 'UPDATE')
    cd = rootFile.Cd
    rootFile.cd()
    for histFullPath, histogramDict in newHistograms.iteritems():
        currentHist = histFullPath.split('/')[-1]
        path = histFullPath.replace(currentHist, '')
        cd('/' + path)
        for histname, histogram in histogramDict.iteritems():
            currentHist = histname.split('/')[-1]
            histogram.Write(currentHist)
    rootFile.Write()
    rootFile.Close()
    del allHistograms
    del rootFile
    del testfile
    del newHistograms
Ejemplo n.º 16
0
                fullRunLumi.append([int(run), lumi])
    return fullRunLumi


inputFile = File(
    '/hdfs/TopQuarkGroup/run2/ntuples/v22/SingleMuonFullDCS/SingleMuon.root')
treeName = 'nTupleTree/tree'
tree = inputFile.Get(treeName)

jsonForFiltering = '/users/ec6821/lumiScripts/lcr2/lcr2/good_list.txt'
json = readJson(jsonForFiltering)

newFile = File('SingleMuonFiltered.root', 'RECREATE')
newFile.mkdir('nTupleTree')
newFile.cd('nTupleTree')
newTree = tree.CloneTree(0)

print 'Number of events in tree : ', tree.GetEntries()

for event in tree:
    run = event.__getattr__('Event.Run')
    lumi = event.__getattr__('Event.LumiSection')

    if [run, lumi] in json:
        newTree.Fill()
    else:
        pass
newFile.Close()

# print run, lumi
# newFile = r.TFile('filtered.root','write')
@author: kreczko
'''

from rootpy.io import File
from rootpy.plotting import Hist
from rootpy import asrootpy

rootpy_hist = Hist(100, 0, 100, type='F')

rootpy_hist.SetName('hist')
test_file = File('test.root', 'RECREATE')
test_file.mkdir("test")
test_file.cd('test')
rootpy_hist.Write()
test_file.Write()
test_file.Close()

read_file = File('test.root')
folder = read_file.Get('test')
hist = folder.hist
print hist.TYPE
read_file.Close()

hist = None

read_file = File('test.root')
hist = read_file.Get('test/hist')
hist1 = hist.empty_clone(type='D')
print hist1.TYPE
read_file.Close()