CMS 3D CMS Logo

CITKPFIsolationSumProducerForPUPPI.cc
Go to the documentation of this file.
4 
16 
20 
21 #include <string>
22 #include <unordered_map>
23 
24 //module to compute isolation sum weighted with PUPPI weights
25 namespace citk {
27  public:
29 
31 
33 
34  void produce(edm::Event&, const edm::EventSetup&) final;
35 
36  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
37 
38  private:
39  // datamembers
40  static constexpr unsigned kNPFTypes = 8;
41  typedef std::unordered_map<std::string, int> TypeMap;
42  typedef std::vector<std::unique_ptr<IsolationConeDefinitionBase>> IsoTypes;
48  // indexed by pf candidate type
49  std::array<IsoTypes, kNPFTypes> _isolation_types;
50  std::array<std::vector<std::string>, kNPFTypes> _product_names;
51  bool useValueMapForPUPPI = true;
53  false; // in case puppi weights are taken from packedCandidate can take weights for puppiNoLeptons
54  };
55 } // namespace citk
56 
58 
60 
61 namespace citk {
63  : _typeMap({{"h+", 1}, {"h0", 5}, {"gamma", 4}, {"electron", 2}, {"muon", 3}, {"HFh", 6}, {"HFgamma", 7}}) {
64  _to_isolate = consumes<CandView>(c.getParameter<edm::InputTag>("srcToIsolate"));
65  _isolate_with = consumes<CandView>(c.getParameter<edm::InputTag>("srcForIsolationCone"));
66  if (!c.getParameter<edm::InputTag>("puppiValueMap").label().empty()) {
67  puppiValueMapToken_ = mayConsume<edm::ValueMap<float>>(
68  c.getParameter<edm::InputTag>("puppiValueMap")); //getting token for puppiValueMap
69  useValueMapForPUPPI = true;
70  } else {
71  useValueMapForPUPPI = false;
72  usePUPPINoLepton = c.getParameter<bool>("usePUPPINoLepton");
73  }
74  const std::vector<edm::ParameterSet>& isoDefs = c.getParameterSetVector("isolationConeDefinitions");
75  for (const auto& isodef : isoDefs) {
76  const std::string& name = isodef.getParameter<std::string>("isolationAlgo");
77  const float coneSize = isodef.getParameter<double>("coneSize");
78  char buf[50];
79  std::sprintf(buf, "DR%.2f", coneSize);
80  std::string coneName(buf);
81  auto decimal = coneName.find('.');
82  if (decimal != std::string::npos)
83  coneName.erase(decimal, 1);
84  const std::string& isotype = isodef.getParameter<std::string>("isolateAgainst");
85  auto theisolator = CITKIsolationConeDefinitionFactory::get()->create(name, isodef);
86  theisolator->setConsumes(consumesCollector());
87  const auto thetype = _typeMap.find(isotype);
88  if (thetype == _typeMap.end()) {
89  throw cms::Exception("InvalidIsolationType") << "Isolation type: " << isotype << " is not available in the "
90  << "list of allowed isolations!.";
91  }
92  const std::string dash("-");
93  std::string pname = isotype + dash + coneName + dash + theisolator->additionalCode();
94  _product_names[thetype->second].emplace_back(pname);
95  produces<edm::ValueMap<float>>(pname);
96  _isolation_types[thetype->second].emplace_back(std::move(theisolator));
97  }
98  }
99 
101  for (const auto& isolators_for_type : _isolation_types) {
102  for (const auto& isolator : isolators_for_type) {
103  isolator->getEventSetupInfo(es);
104  }
105  }
106  }
107 
109  typedef std::unique_ptr<edm::ValueMap<float>> product_type;
110  typedef std::vector<float> product_values;
111  edm::Handle<CandView> to_isolate;
112  edm::Handle<CandView> isolate_with;
113  ev.getByToken(_to_isolate, to_isolate);
114  ev.getByToken(_isolate_with, isolate_with);
116  ev.getByToken(puppiValueMapToken_, puppiValueMap);
117 
118  // the list of value vectors indexed as "to_isolate"
119  std::array<std::vector<product_values>, kNPFTypes> the_values;
120  // get extra event info and setup value cache
121  unsigned i = 0;
122  for (const auto& isolators_for_type : _isolation_types) {
123  the_values[i++].resize(isolators_for_type.size());
124  for (const auto& isolator : isolators_for_type) {
125  isolator->getEventInfo(ev);
126  }
127  }
128  reco::PFCandidate helper; // to translate pdg id to type
129  // loop over the candidates we are isolating and fill the values
130  for (size_t c = 0; c < to_isolate->size(); ++c) {
131  auto cand_to_isolate = to_isolate->ptrAt(c);
132  std::array<std::vector<float>, kNPFTypes> cand_values;
133  unsigned k = 0;
134  for (const auto& isolators_for_type : _isolation_types) {
135  cand_values[k].resize(isolators_for_type.size());
136  for (auto& value : cand_values[k])
137  value = 0.0;
138  ++k;
139  }
140  for (size_t ic = 0; ic < isolate_with->size(); ++ic) {
141  auto isocand = isolate_with->ptrAt(ic);
142  edm::Ptr<pat::PackedCandidate> aspackedCandidate(isocand);
143  auto isotype = helper.translatePdgIdToType(isocand->pdgId());
144  const auto& isolations = _isolation_types[isotype];
145  for (unsigned i = 0; i < isolations.size(); ++i) {
146  if (isolations[i]->isInIsolationCone(cand_to_isolate, isocand)) {
147  double puppiWeight = 0.;
149  puppiWeight = aspackedCandidate->puppiWeight(); // if miniAOD, take puppiWeight directly from the object
151  puppiWeight =
152  aspackedCandidate->puppiWeightNoLep(); // if miniAOD, take puppiWeightNoLep directly from the object
153  else
154  puppiWeight = (*puppiValueMap)[isocand]; // if AOD, take puppiWeight from the valueMap
155  if (puppiWeight > 0.)
156  cand_values[isotype][i] +=
157  (isocand->pt()) *
158  puppiWeight; // this is basically the main change to Lindsey's code: scale pt with puppiWeight for candidates with puppiWeight > 0.
159  }
160  }
161  }
162  // add this candidate to isolation value list
163  for (unsigned i = 0; i < kNPFTypes; ++i) {
164  for (unsigned j = 0; j < cand_values[i].size(); ++j) {
165  the_values[i][j].push_back(cand_values[i][j]);
166  }
167  }
168  }
169  // fill and put all products
170  for (unsigned i = 0; i < kNPFTypes; ++i) {
171  for (unsigned j = 0; j < the_values[i].size(); ++j) {
172  product_type the_product(new edm::ValueMap<float>);
173  edm::ValueMap<float>::Filler fillerprod(*the_product);
174  fillerprod.insert(to_isolate, the_values[i][j].begin(), the_values[i][j].end());
175  fillerprod.fill();
176  ev.put(std::move(the_product), _product_names[i][j]);
177  }
178  }
179  }
180 
181  // ParameterSet description for module
184  iDesc.setComment("PUPPI isolation sum producer");
185 
186  iDesc.add<edm::InputTag>("srcToIsolate", edm::InputTag("no default"))
187  ->setComment("calculate isolation for this collection");
188  iDesc.add<edm::InputTag>("srcForIsolationCone", edm::InputTag("no default"))
189  ->setComment("collection for the isolation calculation: like particleFlow ");
190  iDesc.add<edm::InputTag>("puppiValueMap", edm::InputTag("puppi"))
191  ->setComment("source for puppi, if left empty weight from packedCandidate is taken");
192 
193  edm::ParameterSetDescription descIsoConeDefinitions;
194  descIsoConeDefinitions.add<std::string>("isolationAlgo", "no default");
195  descIsoConeDefinitions.add<double>("coneSize", 0.3);
196  descIsoConeDefinitions.add<std::string>("isolateAgainst", "no default");
197  descIsoConeDefinitions.add<std::vector<unsigned>>("miniAODVertexCodes", {2, 3});
198  descIsoConeDefinitions.addOptional<double>("VetoConeSizeBarrel", 0.0);
199  descIsoConeDefinitions.addOptional<double>("VetoConeSizeEndcaps", 0.0);
200  descIsoConeDefinitions.addOptional<double>("VetoThreshold", 0.0);
201  descIsoConeDefinitions.addOptional<double>("VetoConeSize", 0.0);
202  descIsoConeDefinitions.addOptional<int>("vertexIndex", 0);
203  descIsoConeDefinitions.addOptional<edm::InputTag>("particleBasedIsolation", edm::InputTag("no default"))
204  ->setComment("map for footprint removal that is used for photons");
205 
206  std::vector<edm::ParameterSet> isolationConeDefinitions;
207  edm::ParameterSet chargedHadrons, neutralHadrons, photons;
208  isolationConeDefinitions.push_back(chargedHadrons);
209  isolationConeDefinitions.push_back(neutralHadrons);
211  iDesc.addVPSet("isolationConeDefinitions", descIsoConeDefinitions, isolationConeDefinitions);
212  iDesc.add<bool>("usePUPPINoLepton", false);
213 
214  descriptions.add("CITKPFIsolationSumProducerForPUPPI", iDesc);
215  }
216 
217 } // namespace citk
egmElectronIsolationPUPPI_cff.isolationConeDefinitions
isolationConeDefinitions
Definition: egmElectronIsolationPUPPI_cff.py:27
citk::PFIsolationSumProducerForPUPPI::PFIsolationSumProducerForPUPPI
PFIsolationSumProducerForPUPPI(const edm::ParameterSet &)
Definition: CITKPFIsolationSumProducerForPUPPI.cc:62
edm::helper::Filler::insert
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
mps_fire.i
i
Definition: mps_fire.py:355
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
citk::PFIsolationSumProducerForPUPPI::IsoTypes
std::vector< std::unique_ptr< IsolationConeDefinitionBase > > IsoTypes
Definition: CITKPFIsolationSumProducerForPUPPI.cc:42
PFCandidate.h
edm::LuminosityBlock
Definition: LuminosityBlock.h:50
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm::helper::Filler::fill
void fill()
Definition: ValueMap.h:65
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
citk::PFIsolationSumProducerForPUPPI::CandView
edm::View< reco::Candidate > CandView
Definition: CITKPFIsolationSumProducerForPUPPI.cc:43
EDProducer.h
citk::PFIsolationSumProducerForPUPPI::_product_names
std::array< std::vector< std::string >, kNPFTypes > _product_names
Definition: CITKPFIsolationSumProducerForPUPPI.cc:50
citk::PFIsolationSumProducerForPUPPI::_typeMap
const TypeMap _typeMap
Definition: CITKPFIsolationSumProducerForPUPPI.cc:44
EventDependentAbsVeto.h
edm::Handle
Definition: AssociativeIterator.h:50
edm::ParameterSetDescription::addOptional
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:105
citk::PFIsolationSumProducerForPUPPI::~PFIsolationSumProducerForPUPPI
~PFIsolationSumProducerForPUPPI() override
Definition: CITKPFIsolationSumProducerForPUPPI.cc:30
citk
Definition: CITKIsolationConeDefinitionBase.h:16
end
#define end
Definition: vmac.h:39
CITKPFIsolationSumProducerForPUPPI
citk::PFIsolationSumProducerForPUPPI CITKPFIsolationSumProducerForPUPPI
Definition: CITKPFIsolationSumProducerForPUPPI.cc:57
citk::PFIsolationSumProducerForPUPPI::puppiValueMap
edm::Handle< edm::ValueMap< float > > puppiValueMap
Definition: CITKPFIsolationSumProducerForPUPPI.cc:47
CandidateFwd.h
edm::InputTag::label
std::string const & label() const
Definition: InputTag.h:36
citk::PFIsolationSumProducerForPUPPI::useValueMapForPUPPI
bool useValueMapForPUPPI
Definition: CITKPFIsolationSumProducerForPUPPI.cc:51
MakerMacros.h
citk::PFIsolationSumProducerForPUPPI::beginLuminosityBlock
void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) final
Definition: CITKPFIsolationSumProducerForPUPPI.cc:100
TrackFwd.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
HLT_2018_cff.coneSize
coneSize
Definition: HLT_2018_cff.py:49353
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
MuonFwd.h
citk::PFIsolationSumProducerForPUPPI::produce
void produce(edm::Event &, const edm::EventSetup &) final
Definition: CITKPFIsolationSumProducerForPUPPI.cc:108
citk::PFIsolationSumProducerForPUPPI::TypeMap
std::unordered_map< std::string, int > TypeMap
Definition: CITKPFIsolationSumProducerForPUPPI.cc:41
citk::PFIsolationSumProducerForPUPPI::_isolation_types
std::array< IsoTypes, kNPFTypes > _isolation_types
Definition: CITKPFIsolationSumProducerForPUPPI.cc:49
dqmdumpme.k
k
Definition: dqmdumpme.py:60
unpackData-CaloStage2.pname
pname
Definition: unpackData-CaloStage2.py:76
IsoDeposit.h
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::View::size
size_type size() const
citk::PFIsolationSumProducerForPUPPI::usePUPPINoLepton
bool usePUPPINoLepton
Definition: CITKPFIsolationSumProducerForPUPPI.cc:52
edm::View
Definition: CaloClusterFwd.h:14
HLT_2018_cff.InputTag
InputTag
Definition: HLT_2018_cff.py:79016
edm::ParameterSet
Definition: ParameterSet.h:36
edm::ParameterSetDescription::setComment
void setComment(std::string const &value)
Definition: ParameterSetDescription.cc:33
pat::PackedCandidate::puppiWeight
float puppiWeight() const
Definition: PackedCandidate.cc:380
Event.h
CITKIsolationConeDefinitionBase.h
edm::ParameterSetDescription::addVPSet
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
Definition: ParameterSetDescription.h:149
PackedCandidate.h
helper
Definition: helper.py:1
value
Definition: value.py:1
edm::stream::EDProducer
Definition: EDProducer.h:38
citk::PFIsolationSumProducerForPUPPI::_to_isolate
edm::EDGetTokenT< CandView > _to_isolate
Definition: CITKPFIsolationSumProducerForPUPPI.cc:45
BPHMonitor_cfi.photons
photons
Definition: BPHMonitor_cfi.py:91
edm::EventSetup
Definition: EventSetup.h:57
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
get
#define get
visDQMUpload.buf
buf
Definition: visDQMUpload.py:154
egmElectronIsolationPUPPI_cff.usePUPPINoLepton
usePUPPINoLepton
Definition: egmElectronIsolationPUPPI_cff.py:41
edm::Ptr
Definition: AssociationVector.h:31
eostools.move
def move(src, dest)
Definition: eostools.py:511
edm::ValueMap< float >
ev
bool ev
Definition: Hydjet2Hadronizer.cc:95
Exception
Definition: hltDiff.cc:246
citk::PFIsolationSumProducerForPUPPI::kNPFTypes
static constexpr unsigned kNPFTypes
Definition: CITKPFIsolationSumProducerForPUPPI.cc:40
citk::PFIsolationSumProducerForPUPPI
Definition: CITKPFIsolationSumProducerForPUPPI.cc:26
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
EventSetup.h
reco::PFCandidate
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
ConsumesCollector.h
Candidate.h
IsoDepositFwd.h
edm::helper::Filler
Definition: ValueMap.h:22
citk::PFIsolationSumProducerForPUPPI::puppiValueMapToken_
edm::EDGetTokenT< edm::ValueMap< float > > puppiValueMapToken_
Definition: CITKPFIsolationSumProducerForPUPPI.cc:46
ParameterSet.h
citk::PFIsolationSumProducerForPUPPI::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: CITKPFIsolationSumProducerForPUPPI.cc:182
OwnVector.h
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
edm::Event
Definition: Event.h:73
edm::View::ptrAt
Ptr< value_type > ptrAt(size_type i) const
edm::InputTag
Definition: InputTag.h:15
begin
#define begin
Definition: vmac.h:32
citk::PFIsolationSumProducerForPUPPI::_isolate_with
edm::EDGetTokenT< CandView > _isolate_with
Definition: CITKPFIsolationSumProducerForPUPPI.cc:45
pat::PackedCandidate::puppiWeightNoLep
float puppiWeightNoLep() const
Weight from full PUPPI.
Definition: PackedCandidate.cc:384