CMS 3D CMS Logo

PATMETProducer.cc
Go to the documentation of this file.
1 
30 
31 #include <memory>
32 
33 namespace pat {
34 
36  public:
37  explicit PATMETProducer(const edm::ParameterSet& iConfig);
38  ~PATMETProducer() override;
39 
40  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
41 
42  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
43 
44  private:
45  // configurables
48  bool addGenMET_;
54  // tools
56 
59 
62 
63  //MET Significance
68  std::vector<edm::EDGetTokenT<edm::View<reco::Candidate>>> lepTokens_;
74 
76  const edm::EventSetup& iSetup,
77  const reco::MET& met,
78  double& sumPtUnclustered) const;
79  };
80 
81 } // namespace pat
82 
83 using namespace pat;
84 
85 PATMETProducer::PATMETProducer(const edm::ParameterSet& iConfig) : useUserData_(iConfig.exists("userData")) {
86  // initialize the configurables
87  metSrc_ = iConfig.getParameter<edm::InputTag>("metSource");
88  metToken_ = consumes<edm::View<reco::MET>>(metSrc_);
89  addGenMET_ = iConfig.getParameter<bool>("addGenMET");
90  genMETToken_ = mayConsume<edm::View<reco::GenMET>>(iConfig.getParameter<edm::InputTag>("genMETSource"));
91  addResolutions_ = iConfig.getParameter<bool>("addResolutions");
92 
93  // Efficiency configurables
94  addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
95  if (addEfficiencies_) {
97  pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"), consumesCollector());
98  }
99 
100  // Resolution configurables
101  addResolutions_ = iConfig.getParameter<bool>("addResolutions");
102  if (addResolutions_) {
104  pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"), consumesCollector());
105  }
106 
107  // Check to see if the user wants to add user data
108  if (useUserData_) {
109  userDataHelper_ = PATUserDataHelper<MET>(iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector());
110  }
111 
112  // MET Significance
113  calculateMETSignificance_ = iConfig.getParameter<bool>("computeMETSignificance");
115  edm::InputTag srcWeights = iConfig.getParameter<edm::InputTag>("srcWeights");
116  if (!srcWeights.label().empty())
118  metSigAlgo_ = new metsig::METSignificance(iConfig);
119  rhoToken_ = consumes<double>(iConfig.getParameter<edm::InputTag>("srcRho"));
120  jetToken_ = consumes<edm::View<reco::Jet>>(iConfig.getParameter<edm::InputTag>("srcJets"));
121  pfCandToken_ = consumes<edm::View<reco::Candidate>>(iConfig.getParameter<edm::InputTag>("srcPFCands"));
122  std::vector<edm::InputTag> srcLeptonsTags = iConfig.getParameter<std::vector<edm::InputTag>>("srcLeptons");
123  for (std::vector<edm::InputTag>::const_iterator it = srcLeptonsTags.begin(); it != srcLeptonsTags.end(); it++) {
124  lepTokens_.push_back(consumes<edm::View<reco::Candidate>>(*it));
125  }
126  auto jetSFType = iConfig.getParameter<std::string>("srcJetSF");
127  auto jetResPtType = iConfig.getParameter<std::string>("srcJetResPt");
128  auto jetResPhiType = iConfig.getParameter<std::string>("srcJetResPhi");
129  jetResPtToken_ = esConsumes(edm::ESInputTag("", jetResPtType));
130  jetResPhiToken_ = esConsumes(edm::ESInputTag("", jetResPhiType));
131  jetSFToken_ = esConsumes(edm::ESInputTag("", jetSFType));
132  }
133 
134  // produces vector of mets
135  produces<std::vector<MET>>();
136 }
137 
139 
141  // Get the vector of MET's from the event
143  iEvent.getByToken(metToken_, mets);
144 
145  if (mets->size() != 1)
146  throw cms::Exception("Corrupt Data") << "The input MET collection " << metSrc_.encode() << " has size "
147  << mets->size() << " instead of 1 as it should.\n";
152 
153  // Get the vector of generated met from the event if needed
155  if (addGenMET_) {
156  iEvent.getByToken(genMETToken_, genMETs);
157  }
158 
159  // loop over mets
160  std::vector<MET>* patMETs = new std::vector<MET>();
161  for (edm::View<reco::MET>::const_iterator itMET = mets->begin(); itMET != mets->end(); itMET++) {
162  // construct the MET from the ref -> save ref to original object
163  unsigned int idx = itMET - mets->begin();
164  edm::RefToBase<reco::MET> metsRef = mets->refAt(idx);
165  edm::Ptr<reco::MET> metsPtr = mets->ptrAt(idx);
166  MET amet(metsRef);
167  // add the generated MET
168  if (addGenMET_)
169  amet.setGenMET((*genMETs)[idx]);
170 
171  //add the MET significance
173  double sumPtUnclustered = 0;
174  const reco::METCovMatrix& sigcov = getMETCovMatrix(iEvent, iSetup, amet, sumPtUnclustered);
175  amet.setSignificanceMatrix(sigcov);
176  double metSig = metSigAlgo_->getSignificance(sigcov, amet);
177  amet.setMETSignificance(metSig);
179  }
180 
181  if (efficiencyLoader_.enabled()) {
182  efficiencyLoader_.setEfficiencies(amet, metsRef);
183  }
184 
185  if (resolutionLoader_.enabled()) {
187  }
188 
189  if (useUserData_) {
190  userDataHelper_.add(amet, iEvent, iSetup);
191  }
192 
193  // correct for muons if demanded... never more: it's now done by JetMETCorrections
194  // add the MET to the vector of METs
195  patMETs->push_back(amet);
196  }
197 
198  // sort MET in ET .. don't mess with this
199  // std::sort(patMETs->begin(), patMETs->end(), eTComparator_);
200 
201  // put genEvt object in Event
202  std::unique_ptr<std::vector<MET>> myMETs(patMETs);
203  iEvent.put(std::move(myMETs));
204 }
205 
206 // ParameterSet description for module
209  iDesc.setComment("PAT MET producer module");
210 
211  // input source
212  iDesc.add<edm::InputTag>("metSource", edm::InputTag("no default"))->setComment("input collection");
213 
214  // MC configurations
215  iDesc.add<bool>("addGenMET", false);
216  iDesc.add<edm::InputTag>("genMETSource", edm::InputTag("genMetCalo"));
217 
219 
220  // Efficiency configurables
221  edm::ParameterSetDescription efficienciesPSet;
222  efficienciesPSet.setAllowAnything(); // TODO: the pat helper needs to implement a description.
223  iDesc.add("efficiencies", efficienciesPSet);
224  iDesc.add<bool>("addEfficiencies", false);
225 
226  // Check to see if the user wants to add user data
227  edm::ParameterSetDescription userDataPSet;
229  iDesc.addOptional("userData", userDataPSet);
230 
231  // muon correction
232  iDesc.add<bool>("addMuonCorrections", false);
233  iDesc.add<edm::InputTag>("muonSource", edm::InputTag("muons"));
234 }
235 
237  const edm::EventSetup& iSetup,
238  const reco::MET& met,
239  double& sumPtUnclustered) const {
240  std::vector<edm::Handle<reco::CandidateView>> leptons;
241  for (std::vector<edm::EDGetTokenT<edm::View<reco::Candidate>>>::const_iterator srcLeptons_i = lepTokens_.begin();
242  srcLeptons_i != lepTokens_.end();
243  ++srcLeptons_i) {
245  event.getByToken(*srcLeptons_i, leptons_i);
246  leptons.push_back(leptons_i);
247  }
248  // jets
250  event.getByToken(jetToken_, inputJets);
251 
252  //candidates
254  event.getByToken(pfCandToken_, inputCands);
255 
257  event.getByToken(rhoToken_, rho);
258 
261  event.getByToken(weightsToken_, weights);
262 
263  JME::JetResolution resPtObj = iSetup.getData(jetResPtToken_);
264  JME::JetResolution resPhiObj = iSetup.getData(jetResPhiToken_);
266 
267  //Compute the covariance matrix and fill it
268  const edm::ValueMap<float>* weightsPtr = nullptr;
269  if (met.isWeighted()) {
271  throw cms::Exception("InvalidInput") << "MET is weighted (e.g. PUPPI), but no weights given in PATMETProducer\n";
272  weightsPtr = &*weights;
273  }
275  leptons,
276  inputCands,
277  *rho,
278  resPtObj,
279  resPhiObj,
280  resSFObj,
281  event.isRealData(),
283  weightsPtr);
284 
285  return cov;
286 }
287 
Analysis-level MET class.
Definition: MET.h:40
bool enabled() const
&#39;true&#39; if this there is at least one efficiency configured
void newEvent(const edm::Event &event)
To be called for each new event, reads in the ValueMaps for efficiencies.
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
reco::METCovMatrix getCovariance(const edm::View< reco::Jet > &jets, const std::vector< edm::Handle< reco::CandidateView > > &leptons, const edm::Handle< edm::View< reco::Candidate > > &pfCandidates, double rho, JME::JetResolution &resPtObj, JME::JetResolution &resPhiObj, JME::JetResolutionScaleFactor &resSFObj, bool isRealData, double &sumPtUnclustered, edm::ValueMap< float > const *weights=nullptr)
metsig::METSignificance * metSigAlgo_
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
Produces the pat::MET.
static double getSignificance(const reco::METCovMatrix &cov, const reco::MET &met)
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
void setEfficiencies(pat::PATObject< T > &obj, const R &originalRef) const
Sets the efficiencies for this object, using the reference to the original objects.
void setAllowAnything()
allow any parameter label/value pairs
std::string encode() const
Definition: InputTag.cc:159
pat::PATUserDataHelper< pat::MET > userDataHelper_
ROOT::Math::SMatrix< double, 2 > METCovMatrix
Definition: MET.h:39
void setSignificanceMatrix(const reco::METCovMatrix &matrix)
Definition: MET.cc:142
const reco::METCovMatrix getMETCovMatrix(const edm::Event &event, const edm::EventSetup &iSetup, const reco::MET &met, double &sumPtUnclustered) const
std::vector< edm::EDGetTokenT< edm::View< reco::Candidate > > > lepTokens_
edm::ESGetToken< JME::JetResolutionObject, JetResolutionRcd > jetResPtToken_
constexpr bool isUninitialized() const noexcept
Definition: EDGetToken.h:104
edm::ESGetToken< JME::JetResolutionObject, JetResolutionRcd > jetResPhiToken_
void setResolutions(pat::PATObject< T > &obj) const
Sets the efficiencies for this object, using the reference to the original objects.
edm::InputTag metSrc_
edm::EDGetTokenT< edm::View< reco::Candidate > > pfCandToken_
Definition: HeavyIon.h:7
static void fillDescription(edm::ParameterSetDescription &iDesc)
void setComment(std::string const &value)
edm::InputTag muonSrc_
GreaterByEt< MET > eTComparator_
int iEvent
Definition: GenABIO.cc:224
edm::EDGetTokenT< double > rhoToken_
edm::EDGetTokenT< edm::View< reco::Jet > > jetToken_
edm::EDGetTokenT< edm::ValueMap< float > > weightsToken_
Definition: MET.h:41
void newEvent(const edm::Event &event, const edm::EventSetup &setup)
To be called for each new event, reads in the EventSetup object.
bool enabled() const
&#39;true&#39; if this there is at least one efficiency configured
edm::EDGetTokenT< edm::View< reco::MET > > metToken_
pat::helper::EfficiencyLoader efficiencyLoader_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void add(ObjectType &patObject, edm::Event const &iEvent, edm::EventSetup const &iSetup)
edm::ESGetToken< JME::JetResolutionObject, JetResolutionScaleFactorRcd > jetSFToken_
void setMETSumPtUnclustered(const double &sumPtUnclustered)
Definition: MET.cc:128
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
pat::helper::KinResolutionsLoader resolutionLoader_
edm::EDGetTokenT< edm::View< reco::GenMET > > genMETToken_
METSignificance
____________________________________________________________________________||
static void fillDescription(edm::ParameterSetDescription &iDesc)
Method for documentation and validation of PSet.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
sumPtUnclustered
Definition: met_cff.py:16
void setGenMET(const reco::GenMET &gm)
set the associated GenMET
Definition: MET.cc:118
PATMETProducer(const edm::ParameterSet &iConfig)
void setMETSignificance(const double &metSig)
Definition: MET.cc:124
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1
~PATMETProducer() override