C++InterfacetoTauola
TauolaHepMC3Particle.cxx
1 #include "TauolaHepMC3Particle.h"
2 #include "Log.h"
3 
4 #include "HepMC3/GenVertex.h"
5 #include "HepMC3/Print.h"
6 
7 
8 namespace Tauolapp
9 {
10 
12  m_particle = make_shared<GenParticle>();
13 }
14 
15 TauolaHepMC3Particle::~TauolaHepMC3Particle(){
16 
17  //delete the mother and daughter pointers
18  while(m_mothers.size()!=0){
19  TauolaParticle * temp = m_mothers.back();
20  m_mothers.pop_back();
21  delete temp;
22  }
23  while(m_daughters.size()!=0){
24  TauolaParticle * temp = m_daughters.back();
25  m_daughters.pop_back();
26  delete temp;
27  }
28 
29  while(m_created_particles.size()!=0){
31  m_created_particles.pop_back();
32  //if(temp->getHepMC3()->id()==0) delete temp->getHepMC3();
33  delete temp;
34  }
35 
36 }
37 
38 // NOTE: Not executed by release examples
39 TauolaHepMC3Particle::TauolaHepMC3Particle(int pdg_id, int status, double mass){
40  m_particle = make_shared<GenParticle>();
41  m_particle->set_pid(pdg_id);
42  m_particle->set_status(status);
43  m_particle->set_generated_mass(mass);
44 }
45 
47  m_particle = particle;
48 }
49 
51  return m_particle;
52 }
53 
55 /* NO DELETING YET
56  std::vector<TauolaParticle*> daughters = getDaughters();
57  std::vector<TauolaParticle*>::iterator dIter = daughters.begin();
58 
59  for(; dIter != daughters.end(); dIter++)
60  (*dIter)->undecay();
61 
62  if(m_particle->end_vertex())
63  {
64  while(m_particle->end_vertex()->particles_out().size())
65  {
66  GenParticlePtr p = m_particle->end_vertex()->remove_particle(*(m_particle->end_vertex()->particles_out_const_begin()));
67  delete p;
68  }
69  delete m_particle->end_vertex();
70  }
71 
72  m_daughters.clear();
73  m_particle->set_status(TauolaParticle::STABLE);
74 
75  for(unsigned int i=0;i<daughters.size();i++)
76  delete daughters[i];
77 */
78 }
79 
80 void TauolaHepMC3Particle::setMothers(vector<TauolaParticle*> mothers){
81 
82  /******** Deal with mothers ***********/
83 
84  //If there are mothers
85  if(mothers.size()>0){
86 
87  GenParticlePtr part;
88  part=dynamic_cast<TauolaHepMC3Particle*>(mothers.at(0))->getHepMC3();
89 
90  //Use end vertex of first mother as production vertex for particle
91  GenVertexPtr production_vertex = part->end_vertex();
92  GenVertexPtr orig_production_vertex = production_vertex;
93 
94  //If production_vertex does not exist - create it
95  //If it's tau decay - set the time and position including the tau lifetime correction
96  //otherwise - copy the time and position of decaying particle
97  if(!production_vertex){
98  production_vertex = make_shared<GenVertex>();
99  FourVector point = part->production_vertex()->position();
100  production_vertex->set_position(point);
101  part->parent_event()->add_vertex(production_vertex);
102  }
103 
104  //Loop over all mothers to check that the end points to the right place
105  vector<TauolaParticle*>::iterator mother_itr;
106  for(mother_itr = mothers.begin(); mother_itr != mothers.end();
107  mother_itr++){
108 
109  GenParticlePtr moth;
110  moth = dynamic_cast<TauolaHepMC3Particle*>(*mother_itr)->getHepMC3();
111 
112  if(moth->end_vertex()!=orig_production_vertex)
113  Log::Fatal("Mother production_vertices point to difference places. Can not override. Please delete vertices first.",1);
114  else
115  production_vertex->add_particle_in(moth);
116 
117  //update status info
118  if(moth->status()==TauolaParticle::STABLE)
119  // moth->set_status(TauolaParticle::DECAYED);
120  moth->set_status(2);
121  }
122  production_vertex->add_particle_out(m_particle);
123  }
124 }
125 
126 void TauolaHepMC3Particle::setDaughters(vector<TauolaParticle*> daughters){
127 
128  if(!m_particle->parent_event())
129  Log::Fatal("New particle needs the event set before it's daughters can be added",2);
130 
131  //If there are daughters
132  if(daughters.size()>0){
133  // NOTE: Not executed by release examples
134  // because daughters.size() is always 0
135 
136  //Use production vertex of first daughter as end vertex for particle
137  GenParticlePtr first_daughter;
138  first_daughter = (dynamic_cast<TauolaHepMC3Particle*>(daughters.at(0)))->getHepMC3();
139 
140  GenVertexPtr end_vertex;
141  end_vertex=first_daughter->production_vertex();
142  GenVertexPtr orig_end_vertex = end_vertex;
143 
144  if(!end_vertex){ //if it does not exist create it
145  end_vertex = make_shared<GenVertex>();
146  m_particle->parent_event()->add_vertex(end_vertex);
147  }
148 
149  //Loop over all daughters to check that the end points to the right place
150  vector<TauolaParticle*>::iterator daughter_itr;
151  for(daughter_itr = daughters.begin(); daughter_itr != daughters.end();
152  daughter_itr++){
153 
154  GenParticlePtr daug;
155  daug = dynamic_cast<TauolaHepMC3Particle*>(*daughter_itr)->getHepMC3();
156 
157 
158  if(daug->production_vertex()!=orig_end_vertex)
159  Log::Fatal("Daughter production_vertices point to difference places. Can not override. Please delete vertices first.",3);
160  else
161  end_vertex->add_particle_out(daug);
162  }
163  end_vertex->add_particle_in(m_particle);
164  }
165 }
166 
167 std::vector<TauolaParticle*> TauolaHepMC3Particle::getMothers(){
168 
169  if(m_mothers.size()==0&&m_particle->production_vertex()){
170  for(auto p: m_particle->production_vertex()->particles_in() ) {
171  m_mothers.push_back(new TauolaHepMC3Particle(p));
172  }
173  }
174  return m_mothers;
175 }
176 
177 std::vector<TauolaParticle*> TauolaHepMC3Particle::getDaughters(){
178 
179  if(m_daughters.size()==0&&m_particle->end_vertex()){
180  for(auto p: m_particle->end_vertex()->particles_out() ) {
181  m_daughters.push_back(new TauolaHepMC3Particle(p));
182  }
183  }
184  return m_daughters;
185 }
186 
188 
189  if(!m_particle->end_vertex()) return;
190 
191  // HepMC version of check_momentum_conservation
192  // with added energy check
193 
194  FourVector sum;
195  for(ConstGenParticlePtr p: m_particle->end_vertex()->particles_in() ) {
196  sum += p->momentum();
197  }
198 
199  for(ConstGenParticlePtr p: m_particle->end_vertex()->particles_out() ) {
200  sum -= p->momentum();
201  }
202 
203  if( sum.length() > Tauola::momentum_conservation_threshold ) {
204  Log::Warning()<<"Momentum not conserved in the vertex:"<<endl;
205  Log::RedirectOutput(Log::Warning(false));
206  Print::line(m_particle->end_vertex());
208  return;
209  }
210 
211  return;
212 }
213 
214 // NOTE: Not executed by release examples
216  m_particle->set_pid(pdg_id);
217 }
218 
220  m_particle->set_generated_mass(mass);
221 }
222 
223 // NOTE: Not executed by release examples
225  m_particle->set_status(status);
226 }
227 
229  return m_particle->pid();
230 }
231 
233  return m_particle->status();
234 }
235 
237  return m_particle->pid();
238 }
239 
240 // Set (X,T) Position of tau decay trees
242 
243  double lifetime = Tauola::tau_lifetime * (-log( Tauola::randomDouble() ));
244  FourVector tau_momentum = m_particle->momentum();
245 
246  double mass = sqrt(abs( tau_momentum.e()*tau_momentum.e()
247  - tau_momentum.px()*tau_momentum.px()
248  - tau_momentum.py()*tau_momentum.py()
249  - tau_momentum.pz()*tau_momentum.pz()
250  ) );
251 
252  // Get previous position
253  FourVector previous_position = m_particle->production_vertex()->position();
254 
255  // Calculate new position
256  FourVector new_position(previous_position.x()+tau_momentum.px()/mass*lifetime,
257  previous_position.y()+tau_momentum.py()/mass*lifetime,
258  previous_position.z()+tau_momentum.pz()/mass*lifetime,
259  previous_position.t()+tau_momentum.e() /mass*lifetime);
260 
261  // Set new position
262  m_particle->end_vertex()->set_position(new_position);
263  recursiveSetPosition(m_particle,new_position);
264 }
265 
266 void TauolaHepMC3Particle::recursiveSetPosition(GenParticlePtr p, FourVector pos){
267 
268  if(!p->end_vertex()) return;
269 
270  // Iterate over all outgoing particles
271  for(auto pp: p->end_vertex()->particles_out() ) {
272  if( !pp->end_vertex() ) continue;
273 
274  // Set position
275  /// @bug Position cannot be set (for now)
276  //pp->end_vertex()->set_position(pos);
277  recursiveSetPosition(pp,pos);
278  }
279 }
280 
282  int pdg_id, int status, double mass,
283  double px, double py, double pz, double e){
284 
285  TauolaHepMC3Particle * new_particle = new TauolaHepMC3Particle();
286  new_particle->getHepMC3()->set_pid(pdg_id);
287  new_particle->getHepMC3()->set_status(status);
288  new_particle->getHepMC3()->set_generated_mass(mass);
289 
290  FourVector momentum(px,py,pz,e);
291  new_particle->getHepMC3()->set_momentum(momentum);
292 
293  m_created_particles.push_back(new_particle);
294 
295  return new_particle;
296 }
297 
299  Print::line(m_particle);
300 }
301 
302 
303 /******** Getter and Setter methods: ***********************/
304 
306  return m_particle->momentum().px();
307 }
308 
310  return m_particle->momentum().py();
311 }
312 
314  return m_particle->momentum().pz();
315 }
316 
318  return m_particle->momentum().e();
319 }
320 
322  //make new momentum as something is wrong with
323  //the HepMC momentum setters
324 
325  FourVector momentum(m_particle->momentum());
326  momentum.setPx(px);
327  m_particle->set_momentum(momentum);
328 }
329 
331  FourVector momentum(m_particle->momentum());
332  momentum.setPy(py);
333  m_particle->set_momentum(momentum);
334 }
335 
336 
338  FourVector momentum(m_particle->momentum());
339  momentum.setPz(pz);
340  m_particle->set_momentum(momentum);
341 }
342 
344  FourVector momentum(m_particle->momentum());
345  momentum.setE(e);
346  m_particle->set_momentum(momentum);
347 }
348 
349 } // namespace Tauolapp
void setDaughters(std::vector< TauolaParticle * > daughters)
static void RedirectOutput(void(*func)(), ostream &where=*out)
Definition: Log.cxx:93
void setMothers(std::vector< TauolaParticle * > mothers)
Abstract base class for particle in the event. This class also handles boosting.
void recursiveSetPosition(GenParticlePtr p, FourVector pos)
Interface to GenParticle objects.
TauolaHepMC3Particle * createNewParticle(int pdg_id, int status, double mass, double px, double py, double pz, double e)
std::vector< TauolaParticle * > getDaughters()
std::vector< TauolaParticle * > m_created_particles
static void RevertOutput()
std::vector< TauolaParticle * > getMothers()
static void Fatal(string text, unsigned short int code=0)