Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 10:03:35

0001 #include <memory>
0002 
0003 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "SimCalorimetry/CaloSimAlgos/interface/CaloHitResponse.h"
0006 #include "SimCalorimetry/EcalSimAlgos/interface/APDSimParameters.h"
0007 #include "SimCalorimetry/EcalSimAlgos/interface/ComponentSimParameterMap.h"
0008 #include "SimCalorimetry/EcalSimAlgos/interface/EBHitResponse.h"
0009 #include "SimCalorimetry/EcalSimAlgos/interface/EEHitResponse.h"
0010 #include "SimCalorimetry/EcalSimAlgos/interface/ESElectronicsSim.h"
0011 #include "SimCalorimetry/EcalSimAlgos/interface/ESElectronicsSimFast.h"
0012 #include "SimCalorimetry/EcalSimAlgos/interface/ESHitResponse.h"
0013 #include "SimCalorimetry/EcalSimAlgos/interface/EcalCoder.h"
0014 #include "SimCalorimetry/EcalSimAlgos/interface/EcalElectronicsSim.h"
0015 #include "SimCalorimetry/EcalSimAlgos/interface/EcalSimParameterMap.h"
0016 #include "SimCalorimetry/EcalSimProducers/interface/EcalDigiProducer.h"
0017 //#include "SimCalorimetry/EcalSimAlgos/interface/ESFastTDigitizer.h"
0018 #include "CalibFormats/CaloObjects/interface/CaloSamples.h"
0019 #include "DataFormats/Common/interface/Handle.h"
0020 #include "FWCore/Framework/interface/ConsumesCollector.h"
0021 #include "FWCore/Framework/interface/Event.h"
0022 #include "FWCore/Framework/interface/EventSetup.h"
0023 #include "FWCore/Framework/interface/LuminosityBlock.h"
0024 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/ServiceRegistry/interface/Service.h"
0027 #include "FWCore/Utilities/interface/Exception.h"
0028 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
0029 #include "FWCore/Utilities/interface/StreamID.h"
0030 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0031 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
0032 #include "SimCalorimetry/EcalSimAlgos/interface/ESDigitizer.h"
0033 #include "SimGeneral/MixingModule/interface/PileUpEventPrincipal.h"
0034 
0035 #include "Geometry/EcalAlgo/interface/EcalEndcapGeometry.h"
0036 
0037 EcalDigiProducer::EcalDigiProducer(const edm::ParameterSet &params,
0038                                    edm::ProducesCollector producesCollector,
0039                                    edm::ConsumesCollector &iC)
0040     : EcalDigiProducer(params, iC) {
0041   if (m_apdSeparateDigi)
0042     producesCollector.produces<EBDigiCollection>(m_apdDigiTag);
0043 
0044   if (m_componentSeparateDigi)
0045     producesCollector.produces<EBDigiCollection>(m_componentDigiTag);
0046 
0047   producesCollector.produces<EBDigiCollection>(m_EBdigiCollection);
0048   producesCollector.produces<EEDigiCollection>(m_EEdigiCollection);
0049   producesCollector.produces<ESDigiCollection>(m_ESdigiCollection);
0050 }
0051 
0052 // version for Pre-Mixing, for use outside of MixingModule
0053 EcalDigiProducer::EcalDigiProducer(const edm::ParameterSet &params, edm::ConsumesCollector &iC)
0054     : DigiAccumulatorMixMod(),
0055       m_APDShape(iC),
0056       m_ComponentShapes(iC),
0057       m_EBShape(iC),
0058       m_EEShape(iC),
0059       m_ESShape(),
0060       m_EBdigiCollection(params.getParameter<std::string>("EBdigiCollection")),
0061       m_EEdigiCollection(params.getParameter<std::string>("EEdigiCollection")),
0062       m_ESdigiCollection(params.getParameter<std::string>("ESdigiCollection")),
0063       m_hitsProducerTag(params.getParameter<std::string>("hitsProducer")),
0064       m_HitsEBToken_(iC.consumes<std::vector<PCaloHit>>(edm::InputTag(m_hitsProducerTag, "EcalHitsEB"))),
0065       m_HitsEEToken_(iC.consumes<std::vector<PCaloHit>>(edm::InputTag(m_hitsProducerTag, "EcalHitsEE"))),
0066       m_HitsESToken_(iC.consumes<std::vector<PCaloHit>>(edm::InputTag(m_hitsProducerTag, "EcalHitsES"))),
0067       m_pedestalsToken(iC.esConsumes()),
0068       m_icalToken(iC.esConsumes()),
0069       m_laserToken(iC.esConsumes()),
0070       m_agcToken(iC.esConsumes()),
0071       m_grToken(iC.esConsumes()),
0072       m_geometryToken(iC.esConsumes()),
0073       m_useLCcorrection(params.getUntrackedParameter<bool>("UseLCcorrection")),
0074       m_apdSeparateDigi(params.getParameter<bool>("apdSeparateDigi")),
0075       m_componentSeparateDigi(params.getParameter<bool>("componentSeparateDigi")),
0076 
0077       m_EBs25notCont(params.getParameter<double>("EBs25notContainment")),
0078       m_EEs25notCont(params.getParameter<double>("EEs25notContainment")),
0079 
0080       m_readoutFrameSize(ecalPh1::sampleSize),
0081       m_ParameterMap(new EcalSimParameterMap(params.getParameter<double>("simHitToPhotoelectronsBarrel"),
0082                                              params.getParameter<double>("simHitToPhotoelectronsEndcap"),
0083                                              params.getParameter<double>("photoelectronsToAnalogBarrel"),
0084                                              params.getParameter<double>("photoelectronsToAnalogEndcap"),
0085                                              params.getParameter<double>("samplingFactor"),
0086                                              params.getParameter<double>("timePhase"),
0087                                              m_readoutFrameSize,
0088                                              params.getParameter<int>("binOfMaximum"),
0089                                              params.getParameter<bool>("doPhotostatistics"),
0090                                              params.getParameter<bool>("syncPhase"))),
0091 
0092       m_apdDigiTag(params.getParameter<std::string>("apdDigiTag")),
0093       m_apdParameters(new APDSimParameters(params.getParameter<bool>("apdAddToBarrel"),
0094                                            m_apdSeparateDigi,
0095                                            params.getParameter<double>("apdSimToPELow"),
0096                                            params.getParameter<double>("apdSimToPEHigh"),
0097                                            params.getParameter<double>("apdTimeOffset"),
0098                                            params.getParameter<double>("apdTimeOffWidth"),
0099                                            params.getParameter<bool>("apdDoPEStats"),
0100                                            m_apdDigiTag,
0101                                            params.getParameter<std::vector<double>>("apdNonlParms"))),
0102 
0103       m_componentDigiTag(params.getParameter<std::string>("componentDigiTag")),
0104       m_componentParameters(
0105           std::make_unique<ComponentSimParameterMap>(params.getParameter<bool>("componentAddToBarrel"),
0106                                                      m_componentSeparateDigi,
0107                                                      params.getParameter<double>("simHitToPhotoelectronsBarrel"),
0108                                                      0,  // endcap parameters not needed
0109                                                      params.getParameter<double>("photoelectronsToAnalogBarrel"),
0110                                                      0,
0111                                                      params.getParameter<double>("samplingFactor"),
0112                                                      params.getParameter<double>("componentTimePhase"),
0113                                                      m_readoutFrameSize,
0114                                                      params.getParameter<int>("binOfMaximum"),
0115                                                      params.getParameter<bool>("doPhotostatistics"),
0116                                                      params.getParameter<bool>("syncPhase"))),
0117 
0118       m_APDResponse(!m_apdSeparateDigi ? nullptr
0119                                        : new EBHitResponse(m_ParameterMap.get(),
0120                                                            &m_EBShape,
0121                                                            true,
0122                                                            false,
0123                                                            m_apdParameters.get(),
0124                                                            &m_APDShape,
0125                                                            m_componentParameters.get(),
0126                                                            &m_ComponentShapes)),
0127 
0128       m_ComponentResponse(!m_componentSeparateDigi
0129                               ? nullptr
0130                               : std::make_unique<EBHitResponse>(
0131                                     m_ParameterMap.get(),
0132                                     &m_EBShape,
0133                                     false,
0134                                     true,
0135                                     m_apdParameters.get(),
0136                                     &m_APDShape,
0137                                     m_componentParameters.get(),
0138                                     &m_ComponentShapes)),  // check if that false is correct // TODO HERE JCH
0139 
0140       m_EBResponse(new EBHitResponse(m_ParameterMap.get(),
0141                                      &m_EBShape,
0142                                      false,  // barrel
0143                                      false,  // normal non-component shape based
0144                                      m_apdParameters.get(),
0145                                      &m_APDShape,
0146                                      m_componentParameters.get(),
0147                                      &m_ComponentShapes)),
0148 
0149       m_EEResponse(new EEHitResponse(m_ParameterMap.get(), &m_EEShape)),
0150       m_ESResponse(new ESHitResponse(m_ParameterMap.get(), &m_ESShape)),
0151       m_ESOldResponse(new CaloHitResponse(m_ParameterMap.get(), &m_ESShape)),
0152 
0153       m_addESNoise(params.getParameter<bool>("doESNoise")),
0154       m_PreMix1(params.getParameter<bool>("EcalPreMixStage1")),
0155       m_PreMix2(params.getParameter<bool>("EcalPreMixStage2")),
0156 
0157       m_doFastES(params.getParameter<bool>("doFast")),
0158 
0159       m_doEB(params.getParameter<bool>("doEB")),
0160       m_doEE(params.getParameter<bool>("doEE")),
0161       m_doES(params.getParameter<bool>("doES")),
0162 
0163       m_ESElectronicsSim(m_doFastES ? nullptr : new ESElectronicsSim(m_addESNoise)),
0164 
0165       m_ESOldDigitizer(m_doFastES ? nullptr
0166                                   : new ESOldDigitizer(m_ESOldResponse.get(), m_ESElectronicsSim.get(), m_addESNoise)),
0167 
0168       m_ESElectronicsSimFast(!m_doFastES ? nullptr : new ESElectronicsSimFast(m_addESNoise, m_PreMix1)),
0169 
0170       m_ESDigitizer(!m_doFastES ? nullptr
0171                                 : new ESDigitizer(m_ESResponse.get(), m_ESElectronicsSimFast.get(), m_addESNoise)),
0172 
0173       m_APDDigitizer(nullptr),
0174       m_ComponentDigitizer(nullptr),
0175       m_BarrelDigitizer(nullptr),
0176       m_EndcapDigitizer(nullptr),
0177       m_ElectronicsSim(nullptr),
0178       m_Coder(nullptr),
0179       m_APDElectronicsSim(nullptr),
0180       m_APDCoder(nullptr),
0181       m_Geometry(nullptr),
0182       m_EBCorrNoise({{nullptr, nullptr, nullptr}}),
0183       m_EECorrNoise({{nullptr, nullptr, nullptr}}) {
0184   // "produces" statements taken care of elsewhere.
0185   //   if(m_apdSeparateDigi) mixMod.produces<EBDigiCollection>(m_apdDigiTag);
0186   // mixMod.produces<EBDigiCollection>(m_EBdigiCollection);
0187   // mixMod.produces<EEDigiCollection>(m_EEdigiCollection);
0188   // mixMod.produces<ESDigiCollection>(m_ESdigiCollection);
0189   if (m_doEB)
0190     iC.consumes<std::vector<PCaloHit>>(edm::InputTag(m_hitsProducerTag, "EcalHitsEB"));
0191   if (m_doEE)
0192     iC.consumes<std::vector<PCaloHit>>(edm::InputTag(m_hitsProducerTag, "EcalHitsEE"));
0193   if (m_doES) {
0194     iC.consumes<std::vector<PCaloHit>>(edm::InputTag(m_hitsProducerTag, "EcalHitsES"));
0195     m_esGainToken = iC.esConsumes();
0196     m_esMIPToGeVToken = iC.esConsumes();
0197     m_esPedestalsToken = iC.esConsumes();
0198     m_esMIPsToken = iC.esConsumes();
0199   }
0200 
0201   const std::vector<double> ebCorMatG12 = params.getParameter<std::vector<double>>("EBCorrNoiseMatrixG12");
0202   const std::vector<double> eeCorMatG12 = params.getParameter<std::vector<double>>("EECorrNoiseMatrixG12");
0203   const std::vector<double> ebCorMatG06 = params.getParameter<std::vector<double>>("EBCorrNoiseMatrixG06");
0204   const std::vector<double> eeCorMatG06 = params.getParameter<std::vector<double>>("EECorrNoiseMatrixG06");
0205   const std::vector<double> ebCorMatG01 = params.getParameter<std::vector<double>>("EBCorrNoiseMatrixG01");
0206   const std::vector<double> eeCorMatG01 = params.getParameter<std::vector<double>>("EECorrNoiseMatrixG01");
0207 
0208   const bool applyConstantTerm = params.getParameter<bool>("applyConstantTerm");
0209   const double rmsConstantTerm = params.getParameter<double>("ConstantTerm");
0210 
0211   const bool addNoise = params.getParameter<bool>("doENoise");
0212   const bool cosmicsPhase = params.getParameter<bool>("cosmicsPhase");
0213   const double cosmicsShift = params.getParameter<double>("cosmicsShift");
0214 
0215   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0216 
0217   // further phase for cosmics studies
0218   if (cosmicsPhase) {
0219     if (m_doEB)
0220       m_EBResponse->setPhaseShift(1. + cosmicsShift);
0221     if (m_doEE)
0222       m_EEResponse->setPhaseShift(1. + cosmicsShift);
0223   }
0224 
0225   EcalCorrMatrix ebMatrix[3];
0226   EcalCorrMatrix eeMatrix[3];
0227 
0228   assert(ebCorMatG12.size() == m_readoutFrameSize);
0229   assert(eeCorMatG12.size() == m_readoutFrameSize);
0230   assert(ebCorMatG06.size() == m_readoutFrameSize);
0231   assert(eeCorMatG06.size() == m_readoutFrameSize);
0232   assert(ebCorMatG01.size() == m_readoutFrameSize);
0233   assert(eeCorMatG01.size() == m_readoutFrameSize);
0234 
0235   assert(1.e-7 > fabs(ebCorMatG12[0] - 1.0));
0236   assert(1.e-7 > fabs(ebCorMatG06[0] - 1.0));
0237   assert(1.e-7 > fabs(ebCorMatG01[0] - 1.0));
0238   assert(1.e-7 > fabs(eeCorMatG12[0] - 1.0));
0239   assert(1.e-7 > fabs(eeCorMatG06[0] - 1.0));
0240   assert(1.e-7 > fabs(eeCorMatG01[0] - 1.0));
0241 
0242   for (unsigned int row(0); row != m_readoutFrameSize; ++row) {
0243     assert(0 == row || 1. >= ebCorMatG12[row]);
0244     assert(0 == row || 1. >= ebCorMatG06[row]);
0245     assert(0 == row || 1. >= ebCorMatG01[row]);
0246     assert(0 == row || 1. >= eeCorMatG12[row]);
0247     assert(0 == row || 1. >= eeCorMatG06[row]);
0248     assert(0 == row || 1. >= eeCorMatG01[row]);
0249     for (unsigned int column(0); column <= row; ++column) {
0250       const unsigned int index(row - column);
0251       ebMatrix[0](row, column) = ebCorMatG12[index];
0252       eeMatrix[0](row, column) = eeCorMatG12[index];
0253       ebMatrix[1](row, column) = ebCorMatG06[index];
0254       eeMatrix[1](row, column) = eeCorMatG06[index];
0255       ebMatrix[2](row, column) = ebCorMatG01[index];
0256       eeMatrix[2](row, column) = eeCorMatG01[index];
0257     }
0258   }
0259 
0260   m_EBCorrNoise[0] = std::make_unique<CorrelatedNoisifier<EcalCorrMatrix>>(ebMatrix[0]);
0261   m_EECorrNoise[0] = std::make_unique<CorrelatedNoisifier<EcalCorrMatrix>>(eeMatrix[0]);
0262   m_EBCorrNoise[1] = std::make_unique<CorrelatedNoisifier<EcalCorrMatrix>>(ebMatrix[1]);
0263   m_EECorrNoise[1] = std::make_unique<CorrelatedNoisifier<EcalCorrMatrix>>(eeMatrix[1]);
0264   m_EBCorrNoise[2] = std::make_unique<CorrelatedNoisifier<EcalCorrMatrix>>(ebMatrix[2]);
0265   m_EECorrNoise[2] = std::make_unique<CorrelatedNoisifier<EcalCorrMatrix>>(eeMatrix[2]);
0266 
0267   m_Coder = std::make_unique<EcalCoder>(addNoise,
0268                                         m_PreMix1,
0269                                         m_EBCorrNoise[0].get(),
0270                                         m_EECorrNoise[0].get(),
0271                                         m_EBCorrNoise[1].get(),
0272                                         m_EECorrNoise[1].get(),
0273                                         m_EBCorrNoise[2].get(),
0274                                         m_EECorrNoise[2].get());
0275 
0276   m_ElectronicsSim =
0277       std::make_unique<EcalElectronicsSim_Ph1>(m_ParameterMap.get(), m_Coder.get(), applyConstantTerm, rmsConstantTerm);
0278 
0279   if (m_apdSeparateDigi) {
0280     m_APDCoder = std::make_unique<EcalCoder>(false,
0281                                              m_PreMix1,
0282                                              m_EBCorrNoise[0].get(),
0283                                              m_EECorrNoise[0].get(),
0284                                              m_EBCorrNoise[1].get(),
0285                                              m_EECorrNoise[1].get(),
0286                                              m_EBCorrNoise[2].get(),
0287                                              m_EECorrNoise[2].get());
0288 
0289     m_APDElectronicsSim = std::make_unique<EcalElectronicsSim_Ph1>(
0290         m_ParameterMap.get(), m_APDCoder.get(), applyConstantTerm, rmsConstantTerm);
0291 
0292     m_APDDigitizer = std::make_unique<EBDigitizer>(m_APDResponse.get(), m_APDElectronicsSim.get(), false);
0293   }
0294   if (m_componentSeparateDigi) {
0295     m_ComponentCoder = std::make_unique<EcalCoder>(addNoise,
0296                                                    m_PreMix1,
0297                                                    m_EBCorrNoise[0].get(),
0298                                                    m_EECorrNoise[0].get(),
0299                                                    m_EBCorrNoise[1].get(),
0300                                                    m_EECorrNoise[1].get(),
0301                                                    m_EBCorrNoise[2].get(),
0302                                                    m_EECorrNoise[2].get());
0303     m_ComponentElectronicsSim = std::make_unique<EcalElectronicsSim_Ph1>(
0304         m_ParameterMap.get(), m_ComponentCoder.get(), applyConstantTerm, rmsConstantTerm);
0305     m_ComponentDigitizer =
0306         std::make_unique<EBDigitizer>(m_ComponentResponse.get(), m_ComponentElectronicsSim.get(), addNoise);
0307   }
0308 
0309   if (m_doEB) {
0310     m_BarrelDigitizer = std::make_unique<EBDigitizer>(m_EBResponse.get(), m_ElectronicsSim.get(), addNoise);
0311   }
0312 
0313   if (m_doEE) {
0314     m_EndcapDigitizer = std::make_unique<EEDigitizer>(m_EEResponse.get(), m_ElectronicsSim.get(), addNoise);
0315   }
0316 }
0317 
0318 EcalDigiProducer::~EcalDigiProducer() {}
0319 
0320 void EcalDigiProducer::initializeEvent(edm::Event const &event, edm::EventSetup const &eventSetup) {
0321   edm::Service<edm::RandomNumberGenerator> rng;
0322   randomEngine_ = &rng->getEngine(event.streamID());
0323 
0324   checkGeometry(eventSetup);
0325   checkCalibrations(event, eventSetup);
0326   if (m_doEB) {
0327     m_BarrelDigitizer->initializeHits();
0328     if (m_apdSeparateDigi) {
0329       m_APDDigitizer->initializeHits();
0330     }
0331     if (m_componentSeparateDigi) {
0332       m_ComponentDigitizer->initializeHits();
0333     }
0334   }
0335   if (m_doEE) {
0336     m_EndcapDigitizer->initializeHits();
0337   }
0338   if (m_doES) {
0339     if (m_doFastES) {
0340       m_ESDigitizer->initializeHits();
0341     } else {
0342       m_ESOldDigitizer->initializeHits();
0343     }
0344   }
0345 }
0346 
0347 void EcalDigiProducer::accumulateCaloHits(HitsHandle const &ebHandle,
0348                                           HitsHandle const &eeHandle,
0349                                           HitsHandle const &esHandle,
0350                                           int bunchCrossing) {
0351   if (m_doEB && ebHandle.isValid()) {
0352     m_BarrelDigitizer->add(*ebHandle.product(), bunchCrossing, randomEngine_);
0353 
0354     if (m_apdSeparateDigi) {
0355       m_APDDigitizer->add(*ebHandle.product(), bunchCrossing, randomEngine_);
0356     }
0357     if (m_componentSeparateDigi) {
0358       m_ComponentDigitizer->add(*ebHandle.product(), bunchCrossing, randomEngine_);
0359     }
0360   }
0361 
0362   if (m_doEE && eeHandle.isValid()) {
0363     m_EndcapDigitizer->add(*eeHandle.product(), bunchCrossing, randomEngine_);
0364   }
0365 
0366   if (m_doES && esHandle.isValid()) {
0367     if (m_doFastES) {
0368       m_ESDigitizer->add(*esHandle.product(), bunchCrossing, randomEngine_);
0369     } else {
0370       m_ESOldDigitizer->add(*esHandle.product(), bunchCrossing, randomEngine_);
0371     }
0372   }
0373 }
0374 
0375 void EcalDigiProducer::accumulate(edm::Event const &e, edm::EventSetup const &eventSetup) {
0376   // Step A: Get Inputs
0377   const edm::Handle<std::vector<PCaloHit>> &ebHandle = e.getHandle(m_HitsEBToken_);
0378   if (m_doEB) {
0379     m_EBShape.setEventSetup(eventSetup);
0380     m_APDShape.setEventSetup(eventSetup);
0381     m_ComponentShapes.setEventSetup(eventSetup);
0382   }
0383 
0384   const edm::Handle<std::vector<PCaloHit>> &eeHandle = e.getHandle(m_HitsEEToken_);
0385   if (m_doEE) {
0386     m_EEShape.setEventSetup(eventSetup);
0387   }
0388 
0389   const edm::Handle<std::vector<PCaloHit>> &esHandle = e.getHandle(m_HitsESToken_);
0390 
0391   accumulateCaloHits(ebHandle, eeHandle, esHandle, 0);
0392 }
0393 
0394 void EcalDigiProducer::accumulate(PileUpEventPrincipal const &e,
0395                                   edm::EventSetup const &eventSetup,
0396                                   edm::StreamID const &streamID) {
0397   // Step A: Get Inputs
0398   edm::Handle<std::vector<PCaloHit>> ebHandle;
0399   if (m_doEB) {
0400     edm::InputTag ebTag(m_hitsProducerTag, "EcalHitsEB");
0401     e.getByLabel(ebTag, ebHandle);
0402   }
0403 
0404   edm::Handle<std::vector<PCaloHit>> eeHandle;
0405   if (m_doEE) {
0406     edm::InputTag eeTag(m_hitsProducerTag, "EcalHitsEE");
0407     e.getByLabel(eeTag, eeHandle);
0408   }
0409 
0410   edm::Handle<std::vector<PCaloHit>> esHandle;
0411   if (m_doES) {
0412     edm::InputTag esTag(m_hitsProducerTag, "EcalHitsES");
0413     e.getByLabel(esTag, esHandle);
0414   }
0415 
0416   accumulateCaloHits(ebHandle, eeHandle, esHandle, e.bunchCrossing());
0417 }
0418 
0419 void EcalDigiProducer::finalizeEvent(edm::Event &event, edm::EventSetup const &eventSetup) {
0420   // Step B: Create empty output
0421   std::unique_ptr<EBDigiCollection> apdResult(!m_apdSeparateDigi || !m_doEB ? nullptr : new EBDigiCollection());
0422   std::unique_ptr<EBDigiCollection> componentResult(!m_componentSeparateDigi || !m_doEB ? nullptr
0423                                                                                         : new EBDigiCollection());
0424   std::unique_ptr<EBDigiCollection> barrelResult(new EBDigiCollection());
0425   std::unique_ptr<EEDigiCollection> endcapResult(new EEDigiCollection());
0426   std::unique_ptr<ESDigiCollection> preshowerResult(new ESDigiCollection());
0427 
0428   // run the algorithm
0429 
0430   if (m_doEB) {
0431     m_BarrelDigitizer->run(*barrelResult, randomEngine_);
0432     cacheEBDigis(&*barrelResult);
0433 
0434     edm::LogInfo("DigiInfo") << "EB Digis: " << barrelResult->size();
0435 
0436     if (m_apdSeparateDigi) {
0437       m_APDDigitizer->run(*apdResult, randomEngine_);
0438       edm::LogInfo("DigiInfo") << "APD Digis: " << apdResult->size();
0439     }
0440     if (m_componentSeparateDigi) {
0441       m_ComponentDigitizer->run(*componentResult, randomEngine_);
0442       edm::LogInfo("DigiInfo") << "Component Digis: " << componentResult->size();
0443     }
0444   }
0445 
0446   if (m_doEE) {
0447     m_EndcapDigitizer->run(*endcapResult, randomEngine_);
0448     edm::LogInfo("EcalDigi") << "EE Digis: " << endcapResult->size();
0449     cacheEEDigis(&*endcapResult);
0450   }
0451   if (m_doES) {
0452     if (m_doFastES) {
0453       m_ESDigitizer->run(*preshowerResult, randomEngine_);
0454     } else {
0455       m_ESOldDigitizer->run(*preshowerResult, randomEngine_);
0456     }
0457     edm::LogInfo("EcalDigi") << "ES Digis: " << preshowerResult->size();
0458   }
0459 
0460   // Step D: Put outputs into event
0461   if (m_apdSeparateDigi) {
0462     // event.put(std::move(apdResult),    m_apdDigiTag         ) ;
0463   }
0464 
0465   event.put(std::move(barrelResult), m_EBdigiCollection);
0466   if (m_componentSeparateDigi) {
0467     event.put(std::move(componentResult), m_componentDigiTag);
0468   }
0469   event.put(std::move(endcapResult), m_EEdigiCollection);
0470   event.put(std::move(preshowerResult), m_ESdigiCollection);
0471 
0472   randomEngine_ = nullptr;  // to prevent access outside event
0473 }
0474 
0475 void EcalDigiProducer::beginLuminosityBlock(edm::LuminosityBlock const &lumi, edm::EventSetup const &setup) {
0476   edm::Service<edm::RandomNumberGenerator> rng;
0477   if (!rng.isAvailable()) {
0478     throw cms::Exception("Configuration") << "RandomNumberGenerator service is not available.\n"
0479                                              "You must add the service in the configuration file\n"
0480                                              "or remove the module that requires it.";
0481   }
0482   CLHEP::HepRandomEngine *engine = &rng->getEngine(lumi.index());
0483 
0484   if (m_doEB) {
0485     if (nullptr != m_APDResponse)
0486       m_APDResponse->initialize(engine);
0487     if (nullptr != m_ComponentResponse)
0488       m_ComponentResponse->initialize(engine);
0489     m_EBResponse->initialize(engine);
0490   }
0491 }
0492 
0493 void EcalDigiProducer::checkCalibrations(const edm::Event &event, const edm::EventSetup &eventSetup) {
0494   // Pedestals from event setup
0495 
0496   const EcalPedestals *pedestals = &eventSetup.getData(m_pedestalsToken);
0497 
0498   m_Coder->setPedestals(pedestals);
0499   if (nullptr != m_APDCoder)
0500     m_APDCoder->setPedestals(pedestals);
0501   if (nullptr != m_ComponentCoder)
0502     m_ComponentCoder->setPedestals(pedestals);
0503 
0504   // Ecal Intercalibration Constants
0505   const EcalIntercalibConstantsMC *ical = &eventSetup.getData(m_icalToken);
0506 
0507   m_Coder->setIntercalibConstants(ical);
0508   if (nullptr != m_APDCoder)
0509     m_APDCoder->setIntercalibConstants(ical);
0510   if (nullptr != m_ComponentCoder)
0511     m_ComponentCoder->setIntercalibConstants(ical);
0512 
0513   m_EBResponse->setIntercal(ical);
0514   if (nullptr != m_APDResponse)
0515     m_APDResponse->setIntercal(ical);
0516   if (nullptr != m_ComponentResponse)
0517     m_ComponentResponse->setIntercal(ical);
0518 
0519   // Ecal LaserCorrection Constants
0520   const EcalLaserDbService *laser = &eventSetup.getData(m_laserToken);
0521   const edm::TimeValue_t eventTimeValue = event.time().value();
0522 
0523   m_EBResponse->setEventTime(eventTimeValue);
0524   m_EBResponse->setLaserConstants(laser, m_useLCcorrection);
0525 
0526   m_EEResponse->setEventTime(eventTimeValue);
0527   m_EEResponse->setLaserConstants(laser, m_useLCcorrection);
0528 
0529   // ADC -> GeV Scale
0530   const EcalADCToGeVConstant *agc = &eventSetup.getData(m_agcToken);
0531 
0532   // Gain Ratios
0533   const EcalGainRatios *gr = &eventSetup.getData(m_grToken);
0534 
0535   m_Coder->setGainRatios(gr);
0536   if (nullptr != m_APDCoder)
0537     m_APDCoder->setGainRatios(gr);
0538   if (nullptr != m_ComponentCoder)
0539     m_ComponentCoder->setGainRatios(gr);
0540 
0541   EcalMGPAGainRatio *defaultRatios = new EcalMGPAGainRatio();
0542 
0543   double theGains[m_Coder->NGAINS + 1];
0544   theGains[0] = 0.;
0545   theGains[3] = 1.;
0546   theGains[2] = defaultRatios->gain6Over1();
0547   theGains[1] = theGains[2] * (defaultRatios->gain12Over6());
0548 
0549   LogDebug("EcalDigi") << " Gains: "
0550                        << "\n"
0551                        << " g1 = " << theGains[1] << "\n"
0552                        << " g2 = " << theGains[2] << "\n"
0553                        << " g3 = " << theGains[3];
0554 
0555   delete defaultRatios;
0556 
0557   const double EBscale((agc->getEBValue()) * theGains[1] * (m_Coder->MAXADC) * m_EBs25notCont);
0558 
0559   LogDebug("EcalDigi") << " GeV/ADC = " << agc->getEBValue() << "\n"
0560                        << " notCont = " << m_EBs25notCont << "\n"
0561                        << " saturation for EB = " << EBscale << ", " << m_EBs25notCont;
0562 
0563   const double EEscale((agc->getEEValue()) * theGains[1] * (m_Coder->MAXADC) * m_EEs25notCont);
0564 
0565   LogDebug("EcalDigi") << " GeV/ADC = " << agc->getEEValue() << "\n"
0566                        << " notCont = " << m_EEs25notCont << "\n"
0567                        << " saturation for EB = " << EEscale << ", " << m_EEs25notCont;
0568 
0569   m_Coder->setFullScaleEnergy(EBscale, EEscale);
0570   if (nullptr != m_APDCoder)
0571     m_APDCoder->setFullScaleEnergy(EBscale, EEscale);
0572   if (nullptr != m_ComponentCoder)
0573     m_ComponentCoder->setFullScaleEnergy(EBscale, EEscale);
0574 
0575   if (m_doES) {
0576     // ES condition objects
0577     const ESGain *esgain = &eventSetup.getData(m_esGainToken);
0578     const ESPedestals *espeds = &eventSetup.getData(m_esPedestalsToken);
0579     const ESIntercalibConstants *esmips = &eventSetup.getData(m_esMIPsToken);
0580     const ESMIPToGeVConstant *esMipToGeV = &eventSetup.getData(m_esMIPToGeVToken);
0581     const int ESGain(1.1 > esgain->getESGain() ? 1 : 2);
0582     const double ESMIPToGeV((1 == ESGain) ? esMipToGeV->getESValueLow() : esMipToGeV->getESValueHigh());
0583 
0584     m_ESShape.setGain(ESGain);
0585     if (!m_doFastES) {
0586       m_ESElectronicsSim->setGain(ESGain);
0587       m_ESElectronicsSim->setPedestals(espeds);
0588       m_ESElectronicsSim->setMIPs(esmips);
0589       m_ESElectronicsSim->setMIPToGeV(ESMIPToGeV);
0590     } else {
0591       m_ESDigitizer->setGain(ESGain);
0592       m_ESElectronicsSimFast->setPedestals(espeds);
0593       m_ESElectronicsSimFast->setMIPs(esmips);
0594       m_ESElectronicsSimFast->setMIPToGeV(ESMIPToGeV);
0595     }
0596   }
0597 }
0598 
0599 void EcalDigiProducer::checkGeometry(const edm::EventSetup &eventSetup) {
0600   if (m_geometryWatcher.check(eventSetup)) {
0601     m_Geometry = &eventSetup.getData(m_geometryToken);
0602     updateGeometry();
0603   }
0604 }
0605 
0606 void EcalDigiProducer::updateGeometry() {
0607   if (m_doEB) {
0608     if (nullptr != m_APDResponse)
0609       m_APDResponse->setGeometry(m_Geometry->getSubdetectorGeometry(DetId::Ecal, EcalBarrel));
0610     if (nullptr != m_ComponentResponse)
0611       m_ComponentResponse->setGeometry(m_Geometry->getSubdetectorGeometry(DetId::Ecal, EcalBarrel));
0612     m_EBResponse->setGeometry(m_Geometry->getSubdetectorGeometry(DetId::Ecal, EcalBarrel));
0613   }
0614   if (m_doEE) {
0615     m_EEResponse->setGeometry(m_Geometry->getSubdetectorGeometry(DetId::Ecal, EcalEndcap));
0616   }
0617   if (m_doES) {
0618     m_ESResponse->setGeometry(m_Geometry->getSubdetectorGeometry(DetId::Ecal, EcalPreshower));
0619     m_ESOldResponse->setGeometry(m_Geometry);
0620 
0621     const std::vector<DetId> *theESDets(
0622         nullptr != m_Geometry->getSubdetectorGeometry(DetId::Ecal, EcalPreshower)
0623             ? &m_Geometry->getSubdetectorGeometry(DetId::Ecal, EcalPreshower)->getValidDetIds()
0624             : nullptr);
0625 
0626     if (!m_doFastES) {
0627       if (nullptr != m_ESOldDigitizer && nullptr != theESDets)
0628         m_ESOldDigitizer->setDetIds(*theESDets);
0629     } else {
0630       if (nullptr != m_ESDigitizer && nullptr != theESDets)
0631         m_ESDigitizer->setDetIds(*theESDets);
0632     }
0633   }
0634 }
0635 
0636 void EcalDigiProducer::setEBNoiseSignalGenerator(EcalBaseSignalGenerator *noiseGenerator) {
0637   // noiseGenerator->setParameterMap(theParameterMap);
0638   if (nullptr != m_BarrelDigitizer)
0639     m_BarrelDigitizer->setNoiseSignalGenerator(noiseGenerator);
0640 }
0641 
0642 void EcalDigiProducer::setEENoiseSignalGenerator(EcalBaseSignalGenerator *noiseGenerator) {
0643   // noiseGenerator->setParameterMap(theParameterMap);
0644   if (nullptr != m_EndcapDigitizer)
0645     m_EndcapDigitizer->setNoiseSignalGenerator(noiseGenerator);
0646 }
0647 
0648 void EcalDigiProducer::setESNoiseSignalGenerator(EcalBaseSignalGenerator *noiseGenerator) {
0649   // noiseGenerator->setParameterMap(theParameterMap);
0650   if (nullptr != m_ESDigitizer)
0651     m_ESDigitizer->setNoiseSignalGenerator(noiseGenerator);
0652 }