rivet is hosted by Hepforge, IPPP Durham
Rivet  2.7.0
RivetHepMC.hh
1 // -*- C++ -*-
2 #ifndef RIVET_RivetHepMC_HH
3 #define RIVET_RivetHepMC_HH
4 
5 #include "HepMC/GenEvent.h"
6 #include "HepMC/GenParticle.h"
7 #include "HepMC/GenVertex.h"
8 #include "HepMC/GenRanges.h"
9 #include "HepMC/IO_GenEvent.h"
10 #include "Rivet/Tools/RivetSTL.hh"
11 #include "Rivet/Tools/Exceptions.hh"
12 
13 namespace Rivet {
14 
15 
16  using HepMC::GenEvent;
17  using HepMC::GenParticle;
18  using HepMC::GenVertex;
19 
20  #if HEPMC_VERSION_CODE >= 3000000
21  using HepMC::GenEventPtr;
22  using HepMC::GenParticlePtr;
23  using HepMC::GenVertexPtr;
24  #elif HEPMC_VERSION_CODE >= 2007000
25  // HepMC 2.07 provides its own #defines
26  #else
27  #define GenEventPtr GenEvent*
28  #define GenParticlePtr GenParticle*
29  #define GenVertexPtr GenVertex*
30  #endif
31 
32 
34 
35 
36  inline std::vector<GenParticle const *> particles(const GenEvent* ge) {
37  assert(ge);
38  std::vector<const GenParticle*> rtn;
39  for (GenEvent::particle_const_iterator pi = ge->particles_begin(); pi != ge->particles_end(); ++pi)
40  rtn.push_back(*pi);
41  return rtn;
42  }
43 
44  inline std::vector<GenParticlePtr> particles(GenEvent* ge) {
45  assert(ge);
46  std::vector<GenParticle*> rtn;
47  for (GenEvent::particle_iterator pi = ge->particles_begin(); pi != ge->particles_end(); ++pi)
48  rtn.push_back(*pi);
49  return rtn;
50  }
51 
52 
53  inline std::vector<const GenVertex*> vertices(const GenEvent* ge) {
54  std::vector<GenVertex const *> rtn;
55  for (GenEvent::vertex_const_iterator vi = ge->vertices_begin(); vi != ge->vertices_end(); ++vi)
56  rtn.push_back(*vi);
57  return rtn;
58  }
59 
60  inline std::vector<GenVertex*> vertices(GenEvent* ge) {
61  std::vector<GenVertex*> rtn;
62  for (GenEvent::vertex_iterator vi = ge->vertices_begin(); vi != ge->vertices_end(); ++vi)
63  rtn.push_back(*vi);
64  return rtn;
65  }
66 
67 
69 
70 
71  inline std::vector<const GenParticle*> particles(const GenVertex* gv, HepMC::IteratorRange range=HepMC::relatives) {
72  std::vector<GenParticle const *> rtn;
74  // Before HepMC 2.7.0 there were no GV::particles_const_iterators and constness consistency was all screwed up :-/
75  #if HEPMC_VERSION_CODE >= 2007000
76  // for (GenVertex::particle_iterator pi = gv->particles_const_begin(range); pi != gv->particles_const_end(range); ++pi)
77  for (GenVertex::particle_iterator pi = gv->particles_begin(range); pi != gv->particles_end(range); ++pi)
78  rtn.push_back(*pi);
79  #else
80  GenVertex* gv2 = const_cast<GenVertex*>(gv);
81  for (GenVertex::particle_iterator pi = gv2->particles_begin(range); pi != gv2->particles_end(range); ++pi)
82  rtn.push_back(const_cast<const GenParticle*>(*pi));
83  #endif
84  return rtn;
85  }
86 
87  inline std::vector<GenParticle*> particles(GenVertex* gv, HepMC::IteratorRange range=HepMC::relatives) {
88  std::vector<GenParticle*> rtn;
89  for (GenVertex::particle_iterator pi = gv->particles_begin(range); pi != gv->particles_end(range); ++pi)
90  rtn.push_back(*pi);
91  return rtn;
92  }
93 
94 
95 
96  // Get iterator ranges as wrapped begin/end pairs
99  typedef vector<GenParticle*>::const_iterator genvertex_particles_const_iterator;
100  GenVertexIterRangeC(const genvertex_particles_const_iterator& begin, const genvertex_particles_const_iterator& end)
101  : _begin(begin), _end(end) { }
102  const genvertex_particles_const_iterator& begin() { return _begin; }
103  const genvertex_particles_const_iterator& end() { return _end; }
104  private:
105  const genvertex_particles_const_iterator _begin, _end;
106  };
107 
108  inline GenVertexIterRangeC particles_in(const GenVertex* gv) {
109  return GenVertexIterRangeC(gv->particles_in_const_begin(), gv->particles_in_const_end());
110  }
111 
112  inline GenVertexIterRangeC particles_out(const GenVertex* gv) {
113  return GenVertexIterRangeC(gv->particles_out_const_begin(), gv->particles_out_const_end());
114  }
115 
116 
117 
118  #if HEPMC_VERSION_CODE >= 2007000
119 
120  // Get iterator ranges as wrapped begin/end pairs
122  struct GenVertexIterRange {
123  typedef vector<GenParticle*>::iterator genvertex_particles_iterator;
124  GenVertexIterRange(const genvertex_particles_iterator& begin, const genvertex_particles_iterator& end)
125  : _begin(begin), _end(end) { }
126  const genvertex_particles_iterator& begin() { return _begin; }
127  const genvertex_particles_iterator& end() { return _end; }
128  private:
129  const genvertex_particles_iterator _begin, _end;
130  };
131 
132  inline GenVertexIterRange particles_in(GenVertex* gv) {
133  return GenVertexIterRange(gv->particles_in_begin(), gv->particles_in_end());
134  }
135 
136  inline GenVertexIterRange particles_out(GenVertex* gv) {
137  return GenVertexIterRange(gv->particles_out_begin(), gv->particles_out_end());
138  }
139 
140  #endif
141 
142 
144 
145 
147  inline std::vector<const GenParticle*> particles_in(const GenParticle* gp, HepMC::IteratorRange range=HepMC::ancestors) {
148  if (range != HepMC::parents && range != HepMC::ancestors)
149  throw UserError("Requested particles_in(GenParticle*) with a non-'in' iterator range");
150  if (!gp->production_vertex()) return std::vector<const GenParticle*>();
151  #if HEPMC_VERSION_CODE >= 2007000
152  return particles(gp->production_vertex(), range);
153  #else
154  // Before HepMC 2.7.0 the constness consistency of methods and their return types was all screwed up :-/
155  std::vector<const GenParticle*> rtn;
156  foreach (GenParticle* gp2, particles(gp->production_vertex(), range))
157  rtn.push_back( const_cast<const GenParticle*>(gp2) );
158  return rtn;
159  #endif
160  }
161 
163  inline std::vector<GenParticle*> particles_in(GenParticle* gp, HepMC::IteratorRange range=HepMC::ancestors) {
164  if (range != HepMC::parents && range != HepMC::ancestors)
165  throw UserError("Requested particles_in(GenParticle*) with a non-'in' iterator range");
166  return (gp->production_vertex()) ? particles(gp->production_vertex(), range) : std::vector<GenParticle*>();
167  }
168 
169 
171  inline std::vector<const GenParticle*> particles_out(const GenParticle* gp, HepMC::IteratorRange range=HepMC::descendants) {
172  if (range != HepMC::children && range != HepMC::descendants)
173  throw UserError("Requested particles_out(GenParticle*) with a non-'out' iterator range");
174  if (!gp->end_vertex()) return std::vector<const GenParticle*>();
175  #if HEPMC_VERSION_CODE >= 2007000
176  return particles(gp->end_vertex(), range);
177  #else
178  // Before HepMC 2.7.0 the constness consistency of methods and their return types was all screwed up :-/
179  std::vector<const GenParticle*> rtn;
180  foreach (GenParticle* gp2, particles(gp->end_vertex(), range))
181  rtn.push_back( const_cast<const GenParticle*>(gp2) );
182  return rtn;
183  #endif
184  }
185 
187  inline std::vector<GenParticle*> particles_out(GenParticle* gp, HepMC::IteratorRange range=HepMC::descendants) {
188  if (range != HepMC::children && range != HepMC::descendants)
189  throw UserError("Requested particles_out(GenParticle*) with a non-'out' iterator range");
190  return (gp->end_vertex()) ? particles(gp->end_vertex(), range) : std::vector<GenParticle*>();
191  }
192 
193 
195  inline std::vector<const GenParticle*> particles(const GenParticle* gp, HepMC::IteratorRange range=HepMC::ancestors) {
196  if (range == HepMC::parents || range == HepMC::ancestors)
197  return particles_in(gp, range);
198  if (range == HepMC::children || range == HepMC::descendants)
199  return particles_in(gp, range);
200  throw UserError("Requested particles(GenParticle*) with an unsupported iterator range");
201  }
202 
203 
204 }
205 
206 #endif
Definition: ALICE_2010_I880049.cc:13
Error specialisation for where the problem is between the chair and the computer. ...
Definition: Exceptions.hh:55
std::vector< GenParticle const * > particles(const GenEvent *ge)
Definition: RivetHepMC.hh:36
Definition: RivetHepMC.hh:98