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 // Further editing: Hannu Siikonen
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  produces<edm::ValueMap<float>>("chargedFromPV0EnergyFraction");
51  produces<edm::ValueMap<float>>("chargedFromPV1EnergyFraction");
52  produces<edm::ValueMap<float>>("chargedFromPV2EnergyFraction");
53  produces<edm::ValueMap<float>>("chargedFromPV3EnergyFraction");
54  }
55  ~BetaStarVarProducer() override{};
56 
57  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
58 
59 private:
60  void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override;
61 
62  void fillValueMaps(std::string valueName,
63  const std::vector<float> &values,
65  edm::Event &iEvent) const;
66  std::tuple<float, float, float, float> calculateCHSEnergies(edm::Ptr<pat::Jet> const &jet, double chefrompv0) const;
67 
70  double maxDR_;
71 };
72 
73 template <typename T>
76  iEvent.getByToken(srcJet_, srcJet);
78  iEvent.getByToken(srcPF_, srcPF);
79 
80  unsigned int nJet = srcJet->size();
81 
82  // Create an injective mapping from jets to charged PF candidates with fromPV==0 within the jet cone.
83  // These are the PF candidates supposedly removed by the CHS method - i.e. removed charged pileup.
84  std::vector<double> jet2pue(nJet, 0);
85  for (const auto &cand : *srcPF) {
86  if (cand.charge() == 0 or cand.fromPV() != 0)
87  continue;
88  // Looking for the closest match for this charged frompv==0 candidate
89  float dRMin = 999.;
90  int bestjet = -1, jetidx = 0;
91  for (const auto &jet : *srcJet) {
92  float dR = reco::deltaR(jet, cand);
93  if (dR < dRMin) {
94  dRMin = dR;
95  bestjet = jetidx;
96  }
97  ++jetidx;
98  }
99  // If the candidate is closer than the jet radius to the jet axis, this is a PU particle of interest for the selected jet
100  if (dRMin < maxDR_)
101  jet2pue[bestjet] += cand.energy();
102  }
103 
104  std::vector<float> chargedFromPV0EnergyFraction(nJet, -1);
105  std::vector<float> chargedFromPV1EnergyFraction(nJet, -1);
106  std::vector<float> chargedFromPV2EnergyFraction(nJet, -1);
107  std::vector<float> chargedFromPV3EnergyFraction(nJet, -1);
108 
109  for (unsigned int ij = 0; ij < nJet; ++ij) {
110  auto vals = calculateCHSEnergies(srcJet->ptrAt(ij), jet2pue[ij]);
111  chargedFromPV0EnergyFraction[ij] = std::get<0>(vals);
112  chargedFromPV1EnergyFraction[ij] = std::get<1>(vals);
113  chargedFromPV2EnergyFraction[ij] = std::get<2>(vals);
114  chargedFromPV3EnergyFraction[ij] = std::get<3>(vals);
115  }
116 
117  fillValueMaps("chargedFromPV0EnergyFraction", chargedFromPV0EnergyFraction, srcJet, iEvent);
118  fillValueMaps("chargedFromPV1EnergyFraction", chargedFromPV1EnergyFraction, srcJet, iEvent);
119  fillValueMaps("chargedFromPV2EnergyFraction", chargedFromPV2EnergyFraction, srcJet, iEvent);
120  fillValueMaps("chargedFromPV3EnergyFraction", chargedFromPV3EnergyFraction, srcJet, iEvent);
121 }
122 
123 template <typename T>
125  const std::vector<float> &values,
127  edm::Event &iEvent) const {
128  std::unique_ptr<edm::ValueMap<float>> valuesPtr(new edm::ValueMap<float>());
129  edm::ValueMap<float>::Filler valuesFiller(*valuesPtr);
130  valuesFiller.insert(srcJet, values.begin(), values.end());
131  valuesFiller.fill();
132  iEvent.put(std::move(valuesPtr), valueName);
133 }
134 
135 template <typename T>
136 std::tuple<float, float, float, float> BetaStarVarProducer<T>::calculateCHSEnergies(edm::Ptr<pat::Jet> const &ijet,
137  double chefrompv0) const {
138  // Keep track of energy (present in the jet) for PU stuff
139  double chefrompv1 = 0.0;
140  double chefrompv2 = 0.0;
141  double chefrompv3 = 0.0;
142 
143  // Loop through the PF candidates within the jet.
144  // Store the sum of their energy, and their indices.
145  for (auto pidx = 0u; pidx < ijet->numberOfDaughters(); ++pidx) {
146  auto *dtr = dynamic_cast<const pat::PackedCandidate *>(ijet->daughter(pidx));
147  if (dtr->charge() == 0)
148  continue;
149  if (dtr->fromPV() == 1)
150  chefrompv1 += dtr->energy();
151  else if (dtr->fromPV() == 2)
152  chefrompv2 += dtr->energy();
153  else if (dtr->fromPV() == 3)
154  chefrompv3 += dtr->energy();
155  }
156 
157  // Now get the fractions relative to the raw jet.
158  auto rawP4 = ijet->correctedP4(0);
159  double chffpv0 = chefrompv0 / rawP4.energy();
160  double chffpv1 = chefrompv1 / rawP4.energy();
161  double chffpv2 = chefrompv2 / rawP4.energy();
162  double chffpv3 = chefrompv3 / rawP4.energy();
163 
164  return std::tuple<float, float, float, float>(chffpv0, chffpv1, chffpv2, chffpv3);
165 }
166 
167 template <typename T>
170  desc.add<edm::InputTag>("srcJet")->setComment("jet input collection");
171  desc.add<edm::InputTag>("srcPF")->setComment("PF candidate input collection");
172  desc.add<double>("maxDR")->setComment("Maximum DR to consider for jet-->pf cand association");
173  std::string modname("BetaStarVarProducer");
174  descriptions.add(modname, desc);
175 }
176 
178 
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:125
void fillValueMaps(std::string valueName, const std::vector< float > &values, edm::Handle< edm::View< pat::Jet >> &srcJet, edm::Event &iEvent) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
BetaStarVarProducer< pat::PackedCandidate > BetaStarPackedCandidateVarProducer
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
edm::EDGetTokenT< edm::View< pat::Jet > > srcJet_
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Definition: Jet.py:1
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
edm::EDGetTokenT< edm::View< T > > srcPF_
double energy() const override
energy
const reco::Candidate * daughter(size_t i) const override
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, float, float > calculateCHSEnergies(edm::Ptr< pat::Jet > const &jet, double chefrompv0) const
void add(std::string const &label, ParameterSetDescription const &psetDescription)
size_t numberOfDaughters() const override
BetaStarVarProducer(const edm::ParameterSet &iConfig)
HLT enums.
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
long double T
def move(src, dest)
Definition: eostools.py:511