CMS 3D CMS Logo

ShiftedParticleProducer.cc
Go to the documentation of this file.
1 
24 
25 #include <TF2.h>
26 
27 #include <string>
28 #include <vector>
29 
32 
33 public:
35  ~ShiftedParticleProducer() override;
36 
37 private:
38  void produce(edm::Event& evt, const edm::EventSetup& es) override;
39 
40  double getUncShift(const reco::Candidate& originalParticle);
41 
43 
46 
49  : binSelection_(nullptr), binUncertainty_(uncertainty), energyDep_(false) {
51  std::make_unique<TF2>(std::string(moduleLabel).append("_uncFormula").c_str(), binUncertainty_.c_str());
52  }
54  : binSelection_(new StringCutObjectSelector<reco::Candidate>(cfg.getParameter<std::string>("binSelection"))),
55  binUncertainty_(cfg.getParameter<std::string>("binUncertainty")),
56  energyDep_(false) {
58  std::make_unique<TF2>(std::string(moduleLabel).append("_uncFormula").c_str(), binUncertainty_.c_str());
59  if (cfg.exists("energyDependency")) {
60  energyDep_ = cfg.getParameter<bool>("energyDependency");
61  }
62  }
64  std::unique_ptr<StringCutObjectSelector<reco::Candidate>> binSelection_;
65  //double binUncertainty_;
67  std::unique_ptr<TF2> binUncFormula_;
68  bool energyDep_;
69  };
70  std::vector<binningEntryType*> binning_;
71 
72  double shiftBy_; // set to +1.0/-1.0 for up/down variation of energy scale
73 };
74 
76  moduleLabel_ = cfg.getParameter<std::string>("@module_label");
77  srcToken_ = consumes<CandidateView>(cfg.getParameter<edm::InputTag>("src"));
78  shiftBy_ = cfg.getParameter<double>("shiftBy");
79  edm::InputTag srcWeights = cfg.getParameter<edm::InputTag>("srcWeights");
80  if (!srcWeights.label().empty())
82 
83  if (cfg.exists("binning")) {
84  typedef std::vector<edm::ParameterSet> vParameterSet;
85  vParameterSet cfgBinning = cfg.getParameter<vParameterSet>("binning");
86  for (vParameterSet::const_iterator cfgBinningEntry = cfgBinning.begin(); cfgBinningEntry != cfgBinning.end();
87  ++cfgBinningEntry) {
88  binning_.push_back(new binningEntryType(*cfgBinningEntry, moduleLabel_));
89  }
90  } else {
91  std::string uncertainty = cfg.getParameter<std::string>("uncertainty");
92  binning_.push_back(new binningEntryType(uncertainty, moduleLabel_));
93  }
94 
95  produces<reco::CandidateCollection>();
96 }
97 
99  for (std::vector<binningEntryType*>::const_iterator it = binning_.begin(); it != binning_.end(); ++it) {
100  delete (*it);
101  }
102 }
103 
105  edm::Handle<CandidateView> originalParticles;
106  evt.getByToken(srcToken_, originalParticles);
107 
111 
112  auto shiftedParticles = std::make_unique<reco::CandidateCollection>();
113 
114  for (unsigned i = 0; i < originalParticles->size(); ++i) {
115  float weight = 1.0;
117  edm::Ptr<reco::Candidate> particlePtr = originalParticles->ptrAt(i);
118  while (!weights->contains(particlePtr.id()) && (particlePtr->numberOfSourceCandidatePtrs() > 0))
119  particlePtr = particlePtr->sourceCandidatePtr(0);
120  weight = (*weights)[particlePtr];
121  }
122  const reco::Candidate& originalParticle = originalParticles->at(i);
123  reco::LeafCandidate weightedParticle(originalParticle);
124  weightedParticle.setP4(originalParticle.p4() * weight);
125  double uncertainty = getUncShift(weightedParticle);
126  double shift = shiftBy_ * uncertainty;
127 
128  reco::Candidate::LorentzVector shiftedParticleP4 = originalParticle.p4();
129  //leave 0*nan = 0
130  if ((weight > 0) && (!(edm::isNotFinite(shift) && shiftedParticleP4.mag2() == 0)))
131  shiftedParticleP4 *= (1. + shift);
132 
133  std::unique_ptr<reco::Candidate> shiftedParticle = std::make_unique<reco::LeafCandidate>(originalParticle);
134  shiftedParticle->setP4(shiftedParticleP4);
135 
136  shiftedParticles->push_back(shiftedParticle.release());
137  }
138 
139  evt.put(std::move(shiftedParticles));
140 }
141 
142 double ShiftedParticleProducer::getUncShift(const reco::Candidate& originalParticle) {
143  double valx = 0;
144  double valy = 0;
145  for (std::vector<binningEntryType*>::iterator binningEntry = binning_.begin(); binningEntry != binning_.end();
146  ++binningEntry) {
147  if ((!(*binningEntry)->binSelection_) || (*(*binningEntry)->binSelection_)(originalParticle)) {
148  if ((*binningEntry)->energyDep_)
149  valx = originalParticle.energy();
150  else
151  valx = originalParticle.pt();
152 
153  valy = originalParticle.eta();
154  return (*binningEntry)->binUncFormula_->Eval(valx, valy);
155  }
156  }
157  return 0;
158 }
159 
edm::View< reco::Candidate > CandidateView
virtual double energy() const =0
energy
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
Ptr< value_type > ptrAt(size_type i) const
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:158
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
virtual double pt() const =0
transverse momentum
edm::EDGetTokenT< CandidateView > srcToken_
constexpr bool isUninitialized() const noexcept
Definition: EDGetToken.h:104
Definition: weight.py:1
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:540
binningEntryType(std::string uncertainty, std::string moduleLabel)
size_type size() const
void produce(edm::Event &evt, const edm::EventSetup &es) override
ShiftedParticleProducer(const edm::ParameterSet &cfg)
double getUncShift(const reco::Candidate &originalParticle)
edm::EDGetTokenT< edm::ValueMap< float > > weightsToken_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
binningEntryType(const edm::ParameterSet &cfg, std::string moduleLabel)
const_reference at(size_type pos) const
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
std::unique_ptr< StringCutObjectSelector< reco::Candidate > > binSelection_
fixed size matrix
static unsigned int const shift
virtual CandidatePtr sourceCandidatePtr(size_type i) const
Definition: Candidate.h:169
virtual size_t numberOfSourceCandidatePtrs() const =0
void setP4(const LorentzVector &p4) final
set 4-momentum
def move(src, dest)
Definition: eostools.py:511
std::vector< binningEntryType * > binning_
virtual double eta() const =0
momentum pseudorapidity
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector