1179 def findTracks(self):
1180
1181 hitPosLists = {}
1182 hitPosLists_noT4 = {}
1183 stationCrossed = {}
1184 stationCrossed_noT4 = {}
1185 trackDigiHits = {}
1186 trackDigiHits_noT4 = {}
1187 trackMomentums = {}
1188 trackMomentums_noT4 = {}
1189 trackCandidates = []
1190 trackCandidates_noT4 = []
1191 trackCandidates_all = []
1192 nTrack = -1
1193
1194 self.fGenFitArray.Delete()
1195 self.fitTrack2MC.clear()
1196
1197
1198 if self.sTree.GetBranch("MufluxSpectrometerPoint"):
1199 if global_variables.withT0:
1200 self.SmearedHits = self.withT0Estimate()
1201 self.TaggerHits = self.muonTaggerHits_mc()
1202 else:
1203 self.SmearedHits = self.smearHits(global_variables.withNoStrawSmearing)
1204 self.TaggerHits = self.muonTaggerHits_mc()
1205 elif self.sTree.GetBranch("Digi_MufluxSpectrometerHits"):
1206 self.SmearedHits = self.smearHits_realData()
1207 self.TaggerHits = self.muonTaggerHits_realData()
1208 else:
1209 print('No branches "MufluxSpectrometer" or "Digi_MufluxSpectrometerHits".')
1210 return nTrack
1211
1212 if global_variables.realPR:
1213
1214
1215 track_hits =
MufluxPatRec.execute(self.SmearedHits, self.TaggerHits, withNTaggerHits, withDist2Wire)
1216
1217
1218 for i_track in track_hits.keys():
1219
1220 atrack = track_hits[i_track]
1221 atrack_y12 = atrack['y12']
1222 atrack_stereo12 = atrack['stereo12']
1223 atrack_34 = atrack['34']
1224 atrack_smeared_hits = list(atrack_y12) + list(atrack_stereo12) + list(atrack_34)
1225 atrack_p = np.abs(atrack['p'])
1226 atrack_y_in_magnet = atrack['y_in_magnet']
1227 atrack_x_in_magnet = atrack['x_in_magnet']
1228
1229 for sm in atrack_smeared_hits:
1230
1231
1232 detID = sm['detID']
1233 station = int(detID/10000000)
1234 trID = i_track
1235
1236
1237 if trID not in hitPosLists:
1238 hitPosLists[trID] = ROOT.std.vector('TVectorD')()
1239 stationCrossed[trID] = {}
1240 trackDigiHits[trID] = []
1241 trackMomentums[trID] = atrack_p
1242 m = array('d',[sm['xtop'],sm['ytop'],sm['z'],sm['xbot'],sm['ybot'],sm['z'],sm['dist']])
1243 hitPosLists[trID].push_back(ROOT.TVectorD(7,m))
1244 if station not in stationCrossed[trID]:
1245 stationCrossed[trID][station]=0
1246 stationCrossed[trID][station]+=1
1247 trackDigiHits[trID].append(sm['digiHit'])
1248
1249
1250 if (int(detID/1000000)!=40):
1251 if trID not in hitPosLists_noT4:
1252 hitPosLists_noT4[trID] = ROOT.std.vector('TVectorD')()
1253 stationCrossed_noT4[trID] = {}
1254 trackDigiHits_noT4[trID] = []
1255 trackMomentums_noT4[trID] = atrack_p
1256 m_noT4 = array('d',[sm['xtop'],sm['ytop'],sm['z'],sm['xbot'],sm['ybot'],sm['z'],sm['dist']])
1257 hitPosLists_noT4[trID].push_back(ROOT.TVectorD(7,m_noT4))
1258 if station not in stationCrossed_noT4[trID]:
1259 stationCrossed_noT4[trID][station]=0
1260 stationCrossed_noT4[trID][station]+=1
1261 trackDigiHits_noT4[trID].append(sm['digiHit'])
1262
1263
1264 else:
1265
1266 if not self.sTree.GetBranch("MufluxSpectrometerPoint"):
1267 print('Fake PatRec is impossible. No "MufluxSpectrometerPoint" branch.')
1268 return nTrack
1269
1270 track_hits = {}
1271 for sm in self.SmearedHits:
1272
1273
1274 detID = sm['detID']
1275 station = int(detID/10000000)
1276 vnb = (detID - station * 10000000) // 1000000
1277 is_y12 = (station == 1) * (vnb == 0) + (station == 2) * (vnb == 1)
1278 is_stereo12 = (station == 1) * (vnb == 1) + (station == 2) * (vnb == 0)
1279 is_34 = (station == 3) + (station == 4)
1280
1281 trID = self.sTree.MufluxSpectrometerPoint[sm['digiHit']].GetTrackID()
1282
1283
1284 if trID not in track_hits:
1285 atrack = {'y12': [], 'stereo12': [], '34': []}
1286 track_hits[trID] = atrack
1287 if is_y12:
1288 track_hits[trID]['y12'].append(sm)
1289 if is_stereo12:
1290 track_hits[trID]['stereo12'].append(sm)
1291 if is_34:
1292 track_hits[trID]['34'].append(sm)
1293
1294
1295 if trID not in hitPosLists:
1296 hitPosLists[trID] = ROOT.std.vector('TVectorD')()
1297 stationCrossed[trID] = {}
1298 trackDigiHits[trID] = []
1299 trackMomentums[trID] = 3.
1300 m = array('d',[sm['xtop'],sm['ytop'],sm['z'],sm['xbot'],sm['ybot'],sm['z'],sm['dist']])
1301 hitPosLists[trID].push_back(ROOT.TVectorD(7,m))
1302 if station not in stationCrossed[trID]:
1303 stationCrossed[trID][station]=0
1304 stationCrossed[trID][station]+=1
1305 trackDigiHits[trID].append(sm['digiHit'])
1306
1307
1308 if (int(detID/1000000)!=40):
1309 if trID not in hitPosLists_noT4:
1310 hitPosLists_noT4[trID] = ROOT.std.vector('TVectorD')()
1311 stationCrossed_noT4[trID] = {}
1312 trackDigiHits_noT4[trID] = []
1313 trackMomentums_noT4[trID] = 3.
1314 m_noT4 = array('d',[sm['xtop'],sm['ytop'],sm['z'],sm['xbot'],sm['ybot'],sm['z'],sm['dist']])
1315 hitPosLists_noT4[trID].push_back(ROOT.TVectorD(7,m_noT4))
1316 if station not in stationCrossed_noT4[trID]:
1317 stationCrossed_noT4[trID][station]=0
1318 stationCrossed_noT4[trID][station]+=1
1319 trackDigiHits_noT4[trID].append(sm['digiHit'])
1320
1321
1322
1323 for atrack in hitPosLists:
1324
1325 if atrack < 0:
1326 continue
1327 pdg = 13
1328 if not abs(pdg)==13:
1329 continue
1330
1331 meas = hitPosLists[atrack]
1332 mom_init = trackMomentums[atrack]
1333 nM = meas.size()
1334
1335
1336
1337
1338
1339
1340 charge = self.PDG.GetParticle(pdg).Charge()/(3.)
1341 posM = ROOT.TVector3(0, 0, 0)
1342 momM = ROOT.TVector3(0,0,mom_init * u.GeV)
1343
1344 covM = ROOT.TMatrixDSym(6)
1345 resolution = self.sigma_spatial
1346 if global_variables.withT0:
1347 resolution = resolution*1.4
1348 for i in range(3):
1349 covM[i][i] = resolution*resolution
1350 covM[0][0]=resolution*resolution*100.
1351 for i in range(3,6):
1352 covM[i][i] = ROOT.TMath.Power(resolution / nM / ROOT.TMath.Sqrt(3), 2)
1353
1354 rep = ROOT.genfit.RKTrackRep(pdg)
1355
1356 stateSmeared = ROOT.genfit.MeasuredStateOnPlane(rep)
1357 rep.setPosMomCov(stateSmeared, posM, momM, covM)
1358
1359 seedState = ROOT.TVectorD(6)
1360 seedCov = ROOT.TMatrixDSym(6)
1361 rep.get6DStateCov(stateSmeared, seedState, seedCov)
1362 theTrack = ROOT.genfit.Track(rep, seedState, seedCov)
1363 hitCov = ROOT.TMatrixDSym(7)
1364 hitCov[6][6] = resolution*resolution
1365 for m in meas:
1366 tp = ROOT.genfit.TrackPoint(theTrack)
1367 measurement = ROOT.genfit.WireMeasurement(m,hitCov,1,6,tp)
1368 measurement.setMaxDistance(1.85*u.cm)
1369
1370 tp.addRawMeasurement(measurement)
1371 theTrack.insertPoint(tp)
1372 trackCandidates_all.append([theTrack,atrack])
1373
1374
1375
1376 for atrack in hitPosLists:
1377
1378 if atrack < 0:
1379 continue
1380 pdg = 13
1381 if not abs(pdg)==13:
1382 continue
1383
1384 meas = hitPosLists[atrack]
1385 mom_init = trackMomentums[atrack]
1386 nM = meas.size()
1387
1388
1389
1390 if len(stationCrossed[atrack]) < 4 :
1391 continue
1392
1393 charge = self.PDG.GetParticle(pdg).Charge()/(3.)
1394 posM = ROOT.TVector3(0, 0, 0)
1395 momM = ROOT.TVector3(0,0,mom_init * u.GeV)
1396
1397 covM = ROOT.TMatrixDSym(6)
1398 resolution = self.sigma_spatial
1399 if global_variables.withT0:
1400 resolution = resolution*1.4
1401 for i in range(3):
1402 covM[i][i] = resolution*resolution
1403 covM[0][0]=resolution*resolution*100.
1404 for i in range(3,6):
1405 covM[i][i] = ROOT.TMath.Power(resolution / nM / ROOT.TMath.Sqrt(3), 2)
1406
1407 rep = ROOT.genfit.RKTrackRep(pdg)
1408
1409 stateSmeared = ROOT.genfit.MeasuredStateOnPlane(rep)
1410 rep.setPosMomCov(stateSmeared, posM, momM, covM)
1411
1412 seedState = ROOT.TVectorD(6)
1413 seedCov = ROOT.TMatrixDSym(6)
1414 rep.get6DStateCov(stateSmeared, seedState, seedCov)
1415 theTrack = ROOT.genfit.Track(rep, seedState, seedCov)
1416 hitCov = ROOT.TMatrixDSym(7)
1417 hitCov[6][6] = resolution*resolution
1418 for m in meas:
1419 tp = ROOT.genfit.TrackPoint(theTrack)
1420 measurement = ROOT.genfit.WireMeasurement(m,hitCov,1,6,tp)
1421 measurement.setMaxDistance(1.85*u.cm)
1422
1423 tp.addRawMeasurement(measurement)
1424 theTrack.insertPoint(tp)
1425 trackCandidates.append([theTrack,atrack])
1426
1427
1428 for atrack in hitPosLists_noT4:
1429
1430 if atrack < 0:
1431 continue
1432 pdg = 13
1433 if not abs(pdg)==13:
1434 continue
1435 meas = hitPosLists_noT4[atrack]
1436 mom_init = trackMomentums_noT4[atrack]
1437 nM = meas.size()
1438
1439
1440
1441 if len(stationCrossed[atrack]) < 3 :
1442 continue
1443
1444 charge = self.PDG.GetParticle(pdg).Charge()/(3.)
1445 posM = ROOT.TVector3(0, 0, 0)
1446 momM = ROOT.TVector3(0,0,mom_init * u.GeV)
1447
1448 covM = ROOT.TMatrixDSym(6)
1449 resolution = self.sigma_spatial
1450 if global_variables.withT0:
1451 resolution = resolution*1.4
1452 for i in range(3):
1453 covM[i][i] = resolution*resolution
1454 covM[0][0]=resolution*resolution*100.
1455 for i in range(3,6):
1456 covM[i][i] = ROOT.TMath.Power(resolution / nM / ROOT.TMath.Sqrt(3), 2)
1457
1458 rep = ROOT.genfit.RKTrackRep(pdg)
1459
1460 stateSmeared = ROOT.genfit.MeasuredStateOnPlane(rep)
1461 rep.setPosMomCov(stateSmeared, posM, momM, covM)
1462
1463 seedState = ROOT.TVectorD(6)
1464 seedCov = ROOT.TMatrixDSym(6)
1465 rep.get6DStateCov(stateSmeared, seedState, seedCov)
1466 theTrack = ROOT.genfit.Track(rep, seedState, seedCov)
1467 hitCov = ROOT.TMatrixDSym(7)
1468 hitCov[6][6] = resolution*resolution
1469 for m in meas:
1470 tp = ROOT.genfit.TrackPoint(theTrack)
1471 measurement = ROOT.genfit.WireMeasurement(m,hitCov,1,6,tp)
1472 measurement.setMaxDistance(1.85*u.cm)
1473
1474 tp.addRawMeasurement(measurement)
1475 theTrack.insertPoint(tp)
1476 trackCandidates_noT4.append([theTrack,atrack])
1477
1478 if global_variables.debug:
1479
1480 z_hits_y = []
1481 x_hits_y = []
1482 for ahit in self.SmearedHits:
1483 detID = ahit['detID']
1484 station = int(detID/10000000)
1485 vnb = (detID - station * 10000000) // 1000000
1486 is_y12 = (station == 1) * (vnb == 0) + (station == 2) * (vnb == 1)
1487 is_stereo12 = (station == 1) * (vnb == 1) + (station == 2) * (vnb == 0)
1488 is_34 = (station == 3) + (station == 4)
1489 if is_y12 or is_34:
1490 z_hits_y.append(ahit['z'])
1491 x_hits_y.append(ahit['xtop'])
1492 plt.figure(figsize=(22, 6))
1493 plt.subplot(1, 2, 1)
1494 plt.scatter(z_hits_y, x_hits_y, color='b')
1495
1496 z_tagger_hits_y = []
1497 x_tagger_hits_y = []
1498 for ahit in self.TaggerHits:
1499 detID = ahit['detID']
1500 station = detID // 10000
1501 direction = (detID - station * 10000) // 1000
1502 if direction == 1 or detID < 10:
1503 z_tagger_hits_y.append(ahit['z'])
1504 x_tagger_hits_y.append((ahit['xtop'] + ahit['xbot']) / 2.)
1505 plt.scatter(z_tagger_hits_y, x_tagger_hits_y, color='b')
1506
1507 for i_track in track_hits:
1508 atrack = track_hits[i_track]
1509 atrack_y12 = atrack['y12']
1510 atrack_stereo12 = atrack['stereo12']
1511 atrack_34 = atrack['34']
1512 atrack_tagger = atrack['y_tagger']
1513 atrack_y = list(atrack_y12) + list(atrack_34) + list(atrack_tagger)
1514 z_atrack_y = [ahit['z'] for ahit in atrack_y]
1515 x_atrack_y = [ahit['xtop'] for ahit in atrack_y]
1516 plt.scatter(z_atrack_y, x_atrack_y, color=np.random.rand(3,))
1517
1518 plt.xlabel('z')
1519 plt.ylabel('x')
1520 plt.xlim(0, 1200)
1521 plt.ylim(-150, 150)
1522
1523
1524 plt.subplot(1, 2, 2)
1525 for ahit in self.SmearedHits:
1526 detID = ahit['detID']
1527 station = int(detID/10000000)
1528 vnb = (detID - station * 10000000) // 1000000
1529 is_y12 = (station == 1) * (vnb == 0) + (station == 2) * (vnb == 1)
1530 is_stereo12 = (station == 1) * (vnb == 1) + (station == 2) * (vnb == 0)
1531 is_34 = (station == 3) + (station == 4)
1532 if is_y12 or is_stereo12:
1533 xbot, xtop = ahit['xbot'], ahit['xtop']
1534 ybot, ytop = ahit['ybot'], ahit['ytop']
1535 plt.plot([ybot, ytop], [xbot, xtop], color='b')
1536
1537 for i_track in track_hits:
1538 atrack = track_hits[i_track]
1539 atrack_y12 = atrack['y12']
1540 atrack_stereo12 = atrack['stereo12']
1541 atrack_34 = atrack['34']
1542 atrack_tagger = atrack['y_tagger']
1543 atrack_12 = list(atrack_y12) + list(atrack_stereo12)
1544 color = np.random.rand(3,)
1545 for ahit in atrack_12:
1546 xbot, xtop = ahit['xbot'], ahit['xtop']
1547 ybot, ytop = ahit['ybot'], ahit['ytop']
1548 plt.plot([ybot, ytop], [xbot, xtop], color=color)
1549
1550 plt.xlabel('y')
1551 plt.ylabel('x')
1552 plt.xlim(-50, 50)
1553 plt.ylim(-50, 50)
1554
1555 plt.savefig('pics/event_'+str(self.iEvent)+'.pdf', format='pdf', bbox_inches='tight')
1556 plt.clf()
1557 plt.close()
1558
1559
1560
1561
1562
1563
1564 if self.sTree.GetBranch("MufluxSpectrometerPoint"):
1565
1566
1567 (n_tracks, n_recognized, n_clones, n_ghosts, n_others) = self.target_metrics(track_hits)
1568 h['Reco_target'].Fill("N total", n_tracks)
1569 h['Reco_target'].Fill("N recognized tracks", n_recognized)
1570 h['Reco_target'].Fill("N clones", n_clones)
1571 h['Reco_target'].Fill("N ghosts", n_ghosts)
1572 h['Reco_target'].Fill("N others", n_others)
1573
1574 (n_tracks, n_recognized, n_clones, n_ghosts, n_others) = self.muon_metrics(track_hits)
1575 h['Reco_muon'].Fill("N total", n_tracks)
1576 h['Reco_muon'].Fill("N recognized tracks", n_recognized)
1577 h['Reco_muon'].Fill("N clones", n_clones)
1578 h['Reco_muon'].Fill("N ghosts", n_ghosts)
1579 h['Reco_muon'].Fill("N others", n_others)
1580
1581 (n_tracks, n_recognized, n_clones, n_ghosts, n_others) = self.muon_with_tagger_metrics(track_hits)
1582 h['Reco_muon_with_tagger'].Fill("N total", n_tracks)
1583 h['Reco_muon_with_tagger'].Fill("N recognized tracks", n_recognized)
1584 h['Reco_muon_with_tagger'].Fill("N clones", n_clones)
1585 h['Reco_muon_with_tagger'].Fill("N ghosts", n_ghosts)
1586 h['Reco_muon_with_tagger'].Fill("N others", n_others)
1587
1588 (n_tracks, n_recognized, n_clones, n_ghosts, n_others) = self.muon_metrics_vs_p(track_hits)
1589
1590 (n_tracks, n_recognized, n_clones, n_ghosts, n_others) = self.all_tracks_metrics(track_hits)
1591 h['Reco_all_tracks'].Fill("N total", n_tracks)
1592 h['Reco_all_tracks'].Fill("N recognized tracks", n_recognized)
1593 h['Reco_all_tracks'].Fill("N clones", n_clones)
1594 h['Reco_all_tracks'].Fill("N ghosts", n_ghosts)
1595 h['Reco_all_tracks'].Fill("N others", n_others)
1596
1597
1598 (n_tracks_y12, n_recognized_y12, n_clones_y12, n_ghosts_y12, n_others_y12,
1599 n_tracks_stereo12, n_recognized_stereo12, n_clones_stereo12, n_ghosts_stereo12, n_others_stereo12,
1600 n_tracks_34, n_recognized_34, n_clones_34, n_ghosts_34, n_others_34) = self.recognition_metrics(track_hits)
1601
1602 h['NTrueTracks'].Fill("Stations 1&2, Y views", n_tracks_y12)
1603 h['NTrueTracks'].Fill("Stations 1&2, Stereo views", n_tracks_stereo12)
1604 h['NTrueTracks'].Fill("Stations 3&4", n_tracks_34)
1605
1606 h['Reco_y12'].Fill("N total", n_tracks_y12)
1607 h['Reco_y12'].Fill("N recognized tracks", n_recognized_y12)
1608 h['Reco_y12'].Fill("N clones", n_clones_y12)
1609 h['Reco_y12'].Fill("N ghosts", n_ghosts_y12)
1610 h['Reco_y12'].Fill("N others", n_others_y12)
1611
1612 h['Reco_stereo12'].Fill("N total", n_tracks_stereo12)
1613 h['Reco_stereo12'].Fill("N recognized tracks", n_recognized_stereo12)
1614 h['Reco_stereo12'].Fill("N clones", n_clones_stereo12)
1615 h['Reco_stereo12'].Fill("N ghosts", n_ghosts_stereo12)
1616 h['Reco_stereo12'].Fill("N others", n_others_stereo12)
1617
1618 h['Reco_34'].Fill("N total", n_tracks_34)
1619 h['Reco_34'].Fill("N recognized tracks", n_recognized_34)
1620 h['Reco_34'].Fill("N clones", n_clones_34)
1621 h['Reco_34'].Fill("N ghosts", n_ghosts_34)
1622 h['Reco_34'].Fill("N others", n_others_34)
1623
1624
1625 (n_tracks_y12, n_recognized_y12, n_clones_y12, n_ghosts_y12, n_others_y12,
1626 n_tracks_stereo12, n_recognized_stereo12, n_clones_stereo12, n_ghosts_stereo12, n_others_stereo12,
1627 n_tracks_34, n_recognized_34, n_clones_34, n_ghosts_34, n_others_34) = self.recognition_metrics(track_hits, mode='3hits')
1628
1629 h['NTrueTracks_3hits'].Fill("Stations 1&2, Y views", n_tracks_y12)
1630 h['NTrueTracks_3hits'].Fill("Stations 1&2, Stereo views", n_tracks_stereo12)
1631 h['NTrueTracks_3hits'].Fill("Stations 3&4", n_tracks_34)
1632
1633 h['Reco_y12_3hits'].Fill("N total", n_tracks_y12)
1634 h['Reco_y12_3hits'].Fill("N recognized tracks", n_recognized_y12)
1635 h['Reco_y12_3hits'].Fill("N clones", n_clones_y12)
1636 h['Reco_y12_3hits'].Fill("N ghosts", n_ghosts_y12)
1637 h['Reco_y12_3hits'].Fill("N others", n_others_y12)
1638
1639 h['Reco_stereo12_3hits'].Fill("N total", n_tracks_stereo12)
1640 h['Reco_stereo12_3hits'].Fill("N recognized tracks", n_recognized_stereo12)
1641 h['Reco_stereo12_3hits'].Fill("N clones", n_clones_stereo12)
1642 h['Reco_stereo12_3hits'].Fill("N ghosts", n_ghosts_stereo12)
1643 h['Reco_stereo12_3hits'].Fill("N others", n_others_stereo12)
1644
1645 h['Reco_34_3hits'].Fill("N total", n_tracks_34)
1646 h['Reco_34_3hits'].Fill("N recognized tracks", n_recognized_34)
1647 h['Reco_34_3hits'].Fill("N clones", n_clones_34)
1648 h['Reco_34_3hits'].Fill("N ghosts", n_ghosts_34)
1649 h['Reco_34_3hits'].Fill("N others", n_others_34)
1650
1651
1652 (n_tracks_y12, n_recognized_y12, n_clones_y12, n_ghosts_y12, n_others_y12,
1653 n_tracks_stereo12, n_recognized_stereo12, n_clones_stereo12, n_ghosts_stereo12, n_others_stereo12,
1654 n_tracks_34, n_recognized_34, n_clones_34, n_ghosts_34, n_others_34) = self.recognition_metrics(track_hits, mode='Tr4')
1655
1656 h['NTrueTracks_Tr4'].Fill("Stations 1&2, Y views", n_tracks_y12)
1657 h['NTrueTracks_Tr4'].Fill("Stations 1&2, Stereo views", n_tracks_stereo12)
1658 h['NTrueTracks_Tr4'].Fill("Stations 3&4", n_tracks_34)
1659
1660 h['Reco_y12_Tr4'].Fill("N total", n_tracks_y12)
1661 h['Reco_y12_Tr4'].Fill("N recognized tracks", n_recognized_y12)
1662 h['Reco_y12_Tr4'].Fill("N clones", n_clones_y12)
1663 h['Reco_y12_Tr4'].Fill("N ghosts", n_ghosts_y12)
1664 h['Reco_y12_Tr4'].Fill("N others", n_others_y12)
1665
1666 h['Reco_stereo12_Tr4'].Fill("N total", n_tracks_stereo12)
1667 h['Reco_stereo12_Tr4'].Fill("N recognized tracks", n_recognized_stereo12)
1668 h['Reco_stereo12_Tr4'].Fill("N clones", n_clones_stereo12)
1669 h['Reco_stereo12_Tr4'].Fill("N ghosts", n_ghosts_stereo12)
1670 h['Reco_stereo12_Tr4'].Fill("N others", n_others_stereo12)
1671
1672 h['Reco_34_Tr4'].Fill("N total", n_tracks_34)
1673 h['Reco_34_Tr4'].Fill("N recognized tracks", n_recognized_34)
1674 h['Reco_34_Tr4'].Fill("N clones", n_clones_34)
1675 h['Reco_34_Tr4'].Fill("N ghosts", n_ghosts_34)
1676 h['Reco_34_Tr4'].Fill("N others", n_others_34)
1677
1678
1679
1680 n_true_track_hits_y12 = {}
1681 n_true_track_hits_stereo12 = {}
1682 n_true_track_hits_34 = {}
1683 for ahit in self.sTree.MufluxSpectrometerPoint:
1684
1685 track_id = ahit.GetTrackID()
1686
1687 detID = ahit.GetDetectorID()
1688 statnb = detID // 10000000
1689 vnb = (detID - statnb * 10000000) // 1000000
1690
1691 is_y12 = (statnb == 1) * (vnb == 0) + (statnb == 2) * (vnb == 1)
1692 is_stereo12 = (statnb == 1) * (vnb == 1) + (statnb == 2) * (vnb == 0)
1693 is_34 = (statnb == 3) + (statnb == 4)
1694
1695 if is_y12:
1696 if track_id in n_true_track_hits_y12:
1697 n_true_track_hits_y12[track_id] += 1
1698 else:
1699 n_true_track_hits_y12[track_id] = 1
1700 if is_stereo12:
1701 if track_id in n_true_track_hits_stereo12:
1702 n_true_track_hits_stereo12[track_id] += 1
1703 else:
1704 n_true_track_hits_stereo12[track_id] = 1
1705 if is_34:
1706 if track_id in n_true_track_hits_34:
1707 n_true_track_hits_34[track_id] += 1
1708 else:
1709 n_true_track_hits_34[track_id] = 1
1710
1711 for key in n_true_track_hits_y12.keys():
1712 n = n_true_track_hits_y12[key]
1713 h['NHits_true_y12'].Fill(n)
1714 for key in n_true_track_hits_stereo12.keys():
1715 n = n_true_track_hits_stereo12[key]
1716 h['NHits_true_stereo12'].Fill(n)
1717 for key in n_true_track_hits_34.keys():
1718 n = n_true_track_hits_34[key]
1719 h['NHits_true_34'].Fill(n)
1720
1721 for i_track in track_hits.keys():
1722 atrack = track_hits[i_track]
1723 atrack_y12 = atrack['y12']
1724 atrack_stereo12 = atrack['stereo12']
1725 atrack_34 = atrack['34']
1726
1727 if len(atrack_y12) > 0:
1728 h['NHits_reco_y12'].Fill(len(atrack_y12))
1729 if len(atrack_stereo12) > 0:
1730 h['NHits_reco_stereo12'].Fill(len(atrack_stereo12))
1731 if len(atrack_34) > 0:
1732 h['NHits_reco_34'].Fill(len(atrack_34))
1733
1734
1735
1736
1737 for entry in trackCandidates:
1738
1739
1740 atrack = entry[1]
1741 theTrack = entry[0]
1742 if not theTrack.checkConsistency():
1743 print('Problem with track before fit, not consistent',atrack,theTrack)
1744 continue
1745
1746 try: self.fitter.processTrack(theTrack)
1747 except:
1748 print("genfit failed to fit track")
1749 continue
1750
1751 if not theTrack.checkConsistency():
1752
1753 continue
1754 fitStatus = theTrack.getFitStatus()
1755 nmeas = fitStatus.getNdf()
1756 chi2 = fitStatus.getChi2()/nmeas
1757 pvalue = fitStatus.getPVal()
1758
1759
1760
1761 h['nmeas'].Fill(nmeas)
1762 h['chi2'].Fill(chi2)
1763 h['p-value'].Fill(pvalue)
1764 try:
1765 fittedState = theTrack.getFittedState()
1766 fittedMom = fittedState.getMomMag()
1767 h['p-fittedtracks'].Fill(fittedMom)
1768 h['1/p-fittedtracks'].Fill(1./fittedMom)
1769 Px,Py,Pz = fittedState.getMom().x(),fittedState.getMom().y(),fittedState.getMom().z()
1770 P = fittedMom
1771 Pt = ROOT.TMath.Sqrt(Px*Px+Py*Py)
1772 h['p/pt'].Fill(P,Pt)
1773 h['pt-fittedtracks'].Fill(Pt)
1774 h['1/pt-fittedtracks'].Fill(1./Pt)
1775
1776 if self.sTree.GetBranch("MufluxSpectrometerPoint"):
1777
1778 atrack_ids = []
1779 for digi_hit in trackDigiHits[atrack]:
1780 ahit = self.sTree.MufluxSpectrometerPoint[digi_hit]
1781 atrack_ids.append(ahit.GetTrackID())
1782 frac, tmax = self.fracMCsame(atrack_ids)
1783 Ptruth,Ptruthx,Ptruthy,Ptruthz = self.getPtruthFirst(tmax)
1784 Pgun,Pgunx,Pguny,Pgunz = self.getPtruthAtOrigin(tmax)
1785 Pttruth = ROOT.TMath.Sqrt(Ptruthx*Ptruthx+Ptruthy*Ptruthy)
1786 h['p/pt_truth'].Fill(Ptruth,Pttruth)
1787 perr = (P - Ptruth) / Ptruth
1788 pterr = (Pt - Pttruth) / Pttruth
1789 h['p_rel_error'].Fill(perr)
1790 h['pt_rel_error'].Fill(pterr)
1791
1792 mom_init = trackMomentums[atrack]
1793 print("P_precalc, P_truth: ", mom_init, Ptruth)
1794
1795 if Pz !=0:
1796 pxpzfitted = Px/Pz
1797 pypzfitted = Py/Pz
1798 if Ptruthz !=0:
1799 pxpztrue = Ptruthx/Ptruthz
1800 pypztrue = Ptruthy/Ptruthz
1801 h['Px/Pzfitted'].Fill(pxpzfitted)
1802 h['Py/Pzfitted'].Fill(pypzfitted)
1803 h['Px/Pztrue'].Fill(pxpztrue)
1804 h['Py/Pztrue'].Fill(pypztrue)
1805 h['Px/Pzfitted-Px/Pztruth'].Fill(Ptruth,pxpzfitted-pxpztrue)
1806 h['Py/Pzfitted-Py/Pztruth'].Fill(Ptruth,pypzfitted-pypztrue)
1807 h['ptruth'].Fill(Ptruth)
1808 delPOverP = (P/Ptruth)-1
1809 invdelPOverP = (Ptruth/P)-1
1810 if 1==0:
1811 if invdelPOverP < -0.8:
1812 print("invdelPOverP = ",invdelPOverP)
1813 print("Ptruth =",Ptruth," Pfitted =",P)
1814 for n in range(hitPosLists[atrack].size()):
1815 print("hit=",n," x(top) ",hitPosLists[atrack][n][0]," y(top) ",hitPosLists[atrack][n][1]," z ",hitPosLists[atrack][n][2]," x(bot) ",hitPosLists[atrack][n][3]," y(bot) ", hitPosLists[atrack][n][4], " dist ", hitPosLists[atrack][n][6])
1816 nMufluxHits = self.sTree.MufluxSpectrometerPoint.GetEntriesFast()
1817 for i in range(nMufluxHits):
1818 MufluxHit = self.sTree.MufluxSpectrometerPoint[i]
1819 if ((hitPosLists[atrack][n][0]+1.8 > MufluxHit.GetX()) or(hitPosLists[atrack][n][3]+1.8 > MufluxHit.GetX())) and ((hitPosLists[atrack][n][0]-1.8<MufluxHit.GetX()) or (hitPosLists[atrack][n][3]-1.8<MufluxHit.GetX())) and (hitPosLists[atrack][n][2]+1.>MufluxHit.GetZ()) and (hitPosLists[atrack][n][2]-1.<MufluxHit.GetZ()):
1820 print("hit x=",MufluxHit.GetX()," y=",MufluxHit.GetY()," z=",MufluxHit.GetZ())
1821
1822
1823 h['delPOverP'].Fill(Ptruth,delPOverP)
1824 h['invdelPOverP'].Fill(Ptruth,invdelPOverP)
1825 h['deltaPOverP'].Fill(Ptruth,delPOverP)
1826 h['Pfitted-Pgun'].Fill(Pgun,P)
1827
1828
1829 except:
1830 print("problem with fittedstate")
1831 continue
1832
1833
1834 for entry in trackCandidates_noT4:
1835
1836
1837 atrack = entry[1]
1838 theTrack = entry[0]
1839 if not theTrack.checkConsistency():
1840 print('Problem with track before fit, not consistent',atrack,theTrack)
1841 continue
1842
1843 try: self.fitter.processTrack(theTrack)
1844 except:
1845 print("genfit failed to fit track")
1846 continue
1847
1848 if not theTrack.checkConsistency():
1849 print('Problem with track after fit, not consistent',atrack,theTrack)
1850 continue
1851 fitStatus = theTrack.getFitStatus()
1852 nmeas = fitStatus.getNdf()
1853 chi2 = fitStatus.getChi2()/nmeas
1854 pvalue = fitStatus.getPVal()
1855
1856
1857
1858 h['nmeas-noT4'].Fill(nmeas)
1859 h['chi2-noT4'].Fill(chi2)
1860 h['p-value-noT4'].Fill(pvalue)
1861 try:
1862
1863 fittedState = theTrack.getFittedState()
1864 fittedMom = fittedState.getMomMag()
1865 h['p-fittedtracks-noT4'].Fill(fittedMom)
1866 h['1/p-fittedtracks-noT4'].Fill(1./fittedMom)
1867 Px,Py,Pz = fittedState.getMom().x(),fittedState.getMom().y(),fittedState.getMom().z()
1868 P = fittedMom
1869 Pt = ROOT.TMath.Sqrt(Px*Px+Py*Py)
1870 h['p/pt_noT4'].Fill(P,Pt)
1871 h['pt-fittedtracks-noT4'].Fill(Pt)
1872 h['1/pt-fittedtracks-noT4'].Fill(1./Pt)
1873
1874 if self.sTree.GetBranch("MufluxSpectrometerPoint"):
1875
1876 atrack_ids = []
1877 for digi_hit in trackDigiHits_noT4[atrack]:
1878 ahit = self.sTree.MufluxSpectrometerPoint[digi_hit]
1879 atrack_ids.append(ahit.GetTrackID())
1880 frac, tmax = self.fracMCsame(atrack_ids)
1881 Ptruth,Ptruthx,Ptruthy,Ptruthz = self.getPtruthFirst(tmax)
1882 Pgun,Pgunx,Pguny,Pgunz = self.getPtruthAtOrigin(tmax)
1883 Pttruth = ROOT.TMath.Sqrt(Ptruthx*Ptruthx+Ptruthy*Ptruthy)
1884 h['p/pt_truth_noT4'].Fill(Ptruth,Pttruth)
1885 perr = (P - Ptruth) / Ptruth
1886 pterr = (Pt - Pttruth) / Pttruth
1887 h['p_rel_error_noT4'].Fill(perr)
1888 h['pt_rel_error_noT4'].Fill(pterr)
1889
1890 if Pz !=0:
1891 pxpzfitted = Px/Pz
1892 pypzfitted = Py/Pz
1893 if Ptruthz !=0:
1894 pxpztrue = Ptruthx/Ptruthz
1895 pypztrue = Ptruthy/Ptruthz
1896 h['Px/Pzfitted-Px/Pztruth-noT4'].Fill(Ptruth,pxpzfitted-pxpztrue)
1897 h['Py/Pzfitted-Py/Pztruth-noT4'].Fill(Ptruth,pypzfitted-pypztrue)
1898 h['Px/Pzfitted-noT4'].Fill(pxpzfitted)
1899 h['Py/Pzfitted-noT4'].Fill(pypzfitted)
1900 h['Px/Pztrue-noT4'].Fill(pxpztrue)
1901 h['Py/Pztrue-noT4'].Fill(pypztrue)
1902
1903 h['ptruth-noT4'].Fill(Ptruth)
1904 delPOverP = (P/Ptruth)-1
1905 invdelPOverP = (Ptruth/P)-1
1906 h['delPOverP-noT4'].Fill(Ptruth,delPOverP)
1907 h['invdelPOverP-noT4'].Fill(Ptruth,invdelPOverP)
1908 h['deltaPOverP-noT4'].Fill(Ptruth,delPOverP)
1909 h['Pfitted-Pgun-noT4'].Fill(Pgun,P)
1910
1911 except:
1912 print("noT4 track: problem with fittedstate")
1913 continue
1914
1915
1916 for entry in trackCandidates_all:
1917
1918
1919 atrack = entry[1]
1920 theTrack = entry[0]
1921 if not theTrack.checkConsistency():
1922 print('Problem with track before fit, not consistent',atrack,theTrack)
1923 continue
1924
1925 try: self.fitter.processTrack(theTrack)
1926 except:
1927 print("genfit failed to fit track")
1928 continue
1929
1930 if not theTrack.checkConsistency():
1931 print('Problem with track after fit, not consistent',atrack,theTrack)
1932 continue
1933 fitStatus = theTrack.getFitStatus()
1934 nmeas = fitStatus.getNdf()
1935 chi2 = fitStatus.getChi2()/nmeas
1936 pvalue = fitStatus.getPVal()
1937
1938
1939
1940 h['nmeas-all'].Fill(nmeas)
1941 h['chi2-all'].Fill(chi2)
1942 h['p-value-all'].Fill(pvalue)
1943 try:
1944
1945 fittedState = theTrack.getFittedState()
1946 fittedMom = fittedState.getMomMag()
1947 h['p-fittedtracks-all'].Fill(fittedMom)
1948 h['1/p-fittedtracks-all'].Fill(1./fittedMom)
1949 Px,Py,Pz = fittedState.getMom().x(),fittedState.getMom().y(),fittedState.getMom().z()
1950 P = fittedMom
1951 Pt = ROOT.TMath.Sqrt(Px*Px+Py*Py)
1952 h['p/pt_all'].Fill(P,Pt)
1953 h['pt-fittedtracks-all'].Fill(Pt)
1954 h['1/pt-fittedtracks-all'].Fill(1./Pt)
1955
1956 if self.sTree.GetBranch("MufluxSpectrometerPoint"):
1957
1958 atrack_ids = []
1959 for digi_hit in trackDigiHits_noT4[atrack]:
1960 ahit = self.sTree.MufluxSpectrometerPoint[digi_hit]
1961 atrack_ids.append(ahit.GetTrackID())
1962 frac, tmax = self.fracMCsame(atrack_ids)
1963 Ptruth,Ptruthx,Ptruthy,Ptruthz = self.getPtruthFirst(tmax)
1964 Pgun,Pgunx,Pguny,Pgunz = self.getPtruthAtOrigin(tmax)
1965 Pttruth = ROOT.TMath.Sqrt(Ptruthx*Ptruthx+Ptruthy*Ptruthy)
1966 h['p/pt_truth_all'].Fill(Ptruth,Pttruth)
1967 perr = (P - Ptruth) / Ptruth
1968 pterr = (Pt - Pttruth) / Pttruth
1969 h['p_rel_error_all'].Fill(perr)
1970 h['pt_rel_error_all'].Fill(pterr)
1971
1972 if Pz !=0:
1973 pxpzfitted = Px/Pz
1974 pypzfitted = Py/Pz
1975 if Ptruthz !=0:
1976 pxpztrue = Ptruthx/Ptruthz
1977 pypztrue = Ptruthy/Ptruthz
1978 h['Px/Pzfitted-Px/Pztruth-all'].Fill(Ptruth,pxpzfitted-pxpztrue)
1979 h['Py/Pzfitted-Py/Pztruth-all'].Fill(Ptruth,pypzfitted-pypztrue)
1980 h['Px/Pzfitted-all'].Fill(pxpzfitted)
1981 h['Py/Pzfitted-all'].Fill(pypzfitted)
1982 h['Px/Pztrue-all'].Fill(pxpztrue)
1983 h['Py/Pztrue-all'].Fill(pypztrue)
1984
1985 h['ptruth-all'].Fill(Ptruth)
1986 delPOverP = (P/Ptruth)-1
1987 invdelPOverP = (Ptruth/P)-1
1988 h['delPOverP-all'].Fill(Ptruth,delPOverP)
1989 h['invdelPOverP-all'].Fill(Ptruth,invdelPOverP)
1990 h['deltaPOverP-all'].Fill(Ptruth,delPOverP)
1991 h['Pfitted-Pgun-all'].Fill(Pgun,P)
1992
1993 except:
1994 print("All tracks: problem with fittedstate")
1995 continue
1996
1997
1998 nTrack = self.fGenFitArray.GetEntries()
1999 if not global_variables.debug:
2000 theTrack.prune("CFL")
2001 self.fGenFitArray[nTrack] = theTrack
2002 self.fitTrack2MC.push_back(atrack)
2003 if global_variables.debug:
2004 print('save track',theTrack,chi2,nM,fitStatus.isFitConverged())
2005
2006 self.fitTracks.Fill()
2007 self.mcLink.Fill()
2008 return nTrack+1
2009
execute(SmearedHits, TaggerHits, withNTaggerHits, withDist2Wire, debug=0)