Beispiel #1
0
def Fill_histograms(FILENAME, BRANCHLISTALL, DICHISTLIST, BranchListEachTree):
    gBenchmark.Start("Filling & Writing Histograms")
    #    print("!@#!@#!#",DICHISTLIST.keys())
    #    print("!@#!@#!",BRANCHLISTALL)
    DicNumpyArray_branch = {}
    for numpyarray in BRANCHLISTALL:
        a = numpy.array([0], 'd')
        DicNumpyArray_branch[numpyarray] = a
    DicNumpyArray_branch = collections.OrderedDict(
        sorted(DicNumpyArray_branch.items())
    )  #  !!!the input times are ordered!!!
    #    print("!@#!@#!#",DicNumpyArray_branch)

    f = TFile(FILENAME, "READ")
    dirlist = f.GetListOfKeys()
    ITER = dirlist.MakeIterator()
    key = ITER.Next()
    while key:
        it = 0
        tree = key.ReadObj()
        #        print("!@#!@#!#",DICHISTLIST[tree.GetName()])
        for i in range(len(DicNumpyArray_branch)):
            if (list(DicNumpyArray_branch.keys())[i]
                    in BranchListEachTree[tree.GetName()]):
                it = it + 1
                pass
            else:
                continue
            tree.SetBranchAddress(
                list(DicNumpyArray_branch.keys())[i],
                list(DicNumpyArray_branch.values())[i])

        if (it == 0):
            continue

        ENTRY = tree.GetEntries()
        print("for tree", tree.GetName())
        for i in range(ENTRY):
            tree.GetEntry(i)
            if (i % 1000 == 0):
                print("now looping", i, "th Events, total of ", ENTRY,
                      "events")

#            for j in range(len(DICHISTLIST[tree.GetName()])):
            for j in range(len(DicNumpyArray_branch)):
                for k in range(len(DICHISTLIST[tree.GetName()])):
                    if (list(DicNumpyArray_branch.keys())[j]
                            in DICHISTLIST[tree.GetName()][k].GetName()):
                        DICHISTLIST[tree.GetName()][k].Fill(
                            list(DicNumpyArray_branch.values())[j][0])
                    else:
                        continue

#DICHISTLIST[tree.GetName()][k].Scale(DicNumpyArray_branch["JWeight"][j][0])

        key = ITER.Next()
#    print(DICHISTLIST)
#    gBenchmark.Show("filling")
    f.Close()
    return DICHISTLIST
def Fill_histograms(FILENAME, BRANCHLISTALL, DICHISTLIST):
    gBenchmark.Start("Filling & Writing Histograms")
    DicNumpyArray_branch = {}
    for numpyarray in BRANCHLISTALL:
        a = numpy.array([0], 'd')
        DicNumpyArray_branch[numpyarray] = a
    DicNumpyArray_branch = collections.OrderedDict(
        sorted(DicNumpyArray_branch.items())
    )  #  !!!the input times are ordered!!!
    #    print(DicNumpyArray_branch)

    #    print("!!!!!!",(len(DicNumpyArray_branch)))        # correct DicNumpyArray_branch
    f = TFile(FILENAME, "READ")
    dirlist = f.GetListOfKeys()
    ITER = dirlist.MakeIterator()
    key = ITER.Next()
    while key:
        tree = key.ReadObj()
        #        print("!!!!!!",len(DICHISTLIST[tree.GetName()]))   # not correct, returning 13 which should be 10
        for i in range(len(DicNumpyArray_branch)):
            tree.SetBranchAddress(DicNumpyArray_branch.keys()[i],
                                  DicNumpyArray_branch.values()[i])
        ENTRY = tree.GetEntries()
        print("for tree", tree.GetName())
        for i in range(ENTRY):
            tree.GetEntry(i)
            if (i % 5000 == 0):
                print("now looping", i, "th Events, total of ", ENTRY,
                      "events")
            for j in range(len(DICHISTLIST[tree.GetName()])):
                for k in range(len(DICHISTLIST[tree.GetName()])):
                    if (DicNumpyArray_branch.keys()[j]
                            in DICHISTLIST[tree.GetName()][k].GetName()):
                        DICHISTLIST[tree.GetName()][k].Fill(
                            DicNumpyArray_branch.values()[j][0])
                    else:
                        continue

        key = ITER.Next()
#    print(DICHISTLIST)
#    gBenchmark.Show("filling")
    return DICHISTLIST
Beispiel #3
0
#
# This macro displays the Tree data structures
#

from ROOT import TCanvas, TPaveLabel, TPaveText, TPavesText, TText
from ROOT import TArrow, TLine
from ROOT import gROOT, gBenchmark

#gROOT.Reset()

c1 = TCanvas('c1', 'Tree Data Structure', 200, 10, 750, 940)
c1.Range(0, -0.1, 1, 1.15)

gBenchmark.Start('tree')

branchcolor = 26
leafcolor = 30
basketcolor = 42
offsetcolor = 43
#title = TPaveLabel(.3,1.05,.8,1.13,c1.GetTitle())
title = TPaveLabel(.3, 1.05, .8, 1.13, 'Tree Data Structure')
title.SetFillColor(16)
title.Draw()
tree = TPaveText(.01, .75, .15, 1.00)
tree.SetFillColor(18)
tree.SetTextAlign(12)
tnt = tree.AddText('Tree')
tnt.SetTextAlign(22)
tnt.SetTextSize(0.030)
tree.AddText('fScanField')
tree.AddText('fMaxEventLoop')
Beispiel #4
0
## \macro_code
##
## \author Wim Lavrijsen

from os import path
from ROOT import TCanvas, TFile, TPaveText
from ROOT import gROOT, gBenchmark

c1 = TCanvas('c1', 'The Fit Canvas', 200, 10, 700, 500)
c1.SetGridx()
c1.SetGridy()
c1.GetFrame().SetFillColor(21)
c1.GetFrame().SetBorderMode(-1)
c1.GetFrame().SetBorderSize(5)

gBenchmark.Start('fit1')
#
# We connect the ROOT file generated in a previous tutorial
#
fill = TFile('py-fillrandom.root')

#
# The function "ls()" lists the directory contents of this file
#
fill.ls()

#
# Get object "sqroot" from the file.
#

sqroot = gROOT.FindObject('sqroot')
Beispiel #5
0
from os import path
from ROOT import TCanvas, TPad, TFile, TPaveText
from ROOT import gROOT, gBenchmark

c1 = TCanvas("c1","c1",200,10,700,500)
c1.SetGrid()
c1.GetFrame().SetFillColor(21)
c1.GetFrame().SetBorderMode(-1)
c1.GetFrame().SetBorderSize(5)

gBenchmark.Start("fit1")

fill = TFile("py-fillrandom.root","READ")
fill.ls()

sqroot = gROOT.FindObject("sqroot")
sqroot.Print(); print("\n")

hist = gROOT.FindObject("h1f")
hist.Print(); print("\n"); hist.Draw()
hist.SetFillColor(45)
hist.Fit("sqroot")
c1.Update()





fill.Close()
gBenchmark.Show("fit1")
Beispiel #6
0
from ROOT import gSystem, gROOT
from ROOT import gBenchmark
gBenchmark.Start("All in One")
import sys
import os

os.system("rm -rf ROOT_TGraph_basic")
os.system("rm -rf ROOT_2D_colz")
os.system("rm -rf ROOT_2D_surf3")
os.system("rm -rf ROOT_2D_profileX_pols")
os.system("rm -rf ROOT_files")
os.system("rm -rf ROOT_python_plots")
os.system("rm -rf ROOT_python_hist_texts")

#INfile = "carbon_copied_data/Aqi_Beijing_day.txt"
INfile = "Aqi_Beijing_LTEMP.txt"
BIN_Num_2D = 5  # X bin number of 2D
YBIN_Num_2D = 1000  # Y bin number of 2D
oneD_NBins = 100

#### option to draw ###
N_sigma = 5  ## Skiming of txt outof specific sigma region
TGraph_2d_basic = 1
Marker_style = ",2"
poly19_fit = ",0"
TH1D_transfer = 1
TH2D_transfer = 1
if TH2D_transfer == 1:
    TH2D_colz_surf3 = 1
    TH2D_profileX = 1
Beispiel #7
0
## \file
## \ingroup tutorial_pyroot
## Ntuple drawing example.
##
## \macro_code
##
## \author Wim Lavrijsen

from ROOT import TCanvas, TPad, TFile, TPaveText
from ROOT import gBenchmark, gStyle, gROOT

c1 = TCanvas('c1', 'The Ntuple canvas', 200, 10, 700, 780)

gBenchmark.Start('ntuple1')

#
# Connect ROOT histogram/ntuple demonstration file
# generated by example hsimple.C.
f1 = TFile('py-hsimple.root')

#
# Inside this canvas, we create 4 pads
pad1 = TPad('pad1', 'This is pad1', 0.02, 0.52, 0.48, 0.98, 21)
pad2 = TPad('pad2', 'This is pad2', 0.52, 0.52, 0.98, 0.98, 21)
pad3 = TPad('pad3', 'This is pad3', 0.02, 0.02, 0.48, 0.48, 21)
pad4 = TPad('pad4', 'This is pad4', 0.52, 0.02, 0.98, 0.48, 1)
pad1.Draw()
pad2.Draw()
pad3.Draw()
pad4.Draw()
def main():

    INPUT_FILE_INCLUDING_PATH = "../../../root_generator/tree/root2_tree.root"

    FileNameList = read_file_name(INPUT_FILE_INCLUDING_PATH)
    BranchListAll = get_branch_list_all(FileNameList[2])
    BranchListEachTree = get_branch_list_each_tree(FileNameList[2])

    DicNumpyArray_branch = {}
    for numpyarray in BranchListAll:
        a = numpy.array([0], 'd')
        DicNumpyArray_branch[numpyarray] = a
    DicNumpyArray_branch = collections.OrderedDict(
        sorted(DicNumpyArray_branch.items())
    )  #  !!!the input times are ordered!!!
    print(DicNumpyArray_branch)

    DicNumpyArray_branch_w = {}
    for numpyarray_w in BranchListAll:
        a_w = numpy.array([0], 'd')
        DicNumpyArray_branch_w[numpyarray_w] = a_w
    DicNumpyArray_branch_w = collections.OrderedDict(
        sorted(DicNumpyArray_branch_w.items()))
    print(DicNumpyArray_branch_w)

    WCuts = WhetherAddCut(BranchListEachTree)
    if (WCuts == None):
        print("\n")
        print(
            "*********************************************************************************************"
        )
        print("!!!! Please check input CUT !!!!! ")
        print("     !!!! No Output File !!!!    ")
        print("\n")
        for i in range(len(BranchListEachTree)):
            print("compenets below :")
            print("The Tree name is : ", BranchListEachTree.keys()[i])
            for j in range(len(BranchListEachTree.values()[i])):
                print("        it contains : ",
                      BranchListEachTree.values()[i][j])
            print("\n")
        print("!!!! Please check input CUT !!!!! ")
        print("     !!!! No Output File !!!!    ")
        print(
            "*********************************************************************************************"
        )
    else:
        print("\n")
        print("...Cut added and regenerating tree root file...")
        gBenchmark.Start("Regerating tree root")

        f = TFile(FileNameList[2], "READ")
        dirlist = f.GetListOfKeys()
        ITER = dirlist.MakeIterator()
        key = ITER.Next()
        outfileName = FileNameList[0] + "_cut_tree.root"
        outfile = TFile(outfileName, "RECREATE")
        ijk = 0
        while key:
            tree = key.ReadObj()
            tree_f = TTree(tree.GetName() + "_f", tree.GetName() + "_f")
            ENTRY = tree.GetEntries()
            #print(ENTRY)
            for i in range(len(DicNumpyArray_branch)):
                tree.SetBranchAddress(DicNumpyArray_branch.keys()[i],
                                      DicNumpyArray_branch.values()[i])
                tree_f.Branch(DicNumpyArray_branch_w.keys()[i],
                              DicNumpyArray_branch_w.values()[i],
                              DicNumpyArray_branch_w.keys()[i] + "/D")
            print("for tree", tree.GetName())
            for j in range(ENTRY):
                tree.GetEntry(j)
                if (j % 5000 == 0):
                    print("now looping", j, "th Events, total of ", ENTRY,
                          "events")
                for k in range(len(DicNumpyArray_branch)):
                    DicNumpyArray_branch_w.values(
                    )[k][0] = DicNumpyArray_branch.values()[k][0]
                if (  #FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME
                    (
                        DicNumpyArray_branch.values()[0][0] < 0.5
                    )  # [i][0]  means "i"th branch of the tree, [0] don't change   #FIXME#FIXME#FIXME#FIXME
                        &
                    (
                        DicNumpyArray_branch.values()[1][0] < 0.5
                    )  #FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME
                ):
                    ijk = ijk + 1
                    tree_f.Fill()
                else:
                    continue
            print(" Event left after filtering : ", ijk, "!!!!!!!!!")
            print("\n")
            ijk = 0
            if (tree_f.GetEntries() == 0):
                print("!!!!!!! ", tree_f.GetName(),
                      " is Empty, would not be written !!!!!")
            else:
                tree_f.Write()

            key = ITER.Next()

        print("")
        print("////////////////////////////////////////////////")
        print("outputfile : ")
        print(outfileName)
        print("////////////////////////////////////////////////")
        print("")

        outfile.Close()
        f.Close()
        print(
            "*********************************************************************************************"
        )
        gBenchmark.Show("Regerating tree root")
        print(
            "*********************************************************************************************"
        )
from ROOT import TCanvas, TPad, TFormula, TF1, TPaveLabel, TH1F, TFile
from ROOT import gROOT, gBenchmark

c1 = TCanvas("c1,", "c1", 200, 10, 700, 900)
c1.SetFillColor(18)

pad1 = TPad("pad1", "pad1", 0.05, 0.50, 0.95, 0.95, 21)
pad2 = TPad("pad2", "pad2", 0.05, 0.05, 0.95, 0.45, 21)
pad1.Draw()
pad2.Draw()

gBenchmark.Start("fillrandom")

form1 = TFormula("form1", "abs(sin(x)/x)")
sqroot = TF1("sqroot", "x*gaus(0) + [3]*form1", 0, 10)
sqroot.SetParameters(10, 4, 1, 20)
#pad1.SetGridx()
pad2.SetGridy()
pad1.SetGrid()
c1.Update()
pad1.cd()
sqroot.Draw()
pad1.Modified()
pad1.Update()

pad2.cd()
pad2.GetFrame().SetFillColor(42)
pad2.GetFrame().SetBorderMode(-1)
pad2.GetFrame().SetBorderSize(5)
h1f = TH1F("h1f", "h1f", 200, 0, 10)
h1f.FillRandom("sqroot", 10000)
def REGENERATE_TREE_WITH_CUT(filename, outputpath=''):

    FileNameList = read_file_name(filename)
    #    print(FileNameList)
    BranchListAll = get_branch_list_all(FileNameList[2])
    BranchListEachTree = get_branch_list_each_tree(FileNameList[2])
    #    print("@#!@#!@#",BranchListEachTree)
    #    print("@#!@#!@#",BranchListAll)

    DicNumpyArray_branch = {}
    for numpyarray in BranchListAll:
        a = numpy.array([0], 'd')
        DicNumpyArray_branch[numpyarray] = a
    DicNumpyArray_branch = collections.OrderedDict(
        sorted(DicNumpyArray_branch.items())
    )  #  !!!the input times are ordered!!!
    #    print(DicNumpyArray_branch)

    DicNumpyArray_branch_w = {}
    for numpyarray_w in BranchListAll:
        a_w = numpy.array([0], 'd')
        DicNumpyArray_branch_w[numpyarray_w] = a_w
    DicNumpyArray_branch_w = collections.OrderedDict(
        sorted(DicNumpyArray_branch_w.items()))
    #    print(DicNumpyArray_branch_w)

    #    print(DicNumpyArray_branch.keys())
    #    print(DicNumpyArray_branch.values())

    WCuts = WhetherAddCut(BranchListEachTree)
    if (WCuts == None):
        print("\n")
        print(
            "*********************************************************************************************"
        )
        print("!!!! Please check input CUT !!!!! ")
        print("     !!!! No Output File !!!!    ")
        print("\n")
        #        for i in range(len(BranchListEachTree)):
        #            print("compenets below :")
        #            print("The Tree name is : ",BranchListEachTree.keys()[i])
        #            for j in range(len(BranchListEachTree.values()[i])):
        #                print("        it contains : ", BranchListEachTree.values()[i][j])
        #            print("\n")
        print("!!!! Please check input CUT !!!!! ")
        print("     !!!! No Output File !!!!    ")
        print(
            "*********************************************************************************************"
        )
    else:
        print("\n")
        print("...Cut added and regenerating tree root file...")
        gBenchmark.Start("Regerating tree root")

        f = TFile(FileNameList[2], "READ")
        dirlist = f.GetListOfKeys()
        ITER = dirlist.MakeIterator()
        key = ITER.Next()

        if (outputpath == ''):
            outfileName = FileNameList[3] + "/" + FileNameList[0] + "_cut.root"
            outfileName = outfileName.replace("//", "/")
#            print("!@#!@!@#!@ ",outfileName)
        elif (outputpath[0] == "/"):
            outfileName = outputpath + "/" + FileNameList[0] + "_cut.root"
            outfileName = outfileName.replace("//", "/")
#            print("!@#!@!@#!@ ",outfileName)
        elif (outputpath[0] == "~"):
            outfileName = outputpath.replace(
                "~", os.environ['HOME']) + "/" + FileNameList[0] + "_cut.root"
            outfileName = outfileName.replace("//", "/")
#            print("!@#!@!@#!@ ",outfileName)
        else:
            outfileName = os.getcwd(
            ) + "/" + outputpath + "/" + FileNameList[0] + "_cut.root"
            outfileName = outfileName.replace("//", "/")
#            print("!@#!@!@#!@ ",outfileName)

        outfile = TFile(outfileName, "RECREATE")

        ijk = 0
        while key:
            tree = key.ReadObj()
            tree_f = TTree(tree.GetName() + "_f", tree.GetName() + "_f")
            ENTRY = tree.GetEntries()
            #print(ENTRY)
            for i in range(len(DicNumpyArray_branch)):
                if (list(DicNumpyArray_branch.keys())[i]
                        in BranchListEachTree[tree.GetName()]):
                    tree.SetBranchAddress(
                        list(DicNumpyArray_branch.keys())[i],
                        list(DicNumpyArray_branch.values())[i])
                    tree_f.Branch(
                        list(DicNumpyArray_branch_w.keys())[i],
                        list(DicNumpyArray_branch_w.values())[i],
                        list(DicNumpyArray_branch_w.keys())[i] + "/D")
                else:
                    continue

            print("for tree", tree.GetName())
            #            tt = 0
            for j in range(ENTRY):
                tree.GetEntry(j)
                if (j % 5000 == 0):
                    print("now looping", j, "th Events, total of ", ENTRY,
                          "events")
                for k in range(len(DicNumpyArray_branch)):
                    if (list(DicNumpyArray_branch.keys())[k]
                            in BranchListEachTree[tree.GetName()]
                        ):  ### FIXED MAYBE not correct....
                        pass
                    else:
                        continue
                    list(DicNumpyArray_branch_w.values())[k][0] = list(
                        DicNumpyArray_branch.values())[k][0]


#                    if(j==0):
#                        print(k,DicNumpyArray_branch_w.keys()[k])
#                        print(DicNumpyArray_branch_w.keys()[k])

#                tt = raw_input("press 'Y' to proceed, 'enter' to quit!")  #FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME

                if (True  #FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME

                        #                   & (DicNumpyArray_branch.values()[0][0] > 0)          # [i][0]  means "i+1"th branch of each tree, [0] don't change   #FIXME#FIXME#FIXME#FIXME
                        #&  (DicNumpyArray_branch.values()[1][0] > 0)        #FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME
                    ):
                    ijk = ijk + 1
                    tree_f.Fill()
                else:
                    continue
            print(" Event left after filtering : ", ijk, "!!!!!!!!!")
            print("\n")
            ijk = 0
            if (tree_f.GetEntries() == 0):
                print("!!!!!!! ", tree_f.GetName(),
                      " is Empty, would not be written !!!!!")
            else:
                tree_f.Write()

            key = ITER.Next()

        print("")
        print("////////////////////////////////////////////////")
        print("outputfile : ")
        print(outfileName)
        print("////////////////////////////////////////////////")
        print("")

        outfile.Close()
        f.Close()
        print(
            "*********************************************************************************************"
        )
        gBenchmark.Show("Regerating tree root")
        print(
            "*********************************************************************************************"
        )

        return outfileName
Beispiel #11
0
## \file
## \ingroup tutorial_pyroot
## Tornado example.
## \notebook
##
## \macro_image
## \macro_code
##
## \author Wim Lavrijsen

from ROOT import TCanvas, TView, TPolyMarker3D, TPaveText
from ROOT import gROOT, gBenchmark
from math import cos, sin, pi

gBenchmark.Start('tornado')

d = 16
numberOfPoints = 200
numberOfCircles = 40

# create and open a canvas
sky = TCanvas('sky', 'Tornado', 300, 10, 700, 500)
sky.SetFillColor(14)

# creating view
view = TView.CreateView()
rng = numberOfCircles * d
view.SetRange(0, 0, 0, 4.0 * rng, 2.0 * rng, rng)

polymarkers = []
for j in range(d, numberOfCircles * d, d):
Beispiel #12
0
def Fill_histograms(FILENAME, BRANCHLISTALL, DICHISTLIST, BranchListEachTree):
    gBenchmark.Start("Filling & Writing 2D Histograms")
    DicNumpyArray_branch = {}
    for numpyarray in BRANCHLISTALL:
        a = numpy.array([0], 'd')
        DicNumpyArray_branch[numpyarray] = a
    DicNumpyArray_branch = collections.OrderedDict(
        sorted(DicNumpyArray_branch.items())
    )  #  !!!the input times are ordered!!!
    #    print(DicNumpyArray_branch)

    f = TFile(FILENAME, "READ")
    dirlist = f.GetListOfKeys()
    ITER = dirlist.MakeIterator()
    key = ITER.Next()
    while key:
        it = 0
        tree = key.ReadObj()
        for i in range(len(DicNumpyArray_branch)):
            if (list(DicNumpyArray_branch.keys())[i]
                    in BranchListEachTree[tree.GetName()]):
                it = it + 1
                pass
            else:
                continue
            tree.SetBranchAddress(
                list(DicNumpyArray_branch.keys())[i],
                list(DicNumpyArray_branch.values())[i])
        ENTRY = tree.GetEntries()

        if (it == 0):
            continue

        print("for tree", tree.GetName())
        #        print(len(DICHISTLIST[tree.GetName()]))
        #        print(len(DicNumpyArray_branch))
        #        print(len(DICHISTLIST))
        #        print(DICHISTLIST.keys()[0]); print(DICHISTLIST.values()[0][0].GetName())
        #        print(DICHISTLIST.values())
        for i in range(ENTRY):
            tree.GetEntry(i)
            if (i % 1000 == 0):
                print("now looping", i, "th Events, total of ", ENTRY,
                      "events")
            for j in range(len(DICHISTLIST)):
                if (tree.GetName() == list(DICHISTLIST.keys())[j]):
                    for k in range(len(list(
                            DICHISTLIST.values())[j])):  # 6 rotation for histo
                        jun = 0
                        for jk in range(len(DicNumpyArray_branch)
                                        ):  # 3 rotation for branch
                            if (list(DicNumpyArray_branch.keys())[jk]
                                    in BranchListEachTree[tree.GetName()]):
                                pass
                            else:
                                continue
                            if ((list(DicNumpyArray_branch.keys())[jk] in list(
                                    DICHISTLIST.values())[j][k].GetName()) &
                                (jun == 0)):
                                MOMEM1 = list(DicNumpyArray_branch.keys())[jk]
                                JUN1 = jk
                                jun = jun + 1
                            elif (
                                (list(DicNumpyArray_branch.keys())[jk] in list(
                                    DICHISTLIST.values())[j][k].GetName()) &
                                (jun == 1)):
                                MOMEM2 = list(DicNumpyArray_branch.keys())[jk]
                                JUN2 = jk
                                jun = jun + 1
                            else:
                                continue

                            if (jun == 2):
                                if (("X_" + MOMEM1 + "_Y_" + MOMEM2) in list(
                                        DICHISTLIST.values())[j][k].GetName()):
                                    DICHISTLIST[tree.GetName()][k].Fill(
                                        list(DicNumpyArray_branch.values())
                                        [JUN1][0],
                                        list(DicNumpyArray_branch.values())
                                        [JUN2][0])
                                elif (("X_" + MOMEM2 + "_Y_" + MOMEM1) in list(
                                        DICHISTLIST.values())[j][k].GetName()):
                                    DICHISTLIST[tree.GetName()][k].Fill(
                                        list(DicNumpyArray_branch.values())
                                        [JUN2][0],
                                        list(DicNumpyArray_branch.values())
                                        [JUN1][0])

                else:
                    continue

        key = ITER.Next()

    return DICHISTLIST
from ROOT import TCanvas, TFile, TProfile, TNtuple, TH1F, TH2F from ROOT import gROOT, gBenchmark, gRandom, gSystem, Double
# Create a new canvas, and customize it.
c1 = TCanvas( 'c1', 'Dynamic Filling Example', 200, 10, 700, 500 ) c1.SetFillColor( 42 ) c1.GetFrame().SetFillColor( 21 ) 
c1.GetFrame().SetBorderSize( 6 ) c1.GetFrame().SetBorderMode( -1 )
# Create a new ROOT binary machine independent file. Note that this file may contain any kind of ROOT objects, histograms, 
# pictures, graphics objects, detector geometries, tracks, events, etc.. This file is now becoming the current directory.
hfile = gROOT.FindObject( 'py-hsimple.root' ) if hfile:
   hfile.Close() hfile = TFile( 'py-hsimple.root', 'RECREATE', 'Demo ROOT file with histograms' )
# Create some histograms, a profile histogram and an ntuple
hpx = TH1F( 'hpx', 'This is the px distribution', 100, -4, 4 ) hpxpy = TH2F( 'hpxpy', 'py vs px', 40, -4, 4, 40, -4, 4 ) hprof = 
TProfile( 'hprof', 'Profile of pz versus px', 100, -4, 4, 0, 20 ) ntuple = TNtuple( 'ntuple', 'Demo ntuple', 'px:py:pz:random:i' )
# Set canvas/frame attributes.
hpx.SetFillColor( 48 ) gBenchmark.Start( 'hsimple' )
# Initialize random number generator.
gRandom.SetSeed() rannor, rndm = gRandom.Rannor, gRandom.Rndm
# For speed, bind and cache the Fill member functions,
histos = [ 'hpx', 'hpxpy', 'hprof', 'ntuple' ] for name in histos:
   exec('%sFill = %s.Fill' % (name,name))
# Fill histograms randomly.
px, py = Double(), Double() kUPDATE = 1000 for i in range( 25000 ):
 # Generate random values.
   rannor( px, py )
   pz = px*px + py*py
   random = rndm(1)
 # Fill histograms.
   hpx.Fill( px )
   hpxpy.Fill( px, py )
   hprof.Fill( px, pz )
   ntuple.Fill( px, py, pz, random, i )
 # Update display every kUPDATE events.
   if i and i%kUPDATE == 0:
    def SplitTree(self):
        FileNameList = read_file_name_root(self._infile)
        BranchListAll = self.get_branch_list_all()
        BranchListEachTree = self.get_branch_list_each_tree()

        DicNumpyArray_branch = {}
        for numpyarray in BranchListAll:
            a = numpy.array([0], 'd')
            DicNumpyArray_branch[numpyarray] = a
        DicNumpyArray_branch = collections.OrderedDict(
            sorted(DicNumpyArray_branch.items()))
        print(DicNumpyArray_branch)

        DicNumpyArray_branch_w = {}
        for numpyarray_w in BranchListAll:
            a_w = numpy.array([0], 'd')
            DicNumpyArray_branch_w[numpyarray_w] = a_w
        DicNumpyArray_branch_w = collections.OrderedDict(
            sorted(DicNumpyArray_branch_w.items()))
        print(DicNumpyArray_branch_w)

        gBenchmark.Start("Regerating tree root")
        f = TFile(FileNameList[2], "READ")
        dirlist = f.GetListOfKeys()
        ITER = dirlist.MakeIterator()
        key = ITER.Next()

        outfileName = os.getcwd(
        ) + "/" + FileNameList[0] + "_cut_TTTL.root"  #FIXME
        print("CREATING... :", outfileName)
        outfile = TFile(outfileName, "RECREATE")

        ijk = 0
        break_flag = 0
        while key:
            if (break_flag == 1): break
            break_flag += 1
            tree = key.ReadObj()
            tree_f = TTree(tree.GetName() + "", tree.GetName() + "")
            ENTRY = tree.GetEntries()
            #print(ENTRY)
            for i in range(len(DicNumpyArray_branch)):
                if (list(DicNumpyArray_branch.keys())[i]
                        in BranchListEachTree[tree.GetName()]):
                    tree.SetBranchAddress(
                        list(DicNumpyArray_branch.keys())[i],
                        list(DicNumpyArray_branch.values())[i])
                    tree_f.Branch(
                        list(DicNumpyArray_branch_w.keys())[i],
                        list(DicNumpyArray_branch_w.values())[i],
                        list(DicNumpyArray_branch_w.keys())[i] + "/D")
                else:
                    continue

            print("for tree", tree.GetName())
            for j in range(ENTRY):
                tree.GetEntry(j)
                if (j % 5000 == 0):
                    print("now looping", j, "th Events, total of ", ENTRY,
                          "events")
                for k in range(len(DicNumpyArray_branch)):
                    if (list(DicNumpyArray_branch.keys())[k]
                            in BranchListEachTree[tree.GetName()]
                        ):  ### FIXED MAYBE not correct....
                        pass
                    else:
                        continue
                    list(DicNumpyArray_branch_w.values())[k][0] = list(
                        DicNumpyArray_branch.values())[k][0]
                if (True
                        #& (list(DicNumpyArray_branch.values())[0][0] == 1)  # LL  #FIXME
                        & (list(DicNumpyArray_branch.values())[0][0] == 0
                           )  # TTTL  #FIXME
                    ):
                    ijk = ijk + 1
                    tree_f.Fill()
                else:
                    continue
            print(" Event left after filtering : ", ijk, "!!!!!!!!!")
            print("\n")
            ijk = 0
            if (tree_f.GetEntries() == 0):
                print("!!!!!!! ", tree_f.GetName(),
                      " is Empty, would not be written !!!!!")
            else:
                tree_f.Write()

            key = ITER.Next()

        print("")
        print("////////////////////////////////////////////////")
        print("outputfile : ")
        print(outfileName)
        print("////////////////////////////////////////////////")
        print("")

        outfile.Close()
        f.Close()
        print(
            "*********************************************************************************************"
        )
        gBenchmark.Show("Regerating tree root")
        print(
            "*********************************************************************************************"
        )

        return outfileName
Beispiel #15
0
hfile = gROOT.FindObject('hsimple.root')
if hfile:
    hfile.Close()
hfile = TFile('hsimple.root', 'RECREATE', 'Demo ROOT file with histograms')

# Create some histograms, a profile histogram and an ntuple
hpx = TH1F('hpx', 'This is the px distribution', 100, -4, 4)
hpxpy = TH2F('hpxpy', 'py vs px', 40, -4, 4, 40, -4, 4)
hprof = TProfile('hprof', 'Profile of pz versus px', 100, -4, 4, 0, 20)
ntuple = TNtuple('ntuple', 'Demo ntuple', 'px:py:pz:random:i')

# Set canvas/frame attributes.
hpx.SetFillColor(48)

gBenchmark.Start('hsimple')

# Initialize random number generator.
gRandom.SetSeed()
gauss, rndm = gRandom.Gaus, gRandom.Rndm

# For speed, bind and cache the Fill member functions,
histos = ['hpx', 'hpxpy', 'hprof', 'ntuple']
for name in histos:
    exec '%sFill = %s.Fill' % (name, name)

# Fill histograms randomly.
kUPDATE = 1000
for i in xrange(25000):
    # Generate random values.
    px = gauss()
Beispiel #16
0
from ROOT import TCanvas, TPad, TFormula, TF1, TPaveLabel, TH1F, TFile
from ROOT import gROOT, gBenchmark



c1 = TCanvas( 'c1', 'The FillRandom example', 200, 10, 700, 900 )
c1.SetFillColor( 18 )

pad1 = TPad( 'pad1', 'The pad with the function',  0.05, 0.50, 0.95, 0.95, 21 )
pad2 = TPad( 'pad2', 'The pad with the histogram', 0.05, 0.05, 0.95, 0.45, 21 )
pad1.Draw()
pad2.Draw()
pad1.cd()

gBenchmark.Start( 'fillrandom' )
#
# A function (any dimension) or a formula may reference
# an already defined formula
#
form1 = TFormula( 'form1', 'abs(sin(x)/x)' )
sqroot = TF1( 'sqroot', 'x*gaus(0) + [3]*form1', 0, 10 )
sqroot.SetParameters( 10, 4, 1, 20 )
pad1.SetGridx()
pad1.SetGridy()
pad1.GetFrame().SetFillColor( 42 )
pad1.GetFrame().SetBorderMode( -1 )
pad1.GetFrame().SetBorderSize( 5 )
sqroot.SetLineColor( 4 )
sqroot.SetLineWidth( 6 )
sqroot.Draw()
Beispiel #17
0
## \ingroup tutorial_pyroot
## Simple example illustrating how to use the C++ interpreter
##
## \macro_image
## \macro_code
##
## \author Wim Lavrijsen

from ROOT import TCanvas, TH1F, TSlider
from ROOT import gROOT, gBenchmark, gRandom

# Create a new canvas, and customize it.
c1 = TCanvas('c1', 'The HSUM example', 200, 10, 600, 400)
c1.SetGrid()

gBenchmark.Start('hsum')

# Create some histograms.
total = TH1F('total', 'This is the total distribution', 100, -4, 4)
main = TH1F('main', 'Main contributor', 100, -4, 4)
s1 = TH1F('s1', 'This is the first signal', 100, -4, 4)
s2 = TH1F('s2', 'This is the second signal', 100, -4, 4)
total.Sumw2(
)  # this makes sure that the sum of squares of weights will be stored

# Set canvas/frame attributes.
total.SetMarkerStyle(21)
total.SetMarkerSize(0.7)
main.SetFillColor(16)
s1.SetFillColor(42)
s2.SetFillColor(46)