CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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 
19 
20 // system include files
21 #include <memory>
22 
23 // user include files
27 
33 
35 
39 
43 
45 
46 // ------------------------------------------------------------------------------------------
48 public:
49  explicit PileupJetIdProducer(const edm::ParameterSet&);
51 
52  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
53 
54 private:
55  virtual void produce(edm::Event&, const edm::EventSetup&) override;
56 
57 
58  void initJetEnergyCorrector(const edm::EventSetup &iSetup, bool isData);
59 
63  std::vector<std::pair<std::string, PileupJetIdAlgo *> > algos_;
64 
68  std::vector<JetCorrectorParameters> jetCorPars_;
69 };
70 
71 // ------------------------------------------------------------------------------------------
73 {
74  runMvas_ = iConfig.getParameter<bool>("runMvas");
75  produceJetIds_ = iConfig.getParameter<bool>("produceJetIds");
76  jets_ = iConfig.getParameter<edm::InputTag>("jets");
77  vertexes_ = iConfig.getParameter<edm::InputTag>("vertexes");
78  jetids_ = iConfig.getParameter<edm::InputTag>("jetids");
79  inputIsCorrected_ = iConfig.getParameter<bool>("inputIsCorrected");
80  applyJec_ = iConfig.getParameter<bool>("applyJec");
81  jec_ = iConfig.getParameter<std::string>("jec");
82  rho_ = iConfig.getParameter<edm::InputTag>("rho");
83  residualsFromTxt_ = iConfig.getParameter<bool>("residualsFromTxt");
84  residualsTxt_ = iConfig.getParameter<edm::FileInPath>("residualsTxt");
85  std::vector<edm::ParameterSet> algos = iConfig.getParameter<std::vector<edm::ParameterSet> >("algos");
86 
87  jecCor_ = 0;
88 
89  if( ! runMvas_ ) assert( algos.size() == 1 );
90 
91  if( produceJetIds_ ) {
92  produces<edm::ValueMap<StoredPileupJetIdentifier> > ("");
93  }
94  for(std::vector<edm::ParameterSet>::iterator it=algos.begin(); it!=algos.end(); ++it) {
95  std::string label = it->getParameter<std::string>("label");
96  algos_.push_back( std::make_pair(label,new PileupJetIdAlgo(*it)) );
97  if( runMvas_ ) {
98  produces<edm::ValueMap<float> > (label+"Discriminant");
99  produces<edm::ValueMap<int> > (label+"Id");
100  }
101  }
102 }
103 
104 
105 
106 // ------------------------------------------------------------------------------------------
108 {
109 }
110 
111 
112 // ------------------------------------------------------------------------------------------
113 void
115 {
116  using namespace edm;
117  using namespace std;
118  using namespace reco;
119 
120  // Input jets
121  Handle<View<Jet> > jetHandle;
122  iEvent.getByLabel(jets_,jetHandle);
123  const View<Jet> & jets = *jetHandle;
124  // vertexes
125  Handle<VertexCollection> vertexHandle;
126  if( produceJetIds_ ) {
127  iEvent.getByLabel(vertexes_, vertexHandle);
128  }
129  const VertexCollection & vertexes = *(vertexHandle.product());
130  // input variables
132  if( ! produceJetIds_ ) {
133  iEvent.getByLabel(jetids_, vmap);
134  }
135  // rho
137  double rho = 0.;
138 
139  // products
140  vector<StoredPileupJetIdentifier> ids;
141  map<string, vector<float> > mvas;
142  map<string, vector<int> > idflags;
143 
144  VertexCollection::const_iterator vtx;
145  if( produceJetIds_ ) {
146  // require basic quality cuts on the vertexes
147  vtx = vertexes.begin();
148  while( vtx != vertexes.end() && ( vtx->isFake() || vtx->ndof() < 4 ) ) {
149  ++vtx;
150  }
151  if( vtx == vertexes.end() ) { vtx = vertexes.begin(); }
152  }
153 
154  // Loop over input jets
155  for ( unsigned int i=0; i<jets.size(); ++i ) {
156  // Pick the first algo to compute the input variables
157  vector<pair<string,PileupJetIdAlgo *> >::iterator algoi = algos_.begin();
158  PileupJetIdAlgo * ialgo = algoi->second;
159 
160  const Jet & jet = jets.at(i);
161  //const pat::Jet * patjet = dynamic_cast<const pat::Jet *>(&jet);
162  //bool ispat = patjet != 0;
163 
164  // Get jet energy correction
165  float jec = 0.;
166  if( applyJec_ ) {
167  // If haven't done it get rho from the event
168  if( rho == 0. ) {
169  iEvent.getByLabel(rho_,rhoH);
170  rho = *rhoH;
171  }
172  // jet corrector
173  if( jecCor_ == 0 ) {
174  initJetEnergyCorrector( iSetup, iEvent.isRealData() );
175  }
176  //if( ispat ) {
177  // jecCor_->setJetPt(patjet->correctedJet(0).pt());
178  //} else {
179  jecCor_->setJetPt(jet.pt());
180  //}
181  jecCor_->setJetEta(jet.eta());
182  jecCor_->setJetA(jet.jetArea());
183  jecCor_->setRho(rho);
184  jec = jecCor_->getCorrection();
185  }
186 
187  // If it was requested or the input is an uncorrected jet apply the JEC
188  bool applyJec = applyJec_ || !inputIsCorrected_; //( ! ispat && ! inputIsCorrected_ );
189  reco::Jet * corrJet = 0;
190  if( applyJec ) {
191  float scale = jec;
192  //if( ispat ) {
193  // corrJet = new pat::Jet(patjet->correctedJet(0)) ;
194  //} else {
195  corrJet = dynamic_cast<reco::Jet *>( jet.clone() );
196  //}
197  corrJet->scaleEnergy(scale);
198  }
199  const reco::Jet * theJet = ( applyJec ? corrJet : &jet );
200 
201  PileupJetIdentifier puIdentifier;
202  if( produceJetIds_ ) {
203  // Compute the input variables
204  puIdentifier = ialgo->computeIdVariables(theJet, jec, &(*vtx), vertexes, runMvas_);
205  ids.push_back( puIdentifier );
206  } else {
207  // Or read it from the value map
208  puIdentifier = (*vmap)[jets.refAt(i)];
209  puIdentifier.jetPt(theJet->pt()); // make sure JEC is applied when computing the MVA
210  puIdentifier.jetEta(theJet->eta());
211  puIdentifier.jetPhi(theJet->phi());
212  ialgo->set(puIdentifier);
213  puIdentifier = ialgo->computeMva();
214  }
215 
216  if( runMvas_ ) {
217  // Compute the MVA and WP
218  mvas[algoi->first].push_back( puIdentifier.mva() );
219  idflags[algoi->first].push_back( puIdentifier.idFlag() );
220  for( ++algoi; algoi!=algos_.end(); ++algoi) {
221  ialgo = algoi->second;
222  ialgo->set(puIdentifier);
223  PileupJetIdentifier id = ialgo->computeMva();
224  mvas[algoi->first].push_back( id.mva() );
225  idflags[algoi->first].push_back( id.idFlag() );
226  }
227  }
228 
229  // cleanup
230  if( corrJet ) { delete corrJet; }
231  }
232 
233  // Produce the output value maps
234  if( runMvas_ ) {
235  for(vector<pair<string,PileupJetIdAlgo *> >::iterator ialgo = algos_.begin(); ialgo!=algos_.end(); ++ialgo) {
236  // MVA
237  vector<float> & mva = mvas[ialgo->first];
238  auto_ptr<ValueMap<float> > mvaout(new ValueMap<float>());
239  ValueMap<float>::Filler mvafiller(*mvaout);
240  mvafiller.insert(jetHandle,mva.begin(),mva.end());
241  mvafiller.fill();
242  iEvent.put(mvaout,ialgo->first+"Discriminant");
243 
244  // WP
245  vector<int> & idflag = idflags[ialgo->first];
246  auto_ptr<ValueMap<int> > idflagout(new ValueMap<int>());
247  ValueMap<int>::Filler idflagfiller(*idflagout);
248  idflagfiller.insert(jetHandle,idflag.begin(),idflag.end());
249  idflagfiller.fill();
250  iEvent.put(idflagout,ialgo->first+"Id");
251  }
252  }
253  // input variables
254  if( produceJetIds_ ) {
255  assert( jetHandle->size() == ids.size() );
256  auto_ptr<ValueMap<StoredPileupJetIdentifier> > idsout(new ValueMap<StoredPileupJetIdentifier>());
258  idsfiller.insert(jetHandle,ids.begin(),ids.end());
259  idsfiller.fill();
260  iEvent.put(idsout);
261  }
262 }
263 
264 
265 
266 // ------------------------------------------------------------------------------------------
267 void
269  //The following says we do not know what parameters are allowed so do no validation
270  // Please change this to state exactly what you do use, even if it is no parameters
272  desc.setUnknown();
273  descriptions.addDefault(desc);
274 }
275 
276 
277 // ------------------------------------------------------------------------------------------
278 void
280 {
281  //jet energy correction levels to apply on raw jet
282  std::vector<std::string> jecLevels;
283  jecLevels.push_back("L1FastJet");
284  jecLevels.push_back("L2Relative");
285  jecLevels.push_back("L3Absolute");
286  if(isData && ! residualsFromTxt_ ) jecLevels.push_back("L2L3Residual");
287 
288  //check the corrector parameters needed according to the correction levels
290  iSetup.get<JetCorrectionsRecord>().get(jec_,parameters);
291  for(std::vector<std::string>::const_iterator ll = jecLevels.begin(); ll != jecLevels.end(); ++ll)
292  {
293  const JetCorrectorParameters& ip = (*parameters)[*ll];
294  jetCorPars_.push_back(ip);
295  }
296  if( isData && residualsFromTxt_ ) {
298  }
299 
300  //instantiate the jet corrector
302 }
303 //define this as a plug-in
T getParameter(std::string const &) const
void set(const PileupJetIdentifier &)
int i
Definition: DBlmapReader.cc:9
dictionary parameters
Definition: Parameters.py:2
std::vector< JetCorrectorParameters > jetCorPars_
std::vector< std::pair< std::string, PileupJetIdAlgo * > > algos_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
Base class for all types of Jets.
Definition: Jet.h:20
Definition: DDAxes.h:10
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
virtual void scaleEnergy(double fScale)
scale energy of the jet
Definition: Jet.cc:444
bool isRealData() const
Definition: EventBase.h:60
PileupJetIdProducer(const edm::ParameterSet &)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
virtual CompositePtrCandidate * clone() const
returns a clone of the candidate
virtual float phi() const GCC11_FINAL
momentum azimuthal angle
FactorizedJetCorrector * jecCor_
int iEvent
Definition: GenABIO.cc:243
void addDefault(ParameterSetDescription const &psetDescription)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
vector< PseudoJet > jets
edm::FileInPath residualsTxt_
PileupJetIdentifier computeIdVariables(const reco::Jet *jet, float jec, const reco::Vertex *, const reco::VertexCollection &, bool calculateMva=false)
void initJetEnergyCorrector(const edm::EventSetup &iSetup, bool isData)
virtual void produce(edm::Event &, const edm::EventSetup &) override
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
virtual float eta() const GCC11_FINAL
momentum pseudorapidity
const T & get() const
Definition: EventSetup.h:55
PileupJetIdentifier computeMva()
virtual float jetArea() const
get jet area
Definition: Jet.h:105
std::string fullPath() const
Definition: FileInPath.cc:171
virtual float pt() const GCC11_FINAL
transverse momentum