CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
JetCorrFactorsProducer.cc
Go to the documentation of this file.
1 #include <vector>
2 #include <string>
3 #include <iostream>
4 
14 
18 
19 
20 using namespace pat;
21 
23  emf_(cfg.getParameter<bool>( "emf" )),
24  srcToken_(consumes<edm::View<reco::Jet> >(cfg.getParameter<edm::InputTag>( "src" ))),
25  type_ (cfg.getParameter<std::string>("flavorType")),
26  label_(cfg.getParameter<std::string>( "@module_label" )),
27  payload_( cfg.getParameter<std::string>("payload") ),
28  useNPV_(cfg.getParameter<bool>("useNPV")),
29  useRho_(cfg.getParameter<bool>("useRho")),
30  cacheId_(0)
31 {
32  std::vector<std::string> levels = cfg.getParameter<std::vector<std::string> >("levels");
33  // fill the std::map for levels_, which might be flavor dependent or not;
34  // flavor dependency is determined from the fact whether the std::string
35  // L5Flavor or L7Parton can be found in levels; if flavor dependent four
36  // vectors of strings will be filled into the map corresponding to GLUON,
37  // UDS, CHARM and BOTTOM (according to JetCorrFactors::Flavor), 'L5Flavor'
38  // and 'L7Parton' will be expanded accordingly; if not levels_ is filled
39  // with only one vector of strings according to NONE. This vector will be
40  // equivalent to the original vector of strings.
41  if(std::find(levels.begin(), levels.end(), "L5Flavor")!=levels.end() || std::find(levels.begin(), levels.end(), "L7Parton")!=levels.end()){
46  }
47  else{
48  levels_[JetCorrFactors::NONE ] = levels;
49  }
50  // if the std::string L1JPTOffset can be found in levels an additional
51  // parameter extraJPTOffset is needed, which should pass on the the usual
52  // L1Offset correction, which is an additional input to the L1JPTOffset
53  // corrector
54  if(std::find(levels.begin(), levels.end(), "L1JPTOffset")!=levels.end()){
55  if(cfg.existsAs<std::string>("extraJPTOffset")){
56  extraJPTOffset_.push_back(cfg.getParameter<std::string>("extraJPTOffset"));
57  }
58  else{
59  throw cms::Exception("No parameter extraJPTOffset specified")
60  << "The configured correction levels contain a L1JPTOffset correction, which re- \n"
61  << "quires the additional parameter extraJPTOffset or type std::string. This \n"
62  << "string should correspond to the L1Offset corrections that should be applied \n"
63  << "together with the JPTL1Offset corrections. These corrections can be of type \n"
64  << "L1Offset or L1FastJet. \n";
65  }
66  }
67  // if the std::string L1Offset can be found in levels an additional para-
68  // meter primaryVertices is needed, which should pass on the offline pri-
69  // mary vertex collection. The size of this collection is needed for the
70  // L1Offset correction.
71  if(useNPV_){
72  if(cfg.existsAs<edm::InputTag>("primaryVertices")){
73  primaryVertices_=cfg.getParameter<edm::InputTag>("primaryVertices");
74  primaryVerticesToken_=mayConsume<std::vector<reco::Vertex> >(primaryVertices_);
75  }
76  else{
77  throw cms::Exception("No primaryVertices specified")
78  << "The configured correction levels contain an L1Offset or L1FastJet correction, \n"
79  << "which requires the number of offlinePrimaryVertices. Please specify this col- \n"
80  << "lection as additional optional parameter primaryVertices of type edm::InputTag\n"
81  << "in the jetCorrFactors module. \n";
82  }
83  }
84  // if the std::string L1FastJet can be found in levels an additional
85  // parameter rho is needed, which should pass on the energy density
86  // parameter for the corresponding jet collection.
87  if(useRho_){
88  if((!extraJPTOffset_.empty() && extraJPTOffset_.front()==std::string("L1FastJet")) || std::find(levels.begin(), levels.end(), "L1FastJet")!=levels.end()){
89  if(cfg.existsAs<edm::InputTag>("rho")){
90  rho_=cfg.getParameter<edm::InputTag>("rho");
91  rhoToken_=mayConsume<double>(rho_);
92  }
93  else{
94  throw cms::Exception("No parameter rho specified")
95  << "The configured correction levels contain a L1FastJet correction, which re- \n"
96  << "quires the energy density parameter rho. Please specify this collection as \n"
97  << "additional optional parameter rho of type edm::InputTag in the jetCorrFac- \n"
98  << "tors module. \n";
99  }
100  }
101  else{
102  edm::LogInfo message( "Parameter rho not used" );
103  message << "Module is configured to use the parameter rho, but rho is only used \n"
104  << "for L1FastJet corrections. The configuration of levels does not contain \n"
105  << "L1FastJet corrections though, so rho will not be used by this module. \n";
106  }
107  }
108  produces<JetCorrFactorsMap>();
109 }
110 
111 std::vector<std::string>
112 JetCorrFactorsProducer::expand(const std::vector<std::string>& levels, const JetCorrFactors::Flavor& flavor)
113 {
114  std::vector<std::string> expand;
115  for(std::vector<std::string>::const_iterator level=levels.begin(); level!=levels.end(); ++level){
116  if((*level)=="L5Flavor" || (*level)=="L7Parton"){
117  if(flavor==JetCorrFactors::GLUON ){
118  if(*level=="L7Parton" && type_=="T"){
119  edm::LogWarning message( "L7Parton::GLUON not available" );
120  message << "Jet energy corrections requested for level: L7Parton and type: 'T'. \n"
121  << "For this combination there is no GLUON correction available. The \n"
122  << "correction for this flavor type will be taken from 'J'.";
123  }
124  expand.push_back(std::string(*level).append("_").append("g").append("J"));
125  }
126  if(flavor==JetCorrFactors::UDS ) expand.push_back(std::string(*level).append("_").append("q").append(type_));
127  if(flavor==JetCorrFactors::CHARM ) expand.push_back(std::string(*level).append("_").append("c").append(type_));
128  if(flavor==JetCorrFactors::BOTTOM) expand.push_back(std::string(*level).append("_").append("b").append(type_));
129  }
130  else{
131  expand.push_back(*level);
132  }
133  }
134  return expand;
135 }
136 
137 std::vector<JetCorrectorParameters>
138 JetCorrFactorsProducer::params(const JetCorrectorParametersCollection& parameters, const std::vector<std::string>& levels) const
139 {
140  std::vector<JetCorrectorParameters> params;
141  for(std::vector<std::string>::const_iterator level=levels.begin(); level!=levels.end(); ++level){
142  const JetCorrectorParameters& ip = parameters[*level]; //ip.printScreen();
143  params.push_back(ip);
144  }
145  return params;
146 }
147 
148 float
150 {
151  std::unique_ptr<FactorizedJetCorrector>& corrector = correctors_.find(flavor)->second;
152  // add parameters for JPT corrections
153  const reco::JPTJet* jpt = dynamic_cast<reco::JPTJet const *>( &*jet );
154  if( jpt ){
155  TLorentzVector p4; p4.SetPtEtaPhiE(jpt->getCaloJetRef()->pt(), jpt->getCaloJetRef()->eta(), jpt->getCaloJetRef()->phi(), jpt->getCaloJetRef()->energy());
157  extraJPTOffsetCorrector_->setJPTrawP4(p4);
158  corrector->setJPTrawOff(extraJPTOffsetCorrector_->getSubCorrections()[0]);
159  }
160  corrector->setJPTrawP4(p4);
161  }
162  //For PAT jets undo previous jet energy corrections
163  const Jet* patjet = dynamic_cast<Jet const *>( &*jet );
164  if( patjet ){
165  corrector->setJetEta(patjet->correctedP4(0).eta()); corrector->setJetPt(patjet->correctedP4(0).pt()); corrector->setJetE(patjet->correctedP4(0).energy());
166  } else {
167  corrector->setJetEta(jet->eta()); corrector->setJetPt(jet->pt()); corrector->setJetE(jet->energy());
168  }
169  if( emf_ && dynamic_cast<const reco::CaloJet*>(&*jet)){
170  corrector->setJetEMF(dynamic_cast<const reco::CaloJet*>(&*jet)->emEnergyFraction());
171  }
172  return corrector->getSubCorrections()[level];
173 }
174 
177 {
178  return payload_;
179 }
180 
181 void
183 {
184  // get jet collection from the event
186  event.getByToken(srcToken_, jets);
187 
188  // get primary vertices for L1Offset correction level if needed
190  if(!primaryVertices_.label().empty()) event.getByToken(primaryVerticesToken_, primaryVertices);
191 
192  // get parameter rho for L1FastJet correction level if needed
194  if(!rho_.label().empty()) event.getByToken(rhoToken_, rho);
195 
196  auto const& rec = setup.get<JetCorrectionsRecord>();
197  if (cacheId_ != rec.cacheIdentifier()) {
198  // retreive parameters from the DB
200  setup.get<JetCorrectionsRecord>().get(payload(), parameters);
201  // initialize jet correctors
202  for(FlavorCorrLevelMap::const_iterator flavor=levels_.begin(); flavor!=levels_.end(); ++flavor){
203  correctors_[flavor->first].reset( new FactorizedJetCorrector(params(*parameters, flavor->second)) );
204  }
205  // initialize extra jet corrector for jpt if needed
206  if(!extraJPTOffset_.empty()){
208  }
209  cacheId_ = rec.cacheIdentifier();
210  }
211 
212  // fill the jetCorrFactors
213  std::vector<JetCorrFactors> jcfs;
214  for(edm::View<reco::Jet>::const_iterator jet = jets->begin(); jet!=jets->end(); ++jet){
215  // the JetCorrFactors::CorrectionFactor is a std::pair<std::string, std::vector<float> >
216  // the string corresponds to the label of the correction level, the vector contains four
217  // floats if flavor dependent and one float else. Per construction jet energy corrections
218  // will be flavor independent up to the first flavor dependent correction and flavor de-
219  // pendent afterwards. The first correction level is predefined with label 'Uncorrected'.
220  // Per definition it is flavor independent. The correction factor is 1.
221  std::vector<JetCorrFactors::CorrectionFactor> jec;
222  jec.push_back(std::make_pair<std::string, std::vector<float> >(std::string("Uncorrected"), std::vector<float>(1, 1)));
223 
224  // pick the first element in the map (which could be the only one) and loop all jec
225  // levels listed for that element. If this is not the only element all jec levels, which
226  // are flavor independent will give the same correction factors until the first flavor
227  // dependent correction level is reached. So the first element is still a good choice.
228  FlavorCorrLevelMap::const_iterator corrLevel=levels_.begin();
229  if(corrLevel==levels_.end()){
230  throw cms::Exception("No JECFactors") << "You request to create a jetCorrFactors object with no JEC Levels indicated. \n"
231  << "This makes no sense, either you should correct this or drop the module from \n"
232  << "the sequence.";
233  }
234  for(unsigned int idx=0; idx<corrLevel->second.size(); ++idx){
235  bool flavorDependent=false;
236  std::vector<float> factors;
237  if(flavorDependent ||
238  corrLevel->second[idx].find("L5Flavor")!=std::string::npos ||
239  corrLevel->second[idx].find("L7Parton")!=std::string::npos){
240  flavorDependent=true;
241  // after the first encounter all subsequent correction levels are flavor dependent
242  for(FlavorCorrLevelMap::const_iterator flavor=corrLevel; flavor!=levels_.end(); ++flavor){
243  if(!primaryVertices_.label().empty()){
244  // if primaryVerticesToken_ has a value the number of primary vertices needs to be
245  // specified
246  correctors_.find(flavor->first)->second->setNPV(numberOf(primaryVertices));
247  }
248  if(!rho_.label().empty()){
249  // if rhoToken_ has a value the energy density parameter rho and the jet area need
250  // to be specified
251  correctors_.find(flavor->first)->second->setRho(*rho);
252  correctors_.find(flavor->first)->second->setJetA(jet->jetArea());
253  }
254  factors.push_back(evaluate(jet, flavor->first, idx));
255  }
256  }
257  else{
258  if(!primaryVertices_.label().empty()){
259  // if primaryVerticesToken_ has a value the number of primary vertices needs to be
260  // specified
261  correctors_.find(corrLevel->first)->second->setNPV(numberOf(primaryVertices));
262  }
263  if(!rho_.label().empty()){
264  // if rhoToken_ has a value the energy density parameter rho and the jet area need
265  // to be specified
266  correctors_.find(corrLevel->first)->second->setRho(*rho);
267  correctors_.find(corrLevel->first)->second->setJetA(jet->jetArea());
268  }
269  factors.push_back(evaluate(jet, corrLevel->first, idx));
270  }
271  // push back the set of JetCorrFactors: the first entry corresponds to the label
272  // of the correction level, which is taken from the first element in levels_. For
273  // L5Flavor and L7Parton the part including the first '_' indicating the flavor
274  // of the first element in levels_ is chopped of from the label to avoid confusion
275  // of the correction levels. The second parameter corresponds to the set of jec
276  // factors, which might be flavor dependent or not. In the default configuration
277  // the CorrectionFactor will look like this: 'Uncorrected': 1 ; 'L2Relative': x ;
278  // 'L3Absolute': x ; 'L5Flavor': v, x, y, z ; 'L7Parton': v, x, y, z
279  jec.push_back(std::make_pair((corrLevel->second[idx]).substr(0, (corrLevel->second[idx]).find("_")), factors));
280  }
281  // create the actual object with the scale factors we want the valuemap to refer to
282  // label_ corresponds to the label of the module instance
283  JetCorrFactors corrFactors(label_, jec);
284  jcfs.push_back(corrFactors);
285  }
286  // build the value map
287  std::auto_ptr<JetCorrFactorsMap> jetCorrsMap(new JetCorrFactorsMap());
288  JetCorrFactorsMap::Filler filler(*jetCorrsMap);
289  // jets and jetCorrs have their indices aligned by construction
290  filler.insert(jets, jcfs.begin(), jcfs.end());
291  filler.fill(); // do the actual filling
292  // put our produced stuff in the event
293  event.put(jetCorrsMap);
294 }
295 
296 void
298 {
300  iDesc.add<bool>("emf", false);
301  iDesc.add<std::string>("flavorType", "J");
302  iDesc.add<edm::InputTag>("src", edm::InputTag("ak5CaloJets"));
303  iDesc.add<std::string>("payload", "AK5Calo");
304  iDesc.add<bool>("useNPV", true);
305  iDesc.add<edm::InputTag>("primaryVertices", edm::InputTag("offlinePrimaryVertices"));
306  iDesc.add<bool>("useRho", true);
307  iDesc.add<edm::InputTag>("rho", edm::InputTag("fixedGridRhoFastjetAllCalo"));
308  iDesc.add<std::string>("extraJPTOffset", "L1Offset");
309 
310  std::vector<std::string> levels;
311  levels.push_back(std::string("L1Offset" ));
312  levels.push_back(std::string("L2Relative"));
313  levels.push_back(std::string("L3Absolute"));
314  levels.push_back(std::string("L2L3Residual"));
315  levels.push_back(std::string("L5Flavor" ));
316  levels.push_back(std::string("L7Parton" ));
317  iDesc.add<std::vector<std::string> >("levels", levels);
318  descriptions.add("JetCorrFactorsProducer", iDesc);
319 }
320 
322 
const LorentzVector correctedP4(const std::string &level, const std::string &flavor="none", const std::string &set="") const
Definition: Jet.h:155
T getParameter(std::string const &) const
edm::EDGetTokenT< double > rhoToken_
dictionary parameters
Definition: Parameters.py:2
tuple cfg
Definition: looper.py:293
std::string payload()
map jet algorithm to payload in DB
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
std::string label_
label of jec factors
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
void push_back(key_type i, value_type const &j, label_type const &flav="")
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
edm::EDGetTokenT< std::vector< reco::Vertex > > primaryVerticesToken_
const edm::RefToBase< reco::Jet > & getCaloJetRef() const
Definition: JPTJet.h:130
std::vector< std::string > extraJPTOffset_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
bool emf_
use electromagnetic fraction for jet energy corrections or not (will only have an effect for jets Cal...
virtual double eta() const
momentum pseudorapidity
virtual double pt() const
transverse momentum
std::string payload_
label of payload
JetCorrFactorsProducer(const edm::ParameterSet &cfg)
default constructor
virtual double energy() const
energy
bool flavorDependent() const
return true if the jec levels contain at least one flavor dependent correction level ...
tuple corrector
Definition: mvaPFMET_cff.py:86
double p4[4]
Definition: TauolaWrapper.h:92
virtual void produce(edm::Event &event, const edm::EventSetup &setup) override
everything that needs to be done per event
Jets made from CaloJets corrected for ZSP and tracks.
Definition: JPTJet.h:29
int numberOf(const edm::Handle< std::vector< reco::Vertex > > &primaryVertices)
determines the number of valid primary vertices for the standard L1Offset correction of JetMET ...
Class for the storage of jet correction factors.
vector< PseudoJet > jets
ParameterDescriptionBase * add(U const &iLabel, T const &value)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
Produces a ValueMap between JetCorrFactors and the to the originating reco jets.
edm::ValueMap< pat::JetCorrFactors > JetCorrFactorsMap
value map for JetCorrFactors (to be written into the event)
std::vector< std::string > expand(const std::vector< std::string > &levels, const JetCorrFactors::Flavor &flavor)
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
edm::EDGetTokenT< edm::View< reco::Jet > > srcToken_
input jet collection
const T & get() const
Definition: EventSetup.h:56
std::vector< JetCorrectorParameters > params(const JetCorrectorParametersCollection &parameters, const std::vector< std::string > &levels) const
return the jec parameters as input to the FactorizedJetCorrector for different flavors ...
Analysis-level calorimeter jet class.
Definition: Jet.h:77
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::string const & label() const
Definition: InputTag.h:43
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:85
std::unique_ptr< FactorizedJetCorrector > extraJPTOffsetCorrector_
cache container for JPTOffset jet corrections
std::map< JetCorrFactors::Flavor, std::unique_ptr< FactorizedJetCorrector > > correctors_
cache container for jet corrections
tuple level
Definition: testEve_cfg.py:34
edm::InputTag primaryVertices_
label for L1Offset primaryVertex collection
edm::InputTag rho_
label for L1FastJet energy density parameter rho
bool useNPV_
use the NPV and rho with the JEC? (used for L1Offset/L1FastJet and L1FastJet, resp.)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
description of configuration file parameters
std::string type_
type of flavor dependent JEC factors (only &#39;J&#39; and &#39;T&#39; are allowed)
float evaluate(edm::View< reco::Jet >::const_iterator &jet, const JetCorrFactors::Flavor &flavor, int level)
evaluate jet correction factor up to a given level
virtual double phi() const
momentum azimuthal angle
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
unsigned long long cacheId_
cache identifier for JetCorrectionsRecord