CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/SimGeneral/MixingModule/plugins/MixEvtVtxGenerator.cc

Go to the documentation of this file.
00001 #ifndef HI_MixEvtVtxGenerator_H
00002 #define HI_MixEvtVtxGenerator_H
00003 /*
00004 *   $Date: 2010/11/22 12:41:58 $
00005 *   $Revision: 1.5 $
00006 */
00007 #include "FWCore/PluginManager/interface/ModuleDef.h"
00008 #include "FWCore/Framework/interface/MakerMacros.h"
00009 
00010 #include "FWCore/Framework/interface/EDProducer.h"
00011 #include "FWCore/Utilities/interface/InputTag.h"
00012 #include "FWCore/Framework/interface/Event.h"
00013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00014 #include "FWCore/ServiceRegistry/interface/Service.h"
00015 #include "FWCore/Utilities/interface/Exception.h"
00016 #include "FWCore/Utilities/interface/EDMException.h"
00017 
00018 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00019 #include "DataFormats/Provenance/interface/Provenance.h"
00020 
00021 #include "DataFormats/VertexReco/interface/Vertex.h"
00022 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00023 
00024 #include "TMatrixD.h"
00025 
00026 using namespace edm;
00027 using namespace std;
00028 
00029 
00030 namespace HepMC {
00031    class FourVector ;
00032 }
00033 
00034 
00035 class MixEvtVtxGenerator : public edm::EDProducer
00036 {
00037    public:
00038       
00039    // ctor & dtor
00040    explicit MixEvtVtxGenerator( const edm::ParameterSet& );
00041    virtual ~MixEvtVtxGenerator();
00042       
00043    virtual void produce( edm::Event&, const edm::EventSetup& );
00044       
00045    virtual HepMC::FourVector* getVertex(edm::Event&);
00046    virtual HepMC::FourVector* getRecVertex(edm::Event&);
00047    
00048    protected:
00049 
00050    HepMC::FourVector*       fVertex ;
00051    TMatrixD *boost_;
00052    
00053    private :
00054 
00055    edm::InputTag            signalLabel;
00056    edm::InputTag            hiLabel;
00057    bool                     useRecVertex;
00058    std::vector<double>      vtxOffset;
00059 
00060 };
00061 
00062 MixEvtVtxGenerator::MixEvtVtxGenerator( const ParameterSet& pset ) 
00063         : fVertex(0), boost_(0),
00064           signalLabel(pset.getParameter<edm::InputTag>("signalLabel")),
00065           hiLabel(pset.getParameter<edm::InputTag>("heavyIonLabel")),
00066           useRecVertex(pset.exists("useRecVertex")?pset.getParameter<bool>("useRecVertex"):false)
00067           
00068 {   
00069    produces<bool>("matchedVertex"); 
00070    vtxOffset.resize(3);
00071    if(pset.exists("vtxOffset")) vtxOffset=pset.getParameter< std::vector<double> >("vtxOffset");
00072 }
00073 
00074 MixEvtVtxGenerator::~MixEvtVtxGenerator() 
00075 {
00076    delete fVertex ;
00077    if (boost_ != 0 ) delete boost_;
00078    // no need since now it's done in HepMCProduct
00079    // delete fEvt ;
00080 }
00081 
00082 HepMC::FourVector* MixEvtVtxGenerator::getVertex( Event& evt){
00083 
00084   Handle<HepMCProduct> input;
00085   evt.getByLabel(hiLabel,input);
00086 
00087   const HepMC::GenEvent* inev = input->GetEvent();
00088   HepMC::GenVertex* genvtx = inev->signal_process_vertex();
00089   if(!genvtx){
00090     cout<<"No Signal Process Vertex!"<<endl;
00091     HepMC::GenEvent::particle_const_iterator pt=inev->particles_begin();
00092     HepMC::GenEvent::particle_const_iterator ptend=inev->particles_end();
00093     while(!genvtx || ( genvtx->particles_in_size() == 1 && pt != ptend ) ){
00094       if(!genvtx) cout<<"No Gen Vertex!"<<endl;
00095       if(pt == ptend) cout<<"End reached!"<<endl;
00096       genvtx = (*pt)->production_vertex();
00097       ++pt;
00098     }
00099   }
00100   double aX,aY,aZ,aT;
00101 
00102   aX = genvtx->position().x();
00103   aY = genvtx->position().y();
00104   aZ = genvtx->position().z();
00105   aT = genvtx->position().t();
00106   
00107   if(!fVertex) fVertex = new HepMC::FourVector();
00108   fVertex->set(aX,aY,aZ,aT);
00109   
00110   return fVertex;
00111 
00112 }
00113 
00114 
00115 HepMC::FourVector* MixEvtVtxGenerator::getRecVertex( Event& evt){
00116 
00117   Handle<reco::VertexCollection> input;
00118   evt.getByLabel(hiLabel,input);
00119 
00120   double aX,aY,aZ;
00121 
00122   aX = input->begin()->position().x() + vtxOffset[0];
00123   aY = input->begin()->position().y() + vtxOffset[1];
00124   aZ = input->begin()->position().z() + vtxOffset[2];
00125 
00126   /*
00127   std::cout << "reco::Vertex = " << input->begin()->position().x()
00128             << ", " << input->begin()->position().y()
00129             << ", " << input->begin()->position().z()
00130             << std::endl;
00131 
00132   std::cout << "offset = " << vtxOffset[0]
00133             << ", " << vtxOffset[1]
00134             << ", " << vtxOffset[2]
00135             << std::endl;
00136 
00137   std::cout << "embedded GEN vertex = " << aX
00138             << ", " << aY << ", " << aZ << std::endl;
00139   */
00140   
00141   if(!fVertex) fVertex = new HepMC::FourVector();
00142   fVertex->set(10.0*aX,10.0*aY,10.0*aZ,0.0); // HepMC positions in mm (RECO in cm)
00143   
00144   return fVertex;
00145 
00146 }
00147 
00148 void MixEvtVtxGenerator::produce( Event& evt, const EventSetup& )
00149 {
00150    
00151    
00152    Handle<HepMCProduct> HepMCEvt ;
00153    
00154    evt.getByLabel( signalLabel, HepMCEvt ) ;
00155    
00156    // generate new vertex & apply the shift 
00157    //
00158 
00159    HepMCEvt->applyVtxGen( useRecVertex ? getRecVertex(evt) : getVertex(evt) ) ;
00160 
00161    //   HepMCEvt->boostToLab( GetInvLorentzBoost(), "vertex" );
00162    //   HepMCEvt->boostToLab( GetInvLorentzBoost(), "momentum" );
00163    
00164    // OK, create a (pseudo)product and put in into edm::Event
00165    //
00166    auto_ptr<bool> NewProduct(new bool(true)) ;      
00167    evt.put( NewProduct ,"matchedVertex") ;
00168       
00169    return ;
00170 
00171 }
00172 
00173 DEFINE_FWK_MODULE(MixEvtVtxGenerator);
00174 
00175 #endif