def deinit(self):
        suffix='' if self.suffix==None else '_%s'%self.suffix
        prefix='' if self.prefix==None else '%s_'%self.prefix

        if self.output_type=='root':
            f=OutputFactory.getTFile()
            f.cd()

        # Draw
        c=TCanvas()
        if self.logy:
            c.SetLogy(True)

        for variable in self.variables:
            c.Clear()

            ## Create a stacked histogram
            variable.hist=THStack(prefix+variable.name+suffix,self.bigtitle)

            # Make a list of histograms
            hists=[]
            for category in self.categories:
                h=variable.categories[category.name]
                if h.Integral()==0: continue # ignore empty histograms
                hists.append(h)
            if len(hists)==0: continue
            if self.sort_graphs: hists=sorted(hists,key=lambda h: h.Integral())

            # Add histograms
            for h in hists:
                if self.norm_mode=='1': h.Scale(1./h.Integral())
                h.Sumw2()
                variable.hist.Add(h,h.opt)

            ## Draw it
            opts=''
            if not self.stack:
                opts+='nostack'
            variable.hist.Draw(opts)
            if variable.hist.GetXaxis().GetLabels()!=None: variable.hist.GetXaxis().LabelsOption('v')

            title=variable.title
            if hasattr(variable,'units') and variable.units!=None:
                title+=' (%s)'%variable.units
            variable.hist.GetXaxis().SetTitle(title)

            if len(hists)>1:
                l=c.BuildLegend(.65,.98-len(hists)*0.04,.98,.98)
                l.Draw()
            c.Update()

            # Print it out
            outfileName="%s-%s"%(self.name,variable.hist.GetName())
            outfileName=outfileName.replace('/','-')
            if self.output_type=='png':
                c.SaveAs("%s.png"%outfileName)
            elif self.output_type=='eps':
                c.SaveAs("%s.eps"%outfileName)
            elif self.output_type=='root':
                variable.hist.Write()
Esempio n. 2
0
    def init_eventfile(self):
        if hasattr(self.eventfile,'output'):
            self.fh=OutputFactory.getTFile(self.eventfile.output)
        elif self.output!=None:
            self.fh=OutputFactory.getTFile(self.output)
        else:
            self.fh=OutputFactory.getTFile(os.path.basename(self.eventfile.path))

        # Copy any additional trees
        for tree in self.trees:
            dirname=os.path.dirname(tree)
            if dirname!='':
                d=self.fh.GetDirectory(dirname)
                if not d:
                    d=self.fh.mkdir(dirname)
            else:
                d=self.fh
            d.cd()
            tin=self.eventfile.fh.Get(tree)
            tout=tin.CloneTree(0)
            tin.CopyAddresses(tout)
            tout.CopyEntries(tout)
            tout.Write()
        self.fh.cd()

        # Create the output tree
        if self.branches==None: self.eventfile.tree.SetBranchStatus('*',1) # copy all branches
        else: # only copy requested branches
            for branch in self.branches:
                self.eventfile.tree.SetBranchStatus(branch,1)
            
        self.tree=self.eventfile.tree.CloneTree(0)
        self.eventfile.tree.CopyAddresses(self.tree)

        if self.treeName!=None: self.tree.SetName(self.treeName)

        # Create branches for new variables
        for var in self.variables:
            if var.branch_type!=None:
                self.tree.Branch(var.branchname,var.pointer,var.branch_type)
            else:
                self.tree.Branch(var.branchname,var.pointer)
    def init(self):
        self.fh=OutputFactory.getTFile()

        # Create the tree, branches and variable pointers
        self.tree=TTree('tree','tree')
        
        for var in self.variables:
            if not hasattr(var,'branchname'):
                var.branchname=var.name

            var.branch_type=None
            if type(var.type)==tuple:
                if var.type[0]==list:
                    if var.type[1]==float:
                        var.pointer=std.vector('float')()
                    elif var.type[1]==int:
                        var.pointer=std.vector('int')()
                    elif var.type[1]==bool:
                        var.pointer=std.vector('bool')()
                    elif var.type[1]==str:
                        var.pointer=std.vector('std::string')()
                    else:
                        var.pointer=std.vector(var.type[1].__name__)()
            else:
                if var.type==float:
                    var.branch_type='%s/D'%var.branchname
                    var.pointer=numpy.zeros(1,dtype=var.type)
                elif var.type==int:
                    var.branch_type='%s/I'%var.branchname
                    var.pointer=numpy.zeros(1,dtype=var.type)
                elif var.type==bool:
                    var.branch_type='%s/O'%var.branchname
                    var.pointer=numpy.zeros(1,dtype=var.type)
                elif var.type==TVector3:
                    var.pointer=TVector3()
                elif var.type==TVector2:
                    var.pointer=TVector2()
                elif var.type==str:
                    var.pointer=std.string()

        for var in self.variables:
            if var.branch_type!=None:
                self.tree.Branch(var.branchname,var.pointer,var.branch_type)
            else:
                self.tree.Branch(var.branchname,var.pointer)
    def deinit(self):
        # Draw
        c=TCanvas('c1','c1')
        if self.logz:
            c.SetLogz(True)

        # Turn the histogram list into a dictionary
        histograms=[]
        for key,hists in self.histograms.items():
            for cat,h in hists.items():
                if h.Integral()==0.: continue # Skip empties
                histograms.append(h)

        # Loop and save
        for h in histograms:
            # Normalize histograms, if requested
            if self.norm_mode=='1':
                h.Scale(1./h.Integral())
            
            title1=h.var1.title
            units1=getattr(h.var1,'units',None)
            if units1!=None: title1+=' (%s)'%h.var1.units
            h.SetXTitle(title1)

            title2=h.var2.title
            units2=getattr(h.var2,'units',None)
            if units2!=None: title2+=' (%s)'%h.var2.units
            h.SetYTitle(title2)

            if self.output_type in ['png','eps']: # Draw if saving image
                c.Clear()
                h.Draw('COLZ')

            # Print it out
            outfileName=h.GetName().replace('/','-')
            if self.output_type=='png':
                c.SaveAs("%s.png"%outfileName)
            elif self.output_type=='eps':
                c.SaveAs("%s.eps"%outfileName)
            elif self.output_type=='root':
                f=OutputFactory.getTFile()
                f.cd()
                h.Write()
    def deinit(self):
        # Get list of histograms to save
        hists={}

        for variable in self.variables:
            # Make a list of histograms
            for category,h in variable.categories.items():
                if h.Integral()==0: continue # ignore empty histograms
                h.Sumw2()
                if variable.name in hists:
                    hists[variable.name].append(h)
                else:
                    hists[variable.name]=[h]

        if len(hists.items())>0:
            f=OutputFactory.getTFile()
            f.cd()
            for dirname,hs in hists.items():
                f.mkdir(dirname)
                f.cd(dirname)
                for h in hs:
                    h.Write()
                f.cd()
Esempio n. 6
0
            line = line.strip()
            if line == "":  # Loop list ended
                key = None
                continue
            if key == None:  # New loop configuration
                key = line
                loopkeys.append(key)
                looplists.append([])
                continue
            looplists[-1].append(line)
looplists = list(itertools.product(*looplists))
if len(looplists) == 0:
    looplists.append("")

# Set the suffix for the OutputFactory, if required
OutputFactory.setResults(options.output)

# Add the script location to path to it can load it's own modules
pypath = os.path.dirname(os.path.abspath(pyfile))
sys.path.append(pypath)

## Manager
manager = Analysis.Manager()
manager.nevents = options.nevents
manager.name = pyfile[:-3]

# Load the analysis script
for i in range(max(len(looplists), 1)):
    # Globals used inside analysis setup script
    if len(looplists) > 0:
        defines.update(dict(zip(loopkeys, looplists[i])))
    def init_eventfile(self,eventfile):
        if self.fh==None:
            self.fh=OutputFactory.getTFile(self.filename)
            self.tree=eventfile.tree.CloneTree(0)

        eventfile.tree.CopyAddresses(self.tree)