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