import ROOT, math, sys, os, copy, glob, subprocess pandapath=os.environ.get('PANDAPATH') sys.path.append(pandapath+'/macro/tpc/FOPI/mberger') sys.path.append(pandapath+'/padresponse_COMSOL') sys.path.append(pandapath+'/macro/tpc/FOPI/python/argparse-1.2.1') import argparse from padresponse import electron, electronCollection def findFile(x,y,elecfiles,rfiles): for f in rfiles: fname=findCoordsInFile(x,y,f) if fname: return fname for f in elecfiles: fname=findCoordsInFile(x,y,f) if fname: return fname return False def findCoordsInFile(x,y,f): basename=f.split('/')[-1] basename=basename.replace('.txt','') if basename.find('elecDrift')==-1: return False words=basename.split('_') #print words xfile=float(words[1]) yfile=float(words[2]) #print x,xfile,y,yfile if abs(x-xfile)<1e-10 and abs(y-yfile)<1e-10: return f else: return False def getResponse(fname): if fname.find('.txt')!=-1: print fname print 'no root file existent, creating it' response=subprocess.check_output(['python', 'padresponse_COMSOL/calc_padresponse.py', fname, '--batch', '--rfile', args.rpath+'/'+fname.split('/')[-1].replace('.txt','.root'), '--pfile', args.rpath.replace('rfiles','pdf')+'/'+fname.split('/')[-1].replace('.txt','.pdf')]) response=response.splitlines()[-1] response=float(response.split(':')[-1]) if fname.find('.root')!=-1: #print 'read from root file' rfile=ROOT.TFile(fname,'read') hist=rfile.Get('hc') response=0 for i in range(hist.GetNbinsX()): val=hist.GetBinContent(i) if val!=val: print '******************************' print '******************************' print '******************************' print '******************************' print '******************************' return -2 if val>0: response+=val #response=hist.Integral() rfile.Close() return response def addHistFromFile(fname,hist,hname): if fname.find('.root')!=-1: rfile=ROOT.TFile(fname,'read') ahist=rfile.Get(hname) if ahist==None: return False hist.Add(ahist) rfile.Close() return True else: return False parser=argparse.ArgumentParser(description='calculate the padresponse function for xy scan over pad') parser.add_argument('path',help='path to the folder with the files for each xy bin',type=str) parser.add_argument('rpath',help='path to the folder with root files',type=str) parser.add_argument('--xrange',help='the range for x in [start,stop,step]',type=float,default=[-0.005,0.00325,0.000125],nargs=3) parser.add_argument('--yrange',help='the range for y in [start,stop,step]',type=float,default=[-0.005,0.00325,0.000125],nargs=3) parser.add_argument('--useSym',help='produce the padresponse map with help of symmetry',action='store_true') parser.add_argument('--responsefile',help='the file to write the full padresponse (bin wise) into)',type=str,default='padresponse.txt') args=parser.parse_args() ROOT.gROOT.ProcessLine(".x rootlogon.C") ROOT.gROOT.ProcessLine("gStyle->SetPalette(1)") ROOT.gROOT.LoadMacro("stlPYROOT.h+") canvas=ROOT.TCanvas() elecfiles=glob.glob(args.path+'/*.txt') rfiles=glob.glob(args.rpath+'/*.root') #get normalisation factor print 'getting normalization' normalize=1 fname=findFile(0,0,elecfiles,rfiles) if fname: normalize=getResponse(fname) #get all the other reponse full_padresponse=[] factor=1 if args.useSym: factor=2 args.xrange[1]=0 args.yrange[1]=0 nxbins= factor * int ( math.ceil( (args.xrange[1]-args.xrange[0])/args.xrange[2]) ) nybins= factor * int ( math.ceil( (args.yrange[1]-args.yrange[0])/args.yrange[2]) ) for i in range(nxbins+1): full_padresponse.append([]) for j in range(nybins+1): full_padresponse[i].append(-1) hposx=ROOT.TH1D('hposx','Pos X (cm)',800,-0.004,0.004) hposy=ROOT.TH1D('hposy','Pos Y (cm)',800,-0.004,0.004) hposxy=ROOT.TH2D('hposxy','Pos XY (cm)',800,-0.004,0.004,800,-0.004,0.004) xit=args.xrange[0] while xit<=args.xrange[1]: if os.path.isfile(args.path+'/stop'): break yit=args.yrange[0] while yit<=args.yrange[1]: if os.path.isfile(args.path+'/stop'): break print 'at x=',xit,'y=',yit fname=findFile(xit,yit,elecfiles,rfiles) print fname if fname: #print fname if not addHistFromFile(fname,hposx,'hfpos0'): print 'no xpos hist found' if not addHistFromFile(fname,hposy,'hfpos1'): print 'no ypos hist found' if not addHistFromFile(fname,hposxy,'hfinalpos'): print 'no xypos hist found' response=getResponse(fname) if response==-2: print 'NAN detected delete root file' print fname os.system('rm '+fname) response*=normalize #print response/normalize xbin=int(round( (xit-args.xrange[0])/args.xrange[2] )) ybin=int(round( (yit-args.yrange[0])/args.yrange[2] )) #print xbin,ybin # print nxbins,xbin,nxbins-xbin,xit,(xit-args.xrange[0])/args.xrange[2],xit-args.xrange[0] #print nybins,ybin,nybins-ybin,yit,(yit-args.yrange[0])/args.yrange[2],yit-args.yrange[0] if full_padresponse[xbin][ybin]!=-1: print '******************ALARM FILLING FILLED********************' norm_response=response/normalize if norm_response>1: norm_response=1 full_padresponse[xbin][ybin]=norm_response if args.useSym: #print nxbins,xbin,nxbins-xbin,len(full_padresponse) full_padresponse[nxbins-xbin][ybin]=norm_response full_padresponse[xbin][nybins-ybin]=norm_response full_padresponse[nxbins-xbin][nybins-ybin]=norm_response yit+=args.yrange[2] if abs(yit)<1e-10: #print 'setting yit to 0',yit yit=0 xit+=args.xrange[2] if abs(xit)<1e-10: xit=0 hpadres=ROOT.TH2D('hpadres','Padresponse', nxbins+1,args.xrange[0],-args.xrange[0], nybins+1,args.yrange[0],-args.yrange[0]) outfile=open(args.responsefile,'w') outfile.write(str(nxbins+1)+" "+str(nybins+1)+" "+str(args.xrange[2]/100.)+"\n") for i in range(nxbins+1): for j in range(nybins+1): hpadres.SetBinContent(i+1,j+1,full_padresponse[i][j]) outfile.write(str(i)+" "+str(j)+" "+str(full_padresponse[i][j])+"\n") outfile.close() cpadres=ROOT.TCanvas('cpadres','Padresponse') hpadres.SetStats(0) hpadres.Draw('colz') cpos=ROOT.TCanvas('cpos','Position') cpos.Divide(2,2) cpos.cd(1) hposx.Draw() cpos.cd(2) hposy.Draw() cpos.cd(3) hposxy.Draw() u='w' while u!='q': u=raw_input('done?')