CMS 3D CMS Logo

MVAJetPuIdProducer.cc
Go to the documentation of this file.
1 #include <memory>
9 
11 
16 
20 
22 
23 
25 public:
26  explicit MVAJetPuIdProducer(const edm::ParameterSet&);
27 
28  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
29 
30 private:
31  virtual void produce(edm::Event&, const edm::EventSetup&) override;
32 
33 
34  void initJetEnergyCorrector(const edm::EventSetup &iSetup, bool isData);
35 
39  std::vector<std::pair<std::string, MVAJetPuId *> > algos_;
40 
44  std::vector<JetCorrectorParameters> jetCorPars_;
45 
50 
51 };
52 
53 
55 {
56  runMvas_ = iConfig.getParameter<bool>("runMvas");
57  produceJetIds_ = iConfig.getParameter<bool>("produceJetIds");
58  jets_ = iConfig.getParameter<edm::InputTag>("jets");
59  vertexes_ = iConfig.getParameter<edm::InputTag>("vertexes");
60  jetids_ = iConfig.getParameter<edm::InputTag>("jetids");
61  inputIsCorrected_ = iConfig.getParameter<bool>("inputIsCorrected");
62  applyJec_ = iConfig.getParameter<bool>("applyJec");
63  jec_ = iConfig.getParameter<std::string>("jec");
64  rho_ = iConfig.getParameter<edm::InputTag>("rho");
65  residualsFromTxt_ = iConfig.getParameter<bool>("residualsFromTxt");
66  if(residualsFromTxt_) residualsTxt_ = iConfig.getParameter<edm::FileInPath>("residualsTxt");
67  std::vector<edm::ParameterSet> algos = iConfig.getParameter<std::vector<edm::ParameterSet> >("algos");
68 
69  jecCor_ = 0;
70 
71  if( ! runMvas_ ) assert( algos.size() == 1 );
72 
73  if( produceJetIds_ ) {
74  produces<edm::ValueMap<StoredPileupJetIdentifier> > ("");
75  }
76  for(std::vector<edm::ParameterSet>::iterator it=algos.begin(); it!=algos.end(); ++it) {
77  std::string label = it->getParameter<std::string>("label");
78  algos_.push_back( std::make_pair(label,new MVAJetPuId(*it)) );
79  if( runMvas_ ) {
80  produces<edm::ValueMap<float> > (label+"Discriminant");
81  produces<edm::ValueMap<int> > (label+"Id");
82  }
83  }
84 
85  input_jet_token_ = consumes<edm::View<reco::Jet> >(jets_);
86  input_vertex_token_ = consumes<reco::VertexCollection>(vertexes_);
87  input_vm_pujetid_token_ = consumes<edm::ValueMap<StoredPileupJetIdentifier> >(jetids_);
88  input_rho_token_ = consumes<double>(rho_);
89 
90  }
91 
92 
93  void
95  {
96  using namespace edm;
97  using namespace std;
98  using namespace reco;
99 
100  Handle<View<Jet> > jetHandle;
101  iEvent.getByToken(input_jet_token_,jetHandle);
102  const View<Jet> & jets = *jetHandle;
103  Handle<VertexCollection> vertexHandle;
104  if( produceJetIds_ ) {
105  iEvent.getByToken(input_vertex_token_, vertexHandle);
106  }
107  const VertexCollection & vertexes = *(vertexHandle.product());
109  if( ! produceJetIds_ ) {
110  iEvent.getByToken(input_vm_pujetid_token_, vmap);
111  }
113  double rho = 0.;
114 
115  vector<StoredPileupJetIdentifier> ids;
116  map<string, vector<float> > mvas;
117  map<string, vector<int> > idflags;
118 
119  VertexCollection::const_iterator vtx;
120  if( produceJetIds_ ) {
121  vtx = vertexes.begin();
122  while( vtx != vertexes.end() && ( vtx->isFake() || vtx->ndof() < 4 ) ) {
123  ++vtx;
124  }
125  if( vtx == vertexes.end() ) { vtx = vertexes.begin(); }
126  }
127 
128  for ( unsigned int i=0; i<jets.size(); ++i ) {
129  vector<pair<string,MVAJetPuId *> >::iterator algoi = algos_.begin();
130  MVAJetPuId * ialgo = algoi->second;
131 
132  const Jet & jet = jets[i];
133 
134  float jec = 0.;
135  if( applyJec_ ) {
136  if( rho == 0. ) {
137  iEvent.getByToken(input_rho_token_,rhoH);
138  rho = *rhoH;
139  }
140  jecCor_->setJetPt(jet.pt());
141  jecCor_->setJetEta(jet.eta());
142  jecCor_->setJetA(jet.jetArea());
143  jecCor_->setRho(rho);
144  jec = jecCor_->getCorrection();
145  }
146 
148  reco::Jet * corrJet = 0;
149  if( applyJec ) {
150  float scale = jec;
151  corrJet = dynamic_cast<reco::Jet *>( jet.clone() );
152  corrJet->scaleEnergy(scale);
153  }
154  const reco::Jet * theJet = ( applyJec ? corrJet : &jet );
155  PileupJetIdentifier puIdentifier;
156  if( produceJetIds_ ) {
157  puIdentifier = ialgo->computeIdVariables(theJet, jec, &(*vtx), vertexes, runMvas_);
158  ids.push_back( puIdentifier );
159  } else {
160  puIdentifier = (*vmap)[jets.refAt(i)];
161  puIdentifier.jetPt(theJet->pt()); /*make sure JEC is applied when computing the MVA*/
162  puIdentifier.jetEta(theJet->eta());
163  puIdentifier.jetPhi(theJet->phi());
164  ialgo->set(puIdentifier);
165  puIdentifier = ialgo->computeMva();
166  }
167 
168  if( runMvas_ ) {
169  for( ; algoi!=algos_.end(); ++algoi) {
170  ialgo = algoi->second;
171  ialgo->set(puIdentifier);
172  PileupJetIdentifier id = ialgo->computeMva();
173  mvas[algoi->first].push_back( id.mva() );
174  idflags[algoi->first].push_back( id.idFlag() );
175  }
176  }
177 
178  if( corrJet ) { delete corrJet; }
179  }
180 
181  if( runMvas_ ) {
182  for(vector<pair<string,MVAJetPuId *> >::iterator ialgo = algos_.begin(); ialgo!=algos_.end(); ++ialgo) {
183  vector<float> & mva = mvas[ialgo->first];
184  auto mvaout = std::make_unique<ValueMap<float>>();
185  ValueMap<float>::Filler mvafiller(*mvaout);
186  mvafiller.insert(jetHandle,mva.begin(),mva.end());
187  mvafiller.fill();
188  iEvent.put(std::move(mvaout),ialgo->first+"Discriminant");
189 
190  vector<int> & idflag = idflags[ialgo->first];
191  auto idflagout = std::make_unique<ValueMap<int>>();
192  ValueMap<int>::Filler idflagfiller(*idflagout);
193  idflagfiller.insert(jetHandle,idflag.begin(),idflag.end());
194  idflagfiller.fill();
195  iEvent.put(std::move(idflagout),ialgo->first+"Id");
196  }
197  }
198  if( produceJetIds_ ) {
199  assert( jetHandle->size() == ids.size() );
200  auto idsout = std::make_unique<ValueMap<StoredPileupJetIdentifier>>();
202  idsfiller.insert(jetHandle,ids.begin(),ids.end());
203  idsfiller.fill();
204  iEvent.put(std::move(idsout));
205  }
206  }
207 
208 
209 
210  void
213  desc.add<bool>("runMvas", true);
214  desc.add<bool>("inputIsCorrected", true);
215  desc.add<edm::InputTag>("vertexes", edm::InputTag("hltPixelVertices"));
216  desc.add<bool>("produceJetIds", true);
217  desc.add<std::string>("jec", "AK4PF");
218  desc.add<bool>("residualsFromTxt", false);
219  desc.add<bool>("applyJec", false);
220  desc.add<edm::InputTag>("jetids", edm::InputTag(""));
221  desc.add<edm::InputTag>("rho", edm::InputTag("hltFixedGridRhoFastjetAll"));
222  desc.add<edm::InputTag>("jets", edm::InputTag("hltAK4PFJetsCorrected"));
224  vpsd1.add<std::vector<std::string>>("tmvaVariables", {
225  "rho",
226  "nParticles",
227  "nCharged",
228  "majW",
229  "minW",
230  "frac01",
231  "frac02",
232  "frac03",
233  "frac04",
234  "ptD",
235  "beta",
236  "betaStar",
237  "dR2Mean",
238  "pull",
239  "jetR",
240  "jetRchg",
241  });
242  vpsd1.add<std::string>("tmvaMethod", "JetID");
243  vpsd1.add<bool>("cutBased", false);
244  vpsd1.add<std::string>("tmvaWeights", "RecoJets/JetProducers/data/MVAJetPuID.weights.xml.gz");
245  vpsd1.add<std::vector<std::string>>("tmvaSpectators", {
246  "jetEta",
247  "jetPt",
248  });
249  vpsd1.add<std::string>("label", "CATEv0");
250  vpsd1.add<int>("version", -1);
251  {
253  psd0.add<std::vector<double>>("Pt2030_Tight", {
254  0.73,
255  0.05,
256  -0.26,
257  -0.42,
258  });
259  psd0.add<std::vector<double>>("Pt2030_Loose", {
260  -0.63,
261  -0.6,
262  -0.55,
263  -0.45,
264  });
265  psd0.add<std::vector<double>>("Pt3050_Medium", {
266  0.1,
267  -0.36,
268  -0.54,
269  -0.54,
270  });
271  psd0.add<std::vector<double>>("Pt1020_Tight", {
272  -0.83,
273  -0.81,
274  -0.74,
275  -0.81,
276  });
277  psd0.add<std::vector<double>>("Pt2030_Medium", {
278  0.1,
279  -0.36,
280  -0.54,
281  -0.54,
282  });
283  psd0.add<std::vector<double>>("Pt010_Tight", {
284  -0.83,
285  -0.81,
286  -0.74,
287  -0.81,
288  });
289  psd0.add<std::vector<double>>("Pt1020_Loose", {
290  -0.95,
291  -0.96,
292  -0.94,
293  -0.95,
294  });
295  psd0.add<std::vector<double>>("Pt010_Medium", {
296  -0.83,
297  -0.92,
298  -0.9,
299  -0.92,
300  });
301  psd0.add<std::vector<double>>("Pt1020_Medium", {
302  -0.83,
303  -0.92,
304  -0.9,
305  -0.92,
306  });
307  psd0.add<std::vector<double>>("Pt010_Loose", {
308  -0.95,
309  -0.96,
310  -0.94,
311  -0.95,
312  });
313  psd0.add<std::vector<double>>("Pt3050_Loose", {
314  -0.63,
315  -0.6,
316  -0.55,
317  -0.45,
318  });
319  psd0.add<std::vector<double>>("Pt3050_Tight", {
320  0.73,
321  0.05,
322  -0.26,
323  -0.42,
324  });
325  vpsd1.add<edm::ParameterSetDescription>("JetIdParams", psd0);
326  }
327  vpsd1.add<double>("impactParTkThreshold", 1.0);
328  std::vector<edm::ParameterSet> temp1;
329  temp1.reserve(1);
330 
331  desc.addVPSet("algos", vpsd1, temp1);
332 
333  descriptions.add("MVAJetPuIdProducer", desc);
334 
335  }
336 
338 
339 
T getParameter(std::string const &) const
virtual double pt() const final
transverse momentum
void initJetEnergyCorrector(const edm::EventSetup &iSetup, bool isData)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
virtual void scaleEnergy(double fScale)
scale energy of the jet
FactorizedJetCorrector * jecCor_
virtual double eta() const final
momentum pseudorapidity
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
Base class for all types of Jets.
Definition: Jet.h:20
size_type size() const
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
const float & jetPt() const
edm::FileInPath residualsTxt_
RefToBase< value_type > refAt(size_type i) const
virtual double phi() const final
momentum azimuthal angle
virtual CompositePtrCandidate * clone() const
returns a clone of the candidate
int iEvent
Definition: GenABIO.cc:230
MVAJetPuIdProducer(const edm::ParameterSet &)
Definition: Jet.py:1
vector< PseudoJet > jets
PileupJetIdentifier computeIdVariables(const reco::Jet *jet, float jec, const reco::Vertex *, const reco::VertexCollection &, double rho, bool calculateMva=false)
Definition: MVAJetPuId.cc:210
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< JetCorrectorParameters > jetCorPars_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
virtual void produce(edm::Event &, const edm::EventSetup &) override
edm::EDGetTokenT< edm::View< reco::Jet > > input_jet_token_
const float & jetPhi() const
T const * product() const
Definition: Handle.h:81
std::vector< std::pair< std::string, MVAJetPuId * > > algos_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void set(const PileupJetIdentifier &)
Definition: MVAJetPuId.cc:160
fixed size matrix
HLT enums.
virtual float jetArea() const
get jet area
Definition: Jet.h:105
PileupJetIdentifier computeMva()
Definition: MVAJetPuId.cc:204
edm::EDGetTokenT< double > input_rho_token_
edm::EDGetTokenT< edm::ValueMap< StoredPileupJetIdentifier > > input_vm_pujetid_token_
def move(src, dest)
Definition: eostools.py:510
edm::EDGetTokenT< reco::VertexCollection > input_vertex_token_
const float & jetEta() const