CMS 3D CMS Logo

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