CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/FastSimulation/EventProducer/src/FamosProducer.cc

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