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 
32  void produce(edm::Event&, const edm::EventSetup&) final;
33 
34  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
35 
36  private:
37  // datamembers
38  static constexpr unsigned kNPFTypes = 8;
39  typedef std::unordered_map<std::string, int> TypeMap;
40  typedef std::vector<std::unique_ptr<IsolationConeDefinitionBase>> IsoTypes;
46  // indexed by pf candidate type
47  std::array<IsoTypes, kNPFTypes> _isolation_types;
48  std::array<std::vector<std::string>, kNPFTypes> _product_names;
49  bool useValueMapForPUPPI = true;
51  false; // in case puppi weights are taken from packedCandidate can take weights for puppiNoLeptons
52  };
53 } // namespace citk
54 
56 
58 
59 namespace citk {
61  : _typeMap({{"h+", 1}, {"h0", 5}, {"gamma", 4}, {"electron", 2}, {"muon", 3}, {"HFh", 6}, {"HFgamma", 7}}) {
62  _to_isolate = consumes<CandView>(c.getParameter<edm::InputTag>("srcToIsolate"));
63  _isolate_with = consumes<CandView>(c.getParameter<edm::InputTag>("srcForIsolationCone"));
64  if (!c.getParameter<edm::InputTag>("puppiValueMap").label().empty()) {
65  puppiValueMapToken_ = mayConsume<edm::ValueMap<float>>(
66  c.getParameter<edm::InputTag>("puppiValueMap")); //getting token for puppiValueMap
67  useValueMapForPUPPI = true;
68  } else {
69  useValueMapForPUPPI = false;
70  usePUPPINoLepton = c.getParameter<bool>("usePUPPINoLepton");
71  }
72  const std::vector<edm::ParameterSet>& isoDefs = c.getParameterSetVector("isolationConeDefinitions");
73  for (const auto& isodef : isoDefs) {
74  const std::string& name = isodef.getParameter<std::string>("isolationAlgo");
75  const float coneSize = isodef.getParameter<double>("coneSize");
76  char buf[50];
77  std::sprintf(buf, "DR%.2f", coneSize);
78  std::string coneName(buf);
79  auto decimal = coneName.find('.');
80  if (decimal != std::string::npos)
81  coneName.erase(decimal, 1);
82  const std::string& isotype = isodef.getParameter<std::string>("isolateAgainst");
83  auto theisolator = CITKIsolationConeDefinitionFactory::get()->create(name, isodef);
84  theisolator->setConsumes(consumesCollector());
85  const auto thetype = _typeMap.find(isotype);
86  if (thetype == _typeMap.end()) {
87  throw cms::Exception("InvalidIsolationType") << "Isolation type: " << isotype << " is not available in the "
88  << "list of allowed isolations!.";
89  }
90  const std::string dash("-");
91  std::string pname = isotype + dash + coneName + dash + theisolator->additionalCode();
92  _product_names[thetype->second].emplace_back(pname);
93  produces<edm::ValueMap<float>>(pname);
94  _isolation_types[thetype->second].emplace_back(std::move(theisolator));
95  }
96  }
97 
99  for (const auto& isolators_for_type : _isolation_types) {
100  for (const auto& isolator : isolators_for_type) {
101  isolator->getEventSetupInfo(es);
102  }
103  }
104  }
105 
107  typedef std::unique_ptr<edm::ValueMap<float>> product_type;
108  typedef std::vector<float> product_values;
109  edm::Handle<CandView> to_isolate;
110  edm::Handle<CandView> isolate_with;
111  ev.getByToken(_to_isolate, to_isolate);
112  ev.getByToken(_isolate_with, isolate_with);
114  ev.getByToken(puppiValueMapToken_, puppiValueMap);
115 
116  // the list of value vectors indexed as "to_isolate"
117  std::array<std::vector<product_values>, kNPFTypes> the_values;
118  // get extra event info and setup value cache
119  unsigned i = 0;
120  for (const auto& isolators_for_type : _isolation_types) {
121  the_values[i++].resize(isolators_for_type.size());
122  for (const auto& isolator : isolators_for_type) {
123  isolator->getEventInfo(ev);
124  }
125  }
126  reco::PFCandidate helper; // to translate pdg id to type
127  // loop over the candidates we are isolating and fill the values
128  for (size_t c = 0; c < to_isolate->size(); ++c) {
129  auto cand_to_isolate = to_isolate->ptrAt(c);
130  std::array<std::vector<float>, kNPFTypes> cand_values;
131  unsigned k = 0;
132  for (const auto& isolators_for_type : _isolation_types) {
133  cand_values[k].resize(isolators_for_type.size());
134  for (auto& value : cand_values[k])
135  value = 0.0;
136  ++k;
137  }
138  for (size_t ic = 0; ic < isolate_with->size(); ++ic) {
139  auto isocand = isolate_with->ptrAt(ic);
140  edm::Ptr<pat::PackedCandidate> aspackedCandidate;
141  auto isotype = helper.translatePdgIdToType(isocand->pdgId());
142  const auto& isolations = _isolation_types[isotype];
143  for (unsigned i = 0; i < isolations.size(); ++i) {
144  if (isolations[i]->isInIsolationCone(cand_to_isolate, isocand)) {
145  double puppiWeight = 0.;
146  if (!useValueMapForPUPPI && aspackedCandidate.isNull())
147  aspackedCandidate = edm::Ptr<pat::PackedCandidate>(isocand);
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
float puppiWeight() const
std::unordered_map< std::string, int > TypeMap
citk::PFIsolationSumProducerForPUPPI CITKPFIsolationSumProducerForPUPPI
float puppiWeightNoLep() const
Weight from full PUPPI.
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
Definition: helper.py:1
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
Ptr< value_type > ptrAt(size_type i) const
void produce(edm::Event &, const edm::EventSetup &) final
edm::Handle< edm::ValueMap< float > > puppiValueMap
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
std::array< std::vector< std::string >, kNPFTypes > _product_names
std::string const & label() const
Definition: InputTag.h:36
edm::EDGetTokenT< edm::ValueMap< float > > puppiValueMapToken_
size_type size() const
void setComment(std::string const &value)
bool isNull() const
Checks for null.
Definition: Ptr.h:142
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) final
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Definition: value.py:1
ParameterDescriptionBase * add(U const &iLabel, T const &value)
TracksUtilities< TrackerTraits > helper
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
std::vector< std::unique_ptr< IsolationConeDefinitionBase > > IsoTypes
#define get
def move(src, dest)
Definition: eostools.py:511