CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Type2CorrectionProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 //____________________________________________________________________________||
10 
12 
14 
15 #include <TFormula.h>
16 
17 #include <iostream>
18 #include <vector>
19 
20 //____________________________________________________________________________||
22 {
23 public:
26 
27 private:
28 
29  typedef std::vector<edm::InputTag> vInputTag;
30 
31  typedef std::vector<std::string> vstring;
33  {
34  type2BinningEntryType(const std::string& binCorrformula, const edm::ParameterSet& binCorrParameter, const vInputTag& srcUnclEnergySums, edm::ConsumesCollector && iConsumesCollector)
35  : binLabel_(""),
37  {
38  for (vInputTag::const_iterator inputTag = srcUnclEnergySums.begin(); inputTag != srcUnclEnergySums.end(); ++inputTag)
39  {
40  corrTokens_.push_back(iConsumesCollector.consumes<CorrMETData>(*inputTag));
41  }
42 
43  initialize(binCorrformula, binCorrParameter);
44  }
45  type2BinningEntryType(const edm::ParameterSet& cfg, const vInputTag& srcUnclEnergySums, edm::ConsumesCollector && iConsumesCollector)
46  : binLabel_(cfg.getParameter<std::string>("binLabel")),
48  {
49 
50  for ( vInputTag::const_iterator srcUnclEnergySum = srcUnclEnergySums.begin();
51  srcUnclEnergySum != srcUnclEnergySums.end(); ++srcUnclEnergySum )
52  {
53  std::string instanceLabel = srcUnclEnergySum->instance();
54  if ( instanceLabel != "" && binLabel_ != "" ) instanceLabel.append("#");
55  instanceLabel.append(binLabel_);
56  edm::InputTag inputTag(srcUnclEnergySum->label(), instanceLabel);
57  corrTokens_.push_back(iConsumesCollector.consumes<CorrMETData>(inputTag));
58  }
59 
60  std::string binCorrFormula = cfg.getParameter<std::string>("binCorrFormula").data();
61 
62  edm::ParameterSet binCorrParameter = cfg.getParameter<edm::ParameterSet>("binCorrParameter");
63 
64  initialize(binCorrFormula, binCorrParameter);
65  }
66  void initialize(const std::string& binCorrFormula, const edm::ParameterSet& binCorrParameter)
67  {
68  TString formula = binCorrFormula;
69 
70  vstring parNames = binCorrParameter.getParameterNamesForType<double>();
71  int numParameter = parNames.size();
72  binCorrParameter_.resize(numParameter);
73  for ( int parIndex = 0; parIndex < numParameter; ++parIndex ) {
74  const std::string& parName = parNames[parIndex].data();
75 
76  double parValue = binCorrParameter.getParameter<double>(parName);
77  binCorrParameter_[parIndex] = parValue;
78 
79  TString parName_internal = Form("[%i]", parIndex);
80  formula = formula.ReplaceAll(parName.data(), parName_internal);
81  }
82 
83  std::string binCorrFormulaName = std::string("binCorrFormula").append("_").append(binLabel_);
84  binCorrFormula_ = new TFormula(binCorrFormulaName.data(), formula);
85 
86 //--- check that syntax of formula string is valid
87 // (i.e. that TFormula "compiled" without errors)
88  if ( !(binCorrFormula_->GetNdim() <= 1 && binCorrFormula_->GetNpar() == numParameter) )
89  throw cms::Exception("METCorrectionAlgorithm2")
90  << "Formula for Type 2 correction has invalid syntax = " << formula << " !!\n";
91 
92  for ( int parIndex = 0; parIndex < numParameter; ++parIndex ) {
93  binCorrFormula_->SetParameter(parIndex, binCorrParameter_[parIndex]);
94  binCorrFormula_->SetParName(parIndex, parNames[parIndex].data());
95  }
96  }
98  {
99  delete binCorrFormula_;
100  }
102  std::vector<edm::EDGetTokenT<CorrMETData> > corrTokens_;
103  TFormula* binCorrFormula_;
104  std::vector<double> binCorrParameter_;
105  };
106 
107  std::vector<type2BinningEntryType*> type2Binning_;
108 
109  void produce(edm::Event&, const edm::EventSetup&) override;
110 
111 };
112 
113 //____________________________________________________________________________||
115 {
116  vInputTag srcUnclEnergySums = cfg.getParameter<vInputTag>("srcUnclEnergySums");
117 
118  if ( cfg.exists("type2Binning") )
119  {
120  typedef std::vector<edm::ParameterSet> vParameterSet;
121  vParameterSet cfgType2Binning = cfg.getParameter<vParameterSet>("type2Binning");
122  for ( vParameterSet::const_iterator cfgType2BinningEntry = cfgType2Binning.begin();
123  cfgType2BinningEntry != cfgType2Binning.end(); ++cfgType2BinningEntry ) {
124  type2Binning_.push_back(new type2BinningEntryType(*cfgType2BinningEntry, srcUnclEnergySums, consumesCollector()));
125  }
126  }
127  else
128  {
129  std::string type2CorrFormula = cfg.getParameter<std::string>("type2CorrFormula").data();
130  edm::ParameterSet type2CorrParameter = cfg.getParameter<edm::ParameterSet>("type2CorrParameter");
131  type2Binning_.push_back(new type2BinningEntryType(type2CorrFormula, type2CorrParameter, srcUnclEnergySums, consumesCollector()));
132  }
133  produces<CorrMETData>("");
134 }
135 
136 //____________________________________________________________________________||
138 {
139  CorrMETData product;
140 
141  for ( std::vector<type2BinningEntryType*>::const_iterator type2BinningEntry = type2Binning_.begin();
142  type2BinningEntry != type2Binning_.end(); ++type2BinningEntry )
143  {
144  CorrMETData unclEnergySum;
145 
146  edm::Handle<CorrMETData> unclEnergySummand;
147  for (std::vector<edm::EDGetTokenT<CorrMETData> >::const_iterator corrToken = (*type2BinningEntry)->corrTokens_.begin(); corrToken != (*type2BinningEntry)->corrTokens_.end(); ++corrToken)
148  {
149  evt.getByToken(*corrToken, unclEnergySummand);
150  unclEnergySum += (*unclEnergySummand);
151  }
152 
153  double unclEnergySumPt = sqrt(unclEnergySum.mex*unclEnergySum.mex + unclEnergySum.mey*unclEnergySum.mey);
154  double unclEnergyScaleFactor = (*type2BinningEntry)->binCorrFormula_->Eval(unclEnergySumPt);
155 
156  unclEnergySum.mex = -unclEnergySum.mex;
157  unclEnergySum.mey = -unclEnergySum.mey;
158 
159  product += (unclEnergyScaleFactor - 1.)*unclEnergySum;
160  }
161 
162  std::auto_ptr<CorrMETData> pprod(new CorrMETData(product));
163  evt.put(pprod, "");
164 }
165 
166 //____________________________________________________________________________||
167 
169 
T getParameter(std::string const &) const
std::vector< edm::InputTag > vInputTag
tuple cfg
Definition: looper.py:293
type2BinningEntryType(const std::string &binCorrformula, const edm::ParameterSet &binCorrParameter, const vInputTag &srcUnclEnergySums, edm::ConsumesCollector &&iConsumesCollector)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< type2BinningEntryType * > type2Binning_
Type2CorrectionProducer(const edm::ParameterSet &)
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:194
void produce(edm::Event &, const edm::EventSetup &) override
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:120
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
T sqrt(T t)
Definition: SSEVec.h:48
std::vector< std::string > vstring
std::vector< edm::EDGetTokenT< CorrMETData > > corrTokens_
void initialize(const std::string &binCorrFormula, const edm::ParameterSet &binCorrParameter)
a MET correction term
Definition: CorrMETData.h:14
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
type2BinningEntryType(const edm::ParameterSet &cfg, const vInputTag &srcUnclEnergySums, edm::ConsumesCollector &&iConsumesCollector)
double mey
Definition: CorrMETData.h:18
double mex
Definition: CorrMETData.h:17