CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HepMCConverter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // HepMCConverter.tcc is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2007 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 2 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 //
10 // This is the implementation of the non-inlined, non-templated member
11 // functions of the HepMCConverter class.
12 //
13 
14 // C. Saout: Copied from ThePEG and slightly modified
15 
16 #include <set>
17 
18 #include <HepMC/GenEvent.h>
19 #include <HepMC/GenVertex.h>
20 #include <HepMC/GenParticle.h>
21 #include <HepMC/Polarization.h>
22 
23 #include <ThePEG/StandardModel/StandardModelBase.h>
24 #include <ThePEG/Repository/EventGenerator.h>
25 #include <ThePEG/EventRecord/Particle.h>
26 #include <ThePEG/EventRecord/StandardSelectors.h>
27 #include <ThePEG/EventRecord/Collision.h>
28 #include <ThePEG/EventRecord/Step.h>
29 #include <ThePEG/EventRecord/SubProcess.h>
30 #include <ThePEG/Handlers/XComb.h>
31 #include <ThePEG/Handlers/EventHandler.h>
32 #include <ThePEG/PDF/PartonExtractor.h>
33 #include <ThePEG/PDF/PDF.h>
34 
36 
37 namespace ThePEG {
38 
39 
40 template <typename HepMCEventT, typename Traits>
41 typename HepMCConverter<HepMCEventT,Traits>::GenEvent *
43 convert(const Event & ev, bool nocopies, Energy eunit, Length lunit) {
44  HepMCConverter<HepMCEventT,Traits> converter(ev, nocopies, eunit, lunit);
45  return converter.geneve;
46 }
47 
48 template <typename HepMCEventT, typename Traits>
50 convert(const Event & ev, GenEvent & gev, bool nocopies) {
52  converter(ev, gev, nocopies,
53  Traits::momentumUnit(gev), Traits::lengthUnit(gev));
54 }
55 
56 template <typename HepMCEventT, typename Traits>
58 convert(const Event & ev, GenEvent & gev, bool nocopies,
59  Energy eunit, Length lunit) {
60  HepMCConverter<HepMCEventT,Traits> converter(ev, gev, nocopies, eunit, lunit);
61 }
62 
63 template <typename HepMCEventT, typename Traits>
65 HepMCConverter(const Event & ev, bool nocopies, Energy eunit, Length lunit)
66  : energyUnit(eunit), lengthUnit(lunit) {
67 
68  geneve = Traits::newEvent(ev.number(), ev.weight(), ev.optionalWeights());
69 
70  init(ev, nocopies);
71 
72 }
73 
74 template <typename HepMCEventT, typename Traits>
76 HepMCConverter(const Event & ev, GenEvent & gev, bool nocopies,
77  Energy eunit, Length lunit)
78  : energyUnit(eunit), lengthUnit(lunit) {
79 
80  geneve = &gev;
81 
82  init(ev, nocopies);
83 
84 }
85 
87  bool operator()(tcPPtr a, tcPPtr b) const {
88  return a->number() < b->number();
89  }
90 };
91 
92 template <typename HepMCEventT, typename Traits>
93 void HepMCConverter<HepMCEventT,Traits>::init(const Event & ev, bool nocopies) {
94 
95  if ( Traits::hasUnits() ) {
96  if ( lengthUnit != millimeter && lengthUnit != centimeter )
97  throw HepMCConverterException()
98  << "Length unit used for HepMC::GenEvent was not MM nor CM."
99  << Exception::runerror;
100  if ( energyUnit != GeV && energyUnit != MeV )
101  throw HepMCConverterException()
102  << "Momentum unit used for HepMC::GenEvent was not GEV nor MEV."
103  << Exception::runerror;
104  } else {
105  if ( lengthUnit != millimeter )
106  throw HepMCConverterException()
107  << "Length unit used for HepMC::GenEvent was not MM and the "
108  << "HepMCTraits class claims that the used version of HepMC "
109  << "cannot handle user-defined units."
110  << Exception::runerror;
111  if ( energyUnit != GeV )
112  throw HepMCConverterException()
113  << "Momentum unit used for HepMC::GenEvent was not GEV and the "
114  << "HepMCTraits class claims that the used version of HepMC "
115  << "cannot handle user-defined units."
116  << Exception::runerror;
117  }
118  Traits::setUnits(*geneve, energyUnit, lengthUnit);
119 
120  if ( ev.primaryCollision() && ( eh =
121  dynamic_ptr_cast<tcEHPtr>(ev.primaryCollision()->handler()) ) ) {
122 
123  // Get general event info if present.
124  }
125 
126  // Extract all particles and order them.
127  tcPVector all;
128  ev.select(back_inserter(all), SelectAll());
129  vertices.reserve(all.size()*2);
130  sortTopologically(all);
131 
132  // Create GenParticle's and map them to the ThePEG particles.
133  for ( int i = 0, N = all.size(); i < N; ++i ) {
134  tcPPtr p = all[i];
135  if ( nocopies && p->next() ) continue;
136  if ( pmap.find(p) != pmap.end() ) continue;
137  GenParticle * gp = pmap[p] = createParticle(p);
138  if ( p->hasColourInfo() ) {
139  // Check if the particle is connected to colour lines, in which
140  // case the lines are mapped to an integer and set in the
141  // GenParticle's Flow info.
142  tcColinePtr l;
143  if ( l = p->colourLine() ) {
144  if ( !member(flowmap, l) ) flowmap[l] = flowmap.size() + 500;
145  Traits::setColourLine(*gp, 1, flowmap[l]);
146  }
147  if ( l = p->antiColourLine() ) {
148  if ( !member(flowmap, l) ) flowmap[l] = flowmap.size() + 500;
149  Traits::setColourLine(*gp, 2, flowmap[l]);
150  }
151  }
152 
153  if ( !p->children().empty() || p->next() ) {
154  // If the particle has children it should have a decay vertex:
155  vertices.push_back(Vertex());
156  decv[p] = &vertices.back();
157  vertices.back().in.insert(p);
158  }
159 
160  if ( !p->parents().empty() || p->previous() ||
161  (p->children().empty() && !p->next()) ) {
162  // If the particle has parents it should have a production
163  // vertex. If neither parents or children it should still have a
164  // dummy production vertex.
165  vertices.push_back(Vertex());
166  prov[p] = &vertices.back();
167  vertices.back().out.insert(p);
168  }
169  }
170 
171  // Now go through the the particles again, and join the vertices.
172  for ( int i = 0, N = all.size(); i < N; ++i ) {
173  tcPPtr p = all[i];
174  if ( nocopies ) {
175  if ( p->next() ) continue;
176  for ( int i = 0, N = p->children().size(); i < N; ++i )
177  join(p, p->children()[i]->final());
178  tcPPtr pp = p;
179  while ( pp->parents().empty() && pp->previous() ) pp = pp->previous();
180  for ( int i = 0, N = pp->parents().size(); i < N; ++i )
181  join(pp->parents()[i]->final(), p);
182  } else {
183  for ( int i = 0, N = p->children().size(); i < N; ++i )
184  join(p, p->children()[i]);
185  if ( p->next() ) join(p, p->next());
186  for ( int i = 0, N = p->parents().size(); i < N; ++i )
187  join(p->parents()[i], p);
188  if ( p->previous() ) join(p->previous(), p);
189  }
190  }
191 
192  // Time to create the GenVertex's
193  for ( typename VertexMap::iterator it = prov.begin(); it != prov.end(); ++it )
194  if ( !member(vmap, it->second) )
195  vmap[it->second] = createVertex(it->second);
196  for ( typename VertexMap::iterator it = decv.begin(); it != decv.end(); ++it )
197  if ( !member(vmap, it->second) )
198  vmap[it->second] = createVertex(it->second);
199 
200  // Add the vertices.
201  for ( typename GenVertexMap::iterator it = vmap.begin();
202  it != vmap.end(); ++it )
203  Traits::addVertex(*geneve, it->second);
204 
205  // Now find the primary signal process vertex defined to be the
206  // decay vertex of the first parton coming into the primary hard
207  // sub-collision.
208  tSubProPtr sub = ev.primarySubProcess();
209  if ( sub && sub->incoming().first ) {
210  const Vertex * prim = decv[sub->incoming().first];
211  Traits::setSignalProcessVertex(*geneve, vmap[prim]);
212  vmap.erase(prim);
213  }
214 
215 
216  // and the incoming beam particles
217  geneve->set_beam_particles(pmap[ev.incoming().first],
218  pmap[ev.incoming().second]);
219 
220  // and the PDF info
221 }
222 
223 template <typename HepMCEventT, typename Traits>
226  int status = 1;
227  if ( !p->children().empty() || p->next() ) {
228  tStepPtr step = p->birthStep();
229  if ((!step || (step && (!step->handler() || step->handler() == eh))) && p->id() != 82)
230  status = 3;
231  else
232  status = 2;
233  }
234  GenParticle * gp =
235  Traits::newParticle(p->momentum(), p->id(), status, energyUnit);
236 
237  if ( p->spinInfo() && p->spinInfo()->hasPolarization() ) {
238  DPair pol = p->spinInfo()->polarization();
239  Traits::setPolarization(*gp, pol.first, pol.second);
240  }
241 
242  return gp;
243 
244 }
245 
246 template <typename HepMCEventT, typename Traits>
248  Vertex * dec = decv[parent];
249  Vertex * pro = prov[child];
250  if ( !pro || !dec ) throw HepMCConverterException()
251  << "Found a reference to a ThePEG::Particle which was not in the Event."
252  << Exception::eventerror;
253  if ( pro == dec ) return;
254  while ( !pro->in.empty() ) {
255  dec->in.insert(*(pro->in.begin()));
256  decv[*(pro->in.begin())] = dec;
257  pro->in.erase(pro->in.begin());
258  }
259  while ( !pro->out.empty() ) {
260  dec->out.insert(*(pro->out.begin()));
261  prov[*(pro->out.begin())] = dec;
262  pro->out.erase(pro->out.begin());
263  }
264 }
265 
266 template <typename HepMCEventT, typename Traits>
269  if ( !v ) throw HepMCConverterException()
270  << "Found internal null Vertex." << Exception::abortnow;
271 
272  GenVertex * gv = new GenVertex();
273 
274  // We assume that the vertex position is the average of the decay
275  // vertices of all incoming and the creation vertices of all
276  // outgoing particles in the lab. Note that this will probably not
277  // be useful information for very small distances.
278  LorentzPoint p;
279  for ( tcParticleSet::iterator it = v->in.begin();
280  it != v->in.end(); ++it ) {
281  p += (**it).labDecayVertex();
282  Traits::addIncoming(*gv, pmap[*it]);
283  }
284  for ( tcParticleSet::iterator it = v->out.begin();
285  it != v->out.end(); ++it ) {
286  p += (**it).labVertex();
287  Traits::addOutgoing(*gv, pmap[*it]);
288  }
289 
290  p /= double(v->in.size() + v->out.size());
291  Traits::setPosition(*gv, p, lengthUnit);
292 
293  return gv;
294 }
295 
296 template <typename HepMCEventT, typename Traits>
298  // ids of the partons going into the primary sub process
299  tSubProPtr sub = e.primarySubProcess();
300  int id1 = sub->incoming().first ->id();
301  int id2 = sub->incoming().second->id();
302  // get the event handler
303  tcEHPtr eh = dynamic_ptr_cast<tcEHPtr>(e.handler());
304  // get the values of x
305  double x1 = eh->lastX1();
306  double x2 = eh->lastX2();
307  // get the pdfs
308  pair<PDF,PDF> pdfs;
309  pdfs.first = eh->pdf<PDF>(sub->incoming().first );
310  pdfs.second = eh->pdf<PDF>(sub->incoming().second);
311  // get the scale
312  Energy2 scale = eh->lastScale();
313  // get the values of the pdfs
314  double xf1 = pdfs.first.xfx(sub->incoming().first->dataPtr(), scale, x1);
315  double xf2 = pdfs.second.xfx(sub->incoming().second->dataPtr(), scale, x2);
316 
317  Traits::setPdfInfo(*geneve, id1, id2, x1, x2, sqrt(scale/GeV2), xf1, xf2);
318 
319 }
320 
321 template<typename HepMCEventT, typename Traits>
323 {
324  // order the particles topologically
325  std::set<tcPPtr> visited;
326  for(unsigned int head = 0; head < all.size(); head++) {
327  bool vetoed = true;
328  for(unsigned int cur = head; cur < all.size(); cur++) {
329  vetoed = false;
330  for(tParticleVector::const_iterator iter =
331  all[cur]->parents().begin();
332  iter != all[cur]->parents().end(); ++iter) {
333  if (visited.find(*iter) == visited.end()) {
334  vetoed = true;
335  break;
336  }
337  }
338  if (!vetoed) {
339  if (cur != head)
340  std::swap(all[head], all[cur]);
341  break;
342  }
343  }
344  visited.insert(all[head]);
345  }
346  visited.clear();
347 }
348 
350 
351 
352 }
353 
354 
bool operator()(tcPPtr a, tcPPtr b) const
int i
Definition: DBlmapReader.cc:9
const double GeV
Definition: MathUtil.h:16
void join(tcPPtr parent, tcPPtr child)
TPRegexp parents
Definition: eve_filter.cc:24
list parent
Definition: dbtoconf.py:74
tuple pp
Definition: createTree.py:15
GenParticle * createParticle(tcPPtr p) const
bool ev
Traits::EventT GenEvent
const double MeV
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
T sqrt(T t)
Definition: SSEVec.h:48
GenVertex * createVertex(Vertex *v)
static GenEvent * convert(const Event &ev, bool nocopies=false, Energy eunit=Traits::defaultEnergyUnit(), Length lunit=Traits::defaultLengthUnit())
void setPdfInfo(const Event &e)
void sortTopologically(tcPVector &allv)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
#define N
Definition: blowfish.cc:9
double b
Definition: hdecay.h:120
void init(const Event &ev, bool nocopies)
double a
Definition: hdecay.h:121
Traits::ParticleT GenParticle
Traits::VertexT GenVertex
tuple status
Definition: ntuplemaker.py:245