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 
reco::Candidate::energy
virtual double energy() const =0
energy
ShiftedParticleProducer::binningEntryType::binningEntryType
binningEntryType(std::string uncertainty, std::string moduleLabel)
Definition: ShiftedParticleProducer.cc:48
ShiftedParticleProducer::binning_
std::vector< binningEntryType * > binning_
Definition: ShiftedParticleProducer.cc:70
mps_fire.i
i
Definition: mps_fire.py:428
funct::false
false
Definition: Factorize.h:29
configurableAnalysis::Candidate
char Candidate[]
Definition: modules.cc:20
edm::isNotFinite
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
reco::Candidate::eta
virtual double eta() const =0
momentum pseudorapidity
edm::EDGetTokenT< CandidateView >
mps_merge.weight
weight
Definition: mps_merge.py:88
reco::Candidate::pt
virtual double pt() const =0
transverse momentum
edm::EDGetTokenT::isUninitialized
constexpr bool isUninitialized() const noexcept
Definition: EDGetToken.h:99
EDProducer.h
ShiftedParticleProducer::~ShiftedParticleProducer
~ShiftedParticleProducer() override
Definition: ShiftedParticleProducer.cc:98
ShiftedParticleProducer::ShiftedParticleProducer
ShiftedParticleProducer(const edm::ParameterSet &cfg)
Definition: ShiftedParticleProducer.cc:75
ShiftedParticleProducer::binningEntryType::energyDep_
bool energyDep_
Definition: ShiftedParticleProducer.cc:68
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:46
edm::Handle
Definition: AssociativeIterator.h:50
ShiftedParticleProducer::binningEntryType
Definition: ShiftedParticleProducer.cc:47
ShiftedParticleProducer::binningEntryType::binUncFormula_
std::unique_ptr< TF2 > binUncFormula_
Definition: ShiftedParticleProducer.cc:67
ShiftedParticleProducer::srcToken_
edm::EDGetTokenT< CandidateView > srcToken_
Definition: ShiftedParticleProducer.cc:44
MakerMacros.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
HLT_FULL_cff.weights
weights
Definition: HLT_FULL_cff.py:99207
ShiftedParticleProducer::binningEntryType::binUncertainty_
std::string binUncertainty_
Definition: ShiftedParticleProducer.cc:66
LeafCandidate.h
ShiftedParticleProducer::getUncShift
double getUncShift(const reco::Candidate &originalParticle)
Definition: ShiftedParticleProducer.cc:142
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
ShiftedParticleProducer::moduleLabel_
std::string moduleLabel_
Definition: ShiftedParticleProducer.cc:42
edm::View::size
size_type size() const
edm::View
Definition: CaloClusterFwd.h:14
edm::View::at
const_reference at(size_type pos) const
edm::ParameterSet
Definition: ParameterSet.h:47
edm::Ptr::id
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:158
Event.h
ShiftedParticleProducer::produce
void produce(edm::Event &evt, const edm::EventSetup &es) override
Definition: ShiftedParticleProducer.cc:104
mps_setup.append
append
Definition: mps_setup.py:85
ShiftedParticleProducer::weightsToken_
edm::EDGetTokenT< edm::ValueMap< float > > weightsToken_
Definition: ShiftedParticleProducer.cc:45
edm::Event::put
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
edm::stream::EDProducer
Definition: EDProducer.h:36
edm::EventSetup
Definition: EventSetup.h:58
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
HLT_FULL_cff.srcWeights
srcWeights
Definition: HLT_FULL_cff.py:8709
ShiftedParticleProducer::binningEntryType::binningEntryType
binningEntryType(const edm::ParameterSet &cfg, std::string moduleLabel)
Definition: ShiftedParticleProducer.cc:53
InputTag.h
edm::Ptr< reco::Candidate >
looper.cfg
cfg
Definition: looper.py:296
reco::Candidate
Definition: Candidate.h:27
ValueMap.h
reco::LeafCandidate::setP4
void setP4(const LorentzVector &p4) final
set 4-momentum
Definition: LeafCandidate.h:158
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
reco::Candidate::sourceCandidatePtr
virtual CandidatePtr sourceCandidatePtr(size_type i) const
Definition: Candidate.h:169
StringCutObjectSelector.h
edm::shift
static unsigned const int shift
Definition: LuminosityBlockID.cc:7
isFinite.h
ShiftedParticleProducer::shiftBy_
double shiftBy_
Definition: ShiftedParticleProducer.cc:72
edm::ValueMap< float >
StringCutObjectSelector
Definition: StringCutObjectSelector.h:16
ShiftedParticleProducer
Definition: ShiftedParticleProducer.cc:30
EventSetup.h
reco::Candidate::p4
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector
reco::Candidate::numberOfSourceCandidatePtrs
virtual size_t numberOfSourceCandidatePtrs() const =0
reco::LeafCandidate
Definition: LeafCandidate.h:16
Candidate.h
ShiftedParticleProducer::binningEntryType::binSelection_
std::unique_ptr< StringCutObjectSelector< reco::Candidate > > binSelection_
Definition: ShiftedParticleProducer.cc:64
ParameterSet.h
HerwigMaxPtPartonFilter_cfi.moduleLabel
moduleLabel
Definition: HerwigMaxPtPartonFilter_cfi.py:4
reco::Candidate::LorentzVector
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
edm::Event
Definition: Event.h:73
edm::View::ptrAt
Ptr< value_type > ptrAt(size_type i) const
ShiftedParticleProducer::binningEntryType::~binningEntryType
~binningEntryType()
Definition: ShiftedParticleProducer.cc:63
edm::InputTag
Definition: InputTag.h:15
ShiftedParticleProducer::CandidateView
edm::View< reco::Candidate > CandidateView
Definition: ShiftedParticleProducer.cc:31
weight
Definition: weight.py:1