CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/SimGeneral/MixingModule/plugins/MixingModule.cc

Go to the documentation of this file.
00001 // File: MixingModule.cc
00002 // Description:  see MixingModule.h
00003 // Author:  Ursula Berthon, LLR Palaiseau
00004 //
00005 //--------------------------------------------
00006 
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "FWCore/Utilities/interface/EDMException.h"
00009 #include "FWCore/Utilities/interface/Algorithms.h"
00010 #include "FWCore/Framework/interface/TriggerNamesService.h"
00011 #include "FWCore/ServiceRegistry/interface/Service.h"
00012 #include "DataFormats/Common/interface/Handle.h"
00013 #include "DataFormats/Provenance/interface/Provenance.h"
00014 #include "DataFormats/Provenance/interface/BranchDescription.h"
00015 #include "SimDataFormats/CrossingFrame/interface/CrossingFramePlaybackInfoExtended.h"
00016 #include "FWCore/Utilities/interface/TypeID.h"
00017 #include "MixingModule.h"
00018 #include "MixingWorker.h"
00019 
00020 #include "FWCore/Services/src/Memory.h"
00021 using namespace std;
00022 
00023 namespace edm
00024 {
00025 
00026   // Constructor 
00027   MixingModule::MixingModule(const edm::ParameterSet& ps_mix) : 
00028   BMixingModule(ps_mix),
00029   labelPlayback_(ps_mix.getParameter<std::string>("LabelPlayback")),
00030   mixProdStep2_(ps_mix.getParameter<bool>("mixProdStep2")),
00031   mixProdStep1_(ps_mix.getParameter<bool>("mixProdStep1"))
00032   {
00033     if (!mixProdStep1_ && !mixProdStep2_) LogInfo("MixingModule") << " The MixingModule was run in the Standard mode.";
00034     if (mixProdStep1_) LogInfo("MixingModule") << " The MixingModule was run in the Step1 mode. It produces a mixed secondary source.";
00035     if (mixProdStep2_) LogInfo("MixingModule") << " The MixingModule was run in the Step2 mode. It uses a mixed secondary source.";
00036      
00037     useCurrentProcessOnly_=false;
00038     if (ps_mix.exists("useCurrentProcessOnly")) {
00039       useCurrentProcessOnly_=ps_mix.getParameter<bool>("useCurrentProcessOnly");
00040       LogInfo("MixingModule") <<" using given Parameter 'useCurrentProcessOnly' ="<<useCurrentProcessOnly_;
00041     }
00042     if (labelPlayback_.size()>0){
00043       sel_=new Selector( ModuleLabelSelector(labelPlayback_));
00044     }
00045     else {
00046       sel_=new Selector( MatchAllSelector());
00047     }
00048 
00049     ParameterSet ps=ps_mix.getParameter<ParameterSet>("mixObjects");
00050     std::vector<std::string> names = ps.getParameterNames();
00051     for (std::vector<string>::iterator it=names.begin();it!= names.end();++it)
00052       {
00053           ParameterSet pset=ps.getParameter<ParameterSet>((*it));
00054           if (!pset.exists("type"))  continue; //to allow replacement by empty pset
00055           std::string object = pset.getParameter<std::string>("type");
00056           std::vector<InputTag>  tags=pset.getParameter<std::vector<InputTag> >("input");
00057           
00058           if (mixProdStep2_){
00059           //SimTracks
00060           if (object=="SimTrackPCrossingFrame") {
00061             InputTag tag;
00062             if (tags.size()>0) tag=tags[0];
00063             std::string label;
00064             branchesActivate(TypeID(typeid(PCrossingFrame<SimTrack>)).friendlyClassName(),std::string(""),tag,label);
00065            
00066             LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00067 
00068             //---------------------------------
00069             // set an appropriate label for the CrossingFrame
00070             if (find(names.begin(), names.end(), "mixTracks") != names.end())
00071             { 
00072                edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixTracks");
00073                if (!psin.exists("type"))  continue; //to allow replacement by empty pset
00074                std::string object = psin.getParameter<std::string>("type");
00075                std::vector<InputTag>  tags=psin.getParameter<std::vector<InputTag> >("input");
00076         
00077                InputTag tagCF;
00078                if (tags.size()>0) tagCF=tags[0];
00079                std::string labelCF;
00080            
00081                branchesActivate(TypeID(typeid(std::vector<SimTrack>)).friendlyClassName(),std::string(""),tagCF,labelCF);
00082                workersObjects_.push_back(new MixingWorker<SimTrack>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));  
00083                
00084                produces<CrossingFrame<SimTrack> >(labelCF);
00085             }       
00086             //--------------------------------      
00087 
00088           }else if (object=="SimVertexPCrossingFrame") {
00089             InputTag tag;
00090             if (tags.size()>0) tag=tags[0];
00091             std::string label;
00092             branchesActivate(TypeID(typeid(PCrossingFrame<SimVertex>)).friendlyClassName(),std::string(""),tag,label);
00093 
00094             LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag "<<tag.encode()<<", label will be "<<label;
00095             
00096             //---------------------------------
00097             // set an appropriate label for the CrossingFrame
00098             if (find(names.begin(), names.end(), "mixVertices") != names.end())
00099             { 
00100                edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixVertices");
00101                if (!psin.exists("type"))  continue; //to allow replacement by empty pset
00102                std::string object = psin.getParameter<std::string>("type");
00103                std::vector<InputTag>  tags=psin.getParameter<std::vector<InputTag> >("input");
00104                
00105                InputTag tagCF;
00106                if (tags.size()>0) tagCF=tags[0];
00107                std::string labelCF;
00108            
00109                branchesActivate(TypeID(typeid(std::vector<SimVertex>)).friendlyClassName(),std::string(""),tagCF,labelCF);
00110                workersObjects_.push_back(new MixingWorker<SimVertex>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));  
00111 
00112                produces<CrossingFrame<SimVertex> >(labelCF);
00113             }
00114             //---------------------------------
00115           }
00116 
00117           else if (object=="HepMCProductPCrossingFrame") {
00118             InputTag tag;
00119             if (tags.size()>0) tag=tags[0];
00120             std::string label;
00121             branchesActivate(TypeID(typeid(PCrossingFrame<HepMCProduct>)).friendlyClassName(),std::string(""),tag,label);
00122 
00123             LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00124             
00125             //---------------------------------
00126             // set an appropriate label for the CrossingFrame
00127             if (find(names.begin(), names.end(), "mixHepMC") != names.end())
00128             {   
00129                edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixHepMC");
00130                if (!psin.exists("type"))  continue; //to allow replacement by empty pset
00131                std::string object = psin.getParameter<std::string>("type");
00132                std::vector<InputTag>  tags=psin.getParameter<std::vector<InputTag> >("input");
00133                
00134                InputTag tagCF;
00135                if (tags.size()>0) tagCF=tags[0];
00136                std::string labelCF;
00137                       
00138                branchesActivate(TypeID(typeid(HepMCProduct)).friendlyClassName(),std::string(""),tagCF,labelCF);
00139                workersObjects_.push_back(new MixingWorker<HepMCProduct>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));  
00140 
00141                produces<CrossingFrame<HepMCProduct> >(labelCF);
00142             }
00143             //--------------------------------  
00144           }else if (object=="PCaloHitPCrossingFrame") {
00145             std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00146             for (unsigned int ii=0;ii<subdets.size();ii++) {
00147               InputTag tag;
00148               if (tags.size()==1) tag=tags[0];
00149               else if(tags.size()>1) tag=tags[ii];
00150               std::string label;
00151               branchesActivate(TypeID(typeid(PCrossingFrame<PCaloHit>)).friendlyClassName(),subdets[ii],tag,label);
00152 
00153               LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00154               
00155               //---------------------------------
00156               // set an appropriate label for the product CrossingFrame
00157               if (find(names.begin(), names.end(), "mixCH") != names.end())
00158               {  
00159                  edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixCH");
00160                  if (!psin.exists("type"))  continue; //to allow replacement by empty pset
00161                  std::string object = psin.getParameter<std::string>("type");
00162                  std::vector<InputTag>  tags=psin.getParameter<std::vector<InputTag> >("input");
00163                
00164                  InputTag tagCF;
00165                  if (tags.size()==1) tagCF=tags[0];
00166                  else if(tags.size()>1) tagCF=tags[ii];
00167                  std::string labelCF;
00168         
00169                  branchesActivate(TypeID(typeid(std::vector<PCaloHit>)).friendlyClassName(),subdets[ii],tagCF,labelCF);
00170                  workersObjects_.push_back(new MixingWorker<PCaloHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));  
00171 
00172                  produces<CrossingFrame<PCaloHit> > (labelCF);
00173               }
00174               //--------------------------------
00175             }
00176 
00177           }else if (object=="PSimHitPCrossingFrame") {
00178             std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00179             for (unsigned int ii=0;ii<subdets.size();ii++) {          
00180               InputTag tag;
00181               if (tags.size()==1) tag=tags[0];
00182               else if(tags.size()>1) tag=tags[ii];
00183               std::string label;
00184               branchesActivate(TypeID(typeid(PCrossingFrame<PSimHit>)).friendlyClassName(),subdets[ii],tag,label);
00185            
00186               LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00187               
00188               
00189               //---------------------------------
00190               // set an appropriate label for the CrossingFrame
00191               if (find(names.begin(), names.end(), "mixSH") != names.end())
00192               { 
00193                  edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixSH");
00194                  if (!psin.exists("type"))  continue; //to allow replacement by empty pset
00195                  std::string object = psin.getParameter<std::string>("type");
00196                  std::vector<InputTag>  tags=psin.getParameter<std::vector<InputTag> >("input");
00197                
00198                  InputTag tagCF;
00199                  if (tags.size()==1) tagCF=tags[0];
00200                  else if(tags.size()>1) tagCF=tags[ii];
00201                  std::string labelCF;
00202                  branchesActivate(TypeID(typeid(std::vector<PSimHit>)).friendlyClassName(),subdets[ii],tagCF,labelCF);                     
00203                  workersObjects_.push_back(new MixingWorker<PSimHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));                     
00204                 
00205                  produces<CrossingFrame<PSimHit> > (labelCF);
00206               }
00207               //-------------------------------
00208             }
00209 
00210           }
00211           }//mixProdStep2
00212          
00213           
00214           if (!mixProdStep2_){
00215                 
00216           InputTag tagCF = InputTag();
00217           std::string labelCF = " ";      
00218           
00219           //SimTracks
00220           if (object=="SimTrack") {
00221             InputTag tag;
00222             if (tags.size()>0) tag=tags[0];
00223             std::string label;
00224 
00225             branchesActivate(TypeID(typeid(std::vector<SimTrack>)).friendlyClassName(),std::string(""),tag,label);
00226             workersObjects_.push_back(new MixingWorker<SimTrack>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));  
00227 
00228             produces<CrossingFrame<SimTrack> >(label);
00229             LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00230             
00231 
00232           }else if (object=="SimVertex") {
00233             InputTag tag;
00234             if (tags.size()>0) tag=tags[0];
00235             std::string label;
00236 
00237             branchesActivate(TypeID(typeid(std::vector<SimVertex>)).friendlyClassName(),std::string(""),tag,label);
00238             
00239             workersObjects_.push_back(new MixingWorker<SimVertex>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));  
00240             produces<CrossingFrame<SimVertex> >(label);
00241             LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag "<<tag.encode()<<", label will be "<<label;
00242             
00243           }else if (object=="HepMCProduct") {
00244             InputTag tag;
00245             if (tags.size()>0) tag=tags[0];
00246             std::string label;
00247 
00248             branchesActivate(TypeID(typeid(HepMCProduct)).friendlyClassName(),std::string(""),tag,label);
00249             workersObjects_.push_back(new MixingWorker<HepMCProduct>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));  
00250             
00251             produces<CrossingFrame<HepMCProduct> >(label);
00252             LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00253             
00254 
00255           }else if (object=="PCaloHit") {
00256             std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00257             for (unsigned int ii=0;ii<subdets.size();ii++) {
00258               InputTag tag;
00259               if (tags.size()==1) tag=tags[0];
00260               else if(tags.size()>1) tag=tags[ii];
00261               std::string label;
00262 
00263               branchesActivate(TypeID(typeid(std::vector<PCaloHit>)).friendlyClassName(),subdets[ii],tag,label);
00264               workersObjects_.push_back(new MixingWorker<PCaloHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));  
00265 
00266               produces<CrossingFrame<PCaloHit> > (label);
00267               LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00268               
00269             }
00270 
00271           }else if (object=="PSimHit") {
00272             std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00273             for (unsigned int ii=0;ii<subdets.size();ii++) {
00274               InputTag tag;
00275               if (tags.size()==1) tag=tags[0];
00276               else if(tags.size()>1) tag=tags[ii];
00277               std::string label;
00278 
00279               branchesActivate(TypeID(typeid(std::vector<PSimHit>)).friendlyClassName(),subdets[ii],tag,label);
00280               workersObjects_.push_back(new MixingWorker<PSimHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));  
00281 
00282               produces<CrossingFrame<PSimHit> > (label);
00283               LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00284             }
00285           }
00286           else LogWarning("MixingModule") <<"You have asked to mix an unknown type of object("<<object<<").\n If you want to include it in mixing, please contact the authors of the MixingModule!";
00287          }//if for mixProdStep2
00288       }//while over the mixObjects parameters
00289 
00290     sort_all(wantedBranches_);
00291     for (unsigned int branch=0;branch<wantedBranches_.size();++branch) LogDebug("MixingModule")<<"Will keep branch "<<wantedBranches_[branch]<<" for mixing ";
00292   
00293     dropUnwantedBranches(wantedBranches_);
00294 
00295     produces<PileupMixingContent>();
00296     
00297     produces<CrossingFramePlaybackInfoExtended>();
00298   }
00299  
00300 
00301   void MixingModule::branchesActivate(const std::string &friendlyName, const std::string &subdet, InputTag &tag, std::string &label) {
00302        
00303     label=tag.label()+tag.instance();
00304     wantedBranches_.push_back(friendlyName + '_' +
00305                               tag.label() + '_' +
00306                               tag.instance());
00307                                           
00308     //if useCurrentProcessOnly, we have to change the input tag
00309     if (useCurrentProcessOnly_) {
00310       const std::string processName = edm::Service<edm::service::TriggerNamesService>()->getProcessName();
00311       tag = InputTag(tag.label(),tag.instance(),processName);
00312     }    
00313     
00314   }
00315 
00316   
00317   void MixingModule::checkSignal(const edm::Event &e){
00318     if (workers_.size()==0){
00319       for (unsigned int ii=0;ii<workersObjects_.size();ii++){
00320         if (workersObjects_[ii]->checkSignal(e)){
00321           workers_.push_back(workersObjects_[ii]);
00322         }
00323       }
00324     }
00325   }
00326   
00327   
00328   void MixingModule::createnewEDProduct() {
00329     //create playback info
00330     playbackInfo_=new CrossingFramePlaybackInfoExtended(minBunch_,maxBunch_,maxNbSources_); 
00331 
00332     //and CrossingFrames
00333     for (unsigned int ii=0;ii<workers_.size();ii++){
00334       workers_[ii]->createnewEDProduct();
00335     }  
00336   }
00337  
00338 
00339   // Virtual destructor needed.
00340   MixingModule::~MixingModule() { 
00341     for (unsigned int ii=0;ii<workersObjects_.size();ii++){ 
00342       delete workersObjects_[ii];}
00343     delete sel_;  
00344   }  
00345 
00346   void MixingModule::addSignals(const edm::Event &e, const edm::EventSetup& setup) { 
00347     // fill in signal part of CrossingFrame
00348 
00349     LogDebug("MixingModule")<<"===============> adding signals for "<<e.id();
00350     for (unsigned int ii=0;ii<workers_.size();ii++){ 
00351       workers_[ii]->addSignals(e);
00352     }
00353   }
00354 
00355   void MixingModule::doPileUp(edm::Event &e, const edm::EventSetup& setup)
00356   { //     we first loop over workers
00357     // in order not to keep all CrossingFrames in memory simultaneously
00358     //      
00359     for (unsigned int ii=0;ii<workers_.size();ii++) {
00360       // we have to loop over bunchcrossings first since added objects are all stored in one vector, 
00361       // ordered by bunchcrossing
00362 
00363       for (int bunchCrossing=minBunch_;bunchCrossing<=maxBunch_;++bunchCrossing) {
00364         workers_[ii]->setBcrOffset();
00365         for (unsigned int isource=0;isource<maxNbSources_;++isource) {
00366           workers_[ii]->setSourceOffset(isource);         
00367           if (doit_[isource])   {
00368             merge(bunchCrossing, (pileup_[isource])[bunchCrossing-minBunch_],ii,setup);
00369           }
00370         }       
00371       }
00372     }
00373 
00374     std::auto_ptr< PileupMixingContent > PileupMixing_;
00375     
00376     std::vector<int> bunchCrossingList;
00377     std::vector<int> numInteractionList;
00378     std::vector<float> TrueInteractionList;
00379     
00380     //Makin' a list:
00381     for (int bunchCrossing=minBunch_;bunchCrossing<=maxBunch_;++bunchCrossing) {
00382       bunchCrossingList.push_back(bunchCrossing);
00383       if(!doit_[0]) {
00384         numInteractionList.push_back(0);
00385         TrueInteractionList.push_back(0);
00386       }
00387       else {
00388         numInteractionList.push_back(((pileup_[0])[bunchCrossing-minBunch_]).size());
00389         TrueInteractionList.push_back((TrueNumInteractions_[0])[bunchCrossing-minBunch_]);      
00390       }
00391     }
00392 
00393     
00394     PileupMixing_ = std::auto_ptr< PileupMixingContent >(new PileupMixingContent( bunchCrossingList,
00395                                                                                   numInteractionList,
00396                                                                                   TrueInteractionList));
00397 
00398     e.put(PileupMixing_);
00399 
00400 
00401     // we have to do the ToF transformation for PSimHits once all pileup has been added
00402       for (unsigned int ii=0;ii<workers_.size();ii++) {
00403         //not apply setTof in Step2 mode because it was done in the Step1
00404         if (!mixProdStep2_){ 
00405           workers_[ii]->setTof();
00406         }
00407         workers_[ii]->put(e);
00408       }
00409  }
00410 
00411   void MixingModule::addPileups(const int bcr, EventPrincipal *ep, unsigned int eventNr,unsigned int worker, const edm::EventSetup& setup) {    
00412   // fill in pileup part of CrossingFrame
00413     LogDebug("MixingModule") <<"\n===============> adding objects from event  "<<ep->id()<<" for bunchcrossing "<<bcr;
00414 
00415     workers_[worker]->addPileups(bcr,ep,eventNr,vertexoffset);
00416   }
00417   
00418   void MixingModule::setEventStartInfo(const unsigned int s) {
00419     playbackInfo_->setEventStartInfo(vectorEventIDs_,s); 
00420   }
00421 
00422   void MixingModule::put(edm::Event &e, const edm::EventSetup& setup) {
00423 
00424     if (playbackInfo_) {
00425       std::auto_ptr<CrossingFramePlaybackInfoExtended> pOut(playbackInfo_);
00426       e.put(pOut);
00427     }
00428   }
00429   
00430   void MixingModule::getEventStartInfo(edm::Event & e, const unsigned int s) {
00431     if (playback_) {
00432  
00433       edm::Handle<CrossingFramePlaybackInfoExtended>  playbackInfo_H;
00434       bool got=e.get((*sel_), playbackInfo_H); 
00435       if (got) {
00436         playbackInfo_H->getEventStartInfo(vectorEventIDs_,s);
00437       }else{
00438         throw cms::Exception("MixingProductNotFound") << " No CrossingFramePlaybackInfoExtended on the input file, but playback option set!!!!! Please change the input file if you really want playback!!!!!!"  << endl;    
00439       }
00440     }
00441   }
00442 }//edm