示例#1
0
def limit(signal_n, background_n, bkg_uncert):
    signal = TH1F("signal" + str(random.randrange(1e6)), "signal", 1, 0, 1)
    background = TH1F("background" + str(random.randrange(1e6)), "background",
                      1, 0, 1)
    data = TH1F("data" + str(random.randrange(1e6)), "data", 1, 0, 1)
    signal.Sumw2()
    signal.SetBinContent(1, signal_n)
    signal.SetBinError(1, sqrt(signal_n))
    background.Sumw2()
    background.SetBinContent(1, background_n)
    background.SetBinError(1, sqrt(background_n))
    errorsignal = TVectorD(1)
    errorbackground = TVectorD(1)
    errorsignal[0] = 0.20  # hardcoded to 2015 approximate value
    errorbackground[0] = bkg_uncert
    names = TObjArray()
    name1 = TObjString("bkg uncertainty")
    name2 = TObjString("sig uncertainty")
    names.AddLast(name1)
    names.AddLast(name2)
    datasource = TLimitDataSource()
    datasource = TLimitDataSource(signal, background, data, errorsignal,
                                  errorbackground, names)
    confidence = TConfidenceLevel(TLimit.ComputeLimit(datasource, 5000))
    return 1 - confidence.GetExpectedCLs_b()
示例#2
0
 def test_get_matching_cuts_question_mark_only(self):
     """
     Test getMatchingCuts when supplied with a single question mark.
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(cuts, "?")
     self.assertEqual(cuts.GetEntries(), 11)
示例#3
0
 def test_get_matching_cuts_single_name_non_existing(self):
     """
     Test getMatchingCuts when supplied with a single (non-existing) cut name.
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(cuts, "CutNonExisting")
     self.assertEqual(cuts.GetEntries(), 0)
示例#4
0
 def test_get_matching_cuts_two_astersik(self):
     """
     Test getMatchingCuts when supplied with a two asterisks.
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(cuts, "*/*")
     self.assertEqual(cuts.GetEntries(), 11)
示例#5
0
def getCutFlowFromHistogram(inputdir):
    try:
        from ROOT import AnalysisFramework
    except:
        compileMinimal()
        from ROOT import AnalysisFramework
    CutFlowHist = AnalysisFramework.CutFlows.CutFlowHist

    inputpath = listifyInputFiles(inputdir)

    htemp = CutFlowHist("CutFlow", "CutFlow output of AnalysisFramework",
                        400000, 0, 1)
    for d in inputpath:
        f = TFile.Open(d)
        heach = f.Get("CutFlow")
        #for i in range(heach.GetNbinsX()):
        #    if not heach.GetBinLabel(i+1):
        #        break
        #    print i+1, heach.GetBinLabel(i+1)
        col = TObjArray()
        col.Add(heach)
        htemp.Merge(col)
        f.Close()

    #xaxis = htemp.GetXaxis()
    temp = {}
    for i in range(htemp.GetNbinsX()):
        label = htemp.GetBinLabel(i + 1)
        if not label:
            continue
        flownum = int(label.split('/')[0])
        isweighted = label.split('/')[1] == 'W'
        flowname = label.split('/')[2]
        streamlet = label.split('/')[3]
        if isweighted:
            raw = 0.
            weighted = htemp.GetBinContent(i + 1)
        else:
            raw = htemp.GetBinContent(i + 1)
            weighted = 0.
        if not flownum in temp:
            temp[flownum] = (flowname, {})
        flownametemp, numberstemp = temp[flownum]
        if not streamlet in numberstemp:
            numberstemp[streamlet] = (raw, weighted)
        else:
            rawtemp, weightedtemp = numberstemp[streamlet]
            numberstemp[streamlet] = (raw + rawtemp, weighted + weightedtemp)

    cutflow = []
    totalEvents = getTotalEventsHistogram(inputdir)
    if totalEvents:
        cutflow.append(('OriginalTotalEvents', {
            'All': (totalEvents.GetBinContent(1), totalEvents.GetBinContent(2))
        }))
    for i in sorted(temp.keys()):
        cutflow.append(temp[i])
    return cutflow
示例#6
0
 def test_get_matching_cuts_two_question_mark(self):
     """
     Test getMatchingCuts when supplied with two question marks.
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(cuts, "?/?")
     # matches all except CutBase
     self.assertEqual(cuts.GetEntries(), 10)
示例#7
0
 def test_get_matching_cuts_path_with_two_trailing_asterisk(self):
     """
     Test getMatchingCuts when supplied with a cut path with two trailing asterisk.
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(cuts, "CutVbfTopCR/*/*")
     self.assertEqual(cuts.GetEntries(), 1)
     self.assertEqual(cuts.At(0).GetName(), "CutVbfTopCR")
示例#8
0
 def test_get_matching_cuts_path_non_wildcards_non_existing(self):
     """
     Test getMatchingCuts when supplied with a cut path (non-existing).
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(
         cuts, "CutTrigger/CutNonExisting/CutOS/CutBVeto/CutPreselectionSR")
     self.assertEqual(cuts.GetEntries(), 0)
示例#9
0
 def test_get_matching_cuts_single_name(self):
     """
     Test getMatchingCuts when supplied with a single (existing) cut name.
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(cuts, "CutTrigger")
     self.assertEqual(cuts.GetEntries(), 1)
     self.assertEqual(cuts.At(0).GetName(), "CutTrigger")
示例#10
0
 def test_get_matching_cuts_question_mark_asterisk(self):
     """
     Test getMatchingCuts when supplied with a question mark and an
     asterisk.
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(cuts, "?/*")
     self.assertEqual(cuts.GetEntries(), 11)
示例#11
0
 def test_get_matching_cuts_path_with_asterisk_non_existing(self):
     """
     Test getMatchingCuts when supplied with a cut path with an asterisk,
     which does not match any cut.
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(
         cuts, "CutTrigger/CutNonExisting/*/CutBVeto/CutPreselectionSR")
     self.assertEqual(cuts.GetEntries(), 0)
示例#12
0
 def test_get_matching_cuts_path_with_optional_asterisk(self):
     """
     Test getMatchingCuts when supplied with a cut path containing an
     optional asterisk
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(cuts, "CutPreselectionSR/*/CutVbfSR")
     self.assertEqual(cuts.GetEntries(), 1)
     self.assertEqual(cuts.At(0).GetName(), "CutVbfSR")
示例#13
0
 def test_get_matching_cuts_path_non_wildcards(self):
     """
     Test getMatchingCuts when supplied with a cut path.
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(cuts,
                          "CutTrigger/CutOS/CutBVeto/CutPreselectionSR")
     self.assertEqual(cuts.GetEntries(), 1)
     self.assertEqual(cuts.At(0).GetName(), "CutPreselectionSR")
示例#14
0
def makeTObjArray(theList):
    """Turn a python iterable into a ROOT TObjArray"""
    # Make PyROOT give up ownership of the things that are being placed in the
    # TObjArary. They get deleted because of result.SetOwner()
    result = TObjArray()
    result.SetOwner()
    for item in theList:
        SetOwnership(item, False)
        result.Add(item)
    return result
示例#15
0
 def test_get_matching_cuts_path_with_asterisk_inner_name_non_existing(
         self):
     """
     Test getMatchingCuts when supplied with a cut path with an asterisk in
     a cut name, which does not match any cut.
     """
     base = self.get_cut_hierarchy()
     cuts = TObjArray()
     base.getMatchingCuts(
         cuts, "CutTrigger/CutNonExisting/CutOS/CutB*/CutNonExisting")
     self.assertEqual(cuts.GetEntries(), 0)
示例#16
0
def make_reso(file_name, out_tag, show, thr=0.08):
    f = TFile(file_name, "READ")
    h = f.Get("qa-tracking-resolution/impactParameter/impactParameterRPhiVsPt")
    h.SetDirectory(0)
    f.Close()
    h.SetBit(TH1.kNoStats)
    if show:
        c = "2d" + out_tag
        c = TCanvas(c, c)
        canvases.append(c)
        c.SetLogz()
        h.Draw("COLZ")
    o = TObjArray()
    h.GetYaxis().SetRangeUser(-200, 200)
    h.FitSlicesY(0, 0, -1, 0, "QNR", o)
    h.GetYaxis().SetRange()
    hmean = o.At(1)
    hsigma = o.At(2)
    if show:
        hmean = hmean.DrawCopy("SAME")
        hsigma = hsigma.DrawCopy("SAME")
        hsigma.SetLineColor(2)
        c.Update()
    g = TGraph()
    g.SetName(out_tag)
    g.SetTitle(out_tag)
    g.GetXaxis().SetTitle(h.GetXaxis().GetTitle())
    g.GetYaxis().SetTitle(h.GetYaxis().GetTitle())

    for i in range(1, h.GetNbinsX() + 1):
        x = h.GetXaxis().GetBinCenter(i)
        if x < thr:
            continue
        hh = h.ProjectionY(f"{h.GetName()}_{i}", i, i)
        y = hh.GetRMS()
        y = hsigma.GetBinContent(hsigma.GetXaxis().FindBin(x))
        g.SetPoint(g.GetN(), x, y)
    if show:
        can2 = "1d" + out_tag
        can2 = TCanvas(can2, can2)
        canvases.append(can2)
        can2.SetLogy()
        can2.SetLogx()
        g.SetMarkerStyle(8)
        g.Draw()
        can2.Update()
    print(g.Eval(0.1))
    # input("press enter to continue")
    g.SaveAs(f"{out_tag}.root")
    return g
示例#17
0
    def test4TObjArray(self):
        """Test TObjArray iterator-based copying"""

        a = TObjArray()
        b = list(a)

        self.assertEqual(b, [])
示例#18
0
    def test11WriteTObjArray(self):
        """Test writing of a TObjArray"""

        f = TFile(self.fname, 'RECREATE')
        t = TTree(self.tname, self.ttitle)
        o = TObjArray()
        t.Branch('mydata', o)

        nameds = [TNamed(name, name) for name in self.testnames]
        for name in nameds:
            o.Add(name)
        self.assertEqual(len(o), len(self.testnames))

        t.Fill()

        f.Write()
        f.Close()
示例#19
0
def getCurve(h):

    # Temporary draw
    c = TCanvas()
    c.cd()
    h.Smooth()
    h.SetContour(2)
    #h.Draw("COLZ")
    h.Draw("CONT Z LIST")
    c.Update()

    # Get contours
    curves = []
    conts = TObjArray(gROOT.GetListOfSpecials().FindObject("contours"))
    gs = TGraphSmooth("normal")
    gin = TGraph(conts.At(0).At(0))
    gout = gs.SmoothSuper(gin, "", 3)

    x_m = array('d', [])
    x_p = array('d', [])
    y_m = array('d', [])
    y_p = array('d', [])

    for p in xrange(0, gout.GetN()):
        gr_x = ROOT.Double(0.)
        gr_y = ROOT.Double(0.)
        gout.GetPoint(p, gr_x, gr_y)
        x_m.append(-gr_x)
        y_m.append(-gr_y)
        x_p.append(gr_x)
        y_p.append(gr_y)


#        if opt == 'w':
#            x_p[0] = 0.
#            x_m[0] = 0.

    curves.append(TGraph(len(x_p), x_p, y_p))
    curves.append(TGraph(len(x_m), x_m, y_m))
    curves.append(TGraph(len(x_p), x_p, y_m))
    curves.append(TGraph(len(x_m), x_m, y_p))

    c.Close()

    return curves
def get_bin_resolution(fine_binned_response, bin_number):
    '''
    Return the resolution for the reco and gen distributions for each variable in each fine bin
    '''
    gen_resolution = 0
    reco_resolution = 0

    reco_array = TObjArray()
    gen_array = TObjArray()

    # produces 4 TH1D histograms
    # 0 : constant
    # 1 : mean
    # 2 : sigma
    # 3 : chi-squared of it
    gen_slices = fine_binned_response.FitSlicesY(0, bin_number, bin_number, 1,
                                                 "Q", gen_array)
    # for hist in gen_array:
    #     print (type(hist))
    # print("Gen Sigma : ", gen_array[2].GetBinContent(bin_number))
    # print("Gen Mean : ", gen_array[1].GetBinContent(bin_number))
    # print("Gen Constant : ", gen_array[0].GetBinContent(bin_number))
    gen_resolution = gen_array[2].GetBinContent(bin_number)

    reco_slices = fine_binned_response.FitSlicesX(0, bin_number, bin_number, 1,
                                                  "Q", reco_array)
    # for hist in reco_array:
    #     print (type(hist))
    # print("Reco Sigma : ", reco_array[2].GetBinContent(bin_number))
    # print("Reco Mean : ", reco_array[1].GetBinContent(bin_number))
    # print("Reco Constant : ", reco_array[0].GetBinContent(bin_number))
    reco_resolution = reco_array[2].GetBinContent(bin_number)

    # resolution = max(gen_resolution, reco_resolution)
    # return resolution
    return reco_resolution, gen_resolution
def recursiveMerge(target,
                   infile,
                   path='',
                   cache={'TOTALLUMI': 0},
                   cutflow=True):
    l = infile.GetDirectory(path)
    keys = l.GetListOfKeys()
    cycles = {}
    for entry in range(keys.GetEntries()):
        name = keys.At(entry).GetName() + ";" + str(keys.At(entry).GetCycle())
        if path:
            cachename = path + "/" + name
        else:
            cachename = name
        obj = l.Get(name)
        if type(obj) == TDirectoryFile:
            #print obj, "DIRECTORY"
            targetpath = keys.At(entry).GetName()
            if not target.Get(targetpath):
                target.mkdir(targetpath)
            recursiveMerge(target, infile, path + "/" + obj.GetName(), cache)
        elif type(obj) == TTree:
            #                print obj, cachename, "TTree"
            cyclename, cyclenumber = cachename.split(';')
            if cyclename in cycles: continue
            #                print cachename, "Used!"
            cycles[cyclename] = cyclenumber
            if not cyclename in cache:
                target.cd(path)
                cache[cyclename] = obj.CloneTree()
            else:
                objcached = cache[cyclename]
                col = TObjArray()
                col.Add(obj)
                objcached.Merge(col)
        elif issubclass(obj.__class__, TH1):
            #print obj, "TH1"
            if not cutflow and keys.At(entry).GetName() == "CutFlow":
                continue
            if not cachename in cache:
                target.cd(path)
                cache[cachename] = obj.Clone()
            else:
                objcached = cache[cachename]
                col = TObjArray()
                col.Add(obj)
                objcached.Merge(col)
        elif type(obj) == TObjString:
            #print type(obj), name, "TObjString"
            if obj:
                target.cd(path)
                objnew = TObjString(obj.GetString().Data())
                objnew.Write(keys.At(entry).GetName())
                cache['TOTALLUMI'] += 1
        else:
            print "UNKNOWN OBJECT", name, "OF TYPE", type(obj)
示例#22
0
    def test_get_matching_cuts_path_with_asterisk_inner_name(self):
        """
        Test getMatchingCuts when supplied with a cut path with an asterisk in
        a cut name.
        """
        # mid
        base = self.get_cut_hierarchy()
        cuts = TObjArray()
        base.getMatchingCuts(cuts, "CutTrigger/CutOS/CutB*/CutPreselection*R")
        self.assertEqual(cuts.GetEntries(), 2)
        self.assertEqual(cuts.At(0).GetName(), "CutPreselectionSR")
        self.assertEqual(cuts.At(1).GetName(), "CutPreselectionTopCR")

        # end
        base = self.get_cut_hierarchy()
        cuts = TObjArray()
        base.getMatchingCuts(cuts, "CutTrigger/CutOS/CutB*")
        self.assertEqual(cuts.GetEntries(), 2)
        self.assertEqual(cuts.At(0).GetName(), "CutBVeto")
        self.assertEqual(cuts.At(1).GetName(), "CutBReq")
示例#23
0
def addBranch(self, name, dtype='f', default=None, standard=True):
    """Add branch with a given name, and create an array of the same name as address."""
    if hasattr(self, name):
        print "ERROR! TreeProducerCommon.addBranch: Branch of name '%s' already exists!" % (
            name)
        exit(1)
    if isinstance(dtype, str):
        if dtype.lower(
        ) == 'f':  # 'f' is only a 'float32', and 'F' is a 'complex64', which do not work for filling float branches
            dtype = float  # float is a 'float64' ('f8')
        elif dtype.lower() == 'i':  # 'i' is only a 'int32'
            dtype = int  # int is a 'int64' ('i8')
    if standard:
        setattr(self, name, num.zeros(1, dtype=dtype))
        self.Branch(name, getattr(self, name),
                    '%s/%s' % (name, root_dtype[dtype]))
        if default != None:
            getattr(self, name)[0] = default
    else:
        l = array('f', [0.])
        setattr(self, name, TObjArray(l))
        # self.Branch(name, getattr(self, name))
        print type(getattr(self, name))
        self.Branch(getattr(self, name))
示例#24
0
    setColor(h, c, m)
for h, c, m in fitChColorMap:
    setColor(h, c, m)

# ----------------------------------------------
# Plot MP of charge vs intergrated Lumi.
# https://www.slac.stanford.edu/BFROOT/www/doc/tutorials/PhysicsWeekApril2000/RootTutorial-Histograms-intro.html
# ----------------------------------------------
h_sliceY = []
c_ch_d_MP_intL = TCanvas('c_ch_d_MP_intL', 'c_ch_d_MP_intL', 700, 800)
c_ch_d_MP_intL.SetGrid()

for h in range(0, 3):  #len(h_2D_ch)
    n = h_2D_ch[h].GetName()
    if 'ch' in n:
        aSlices = TObjArray()
        h_fit[h].SetRange(10, 60)
        fitS = h_2D_ch[h].FitSlicesY(h_fit[h], 0, -1, 0, "QNR", aSlices)
        #fitS = h_2D_ch[h].FitSlicesY(h_fit[h], 7, 100, 0, "QNR", aSlices)
        aSlices[1].GetYaxis().SetRangeUser(12., 22.)
        aSlices[1].SetAxisRange(0., 70., "X")
        h_sliceY.append(aSlices[1])

hSliceYdColorMap = zip(h_sliceY[0:3], lsColor[0:3], lsMarker[0:3])
ymax = 0
ymin = 9999
for h, c, m in hSliceYdColorMap:
    ymax = h.GetMaximum()
    ymin = h.GetMinimum()
    setColor(h, c, m)
    if lumiType == 'int':
示例#25
0
from cpyroot import *
import sys

particles = sys.argv[1]
f = TFile('./rootfiles/sample_{ptcs}.root'.format(ptcs=particles))
tree = f.Get('jets')
rec = TCut('jet1_rec_e>0 && jet1_e>0')
a = TCanvas()
if particles == 'h0':
    tree.Draw('jet1_rec_e/jet1_e:jet1_e>>h(200,0.5,1000.,100,0.,3.5)', rec)
elif particles == 'photon':
    tree.Draw('jet1_rec_e/jet1_e:jet1_e>>h(200,0.5,1000.,1000,0.,1.5)', rec)
elif particles == 'photon_low_e':
    tree.Draw('jet1_rec_e/jet1_e:jet1_e>>h(200,0.5,100.,500,0.,1.5)', rec)
h = gDirectory.Get('h')
arr = TObjArray()
gaus = TF1('gaus', 'gaus')
h.FitSlicesY(gaus)
mean = gDirectory.Get('h_1')
b = TCanvas()
mean.Draw()
sigma = gDirectory.Get('h_2')
c = TCanvas()
sigma.Draw()
if particles == 'photon':
    Ecal_res = TF1('Ecal_res',
                   'sqrt((([0]/(sqrt(x)))**2)+(([1]/x)**2)+([2]**2))', 0.5,
                   1000)
    sigma.Fit('Ecal_res', "", "", 0.5, 1000)
elif particles == 'photon_low_e':
    Ecal_res = TF1('Ecal_res',
示例#26
0
    f1.cd("Higgs")
    f1.ls()

    hDen = gDirectory.Get(Deno)
    hNum = gDirectory.Get(Numer)

    i1 = 0
    i2 = 800
    hNum.GetXaxis().SetRangeUser(i1, i2)
    hDen.GetXaxis().SetRangeUser(i1, i2)
    hDen.SetLineColor(ROOT.kRed)

    hRat = hNum.Clone()
    hRat.SetName("Ratio")
    hRat.Divide(hNum, hDen, 1., 1., "B")
    Hlist = TObjArray()
    Hlist.Add(hRat)

    cname = "pT"
    c1 = prepPlot("c1", cname, 700, 20, 500, 500)
    c1.SetLogy(1)

    hDen.Draw()
    hNum.Draw("same")

    cname = "Ratio"
    c2 = prepPlot("c2", cname, 150, 120, 500, 500)
    c2.SetLogy(0)

    min = 0.65
    max = 1.04
                "muEffWeightDown",
                "muEffWeightUp",
                "puWeightDown",
                "puWeightUp",
]

results = {}

## Get data from the input root file
data = _file.Get(?)

## Loop over the list of systematics
for syst in systematics:
    
    ## Add histograms from Isolated and NonPrompt categories to the array 'mc'
    mc = TObjArray(2)
    mc.Add(_file.Get(?))
   
     ## Fit the MC histograms to data 
    fit = TFractionFitter(?)
    
    ## fit.Fit() actually performs the fit
    ## check the fit status
    status = ?
    
    ## status==0 corresponds to fits that converged, and we can then obtain the fit result 
    fitResults = ?
    
    ## Calculating the scale factor for isolated photons 
    isolatedSF  = data.Integral()*fitResults[0]/mc[0].Integral()
    
示例#28
0
class Angel:
    fname = None
    title = None
    subtitles = []
    xtitle = None
    ytitle = None
    ztitle = None
    mesh = None
    axis = []  # there might be several axes -> list
    reg = []
    output = None
    output_title = None  # commented part of output line - for z-title
    unit = None
    unit_title = None  # commented part of the unit line - for z-title
    part = []  # list of particles
    lines = []
    return_value = 0

    # this group of variables is used to convert a set of 1D histograms to 2D (if necessary):
    dict_nbins = {
    }  # dictionary of number of bins - to guess if 2D histo is needed
    last_nbins_read = None  # last name of binning read (ne, nt, na, ...)
    dict_edges_array = {}  # dictionary of arrays with bin edges

    histos = TObjArray()
    ihist = 0  # histogram number - must start from ZERO
    fname_out = None

    def __init__(self, fname_in, fname_out, **kwargs):
        #        global DEBUG
        #: a python list which contains the numbers of lines which separate
        #: pages
        pageSepLineLST = []
        #: a list of tuples; each tuple contains the contents of a page.
        pageLST = []
        #: the number of ``newpage:``.
        numNewpages = 0

        self.fname = fname_in
        file = open(self.fname)
        self.lines = tuple(file.readlines())
        file.close()

        self.fname_out = fname_out
        self.avBitSet = kwargs["avBitSet"]

        ipage = -1

        ##################################################
        # scan whole the file and count the "^[\s#]newpage:$",
        ##################################################
        for idx, line in enumerate(self.lines):
            modLine = line.rstrip()  # strip spaces/newlines at the right
            if pageSepRE.search(modLine):
                numNewpages += 1
                pageSepLineLST.append(idx)
        if DEBUG: print("pageSepLineLST: ", pageSepLineLST)

        ##################################################
        # Separate each page into a tuple and
        # store them into a list named 'pageLST'
        ##################################################
        #   the 1st page
        pageLST.append(tuple(self.lines[:pageSepLineLST[0]]))
        #   intermediate
        for pageIdx in range(1, numNewpages):
            pageLST.append(self.lines[pageSepLineLST[pageIdx - 1] +
                                      1:pageSepLineLST[pageIdx]])
        #   the last page
        pageLST.append(tuple(self.lines[pageSepLineLST[-1] + 1:]))

        ##################################################
        # scan the first page and extract header information
        ##################################################
        for iline, line in enumerate(pageLST[0]):
            line.strip()
            if re.search("title = ", line):
                words = line.split()
                self.title = ' '.join(words[2:])
                continue
            if re.search("mesh = ", line):
                words = line.split()
                self.mesh = words[2]
                continue
            if re.search("axis = ", line):
                for a in line.split()[2:]:
                    if a == '#': break
                    self.axis.append(a)
                if DEBUG: print("axis: ", self.axis)
                continue
            if re.search("reg = ", line):
                for r in line.split()[2:]:
                    if r == '#': break
                    self.reg.append(r)
                if DEBUG: print("reg: ", self.reg)
                continue
            if re.search(
                    "^n[eartxyz] = ", line.strip()
            ):  # !!! make sence if we specify number of bins but not the bin width
                words = line.split()
                self.dict_nbins[words[0]] = int(words[2])
                self.last_nbins_read = words[0]
                if DEBUG: print("dict_nbins:", self.dict_nbins)
                continue
            if re.search("#    data = ", line):
                self.dict_edges_array[self.last_nbins_read] = self.GetBinEdges(
                    iline)
                continue
            if re.search("part = ", line):
                words = line.split()
                # this loop is needed in case we define particles in separate lines as shown on page 121 of the Manual. Otherwise we could have used 'self.part = words[2:]'
                for w in words[2:]:
                    self.part.append(w)
                if DEBUG: print("particles:", self.part)
                continue
            if re.search("output = ", line):
                words = line.split()
                self.output = words[2]
                self.output_title = ' '.join(words[4:])
                if self.unit_title != None:
                    self.ztitle = self.output_title + " " + self.unit_title
                continue
            if re.search("unit = ", line):
                words = line.split()
                self.unit = words[2]
                self.unit_title = ' '.join(words[6:])
                if self.output_title != None:
                    self.ztitle = self.output_title + " " + self.unit_title
                continue

        ##################################################
        # scan the remaining data pages one by one
        # book and fill histograms
        ##################################################
        for npage in range(1, len(pageLST)):
            # first scan: extract header info in advance to the data extraction
            for iline, line in enumerate(pageLST[npage]):
                line.strip()
                if re.search("^x:", line):
                    words = line.split()
                    self.xtitle = ' '.join(words[1:])
                    if DEBUG: print("xtitle:", self.xtitle)
                    continue
                elif re.search("^y:", line):
                    words = line.split()
                    self.ytitle = ' '.join(words[1:])
                    if DEBUG: print("ytitle:", self.ytitle)
                    continue
                elif re.search("^z:", line):
                    if not re.search("xorg", line):
                        print(line)
                        print("new graph - not yet implemented")
                    continue
                elif re.search("'no. =", line):  # subtitles of 2D histogram
                    self.subtitles.append(' '.join(
                        line[line.find(',') + 1:].split()).replace("\'",
                                                                   '').strip())
                    if DEBUG: print("subtitle:", self.subtitles)

            # second scan: extract data.
            #              scan only within the current page, pass the corresponding
            #              global line number for data decoding
            for iline, line in enumerate(pageLST[npage]):
                line.strip()
                #: 'global' line number (not in the current page).
                #: The counting of local line number (iline) start just after
                #: the location of "^[\s#]newpage:$" and therefore '+1'
                igline = iline + pageSepLineLST[npage - 1] + 1
                if re.search("^h", line):
                    if re.search(
                            "^h: n", line
                    ):  # !!! We are looking for 'h: n' instead of 'h' due to rz-plots.
                        if DEBUG: print("one dimentional graph section")
                        self.Read1DHist(igline)
                        continue
                    elif re.search("h:              x", line):
                        self.Read1DGraphErrors(igline)
                        continue
                    elif re.search("^h[2dc]:", line):
                        if DEBUG:
                            if re.search("^h2", line):
                                print(
                                    "h2: two dimentional contour plot section")
                            if re.search("^hd", line):
                                print(
                                    "hd: two dimentional cluster plot section")
                            if re.search("^hc", line):
                                print(
                                    "hc: two dimentional colour cluster plot section"
                                )
                        self.Read2DHist(igline)
                        continue
                    elif 'reg' in self.axis:  # line starts with 'h' and axis is 'reg' => 1D histo in region mesh. For instance, this is whe case with [t-deposit] tally and mesh = reg.
                        self.Read1DHist(igline)
                        continue

#        print(self.dict_edges_array)
        if self.is1D():
            if DEBUG: print("1D")
        else:
            if DEBUG: print("2D")
#            self.Make2Dfrom1D()

        if self.histos.GetEntries():
            fout = TFile(self.fname_out, "recreate")
            self.histos.Write()
            fout.Close()
            self.return_value = 0
        else:
            print("Have not found any histograms/graphs in this file")
            self.return_value = 1

    def is1D(self):
        """
        Trying to guess if we have many 1D histograms which actually form a 2D one.
        """
        nn1 = 0  # number of cases when number of bins is not 1
        for key in self.dict_nbins:
            if int(self.dict_nbins[key]) > 1: nn1 += 1
        if DEBUG: print("nn1:", nn1)

        if nn1 <= 1:
            return True
        else:
            return False

    # def GetBinEdgesOrig(self, iline):
    #     print("iline:", iline)
    #     edges = []
    #     for line in self.lines[iline+1:]:
    #         print("line: ", line)
    #         if line[0] == '#':
    #             words =  line[1:].split()
    #             print(words)
    #             for w in words:
    #                 edges.append(w)
    #         else: break
    #     if len(edges)-1 != self.dict_nbins[self.last_nbins_read]:
    #         print("ERROR in GetBinEdges: wrong edge or bin number:", len(edges)-1, self.dict_nbins[self.last_nbins_read])
    #         sys.exit(1)
    #    # print('edges:', edges)
    #     return tuple(edges)

    def GetBinEdges(self, iline):
        edges = []
        for line in self.lines[iline + 1:]:
            words = line.split()
            if line[0] == '#':  # if the distribution type is 1 or 2 then '#' is used
                if DEBUG: print(words[1:])
                for w in words[1:]:
                    edges.append(w)
            elif is_float(words[0]):
                for w in words:  # if the distribution type is 3 then there is no "#"
                    edges.append(w)
            else:
                break
        if len(edges) - 1 != self.dict_nbins[self.last_nbins_read]:
            print("ERROR in GetBinEdges: wrong edge or bin number:",
                  len(edges) - 1, self.dict_nbins[self.last_nbins_read])
            sys.exit(1)

    # print('edges:', edges)
        return tuple(edges)

    def GetNhist(self, line):
        """
        Analyzes the section header and return the number of histograms in the section data
        (but not in the entire file!)
        """
        # Let's remove all spaces between ')'. For some reason line.replace('\s*)', ')') does not work
        # so we do it in this weird way:
        line1 = None
        while line1 != line:
            line1 = line
            line = line.replace(' )', ')')

        words = line.split()
        nhists = 0
        for w in words:
            if re.search("^y", w):
                nhists += 1
                mo = SUBT.search(w)
                if mo:
                    self.subtitles.append(mo.group('subtitle'))
                else:
                    self.subtitles.append('')
###        if re.search("^n", words[1]) and re.search("^x", words[2]) and re.search("^y", words[3]) and re.search("^n", words[4]):
        if DEBUG: print("Section Header: 1D histo", nhists, self.subtitles)
        return nhists

    def Read1DHist(self, iline):
        """
        Read 1D histogram section
        """
        nhist = self.GetNhist(
            self.lines[iline]
        )  # number of histograms to read in the current section
        if DEBUG: print('nhist: ', nhist)
        isCharge = False
        if re.search("x-0.5", self.lines[iline].split()[1]):
            isCharge = True  # the charge-mass-chart distribution, x-axis is defined by the 1st column only
            if DEBUG: print("isCharge = True")
        xarray = []
        xmax = None
        data = {}  # dictionary for all histograms in the current section
        errors = {}  # dictionary for all histograms in the current section
        bin_labels = []  # relevant for self.axis == 'reg' only

        for ihist in range(
                nhist):  # create the empty lists, so we could append later
            data[ihist] = []
            errors[ihist] = []

        for line in self.lines[iline + 1:]:
            line = line.strip()
            if line == '': break
            elif re.search("^#", line): continue
            words = line.split()
            #            if DEBUG: print(words)
            if isCharge:
                xarray.append(float(words[0]) - 0.5)
                xmax = float(words[0]) + 0.5
                data[0].append(float(words[1]))
                errors[0].append(float(words[2]))
            elif 'reg' in self.axis:
                xarray.append(float(words[0]) - 0.5)
                xmax = float(words[0]) + 0.5
                bin_labels.append(words[1])  # region number
                for ihist in range(nhist):
                    data[ihist].append(float(words[(ihist + 1) * 2 + 1]))
                    errors[ihist].append(float(words[(ihist + 1) * 2 + 2]))
            else:
                xarray.append(float(words[0]))
                xmax = float(words[1])
                for ihist in range(nhist):
                    data[ihist].append(float(words[(ihist + 1) * 2]))
                    errors[ihist].append(float(words[(ihist + 1) * 2 + 1]))

        nbins = len(xarray)
        xarray.append(xmax)

        #        if DEBUG: print("data: ", data)

        for ihist in range(nhist):
            if self.subtitles[ihist]: subtitle = ' - ' + self.subtitles[ihist]
            else: subtitle = ''
            self.FixTitles()
            # self.ihist+1 - start from ONE as in Angel - easy to compare
            h = TH1F(
                "h%d" % (self.ihist + 1), "%s%s;%s;%s" %
                (self.title, subtitle, self.xtitle, self.ytitle), nbins,
                array('f', xarray))
            if self.avBitSet:
                h.SetBit(TH1F.kIsAverage)
            self.ihist += 1
            for i in range(nbins):
                val = data[ihist][i]
                err = errors[ihist][i] * val
                h.SetBinContent(i + 1, val)
                h.SetBinError(i + 1, err)

            if 'reg' in self.axis:
                for i in range(nbins):
                    h.GetXaxis().SetBinLabel(i + 1, bin_labels[i])
                h.GetXaxis().SetTitle("Region number")

            self.histos.Add(h)
        del self.subtitles[:]

    def Read1DGraphErrors(self, iline):
        """
        Read 1D graph section
        """
        ngraphs = self.GetNhist(
            self.lines[iline])  # graph and hist format is the same
        xarray = []
        data = {}
        errors = {}

        for igraph in range(ngraphs):
            data[igraph] = []
            errors[igraph] = []

        for line in self.lines[iline + 1:]:
            line = line.strip()
            if line == '': break
            elif re.search("^#", line): continue
            words = line.split()
            xarray.append(float(words[0]))
            for igraph in range(ngraphs):
                data[igraph].append(float(words[(igraph + 1) * 2 - 1]))
                errors[igraph].append(float(words[(igraph + 1) * 2]))

        npoints = len(xarray)

        for igraph in range(ngraphs):
            if self.subtitles[igraph]:
                subtitle = ' - ' + self.subtitles[igraph]
            else:
                subtitle = ''
            self.FixTitles()
            g = TGraphErrors(npoints)
            # self.ihist+1 - start from ONE as in Angel - easy to compare
            g.SetNameTitle(
                "g%d" % (self.ihist + 1), "%s%s;%s;%s" %
                (self.title, subtitle, self.xtitle, self.ytitle))
            self.ihist += 1
            for i in range(npoints):
                x = xarray[i]
                y = data[igraph][i]
                ey = errors[igraph][i]
                g.SetPoint(i, x, y)
                g.SetPointError(i, 0, ey * y)

            self.histos.Add(g)
        del self.subtitles[:]

    def FixTitles(self):
        """
        Makes some ROOT fixes

        """
        self.ytitle = self.ytitle.replace("cm^2", "cm^{2}")
        self.ytitle = self.ytitle.replace("cm^3", "cm^{3}")
        self.title = self.title.replace("cm^2", "cm^{2}")
        self.title = self.title.replace("cm^3", "cm^{3}")

    def Read2DHist(self, iline):
        """
        Read 2D histogram section
        """

        line = self.lines[iline].replace(
            " =", "=")  # sometimes Angel writes 'y=' and sometimes 'y ='
        words = line.split()
        if len(words) != 15:
            print(words)
            print(len(words))
            sys.exit("Read2DHist: format error")
#        print(words)

        dy = float(words[6])
        ymin = float(words[2])
        ymax = float(words[4])
        if dy > 0:
            if ymin < ymax:
                ymin, ymax = ymin - dy / 2.0, ymax + dy / 2.0
            else:
                ymin, ymax = ymax - dy / 2.0, ymin + dy / 2.0
        elif dy < 0:
            if ymin < ymax:
                sys.exit("Fix me: ymin<ymax when dy<0")
#                ymin,ymax = ymin-dy/2.0,ymax+dy/2.0
            else:
                ymin, ymax = ymax + dy / 2.0, ymin - dy / 2.0
        ny = abs(int(round((ymax - ymin) / dy)))
        if DEBUG: print("y:", dy, ymin, ymax, ny)

        dx = float(words[13])
        xmin = float(words[9])
        xmax = float(words[11])
        if xmin < xmax:
            xmin, xmax = xmin - dx / 2.0, xmax + dx / 2.0
        else:
            xmin, xmax = xmax - dx / 2.0, xmin + dx / 2.0
        nx = int(round((xmax - xmin) / dx))
        if DEBUG: print("x:", dx, xmin, xmax, nx)

        data = []
        for line in self.lines[iline + 1:]:
            line = line.strip()
            if line == '': break
            elif re.search("^#", line): continue
            words = line.split()
            #            if DEBUG: print("words: ", words)
            for w in words:
                if w == 'z:':
                    #                    if DEBUG: print("this is a color palette -> exit")
                    return  # this was a color palette
                data.append(float(w))
#        if DEBUG: print(data)

# self.ihist+1 - start from ONE as in Angel - easy to compare
        h = TH2F(
            "h%d" % (self.ihist + 1),
            "%s - %s;%s;%s;%s" % (self.title, self.subtitles[self.ihist],
                                  self.xtitle, self.ytitle, self.ztitle), nx,
            xmin, xmax, ny, ymin, ymax)
        self.ihist += 1

        for y in range(ny - 1, -1, -1):
            for x in range(nx):
                d = data[x + (ny - 1 - y) * nx]
                h.SetBinContent(x + 1, y + 1, d)
        self.histos.Add(h)

    def isSameXaxis(self):
        """
        Return True if all the histograms in self.histos have the same x-axis
        """
        nhist = self.histos.GetEntries()
        nbins0 = self.histos[0].GetNbinsX()
        for i in range(1, nhist):
            h = self.histos[i]
            if nbins0 != self.histos[i].GetNbinsX():
                print("not the same bin number", i)
                return false
            for bin in range(nbins0):
                if self.histos[0].GetBinLowEdge(
                        i + 1) != self.histos[i].GetBinLowEdge(i + 1):
                    print("Low edge differ for bin %d of histo %d" % (bin, i))
                    return False
        return True

    def getXarray(self, h):
        """
        Return the tuple with x-low-edges of TH1 'h'
        """
        nbins = h.GetNbinsX()
        xarray = []
        for i in range(nbins + 1):
            xarray.append(float(h.GetBinLowEdge(i + 1)))

        return xarray

    def Make2Dfrom1D(self):
        """
        Makes a 2D histogram from a set of 1D !!! works only with 1 set of particles requested !!!
        """
        # check if all histograms have the same x-range:
        if not self.isSameXaxis():
            print("ERROR in Make2Dfrom1D: x-axes are different")
            sys.exit(1)

        # guess which dict_edges_array correspond to 1D histos
        nbins0 = self.histos[0].GetNbinsX()
        second_dimention = None
        second_dimention_nbins = None
        for key in self.dict_edges_array:
            nbins = len(self.dict_edges_array[key]) - 1
            if nbins == 1: continue  # we do not care
            if nbins0 != nbins:
                second_dimention = key
                second_dimention_nbins = nbins

        if second_dimention:
            if DEBUG:
                print("the second dimention is", second_dimention,
                      second_dimention_nbins)
        else:
            if DEBUG:
                print(
                    "Second dimention was not found based on the number of bins -> bin edges comparing needed"
                )
            sys.exit(3)


#        h2 = TH2F("hall%s" % second_dimention, "", nbins0, 0, 1, 20, 0, 1)

#        if DEBUG: print(array('f', self.getXarray(self.histos[0])))
        second_dimention_xarray = []
        for w in self.dict_edges_array[second_dimention]:
            second_dimention_xarray.append(float(w))
        #        for w in self.dict_edges_array[second_dimention]: if DEBUG: print(float(w))
        #        array('f', second_dimention_xarray)

        h2 = TH2F(
            "hall%s" % second_dimention, "%s;%s;%s;%s" %
            (self.histos[0].GetYaxis().GetTitle(),
             self.histos[0].GetXaxis().GetTitle(), "Time [nsec]",
             self.histos[0].GetYaxis().GetTitle()), nbins0,
            array('f', self.getXarray(self.histos[0])), second_dimention_nbins,
            array('f', second_dimention_xarray))

        nhist = self.histos.GetEntries()  # number of 1D histograms
        for biny in range(nhist):
            h1 = self.histos[biny]
            for binx in range(nbins0):
                h2.SetBinContent(binx + 1, biny + 1,
                                 h1.GetBinContent(binx + 1))
                h2.SetBinError(binx + 1, biny + 1, h1.GetBinError(binx + 1))

        self.histos.Add(h2)
示例#29
0
def recursiveMerge(target,
                   infile,
                   path='',
                   cache={'TOTALLUMI': 0},
                   cutflow=True):
    l = infile.GetDirectory(path)
    keys = l.GetListOfKeys()
    cycles = {}

    #print("keys in input file: \n\n{0}\n\n".format(keys.ls()))

    for entry in range(keys.GetEntries()):
        name = keys.At(entry).GetName() + ";" + str(keys.At(entry).GetCycle())
        if path:
            cachename = path + "/" + name
        else:
            cachename = name
        obj = l.Get(name)

        if type(obj) == TDirectoryFile:
            #print("TDirectory obj name: {0}".format(obj.GetName()))
            targetpath = keys.At(entry).GetName()
            if not target.Get(targetpath):
                target.mkdir(targetpath)
            recursiveMerge(target, infile, path + "/" + obj.GetName(), cache)
        elif type(obj) == TTree:
            #print("TTree obj name: {0} - cachename: {1} ".format(obj.GetName(), cachename))
            cyclename, cyclenumber = cachename.split(';')
            if cyclename in cycles: continue
            #print("cyclename: {0} - cyclenumber: {1}".format(cyclename, cyclenumber))
            cycles[cyclename] = cyclenumber
            if not cyclename in cache:
                #print("adding cyclename {0} to cache (via TTree::CloneTree())".format(cyclename))
                target.cd(path)
                cache[cyclename] = obj.CloneTree()
            else:
                objcached = cache[cyclename]
                col = TObjArray()
                col.Add(obj)
                #print("merging TTree obj to cached object")
                objcached.Merge(col)
        elif issubclass(obj.__class__, TH1):
            #print("TH1 obj name: {0}".format(obj.GetName()))
            if not cutflow and keys.At(entry).GetName() == "CutFlow":
                continue
            if not cachename in cache:
                target.cd(path)
                cache[cachename] = obj.Clone()
            else:
                objcached = cache[cachename]
                col = TObjArray()
                col.Add(obj)
                objcached.Merge(col)
        elif type(obj) == TObjString:
            #print("TObjString obj name: {0}".format(obj.GetName()))
            if obj:
                target.cd(path)
                objnew = TObjString(obj.GetString().Data())
                objnew.Write(keys.At(entry).GetName())
                cache['TOTALLUMI'] += 1
        elif issubclass(obj.__class__, TList):
            #print("TList obj name: {0}".format(obj.GetName()))
            if obj:
                target.cd(path)
                objnew = TList(obj)
                objnew.Write(keys.At(entry).GetName())  # not working...
        else:
            print "UNKNOWN OBJECT", name, "OF TYPE", type(obj)
示例#30
0
    def test_get_matching_cuts_path_non_base(self):
        """
        Test getMatchingCuts when supplied with a cut path not starting at the
        base cut.
        """
        base = self.get_cut_hierarchy()
        cuts = TObjArray()
        base.getMatchingCuts(cuts, "CutPreselection*/*SR")
        self.assertEqual(cuts.GetEntries(), 2)
        self.assertEqual(cuts.At(0).GetName(), "CutVbfSR")
        self.assertEqual(cuts.At(1).GetName(), "CutBoostedSR")

        base = self.get_cut_hierarchy()
        cuts = TObjArray()
        base.getMatchingCuts(cuts, "CutPreselectionSR/CutVbf?R")
        self.assertEqual(cuts.GetEntries(), 1)
        self.assertEqual(cuts.At(0).GetName(), "CutVbfSR")

        base = self.get_cut_hierarchy()
        cuts = TObjArray()
        base.getMatchingCuts(cuts, "CutOS/?")
        self.assertEqual(cuts.GetEntries(), 2)
        self.assertEqual(cuts.At(0).GetName(), "CutBVeto")
        self.assertEqual(cuts.At(1).GetName(), "CutBReq")

        base = self.get_cut_hierarchy()
        cuts = TObjArray()
        base.getMatchingCuts(cuts, "CutOS/*")
        self.assertEqual(cuts.GetEntries(), 9)
        self.assertEqual(cuts.At(0).GetName(), "CutOS")
        self.assertEqual(cuts.At(1).GetName(), "CutBVeto")
        self.assertEqual(cuts.At(2).GetName(), "CutPreselectionSR")
        self.assertEqual(cuts.At(3).GetName(), "CutVbfSR")
        self.assertEqual(cuts.At(4).GetName(), "CutBoostedSR")
        self.assertEqual(cuts.At(5).GetName(), "CutBReq")
        self.assertEqual(cuts.At(6).GetName(), "CutPreselectionTopCR")
        self.assertEqual(cuts.At(7).GetName(), "CutVbfTopCR")
        self.assertEqual(cuts.At(8).GetName(), "CutBoostedTopCR")

        base = self.get_cut_hierarchy()
        cuts = TObjArray()
        base.getMatchingCuts(cuts, "CutOS/Cut*")
        self.assertEqual(cuts.GetEntries(), 2)
        self.assertEqual(cuts.At(0).GetName(), "CutBVeto")
        self.assertEqual(cuts.At(1).GetName(), "CutBReq")