1 import ROOT,os,sys,getopt
4 from pythia8_conf
import addHNLtoROOT
5 from array
import array
8 from FitTrackInfo
import FitTrackInfo
14 def __init__(self, tree, modules, resolution, debug=0, mhistdict=None, ship_geo=None):
36 ROOT.gRandom.SetSeed(13)
44 if (mhistdict
and ship_geo) :
45 fm = ROOT.genfit.FieldManager.getInstance()
47 sbf = ROOT.ShipBellField(
"wilfried", ship_geo.Bfield.max,ship_geo.Bfield.z,2,ship_geo.Yheight/2.*u.m )
48 for i
in range (0,300):
50 pvec3 = ROOT.TVector3(0,0,z)
55 fm.getField().get(0,0,z,fx,fy,fz)
56 fvec3f = ROOT.TVector3(fx,fy,fz)
58 fvec3s = ROOT.TVector3( sbf.GetBx(pvec3.X(),pvec3.Y(),pvec3.Z()),sbf.GetBy(pvec3.X(),pvec3.Y(),pvec3.Z()),sbf.GetBz(pvec3.X(),pvec3.Y(),pvec3.Z()))
62 mhistdict[
'magZfit'].Fill(z,fvec3f.Mag())
63 mhistdict[
'magZsim'].Fill(z,fvec3s.Mag())
65 zdict = {1:2500., 2:2800., 3:3000.}
67 for xi
in range (-30, 30):
68 for yi
in range (-30,30):
71 pvec3 = ROOT.TVector3(x, y, zdict[zi])
76 fm.getField().get(x,y,zdict[zi],fx,fy,fz)
77 fvec3f = ROOT.TVector3(fx,fy,fz)
79 fvec3s = ROOT.TVector3( sbf.GetBx(pvec3.X(),pvec3.Y(),pvec3.Z()),sbf.GetBy(pvec3.X(),pvec3.Y(),pvec3.Z()),sbf.GetBz(pvec3.X(),pvec3.Y(),pvec3.Z()))
83 mhistdict[
'magXY'+str(zi)+
"fit"].Fill(x, y,fvec3f.Mag())
84 mhistdict[
'magXY'+str(zi)+
"sim"].Fill(x, y,fvec3s.Mag())
91 self.__trackHits.clear()
92 self.__oldSmearedHits.clear()
93 self.__trackEdgeHits.clear()
94 self.__vetoHits.clear()
95 self.__nStations.clear()
123 return self.__reFitTracks.getTrIDs()
128 return self.__reFitTracks.getChi2Ndf(tid)
133 return self.__reFitTracks.getNdf(tid)
141 return self.__reFitTracks.getVertex()
150 if ( step>RecoSettings.VertexExtrSteps
or (
not self.__reFitTracks.Vertex) ) :
return None
158 return self.__reFitTracks.getPosDirPval(tid)
191 self.__reFitTracks.Print()
196 pos, direct, pval = theFitTracks.getPosDirPval(tid)
197 return self.__reFitTracks.compareTracks(tid, pos, direct, pval)
206 top = ROOT.TVector3()
207 bot = ROOT.TVector3()
211 self.
__modules[
"Strawtubes"].StrawEndPoints(detID,bot,top)
217 smearedHit = {
'xtop':top.x(),
'ytop':top.y(),
'z':top.z(),
'xbot':bot.x(),
'ybot':bot.y(),
'z':bot.z(),
'dist':smear}
220 print "\tsmear :",
"".join(
"{:8.2f}".format(self.
__trackHits[tid][hid][
'pos'](ii))
for ii
in range(0,3)),
221 print "{:6.2f}".format(dw),
222 print "\t(xt,xb, yt, yb, z, dw) : ",
223 for x
in [
'xtop',
'xbot',
'ytop',
'ybot',
'z',
'dist']:
224 print "".join(
"{:8.2f}".format(smearedHit[x])),
239 for ahit
in self.__tree.strawtubesPoint:
240 detID = ahit.GetDetectorID()
241 trID = ahit.GetTrackID()
245 origSmHit = self.__tree.SmearedHits.At(hindx)
246 if( (abs(ahit.GetZ()-origSmHit[3])>0.8)
or (abs(ahit.dist2Wire()-origSmHit[7])>0.2) ):
247 print "problem getting smeared his, but do not change anything"
248 print "=>", ahit.GetZ(), origSmHit[3], ahit.dist2Wire(), origSmHit[7]
254 if (
not self.__trackHits.has_key(trID)):
256 stationList[trID] = []
259 hinfo[
'pos'] = ROOT.TVector3(ahit.GetX(), ahit.GetY(), ahit.GetZ())
260 hinfo[
'det'] = ahit.GetDetectorID()
261 hinfo[
'dw'] = ahit.dist2Wire()
262 hinfo[
'smdw'] = origSmHit[7]
267 if(
not trID
in toSort):
269 if(self.
__debug>0):
print "StrawHitsEntry: wrong order of hits for track ", trID
271 station = int(ahit.GetDetectorID()/10000000)
272 if station > 4 :
continue
273 if (
not station
in stationList[trID]) : stationList[trID].append(station)
277 if(self.
__debug>0):
print "StrawHitsEntry: will sort hits for track ", trID
279 print "\t\thits to be sorted"
282 print "\t\t\t\t", vec3.X(),
"\t", vec3.Y(),
"\t", vec3.Z(), hinfo[
'dw']
283 self.
__trackHits[trID].sort(key=
lambda x: x[
'pos'].Z(), reverse=
False)
285 print "\t\thits after sorting"
288 print "\t\t\t\t", vec3.X(),
"\t", vec3.Y(),
"\t", vec3.Z(), hinfo[
'dw']
294 print "Number of crossed stations (trID:n)", trID,
" : ", self.
__nStations[trID]
299 print "hits for trID ", trID
302 print "\t", vec3.X(),
"\t", vec3.Y(),
"\t", vec3.Z(), hinfo[
'dw']
303 if(self.
__debug>0):
print "start/stop position for hits of track ", trID
307 while( firstHit<nHits
and (self.
__trackHits[trID][firstHit][
'pos'].Z()<0) ):
312 if( (firstHit<nHits)
and ((nHits-firstHit)>RecoSettings.trackMinNofHits) ):
320 print "\t", pos, vec3.X(),
"\t", vec3.Y(),
"\t", vec3.Z()
321 elif( self.
__debug>0):
print "not set due to small number of hits"
337 dv = ROOT.TVector3(0., 0., 1.)
339 print "trying to get initial direction having just one hit, will set (0,0,1)"
340 return dv*(1./dv.Mag())
345 pos = ROOT.TVector3(0, 0, 0)
346 mom = ROOT.TVector3(0,0,3.*u.GeV)
347 cov = ROOT.TMatrixDSym(6)
349 for i
in range(3): cov[i][i] = resolution*resolution
350 cov[0][0]=resolution*resolution*100.
352 for i
in range(3,6): cov[i][i] = ROOT.TMath.pow(resolution / nM / ROOT.TMath.sqrt(3), 2)
356 cov = ROOT.TMatrixDSym(6)
358 for i
in range(3): cov[i][i] = resolution*resolution
359 cov[0][0]=resolution*resolution*100.
361 for i
in range(3,6): cov[i][i] = ROOT.TMath.pow(resolution / nM / ROOT.TMath.sqrt(3), 2)
377 mVector = ROOT.TVectorD(7,array(
'd',[sm[
'xtop'],sm[
'ytop'],sm[
'z'],sm[
'xbot'],sm[
'ybot'],sm[
'z'],sm[
'dist']]))
380 hitCov = ROOT.TMatrixDSym(7)
383 tp = ROOT.genfit.TrackPoint(fTrack)
384 measurement = ROOT.genfit.WireMeasurement(mVector,hitCov,1,6,tp)
386 measurement.setMaxDistance(0.5*u.cm)
388 tp.addRawMeasurement(measurement)
391 fTrack.insertPoint(tp)
401 self.__reFitTracks.clean()
413 if ( self.
__nStations<RecoSettings.trackMinNofStations):
continue
415 pdg = self.__tree.MCTrack[trID].GetPdgCode()
419 if( (
not charge)
or (charge==0) ):
420 print "StrawHits.FitTracks for TrID ", trID,
"finds charge of track of ", charge,
" and does nothing."
425 rep = ROOT.genfit.RKTrackRep(pdg)
426 stateSmeared = ROOT.genfit.MeasuredStateOnPlane(rep)
427 rep.setPosMomCov(stateSmeared, posM, momM, covM)
429 seedState = ROOT.TVectorD(6)
430 seedCov = ROOT.TMatrixDSym(6)
431 rep.get6DStateCov(stateSmeared, seedState, seedCov)
433 fitTrack[trID] = ROOT.genfit.Track(rep, seedState, seedCov)
434 ROOT.SetOwnership(fitTrack[trID],
False)
436 if(self.
__debug>2):
print "preparing measurements for track ID", trID
440 if not fitTrack[trID].checkConsistency():
441 print 'Problem with track before fit, not consistent',self.fitTrack[atrack]
443 try: self.__fitter.processTrack(fitTrack[trID])
445 print "genfit failed to fit track"
447 if not fitTrack[trID].checkConsistency():
448 print 'Problem with track after fit, not consistent',self.fitTrack[atrack]
451 stat = fitTrack[trID].getFitStatus()
452 if not stat.isFitConverged() :
continue
453 f = fitTrack[trID].getFittedState()
459 self.__reFitTracks.addNewTrack(trID, f.getPos(), f.getDir(), f.getMomMag(),
460 stat.getNdf(), stat.getChi2())
465 newFitTrIDs = self.__reFitTracks.getTrIDs()
466 twoTracks = ( len(newFitTrIDs)==2 )
470 self.__reFitTracks.createVertex(newFitTrIDs[0], newFitTrIDs[1], flag=0)
471 iniDoca = self.__reFitTracks.Doca
472 iniY = self.__reFitTracks.Vertex.Y()
473 while ( theStep<RecoSettings.VertexExtrSteps
and twoTracks):
475 newFitTrIDs = self.__reFitTracks.getTrIDs()
477 print "==>vertex ", theStep,
" ", self.__reFitTracks.Doca
478 self.__reFitTracks.Vertex.Print()
479 self.__docaEval.append(self.__reFitTracks.Doca)
480 for tid
in fitTrack :
482 state = fitTrack[tid].getFittedState()
484 print "can't get fittedState"
486 vPosEx = ROOT.TVector3(0,0,0)
487 vMomEx = ROOT.TVector3(0,0,0)
489 state.extrapolateToPoint(self.__reFitTracks.Vertex)
492 print "track exctrapolation failed!tid: ", tid
494 status = fitTrack[tid].getFitStatus()
498 self.__reFitTracks.addNewTrack(trID, state.getPos(), state.getDir(), state.getMomMag(),
499 status.getNdf(), status.getChi2(), verb=
False)
501 self.__reFitTracks.createVertex(newFitTrIDs[0], newFitTrIDs[1], flag)
502 self.__reFitTracks.Vertex.SetY(iniY)
504 twoTacks = ( len(self.__reFitTracks.getTrIDs())==2 )
505 return len(newFitTrIDs)
def __prepareIniPosMomCov
def readEvent
to be called per each event.
def getNofPHits
returns number of hits in proper tracker stations (Z>0) calculated from __trackHits and __vetoHits...
def getReFitPosDirPval
returns vertex (if number of tracks!=2 will return None!).
__trackHits
{MCtrackID : [{'pos':TVector3, 'det':detID, 'dw':distance to wire, 'smdw': smeared dw} where [TVector...
def getReFitTrIDs
returns list of keys __reFitTracks.
def getTrIDsALL
returns list of keys __trackHits (MCtrackIDs>0.
__resolution
hit resolition
__trackEdgeHits
{MCtrackID : {X : TVector3}} where x='entry' or 'exit', TVector3 coordinates of last or first hit...
def getStartHit
returns TVector3 of a tracker entry hit (Z>0) from __trackEdgeHits.
def __clean
to be called for each new event (called in readEvent()) cleans all dictionaries (__trackHits, __trackEdgeHits, __vetoHits, __nStations).
__random
root random engent for hit smearing (see __hitSmear).
def getTrIDs
returns list of keys __trackEdgeHits (MCtrackIDs>0 with more than RecoSettings .trackMinNofHits hits)...
def getStepDoca
returns doca's of each extrapolation steps (size is defined in RecoSettings .VertexExtrSteps).
__modules
geometry description modules.
def __prepareWireMeasurements
def __hitSmear
returns a dictionary {xtop, ytop, z, ybot, ybot, z, dist} for a smeared hit.
def getReFitVertex
returns vertex (if number of tracks!=2 will return None!).
__nStations
{MCtrackID: number of crossed stations (exclude veto tracker)}.
__vetoHits
{MCtrackID : number of hits at Z<0 (veto tracker)}.
__tree
root tree to be read.
def checkVetoHits
returns number of hits with Z<0 of __trackHits.