CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/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.6 2009/11/13 14:14:22 fambrogl Exp $
00018 //
00019 //
00020 
00021 
00022 // system include files
00023 #include <memory>
00024 // user include files
00025 #include "SimTracker/SiPixelDigitizer/interface/SiPixelDigitizer.h"
00026 #include "SimTracker/SiPixelDigitizer/interface/SiPixelDigitizerAlgorithm.h"
00027 
00028 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
00029 #include "DataFormats/Common/interface/Handle.h"
00030 #include "FWCore/Framework/interface/ESHandle.h"
00031 #include "FWCore/Framework/interface/EventSetup.h"
00032 #include "DataFormats/SiPixelDigi/interface/PixelDigi.h"
00033 #include "SimDataFormats/TrackerDigiSimLink/interface/PixelDigiSimLink.h"
00034 #include "DataFormats/Common/interface/DetSetVector.h"
00035 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00036 #include "DataFormats/GeometryVector/interface/LocalPoint.h"
00037 #include "DataFormats/GeometryVector/interface/LocalVector.h"
00038 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00039 
00040 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00041 
00042 #include "Geometry/CommonTopologies/interface/PixelTopology.h"
00043 #include "Geometry/TrackerGeometryBuilder/interface/PixelGeomDetType.h"
00044 
00045 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
00046 // user include files
00047 #include "FWCore/Framework/interface/Frameworkfwd.h"
00048 #include "FWCore/Framework/interface/EDAnalyzer.h"
00049 
00050 #include "FWCore/Framework/interface/Event.h"
00051 #include "FWCore/Framework/interface/MakerMacros.h"
00052 
00053 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00054 
00055 #include "MagneticField/Engine/interface/MagneticField.h"
00056 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00057 
00058 #include "FWCore/Framework/interface/ESHandle.h"
00059 #include "FWCore/Framework/interface/EventSetup.h"
00060 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00061 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00062 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
00063 
00064 //Random Number
00065 #include "FWCore/ServiceRegistry/interface/Service.h"
00066 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00067 #include "FWCore/Utilities/interface/Exception.h"
00068 #include "CLHEP/Random/RandomEngine.h"
00069 
00070 //
00071 // constants, enums and typedefs
00072 //
00073 
00074 //
00075 // static data member definitions
00076 //
00077 
00078 //
00079 // constructors and destructor
00080 //
00081 //using namespace std;
00082 
00083 
00084 namespace cms
00085 {
00086   SiPixelDigitizer::SiPixelDigitizer(const edm::ParameterSet& iConfig):
00087     conf_(iConfig),first(true)
00088   {
00089     edm::LogInfo ("PixelDigitizer ") <<"Enter the Pixel Digitizer";
00090     
00091     std::string alias ( iConfig.getParameter<std::string>("@module_label") ); 
00092     
00093     produces<edm::DetSetVector<PixelDigi> >().setBranchAlias( alias );
00094     produces<edm::DetSetVector<PixelDigiSimLink> >().setBranchAlias ( alias + "siPixelDigiSimLink");
00095     trackerContainers.clear();
00096     trackerContainers = iConfig.getParameter<std::vector<std::string> >("ROUList");
00097     geometryType = iConfig.getParameter<std::string>("GeometryType");
00098     edm::Service<edm::RandomNumberGenerator> rng;
00099     if ( ! rng.isAvailable()) {
00100       throw cms::Exception("Configuration")
00101         << "SiPixelDigitizer requires the RandomNumberGeneratorService\n"
00102         "which is not present in the configuration file.  You must add the service\n"
00103         "in the configuration file or remove the modules that require it.";
00104     }
00105   
00106     rndEngine       = &(rng->getEngine());
00107     _pixeldigialgo = new SiPixelDigitizerAlgorithm(iConfig,(*rndEngine));
00108 
00109   }
00110   
00111   SiPixelDigitizer::~SiPixelDigitizer(){  
00112     edm::LogInfo ("PixelDigitizer ") <<"Destruct the Pixel Digitizer";
00113     delete _pixeldigialgo;
00114   }
00115 
00116 
00117   //
00118   // member functions
00119   //
00120   
00121   // ------------ method called to produce the data  ------------
00122   void
00123   SiPixelDigitizer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00124   {
00125 
00126     if(first){
00127       _pixeldigialgo->init(iSetup);
00128       first = false;
00129     }
00130 
00131     // Step A: Get Inputs
00132     edm::Handle<CrossingFrame<PSimHit> > cf_simhit;
00133     std::vector<const CrossingFrame<PSimHit> *> cf_simhitvec;
00134     for(uint32_t i = 0; i< trackerContainers.size();i++){
00135       iEvent.getByLabel("mix",trackerContainers[i],cf_simhit);
00136       cf_simhitvec.push_back(cf_simhit.product());
00137     }
00138     
00139     std::auto_ptr<MixCollection<PSimHit> > allPixelTrackerHits(new MixCollection<PSimHit>(cf_simhitvec));
00140 
00141     edm::ESHandle<TrackerGeometry> pDD;
00142     
00143     iSetup.get<TrackerDigiGeometryRecord> ().get(geometryType,pDD);
00144  
00145     edm::ESHandle<MagneticField> pSetup;
00146     iSetup.get<IdealMagneticFieldRecord>().get(pSetup);
00147 
00148     //Loop on PSimHit
00149     SimHitMap.clear();
00150 
00151     MixCollection<PSimHit>::iterator isim;
00152     for (isim=allPixelTrackerHits->begin(); isim!= allPixelTrackerHits->end();isim++) {
00153       DetId detid=DetId((*isim).detUnitId());
00154       unsigned int subid=detid.subdetId();
00155       if ((subid==  PixelSubdetector::PixelBarrel) || (subid== PixelSubdetector::PixelEndcap)) {
00156         SimHitMap[(*isim).detUnitId()].push_back((*isim));
00157       }
00158     }
00159 
00160     // Step B: LOOP on PixelGeomDetUnit //
00161     for(TrackingGeometry::DetUnitContainer::const_iterator iu = pDD->detUnits().begin(); iu != pDD->detUnits().end(); iu ++){
00162       DetId idet=DetId((*iu)->geographicalId().rawId());
00163       unsigned int isub=idet.subdetId();
00164       
00165       
00166       if  ((isub==  PixelSubdetector::PixelBarrel) || (isub== PixelSubdetector::PixelEndcap)) {  
00167         
00168         
00169         //access to magnetic field in global coordinates
00170         GlobalVector bfield=pSetup->inTesla((*iu)->surface().position());
00171         LogDebug ("PixelDigitizer ") << "B-field(T) at "<<(*iu)->surface().position()<<"(cm): " 
00172                                      << pSetup->inTesla((*iu)->surface().position());
00173         //
00174         
00175         edm::DetSet<PixelDigi> collector((*iu)->geographicalId().rawId());
00176         edm::DetSet<PixelDigiSimLink> linkcollector((*iu)->geographicalId().rawId());
00177         
00178         
00179         collector.data=
00180           _pixeldigialgo->run(SimHitMap[(*iu)->geographicalId().rawId()],
00181                              dynamic_cast<PixelGeomDetUnit*>((*iu)),
00182                              bfield);
00183         if (collector.data.size()>0){
00184           theDigiVector.push_back(collector);
00185           
00186           //digisimlink
00187           if(SimHitMap[(*iu)->geographicalId().rawId()].size()>0){
00188               linkcollector.data=_pixeldigialgo->make_link();
00189               if (linkcollector.data.size()>0) theDigiLinkVector.push_back(linkcollector);
00190           }
00191           
00192         }
00193       }
00194       
00195     }
00196     
00197     // Step C: create collection with the cache vector of DetSet 
00198     std::auto_ptr<edm::DetSetVector<PixelDigi> > 
00199       output(new edm::DetSetVector<PixelDigi>(theDigiVector) );
00200     std::auto_ptr<edm::DetSetVector<PixelDigiSimLink> > 
00201       outputlink(new edm::DetSetVector<PixelDigiSimLink>(theDigiLinkVector) );
00202 
00203     // Step D: write output to file 
00204     iEvent.put(output);
00205     iEvent.put(outputlink);
00206   }
00207 
00208 
00209 
00210 
00211 }// end namespace cms::
00212