File indexing completed on 2026-04-09 07:48:50
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 """
0022 tconcentric.py
0023 =============================================
0024
0025 Loads test events from Opticks and Geant4 and
0026 created by OKG4Test and
0027 compares their bounce histories.
0028
0029 """
0030 import os, sys, logging, numpy as np
0031 log = logging.getLogger(__name__)
0032
0033 from opticks.ana.base import opticks_main
0034 from opticks.ana.nbase import vnorm, costheta_
0035 from opticks.ana.ab import AB
0036 from opticks.ana.cfh import CFH
0037
0038 STEP = 4
0039
0040 X,Y,Z=0,1,2
0041
0042
0043 def butterfly_yz(plt, a, b, pt):
0044 plt.subplot(1, 2, 1)
0045 plt.scatter(a[:,pt,Y],a[:,pt,Z])
0046
0047 plt.subplot(1, 2, 2)
0048 plt.scatter(b[:,pt,Y],b[:,pt,Z])
0049
0050
0051 def butterfly(plt, ab):
0052 """
0053 Expecting 8cc6ccd
0054
0055 TO BT BT SC BT BT AB
0056 p0 p1 p2 p3 p4 p5 p6
0057
0058 p3: scatter occurs at point on X axis
0059 p4: first intersection point after the scatter
0060 """
0061 a,b = ab.rpost()
0062 butterfly_yz(plt, a, b, pt=4)
0063
0064
0065 def isolated_scatter():
0066 """
0067 Without selection scatter distrib plots
0068 """
0069 OLDMOM,OLDPOL,NEWMOM,NEWPOL = 0,1,2,3
0070 aa = np.load(os.path.expandvars("$TMP/RayleighTest/ok.npy"))
0071 bb = np.load(os.path.expandvars("$TMP/RayleighTest/cfg4.npy"))
0072
0073 bins = 100
0074 nx = 6
0075 ny = 2
0076
0077
0078
0079 qwns = [
0080 (1,aa[:,NEWMOM,X],"Amomx"),
0081 (2,aa[:,NEWMOM,Y],"Amomy"),
0082 (3,aa[:,NEWMOM,Z],"Amomz"),
0083 (4,aa[:,NEWPOL,X],"Apolx"),
0084 (5,aa[:,NEWPOL,Y],"Apoly"),
0085 (6,aa[:,NEWPOL,Z],"Apolz"),
0086
0087 (7,bb[:,NEWMOM,X],"Bmomx"),
0088 (8,bb[:,NEWMOM,Y],"Bmomy"),
0089 (9,bb[:,NEWMOM,Z],"Bmomz"),
0090 (10,bb[:,NEWPOL,X],"Bpolx"),
0091 (11,bb[:,NEWPOL,Y],"Bpoly"),
0092 (12,bb[:,NEWPOL,Z],"Bpolz"),
0093
0094 ]
0095
0096 for i,q,label in qwns:
0097 plt.subplot(ny, nx, i)
0098 plt.hist(q, bins=bins, histtype="step", label=label)
0099 pass
0100 plt.show()
0101
0102
0103
0104
0105 def dirpol(ab, flg0="SC"):
0106
0107 iflg = ab.iflg(flg0)
0108 nrec = ab.nrec()
0109
0110 fr0 = iflg - 1
0111 fr1 = nrec - 1
0112
0113 bins = 100
0114 nx = 12
0115 ny = fr1 - fr0
0116 offset = 0
0117
0118 log.info(" lab0 %s fr0 %d fr1 %d ny %d " % (lab0, fr0, fr1, ny))
0119
0120 for fr in range(fr0,fr1):
0121 to = fr + 1
0122
0123 adir, bdir = ab.rdir(fr, to)
0124 apol, bpol = ab.rpol_(fr)
0125
0126 qwns = [
0127 (adir[:,X],"adirx"),
0128 (adir[:,Y],"adiry"),
0129 (adir[:,Z],"adirz"),
0130 (apol[:,X],"apolx"),
0131 (apol[:,Y],"apoly"),
0132 (apol[:,Z],"apolz"),
0133
0134 (bdir[:,X],"bdirx"),
0135 (bdir[:,Y],"bdiry"),
0136 (bdir[:,Z],"bdirz"),
0137 (bpol[:,X],"bpolx"),
0138 (bpol[:,Y],"bpoly"),
0139 (bpol[:,Z],"bpolz"),
0140 ]
0141
0142 for i,(q,label) in enumerate(qwns):
0143 plt.subplot(ny, nx, offset+i+1)
0144 plt.hist(q, bins=bins, histtype="step", label=label)
0145 pass
0146 offset += nx
0147 pass
0148 plt.show()
0149
0150
0151
0152 def abplt(a,b, bins=100,nx=2,ny=1,offset=0, title=""):
0153 """
0154 Two subplots containg x,y,z coordinates of a and b
0155 """
0156 ax = a[:,0]
0157 ay = a[:,1]
0158 az = a[:,2]
0159
0160 nax = np.count_nonzero(np.isnan(ax))
0161 nay = np.count_nonzero(np.isnan(ay))
0162 naz = np.count_nonzero(np.isnan(az))
0163
0164 if nax+nay+naz > 0:
0165 log.warning("A: nan found in %s nax %d nay %d naz %d " % (title, nax, nay, naz))
0166
0167 ax = ax[~np.isnan(ax)]
0168 ay = ay[~np.isnan(ay)]
0169 az = az[~np.isnan(az)]
0170
0171 bx = b[:,0]
0172 by = b[:,1]
0173 bz = b[:,2]
0174
0175 nbx = np.count_nonzero(np.isnan(bx))
0176 nby = np.count_nonzero(np.isnan(by))
0177 nbz = np.count_nonzero(np.isnan(bz))
0178
0179 if nbx+nby+nbz > 0:
0180 log.warning("B: nan found in %s nbx %d nby %d nbz %d " % (title, nbx, nby, nbz))
0181
0182 bx = bx[~np.isnan(bx)]
0183 by = by[~np.isnan(by)]
0184 bz = bz[~np.isnan(bz)]
0185
0186 plt.subplot(ny,nx,1+offset+0)
0187 plt.hist(ax,bins=bins,histtype="step", label="ax")
0188 plt.hist(ay,bins=bins,histtype="step", label="ay")
0189 plt.hist(az,bins=bins,histtype="step", label="az")
0190
0191 plt.subplot(ny,nx,1+offset+1)
0192 plt.hist(bx,bins=bins,histtype="step", label="bx")
0193 plt.hist(by,bins=bins,histtype="step", label="by")
0194 plt.hist(bz,bins=bins,histtype="step", label="bz")
0195
0196
0197 def dirpol(ab, fr, to):
0198 """
0199 """
0200
0201 log.info("dirpol fr %d to %d " % (fr, to ))
0202
0203 nx = 2
0204 ny = 2
0205
0206 a,b = ab.rdir(fr=fr,to=to)
0207 abplt(a,b, bins=100, nx=nx, ny=ny, offset=0, title="dirpol/rdir")
0208
0209 a,b = ab.rpol_(fr=fr)
0210 abplt(a,b, bins=100, nx=nx, ny=ny, offset=ny, title="dirpol/rpol")
0211
0212 plt.show()
0213
0214
0215
0216 def scatter(ab):
0217 """
0218 """
0219 ab.sel = "TO SC BT BT BT BT SA"
0220
0221 fr = ab.iflg("SC")
0222
0223 a_opol, b_opol = ab.rpol_(fr=fr-1)
0224 a_npol, b_npol = ab.rpol_(fr=fr)
0225
0226 a_odir, b_odir = ab.rdir(fr=fr-1,to=fr)
0227 a_ndir, b_ndir = ab.rdir(fr=fr,to=fr+1)
0228
0229 a_ct = costheta_( np.cross(a_opol, a_ndir ), a_opol )
0230 b_ct = costheta_( np.cross(b_opol, b_ndir) , b_opol )
0231
0232 return a_ct, b_ct
0233
0234
0235
0236
0237 def poldot(ab, fr, oldpol=[0,1,0], bins=100):
0238 """
0239 dot product between old and new polarization
0240
0241
0242
0243 o_pol,o_dir / n_pol,n_dir
0244 | /
0245 |--------/
0246
0247
0248 * https://bugzilla-geant4.kek.jp/show_bug.cgi?id=207
0249
0250 New pol needs to be
0251
0252 * Perpendicular to the new momentum vector
0253 * Same plane as the new momentum vector and initial polarization vector
0254
0255
0256 OpRayleigh.cc::
0257
0258 164 // calculate the new polarization direction
0259 165 // The new polarization needs to be in the same plane as the new
0260 166 // momentum direction and the old polarization direction
0261 167 OldPolarization = aParticle->GetPolarization();
0262 168 constant = -NewMomentumDirection.dot(OldPolarization);
0263 ...
0264 170 NewPolarization = OldPolarization + constant*NewMomentumDirection;
0265 ///
0266 /// linear combination of oldpol and newdir is in same plane as these
0267 ///
0268 171 NewPolarization = NewPolarization.unit();
0269
0270
0271 ::
0272
0273 constant = -n_dir.o_pol
0274
0275 n_pol = o_pol + (-n_dir.o_pol )n_dir
0276
0277 n_dir.n_pol = n_dir.o_pol + (-n_dir.o_pol) n_dir.n_dir = 0
0278
0279
0280 o_pol ^ n_dir : is normal to the plane
0281
0282 n_pol.( o_pol ^ n_dir ) == 0
0283
0284
0285
0286 """
0287 a,b = ab.rpol_(fr=fr)
0288 act = costheta_( np.tile( oldpol, len(a) ).reshape(-1,3), a)
0289 bct = costheta_( np.tile( oldpol, len(b) ).reshape(-1,3), b)
0290
0291 plt.hist(act, bins=bins, histtype="step", label="act")
0292 plt.hist(bct, bins=bins, histtype="step", label="bct")
0293
0294 plt.show()
0295
0296
0297
0298
0299 def debug_plotting(ok, ab):
0300 pfxseqhis = ok.pfxseqhis
0301 pfxseqmat = ok.pfxseqmat
0302
0303 if len(pfxseqhis) > 0:
0304 log.info(" pfxseqhis [%s] " % (pfxseqhis))
0305
0306 ab.sel = pfxseqhis
0307
0308 fr = ab.iflg("SC")
0309
0310 if fr is None:
0311 log.fatal("expecting one line selection including SC")
0312 else:
0313 dirpol(ab, fr, fr+1)
0314 poldot(ab, fr )
0315 pass
0316 elif len(pfxseqmat) > 0:
0317 log.info(" pfxseqmat [%s] " % (pfxseqmat))
0318 ab.flv = "seqmat"
0319 ab.sel = pfxseqmat
0320 else:
0321 pass
0322
0323
0324
0325
0326 if __name__ == '__main__':
0327 ok = opticks_main(doc=__doc__, tag="1", src="torch", det="concentric", smry=True)
0328
0329 print "ok.smry %d " % ok.smry
0330 log.info(ok.brief)
0331
0332
0333
0334 ab = AB(ok)
0335 print ab
0336
0337 if not ok.ipython:
0338 log.info("early exit as non-interactive")
0339 sys.exit(0)
0340
0341
0342 debug_plotting(ok, ab)
0343
0344
0345
0346
0347
0348
0349
0350
0351