CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/SimGeneral/DataMixingModule/plugins/DataMixingModule.cc

Go to the documentation of this file.
00001 // File: DataMixingModule.cc
00002 // Description:  see DataMixingModule.h
00003 // Author:  Mike Hildreth, University of Notre Dame
00004 //
00005 //--------------------------------------------
00006 
00007 #include <map>
00008 #include <iostream>
00009 #include <boost/bind.hpp>
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "FWCore/Utilities/interface/EDMException.h"
00012 #include "FWCore/Framework/interface/ConstProductRegistry.h"
00013 #include "FWCore/ServiceRegistry/interface/Service.h"
00014 #include "DataFormats/Common/interface/Handle.h"
00015 #include "DataFormats/Provenance/interface/Provenance.h"
00016 #include "DataFormats/Provenance/interface/BranchDescription.h"
00017 //
00018 //
00019 #include "DataMixingModule.h"
00020 
00021 
00022 using namespace std;
00023 
00024 namespace edm
00025 {
00026 
00027   // Constructor 
00028   DataMixingModule::DataMixingModule(const edm::ParameterSet& ps) : BMixingModule(ps),
00029                                                             label_(ps.getParameter<std::string>("Label"))
00030 
00031   {                                                         // what's "label_"?
00032 
00033     // get the subdetector names
00034     this->getSubdetectorNames();  //something like this may be useful to check what we are supposed to do...
00035 
00036     // For now, list all of them here.  Later, make this selectable with input parameters
00037     // 
00038 
00039     // Check to see if we are working in Full or Fast Simulation
00040 
00041     DoFastSim_ = (ps.getParameter<std::string>("IsThisFastSim")).compare("YES") == 0;
00042     MergeEMDigis_ = (ps.getParameter<std::string>("EcalMergeType")).compare("Digis") == 0;
00043     MergeHcalDigis_ = (ps.getParameter<std::string>("HcalMergeType")).compare("Digis") == 0;
00044     if(MergeHcalDigis_) MergeHcalDigisProd_ = (ps.getParameter<std::string>("HcalDigiMerge")=="FullProd");
00045 
00046     // Put Fast Sim Sequences here for Simplification: Fewer options!
00047 
00048     if(DoFastSim_) {
00049 
00050     // declare the products to produce
00051 
00052       //Ecal:
00053 
00054       EBRecHitCollectionDM_        = ps.getParameter<std::string>("EBRecHitCollectionDM");
00055       EERecHitCollectionDM_        = ps.getParameter<std::string>("EERecHitCollectionDM");
00056       ESRecHitCollectionDM_        = ps.getParameter<std::string>("ESRecHitCollectionDM");
00057 
00058       produces< EBRecHitCollection >(EBRecHitCollectionDM_);
00059       produces< EERecHitCollection >(EERecHitCollectionDM_);
00060       produces< ESRecHitCollection >(ESRecHitCollectionDM_);
00061 
00062       EMWorker_ = new DataMixingEMWorker(ps);
00063 
00064       //Hcal:
00065 
00066       HBHERecHitCollectionDM_ = ps.getParameter<std::string>("HBHERecHitCollectionDM");
00067       HORecHitCollectionDM_   = ps.getParameter<std::string>("HORecHitCollectionDM");
00068       HFRecHitCollectionDM_   = ps.getParameter<std::string>("HFRecHitCollectionDM");
00069       ZDCRecHitCollectionDM_  = ps.getParameter<std::string>("ZDCRecHitCollectionDM");
00070 
00071       produces< HBHERecHitCollection >(HBHERecHitCollectionDM_);
00072       produces< HORecHitCollection >(HORecHitCollectionDM_);
00073       produces< HFRecHitCollection >(HFRecHitCollectionDM_);
00074       produces< ZDCRecHitCollection >(ZDCRecHitCollectionDM_);
00075 
00076       HcalWorker_ = new DataMixingHcalWorker(ps);
00077 
00078       //Muons:
00079 
00080       DTDigiCollectionDM_  = ps.getParameter<std::string>("DTDigiCollectionDM");
00081       RPCDigiCollectionDM_ = ps.getParameter<std::string>("RPCDigiCollectionDM");
00082       CSCStripDigiCollectionDM_ = ps.getParameter<std::string>("CSCStripDigiCollectionDM");
00083       CSCWireDigiCollectionDM_  = ps.getParameter<std::string>("CSCWireDigiCollectionDM");
00084       CSCComparatorDigiCollectionDM_  = ps.getParameter<std::string>("CSCComparatorDigiCollectionDM");
00085 
00086       produces< DTDigiCollection >();
00087       produces< RPCDigiCollection >();
00088       produces< CSCStripDigiCollection >(CSCStripDigiCollectionDM_);
00089       produces< CSCWireDigiCollection >(CSCWireDigiCollectionDM_);
00090       produces< CSCComparatorDigiCollection >(CSCComparatorDigiCollectionDM_);
00091 
00092       MuonWorker_ = new DataMixingMuonWorker(ps);
00093 
00094       //Tracks:
00095 
00096       GeneralTrackCollectionDM_  = ps.getParameter<std::string>("GeneralTrackDigiCollectionDM");
00097       produces< reco::TrackCollection >(GeneralTrackCollectionDM_);
00098       GeneralTrackWorker_ = new DataMixingGeneralTrackWorker(ps);
00099 
00100     }
00101     else{  // Full Simulation options
00102 
00103       //cout<<"FastSim False!!!"<<endl;
00104 
00105     // declare the products to produce
00106     // Start with EM
00107     if(MergeEMDigis_) {
00108 
00109       // cout<<"EM Digis TRUE!!!"<<endl;
00110 
00111       EBDigiCollectionDM_        = ps.getParameter<std::string>("EBDigiCollectionDM");
00112       EEDigiCollectionDM_        = ps.getParameter<std::string>("EEDigiCollectionDM");
00113       ESDigiCollectionDM_        = ps.getParameter<std::string>("ESDigiCollectionDM");
00114       //   nMaxPrintout_            = ps.getUntrackedParameter<int>("nMaxPrintout",10);
00115 
00116       produces< EBDigiCollection >(EBDigiCollectionDM_);
00117       produces< EEDigiCollection >(EEDigiCollectionDM_);
00118       produces< ESDigiCollection >(ESDigiCollectionDM_);
00119 
00120       EMDigiWorker_ = new DataMixingEMDigiWorker(ps);
00121     }
00122     else { // merge RecHits 
00123       EBRecHitCollectionDM_        = ps.getParameter<std::string>("EBRecHitCollectionDM");
00124       EERecHitCollectionDM_        = ps.getParameter<std::string>("EERecHitCollectionDM");
00125       ESRecHitCollectionDM_        = ps.getParameter<std::string>("ESRecHitCollectionDM");
00126       //   nMaxPrintout_            = ps.getUntrackedParameter<int>("nMaxPrintout",10);
00127 
00128       produces< EBRecHitCollection >(EBRecHitCollectionDM_);
00129       produces< EERecHitCollection >(EERecHitCollectionDM_);
00130       produces< ESRecHitCollection >(ESRecHitCollectionDM_);
00131 
00132       EMWorker_ = new DataMixingEMWorker(ps);
00133     }
00134     // Hcal next
00135 
00136     if(MergeHcalDigis_){
00137       //       cout<<"Hcal Digis TRUE!!!"<<endl;
00138 
00139       HBHEDigiCollectionDM_ = ps.getParameter<std::string>("HBHEDigiCollectionDM");
00140       HODigiCollectionDM_   = ps.getParameter<std::string>("HODigiCollectionDM");
00141       HFDigiCollectionDM_   = ps.getParameter<std::string>("HFDigiCollectionDM");
00142       ZDCDigiCollectionDM_  = ps.getParameter<std::string>("ZDCDigiCollectionDM");
00143 
00144       produces< HBHEDigiCollection >();
00145       produces< HODigiCollection >();
00146       produces< HFDigiCollection >();
00147       produces< ZDCDigiCollection >();
00148 
00149       if(MergeHcalDigisProd_) {
00150         HcalDigiWorkerProd_ = new DataMixingHcalDigiWorkerProd(ps);
00151       }
00152       else {HcalDigiWorker_ = new DataMixingHcalDigiWorker(ps);
00153       }
00154 
00155 
00156     }
00157     else{
00158       HBHERecHitCollectionDM_ = ps.getParameter<std::string>("HBHERecHitCollectionDM");
00159       HORecHitCollectionDM_   = ps.getParameter<std::string>("HORecHitCollectionDM");
00160       HFRecHitCollectionDM_   = ps.getParameter<std::string>("HFRecHitCollectionDM");
00161       ZDCRecHitCollectionDM_  = ps.getParameter<std::string>("ZDCRecHitCollectionDM");
00162 
00163       produces< HBHERecHitCollection >(HBHERecHitCollectionDM_);
00164       produces< HORecHitCollection >(HORecHitCollectionDM_);
00165       produces< HFRecHitCollection >(HFRecHitCollectionDM_);
00166       produces< ZDCRecHitCollection >(ZDCRecHitCollectionDM_);
00167 
00168       HcalWorker_ = new DataMixingHcalWorker(ps);
00169     }
00170 
00171     // Muons
00172 
00173     DTDigiCollectionDM_  = ps.getParameter<std::string>("DTDigiCollectionDM");
00174     RPCDigiCollectionDM_ = ps.getParameter<std::string>("RPCDigiCollectionDM");
00175     CSCStripDigiCollectionDM_ = ps.getParameter<std::string>("CSCStripDigiCollectionDM");
00176     CSCWireDigiCollectionDM_  = ps.getParameter<std::string>("CSCWireDigiCollectionDM");
00177     CSCComparatorDigiCollectionDM_  = ps.getParameter<std::string>("CSCComparatorDigiCollectionDM");
00178 
00179 
00180     produces< DTDigiCollection >();
00181     produces< RPCDigiCollection >();
00182     produces< CSCStripDigiCollection >(CSCStripDigiCollectionDM_);
00183     produces< CSCWireDigiCollection >(CSCWireDigiCollectionDM_);
00184     produces< CSCComparatorDigiCollection >(CSCComparatorDigiCollectionDM_);
00185 
00186     MuonWorker_ = new DataMixingMuonWorker(ps);
00187 
00188     // Si-Strips
00189 
00190     useSiStripRawDigi_ = ps.exists("SiStripRawDigiSource")?
00191       ps.getParameter<std::string>("SiStripRawDigiSource")=="PILEUP" ||
00192       ps.getParameter<std::string>("SiStripRawDigiSource")=="SIGNAL" : false;
00193 
00194     SiStripDigiCollectionDM_  = ps.getParameter<std::string>("SiStripDigiCollectionDM");
00195 
00196     if(useSiStripRawDigi_) {
00197 
00198       produces< edm::DetSetVector<SiStripRawDigi> > (SiStripDigiCollectionDM_);
00199       SiStripRawWorker_ = new DataMixingSiStripRawWorker(ps);
00200 
00201     } else {
00202 
00203       produces< edm::DetSetVector<SiStripDigi> > (SiStripDigiCollectionDM_);
00204       SiStripWorker_ = new DataMixingSiStripWorker(ps);
00205 
00206     }
00207 
00208     // Pixels
00209 
00210     PixelDigiCollectionDM_  = ps.getParameter<std::string>("PixelDigiCollectionDM");
00211 
00212     produces< edm::DetSetVector<PixelDigi> > (PixelDigiCollectionDM_);
00213 
00214     SiPixelWorker_ = new DataMixingSiPixelWorker(ps);
00215 
00216     }
00217 
00218   }
00219 
00220   void DataMixingModule::getSubdetectorNames() {
00221     // get subdetector names
00222     // edm::Service<edm::ConstProductRegistry> reg;
00223     // Loop over provenance of products in registry.
00224     //for (edm::ProductRegistry::ProductList::const_iterator it = reg->productList().begin(); it != reg->productList().end(); ++it) {
00225 
00226       //  **** Check this out.... ****
00227 
00228       // See FWCore/Framework/interface/BranchDescription.h
00229       // BranchDescription contains all the information for the product.
00230 
00231       // This section not very backwards-compatible in terms of digi-merging.  Need to be able to specify here which data format
00232       // to look at...
00233 
00234       //      edm::BranchDescription desc = it->second;
00235       //if (!desc.friendlyClassName_.compare(0,9,"EBRecHitC")) {
00236       //        Subdetectors_.push_back(desc.productInstanceName_);
00237       //LogInfo("DataMixingModule") <<"Adding container "<<desc.productInstanceName_ <<" for pileup treatment";
00238       //}
00239       //else if (!desc.friendlyClassName_.compare(0,9,"EERecHitC")) {
00240         //      else if (!desc.friendlyClassName_.compare(0,9,"EErechitC") && desc.productInstanceName_.compare(0,11,"TrackerHits")) {
00241       //        Subdetectors_.push_back(desc.productInstanceName_);
00242       //LogInfo("DataMixingModule") <<"Adding container "<<desc.productInstanceName_ <<" for pileup treatment";
00243       //}
00244       //else if (!desc.friendlyClassName_.compare(0,9,"HBRecHitC")) {
00245       //        Subdetectors_.push_back(desc.productInstanceName_);
00246       //LogInfo("DataMixingModule") <<"Adding container "<<desc.productInstanceName_ <<" for pileup treatment";
00247       //}
00248       //else if (!desc.friendlyClassName_.compare(0,9,"HERecHitC")) {
00249       //        Subdetectors_.push_back(desc.productInstanceName_);
00250       //LogInfo("DataMixingModule") <<"Adding container "<<desc.productInstanceName_ <<" for pileup treatment";
00251       // }
00252         // and so on with other detector types...
00253     // }
00254   }       
00255                
00256 
00257  
00258 
00259   // Virtual destructor needed.
00260   DataMixingModule::~DataMixingModule() { 
00261     if(MergeEMDigis_){ delete EMDigiWorker_;}
00262     else {delete EMWorker_;}
00263     if(MergeHcalDigis_) { 
00264       if(MergeHcalDigisProd_) { delete HcalDigiWorkerProd_;}
00265       else { delete HcalDigiWorker_; }}
00266     else {delete HcalWorker_;}
00267     if(MuonWorker_) delete MuonWorker_;
00268     if(DoFastSim_){
00269       delete GeneralTrackWorker_;
00270     }else{
00271       if(useSiStripRawDigi_)
00272         delete SiStripRawWorker_;
00273       else
00274         delete SiStripWorker_;
00275       delete SiPixelWorker_;
00276     }
00277   }
00278 
00279   void DataMixingModule::addSignals(const edm::Event &e, const edm::EventSetup& ES) { 
00280     // fill in maps of hits
00281 
00282     LogDebug("DataMixingModule")<<"===============> adding MC signals for "<<e.id();
00283 
00284     // Ecal
00285     if(MergeEMDigis_) { EMDigiWorker_->addEMSignals(e, ES); }
00286     else{ EMWorker_->addEMSignals(e);}
00287 
00288     // Hcal
00289     if(MergeHcalDigis_) { 
00290       if(MergeHcalDigisProd_){
00291         HcalDigiWorkerProd_->addHcalSignals(e, ES);
00292       }
00293       else{
00294         HcalDigiWorker_->addHcalSignals(e, ES);
00295       }
00296     }
00297     else {HcalWorker_->addHcalSignals(e);}
00298     
00299     // Muon
00300     MuonWorker_->addMuonSignals(e);
00301 
00302     if(DoFastSim_){
00303        GeneralTrackWorker_->addGeneralTrackSignals(e);
00304     }else{
00305     // SiStrips
00306     if(useSiStripRawDigi_) SiStripRawWorker_->addSiStripSignals(e);
00307     else SiStripWorker_->addSiStripSignals(e);
00308 
00309     // SiPixels
00310     SiPixelWorker_->addSiPixelSignals(e);
00311     }    
00312   } // end of addSignals
00313 
00314   
00315 
00316 
00317   void DataMixingModule::pileWorker(const EventPrincipal &ep, int bcr, int eventNr, const edm::EventSetup& ES) {  
00318 
00319 
00320     LogDebug("DataMixingModule") <<"\n===============> adding pileups from event  "<<ep.id()<<" for bunchcrossing "<<bcr;
00321 
00322     // fill in maps of hits; same code as addSignals, except now applied to the pileup events
00323 
00324     // Ecal
00325     if(MergeEMDigis_) {    EMDigiWorker_->addEMPileups(bcr, &ep, eventNr, ES);}
00326     else {EMWorker_->addEMPileups(bcr, &ep, eventNr); }
00327 
00328     // Hcal
00329     if(MergeHcalDigis_) {    
00330       if(MergeHcalDigisProd_) {    
00331         HcalDigiWorkerProd_->addHcalPileups(bcr, &ep, eventNr, ES);
00332       }
00333       else{
00334         HcalDigiWorker_->addHcalPileups(bcr, &ep, eventNr, ES);}
00335     }
00336     else {HcalWorker_->addHcalPileups(bcr, &ep, eventNr);}
00337 
00338     // Muon
00339     MuonWorker_->addMuonPileups(bcr, &ep, eventNr);
00340 
00341     if(DoFastSim_){
00342       GeneralTrackWorker_->addGeneralTrackPileups(bcr, &ep, eventNr);
00343     }else{
00344       
00345       // SiStrips
00346       if(useSiStripRawDigi_) SiStripRawWorker_->addSiStripPileups(bcr, &ep, eventNr);
00347       else SiStripWorker_->addSiStripPileups(bcr, &ep, eventNr);
00348       
00349       // SiPixels
00350       SiPixelWorker_->addSiPixelPileups(bcr, &ep, eventNr);
00351     }
00352   }
00353 
00354 
00355   
00356   void DataMixingModule::doPileUp(edm::Event &e, const edm::EventSetup& ES)
00357   {
00358     std::vector<edm::EventID> recordEventID;
00359     std::vector<int> PileupList;
00360     PileupList.clear();
00361     TrueNumInteractions_.clear();
00362 
00363     for (int bunchCrossing=minBunch_;bunchCrossing<=maxBunch_;++bunchCrossing) {
00364       for (unsigned int isource=0;isource<maxNbSources_;++isource) {
00365         boost::shared_ptr<PileUp> source = inputSources_[isource];
00366         if (not source or not source->doPileUp()) 
00367           continue;
00368 
00369         if (isource==0) 
00370           source->CalculatePileup(minBunch_, maxBunch_, PileupList, TrueNumInteractions_);
00371 
00372         int NumPU_Events = 0;
00373         if (isource ==0) { 
00374           NumPU_Events = PileupList[bunchCrossing - minBunch_];
00375         } else {
00376           // non-minbias pileup only gets one event for now. Fix later if desired.
00377           NumPU_Events = 1;
00378         }  
00379 
00380         source->readPileUp(
00381                 e.id(),
00382                 recordEventID,
00383                 boost::bind(&DataMixingModule::pileWorker, boost::ref(*this),
00384                             _1, bunchCrossing, _2, boost::cref(ES)),
00385                 NumPU_Events
00386                 );
00387       }
00388     }
00389   }
00390 
00391 
00392   void DataMixingModule::put(edm::Event &e,const edm::EventSetup& ES) {
00393 
00394     // individual workers...
00395 
00396     // Ecal
00397     if(MergeEMDigis_) {EMDigiWorker_->putEM(e,ES);}
00398     else {EMWorker_->putEM(e);}
00399 
00400     // Hcal
00401     if(MergeHcalDigis_) {
00402       if(MergeHcalDigisProd_) {
00403         HcalDigiWorkerProd_->putHcal(e,ES);
00404       }
00405       else{
00406         HcalDigiWorker_->putHcal(e,ES);
00407       }
00408     }
00409     else {HcalWorker_->putHcal(e);}
00410 
00411     // Muon
00412     MuonWorker_->putMuon(e);
00413 
00414     if(DoFastSim_){
00415        GeneralTrackWorker_->putGeneralTrack(e);
00416     }else{
00417        // SiStrips
00418       if(useSiStripRawDigi_) SiStripRawWorker_->putSiStrip(e);
00419       else SiStripWorker_->putSiStrip(e);
00420        
00421        // SiPixels
00422        SiPixelWorker_->putSiPixel(e);
00423     }
00424   }
00425 
00426 
00427 } //edm