Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:33

0001 // File: DataMixingPileupCopy.cc
0002 // Description:  see DataMixingPileupCopy.h
0003 // Author:  Mike Hildreth, University of Notre Dame
0004 //
0005 //--------------------------------------------
0006 #include "DataFormats/Common/interface/Handle.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include <map>
0009 #include <memory>
0010 
0011 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0012 //
0013 //
0014 #include "DataMixingPileupCopy.h"
0015 
0016 using namespace std;
0017 
0018 namespace edm {
0019 
0020   // Virtual constructor
0021 
0022   DataMixingPileupCopy::DataMixingPileupCopy() {}
0023 
0024   // Constructor
0025   DataMixingPileupCopy::DataMixingPileupCopy(const edm::ParameterSet &ps, edm::ConsumesCollector &&iC)
0026       : label_(ps.getParameter<std::string>("Label"))
0027 
0028   {
0029     // Pileup/Playback information
0030 
0031     PileupInfoInputTag_ = ps.getParameter<edm::InputTag>("PileupInfoInputTag");
0032     BunchSpacingInputTag_ = ps.getParameter<edm::InputTag>("BunchSpacingInputTag");
0033     CFPlaybackInputTag_ = ps.getParameter<edm::InputTag>("CFPlaybackInputTag");
0034 
0035     GenPUProtonsInputTags_ = ps.getParameter<std::vector<edm::InputTag>>("GenPUProtonsInputTags");
0036 
0037     // apparently, we don't need consumes from Secondary input stream
0038     // iC.consumes<std::vector<PileupSummaryInfo>>(PileupInfoInputTag_);
0039     // iC.consumes<int>(BunchSpacingInputTag_);
0040     // iC.consumes<CrossingFramePlaybackInfoNew>(CFPlaybackInputTag_);
0041   }
0042 
0043   // Virtual destructor needed.
0044   DataMixingPileupCopy::~DataMixingPileupCopy() {}
0045 
0046   void DataMixingPileupCopy::addPileupInfo(const EventPrincipal *ep,
0047                                            unsigned int eventNr,
0048                                            ModuleCallingContext const *mcc) {
0049     LogDebug("DataMixingPileupCopy") << "\n===============> adding pileup Info from event  " << ep->id();
0050 
0051     // find PileupSummaryInfo, CFPlayback information, if it's there
0052 
0053     // Pileup info first
0054 
0055     std::shared_ptr<Wrapper<std::vector<PileupSummaryInfo>> const> PileupInfoPTR =
0056         getProductByTag<std::vector<PileupSummaryInfo>>(*ep, PileupInfoInputTag_, mcc);
0057 
0058     std::shared_ptr<Wrapper<int> const> bsPTR = getProductByTag<int>(*ep, BunchSpacingInputTag_, mcc);
0059 
0060     if (PileupInfoPTR) {
0061       PileupSummaryStorage_ = *(PileupInfoPTR->product());
0062       LogDebug("DataMixingEMWorker") << "PileupInfo Size: " << PileupSummaryStorage_.size();
0063     }
0064 
0065     if (bsPTR) {
0066       bsStorage_ = *(bsPTR->product());
0067     } else {
0068       bsStorage_ = 10000;
0069     }
0070 
0071     // Gen. PU protons
0072     std::shared_ptr<edm::Wrapper<std::vector<reco::GenParticle>> const> GenPUProtonsPTR;
0073     for (std::vector<edm::InputTag>::const_iterator it_InputTag = GenPUProtonsInputTags_.begin();
0074          it_InputTag != GenPUProtonsInputTags_.end();
0075          ++it_InputTag) {
0076       GenPUProtonsPTR = getProductByTag<std::vector<reco::GenParticle>>(*ep, *it_InputTag, mcc);
0077       if (GenPUProtonsPTR != nullptr) {
0078         GenPUProtons_.push_back(*(GenPUProtonsPTR->product()));
0079         GenPUProtons_labels_.push_back(it_InputTag->label());
0080       } else
0081         edm::LogWarning("DataMixingPileupCopy") << "Missing product with label: " << (*it_InputTag).label();
0082     }
0083 
0084     // Playback
0085     std::shared_ptr<Wrapper<CrossingFramePlaybackInfoNew> const> PlaybackPTR =
0086         getProductByTag<CrossingFramePlaybackInfoNew>(*ep, CFPlaybackInputTag_, mcc);
0087     FoundPlayback_ = false;
0088     if (PlaybackPTR) {
0089       CrossingFramePlaybackStorage_ = *(PlaybackPTR->product());
0090       FoundPlayback_ = true;
0091     }
0092   }
0093 
0094   void DataMixingPileupCopy::putPileupInfo(edm::Event &e) {
0095     std::unique_ptr<std::vector<PileupSummaryInfo>> PSIVector(new std::vector<PileupSummaryInfo>);
0096     std::unique_ptr<int> bsInt(new int);
0097 
0098     std::vector<PileupSummaryInfo>::const_iterator PSiter;
0099     for (PSiter = PileupSummaryStorage_.begin(); PSiter != PileupSummaryStorage_.end(); PSiter++) {
0100       PSIVector->push_back(*PSiter);
0101     }
0102 
0103     *bsInt = bsStorage_;
0104 
0105     if (FoundPlayback_) {
0106       std::unique_ptr<CrossingFramePlaybackInfoNew> CFPlaybackInfo(
0107           new CrossingFramePlaybackInfoNew(CrossingFramePlaybackStorage_));
0108       e.put(std::move(CFPlaybackInfo));
0109     }
0110     e.put(std::move(PSIVector));
0111     e.put(std::move(bsInt), "bunchSpacing");
0112 
0113     // Gen. PU protons
0114     for (size_t idx = 0; idx < GenPUProtons_.size(); ++idx) {
0115       std::unique_ptr<std::vector<reco::GenParticle>> GenPUProtons_ptr(new std::vector<reco::GenParticle>());
0116       std::vector<reco::GenParticle>::const_iterator it_GenParticle = GenPUProtons_.at(idx).begin();
0117       std::vector<reco::GenParticle>::const_iterator it_GenParticle_end = GenPUProtons_.at(idx).end();
0118       for (; it_GenParticle != it_GenParticle_end; ++it_GenParticle)
0119         GenPUProtons_ptr->push_back(*it_GenParticle);
0120 
0121       e.put(std::move(GenPUProtons_ptr), GenPUProtons_labels_.at(idx));
0122     }
0123 
0124     // clear local storage after this event
0125     PileupSummaryStorage_.clear();
0126     GenPUProtons_.clear();
0127     GenPUProtons_labels_.clear();
0128   }
0129 }  // namespace edm