CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/SimCalorimetry/CastorSim/plugins/CastorDigiProducer.cc

Go to the documentation of this file.
00001 #include "SimCalorimetry/CastorSim/plugins/CastorDigiProducer.h"
00002 #include "FWCore/Framework/interface/EDProducer.h"
00003 #include "DataFormats/Common/interface/Handle.h"
00004 #include "FWCore/Framework/interface/EDProducer.h"
00005 #include "FWCore/Framework/interface/ESHandle.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "SimCalorimetry/CaloSimAlgos/interface/CaloTDigitizer.h"
00008 #include "SimCalorimetry/CaloSimAlgos/interface/CaloShapeIntegrator.h"
00009 #include "DataFormats/HcalDigi/interface/HcalDigiCollections.h"
00010 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00011 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00012 #include "CalibFormats/CastorObjects/interface/CastorDbService.h"
00013 #include "CalibFormats/CastorObjects/interface/CastorDbRecord.h"
00014 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00015 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
00016 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00017 #include "FWCore/ServiceRegistry/interface/Service.h"
00018 #include "DataFormats/HcalDetId/interface/HcalCastorDetId.h"
00019 #include "SimGeneral/MixingModule/interface/PileUpEventPrincipal.h"
00020 
00021 CastorDigiProducer::CastorDigiProducer(const edm::ParameterSet& ps, edm::EDProducer& mixMod) 
00022 : theParameterMap(new CastorSimParameterMap(ps)),
00023   theCastorShape(new CastorShape()),
00024   theCastorIntegratedShape(new CaloShapeIntegrator(theCastorShape)),
00025   theCastorResponse(new CaloHitResponse(theParameterMap, theCastorIntegratedShape)),
00026   theAmplifier(0),
00027   theCoderFactory(0),
00028   theElectronicsSim(0),
00029   theHitCorrection(0),
00030   theCastorDigitizer(0),
00031   theCastorHits()
00032 {
00033   
00034   mixMod.produces<CastorDigiCollection>();
00035 
00036   theCastorResponse->setHitFilter(&theCastorHitFilter);
00037 
00038   bool doTimeSlew = ps.getParameter<bool>("doTimeSlew");
00039   if(doTimeSlew) {
00040     // no time slewing for HF
00041     theCastorResponse->setHitCorrection(theHitCorrection);
00042   }
00043 
00044   bool doNoise = ps.getParameter<bool>("doNoise");
00045   theAmplifier = new CastorAmplifier(theParameterMap, doNoise);
00046   theCoderFactory = new CastorCoderFactory(CastorCoderFactory::DB);
00047   theElectronicsSim = new CastorElectronicsSim(theAmplifier, theCoderFactory);
00048 
00049   theCastorDigitizer = new CastorDigitizer(theCastorResponse, theElectronicsSim, doNoise);
00050 
00051   edm::Service<edm::RandomNumberGenerator> rng;
00052   if ( ! rng.isAvailable()) {
00053     throw cms::Exception("Configuration")
00054       << "CastorDigiProducer requires the RandomNumberGeneratorService\n"
00055          "which is not present in the configuration file.  You must add the service\n"
00056          "in the configuration file or remove the modules that require it.";
00057   }
00058 
00059   CLHEP::HepRandomEngine& engine = rng->getEngine();
00060   theAmplifier->setRandomEngine(engine);
00061   theElectronicsSim->setRandomEngine(engine);
00062 }
00063 
00064 
00065 CastorDigiProducer::~CastorDigiProducer() {
00066   delete theCastorDigitizer;
00067   delete theParameterMap;
00068   delete theCastorShape;
00069   delete theCastorIntegratedShape;
00070   delete theCastorResponse;
00071   delete theElectronicsSim;
00072   delete theAmplifier;
00073   delete theCoderFactory;
00074   delete theHitCorrection;
00075 }
00076 
00077 void CastorDigiProducer::initializeEvent(edm::Event const&, edm::EventSetup const& eventSetup) {
00078   // get the appropriate gains, noises, & widths for this event
00079   edm::ESHandle<CastorDbService> conditions;
00080   eventSetup.get<CastorDbRecord>().get(conditions);
00081   theAmplifier->setDbService(conditions.product());
00082   theCoderFactory->setDbService(conditions.product());
00083   theParameterMap->setDbService(conditions.product());
00084 
00085   edm::LogInfo("CastorDigiProducer") << "checking the geometry...";
00086 
00087   // get the correct geometry
00088   checkGeometry(eventSetup);
00089   
00090   theCastorHits.clear();
00091 
00092   theCastorDigitizer->initializeHits();
00093 }
00094 
00095 void CastorDigiProducer::accumulateCaloHits(std::vector<PCaloHit> const& hcalHits, int bunchCrossing) {
00096   //fillFakeHits();
00097 
00098   if(theHitCorrection != 0) {
00099     theHitCorrection->fillChargeSums(hcalHits);
00100   }
00101   theCastorDigitizer->add(hcalHits, bunchCrossing); 
00102 }
00103 
00104 void CastorDigiProducer::accumulate(edm::Event const& e, edm::EventSetup const&) {
00105   // Step A: Get and accumulate digitized hits 
00106   edm::InputTag castorTag("g4SimHits", "CastorFI");
00107   edm::Handle<std::vector<PCaloHit> > castorHandle;
00108   e.getByLabel(castorTag, castorHandle);
00109 
00110   accumulateCaloHits(*castorHandle.product(), 0);
00111 }
00112 
00113 void CastorDigiProducer::accumulate(PileUpEventPrincipal const& e, edm::EventSetup const&) {
00114   // Step A: Get and accumulate digitized hits 
00115   edm::InputTag castorTag("g4SimHits", "CastorFI");
00116   edm::Handle<std::vector<PCaloHit> > castorHandle;
00117   e.getByLabel(castorTag, castorHandle);
00118 
00119   accumulateCaloHits(*castorHandle.product(), e.bunchCrossing());
00120 }
00121 
00122 void CastorDigiProducer::finalizeEvent(edm::Event& e, const edm::EventSetup& eventSetup) {
00123   // Step B: Create empty output
00124 
00125   std::auto_ptr<CastorDigiCollection> castorResult(new CastorDigiCollection());
00126 
00127   // Step C: Invoke the algorithm, getting back outputs.
00128   theCastorDigitizer->run(*castorResult);
00129 
00130   edm::LogInfo("CastorDigiProducer") << "HCAL/Castor digis   : " << castorResult->size();
00131 
00132   // Step D: Put outputs into event
00133   e.put(castorResult);
00134 }
00135 
00136 
00137 void CastorDigiProducer::sortHits(const edm::PCaloHitContainer & hits){
00138   for(edm::PCaloHitContainer::const_iterator hitItr = hits.begin();
00139       hitItr != hits.end(); ++hitItr){
00140     DetId detId = hitItr->id();
00141     if (detId.det()==DetId::Calo && detId.subdetId()==HcalCastorDetId::SubdetectorId){
00142       theCastorHits.push_back(*hitItr);
00143     }
00144       else {
00145         edm::LogError("CastorDigiProducer") << "Bad Hit subdetector " << detId.subdetId();
00146       }
00147   }
00148 }
00149 
00150 void CastorDigiProducer::fillFakeHits() {
00151   HcalCastorDetId castorDetId(HcalCastorDetId::Section(2),true,1,1);
00152 
00153   theCastorHits.emplace_back(castorDetId.rawId(), 50.0, 0.);
00154 }
00155 
00156 
00157 void CastorDigiProducer::checkGeometry(const edm::EventSetup & eventSetup) {
00158   // TODO find a way to avoid doing this every event
00159   edm::ESHandle<CaloGeometry> geometry;
00160   eventSetup.get<CaloGeometryRecord>().get(geometry);
00161   theCastorResponse->setGeometry(&*geometry);
00162 
00163   const std::vector<DetId>& castorCells = geometry->getValidDetIds(DetId::Calo, HcalCastorDetId::SubdetectorId);
00164 
00165   //std::cout<<"CastorDigiProducer::CheckGeometry number of cells: "<<castorCells.size()<<std::endl;
00166   theCastorDigitizer->setDetIds(castorCells);
00167 }
00168 
00169