CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/SimCalorimetry/EcalTestBeam/src/EcalTBDigiProducer.cc

Go to the documentation of this file.
00001 
00002 #include "SimCalorimetry/EcalTestBeam/interface/EcalTBDigiProducer.h"
00003 #include "DataFormats/Common/interface/EDProduct.h"
00004 #include "FWCore/Framework/interface/ESHandle.h"
00005 #include "DataFormats/Common/interface/Handle.h"
00006 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "DataFormats/Provenance/interface/Provenance.h"
00009 #include "SimDataFormats/EcalTestBeam/interface/PEcalTBInfo.h"
00010 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
00011 #include "SimCalorimetry/CaloSimAlgos/interface/CaloDigiCollectionSorter.h"
00012 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00013 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00014 #include "CondFormats/EcalObjects/interface/EcalPedestals.h"
00015 #include "CondFormats/DataRecord/interface/EcalPedestalsRcd.h"
00016 #include "CondFormats/EcalObjects/interface/EcalIntercalibConstantsMC.h"
00017 #include "CondFormats/DataRecord/interface/EcalIntercalibConstantsMCRcd.h"
00018 #include "CondFormats/EcalObjects/interface/EcalADCToGeVConstant.h"
00019 #include "CondFormats/DataRecord/interface/EcalADCToGeVConstantRcd.h"
00020 #include "CondFormats/EcalObjects/interface/EcalGainRatios.h"
00021 #include "CondFormats/DataRecord/interface/EcalGainRatiosRcd.h"
00022 
00023 
00024 EcalTBDigiProducer::EcalTBDigiProducer(const edm::ParameterSet& params)
00025 {
00026 
00028 
00029   EBdigiCollection_ = params.getParameter<std::string>("EBdigiCollection");
00030   EEdigiCollection_ = params.getParameter<std::string>("EEdigiCollection");
00031 
00032   const std::string hitsProducer (
00033      params.getParameter<std::string>("hitsProducer") ) ;
00034   
00035   m_barrelHitsName = hitsProducer + "EcalHitsEB" ;
00036   m_endcapHitsName = hitsProducer + "EcalHitsEE" ;
00037 
00038   produces<EBDigiCollection>( EBdigiCollection_ );
00039   produces<EEDigiCollection>( EEdigiCollection_ );
00040 
00041   produces<EcalTBTDCRawInfo>(); //For TB
00042 
00043   // initialize the default valuer for hardcoded parameters and the EB/EE shape
00044 
00045   bool addNoise = params.getParameter<bool>("doNoise"); 
00046   double simHitToPhotoelectronsBarrel = params.getParameter<double>("simHitToPhotoelectronsBarrel");
00047   double simHitToPhotoelectronsEndcap = params.getParameter<double>("simHitToPhotoelectronsEndcap");
00048   double photoelectronsToAnalogBarrel = params.getParameter<double>("photoelectronsToAnalogBarrel");
00049   double photoelectronsToAnalogEndcap = params.getParameter<double>("photoelectronsToAnalogEndcap");
00050   double samplingFactor = params.getParameter<double>("samplingFactor");
00051   double timePhase = params.getParameter<double>("timePhase");
00052   int readoutFrameSize = params.getParameter<int>("readoutFrameSize");
00053   int binOfMaximum = params.getParameter<int>("binOfMaximum");
00054   bool doPhotostatistics = params.getParameter<bool>("doPhotostatistics");
00055   bool syncPhase = params.getParameter<bool>("syncPhase");
00056 
00057   // possible phase shift for asynchronous trigger (e.g. test-beam)
00058 
00059   doPhaseShift = !syncPhase; //For TB
00060   thisPhaseShift = 1.; //For TB
00061 
00062   theParameterMap = new EcalSimParameterMap(simHitToPhotoelectronsBarrel, simHitToPhotoelectronsEndcap, 
00063                                             photoelectronsToAnalogBarrel, photoelectronsToAnalogEndcap, 
00064                                             samplingFactor, timePhase, readoutFrameSize, binOfMaximum,
00065                                             doPhotostatistics, syncPhase);
00066 
00067   
00068   theEBResponse = new CaloHitRespoNew(theParameterMap, &theEBShape);
00069   theEEResponse = new CaloHitRespoNew(theParameterMap, &theEEShape);
00070 
00071   EcalCorrMatrix thisMatrix;
00072 
00073   std::vector<double> corrNoiseMatrix = params.getParameter< std::vector<double> >("CorrelatedNoiseMatrix");
00074   if ( corrNoiseMatrix.size() == (unsigned int)(readoutFrameSize*readoutFrameSize) ) {
00075     for ( int row = 0 ; row < readoutFrameSize; ++row ) {
00076       for ( int column = 0 ; column < readoutFrameSize; ++column ) {
00077         int index = column + readoutFrameSize*row;
00078         thisMatrix(row,column) = corrNoiseMatrix[index];
00079       }
00080     }
00081   }
00082   theNoiseMatrix = new EcalCorrMatrix(thisMatrix);
00083 
00084   theCorrNoise = new CorrelatedNoisifier<EcalCorrMatrix>(thisMatrix);
00085 
00086   theCoder = new EcalCoder(addNoise, theCorrNoise);
00087   bool applyConstantTerm = params.getParameter<bool>("applyConstantTerm");
00088   double rmsConstantTerm = params.getParameter<double> ("ConstantTerm");
00089   theElectronicsSim = new EcalElectronicsSim(theParameterMap, theCoder, applyConstantTerm, rmsConstantTerm);
00090 
00091 
00092   theBarrelDigitizer = new EBDigitizer( theEBResponse, 
00093                                         theElectronicsSim, 
00094                                         addNoise           );
00095 
00096   theEndcapDigitizer = new EEDigitizer( theEEResponse, 
00097                                         theElectronicsSim, 
00098                                         addNoise           );
00099 
00100 
00101   // not containment corrections
00102   EBs25notCont = params.getParameter<double>("EBs25notContainment");
00103   EEs25notCont = params.getParameter<double>("EEs25notContainment");
00104 
00105 //For TB --------------------------------------
00106 
00108 
00109   typedef std::vector< edm::ParameterSet > Parameters;
00110   Parameters ranges=params.getParameter<Parameters>("tdcRanges");
00111   for(Parameters::iterator itRanges = ranges.begin(); itRanges != ranges.end(); ++itRanges) 
00112     {
00113       EcalTBTDCRecInfoAlgo::EcalTBTDCRanges aRange;
00114       aRange.runRanges.first = itRanges->getParameter<int>("startRun");
00115       aRange.runRanges.second = itRanges->getParameter<int>("endRun");
00116       aRange.tdcMin = itRanges->getParameter< std::vector<double> >("tdcMin");
00117       aRange.tdcMax = itRanges->getParameter< std::vector<double> >("tdcMax");
00118       tdcRanges.push_back(aRange);
00119     }
00120 
00121   use2004OffsetConvention_ = params.getUntrackedParameter< bool >("use2004OffsetConvention",false);
00122 
00123   ecalTBInfoLabel = params.getUntrackedParameter<std::string>("EcalTBInfoLabel","SimEcalTBG4Object");
00124   doReadout = params.getParameter<bool>("doReadout");
00125 
00126   theTBReadout = new EcalTBReadout(ecalTBInfoLabel);
00127 
00128   tunePhaseShift =  params.getParameter<double>("tunePhaseShift");
00129 
00130 //For TB --------------------------------------
00131 }
00132 
00133 
00134 EcalTBDigiProducer::~EcalTBDigiProducer() 
00135 {
00136   if (theParameterMap)  { delete theParameterMap; }
00137   if (theEBResponse)  { delete theEBResponse; }
00138   if (theEEResponse)  { delete theEEResponse; }
00139   if (theCorrNoise)     { delete theCorrNoise; }
00140   if (theNoiseMatrix)   { delete theNoiseMatrix; }
00141   if (theCoder)         { delete theCoder; }
00142   if (theElectronicsSim){ delete theElectronicsSim; }
00143   if (theBarrelDigitizer){ delete theBarrelDigitizer; }
00144   if (theEndcapDigitizer){ delete theEndcapDigitizer; }
00145 }
00146 
00147 void EcalTBDigiProducer::produce( edm::Event&            event      ,
00148                                   const edm::EventSetup& eventSetup   ) 
00149 {
00150    // Step A: Get Inputs
00151 
00152 //For TB ----------------
00153    edm::ESHandle<CaloGeometry>               hGeometry ;
00154    eventSetup.get<CaloGeometryRecord>().get( hGeometry ) ;
00155    theEBResponse->setGeometry( 
00156       hGeometry->getSubdetectorGeometry( DetId::Ecal, EcalBarrel    ) ) ;
00157    theEEResponse->setGeometry( 
00158       hGeometry->getSubdetectorGeometry( DetId::Ecal, EcalEndcap    ) ) ;
00159 
00160    // takes no time because gives back const ref
00161    const std::vector<DetId>& theBarrelDets (
00162       hGeometry->getValidDetIds(DetId::Ecal, EcalBarrel) ) ;
00163 //   const std::vector<DetId>& theEndcapDets (
00164 //      hGeometry->getValidDetIds(DetId::Ecal, EcalEndcap) ) ;
00165 
00166 //   theBarrelDigitizer->setDetIds( theBarrelDets ) ;
00167 //   theEndcapDigitizer->setDetIds( theEndcapDets ) ;
00168    theTBReadout      ->setDetIds( theBarrelDets ) ;
00169 
00170 //For TB ----------------
00171 
00172    checkCalibrations(eventSetup);
00173 
00174    // Get input
00175    edm::Handle<CrossingFrame<PCaloHit> >      crossingFrame;
00176    event.getByLabel( "mix", m_barrelHitsName, crossingFrame ) ;
00177 
00178    MixCollection<PCaloHit>* EBHits ( crossingFrame.isValid() ?
00179                                      new MixCollection<PCaloHit>( crossingFrame.product() ) : 0 ) ;
00180 
00181    const bool isEB ( 0 != EBHits &&
00182                      0 != EBHits->size() ) ;
00183 
00184    event.getByLabel( "mix", m_endcapHitsName, crossingFrame ) ;
00185    MixCollection<PCaloHit>* EEHits ( crossingFrame.isValid() ?
00186                                      new MixCollection<PCaloHit>( crossingFrame.product() ) : 0 ) ;
00187 
00188    const bool isEE ( 0 != EEHits &&
00189                      0 != EEHits->size() ) ;
00190 
00191 //For TB ----------------------------------------  
00192    std::auto_ptr<EcalTBTDCRawInfo> TDCproduct(new EcalTBTDCRawInfo(1));
00193    if( doPhaseShift ) 
00194    {
00195       edm::Handle<PEcalTBInfo>           theEcalTBInfo   ;
00196       event.getByLabel( ecalTBInfoLabel, theEcalTBInfo ) ;
00197       thisPhaseShift = theEcalTBInfo->phaseShift();
00198     
00199       DetId detId( DetId::Ecal, 1 ) ;
00200       setPhaseShift( detId );
00201 
00202       fillTBTDCRawInfo( *TDCproduct ) ; // fill the TDC info in the event
00203     
00204   }
00205 //For TB ----------------------------------------  
00206 
00207   // Step B: Create empty output and then fill it
00208    std::auto_ptr<EBDigiCollection> barrelResult( new EBDigiCollection() ) ;
00209    std::auto_ptr<EEDigiCollection> endcapResult( new EEDigiCollection() ) ;
00210 
00211    if ( isEB ) 
00212    {
00213       std::auto_ptr<MixCollection<PCaloHit> >  barrelHits( EBHits );
00214       theBarrelDigitizer->run( *barrelHits, *barrelResult ) ;
00215       edm::LogInfo("DigiInfo") << "EB Digis: " << barrelResult->size();
00216       std::cout << "EB Digis: " << barrelResult->size()<<std::endl;
00217 
00218 /*
00219         CaloDigiCollectionSorter sorter(5) ;
00220 
00221         std::vector<EBDataFrame> sortedDigisEB = sorter.sortedVector(*barrelResult);
00222         LogDebug("EcalDigi") << "Top 10 EB digis";
00223         std::cout<< "Top 10 EB digis\n";
00224         for(int i = 0; i < std::min(10,(int) sortedDigisEB.size()); ++i) 
00225         {
00226            LogDebug("EcalDigi") << sortedDigisEB[i];
00227            std::cout << sortedDigisEB[i]<<"\n";
00228         }
00229         std::cout<< std::endl ;
00230 */      
00231    }
00232 
00233    if( isEE ) 
00234    {
00235       std::auto_ptr<MixCollection<PCaloHit> >  endcapHits( EEHits );
00236       theEndcapDigitizer->run( *endcapHits, *endcapResult ) ;
00237       edm::LogInfo("EcalDigi") << "EE Digis: " << endcapResult->size();
00238       std::cout << "EE Digis: " << endcapResult->size()<<std::endl;
00239    }
00240 
00241    //For TB -------------------------------------------
00242    // perform the TB readout if required, 
00243    // otherwise simply fill the proper object
00244 
00245    std::auto_ptr<EBDigiCollection> barrelReadout( new EBDigiCollection() ) ;
00246    if ( doReadout ) 
00247    {
00248       theTBReadout->performReadout( event,
00249                                     *theTTmap,
00250                                     *barrelResult,
00251                                     *barrelReadout);
00252    }
00253    else 
00254    {
00255       barrelResult->swap(*barrelReadout);
00256    }
00257 
00258   // Step D: Put outputs into event
00259 //TB  event.put(barrelResult, EBdigiCollection_);
00260    event.put( barrelReadout, EBdigiCollection_ ) ;
00261    event.put( endcapResult,  EEdigiCollection_ ) ;
00262    event.put( TDCproduct ) ;
00263 
00264 //For TB -------------------------------------------
00265 }
00266 
00267 
00268 void  EcalTBDigiProducer::checkCalibrations(const edm::EventSetup & eventSetup) 
00269 {
00270 
00271   // Pedestals from event setup
00272 
00273   edm::ESHandle<EcalPedestals> dbPed;
00274   eventSetup.get<EcalPedestalsRcd>().get( dbPed );
00275   const EcalPedestals* thePedestals=dbPed.product();
00276   
00277   theCoder->setPedestals( thePedestals );
00278 
00279   // Ecal Intercalibration Constants
00280   edm::ESHandle<EcalIntercalibConstantsMC> pIcal;
00281   eventSetup.get<EcalIntercalibConstantsMCRcd>().get(pIcal);
00282   const EcalIntercalibConstantsMC *ical = pIcal.product();
00283   
00284   theCoder->setIntercalibConstants( ical );
00285 
00286   // ADC -> GeV Scale
00287   edm::ESHandle<EcalADCToGeVConstant> pAgc;
00288   eventSetup.get<EcalADCToGeVConstantRcd>().get(pAgc);
00289   const EcalADCToGeVConstant* agc = pAgc.product();
00290   
00291   // Gain Ratios
00292   edm::ESHandle<EcalGainRatios> pRatio;
00293   eventSetup.get<EcalGainRatiosRcd>().get(pRatio);
00294   const EcalGainRatios* gr = pRatio.product();
00295 
00296   theCoder->setGainRatios( gr );
00297 
00298   EcalMGPAGainRatio * defaultRatios = new EcalMGPAGainRatio();
00299 
00300   double theGains[theCoder->NGAINS+1];
00301   theGains[0] = 0.;
00302   theGains[3] = 1.;
00303   theGains[2] = defaultRatios->gain6Over1() ;
00304   theGains[1] = theGains[2]*(defaultRatios->gain12Over6()) ;
00305 
00306   LogDebug("EcalDigi") << " Gains: " << "\n" << " g1 = " << theGains[1] << "\n" << " g2 = " << theGains[2] << "\n" << " g3 = " << theGains[3];
00307 
00308   delete defaultRatios;
00309 
00310   const double EBscale = (agc->getEBValue())*theGains[1]*(theCoder->MAXADC)*EBs25notCont;
00311   LogDebug("EcalDigi") << " GeV/ADC = " << agc->getEBValue() << "\n" << " notCont = " << EBs25notCont << "\n" << " saturation for EB = " << EBscale << ", " << EBs25notCont;
00312   const double EEscale = (agc->getEEValue())*theGains[1]*(theCoder->MAXADC)*EEs25notCont;
00313   LogDebug("EcalDigi") << " GeV/ADC = " << agc->getEEValue() << "\n" << " notCont = " << EEs25notCont << "\n" << " saturation for EB = " << EEscale << ", " << EEs25notCont;
00314   theCoder->setFullScaleEnergy( EBscale , EEscale );
00315 
00316 }
00317 
00318 /* //TB
00319 void EcalTBDigiProducer::checkGeometry(const edm::EventSetup & eventSetup) 
00320 {
00321   // TODO find a way to avoid doing this every event
00322   edm::ESHandle<CaloGeometry> hGeometry;
00323   eventSetup.get<CaloGeometryRecord>().get(hGeometry);
00324 
00325   const CaloGeometry * pGeometry = &*hGeometry;
00326   
00327   // see if we need to update
00328   if(pGeometry != theGeometry) {
00329     theGeometry = pGeometry;
00330     updateGeometry();
00331   }
00332 }
00333 
00334 void EcalTBDigiProducer::updateGeometry() {
00335   theEcalResponse->setGeometry(theGeometry);
00336 //TB  theESResponse->setGeometry(theGeometry);
00337 
00338   const std::vector<DetId>& theBarrelDets =  theGeometry->getValidDetIds(DetId::Ecal, EcalBarrel);
00339   const std::vector<DetId>& theEndcapDets =  theGeometry->getValidDetIds(DetId::Ecal, EcalEndcap);
00340 //TB  const std::vector<DetId>& theESDets     =  theGeometry->getValidDetIds(DetId::Ecal, EcalPreshower);
00341 
00342   edm::LogInfo("EcalDigi") << "deb geometry: " << "\n" 
00343                            << "\t barrel: " << theBarrelDets.size () << "\n"
00344                            << "\t endcap: " << theEndcapDets.size () << "\n";
00345 //TB                      << "\t preshower: " << theESDets.size();
00346   
00347   theBarrelDigitizer->setDetIds(theBarrelDets);
00348   theEndcapDigitizer->setDetIds(theEndcapDets);
00349 //TB  if (!doFast) { theESDigitizer->setDetIds(theESDets); }
00350 //TB  if ( doFast) { theESDigitizerFast->setDetIds(theESDets); }
00351 
00352   theTBReadout->setDetIds(theBarrelDets);
00353 }
00354 */
00355 //For TB --------------------------------------------------------
00356 
00357 void EcalTBDigiProducer::setPhaseShift(const DetId & detId) {
00358   
00359   const CaloSimParameters & parameters = theParameterMap->simParameters(detId);
00360   if ( !parameters.syncPhase() ) {
00361 
00362     int myDet = detId.subdetId();
00363 
00364     LogDebug("EcalDigi") << "Setting the phase shift " << thisPhaseShift << " and the offset " << tunePhaseShift << " for the subdetector " << myDet;
00365 
00366     if ( myDet == 1) {
00367       double passPhaseShift = thisPhaseShift+tunePhaseShift;
00368       if ( use2004OffsetConvention_ ) passPhaseShift = 1.-passPhaseShift;
00369       theEBResponse->setPhaseShift(passPhaseShift);
00370       theEEResponse->setPhaseShift(passPhaseShift);
00371     }
00372     
00373   }
00374   
00375 }
00376 
00377 void EcalTBDigiProducer::fillTBTDCRawInfo(EcalTBTDCRawInfo & theTBTDCRawInfo) {
00378 
00379   unsigned int thisChannel = 1;
00380   
00381   unsigned int thisCount = (unsigned int)(thisPhaseShift*(tdcRanges[0].tdcMax[0]-tdcRanges[0].tdcMin[0]) + tdcRanges[0].tdcMin[0]);
00382 
00383   EcalTBTDCSample theTBTDCSample(thisChannel, thisCount);
00384 
00385   unsigned int sampleIndex = 0;
00386   theTBTDCRawInfo.setSample(sampleIndex, theTBTDCSample);
00387 
00388   LogDebug("EcalDigi") << theTBTDCSample << "\n" << theTBTDCRawInfo;
00389 
00390 }
00391 //For TB --------------------------------------------------------