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
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
edm::EDGetTokenT< ParticleCollection > srcToken_
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
std::vector< binningEntryType * > binning_
ShiftedParticleProducerT(const edm::ParameterSet &cfg)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:540
void produce(edm::Event &evt, const edm::EventSetup &es) override
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
static unsigned int const shift
long double T
def move(src, dest)
Definition: eostools.py:511