CMS 3D CMS Logo

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/ConstProductRegistry.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/CrossingFramePlaybackInfo.h"
00016 #include "MixingModule.h"
00017 #include "MixingWorker.h"
00018 
00019 #include "FWCore/Services/src/Memory.h"
00020 using namespace std;
00021 
00022 namespace edm
00023 {
00024 
00025   // Constructor 
00026   MixingModule::MixingModule(const edm::ParameterSet& ps_mix) : BMixingModule(ps_mix),labelPlayback_(ps_mix.getParameter<std::string>("LabelPlayback"))
00027 
00028   {
00029     if (labelPlayback_.size()>0){
00030       sel_=new Selector( ModuleLabelSelector(labelPlayback_));
00031     }
00032     else {
00033       sel_=new Selector( MatchAllSelector());
00034     }
00035 
00036     ParameterSet ps=ps_mix.getParameter<ParameterSet>("mixObjects");
00037     std::vector<std::string> names = ps.getParameterNames();
00038     for (std::vector<string>::iterator it=names.begin();it!= names.end();++it)
00039       {
00040           ParameterSet pset=ps.getParameter<ParameterSet>((*it));
00041           if (!pset.exists("type"))  continue; //to allow replacement by empty pset
00042           std::string object = pset.getParameter<std::string>("type");
00043           std::vector<InputTag>  tags=pset.getParameter<std::vector<InputTag> >("input");
00044         
00045           //SimTracks
00046           if (object=="SimTrack") {
00047             InputTag tag;
00048             if (tags.size()>0) tag=tags[0];
00049             std::string label;
00050             if (verifyRegistry(object,std::string(""),tag,label));
00051             {
00052               workers_.push_back(new MixingWorker<SimTrack>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,maxNbSources_,tag));  
00053               produces<CrossingFrame<SimTrack> >(label);
00054               LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00055             }
00056 
00057           }else if (object=="SimVertex") {
00058             InputTag tag;
00059             if (tags.size()>0) tag=tags[0];
00060             std::string label;
00061             if (verifyRegistry(object,std::string(""),tag,label))
00062             {
00063               workers_.push_back(new MixingWorker<SimVertex>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,maxNbSources_,tag));  
00064               produces<CrossingFrame<SimVertex> >(label);
00065               LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag "<<tag.encode()<<", label will be "<<label;
00066             }
00067           }
00068 
00069             else if (object=="HepMCProduct") {
00070             InputTag tag;
00071             if (tags.size()>0) tag=tags[0];
00072             std::string label;
00073             if (verifyRegistry(object,std::string(""),tag,label)){
00074             workers_.push_back(new MixingWorker<HepMCProduct>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,maxNbSources_,tag));  
00075             produces<CrossingFrame<HepMCProduct> >(label);
00076             LogInfo("MixingModule") <<"Will mix"<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00077             }
00078 
00079           }else if (object=="PCaloHit") {
00080             std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00081             for (unsigned int ii=0;ii<subdets.size();ii++) {
00082               InputTag tag;
00083               if (tags.size()==1) tag=tags[0];
00084               else if(tags.size()>1) tag=tags[ii]; //FIXME: verify sizes
00085               std::string label;
00086               if (verifyRegistry(object,subdets[ii],tag,label)){
00087               workers_.push_back(new MixingWorker<PCaloHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,maxNbSources_,tag));  
00088               produces<CrossingFrame<PCaloHit> > (label);
00089               LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00090               }
00091             }
00092 
00093           }else if (object=="PSimHit") {
00094             std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00095             for (unsigned int ii=0;ii<subdets.size();ii++) {
00096               InputTag tag;
00097               if (tags.size()==1) tag=tags[0];
00098               else if(tags.size()>1) tag=tags[ii]; //FIXME: verify sizes
00099               std::string label;
00100               if (!verifyRegistry(object,subdets[ii],tag,label)) continue;
00101               if ((subdets[ii].find("HighTof")==std::string::npos) && (subdets[ii].find("LowTof")==std::string::npos)) {
00102                 workers_.push_back(new MixingWorker<PSimHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,maxNbSources_,tag));  
00103                 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00104               }else {
00105                 workers_.push_back(new MixingWorker<PSimHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,maxNbSources_,tag,true));  
00106                 // here we have to give the opposite selector too (low for high, high for low)
00107                 int slow=(subdets[ii]).find("LowTof");//FIXME: to be done before when creating trackerPids
00108                 int iend=(subdets[ii]).size();
00109                 std::string productInstanceNameOpp;
00110                 if (slow>0) {
00111                   productInstanceNameOpp=tag.instance().substr(0,iend-6)+"HighTof";
00112                 }else{
00113                   productInstanceNameOpp=tag.instance().substr(0,iend-7)+"LowTof";
00114                 }
00115                 InputTag tagOpp(tag.label(),productInstanceNameOpp,tag.process());
00116                 workers_[workers_.size()-1]->setOppositeTag(tagOpp);
00117                 workers_[workers_.size()-1]->setCheckTof(ps.getUntrackedParameter<bool>("checktof",true));
00118                 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00119               }
00120               produces<CrossingFrame<PSimHit> > (label);
00121             }
00122 
00123           }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!";
00124       }
00125 
00126     sort_all(wantedBranches_);
00127     for (unsigned int branch=0;branch<wantedBranches_.size();++branch) LogDebug("MixingModule")<<"Will keep branch "<<wantedBranches_[branch];
00128   
00129     dropUnwantedBranches(wantedBranches_);
00130     produces<CrossingFramePlaybackInfo>();
00131   }
00132  
00133   bool MixingModule::verifyRegistry(std::string object, std::string subdet, InputTag &tag,std::string &label) {
00134     // verify that the given product exists in the product registry
00135     // and create the label to be given to the CrossingFrame
00136 
00137     edm::Service<edm::ConstProductRegistry> reg;
00138     // Loop over provenance of products in registry.
00139     std::string lookfor;
00140     if (object=="HepMCProduct") lookfor="edm::"+object;//exception for HepMCProduct
00141     else if (object=="edm::HepMCProduct") lookfor=object;
00142     else  lookfor="std::vector<"+object+">";
00143     bool found=false;
00144     for (edm::ProductRegistry::ProductList::const_iterator it = reg->productList().begin();
00145          it != reg->productList().end(); ++it) {
00146       // See FWCore/Framework/interface/BranchDescription.h
00147       // BranchDescription contains all the information for the product.
00148       edm::BranchDescription desc = it->second;
00149       if (desc.className()==lookfor && desc.moduleLabel()==tag.label() && desc.productInstanceName()==tag.instance()) {
00150         label=desc.moduleLabel()+desc.productInstanceName();
00151         found=true;
00152         wantedBranches_.push_back(desc.friendlyClassName() + '_' +
00153                                   desc.moduleLabel() + '_' +
00154                                   desc.productInstanceName());
00155         break; 
00156         }
00157     }
00158     if (!found) {
00159       LogWarning("MixingModule")<<"!!!!!!!!!Could not find in registry requested object: "<<object<<" with "<<tag<<".\nWill NOT be considered for mixing!!!!!!!!!";
00160       return false;
00161     }
00162     
00163     return true;
00164   }
00165 
00166 //   Selector * MixingModule::createSelector(InputTag &tag){
00167 //     //FIXME: how to distinguish in input tags between ="" and any?
00168 //     Selector *sel=0;
00169 //     if (tag.label()=="") {
00170 //       if (tag.instance()=="")  sel = new Selector(MatchAllSelector());
00171 //       else sel = new Selector(ProductInstanceNameSelector(tag.instance()));
00172 //     } else {
00173 //       if (tag.instance()=="") sel =new Selector(ModuleLabelSelector(tag.label()));
00174 //       else sel =new Selector(ModuleLabelSelector(tag.label()) && ProductInstanceNameSelector(tag.instance()));
00175 //     }
00176 //     return sel;
00177 //   }
00178 
00179   void MixingModule::beginJob(edm::EventSetup const&iSetup) {
00180   }
00181 
00182   void MixingModule::createnewEDProduct() {
00183     //create playback info
00184     playbackInfo_=new CrossingFramePlaybackInfo(minBunch_,maxBunch_,maxNbSources_); 
00185 
00186     //and CrossingFrames
00187     for (unsigned int ii=0;ii<workers_.size();ii++) 
00188       workers_[ii]->createnewEDProduct();
00189   }
00190  
00191 
00192   // Virtual destructor needed.
00193   MixingModule::~MixingModule() { 
00194     for (unsigned int ii=0;ii<workers_.size();ii++) 
00195       delete workers_[ii];
00196   }  
00197 
00198   void MixingModule::addSignals(const edm::Event &e) { 
00199     // fill in signal part of CrossingFrame
00200 
00201     LogDebug("MixingModule")<<"===============> adding signals for "<<e.id();
00202     for (unsigned int ii=0;ii<workers_.size();ii++){ 
00203       workers_[ii]->addSignals(e);
00204     }
00205 
00206   }
00207 
00208   void MixingModule::doPileUp(edm::Event &e)
00209   {//     we first loop over workers
00210     // in order not to keep all CrossingFrames in memory simultaneously
00211     //
00212 
00213     for (unsigned int ii=0;ii<workers_.size();ii++) {
00214       // we have to loop over bunchcrossings first since added objects are all stored in one vector, 
00215       // ordered by bunchcrossing
00216       for (int bunchCrossing=minBunch_;bunchCrossing<=maxBunch_;++bunchCrossing) {
00217         workers_[ii]->setBcrOffset();
00218         for (unsigned int isource=0;isource<maxNbSources_;++isource) {
00219           workers_[ii]->setSourceOffset(isource);
00220           if (doit_[isource])   {
00221             merge(bunchCrossing, (pileup_[isource])[bunchCrossing-minBunch_],ii);
00222           }     
00223         }
00224       }
00225       workers_[ii]->put(e);
00226     }
00227   }
00228 
00229   void MixingModule::addPileups(const int bcr, Event *e, unsigned int eventNr,unsigned int worker) {    // fill in pileup part of CrossingFrame
00230 
00231   
00232     LogDebug("MixingModule") <<"\n===============> adding objects from event  "<<e->id()<<" for bunchcrossing "<<bcr;
00233 
00234     workers_[worker]->addPileups(bcr,e,eventNr,vertexoffset);
00235   }
00236   void MixingModule::setEventStartInfo(const unsigned int s) {
00237     playbackInfo_->setEventStartInfo(eventIDs_,fileSeqNrs_,nrEvents_,s); 
00238   }
00239 
00240   void MixingModule::put(edm::Event &e) {
00241 
00242     if (playbackInfo_) {
00243       std::auto_ptr<CrossingFramePlaybackInfo> pOut(playbackInfo_);
00244       e.put(pOut);
00245     }
00246   }
00247   
00248   void MixingModule::getEventStartInfo(edm::Event & e, const unsigned int s) {
00249     if (playback_) {
00250  
00251       edm::Handle<CrossingFramePlaybackInfo>  playbackInfo_H;
00252       bool got=e.get((*sel_), playbackInfo_H); 
00253       if (got) {
00254         playbackInfo_H->getEventStartInfo(eventIDs_,fileSeqNrs_,nrEvents_,s);
00255       }else{
00256         LogWarning("MixingModule")<<"\n\nAttention: No CrossingFramePlaybackInfo on the input file, but playback option set!!!!!!!\nAttention: Job is executed without playback, please change the input file if you really want playback!!!!!!!";
00257         //FIXME: defaults
00258       }
00259     }
00260   }
00261 }//edm

Generated on Tue Jun 9 17:47:28 2009 for CMSSW by  doxygen 1.5.4