CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/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 <iostream>
00042 #include <memory>
00043 #include <vector>
00044 
00045 using namespace HepMC;
00046 
00047 FamosManager::FamosManager(edm::ParameterSet const & p)
00048     : iEvent(0),
00049       myPileUpSimulator(0),
00050       myCalorimetry(0),
00051       m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
00052       m_Tracking(p.getParameter<bool>("SimulateTracking")),
00053       m_Calorimetry(p.getParameter<bool>("SimulateCalorimetry")),
00054       m_Alignment(p.getParameter<bool>("ApplyAlignment")),
00055       m_pRunNumber(p.getUntrackedParameter<int>("RunNumber",1)),
00056       m_pVerbose(p.getUntrackedParameter<int>("Verbosity",1))
00057 {
00058 
00059   // Initialize the random number generator service
00060   edm::Service<edm::RandomNumberGenerator> rng;
00061   if ( ! rng.isAvailable() ) {
00062     throw cms::Exception("Configuration")
00063       << "FamosManager requires the RandomGeneratorService\n"
00064          "which is not present in the configuration file.\n"
00065          "You must add the service in the configuration file\n"
00066          "or remove the module that requires it";
00067   }
00068 
00069   random = new RandomEngine(&(*rng));
00070 
00071   // Initialize the FSimEvent
00072   mySimEvent = 
00073     new FSimEvent(p.getParameter<edm::ParameterSet>("VertexGenerator"),
00074                   p.getParameter<edm::ParameterSet>("ParticleFilter"),
00075                   random);
00076 
00078   myTrajectoryManager = 
00079     new TrajectoryManager(mySimEvent,
00080                           p.getParameter<edm::ParameterSet>("MaterialEffects"),
00081                           p.getParameter<edm::ParameterSet>("TrackerSimHits"),
00082                           p.getParameter<edm::ParameterSet>("ActivateDecays"),
00083                           random);
00084 
00085   // Initialize PileUp Producer (if requested)
00086   myPileUpSimulator = new PileUpSimulator(mySimEvent);
00087 
00088   // Initialize Calorimetry Fast Simulation (if requested)
00089   if ( m_Calorimetry) 
00090     myCalorimetry = 
00091       new CalorimetryManager(mySimEvent,
00092                              p.getParameter<edm::ParameterSet>("Calorimetry"),                       
00093                              p.getParameter<edm::ParameterSet>("MaterialEffectsForMuonsInECAL"),
00094                              p.getParameter<edm::ParameterSet>("MaterialEffectsForMuonsInHCAL"),
00095                              p.getParameter<edm::ParameterSet>("GFlash"),
00096                              random);
00097 
00098 }
00099 
00100 FamosManager::~FamosManager()
00101 { 
00102   if ( mySimEvent ) delete mySimEvent; 
00103   if ( myTrajectoryManager ) delete myTrajectoryManager; 
00104   if ( myPileUpSimulator ) delete myPileUpSimulator;
00105   if ( myCalorimetry) delete myCalorimetry;
00106   delete random;
00107 }
00108 
00109 void 
00110 FamosManager::setupGeometryAndField(edm::Run & run, const edm::EventSetup & es)
00111 {
00112   // Particle data table (from Pythia)
00113   edm::ESHandle < HepPDT::ParticleDataTable > pdt;
00114   es.getData(pdt);
00115   mySimEvent->initializePdt(&(*pdt));
00116   ParticleTable::instance(&(*pdt));
00117 
00118   // Initialize the full (misaligned) tracker geometry 
00119   // (only if tracking is requested)
00120   std::string misAligned = m_Alignment ? "MisAligned" : "";
00121   // 1) By default, the aligned geometry is chosen (m_Alignment = false)
00122   // 2) By default, the misaligned geometry is aligned
00123   edm::ESHandle<TrackerGeometry> tracker;
00124   es.get<TrackerDigiGeometryRecord>().get(misAligned,tracker);
00125   if (m_Tracking)  myTrajectoryManager->initializeTrackerGeometry(&(*tracker)); 
00126 
00127   // Initialize the tracker misaligned reco geometry (always needed)
00128   // By default, the misaligned geometry is aligned
00129   edm::ESHandle<GeometricSearchTracker>       theGeomSearchTracker;
00130   es.get<TrackerRecoGeometryRecord>().get(misAligned, theGeomSearchTracker );
00131 
00132   // Initialize the misaligned tracker interaction geometry 
00133   edm::ESHandle<TrackerInteractionGeometry>  theTrackerInteractionGeometry;
00134   es.get<TrackerInteractionGeometryRecord>().get(misAligned, theTrackerInteractionGeometry );
00135 
00136   // Initialize the magnetic field
00137   double bField000 = 0.;
00138   if (m_pUseMagneticField) {
00139     edm::ESHandle<MagneticFieldMap> theMagneticFieldMap;
00140     es.get<MagneticFieldMapRecord>().get(misAligned, theMagneticFieldMap);
00141     const GlobalPoint g(0.,0.,0.);
00142     bField000 = theMagneticFieldMap->inTeslaZ(g);
00143     myTrajectoryManager->initializeRecoGeometry(&(*theGeomSearchTracker),
00144                                                 &(*theTrackerInteractionGeometry),
00145                                                 &(*theMagneticFieldMap));
00146   } else { 
00147     myTrajectoryManager->initializeRecoGeometry(&(*theGeomSearchTracker),
00148                                                 &(*theTrackerInteractionGeometry),
00149                                                 0);
00150     bField000 = 4.0;
00151   }
00152   // The following should be on LogInfo
00153   //std::cout << "B-field(T) at (0,0,0)(cm): " << bField000 << std::endl;      
00154     
00155   //  Initialize the calorimeter geometry
00156   if ( myCalorimetry ) {
00157     edm::ESHandle<CaloGeometry> pG;
00158     es.get<CaloGeometryRecord>().get(pG);   
00159     myCalorimetry->getCalorimeter()->setupGeometry(*pG);
00160 
00161     edm::ESHandle<CaloTopology> theCaloTopology;
00162     es.get<CaloTopologyRecord>().get(theCaloTopology);     
00163     myCalorimetry->getCalorimeter()->setupTopology(*theCaloTopology);
00164     myCalorimetry->getCalorimeter()->initialize(bField000);
00165   }
00166 
00167   m_pRunNumber = run.run();
00168 
00169 }
00170 
00171 
00172 void 
00173 FamosManager::reconstruct(const HepMC::GenEvent* evt,
00174                           const reco::GenParticleCollection* particles,
00175                           const HepMC::GenEvent* pu) 
00176 {
00177 
00178   //  myGenEvent = evt;
00179 
00180   if (evt != 0 || particles != 0) {
00181     iEvent++;
00182     edm::EventID id(m_pRunNumber,1U,iEvent);
00183 
00184 
00185     // Fill the event from the original generated event
00186     if (evt ) 
00187       mySimEvent->fill(*evt,id);
00188     else
00189       mySimEvent->fill(*particles,id);
00190 
00191 
00192     //    mySimEvent->printMCTruth(*evt);
00193     /*
00194     mySimEvent->print();
00195     std::cout << "----------------------------------------" << std::endl;
00196     */
00197 
00198     // Get the pileup events and add the particles to the main event
00199     myPileUpSimulator->produce(pu);
00200 
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 }