CMS 3D CMS Logo

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