CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/src/FastSimulation/EventProducer/src/FamosManager.cc

Go to the documentation of this file.
00001 // CMSSW Header
00002 #include "FWCore/Framework/interface/EventSetup.h"
00003 #include "FWCore/Framework/interface/Run.h"
00004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00005 #include "FWCore/Framework/interface/ESHandle.h"
00006 #include "FWCore/ServiceRegistry/interface/Service.h"
00007 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00008 #include "FWCore/Utilities/interface/Exception.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 #include "DataFormats/Provenance/interface/EventID.h"
00011 
00012 #include "SimGeneral/HepPDTRecord/interface/ParticleDataTable.h"
00013 
00014 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00015 
00016 //#include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00017 //#include "RecoTracker/TkDetLayers/interface/GeometricSearchTracker.h"
00018 #include "RecoTracker/Record/interface/TrackerRecoGeometryRecord.h"
00019 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00020 #include "FastSimulation/TrackerSetup/interface/TrackerInteractionGeometryRecord.h"
00021 #include "FastSimulation/ParticlePropagator/interface/MagneticFieldMapRecord.h"
00022 
00023 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00024 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00025 #include "Geometry/CaloEventSetup/interface/CaloTopologyRecord.h"
00026 //#include "Geometry/CaloTopology/interface/CaloTopology.h"
00027 
00028 // HepMC headers
00029 //#include "HepMC/GenEvent.h"
00030 
00031 // FAMOS Header
00032 #include "FastSimulation/Utilities/interface/RandomEngine.h"
00033 #include "FastSimulation/EventProducer/interface/FamosManager.h"
00034 #include "FastSimulation/TrajectoryManager/interface/TrajectoryManager.h"
00035 #include "FastSimulation/PileUpProducer/interface/PileUpSimulator.h"
00036 #include "FastSimulation/Event/interface/FSimEvent.h"
00037 #include "FastSimulation/ParticlePropagator/interface/MagneticFieldMap.h"
00038 #include "FastSimulation/Particle/interface/ParticleTable.h"
00039 #include "FastSimulation/Calorimetry/interface/CalorimetryManager.h"
00040 #include "FastSimulation/CaloGeometryTools/interface/CaloGeometryHelper.h"  
00041 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00042 #include <iostream>
00043 #include <memory>
00044 #include <vector>
00045 
00046 using namespace HepMC;
00047 
00048 FamosManager::FamosManager(edm::ParameterSet const & p)
00049     : iEvent(0),
00050       myPileUpSimulator(0),
00051       myCalorimetry(0),
00052       m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
00053       m_Tracking(p.getParameter<bool>("SimulateTracking")),
00054       m_Calorimetry(p.getParameter<bool>("SimulateCalorimetry")),
00055       m_Alignment(p.getParameter<bool>("ApplyAlignment")),
00056       m_pRunNumber(p.getUntrackedParameter<int>("RunNumber",1)),
00057       m_pVerbose(p.getUntrackedParameter<int>("Verbosity",1))
00058 {
00059 
00060   // Initialize the random number generator service
00061   edm::Service<edm::RandomNumberGenerator> rng;
00062   if ( ! rng.isAvailable() ) {
00063     throw cms::Exception("Configuration")
00064       << "FamosManager requires the RandomGeneratorService\n"
00065          "which is not present in the configuration file.\n"
00066          "You must add the service in the configuration file\n"
00067          "or remove the module that requires it";
00068   }
00069 
00070   random = new RandomEngine(&(*rng));
00071 
00072   // Initialize the FSimEvent
00073   mySimEvent = 
00074     new FSimEvent(p.getParameter<edm::ParameterSet>("VertexGenerator"),
00075                   p.getParameter<edm::ParameterSet>("ParticleFilter"),
00076                   random);
00077 
00079   myTrajectoryManager = 
00080     new TrajectoryManager(mySimEvent,
00081                           p.getParameter<edm::ParameterSet>("MaterialEffects"),
00082                           p.getParameter<edm::ParameterSet>("TrackerSimHits"),
00083                           p.getParameter<edm::ParameterSet>("ActivateDecays"),
00084                           random);
00085 
00086   // Initialize PileUp Producer (if requested)
00087   myPileUpSimulator = new PileUpSimulator(mySimEvent);
00088 
00089   // Initialize Calorimetry Fast Simulation (if requested)
00090   if ( m_Calorimetry) 
00091     myCalorimetry = 
00092       new CalorimetryManager(mySimEvent,
00093                              p.getParameter<edm::ParameterSet>("Calorimetry"),                       
00094                              p.getParameter<edm::ParameterSet>("MaterialEffectsForMuonsInECAL"),
00095                              p.getParameter<edm::ParameterSet>("MaterialEffectsForMuonsInHCAL"),
00096                              p.getParameter<edm::ParameterSet>("GFlash"),
00097                              random);
00098 
00099 }
00100 
00101 FamosManager::~FamosManager()
00102 { 
00103   if ( mySimEvent ) delete mySimEvent; 
00104   if ( myTrajectoryManager ) delete myTrajectoryManager; 
00105   if ( myPileUpSimulator ) delete myPileUpSimulator;
00106   if ( myCalorimetry) delete myCalorimetry;
00107   delete random;
00108 }
00109 
00110 void 
00111 FamosManager::setupGeometryAndField(edm::Run & run, const edm::EventSetup & es)
00112 {
00113   // Particle data table (from Pythia)
00114   edm::ESHandle < HepPDT::ParticleDataTable > pdt;
00115   es.getData(pdt);
00116   mySimEvent->initializePdt(&(*pdt));
00117   ParticleTable::instance(&(*pdt));
00118 
00119   // Initialize the full (misaligned) tracker geometry 
00120   // (only if tracking is requested)
00121   std::string misAligned = m_Alignment ? "MisAligned" : "";
00122   // 1) By default, the aligned geometry is chosen (m_Alignment = false)
00123   // 2) By default, the misaligned geometry is aligned
00124   edm::ESHandle<TrackerGeometry> tracker;
00125   es.get<TrackerDigiGeometryRecord>().get(misAligned,tracker);
00126   if (m_Tracking)  myTrajectoryManager->initializeTrackerGeometry(&(*tracker)); 
00127 
00128   // Initialize the tracker misaligned reco geometry (always needed)
00129   // By default, the misaligned geometry is aligned
00130   edm::ESHandle<GeometricSearchTracker>       theGeomSearchTracker;
00131   es.get<TrackerRecoGeometryRecord>().get(misAligned, theGeomSearchTracker );
00132 
00133   // Initialize the misaligned tracker interaction geometry 
00134   edm::ESHandle<TrackerInteractionGeometry>  theTrackerInteractionGeometry;
00135   es.get<TrackerInteractionGeometryRecord>().get(misAligned, theTrackerInteractionGeometry );
00136 
00137   // Initialize the magnetic field
00138   double bField000 = 0.;
00139   if (m_pUseMagneticField) {
00140     edm::ESHandle<MagneticFieldMap> theMagneticFieldMap;
00141     es.get<MagneticFieldMapRecord>().get(misAligned, theMagneticFieldMap);
00142     const GlobalPoint g(0.,0.,0.);
00143     bField000 = theMagneticFieldMap->inTeslaZ(g);
00144     myTrajectoryManager->initializeRecoGeometry(&(*theGeomSearchTracker),
00145                                                 &(*theTrackerInteractionGeometry),
00146                                                 &(*theMagneticFieldMap));
00147   } else { 
00148     myTrajectoryManager->initializeRecoGeometry(&(*theGeomSearchTracker),
00149                                                 &(*theTrackerInteractionGeometry),
00150                                                 0);
00151     bField000 = 4.0;
00152   }
00153   // The following should be on LogInfo
00154   //std::cout << "B-field(T) at (0,0,0)(cm): " << bField000 << std::endl;      
00155     
00156   //  Initialize the calorimeter geometry
00157   if ( myCalorimetry ) {
00158     edm::ESHandle<CaloGeometry> pG;
00159     es.get<CaloGeometryRecord>().get(pG);   
00160     myCalorimetry->getCalorimeter()->setupGeometry(*pG);
00161 
00162     edm::ESHandle<CaloTopology> theCaloTopology;
00163     es.get<CaloTopologyRecord>().get(theCaloTopology);     
00164     myCalorimetry->getCalorimeter()->setupTopology(*theCaloTopology);
00165     myCalorimetry->getCalorimeter()->initialize(bField000);
00166   }
00167 
00168   m_pRunNumber = run.run();
00169 
00170 }
00171 
00172 
00173 void 
00174 FamosManager::reconstruct(const HepMC::GenEvent* evt,
00175                           const reco::GenParticleCollection* particles,
00176                           const HepMC::GenEvent* pu)
00177 {
00178 
00179   //  myGenEvent = evt;
00180   
00181   if (evt != 0 || particles != 0) {
00182     iEvent++;
00183     edm::EventID id(m_pRunNumber,1U,iEvent);
00184 
00185 
00186     // Fill the event from the original generated event
00187     if (evt ) 
00188       mySimEvent->fill(*evt,id);
00189     
00190     else 
00191       mySimEvent->fill(*particles,id);
00192     
00193     //    mySimEvent->printMCTruth(*evt);
00194     /*
00195       mySimEvent->print();
00196       std::cout << "----------------------------------------" << std::endl;
00197     */
00198     
00199     // Get the pileup events and add the particles to the main event
00200     myPileUpSimulator->produce(pu);
00201     /*
00202       mySimEvent->print();
00203     std::cout << "----------------------------------------" << std::endl;
00204     */
00205     
00206     // And propagate the particles through the detector
00207     myTrajectoryManager->reconstruct();
00208     /*
00209       mySimEvent->print();
00210       std::cout << "=========================================" 
00211       << std::endl
00212       << std::endl;
00213     */
00214     
00215     if ( myCalorimetry ) myCalorimetry->reconstruct();
00216     
00217   }
00218   
00219   // Should be moved to LogInfo
00220   edm::LogInfo("FamosManager")  << " saved : Event  " << iEvent 
00221                                 << " of weight " << mySimEvent->weight()
00222                                 << " with " << mySimEvent->nTracks() 
00223                                 << " tracks and " << mySimEvent->nVertices()
00224                                 << " vertices, generated by " 
00225                                 << mySimEvent->nGenParts() << " particles " << std::endl;
00226   
00227 }
00228 
00229 void FamosManager::reconstruct(const reco::GenParticleCollection* particles){
00230   iEvent++;
00231   edm::EventID id(m_pRunNumber,1U,iEvent);
00232   mySimEvent->fill(*particles,id);
00233   myTrajectoryManager->reconstruct();
00234   if ( myCalorimetry ) myCalorimetry->reconstruct();
00235 
00236   
00237 
00238 }
00239 
00240 
00241