def GetSensor(detid, layerid, color=ROOT.kGreen + 2):
    # print("looking for detid="+str(detid)+" and layerid="+str(layerid))
    row = df[(df['detid'] == detid) & (df['layerid'] == layerid)]
    # print("row=",row)
    x = row["translation_x"].tolist()[0]
    y = row["translation_y"].tolist()[0]
    z = row["translation_z"].tolist()[0]
    xsize = row["size_x"].tolist()[0]
    ysize = row["size_y"].tolist()[0]
    xhalf = xsize / 2
    yhalf = ysize / 2
    # print("x=",x)
    # print("y=",y)
    # print("z=",z)
    # print("xsize=",xsize)
    # print("ysize=",ysize)
    xarr = array.array('d',
                       [x - xhalf, x - xhalf, x + xhalf, x + xhalf, x - xhalf])
    yarr = array.array('d',
                       [y - yhalf, y + yhalf, y + yhalf, y - yhalf, y - yhalf])
    zarr = array.array('d', [z, z, z, z, z])
    n = len(xarr)
    sensor = TPolyLine3D(n, xarr, yarr, zarr)
    sensor.SetLineColor(color)
    return sensor
def GetTrackLine(rlist, color=ROOT.kRed):
    n = len(rlist)
    track = TPolyLine3D(n + 1)
    track.SetLineColor(color)
    for i in range(n):
        track.SetPoint(i, rlist[i][0], rlist[i][1], rlist[i][2])
    return track
def seed3dfitSVD(name,r1,r2,r3,r4,dodraw):
   g = TGraph2D()
   g.SetMarkerSize(3)
   g.SetMarkerStyle(20)
   g.SetMarkerColor(ROOT.kRed)
   g.SetLineColor(ROOT.kRed)
   g.SetPoint(0,r1[2],r1[1],r1[0])
   g.SetPoint(1,r2[2],r2[1],r2[0])
   g.SetPoint(2,r3[2],r3[1],r3[0])
   g.SetPoint(3,r4[2],r4[1],r4[0])
   g.GetXaxis().SetRangeUser(290,340)
   g.GetYaxis().SetRangeUser(-0.8,+0.8)
   g.GetZaxis().SetRangeUser( 0 if(isel(r1[0])) else xPsideL, xEsideR if(isel(r1[0])) else 0 )
   
   x = np.array([r1[0],r2[0],r3[0],r4[0]])
   y = np.array([r1[1],r2[1],r3[1],r4[1]])
   z = np.array([r1[2],r2[2],r3[2],r4[2]])

   data = np.concatenate((x[:, np.newaxis], 
                          y[:, np.newaxis], 
                          z[:, np.newaxis]), 
                         axis=1)

   # Calculate the mean of the points, i.e. the 'center' of the cloud
   datamean = data.mean(axis=0)

   # Do an SVD on the mean-centered data (Singular Value Decomposition)
   uu, dd, vv = np.linalg.svd(data - datamean) 

   # Now vv[0] contains the first principal component, i.e. the direction
   # vector of the 'best fit' line in the least squares sense.

   # Now generate some points along this best fit line, for plotting.

   # I use -7, 7 since the spread of the data is roughly 14
   # and we want it to have mean 0 (like the points we did
   # the svd on). Also, it's a straight line, so we only need 2 points.
   # linepts = vv[0] * np.mgrid[-7:7:2j][:, np.newaxis]
   linepts = vv[0] * np.mgrid[-50:50:2j][:, np.newaxis]

   # shift by the mean to get the line in the right place
   linepts += datamean
   
   if(dodraw):
      lfit = TPolyLine3D()
      for point in linepts:
         lfit.SetNextPoint(point[2],point[1],point[0])
      lfit.SetLineColor(ROOT.kBlue)
      cnv = TCanvas("","",2000,2000)
      view = TView.CreateView(1)
      xviewmin = 0 if(isel(r1[0])) else xPsideL
      xviewmax = xEsideR if(isel(r1[0])) else 0
      view.SetRange(290,-0.8, xviewmin , 340,+0.8,xviewmax)
      view.ShowAxis()
      g.Draw("p0")
      lfit.Draw("smae")
      cnv.SaveAs(name)
   
   return linepts, dd ## dd is a 1D array of the data singular values
def GetExtendedTrackLine(rlist, color=ROOT.kOrange):
    n = len(rlist)
    track = TPolyLine3D(n + 1)
    track.SetLineColor(color)
    x = xofz(rlist[1], rlist[0], zDipoleExit)
    y = yofz(rlist[1], rlist[0], zDipoleExit)
    rDipoleExit = [x, y, zDipoleExit]
    rlistextended = [rlist[1], rDipoleExit]
    for i in range(n):
        track.SetPoint(i, rlistextended[i][0], rlistextended[i][1],
                       rlistextended[i][2])
    return track
def seed3dfit(name,r1,r2,r3,r4,dodraw):
   g = TGraph2D()
   g.SetMarkerSize(3)
   g.SetMarkerStyle(20)
   g.SetMarkerColor(ROOT.kRed)
   g.SetLineColor(ROOT.kRed)
   g.SetPoint(0,r1[2],r1[1],r1[0])
   g.SetPoint(1,r2[2],r2[1],r2[0])
   g.SetPoint(2,r3[2],r3[1],r3[0])
   g.SetPoint(3,r4[2],r4[1],r4[0])
   g.GetXaxis().SetRangeUser(290,340)
   g.GetYaxis().SetRangeUser(-0.8,+0.8)
   g.GetZaxis().SetRangeUser(0 if(isel(r1[0])) else xPsideL, xEsideR if(isel(r1[0])) else 0 )
   
   x = np.array([r1[0],r2[0],r3[0],r4[0]])
   y = np.array([r1[1],r2[1],r3[1],r4[1]])
   z = np.array([r1[2],r2[2],r3[2],r4[2]])
   
   # this will find the slope and x-intercept of a plane
   # parallel to the y-axis that best fits the data
   A_xz = np.vstack((x, np.ones(len(x)))).T
   # m_xz, c_xz = np.linalg.lstsq(A_xz, z,rcond=None)[0]
   result_xz = np.linalg.lstsq(A_xz, z,rcond=None)
   m_xz, c_xz = result_xz[0]
   residuals_xz = result_xz[1]

   # again for a plane parallel to the x-axis
   A_yz = np.vstack((y, np.ones(len(y)))).T
   # m_yz, c_yz = np.linalg.lstsq(A_yz, z,rcond=None)[0]
   result_yz = np.linalg.lstsq(A_yz, z,rcond=None)
   m_yz, c_yz = result_yz[0]
   residuals_yz = result_yz[1]

   if(dodraw):
      zz = np.array([300,310,320,330])
      xx,yy = line3d(zz, m_xz,c_xz,m_yz,c_yz)
      lfit = TPolyLine3D()
      for i in range(4):
         lfit.SetNextPoint(zz[i],yy[i],xx[i])
      lfit.SetLineColor(ROOT.kBlue)
      cnv = TCanvas("","",2000,2000)
      view = TView.CreateView(1)
      xviewmin = 0 if(isel(r1[0])) else xPsideL
      xviewmax = xEsideR if(isel(r1[0])) else 0
      view.SetRange(290,-0.8, xviewmin , 340,+0.8,xviewmax)
      view.ShowAxis()
      g.Draw("p0")
      lfit.Draw("smae")
      cnv.SaveAs(name)
   
   return residuals_xz,residuals_yz
def GetDipole(color=ROOT.kBlack):
    xarr = array.array('d', [
        -xDipoleWidth / 2, -xDipoleWidth / 2, +xDipoleWidth / 2,
        +xDipoleWidth / 2, -xDipoleWidth / 2
    ])
    yarr = array.array('d', [
        -yDipoleHeight / 2, +yDipoleHeight / 2, +yDipoleHeight / 2,
        -yDipoleHeight / 2, -yDipoleHeight / 2
    ])
    zarr = array.array(
        'd', [zDipoleExit, zDipoleExit, zDipoleExit, zDipoleExit, zDipoleExit])
    n = len(xarr)
    dipole = TPolyLine3D(n, xarr, yarr, zarr)
    dipole.SetLineColor(color)
    return dipole
def windowline(points,isclosed=True):
   line = TPolyLine3D()
   arr = []
   for i in range(points.GetN()):
      r = [ROOT.Double(), ROOT.Double(), ROOT.Double()]
      points.GetPoint(i,r[0],r[1],r[2])
      if(r[0]==0): continue
      arr.append([r[0],r[1],r[2]])      
   if(isclosed):
      r = [ROOT.Double(), ROOT.Double(), ROOT.Double()]
      points.GetPoint(0,r[0],r[1],r[2])
      arr.append([r[0],r[1],r[2]])
   for point in arr:
      line.SetNextPoint(point[0],point[1],point[2])
   return line
Esempio n. 8
0
def bkgtracks(N):
    tracks = []
    resolution = 0.001  ## cm (10 um)
    rnd = TRandom()
    rnd.SetSeed()
    for i in range(N):
        R = cfgmap["Rbeampipe"] - cfgmap["Wbeampipe"]
        phi = rnd.Uniform(0, 2 * ROOT.TMath.Pi())
        x0 = R * ROOT.TMath.Cos(phi)
        y0 = R * ROOT.TMath.Sin(phi)
        z0 = cfgmap["zDipoleExit"]

        z4 = cfgmap["zLayer4"]
        x4 = rnd.Uniform(1.15 * cfgmap["xPsideL"], 1.15 * cfgmap["xEsideR"])
        y4 = rnd.Uniform(-1.15 * cfgmap["Hstave"] / 2,
                         1.15 * cfgmap["Hstave"] / 2)

        r0 = [x0, y0, z0]
        r4 = [x4, y4, z4]

        z1 = cfgmap["zLayer1"]
        x1 = xofz(r4, r0, z1) + rnd.Gaus(0, resolution)
        y1 = yofz(r4, r0, z1) + rnd.Gaus(0, resolution)

        z2 = cfgmap["zLayer2"]
        x2 = xofz(r4, r0, z2) + rnd.Gaus(0, resolution)
        y2 = yofz(r4, r0, z2) + rnd.Gaus(0, resolution)

        z3 = cfgmap["zLayer3"]
        x3 = xofz(r4, r0, z3) + rnd.Gaus(0, resolution)
        y3 = yofz(r4, r0, z3) + rnd.Gaus(0, resolution)

        z5 = 360
        x5 = xofz(r4, r0, z5)
        y5 = yofz(r4, r0, z5)

        line = TPolyLine3D()
        line.SetNextPoint(x0, y0, z0)
        line.SetNextPoint(x1, y1, z1)
        line.SetNextPoint(x2, y2, z2)
        line.SetNextPoint(x3, y3, z3)
        line.SetNextPoint(x4, y4, z4)
        line.SetNextPoint(x5, y5, z5)
        line.SetLineColor(ROOT.kRed)
        tracks.append(line)

    return tracks
Esempio n. 9
0
            start = [
                chain.MuCS_Start_TPC[0], chain.MuCS_Start_TPC[1],
                chain.MuCS_Start_TPC[2]
            ]
            end_tpc = [
                chain.MuCS_Start[0], chain.MuCS_Start[1], chain.MuCS_Start[2]
            ]
            direction = [a_i - b_i for a_i, b_i in zip(end_tpc, start)]
            end = [
                chain.MuCS_Start_TPC[0] - direction[0],
                chain.MuCS_Start_TPC[1] - direction[1],
                chain.MuCS_Start_TPC[2] - direction[2]
            ]

            polyline = TPolyLine3D(2)
            polyline.SetPoint(0, start[0], start[1], start[2])
            polyline.SetPoint(1, end[0], end[1], end[2])

            #red_lines.append(polyline)

        if theta_mucs > 75 and theta_mucs < 105:
            start = [
                chain.MuCS_Start_TPC[0], chain.MuCS_Start_TPC[1],
                chain.MuCS_Start_TPC[2]
            ]
            end_tpc = [
                chain.MuCS_Start[0], chain.MuCS_Start[1], chain.MuCS_Start[2]
            ]
            direction = [a_i - b_i for a_i, b_i in zip(end_tpc, start)]
            end = [
def seed2dfit(name,r1,r2,r3,r4,dodraw):
   gxyz = TGraph2D()
   gxyz.SetMarkerSize(1)
   gxyz.SetMarkerStyle(24)
   gxyz.SetMarkerColor(ROOT.kBlack)
   gxyz.SetPoint(0,r1[2],r1[1],r1[0])
   gxyz.SetPoint(1,r2[2],r2[1],r2[0])
   gxyz.SetPoint(2,r3[2],r3[1],r3[0])
   gxyz.SetPoint(3,r4[2],r4[1],r4[0])
   gxyz.GetXaxis().SetRangeUser(290,340)
   gxyz.GetYaxis().SetRangeUser(-0.8,+0.8)
   gxyz.GetZaxis().SetRangeUser( 0 if(isel(r1[0])) else xPsideL, xEsideR if(isel(r1[0])) else 0 )
      
   gxz = TGraph()
   gxz.SetMarkerSize(1)
   gxz.SetMarkerStyle(24)
   gxz.SetMarkerColor(ROOT.kBlack)
   gxz.SetPoint(0,r1[2],r1[0])
   gxz.SetPoint(1,r2[2],r2[0])
   gxz.SetPoint(2,r3[2],r3[0])
   gxz.SetPoint(3,r4[2],r4[0])
   gxz.GetXaxis().SetRangeUser(290,340)
   if(isel(r1[0])): gxz.GetYaxis().SetRangeUser(0,xEsideR)
   else:            gxz.GetYaxis().SetRangeUser(xPsideL,0)
   
   gyz = TGraph()
   gyz.SetMarkerSize(1)
   gyz.SetMarkerStyle(24)
   gyz.SetMarkerColor(ROOT.kBlack)
   gyz.SetLineColor(ROOT.kRed)
   gyz.SetPoint(0,r1[2],r1[1])
   gyz.SetPoint(1,r2[2],r2[1])
   gyz.SetPoint(2,r3[2],r3[1])
   gyz.SetPoint(3,r4[2],r4[1])
   gyz.GetXaxis().SetRangeUser(290,340)
   gyz.GetYaxis().SetRangeUser(-0.8,+0.8)
   
   fxz = TF1("fxz","pol1",300,330)
   fitr_xz = gxz.Fit(fxz,"Q")
   fitf_xz = gxz.FindObject("fxz")
   
   fyz = TF1("fyz","pol1",300,330)
   fitr_yz = gyz.Fit(fyz,"Q")
   fitf_yz = gyz.FindObject("fyz")
   
   if(dodraw):
      lfit = TPolyLine3D()
      for z in [300,310,320,330]:
         x = fitf_xz.Eval(z)
         y = fitf_yz.Eval(z)
         lfit.SetNextPoint(z,y,x)
      lfit.SetLineColor(ROOT.kBlue)
      cnv = TCanvas("","",1500,500)
      cnv.Divide(3,1)
      cnv.cd(1)
      view = TView.CreateView(1)
      xviewmin = 0 if(isel(r1[0])) else xPsideL
      xviewmax = xEsideR if(isel(r1[0])) else 0
      view.SetRange(290,-0.8, xviewmin , 340,+0.8,xviewmax)
      view.ShowAxis()
      gxyz.Draw("p0")
      lfit.Draw("smae")
      cnv.cd(2)
      gxz.Draw()
      cnv.cd(3)
      gyz.Draw()
      cnv.SaveAs(name)
   
   chi2_xz = fitf_xz.GetChisquare()/fitf_xz.GetNDF()
   chi2_yz = fitf_yz.GetChisquare()/fitf_yz.GetNDF()
   prob_xz = fitf_xz.GetProb()
   prob_yz = fitf_yz.GetProb()
   return  chi2_xz,prob_xz,chi2_yz,prob_yz