CMS 3D CMS Logo

PreMixingCaloParticleWorker.cc
Go to the documentation of this file.
7 
13 
16 
18 public:
20  ~PreMixingCaloParticleWorker() override = default;
21 
22  void initializeEvent(edm::Event const& iEvent, edm::EventSetup const& iSetup) override;
23  void addSignals(edm::Event const& iEvent, edm::EventSetup const& iSetup) override;
24  void addPileups(PileUpEventPrincipal const& pep, edm::EventSetup const& iSetup) override;
25  void put(edm::Event& iEvent, edm::EventSetup const& iSetup, std::vector<PileupSummaryInfo> const& ps, int bunchSpacing) override;
26 
27 private:
28  using EnergyMap = std::vector<std::pair<unsigned, float> >;
29 
30  void add(const SimClusterCollection& clusters, const CaloParticleCollection& particles, const EnergyMap& energyMap);
31 
35 
38 
39  std::unordered_map<unsigned, float> totalEnergy_;
40 
41  std::unique_ptr<SimClusterCollection> newClusters_;
42  std::unique_ptr<CaloParticleCollection> newParticles_;
44 };
45 
47  sigClusterToken_(iC.consumes<SimClusterCollection>(ps.getParameter<edm::InputTag>("labelSig"))),
48  sigParticleToken_(iC.consumes<CaloParticleCollection>(ps.getParameter<edm::InputTag>("labelSig"))),
49  sigEnergyToken_(iC.consumes<EnergyMap>(ps.getParameter<edm::InputTag>("labelSig"))),
50  particlePileInputTag_(ps.getParameter<edm::InputTag>("pileInputTag")),
51  particleCollectionDM_(ps.getParameter<std::string>("collectionDM"))
52 {
55 }
56 
58  newClusters_ = std::make_unique<SimClusterCollection>();
59  newParticles_ = std::make_unique<CaloParticleCollection>();
60 
61  // need RefProds in order to re-key the CaloParticle->SimCluster refs
62  // TODO: try to remove const_cast, requires making Event non-const in BMixingModule::initializeEvent
63  clusterRef_ = const_cast<edm::Event&>(iEvent).getRefBeforePut<SimClusterCollection>(particleCollectionDM_);
64 }
65 
68  iEvent.getByToken(sigClusterToken_, clusters);
69 
71  iEvent.getByToken(sigParticleToken_, particles);
72 
74  iEvent.getByToken(sigEnergyToken_, energy);
75 
76  if(clusters.isValid() && particles.isValid() && energy.isValid()) {
77  add(*clusters, *particles, *energy);
78  }
79 }
80 
83  pep.getByLabel(particlePileInputTag_, clusters);
84 
86  pep.getByLabel(particlePileInputTag_, particles);
87 
89  pep.getByLabel(particlePileInputTag_, energy);
90 
91  if(clusters.isValid() && particles.isValid() && energy.isValid()) {
92  add(*clusters, *particles, *energy);
93  }
94 }
95 
97  const size_t startingIndex = newClusters_->size();
98 
99  // Copy SimClusters
100  newClusters_->reserve(newClusters_->size() + clusters.size());
101  std::copy(clusters.begin(), clusters.end(), std::back_inserter(*newClusters_));
102 
103  // Copy CaloParticles
104  newParticles_->reserve(newParticles_->size() + particles.size());
105  for(const auto& p: particles) {
106  newParticles_->push_back(p);
107  auto& particle = newParticles_->back();
108 
109  // re-key the refs to SimClusters
110  particle.clearSimClusters();
111  for(const auto& ref: p.simClusters()) {
112  particle.addSimCluster(SimClusterRef(clusterRef_, startingIndex + ref.index()));
113  }
114  }
115 
116  // Add energies
117  for(const auto elem: energy) {
118  totalEnergy_[elem.first] += elem.second;
119  }
120 }
121 
122 void PreMixingCaloParticleWorker::put(edm::Event& iEvent, edm::EventSetup const& iSetup, std::vector<PileupSummaryInfo> const& ps, int bunchSpacing) {
123  for (auto& sc : *newClusters_) {
124  auto hitsAndEnergies = sc.hits_and_fractions();
126  for (auto& hAndE : hitsAndEnergies) {
127  const float totalenergy = totalEnergy_[hAndE.first];
128  float fraction = 0.;
129  if (totalenergy > 0)
130  fraction = hAndE.second / totalenergy;
131  else
132  edm::LogWarning("PreMixingParticleWorker") << "TotalSimEnergy for hit " << hAndE.first
133  << " is 0! The fraction for this hit cannot be computed.";
134  sc.addRecHitAndFraction(hAndE.first, fraction);
135  }
136  }
137 
138  // clear memory
139  std::unordered_map<unsigned, float>{}.swap(totalEnergy_);
140 
141  iEvent.put(std::move(newClusters_), particleCollectionDM_);
143 }
144 
BranchAliasSetterT< ProductType > produces()
declare what type of product will make and with which optional label
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
void addSignals(edm::Event const &iEvent, edm::EventSetup const &iSetup) override
def copy(args, dbName)
std::vector< std::pair< uint32_t, float > > hits_and_fractions() const
Returns list of rechit IDs and fractions for this SimCluster.
Definition: SimCluster.h:210
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
edm::Ref< SimClusterCollection > SimClusterRef
Definition: SimClusterFwd.h:10
void addPileups(PileUpEventPrincipal const &pep, edm::EventSetup const &iSetup) override
edm::EDGetTokenT< CaloParticleCollection > sigParticleToken_
edm::EDGetTokenT< EnergyMap > sigEnergyToken_
void put(edm::Event &iEvent, edm::EventSetup const &iSetup, std::vector< PileupSummaryInfo > const &ps, int bunchSpacing) override
void initializeEvent(edm::Event const &iEvent, edm::EventSetup const &iSetup) override
void addRecHitAndFraction(uint32_t hit, float fraction)
add rechit with fraction
Definition: SimCluster.h:204
int iEvent
Definition: GenABIO.cc:230
~PreMixingCaloParticleWorker() override=default
std::unordered_map< unsigned, float > totalEnergy_
void clearHitsAndFractions()
clear the hits and fractions list
Definition: SimCluster.h:219
std::unique_ptr< SimClusterCollection > newClusters_
edm::EDGetTokenT< SimClusterCollection > sigClusterToken_
std::vector< std::pair< unsigned, float > > EnergyMap
bool isValid() const
Definition: HandleBase.h:74
PreMixingCaloParticleWorker(const edm::ParameterSet &ps, edm::ProducerBase &producer, edm::ConsumesCollector &&iC)
def elem(elemtype, innerHTML='', html_class='', kwargs)
Definition: HTMLExport.py:18
void add(const SimClusterCollection &clusters, const CaloParticleCollection &particles, const EnergyMap &energyMap)
HLT enums.
std::vector< CaloParticle > CaloParticleCollection
bool getByLabel(edm::InputTag const &tag, edm::Handle< T > &result) const
std::vector< SimCluster > SimClusterCollection
Definition: SimClusterFwd.h:8
#define DEFINE_PREMIXING_WORKER(TYPE)
std::unique_ptr< CaloParticleCollection > newParticles_
def move(src, dest)
Definition: eostools.py:510