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 #include <memory>
21 
22 
23 // ------------------------------------------------------------------------------------------
25 {
26  runMvas_ = iConfig.getParameter<bool>("runMvas");
27  produceJetIds_ = iConfig.getParameter<bool>("produceJetIds");
28  jets_ = iConfig.getParameter<edm::InputTag>("jets");
29  vertexes_ = iConfig.getParameter<edm::InputTag>("vertexes");
30  jetids_ = iConfig.getParameter<edm::InputTag>("jetids");
31  inputIsCorrected_ = iConfig.getParameter<bool>("inputIsCorrected");
32  applyJec_ = iConfig.getParameter<bool>("applyJec");
33  jec_ = iConfig.getParameter<std::string>("jec");
34  rho_ = iConfig.getParameter<edm::InputTag>("rho");
35  residualsFromTxt_ = iConfig.getParameter<bool>("residualsFromTxt");
36  if(residualsFromTxt_) residualsTxt_ = iConfig.getParameter<edm::FileInPath>("residualsTxt");
37  std::vector<edm::ParameterSet> algos = iConfig.getParameter<std::vector<edm::ParameterSet> >("algos");
38 
39  jecCor_ = 0;
40 
41  if( ! runMvas_ ) assert( algos.size() == 1 );
42 
43  if( produceJetIds_ ) {
44  produces<edm::ValueMap<StoredPileupJetIdentifier> > ("");
45  }
46  for(std::vector<edm::ParameterSet>::iterator it=algos.begin(); it!=algos.end(); ++it) {
47  std::string label = it->getParameter<std::string>("label");
48  algos_.push_back( std::make_pair(label,new PileupJetIdAlgo(*it, runMvas_)) );
49  if( runMvas_ ) {
50  produces<edm::ValueMap<float> > (label+"Discriminant");
51  produces<edm::ValueMap<int> > (label+"Id");
52  }
53  }
54 
55  input_jet_token_ = consumes<edm::View<reco::Jet> >(jets_);
56  input_vertex_token_ = consumes<reco::VertexCollection>(vertexes_);
57  input_vm_pujetid_token_ = consumes<edm::ValueMap<StoredPileupJetIdentifier> >(jetids_);
58  input_rho_token_ = consumes<double>(rho_);
59 
60 }
61 
62 
63 
64 // ------------------------------------------------------------------------------------------
66 {
67  for(std::vector<std::pair<std::string,PileupJetIdAlgo *> >::iterator
68  ialgo = algos_.begin(); ialgo!=algos_.end(); ++ialgo) {
69  delete ialgo->second;
70  }
71 }
72 
73 
74 // ------------------------------------------------------------------------------------------
75 void
77 {
78  using namespace edm;
79  using namespace std;
80  using namespace reco;
81 
82  // Input jets
83  Handle<View<Jet> > jetHandle;
84  iEvent.getByToken(input_jet_token_,jetHandle);
85  const View<Jet> & jets = *jetHandle;
86  // vertexes
87  Handle<VertexCollection> vertexHandle;
88  if( produceJetIds_ ) {
89  iEvent.getByToken(input_vertex_token_, vertexHandle);
90  }
91  const VertexCollection & vertexes = *(vertexHandle.product());
92  // input variables
94  if( ! produceJetIds_ ) {
95  iEvent.getByToken(input_vm_pujetid_token_, vmap);
96  }
97  // rho
99  double rho = 0.;
100 
101  // products
102  vector<StoredPileupJetIdentifier> ids;
103  map<string, vector<float> > mvas;
104  map<string, vector<int> > idflags;
105 
106  VertexCollection::const_iterator vtx;
107  if( produceJetIds_ ) {
108  // require basic quality cuts on the vertexes
109  vtx = vertexes.begin();
110  while( vtx != vertexes.end() && ( vtx->isFake() || vtx->ndof() < 4 ) ) {
111  ++vtx;
112  }
113  if( vtx == vertexes.end() ) { vtx = vertexes.begin(); }
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  vector<pair<string,PileupJetIdAlgo *> >::iterator algoi = algos_.begin();
121  PileupJetIdAlgo * ialgo = algoi->second;
122 
123  const Jet & jet = jets.at(i);
124  const pat::Jet * patjet = 0;
125  if(ispat) {
126  patjet=dynamic_cast<const pat::Jet *>(&jet);
127  ispat = patjet != 0;
128  }
129 
130  // Get jet energy correction
131  float jec = 0.;
132  if( 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( jecCor_ == 0 ) {
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 = applyJec_ && ( ispat || !inputIsCorrected_ );
154  reco::Jet * corrJet = 0;
155 
156  if( applyJec ) {
157  float scale = jec;
158  if( ispat ) {
159  corrJet = new pat::Jet(patjet->correctedJet(0)) ;
160  } else {
161  corrJet = dynamic_cast<reco::Jet *>( jet.clone() );
162  }
163  corrJet->scaleEnergy(scale);
164  }
165  const reco::Jet * theJet = ( applyJec ? corrJet : &jet );
166 
167  PileupJetIdentifier puIdentifier;
168  if( produceJetIds_ ) {
169  // Compute the input variables
170  puIdentifier = ialgo->computeIdVariables(theJet, jec, &(*vtx), vertexes, rho);
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( 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;
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  // cleanup
196  if( corrJet ) { delete corrJet; }
197  }
198 
199  // Produce the output value maps
200  if( runMvas_ ) {
201  for(vector<pair<string,PileupJetIdAlgo *> >::iterator ialgo = algos_.begin(); ialgo!=algos_.end(); ++ialgo) {
202  // MVA
203  vector<float> & mva = mvas[ialgo->first];
204  auto_ptr<ValueMap<float> > mvaout(new ValueMap<float>());
205  ValueMap<float>::Filler mvafiller(*mvaout);
206  mvafiller.insert(jetHandle,mva.begin(),mva.end());
207  mvafiller.fill();
208  iEvent.put(mvaout,ialgo->first+"Discriminant");
209 
210  // WP
211  vector<int> & idflag = idflags[ialgo->first];
212  auto_ptr<ValueMap<int> > idflagout(new ValueMap<int>());
213  ValueMap<int>::Filler idflagfiller(*idflagout);
214  idflagfiller.insert(jetHandle,idflag.begin(),idflag.end());
215  idflagfiller.fill();
216  iEvent.put(idflagout,ialgo->first+"Id");
217  }
218  }
219  // input variables
220  if( produceJetIds_ ) {
221  assert( jetHandle->size() == ids.size() );
222  auto_ptr<ValueMap<StoredPileupJetIdentifier> > idsout(new ValueMap<StoredPileupJetIdentifier>());
224  idsfiller.insert(jetHandle,ids.begin(),ids.end());
225  idsfiller.fill();
226  iEvent.put(idsout);
227  }
228 }
229 
230 
231 
232 // ------------------------------------------------------------------------------------------
233 void
235  //The following says we do not know what parameters are allowed so do no validation
236  // Please change this to state exactly what you do use, even if it is no parameters
238  desc.setUnknown();
239  descriptions.addDefault(desc);
240 }
241 
242 
243 // ------------------------------------------------------------------------------------------
244 void
246 {
247  //jet energy correction levels to apply on raw jet
248  std::vector<std::string> jecLevels;
249  jecLevels.push_back("L1FastJet");
250  jecLevels.push_back("L2Relative");
251  jecLevels.push_back("L3Absolute");
252  if(isData && ! residualsFromTxt_ ) jecLevels.push_back("L2L3Residual");
253 
254  //check the corrector parameters needed according to the correction levels
256  iSetup.get<JetCorrectionsRecord>().get(jec_,parameters);
257  for(std::vector<std::string>::const_iterator ll = jecLevels.begin(); ll != jecLevels.end(); ++ll)
258  {
259  const JetCorrectorParameters& ip = (*parameters)[*ll];
260  jetCorPars_.push_back(ip);
261  }
262  if( isData && residualsFromTxt_ ) {
264  }
265 
266  //instantiate the jet corrector
268 }
269 //define this as a plug-in
T getParameter(std::string const &) const
void set(const PileupJetIdentifier &)
int i
Definition: DBlmapReader.cc:9
std::vector< JetCorrectorParameters > jetCorPars_
virtual void scaleEnergy(double fScale)
scale energy of the jet
edm::EDGetTokenT< reco::VertexCollection > input_vertex_token_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
const int & idFlag() const
std::vector< std::pair< std::string, PileupJetIdAlgo * > > algos_
const float & mva() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
Base class for all types of Jets.
Definition: Jet.h:20
assert(m_qm.get())
edm::EDGetTokenT< edm::View< reco::Jet > > input_jet_token_
virtual double phi() const final
momentum azimuthal angle
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
bool isRealData() const
Definition: EventBase.h:63
const float & jetPt() const
PileupJetIdProducer(const edm::ParameterSet &)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
virtual CompositePtrCandidate * clone() const
returns a clone of the candidate
FactorizedJetCorrector * jecCor_
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
vector< PseudoJet > jets
edm::EDGetTokenT< double > input_rho_token_
edm::FileInPath residualsTxt_
void initJetEnergyCorrector(const edm::EventSetup &iSetup, bool isData)
virtual void produce(edm::Event &, const edm::EventSetup &) override
const float & jetPhi() const
PileupJetIdentifier computeIdVariables(const reco::Jet *jet, float jec, const reco::Vertex *, const reco::VertexCollection &, double rho)
const T & get() const
Definition: EventSetup.h:56
PileupJetIdentifier computeMva()
Analysis-level calorimeter jet class.
Definition: Jet.h:78
virtual float jetArea() const
get jet area
Definition: Jet.h:105
edm::EDGetTokenT< edm::ValueMap< StoredPileupJetIdentifier > > input_vm_pujetid_token_
virtual double eta() const final
momentum pseudorapidity
std::string fullPath() const
Definition: FileInPath.cc:184
Jet correctedJet(const std::string &level, const std::string &flavor="none", const std::string &set="") const
virtual double pt() const final
transverse momentum
const float & jetEta() const