CMS 3D CMS Logo

ShiftedParticleProducerT.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_PatUtils_ShiftedParticleProducerT_h
2 #define PhysicsTools_PatUtils_ShiftedParticleProducerT_h
3 
23 
26 
27 #include <string>
28 #include <vector>
29 
30 template <typename T>
32  typedef std::vector<T> ParticleCollection;
33 
34 public:
36  : moduleLabel_(cfg.getParameter<std::string>("@module_label")) {
37  srcToken_ = consumes<ParticleCollection>(cfg.getParameter<edm::InputTag>("src"));
38 
39  shiftBy_ = cfg.getParameter<double>("shiftBy");
40 
41  if (cfg.exists("binning")) {
42  typedef std::vector<edm::ParameterSet> vParameterSet;
43  vParameterSet cfgBinning = cfg.getParameter<vParameterSet>("binning");
44  for (vParameterSet::const_iterator cfgBinningEntry = cfgBinning.begin(); cfgBinningEntry != cfgBinning.end();
45  ++cfgBinningEntry) {
46  binning_.push_back(new binningEntryType(*cfgBinningEntry));
47  }
48  } else {
49  double uncertainty = cfg.getParameter<double>("uncertainty");
50  binning_.push_back(new binningEntryType(uncertainty));
51  }
52 
53  produces<ParticleCollection>();
54  }
56  for (typename std::vector<binningEntryType*>::const_iterator it = binning_.begin(); it != binning_.end(); ++it) {
57  delete (*it);
58  }
59  }
60 
61 private:
62  void produce(edm::Event& evt, const edm::EventSetup& es) override {
63  edm::Handle<ParticleCollection> originalParticles;
64  evt.getByToken(srcToken_, originalParticles);
65 
66  auto shiftedParticles = std::make_unique<ParticleCollection>();
67 
68  for (typename ParticleCollection::const_iterator originalParticle = originalParticles->begin();
69  originalParticle != originalParticles->end();
70  ++originalParticle) {
71  double uncertainty = 0.;
72  for (typename std::vector<binningEntryType*>::iterator binningEntry = binning_.begin();
73  binningEntry != binning_.end();
74  ++binningEntry) {
75  if ((!(*binningEntry)->binSelection_) || (*(*binningEntry)->binSelection_)(*originalParticle)) {
76  uncertainty = (*binningEntry)->binUncertainty_;
77  break;
78  }
79  }
80 
81  double shift = shiftBy_ * uncertainty;
82 
83  reco::Candidate::LorentzVector shiftedParticleP4 = originalParticle->p4();
84  //leave 0*nan = 0
85  if (!(edm::isNotFinite(shift) && shiftedParticleP4.mag2() == 0))
86  shiftedParticleP4 *= (1. + shift);
87 
88  T shiftedParticle(*originalParticle);
89  shiftedParticle.setP4(shiftedParticleP4);
90 
91  shiftedParticles->push_back(shiftedParticle);
92  }
93 
94  evt.put(std::move(shiftedParticles));
95  }
96 
98 
100 
102  binningEntryType(double uncertainty) : binSelection_(nullptr), binUncertainty_(uncertainty) {}
104  : binSelection_(new StringCutObjectSelector<T>(cfg.getParameter<std::string>("binSelection"))),
105  binUncertainty_(cfg.getParameter<double>("binUncertainty")) {}
109  };
110  std::vector<binningEntryType*> binning_;
111 
112  double shiftBy_; // set to +1.0/-1.0 for up/down variation of energy scale
113 };
114 
115 #endif
edm::isNotFinite
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
ShiftedParticleProducerT::ParticleCollection
std::vector< T > ParticleCollection
Definition: ShiftedParticleProducerT.h:32
edm::EDGetTokenT< ParticleCollection >
ShiftedParticleProducerT::binningEntryType::binUncertainty_
double binUncertainty_
Definition: ShiftedParticleProducerT.h:108
EDProducer.h
ShiftedParticleProducerT
Definition: ShiftedParticleProducerT.h:31
edm::Handle
Definition: AssociativeIterator.h:50
ShiftedParticleProducerT::ShiftedParticleProducerT
ShiftedParticleProducerT(const edm::ParameterSet &cfg)
Definition: ShiftedParticleProducerT.h:35
ShiftedParticleProducerT::binningEntryType::binningEntryType
binningEntryType(double uncertainty)
Definition: ShiftedParticleProducerT.h:102
ShiftedParticleProducerT::binning_
std::vector< binningEntryType * > binning_
Definition: ShiftedParticleProducerT.h:110
ShiftedParticleProducerT::produce
void produce(edm::Event &evt, const edm::EventSetup &es) override
Definition: ShiftedParticleProducerT.h:62
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:531
ShiftedParticleProducerT::binningEntryType::binSelection_
StringCutObjectSelector< T > * binSelection_
Definition: ShiftedParticleProducerT.h:107
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
ShiftedParticleProducerT::binningEntryType::~binningEntryType
~binningEntryType()
Definition: ShiftedParticleProducerT.h:106
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:38
edm::EventSetup
Definition: EventSetup.h:57
ShiftedParticleProducerT::binningEntryType
Definition: ShiftedParticleProducerT.h:101
InputTag.h
looper.cfg
cfg
Definition: looper.py:297
ShiftedParticleProducerT::shiftBy_
double shiftBy_
Definition: ShiftedParticleProducerT.h:112
ShiftedParticleProducerT::~ShiftedParticleProducerT
~ShiftedParticleProducerT() override
Definition: ShiftedParticleProducerT.h:55
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
StringCutObjectSelector.h
edm::shift
static unsigned const int shift
Definition: LuminosityBlockID.cc:7
isFinite.h
T
long double T
Definition: Basic3DVectorLD.h:48
StringCutObjectSelector
Definition: StringCutObjectSelector.h:16
EventSetup.h
Candidate.h
ParameterSet.h
reco::Candidate::LorentzVector
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
ShiftedParticleProducerT::moduleLabel_
std::string moduleLabel_
Definition: ShiftedParticleProducerT.h:97
edm::Event
Definition: Event.h:73
ShiftedParticleProducerT::binningEntryType::binningEntryType
binningEntryType(const edm::ParameterSet &cfg)
Definition: ShiftedParticleProducerT.h:103
edm::InputTag
Definition: InputTag.h:15
ShiftedParticleProducerT::srcToken_
edm::EDGetTokenT< ParticleCollection > srcToken_
Definition: ShiftedParticleProducerT.h:99