Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "PreMixingPileupCopy.h"
0002 
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "SimGeneral/MixingModule/interface/PileUpEventPrincipal.h"
0005 #include "DataFormats/Common/interface/Handle.h"
0006 
0007 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0008 
0009 #include <memory>
0010 
0011 namespace edm {
0012   PreMixingPileupCopy::PreMixingPileupCopy(const edm::ParameterSet& ps,
0013                                            edm::ProducesCollector producesCollector,
0014                                            edm::ConsumesCollector&& iC)
0015       : pileupInfoInputTag_(ps.getParameter<edm::InputTag>("PileupInfoInputTag")),
0016         bunchSpacingInputTag_(ps.getParameter<edm::InputTag>("BunchSpacingInputTag")),
0017         cfPlaybackInputTag_(ps.getParameter<edm::InputTag>("CFPlaybackInputTag")),
0018         genPUProtonsInputTags_(ps.getParameter<std::vector<edm::InputTag>>("GenPUProtonsInputTags")) {
0019     producesCollector.produces<std::vector<PileupSummaryInfo>>();
0020     producesCollector.produces<int>("bunchSpacing");
0021     producesCollector.produces<CrossingFramePlaybackInfoNew>();
0022 
0023     for (const auto& tag : genPUProtonsInputTags_) {
0024       producesCollector.produces<std::vector<reco::GenParticle>>(tag.label());
0025     }
0026   }
0027 
0028   float PreMixingPileupCopy::getTrueNumInteractions(PileUpEventPrincipal const& pep) const {
0029     edm::Handle<std::vector<PileupSummaryInfo>> pileupInfoHandle;
0030     pep.getByLabel(pileupInfoInputTag_, pileupInfoHandle);
0031 
0032     auto it = std::find_if(
0033         pileupInfoHandle->begin(), pileupInfoHandle->end(), [](const auto& s) { return s.getBunchCrossing() == 0; });
0034     if (it == pileupInfoHandle->end()) {
0035       throw cms::Exception("LogicError") << "Did not find PileupSummaryInfo in bunch crossing 0";
0036     }
0037 
0038     return it->getTrueNumInteractions();
0039   }
0040 
0041   void PreMixingPileupCopy::addPileupInfo(const PileUpEventPrincipal& pep) {
0042     LogDebug("PreMixingPileupCopy") << "\n===============> adding pileup Info from event  " << pep.principal().id();
0043 
0044     // find PileupSummaryInfo, CFPlayback information, if it's there
0045 
0046     // Pileup info first
0047     edm::Handle<std::vector<PileupSummaryInfo>> pileupInfoHandle;
0048     pep.getByLabel(pileupInfoInputTag_, pileupInfoHandle);
0049 
0050     edm::Handle<int> bsHandle;
0051     pep.getByLabel(bunchSpacingInputTag_, bsHandle);
0052 
0053     if (pileupInfoHandle.isValid()) {
0054       pileupSummaryStorage_ = *pileupInfoHandle;
0055       LogDebug("PreMixingPileupCopy") << "PileupInfo Size: " << pileupSummaryStorage_.size();
0056     }
0057     bsStorage_ = bsHandle.isValid() ? *bsHandle : 10000;
0058 
0059     // Gen. PU protons
0060     edm::Handle<std::vector<reco::GenParticle>> genPUProtonsHandle;
0061     for (const auto& tag : genPUProtonsInputTags_) {
0062       pep.getByLabel(tag, genPUProtonsHandle);
0063       if (genPUProtonsHandle.isValid()) {
0064         genPUProtons_.push_back(*genPUProtonsHandle);
0065         genPUProtons_labels_.push_back(tag.label());
0066       } else {
0067         edm::LogWarning("PreMixingPileupCopy") << "Missing product with label: " << tag.label();
0068       }
0069     }
0070 
0071     // Playback
0072     edm::Handle<CrossingFramePlaybackInfoNew> playbackHandle;
0073     pep.getByLabel(cfPlaybackInputTag_, playbackHandle);
0074     foundPlayback_ = false;
0075     if (playbackHandle.isValid()) {
0076       crossingFramePlaybackStorage_ = *playbackHandle;
0077       foundPlayback_ = true;
0078     }
0079   }
0080 
0081   void PreMixingPileupCopy::putPileupInfo(edm::Event& e) {
0082     if (foundPlayback_) {
0083       e.put(std::make_unique<CrossingFramePlaybackInfoNew>(std::move(crossingFramePlaybackStorage_)));
0084     }
0085     e.put(std::make_unique<std::vector<PileupSummaryInfo>>(std::move(pileupSummaryStorage_)));
0086     e.put(std::make_unique<int>(bsStorage_), "bunchSpacing");
0087 
0088     // Gen. PU protons
0089     for (size_t idx = 0; idx < genPUProtons_.size(); ++idx) {
0090       e.put(std::make_unique<std::vector<reco::GenParticle>>(std::move(genPUProtons_[idx])), genPUProtons_labels_[idx]);
0091     }
0092 
0093     // clear local storage after this event
0094     pileupSummaryStorage_.clear();
0095     genPUProtons_.clear();
0096     genPUProtons_labels_.clear();
0097   }
0098 }  // namespace edm