/
ParticleMC.cxx
337 lines (308 loc) · 10.3 KB
/
ParticleMC.cxx
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
\file
Implementation of class erhic::ParticleMC.
\author Thomas Burton
\date 2011-10-10
\copyright 2011 Brookhaven National Lab
*/
#include "eicsmear/erhic/ParticleMC.h"
#include <iostream>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <string>
#include <TLorentzRotation.h>
#include <TParticlePDG.h>
#include <TRotation.h>
#include "eicsmear/erhic/EventBase.h"
#include "eicsmear/functions.h"
namespace {
/*
Returns the boost to transform to the rest frame of the vector "rest".
If z is non-NULL, rotate the frame so that z AFTER BOOSTING
defines the positive z direction of that frame.
e.g. To boost a gamma-p interaction from the lab frame to the
proton rest frame with the virtual photon defining z:
computeBoost(protonLab, photonLab);
*/
TLorentzRotation computeBoost(const TLorentzVector& rest,
const TLorentzVector* z) {
TLorentzRotation toRest(-(rest.BoostVector()));
if (z) {
TRotation rotate;
TLorentzVector boostedZ(*z);
boostedZ *= toRest;
rotate.SetZAxis(boostedZ.Vect());
// We need the rotation of the frame, so take the inverse.
// See the TRotation documentation for more explanation.
rotate = rotate.Inverse();
toRest.Transform(rotate);
} // if
return toRest;
}
} // anonymous namespace
namespace erhic {
ParticleMCbase::ParticleMCbase()
: I(0)
, KS(0)
, id(0)
, orig(0)
, orig1(0)
, daughter(0)
, ldaughter(0)
, px(0.)
, py(0.)
, pz(0.)
, E(0.)
, m(0.)
, pt(0.)
, xv(0.)
, yv(0.)
, zv(0.)
, parentId(std::numeric_limits<Int_t>::min())
, p(0.)
, theta(0.)
, phi(0.)
, rapidity(0.)
, eta(0.)
, z(0.)
, xFeynman(0.)
, thetaGamma(0.)
, ptVsGamma(0.)
, thetaGammaHCM(0.)
, ptVsGammaHCM(0.)
, phiPrf(0.)
{
}
ParticleMC::ParticleMC(const std::string &line, bool eAflag): ParticleMCbase(), eA(0)
{
// Initialise to nonsense values to make input errors easy to spot
if (!line.empty()) {
static std::stringstream ss;
ss.str("");
ss.clear();
ss << line;
if (eAflag) {
eA = new ParticleMCeA();
ss >>
//changed by liang to add particle mother1
I >> KS >> id >> orig1 >> orig >> daughter >> ldaughter >>
px >> py >> pz >> E >> m >> xv >> yv >> zv
//added by liang to include add particle data structure
>> eA->massNum >> eA->charge >> eA->NoBam;
//eA->pz = pz; orig1 = eA->orig1;
} else {
ss >>
I >> KS >> id >> orig >> daughter >> ldaughter >>
px >> py >> pz >> E >> m >> xv >> yv >> zv;
orig1 = 0;
} //if
// We should have no stream errors and should have exhausted
// the whole of the stream filling the particle.
if (ss.fail() || !ss.eof()) {
throw std::runtime_error("Bad particle input: " + line);
} // if
ComputeDerivedQuantities();
} // if
}
ParticleMC::~ParticleMC()
{
if (eA) delete eA;
}
// FIXME: may also want to print out ParticleMCeA variables?;
void ParticleMCbase::Print(Option_t* /* option */) const {
std::cout << I << '\t' << KS << '\t' << id << '\t' << orig << '\t' <<
daughter << '\t' << ldaughter << '\t' << px << '\t' << py << '\t' << pz
<< '\t' << E << '\t' << m << '\t' << xv << '\t' << yv << '\t' << zv <<
std::endl;
}
void ParticleMCbase::ComputeDerivedQuantities() {
// Calculate quantities that depend only on the properties already read.
pt = sqrt(pow(px, 2.) + pow(py, 2.));
p = sqrt(pow(pt, 2.) + pow(pz, 2.));
// Rapidity and pseudorapidity
Double_t Epluspz = E + pz;
Double_t Eminuspz = E - pz;
Double_t Ppluspz = p + pz;
Double_t Pminuspz = p - pz;
if (Eminuspz <= 0.0 || Pminuspz == 0.0 ||
Ppluspz == 0.0 || Epluspz <= 0.0) {
// Dummy values to avoid zero or infinite arguments in calculations
rapidity = -19.;
eta = -19.;
} else {
rapidity = 0.5 * log(Epluspz / Eminuspz);
eta = 0.5 * log(Ppluspz / Pminuspz);
} // if
theta = atan2(pt, pz);
phi = TVector2::Phi_0_2pi(atan2(py, px));
}
void ParticleMCbase::ComputeEventDependentQuantities(EventMC& event) {
try {
// Get the beam hadon, beam lepton and exchange boson.
const TLorentzVector& hadron = event.BeamHadron()->Get4Vector();
const TLorentzVector& lepton = event.ScatteredLepton()->Get4Vector();
// Determine the exchange boson 4-vector from the scattered lepton,
// since we're not always guaranteed to have one (weak neutral current e.g.)
// const TLorentzVector& boson = event.ExchangeBoson()->Get4Vector();
const TLorentzVector boson = event.BeamLepton()->Get4Vector() - lepton;
// Calculate z using the 4-vector definition,
// so we don't care about frame of reference.
z = hadron.Dot(Get4Vector()) / hadron.Dot(boson);
// Calculate properties in the proton rest frame.
// We want pT and angle with respect to the virtual photon,
// so use that to define the z axis.
TLorentzRotation toHadronRest = computeBoost(hadron, &boson);
// Boost this particle to the proton rest frame and calculate its
// pT and angle with respect to the virtual photon:
TLorentzVector p = (Get4Vector() *= toHadronRest);
thetaGamma = p.Theta();
ptVsGamma = p.Pt();
// Calculate phi angle around virtual photon according
// to the HERMES convention.
TLorentzVector bosonPrf = (TLorentzVector(boson) *= toHadronRest);
TLorentzVector leptonPrf = (TLorentzVector(lepton) *= toHadronRest);
phiPrf = computeHermesPhiH(p, leptonPrf, bosonPrf);
// Feynman x with xF = 2 * pz / W in the boson-hadron CM frame.
// First boost to boson-hadron centre-of-mass frame.
// Use the photon to define the z direction.
TLorentzRotation boost = computeBoost(boson + hadron, &boson);
xFeynman = 2. * (Get4Vector() *= boost).Pz() / sqrt(event.GetW2());
thetaGammaHCM = (Get4Vector() *= boost).Theta();
ptVsGammaHCM = (Get4Vector() *= boost).Pt();
// Determine the PDG code of the parent particle, if the particle
// has a parent and the parent is present in the particle array.
// The index of the particles from the Monte Carlo runs from [1,N]
// while the index in the array runs from [0,N-1], so subtract 1
// from the parent index to find its position.
if (event.GetNTracks() > unsigned(orig - 1)) {
parentId = event.GetTrack(orig - 1)->Id();
} // if
} // try
catch(std::exception& e) {
std::cerr <<
"Exception in Particle::ComputeEventDependentQuantities: " <<
e.what() << std::endl;
} // catch
}
TLorentzVector ParticleMCbase::Get4Vector() const {
return TLorentzVector(px, py, pz, E);
}
const EventMC* ParticleMCbase::GetEvent() const {
return static_cast<EventMC*>(event.GetObject());
}
const ParticleMC* ParticleMC::GetChild(UShort_t u) const {
// Look up this particle's child via the event
// containing it and the index of the child in that event.
if (!GetEvent()) {
return NULL;
} // if
// index is in the range [1,N]
unsigned idx = daughter + u;
if (daughter < 1 || // If first daughter index = 0, it has no children
u >= GetNChildren()) { // Insufficient children
return NULL;
} // if
--idx; // Convert [1,N] --> [0,N)
const ParticleMC* p(NULL);
// Check this index is within the # of particles in the event
if (idx < GetEvent()->GetNTracks()) {
p = GetEvent()->GetTrack(idx);
} // if
return p;
}
const ParticleMC* ParticleMC::GetParent() const {
// Look up this particle's parent via the event
// containing it and the index of the parent in that event.
const ParticleMC* p(NULL);
if (GetEvent()) {
if (GetEvent()->GetNTracks() >= GetParentIndex()) {
p = GetEvent()->GetTrack(GetParentIndex() - 1);
} // if
} // if
return p;
}
Bool_t ParticleMC::HasChild(Int_t pdg) const {
for (UInt_t i(0); i < GetNChildren(); ++i) {
if (!GetChild(i)) {
continue;
} // if
if (pdg == GetChild(i)->Id()) {
return true;
} // if
} // for
return false;
}
TLorentzVector ParticleMCbase::Get4VectorInHadronBosonFrame() const {
double p_(0.), e_(0.), px_(ptVsGammaHCM), py_(0.), pz_(0.);
// Calculate mangitude of momentum from pT and polar angle in
// hadron-boson frame. If theta is ~parallel to the beam just set
// p to whatever pT is (not that it makes all that much sense).
if (thetaGammaHCM > 1.e-6) {
p_ = ptVsGammaHCM / sin(thetaGammaHCM);
} // if
// Deal with virtual particles later, so check if particle is off mass-shell
if (!(m < 0.)) {
e_ = sqrt(pow(p_, 2.) + pow(m, 2.));
} else {
e_ = sqrt(pow(p_, 2.) - pow(m, 2.));
if (TMath::IsNaN(e_)) {
e_ = 0.;
} // if
} // if
// Calculate pZ from pT and theta, unless it's very close to the beam,
// in which case set it to p.
if (thetaGammaHCM > 1.e-6) {
pz_ = ptVsGammaHCM / tan(thetaGammaHCM);
} // if
// Calculate px and py, unless a dummy phi value is present.
if (phiPrf > -100.) {
px_ = ptVsGammaHCM * cos(phiPrf);
py_ = ptVsGammaHCM * sin(phiPrf);
} // if
// If we ended up with no energy, it's likely ths is the exchange boson,
// as nothing will have happened above. If so, try to reference the event
// record to get the necessary information, as we don't have enough
// in the particle itself.
// Note that this appears not to work in a TTree as ROOT doesn't read
// the event when it reads the particle, though I'm not certain of the
// exact cause.
if (m < 0. && GetEvent()) {
if (GetEvent()->ExchangeBoson()) {
// Don't check if GetEvent()->ExchangeBoson() == this, in case this
// particle is a copy of the event track that isn't part of a copied
// event.
if (GetEvent()->ExchangeBoson()->GetIndex() == GetIndex()) {
// If it's the exchange boson just set E == nu.
e_ = GetEvent()->GetNu();
// Calculate p, careful about negative mass.
p_ = sqrt(pow(e_, 2.) + pow(m, 2.));
pz_ = p_ * cos(thetaGammaHCM);
} // if
} // if
} // if
return TLorentzVector(px_, py_, pz_, e_);
}
void ParticleMCbase::SetEvent(EventMC* e) {
event = e;
}
void ParticleMCbase::Set4Vector(const TLorentzVector& v) {
E = v.Energy();
px = v.Px();
py = v.Py();
pz = v.Pz();
m = v.M();
ComputeDerivedQuantities(); // Rapidity etc
// If an event reference is set, recalculate event-dependent quantities
// like z, xF
EventMC* ev = static_cast<EventMC*>(event.GetObject());
if (ev) {
ComputeEventDependentQuantities(*ev);
} // if
}
void ParticleMCbase::SetVertex(const TVector3& v) {
xv = v.X();
yv = v.Y();
zv = v.Z();
}
} // namespace erhic