CMS 3D CMS Logo

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