CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PileupJetIdProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PileupJetIdProducer
4 // Class: PileupJetIdProducer
5 //
13 //
14 // Original Author: Pasquale Musella,40 2-A12,+41227671706,
15 // Created: Wed Apr 18 15:48:47 CEST 2012
16 //
17 //
18 
20 
21 #include <memory>
22 
24  : runMvas_(iConfig.getParameter<bool>("runMvas")),
25  produceJetIds_(iConfig.getParameter<bool>("produceJetIds")),
26  inputIsCorrected_(iConfig.getParameter<bool>("inputIsCorrected")),
27  applyJec_(iConfig.getParameter<bool>("applyJec")),
28  jec_(iConfig.getParameter<std::string>("jec")),
29  residualsFromTxt_(iConfig.getParameter<bool>("residualsFromTxt")),
30  usePuppi_(iConfig.getParameter<bool>("usePuppi")) {
31  if (residualsFromTxt_) {
32  residualsTxt_ = iConfig.getParameter<edm::FileInPath>("residualsTxt");
33  }
34 
35  std::vector<edm::ParameterSet> algos = iConfig.getParameter<std::vector<edm::ParameterSet>>("algos");
36  for (auto const& algoPset : algos) {
37  vAlgoGBRForestsAndConstants_.emplace_back(algoPset, runMvas_);
38  }
39 
40  if (!runMvas_) {
41  assert(algos.size() == 1);
42  }
43 }
44 
45 // ------------------------------------------------------------------------------------------
47  if (globalCache->produceJetIds()) {
48  produces<edm::ValueMap<StoredPileupJetIdentifier>>("");
49  }
50  for (auto const& algoGBRForestsAndConstants : globalCache->vAlgoGBRForestsAndConstants()) {
51  std::string const& label = algoGBRForestsAndConstants.label();
52  algos_.emplace_back(label, std::make_unique<PileupJetIdAlgo>(&algoGBRForestsAndConstants));
53  if (globalCache->runMvas()) {
54  produces<edm::ValueMap<float>>(label + "Discriminant");
55  produces<edm::ValueMap<int>>(label + "Id");
56  }
57  }
58 
59  input_jet_token_ = consumes<edm::View<reco::Jet>>(iConfig.getParameter<edm::InputTag>("jets"));
60  input_vertex_token_ = consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexes"));
62  consumes<edm::ValueMap<StoredPileupJetIdentifier>>(iConfig.getParameter<edm::InputTag>("jetids"));
63  input_rho_token_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho"));
64  parameters_token_ = esConsumes(edm::ESInputTag("", globalCache->jec()));
65 }
66 
67 // ------------------------------------------------------------------------------------------
69 
70 // ------------------------------------------------------------------------------------------
72  GBRForestsAndConstants const* gc = globalCache();
73 
74  using namespace edm;
75  using namespace std;
76  using namespace reco;
77 
78  // Input jets
79  Handle<View<Jet>> jetHandle;
80  iEvent.getByToken(input_jet_token_, jetHandle);
81  const View<Jet>& jets = *jetHandle;
82 
83  // input variables
85  if (!gc->produceJetIds()) {
86  iEvent.getByToken(input_vm_pujetid_token_, vmap);
87  }
88  // rho
90  double rho = 0.;
91 
92  // products
93  vector<StoredPileupJetIdentifier> ids;
94  map<string, vector<float>> mvas;
95  map<string, vector<int>> idflags;
96 
97  const VertexCollection* vertexes = nullptr;
98  VertexCollection::const_iterator vtx;
99  if (gc->produceJetIds()) {
100  // vertexes
101  Handle<VertexCollection> vertexHandle;
102  iEvent.getByToken(input_vertex_token_, vertexHandle);
103 
104  vertexes = vertexHandle.product();
105 
106  // require basic quality cuts on the vertexes
107  vtx = vertexes->begin();
108  while (vtx != vertexes->end() && (vtx->isFake() || vtx->ndof() < 4)) {
109  ++vtx;
110  }
111  if (vtx == vertexes->end()) {
112  vtx = vertexes->begin();
113  }
114  }
115 
116  // Loop over input jets
117  bool ispat = true;
118  for (unsigned int i = 0; i < jets.size(); ++i) {
119  // Pick the first algo to compute the input variables
120  auto algoi = algos_.begin();
121  PileupJetIdAlgo* ialgo = algoi->second.get();
122 
123  const Jet& jet = jets.at(i);
124  const pat::Jet* patjet = nullptr;
125  if (ispat) {
126  patjet = dynamic_cast<const pat::Jet*>(&jet);
127  ispat = patjet != nullptr;
128  }
129 
130  // Get jet energy correction
131  float jec = 0.;
132  if (gc->applyJec()) {
133  // If haven't done it get rho from the event
134  if (rho == 0.) {
135  iEvent.getByToken(input_rho_token_, rhoH);
136  rho = *rhoH;
137  }
138  // jet corrector
139  if (not jecCor_) {
140  initJetEnergyCorrector(iSetup, iEvent.isRealData());
141  }
142  if (ispat) {
143  jecCor_->setJetPt(patjet->correctedJet(0).pt());
144  } else {
145  jecCor_->setJetPt(jet.pt());
146  }
147  jecCor_->setJetEta(jet.eta());
148  jecCor_->setJetA(jet.jetArea());
149  jecCor_->setRho(rho);
150  jec = jecCor_->getCorrection();
151  }
152  // If it was requested AND the input is an uncorrected jet apply the JEC
153  bool applyJec = gc->applyJec() && (ispat || !gc->inputIsCorrected());
154  std::unique_ptr<reco::Jet> corrJet;
155 
156  if (applyJec) {
157  float scale = jec;
158  if (ispat) {
159  corrJet = std::make_unique<pat::Jet>(patjet->correctedJet(0));
160  } else {
161  corrJet.reset(dynamic_cast<reco::Jet*>(jet.clone()));
162  }
163  corrJet->scaleEnergy(scale);
164  }
165  const reco::Jet* theJet = (applyJec ? corrJet.get() : &jet);
166 
167  PileupJetIdentifier puIdentifier;
168  if (gc->produceJetIds()) {
169  // Compute the input variables
170  puIdentifier = ialgo->computeIdVariables(theJet, jec, &(*vtx), *vertexes, rho, gc->usePuppi());
171  ids.push_back(puIdentifier);
172  } else {
173  // Or read it from the value map
174  puIdentifier = (*vmap)[jets.refAt(i)];
175  puIdentifier.jetPt(theJet->pt()); // make sure JEC is applied when computing the MVA
176  puIdentifier.jetEta(theJet->eta());
177  puIdentifier.jetPhi(theJet->phi());
178  ialgo->set(puIdentifier);
179  puIdentifier = ialgo->computeMva();
180  }
181 
182  if (gc->runMvas()) {
183  // Compute the MVA and WP
184  mvas[algoi->first].push_back(puIdentifier.mva());
185  idflags[algoi->first].push_back(puIdentifier.idFlag());
186  for (++algoi; algoi != algos_.end(); ++algoi) {
187  ialgo = algoi->second.get();
188  ialgo->set(puIdentifier);
189  PileupJetIdentifier id = ialgo->computeMva();
190  mvas[algoi->first].push_back(id.mva());
191  idflags[algoi->first].push_back(id.idFlag());
192  }
193  }
194  }
195 
196  // Produce the output value maps
197  if (gc->runMvas()) {
198  for (const auto& ialgo : algos_) {
199  // MVA
200  vector<float>& mva = mvas[ialgo.first];
201  auto mvaout = std::make_unique<ValueMap<float>>();
202  ValueMap<float>::Filler mvafiller(*mvaout);
203  mvafiller.insert(jetHandle, mva.begin(), mva.end());
204  mvafiller.fill();
205  iEvent.put(std::move(mvaout), ialgo.first + "Discriminant");
206 
207  // WP
208  vector<int>& idflag = idflags[ialgo.first];
209  auto idflagout = std::make_unique<ValueMap<int>>();
210  ValueMap<int>::Filler idflagfiller(*idflagout);
211  idflagfiller.insert(jetHandle, idflag.begin(), idflag.end());
212  idflagfiller.fill();
213  iEvent.put(std::move(idflagout), ialgo.first + "Id");
214  }
215  }
216  // input variables
217  if (gc->produceJetIds()) {
218  assert(jetHandle->size() == ids.size());
219  auto idsout = std::make_unique<ValueMap<StoredPileupJetIdentifier>>();
221  idsfiller.insert(jetHandle, ids.begin(), ids.end());
222  idsfiller.fill();
223  iEvent.put(std::move(idsout));
224  }
225 }
226 
227 // ------------------------------------------------------------------------------------------
229  //The following says we do not know what parameters are allowed so do no validation
230  // Please change this to state exactly what you do use, even if it is no parameters
232  desc.setUnknown();
233  descriptions.addDefault(desc);
234 }
235 
236 // ------------------------------------------------------------------------------------------
238  GBRForestsAndConstants const* gc = globalCache();
239 
240  //jet energy correction levels to apply on raw jet
241  std::vector<std::string> jecLevels;
242  jecLevels.push_back("L1FastJet");
243  jecLevels.push_back("L2Relative");
244  jecLevels.push_back("L3Absolute");
245  if (isData && !gc->residualsFromTxt())
246  jecLevels.push_back("L2L3Residual");
247 
248  //check the corrector parameters needed according to the correction levels
249  auto const& parameters = iSetup.getData(parameters_token_);
250  for (std::vector<std::string>::const_iterator ll = jecLevels.begin(); ll != jecLevels.end(); ++ll) {
251  const JetCorrectorParameters& ip = parameters[*ll];
252  jetCorPars_.push_back(ip);
253  }
254  if (isData && gc->residualsFromTxt()) {
256  }
257 
258  //instantiate the jet corrector
259  jecCor_ = std::make_unique<FactorizedJetCorrector>(jetCorPars_);
260 }
261 //define this as a plug-in
std::vector< PileupJetIdAlgo::AlgoGBRForestsAndConstants > const & vAlgoGBRForestsAndConstants() const
void set(const PileupJetIdentifier &)
std::vector< JetCorrectorParameters > jetCorPars_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
double pt() const final
transverse momentum
edm::EDGetTokenT< reco::VertexCollection > input_vertex_token_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
const int & idFlag() const
const float & mva() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Base class for all types of Jets.
Definition: Jet.h:20
edm::EDGetTokenT< edm::View< reco::Jet > > input_jet_token_
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
assert(be >=bs)
bool isRealData() const
Definition: EventBase.h:62
const float & jetPt() const
PileupJetIdProducer(const edm::ParameterSet &, GBRForestsAndConstants const *)
bool getData(T &iHolder) const
Definition: EventSetup.h:122
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
char const * label
PileupJetIdentifier computeIdVariables(const reco::Jet *jet, float jec, const reco::Vertex *, const reco::VertexCollection &, double rho, bool usePuppi)
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
std::string const & jec() const
vector< PseudoJet > jets
def move
Definition: eostools.py:511
edm::FileInPath const & residualsTxt() const
edm::EDGetTokenT< double > input_rho_token_
void initJetEnergyCorrector(const edm::EventSetup &iSetup, bool isData)
void produce(edm::Event &, const edm::EventSetup &) override
tuple algos
Definition: Puppi_cff.py:76
std::vector< PileupJetIdAlgo::AlgoGBRForestsAndConstants > vAlgoGBRForestsAndConstants_
const float & jetPhi() const
edm::FileInPath residualsTxt_
T const * product() const
Definition: Handle.h:70
std::vector< std::pair< std::string, std::unique_ptr< PileupJetIdAlgo > > > algos_
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
PileupJetIdentifier computeMva()
Analysis-level calorimeter jet class.
Definition: Jet.h:77
GBRForestsAndConstants(edm::ParameterSet const &)
virtual float jetArea() const
get jet area
Definition: Jet.h:103
edm::ESGetToken< JetCorrectorParametersCollection, JetCorrectionsRecord > parameters_token_
edm::EDGetTokenT< edm::ValueMap< StoredPileupJetIdentifier > > input_vm_pujetid_token_
std::string fullPath() const
Definition: FileInPath.cc:161
Jet correctedJet(const std::string &level, const std::string &flavor="none", const std::string &set="") const
double phi() const final
momentum azimuthal angle
CompositePtrCandidate * clone() const override
returns a clone of the candidate
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
std::unique_ptr< FactorizedJetCorrector > jecCor_
const float & jetEta() const
double eta() const final
momentum pseudorapidity