Example #1
0
    def update(self, v=True):
        # Update branches:
        if hasattr(self.ana, "branches"):
            self.branches = self.ana.branches[self.key]
        else:
            self.branches = False

        # Apply cuts:
        if hasattr(self.ana, "cut"):
            gROOT.cd()
            self.tt_in = self.tt_in.CopyTree(self.ana.cut)

        if v: print "Fetching the number of events in {} ...".format(self.key)
        #		self.n_total = self.tt_in.GetEntries()                      # The total number of events in the input dataset
        if self.ana.count:
            self.ns = root.tc_nevents(
                self.tt_in
            )  # The total number of events in each file of the input TChain
            self.n = sum(self.ns)
        else:
            if not self.ns:
                print "This isn't implemented, yet!"
                print "I have to use the key to look in the DB for tuple nevents"
                sys.exit()
#				samples = dataset.fetch_samples(process=self.key)
#				tuples = []
#				for sample in samples:
#					tuples.extend([tup for tup in sample.tuples if tup.generation == "spring15" and tup.suffix == "pt400"])
#				self.n_total_list = [N for tup in tuples for N in tup.ns]
        if not self.n:
            print "[!!] The TChain you want to run over has no events."
            sys.exit()
        if v: print "\tThere are {} events.".format(self.n)
    def __init__(self, sample ,File, WS , PDF , eff_file , thqDomBinName , low,high):
        self.Sample = sample
        self.File = File
        self.WS = WS
        self.PDF = PDF

        self.file = TFile.Open( self.File )
        #self.file.ls()
        self.ws = self.file.Get(self.WS)
        #print sample
        #self.file.ls()
        #self.ws.Print()
        #print self.ws, self.File, self.WS
        self.pdf = self.ws.pdf( self.PDF )
        self.norm = self.ws.function( self.PDF + "_norm" )
        self.CT = self.ws.var("CtOverCv")
        self.CV = self.ws.var("CV")

        self.Low = low
        self.High = high

        self.EfficiencyFile = TFile.Open( eff_file )
        gROOT.cd()
        self.EffHist = self.EfficiencyFile.Get("%(sample)s/%(bin)s/h%(sample)s_%(bin)s_eff_CtCv" % {"sample":sample , "bin":thqDomBinName}).Clone("Eff_%s" % sample )
        self.EfficiencyFile.Close()
        self.EffHist.Print()
Example #3
0
 def __init__(
         self,
         ana,  # The analyzer it's in
         key,  # Key used to identify the loop
         n_default=-1,  # The default number of events to run over (use "-1" for "all") (see "run" to see why)
         progress=True,  # Turn on progress bar.
 ):
     self.ana = ana
     self.key = key
     self.n_default = n_default
     self.ns = self.ana.tt_info[self.key]["ns"]
     self.n = sum(self.ns)
     self.n_run = 0  # The number of events to loop over in the current "run".
     #		self.rand = True               # True: select the events randomly, False: select events from the beginning
     #		self.v = True                  # Verbose mode
     tt_in = ana.tt_in[key]
     self.progress = progress
     # Apply cuts:
     if hasattr(self.ana, "cut"):
         gROOT.cd()
         tt_in = tt_in.CopyTree(self.ana.cut)
     self.tt_in = tt_in
     self.tt_out = None
     if ana.save:
         self.tt_out = ana.tt_out[key]
Example #4
0
def make_root_plot_from_results_file(rid,
                                     is_parallel,
                                     draw_string,
                                     cut_string="",
                                     option_string=""):
    """Makes a ROOT plot from results file based on a draw and cut string."""

    # gStyle.SetOptStat("iMRKS")

    if is_parallel:
        letter = 'P'
    else:
        letter = 'A'
    file_path = srkglobal.results_dir + "Results_RID" + str(
        rid) + "_" + letter + ".root"

    if not srkmisc.file_exits_and_not_zombie(file_path):
        print file_path + " doesn't exist or is zombie."
        return

    root_file = TFile(file_path, "READ")
    hit_tree = gDirectory.Get('hitTree')
    gROOT.cd()

    c1 = rootnotes.canvas("Canvas", (1200, 800))
    hit_tree.Draw(draw_string, cut_string, option_string)
    root_file.Close()
    return c1
Example #5
0
def make_txt_hist_from_root_file(file_path_txt, rid, is_parallel, draw_string, hist_dim, cut_string="",
                                 option_string="", titles=("", "", "")):
    """Given a draw/cut string, take an SRK ROOT results file and create a histogram from it."""

    if is_parallel:
        letter = 'P'
    else:
        letter = 'A'
    file_path = srkdata.srkglobal.results_dir + "Results_RID" + str(rid) + "_" + letter + ".root"

    if not srkmisc.file_exits_and_not_zombie(file_path):
        print file_path + " doesn't exist or is zombie."
        return

    root_file = TFile(file_path, "READ")
    hit_tree = gDirectory.Get('hitTree')
    gROOT.cd()

    histogram = TH1D("tempHist", "tempHist", hist_dim[0], hist_dim[1], hist_dim[2])
    total_draw_string = draw_string + " >> tempHist"
    hit_tree.Draw(total_draw_string, cut_string, option_string)

    make_txt_from_hist(file_path_txt, histogram, titles)

    root_file.Close()

    return
Example #6
0
def make_txt_hist_from_root_file(file_path_txt,
                                 rid,
                                 is_parallel,
                                 draw_string,
                                 hist_dim,
                                 cut_string="",
                                 option_string="",
                                 titles=("", "", "")):
    """Given a draw/cut string, take an SRK ROOT results file and create a histogram from it."""

    if is_parallel:
        letter = 'P'
    else:
        letter = 'A'
    file_path = srkdata.srkglobal.results_dir + "Results_RID" + str(
        rid) + "_" + letter + ".root"

    if not srkmisc.file_exits_and_not_zombie(file_path):
        print file_path + " doesn't exist or is zombie."
        return

    root_file = TFile(file_path, "READ")
    hit_tree = gDirectory.Get('hitTree')
    gROOT.cd()

    histogram = TH1D("tempHist", "tempHist", hist_dim[0], hist_dim[1],
                     hist_dim[2])
    total_draw_string = draw_string + " >> tempHist"
    hit_tree.Draw(total_draw_string, cut_string, option_string)

    make_txt_from_hist(file_path_txt, histogram, titles)

    root_file.Close()

    return
Example #7
0
def calc_delta_stats_same_tracks(run_id, use_wrapping=True):
    """Calculates differences in orientation of fields when tracks are the same"""
    hit_trees = []
    root_files = []
    for is_parallel in [True, False]:
        if is_parallel:
            letter = 'P'
        else:
            letter = 'A'
        file_path = srkglobal.results_dir + "Results_RID" + str(run_id) + "_" + letter + ".root"

        if not srkmisc.file_exits_and_not_zombie(file_path):
            print file_path + " doesn't exist or is zombie."
            return {}

        root_files.append(TFile(file_path, "READ"))
        hit_trees.append(gDirectory.Get('hitTree'))

    gROOT.cd()

    delta_phi_list = []
    for i in xrange(hit_trees[0].GetEntries()):
        hit_trees[0].GetEntry(i)
        hit_trees[1].GetEntry(i)
        delta_phi = hit_trees[0].phi - hit_trees[1].phi
        delta_phi_list.append(delta_phi)
    if use_wrapping:
        delta_phi_mean = srkmisc.reduce_periodics(delta_phi_list)
    else:
        delta_phi_mean = srkmisc.careful_mean(delta_phi_list)
    delta_phi_std = srkmisc.careful_std(delta_phi_list)
    root_files[0].Close()
    root_files[1].Close()
    return [delta_phi_mean, delta_phi_std]
Example #8
0
def make_alpha_vs_phi_plot(run_id, is_parallel):
    if is_parallel:
        letter = 'P'
    else:
        letter = 'A'
    file_path = srkdata.srkglobal.results_dir + "Results_RID" + str(run_id) + "_" + letter + ".root"

    if not srkmisc.file_exits_and_not_zombie(file_path):
        print file_path + " doesn't exist or is zombie."
        return {}

    root_file = TFile(file_path, "READ")
    hit_tree = gDirectory.Get('hitTree')
    gROOT.cd()
    num_events = hit_tree.GetEntries()

    phi_list = []
    alpha_list = []
    radius = srkdata.get_data_for_rids_from_database([run_id], "ChamberRadius")[0][0]
    print radius
    for i in xrange(num_events):
        hit_tree.GetEntry(i)
        phi_list.append(hit_tree.phi)
        alpha = get_alpha_angle_2d(radius, hit_tree.pos0, hit_tree.vel0)
        alpha_list.append(alpha)
    root_file.Close()

    phi_mean = srkmisc.reduce_periodics(phi_list)

    delta_phi_list = map(lambda x: x - phi_mean, phi_list)

    plt.scatter(alpha_list, delta_phi_list)
Example #9
0
def make_alpha_vs_phi_plot(run_id, is_parallel):
    if is_parallel:
        letter = 'P'
    else:
        letter = 'A'
    file_path = srkdata.srkglobal.results_dir + "Results_RID" + str(
        run_id) + "_" + letter + ".root"

    if not srkmisc.file_exits_and_not_zombie(file_path):
        print file_path + " doesn't exist or is zombie."
        return {}

    root_file = TFile(file_path, "READ")
    hit_tree = gDirectory.Get('hitTree')
    gROOT.cd()
    num_events = hit_tree.GetEntries()

    phi_list = []
    alpha_list = []
    radius = srkdata.get_data_for_rids_from_database([run_id],
                                                     "ChamberRadius")[0][0]
    print radius
    for i in xrange(num_events):
        hit_tree.GetEntry(i)
        phi_list.append(hit_tree.phi)
        alpha = get_alpha_angle_2d(radius, hit_tree.pos0, hit_tree.vel0)
        alpha_list.append(alpha)
    root_file.Close()

    phi_mean = srkmisc.reduce_periodics(phi_list)

    delta_phi_list = map(lambda x: x - phi_mean, phi_list)

    plt.scatter(alpha_list, delta_phi_list)
Example #10
0
def get_result_data(leaf_names, run_id, is_parallel):
    """Get result data directly from root files based on leaf names"""
    if is_parallel:
        letter = 'P'
    else:
        letter = 'A'
    file_path = srkdata.srkglobal.results_dir + "Results_RID" + str(
        run_id) + "_" + letter + ".root"
    print "Opening ", file_path

    if not srkmisc.file_exits_and_not_zombie(file_path):
        print file_path + " doesn't exist or is zombie."
        return {}

    root_file = TFile(file_path, "READ")
    hit_tree = gDirectory.Get('hitTree')
    gROOT.cd()
    num_events = hit_tree.GetEntries()

    data = []
    for leaf_name in leaf_names:
        temp_data = []
        for i in xrange(num_events):
            hit_tree.GetEntry(i)
            temp_data.append(
                hit_tree.GetBranch(leaf_name).GetListOfLeaves()[0].GetValue())
        data.append(temp_data)
    root_file.Close()

    return data
Example #11
0
def calc_delta_stats_same_tracks(run_id, use_wrapping=True):
    """Calculates differences in orientation of fields when tracks are the same"""
    hit_trees = []
    root_files = []
    for is_parallel in [True, False]:
        if is_parallel:
            letter = 'P'
        else:
            letter = 'A'
        file_path = srkglobal.results_dir + "Results_RID" + str(
            run_id) + "_" + letter + ".root"

        if not srkmisc.file_exits_and_not_zombie(file_path):
            print file_path + " doesn't exist or is zombie."
            return {}

        root_files.append(TFile(file_path, "READ"))
        hit_trees.append(gDirectory.Get('hitTree'))

    gROOT.cd()

    delta_phi_list = []
    for i in xrange(hit_trees[0].GetEntries()):
        hit_trees[0].GetEntry(i)
        hit_trees[1].GetEntry(i)
        delta_phi = hit_trees[0].phi - hit_trees[1].phi
        delta_phi_list.append(delta_phi)
    if use_wrapping:
        delta_phi_mean = srkmisc.reduce_periodics(delta_phi_list)
    else:
        delta_phi_mean = srkmisc.careful_mean(delta_phi_list)
    delta_phi_std = srkmisc.careful_std(delta_phi_list)
    root_files[0].Close()
    root_files[1].Close()
    return [delta_phi_mean, delta_phi_std]
Example #12
0
def GetTriggerScalers(filename):
    inputfile = TFile.Open(filename)
    inputfile.cd("PtEMCalTriggerTask")
    gDirectory.ls()
    tasklist = gDirectory.Get("results")
    histlist = tasklist.FindObject("histosPtEMCalTriggerHistograms")
    gROOT.cd()
    triggerhist = histlist.FindObject("hEventTriggers")
    inputfile.Close()
    
    # Get number of Min. Bias counts
    mbcounts = GetCounts(triggerhist, "MinBias")
    print "MinBias counts: %d" %(mbcounts)

    triggerhist.GetAxis(0).SetRange(2,2)
    triggercounts = {}
    for trigger in triggerlookup.keys():
        if trigger == "MinBias":
            continue
        triggercounts[trigger] = GetCounts(triggerhist, trigger)
        print "Number of events for trigger %s: %d" %(trigger, triggercounts[trigger])
    
    hScalers = TH1D("triggerScalers", "trigger scalers", len(triggercounts), -0.5, len(triggercounts) - 0.5)
    counter = 1
    for trigger in triggercounts.keys():
        scaler = float(mbcounts)/float(triggercounts[trigger])
        print "Scaler for trigger %s: %f" %(trigger, scaler)
        hScalers.GetXaxis().SetBinLabel(counter, trigger)
        hScalers.SetBinContent(counter, scaler)
        counter += 1
        
    outputfile = TFile("TriggerScalers.root", "RECREATE")
    outputfile.cd()
    hScalers.Write()
    outputfile.Close()
Example #13
0
def get_result_data(leaf_names, run_id, is_parallel):
    """Get result data directly from root files based on leaf names"""
    if is_parallel:
        letter = 'P'
    else:
        letter = 'A'
    file_path = srkdata.srkglobal.results_dir + "Results_RID" + str(run_id) + "_" + letter + ".root"
    print "Opening ", file_path

    if not srkmisc.file_exits_and_not_zombie(file_path):
        print file_path + " doesn't exist or is zombie."
        return {}

    root_file = TFile(file_path, "READ")
    hit_tree = gDirectory.Get('hitTree')
    gROOT.cd()
    num_events = hit_tree.GetEntries()

    data = []
    for leaf_name in leaf_names:
        temp_data = []
        for i in xrange(num_events):
            hit_tree.GetEntry(i)
            temp_data.append(hit_tree.GetBranch(leaf_name).GetListOfLeaves()[0].GetValue())
        data.append(temp_data)
    root_file.Close()

    return data
 def __ReadFile(self, filename):
     inputfile = TFile.Open(filename)
     gROOT.cd()
     graphlist = inputfile.Get("Results%d" %(self.__limit))
     ptreachgraph = graphlist.FindObject("ptreach")
     inputfile.Close()
     return ptreachgraph
    def __init__(self, sample ,File, WS , PDF , eff_file , thqDomBinName , low,high , binCut):
        self.Cut = binCut
        self.Sample = sample
        self.File = File
        self.WS = WS
        self.PDF = PDF

        
        self.file = TFile.Open( self.File )
        self.ws = self.file.Get(self.WS)
        self.DS = self.ws.data( "%s_125_13TeV_THQLeptonicTag_50" % (self.Sample) )
        #self.file.ls()
        #print sample
        #self.file.ls()
        #self.ws.Print()
        #print self.ws, self.File, self.WS
        self.pdf = self.ws.pdf( self.PDF )
        self.norm = self.ws.function( self.PDF + "_norm" )
        self.CT = self.ws.var("CtOverCv")
        self.CT_Default = self.CT.getVal()
        
        self.CV = self.ws.var("CV")
        self.CV_Default = self.CV.getVal()
        
        self.Low = low
        self.High = high

        self.EfficiencyFile = TFile.Open( eff_file )
        gROOT.cd()
        self.EffHist = self.EfficiencyFile.Get("%(sample)s/%(bin)s/h%(sample)s_%(bin)s_eff_CtCv" % {"sample":sample , "bin":thqDomBinName}).Clone("Eff_%s" % sample )
        self.EfficiencyFile.Close()
        #self.EffHist.Print()
        self.Eff_CtCvInfo = CtCvCpInfo( "Eff_CtCvInfo_%s_%s" % (sample , thqDomBinName) )
        self.Eff_CtCvInfo.FillFrom2DHisto( self.EffHist )
Example #16
0
    def __call__(self , x , p=0):
        #x[0] = f_Neg
        #x[1] = f_0
        #x[2] = rec_gen factor
        #no parameter is needed

        LL = 0.0

        for nBin in self.CosThetaRange():
            N_BKG = self.hSum.GetBinContent(nBin)

            CosTheta = self.hSum.GetBinCenter(nBin)
            self.WF1.SetParameters(x[0] , x[1])
            N_TT = 0.0

            if self.Tree:
                # treeName = self.TreeNameFormat % {'bin':nBin}
                # tree = self.TreeFile.Get(treeName)

                # genCosTheta = array.array('d' , [Double(0.0)])
                # eventWeight = array.array('d' , [Double(0.0)])
                # lumiWeight  = array.array('d' , [Double(0.0)])

                # tree.SetBranchAddress("GenCosTheta" , genCosTheta)
                # tree.SetBranchAddress("EventWeight" , eventWeight)
                # tree.SetBranchAddress("LumiWeight" , lumiWeight)
                
                # for entry in range(0 ,  tree.GetEntries() ):
                #     tree.GetEntry(entry)

                #     evtweight = eventWeight[0]*lumiWeight[0]
                #     WeightVal = self.WF1.Eval(genCosTheta[0])

                for entry in self.TreeInfo[nBin]:

                    evtweight = entry[1]
                    WeightVal = self.WF1.Eval(entry[0])
                    
                    N_TT += (evtweight*WeightVal)

            elif not self.TwoD :
                WeightVal = self.WF1.Eval(CosTheta)
                N_TT = WeightVal*self.TTBar.GetBinContent(nBin)
            else:
                gROOT.cd()
                hGenInfo = self.TTBar.ProjectionY( '_py' , nBin , nBin  , 'o')
                hGenInfo.Multiply( self.WF1 )
                N_TT = hGenInfo.Integral()

            N_MC = N_BKG + x[2]*N_TT

            N_Data = self.hData.GetBinContent(nBin)
            if not len(self.ErrorVariations) == 0:
                variation = self.ErrorVariations[nBin]
                N_Data += variation*sqrt(N_Data)

            LL += self.DistributionFunction( N_MC , N_Data )
        
        return LL
def get_hist_from_files(directory, ):
    """
    Return a histogram energy spectrum constructed from root files in directory.
    """
    root_file_names = glob.glob('%s/*.root' % directory)
    root_file_names.sort()

    gROOT.cd()  # deal with TH1D/TFile/python scope issues!!

    name = directory.split('/')[0]
    hist = TH1D('%s' % name, '', 3000, 0, 3000)
    hist.Sumw2()
    hist.SetLineWidth(2)

    for root_file_name in root_file_names:

        root_file = TFile(root_file_name)
        tree = root_file.Get('fTree')

        hist.GetDirectory().cd()
        n_entries = tree.Draw('fTotalEnergy*1e3 >> +%s' % hist.GetName(),
                              'fTotalEnergy>0', 'goff')

        tree.GetEntry(0)
        a_max = tree.fMCRun.GetAmax()

    peak_energy = 2614.5
    if a_max == 214:
        #peak_energy = 1764.49
        peak_energy = 609.320

    peak_counts = hist.GetBinContent(hist.FindBin(peak_energy))

    roi_counts = hist.Integral(
        hist.FindBin(2039),
        hist.FindBin(2041) - 1,
    )

    ratio = roi_counts / peak_counts
    ratio_err = ratio * math.sqrt(1.0 / peak_counts + 1.0 / roi_counts)

    print '%s | %.1f-keV peak: %i +/- %.1f | roi: %i +/- %.1f | ratio: (%.2f +/- %.2f) x 10^-3' % (
        name,
        peak_energy,
        peak_counts,
        math.sqrt(peak_counts),
        roi_counts,
        math.sqrt(roi_counts),
        ratio * 1e3,
        ratio_err * 1e3,
    )

    hist.Rebin(5)
    hist.SetXTitle('Energy [keV]')
    hist.SetYTitle('Counts / %.1f keV' % hist.GetBinWidth(1))
    hist.GetYaxis().SetTitleOffset(1.2)

    return hist
Example #18
0
def ProjectDraw(var, cut, Lumi, samplelist, pd):

    plt.cfg.register(samplelist)
    groupList = plt.cfg.getGroupPlot()

    histList = OrderedDict()
    VAR = var
    # remove extra space
    CUT = ' '.join(filter(None, selection[cut].split(" ")))

    # group tag
    for igroup in groupList:
        CUT_ = CUT
        if igroup == 'BkgSum': continue
        #if igroup in [ 'Fake', 'DATA' ] : CUT_ =  CUT.replace("isbVeto && ","")

        print col.CYAN + "drawLambda::GroupTag : ", igroup + col.ENDC
        histList[igroup] = {}
        ##sample tag
        for isample in groupList[igroup]['samples']:
            print("isample : ", isample)
            histList[igroup][isample] = []
            if 'weights' in samples[isample].keys():
                for jsample in samples[isample]['weights'].keys():
                    WEIGHTS = '(%s)*(%s)' % (
                        expressAliases(samples[isample]['weight']),
                        samples[isample]['weights'][jsample])
                    #WEIGHTS = '(%s)*(%s)' %( samples[isample]['weight'] , samples[isample]['weights'][jsample] )
                    if igroup not in ['Fake', 'DATA']:
                        WEIGHTS = "%s*(%s)" % (str(
                            float(Lumi) / 1000.), WEIGHTS)
                    filelist = [
                        x for x in samples[isample]['name']
                        if os.path.basename(x).split('_', 1)[-1].replace(
                            '.root', '').split('__part')[0] == jsample
                    ]
                    files = makeVectorList(filelist)
                    df = ROOT.RDataFrame("Events", files)
                    gROOT.cd()
                    histList[igroup][isample].append(
                        makeHisto(df, VAR, CUT_, WEIGHTS, jsample))
            else:
                WEIGHTS = expressAliases(samples[isample]['weight'])
                #WEIGHTS = samples[isample]['weight']
                if igroup not in ['Fake', 'DATA']:
                    WEIGHTS = "%s*(%s)" % (str(float(Lumi) / 1000.), WEIGHTS)
                filelist = samples[isample]['name']
                files = makeVectorList(filelist)
                df = ROOT.RDataFrame("Events", files)
                gROOT.cd()
                histList[igroup][isample].append(
                    makeHisto(df, VAR, CUT_, WEIGHTS, isample))

        pass
    # apply action
    return applyAction(histList, groupList)
Example #19
0
def mybook(name, n, lo, hi):
    # This will make a AIDA histogram.
    # I don't use this because i want to use all root methods
    # on the histograms, and i also want to be able to use root_pickle
    # with them.
    #return book ("/stat/" + name, name, n, lo, hi)

    # This will make a native root histogram.
    gROOT.cd()  # otherwise, we can get hists created in input files...
    return TH1F(name, name, n, lo, hi)
Example #20
0
    def readTree(self, file_name, tree_name='tree'):
        if file_name in self.__class__._t_keeper:
            ttree = self.__class__._t_keeper[file_name]
        else:
            tfile = self.__class__._f_keeper[file_name] = TFile.Open(file_name)
            ttree = self.__class__._t_keeper[file_name] = tfile.Get(tree_name)

        gROOT.cd()

        return ttree
Example #21
0
    def unpickle(self, fileName = '', quiet=False):
        """
        Unpickle (load) a Sample class from disk into memory.

        fileName             : pickled file (path + file name)
        quite                : print debug messages ?
        """ 
        if fileName == '':
            fileName = './'+self.name+'.pickled.root'

        fileName = fileName.strip()

        self.pickleFileName = fileName
        
        theFile = TFile( fileName )
        if not theFile:
            print " ERROR loading root file"
            return

        
        res = root_pickle.load( theFile, 0)


        theList = self.listPickleVars
        
        # add variables and histograms
        for v in theList:
            if v.find("resultTObjects") != -1 and res.has_key("resultTObjects"):
                for obj in res["resultTObjects"]:
                    try:
                        res["resultTObjects"][obj]
                    except NameError:
                        print "can't access object: %s in resultTObjects dict ?!?" % obj
                        continue
                    gROOT.cd("/")
                    # print "hist = %s" % hist
                    self.resultTObjects[obj] = res["resultTObjects"][obj].Clone()
            else:
                try:
                    exec("self."+v+" = deepcopy(res[v])")
                except AttributeError:
                    if not quiet:
                        print "warning : this class has no variables with the name: %s  --> go on w/o setting this variable" % (v)
                except KeyError:
                    if not quiet:
                        print "warning : the variable %s was not found in the pickled result --> go on w/o setting this variable" % (v)
                        pass
                    pass
                pass
            pass

        del res
        theFile.Close()
        del theFile
        pass
Example #22
0
 def do_cd(self, args=''):
     """Change directory to specified directory.  See also: pathspec."""
     if args.strip() == '-':
         success = self.oldpwd.cd()
     else:
         success = self.pwd.cd(args)
     if not success:
         print('cd: {}: No such file or directory'.format(args))
     else:
         if not args.strip():
             gROOT.cd()
Example #23
0
 def do_cd(self, args=''):
     """Change directory to specified directory.  See also: pathspec."""
     if args.strip() == '-':
         success = self.oldpwd.cd()
     else:
         success = self.pwd.cd(args)
     if not success:
         print('cd: {}: No such file or directory'.format(args))
     else:
         if not args.strip():
             gROOT.cd()
Example #24
0
def read_back_profile_file(profile_file): 
    from ROOT import TFile, TIter, gROOT
    root_file = TFile(profile_file)
    list_of_keys = root_file.GetListOfKeys()
    hists = {}
    gROOT.cd()                  # clone into memory
    for key_itr in TIter(list_of_keys): 
        key_name = key_itr.GetName()
        hist = key_itr.ReadObj().Clone(str(random.random()))
        hists[key_name] = hist

    return hists
Example #25
0
def make_sz_prob_dist(run_id, is_parallel, use_wrapping=True):
    """Make a spin direction (after flip) detection probability histogram"""
    if is_parallel:
        letter = 'P'
    else:
        letter = 'A'

    file_path = srkdata.srkglobal.results_dir + "Results_RID" + str(
        run_id) + "_" + letter + ".root"

    if not srkmisc.file_exits_and_not_zombie(file_path):
        print file_path + " doesn't exist or is zombie."
        return []

    root_file = TFile(file_path, "READ")

    hit_tree = gDirectory.Get('hitTree')
    gROOT.cd()
    num_events = hit_tree.GetEntries()

    phi_list = []
    theta_list = []
    for i in xrange(num_events):
        hit_tree.GetEntry(i)
        phi_list.append(hit_tree.phi)
        theta_list.append(hit_tree.theta)
    root_file.Close()

    if use_wrapping:
        phi_mean = srkmisc.reduce_periodics(phi_list)
    else:
        phi_mean = srkmisc.careful_mean(phi_list)

    phi_std = srkmisc.careful_std(phi_list)

    theta_std = srkmisc.careful_std(theta_list)

    comb_std = np.sqrt(phi_std * phi_std + theta_std * theta_std)

    print "Combined Standard deviation: %e" % (comb_std)

    x = np.linspace(-30, 30, num=60)

    y = []
    for num_std_dev in x:
        sz_det_prob = 0
        for phi, theta in zip(phi_list, theta_list):
            sz_det_prob += calc_opposite_spin_prob(
                phi - phi_mean + comb_std * num_std_dev, theta)
        sz_det_prob /= num_events
        y.append(1 - sz_det_prob)

    return x, y
def get_hist(
    name,
    bin_width=5.0,
    max_bin=2000,
):

    gROOT.cd() # deal with TH1D/TFile/python scope issues!!
    n_bins = int(1.0*max_bin/bin_width)
    hist = TH1D('%s' % name, '', n_bins, 0, max_bin)
    hist.Sumw2()
    #print '----> making hist: %s' % hist.GetName()
    return hist
    def __init__(self , file0):
        gROOT.cd()
        self.PullF0 = file0.Get("hPullF0").Rebin(10 , "PullF0")
        self.PullFNeg = file0.Get("hPullFNeg").Rebin( 10 , "PullFNeg")
        self.PullFPos = file0.Get("hPullFPos").Rebin( 10 , "PullFPos")

        self.ResF0 = file0.Get("hResF0").Clone("ResF0")
        self.ResFNeg = file0.Get("hResFNeg").Clone("ResFNeg")
        self.ResFPos = file0.Get("hResFPos").Clone("ResFPos")

        self.NEntries = self.PullFPos.GetEntries()
        self.AllResutls = {}
Example #28
0
def createRatio(h1, h2):
    Nbins = h1.GetNbinsX()
    gROOT.cd()
    Ratio = h1.Clone("Ratio")
    hStackLast = h2.Clone("hStackLast")
    for i in range(Nbins):
        stackcontent = hStackLast.GetBinContent(i)
        stackerror = hStackLast.GetBinError(i)
        datacontent = h1.GetBinContent(i)
        dataerror = h1.GetBinError(i)
        print "stackcontent: ", stackcontent, " and data content: ", datacontent
        ratiocontent = 0
        if (datacontent != 0):
            ratiocontent = datacontent / stackcontent
        if (datacontent != 0):
            error = ratiocontent * (math.sqrt(
                math.pow((dataerror / datacontent), 2) +
                math.pow((stackerror / stackcontent), 2)))
        else:
            error = 2.07
        print "ratio content: ", ratiocontent, " and error: ", error
        Ratio.SetBinContent(i, ratiocontent)
        Ratio.SetBinError(i, error)

    Ratio.GetYaxis().SetRangeUser(0.0, 2.2)
    Ratio.SetStats(0)
    Ratio.GetYaxis().CenterTitle()
    Ratio.SetMarkerStyle(20)
    Ratio.SetMarkerSize(0.7)

    line = ROOT.TLine(40, 1., 120, 1.)
    line.SetLineStyle(8)

    Ratio.GetYaxis().SetLabelSize(0.14)
    Ratio.GetYaxis().SetTitleSize(0.12)
    Ratio.GetYaxis().SetLabelFont(42)
    Ratio.GetYaxis().SetTitleFont(42)
    Ratio.GetYaxis().SetTitleOffset(0.25)
    Ratio.GetYaxis().SetNdivisions(100)
    Ratio.GetYaxis().SetTickLength(0.05)

    Ratio.GetXaxis().SetLabelSize(0.15)
    Ratio.GetXaxis().SetTitleSize(0.12)
    Ratio.GetXaxis().SetLabelFont(42)
    Ratio.GetXaxis().SetTitleFont(42)
    Ratio.GetXaxis().SetTitleOffset(0.90)
    Ratio.GetXaxis().SetTickLength(0.05)
    #Ratio.Draw("pex0")
    #line.SetLineColor(kBlack)
    #line.Draw("same");

    return Ratio, line
Example #29
0
    def GetFunction(name ,F3 , VarToFix , Value ):
        functor = F32F2(F3 , VarToFix , Value)
        gROOT.cd()
        ret = None

        if VarToFix == 1:
            ret = TF2( name , functor , F3.GetYmin() , F3.GetYmax() , F3.GetZmin() , F3.GetZmax() , F3.GetNpar() )
        elif VarToFix == 2:
            ret = TF2( name , functor , F3.GetXmin() , F3.GetXmax() , F3.GetZmin() , F3.GetZmax() , F3.GetNpar() )
        elif VarToFix == 3:
            ret = TF2( name , functor , F3.GetXmin() , F3.GetXmax() , F3.GetYmin() , F3.GetYmax() , F3.GetNpar() )

        return [ret , functor]
Example #30
0
def make_sz_prob_dist(run_id, is_parallel, use_wrapping=True):
    """Make a spin direction (after flip) detection probability histogram"""
    if is_parallel:
        letter = 'P'
    else:
        letter = 'A'

    file_path = srkdata.srkglobal.results_dir + "Results_RID" + str(run_id) + "_" + letter + ".root"

    if not srkmisc.file_exits_and_not_zombie(file_path):
        print file_path + " doesn't exist or is zombie."
        return []

    root_file = TFile(file_path, "READ")

    hit_tree = gDirectory.Get('hitTree')
    gROOT.cd()
    num_events = hit_tree.GetEntries()

    phi_list = []
    theta_list = []
    for i in xrange(num_events):
        hit_tree.GetEntry(i)
        phi_list.append(hit_tree.phi)
        theta_list.append(hit_tree.theta)
    root_file.Close()

    if use_wrapping:
        phi_mean = srkmisc.reduce_periodics(phi_list)
    else:
        phi_mean = srkmisc.careful_mean(phi_list)

    phi_std = srkmisc.careful_std(phi_list)

    theta_std = srkmisc.careful_std(theta_list)

    comb_std = np.sqrt(phi_std * phi_std + theta_std * theta_std)

    print "Combined Standard deviation: %e" % (comb_std)

    x = np.linspace(-30, 30, num=60)

    y = []
    for num_std_dev in x:
        sz_det_prob = 0
        for phi, theta in zip(phi_list, theta_list):
            sz_det_prob += calc_opposite_spin_prob(phi - phi_mean + comb_std * num_std_dev, theta)
        sz_det_prob /= num_events
        y.append(1 - sz_det_prob)

    return x, y
Example #31
0
def createDataH1(ch, channels):
    mc2017Files = glob(
        "/data/uhussain/SkimmedZZNtuples_mc_2018-03-06_hadd/2017ZZTo4L/2018-03-06-ZZTo4L-powheg_ext1-ZZ4l2018-preselectionToQCDVeto-v1/*.root"
    )
    tree = ROOT.TChain(ch + "/ntuple")
    meta_tree = ROOT.TChain("metaInfo/metaInfo")
    for f in mc2017Files:
        tree.Add(f)
        meta_tree.Add(f)
    data_hist = "ZMass_2017_" + mcSamples[0] + "_" + ch
    #dataFile = ROOT.TFile("/data/uhussain/SkimmedZZNtuples_mc_2018-03-06_hadd/results_full/ZZTo4L-powheg_ext1.root")
    ##print dataFiles
    #tree = dataFile.Get(ch+"/ntuple")
    #meta_tree = dataFile.Get("metaInfo/metaInfo")
    data_hist = "ZMass2017_" + ch
    h1 = ROOT.TH1F(data_hist, data_hist, nbins, 40, 120)
    if (ch == "eeee" or ch == "mmmm"):
        tree.Draw(channels[ch] + ">>" + data_hist,
                  "genWeight*(nZZTightIsoElec+nZZTightIsoMu==4)")
    #Function to retrieve mZ1 in ch=eemm
    else:
        tree.Draw(
            "(( abs(e1_e2_Mass-91.1876)<abs(m1_m2_Mass-91.1876) ) ? e1_e2_Mass : m1_m2_Mass)>>"
            + data_hist, "genWeight*(nZZTightIsoElec+nZZTightIsoMu==4)")

    sumweights_hist = ROOT.TH1D("sumweights", "sumweights", 1, 0, 100)
    meta_tree.Draw("1>>sumweights", "summedWeights")
    sumweights = sumweights_hist.Integral()
    print "2017sumweights: ", sumweights

    nevents_hist = ROOT.TH1D("nevents", "nevents", 1, 0, 100)
    meta_tree.Draw("1>>nevents", "nevents")
    nevents = nevents_hist.Integral()
    print "2017nevents: ", nevents
    xsec = 1.256 * 1000
    h1.Scale((xsec * lumi) / sumweights)
    h1.SetLineColor(ROOT.kRed)
    h1.SetLineWidth(2)
    h1.SetStats(0)
    h1.SetTitle("")
    h1.GetXaxis().SetTitle("")
    h1.GetXaxis().SetTickLength(0)
    h1.GetXaxis().SetLabelOffset(999)
    h1.GetYaxis().SetTitle("")
    h1.GetYaxis().SetTickLength(0)
    h1.GetYaxis().SetLabelOffset(999)
    print h1.Integral()
    gROOT.cd()
    h2017 = h1.Clone()
    return h2017
Example #32
0
    def readTree(self, file_name, tree_name='tree', verbose=False):
        '''Cache files/trees'''
        if file_name in self.__class__._t_keeper:
            ttree = self.__class__._t_keeper[file_name]
            if verbose:
                print 'got cached tree', ttree
        else:
            tfile = self.__class__._f_keeper[file_name] = TFile.Open(file_name)
            ttree = self.__class__._t_keeper[file_name] = tfile.Get(tree_name)
            if verbose:
                print 'read tree', ttree, 'from file', file_name

        gROOT.cd()

        return ttree
Example #33
0
    def GetFunction(name , justOne = -2):
        functor = dGammaFunctin(justOne)
        gROOT.cd()
        ret = TF1(name , functor , -1.0 , 1.0 , 3)

        ret.SetParName(0 , "FNeg" );
        ret.SetParName(1 , "F0" );
        ret.SetParName(2 , "NormaFactor" );
            
        ret.SetParLimits(0 , 0.0 , 1.0);
        ret.SetParLimits(1 , 0.0 , 1.0);
            
        ret.SetParameters( 0.3 , 0.7 , 1.0 );
            
        return ret;
Example #34
0
 def BuildFromRootFile(filename, name):
     result = ResultData(name)
     inputfile = TFile.Open(filename)
     gROOT.cd()
     keyIter = TIter(inputfile.GetListOfKeys())
     key = keyIter.Next()
     while key:
         if key.GetName() == "MCTruth":
             result.SetMCTruth(ParticleTHnSparse(key.ReadObj()))
         else:
             result.SetData(key.GetName(), DataSet.BuildFromRootPrimitive(key.ReadObj()))
         key = keyIter.Next()
     inputfile.Close()
     print "Results successfully reconstructed from file %s" %(filename)
     #result.Print()
     return result
Example #35
0
 def BuildFromRootFile(filename, name):
     result = ResultData(name)
     inputfile = TFile.Open(filename)
     gROOT.cd()
     keyIter = TIter(inputfile.GetListOfKeys())
     key = keyIter.Next()
     while key:
         if key.GetName() == "MCTruth":
             result.SetMCTruth(ParticleTHnSparse(key.ReadObj()))
         else:
             result.SetData(key.GetName(), DataSet.BuildFromRootPrimitive(key.ReadObj()))
         key = keyIter.Next()
     inputfile.Close()
     print "Results successfully reconstructed from file %s" %(filename)
     #result.Print()
     return result
Example #36
0
def createMCStack(ch,channels):
    mcSamples = {"DYJetsToLL_M10to50":18610,"DYJetsToLLM-50_ext1":6104,"ggHZZ_ext1":0.01218,"GluGluZZTo2e2mu":0.00319,
            "GluGluZZTo2e2tau":0.00319,"GluGluZZTo2mu2tau": 0.00319,"GluGluZZTo4e":0.00159,"GluGluZZTo4mu":0.00159,
            "ttH_HToZZ_4L_ext1":0.000393,"TTJets-amcatnlo":815.96,"TTTo2L2Nu-powheg":87.31,"WminusHToZZ_ext1":0.000147,
            "WplusHToZZ_ext1":0.000232,"WZTo3LNu":4.430,"ZHToZZ_4L":0.000668,"ZZTo4L-powheg_ext1":1.256} 
    #channels = ["eeee/ntuple","eemm/ntuple", "eeee/ntuple"]  
    MCStack = ROOT.THStack("stack", "stack")
    ROOT.SetOwnership(MCStack, False)
    for mc_Sample in mcSamples.keys():
        #print "mcSample is: ", mc_Sample
        mcPath = Date+'-'+mc_Sample+'-'+analysis+'-'+selection
        mcDir = os.path.join(Dir,mcPath)
        print 'mcDir: ',mcDir
        mcFiles = glob(mcDir+"/*.root")
        MCchain = ROOT.TChain(ch+"/ntuple")
        MetaChain = ROOT.TChain("metaInfo/metaInfo")
        for g in mcFiles:
            MCchain.Add(g)
            MetaChain.Add(g)
        hist_name = "ZMass"+mc_Sample+ch
        hist = ROOT.TH1F(hist_name, hist_name, nbins, 40, 120)
        #ROOT.SetOwnership(hist, False)
        if (ch=="eeee" or ch=="mmmm"):
            MCchain.Draw(channels[ch]+">>"+hist_name,"nZZTightIsoElec+nZZTightIsoMu==4")
        #Function to retrieve mZ1 in ch=eemm
        else:
            MCchain.Draw("(( abs(e1_e2_Mass-91.1876)<abs(m1_m2_Mass-91.1876) ) ? e1_e2_Mass : m1_m2_Mass)>>"+hist_name, "nZZTightIsoElec+nZZTightIsoMu==4")
        sumweights_hist = ROOT.TH1D("sumweights", "sumweights", 1,0,100)
        MetaChain.Draw("1>>sumweights","summedWeights")
        sumweights = sumweights_hist.Integral()
        xsec = mcSamples[mc_Sample]*1000
        print "sumweights: ",sumweights
        hist.Scale((xsec*lumi)/sumweights)
        #hist.Scale(xsec*lumi)
        hist.SetFillColor(ROOT.kBlue)
        gROOT.cd()
        hnew = hist.Clone()
        #if MCStack.GetHists():
        #    print "Before length is", len(MCStack.GetHists())
        MCStack.Add(hnew)
        #print "length is", len(MCStack.GetHists())
        #print hist.Integral()
        
    #ROOT.SetOwnership(MCStack, False)
    for i in MCStack.GetHists(): print "Name is", i.GetName(), "Integral is", i.Integral()
    return MCStack
Example #37
0
File: rdir.py Project: suvayu/rplot
    def get_dir(self, path=None):
        """Return directory from path.

        Doesn't check for non-directory.  It's the caller's
        responsibility.

        """
        if not path:
            return gDirectory.GetDirectory('')
        else:
            path = pathspec(path)
            with savepwd():
                if path.rfile:  # need to change to correct file first
                    if path.rfile not in [f.GetName() for f in self.files]:
                        # opening a file changes dir to the new file
                        self.files += [ROOT.TFile.Open(path.rfile, 'read')]
                    else:
                        gROOT.cd('{}:'.format(path.rfile))
                return gDirectory.GetDirectory(path.rpath)
Example #38
0
File: rdir.py Project: suvayu/rplot
    def get_dir(self, path=None):
        """Return directory from path.

        Doesn't check for non-directory.  It's the caller's
        responsibility.

        """
        if not path:
            return gDirectory.GetDirectory('')
        else:
            path = pathspec(path)
            with savepwd():
                if path.rfile:  # need to change to correct file first
                    if path.rfile not in [f.GetName() for f in self.files]:
                        # opening a file changes dir to the new file
                        self.files += [ROOT.TFile.Open(path.rfile, 'read')]
                    else:
                        gROOT.cd('{}:'.format(path.rfile))
                return gDirectory.GetDirectory(path.rpath)
def makeTrajPlot():
    gStyle.SetCanvasPreferGL(kTRUE)
    gROOT.cd()
    if not h.has_key("world"):
        h["world"] = TH3F("world", "world", 100, -5.0, 5.0, 100, -5.0, 5.0, 100, -50.0, 51.0)
        h["world2d"] = TH2F("world2d", "world2d", 100, -5.0, 5.0, 100, -50.0, 51.0)
    h["world"].GetXaxis().SetTitle("x-axis")
    h["world"].GetYaxis().SetTitle("y-axis")
    h["world"].GetZaxis().SetTitle("beam")
    h["world"].SetStats(0)
    h["trajectories"].cd(1)
    h["world"].Draw("gl")
    h["trajectories"].cd(2)
    h["world2d"].Draw()
    h["world_leg"] = TLegend(0.38, 0.62, 0.62, 0.89)
    for E in range(25, 425, 50):
        makeEntryLists(E - 25, E + 25, 3)
    h["trajectories"].cd(2)
    h["world_leg"].Draw()
Example #40
0
def main():
    ''' please make sure the script is running on ROOT>=6 , otherwise you need to add SumW2 when you declare a histograms '''
    for region in Regions:
        for POI in POIs:
            dirOfRootplas = DirOfRootplas + POI + "/"

            for channel in channels:
                # select which samples to loop over for each channel
                if "mm" in channel: samplesToLoop = samplesMM
                elif "ee" in channel: samplesToLoop = samplesEE
                else: samplesToLoop = samples

                samplesToUse = samplesToLoop[:]

                # create a log file to save the output
                SystLogName = dirOfRootplas + Prefix + region + "_" + channel + "_TableOfSystematics_" + Nuisance + ".txt"
                SystLogFile = file(SystLogName, "w")

                # open and read root file
                filename = Prefix + region + "_" + channel + Postfix
                inputfile = read_rootfile(filename, dirOfRootplas)
                gROOT.cd()

                print samplesToLoop
                for sample in samplesToLoop:
                    hist = inputfile.Get(sample)
                    hist_error = ROOT.Double(0)
                    hist_integral = hist.IntegralAndError(
                        0, hist.GetNbinsX(), hist_error)
                    Rates[sample] = round(hist_integral, 3)
                    Uncertainties[sample] = round(hist_error, 3)

                # write begin parts of datacards
                print >> SystLogFile, "In Channel " + channel + ":"
                begin = "Samples Nominal Stats"
                for np in Nuisances[Nuisance]:
                    begin += "  " + np
                print >> SystLogFile, begin

                # write each systematics
                writecard(samplesToUse, Nuisance, channel, inputfile,
                          SystLogFile)
Example #41
0
def calc_step_tree(file_path, inp_periodic_stop_time):
    """Calculate time and spin detection probabilities from step tree."""
    if not srkmisc.file_exits_and_not_zombie(file_path):
        print file_path + " doesn't exist or is zombie."
        return []
    print "Calculating step tree for {}".format(file_path)
    root_file = TFile(file_path, "READ")
    step_tree = gDirectory.Get('stepTree')
    hit_tree = gDirectory.Get('hitTree')
    gROOT.cd()
    user_info_list = hit_tree.GetUserInfo()

    periodic_stop_time = inp_periodic_stop_time
    for i in xrange(user_info_list.GetEntries()):
        par = user_info_list.At(i)
        if par.GetName() == 'PeriodicStopTime':
            periodic_stop_time = float(par.GetTitle())
            break

    num_events = hit_tree.GetEntries()
    num_steps_per_event = step_tree.GetEntries() / num_events

    time_data = np.zeros(num_steps_per_event)
    sx_prob_data = np.zeros(num_steps_per_event)

    # Set times
    for j in xrange(num_steps_per_event):
        time_data[j] = ((j + 1) * periodic_stop_time)

    # Add data
    for i in xrange(num_events):
        if i % 100 == 0:
            print "Stepping for Event {} in {}".format(i, file_path)
        for j in xrange(num_steps_per_event):
            step_tree.GetEntry(i * num_steps_per_event + j)
            sx_prob_data[j] += step_tree.sxProb

    # Normalize
    sx_prob_data /= float(num_events)

    root_file.Close()
    return zip(time_data, sx_prob_data)
Example #42
0
def calc_step_tree(file_path, inp_periodic_stop_time):
    """Calculate time and spin detection probabilities from step tree."""
    if not srkmisc.file_exits_and_not_zombie(file_path):
        print file_path + " doesn't exist or is zombie."
        return []
    print "Calculating step tree for {}".format(file_path)
    root_file = TFile(file_path, "READ")
    step_tree = gDirectory.Get('stepTree')
    hit_tree = gDirectory.Get('hitTree')
    gROOT.cd()
    user_info_list = hit_tree.GetUserInfo()

    periodic_stop_time = inp_periodic_stop_time
    for i in xrange(user_info_list.GetEntries()):
        par = user_info_list.At(i)
        if par.GetName() == 'PeriodicStopTime':
            periodic_stop_time = float(par.GetTitle())
            break

    num_events = hit_tree.GetEntries()
    num_steps_per_event = step_tree.GetEntries() / num_events

    time_data = np.zeros(num_steps_per_event)
    sx_prob_data = np.zeros(num_steps_per_event)

    # Set times
    for j in xrange(num_steps_per_event):
        time_data[j] = ((j + 1) * periodic_stop_time)

    # Add data
    for i in xrange(num_events):
        if i % 100 == 0:
            print "Stepping for Event {} in {}".format(i, file_path)
        for j in xrange(num_steps_per_event):
            step_tree.GetEntry(i * num_steps_per_event + j)
            sx_prob_data[j] += step_tree.sxProb

    # Normalize
    sx_prob_data /= float(num_events)

    root_file.Close()
    return zip(time_data, sx_prob_data)
Example #43
0
    def __init__(self, Samples , directory):
        self.PropName = directory.GetName()
        
        dircontents = directory.GetListOfKeys()
        firsthisto = directory.Get( dircontents.At(0).GetName() )
        #firsthisto.Print("ALL")
        self.ForLegend = {}
        self.XSections = {}
        self.AllSampleHistos = {}

        for sample in Samples:
            if sample.IsData:
                self.DataSName = sample.HistoCat


            # h_in_dir = directory.Get( "%s_%s" % ( self.PropName , sample.Name ) )
            # if h_in_dir :
            #     h_in_dir.reset()
            #     setattr( self , sample.Name , h_in_dir )
            # else:
            gROOT.cd()
            hnew = firsthisto.Clone("%s_%s" % ( self.PropName , sample.Name ) )
            hnew.Reset()
            setattr( self , sample.Name , hnew )
                
            hhh = getattr( self , sample.Name )
            hhh.SetLineColor( 1 )
            hhh.SetLineWidth( 2 )
            if not sample.IsData :
                hhh.SetFillColor( sample.Color )
                hhh.SetFillStyle( 1001 )
            else:
                hhh.SetStats(0)

            self.AllSampleHistos[sample.Name] = hhh    
                
            if( self.ForLegend.get(sample.HistoCat) ):
                self.ForLegend[sample.HistoCat].append( sample.Name )
            else:
                self.ForLegend[sample.HistoCat] = [ sample.Name ]

            self.XSections[sample.Name] = sample.XSection
Example #44
0
def makeTrajPlot():
 gStyle.SetCanvasPreferGL(kTRUE)
 bookCanvas(h,key='trajectories',title='trajectories of surviving muons',nx=1600,ny=800,cx=2,cy=1)
 gROOT.cd()
 if not h.has_key('world'): 
   h['world']   = TH3F('world','world',100,-5.,5.,100,-5.,5.,100,-50.,51.)
   h['world2d'] = TH2F('world2d','world2d',100,-5.,5.,100,-50.,51.)
 h['world'].GetXaxis().SetTitle('x-axis')
 h['world'].GetYaxis().SetTitle('y-axis')
 h['world'].GetZaxis().SetTitle('beam')
 h['world'].SetStats(0)
 h['trajectories'].cd(1)
 h['world'].Draw('gl')
 h['trajectories'].cd(2)
 h['world2d'].Draw()
 h['world_leg'] = TLegend(0.38,0.62,0.62,0.89)
 for E in range(50,450,50):
  makeEntryLists(E,3)
 h['trajectories'].cd(2)
 h['world_leg'].Draw()
Example #45
0
def ReduceFile(inputRootFile):
    from ROOT import TTree, TFile, gROOT
    print 'Processing ', file 
    
    outRootFile = '' + inputRootFile
    strreplace = ['RD_', '_CMSSW532', '_private']
    for str in strreplace:  
        outRootFile = outRootFile.replace(str, '')

    fout = TFile(outRootFile,"recreate")
    f = TFile(dir + inputRootFile,"read")
    tree = f.Get("WJet")
    tree.SetBranchStatus('*', 0)
    for name in Branches: tree.SetBranchStatus(name, 1)
    
    gROOT.cd()
    tree2 = tree.CopyTree(cuts)
    fout.cd()
    tree2.Write()
    fout.Close()
Example #46
0
    def readTree(self, file_name, tree_name='tree', verbose=False, friend_func=None):
        '''Cache files/trees'''
        if file_name in self.__class__._t_keeper:
            ttree = self.__class__._t_keeper[file_name]
            if verbose:
                print 'got cached tree', ttree
        else:
            tfile = self.__class__._f_keeper[file_name] = TFile.Open(file_name)
            ttree = self.__class__._t_keeper[file_name] = tfile.Get(tree_name)
            if verbose:
                print 'read tree', ttree, 'from file', file_name

        if friend_func:
            file_name = friend_func(file_name)
            friend_tree = self.readTree(file_name, tree_name, verbose)
            ttree.AddFriend(friend_tree)

        gROOT.cd()

        return ttree
Example #47
0
 def makeCovariancePlot(self, varList1, varList2, cut=""):
     fIn = TFile(self.sigFilePath)
     t = fIn.Get(self.treeName)
     gROOT.cd()
     c = Covariator(t)
     nVars1 = len(varList1)
     isSymmetric = False
     if varList2 == None:
         varList2 = varList1
         isSymmetric = True
     nVars2 = len(varList2)
     hplot = TH2D('cov' + str(Plotter.counter),
                  'cov' + str(Plotter.counter), nVars1, 0, nVars1, nVars2,
                  0, nVars2)
     Plotter.counter += 1
     xaxis = hplot.GetXaxis()
     yaxis = hplot.GetYaxis()
     matrix = empty([nVars1, nVars2])
     if isSymmetric:
         for iV in xrange(nVars1):
             xaxis.SetBinLabel(iV + 1, varList1[iV])
             yaxis.SetBinLabel(iV + 1, varList1[iV])
             for jV in xrange(iV + 1):
                 varA = varList1[iV]
                 varB = varList1[jV]
                 val = c.covSuperFast(varA, varB, cut)
                 hplot.SetBinContent(jV + 1, iV + 1, val)
                 hplot.SetBinContent(iV + 1, jV + 1, val)
                 matrix[iV, jV] = val
                 matrix[jV, iV] = val
     else:
         for iV in xrange(nVars1):
             xaxis.SetBinLabel(iV + 1, varList1[iV])
             for jV in xrange(nVars2):
                 yaxis.SetBinLabel(jV + 1, varList2[jV])
                 varA = varList1[iV]
                 varB = varList2[jV]
                 val = c.covSuperFast(varA, varB, cut)
                 hplot.SetBinContent(iV + 1, jV + 1, val)
                 matrix[iV, jV] = val
     return hplot, matrix
Example #48
0
    def __init__(self, Samples, directory):
        self.PropName = directory.GetName()

        dircontents = directory.GetListOfKeys()
        firsthisto = directory.Get(dircontents.At(0).GetName())
        #firsthisto.Print("ALL")
        self.ForLegend = {}
        self.XSections = {}
        self.AllSampleHistos = {}

        for sample in Samples:
            if sample.IsData:
                self.DataSName = sample.HistoCat

            # h_in_dir = directory.Get( "%s_%s" % ( self.PropName , sample.Name ) )
            # if h_in_dir :
            #     h_in_dir.reset()
            #     setattr( self , sample.Name , h_in_dir )
            # else:
            gROOT.cd()
            hnew = firsthisto.Clone("%s_%s" % (self.PropName, sample.Name))
            hnew.Reset()
            setattr(self, sample.Name, hnew)

            hhh = getattr(self, sample.Name)
            hhh.SetLineColor(1)
            hhh.SetLineWidth(2)
            if not sample.IsData:
                hhh.SetFillColor(sample.Color)
                hhh.SetFillStyle(1001)
            else:
                hhh.SetStats(0)

            self.AllSampleHistos[sample.Name] = hhh

            if (self.ForLegend.get(sample.HistoCat)):
                self.ForLegend[sample.HistoCat].append(sample.Name)
            else:
                self.ForLegend[sample.HistoCat] = [sample.Name]

            self.XSections[sample.Name] = sample.XSection
Example #49
0
def processSpectrum(doc_list, input_directory='..', type='pulser',\
                    hist_name='hist', \
		    energy_scale='*1', cuts='', use_baseline_cut=False, opposite_baseline_cut=False):
    import re
    from array import array
    from ROOT import TFile, TH1D, TTree, TObject, TEventList, gROOT
    from soudan_database import SoudanServer
    
    number_of_sigma = 3
    hist = TH1D(hist_name, hist_name, 8192, 0, eval("8192%s" % energy_scale))
    hist.GetXaxis().SetTitle("Energy (keV)")
    hist.GetYaxis().SetTitle("Counts")

    server = SoudanServer()

    for run_number in doc_list:
        doc = server.get_run(run_number.id) 
        try:
            string_of_file_name = eval('doc.output_data_file_tier_3.%s.lfn' % type)
        except:
            print "Incorrect type"
            continue
        print doc._get_id()
        open_file = TFile("%s/%s" % (input_directory,string_of_file_name))
        main_tree = open_file.Get("wf_analysis") 
        coinc_tree = open_file.Get("event_coincidence") 
        main_tree.AddFriend(coinc_tree)
        real_cuts = cuts
        if use_baseline_cut:
            not_string = ""
            if opposite_baseline_cut:
                not_string = "!"
            additional_cuts = " && %s(abs(fitConstant-%f) <= %f*%f) " % \
                 (not_string, doc.baseline_dict.average_fit_constant,\
                   number_of_sigma, doc.baseline_dict.average_fit_rms)
            real_cuts += additional_cuts 
        gROOT.cd()
        main_tree.Draw("-energy%s>> +%s" % (energy_scale, hist.GetName()),real_cuts, "goff")
        open_file.Close()

    return hist
Example #50
0
    def makeRootDataFrameFromTree(self, tree_file_name, tree_name='tree', verbose=False, friend_name='ML', friend_file_name=None):
        '''Cache files/trees'''

        ttree = self.readTree(tree_file_name, tree_name, verbose)

        if verbose:
            print ('read dataframe', dataframe, 'from file', tree_file_name)

        if friend_file_name:
            ttree.AddFriend(friend_name + '=tree',friend_file_name)
            #VALIDATE#
            validate1 = ttree.GetEntries('l2_pt - %s.l2_pt'%friend_name)
            validate2 = ttree.GetEntries('event - %s.event'%friend_name)

            if not validate1+validate2 == 0: print ('\n\tERROR: FRIEND TREE NOT ALIGNED, FAKERATE USELESS', m, n)

        gROOT.cd()
        # dataframe = RDataFrame(tree_name,tree_file_name)
        dataframe = RDataFrame(ttree)

        return dataframe
Example #51
0
def createMCStack(ch,channels):
    mcSamples = {"ggHZZ":0.01218,"GluGluZZTo2e2mu":0.00319,
            "GluGluZZTo2e2tau":0.00319,"GluGluZZTo2mu2tau": 0.00319,"GluGluZZTo4e":0.00159,"GluGluZZTo4mu":0.00159,
            "TTTo2L2Nu-powheg":87.31,"ZZTo4L-powheg_ext1":1.256} 
    #channels = ["eeee/ntuple","eemm/ntuple", "eeee/ntuple"]  
    MCStack = ROOT.THStack("stack", "stack")
    ROOT.SetOwnership(MCStack, False)
    for mc_Sample in mcSamples.keys():
        #print "mcSample is: ", mc_Sample
        mcFile = os.path.join(mcDir,mc_Sample)
        mcFile = mcFile+'.root'
        f1 = ROOT.TFile(mcFile)
        tree = f1.Get(ch+"/ntuple")
        meta_tree = f1.Get("metaInfo/metaInfo")
        hist_name = "ZMass"+mc_Sample+ch
        hist = ROOT.TH1F(hist_name, hist_name, nbins, 40, 120)
        #ROOT.SetOwnership(hist, False)
        if (ch=="eeee" or ch=="mmmm"):
            tree.Draw(channels[ch]+">>"+hist_name,"nZZTightIsoElec+nZZTightIsoMu==4")
        #Function to retrieve mZ1 in ch=eemm
        else:
            tree.Draw("(( abs(e1_e2_Mass-91.1876)<abs(m1_m2_Mass-91.1876) ) ? e1_e2_Mass : m1_m2_Mass)>>"+hist_name, "nZZTightIsoElec+nZZTightIsoMu==4")
        sumweights_hist = ROOT.TH1D("sumweights", "sumweights", 1,0,100)
        meta_tree.Draw("1>>sumweights","summedWeights")
        sumweights = sumweights_hist.Integral()
        xsec = mcSamples[mc_Sample]*1000
        hist.Scale(xsec*lumi/sumweights)
        #hist.Scale(xsec*lumi)
        hist.SetFillColor(ROOT.kBlue)
        gROOT.cd()
        hnew = hist.Clone()
        #if MCStack.GetHists():
        #    print "Before length is", len(MCStack.GetHists())
        MCStack.Add(hnew)
        #print "length is", len(MCStack.GetHists())
        #print hist.Integral()
        
    #ROOT.SetOwnership(MCStack, False)
    for i in MCStack.GetHists(): print "Name is", i.GetName(), "Integral is", i.Integral()
    return MCStack
Example #52
0
def create2DHistograms(hist_cfg):
    histos = HistoStack(hist_cfg.name)
    for cfg in hist_cfg.cfgs:
        # First check whether it's a sub-histo or not
        if isinstance(cfg, HistogramCfg):
            histos.Add(cfg.name, createHistograms(cfg).Sum())
        elif isinstance(cfg, BasicHistogramCfg):
            print "Opening file", cfg.histo_file_name
            histo_file = TFile.Open(cfg.histo_file_name)
            print "Retrieving histo", cfg.histo_name
            hist = histo_file.Get(cfg.histo_name)
            hist.__class__ = TH2F
            hist.SetDirectory(0)
            hist.SetName(cfg.name + '_' + hist.GetName())
            histo_file.Close()
            gROOT.cd()
            #
            hist.Scale(cfg.scale)
            if not cfg.is_data:
                hist.Scale(hist_cfg.lumi * cfg.xsec / cfg.sumweights)
            histos.Add(cfg.name, hist)
    return histos
Example #53
0
def morphAllLambda(basisFile, lambdaZ, dKappaGamma, morphedHists, debug=False):
    fbasis = TFile(basisFile)
    gROOT.cd()

    for i in range(1, 4):
        h0Name = 'signal_lambdaZ_%.2f_deltaKappaGamma_%.1f' % (lambdaZ,
                                                               dKappaGamma)
        h1Name = 'signal_lambdaZ_%.2f_deltaKappaGamma_%.1f' % (lambdaZ + 0.04,
                                                               dKappaGamma)
        morphName = 'signal_lambdaZ_%.2f_deltaKappaGamma_%.2f' % (
            lambdaZ + 0.01 * i, dKappaGamma)

        hist0 = fbasis.Get(h0Name)
        hist1 = fbasis.Get(h1Name)
        if i == lambdaZ:
            morphedHists.append(hist0)
            morphedHists.append(hist1)

        gROOT.cd()
        morphedhist = morph(hist0, hist1, lambdaZ, lambdaZ + 0.04,
                            lambdaZ + 0.01 * i, debug)
        morphedhist.SetName(morphName)
        morphedHists.append(morphedhist)

        if debug:
            hist0.SetLineColor(kBlue)
            hist1.SetLineColor(kRed)
            if mHbasis < mHmorph:
                hist0.Draw('hist')
                hist1.Draw('histsame')
            else:
                hist1.Draw('hist')
                hist0.Draw('histsame')

            morphedHists[-1].SetLineStyle(9)
            morphedHists[-1].Draw('histsame')
            gPad.Update()
            gPad.WaitPrimitive()
def morphAllLambda(basisFile, lambdaZ, dKappaGamma, morphedHists, debug = False):
    fbasis = TFile(basisFile)
    gROOT.cd()
    
    
    for i in range(1,4): 
        h0Name = 'signal_lambdaZ_%.2f_deltaKappaGamma_%.1f' % (lambdaZ, dKappaGamma)
        h1Name = 'signal_lambdaZ_%.2f_deltaKappaGamma_%.1f' % (lambdaZ+0.04, dKappaGamma)
        morphName = 'signal_lambdaZ_%.2f_deltaKappaGamma_%.2f' % (lambdaZ+0.01*i, dKappaGamma)
        

        hist0 = fbasis.Get(h0Name)
        hist1 = fbasis.Get(h1Name)
        if i==lambdaZ:
            morphedHists.append(hist0)
            morphedHists.append(hist1)            

        
        gROOT.cd()
        morphedhist = morph(hist0, hist1, lambdaZ, lambdaZ+0.04, 
                                  lambdaZ+0.01*i, debug)
        morphedhist.SetName(morphName)
        morphedHists.append(morphedhist)

        if debug:
            hist0.SetLineColor(kBlue)
            hist1.SetLineColor(kRed)
            if mHbasis < mHmorph:
                hist0.Draw('hist')
                hist1.Draw('histsame')
            else:
                hist1.Draw('hist')
                hist0.Draw('histsame')                
                
            morphedHists[-1].SetLineStyle(9)
            morphedHists[-1].Draw('histsame')
            gPad.Update()
            gPad.WaitPrimitive()
Example #55
0
def DrawHiggsMass(inputRootFile, scale = 1.0):
 
    f = TFile('data/' + inputRootFile,"read")
    tree = f.Get("WJet")
    gROOT.cd()

    histName = inputRootFile.replace('.root','')
    hist = TH1D(histName, '', 20, 100., 900.)
    xaxis = hist.GetXaxis()
    yaxis = hist.GetYaxis()
    xaxis.SetTitle('Higgs mass [GeV]')
    yaxis.SetTitle('Events / 40 GeV')

    xaxis.SetLabelSize(0.05)
    xaxis.SetTitleSize(0.05)
    xaxis.SetNdivisions(505)
    yaxis.SetLabelSize(0.05)
    yaxis.SetTitleSize(0.05)
    yaxis.SetTitleOffset(1.6)
    yaxis.SetNoExponent()
 
    tree.Draw("fit_mlvjj>>"+histName, cuts, "goff")
    hist.Scale(scale)
    return hist
def sobWeightedPlot(fileName, datasetName, channel, cat, log, mass, tanb):
    c = TCanvas(fileName, '', 600, 600)
    c.cd()
    if log: c.SetLogy(1)

    f = TFile('Plot_'+fileName+'.root')
    gROOT.cd()

    samples = ['ggH', 'Ztt', 'signal', 'data_obs', 'ttbar', 'EWK', 'Fakes', 'ggH_hww']

    if fileName.find('emu') != -1 and isSM:
        samples.append('ggH_hww')

    histDict = {}
    for sample in samples:
        histDict[sample] = tfileGet(f, sample)
        if not histDict[sample]:
            print 'Missing histogram', sample, 'in file', 'Plot_'+fileName+'.root'

    xminInset = 60 # 0
    xmaxInset = 180 # 340 (for full range)

    if tanb > 0:
        xminInset = mass - 100
        xmaxInset = mass + 100

    ztt = histDict['Ztt']
    ggH = histDict['ggH']
    data = histDict['data_obs']
    signal = histDict['signal']
    ggH_hww = histDict['ggH_hww']
    tt = histDict['ttbar']
    ewk = histDict['EWK']
    fakes = histDict['Fakes']

    ztt.GetYaxis().SetRangeUser(0., 1.3*findMaxY(data, 0))
    if log: 
        ztt.GetYaxis().SetRangeUser(0.001, 50.*findMaxY(data, 0))

    ztt.GetXaxis().SetTitle('#bf{m_{#tau#tau}  [GeV]}')
    ztt.GetYaxis().SetTitle('#bf{S/B Weighted dN/dm_{#tau#tau} [1/GeV]}')
    if tanb > 0. and not log:
        ztt.GetXaxis().SetRangeUser(0., mass+200.)

    ztt.SetTitleOffset(1.3, 'Y')
    ztt.SetTitleOffset(1., 'X')
    ztt.SetNdivisions(505)

    for b in range(0, signal.GetNbinsX()+2):
        if signal.GetBinCenter(b) < xminInset or xmaxInset < signal.GetBinCenter(b):
            signal.SetBinContent(b, 0.)
            signal.SetBinError(b, 0.)

    signal.SetName('sig')
    signal.SetFillStyle(3353) # 1001=solid , 3004,3005=diagonal
    signal.SetFillColor(2)
    signal.SetLineColor(2)
    signal.SetLineStyle(1)
    signal.SetLineWidth(0)

    ggH.SetBinContent(0,0) # remove red line on top of y axis in plot
    ggH.SetBinContent(ggH.GetNbinsX()+1,0)
    ggH.SetBinError(0,0)
    ggH.SetBinError(ggH.GetNbinsX()+1,0)
    ggH.SetName('ggH')
    ggH.SetFillStyle(3353) # 1001=solid , 3004,3005=diagonal
    ggH.SetFillColor(2)
    ggH.SetLineColor(2)
    ggH.SetLineStyle(1)
    ggH.SetLineWidth(0)

    if ggH_hww:
        errorBand = ggH_hww.Clone("errorBand")
    else:
        errorBand = ztt.Clone("errorBand")
    errorBand.SetMarkerSize(0)
    errorBand.SetFillColor(1)
    errorBand.SetFillStyle(3013)
    errorBand.SetLineWidth(1)

    legend = TLegend()
    mssmLabel = ''
    if tanb > 0:
        mssmLabel = "tan#beta={tanb}".format(tanb=tanb)
    higgsLabel = "H(125 GeV)#rightarrow#tau#tau"
    if tanb > 0:
        higgsLabel="H(125 GeV)#rightarrow#tau#tau"

    legend.SetFillStyle(0)
    legend.SetFillColor(0)
    legend.SetBorderSize(0)
    legend.AddEntry(ggH,higgsLabel,"F")
    if tanb>0: 
        legend.AddEntry(TObject(0), mssmLabel, "")
    legend.AddEntry(data,"observed", "LP")
    if ggH_hww:
        legend.AddEntry(ggH_hww, "H(125 GeV)#rightarrowWW", "F")
    legend.AddEntry(ztt, "Z#rightarrow#tau#tau","F")
    legend.AddEntry(tt, "t#bar{t}","F")
    legend.AddEntry(ewk, "electroweak","F")
    legend.AddEntry(fakes, "QCD","F")

    legend.SetX1NDC(0.63)
    legend.SetX2NDC(1.05)
    legend.SetY1NDC(0.27)
    legend.SetY2NDC(0.48)
    if log:
        legend.SetX1NDC(0.18)
        legend.SetX2NDC(0.60)
        legend.SetY1NDC(0.17)
        legend.SetY2NDC(0.38)
    
    legend.SetTextSize(.028)
    legend.SetTextAlign(12)

    if ggH_hww:
        dataDiff = diffPlot(data, ggH_hww, 1)
        errBand=getErrorBand(ggH_hww)
    else:
        dataDiff = diffPlot(data, ztt, 1)
        errBand=getErrorBand(ztt)

    errBand.SetFillStyle(3013) # 1001=solid , 3004,3005=diagonal, 3013=hatched official for H.tau tau
    errBand.SetFillColor(1)
    errBand.SetLineStyle(1)
    errBand.SetLineColor(1)
    errBand.SetLineWidth(1)

    errBandFrame = TH1F('errBandFrame', '', int((xmaxInset-xminInset)/dataDiff.GetBinWidth(1)),xminInset,xmaxInset)
    

    errBandFrame.GetYaxis().SetTitle("")
    errBandFrame.GetYaxis().SetRangeUser(-1.1*findMinY(dataDiff,0,xminInset,xmaxInset),2.0*findMaxY(dataDiff,0,xminInset,xmaxInset))
    errBandFrame.GetYaxis().SetNdivisions(5)
    errBandFrame.GetYaxis().SetLabelSize(0.06)
    errBandFrame.GetXaxis().SetTitle("#bf{m_{#tau#tau} [GeV]}    ")
    errBandFrame.GetXaxis().SetTitleColor(kBlack)
    errBandFrame.GetXaxis().SetTitleSize(0.07)
    errBandFrame.GetXaxis().SetTitleOffset(0.95)
    errBandFrame.GetXaxis().SetLabelSize(0.06)
    errBandFrame.SetNdivisions(505)

    legendDiff = TLegend()
    legendDiff.SetFillStyle(0)
    legendDiff.SetFillColor(0)
    legendDiff.SetBorderSize(0)
    legendDiff.AddEntry(signal,higgsLabel,"F")

    if tanb>0:
        legendDiff.AddEntry(TObject(0), mssmLabel, '') # That might not work in python
    legendDiff.AddEntry(dataDiff,"Data - Background","LP")  
    legendDiff.AddEntry(errBand,"Bkg. Uncertainty","F")
    legendDiff.SetX1NDC(0.45)
    legendDiff.SetX2NDC(0.88)
    legendDiff.SetY1NDC(0.67)
    legendDiff.SetY2NDC(0.88)
    if dataDiff.GetBinContent(dataDiff.FindBin(mass)) < 0.:
        legendDiff.SetX1NDC(0.45)
        legendDiff.SetX2NDC(0.88)
        legendDiff.SetY1NDC(0.24)
        legendDiff.SetY2NDC(0.45)

    legendDiff.SetTextSize(.045)
    legendDiff.SetTextAlign(12)

    padBack = TPad("padBack","padBack",0.57,0.58,0.975,0.956) # TPad must be created after TCanvas otherwise ROOT crashes
    padBack.SetFillColor(0)
    pad = TPad("diff","diff",0.45,0.5,0.9765,0.957) # TPad must be created after TCanvas otherwise ROOT crashes
    pad.cd()
    pad.SetFillColor(0)
    pad.SetFillStyle(0)
    errBandFrame.Draw()
    errBand.Draw("e2lsame")
    signal.Draw("histsame")
    line = TLine()
    line.DrawLine(xminInset, 0, xmaxInset, 0)
    dataDiff.Draw("pe same")
    legendDiff.Draw()
    pad.RedrawAxis()

    c.cd()
    ztt.Draw("hist")
    ggH.Draw("histsame")
    if ggH_hww:
        ggH_hww.Draw("hist same")
    ztt.Draw("hist same")
    errorBand.Draw("e2 same")
    tt.Draw("hist same")
    ewk.Draw("hist same")
    fakes.Draw("hist same")
    data.Draw("pe same")
    legend.Draw()
    c.RedrawAxis()
    padBack.Draw() # clear the background axe
    pad.Draw()

    CMSPrelim(datasetName, channel, cat)
    c.Print('Plot_'+fileName+".eps")
    c.Print('Plot_'+fileName+".png")
    c.Print('Plot_'+fileName+".pdf")