Exemplo n.º 1
0
def make_histo(title, args, include_signal=True):
    """
    Make and return a histogram describe by the above parameters
    """
    histo = TH3F(
        title,
        "3D BumpHunter Test %s" % title,
        args.nbins,
        0,
        args.nbins * args.binsize,
        args.nbins,
        0,
        args.nbins * args.binsize,
        args.nbins,
        0,
        args.nbins * args.binsize,
    )
    histo.GetXaxis().SetTitle("X")
    histo.GetXaxis().SetTitleOffset(2)
    histo.GetYaxis().SetTitle("Y")
    histo.GetYaxis().SetTitleOffset(2)
    histo.GetZaxis().SetTitle("Z")
    histo.GetZaxis().SetTitleOffset(2)

    for i in range(args.nbkg):
        histo.Fill(gRandom.Exp(args.bkg_mean), gRandom.Exp(args.bkg_mean),
                   gRandom.Exp(args.bkg_mean))
    if include_signal:
        for i in range(args.nsig):
            x = gRandom.Gaus(args.sig_x, args.sig_spread_x)
            y = gRandom.Gaus(args.sig_y, args.sig_spread_y)
            z = gRandom.Gaus(args.sig_z, args.sig_spread_z)
            histo.Fill(x, y, z)
    return histo
Exemplo n.º 2
0
def make_phi_hist_with_noise(rid, is_parallel, hist_dim, noise_stdev,
                             normalize):
    """Makes a histogram of the phi angle with added noise."""
    hist = TH1D("blurredHist" + str(rid), "blurredHist" + str(rid),
                hist_dim[0], hist_dim[1], hist_dim[2])

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

    f = ROOT.TFile.Open(file_path)
    phi_list = []
    for event in f.hitTree:
        phi_list.append(gRandom.Gaus(event.phi, noise_stdev))

    mean = srkmisc.reduce_periodics(phi_list)
    stdev = srkmisc.careful_std(phi_list)
    for x in phi_list:
        if normalize:
            hist.Fill((x - mean) / stdev)
        else:
            hist.Fill(x)

    f.Close()
    ROOT.SetOwnership(hist, True)
    return hist
Exemplo n.º 3
0
def addBranchToDirectory():
    print ">>> Add branch to directory."

    file = TFile("tree.root", 'update')

    # make directory if it does not exist
    dir = file.GetDirectory("dir1")
    if not dir:
        print ">>>   created dir1"
        dir = file.mkdir("dir1")

    # make this directory the current one: everything you write will be saved here
    dir.cd()

    # make new tree with a branch
    tree = TTree("tree2", "tree2")
    n = array('d', [0])
    #n = numpy.zeros(1, dtype=float)
    tree.Branch("normal5", n, 'normal5/D')

    # fill tree
    for i in xrange(10000):
        n[0] = gRandom.Gaus(3, 2)
        tree.Fill()

    file.Write("", TFile.kOverwrite)
    file.Close()
Exemplo n.º 4
0
def makeTH1():
    """Create ROOT TH1 filled with a Gaussian distribution"""

    hist = TH1D("hist", "hist", 25, -10, 10)
    for i in range(500):
        hist.Fill(gRandom.Gaus(0, 3))
    return hist
Exemplo n.º 5
0
def writeTree():
    print ">>> Writing a tree."

    # make new root file with new tree
    file = TFile("tree.root", 'recreate')
    tree = TTree("tree_name", "tree title")

    # create 1 dimensional float arrays as fill variables, in this way the float array serves
    # as a pointer which can be passed to the branch
    n = array('d', [0])
    u = array('d', [0])

    # using numpy instead of array class (note python's float datatype corresponds to C++ doubles):
    #n = numpy.zeros(1, dtype=float)
    #u = numpy.zeros(1, dtype=float)

    # create the branches and assign the fill-variables to them as doubles (D)
    tree.Branch("normal", n, 'normal/D')
    tree.Branch("uniform", u, 'uniform/D')

    # create some random numbers, assign them into the fill variables and call Fill()
    for i in xrange(10000):
        n[0] = gRandom.Gaus()
        u[0] = gRandom.Uniform()
        tree.Fill()

    # write the tree into the output file and close the file
    file.Write()
    file.Close()
Exemplo n.º 6
0
def smear(xt):
    xeff = 0.3 + (1.0 - 0.3) / 20 * (xt + 10.0)
    #  efficiency
    x = gRandom.Rndm()
    if x > xeff: return None
    xsmear = gRandom.Gaus(-2.5, 0.2)
    #  bias and smear
    return xt + xsmear
Exemplo n.º 7
0
def resolution(value, binw):
    delta = gRandom.Gaus(0.0, 1.0)
    deltaValue = value + delta * binw / 2.0
    # if deltaValue <= 0.0:
    #     deltaValue= 0.0001
    # if deltaValue > 0.5:
    #     deltaValue= 0.4999
    return deltaValue
Exemplo n.º 8
0
def cov_gen(C, x, NP):
    z = array('f', NP * [0.])
    for i in xrange(NP):
        z[i] = gRandom.Gaus(0., 1.)
    for i in xrange(NP):
        x[i] = 0
        for j in xrange(NP):
            if j > i: continue
            x[i] += C[i][j] * z[j]
Exemplo n.º 9
0
def writeHistogramToFile(filename,name,Nbins=600,option='recreate',N=10000):
    print ">>> write a root file with a histogram."

    f = TFile(filename, option)
    h1 = TH1D(name,"Hists be histin'",Nbins,-4,8)

    for i in xrange(N):
        h1.Fill(gRandom.Gaus(2,2))

    f.Write()
    f.Close()
Exemplo n.º 10
0
def makeTTree():
    """Create ROOT TTree filled with a Gaussian distribution in x
       and a uniform distribution in y"""

    tree = TTree("tree", "tree")
    px = array('d', [0])
    py = array('d', [0])
    tree.Branch("px", px, "px/D")
    tree.Branch("py", py, "py/D")
    for i in range(500):
        px[0] = gRandom.Gaus(0, 3)
        py[0] = gRandom.Uniform() * 30 - 15
        tree.Fill()
    return tree
Exemplo n.º 11
0
def generate_dataset(Icase=1):
    xmin = 20.
    xmax = 230.
    func = TF1('func', ftrue, xmin, xmax, 3)
    func.SetParameters(0.01,1,0.2)
    Cs = TCanvas('Cs', '', 10, 10, 800, 600)
    func.Draw()
    func.GetXaxis().SetTitle('mass [GeV]')
    func.GetYaxis().SetTitle('Arbitrary Scale')
    Cs.SaveAs('Cs_func.png')
    Cs.SaveAs('Cs_func.pdf')
    f = TFile('data_'+str(Icase)+'.root', 'recreate')
    tree = TTree('nominal', '')
    mtrue = array('f', [0])
    mrec = array('f', [0])
    tree.Branch('mtrue', mtrue, 'mtrue/F')
    tree.Branch('mrec', mrec, 'mrec/F')
    num = 10000
    for i in range(num):
        x0 = func.GetRandom(xmin, xmax)
        if Icase == 1:
            x = gRandom.Gaus(x0+5, 5)
        elif Icase == 2:
            x = gRandom.Gaus(x0+5, 10)
        elif Icase == 3:
            x = gRandom.Gaus(x0+5, 20)
        else:
            print('ERROR!!!')
            break
        #print(x0,x)
        mtrue[0]=x0
        mrec[0] = x
        tree.Fill()
    tree.Write()
    f.Save()
    return
Exemplo n.º 12
0
def addHist():
    print ">>> Add histograms to a tree and one to a new branch."

    # reopen tree file
    file = TFile("tree.root", 'update')
    tree = file.Get("tree_name")

    # make new histogram
    hist = TH1D("normal3", "Haters be histin'", 50, -2, 6)

    # fill histogram
    for i in xrange(10000):
        hist.Fill(gRandom.Gaus(2, 2))

    file.Write("", TObject.kOverwrite)
    file.Close()
Exemplo n.º 13
0
def createhists(nhist=3):
  nbins    = 50
  xmin     = 0
  xmax     = 100
  nevts    = 10000
  rrange   = 0.5
  hists    = [ ]
  gRandom.SetSeed(1777)
  for i in xrange(1,nhist+1):
    mu     = 48+i
    sigma  = 10
    hname  = "hist%d"%(i)
    htitle = "#mu = %s, #sigma = %s"%(mu,sigma)
    hist   = TH1D(hname,hname,nbins,xmin,xmax)
    for j in xrange(nevts):
      hist.Fill(gRandom.Gaus(mu,sigma))
    hists.append(hist)
  return hists
Exemplo n.º 14
0
def addBranch():
    print ">>> Add branch."

    # reopen tree file
    file = TFile("tree.root", 'update')
    tree = file.Get("tree_name")

    # make new variable
    n = array('d', [0])
    #n = numpy.zeros(1, dtype=float)
    b = tree.Branch("normal2", n, 'normal2/D')

    # fill the branch
    N = tree.GetEntries()
    for i in xrange(N):
        tree.GetEntry(i)
        n[0] = gRandom.Gaus(3, 2)
        b.Fill()

    # overwrite the tree in the output file and close the file
    file.Write("", TFile.kOverwrite)
    file.Close()
Exemplo n.º 15
0
def make_histo_1d(title, args, include_signal=True):
    """
    Make and return a 1D TH2F. All the data will have a y coordinate of args.binsize/2.0
    (to keep the data in the first bin)
    but the fit needs at least 4 points in y, so we make it 4 bins wide along y
    """
    histo = TH2F(title, "1D BumpHunter2D Test %s" % title, args.nbins, 0,
                 args.nbins * args.binsize, 4, 0, 4 * args.binsize)
    histo.GetXaxis().SetTitle("X")
    histo.GetXaxis().SetTitleOffset(2)
    histo.GetYaxis().SetTitle("Y")
    histo.GetYaxis().SetTitleOffset(2)

    for i in range(args.nbkg):
        histo.Fill(gRandom.Exp(args.bkg_mean), args.binsize / 2.0)
    if include_signal:
        for i in range(args.nsig):
            x, y = gRandom.Gaus(args.sig_x,
                                args.sig_spread_x), args.binsize / 2.0
            # print x, y
            histo.Fill(x, y)

    return histo
Exemplo n.º 16
0
def addHistToDirectory():
    print ">>> Add histogram to directory."

    # reopen tree file
    file = TFile("tree.root", 'update')

    # make directory if it does not exist
    dir = file.GetDirectory("dir1")
    if not dir:
        print ">>>   created dir1"
        dir = file.mkdir("dir1")

    # make this directory the current one: everything you write will be saved here
    dir.cd()

    # declare histogram
    hist = TH1D("normal4", "Hist in folder", 50, -1, 9)

    # write histogram
    for i in xrange(10000):
        hist.Fill(gRandom.Gaus(4, 3))

    file.Write("", TFile.kOverwrite)
    file.Close()
Exemplo n.º 17
0
def makesamples(nevts=10000,**kwargs):
  """Create pseudo MC and data tree for quick and reproducible testing of Plotter tools."""
  
  outdir  = kwargs.get('outdir',  'plots' )
  channel = kwargs.get('channel', 'mutau' )
  samples = kwargs.get('sample',  ['Data','ZTT','WJ','QCD','TT'])
  scales  = kwargs.get('scales',  None    )
  samples = unwraplistargs(samples)
  
  scaledict = { # relative contribtions to pseudo data
    'ZTT': 1.00,
    'WJ':  0.40,
    'QCD': 0.25,
    'TT':  0.15,
    'DYJets':  1.0,
    'DY1Jets': 0.5,
    'DY2Jets': 0.3,
    'DY3Jets': 0.2,
    'DY4Jets': 0.1,
  }
  if scales:
    scaledict.update(scales)
  scaledict.pop('Data',None)
  vardict = { # uncorrelated pseudo distributions for variables
    'm_vis': {
      '*':   lambda gm: gRandom.Gaus( 72, 9) if gm==5 else gRandom.Gaus( 91, 2), # default
      'ZTT': lambda gm: gRandom.Gaus( 72, 9),
      'WJ':  lambda gm: gRandom.Gaus( 65,28),
      'QCD': lambda gm: gRandom.Gaus( 80,44),
      'TT':  lambda gm: gRandom.Gaus(110,70),
    },
    'genmatch_2': { # chance that genmatch_2==5 (real tau)
      '*':   0.9, # default
      'ZTT': 1.0,
      'WJ':  0.5,
      'QCD': 0.5,
      'TT':  0.8,
    },
    'pt_1': {
      '*':   lambda: gRandom.Landau(30,2), # default
      'QCD': lambda: gRandom.Landau(30,5),
      'TT':  lambda: gRandom.Landau(40,6),
    },
    'pt_2': {
      '*':   lambda: gRandom.Landau(24,2), # default
      'WJ':  lambda: gRandom.Landau(24,2),
      'QCD': lambda: gRandom.Landau(24,5),
      'TT':  lambda: gRandom.Landau(30,6),
    },
    'eta_1': {
      '*': lambda: gRandom.Uniform(-2.5,2.5), # default
    },
    'eta_2': {
      '*': lambda: gRandom.Uniform(-2.5,2.5), # default
    },
    'dm_2': {
      '*':   lambda r: 0 if r<0.4 else 1 if r<0.6 else 10 if r<0.9 else 11, # default
    },
    'njets': {
      '*':   lambda: gRandom.Poisson(0.2,), # default
      'ZTT': lambda: gRandom.Poisson(0.2,),
      'WJ':  lambda: gRandom.Poisson(1.2,),
      'QCD': lambda: gRandom.Poisson(2.0,),
      'TT':  lambda: gRandom.Poisson(2.5,),
      'DYJets':  lambda: gRandom.Poisson(0.2,),
      'DY1Jets': lambda: 1,
      'DY2Jets': lambda: 2,
      'DY3Jets': lambda: 3,
      'DY4Jets': lambda: 4,
    },
    'NUP': {
      '*':   lambda: gRandom.Poisson(0.2,), # default
      'ZTT': lambda: gRandom.Poisson(0.2,),
      'WJ':  lambda: gRandom.Poisson(1.2,),
      'QCD': lambda: gRandom.Poisson(2.0,),
      'TT':  lambda: gRandom.Poisson(2.5,),
      'DYJets':  lambda: gRandom.Poisson(0.2,),
      'DY1Jets': lambda: 1,
      'DY2Jets': lambda: 2,
      'DY3Jets': lambda: 3,
      'DY4Jets': lambda: 4,
    },
    'weight': {
      '*':   lambda: gRandom.Gaus(1.0,0.10), # default
      'ZTT': lambda: gRandom.Gaus(1.0,0.10),
      'WJ':  lambda: gRandom.Gaus(1.0,0.10),
      'QCD': lambda: gRandom.Gaus(1.0,0.05),
      'TT':  lambda: gRandom.Gaus(1.0,0.08),
    },
  }
  
  # PREPARE TREES
  ensuredir(outdir)
  filedict   = { }
  histdict   = { }
  m_vis      = np.zeros(1,dtype='f')
  pt_1       = np.zeros(1,dtype='f')
  pt_2       = np.zeros(1,dtype='f')
  dm_2       = np.zeros(1,dtype='f')
  eta_1      = np.zeros(1,dtype='f')
  eta_2      = np.zeros(1,dtype='f')
  njets      = np.zeros(1,dtype='i')
  genmatch_2 = np.zeros(1,dtype='i') # genmatch_2: 5 = real tau; 0 = fake tau
  NUP        = np.zeros(1,dtype='i') # number of LHE-level partons for jet stitching
  weight     = np.zeros(1,dtype='f')
  def makesample(sample): # help function to create file with tree
    fname = "%s/%s_%s.root"%(outdir,sample,channel)
    file  = TFile(fname,'RECREATE')
    hist  = TH1D('cutflow','cutflow',20,0,20)
    tree  = TTree('tree','tree')
    tree.Branch('m_vis',      m_vis,      'm_vis/F')
    tree.Branch('pt_1',       pt_1,       'pt_1/F')
    tree.Branch('pt_2',       pt_2,       'pt_2/F')
    tree.Branch('dm_2',       dm_2,       'dm_2/I')
    tree.Branch('eta_1',      eta_1,      'eta_1/F')
    tree.Branch('eta_2',      eta_2,      'eta_2/F')
    tree.Branch('njets',      njets,      'njets/I')
    tree.Branch('genmatch_2', genmatch_2, 'genmatch_2/I')
    tree.Branch('NUP',        NUP,        'NUP/I')
    tree.Branch('weight',     weight,     'weight/F')
    tree.SetDirectory(file)
    hist.SetDirectory(file)
    hist.SetBinContent( 1,1)
    hist.SetBinContent(17,1)
    histdict[sample] = hist
    filedict[sample] = (file,tree)
  for sample in scaledict:
    makesample(sample)
  makesample('Data')
  
  def getgenerator(var,sample): # get random generator from dictionary
    if sample in vardict[var]: return vardict[var][sample]
    else: return vardict[var]['*']
  def fill(sample,tree,nevts): # help function to fill trees
    for i in xrange(nevts):
      genmatch_2[0] = 5 if gRandom.Uniform(1.)<getgenerator('genmatch_2',sample) else 0
      m_vis[0]      = getgenerator('m_vis',sample)(genmatch_2[0])
      pt_1[0]       = getgenerator('pt_1', sample)()
      pt_2[0]       = getgenerator('pt_2', sample)()
      if m_vis[0]<0 or pt_1[0]<0 or pt_2[0]<0: continue
      if pt_1[0]<pt_1[0]:
        pt_1[0], pt_2[0] = pt_2[0], pt_1[0]
      dm_2[0]   = getgenerator('dm_2', sample)(gRandom.Uniform(1.))
      eta_1[0]  = getgenerator('eta_1', sample)()
      eta_2[0]  = getgenerator('eta_2', sample)()
      njets[0]  = getgenerator('njets', sample)()
      weight[0] = getgenerator('weight',sample)() if sample!='Data' else 1.0
      tree.Fill()
  
  # PSEUDO MC
  print ">>> Generating pseudo MC..."
  #time0 = time.time()
  for sample in samples:
    if sample=='Data': continue
    #print ">>>   %r"%(sample)
    file, tree = filedict[sample]
    file.cd()
    fill(sample,tree,nevts)
    histdict[sample].Write()
    tree.Write()
  #print ">>>   %.1f seconds"%(time.time()-time0)
  
  # PSEUDO DATA
  if 'Data' in samples:
    print ">>> Generating pseudo data..."
    file, tree = filedict['Data']
    file.cd()
    #time0 = time.time()
    for sample in samples:
      if sample=='Data': continue
      prob = scaledict.get(sample,1.0)
      ndevts = int(prob*nevts) # relative contribtion to pseudo data
      fill(sample,tree,ndevts)
    histdict[sample].Write()
    tree.Write()
    #print ">>>   %.1f seconds"%(time.time()-time0)
  
  return filedict
Exemplo n.º 18
0
from ROOT import THStack, TH1D, gRandom
from math import sqrt

hist1 = TH1D("hist1", "hist1", 10, 0, 100)
hist2 = TH1D("hist2", "hist2", 10, 0, 100)
hist1.Sumw2()
hist2.Sumw2()
stack = THStack("stack", "stack")

for i in xrange(1000):
    weight1 = gRandom.Gaus(1, 0.1)
    weight2 = gRandom.Gaus(1, 0.2)
    hist1.Fill(gRandom.Gaus(55, 20), weight1)
    hist2.Fill(gRandom.Gaus(50, 25), weight2)

stack.Add(hist1)
stack.Add(hist2)

hist_stack = stack.GetStack().Last()
print "\n>>> hist_stack = stack.GetStack().Last()"
print ">>>   hist_stack = %s" % hist_stack
print ">>>   type(hist_stack) = %s" % type(hist_stack)
print ">>>   hist_stack.GetName() = %s" % hist_stack.GetName()
print ">>>   hist_stack.GetTitle() = %s" % hist_stack.GetTitle()

print "\n>>> comparing integrals"
print ">>>   stack = %s" % hist_stack.Integral()
print ">>>   hist1 = %s" % hist1.Integral()
print ">>>   hist2 = %s" % hist2.Integral()

print "\n>>> comparing entries"
Exemplo n.º 19
0
#  Train with a Breit-Wigner, mean 0.3 and width 2.5.
for i in xrange(100000):
    xt = gRandom.BreitWigner(0.3, 2.5)
    x = smear(xt)
    if x != None:
        response.Fill(x, xt)
    else:
        response.Miss(xt)

print "==================================== TEST ====================================="
hTrue = TH1D("true", "Test Truth", 40, -10.0, 10.0)
hMeas = TH1D("meas", "Test Measured", 40, -10.0, 10.0)
#  Test with a Gaussian, mean 0 and width 2.
for i in xrange(10000):
    xt = gRandom.Gaus(0.0, 2.0)
    x = smear(xt)
    hTrue.Fill(xt)
    if x != None: hMeas.Fill(x)

print "==================================== UNFOLD ==================================="
unfold = RooUnfoldBayes(response, hMeas, 4)
#  OR
# unfold= RooUnfoldSvd     (response, hMeas, 20);     #  OR
# unfold= RooUnfoldTUnfold (response, hMeas);         #  OR
# unfold= RooUnfoldIds     (response, hMeas, 3);      #  OR

hReco = unfold.Hreco()
unfold.PrintTable(cout, hTrue)
hReco.Draw()
hMeas.Draw("SAME")
Exemplo n.º 20
0
            yd = int(l * 1. / xd * 1.)

        c1.Divide(xd, yd)

        for i in range(1, l + 1):
            c1.cd(i)
            Histos[i - 1].Draw()

        s = raw_input("return to continue")


if __name__ == '__main__':
    gRandom.SetSeed()

    th = THisto()
    th.BookH1("s1", "This is the first signal", 100, -4, 4)
    th.BookH1("s2", "This is the second signal", 100, -4, 4)
    th.BookH2("s1s2", "This is the first versus the econd signal", 100, -4, 4,
              100, -4, 4)

    for i in range(0, 500):
        xs1 = gRandom.Gaus(-0.5, 0.5)
        xs2 = gRandom.Landau(1, 0.15)
        th.FillH1("s1", xs1, 0.3)
        th.FillH1("s2", xs2, 0.2)
        th.FillH2("s1s2", xs1, xs2)

    print "histograms ={0}".format(th)
    th.DrawList(["s1", "s2"], xd=1, yd=2)
    th.DrawAll()
Exemplo n.º 21
0
# Example: displaying a ROOT histogram from Python
from ROOT import gRandom, TCanvas, TH1F, TFile  # this is the "include" of python.. what does "from ROOT" mean?

c1 = TCanvas('c1', 'Example', 200, 10, 700,
             500)  # the same method as in c++ (wow!)
hpx = TH1F('hpx', 'px', 100, -4, 4)
for i in xrange(25000):
    px = gRandom.Gaus()
    hpx.Fill(px)
hpx.Draw()
c1.Update()

# some lines to save output
file = TFile("HelloWorld_output.root", "recreate")
print "Hello world. While I said this I also saved a .pdf with a gaussian distribution."
hpx.Write("hpx")
file.Close()
print "Ciao!"
Exemplo n.º 22
0
  print gDirectory.GetName()
  print gDirectory.ls()
  file = TFile("treeParticles.root", 'recreate')
  tree = TTree("tree_name", "tree title")
  
  nevts = 100
  Nmax = 10
  nParticles = array( 'i', [ 0 ] )
  pt = array( 'd', Nmax*[ 0. ] )
  tree.Branch( 'nParticles', nParticles, 'nParticles/I' )
  tree.Branch( 'pt', pt, 'pt[nParticles]/D' )
  
  for i in xrange(nevts):
    nParticles[0] = int(gRandom.Uniform()*10)
    for j in range(nParticles[0]):
       pt[j] = gRandom.Gaus(20,2)
    tree.Fill()
  
  tree.Draw("pt[0] >> h(100,0,10)")
  hist = gDirectory.Get("h")
  print gDirectory.GetName()
  print gDirectory.ls()
  print hist
  print type(hist)
  
  file.Write()
  file.Close()
  

def main():
  print "\n>>> Hello, world!"
Exemplo n.º 23
0
    def Simulate(self, save=None, draw=None):
        '''

        :param save: if True: saves the root file as well as the true signal distribution
        :param draw: if True draws the signal distribution
        :param ask:
        :return:
        '''

        # Settings:
        MPV = self.MCAttributes['Landau_MPV_bkg']  # background for Landau MPV distribution
        sigma = self.MCAttributes['Landau_Sigma']  # Landau scaling sigma
        NPeaks = self.MCAttributes['NPeaks']
        MCRunPath = self.MCAttributes['MCRunPath'].format(self.RunNumber)
        xmin = self.diamond.Position['xmin'] + 0.01
        xmax = self.diamond.Position['xmax'] - 0.01
        ymin = self.diamond.Position['ymin'] + 0.01
        ymax = self.diamond.Position['ymax'] - 0.01
        center_x = (xmax + xmin) / 2.
        center_y = (ymax + ymin) / 2.
        if draw != None:
            assert (type(draw) == t.BooleanType), "draw has to be boolean type"
            self.MCAttributes['DrawRealDistribution'] = draw
        if save != None:
            assert (type(save) == t.BooleanType), "save argument has to be of type boolean"
            self.MCAttributes['Save'] = save

        def ManualHitDistribution(x, par):
            '''
            Probability density function for hit distribution based on
            6 2d gaussian in a rectangular pattern. The PDF is NOT
            normalized to 1
            :param x: array, x[0]: x position, x[1]: y position
            :param par: parameter array;
            :return:
            '''
            result = 0.1  # bkg
            norm = 1.
            sigma = 0.04
            for i in range(len(par) / 2):
                result += norm * TMath.Gaus(x[0], par[2 * i], sigma) * TMath.Gaus(x[1], par[2 * i + 1], sigma)
            return result

        def CreateRandomPeaksConfig(xmin, xmax, ymin, ymax, bkg=120, peak_height=0.5, npeaks=NPeaks):
            '''
            Creates the input parameters for SignalShape, which describes the peak distribution in the
            Signal distribution
            :param xmin: window parameter
            :param xmax: window parameter
            :param ymin: window parameter
            :param ymax: window parameter
            :param bkg: background of signal response
            :param peak_height: height of one peak in % of bkg, if bkg==0: the peaks heights are set to 1
            :param npeaks: number of peaks in window - if none it picks random between 0 and 15
            :return: array containing the parameters
            '''
            if npeaks == None:
                npeaks = int(round(gRandom.Uniform(0, 15)))

            if self.MCAttributes['SpecialDistribution'] in ["Central", "central"]:
                npeaks = 1
                dxy = 0.02
                parameters = np.zeros(7)
                parameters[0] = npeaks
                parameters[1] = bkg
                parameters[2] = peak_height
                parameters[3] = gRandom.Uniform(center_x - dxy, center_x + dxy)
                parameters[4] = gRandom.Uniform(center_y - dxy, center_y + dxy)
                parameters[5] = gRandom.Uniform(self.MCAttributes['PeakSigmaX_min'], self.MCAttributes['PeakSigmaX_max'])
                parameters[6] = gRandom.Uniform(self.MCAttributes['PeakSigmaY_min'], self.MCAttributes['PeakSigmaY_max'])
            elif self.MCAttributes['SpecialDistribution'] in ["4Peaks", "4peaks", "L", "3Peaks"]:
                npeaks = 4
                dxy = 0.02
                parameters = np.zeros(3 + 4 * npeaks)
                parameters[0] = npeaks
                parameters[1] = bkg
                parameters[2] = peak_height
                # peaks:
                peaknr = 0
                for x in [center_x - 0.07, center_x + 0.07]:
                    for y in [center_y - 0.07, center_y + 0.07]:
                        parameters[3 + 4 * peaknr] = gRandom.Uniform(x - dxy, x + dxy)
                        parameters[4 + 4 * peaknr] = gRandom.Uniform(y - dxy, y + dxy)
                        parameters[5 + 4 * peaknr] = gRandom.Uniform(self.MCAttributes['PeakSigmaX_min'], self.MCAttributes['PeakSigmaX_max'])
                        parameters[6 + 4 * peaknr] = gRandom.Uniform(self.MCAttributes['PeakSigmaY_min'], self.MCAttributes['PeakSigmaY_max'])
                        peaknr += 1
                if self.MCAttributes['SpecialDistribution'] in ["L", "3Peaks"]:
                    npeaks = 3
                    parameters[0] = npeaks
                    parameters = parameters[:3 + 4 * npeaks]
            elif self.MCAttributes['SpecialDistribution'] in ["Testpattern", "testpattern", "Test", "test"]:
                npeaks = 8
                Center_x = gRandom.Uniform(center_x - 0.01, center_x + 0.01)
                Center_y = gRandom.Uniform(center_y - 0.01, center_y + 0.01)
                parameters = np.zeros(3 + 4 * npeaks)
                parameters[0] = npeaks
                parameters[1] = bkg
                parameters[2] = peak_height
                parameters[3] = Center_x - 0.1
                parameters[4] = Center_y + 0.08
                parameters[5] = 0.02
                parameters[6] = 0.02
                parameters[7] = Center_x - 0.04
                parameters[8] = Center_y + 0.08
                parameters[9] = 0.02
                parameters[10] = 0.02
                parameters[11] = Center_x - 0.1
                parameters[12] = Center_y
                parameters[13] = 0.025
                parameters[14] = 0.025
                parameters[15] = Center_x
                parameters[16] = Center_y
                parameters[17] = 0.02
                parameters[18] = 0.02
                parameters[19] = Center_x + 0.1
                parameters[20] = Center_y
                parameters[21] = 0.03
                parameters[22] = 0.03
                parameters[23] = Center_x - 0.04
                parameters[24] = Center_y - 0.06
                parameters[25] = 0.015
                parameters[26] = 0.015
                parameters[27] = Center_x - 0.12
                parameters[28] = Center_y - 0.12
                parameters[29] = 0.03
                parameters[30] = 0.03
                parameters[31] = Center_x + 0.08
                parameters[32] = Center_y - 0.12
                parameters[33] = 0.04
                parameters[34] = 0.04
            else:
                parameters = np.zeros(3 + 4 * npeaks)
                parameters[0] = npeaks
                parameters[1] = bkg
                parameters[2] = peak_height
                for i in range(npeaks):
                    parameters[3 + 4 * i] = gRandom.Uniform(xmin, xmax)
                    parameters[4 + 4 * i] = gRandom.Uniform(ymin, ymax)
                    parameters[5 + 4 * i] = gRandom.Uniform(0.02, 0.07)
                    parameters[6 + 4 * i] = gRandom.Uniform(0.02, 0.07)
            self.MCAttributes['NPeaks'] = npeaks
            return parameters

        def SignalShape(x, par):
            '''

            :param x:   x[0]: x position
                        x[1]: y position
            :param par: par[0]: number of peaks
                        par[1]: mean bkg signal
                        par[2]: peak height in percent of bkg
                        par[3]: peak1 x position
                        par[4]: peak1 y position
                        par[5]: peak1 sigma in x
                        par[6]: peak1 sigma in y
                        ...
                        par[3+4*n]: peakn x position
                        par[4+4*n]: peakn y position
                        par[5+4*n]: peakn sigma in x
                        par[6+4*n]: peakn sigma in y
            :return:
            '''
            norm = par[1] * par[2]
            result = par[1]
            if par[1] == 0:
                norm = 1
            for i in range(int(par[0])):
                result += norm * TMath.Gaus(x[0], par[3 + 4 * i], par[5 + 4 * i]) * TMath.Gaus(x[1], par[4 + 4 * i], par[6 + 4 * i])
            return result

        # Set seed for random number generator:
        today = datetime.today()
        seed = int((today - datetime(today.year, today.month, today.day, 0, 0, 0, 0)).total_seconds() % 1800 * 1e6)
        gRandom.SetSeed(seed)

        # create track_info ROOT file
        if not os.path.exists(MCRunPath):
            os.makedirs(MCRunPath)
        if self.MCAttributes['Save']:
            file = TFile(MCRunPath + 'track_info.root', 'RECREATE')
        self.track_info = TTree('track_info', 'MC track_info')
        track_x = array('f', [0])
        track_y = array('f', [0])
        integral50 = array('f', [0])
        calibflag = array('i', [0])
        calib_offset = array('i', [0])
        time_stamp = array('f', [0])
        self.track_info.Branch('track_x', track_x, 'track_x/F')
        self.track_info.Branch('track_y', track_y, 'track_y/F')
        self.track_info.Branch('integral50', integral50, 'integral50/F')
        self.track_info.Branch('calibflag', calibflag, 'calibflag/I')
        self.track_info.Branch('calib_offset', calib_offset, 'calib_offset/I')
        self.track_info.Branch('time_stamp', time_stamp, 'time_stamp/F')

        # Create Manual Hit Distribution:
        if self.MCAttributes['HitDistributionMode'] == 'Manual':
            dx = 0.08
            dy = 0.07
            f_lateral = TF2('f_lateral', ManualHitDistribution, xmin, xmax, ymin, ymax, 12)
            f_lateral.SetNpx(80)
            f_lateral.SetNpy(80)
            # 6 gaus centers:
            par = np.array([center_x - dx / 2.,  # x1
                            center_y + dy,  # y1
                            center_x + dx / 2.,  # x2
                            center_y + dy,  # y2
                            center_x + dx / 2.,  # x3
                            center_y,  # y3
                            center_x - dx / 2.,  # x4
                            center_y,  # y4
                            center_x - dx / 2.,  # x5
                            center_y - dy,  # y5
                            center_x + dx / 2.,  # x6
                            center_y - dy  # y6
                            ])
            f_lateral.SetParameters(par)
        a = Double()
        b = Double()

        # Generate Signal Distribution:
        if self.MCAttributes['SignalMode'] == 'Landau':
            self.SignalParameters = CreateRandomPeaksConfig(xmin, xmax, ymin, ymax, peak_height=self.MCAttributes['PeakHeight'], bkg=MPV)
        else:
            self.SignalParameters = CreateRandomPeaksConfig(xmin, xmax, ymin, ymax, peak_height=self.MCAttributes['PeakHeight'], bkg=100)
        f_signal = TF2('f_signal', SignalShape, xmin, xmax, ymin, ymax, len(self.SignalParameters))
        f_signal.SetNpx(40)  # Resolution
        f_signal.SetNpy(40)
        f_signal.SetParameters(self.SignalParameters)
        if self.MCAttributes['DrawRealDistribution']:
            canvas = TCanvas('canvas', 'canvas')
            canvas.cd()
            gStyle.SetPalette(55)  # a Rain Bow palette == used.
            gStyle.SetNumberContours(8)
            f_signal.Draw('surf1')
            gPad.Print(MCRunPath + 'RealSignalDistribution.png')
            if self.ShowAndWait:
                answer = input('for data creation, type `yes`: ')
            else:
                answer = 'yes'
        else:
            answer = 'yes'

        # Set the Hit distribution for Manual or Import
        if self.MCAttributes['HitDistributionMode'] == 'Manual':
            HitsTemplate = f_lateral
        elif self.MCAttributes['HitDistributionMode'] == 'Import':
            HitsTemplate = self.counthisto

        mc_start_timestamp = 42.  # arbitrary timestamp for the first MC event
        MCEventDeltaTime = 30. * 60. / 300000.  # 1800s/300000Hits = 0.006s/Hit
        tmp_time = mc_start_timestamp
        # Generate Toy Data:
        if answer == 'yes':
            if self.verbose:
                self.ShowMCConfig()
            self.verbose_print('Creating Toy Data with {0} Hits'.format(self.NumberOfHits))
            integral50_max = self.MCAttributes['integral50_max']  # Maximum of Signal response allowed (data: 500 ?)
            i = 0
            j = 0
            while i < self.NumberOfHits and j < 2 * self.NumberOfHits:
                # Get x and y
                if self.MCAttributes['HitDistributionMode'] == 'Uniform':
                    track_x[0] = gRandom.Uniform(xmin, xmax)
                    track_y[0] = gRandom.Uniform(ymin, ymax)
                else:
                    HitsTemplate.GetRandom2(a, b)
                    track_x[0] = gRandom.Gaus(a, self.MCAttributes['TrackResolution'])  # 0.002 = 20mu track resolution (first guess)
                    track_y[0] = gRandom.Gaus(b, self.MCAttributes['TrackResolution'])

                # Get Signal at x and y
                if self.MCAttributes['SignalMode'] == 'Landau':
                    integral50[0] = gRandom.Landau(f_signal(track_x[0], track_y[0]), sigma)
                else:
                    integral50[0] = gRandom.Gaus(f_signal(track_x[0], track_y[0]), 0.6 * f_signal(track_x[0], track_y[0]) - 33)

                # check if found values fulfill requirements
                if xmin < track_x[0] < xmax and ymin < track_y[0] < ymax and integral50[0] < integral50_max:
                    tmp_time += MCEventDeltaTime
                    time_stamp[0] = tmp_time
                    self.track_info.Fill()
                    self.Data['track_x'].append(track_x[0])
                    self.Data['track_y'].append(track_y[0])
                    self.Data['integral50'].append(integral50[0])
                    i += 1
                j += 1
            if not j < 2 * self.NumberOfHits:  # if too many times requirements were not fulfilled
                assert (False), "Bad MC Parameters"

            # Save root file and true Signal Shape:
            if self.MCAttributes['Save']:
                file.Write()
                f_signal.SaveAs(MCRunPath + 'RealSignalDistribution.root')
                self.verbose_print(MCRunPath + 'track_info.root has been written')
                self.TrackingPadAnalysis['ROOTFile'] = MCRunPath + 'track_info.root'

            # Print Settings of created data:
            if self.verbose:
                print("\nToydata containing {:.0f} peaks generated.".format(self.SignalParameters[0]))
                if self.MCAttributes['SignalMode'] == 'Landau':
                    for i in range(int(self.SignalParameters[0])):
                        x = self.SignalParameters[3 + 4 * i]
                        y = self.SignalParameters[4 + 4 * i]
                        print("Peak {0:.0f} at position: ({1:.3f}/{2:.3f}) with Laundau Response MPV: {3:.2f} Sigma: {4:.1f}".format(i + 1, x, y, f_signal(x, y), sigma))
                else:
                    integral50[0] = gRandom.Gaus(f_signal(track_x[0], track_y[0]), 0.6 * f_signal(track_x[0], track_y[0]) - 33)
                    for i in range(int(self.SignalParameters[0])):
                        x = self.SignalParameters[3 + 4 * i]
                        y = self.SignalParameters[4 + 4 * i]
                        print("Peak {0:.0f} at position: ({1:.3f}/{2:.3f}) with Gaussian Response Mean: {3:.2f} Sigma: {4:.1f}".format(i + 1, x, y, f_signal(x, y), 0.6 * f_signal(x, y) - 33))

            self.DataIsMade = True
            self.verbose_print('Monte Carlo Toy Data created.\n')
Exemplo n.º 24
0
def smear(mu, sigma0, smearfactor, N=100000, lcut=None, ucut=None, nbins=80):
    print ">>> smearing for N(%s,%s) with a factor of %s" % (mu, sigma0,
                                                             smearfactor)

    # HISTS
    xmin, xmax = mu - sigma0 * 5, mu + sigma0 * 4
    sigma1 = sigma0 * (1 + smearfactor)
    histname0 = "unsmeared"
    histname1 = "smeared"
    histtitle0 = "unsmeared, #sigma_{0} = %s" % (sigma0)
    histtitle1 = "smeared, #sigma_{new} = %s" % (sigma1)
    hist0 = TH1F(histname0, histtitle0, nbins, xmin, xmax)
    hist1 = TH1F(histname1, histtitle1, nbins, xmin, xmax)

    # FIT FUNCTIONS
    xminf = xmin if lcut == None else lcut
    xmaxf = xmax if ucut == None else ucut
    gaus0 = TF1("gaus0", "gaus", xminf, xmaxf)
    gaus1 = TF1("gaus1", "gaus", xminf, xmaxf)
    gaus0.SetTitle("%s fit" % histname0)
    gaus1.SetTitle("%s fit" % histname1)
    gaus0.SetParameters(N, mu, sigma0)
    gaus1.SetParameters(N, mu, sigma1)
    #gaus0.SetParLimits(2,sigma0*0.9,sigma0*1.1)
    #gaus1.SetParLimits(2,sigma1*0.9,sigma1*1.1)
    hists = [(hist0, gaus0), (hist1, gaus1)]

    # SMEAR & FILL
    #sigma1 = smearfactor
    #sigma1 = (smearfactor**2)/2.
    #sigma1 = sqrt(2.*(smearfactor))
    #sigma1 = sqrt(-2.*log(smearfactor))
    #sigma1 = 1./(2*smearfactor**2)
    sigma2 = sqrt(sigma1**2 - sigma0**2)
    print ">>>   sigma0 = %.3f, sigma1 = %.3f, sigma2 = %.3f" % (
        sigma0, sigma1, sigma2)
    print ">>>   generating %s events..." % (N)
    for i in xrange(N):
        xval0 = gRandom.Gaus(mu, sigma0)
        if lcut != None and xval0 < lcut: continue
        if ucut != None and xval0 > ucut: continue
        #rand  = gRandom.Gaus(1,smearfactor)
        #xval1 = xval0 * rand
        #rand  = gRandom.Gaus(0,1+smearfactor)
        #xval1 = xval0 + rand
        rand = gRandom.Gaus(0, 1)
        xval1 = xval0 + sigma2 * rand
        hist0.Fill(xval0)
        if lcut != None and xval1 < lcut: continue
        if ucut != None and xval1 > ucut: continue
        hist1.Fill(xval1)

    # PLOT SETTINGS
    xtitle = "x variable"
    ytitle = "events"
    title = "Gauss(%s,%s)" % (mu, "#sigma")
    canvasname = "smear_%.1f-%.1f_by_%.2f" % (mu, sigma0, smearfactor)
    if lcut != None: canvasname += "_gt%.1f" % (lcut)
    if ucut != None: canvasname += "_lt%.1f" % (ucut)
    canvasname = canvasname.replace('.', 'p')
    ymin, ymax = 0, 1.14 * max(hist0.GetMaximum(), hist1.GetMaximum())
    rmin, rmax = 0.60, 1.40
    lmargin, rmargin = 0.14, 0.04

    # CANVAS
    print ">>> plotting..."
    canvas = TCanvas("canvas", "canvas", 100, 100, 800, 800)
    canvas.Divide(2)

    # MAIN plot
    canvas.cd(1)
    gPad.SetPad("pad1", "pad1", 0, 0.33, 1, 1, 0, -1, 0)
    gPad.SetFillColor(0)
    gPad.SetBorderMode(0)
    gPad.SetFrameFillStyle(0)
    gPad.SetFrameBorderMode(0)
    gPad.SetTopMargin(0.06)
    gPad.SetBottomMargin(0.02)
    gPad.SetLeftMargin(lmargin)
    gPad.SetRightMargin(rmargin)
    gPad.SetGrid()
    gPad.cd()

    textsize = 0.050
    x1, width = 0.18, 0.25
    y1, height = 0.89, textsize * 1.08 * 5
    legend = TLegend(x1, y1, x1 + width, y1 - height)
    legend.SetTextSize(textsize)
    legend.SetBorderSize(0)
    legend.SetFillStyle(0)
    legend.SetFillColor(0)
    legend.SetTextFont(62)
    legend.SetHeader(title)
    legend.SetTextFont(42)

    # FRAME
    frame = gPad.DrawFrame(xmin, ymin, xmax, ymax)
    frame.GetYaxis().SetTitleSize(0.070)
    frame.GetXaxis().SetTitleSize(0.070)
    frame.GetXaxis().SetLabelSize(0.000)
    frame.GetYaxis().SetLabelSize(0.060)
    frame.GetXaxis().SetTitleOffset(1.00)
    frame.GetYaxis().SetTitleOffset(1.06)
    frame.GetXaxis().SetNdivisions(508)
    frame.GetXaxis().SetTitle(xtitle)
    frame.GetYaxis().SetTitle(ytitle)

    # DRAW & FIT
    print ">>>   fitting..."
    fits = []
    for i, (hist, gaus) in enumerate(hists):
        color = colors[i % len(colors)]
        hist.SetLineColor(color)
        hist.SetLineWidth(2)
        hist.SetLineStyle(1)
        gaus.SetLineColor(color + 1)
        gaus.SetLineWidth(2)
        gaus.SetLineStyle(2)
        hist.Fit(gaus.GetName(), '0', '', xminf, xmaxf)
        hist.Draw('SAME')
        gaus.Draw('SAME')
        gtitle = "#sigma_{fit} = %.3f" % (gaus.GetParameter(2)
                                          )  #gaus.GetTitle()
        legend.AddEntry(hist, hist.GetTitle(), 'l')
        legend.AddEntry(gaus, gtitle, 'l')
    print ">>>   real unsmeared sigma:    %5.3f" % (sigma0)
    print ">>>   fitted unsmeared sigma:  %5.3f" % (gaus0.GetParameter(2))
    print ">>>   real smear factor:       %5.3f" % (smearfactor)
    print ">>>   fitted smeared sigma:    %5.3f" % (gaus1.GetParameter(2))

    legend.Draw()
    frame.Draw('SAMEAXIS')
    gPad.Modified()

    # RATIO plot
    canvas.cd(2)
    gPad.SetPad("pad2", "pad2", 0, 0, 1, 0.32, 0, -1, 0)
    gPad.SetTopMargin(0.04)
    gPad.SetBottomMargin(0.29)
    gPad.SetLeftMargin(lmargin)
    gPad.SetRightMargin(rmargin)

    # RATIO FRAME
    rframe = gPad.DrawFrame(xmin, rmin, xmax, rmax)
    rframe.GetYaxis().CenterTitle()
    rframe.GetXaxis().SetTitleSize(0.144)
    rframe.GetYaxis().SetTitleSize(0.140)
    rframe.GetXaxis().SetLabelSize(0.130)
    rframe.GetYaxis().SetLabelSize(0.120)
    rframe.GetXaxis().SetLabelOffset(0.012)
    rframe.GetXaxis().SetTitleOffset(0.85)
    rframe.GetYaxis().SetTitleOffset(0.53)
    rframe.GetXaxis().SetNdivisions(508)
    rframe.GetYaxis().CenterTitle(True)
    rframe.GetYaxis().SetTitle("ratio")
    rframe.GetXaxis().SetTitle(xtitle)
    rframe.GetYaxis().SetNdivisions(5)

    # RATIO
    ratios = []
    for i, (hist, gaus) in enumerate(hists):
        #if i==0: continue
        #ratio = hist.Clone(hist.GetName()+"_ratio")
        #ratio.Divide(hist0)
        ratio = divideHists(hist, hist0, name=hist.GetName() + "_ratio")
        ratio.Draw('HISTSAME')
        ratios.append(ratio)
        #ratiof = createTH1FromTF1(gaus,nbins,xmin,xmax)
        #ratiof.Divide(hist0)
        ratiof = divideTF1ByTH1(gaus, hist0, name=gaus.GetName() + "_ratio")
        ratiof.Draw('HISTSAME')
        ratios.append(ratiof)
        print ratiof
    #line = TLine(xmin,1.,xmax,1.)
    #line.SetLineColor(hist0.GetLineColor())
    #line.SetLineWidth(hist0.GetLineWidth())
    #line.SetLineStyle(1)
    #line.Draw('SAME')

    gPad.SetGrid()
    gPad.Modified()
    rframe.Draw('sameaxis')

    canvas.SaveAs(canvasname + ".png")
    canvas.SaveAs(canvasname + ".pdf")
    canvas.Close()
    print ">>> "
Exemplo n.º 25
0
def main(optunf="Bayes"):

    optunfs = ["Bayes", "SVD", "TUnfold", "Invert", "Reverse"]
    if not optunf in optunfs:
        txt = "Unfolding option " + optunf + " not recognised"
        raise ValueError(txt)

    global hReco, hMeas, hTrue

    print "==================================== TRAIN ===================================="
    # Create response matrix object for 40 measured and 20
    # unfolded bins:
    response = RooUnfoldResponse(40, -10.0, 10.0, 20, -10.0, 10.0)

    #  Train with a Breit-Wigner, mean 0.3 and width 2.5.
    for i in xrange(100000):
        # xt= gRandom.BreitWigner( 0.3, 2.5 )
        xt = gRandom.Gaus(0.0, 5.0)
        x = smear(xt)
        if x != None:
            response.Fill(x, xt)
        else:
            response.Miss(xt)

    print "==================================== TEST ====================================="
    hTrue = TH1D("true", "Test Truth", 20, -10.0, 10.0)
    hMeas = TH1D("meas", "Test Measured", 40, -10.0, 10.0)
    #  Test with a Gaussian, mean 0 and width 2.
    for i in xrange(10000):
        # xt= gRandom.Gaus( 0.0, 2.0 )
        xt = gRandom.BreitWigner(0.3, 2.5)
        x = smear(xt)
        hTrue.Fill(xt)
        if x != None:
            hMeas.Fill(x)

    print "==================================== UNFOLD ==================================="
    print "Unfolding method:", optunf
    if "Bayes" in optunf:
        # Bayes unfoldung with 4 iterations
        # unfold= RooUnfoldBayes( response, hMeas, 4 )
        unfold = RooUnfoldBayes(response, hMeas, 10, False, True)
    elif "SVD" in optunf:
        # SVD unfoding with free regularisation
        # unfold= RooUnfoldSvd( response, hMeas, 20 )
        unfold = RooUnfoldSvd(response, hMeas)
    elif "TUnfold" in optunf:
        # TUnfold with fixed regularisation tau=0.002
        # unfold= RooUnfoldTUnfold( response, hMeas )
        unfold = RooUnfoldTUnfold(response, hMeas, 0.002)
    elif "Invert" in optunf:
        unfold = RooUnfoldInvert(response, hMeas)
    elif "Reverse" in optunf:
        unfold = RooUnfoldBayes(response, hMeas, 1)

    hReco = unfold.Hreco()
    # unfold.PrintTable( cout, hTrue )
    unfold.PrintTable(cout, hTrue, 2)

    hReco.Draw()
    hMeas.Draw("SAME")
    hTrue.SetLineColor(8)
    hTrue.Draw("SAME")

    return
Exemplo n.º 26
0
outFileName = "../input/myTMVA.root"
outFile = TFile(outFileName, "recreate")
print "[MakeInputTMVA.py] Hello, I just created ", outFileName

TreeS = TTree("TreeS", "Tree of signal events TMVA")
TreeB = TTree("TreeB", "Tree of background events TMVA")
x1 = array("f", [0.])
x2 = array("f", [0.])
TreeS.Branch("x1", x1, "x1/F")
TreeS.Branch("x2", x2, "x2/F")
TreeB.Branch("x1", x1, "x1/F")
TreeB.Branch("x2", x2, "x2/F")

for i in range(100000):
    #signal
    x1[0] = gRandom.Gaus(1., 1.)
    x2[0] = gRandom.Gaus(10., 1.)
    TreeS.Fill()
    #background
    x1[0] = gRandom.Gaus(2., 1.)
    x2[0] = gRandom.Gaus(9., 0.8)
    TreeB.Fill()
#-------------------------------
outFile.Write()
outFile.Close()
print "[MakeInputTMVA.py] Goodbye!"

#### HOW TO DECLARE ARRAYS IN PYTHON
#maxn = 10
#n = array('i',[0]) # array int[1]
#d = array('f',maxn*[0.]) # array float[maxn]
Exemplo n.º 27
0
from ROOT import THStack, TH1D, gRandom
from math import sqrt

hist1 = TH1D("hist1", "hist1", 10, 0, 100)
hist1.Sumw2()

for i in xrange(1000):
    weight1 = gRandom.Gaus(1, 0.1)
    hist1.Fill(gRandom.Gaus(55, 20), weight1)

bincontents1 = []
for i, binc in enumerate(hist1):
    bincontents1.append((i, binc, hist1.GetBinError(i)))

print "\n>>> before scaling"
print ">>>   hist1.Integral()   = %s" % hist1.Integral()
print ">>>   hist1.GetEntries() = %s" % hist1.GetEntries()

scale = 0.5
hist1.Scale(scale)
bincontents2 = []
for i, binc in enumerate(hist1):
    bincontents2.append((i, binc, hist1.GetBinError(i)))

print "\n>>> after scaling with scale=%s" % scale
print ">>>   hist1.Integral()   = %s" % hist1.Integral()
print ">>>   hist1.GetEntries() = %s" % hist1.GetEntries()

print "\n>>> comparing bin contents"
print ">>>   %3s %20s %20s" % ("", "before scaling", "after scaling")
print ">>>   %3s    %8s %8s    %8s %8s" % ("bin", "content", "error",
Exemplo n.º 28
0
import ROOT
from ROOT import TCanvas, TH1F
from ROOT import gROOT, gRandom

h = ROOT.TH1F("h", "h", 100, -5, 5)
for i in range(1000000):
    d = gRandom.Gaus(0)
    h.Fill(d)
Exemplo n.º 29
0
 def smear(self, xt):
     xsmear = gRandom.Gaus(self.mean, self.sigma)
     return xt + xsmear
Exemplo n.º 30
0
#Tianai
from ROOT import TH1D, gRandom

h1 = TH1D("hist","histogram",100,-5,5)
for i in xrange(10000):
	hist = gRandom.Gaus()
	h1.Fill(hist)
h1.Draw()