CMS 3D CMS Logo

CandIsolatorFromDeposits.cc
Go to the documentation of this file.
2 
3 // Framework
9 
11 
18 
21 #include <string>
22 #include <boost/regex.hpp>
23 
25 
26 using namespace edm;
27 using namespace reco;
28 using namespace reco::isodeposit;
29 
30 bool isNumber(const std::string &str) {
31  static boost::regex re("^[+-]?(\\d+\\.?|\\d*\\.\\d*)$");
32  return regex_match(str.c_str(), re);
33 }
34 double toNumber(const std::string &str) {
35  return atof(str.c_str());
36 }
37 
39  srcToken_(iC.consumes<reco::IsoDepositMap>(iConfig.getParameter<edm::InputTag>("src"))),
40  deltaR_(iConfig.getParameter<double>("deltaR")),
41  weightExpr_(iConfig.getParameter<std::string>("weight")),
42  skipDefaultVeto_(iConfig.getParameter<bool>("skipDefaultVeto"))
43  //,vetos_(new AbsVetos())
44 {
45  std::string mode = iConfig.getParameter<std::string>("mode");
46  if (mode == "sum") mode_ = Sum;
47  else if (mode == "sumRelative") mode_ = SumRelative;
48  else if (mode == "sum2") mode_ = Sum2;
49  else if (mode == "sum2Relative") mode_ = Sum2Relative;
50  else if (mode == "max") mode_ = Max;
51  else if (mode == "maxRelative") mode_ = MaxRelative;
52  else if (mode == "nearestDR") mode_ = NearestDR;
53  else if (mode == "count") mode_ = Count;
54  else if (mode == "meanDR") mode_ = MeanDR;
55  else if (mode == "sumDR") mode_ = SumDR;
56  else throw cms::Exception("Not Implemented") << "Mode '" << mode << "' not implemented. " <<
57  "Supported modes are 'sum', 'sumRelative', 'count'." <<
58  //"Supported modes are 'sum', 'sumRelative', 'max', 'maxRelative', 'count'." << // TODO: on request only
59  "New methods can be easily implemented if requested.";
60  typedef std::vector<std::string> vstring;
61  vstring vetos = iConfig.getParameter< vstring >("vetos");
63  for (vstring::const_iterator it = vetos.begin(), ed = vetos.end(); it != ed; ++it) {
64  vetos_.push_back(IsoDepositVetoFactory::make(it->c_str(), evdep, iC));
65  if (evdep) evdepVetos_.push_back(evdep);
66  }
67  std::string weight = iConfig.getParameter<std::string>("weight");
68  if (isNumber(weight)) {
69  //std::cout << "Weight is a simple number, " << toNumber(weight) << std::endl;
70  weight_ = toNumber(weight);
71  usesFunction_ = false;
72  } else {
73  usesFunction_ = true;
74  //std::cout << "Weight is a function, this might slow you down... " << std::endl;
75  }
76  //std::cout << "CandIsolatorFromDeposits::SingleDeposit::SingleDeposit: Total of " << vetos_.size() << " vetos" << std::endl;
77 }
79  for (AbsVetos::iterator it = vetos_.begin(), ed = vetos_.end(); it != ed; ++it) {
80  delete *it;
81  }
82  vetos_.clear();
83  // NOTE: we DON'T have to delete the evdepVetos_, they have already been deleted above. We just clear the vectors
84  evdepVetos_.clear();
85 }
87  iEvent.getByToken(srcToken_, hDeps_);
88  for (EventDependentAbsVetos::iterator it = evdepVetos_.begin(), ed = evdepVetos_.end(); it != ed; ++it) {
89  (*it)->setEvent(iEvent,iSetup);
90  }
91 }
92 
94  const IsoDeposit &dep = (*hDeps_)[cand];
95  double eta = dep.eta(), phi = dep.phi(); // better to center on the deposit direction
96  // that could be, e.g., the impact point at calo
97  for (AbsVetos::iterator it = vetos_.begin(), ed = vetos_.end(); it != ed; ++it) {
98  (*it)->centerOn(eta, phi);
99  }
100  double weight = (usesFunction_ ? weightExpr_(*cand) : weight_);
101  switch (mode_) {
102  case Count: return weight * dep.countWithin(deltaR_, vetos_, skipDefaultVeto_);
103  case Sum: return weight * dep.sumWithin(deltaR_, vetos_, skipDefaultVeto_);
104  case SumRelative: return weight * dep.sumWithin(deltaR_, vetos_, skipDefaultVeto_) / dep.candEnergy() ;
105  case Sum2: return weight * dep.sum2Within(deltaR_, vetos_, skipDefaultVeto_);
106  case Sum2Relative: return weight * dep.sum2Within(deltaR_, vetos_, skipDefaultVeto_) / (dep.candEnergy() * dep.candEnergy()) ;
107  case Max: return weight * dep.maxWithin(deltaR_, vetos_, skipDefaultVeto_);
108  case NearestDR: return weight * dep.nearestDR(deltaR_, vetos_, skipDefaultVeto_);
109  case MaxRelative: return weight * dep.maxWithin(deltaR_, vetos_, skipDefaultVeto_) / dep.candEnergy() ;
112  }
113  throw cms::Exception("Logic error") << "Should not happen at " << __FILE__ << ", line " << __LINE__; // avoid gcc warning
114 }
115 
116 
119  typedef std::vector<edm::ParameterSet> VPSet;
120  VPSet depPSets = par.getParameter<VPSet>("deposits");
121  for (VPSet::const_iterator it = depPSets.begin(), ed = depPSets.end(); it != ed; ++it) {
122  sources_.push_back(SingleDeposit(*it, consumesCollector()));
123  }
124  if (sources_.empty()) throw cms::Exception("Configuration Error") << "Please specify at least one deposit!";
125  produces<CandDoubleMap>();
126 }
127 
130  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
131  for (it = begin; it != end; ++it) it->cleanup();
132 }
133 
136 
137  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
138  for (it = begin; it != end; ++it) it->open(event, eventSetup);
139 
140  const IsoDepositMap & map = begin->map();
141 
142  if (map.empty()) { // !!???
143  event.put(std::make_unique<CandDoubleMap>());
144  return;
145  }
146  auto ret = std::make_unique<CandDoubleMap>();
148 
149  typedef reco::IsoDepositMap::const_iterator iterator_i;
150  typedef reco::IsoDepositMap::container::const_iterator iterator_ii;
151  iterator_i depI = map.begin();
152  iterator_i depIEnd = map.end();
153  for (; depI != depIEnd; ++depI){
154  std::vector<double> retV(depI.size(),0);
156  event.get(depI.id(), candH);
157  const edm::View<reco::Candidate>& candV = *candH;
158 
159  iterator_ii depII = depI.begin();
160  iterator_ii depIIEnd = depI.end();
161  size_t iRet = 0;
162  for (; depII != depIIEnd; ++depII,++iRet){
163  double sum=0;
164  for (it = begin; it != end; ++it) sum += it->compute(candV.refAt(iRet));
165  retV[iRet] = sum;
166  }
167  filler.insert(candH, retV.begin(), retV.end());
168  }
169  filler.fill();
170  event.put(std::move(ret));
171 }
172 
static reco::isodeposit::AbsVeto * make(const char *string, edm::ConsumesCollector &iC)
bool empty() const
Definition: ValueMap.h:160
T getParameter(std::string const &) const
double candEnergy() const
Get energy or pT attached to cand trajectory.
Definition: IsoDeposit.h:136
bool isNumber(const std::string &str)
double sum2Within(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:146
double compute(const reco::CandidateBaseRef &cand)
const_iterator end() const
Definition: ValueMap.h:209
double eta() const
Definition: IsoDeposit.h:74
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:579
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
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:288
double phi() const
Definition: IsoDeposit.h:75
StringObjectFunction< reco::Candidate > weightExpr_
std::vector< SingleDeposit > sources_
double deltaR_
Definition: weight.py:1
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:150
edm::Handle< reco::IsoDepositMap > hDeps_
double sumWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:138
RefToBase< value_type > refAt(size_type i) const
double toNumber(const std::string &str)
int iEvent
Definition: GenABIO.cc:230
const_iterator begin() const
double nearestDR(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:155
void open(const edm::Event &iEvent, const edm::EventSetup &iSetup)
#define end
Definition: vmac.h:39
edm::EDGetTokenT< reco::IsoDepositMap > srcToken_
const_iterator begin() const
Definition: ValueMap.h:208
void produce(edm::Event &, const edm::EventSetup &) override
build deposits
~CandIsolatorFromDeposits() override
destructor
CandIsolatorFromDeposits(const edm::ParameterSet &)
constructor with config
fixed size matrix
#define begin
Definition: vmac.h:32
HLT enums.
reco::isodeposit::EventDependentAbsVetos evdepVetos_
#define str(s)
def move(src, dest)
Definition: eostools.py:510
Definition: event.py:1