CMS 3D CMS Logo

BetaStarVarProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PhysicsTools/NanoAOD
4 // Class: BetaStarVarProducer
5 //
18 //
19 // Original Author: Sal Rappoccio
20 //
21 //
22 
23 
24 #include <memory>
25 
28 
31 
34 
38 
40 
42 
43 template <typename T>
45  public:
46  explicit BetaStarVarProducer(const edm::ParameterSet &iConfig):
47  srcJet_(consumes<edm::View<pat::Jet>>(iConfig.getParameter<edm::InputTag>("srcJet"))),
48  srcPF_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("srcPF"))),
49  maxDR_( iConfig.getParameter<double>("maxDR") )
50  {
51  produces<edm::ValueMap<float>>("chargedHadronPUEnergyFraction");
52  produces<edm::ValueMap<float>>("chargedHadronCHSEnergyFraction");
53  }
54  ~BetaStarVarProducer() override {};
55 
56  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
57 
58  private:
59  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
60 
61  std::tuple<float,float> calculateCHSEnergies( edm::Ptr<pat::Jet> const & jet, edm::View<T> const & cands) const;
62 
65  double maxDR_;
66 };
67 
68 template <typename T>
69 void
71 {
72 
74  iEvent.getByToken(srcJet_, srcJet);
76  iEvent.getByToken(srcPF_, srcPF);
77 
78  unsigned int nJet = srcJet->size();
79 
80  std::vector<float> chargedHadronPUEnergyFraction(nJet,-1);
81  std::vector<float> chargedHadronCHSEnergyFraction(nJet,-1);
82 
83  for ( unsigned int ij = 0; ij < nJet; ++ij ) {
84  auto jet = srcJet->ptrAt(ij);
85  std::tuple<float,float> vals = calculateCHSEnergies( jet, *srcPF );
86  auto chpuf = std::get<0>(vals);
87  auto chef = std::get<1>(vals);
88  chargedHadronPUEnergyFraction[ij] = chpuf;
89  chargedHadronCHSEnergyFraction[ij] = chef;
90  }
91 
92  std::unique_ptr<edm::ValueMap<float>> chargedHadronPUEnergyFractionV(new edm::ValueMap<float>());
93  edm::ValueMap<float>::Filler fillerPU(*chargedHadronPUEnergyFractionV);
94  fillerPU.insert(srcJet,chargedHadronPUEnergyFraction.begin(),chargedHadronPUEnergyFraction.end());
95  fillerPU.fill();
96  iEvent.put(std::move(chargedHadronPUEnergyFractionV),"chargedHadronPUEnergyFraction");
97 
98  std::unique_ptr<edm::ValueMap<float>> chargedHadronCHSEnergyFractionV(new edm::ValueMap<float>());
99  edm::ValueMap<float>::Filler fillerCHE(*chargedHadronCHSEnergyFractionV);
100  fillerCHE.insert(srcJet,chargedHadronCHSEnergyFraction.begin(),chargedHadronCHSEnergyFraction.end());
101  fillerCHE.fill();
102  iEvent.put(std::move(chargedHadronCHSEnergyFractionV),"chargedHadronCHSEnergyFraction");
103 
104 }
105 
106 template <typename T>
107 std::tuple<float,float>
109 
110  auto rawP4 = ijet->correctedP4(0);
111  std::vector<unsigned int> jet2pu;
112 
113  // Get all of the PF candidates within a cone of dR to the jet that
114  // do NOT belong to the primary vertex.
115  // Store their indices.
116  for ( unsigned int icand = 0; icand < cands.size(); ++icand ) {
117  auto cand = cands.ptrAt(icand);
118  if (cand->fromPV()!=0) continue;
119  float dR = reco::deltaR(*ijet,*cand);
120  if (dR<maxDR_) {
121  jet2pu.emplace_back( cand.key() );
122  }
123  }
124 
125  // Keep track of energy for PU stuff
126  double che = 0.0;
127  double pue = 0.0;
128 
129  // Loop through the PF candidates within the jet.
130  // Store the sum of their energy, and their indices.
131  std::vector<unsigned int> used;
132  auto jetConstituents = ijet->daughterPtrVector();
133  for (auto const & ic : jetConstituents ) {
134  auto icpc = dynamic_cast<pat::PackedCandidate const *>(ic.get());
135  if ( icpc->charge()!=0) {
136  che += icpc->energy();
137  if (icpc->fromPV()==0) {
138  used.push_back( ic.key() );
139  }
140  }
141  }
142  // Loop through the pileup PF candidates within the jet.
143  for (auto pidx : jet2pu) {
144  auto const & dtr = cands.ptrAt(pidx);
145  // We take the candidates that have not appeared before: these were removed by CHS
146  if (dtr->charge()!=0 and std::find(used.begin(),used.end(),dtr.key() )==used.end())
147  pue += dtr->energy();
148  }
149 
150  // Now get the fractions relative to the raw jet.
151  auto puf = pue / rawP4.energy();
152  auto chf = che / rawP4.energy();
153 
154  return std::tuple<float,float>(puf,chf);
155 }
156 
157 template <typename T>
158 void
161  desc.add<edm::InputTag>("srcJet")->setComment("jet input collection");
162  desc.add<edm::InputTag>("srcPF")->setComment("PF candidate input collection");
163  desc.add<double>("maxDR")->setComment("Maximum DR to consider for jet-->pf cand association");
164  std::string modname ("BetaStarVarProducer");
165  descriptions.add(modname,desc);
166 }
167 
169 
171 
const LorentzVector correctedP4(const std::string &level, const std::string &flavor="none", const std::string &set="") const
Definition: Jet.h:158
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
BetaStarVarProducer< pat::PackedCandidate > BetaStarPackedCandidateVarProducer
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
edm::EDGetTokenT< edm::View< pat::Jet > > srcJet_
int iEvent
Definition: GenABIO.cc:230
Definition: Jet.py:1
edm::EDGetTokenT< edm::View< T > > srcPF_
double energy() const override
energy
ParameterDescriptionBase * add(U const &iLabel, T const &value)
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:28
std::tuple< float, float > calculateCHSEnergies(edm::Ptr< pat::Jet > const &jet, edm::View< T > const &cands) const
void add(std::string const &label, ParameterSetDescription const &psetDescription)
BetaStarVarProducer(const edm::ParameterSet &iConfig)
HLT enums.
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const reco::CompositePtrCandidate::daughters & daughterPtrVector() const override
references to daughtes
long double T
def move(src, dest)
Definition: eostools.py:510