CMS 3D CMS Logo

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 }
65 
66 // ------------------------------------------------------------------------------------------
68 
69 // ------------------------------------------------------------------------------------------
71  GBRForestsAndConstants const* gc = globalCache();
72 
73  using namespace edm;
74  using namespace std;
75  using namespace reco;
76 
77  // Input jets
78  Handle<View<Jet>> jetHandle;
79  iEvent.getByToken(input_jet_token_, jetHandle);
80  const View<Jet>& jets = *jetHandle;
81 
82  // input variables
84  if (!gc->produceJetIds()) {
85  iEvent.getByToken(input_vm_pujetid_token_, vmap);
86  }
87  // rho
89  double rho = 0.;
90 
91  // products
92  vector<StoredPileupJetIdentifier> ids;
93  map<string, vector<float>> mvas;
94  map<string, vector<int>> idflags;
95 
96  const VertexCollection* vertexes = nullptr;
97  VertexCollection::const_iterator vtx;
98  if (gc->produceJetIds()) {
99  // vertexes
100  Handle<VertexCollection> vertexHandle;
101  iEvent.getByToken(input_vertex_token_, vertexHandle);
102 
103  vertexes = vertexHandle.product();
104 
105  // require basic quality cuts on the vertexes
106  vtx = vertexes->begin();
107  while (vtx != vertexes->end() && (vtx->isFake() || vtx->ndof() < 4)) {
108  ++vtx;
109  }
110  if (vtx == vertexes->end()) {
111  vtx = vertexes->begin();
112  }
113  }
114 
115  // Loop over input jets
116  bool ispat = true;
117  for (unsigned int i = 0; i < jets.size(); ++i) {
118  // Pick the first algo to compute the input variables
119  auto algoi = algos_.begin();
120  PileupJetIdAlgo* ialgo = algoi->second.get();
121 
122  const Jet& jet = jets.at(i);
123  const pat::Jet* patjet = nullptr;
124  if (ispat) {
125  patjet = dynamic_cast<const pat::Jet*>(&jet);
126  ispat = patjet != nullptr;
127  }
128 
129  // Get jet energy correction
130  float jec = 0.;
131  if (gc->applyJec()) {
132  // If haven't done it get rho from the event
133  if (rho == 0.) {
134  iEvent.getByToken(input_rho_token_, rhoH);
135  rho = *rhoH;
136  }
137  // jet corrector
138  if (not jecCor_) {
139  initJetEnergyCorrector(iSetup, iEvent.isRealData());
140  }
141  if (ispat) {
142  jecCor_->setJetPt(patjet->correctedJet(0).pt());
143  } else {
144  jecCor_->setJetPt(jet.pt());
145  }
146  jecCor_->setJetEta(jet.eta());
147  jecCor_->setJetA(jet.jetArea());
148  jecCor_->setRho(rho);
149  jec = jecCor_->getCorrection();
150  }
151  // If it was requested AND the input is an uncorrected jet apply the JEC
152  bool applyJec = gc->applyJec() && (ispat || !gc->inputIsCorrected());
153  std::unique_ptr<reco::Jet> corrJet;
154 
155  if (applyJec) {
156  float scale = jec;
157  if (ispat) {
158  corrJet = std::make_unique<pat::Jet>(patjet->correctedJet(0));
159  } else {
160  corrJet.reset(dynamic_cast<reco::Jet*>(jet.clone()));
161  }
162  corrJet->scaleEnergy(scale);
163  }
164  const reco::Jet* theJet = (applyJec ? corrJet.get() : &jet);
165 
166  PileupJetIdentifier puIdentifier;
167  if (gc->produceJetIds()) {
168  // Compute the input variables
169  puIdentifier = ialgo->computeIdVariables(theJet, jec, &(*vtx), *vertexes, rho, gc->usePuppi());
170  ids.push_back(puIdentifier);
171  } else {
172  // Or read it from the value map
173  puIdentifier = (*vmap)[jets.refAt(i)];
174  puIdentifier.jetPt(theJet->pt()); // make sure JEC is applied when computing the MVA
175  puIdentifier.jetEta(theJet->eta());
176  puIdentifier.jetPhi(theJet->phi());
177  ialgo->set(puIdentifier);
178  puIdentifier = ialgo->computeMva();
179  }
180 
181  if (gc->runMvas()) {
182  // Compute the MVA and WP
183  mvas[algoi->first].push_back(puIdentifier.mva());
184  idflags[algoi->first].push_back(puIdentifier.idFlag());
185  for (++algoi; algoi != algos_.end(); ++algoi) {
186  ialgo = algoi->second.get();
187  ialgo->set(puIdentifier);
188  PileupJetIdentifier id = ialgo->computeMva();
189  mvas[algoi->first].push_back(id.mva());
190  idflags[algoi->first].push_back(id.idFlag());
191  }
192  }
193  }
194 
195  // Produce the output value maps
196  if (gc->runMvas()) {
197  for (const auto& ialgo : algos_) {
198  // MVA
199  vector<float>& mva = mvas[ialgo.first];
200  auto mvaout = std::make_unique<ValueMap<float>>();
201  ValueMap<float>::Filler mvafiller(*mvaout);
202  mvafiller.insert(jetHandle, mva.begin(), mva.end());
203  mvafiller.fill();
204  iEvent.put(std::move(mvaout), ialgo.first + "Discriminant");
205 
206  // WP
207  vector<int>& idflag = idflags[ialgo.first];
208  auto idflagout = std::make_unique<ValueMap<int>>();
209  ValueMap<int>::Filler idflagfiller(*idflagout);
210  idflagfiller.insert(jetHandle, idflag.begin(), idflag.end());
211  idflagfiller.fill();
212  iEvent.put(std::move(idflagout), ialgo.first + "Id");
213  }
214  }
215  // input variables
216  if (gc->produceJetIds()) {
217  assert(jetHandle->size() == ids.size());
218  auto idsout = std::make_unique<ValueMap<StoredPileupJetIdentifier>>();
220  idsfiller.insert(jetHandle, ids.begin(), ids.end());
221  idsfiller.fill();
222  iEvent.put(std::move(idsout));
223  }
224 }
225 
226 // ------------------------------------------------------------------------------------------
228  //The following says we do not know what parameters are allowed so do no validation
229  // Please change this to state exactly what you do use, even if it is no parameters
231  desc.setUnknown();
232  descriptions.addDefault(desc);
233 }
234 
235 // ------------------------------------------------------------------------------------------
237  GBRForestsAndConstants const* gc = globalCache();
238 
239  //jet energy correction levels to apply on raw jet
240  std::vector<std::string> jecLevels;
241  jecLevels.push_back("L1FastJet");
242  jecLevels.push_back("L2Relative");
243  jecLevels.push_back("L3Absolute");
244  if (isData && !gc->residualsFromTxt())
245  jecLevels.push_back("L2L3Residual");
246 
247  //check the corrector parameters needed according to the correction levels
249  iSetup.get<JetCorrectionsRecord>().get(gc->jec(), parameters);
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
PileupJetIdProducer::input_vertex_token_
edm::EDGetTokenT< reco::VertexCollection > input_vertex_token_
Definition: PileupJetIdProducer.h:103
BeamSpotPI::parameters
parameters
Definition: BeamSpotPayloadInspectorHelper.h:29
electrons_cff.bool
bool
Definition: electrons_cff.py:393
mps_fire.i
i
Definition: mps_fire.py:428
reco::Jet
Base class for all types of Jets.
Definition: Jet.h:20
PileupJetIdProducer::~PileupJetIdProducer
~PileupJetIdProducer() override
Definition: PileupJetIdProducer.cc:67
PileupJetIdProducer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: PileupJetIdProducer.cc:227
edm::Handle::product
T const * product() const
Definition: Handle.h:70
PileupJetIdProducer::input_jet_token_
edm::EDGetTokenT< edm::View< reco::Jet > > input_jet_token_
Definition: PileupJetIdProducer.h:102
PileupJetIdProducer::jetCorPars_
std::vector< JetCorrectorParameters > jetCorPars_
Definition: PileupJetIdProducer.h:100
HLT_FULL_cff.scale
scale
Definition: HLT_FULL_cff.py:6637
edm
HLT enums.
Definition: AlignableModifier.h:19
reco::VertexCollection
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
PileupJetIdentifier::mva
const float & mva() const
Definition: PileupJetIdentifier.h:94
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
cms::cuda::assert
assert(be >=bs)
jets_cff.vertexes
vertexes
Definition: jets_cff.py:702
singleTopDQM_cfi.jets
jets
Definition: singleTopDQM_cfi.py:42
GBRForestsAndConstants::runMvas_
bool runMvas_
Definition: PileupJetIdProducer.h:69
reco::LeafCandidate::pt
double pt() const final
transverse momentum
Definition: LeafCandidate.h:146
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
PileupJetIdProducer::jecCor_
std::unique_ptr< FactorizedJetCorrector > jecCor_
Definition: PileupJetIdProducer.h:99
beam_dqm_sourceclient-live_cfg.mva
mva
Definition: beam_dqm_sourceclient-live_cfg.py:122
GBRForestsAndConstants::applyJec
bool applyJec() const
Definition: PileupJetIdProducer.h:60
edm::Handle
Definition: AssociativeIterator.h:50
PileupJetIdAlgo::set
void set(const PileupJetIdentifier &)
Definition: PileupJetIdAlgo.cc:153
GBRForestsAndConstants::usePuppi
bool usePuppi() const
Definition: PileupJetIdProducer.h:64
edm::FileInPath
Definition: FileInPath.h:64
pat::Jet
Analysis-level calorimeter jet class.
Definition: Jet.h:77
GBRForestsAndConstants
Definition: PileupJetIdProducer.h:49
edm::EventSetup::get
T get() const
Definition: EventSetup.h:80
GBRForestsAndConstants::residualsFromTxt
bool residualsFromTxt() const
Definition: PileupJetIdProducer.h:62
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Jet
Definition: Jet.py:1
jets_cff.algos
algos
Definition: jets_cff.py:702
GBRForestsAndConstants::runMvas
bool runMvas() const
Definition: PileupJetIdProducer.h:57
GBRForestsAndConstants::vAlgoGBRForestsAndConstants
std::vector< PileupJetIdAlgo::AlgoGBRForestsAndConstants > const & vAlgoGBRForestsAndConstants() const
Definition: PileupJetIdProducer.h:53
HLTBitAnalyser_cfi.isData
isData
Definition: HLTBitAnalyser_cfi.py:29
PileupJetIdProducer::input_rho_token_
edm::EDGetTokenT< double > input_rho_token_
Definition: PileupJetIdProducer.h:105
PileupJetIdProducer::PileupJetIdProducer
PileupJetIdProducer(const edm::ParameterSet &, GBRForestsAndConstants const *)
Definition: PileupJetIdProducer.cc:46
edm::ESHandle
Definition: DTSurvey.h:22
PileupJetIdProducer::initJetEnergyCorrector
void initJetEnergyCorrector(const edm::EventSetup &iSetup, bool isData)
Definition: PileupJetIdProducer.cc:236
PileupJetIdAlgo::computeIdVariables
PileupJetIdentifier computeIdVariables(const reco::Jet *jet, float jec, const reco::Vertex *, const reco::VertexCollection &, double rho, bool usePuppi)
Definition: PileupJetIdAlgo.cc:260
JetCorrectionsRecord
Definition: JetCorrectionsRecord.h:11
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
DDAxes::rho
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::View
Definition: CaloClusterFwd.h:14
edm::ParameterSet
Definition: ParameterSet.h:47
reco::LeafCandidate::eta
double eta() const final
momentum pseudorapidity
Definition: LeafCandidate.h:152
GBRForestsAndConstants::inputIsCorrected
bool inputIsCorrected() const
Definition: PileupJetIdProducer.h:59
GBRForestsAndConstants::residualsTxt
edm::FileInPath const & residualsTxt() const
Definition: PileupJetIdProducer.h:63
PileupJetIdAlgo::computeMva
PileupJetIdentifier computeMva()
Definition: PileupJetIdAlgo.cc:254
iEvent
int iEvent
Definition: GenABIO.cc:224
PileupJetIdentifier
Definition: PileupJetIdentifier.h:74
edm::EventSetup
Definition: EventSetup.h:57
PileupJetIdProducer::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: PileupJetIdProducer.cc:70
PileupJetIdProducer
Definition: PileupJetIdProducer.h:79
get
#define get
PileupJetIdentifier::idFlag
const int & idFlag() const
Definition: PileupJetIdentifier.h:91
PileupJetIdProducer.h
StoredPileupJetIdentifier::jetEta
const float & jetEta() const
Definition: PileupJetIdentifier.h:31
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
GBRForestsAndConstants::residualsFromTxt_
bool residualsFromTxt_
Definition: PileupJetIdProducer.h:74
reco::LeafCandidate::phi
double phi() const final
momentum azimuthal angle
Definition: LeafCandidate.h:148
extraflags_cff.vtx
vtx
Definition: extraflags_cff.py:18
GBRForestsAndConstants::produceJetIds
bool produceJetIds() const
Definition: PileupJetIdProducer.h:58
PileupJetIdentifier::jetPhi
const float & jetPhi() const
Definition: PileupJetIdentifier.h:96
metsig::jet
Definition: SignAlgoResolutions.h:47
edm::ValueMap
Definition: ValueMap.h:107
GBRForestsAndConstants::jec
std::string const & jec() const
Definition: PileupJetIdProducer.h:61
PileupJetIdProducer::algos_
std::vector< std::pair< std::string, std::unique_ptr< PileupJetIdAlgo > > > algos_
Definition: PileupJetIdProducer.h:97
JetCorrectorParameters
Definition: JetCorrectorParameters.h:25
pat::Jet::correctedJet
Jet correctedJet(const std::string &level, const std::string &flavor="none", const std::string &set="") const
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
GBRForestsAndConstants::GBRForestsAndConstants
GBRForestsAndConstants(edm::ParameterSet const &)
Definition: PileupJetIdProducer.cc:23
jetMETDQMOfflineSource_cff.jec
jec
Definition: jetMETDQMOfflineSource_cff.py:27
GBRForestsAndConstants::residualsTxt_
edm::FileInPath residualsTxt_
Definition: PileupJetIdProducer.h:75
GBRForestsAndConstants::vAlgoGBRForestsAndConstants_
std::vector< PileupJetIdAlgo::AlgoGBRForestsAndConstants > vAlgoGBRForestsAndConstants_
Definition: PileupJetIdProducer.h:67
edm::helper::Filler
Definition: ValueMap.h:22
PileupJetIdAlgo
Definition: PileupJetIdAlgo.h:22
StoredPileupJetIdentifier::jetPt
const float & jetPt() const
Definition: PileupJetIdentifier.h:32
PileupJetIdProducer::input_vm_pujetid_token_
edm::EDGetTokenT< edm::ValueMap< StoredPileupJetIdentifier > > input_vm_pujetid_token_
Definition: PileupJetIdProducer.h:104
edm::Event
Definition: Event.h:73
edm::ConfigurationDescriptions::addDefault
void addDefault(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:99
edm::InputTag
Definition: InputTag.h:15
jetMETDQMOfflineSource_cff.applyJec
applyJec
Definition: jetMETDQMOfflineSource_cff.py:28
label
const char * label
Definition: PFTauDecayModeTools.cc:11
edm::FileInPath::fullPath
std::string fullPath() const
Definition: FileInPath.cc:163