CMS 3D CMS Logo

CandIsolatorFromDeposits.cc
Go to the documentation of this file.
19 
20 #include <string>
21 #include <regex>
22 
24 public:
26 
29 
30  ~CandIsolatorFromDeposits() override;
31 
32  void produce(edm::Event &, const edm::EventSetup &) override;
33 
34 private:
35  class SingleDeposit {
36  public:
38  void cleanup();
39  void open(const edm::Event &iEvent, const edm::EventSetup &iSetup);
40  double compute(const reco::CandidateBaseRef &cand);
41  const reco::IsoDepositMap &map() { return *hDeps_; }
42 
43  private:
46  double deltaR_;
48  double weight_;
51  reco::isodeposit::EventDependentAbsVetos evdepVetos_; // note: these are a subset of the above. Don't delete twice!
54  };
55  // datamembers
56  std::vector<SingleDeposit> sources_;
57 };
58 
59 using namespace edm;
60 using namespace reco;
61 using namespace reco::isodeposit;
62 
63 bool isNumber(const std::string &str) {
64  static const std::regex re("^[+-]?(\\d+\\.?|\\d*\\.\\d*)$");
65  return regex_match(str.c_str(), re);
66 }
67 double toNumber(const std::string &str) { return atof(str.c_str()); }
68 
70  : srcToken_(iC.consumes<reco::IsoDepositMap>(iConfig.getParameter<edm::InputTag>("src"))),
71  deltaR_(iConfig.getParameter<double>("deltaR")),
72  weightExpr_(iConfig.getParameter<std::string>("weight")),
73  skipDefaultVeto_(iConfig.getParameter<bool>("skipDefaultVeto"))
74 //,vetos_(new AbsVetos())
75 {
76  std::string mode = iConfig.getParameter<std::string>("mode");
77  if (mode == "sum")
78  mode_ = Sum;
79  else if (mode == "sumRelative")
81  else if (mode == "sum2")
82  mode_ = Sum2;
83  else if (mode == "sum2Relative")
85  else if (mode == "max")
86  mode_ = Max;
87  else if (mode == "maxRelative")
89  else if (mode == "nearestDR")
90  mode_ = NearestDR;
91  else if (mode == "count")
92  mode_ = Count;
93  else if (mode == "meanDR")
94  mode_ = MeanDR;
95  else if (mode == "sumDR")
96  mode_ = SumDR;
97  else
98  throw cms::Exception("Not Implemented") << "Mode '" << mode << "' not implemented. "
99  << "Supported modes are 'sum', 'sumRelative', 'count'." <<
100  //"Supported modes are 'sum', 'sumRelative', 'max', 'maxRelative', 'count'." << // TODO: on request only
101  "New methods can be easily implemented if requested.";
102  typedef std::vector<std::string> vstring;
103  vstring vetos = iConfig.getParameter<vstring>("vetos");
105  for (vstring::const_iterator it = vetos.begin(), ed = vetos.end(); it != ed; ++it) {
106  vetos_.push_back(IsoDepositVetoFactory::make(it->c_str(), evdep, iC));
107  if (evdep)
108  evdepVetos_.push_back(evdep);
109  }
110  std::string weight = iConfig.getParameter<std::string>("weight");
111  if (isNumber(weight)) {
112  //std::cout << "Weight is a simple number, " << toNumber(weight) << std::endl;
114  usesFunction_ = false;
115  } else {
116  usesFunction_ = true;
117  //std::cout << "Weight is a function, this might slow you down... " << std::endl;
118  }
119  //std::cout << "CandIsolatorFromDeposits::SingleDeposit::SingleDeposit: Total of " << vetos_.size() << " vetos" << std::endl;
120 }
122  for (AbsVetos::iterator it = vetos_.begin(), ed = vetos_.end(); it != ed; ++it) {
123  delete *it;
124  }
125  vetos_.clear();
126  // NOTE: we DON'T have to delete the evdepVetos_, they have already been deleted above. We just clear the vectors
127  evdepVetos_.clear();
128 }
130  iEvent.getByToken(srcToken_, hDeps_);
131  for (EventDependentAbsVetos::iterator it = evdepVetos_.begin(), ed = evdepVetos_.end(); it != ed; ++it) {
132  (*it)->setEvent(iEvent, iSetup);
133  }
134 }
135 
137  const IsoDeposit &dep = (*hDeps_)[cand];
138  double eta = dep.eta(), phi = dep.phi(); // better to center on the deposit direction
139  // that could be, e.g., the impact point at calo
140  for (AbsVetos::iterator it = vetos_.begin(), ed = vetos_.end(); it != ed; ++it) {
141  (*it)->centerOn(eta, phi);
142  }
143  double weight = (usesFunction_ ? weightExpr_(*cand) : weight_);
144  switch (mode_) {
145  case Count:
146  return weight * dep.countWithin(deltaR_, vetos_, skipDefaultVeto_);
147  case Sum:
148  return weight * dep.sumWithin(deltaR_, vetos_, skipDefaultVeto_);
149  case SumRelative:
150  return weight * dep.sumWithin(deltaR_, vetos_, skipDefaultVeto_) / dep.candEnergy();
151  case Sum2:
152  return weight * dep.sum2Within(deltaR_, vetos_, skipDefaultVeto_);
153  case Sum2Relative:
154  return weight * dep.sum2Within(deltaR_, vetos_, skipDefaultVeto_) / (dep.candEnergy() * dep.candEnergy());
155  case Max:
156  return weight * dep.maxWithin(deltaR_, vetos_, skipDefaultVeto_);
157  case NearestDR:
158  return weight * dep.nearestDR(deltaR_, vetos_, skipDefaultVeto_);
159  case MaxRelative:
160  return weight * dep.maxWithin(deltaR_, vetos_, skipDefaultVeto_) / dep.candEnergy();
161  case MeanDR:
162  return weight * dep.algoWithin<reco::IsoDeposit::MeanDRAlgo>(deltaR_, vetos_, skipDefaultVeto_);
163  case SumDR:
164  return weight * dep.algoWithin<reco::IsoDeposit::SumDRAlgo>(deltaR_, vetos_, skipDefaultVeto_);
165  }
166  throw cms::Exception("Logic error") << "Should not happen at " << __FILE__ << ", line "
167  << __LINE__; // avoid gcc warning
168 }
169 
172  typedef std::vector<edm::ParameterSet> VPSet;
173  VPSet depPSets = par.getParameter<VPSet>("deposits");
174  for (VPSet::const_iterator it = depPSets.begin(), ed = depPSets.end(); it != ed; ++it) {
175  sources_.push_back(SingleDeposit(*it, consumesCollector()));
176  }
177  if (sources_.empty())
178  throw cms::Exception("Configuration Error") << "Please specify at least one deposit!";
179  produces<CandDoubleMap>();
180 }
181 
184  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
185  for (it = begin; it != end; ++it)
186  it->cleanup();
187 }
188 
191  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
192  for (it = begin; it != end; ++it)
193  it->open(event, eventSetup);
194 
195  const IsoDepositMap &map = begin->map();
196 
197  if (map.empty()) { // !!???
198  event.put(std::make_unique<CandDoubleMap>());
199  return;
200  }
201  auto ret = std::make_unique<CandDoubleMap>();
203 
204  typedef reco::IsoDepositMap::const_iterator iterator_i;
206  iterator_i depI = map.begin();
207  iterator_i depIEnd = map.end();
208  for (; depI != depIEnd; ++depI) {
209  std::vector<double> retV(depI.size(), 0);
211  event.get(depI.id(), candH);
212  const edm::View<reco::Candidate> &candV = *candH;
213 
214  iterator_ii depII = depI.begin();
215  iterator_ii depIIEnd = depI.end();
216  size_t iRet = 0;
217  for (; depII != depIIEnd; ++depII, ++iRet) {
218  double sum = 0;
219  for (it = begin; it != end; ++it)
220  sum += it->compute(candV.refAt(iRet));
221  retV[iRet] = sum;
222  }
223  filler.insert(candH, retV.begin(), retV.end());
224  }
225  filler.fill();
226  event.put(std::move(ret));
227 }
228 
static reco::isodeposit::AbsVeto * make(const char *string, edm::ConsumesCollector &iC)
bool isNumber(const std::string &str)
double sumWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:137
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
double phi() const
Definition: IsoDeposit.h:77
double compute(const reco::CandidateBaseRef &cand)
RefToBase< value_type > refAt(size_type i) const
vector< string > vstring
Definition: ExoticaDQM.cc:7
SingleDeposit(const edm::ParameterSet &, edm::ConsumesCollector &&iC)
ret
prodAgent to be discontinued
double sum2Within(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:143
double nearestDR(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:150
StringObjectFunction< reco::Candidate > weightExpr_
std::vector< SingleDeposit > sources_
Definition: weight.py:1
double maxWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:146
edm::ValueMap< double > CandDoubleMap
edm::Handle< reco::IsoDepositMap > hDeps_
std::vector< EventDependentAbsVeto * > EventDependentAbsVetos
double toNumber(const std::string &str)
int iEvent
Definition: GenABIO.cc:224
double candEnergy() const
Get energy or pT attached to cand trajectory.
Definition: IsoDeposit.h:129
void open(const edm::Event &iEvent, const edm::EventSetup &iSetup)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
double eta() const
Definition: IsoDeposit.h:76
edm::EDGetTokenT< reco::IsoDepositMap > srcToken_
void produce(edm::Event &, const edm::EventSetup &) override
build deposits
~CandIsolatorFromDeposits() override
destructor
CandIsolatorFromDeposits(const edm::ParameterSet &)
constructor with config
fixed size matrix
HLT enums.
std::vector< AbsVeto * > AbsVetos
Definition: IsoDeposit.h:43
reco::isodeposit::EventDependentAbsVetos evdepVetos_
const_iterator begin() const
double algoWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Get some info about the deposit (e.g. sum, max, sum2, count)
Definition: IsoDeposit.h:309
#define str(s)
double countWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:134
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1