Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-02 03:46:12

0001 #ifndef MixingWorker_h
0002 #define MixingWorker_h
0003 
0004 /** \class MixingWorker
0005  *
0006  * MixingWorker is an auxiliary class for the MixingModule
0007  *
0008  * \author Ursula Berthon, LLR Palaiseau
0009  *
0010  * \version   1st Version JMarch 2008
0011 
0012  *
0013  ************************************************************/
0014 
0015 #include "FWCore/Framework/interface/Event.h"
0016 #include "FWCore/Framework/interface/EventPrincipal.h"
0017 #include "DataFormats/Common/interface/Wrapper.h"
0018 #include "DataFormats/Common/interface/Handle.h"
0019 
0020 #include "FWCore/Framework/interface/EventSetup.h"
0021 
0022 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
0023 #include "SimDataFormats/CrossingFrame/interface/PCrossingFrame.h"
0024 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0025 #include "FWCore/Utilities/interface/InputTag.h"
0026 
0027 #include <memory>
0028 #include <vector>
0029 #include <string>
0030 #include <typeinfo>
0031 #include "MixingWorkerBase.h"
0032 
0033 class SimTrack;
0034 class SimVertex;
0035 namespace edm {
0036   template <class T>
0037   class MixingWorker : public MixingWorkerBase {
0038   public:
0039     /** standard constructor*/
0040     explicit MixingWorker()
0041         : minBunch_(-5),
0042           maxBunch_(3),
0043           bunchSpace_(75),
0044           subdet_(std::string(" ")),
0045           label_(std::string(" ")),
0046           labelCF_(std::string(" ")),
0047           maxNbSources_(5),
0048           makePCrossingFrame_(false),
0049           tag_(),
0050           tagSignal_(),
0051           allTags_(),
0052           crFrame_(nullptr) {}
0053 
0054     /*Normal constructor*/
0055     MixingWorker(int minBunch,
0056                  int maxBunch,
0057                  int bunchSpace,
0058                  std::string subdet,
0059                  std::string label,
0060                  std::string labelCF,
0061                  int maxNbSources,
0062                  InputTag &tag,
0063                  InputTag &tagCF,
0064                  bool makePCrossingFrame = false)
0065         : MixingWorkerBase(),
0066           minBunch_(minBunch),
0067           maxBunch_(maxBunch),
0068           bunchSpace_(bunchSpace),
0069           subdet_(subdet),
0070           label_(label),
0071           labelCF_(labelCF),
0072           maxNbSources_(maxNbSources),
0073           makePCrossingFrame_(makePCrossingFrame),
0074           tag_(tag),
0075           tagSignal_(tagCF),
0076           allTags_(),
0077           crFrame_(nullptr) {}
0078 
0079     /*constructor for HepMCproduct case*/
0080     MixingWorker(int minBunch,
0081                  int maxBunch,
0082                  int bunchSpace,
0083                  std::string subdet,
0084                  std::string label,
0085                  std::string labelCF,
0086                  int maxNbSources,
0087                  InputTag &tag,
0088                  InputTag &tagCF,
0089                  std::vector<InputTag> const &tags)
0090         : MixingWorkerBase(),
0091           minBunch_(minBunch),
0092           maxBunch_(maxBunch),
0093           bunchSpace_(bunchSpace),
0094           subdet_(subdet),
0095           label_(label),
0096           labelCF_(labelCF),
0097           maxNbSources_(maxNbSources),
0098           makePCrossingFrame_(false),
0099           tag_(tag),
0100           tagSignal_(tagCF),
0101           allTags_(tags),
0102           crFrame_(nullptr) {}
0103 
0104     /**Default destructor*/
0105     ~MixingWorker() override { ; }
0106 
0107   public:
0108     void reload(int minBunch, int maxBunch, int bunchSpace) override {
0109       minBunch_ = minBunch;
0110       maxBunch_ = maxBunch;
0111       bunchSpace_ = bunchSpace;
0112     }
0113 
0114     bool checkSignal(const edm::Event &e) override {
0115       bool got;
0116       InputTag t;
0117       edm::Handle<std::vector<T> > result_t;
0118       got = e.getByLabel(tag_, result_t);
0119       t = InputTag(tag_.label(), tag_.instance());
0120 
0121       if (got)
0122         LogInfo("MixingModule") << " Will create a CrossingFrame for " << typeid(T).name()
0123                                 << " with InputTag= " << t.encode();
0124 
0125       return got;
0126     }
0127 
0128     void createnewEDProduct() override {
0129       crFrame_ = std::make_unique<CrossingFrame<T> >(minBunch_, maxBunch_, bunchSpace_, subdet_, maxNbSources_);
0130     }
0131 
0132     void addSignals(const edm::Event &e) override {
0133       edm::Handle<std::vector<T> > result_t;
0134       bool got = e.getByLabel(tag_, result_t);
0135       if (got) {
0136         LogDebug("MixingModule") << " adding " << result_t.product()->size() << " signal objects for "
0137                                  << typeid(T).name() << " with " << tag_;
0138         crFrame_->addSignals(result_t.product(), e.id());
0139       } else {
0140         LogInfo("MixingModule") << "!!!!!!! Did not get any signal data for " << typeid(T).name() << ", with " << tag_;
0141       }
0142     }
0143 
0144     void addPileups(const EventPrincipal &ep, ModuleCallingContext const *, unsigned int eventNr) override;
0145 
0146     void setBcrOffset() override { crFrame_->setBcrOffset(); }
0147     void setSourceOffset(const unsigned int s) override { crFrame_->setSourceOffset(s); }
0148 
0149     void setTof() override;
0150 
0151     void put(edm::Event &e) override {
0152       if (makePCrossingFrame_) {
0153         e.put(std::make_unique<PCrossingFrame<T> >(*crFrame_), label_);
0154       }
0155       e.put(std::move(crFrame_), label_);
0156       LogDebug("MixingModule") << " CF was put for type " << typeid(T).name() << " with " << label_;
0157     }
0158 
0159     // When using mixed secondary source
0160     // Copy the data from the PCrossingFrame to the CrossingFrame
0161     virtual void copyPCrossingFrame(const PCrossingFrame<T> *PCF);
0162 
0163   private:
0164     int minBunch_;
0165     int maxBunch_;
0166     int bunchSpace_;
0167     std::string const subdet_;
0168     std::string const label_;
0169     std::string const labelCF_;
0170     unsigned int const maxNbSources_;
0171     bool const makePCrossingFrame_;
0172     InputTag tag_;
0173     InputTag tagSignal_;
0174     std::vector<InputTag> allTags_;  // for HepMCProduct
0175 
0176     std::unique_ptr<CrossingFrame<T> > crFrame_;
0177   };
0178 
0179   template <typename T>
0180   void MixingWorker<T>::addPileups(const EventPrincipal &ep, ModuleCallingContext const *mcc, unsigned int eventNr) {
0181     std::shared_ptr<Wrapper<std::vector<T> > const> shPtr = getProductByTag<std::vector<T> >(ep, tag_, mcc);
0182     if (shPtr) {
0183       LogDebug("MixingModule") << shPtr->product()->size() << "  pileup objects  added, eventNr " << eventNr;
0184       crFrame_->setPileupPtr(shPtr);
0185       crFrame_->addPileups(*shPtr->product());
0186     }
0187   }
0188   //=============== template specializations ====================================================================================
0189 
0190   template <>
0191   void MixingWorker<HepMCProduct>::addPileups(const EventPrincipal &ep,
0192                                               ModuleCallingContext const *,
0193                                               unsigned int eventNr);
0194 
0195   template <class T>
0196   void MixingWorker<T>::setTof() {
0197     ;
0198   }
0199 
0200   template <class T>
0201   void MixingWorker<T>::copyPCrossingFrame(const PCrossingFrame<T> *PCF) {
0202     crFrame_->setBunchRange(PCF->getBunchRange());
0203     crFrame_->setBunchSpace(PCF->getBunchSpace());
0204     crFrame_->setMaxNbSources(PCF->getMaxNbSources());
0205     crFrame_->setSubDet(PCF->getSubDet());
0206     crFrame_->setPileupOffsetsBcr(PCF->getPileupOffsetsBcr());
0207     crFrame_->setPileupOffsetsSource(PCF->getPileupOffsetsSource());
0208     crFrame_->setPileups(PCF->getPileups());
0209 
0210     // For playback option
0211     crFrame_->setPileupFileNr(PCF->getPileupFileNr());
0212     crFrame_->setIdFirstPileup(PCF->getIdFirstPileup());
0213   }
0214 
0215 }  // namespace edm
0216 
0217 #endif