CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/SimTracker/SiPixelDigitizer/plugins/SiPixelDigitizer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    SiPixelDigitizer
00004 // Class:      SiPixelDigitizer
00005 // 
00013 //
00014 // Original Author:  Michele Pioppi-INFN perugia
00015 //   Modifications: Freya Blekman - Cornell University
00016 //         Created:  Mon Sep 26 11:08:32 CEST 2005
00017 // $Id: SiPixelDigitizer.cc,v 1.9 2013/01/06 19:27:01 dlange Exp $
00018 //
00019 //
00020 
00021 
00022 // system include files
00023 #include <memory>
00024 #include <set>
00025 
00026 // user include files
00027 #include "SiPixelDigitizer.h"
00028 #include "SiPixelDigitizerAlgorithm.h"
00029 
00030 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
00031 #include "DataFormats/Common/interface/Handle.h"
00032 #include "FWCore/Framework/interface/EDProducer.h"
00033 #include "FWCore/Framework/interface/ESHandle.h"
00034 #include "FWCore/Framework/interface/EventSetup.h"
00035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00036 #include "DataFormats/SiPixelDigi/interface/PixelDigi.h"
00037 #include "SimDataFormats/TrackerDigiSimLink/interface/PixelDigiSimLink.h"
00038 #include "DataFormats/Common/interface/DetSet.h"
00039 #include "DataFormats/Common/interface/DetSetVector.h"
00040 #include "DataFormats/SiPixelDigi/interface/PixelDigi.h"
00041 #include "DataFormats/SiPixelDigi/interface/PixelDigiCollection.h"
00042 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00043 #include "DataFormats/GeometryVector/interface/LocalPoint.h"
00044 #include "DataFormats/GeometryVector/interface/LocalVector.h"
00045 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00046 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00047 #include "Geometry/TrackerGeometryBuilder/interface/PixelGeomDetUnit.h"
00048 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00049 
00050 #include "Geometry/CommonTopologies/interface/PixelTopology.h"
00051 #include "Geometry/TrackerGeometryBuilder/interface/PixelGeomDetType.h"
00052 
00053 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
00054 // user include files
00055 #include "FWCore/Framework/interface/Frameworkfwd.h"
00056 #include "FWCore/Framework/interface/EDAnalyzer.h"
00057 
00058 #include "FWCore/Framework/interface/Event.h"
00059 #include "FWCore/Framework/interface/MakerMacros.h"
00060 
00061 #include "MagneticField/Engine/interface/MagneticField.h"
00062 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00063 
00064 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00065 #include "SimGeneral/MixingModule/interface/PileUpEventPrincipal.h"
00066 
00067 //Random Number
00068 #include "FWCore/ServiceRegistry/interface/Service.h"
00069 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00070 #include "FWCore/Utilities/interface/Exception.h"
00071 #include "CLHEP/Random/RandomEngine.h"
00072 
00073 //
00074 // constants, enums and typedefs
00075 //
00076 
00077 //
00078 // static data member definitions
00079 //
00080 
00081 //
00082 // constructors and destructor
00083 //
00084 //using namespace std;
00085 
00086 
00087 namespace cms
00088 {
00089   SiPixelDigitizer::SiPixelDigitizer(const edm::ParameterSet& iConfig, edm::EDProducer& mixMod):
00090     first(true),
00091     _pixeldigialgo(),
00092     hitsProducer(iConfig.getParameter<std::string>("hitsProducer")),
00093     trackerContainers(iConfig.getParameter<std::vector<std::string> >("ROUList")),
00094     geometryType(iConfig.getParameter<std::string>("GeometryType"))
00095   {
00096     edm::LogInfo ("PixelDigitizer ") <<"Enter the Pixel Digitizer";
00097     
00098     const std::string alias ("simSiPixelDigis"); 
00099     
00100     mixMod.produces<edm::DetSetVector<PixelDigi> >().setBranchAlias(alias);
00101     mixMod.produces<edm::DetSetVector<PixelDigiSimLink> >().setBranchAlias(alias + "siPixelDigiSimLink");
00102     edm::Service<edm::RandomNumberGenerator> rng;
00103     if ( ! rng.isAvailable()) {
00104       throw cms::Exception("Configuration")
00105         << "SiPixelDigitizer requires the RandomNumberGeneratorService\n"
00106         "which is not present in the configuration file.  You must add the service\n"
00107         "in the configuration file or remove the modules that require it.";
00108     }
00109   
00110     rndEngine       = &(rng->getEngine());
00111     _pixeldigialgo.reset(new SiPixelDigitizerAlgorithm(iConfig,(*rndEngine)));
00112 
00113   }
00114   
00115   SiPixelDigitizer::~SiPixelDigitizer(){  
00116     edm::LogInfo ("PixelDigitizer ") <<"Destruct the Pixel Digitizer";
00117   }
00118 
00119 
00120   //
00121   // member functions
00122   //
00123 
00124   void
00125   SiPixelDigitizer::accumulatePixelHits(edm::Handle<std::vector<PSimHit> > hSimHits) {
00126     if(hSimHits.isValid()) {
00127        std::set<unsigned int> detIds;
00128        std::vector<PSimHit> const& simHits = *hSimHits.product();
00129        for(std::vector<PSimHit>::const_iterator it = simHits.begin(), itEnd = simHits.end(); it != itEnd; ++it) {
00130          unsigned int detId = (*it).detUnitId();
00131          if(detIds.insert(detId).second) {
00132            // The insert succeeded, so this detector element has not yet been processed.
00133            unsigned int isub = DetId(detId).subdetId();
00134            if((isub == PixelSubdetector::PixelBarrel) || (isub == PixelSubdetector::PixelEndcap)) {
00135              PixelGeomDetUnit* pixdet = detectorUnits[detId];
00136              //access to magnetic field in global coordinates
00137              GlobalVector bfield = pSetup->inTesla(pixdet->surface().position());
00138              LogDebug ("PixelDigitizer ") << "B-field(T) at " << pixdet->surface().position() << "(cm): " 
00139                                           << pSetup->inTesla(pixdet->surface().position());
00140              _pixeldigialgo->accumulateSimHits(it, itEnd, pixdet, bfield);
00141            }
00142          }
00143        }
00144     }
00145   }
00146   
00147   void
00148   SiPixelDigitizer::initializeEvent(edm::Event const& e, edm::EventSetup const& iSetup) {
00149     if(first){
00150       _pixeldigialgo->init(iSetup);
00151       first = false;
00152     }
00153     _pixeldigialgo->initializeEvent();
00154     iSetup.get<TrackerDigiGeometryRecord>().get(geometryType, pDD);
00155     iSetup.get<IdealMagneticFieldRecord>().get(pSetup);
00156 
00157     // FIX THIS! We only need to clear and (re)fill this map when the geometry type IOV changes.  Use ESWatcher to determine this.
00158     if(true) { // Replace with ESWatcher 
00159       detectorUnits.clear();
00160       for(TrackingGeometry::DetUnitContainer::const_iterator iu = pDD->detUnits().begin(); iu != pDD->detUnits().end(); ++iu) {
00161         unsigned int detId = (*iu)->geographicalId().rawId();
00162         DetId idet=DetId(detId);
00163         unsigned int isub=idet.subdetId();
00164         if((isub == PixelSubdetector::PixelBarrel) || (isub == PixelSubdetector::PixelEndcap)) {  
00165           PixelGeomDetUnit* pixdet = dynamic_cast<PixelGeomDetUnit*>((*iu));
00166           assert(pixdet != 0);
00167           detectorUnits.insert(std::make_pair(detId, pixdet));
00168         }
00169       }
00170     }
00171   }
00172 
00173   void
00174   SiPixelDigitizer::accumulate(edm::Event const& iEvent, edm::EventSetup const& iSetup) {
00175     // Step A: Get Inputs
00176     for(vstring::const_iterator i = trackerContainers.begin(), iEnd = trackerContainers.end(); i != iEnd; ++i) {
00177       edm::Handle<std::vector<PSimHit> > simHits;
00178       edm::InputTag tag(hitsProducer, *i);
00179 
00180       iEvent.getByLabel(tag, simHits);
00181       accumulatePixelHits(simHits);
00182     }
00183   }
00184 
00185   void
00186   SiPixelDigitizer::accumulate(PileUpEventPrincipal const& iEvent, edm::EventSetup const& iSetup) {
00187     // Step A: Get Inputs
00188     for(vstring::const_iterator i = trackerContainers.begin(), iEnd = trackerContainers.end(); i != iEnd; ++i) {
00189       edm::Handle<std::vector<PSimHit> > simHits;
00190       edm::InputTag tag(hitsProducer, *i);
00191 
00192       iEvent.getByLabel(tag, simHits);
00193       accumulatePixelHits(simHits);
00194     }
00195   }
00196 
00197   // ------------ method called to produce the data  ------------
00198   void
00199   SiPixelDigitizer::finalizeEvent(edm::Event& iEvent, const edm::EventSetup& iSetup) {
00200 
00201     edm::ESHandle<TrackerTopology> tTopoHand;
00202     iSetup.get<IdealGeometryRecord>().get(tTopoHand);
00203     const TrackerTopology *tTopo=tTopoHand.product();
00204 
00205     std::vector<edm::DetSet<PixelDigi> > theDigiVector;
00206     std::vector<edm::DetSet<PixelDigiSimLink> > theDigiLinkVector;
00207 
00208     for(TrackingGeometry::DetUnitContainer::const_iterator iu = pDD->detUnits().begin(); iu != pDD->detUnits().end(); iu ++){
00209       DetId idet=DetId((*iu)->geographicalId().rawId());
00210       unsigned int isub=idet.subdetId();
00211       
00212       if((isub == PixelSubdetector::PixelBarrel) || (isub == PixelSubdetector::PixelEndcap)) {  
00213         
00214         //
00215         
00216         edm::DetSet<PixelDigi> collector((*iu)->geographicalId().rawId());
00217         edm::DetSet<PixelDigiSimLink> linkcollector((*iu)->geographicalId().rawId());
00218         
00219         
00220         _pixeldigialgo->digitize(dynamic_cast<PixelGeomDetUnit*>((*iu)),
00221                                  collector.data,
00222                                  linkcollector.data,
00223                                  tTopo);
00224         if(collector.data.size() > 0) {
00225           theDigiVector.push_back(std::move(collector));
00226         }
00227         if(linkcollector.data.size() > 0) {
00228           theDigiLinkVector.push_back(std::move(linkcollector));
00229         }
00230       }
00231     }
00232     
00233     // Step C: create collection with the cache vector of DetSet 
00234     std::auto_ptr<edm::DetSetVector<PixelDigi> > 
00235       output(new edm::DetSetVector<PixelDigi>(theDigiVector) );
00236     std::auto_ptr<edm::DetSetVector<PixelDigiSimLink> > 
00237       outputlink(new edm::DetSetVector<PixelDigiSimLink>(theDigiLinkVector) );
00238 
00239     // Step D: write output to file 
00240     iEvent.put(output);
00241     iEvent.put(outputlink);
00242   }
00243 
00244 
00245 
00246 
00247 }// end namespace cms::
00248