CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/SimCalorimetry/CaloSimAlgos/interface/CaloTDigitizer.h

Go to the documentation of this file.
00001 #ifndef CaloSimAlgos_CaloTDigitizer_h
00002 #define CaloSimAlgos_CaloTDigitizer_h
00003 
00009 #include "SimCalorimetry/CaloSimAlgos/interface/CaloHitResponse.h"
00010 #include "SimCalorimetry/CaloSimAlgos/interface/CaloVNoiseHitGenerator.h"
00011 #include "SimCalorimetry/CaloSimAlgos/interface/CaloVNoiseSignalGenerator.h"
00012 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
00013 #include "SimDataFormats/CaloHit/interface/PCaloHit.h"
00014 #include <cassert>
00015 #include <vector>
00016 
00017 template<class Traits>
00018 class CaloTDigitizer
00019 {
00020 public:
00024   typedef typename Traits::ElectronicsSim ElectronicsSim;
00025   typedef typename Traits::Digi Digi;
00026   typedef typename Traits::DigiCollection DigiCollection;
00027 
00028   CaloTDigitizer(CaloHitResponse * hitResponse, ElectronicsSim * electronicsSim, bool addNoise)
00029   :  theHitResponse(hitResponse),
00030      theNoiseHitGenerator(0),
00031      theNoiseSignalGenerator(0),
00032      theElectronicsSim(electronicsSim),
00033      theDetIds(0),
00034      addNoise_(addNoise)
00035   {
00036   }
00037 
00038 
00040   ~CaloTDigitizer() {}
00041 
00043   const std::vector<DetId>&  detIds() const {assert( 0 != theDetIds ) ; return *theDetIds;}
00044   void setDetIds(const std::vector<DetId> & detIds) {theDetIds = &detIds;}
00045 
00046   void setNoiseHitGenerator(CaloVNoiseHitGenerator * generator) 
00047   {
00048     theNoiseHitGenerator = generator;
00049   }
00050 
00051   void setNoiseSignalGenerator(CaloVNoiseSignalGenerator * generator)
00052   {
00053     theNoiseSignalGenerator = generator;
00054   }
00055 
00056   void setRandomEngine(CLHEP::HepRandomEngine & engine)
00057   {
00058     theHitResponse->setRandomEngine(engine);
00059     theElectronicsSim->setRandomEngine(engine);
00060   }
00061 
00062   void add(const std::vector<PCaloHit> & hits, int bunchCrossing) {
00063     if(theHitResponse->withinBunchRange(bunchCrossing)) {
00064       for(std::vector<PCaloHit>::const_iterator it = hits.begin(), itEnd = hits.end(); it != itEnd; ++it) {
00065         theHitResponse->add(*it);
00066       }
00067     }
00068   }
00069 
00070   void initializeHits() {
00071      theHitResponse->initializeHits();
00072   }
00073 
00075   void run(MixCollection<PCaloHit> &, DigiCollection &) {
00076     assert(0);
00077   }
00078 
00080   void run(DigiCollection & output) {
00081     theHitResponse->finalizeHits();
00082 
00083     assert(theDetIds->size() != 0);
00084 
00085     if(theNoiseHitGenerator != 0) addNoiseHits();
00086     if(theNoiseSignalGenerator != 0) addNoiseSignals();
00087 
00088     theElectronicsSim->newEvent();
00089 
00090     // reserve space for how many digis we expect
00091     int nDigisExpected = addNoise_ ? theDetIds->size() : theHitResponse->nSignals();
00092     output.reserve(nDigisExpected);
00093 
00094     // make a raw digi for evey cell
00095     for(std::vector<DetId>::const_iterator idItr = theDetIds->begin();
00096         idItr != theDetIds->end(); ++idItr)
00097     {
00098        Digi digi(*idItr);
00099        CaloSamples * analogSignal = theHitResponse->findSignal(*idItr);
00100        bool needToDeleteSignal = false;
00101        // don't bother digitizing if no signal and no noise
00102        if(analogSignal == 0 && addNoise_) {
00103          // I guess we need to make a blank signal for this cell.
00104          // Don't bother storing it anywhere.
00105          analogSignal = new CaloSamples(theHitResponse->makeBlankSignal(*idItr));
00106          needToDeleteSignal = true;
00107        }
00108        if(analogSignal != 0) { 
00109          theElectronicsSim->analogToDigital(*analogSignal , digi);
00110          output.push_back(std::move(digi));
00111          if(needToDeleteSignal) delete analogSignal;
00112       }
00113     }
00114 
00115     // free up some memory
00116     theHitResponse->clear();
00117   }
00118 
00119 
00120   void addNoiseHits()
00121   {
00122     std::vector<PCaloHit> noiseHits;
00123     theNoiseHitGenerator->getNoiseHits(noiseHits);
00124     for(std::vector<PCaloHit>::const_iterator hitItr = noiseHits.begin(),
00125         hitEnd = noiseHits.end(); hitItr != hitEnd; ++hitItr)
00126     {
00127       theHitResponse->add(*hitItr);
00128     }
00129   }
00130 
00131   void addNoiseSignals()
00132   {
00133     std::vector<CaloSamples> noiseSignals;
00134     // noise signals need to be in units of photoelectrons.  Fractional is OK
00135     theNoiseSignalGenerator->fillEvent();
00136     theNoiseSignalGenerator->getNoiseSignals(noiseSignals);
00137     for(std::vector<CaloSamples>::const_iterator signalItr = noiseSignals.begin(),
00138         signalEnd = noiseSignals.end(); signalItr != signalEnd; ++signalItr)
00139     {
00140       theHitResponse->add(*signalItr);
00141     }
00142   }
00143 
00144 private:
00145   CaloHitResponse * theHitResponse;
00146   CaloVNoiseHitGenerator * theNoiseHitGenerator;
00147   CaloVNoiseSignalGenerator * theNoiseSignalGenerator;
00148   ElectronicsSim * theElectronicsSim;
00149   const std::vector<DetId>* theDetIds;
00150   bool addNoise_;
00151 };
00152 
00153 #endif
00154