-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDataFormats.py
More file actions
160 lines (125 loc) · 5.95 KB
/
DataFormats.py
File metadata and controls
160 lines (125 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from ROOT import TLorentzVector
import ROOT
from math import fabs,sqrt
from CMGTools.RootTools.utils.DeltaR import deltaR,deltaPhi
from array import array
import numpy
class ShowerFromChargedPion(object):
def __init__(self,track,dr = 0.5):
self.track=track
self.dr = dr
self.ecalEntrance =track.extrapolatedPoint(4).position()
self.hcalEntrance =track.extrapolatedPoint(6).position()
self.trackMomentum = self.track.trajectoryPoints()[0].momentum().energy()
self.trackVector = self.track.trajectoryPoints()[0].momentum()
self.ecalVector=None
self.hcalVector=None
self.ecalConstituents=[]
self.hcalConstituents=[]
self.hcalTiming=[]
#for visualization
#type = 0 track 1 ECAL , 2 HCAL
self.typeO=[-1,0]
self.eta=[self.ecalEntrance.Eta(),self.hcalEntrance.Eta()]
self.phi=[self.ecalEntrance.Phi(),self.hcalEntrance.Phi()]
self.ieta=[0,0]
self.iphi=[0,0]
self.rho=[self.ecalEntrance.Rho(),self.ecalEntrance.Rho()]
self.energy=[self.trackMomentum,self.trackMomentum]
self.depth=[-1,-1]
def vectorFromConstituent(self,cluster):
vec = ROOT.TVector3(cluster.position().x(),cluster.position().y(),cluster.position().z())
vec= vec.Unit()
vec*=cluster.energy()
return ROOT.TLorentzVector(vec.x(),vec.y(),vec.z(),cluster.energy())
def addConstituent(self,constituent,ecal = False):
vec = self.vectorFromConstituent(constituent)
if ecal and deltaR(vec.Eta(),vec.Phi(),self.trackVector.Eta(),self.trackVector.Phi())<self.dr:
self.ecalConstituents.append(constituent)
if self.ecalVector is None:
self.ecalVector = vec
else:
self.ecalVector += vec
self.typeO.append(1)
self.depth.append(0)
self.eta.append(constituent.position().Eta()-self.ecalEntrance.Eta())
self.phi.append(deltaPhi(constituent.position().Phi(),self.ecalEntrance.Phi()))
self.rho.append(constituent.position().Rho()-self.ecalEntrance.Rho())
self.energy.append(constituent.energy())
self.ieta.append(0)
self.iphi.append(0)
return True
elif deltaR(vec.Eta(),vec.Phi(),self.trackVector.Eta(),self.trackVector.Phi())<self.dr:
self.hcalConstituents.append(constituent)
self.hcalTiming.append(constituent.time())
if self.hcalVector is None:
self.hcalVector = vec
else:
self.hcalVector += vec
self.typeO.append(2)
hcalid = ROOT.HcalDetId(constituent.detId())
self.depth.append(hcalid.depth())
self.ieta.append(hcalid.ieta())
self.iphi.append(hcalid.iphi())
self.eta.append(constituent.position().Eta()-self.ecalEntrance.Eta())
self.phi.append(deltaPhi(constituent.position().Phi(),self.ecalEntrance.Phi()))
self.rho.append(constituent.position().Rho()-self.ecalEntrance.Rho())
self.energy.append(constituent.energy())
return True
else:
return False
def calculateTrackDetId(self):
if len(self.hcalConstituents)>0:
constituents=sorted(self.hcalConstituents,key = lambda x: deltaR(x.position().Eta(),x.position().Phi(),self.ecalEntrance.Eta(),self.ecalEntrance.Phi()))
hcalid = ROOT.HcalDetId(constituents[0].detId())
self.ieta[1] = hcalid.ieta()
self.iphi[1] = hcalid.iphi()
def makeVisTree(self,tfile,treename):
tfile.cd()
self.calculateTrackDetId()
tree =ROOT.TTree(treename,treename)
typeO=numpy.zeros(1,float)
tree.Branch("type",typeO,'type/D')
eta=numpy.zeros(1,float)
tree.Branch("eta",eta,'eta/D')
phi=numpy.zeros(1,float)
tree.Branch("phi",phi,'phi/D')
ieta=numpy.zeros(1,float)
tree.Branch("ieta",ieta,'ieta/D')
iphi=numpy.zeros(1,float)
tree.Branch("iphi",iphi,'iphi/D')
rho=numpy.zeros(1,float)
tree.Branch("rho",rho,'rho/D')
energy=numpy.zeros(1,float)
tree.Branch("energy",energy,'energy/D')
depth=numpy.zeros(1,float)
tree.Branch("depth",depth,'depth/D')
for t,e,p,r,en,d,ie,ip in zip(self.typeO,self.eta,self.phi,self.rho,self.energy,self.depth,self.ieta,self.iphi):
typeO[0]=t
eta[0]=e
phi[0]=p
rho[0]=r
energy[0]=en
depth[0]=d
ieta[0]=ie
iphi[0]=ip
tree.Fill()
tree.Write()
def __str__(self):
str = """
New Shower from Pion
--------------------
"""
str=str+ 'track pt,eta,phi,p,ecal eta, ecal phi, hcal eta, hcal phi {pt},{eta},{phi},{p},{eeta},{ephi},{heta},{hphi} '.format(pt=self.trackVector.Pt(),eta=self.trackVector.Eta(),phi=self.trackVector.Phi(),p=self.trackMomentum,eeta=self.ecalEntrance.Eta(),ephi=self.ecalEntrance.Phi(),heta=self.hcalEntrance.Eta(),hphi=self.hcalEntrance.Phi())+'\n'
str=str+ """ECAL constituents
-----------------
"""
for c in self.ecalConstituents:
str=str+ 'ID={id},Drho={rho},eta={eta},Dphi={phi},z={z},energy={energy} \n'.format(id= c.detId(),rho=c.position().rho(),eta=c.position().eta(),phi=c.position().phi(),z=c.position().z(),energy=c.energy())
str=str+ """HCAL constituents
-----------------
"""
for c in self.hcalConstituents:
hcalid = ROOT.HcalDetId(c.detId())
str=str+ 'ID={id},Drho={rho},Deta={eta},Dphi={phi},z={z},energy={energy},depth={depth}\n'.format(id= c.detId(),rho=c.position().rho(),eta=c.position().eta(),phi=c.position().phi(),z=c.position().z(),energy=c.energy(),depth=hcalid.depth())
return str