CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/SimDataFormats/GeneratorProducts/src/HepMCProduct.cc

Go to the documentation of this file.
00001 /*************************
00002  *
00003  *  Date: 2005/08 $
00004  *  \author J Weng - F. Moortgat'
00005  */
00006 
00007 #include <iostream>
00008 #include <algorithm> // because we use std::swap
00009 
00010 //#include "CLHEP/Vector/ThreeVector.h"
00011 
00012 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00013 
00014 using namespace edm;
00015 using namespace std;
00016 
00017 HepMCProduct::HepMCProduct( HepMC::GenEvent * evt ) {
00018     evt_ = evt;
00019     cout << "Contructing HepMCProduct" << endl;
00020     
00021     //note: this is not quite safe, because GenEvent does NOT
00022     //      know if vertex smearing applied or nor - only the
00023     //      HepMCProduct itself knows !
00024     // isVtxGenApplied_ = 0 ;  // ???
00025 }
00026 
00027 HepMCProduct::~HepMCProduct(){
00028 
00029         delete evt_; evt_ = 0; isVtxGenApplied_ = false;
00030         isVtxBoostApplied_ = false;
00031         isPBoostApplied_ = false;
00032 }
00033 
00034 
00035 void HepMCProduct::addHepMCData( HepMC::GenEvent  *evt){
00036   //  evt->print();
00037   // cout <<sizeof (evt)  <<"  " << sizeof ( HepMC::GenEvent)   << endl;
00038   evt_ = evt;
00039   
00040   // same story about vertex smearing - GenEvent won't know it...
00041   // in fact, would be better to implement CmsGenEvent...
00042   
00043 }
00044 
00045 void HepMCProduct::applyVtxGen( HepMC::FourVector* vtxShift ) const
00046 {
00047         //std::cout<< " applyVtxGen called " << isVtxGenApplied_ << endl;
00048         //fTimeOffset = 0;
00049         
00050    if ( isVtxGenApplied() ) return ;
00051       
00052    for ( HepMC::GenEvent::vertex_iterator vt=evt_->vertices_begin();
00053                                           vt!=evt_->vertices_end(); ++vt )
00054    {
00055             
00056       double x = (*vt)->position().x() + vtxShift->x() ;
00057       double y = (*vt)->position().y() + vtxShift->y() ;
00058       double z = (*vt)->position().z() + vtxShift->z() ;
00059       double t = (*vt)->position().t() + vtxShift->t() ;
00060       //std::cout << " vertex (x,y,z)= " << x <<" " << y << " " << z << std::endl;
00061       (*vt)->set_position( HepMC::FourVector(x,y,z,t) ) ;      
00062    }
00063       
00064    isVtxGenApplied_ = true ;
00065    
00066    return ;
00067 
00068 } 
00069 
00070 void HepMCProduct::boostToLab( TMatrixD* lorentz, std::string type ) const {
00071 
00072         //std::cout << "from boostToLab:" << std::endl;
00073         
00074   
00075   
00076         if ( lorentz == 0 ) {
00077 
00078                 //std::cout << " lorentz = 0 " << std::endl;
00079                 return;
00080         }
00081 
00082         //lorentz->Print();
00083 
00084   TMatrixD tmplorentz(*lorentz);
00085   //tmplorentz.Print();
00086   
00087         if ( type == "vertex") {
00088 
00089                 if ( isVtxBoostApplied() ) {
00090                         //std::cout << " isVtxBoostApplied true " << std::endl;
00091                         return ;
00092                 }
00093                 
00094                 for ( HepMC::GenEvent::vertex_iterator vt=evt_->vertices_begin();
00095                           vt!=evt_->vertices_end(); ++vt ) {
00096 
00097                         // change basis to lorentz boost definition: (t,x,z,y)
00098                         TMatrixD p4(4,1);
00099                         p4(0,0) = (*vt)->position().t();
00100                         p4(1,0) = (*vt)->position().x();
00101                         p4(2,0) = (*vt)->position().z();
00102                         p4(3,0) = (*vt)->position().y();
00103 
00104                         TMatrixD p4lab(4,1);
00105                         p4lab = tmplorentz * p4;
00106                         //std::cout << " vertex lorentz: " << p4lab(1,0) << " " << p4lab(3,0) << " " << p4lab(2,0) << std::endl;
00107                         (*vt)->set_position( HepMC::FourVector(p4lab(1,0),p4lab(3,0),p4lab(2,0), p4lab(0,0) ) ) ;      
00108                 }
00109       
00110                 isVtxBoostApplied_ = true ;
00111         }
00112         else if ( type == "momentum") {
00113                 
00114                 if ( isPBoostApplied() ) {
00115                         //std::cout << " isPBoostApplied true " << std::endl;
00116                         return ;
00117                 }
00118                 
00119                 for ( HepMC::GenEvent::particle_iterator part=evt_->particles_begin();
00120                           part!=evt_->particles_end(); ++part ) {
00121 
00122                         // change basis to lorentz boost definition: (E,Px,Pz,Py)
00123                         TMatrixD p4(4,1);
00124                         p4(0,0) = (*part)->momentum().e();
00125                         p4(1,0) = (*part)->momentum().x();
00126                         p4(2,0) = (*part)->momentum().z();
00127                         p4(3,0) = (*part)->momentum().y();
00128 
00129                         TMatrixD p4lab(4,1);
00130                         p4lab = tmplorentz * p4;
00131                         //std::cout << " momentum lorentz: " << p4lab(1,0) << " " << p4lab(3,0) << " " << p4lab(2,0) << std::endl;
00132                         (*part)->set_momentum( HepMC::FourVector(p4lab(1,0),p4lab(3,0),p4lab(2,0),p4lab(0,0) ) ) ;      
00133                 }
00134       
00135                 isPBoostApplied_ = true ;
00136         }
00137         else {
00138                 std::cout << " no type found for boostToLab(std::string), options are vertex or momentum" << std::endl;
00139         }
00140                 
00141                  
00142         return ;
00143 }
00144 
00145 
00146 const HepMC::GenEvent&  
00147 HepMCProduct::getHepMCData()const   {
00148 
00149   return  * evt_;
00150 }
00151 
00152 // copy constructor
00153 HepMCProduct::HepMCProduct(HepMCProduct const& other) :
00154   evt_(0) {
00155   
00156    if (other.evt_) evt_=new HepMC::GenEvent(*other.evt_);
00157    isVtxGenApplied_ = other.isVtxGenApplied_ ;
00158    isVtxBoostApplied_ = other.isVtxBoostApplied_;
00159    isPBoostApplied_ = other.isPBoostApplied_;
00160    //fTimeOffset = other.fTimeOffset;
00161 }
00162 
00163 // swap
00164 void
00165 HepMCProduct::swap(HepMCProduct& other) {
00166   std::swap(evt_, other.evt_);
00167   std::swap(isVtxGenApplied_, other.isVtxGenApplied_);
00168   std::swap(isVtxBoostApplied_, other.isVtxBoostApplied_);
00169   std::swap(isPBoostApplied_, other.isPBoostApplied_);
00170   //std::swap(fTimeOffset, other.fTimeOffset);
00171 }
00172 
00173 // assignment: use copy/swap idiom for exception safety.
00174 HepMCProduct&
00175 HepMCProduct::operator=(HepMCProduct const& other) {
00176   HepMCProduct temp(other);
00177   swap(temp);
00178   return *this;
00179 }