CMS 3D CMS Logo

PFCandIsolatorFromDeposit.cc
Go to the documentation of this file.
2 
3 // Framework
9 
11 
23 #include <string>
24 #include <boost/regex.hpp>
25 
27 
28 using namespace edm;
29 using namespace reco;
30 using namespace reco::isodeposit;
31 
33  static boost::regex re("^[+-]?(\\d+\\.?|\\d*\\.\\d*)$");
34  return regex_match(str.c_str(), re);
35 }
37  return atof(str.c_str());
38 }
39 
41  srcToken_(iC.consumes<reco::IsoDepositMap>(iConfig.getParameter<edm::InputTag>("src"))),
42  deltaR_(iConfig.getParameter<double>("deltaR")),
43  weightExpr_(iConfig.getParameter<std::string>("weight")),
44  skipDefaultVeto_(iConfig.getParameter<bool>("skipDefaultVeto")),
45  usePivotForBarrelEndcaps_(iConfig.getParameter<bool>("PivotCoordinatesForEBEE"))
46  //,vetos_(new AbsVetos())
47 {
48  std::string mode = iConfig.getParameter<std::string>("mode");
49  if (mode == "sum") mode_ = Sum;
50  else if (mode == "sumRelative") mode_ = SumRelative;
51  else if (mode == "sum2") mode_ = Sum2;
52  else if (mode == "sum2Relative") mode_ = Sum2Relative;
53  else if (mode == "max") mode_ = Max;
54  else if (mode == "maxRelative") mode_ = MaxRelative;
55  else if (mode == "nearestDR") mode_ = NearestDR;
56  else if (mode == "count") mode_ = Count;
57  else throw cms::Exception("Not Implemented") << "Mode '" << mode << "' not implemented. " <<
58  "Supported modes are 'sum', 'sumRelative', 'count'." <<
59  //"Supported modes are 'sum', 'sumRelative', 'max', 'maxRelative', 'count'." << // TODO: on request only
60  "New methods can be easily implemented if requested.";
61  typedef std::vector<std::string> vstring;
62  vstring vetos = iConfig.getParameter< vstring >("vetos");
64  static boost::regex ecalSwitch("^Ecal(Barrel|Endcaps):(.*)");
65 
66  for (vstring::const_iterator it = vetos.begin(), ed = vetos.end(); it != ed; ++it) {
67  boost::cmatch match;
68  // in that case, make two series of vetoes
70  if (regex_match(it->c_str(), match, ecalSwitch))
71  {
72  if(match[1] == "Barrel") {
73  // std::cout << " Adding Barrel veto " << std::string(match[2]) << std::endl;
74  barrelVetos_.push_back(IsoDepositVetoFactory::make(std::string(match[2]).c_str(), evdep, iC)); // I don't know a better syntax
75  }
76  if(match[1] == "Endcaps") {
77  // std::cout << " Adding Endcap veto " << std::string(match[2]) << std::endl;
78  endcapVetos_.push_back(IsoDepositVetoFactory::make(std::string(match[2]).c_str(), evdep, iC));
79  }
80  }
81  else
82  {
83  barrelVetos_.push_back(IsoDepositVetoFactory::make(it->c_str(), evdep, iC));
84  endcapVetos_.push_back(IsoDepositVetoFactory::make(it->c_str(), evdep, iC));
85  }
86  } else {
87  //only one serie of vetoes, just barrel
88  barrelVetos_.push_back(IsoDepositVetoFactory::make(it->c_str(), evdep, iC));
89  }
90  if (evdep) evdepVetos_.push_back(evdep);
91  }
92 
93  std::string weight = iConfig.getParameter<std::string>("weight");
94  if (isNumber(weight)) {
95  //std::cout << "Weight is a simple number, " << toNumber(weight) << std::endl;
96  weight_ = toNumber(weight);
97  usesFunction_ = false;
98  } else {
99  usesFunction_ = true;
100  //std::cout << "Weight is a function, this might slow you down... " << std::endl;
101  }
102  //std::cout << "PFCandIsolatorFromDeposits::SingleDeposit::SingleDeposit: Total of " << vetos_.size() << " vetos" << std::endl;
103 }
105  for (AbsVetos::iterator it = barrelVetos_.begin(), ed = barrelVetos_.end(); it != ed; ++it) {
106  delete *it;
107  }
108  for (AbsVetos::iterator it = endcapVetos_.begin(), ed = endcapVetos_.end(); it != ed; ++it) {
109  delete *it;
110  }
111  barrelVetos_.clear();
112  endcapVetos_.clear();
113  // NOTE: we DON'T have to delete the evdepVetos_, they have already been deleted above. We just clear the vectors
114  evdepVetos_.clear();
115 }
117  iEvent.getByToken(srcToken_, hDeps_);
118  for (EventDependentAbsVetos::iterator it = evdepVetos_.begin(), ed = evdepVetos_.end(); it != ed; ++it) {
119  (*it)->setEvent(iEvent,iSetup);
120  }
121 }
122 
124  const IsoDeposit &dep = (*hDeps_)[cand];
125  double eta = dep.eta(), phi = dep.phi(); // better to center on the deposit direction
126  // that could be, e.g., the impact point at calo
127  bool barrel=true;
129  const reco::PFCandidate * myPFCand = dynamic_cast<const reco::PFCandidate*>(&(*cand));
130  if(myPFCand) {
131  // exact barrel boundary
132  barrel = fabs(myPFCand->positionAtECALEntrance().eta())<1.479;
133  }
134  else {
135  const reco::RecoCandidate * myRecoCand = dynamic_cast<const reco::RecoCandidate*>(&(*cand));
136  if(myRecoCand) {
137  // not optimal. isEB should be used.
138  barrel = ( fabs(myRecoCand->superCluster()->eta())<1.479 );
139  }
140  }
141  }
142  // if ! usePivotForBarrelEndcaps_ only the barrel series is used, which does not prevent the vetoes do be different in barrel & endcaps
144 
145  for (AbsVetos::iterator it = vetos->begin(), ed = vetos->end(); it != ed; ++it) {
146  (*it)->centerOn(eta, phi);
147  }
148  double weight = (usesFunction_ ? weightExpr_(*cand) : weight_);
149  switch (mode_) {
150  case Count: return weight * dep.countWithin(deltaR_, *vetos, skipDefaultVeto_);
151  case Sum: return weight * dep.sumWithin(deltaR_, *vetos, skipDefaultVeto_);
152  case SumRelative: return weight * dep.sumWithin(deltaR_, *vetos, skipDefaultVeto_) / dep.candEnergy() ;
153  case Sum2: return weight * dep.sum2Within(deltaR_, *vetos, skipDefaultVeto_);
154  case Sum2Relative: return weight * dep.sum2Within(deltaR_, *vetos, skipDefaultVeto_) / (dep.candEnergy() * dep.candEnergy()) ;
155  case Max: return weight * dep.maxWithin(deltaR_, *vetos, skipDefaultVeto_);
156  case NearestDR: return weight * dep.nearestDR(deltaR_, *vetos, skipDefaultVeto_);
157  case MaxRelative: return weight * dep.maxWithin(deltaR_, *vetos, skipDefaultVeto_) / dep.candEnergy() ;
158  }
159  throw cms::Exception("Logic error") << "Should not happen at " << __FILE__ << ", line " << __LINE__; // avoid gcc warning
160 }
161 
164  typedef std::vector<edm::ParameterSet> VPSet;
165  VPSet depPSets = par.getParameter<VPSet>("deposits");
166  for (VPSet::const_iterator it = depPSets.begin(), ed = depPSets.end(); it != ed; ++it) {
167  sources_.push_back(SingleDeposit(*it, consumesCollector()));
168  }
169  if (sources_.empty()) throw cms::Exception("Configuration Error") << "Please specify at least one deposit!";
170  produces<CandDoubleMap>();
171 }
172 
175  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
176  for (it = begin; it != end; ++it) it->cleanup();
177 }
178 
181 
182  std::vector<SingleDeposit>::iterator it, begin = sources_.begin(), end = sources_.end();
183  for (it = begin; it != end; ++it) it->open(event, eventSetup);
184 
185  const IsoDepositMap & map = begin->map();
186 
187  if (map.empty()) { // !!???
188  event.put(std::unique_ptr<CandDoubleMap>(new CandDoubleMap()));
189  return;
190  }
191  std::unique_ptr<CandDoubleMap> ret(new CandDoubleMap());
193 
194  typedef reco::IsoDepositMap::const_iterator iterator_i;
195  typedef reco::IsoDepositMap::container::const_iterator iterator_ii;
196  iterator_i depI = map.begin();
197  iterator_i depIEnd = map.end();
198  for (; depI != depIEnd; ++depI){
199  std::vector<double> retV(depI.size(),0);
201  event.get(depI.id(), candH);
202  const edm::View<reco::Candidate>& candV = *candH;
203 
204  iterator_ii depII = depI.begin();
205  iterator_ii depIIEnd = depI.end();
206  size_t iRet = 0;
207  for (; depII != depIIEnd; ++depII,++iRet){
208  double sum=0;
209  for (it = begin; it != end; ++it) sum += it->compute(candV.refAt(iRet));
210  retV[iRet] = sum;
211  }
212  filler.insert(candH, retV.begin(), retV.end());
213  }
214  filler.fill();
215  event.put(std::move(ret));
216 }
217 
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
double sum2Within(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:146
std::vector< SingleDeposit > sources_
const_iterator end() const
Definition: ValueMap.h:209
double compute(const reco::CandidateBaseRef &cand)
double eta() const
Definition: IsoDeposit.h:74
vector< string > vstring
Definition: ExoticaDQM.cc:8
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
double phi() const
Definition: IsoDeposit.h:75
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
StringObjectFunction< reco::Candidate > weightExpr_
double deltaR_
Definition: weight.py:1
double countWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:134
SingleDeposit(const edm::ParameterSet &, edm::ConsumesCollector &&iC)
double maxWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:150
const math::XYZPointF & positionAtECALEntrance() const
Definition: PFCandidate.h:367
double sumWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:138
RefToBase< value_type > refAt(size_type i) const
~PFCandIsolatorFromDeposits() override
destructor
int iEvent
Definition: GenABIO.cc:230
reco::isodeposit::EventDependentAbsVetos evdepVetos_
const_iterator begin() const
edm::Handle< reco::IsoDepositMap > hDeps_
double nearestDR(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:155
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
edm::EDGetTokenT< reco::IsoDepositMap > srcToken_
#define end
Definition: vmac.h:37
bool isNumber(const std::string &str) const
void produce(edm::Event &, const edm::EventSetup &) override
build deposits
PFCandIsolatorFromDeposits(const edm::ParameterSet &)
constructor with config
const_iterator begin() const
Definition: ValueMap.h:208
void open(const edm::Event &iEvent, const edm::EventSetup &iSetup)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
fixed size matrix
#define begin
Definition: vmac.h:30
HLT enums.
std::vector< AbsVeto * > AbsVetos
Definition: IsoDeposit.h:40
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
double toNumber(const std::string &str) const
edm::ValueMap< double > CandDoubleMap
def move(src, dest)
Definition: eostools.py:510
Definition: event.py:1
virtual reco::SuperClusterRef superCluster() const
reference to a SuperCluster