CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TauJetCorrFactorsProducer.cc
Go to the documentation of this file.
2 
4 
7 
9 
12 
13 using namespace pat;
14 
16  : moduleLabel_(cfg.getParameter<std::string>("@module_label")),
17  srcToken_(consumes<edm::View<reco::BaseTau> >(cfg.getParameter<edm::InputTag>("src"))),
18  levels_(cfg.getParameter<std::vector<std::string> >("levels"))
19 {
20  typedef std::vector<edm::ParameterSet> vParameterSet;
21  vParameterSet parameters = cfg.getParameter<vParameterSet>("parameters");
22  for ( vParameterSet::const_iterator param = parameters.begin();
23  param != parameters.end(); ++param ) {
24  payloadMappingType payloadMapping;
25 
26  payloadMapping.payload_ = param->getParameter<std::string>("payload");
27 
28  vstring decayModes_string = param->getParameter<vstring>("decayModes");
29  for ( vstring::const_iterator decayMode = decayModes_string.begin();
30  decayMode != decayModes_string.end(); ++decayMode ) {
31  if ( (*decayMode) == "*" ) {
32  defaultPayload_ = payloadMapping.payload_;
33  } else {
34  payloadMapping.decayModes_.push_back(atoi(decayMode->data()));
35  }
36  }
37 
38  if ( payloadMapping.decayModes_.size() > 0 ) payloadMappings_.push_back(payloadMapping);
39  }
40 
41  produces<TauJetCorrFactorsMap>();
42 }
43 
44 std::vector<JetCorrectorParameters>
45 TauJetCorrFactorsProducer::params(const JetCorrectorParametersCollection& jecParameters, const std::vector<std::string>& levels) const
46 {
47  std::vector<JetCorrectorParameters> retVal;
48  for ( std::vector<std::string>::const_iterator corrLevel = levels.begin();
49  corrLevel != levels.end(); ++corrLevel ) {
50  const JetCorrectorParameters& jecParameter_level = jecParameters[*corrLevel];
51  retVal.push_back(jecParameter_level);
52  }
53  return retVal;
54 }
55 
56 float
58  boost::shared_ptr<FactorizedJetCorrector>& corrector, int corrLevel)
59 {
60  corrector->setJetEta(tauJet->eta());
61  corrector->setJetPt(tauJet->pt());
62  corrector->setJetE(tauJet->energy());
63  return corrector->getSubCorrections()[corrLevel];
64 }
65 
66 void
68 {
69  // get tau-jet collection from the event
71  evt.getByToken(srcToken_, tauJets);
72 
73  typedef boost::shared_ptr<FactorizedJetCorrector> FactorizedJetCorrectorPtr;
74  std::map<std::string, FactorizedJetCorrectorPtr> correctorMapping;
75 
76  // fill the tauJetCorrFactors
77  std::vector<TauJetCorrFactors> tauJetCorrections;
78  for ( edm::View<reco::BaseTau>::const_iterator tauJet = tauJets->begin();
79  tauJet != tauJets->end(); ++tauJet ) {
80 
81  // the TauJetCorrFactors::CorrectionFactor is a std::pair<std::string, float>
82  // the string corresponds to the label of the correction level, the float to the tau-jet energy correction factor.
83  // The first correction level is predefined with label 'Uncorrected'. The correction factor is 1.
84  std::vector<TauJetCorrFactors::CorrectionFactor> jec;
85  jec.push_back(std::make_pair(std::string("Uncorrected"), 1.0));
86 
87  if ( levels_.size() == 0 )
88  throw cms::Exception("No JECFactors")
89  << "You request to create a jetCorrFactors object with no JEC Levels indicated. \n"
90  << "This makes no sense, either you should correct this or drop the module from \n"
91  << "the sequence.";
92 
93  std::string payload = defaultPayload_;
94  if ( dynamic_cast<const reco::PFTau*>(&(*tauJet)) ) {
95  const reco::PFTau* pfTauJet = dynamic_cast<const reco::PFTau*>(&(*tauJet));
96  for ( std::vector<payloadMappingType>::const_iterator payloadMapping = payloadMappings_.begin();
97  payloadMapping != payloadMappings_.end(); ++payloadMapping ) {
98  for( vint::const_iterator decayMode = payloadMapping->decayModes_.begin();
99  decayMode != payloadMapping->decayModes_.end(); ++decayMode ) {
100  if ( pfTauJet->decayMode() == (*decayMode) ) payload = payloadMapping->payload_;
101  }
102  }
103  }
104 
105  // retrieve JEC parameters from the DB and build a new corrector,
106  // in case it does not exist already for current payload
107  if ( correctorMapping.find(payload) == correctorMapping.end() ) {
109  es.get<JetCorrectionsRecord>().get(payload, jecParameters);
110 
111  correctorMapping[payload] = FactorizedJetCorrectorPtr(new FactorizedJetCorrector(params(*jecParameters, levels_)));
112  }
113  FactorizedJetCorrectorPtr& corrector = correctorMapping[payload];
114 
115  // evaluate tau-jet energy corrections
116  size_t numLevels = levels_.size();
117  for ( size_t idx = 0; idx < numLevels; ++idx ) {
118  const std::string& corrLevel = levels_[idx];
119 
120  float jecFactor = evaluate(tauJet, corrector, idx);
121 
122  // push back the set of JetCorrFactors: the first entry corresponds to the label
123  // of the correction level. The second parameter corresponds to the jec factor.
124  // In the default configuration the CorrectionFactor will look like this:
125  // 'Uncorrected' : 1 ;
126  // 'L2Relative' : x ;
127  // 'L3Absolute' : x ;
128  jec.push_back(std::make_pair(corrLevel.substr(0, corrLevel.find("_")), jecFactor));
129  }
130 
131  // create the actual object with the scale factors we want the valuemap to refer to
132  // moduleLabel_ corresponds to the python label of the TauJetCorrFactorsProducer module instance
133  TauJetCorrFactors tauJetCorrection(moduleLabel_, jec);
134  tauJetCorrections.push_back(tauJetCorrection);
135  }
136 
137  // build the valuemap
138  std::auto_ptr<TauJetCorrFactorsMap> jecMapping(new TauJetCorrFactorsMap());
139  TauJetCorrFactorsMap::Filler filler(*jecMapping);
140  // tauJets and tauJetCorrections vectors have their indices aligned by construction
141  filler.insert(tauJets, tauJetCorrections.begin(), tauJetCorrections.end());
142  filler.fill(); // do the actual filling
143 
144  // add valuemap to the event
145  evt.put(jecMapping);
146 }
147 
149 
TauJetCorrFactorsProducer(const edm::ParameterSet &)
default constructor
T getParameter(std::string const &) const
dictionary parameters
Definition: Parameters.py:2
float evaluate(edm::View< reco::BaseTau >::const_iterator &, boost::shared_ptr< FactorizedJetCorrector > &, int)
evaluate jet correction factor up to a given level
tuple cfg
Definition: looper.py:293
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
std::vector< JetCorrectorParameters > params(const JetCorrectorParametersCollection &, const std::vector< std::string > &) const
return the jec parameters as input to the FactorizedJetCorrector for different flavors ...
virtual void produce(edm::Event &, const edm::EventSetup &) override
everything that needs to be done per event
hadronicDecayMode decayMode() const
Definition: PFTau.cc:212
std::vector< std::string > vstring
jec levels
tuple corrector
Definition: mvaPFMET_cff.py:86
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:120
edm::EDGetTokenT< edm::View< reco::BaseTau > > srcToken_
input tau-jet collection
std::string moduleLabel_
python label of this TauJetCorrFactorsProducer module
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
const T & get() const
Definition: EventSetup.h:56
edm::ValueMap< pat::TauJetCorrFactors > TauJetCorrFactorsMap
value map for JetCorrFactors (to be written into the event)
std::vector< payloadMappingType > payloadMappings_
Produces a ValueMap between TauJetCorrFactors and the originating reco taus.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:85
moduleLabel_(iConfig.getParameter< string >("@module_label"))