ROOTPWA
event.cc
Go to the documentation of this file.
1 #include <iomanip>
2 #include <limits>
3 
4 #include "event.h"
5 
6 
7 using namespace std;
8 
9 
11 
12 
14  : _beam (NULL),
15  _target (NULL),
16  _ioversion(1)
17 {
18 }
19 
20 bool
22  return (_beam!=NULL) && (_final.size()!=0);
23 }
24 
25 
26 
28 {
29  _final = e._final;
30  _initial = e._initial;
31  _beam = new particle(*e._beam);
32  _target = new particle(*e._target);
34 }
35 
36 
38 {
39  if (_beam)
40  delete _beam;
41  if (_target)
42  delete _target;
43 }
44 
45 
46 event&
48 {
49  if (_beam)
50  delete _beam;
51  if (_target)
52  delete _target;
53  _final = e._final;
54  _initial = e._initial;
55  _beam = new particle(*e._beam);
56  _target = new particle(*e._target);
58  return *this;
59 }
60 
61 
62 event
64  const event& e)
65 {
66  event r;
67  r.beam (L * e.beam());
68  r.target(L * e.target());
69  list<particle>::const_iterator p = e._initial.begin();
70  while (p != e._initial.end()) {
71  r.addinitial(L * (*p));
72  ++p;
73  }
74  p = e._final.begin();
75  while (p != e._final.end()) {
76  r.addfinal(L * (*p));
77  ++p;
78  }
79  return r;
80 }
81 
82 
83 event&
85 {
86  _final.push_back(p);
87  return *this;
88 }
89 
90 
91 event&
93 {
94  _initial.push_back(p);
95  return *this;
96 }
97 
98 
99 event&
101 {
102  if (!_initial.empty())
103  _initial.erase(_initial.begin(), _initial.end());
104  if (!_final.empty())
105  _final.erase(_final.begin(), _final.end());
106  return *this;
107 }
108 
109 
110 event&
112 {
113  _initial.push_front(p);
114  if (_beam)
115  *_beam = p;
116  else
117  _beam = new particle(p);
118  return *this;
119 }
120 
121 
122 event&
124 {
125  _initial.push_back(p);
126  if (_target)
127  *_target = p;
128  else
129  _target = new particle(p);
130  return *this;
131 }
132 
133 
134 int
135 event::OK(const double epsilon) const
136 {
137  list<particle>::const_iterator p;
138  int q_initial = 0;
139  int q_final = 0;
140  fourVec p_initial, p_final;
141  int q_conserved, p_conserved;
142 
143  p = _initial.begin();
144  while (p != _initial.end()) {
145  q_initial += p->Charge();
146  p_initial += p->get4P();
147  ++p;
148  }
149  p = _final.begin();
150  while (p != _final.end()) {
151  q_final += p->Charge();
152  p_final += p->get4P();
153  ++p;
154  }
155  q_conserved = q_initial == q_final;
156  p_conserved = (p_initial - p_final).lenSq() < epsilon;
157 
158  return(q_conserved && p_conserved);
159 }
160 
161 
162 fourVec
163 event::getPartPFinal(const string& name,
164  const int charge,
165  const int index,
166  const int debug) const
167 {
168  int i = 0;
169  if (debug)
170  cout << "Looking for " << name << charge << "[" << index << "] in event" << endl;
171  list<particle>::const_iterator p = _final.begin();
172  while (p != _final.end()) {
173  if (debug)
174  cout << "checking against " << p->Name() << p->Charge() << endl;
175  if ((p->Name() == name) && (p->Charge() == charge)) {
176  ++i;
177  if (debug)
178  cout << "found one" << endl
179  << "checking against index " << i << endl;
180  if (i == index) {
181  if (debug) {
182  cout << "found the right one, getting 4p" << endl
183  << "4p:" << endl;
184  p->get4P().print();
185  }
186  return p->get4P();
187  }
188  }
189  ++p;
190  }
191  throw("PartNotFound");
192  return(fourVec(0, threeVec(0, 0, 0)));
193 }
194 
195 
196 fourVec
197 event::getPartPInitial(const string& name,
198  const int charge,
199  const int index) const
200 {
201  int i = 1;
202  list<particle>::const_iterator p = _initial.begin();
203  while (p != _initial.end())
204  if ((p->Name() == name) && (i++ == index))
205  return p->get4P();
206  throw("PartNotFound");
207  return(fourVec(0, threeVec(0, 0, 0)));
208 }
209 
210 
211 int
213 {
214  int q = 0;
215  list<particle>::const_iterator p = _final.begin();
216  while (p != _final.end()) {
217  q += p->Charge();
218  ++p;
219  }
220  return q;
221 }
222 
223 
224 double
226 {
227  list<particle>::const_iterator it = _final.begin();
228  fourVec pX;
229  fourVec p;
230  while (it != _final.end()) {
231  pX = it->get4P();
232  p += pX;
233  ++it;
234  }
235  // sort by mass
236  double m = p.len();
237  return m;
238 }
239 
240 
241 list<particle>
243 {
244  list<particle> l;
245  list<particle>::const_iterator p = _final.begin();
246  while (p != _final.end()) {
247  if (p->J()%2 == 0)
248  l.push_back(*p);
249  ++p;
250  }
251  return l;
252 }
253 
254 
255 list<particle>
257 {
258  list<particle> l;
259  list<particle>::const_iterator p = _final.begin();
260  while (p != _final.end()) {
261  if (p->J()%2 == 1)
262  l.push_back(*p);
263  ++p;
264  }
265  return l;
266 }
267 
268 
269 particle
270 event::f_particle(const string& name,
271  const int charge,
272  const int index) const
273 {
274  int i = 0;
275  list<particle>::const_iterator p = _final.begin();
276  while (p != _final.end()) {
277  if ((p->Name() == name) && (p->Charge() == charge)) {
278  ++i;
279  if (i == index)
280  return *p;
281  }
282  ++p;
283  }
284  throw("PartNotFound");
285 }
286 
287 
288 void
289 event::set_f_mesons(const list<particle>& l)
290 {
291  _final.clear();
292  _final = l;
293 }
294 
295 
296 int
298 {
299  int q = 0;
300  list<particle>::const_iterator p = _initial.begin();
301  while (p != _initial.end()) {
302  q += p->Charge();
303  ++p;
304  }
305  return q;
306 }
307 
308 
309 list<particle>
311 {
312  list<particle> l;
313  list<particle>::const_iterator p = _initial.begin();
314  while (p != _initial.end()) {
315  if (p->J()%2 == 0)
316  l.push_back(*p);
317  ++p;
318  }
319  return l;
320 }
321 
322 
323 list<particle>
325 {
326  list<particle> l;
327  list<particle>::const_iterator p = _initial.begin();
328  while (p != _initial.end()) {
329  if (p->J()%2 == 1)
330  l.push_back(*p);
331  ++p;
332  }
333  return l;
334 }
335 
336 
337 particle
338 event::i_particle(const string& name,
339  const int charge,
340  const int index) const
341 {
342  int i = 0;
343  list<particle>::const_iterator p = _initial.begin();
344  while (p != _initial.end()) {
345  if ((p->Name() == name) && (p->Charge() == charge)) {
346  ++i;
347  if (i == index)
348  return *p;
349  }
350  ++p;
351  }
352  throw("PartNotFound");
353 }
354 
355 
356 threeVec
358 {
359  list<particle> i = i_mesons();
360  threeVec A;
361  list<particle>::const_iterator p = i.begin();
362  while (p != i.end()) {
363  A += p->get3P();
364  ++p;
365  }
366  list<particle> f = f_mesons();
367  threeVec C;
368  p = f.begin();
369  while (p != f.end()) {
370  C += p->get3P();
371  ++p;
372  }
373  if ((A < threeVec(1e-4, 0, 0)) || (C < threeVec(1e-4, 0, 0)))
374  return threeVec(0, 0, 0);
375  threeVec N = A / C;
376  N *= (1 / N.len());
377  return N;
378 }
379 
380 
381 threeVec
383 {
384  list<particle> i = i_baryons();
385  threeVec B;
386  list<particle>::const_iterator p = i.begin();
387  while (p != i.end()) {
388  B += p->get3P();
389  ++p;
390  }
391  list<particle> f = f_baryons();
392  threeVec D;
393  p = f.begin();
394  while (p != f.end()) {
395  D += p->get3P();
396  ++p;
397  }
398  if ((B < threeVec(1e-4, 0, 0)) || (D < threeVec(1e-4, 0, 0)))
399  return threeVec(0, 0, 0);
400  threeVec N = B / D;
401  N *= (1 / N.len());
402 
403  return N;
404 }
405 
406 
407 void
409 {
410  cout << "beam: ";
411  _beam->get4P().print();
412  cout << "target: ";
413  _target->get4P().print();
414  cout << "final particles: ";
415  cout << endl;
416  list<particle>::const_iterator p = _final.begin();
417  while (p != _final.end()) {
418  p->print();
419  ++p;
420  }
421 }
422 
423 
424 ostream&
425 operator << (ostream& os, const event& e)
426 {
427  switch (e._ioversion) {
428  case 1:
429  return e.write1(os);
430  break;
431  case 2:
432  return e.write2(os);
433  break;
434  default:
435  throw("badIOVersion");
436  }
437 }
438 
439 
440 ostream&
441 event::write1(ostream& out) const
442 {
443  const unsigned int nmbDigits = numeric_limits<double>::digits10 + 1;
444  ostringstream s;
445  s.precision(nmbDigits);
446  s.setf(ios_base::scientific, ios_base::floatfield);
447  s << _final.size() + 1 << endl;
448  fourVec v = _beam->get4P();
449  s << name2id(_beam->Name(), _beam->Charge()) << " "
450  << _beam->Charge() << " "
451  << v.x() << " " << v.y() << " " << v.z() << " "
452  << v.t() << endl;
453  list<particle>::const_iterator part = _final.begin();
454  while (part != _final.end()) {
455  v = part->get4P();
456  s << name2id(part->Name(), part->Charge()) << " "
457  << part->Charge() << " "
458  << v.x() << " " << v.y() << " " << v.z() << " "
459  << v.t() << endl;
460  ++part;
461  }
462  out << s.str();
463  return out;
464 }
465 
466 
467 ostream&
468 event::write2(ostream& out) const
469 {
470  const unsigned int nmbDigits = numeric_limits<double>::digits10 + 1;
471  ostringstream s;
472  s.precision(nmbDigits);
473  s.setf(ios_base::scientific, ios_base::floatfield);
474  fourVec v = _beam->get4P();
475  s << "B " << name2id(_beam->Name(), _beam->Charge()) << " "
476  << _beam->Charge() << " "
477  << v.t() << " "
478  << v.x() << " " << v.y() << " " << v.z() << " "
479  << endl;
480  v = _target->get4P();
481  s << "T " << name2id(_target->Name(), _target->Charge()) << " "
482  << _target->Charge() << " "
483  << v.t() << " "
484  << v.x() << " " << v.y() << " " << v.z() << " "
485  << endl;
486  list<particle>::const_iterator part = _final.begin();
487  while (part != _final.end()) {
488  v = part->get4P();
489  s << "F " << name2id(part->Name(), part->Charge()) << " "
490  << part->Charge() << " "
491  << v.t() << " "
492  << v.x() << " " << v.y() << " " << v.z() << " "
493  << endl;
494  ++part;
495  }
496  s << "E" << endl;
497  out << s.str();
498  return out;
499 }
500 
501 
502 istream&
503 operator >> (istream& is, event& e)
504 {
505  switch (e._ioversion) {
506  case 1:
507  return e.read1(is);
508  break;
509  case 2:
510  return e.read2(is);
511  break;
512  default:
513  throw("badIOVersion");
514  }
515 }
516 
517 
518 istream&
519 event::read1(istream& is)
520 {
521  erase();
522  particle Target(PDGtable.get("p"), 1);
523  Target.set4P(fourVec(Target.Mass(), threeVec(0, 0, 0)));
524  target(Target);
525  int nparticles = 0;
526  is >> nparticles;
527  for (int i = 0; i < nparticles; ++i) {
528  int ptype, q;
529  double px, py, pz, t;
530  is >> ptype >> q >> px >> py >> pz >> t;
531  string name = id2name((Geant_ID)ptype);
532  particle part(PDGtable.get(name), q);
533  part.setName(name);
534  part.set4P(fourVec(t, threeVec(px, py, pz)));
535  if (i == 0)
536  beam(part);
537  else
538  addfinal(part);
539  }
540  return is;
541 }
542 
543 
544 istream&
545 event::read2(istream& is)
546 {
547  erase();
548  char Tag = 0;
549  while (!(is >> Tag).eof()) {
550  int ptype, q;
551  double px, py, pz, t;
552  is >> ptype >> q >> t >> px >> py >> pz;
553  string name;
554  name = id2name((Geant_ID)ptype);
555  particle part(PDGtable.get(name), q);
556  part.set4P(fourVec(t, threeVec(px, py, pz)));
557  switch (Tag) {
558  case 'I':
559  addinitial(part);
560  break;
561  case 'F':
562  addfinal(part);
563  break;
564  case 'B':
565  beam(part);
566  break;
567  case 'T':
568  target(part);
569  break;
570  case 'E':
571  return is;
572  }
573  }
574  return is;
575 }
576 
577 
578 event&
580 {
581  if ((ver >= 1) && (ver <= 2))
582  _ioversion = ver;
583  else {
584  cerr << "unknown io version " << ver << endl;
585  throw ("UnknownIOVersion");
586  }
587  return *this;
588 }