| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

pmtHits.py

Go to the documentation of this file.
00001 #############################################################################
00002 ###
00003 ### Python script for visualizing PMT hits on AD detectors
00004 ###
00005 ### Kim Boddy, kboddy@caltech.edu
00006 ### 31 Jul 2008 Created initial code
00007 ### 01 Aug 2008 Implemented code to use Gaudi Services, from: 
00008 ###    NuWa-trunk/dybgaudi/Detector/DetHelpers/python/DetHelpers/pmtCoords.py
00009 ###
00010 #############################################################################
00011 
00012 # Load ROOT.  Be sure to do this before loading Gaudi.
00013 import ROOT
00014 # Need CLHEP classes for geometry
00015 ROOT.gSystem.Load("libCLHEPRflx")
00016 # GaudiPython overwrites ROOT, so save a reference
00017 _ROOT = ROOT
00018 
00019 # Configure the detector geometry
00020 import xmldetdesc
00021 xmldetdesc.config()
00022 
00023 # Get units information
00024 from GaudiKernel import SystemOfUnits as units
00025 
00026 # Set configuration for the PMT Geometry Service
00027 from Gaudi.Configuration import *
00028 conf = ApplicationMgr()
00029 from DetHelpers.DetHelpersConf import PmtGeomInfoSvc
00030 pgisvc = PmtGeomInfoSvc("PmtGeomInfoSvc")
00031 #pgisvc.OutputLevel = 1
00032 pgisvc.StreamItems = [ "/dd/Structure/DayaBay" ]
00033 
00034 # Add a dummy algorithm to initialize gaudi 
00035 from GaudiPython.GaudiAlgs import GaudiAlgo
00036 from GaudiPython import *
00037 class InitAlg(GaudiAlgo):
00038     """A dummy algorithm that does nothing but provide access to gaudi
00039     services"""
00040     def __init__(self,name):
00041         GaudiAlgo.__init__(self,name)
00042         print "Making InitAlg",name
00043 
00044     def initialize(self):
00045         status = GaudiAlgo.initialize(self)
00046         return status
00047 
00048     def execute(self):
00049         return SUCCESS
00050 
00051 # Prepare Application
00052 from GaudiPython import AppMgr
00053 app = AppMgr()
00054 
00055 app.EvtSel = "NONE"
00056 app.ExtSvc += [ "PmtGeomInfoSvc" ]
00057 initalg = InitAlg("InitAlg")
00058 app.setAlgorithms( [ initalg ] )
00059 
00060 # Run Application (which does nothing but initialize gaudi)
00061 app.run(1)
00062 
00063 # GaudiPython overwrites ROOT, so restore from saved reference
00064 ROOT = _ROOT
00065 del _ROOT
00066 
00067 # Get the pmt geometry service
00068 #pmtSvc = initalg.pgis
00069 pmtSvc = initalg.svc("IPmtGeomInfoSvc","PmtGeomInfoSvc")
00070 
00071 #################################################################
00072 # From here on out, we treat this like a normal ROOT script.
00073 
00074 import ROOT
00075 
00076 ### Input settings
00077 # set output: contour plot (0), exact hit location (1), or exact 2D (2)
00078 output_st = raw_input("Choose to plot as contour (default), exact, exact2d: ")
00079 if    output_st=='contour': output=0
00080 elif  output_st=='exact':   output=1
00081 elif  output_st=='exact2d': output=2
00082 else: output=0
00083 # set input file and associated tree
00084 file_st = raw_input("ROOT file to read (default:electron.root): ")
00085 tree_st = raw_input("Tree file to read (default:DetSimValiTree): ")
00086 if not file_st: file_st = 'electron.root'
00087 if not tree_st: tree_st = 'DetSimValiTree'
00088 f = ROOT.TFile(file_st,'READ') # ROOT will return error if file_st does not exist
00089 tree = f.Get(tree_st)          # ROOT will return error if tree_st does not exist
00090 
00091 ### Indexing Conventions
00092 # site:  DayaBay=0x01, LingAo=0x02, Far=0x04
00093 # DetID: AD1=1, AD2=2, AD3=3, AD4=4
00094 # pmtID consists of 32 bits, 4 pieces of 8-bit information strung together
00095 #   site, detector ID, ring, column 
00096 #   (i.e., site info is leftmost 8 bits and column info is rightmost 8 bits)
00097 
00098 ### Create histograms and graphs for 8 detectors
00099 hist = [ROOT.TH2F('hist0','PMT Hits: DB site, AD1',24,1,25,8,1,9),
00100         ROOT.TH2F('hist1','PMT Hits: DB site, AD2',24,1,25,8,1,9),
00101         ROOT.TH2F('hist2','PMT Hits: LA site, AD1',24,1,25,8,1,9),
00102         ROOT.TH2F('hist3','PMT Hits: LA site, AD2',24,1,25,8,1,9),
00103         ROOT.TH2F('hist4','PMT Hits: far site, AD1',24,1,25,8,1,9),
00104         ROOT.TH2F('hist5','PMT Hits: far site, AD2',24,1,25,8,1,9),
00105         ROOT.TH2F('hist6','PMT Hits: far site, AD3',24,1,25,8,1,9),
00106         ROOT.TH2F('hist7','PMT Hits: far site, AD4',24,1,25,8,1,9)]
00107 
00108 if output==1:
00109     gr = [ROOT.TGraph(),ROOT.TGraph(),ROOT.TGraph(),ROOT.TGraph(),
00110           ROOT.TGraph(),ROOT.TGraph(),ROOT.TGraph(),ROOT.TGraph()]
00111 
00112 if output==2:
00113     gr = [ROOT.TGraph2D(),ROOT.TGraph2D(),ROOT.TGraph2D(),ROOT.TGraph2D(),
00114           ROOT.TGraph2D(),ROOT.TGraph2D(),ROOT.TGraph2D(),ROOT.TGraph2D()]
00115 
00116 if (output==1 or output==2):
00117     gr[0].SetTitle('PMT Hits: DB site, AD1')
00118     gr[1].SetTitle('PMT Hits: DB site, AD2')
00119     gr[2].SetTitle('PMT Hits: LA site, AD1')
00120     gr[3].SetTitle('PMT Hits: LA site, AD2')
00121     gr[4].SetTitle('PMT Hits: far site, AD1')
00122     gr[5].SetTitle('PMT Hits: far site, AD2')
00123     gr[6].SetTitle('PMT Hits: far site, AD3')
00124     gr[7].SetTitle('PMT Hits: far site, AD4')
00125 
00126 c = []
00127 
00128 ### Set Units
00129 meter  = float(units.meter)
00130 degree = float(units.degree)
00131 
00132 ### Read tree
00133 entries = tree.GetEntries()
00134 
00135 for j in xrange(entries):
00136     i = tree.LoadTree(j)
00137     if i < 0:
00138         break
00139 
00140     nb = tree.GetEntry(j)
00141     if nb <=0:
00142         continue
00143 
00144     SimHits = tree.SimHitsEntries
00145     pmtID   = tree.sensID
00146     hitx    = tree.hitx
00147     hity    = tree.hity
00148     hitz    = tree.hitz
00149     if SimHits<0:
00150         continue
00151 
00152     for k in xrange(SimHits):
00153         site = pmtID[k]>>24
00154         det  = ((site<<24)^pmtID[k])>>16
00155         ring = (((site<<24)|(det<<16))^pmtID[k])>>8
00156         col  = (((site<<24)|(det<<16)|(ring<<8))^pmtID[k])
00157 
00158         if   (site==1) and (det==1): idx=0
00159         elif (site==1) and (det==2): idx=1
00160         elif (site==2) and (det==1): idx=2
00161         elif (site==2) and (det==2): idx=3
00162         elif (site==4) and (det==1): idx=4
00163         elif (site==4) and (det==2): idx=5
00164         elif (site==4) and (det==3): idx=6
00165         elif (site==4) and (det==4): idx=7
00166         else:
00167             print 'Unexpected site/AdID number - skipping this hit'
00168             continue
00169         
00170         # Get detector information
00171         pmtGeom     = pmtSvc.get(pmtID[k])      # get PMT geometry
00172         adDetector  = pmtGeom.parentDetector()  # get AD for this PMT
00173         pmtDetector = pmtGeom.detectorElement() # get PMT detector
00174         
00175         # Find PMT hit points 
00176         pmtHit = gbl.Gaudi.XYZPoint(hitx[k],hity[k],hitz[k]) # hit on PMT (x,y,z)
00177         gpHit  = pmtDetector.geometry().toGlobal(pmtHit)     # global hit position
00178         lpHit  = adDetector.geometry().toLocal(gpHit)        # local hit in AD coords
00179             
00180         # Convert (x,y,z) coords to (rho,z,phi) coords 
00181         # Is there a Gaudi Service for this?
00182         x = lpHit.x() / meter
00183         y = lpHit.y() / meter
00184         z = lpHit.z() / meter
00185         rho = ROOT.TMath.Sqrt(x*x + y*y) 
00186         phi = ROOT.TMath.ATan2(y,x) / degree
00187 
00188         hist[idx].Fill(col,ring)
00189         NPoint = int(hist[idx].GetEntries())-1 #since SetPoint starts at point 0
00190         if output==1: gr[idx].SetPoint(NPoint,phi,z)
00191         if output==2: gr[idx].SetPoint(NPoint,x,y,z)
00192 
00193 ROOT.gStyle.SetPalette(1)
00194 ROOT.gStyle.SetOptStat(11)
00195 
00196 ### Draw only non-empty histograms/graphs
00197 for i in xrange(8):
00198     if hist[i].GetEntries()==0: continue
00199     c += [ROOT.TCanvas()]   
00200 
00201     if output==0:   
00202         hist[i].GetXaxis().SetTitle('Column')
00203         hist[i].GetYaxis().SetTitle('Ring')
00204         hist[i].Draw('COLZ')
00205     elif output==1: 
00206         gr[i].GetXaxis().SetTitle('#phi [deg]')
00207         gr[i].GetYaxis().SetTitle('Z [m]')
00208         gr[i].SetMarkerStyle(1)
00209         gr[i].SetMarkerColor(2)
00210         gr[i].Draw('AP')
00211     elif output==2:
00212         gr[i].GetXaxis().SetTitle('X [m]')
00213         gr[i].GetYaxis().SetTitle('Y [m]')
00214         gr[i].GetZaxis().SetTitle('Z [m]')
00215         gr[i].GetXaxis().SetTitleOffset(1.75)
00216         gr[i].GetYaxis().SetTitleOffset(1.75)
00217         gr[i].GetZaxis().SetTitleOffset(1)
00218         gr[i].SetMarkerStyle(1)
00219         gr[i].SetMarkerColor(2)
00220         gr[i].Draw('P')
00221     else: print 'Invalid draw command: problem with reading user input'
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:56:26 2011 for DetSimValidation by doxygen 1.4.7