CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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  src_(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 throw cms::Exception("Not Implemented") << "Mode '" << mode << "' not implemented. " <<
55  "Supported modes are 'sum', 'sumRelative', 'count'." <<
56  //"Supported modes are 'sum', 'sumRelative', 'max', 'maxRelative', 'count'." << // TODO: on request only
57  "New methods can be easily implemented if requested.";
58  typedef std::vector<std::string> vstring;
59  vstring vetos = iConfig.getParameter< vstring >("vetos");
61  for (vstring::const_iterator it = vetos.begin(), ed = vetos.end(); it != ed; ++it) {
62  vetos_.push_back(IsoDepositVetoFactory::make(it->c_str(), evdep));
63  if (evdep) evdepVetos_.push_back(evdep);
64  }
65  std::string weight = iConfig.getParameter<std::string>("weight");
66  if (isNumber(weight)) {
67  //std::cout << "Weight is a simple number, " << toNumber(weight) << std::endl;
68  weight_ = toNumber(weight);
69  usesFunction_ = false;
70  } else {
71  usesFunction_ = true;
72  //std::cout << "Weight is a function, this might slow you down... " << std::endl;
73  }
74  //std::cout << "CandIsolatorFromDeposits::SingleDeposit::SingleDeposit: Total of " << vetos_.size() << " vetos" << std::endl;
75 }
77  for (AbsVetos::iterator it = vetos_.begin(), ed = vetos_.end(); it != ed; ++it) {
78  delete *it;
79  }
80  vetos_.clear();
81  // NOTE: we DON'T have to delete the evdepVetos_, they have already been deleted above. We just clear the vectors
82  evdepVetos_.clear();
83 }
85  iEvent.getByLabel(src_, hDeps_);
86  for (EventDependentAbsVetos::iterator it = evdepVetos_.begin(), ed = evdepVetos_.end(); it != ed; ++it) {
87  (*it)->setEvent(iEvent,iSetup);
88  }
89 }
90 
92  const IsoDeposit &dep = (*hDeps_)[cand];
93  double eta = dep.eta(), phi = dep.phi(); // better to center on the deposit direction
94  // that could be, e.g., the impact point at calo
95  for (AbsVetos::iterator it = vetos_.begin(), ed = vetos_.end(); it != ed; ++it) {
96  (*it)->centerOn(eta, phi);
97  }
98  double weight = (usesFunction_ ? weightExpr_(*cand) : weight_);
99  switch (mode_) {
100  case Count: return weight * dep.countWithin(deltaR_, vetos_, skipDefaultVeto_);
101  case Sum: return weight * dep.sumWithin(deltaR_, vetos_, skipDefaultVeto_);
102  case SumRelative: return weight * dep.sumWithin(deltaR_, vetos_, skipDefaultVeto_) / dep.candEnergy() ;
103  case Sum2: return weight * dep.sum2Within(deltaR_, vetos_, skipDefaultVeto_);
104  case Sum2Relative: return weight * dep.sum2Within(deltaR_, vetos_, skipDefaultVeto_) / (dep.candEnergy() * dep.candEnergy()) ;
105  case Max: return weight * dep.maxWithin(deltaR_, vetos_, skipDefaultVeto_);
106  case NearestDR: return weight * dep.nearestDR(deltaR_, vetos_, skipDefaultVeto_);
107  case MaxRelative: return weight * dep.maxWithin(deltaR_, vetos_, skipDefaultVeto_) / dep.candEnergy() ;
108  }
109  throw cms::Exception("Logic error") << "Should not happen at " << __FILE__ << ", line " << __LINE__; // avoid gcc warning
110 }
111 
112 
115  typedef std::vector<edm::ParameterSet> VPSet;
116  VPSet depPSets = par.getParameter<VPSet>("deposits");
117  for (VPSet::const_iterator it = depPSets.begin(), ed = depPSets.end(); it != ed; ++it) {
118  sources_.push_back(SingleDeposit(*it));
119  }
120  if (sources_.size() == 0) throw cms::Exception("Configuration Error") << "Please specify at least one deposit!";
121  produces<CandDoubleMap>();
122 }
123 
126  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
127  for (it = begin; it != end; ++it) it->cleanup();
128 }
129 
132 
133  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
134  for (it = begin; it != end; ++it) it->open(event, eventSetup);
135 
136  const IsoDepositMap & map = begin->map();
137 
138  if (map.size()==0) { // !!???
139  event.put(std::auto_ptr<CandDoubleMap>(new CandDoubleMap()));
140  return;
141  }
142  std::auto_ptr<CandDoubleMap> ret(new CandDoubleMap());
143  CandDoubleMap::Filler filler(*ret);
144 
145  typedef reco::IsoDepositMap::const_iterator iterator_i;
147  iterator_i depI = map.begin();
148  iterator_i depIEnd = map.end();
149  for (; depI != depIEnd; ++depI){
150  std::vector<double> retV(depI.size(),0);
152  event.get(depI.id(), candH);
153  const edm::View<reco::Candidate>& candV = *candH;
154 
155  iterator_ii depII = depI.begin();
156  iterator_ii depIIEnd = depI.end();
157  size_t iRet = 0;
158  for (; depII != depIIEnd; ++depII,++iRet){
159  double sum=0;
160  for (it = begin; it != end; ++it) sum += it->compute(candV.refAt(iRet));
161  retV[iRet] = sum;
162  }
163  filler.insert(candH, retV.begin(), retV.end());
164  }
165  filler.fill();
166  event.put(ret);
167 }
168 
T getParameter(std::string const &) const
double candEnergy() const
Get energy or pT attached to cand trajectory.
Definition: IsoDeposit.h:131
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:197
double eta() const
Definition: IsoDeposit.h:69
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
double phi() const
Definition: IsoDeposit.h:70
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
std::vector< SingleDeposit > sources_
double deltaR_
double countWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:134
RefToBase< value_type > refAt(size_type i) const
T eta() const
double maxWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:150
edm::ValueMap< double > CandDoubleMap
double sumWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:138
double toNumber(const std::string &str)
int iEvent
Definition: GenABIO.cc:243
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:38
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
virtual ~CandIsolatorFromDeposits()
destructor
const_iterator begin() const
Definition: ValueMap.h:196
CandIsolatorFromDeposits(const edm::ParameterSet &)
constructor with config
std::vector< std::string > vstring
#define begin
Definition: vmac.h:31
static reco::isodeposit::AbsVeto * make(const char *string)
size_t size() const
Definition: ValueMap.h:152
const_iterator begin() const
reco::isodeposit::EventDependentAbsVetos evdepVetos_
virtual void produce(edm::Event &, const edm::EventSetup &)
build deposits
Definition: DDAxes.h:10