CMS 3D CMS Logo

PFCandIsolatorFromDeposit.cc
Go to the documentation of this file.
26 
27 #include <memory>
28 #include <regex>
29 #include <string>
30 
32 public:
34 
37 
38  ~PFCandIsolatorFromDeposits() override;
39 
40  void produce(edm::Event &, const edm::EventSetup &) override;
41 
42 private:
43  class SingleDeposit {
44  public:
46  void cleanup();
47  void open(const edm::Event &iEvent, const edm::EventSetup &iSetup);
48  double compute(const reco::CandidateBaseRef &cand);
49  const reco::IsoDepositMap &map() { return *hDeps_; }
50 
51  private:
54  double deltaR_;
56  double weight_;
57 
61  reco::isodeposit::EventDependentAbsVetos evdepVetos_; // note: these are a subset of the above. Don't delete twice!
65 
66  bool isNumber(const std::string &str) const;
67  double toNumber(const std::string &str) const;
68  };
69  // datamembers
70  std::vector<SingleDeposit> sources_;
71 };
72 
73 using namespace edm;
74 using namespace reco;
75 using namespace reco::isodeposit;
76 
78  static const std::regex re("^[+-]?(\\d+\\.?|\\d*\\.\\d*)$");
79  return regex_match(str.c_str(), re);
80 }
81 double PFCandIsolatorFromDeposits::SingleDeposit::toNumber(const std::string &str) const { return atof(str.c_str()); }
82 
84  : srcToken_(iC.consumes<reco::IsoDepositMap>(iConfig.getParameter<edm::InputTag>("src"))),
85  deltaR_(iConfig.getParameter<double>("deltaR")),
86  weightExpr_(iConfig.getParameter<std::string>("weight")),
87  skipDefaultVeto_(iConfig.getParameter<bool>("skipDefaultVeto")),
88  usePivotForBarrelEndcaps_(iConfig.getParameter<bool>("PivotCoordinatesForEBEE"))
89 //,vetos_(new AbsVetos())
90 {
91  std::string mode = iConfig.getParameter<std::string>("mode");
92  if (mode == "sum")
93  mode_ = Sum;
94  else if (mode == "sumRelative")
96  else if (mode == "sum2")
97  mode_ = Sum2;
98  else if (mode == "sum2Relative")
100  else if (mode == "max")
101  mode_ = Max;
102  else if (mode == "maxRelative")
103  mode_ = MaxRelative;
104  else if (mode == "nearestDR")
105  mode_ = NearestDR;
106  else if (mode == "count")
107  mode_ = Count;
108  else
109  throw cms::Exception("Not Implemented") << "Mode '" << mode << "' not implemented. "
110  << "Supported modes are 'sum', 'sumRelative', 'count'." <<
111  //"Supported modes are 'sum', 'sumRelative', 'max', 'maxRelative', 'count'." << // TODO: on request only
112  "New methods can be easily implemented if requested.";
113  typedef std::vector<std::string> vstring;
114  vstring vetos = iConfig.getParameter<vstring>("vetos");
116  static const std::regex ecalSwitch("^Ecal(Barrel|Endcaps):(.*)");
117 
118  for (vstring::const_iterator it = vetos.begin(), ed = vetos.end(); it != ed; ++it) {
119  std::cmatch match;
120  // in that case, make two series of vetoes
122  if (regex_match(it->c_str(), match, ecalSwitch)) {
123  if (match[1] == "Barrel") {
124  // std::cout << " Adding Barrel veto " << std::string(match[2]) << std::endl;
125  barrelVetos_.push_back(
126  IsoDepositVetoFactory::make(std::string(match[2]).c_str(), evdep, iC)); // I don't know a better syntax
127  }
128  if (match[1] == "Endcaps") {
129  // std::cout << " Adding Endcap veto " << std::string(match[2]) << std::endl;
130  endcapVetos_.push_back(IsoDepositVetoFactory::make(std::string(match[2]).c_str(), evdep, iC));
131  }
132  } else {
133  barrelVetos_.push_back(IsoDepositVetoFactory::make(it->c_str(), evdep, iC));
134  endcapVetos_.push_back(IsoDepositVetoFactory::make(it->c_str(), evdep, iC));
135  }
136  } else {
137  //only one serie of vetoes, just barrel
138  barrelVetos_.push_back(IsoDepositVetoFactory::make(it->c_str(), evdep, iC));
139  }
140  if (evdep)
141  evdepVetos_.push_back(evdep);
142  }
143 
144  std::string weight = iConfig.getParameter<std::string>("weight");
145  if (isNumber(weight)) {
146  //std::cout << "Weight is a simple number, " << toNumber(weight) << std::endl;
148  usesFunction_ = false;
149  } else {
150  usesFunction_ = true;
151  //std::cout << "Weight is a function, this might slow you down... " << std::endl;
152  }
153  //std::cout << "PFCandIsolatorFromDeposits::SingleDeposit::SingleDeposit: Total of " << vetos_.size() << " vetos" << std::endl;
154 }
156  for (AbsVetos::iterator it = barrelVetos_.begin(), ed = barrelVetos_.end(); it != ed; ++it) {
157  delete *it;
158  }
159  for (AbsVetos::iterator it = endcapVetos_.begin(), ed = endcapVetos_.end(); it != ed; ++it) {
160  delete *it;
161  }
162  barrelVetos_.clear();
163  endcapVetos_.clear();
164  // NOTE: we DON'T have to delete the evdepVetos_, they have already been deleted above. We just clear the vectors
165  evdepVetos_.clear();
166 }
168  iEvent.getByToken(srcToken_, hDeps_);
169  for (EventDependentAbsVetos::iterator it = evdepVetos_.begin(), ed = evdepVetos_.end(); it != ed; ++it) {
170  (*it)->setEvent(iEvent, iSetup);
171  }
172 }
173 
175  const IsoDeposit &dep = (*hDeps_)[cand];
176  double eta = dep.eta(), phi = dep.phi(); // better to center on the deposit direction
177  // that could be, e.g., the impact point at calo
178  bool barrel = true;
179  if (usePivotForBarrelEndcaps_) {
180  const reco::PFCandidate *myPFCand = dynamic_cast<const reco::PFCandidate *>(&(*cand));
181  if (myPFCand) {
182  // exact barrel boundary
183  barrel = fabs(myPFCand->positionAtECALEntrance().eta()) < 1.479;
184  } else {
185  const reco::RecoCandidate *myRecoCand = dynamic_cast<const reco::RecoCandidate *>(&(*cand));
186  if (myRecoCand) {
187  // not optimal. isEB should be used.
188  barrel = (fabs(myRecoCand->superCluster()->eta()) < 1.479);
189  }
190  }
191  }
192  // if ! usePivotForBarrelEndcaps_ only the barrel series is used, which does not prevent the vetoes do be different in barrel & endcaps
193  reco::isodeposit::AbsVetos *vetos = (barrel) ? &barrelVetos_ : &endcapVetos_;
194 
195  for (AbsVetos::iterator it = vetos->begin(), ed = vetos->end(); it != ed; ++it) {
196  (*it)->centerOn(eta, phi);
197  }
198  double weight = (usesFunction_ ? weightExpr_(*cand) : weight_);
199  switch (mode_) {
200  case Count:
201  return weight * dep.countWithin(deltaR_, *vetos, skipDefaultVeto_);
202  case Sum:
203  return weight * dep.sumWithin(deltaR_, *vetos, skipDefaultVeto_);
204  case SumRelative:
205  return weight * dep.sumWithin(deltaR_, *vetos, skipDefaultVeto_) / dep.candEnergy();
206  case Sum2:
207  return weight * dep.sum2Within(deltaR_, *vetos, skipDefaultVeto_);
208  case Sum2Relative:
209  return weight * dep.sum2Within(deltaR_, *vetos, skipDefaultVeto_) / (dep.candEnergy() * dep.candEnergy());
210  case Max:
211  return weight * dep.maxWithin(deltaR_, *vetos, skipDefaultVeto_);
212  case NearestDR:
213  return weight * dep.nearestDR(deltaR_, *vetos, skipDefaultVeto_);
214  case MaxRelative:
215  return weight * dep.maxWithin(deltaR_, *vetos, skipDefaultVeto_) / dep.candEnergy();
216  }
217  throw cms::Exception("Logic error") << "Should not happen at " << __FILE__ << ", line "
218  << __LINE__; // avoid gcc warning
219 }
220 
223  typedef std::vector<edm::ParameterSet> VPSet;
224  VPSet depPSets = par.getParameter<VPSet>("deposits");
225  for (VPSet::const_iterator it = depPSets.begin(), ed = depPSets.end(); it != ed; ++it) {
226  sources_.push_back(SingleDeposit(*it, consumesCollector()));
227  }
228  if (sources_.empty())
229  throw cms::Exception("Configuration Error") << "Please specify at least one deposit!";
230  produces<CandDoubleMap>();
231 }
232 
235  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
236  for (it = begin; it != end; ++it)
237  it->cleanup();
238 }
239 
242  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
243  for (it = begin; it != end; ++it)
244  it->open(event, eventSetup);
245 
246  const IsoDepositMap &map = begin->map();
247 
248  if (map.empty()) { // !!???
249  event.put(std::make_unique<CandDoubleMap>());
250  return;
251  }
252  std::unique_ptr<CandDoubleMap> ret(new CandDoubleMap());
254 
255  typedef reco::IsoDepositMap::const_iterator iterator_i;
257  iterator_i depI = map.begin();
258  iterator_i depIEnd = map.end();
259  for (; depI != depIEnd; ++depI) {
260  std::vector<double> retV(depI.size(), 0);
262  event.get(depI.id(), candH);
263  const edm::View<reco::Candidate> &candV = *candH;
264 
265  iterator_ii depII = depI.begin();
266  iterator_ii depIIEnd = depI.end();
267  size_t iRet = 0;
268  for (; depII != depIIEnd; ++depII, ++iRet) {
269  double sum = 0;
270  for (it = begin; it != end; ++it)
271  sum += it->compute(candV.refAt(iRet));
272  retV[iRet] = sum;
273  }
274  filler.insert(candH, retV.begin(), retV.end());
275  }
276  filler.fill();
277  event.put(std::move(ret));
278 }
279 
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:303
double phi() const
Definition: IsoDeposit.h:77
std::vector< SingleDeposit > sources_
RefToBase< value_type > refAt(size_type i) const
double compute(const reco::CandidateBaseRef &cand)
bool isNumber(const std::string &str) const
vector< string > vstring
Definition: ExoticaDQM.cc:7
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_
const math::XYZPointF & positionAtECALEntrance() const
Definition: PFCandidate.h:388
Definition: weight.py:1
double maxWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:146
SingleDeposit(const edm::ParameterSet &, edm::ConsumesCollector &&iC)
std::vector< EventDependentAbsVeto * > EventDependentAbsVetos
~PFCandIsolatorFromDeposits() override
destructor
int iEvent
Definition: GenABIO.cc:224
reco::isodeposit::EventDependentAbsVetos evdepVetos_
double candEnergy() const
Get energy or pT attached to cand trajectory.
Definition: IsoDeposit.h:129
edm::EDGetTokenT< reco::IsoDepositMap > srcToken_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void produce(edm::Event &, const edm::EventSetup &) override
build deposits
double eta() const
Definition: IsoDeposit.h:76
PFCandIsolatorFromDeposits(const edm::ParameterSet &)
constructor with config
void open(const edm::Event &iEvent, const edm::EventSetup &iSetup)
virtual reco::SuperClusterRef superCluster() const
reference to a SuperCluster
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
fixed size matrix
HLT enums.
double toNumber(const std::string &str) const
std::vector< AbsVeto * > AbsVetos
Definition: IsoDeposit.h:43
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
const_iterator begin() const
#define str(s)
double countWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:134
edm::ValueMap< double > CandDoubleMap
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1