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 namespace {
64  bool isNumber(const std::string &str) {
65  static const std::regex re("^[+-]?(\\d+\\.?|\\d*\\.\\d*)$");
66  return regex_match(str.c_str(), re);
67  }
68  double toNumber(const std::string &str) { return atof(str.c_str()); }
69 } // namespace
70 
72  : srcToken_(iC.consumes<reco::IsoDepositMap>(iConfig.getParameter<edm::InputTag>("src"))),
73  deltaR_(iConfig.getParameter<double>("deltaR")),
74  weightExpr_(iConfig.getParameter<std::string>("weight")),
75  skipDefaultVeto_(iConfig.getParameter<bool>("skipDefaultVeto"))
76 //,vetos_(new AbsVetos())
77 {
78  std::string mode = iConfig.getParameter<std::string>("mode");
79  if (mode == "sum")
80  mode_ = Sum;
81  else if (mode == "sumRelative")
83  else if (mode == "sum2")
84  mode_ = Sum2;
85  else if (mode == "sum2Relative")
87  else if (mode == "max")
88  mode_ = Max;
89  else if (mode == "maxRelative")
91  else if (mode == "nearestDR")
92  mode_ = NearestDR;
93  else if (mode == "count")
94  mode_ = Count;
95  else if (mode == "meanDR")
96  mode_ = MeanDR;
97  else if (mode == "sumDR")
98  mode_ = SumDR;
99  else
100  throw cms::Exception("Not Implemented") << "Mode '" << mode << "' not implemented. "
101  << "Supported modes are 'sum', 'sumRelative', 'count'." <<
102  //"Supported modes are 'sum', 'sumRelative', 'max', 'maxRelative', 'count'." << // TODO: on request only
103  "New methods can be easily implemented if requested.";
104  typedef std::vector<std::string> vstring;
105  vstring vetos = iConfig.getParameter<vstring>("vetos");
107  for (vstring::const_iterator it = vetos.begin(), ed = vetos.end(); it != ed; ++it) {
108  vetos_.push_back(IsoDepositVetoFactory::make(it->c_str(), evdep, iC));
109  if (evdep)
110  evdepVetos_.push_back(evdep);
111  }
112  std::string weight = iConfig.getParameter<std::string>("weight");
113  if (isNumber(weight)) {
114  //std::cout << "Weight is a simple number, " << toNumber(weight) << std::endl;
115  weight_ = toNumber(weight);
116  usesFunction_ = false;
117  } else {
118  usesFunction_ = true;
119  //std::cout << "Weight is a function, this might slow you down... " << std::endl;
120  }
121  //std::cout << "CandIsolatorFromDeposits::SingleDeposit::SingleDeposit: Total of " << vetos_.size() << " vetos" << std::endl;
122 }
124  for (AbsVetos::iterator it = vetos_.begin(), ed = vetos_.end(); it != ed; ++it) {
125  delete *it;
126  }
127  vetos_.clear();
128  // NOTE: we DON'T have to delete the evdepVetos_, they have already been deleted above. We just clear the vectors
129  evdepVetos_.clear();
130 }
132  iEvent.getByToken(srcToken_, hDeps_);
133  for (EventDependentAbsVetos::iterator it = evdepVetos_.begin(), ed = evdepVetos_.end(); it != ed; ++it) {
134  (*it)->setEvent(iEvent, iSetup);
135  }
136 }
137 
139  const IsoDeposit &dep = (*hDeps_)[cand];
140  double eta = dep.eta(), phi = dep.phi(); // better to center on the deposit direction
141  // that could be, e.g., the impact point at calo
142  for (AbsVetos::iterator it = vetos_.begin(), ed = vetos_.end(); it != ed; ++it) {
143  (*it)->centerOn(eta, phi);
144  }
145  double weight = (usesFunction_ ? weightExpr_(*cand) : weight_);
146  switch (mode_) {
147  case Count:
148  return weight * dep.countWithin(deltaR_, vetos_, skipDefaultVeto_);
149  case Sum:
150  return weight * dep.sumWithin(deltaR_, vetos_, skipDefaultVeto_);
151  case SumRelative:
152  return weight * dep.sumWithin(deltaR_, vetos_, skipDefaultVeto_) / dep.candEnergy();
153  case Sum2:
154  return weight * dep.sum2Within(deltaR_, vetos_, skipDefaultVeto_);
155  case Sum2Relative:
156  return weight * dep.sum2Within(deltaR_, vetos_, skipDefaultVeto_) / (dep.candEnergy() * dep.candEnergy());
157  case Max:
158  return weight * dep.maxWithin(deltaR_, vetos_, skipDefaultVeto_);
159  case NearestDR:
160  return weight * dep.nearestDR(deltaR_, vetos_, skipDefaultVeto_);
161  case MaxRelative:
162  return weight * dep.maxWithin(deltaR_, vetos_, skipDefaultVeto_) / dep.candEnergy();
163  case MeanDR:
164  return weight * dep.algoWithin<reco::IsoDeposit::MeanDRAlgo>(deltaR_, vetos_, skipDefaultVeto_);
165  case SumDR:
166  return weight * dep.algoWithin<reco::IsoDeposit::SumDRAlgo>(deltaR_, vetos_, skipDefaultVeto_);
167  }
168  throw cms::Exception("Logic error") << "Should not happen at " << __FILE__ << ", line "
169  << __LINE__; // avoid gcc warning
170 }
171 
174  typedef std::vector<edm::ParameterSet> VPSet;
175  VPSet depPSets = par.getParameter<VPSet>("deposits");
176  for (VPSet::const_iterator it = depPSets.begin(), ed = depPSets.end(); it != ed; ++it) {
177  sources_.push_back(SingleDeposit(*it, consumesCollector()));
178  }
179  if (sources_.empty())
180  throw cms::Exception("Configuration Error") << "Please specify at least one deposit!";
181  produces<CandDoubleMap>();
182 }
183 
186  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
187  for (it = begin; it != end; ++it)
188  it->cleanup();
189 }
190 
193  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
194  for (it = begin; it != end; ++it)
195  it->open(event, eventSetup);
196 
197  const IsoDepositMap &map = begin->map();
198 
199  if (map.empty()) { // !!???
200  event.put(std::make_unique<CandDoubleMap>());
201  return;
202  }
203  auto ret = std::make_unique<CandDoubleMap>();
205 
206  typedef reco::IsoDepositMap::const_iterator iterator_i;
208  iterator_i depI = map.begin();
209  iterator_i depIEnd = map.end();
210  for (; depI != depIEnd; ++depI) {
211  std::vector<double> retV(depI.size(), 0);
213  event.get(depI.id(), candH);
214  const edm::View<reco::Candidate> &candV = *candH;
215 
216  iterator_ii depII = depI.begin();
217  iterator_ii depIIEnd = depI.end();
218  size_t iRet = 0;
219  for (; depII != depIIEnd; ++depII, ++iRet) {
220  double sum = 0;
221  for (it = begin; it != end; ++it)
222  sum += it->compute(candV.refAt(iRet));
223  retV[iRet] = sum;
224  }
225  filler.insert(candH, retV.begin(), retV.end());
226  }
227  filler.fill();
228  event.put(std::move(ret));
229 }
230 
static reco::isodeposit::AbsVeto * make(const char *string, edm::ConsumesCollector &iC)
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:307
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
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