1 package org.lcsim.util.loop;
2
3 import hep.io.stdhep.*;
4 import hep.physics.event.generator.GeneratorFactory;
5 import hep.physics.event.generator.MCEvent;
6 import hep.physics.particle.BasicParticle;
7 import hep.physics.particle.properties.ParticlePropertyManager;
8 import hep.physics.particle.properties.ParticlePropertyProvider;
9 import hep.physics.particle.properties.ParticleType;
10 import hep.physics.vec.BasicHep3Vector;
11 import hep.physics.vec.BasicHepLorentzVector;
12 import hep.physics.vec.Hep3Vector;
13 import hep.physics.vec.HepLorentzVector;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20
21
22
23
24
25
26 class StdhepConverter
27 {
28 private ParticlePropertyProvider ppp;
29 private GeneratorFactory factory;
30 private boolean haveWarned;
31
32 StdhepConverter()
33 {
34 this(ParticlePropertyManager.getParticlePropertyProvider());
35 }
36 StdhepConverter(ParticlePropertyProvider ppp)
37 {
38 this(ppp, new GeneratorFactory());
39 }
40 StdhepConverter(ParticlePropertyProvider ppp, GeneratorFactory factory)
41 {
42 this.ppp = ppp;
43 this.factory = factory;
44 }
45
46
47
48
49 MCEvent convert(StdhepEvent hepevt)
50 {
51 MCEvent event = factory.createEvent(0,hepevt.getNEVHEP());
52
53 int n = hepevt.getNHEP();
54 BasicParticle[] particle = new BasicParticle[n];
55 for (int i=0; i<n; i++)
56 {
57 Hep3Vector origin = new BasicHep3Vector(hepevt.getVHEP(i,0),hepevt.getVHEP(i,1),hepevt.getVHEP(i,2));
58 Hep3Vector momentum = new BasicHep3Vector(hepevt.getPHEP(i,0),hepevt.getPHEP(i,1),hepevt.getPHEP(i,2));
59 HepLorentzVector p = new BasicHepLorentzVector(hepevt.getPHEP(i,3),momentum);
60 ParticleType type = ppp.get(hepevt.getIDHEP(i));
61 particle[i] = factory.createParticle(origin,p,type,hepevt.getISTHEP(i), hepevt.getVHEP(i,3));
62 particle[i].setMass(hepevt.getPHEP(i,4));
63 }
64 int[] vec = new int[n];
65 List<Set<BasicParticle>> ancestors = new ArrayList<Set<BasicParticle>>(n);
66 for (int i=0; i<n; i++) ancestors.add(new HashSet<BasicParticle>());
67
68 for (int i=0; i<n; i++)
69 {
70 int idx1 = hepevt.getJMOHEP(i,0) - 1;
71 int idx2 = hepevt.getJMOHEP(i,1) - 1;
72 int l = fillIndexVec(vec,idx1,idx2);
73
74 for (int j=0; j<l; j++)
75 {
76 checkAndAddDaughter(particle,ancestors,vec[j],i);
77 }
78 }
79
80 for (int i=0; i<n; i++)
81 {
82 int idx1 = hepevt.getJDAHEP(i,0) % 10000 - 1;
83 int idx2 = hepevt.getJDAHEP(i,1) % 10000 - 1;
84 int l = fillIndexVec(vec,idx1,idx2);
85
86 for (int j=0; j<l; j++)
87 {
88 checkAndAddDaughter(particle,ancestors,i,vec[j]);
89 }
90 }
91 event.put(MCEvent.MC_PARTICLES,Arrays.asList(particle));
92
93 event.put("StdhepEvent",hepevt);
94 return event;
95 }
96 private void checkAndAddDaughter(BasicParticle[] particle, List<Set<BasicParticle>> ancestors, int parentID, int childID)
97 {
98 if (parentID == childID) return;
99 Set<BasicParticle> ancestor = ancestors.get(childID);
100 boolean added = ancestor.add(particle[parentID]);
101 if (added) particle[parentID].addDaughter(particle[childID]);
102
103 }
104 private int fillIndexVec(int[] vec, int idx1, int idx2)
105 {
106 int l = 0;
107 try
108 {
109 if ( idx1 >= 0 && idx2 >= 0 )
110 {
111 if ( idx1 < idx2 )
112 {
113 for ( int i = idx1; i < (idx2 + 1); i++ )
114 {
115 vec[l++] = i;
116 }
117 }
118 else if ( idx1 > idx2 )
119 {
120 vec[l++] = idx1;
121 vec[l++] = idx2;
122 }
123
124 else
125 {
126 vec[l++] = idx1;
127 }
128 }
129 else if ( idx1 >= 0 )
130 {
131 vec[l++] = idx1;
132 }
133 }
134 catch (ArrayIndexOutOfBoundsException x)
135 {
136 if (!haveWarned) System.err.println("Warning: Array index out of bounds exception caused by corrupt stdhep file ignored");
137 haveWarned = true;
138 }
139 return l;
140 }
141 }