TauolaHepMCParticle.cxx
00001 #include "TauolaHepMCParticle.h"
00002 #include "Log.h"
00003
00004 namespace Tauolapp
00005 {
00006
00007 TauolaHepMCParticle::TauolaHepMCParticle(){
00008 m_particle = new HepMC::GenParticle();
00009 }
00010
00011 TauolaHepMCParticle::~TauolaHepMCParticle(){
00012
00013
00014 while(m_mothers.size()!=0){
00015 TauolaParticle * temp = m_mothers.back();
00016 m_mothers.pop_back();
00017 delete temp;
00018 }
00019 while(m_daughters.size()!=0){
00020 TauolaParticle * temp = m_daughters.back();
00021 m_daughters.pop_back();
00022 delete temp;
00023 }
00024
00025 while(m_created_particles.size()!=0){
00026 TauolaHepMCParticle * temp = (TauolaHepMCParticle*) m_created_particles.back();
00027 m_created_particles.pop_back();
00028 if(temp->getHepMC()->barcode()==0) delete temp->getHepMC();
00029 delete temp;
00030 }
00031
00032 }
00033
00034
00035 TauolaHepMCParticle::TauolaHepMCParticle(int pdg_id, int status, double mass){
00036 m_particle = new HepMC::GenParticle();
00037 m_particle->set_pdg_id(pdg_id);
00038 m_particle->set_status(status);
00039 m_particle->set_generated_mass(mass);
00040 }
00041
00042 TauolaHepMCParticle::TauolaHepMCParticle(HepMC::GenParticle * particle){
00043 m_particle = particle;
00044 }
00045
00046 HepMC::GenParticle * TauolaHepMCParticle::getHepMC(){
00047 return m_particle;
00048 }
00049
00050 void TauolaHepMCParticle::undecay(){
00051 std::vector<TauolaParticle*> daughters = getDaughters();
00052 std::vector<TauolaParticle*>::iterator dIter = daughters.begin();
00053
00054 for(; dIter != daughters.end(); dIter++)
00055 (*dIter)->undecay();
00056
00057 if(m_particle->end_vertex())
00058 {
00059 while(m_particle->end_vertex()->particles_out_size())
00060 {
00061 HepMC::GenParticle *p = m_particle->end_vertex()->remove_particle(*(m_particle->end_vertex()->particles_out_const_begin()));
00062 delete p;
00063 }
00064 delete m_particle->end_vertex();
00065 }
00066
00067 m_daughters.clear();
00068 m_particle->set_status(TauolaParticle::STABLE);
00069
00070 for(unsigned int i=0;i<daughters.size();i++)
00071 delete daughters[i];
00072 }
00073
00074 void TauolaHepMCParticle::setMothers(vector<TauolaParticle*> mothers){
00075
00076
00077
00078
00079 if(mothers.size()>0){
00080
00081 HepMC::GenParticle * part;
00082 part=dynamic_cast<TauolaHepMCParticle*>(mothers.at(0))->getHepMC();
00083
00084
00085 HepMC::GenVertex * production_vertex = part->end_vertex();
00086 HepMC::GenVertex * orig_production_vertex = production_vertex;
00087
00088
00089
00090
00091 if(!production_vertex){
00092 production_vertex = new HepMC::GenVertex();
00093 HepMC::FourVector point = part->production_vertex()->position();
00094 part->parent_event()->add_vertex(production_vertex);
00095 }
00096
00097
00098 vector<TauolaParticle*>::iterator mother_itr;
00099 for(mother_itr = mothers.begin(); mother_itr != mothers.end();
00100 mother_itr++){
00101
00102 HepMC::GenParticle * moth;
00103 moth = dynamic_cast<TauolaHepMCParticle*>(*mother_itr)->getHepMC();
00104
00105 if(moth->end_vertex()!=orig_production_vertex)
00106 Log::Fatal("Mother production_vertices point to difference places. Can not override. Please delete vertices first.",1);
00107 else
00108 production_vertex->add_particle_in(moth);
00109
00110
00111 if(moth->status()==TauolaParticle::STABLE)
00112 moth->set_status(TauolaParticle::DECAYED);
00113 }
00114 production_vertex->add_particle_out(m_particle);
00115 }
00116 }
00117
00118 void TauolaHepMCParticle::setDaughters(vector<TauolaParticle*> daughters){
00119
00120 if(!m_particle->parent_event())
00121 Log::Fatal("New particle needs the event set before it's daughters can be added",2);
00122
00123
00124 if(daughters.size()>0){
00125
00126
00127
00128
00129 HepMC::GenParticle * first_daughter;
00130 first_daughter = (dynamic_cast<TauolaHepMCParticle*>(daughters.at(0)))->getHepMC();
00131
00132 HepMC::GenVertex * end_vertex;
00133 end_vertex=first_daughter->production_vertex();
00134 HepMC::GenVertex * orig_end_vertex = end_vertex;
00135
00136 if(!end_vertex){
00137 end_vertex = new HepMC::GenVertex();
00138 m_particle->parent_event()->add_vertex(end_vertex);
00139 }
00140
00141
00142 vector<TauolaParticle*>::iterator daughter_itr;
00143 for(daughter_itr = daughters.begin(); daughter_itr != daughters.end();
00144 daughter_itr++){
00145
00146 HepMC::GenParticle * daug;
00147 daug = dynamic_cast<TauolaHepMCParticle*>(*daughter_itr)->getHepMC();
00148
00149
00150 if(daug->production_vertex()!=orig_end_vertex)
00151 Log::Fatal("Daughter production_vertices point to difference places. Can not override. Please delete vertices first.",3);
00152 else
00153 end_vertex->add_particle_out(daug);
00154 }
00155 end_vertex->add_particle_in(m_particle);
00156 }
00157 }
00158
00159 std::vector<TauolaParticle*> TauolaHepMCParticle::getMothers(){
00160
00161 if(m_mothers.size()==0&&m_particle->production_vertex()){
00162 HepMC::GenVertex::particles_in_const_iterator pcle_itr;
00163 pcle_itr=m_particle->production_vertex()->particles_in_const_begin();
00164
00165 HepMC::GenVertex::particles_in_const_iterator pcle_itr_end;
00166 pcle_itr_end=m_particle->production_vertex()->particles_in_const_end();
00167
00168 for(;pcle_itr != pcle_itr_end; pcle_itr++){
00169 m_mothers.push_back(new TauolaHepMCParticle(*pcle_itr));
00170 }
00171 }
00172 return m_mothers;
00173 }
00174
00175 std::vector<TauolaParticle*> TauolaHepMCParticle::getDaughters(){
00176
00177 if(m_daughters.size()==0&&m_particle->end_vertex()){
00178 HepMC::GenVertex::particles_out_const_iterator pcle_itr;
00179 pcle_itr=m_particle->end_vertex()->particles_out_const_begin();
00180
00181 HepMC::GenVertex::particles_out_const_iterator pcle_itr_end;
00182 pcle_itr_end=m_particle->end_vertex()->particles_out_const_end();
00183
00184 for(;pcle_itr != pcle_itr_end; pcle_itr++){
00185 m_daughters.push_back(new TauolaHepMCParticle(*pcle_itr));
00186 }
00187 }
00188 return m_daughters;
00189 }
00190
00191 void TauolaHepMCParticle::checkMomentumConservation(){
00192
00193 if(!m_particle->end_vertex()) return;
00194
00195
00196
00197
00198 double sumpx = 0, sumpy = 0, sumpz = 0, sume = 0;
00199 for( HepMC::GenVertex::particles_in_const_iterator part1 = m_particle->end_vertex()->particles_in_const_begin();
00200 part1 != m_particle->end_vertex()->particles_in_const_end(); part1++ ){
00201
00202 sumpx += (*part1)->momentum().px();
00203 sumpy += (*part1)->momentum().py();
00204 sumpz += (*part1)->momentum().pz();
00205 sume += (*part1)->momentum().e();
00206 }
00207
00208 for( HepMC::GenVertex::particles_out_const_iterator part2 = m_particle->end_vertex()->particles_out_const_begin();
00209 part2 != m_particle->end_vertex()->particles_out_const_end(); part2++ ){
00210
00211 sumpx -= (*part2)->momentum().px();
00212 sumpy -= (*part2)->momentum().py();
00213 sumpz -= (*part2)->momentum().pz();
00214 sume -= (*part2)->momentum().e();
00215 }
00216
00217 if( sqrt( sumpx*sumpx + sumpy*sumpy + sumpz*sumpz + sume*sume) > Tauola::momentum_conservation_threshold ) {
00218 Log::Warning()<<"Momentum not conserved in the vertex:"<<endl;
00219 Log::RedirectOutput(Log::Warning(false));
00220 m_particle->end_vertex()->print();
00221 Log::RevertOutput();
00222 return;
00223 }
00224
00225 return;
00226 }
00227
00228
00229 void TauolaHepMCParticle::setPdgID(int pdg_id){
00230 m_particle->set_pdg_id(pdg_id);
00231 }
00232
00233 void TauolaHepMCParticle::setMass(double mass){
00234 m_particle->set_generated_mass(mass);
00235 }
00236
00237
00238 void TauolaHepMCParticle::setStatus(int status){
00239 m_particle->set_status(status);
00240 }
00241
00242 int TauolaHepMCParticle::getPdgID(){
00243 return m_particle->pdg_id();
00244 }
00245
00246 int TauolaHepMCParticle::getStatus(){
00247 return m_particle->status();
00248 }
00249
00250 int TauolaHepMCParticle::getBarcode(){
00251 return m_particle->barcode();
00252 }
00253
00254
00255 void TauolaHepMCParticle::decayEndgame(){
00256
00257 double lifetime = Tauola::tau_lifetime * (-log( Tauola::randomDouble() ));
00258 HepMC::FourVector tau_momentum = m_particle->momentum();
00259
00260 double mass = sqrt(abs( tau_momentum.e()*tau_momentum.e()
00261 - tau_momentum.px()*tau_momentum.px()
00262 - tau_momentum.py()*tau_momentum.py()
00263 - tau_momentum.pz()*tau_momentum.pz()
00264 ) );
00265
00266
00267 HepMC::FourVector previous_position = m_particle->production_vertex()->position();
00268
00269
00270 HepMC::FourVector new_position(previous_position.x()+tau_momentum.px()/mass*lifetime,
00271 previous_position.y()+tau_momentum.py()/mass*lifetime,
00272 previous_position.z()+tau_momentum.pz()/mass*lifetime,
00273 previous_position.t()+tau_momentum.e() /mass*lifetime);
00274
00275
00276 m_particle->end_vertex()->set_position(new_position);
00277 recursiveSetPosition(m_particle,new_position);
00278 }
00279
00280 void TauolaHepMCParticle::recursiveSetPosition(HepMC::GenParticle *p, HepMC::FourVector pos){
00281
00282 if(!p->end_vertex()) return;
00283
00284
00285 for(HepMC::GenVertex::particles_out_const_iterator pp = p->end_vertex()->particles_out_const_begin();
00286 pp != p->end_vertex()->particles_out_const_end();
00287 ++pp){
00288 if( !(*pp)->end_vertex() ) continue;
00289
00290
00291 (*pp)->end_vertex()->set_position(pos);
00292 recursiveSetPosition(*pp,pos);
00293 }
00294 }
00295
00296 TauolaHepMCParticle * TauolaHepMCParticle::createNewParticle(
00297 int pdg_id, int status, double mass,
00298 double px, double py, double pz, double e){
00299
00300 TauolaHepMCParticle * new_particle = new TauolaHepMCParticle();
00301 new_particle->getHepMC()->set_pdg_id(pdg_id);
00302 new_particle->getHepMC()->set_status(status);
00303 new_particle->getHepMC()->set_generated_mass(mass);
00304
00305 HepMC::FourVector momentum(px,py,pz,e);
00306 new_particle->getHepMC()->set_momentum(momentum);
00307
00308 m_created_particles.push_back(new_particle);
00309
00310 return new_particle;
00311 }
00312
00313 void TauolaHepMCParticle::print(){
00314 m_particle->print();
00315 }
00316
00317
00318
00319
00320 inline double TauolaHepMCParticle::getPx(){
00321 return m_particle->momentum().px();
00322 }
00323
00324 inline double TauolaHepMCParticle::getPy(){
00325 return m_particle->momentum().py();
00326 }
00327
00328 double TauolaHepMCParticle::getPz(){
00329 return m_particle->momentum().pz();
00330 }
00331
00332 double TauolaHepMCParticle::getE(){
00333 return m_particle->momentum().e();
00334 }
00335
00336 void TauolaHepMCParticle::setPx(double px){
00337
00338
00339
00340 HepMC::FourVector momentum(m_particle->momentum());
00341 momentum.setPx(px);
00342 m_particle->set_momentum(momentum);
00343 }
00344
00345 void TauolaHepMCParticle::setPy(double py){
00346 HepMC::FourVector momentum(m_particle->momentum());
00347 momentum.setPy(py);
00348 m_particle->set_momentum(momentum);
00349 }
00350
00351
00352 void TauolaHepMCParticle::setPz(double pz){
00353 HepMC::FourVector momentum(m_particle->momentum());
00354 momentum.setPz(pz);
00355 m_particle->set_momentum(momentum);
00356 }
00357
00358 void TauolaHepMCParticle::setE(double e){
00359 HepMC::FourVector momentum(m_particle->momentum());
00360 momentum.setE(e);
00361 m_particle->set_momentum(momentum);
00362 }
00363
00364 }