CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/SimGeneral/MixingModule/plugins/MixingWorker.h

Go to the documentation of this file.
00001 #ifndef MixingWorker_h
00002 #define MixingWorker_h
00003 
00015 #include "FWCore/Framework/interface/Event.h"
00016 #include "FWCore/Framework/interface/EventPrincipal.h"
00017 #include "DataFormats/Common/interface/Wrapper.h"
00018 #include "DataFormats/Common/interface/Handle.h"
00019 
00020 #include "FWCore/Framework/interface/ESHandle.h"
00021 #include "FWCore/Framework/interface/EventSetup.h"
00022 #include "CondFormats/DataRecord/interface/MixingRcd.h"
00023 #include "CondFormats/RunInfo/interface/MixingModuleConfig.h"
00024 
00025 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00026 #include "SimDataFormats/CrossingFrame/interface/PCrossingFrame.h"
00027 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00028 #include "FWCore/Utilities/interface/InputTag.h" 
00029 
00030 #include <vector>
00031 #include <string>
00032 #include <typeinfo>
00033 #include "MixingWorkerBase.h"
00034 
00035 class SimTrack;
00036 class SimVertex;
00037 namespace edm
00038 {
00039   template <class T> 
00040     class MixingWorker: public MixingWorkerBase 
00041     {
00042     public:
00043 
00045       explicit MixingWorker() :
00046         minBunch_(-5),
00047         maxBunch_(3),
00048         bunchSpace_(75),
00049         subdet_(std::string(" ")),
00050         label_(std::string(" ")),
00051         labelCF_(std::string(" ")),
00052         maxNbSources_(5),
00053         mixProdStep2_(false)
00054           {
00055             tag_=InputTag();
00056             tagSignal_=InputTag();
00057           }
00058 
00059       /*Normal constructor*/ 
00060       MixingWorker(int minBunch,int maxBunch, int bunchSpace,
00061                    std::string subdet,std::string label,
00062                    std::string labelCF,int maxNbSources, InputTag& tag,
00063                    InputTag& tagCF, bool mixProdStep2):
00064         MixingWorkerBase(),
00065         minBunch_(minBunch),
00066         maxBunch_(maxBunch),
00067         bunchSpace_(bunchSpace),
00068         subdet_(subdet),
00069         label_(label),
00070         labelCF_(labelCF),
00071         maxNbSources_(maxNbSources),
00072         tag_(tag),
00073         tagSignal_(tagCF)
00074         {
00075           mixProdStep2_ = mixProdStep2;
00076         }
00077 
00079       virtual ~MixingWorker() {;}
00080 
00081     public:
00082 
00083       virtual void reload(const edm::EventSetup & setup){
00084         //get the required parameters from DB.
00085         // watch the label/tag
00086         edm::ESHandle<MixingModuleConfig> config;
00087         setup.get<MixingRcd>().get(config);
00088         minBunch_=config->minBunch();
00089         maxBunch_=config->maxBunch();
00090         bunchSpace_=config->bunchSpace();
00091       }
00092 
00093       virtual bool checkSignal(const edm::Event &e){
00094           bool got;
00095           InputTag t;
00096           edm::Handle<std::vector<T> >  result_t;
00097           if (mixProdStep2_){   
00098              got = e.getByLabel(tagSignal_,result_t);
00099              t = InputTag(tagSignal_.label(),tagSignal_.instance());   
00100           }
00101           else{
00102              got = e.getByLabel(tag_,result_t);
00103              t = InputTag(tag_.label(),tag_.instance());
00104           }
00105           
00106           if (got)
00107                LogInfo("MixingModule") <<" Will create a CrossingFrame for "<< typeid(T).name() 
00108                                        << " with InputTag= "<< t.encode();
00109                                        
00110           return got;
00111       }
00112       
00113       
00114       virtual void createnewEDProduct(){        
00115           crFrame_=new CrossingFrame<T>(minBunch_,maxBunch_,bunchSpace_,subdet_,maxNbSources_);
00116       }
00117            
00118       virtual void addSignals(const edm::Event &e){
00119         if (mixProdStep2_){       
00120           edm::Handle<std::vector<T> >  result_t;
00121           bool got = e.getByLabel(tagSignal_,result_t);
00122           if (got) {
00123             LogDebug("MixingModule") <<" adding " << result_t.product()->size()<<" signal objects for "<<typeid(T).name()<<" with "<<tagSignal_;
00124             crFrame_->addSignals(result_t.product(),e.id());
00125           }
00126           else    LogInfo("MixingModule") <<"!!!!!!! Did not get any signal data for "<<typeid(T).name()<<", with "<<tagSignal_;
00127         }
00128         else{
00129         // Default version
00130           edm::Handle<std::vector<T> >  result_t;
00131           bool got = e.getByLabel(tag_,result_t);
00132           if (got) {
00133             LogDebug("MixingModule") <<" adding " << result_t.product()->size()<<" signal objects for "<<typeid(T).name()<<" with "<<tag_;
00134             crFrame_->addSignals(result_t.product(),e.id());
00135           }
00136           else    LogInfo("MixingModule") <<"!!!!!!! Did not get any signal data for "<<typeid(T).name()<<", with "<<tag_;
00137 
00138         }
00139         
00140       }
00141 
00142       virtual void addPileups(const int bcr, const EventPrincipal &ep, unsigned int eventNr,int vertexoffset);
00143 
00144       virtual void setBcrOffset() {crFrame_->setBcrOffset();}
00145       virtual void setSourceOffset(const unsigned int s) {crFrame_->setSourceOffset(s);}
00146 
00147       void setTof();
00148 
00149       virtual void put(edm::Event &e) { 
00150         std::auto_ptr<CrossingFrame<T> > pOut(crFrame_);
00151         if (!mixProdStep2_){
00152           e.put(pOut,label_);
00153           LogDebug("MixingModule") <<" CF was put for type "<<typeid(T).name()<<" with "<<label_;
00154         }
00155         else {
00156           e.put(pOut,labelCF_);
00157           LogDebug("MixingModule") <<" CF was put for type "<<typeid(T).name()<<" with "<<labelCF_;
00158         }
00159       }
00160 
00161 
00162       // When using mixed secondary source 
00163       // Copy the data from the PCrossingFrame to the CrossingFrame
00164       virtual void copyPCrossingFrame(const PCrossingFrame<T> *PCF);
00165       
00166     private:
00167       int minBunch_;
00168       int maxBunch_;
00169       int bunchSpace_;
00170       std::string const subdet_;
00171       std::string const label_;
00172       std::string const labelCF_;
00173       unsigned int const maxNbSources_;
00174       InputTag tag_;
00175       InputTag tagSignal_;
00176       bool mixProdStep2_;
00177 
00178       CrossingFrame<T> * crFrame_;
00179       PCrossingFrame<T> * secSourceCF_;
00180     };
00181 
00182 //=============== template specializations ====================================================================================
00183   template <class T>
00184     void MixingWorker<T>::addPileups(const int bcr, const EventPrincipal &ep, unsigned int eventNr,int vertexoffset)
00185     {
00186       if (!mixProdStep2_){
00187         // default version
00188         // valid for CaloHits 
00189         boost::shared_ptr<Wrapper<std::vector<T> > const> shPtr =
00190         edm::getProductByTag<std::vector<T> >(ep, tag_);
00191 
00192         if (shPtr) {
00193           LogDebug("MixingModule") <<shPtr->product()->size()<<"  pileup objects  added, eventNr "<<eventNr;
00194           crFrame_->setPileupPtr(shPtr);
00195           crFrame_->addPileups(bcr,const_cast< std::vector<T> * >(shPtr->product()),eventNr);
00196         }
00197         
00198       }
00199       else
00200       {
00201         boost::shared_ptr<Wrapper<PCrossingFrame<T> > const> shPtr = getProductByTag<PCrossingFrame<T> >(ep, tag_);
00202      
00203         if (shPtr){                     
00204           crFrame_->setPileupPtr(shPtr);
00205           secSourceCF_ = const_cast<PCrossingFrame<T> * >(shPtr->product());
00206           LogDebug("MixingModule") << "Add PCrossingFrame<T>  eventNr " << secSourceCF_->getEventID();
00207 
00208           copyPCrossingFrame(secSourceCF_);
00209 
00210         }
00211         else
00212           LogDebug("MixingModule") << "Could not get the PCrossingFrame<T>!";
00213       }
00214     }
00215 
00216     
00217 template <>
00218     void MixingWorker<PSimHit>::addPileups(const int bcr, const EventPrincipal &ep, unsigned int eventNr,int vertexoffset);
00219 
00220 template <>
00221     void MixingWorker<SimTrack>::addPileups(const int bcr, const EventPrincipal &ep, unsigned int eventNr,int vertexoffset);
00222 
00223 template <>
00224     void MixingWorker<SimVertex>::addPileups(const int bcr, const EventPrincipal& ep, unsigned int eventNr,int vertexoffset);
00225 
00226 template <>
00227     void MixingWorker<HepMCProduct>::addPileups(const int bcr, const EventPrincipal &ep, unsigned int eventNr,int vertexoffset);
00228 
00229 template <class T>
00230     void MixingWorker<T>::setTof() {;}
00231 
00232 template <class T>
00233     void MixingWorker<T>::copyPCrossingFrame(const PCrossingFrame<T> *PCF)
00234     { 
00235       crFrame_->setBunchRange(PCF->getBunchRange()); 
00236       crFrame_->setBunchSpace(PCF->getBunchSpace());
00237       crFrame_->setMaxNbSources(PCF->getMaxNbSources());
00238       crFrame_->setSubDet(PCF->getSubDet());
00239       crFrame_->setPileupOffsetsBcr(PCF->getPileupOffsetsBcr());
00240       crFrame_->setPileupOffsetsSource(PCF->getPileupOffsetsSource());
00241       crFrame_->setPileups(PCF->getPileups());
00242       
00243       // For playback option
00244       crFrame_->setPileupFileNr(PCF->getPileupFileNr());
00245       crFrame_->setIdFirstPileup(PCF->getIdFirstPileup());
00246     }
00247       
00248 }//edm
00249 
00250 #endif