-
Notifications
You must be signed in to change notification settings - Fork 0
/
StdhepConverter.java
executable file
·148 lines (133 loc) · 5.12 KB
/
StdhepConverter.java
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
package hep.physics.stdhep.convert;
import hep.io.stdhep.*;
import hep.physics.event.generator.GeneratorFactory;
import hep.physics.event.generator.MCEvent;
import hep.physics.particle.BasicParticle;
import hep.physics.particle.Particle;
import hep.physics.particle.properties.ParticlePropertyManager;
import hep.physics.particle.properties.ParticlePropertyProvider;
import hep.physics.particle.properties.ParticleType;
import hep.physics.vec.BasicHep3Vector;
import hep.physics.vec.BasicHepLorentzVector;
import hep.physics.vec.Hep3Vector;
import hep.physics.vec.HepLorentzVector;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* A class that converts MCEvent<-->StdhepEvent
* @author Tony Johnson ([email protected])
* @version $Id: StdhepConverter.java 8584 2006-08-10 23:06:37Z duns $
*/
public class StdhepConverter
{
private static ParticlePropertyProvider ppp;
private static GeneratorFactory factory;
public StdhepConverter()
{
this(ParticlePropertyManager.getParticlePropertyProvider());
}
public StdhepConverter(ParticlePropertyProvider ppp)
{
this(ppp, new GeneratorFactory());
}
public StdhepConverter(ParticlePropertyProvider ppp, GeneratorFactory factory)
{
this.ppp = ppp;
this.factory = factory;
}
/**
* Convert from a StdhepEvent to an MCEvent.
* Useful when reading stdhep files.
*/
public MCEvent convert(StdhepEvent hepevt)
{
MCEvent event = factory.createEvent(0,hepevt.getNEVHEP());
int n = hepevt.getNHEP();
BasicParticle[] particle = new BasicParticle[n];
for (int i=0; i<n; i++)
{
Hep3Vector origin = new BasicHep3Vector(hepevt.getVHEP(i,0),hepevt.getVHEP(i,1),hepevt.getVHEP(i,2));
Hep3Vector momentum = new BasicHep3Vector(hepevt.getPHEP(i,0),hepevt.getPHEP(i,1),hepevt.getPHEP(i,2));
HepLorentzVector p = new BasicHepLorentzVector(hepevt.getPHEP(i,3),momentum);
ParticleType type = ppp.get(hepevt.getIDHEP(i));
particle[i] = factory.createParticle(origin,p,type,hepevt.getISTHEP(i), hepevt.getVHEP(i,3));
particle[i].setMass(hepevt.getPHEP(i,4));
}
// Deal with daughters
for (int i=0; i<n; i++)
{
if (hepevt.getJDAHEP(i,0) <= 0) continue;
for (int j=hepevt.getJDAHEP(i,0)-1;j<hepevt.getJDAHEP(i,1);j++)
{
particle[i].addDaughter(particle[j]);
}
}
event.put(MCEvent.MC_PARTICLES,Arrays.asList(particle));
return event;
}
/**
* Convert from an EventHeader to a StdhepEvent.
* Useful when writing stdhep files.
*
* Note the stdhep format requires all daughters to be stored consecutively.
* In principle we would have to do a sort on daughter to gaurantee that this could be
* accomodated. Right now we just throw a RuntimeException if it is not true.
*/
public StdhepEvent convert(MCEvent event)
{
List particles = event.getMCParticles();
int nevhep = event.getEventNumber();
int nhep = particles.size();
double[] phep = new double[5*nhep];
double[] vhep = new double[4*nhep];
int[] isthep = new int[nhep];
int[] idhep = new int[nhep];
int[] jmohep = new int[2*nhep];
int[] jdahep = new int[2*nhep];
int j = 0;
int k = 0;
int l = 0;
int m = 0;
for (int i=0; i<nhep; i++)
{
Particle particle = (Particle) particles.get(i);
idhep[i] = particle.getType().getPDGID();
isthep[i] = particle.getGeneratorStatus();
phep[j++] = particle.getPX();
phep[j++] = particle.getPY();
phep[j++] = particle.getPZ();
phep[j++] = particle.getEnergy();
phep[j++] = particle.getMass();
vhep[k++] = particle.getOriginX();
vhep[k++] = particle.getOriginY();
vhep[k++] = particle.getOriginZ();
vhep[k++] = particle.getProductionTime();
// Deal with the particle's parents
Iterator parents = particle.getParents().iterator();
jmohep[l++] = parents.hasNext() ? particles.indexOf(parents.next()) : 0;
jmohep[l++] = parents.hasNext() ? particles.indexOf(parents.next()) : 0;
// Deal with the particle's daughters.
Iterator daughters = particle.getDaughters().iterator();
if (!daughters.hasNext())
{
jdahep[m++] = 0;
jdahep[m++] = -1;
}
else
{
Object firstDaughter = daughters.next();
Object lastDaughter = firstDaughter;
for (int n = particles.indexOf(firstDaughter)+1; daughters.hasNext(); n++)
{
lastDaughter = daughters.next();
if (particles.indexOf(lastDaughter) != n)
throw new RuntimeException("Daughters are not consecutive");
}
jdahep[m++] = particles.indexOf(firstDaughter);
jdahep[m++] = particles.indexOf(lastDaughter);
}
}
return new StdhepEvent(nevhep,nhep,isthep,idhep,jmohep,jdahep,phep,vhep);
}
}