CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/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 
00016 template<class Traits>
00017 class CaloTDigitizer
00018 {
00019 public:
00023   typedef typename Traits::ElectronicsSim ElectronicsSim;
00024   typedef typename Traits::Digi Digi;
00025   typedef typename Traits::DigiCollection DigiCollection;
00026 
00027   CaloTDigitizer(CaloHitResponse * hitResponse, ElectronicsSim * electronicsSim, bool addNoise)
00028   :  theHitResponse(hitResponse),
00029      theNoiseHitGenerator(0),
00030      theNoiseSignalGenerator(0),
00031      theElectronicsSim(electronicsSim),
00032      theDetIds(0),
00033      addNoise_(addNoise)
00034   {
00035   }
00036 
00037 
00039   ~CaloTDigitizer() {}
00040 
00042   const std::vector<DetId>&  detIds() const {assert( 0 != theDetIds ) ; return *theDetIds;}
00043   void setDetIds(const std::vector<DetId> & detIds) {theDetIds = &detIds;}
00044 
00045   void setNoiseHitGenerator(CaloVNoiseHitGenerator * generator) 
00046   {
00047     theNoiseHitGenerator = generator;
00048   }
00049 
00050   void setNoiseSignalGenerator(CaloVNoiseSignalGenerator * generator)
00051   {
00052     theNoiseSignalGenerator = generator;
00053   }
00054 
00055   void setRandomEngine(CLHEP::HepRandomEngine & engine)
00056   {
00057     theHitResponse->setRandomEngine(engine);
00058     theElectronicsSim->setRandomEngine(engine);
00059   }
00060 
00062   void run(MixCollection<PCaloHit> & input, DigiCollection & output) {
00063     assert(theDetIds->size() != 0);
00064 
00065     if(theNoiseHitGenerator != 0) addNoiseHits();
00066     if(theNoiseSignalGenerator != 0) addNoiseSignals();
00067 
00068     theHitResponse->run(input);
00069     theElectronicsSim->newEvent();
00070 
00071     // reserve space for how many digis we expect
00072     int nDigisExpected = addNoise_ ? theDetIds->size() : theHitResponse->nSignals();
00073     output.reserve(nDigisExpected);
00074 
00075     // make a raw digi for evey cell
00076     for(std::vector<DetId>::const_iterator idItr = theDetIds->begin();
00077         idItr != theDetIds->end(); ++idItr)
00078     {
00079        Digi digi(*idItr);
00080        CaloSamples * analogSignal = theHitResponse->findSignal(*idItr);
00081        bool needToDeleteSignal = false;
00082        // don't bother digitizing if no signal and no noise
00083        if(analogSignal == 0 && addNoise_) {
00084          // I guess we need to make a blank signal for this cell.
00085          // Don't bother storing it anywhere.
00086          analogSignal = new CaloSamples(theHitResponse->makeBlankSignal(*idItr));
00087          needToDeleteSignal = true;
00088        }
00089        if(analogSignal != 0) { 
00090          theElectronicsSim->analogToDigital(*analogSignal , digi);
00091          output.push_back(digi);
00092          if(needToDeleteSignal) delete analogSignal;
00093       }
00094     }
00095 
00096     // free up some memory
00097     theHitResponse->clear();
00098   }
00099 
00100 
00101   void addNoiseHits()
00102   {
00103     std::vector<PCaloHit> noiseHits;
00104     theNoiseHitGenerator->getNoiseHits(noiseHits);
00105     for(std::vector<PCaloHit>::const_iterator hitItr = noiseHits.begin(),
00106         hitEnd = noiseHits.end(); hitItr != hitEnd; ++hitItr)
00107     {
00108       theHitResponse->add(*hitItr);
00109     }
00110   }
00111 
00112   void addNoiseSignals()
00113   {
00114     std::vector<CaloSamples> noiseSignals;
00115     // noise signals need to be in units of photoelectrons.  Fractional is OK
00116     theNoiseSignalGenerator->fillEvent();
00117     theNoiseSignalGenerator->getNoiseSignals(noiseSignals);
00118     for(std::vector<CaloSamples>::const_iterator signalItr = noiseSignals.begin(),
00119         signalEnd = noiseSignals.end(); signalItr != signalEnd; ++signalItr)
00120     {
00121       theHitResponse->add(*signalItr);
00122     }
00123   }
00124 
00125 private:
00126   CaloHitResponse * theHitResponse;
00127   CaloVNoiseHitGenerator * theNoiseHitGenerator;
00128   CaloVNoiseSignalGenerator * theNoiseSignalGenerator;
00129   ElectronicsSim * theElectronicsSim;
00130   const std::vector<DetId>* theDetIds;
00131   bool addNoise_;
00132 };
00133 
00134 #endif
00135