1 package org.lcsim.recon.util;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.LinkedHashMap;
9 import java.util.LinkedList;
10 import java.util.List;
11 import java.util.Map;
12
13 import org.lcsim.event.CalorimeterHit;
14 import org.lcsim.event.Cluster;
15 import org.lcsim.event.EventHeader;
16 import org.lcsim.event.LCRelation;
17 import org.lcsim.event.MCParticle;
18 import org.lcsim.event.ReconstructedParticle;
19 import org.lcsim.event.RelationalTable;
20 import org.lcsim.event.SimCalorimeterHit;
21 import org.lcsim.event.Track;
22 import org.lcsim.event.TrackerHit;
23 import org.lcsim.event.MCParticle.SimulatorStatus;
24 import org.lcsim.event.base.BaseLCRelation;
25 import org.lcsim.event.base.BaseRelationalTable;
26 import org.lcsim.util.Driver;
27 import org.lcsim.lcio.LCIOConstants;
28 import org.lcsim.lcio.LCIOUtil;
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 public class McTruthLinker extends Driver {
80
81 protected String trackHitMcRelationName = "HelicalTrackMCRelations";
82 protected String trackCollectionName = "Tracks";
83 protected String trackMcRelationName = "TrackMCTruthLink";
84 protected String caloHitSimHitRelationName = "CalorimeterHitRelations";
85 protected String clusterCollectionName = "ReconClusters";
86 protected String clusterMcRelationName = "ClusterMCTruthLink";
87 protected String pfoCollectionName = "PandoraPFOCollection";
88 protected String pfoMcRelationName = "RecoMCTruthLink";
89 protected String mcParticleCollectionName = EventHeader.MC_PARTICLES;
90 protected String mcParticlesSkimmedName = "MCParticlesSkimmed";
91 protected double pfoTrackWeight = 1.0;
92 protected double pfoClusterWeight = 0.0;
93 protected boolean fullRecoRelation = false;
94 protected boolean useTanimotoDistance = false;
95 protected boolean useSkimmedMcParticles = true;
96 protected List<MCParticle> mcParticlesSkimmed;
97 protected List<Integer> keepDaughtersPDGID = new ArrayList<Integer>();
98 protected Map<MCParticle, MCParticle> mcParticleToSkimmed;
99 protected double daughterEnergyCut = 0.01;
100
101
102
103 public McTruthLinker() {
104 keepDaughtersPDGID.add(22);
105 keepDaughtersPDGID.add(111);
106 keepDaughtersPDGID.add(310);
107 }
108
109
110
111 @Override
112 protected void startOfData() {
113 if (mcParticlesSkimmedName.equals("")) {
114 this.useSkimmedMcParticles = false;
115 }
116 }
117
118 @Override
119 protected void process(EventHeader event) {
120
121 List<LCRelation> trackMcRelation = null;
122 List<LCRelation> caloHitMcRelation = null;
123 List<LCRelation> clusterMcRelation = null;
124 List<LCRelation> pfoMcRelation = null;
125 mcParticlesSkimmed = null;
126 mcParticleToSkimmed = null;
127
128
129 if (useSkimmedMcParticles) {
130 try {
131 List<MCParticle> mcParticles = event.get(MCParticle.class, mcParticleCollectionName);
132 mcParticlesSkimmed = createSkimmedMcParticleList(mcParticles);
133 mcParticleToSkimmed = fillMcParticleToSkimmedMap(mcParticles, mcParticlesSkimmed);
134 int flags = event.getMetaData(mcParticles).getFlags();
135 flags = LCIOUtil.bitSet(flags, LCIOConstants.BITSubset, true);
136 event.put(mcParticlesSkimmedName, mcParticlesSkimmed, MCParticle.class, flags);
137 print(HLEVEL_NORMAL, "Added skimmed mc particles \"" + mcParticlesSkimmedName + "\" to the event.");
138 } catch (IllegalArgumentException e) {
139 print(HLEVEL_DEFAULT, "WARNING: no skimmed mc particle collection created.\n" + "e.getMessage()", true);
140 }
141 }
142
143
144 if (!trackHitMcRelationName.equals("") && !trackCollectionName.equals("")) {
145 try {
146 List<Track> tracks = event.get(Track.class, trackCollectionName);
147 List<LCRelation> trackHitMcRelation = event.get(LCRelation.class, trackHitMcRelationName);
148 trackMcRelation = createTrackMcRelation(tracks, trackHitMcRelation);
149 if (!trackMcRelationName.equals("")) {
150 int flags = 0;
151 flags = LCIOUtil.bitSet(flags, LCIOConstants.LCREL_WEIGHTED, true);
152 event.put(trackMcRelationName, trackMcRelation, LCRelation.class, flags);
153 print(HLEVEL_NORMAL, "Added track to mc particle relations \"" + trackMcRelationName
154 + "\" to the event.");
155 }
156 } catch (IllegalArgumentException e) {
157 print(HLEVEL_DEFAULT, "WARNING: no track to mc particle relation created.\n" + "e.getMessage()", true);
158 }
159 }
160
161
162 if (!caloHitSimHitRelationName.equals("")) {
163 try {
164 caloHitMcRelation = createCaloHitMcRelation(event.get(LCRelation.class, caloHitSimHitRelationName));
165 } catch (IllegalArgumentException e) {
166 print(HLEVEL_DEFAULT, "WARNING: no calorimeter hit to mc particle relation created.\n"
167 + "e.getMessage()", true);
168 }
169 }
170
171
172 if (!clusterCollectionName.equals("")) {
173 try {
174 List<Cluster> clusters = event.get(Cluster.class, clusterCollectionName);
175 clusterMcRelation = createClusterMcRelation(clusters, caloHitMcRelation);
176 if (!clusterMcRelationName.equals("")) {
177 int flags = 0;
178 flags = LCIOUtil.bitSet(flags, LCIOConstants.LCREL_WEIGHTED, true);
179 event.put(clusterMcRelationName, clusterMcRelation, LCRelation.class, flags);
180 print(HLEVEL_NORMAL, "Added cluster to mc particle relations \"" + clusterMcRelationName
181 + "\" to the event.");
182 }
183 } catch (IllegalArgumentException e) {
184 print(HLEVEL_DEFAULT, "WARNING: no cluster to mc particle relation created.\n" + "e.getMessage()", true);
185 }
186 }
187
188
189 if (!pfoCollectionName.equals("")) {
190 try {
191 List<ReconstructedParticle> PFOs = event.get(ReconstructedParticle.class, pfoCollectionName);
192 pfoMcRelation = createPfoMcRelation(PFOs, trackMcRelation, clusterMcRelation);
193 if (!pfoMcRelationName.equals("")) {
194 int flags = 0;
195 flags = LCIOUtil.bitSet(flags, LCIOConstants.LCREL_WEIGHTED, true);
196 event.put(pfoMcRelationName, pfoMcRelation, LCRelation.class, flags);
197 print(HLEVEL_NORMAL, "Added PFO to mc particle relations \"" + pfoMcRelationName
198 + "\" to the event.");
199 }
200 } catch (IllegalArgumentException e) {
201 print(HLEVEL_DEFAULT, "WARNING: no PFO to mc particle relation created.\n" + "e.getMessage()", true);
202 }
203 }
204 }
205
206
207
208 public void setFullRecoRelation(boolean fullRecoRelation) {
209 this.fullRecoRelation = fullRecoRelation;
210 }
211
212 public void setUseTanimotoDistance(boolean useTanimotoDistance) {
213 this.useTanimotoDistance = useTanimotoDistance;
214 }
215
216 public void setPfoTrackWeight(double pfoTrackWeight) throws IllegalArgumentException {
217 if (pfoTrackWeight < 0)
218 throw new IllegalArgumentException("PFO track weight can not be negative.");
219 this.pfoTrackWeight = pfoTrackWeight;
220 }
221
222 public void setPfoClusterWeight(double pfoClusterWeight) throws IllegalArgumentException {
223 if (pfoTrackWeight < 0)
224 throw new IllegalArgumentException("PFO cluster weight can not be negative.");
225 this.pfoClusterWeight = pfoClusterWeight;
226 }
227
228 public void setTrackHitMcRelationName(String trackHitMcRelationName) {
229 this.trackHitMcRelationName = trackHitMcRelationName;
230 }
231
232 public void setTrackCollectionName(String trackCollectionName) {
233 this.trackCollectionName = trackCollectionName;
234 }
235
236 public void setTrackMcRelationName(String trackMcRelationName) {
237 this.trackMcRelationName = trackMcRelationName;
238 }
239
240 public void setCaloHitSimHitRelationName(String caloHitSimHitRelationName) {
241 this.caloHitSimHitRelationName = caloHitSimHitRelationName;
242 }
243
244 public void setClusterCollectionName(String clusterCollectionName) {
245 this.clusterCollectionName = clusterCollectionName;
246 }
247
248 public void setClusterMcRelationName(String clusterMcRelationName) {
249 this.clusterMcRelationName = clusterMcRelationName;
250 }
251
252 public void setPfoCollectionName(String pfoCollectionName) {
253 this.pfoCollectionName = pfoCollectionName;
254 }
255
256 public void setPfoMcRelationName(String pfoMcRelationName) {
257 this.pfoMcRelationName = pfoMcRelationName;
258 }
259
260 public void setMcParticleCollectionName(String mcParticleCollectionName) {
261 this.mcParticleCollectionName = mcParticleCollectionName;
262 }
263
264 public void setMcParticlesSkimmedName(String mcParticlesSkimmedName) {
265 this.mcParticlesSkimmedName = mcParticlesSkimmedName;
266 }
267
268 public void setKeepDaughtersPDGID(int[] keepDaughtersPDGID) {
269 this.keepDaughtersPDGID.clear();
270 for (int pdgid : keepDaughtersPDGID) {
271 this.keepDaughtersPDGID.add(pdgid);
272 }
273 }
274
275 public void setDaughterEnergyCut(double daughterEnergyCut) {
276 this.daughterEnergyCut = daughterEnergyCut;
277 }
278
279
280
281
282
283
284
285
286
287
288
289 protected List<MCParticle> createSkimmedMcParticleList(List<MCParticle> mcParticles) {
290
291 List<MCParticle> skimmedMcParticles = new ArrayList<MCParticle>();
292
293 for (MCParticle mcParticle : mcParticles) {
294 SimulatorStatus simStatus = mcParticle.getSimulatorStatus();
295 if (mcParticle.getGeneratorStatus() == MCParticle.INTERMEDIATE) {
296
297 addMcParticleWithParents(mcParticle, skimmedMcParticles);
298 }
299 if (mcParticle.getGeneratorStatus() == MCParticle.DOCUMENTATION) {
300
301 addMcParticleWithParents(mcParticle, skimmedMcParticles);
302 }
303 if (mcParticle.getGeneratorStatus() > 3) {
304
305
306
307 addMcParticleWithParents(mcParticle, skimmedMcParticles);
308 }
309 if (mcParticle.getGeneratorStatus() == MCParticle.FINAL_STATE) {
310
311 addMcParticleWithParents(mcParticle, skimmedMcParticles);
312
313 if (simStatus.isDecayedInCalorimeter()) {
314
315 for (MCParticle daughter : mcParticle.getDaughters()) {
316 if (daughter.getPDGID() == 22 && daughter.getEnergy() > daughterEnergyCut
317 && !daughter.getSimulatorStatus().isBackscatter()) {
318 addMcParticleWithParents(daughter, skimmedMcParticles);
319 }
320 }
321 }
322
323 } else if (mcParticle.getSimulatorStatus().isDecayedInTracker()) {
324
325
326 if (keepDaughtersPDGID.contains(mcParticle.getPDGID())) {
327 for (MCParticle daughter : mcParticle.getDaughters()) {
328 if (daughter.getEnergy() > daughterEnergyCut && !daughter.getSimulatorStatus().isBackscatter()) {
329 addMcParticleWithParents(daughter, skimmedMcParticles);
330 }
331 }
332 }
333
334 }
335 }
336
337 print(HLEVEL_NORMAL, "Keeping " + skimmedMcParticles.size() + " of " + mcParticles.size()
338 + " mc particles in skimmed list.");
339
340 return skimmedMcParticles;
341 }
342
343
344
345
346
347
348
349
350
351
352
353
354
355 protected Map<MCParticle, MCParticle> fillMcParticleToSkimmedMap(List<MCParticle> mcParticles,
356 List<MCParticle> skimmedMcParticles) {
357
358 Map<MCParticle, MCParticle> mcParticleToSkimmedMap = new HashMap<MCParticle, MCParticle>();
359
360 for (MCParticle mcParticle : mcParticles) {
361 MCParticle ancestor = findMcParticleAncestor(mcParticle, skimmedMcParticles);
362 mcParticleToSkimmedMap.put(mcParticle, ancestor);
363 String motherPDGID = "none";
364 if (mcParticle.getParents().size() > 0) {
365 motherPDGID = Integer.toString(mcParticle.getParents().get(0).getPDGID());
366 }
367 if (mcParticle != ancestor) {
368 print(HLEVEL_FULL, "Warning: Rejecting mc particle." + "\tEnergy: " + mcParticle.getEnergy() + "\n"
369 + "\tCharge: " + mcParticle.getCharge() + "\n" + "\tPDGID: " + mcParticle.getPDGID() + "\n"
370 + "\tGenStatus: " + mcParticle.getGeneratorStatus() + "\n" + "\tCreated in simulation: "
371 + mcParticle.getSimulatorStatus().isCreatedInSimulation() + "\n" + "\tBackscatter: "
372 + mcParticle.getSimulatorStatus().isBackscatter() + "\n" + "\tDecay in calorimeter: "
373 + mcParticle.getSimulatorStatus().isDecayedInCalorimeter() + "\n" + "\tDecay in tracker: "
374 + mcParticle.getSimulatorStatus().isDecayedInTracker() + "\n" + "\tStopped: "
375 + mcParticle.getSimulatorStatus().isStopped() + "\n" + "\tMother: " + motherPDGID, true);
376 }
377 }
378 return mcParticleToSkimmedMap;
379 }
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401 protected List<LCRelation> createTrackMcRelation(List<Track> tracks, List<LCRelation> trackHitMcRelation) {
402
403 if (trackHitMcRelation == null) {
404 throw new IllegalArgumentException("No tracker hit to mc relations given.");
405 }
406
407 RelationalTable<TrackerHit, MCParticle> trackHitMcRelationTable = createRelationalTable(trackHitMcRelation);
408 List<LCRelation> trackMcRelation = new ArrayList<LCRelation>();
409
410 for (Track track : tracks) {
411
412 Map<MCParticle, Integer> mcParticleContribution = new HashMap<MCParticle, Integer>();
413 List<TrackerHit> trackHitsList = track.getTrackerHits();
414 double trackHits = trackHitsList.size();
415 double sumOfWeights = 0;
416 for (TrackerHit trackHit : trackHitsList) {
417 for (MCParticle mcParticle : trackHitMcRelationTable.allFrom(trackHit)) {
418 if (useSkimmedMcParticles)
419 mcParticle = mcParticleToSkimmed.get(mcParticle);
420 if (mcParticleContribution.containsKey(mcParticle)) {
421 mcParticleContribution.put(mcParticle, mcParticleContribution.get(mcParticle) + 1);
422 } else {
423 mcParticleContribution.put(mcParticle, 1);
424 }
425 }
426 }
427 mcParticleContribution = sortMapByHighestValue(mcParticleContribution);
428 for (MCParticle mcParticle : mcParticleContribution.keySet()) {
429 double weight = 0.0;
430 double recoHits = mcParticleContribution.get(mcParticle);
431 if (useTanimotoDistance) {
432 double trueHits = trackHitMcRelationTable.allTo(mcParticle).size();
433 weight = 1 - (trackHits + trueHits - 2 * recoHits) / (trackHits + trueHits - recoHits);
434 } else {
435 weight = recoHits / trackHits;
436 }
437 sumOfWeights += weight;
438 trackMcRelation.add(new BaseLCRelation(track, mcParticle, weight));
439 print(HLEVEL_FULL, "Added a track to mc particle relation with weight " + weight + ".");
440 if (!fullRecoRelation)
441 break;
442 }
443 print(HLEVEL_HIGH, "Total weight of track contributions is " + sumOfWeights + ".");
444 }
445
446 print(HLEVEL_NORMAL, "Created " + trackMcRelation.size() + " track to mc particle relations.");
447
448 return trackMcRelation;
449 }
450
451
452
453
454
455
456
457
458
459
460
461
462
463 protected List<LCRelation> createCaloHitMcRelation(List<LCRelation> caloHitSimHitRelation) {
464
465 List<LCRelation> caloHitMcRelation = new ArrayList<LCRelation>();
466
467 for (LCRelation relation : caloHitSimHitRelation) {
468 CalorimeterHit digiHit = (CalorimeterHit) relation.getFrom();
469 SimCalorimeterHit simHit = (SimCalorimeterHit) relation.getTo();
470 double hitEnergy = simHit.getRawEnergy();
471 double sumOfWeights = 0;
472 for (int i = 0; i < simHit.getMCParticleCount(); i++) {
473 double weight = simHit.getContributedEnergy(i) / hitEnergy;
474 sumOfWeights += weight;
475 caloHitMcRelation.add(new BaseLCRelation(digiHit, simHit.getMCParticle(i), weight));
476 print(HLEVEL_FULL, "Added a calorimeter hit to mc particle relation with weight " + weight + ".");
477 }
478 print(HLEVEL_FULL, "Total weight of calorimeter hit contributions is " + sumOfWeights + ".");
479 }
480
481 print(HLEVEL_NORMAL, "Created " + caloHitMcRelation.size() + " calorimeter hit to mc particle relations.");
482
483 return caloHitMcRelation;
484 }
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500 protected List<LCRelation> createClusterMcRelation(List<Cluster> clusters, List<LCRelation> caloHitMcRelation)
501 throws IllegalArgumentException {
502
503 if (caloHitMcRelation == null) {
504 throw new IllegalArgumentException("No calorimeter hit to mc relations given.");
505 }
506
507 RelationalTable<CalorimeterHit, MCParticle> caloHitMcRelationTable = createRelationalTable(caloHitMcRelation);
508 List<LCRelation> clusterMcRelation = new ArrayList<LCRelation>();
509
510 for (Cluster cluster : clusters) {
511 double sumOfWeights = 0;
512 double clusterEnergy = cluster.getEnergy();
513 Map<MCParticle, Double> mcParticlesWeight = new HashMap<MCParticle, Double>();
514 for (CalorimeterHit hit : cluster.getCalorimeterHits()) {
515 double hitEnergy = hit.getCorrectedEnergy();
516 double hitWeight = hitEnergy / clusterEnergy;
517 Map<MCParticle, Double> hitMcParticlesWeight = caloHitMcRelationTable.allFromWithWeights(hit);
518 for (MCParticle mcParticle : hitMcParticlesWeight.keySet()) {
519
520 double weight = hitWeight * hitMcParticlesWeight.get(mcParticle);
521 if (useSkimmedMcParticles)
522 mcParticle = mcParticleToSkimmed.get(mcParticle);
523 if (mcParticlesWeight.containsKey(mcParticle)) {
524 mcParticlesWeight.put(mcParticle, mcParticlesWeight.get(mcParticle) + weight);
525 } else {
526 mcParticlesWeight.put(mcParticle, weight);
527 }
528 }
529 }
530 mcParticlesWeight = sortMapByHighestValue(mcParticlesWeight);
531 for (MCParticle mcParticle : mcParticlesWeight.keySet()) {
532 double weight = mcParticlesWeight.get(mcParticle);
533 sumOfWeights += weight;
534 clusterMcRelation.add(new BaseLCRelation(cluster, mcParticle, weight));
535 print(HLEVEL_FULL, "Added a cluster to mc particle relation with weight " + weight + ".");
536 if (!fullRecoRelation)
537 break;
538 }
539 print(HLEVEL_HIGH, "Total weight of cluster contributions is " + sumOfWeights + ".");
540 }
541
542 print(HLEVEL_NORMAL, "Created " + clusterMcRelation.size() + " cluster to mc particle relations.");
543
544 return clusterMcRelation;
545 }
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569 protected List<LCRelation> createPfoMcRelation(List<ReconstructedParticle> recoParticles,
570 List<LCRelation> trackMcRelation, List<LCRelation> clusterMcRelation) throws IllegalArgumentException {
571
572 if (trackMcRelation == null) {
573 throw new IllegalArgumentException("No track to mc relations given.");
574 }
575 if (clusterMcRelation == null) {
576 throw new IllegalArgumentException("No cluster to mc relations given.");
577 }
578
579 RelationalTable<Track, MCParticle> trackMcRelationTable = createRelationalTable(trackMcRelation);
580 RelationalTable<Cluster, MCParticle> clusterMcRelationTable = createRelationalTable(clusterMcRelation);
581 List<LCRelation> pfoMcRelation = new ArrayList<LCRelation>();
582
583 for (ReconstructedParticle recoParticle : recoParticles) {
584 double sumOfWeights = 0;
585 int pfoTrackHits = 0;
586 double pfoEnergy = recoParticle.getEnergy();
587 double thisPfoClusterWeight = pfoClusterWeight;
588 double trackClusterNormalization = pfoTrackWeight + pfoClusterWeight;
589 Map<MCParticle, Double> mcParticlesWeight = new HashMap<MCParticle, Double>();
590
591 if (pfoTrackWeight != 0) {
592 for (Track track : recoParticle.getTracks()) {
593 pfoTrackHits += track.getTrackerHits().size();
594 }
595 for (Track track : recoParticle.getTracks()) {
596 Map<MCParticle, Double> trackMcParticlesWeight = trackMcRelationTable.allFromWithWeights(track);
597 double trackWeight = track.getTrackerHits().size() / (double) pfoTrackHits;
598
599 trackWeight *= pfoTrackWeight / trackClusterNormalization;
600 for (MCParticle mcParticle : trackMcParticlesWeight.keySet()) {
601 double weight = trackWeight * trackMcParticlesWeight.get(mcParticle);
602 if (useSkimmedMcParticles)
603 mcParticle = mcParticleToSkimmed.get(mcParticle);
604 if (mcParticlesWeight.containsKey(mcParticle)) {
605 mcParticlesWeight.put(mcParticle, mcParticlesWeight.get(mcParticle) + weight);
606 } else {
607 mcParticlesWeight.put(mcParticle, weight);
608 }
609 }
610 }
611 }
612
613 if (pfoTrackHits == 0) {
614 thisPfoClusterWeight = 1.0;
615 trackClusterNormalization = 1.0;
616 }
617
618 if (thisPfoClusterWeight != 0) {
619 for (Cluster cluster : recoParticle.getClusters()) {
620 Map<MCParticle, Double> clusterMcParticlesWeight = clusterMcRelationTable
621 .allFromWithWeights(cluster);
622 double clusterWeight = cluster.getEnergy() / pfoEnergy;
623
624 clusterWeight *= thisPfoClusterWeight / trackClusterNormalization;
625 for (MCParticle mcParticle : clusterMcParticlesWeight.keySet()) {
626 double weight = clusterWeight * clusterMcParticlesWeight.get(mcParticle);
627 if (useSkimmedMcParticles)
628 mcParticle = mcParticleToSkimmed.get(mcParticle);
629 if (mcParticlesWeight.containsKey(mcParticle)) {
630 mcParticlesWeight.put(mcParticle, mcParticlesWeight.get(mcParticle) + weight);
631 } else {
632 mcParticlesWeight.put(mcParticle, weight);
633 }
634 }
635 }
636 }
637 mcParticlesWeight = sortMapByHighestValue(mcParticlesWeight);
638 for (MCParticle mcParticle : mcParticlesWeight.keySet()) {
639 double weight = mcParticlesWeight.get(mcParticle);
640
641 sumOfWeights += weight;
642 pfoMcRelation.add(new BaseLCRelation(recoParticle, mcParticle, weight));
643 print(HLEVEL_FULL, "Added a PFO to mc particle relation with weight " + weight + ".\n" + "\tEnergy: "
644 + mcParticle.getEnergy() + "\n" + "\tCharge: " + mcParticle.getCharge() + "\n" + "\tPDGID: "
645 + mcParticle.getPDGID());
646 if (!fullRecoRelation)
647 break;
648 }
649 print(HLEVEL_HIGH, "Total weight of PFO contributions is " + sumOfWeights + ".");
650 }
651
652 print(HLEVEL_NORMAL, "Created " + pfoMcRelation.size() + " PFO to mc particle relations.");
653
654 return pfoMcRelation;
655 }
656
657
658
659
660
661
662
663
664
665
666
667
668
669 protected void print(int histogramLevel, String message) {
670 print(histogramLevel, message, false);
671 }
672
673
674
675
676
677
678
679
680
681
682
683
684 protected void print(int histogramLevel, String message, boolean error) {
685 if (getHistogramLevel() >= histogramLevel) {
686 message = getName() + ": " + message;
687 if (error) {
688 System.err.println(message);
689 } else
690 System.out.println(message);
691 }
692 }
693
694
695
696
697
698
699
700
701
702
703 protected void addMcParticleWithParents(MCParticle mcParticle, List<MCParticle> mcParticles) {
704 if (!mcParticles.contains(mcParticle)) {
705 mcParticles.add(mcParticle);
706 print(HLEVEL_FULL, "Adding mc particle to skimmed list.\n" + "\tEnergy: " + mcParticle.getEnergy() + "\n"
707 + "\tCharge: " + mcParticle.getCharge() + "\n" + "\tPDGID: " + mcParticle.getPDGID() + "\n"
708 + "\tGenStatus: " + mcParticle.getGeneratorStatus() + "\n" + "\tSimStatus: "
709 + mcParticle.getSimulatorStatus().getValue());
710 for (MCParticle parent : mcParticle.getParents()) {
711 addMcParticleWithParents(parent, mcParticles);
712 }
713 }
714 }
715
716
717
718
719
720
721
722
723
724
725
726
727 protected MCParticle findMcParticleAncestor(MCParticle mcParticle, List<MCParticle> mcParticles) {
728 MCParticle ancestor = null;
729
730 if (mcParticles.contains(mcParticle)) {
731 ancestor = mcParticle;
732 } else {
733 List<MCParticle> parents = mcParticle.getParents();
734 if (parents.size() > 0) {
735
736 ancestor = findMcParticleAncestor(parents.get(0), mcParticles);
737 }
738 }
739 if (ancestor == null) {
740 print(HLEVEL_DEFAULT,
741 "Warning: no ancestor found in mc particle list." + "\tEnergy: " + mcParticle.getEnergy() + "\n"
742 + "\tCharge: " + mcParticle.getCharge() + "\n" + "\tPDGID: " + mcParticle.getPDGID() + "\n"
743 + "\tGenStatus: " + mcParticle.getGeneratorStatus() + "\n" + "\tSimStatus: "
744 + mcParticle.getSimulatorStatus().getValue(), true);
745 }
746 return ancestor;
747 }
748
749
750
751
752
753
754
755
756
757
758
759 public static <F, T> RelationalTable<F, T> createRelationalTable(List<LCRelation> relations) {
760 RelationalTable<F, T> relationalTable = new BaseRelationalTable<F, T>();
761 for (LCRelation relation : relations) {
762 relationalTable.add((F) relation.getFrom(), (T) relation.getTo(), relation.getWeight());
763 }
764 return relationalTable;
765 }
766
767
768
769
770
771
772
773
774
775 public static Map sortMapByHighestValue(Map map) {
776 List list = new LinkedList(map.entrySet());
777 Collections.sort(list, new Comparator() {
778 public int compare(Object o1, Object o2) {
779 return -((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
780 }
781 });
782
783 Map result = new LinkedHashMap();
784 for (Iterator it = list.iterator(); it.hasNext();) {
785 Map.Entry entry = (Map.Entry) it.next();
786 result.put(entry.getKey(), entry.getValue());
787 }
788 return result;
789 }
790 }