CMS 3D CMS Logo

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