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)) );
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 }
68 
69 
70 // ------------------------------------------------------------------------------------------
71 void
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  // vertexes
83  Handle<VertexCollection> vertexHandle;
84  if( produceJetIds_ ) {
85  iEvent.getByToken(input_vertex_token_, vertexHandle);
86  }
87  const VertexCollection & vertexes = *(vertexHandle.product());
88  // input variables
90  if( ! produceJetIds_ ) {
91  iEvent.getByToken(input_vm_pujetid_token_, vmap);
92  }
93  // rho
95  double rho = 0.;
96 
97  // products
98  vector<StoredPileupJetIdentifier> ids;
99  map<string, vector<float> > mvas;
100  map<string, vector<int> > idflags;
101 
102  VertexCollection::const_iterator vtx;
103  if( produceJetIds_ ) {
104  // require basic quality cuts on the vertexes
105  vtx = vertexes.begin();
106  while( vtx != vertexes.end() && ( vtx->isFake() || vtx->ndof() < 4 ) ) {
107  ++vtx;
108  }
109  if( vtx == vertexes.end() ) { vtx = vertexes.begin(); }
110  }
111 
112  // Loop over input jets
113  for ( unsigned int i=0; i<jets.size(); ++i ) {
114  // Pick the first algo to compute the input variables
115  vector<pair<string,PileupJetIdAlgo *> >::iterator algoi = algos_.begin();
116  PileupJetIdAlgo * ialgo = algoi->second;
117 
118  const Jet & jet = jets.at(i);
119  //const pat::Jet * patjet = dynamic_cast<const pat::Jet *>(&jet);
120  //bool ispat = patjet != 0;
121 
122  // Get jet energy correction
123  float jec = 0.;
124  if( applyJec_ ) {
125  // If haven't done it get rho from the event
126  if( rho == 0. ) {
127  iEvent.getByToken(input_rho_token_,rhoH);
128  rho = *rhoH;
129  }
130  // jet corrector
131  if( jecCor_ == 0 ) {
132  initJetEnergyCorrector( iSetup, iEvent.isRealData() );
133  }
134  //if( ispat ) {
135  // jecCor_->setJetPt(patjet->correctedJet(0).pt());
136  //} else {
137  jecCor_->setJetPt(jet.pt());
138  //}
139  jecCor_->setJetEta(jet.eta());
140  jecCor_->setJetA(jet.jetArea());
141  jecCor_->setRho(rho);
142  jec = jecCor_->getCorrection();
143  }
144 
145  // If it was requested or the input is an uncorrected jet apply the JEC
146  bool applyJec = applyJec_ || !inputIsCorrected_; //( ! ispat && ! inputIsCorrected_ );
147  reco::Jet * corrJet = 0;
148  if( applyJec ) {
149  float scale = jec;
150  //if( ispat ) {
151  // corrJet = new pat::Jet(patjet->correctedJet(0)) ;
152  //} else {
153  corrJet = dynamic_cast<reco::Jet *>( jet.clone() );
154  //}
155  corrJet->scaleEnergy(scale);
156  }
157  const reco::Jet * theJet = ( applyJec ? corrJet : &jet );
158 
159  PileupJetIdentifier puIdentifier;
160  if( produceJetIds_ ) {
161  // Compute the input variables
162  puIdentifier = ialgo->computeIdVariables(theJet, jec, &(*vtx), vertexes, runMvas_);
163  ids.push_back( puIdentifier );
164  } else {
165  // Or read it from the value map
166  puIdentifier = (*vmap)[jets.refAt(i)];
167  puIdentifier.jetPt(theJet->pt()); // make sure JEC is applied when computing the MVA
168  puIdentifier.jetEta(theJet->eta());
169  puIdentifier.jetPhi(theJet->phi());
170  ialgo->set(puIdentifier);
171  puIdentifier = ialgo->computeMva();
172  }
173 
174  if( runMvas_ ) {
175  // Compute the MVA and WP
176  mvas[algoi->first].push_back( puIdentifier.mva() );
177  idflags[algoi->first].push_back( puIdentifier.idFlag() );
178  for( ++algoi; algoi!=algos_.end(); ++algoi) {
179  ialgo = algoi->second;
180  ialgo->set(puIdentifier);
181  PileupJetIdentifier id = ialgo->computeMva();
182  mvas[algoi->first].push_back( id.mva() );
183  idflags[algoi->first].push_back( id.idFlag() );
184  }
185  }
186 
187  // cleanup
188  if( corrJet ) { delete corrJet; }
189  }
190 
191  // Produce the output value maps
192  if( runMvas_ ) {
193  for(vector<pair<string,PileupJetIdAlgo *> >::iterator ialgo = algos_.begin(); ialgo!=algos_.end(); ++ialgo) {
194  // MVA
195  vector<float> & mva = mvas[ialgo->first];
196  auto_ptr<ValueMap<float> > mvaout(new ValueMap<float>());
197  ValueMap<float>::Filler mvafiller(*mvaout);
198  mvafiller.insert(jetHandle,mva.begin(),mva.end());
199  mvafiller.fill();
200  iEvent.put(mvaout,ialgo->first+"Discriminant");
201 
202  // WP
203  vector<int> & idflag = idflags[ialgo->first];
204  auto_ptr<ValueMap<int> > idflagout(new ValueMap<int>());
205  ValueMap<int>::Filler idflagfiller(*idflagout);
206  idflagfiller.insert(jetHandle,idflag.begin(),idflag.end());
207  idflagfiller.fill();
208  iEvent.put(idflagout,ialgo->first+"Id");
209  }
210  }
211  // input variables
212  if( produceJetIds_ ) {
213  assert( jetHandle->size() == ids.size() );
214  auto_ptr<ValueMap<StoredPileupJetIdentifier> > idsout(new ValueMap<StoredPileupJetIdentifier>());
216  idsfiller.insert(jetHandle,ids.begin(),ids.end());
217  idsfiller.fill();
218  iEvent.put(idsout);
219  }
220 }
221 
222 
223 
224 // ------------------------------------------------------------------------------------------
225 void
227  //The following says we do not know what parameters are allowed so do no validation
228  // Please change this to state exactly what you do use, even if it is no parameters
230  desc.setUnknown();
231  descriptions.addDefault(desc);
232 }
233 
234 
235 // ------------------------------------------------------------------------------------------
236 void
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 && ! residualsFromTxt_ ) jecLevels.push_back("L2L3Residual");
245 
246  //check the corrector parameters needed according to the correction levels
248  iSetup.get<JetCorrectionsRecord>().get(jec_,parameters);
249  for(std::vector<std::string>::const_iterator ll = jecLevels.begin(); ll != jecLevels.end(); ++ll)
250  {
251  const JetCorrectorParameters& ip = (*parameters)[*ll];
252  jetCorPars_.push_back(ip);
253  }
254  if( isData && residualsFromTxt_ ) {
256  }
257 
258  //instantiate the jet corrector
260 }
261 //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_
virtual void scaleEnergy(double fScale)
scale energy of the jet
virtual float pt() const
transverse momentum
edm::EDGetTokenT< reco::VertexCollection > input_vertex_token_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:446
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
virtual float phi() const
momentum azimuthal angle
Base class for all types of Jets.
Definition: Jet.h:20
Definition: DDAxes.h:10
edm::EDGetTokenT< edm::View< reco::Jet > > input_jet_token_
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
bool isRealData() const
Definition: EventBase.h:60
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:113
virtual float eta() const
momentum pseudorapidity
vector< PseudoJet > jets
edm::EDGetTokenT< double > input_rho_token_
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
const float & jetPhi() const
const T & get() const
Definition: EventSetup.h:55
PileupJetIdentifier computeMva()
virtual float jetArea() const
get jet area
Definition: Jet.h:105
edm::EDGetTokenT< edm::ValueMap< StoredPileupJetIdentifier > > input_vm_pujetid_token_
std::string fullPath() const
Definition: FileInPath.cc:165
const float & jetEta() const