CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/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: 2013/04/18 21:57:33 $
00005 *   $Revision: 1.7 $
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/CrossingFrame/interface/MixCollection.h"
00019 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00020 #include "DataFormats/Provenance/interface/Provenance.h"
00021 
00022 #include "DataFormats/VertexReco/interface/Vertex.h"
00023 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00024 
00025 #include "TMatrixD.h"
00026 
00027 using namespace edm;
00028 using namespace std;
00029 
00030 
00031 namespace HepMC {
00032    class FourVector ;
00033 }
00034 
00035 
00036 class MixEvtVtxGenerator : public edm::EDProducer
00037 {
00038    public:
00039   
00040   // ctor & dtor
00041   explicit MixEvtVtxGenerator( const edm::ParameterSet& );
00042   virtual ~MixEvtVtxGenerator();
00043   
00044   virtual void produce( edm::Event&, const edm::EventSetup& );
00045   
00046   virtual HepMC::FourVector* getVertex(edm::Event&);
00047   virtual HepMC::FourVector* getRecVertex(edm::Event&);
00048   
00049   protected:
00050   
00051   HepMC::FourVector*       fVertex ;
00052   TMatrixD *boost_;
00053   
00054   private :
00055   
00056   edm::InputTag            signalLabel;
00057   edm::InputTag            hiLabel;
00058   edm::InputTag            cfLabel;
00059   
00060    bool                     useRecVertex;
00061    std::vector<double>      vtxOffset;
00062   bool useCF_;
00063 
00064 };
00065 
00066 MixEvtVtxGenerator::MixEvtVtxGenerator( const ParameterSet& pset ) 
00067         : fVertex(0), boost_(0),
00068           signalLabel(pset.getParameter<edm::InputTag>("signalLabel")),
00069           hiLabel(pset.getParameter<edm::InputTag>("heavyIonLabel")),
00070           useRecVertex(pset.exists("useRecVertex")?pset.getParameter<bool>("useRecVertex"):false)
00071           
00072 {   
00073    produces<bool>("matchedVertex"); 
00074    vtxOffset.resize(3);
00075    if(pset.exists("vtxOffset")) vtxOffset=pset.getParameter< std::vector<double> >("vtxOffset");
00076 
00077    if(useRecVertex) useCF_ = 0;
00078    else{
00079      useCF_ = pset.getUntrackedParameter<bool>("useCF",false);
00080      cfLabel = pset.getUntrackedParameter<edm::InputTag>("mixLabel",edm::InputTag("mixGen","generator"));
00081    }
00082 }
00083 
00084 MixEvtVtxGenerator::~MixEvtVtxGenerator() 
00085 {
00086    delete fVertex ;
00087    if (boost_ != 0 ) delete boost_;
00088    // no need since now it's done in HepMCProduct
00089    // delete fEvt ;
00090 }
00091 
00092 HepMC::FourVector* MixEvtVtxGenerator::getVertex( Event& evt){
00093 
00094   HepMC::GenVertex* genvtx = 0;
00095   const HepMC::GenEvent* inev = 0;
00096 
00097   //cout<<" use CF "<<useCF_<<endl;
00098   
00099   if(useCF_){
00100     Handle<CrossingFrame<HepMCProduct> > cf;
00101     evt.getByLabel(cfLabel,cf);
00102     MixCollection<HepMCProduct> mix(cf.product());
00103     if(mix.size() < 2){
00104       cout<<"Less than 2 sub-events, mixing seems to have failed!"<<endl;
00105     }
00106     const HepMCProduct& bkg = mix.getObject(1);
00107     if(!(bkg.isVtxGenApplied())){
00108       cout<<"Input does not have smeared vertex!"<<endl;
00109     }else{
00110       inev = bkg.GetEvent();
00111    } 
00112   }else{
00113     //cout<<" hiLabel "<<hiLabel<<endl;
00114     Handle<HepMCProduct> input;
00115     evt.getByLabel(hiLabel,input);
00116     inev = input->GetEvent();
00117   }
00118 
00119   genvtx = inev->signal_process_vertex();
00120   if(!genvtx){
00121     //cout<<"No Signal Process Vertex!"<<endl;
00122     HepMC::GenEvent::particle_const_iterator pt=inev->particles_begin();
00123     HepMC::GenEvent::particle_const_iterator ptend=inev->particles_end();
00124     while(!genvtx || ( genvtx->particles_in_size() == 1 && pt != ptend ) ){
00125       //if(!genvtx) cout<<"No Gen Vertex!"<<endl;
00126       if(pt == ptend) cout<<"End reached, No Gen Vertex!"<<endl;
00127       genvtx = (*pt)->production_vertex();
00128       ++pt;
00129     }
00130   }
00131   double aX,aY,aZ,aT;
00132 
00133   aX = genvtx->position().x();
00134   aY = genvtx->position().y();
00135   aZ = genvtx->position().z();
00136   aT = genvtx->position().t();
00137   
00138   if(!fVertex){
00139     fVertex = new HepMC::FourVector();
00140     //cout<<" creating new vertex "<<endl;
00141   }
00142   //cout<<" setting vertex "<<" aX "<<aX<<" aY "<<aY<<" aZ "<<aZ<<" aT "<<aT<<endl;
00143   fVertex->set(aX,aY,aZ,aT);
00144   
00145 
00146   return fVertex;
00147 
00148 }
00149 
00150 
00151 HepMC::FourVector* MixEvtVtxGenerator::getRecVertex( Event& evt){
00152 
00153   Handle<reco::VertexCollection> input;
00154   evt.getByLabel(hiLabel,input);
00155 
00156   double aX,aY,aZ;
00157 
00158   aX = input->begin()->position().x() + vtxOffset[0];
00159   aY = input->begin()->position().y() + vtxOffset[1];
00160   aZ = input->begin()->position().z() + vtxOffset[2];
00161 
00162   /*
00163   std::cout << "reco::Vertex = " << input->begin()->position().x()
00164             << ", " << input->begin()->position().y()
00165             << ", " << input->begin()->position().z()
00166             << std::endl;
00167 
00168   std::cout << "offset = " << vtxOffset[0]
00169             << ", " << vtxOffset[1]
00170             << ", " << vtxOffset[2]
00171             << std::endl;
00172 
00173   std::cout << "embedded GEN vertex = " << aX
00174             << ", " << aY << ", " << aZ << std::endl;
00175 
00176   */
00177   if(!fVertex) fVertex = new HepMC::FourVector();
00178   fVertex->set(10.0*aX,10.0*aY,10.0*aZ,0.0); // HepMC positions in mm (RECO in cm)
00179   
00180   return fVertex;
00181 
00182 }
00183 
00184 void MixEvtVtxGenerator::produce( Event& evt, const EventSetup& )
00185 {
00186    
00187    
00188    Handle<HepMCProduct> HepMCEvt ;
00189    
00190    evt.getByLabel( signalLabel, HepMCEvt ) ;
00191    
00192    // generate new vertex & apply the shift 
00193    //
00194 
00195    HepMCEvt->applyVtxGen( useRecVertex ? getRecVertex(evt) : getVertex(evt) ) ;
00196 
00197    //   HepMCEvt->boostToLab( GetInvLorentzBoost(), "vertex" );
00198    //   HepMCEvt->boostToLab( GetInvLorentzBoost(), "momentum" );
00199    
00200    // OK, create a (pseudo)product and put in into edm::Event
00201    //
00202    auto_ptr<bool> NewProduct(new bool(true)) ;      
00203    evt.put( NewProduct ,"matchedVertex") ;
00204       
00205    return ;
00206 
00207 }
00208 
00209 DEFINE_FWK_MODULE(MixEvtVtxGenerator);
00210 
00211 #endif