CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/FastSimulation/EventProducer/src/FamosProducer.cc

Go to the documentation of this file.
00001 
00002 //#include "FWCore/ParameterSet/interface/ParameterSet.h"
00003 #include "FWCore/Framework/interface/Event.h"
00004 #include "FWCore/Framework/interface/MakerMacros.h"
00005 #include "FWCore/Framework/interface/EventSetup.h"
00006 
00007 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00008 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
00009 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00010 
00011 #include "FastSimDataFormats/NuclearInteractions/interface/FSimVertexTypeFwd.h"
00012 
00013 #include "DataFormats/Common/interface/Handle.h"
00014 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00015 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00016 
00017 #include "FastSimulation/EventProducer/interface/FamosProducer.h"
00018 #include "FastSimulation/EventProducer/interface/FamosManager.h"
00019 #include "FastSimulation/Event/interface/FSimEvent.h"
00020 #include "FastSimulation/Event/interface/KineParticleFilter.h"
00021 #include "FastSimulation/Event/interface/PrimaryVertexGenerator.h"
00022 #include "FastSimulation/Calorimetry/interface/CalorimetryManager.h"
00023 #include "FastSimulation/TrajectoryManager/interface/TrajectoryManager.h"
00024 
00025 #include "HepMC/GenEvent.h"
00026 #include "HepMC/GenVertex.h"
00027 //#include "HepMC/FourVector.h"
00028 
00029 #include <iostream>
00030 #include <memory>
00031 #include <vector>
00032 
00033 FamosProducer::FamosProducer(edm::ParameterSet const & p)      
00034 {    
00035 
00036     produces<edm::SimTrackContainer>();
00037     produces<edm::SimVertexContainer>();
00038     produces<FSimVertexTypeCollection>("VertexTypes");
00039     produces<edm::PSimHitContainer>("TrackerHits");
00040     produces<edm::PCaloHitContainer>("EcalHitsEB");
00041     produces<edm::PCaloHitContainer>("EcalHitsEE");
00042     produces<edm::PCaloHitContainer>("EcalHitsES");
00043     produces<edm::PCaloHitContainer>("HcalHits");
00044     // Temporary facility to allow for the crossing frame to work...
00045     simulateMuons = p.getParameter<bool>("SimulateMuons");
00046     if ( simulateMuons ) produces<edm::SimTrackContainer>("MuonSimTracks");
00047 
00048     // The generator input label
00049     theSourceLabel = p.getParameter<edm::InputTag>("SourceLabel");
00050     theGenParticleLabel = p.getParameter<edm::InputTag>("GenParticleLabel");
00051     theBeamSpotLabel = p.getParameter<edm::InputTag>("BeamSpotLabel");
00052 
00053     famosManager_ = new FamosManager(p);
00054 
00055 }
00056 
00057 FamosProducer::~FamosProducer() 
00058 { if ( famosManager_ ) delete famosManager_; }
00059 
00060 void
00061 FamosProducer::beginRun(edm::Run & run, const edm::EventSetup & es) {
00062   famosManager_->setupGeometryAndField(run,es);
00063 }
00064  
00065 void FamosProducer::endJob()
00066 { 
00067 }
00068  
00069 void FamosProducer::produce(edm::Event & iEvent, const edm::EventSetup & es)
00070 {
00071    using namespace edm;
00072 
00073    // The beam spot position
00074    edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
00075    iEvent.getByLabel(theBeamSpotLabel,recoBeamSpotHandle); 
00076    math::XYZPoint BSPosition_ = recoBeamSpotHandle->position();
00077 
00078    // Get the generated event(s) from the edm::Event
00079    // 1. Check if a HepMCProduct exists
00080    //    a. Take the VtxSmeared if it exists
00081    //    b. Take the source  otherwise
00082    // 2. Otherwise go for the CandidateCollection
00083    Handle<HepMCProduct> theHepMCProduct;
00084 
00085    const HepMC::GenEvent* myGenEvent = 0;
00086    FSimEvent* fevt = famosManager_->simEvent();
00087    fevt->setBeamSpot(BSPosition_);
00088    PrimaryVertexGenerator* theVertexGenerator = fevt->thePrimaryVertexGenerator();
00089 
00090    // Get the generated signal event
00091    bool source = iEvent.getByLabel(theSourceLabel,theHepMCProduct);
00092    if ( source ) { 
00093      myGenEvent = theHepMCProduct->GetEvent();
00094      // First rotate in case of beam crossing angle (except if done already)
00095      if ( theVertexGenerator ) { 
00096        TMatrixD* boost = theVertexGenerator->boost();
00097        if ( boost ) theHepMCProduct->boostToLab(boost,"momentum");
00098      }          
00099      myGenEvent = theHepMCProduct->GetEvent();
00100    } 
00101 
00102    // In case there is no HepMCProduct, seek a genParticle Candidate Collection
00103    bool genPart = false;
00104    const reco::GenParticleCollection* myGenParticles = 0;
00105    if ( !myGenEvent ) { 
00106      // Look for the particle CandidateCollection
00107      Handle<reco::GenParticleCollection> genEvt;
00108      genPart = iEvent.getByLabel(theGenParticleLabel,genEvt);
00109      if ( genPart ) myGenParticles = &(*genEvt);
00110    } 
00111 
00112    if ( !myGenEvent && !genPart )
00113      std::cout << "There is no generator input for this event, under " 
00114                << "any form (HepMCProduct, genParticles)" << std::endl
00115                << "Please check SourceLabel or GenParticleLabel" << std::endl;
00116 
00117    // Get the pile-up events from the pile-up producer
00118    // There might be no pile-up events, by the way, in that case, just continue
00119    
00120    Handle<HepMCProduct> thePileUpEvents;
00121    bool isPileUp = iEvent.getByLabel("famosPileUp","PileUpEvents",thePileUpEvents);
00122    const HepMC::GenEvent* thePUEvents = isPileUp ? thePileUpEvents->GetEvent() : 0;
00123 
00124    // .and pass the event to the Famos Manager for propagation and simulation
00125    if ( myGenEvent || myGenParticles ) 
00126      famosManager_->reconstruct(myGenEvent,myGenParticles,thePUEvents);
00127    
00128    // Set the vertex back to the HepMCProduct (except if it was smeared already)
00129    if ( myGenEvent ) { 
00130      if ( theVertexGenerator ) { 
00131        HepMC::FourVector theVertex(
00132         (theVertexGenerator->X()-theVertexGenerator->beamSpot().X()+BSPosition_.X())*10.,
00133         (theVertexGenerator->Y()-theVertexGenerator->beamSpot().Y()+BSPosition_.Y())*10.,
00134         (theVertexGenerator->Z()-theVertexGenerator->beamSpot().Z()+BSPosition_.Z())*10.,
00135                                    0.);
00136        if ( fabs(theVertexGenerator->Z()) > 1E-10 ) theHepMCProduct->applyVtxGen( &theVertex );
00137      }
00138    }
00139    
00140    CalorimetryManager * calo = famosManager_->calorimetryManager();
00141    TrajectoryManager * tracker = famosManager_->trackerManager();
00142 
00143    // Save everything in the edm::Event
00144    std::auto_ptr<edm::SimTrackContainer> p1(new edm::SimTrackContainer);
00145    std::auto_ptr<edm::SimTrackContainer> m1(new edm::SimTrackContainer);
00146    std::auto_ptr<edm::SimVertexContainer> p2(new edm::SimVertexContainer);
00147    std::auto_ptr<FSimVertexTypeCollection> v1(new FSimVertexTypeCollection);
00148    std::auto_ptr<edm::PSimHitContainer> p3(new edm::PSimHitContainer);
00149    std::auto_ptr<edm::PCaloHitContainer> p4(new edm::PCaloHitContainer);
00150    std::auto_ptr<edm::PCaloHitContainer> p5(new edm::PCaloHitContainer);
00151    std::auto_ptr<edm::PCaloHitContainer> p6(new edm::PCaloHitContainer); 
00152    std::auto_ptr<edm::PCaloHitContainer> p7(new edm::PCaloHitContainer);
00153 
00154    fevt->load(*p1,*m1);
00155    fevt->load(*p2);
00156    fevt->load(*v1);
00157    //   fevt->print();
00158    tracker->loadSimHits(*p3);
00159 
00160    //   fevt->print();
00161 
00162    if ( calo ) {  
00163      calo->loadFromEcalBarrel(*p4);
00164      calo->loadFromEcalEndcap(*p5);
00165      calo->loadFromPreshower(*p6);
00166      calo->loadFromHcal(*p7);
00167      // update the muon SimTracks
00168      calo->loadMuonSimTracks(*m1);
00169    }
00170 
00171    // Write muon first, to allow tracking particles to work... (pending MixingModule fix)
00172    if ( simulateMuons ) iEvent.put(m1,"MuonSimTracks");
00173    iEvent.put(p1);
00174    iEvent.put(p2);
00175    iEvent.put(p3,"TrackerHits");
00176    iEvent.put(v1,"VertexTypes");
00177    iEvent.put(p4,"EcalHitsEB");
00178    iEvent.put(p5,"EcalHitsEE");
00179    iEvent.put(p6,"EcalHitsES");
00180    iEvent.put(p7,"HcalHits");
00181 
00182 }
00183 
00184 DEFINE_FWK_MODULE(FamosProducer);