CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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;
45  typedef edm::View<reco::Candidate> CandView;
46  const TypeMap _typeMap;
47  edm::EDGetTokenT<CandView> _to_isolate, _isolate_with;
48  edm::EDGetTokenT<edm::ValueMap<float> > puppiValueMapToken_;//for puppiValueMap
49  edm::Handle<edm::ValueMap<float>> puppiValueMap;//puppiValueMap
50  // indexed by pf candidate type
51  std::array<IsoTypes,kNPFTypes> _isolation_types;
52  std::array<std::vector<std::string>,kNPFTypes> _product_names;
54  bool usePUPPINoLepton = false;// in case puppi weights are taken from packedCandidate can take weights for puppiNoLeptons
55  };
56 }
57 
59 
60 DEFINE_FWK_MODULE(CITKPFIsolationSumProducerForPUPPI);
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 useValueMapForPUPPI = false;
80  const std::vector<edm::ParameterSet>& isoDefs =
81  c.getParameterSetVector("isolationConeDefinitions");
82  for( const auto& isodef : isoDefs ) {
83  const std::string& name =
84  isodef.getParameter<std::string>("isolationAlgo");
85  const float coneSize = isodef.getParameter<double>("coneSize");
86  char buf[50];
87  std::sprintf(buf,"DR%.2f",coneSize);
88  std::string coneName(buf);
89  auto decimal = coneName.find('.');
90  if( decimal != std::string::npos ) coneName.erase(decimal,1);
91  const std::string& isotype =
92  isodef.getParameter<std::string>("isolateAgainst");
93  IsolationConeDefinitionBase* theisolator =
94  CITKIsolationConeDefinitionFactory::get()->create(name,isodef);
95  theisolator->setConsumes(consumesCollector());
96  const auto thetype = _typeMap.find(isotype);
97  if( thetype == _typeMap.end() ) {
98  throw cms::Exception("InvalidIsolationType")
99  << "Isolation type: " << isotype << " is not available in the "
100  << "list of allowed isolations!.";
101  }
102  _isolation_types[thetype->second].emplace_back(theisolator);
103  const std::string dash("-");
104  std::string pname = isotype+dash+coneName+dash+theisolator->additionalCode();
105  _product_names[thetype->second].emplace_back(pname);
106  produces<edm::ValueMap<float> >(pname);
107  }
108  }
109 
112  const edm::EventSetup& es) {
113  for( const auto& isolators_for_type : _isolation_types ) {
114  for( const auto& isolator : isolators_for_type ) {
115  isolator->getEventSetupInfo(es);
116  }
117  }
118  }
119 
122  typedef std::unique_ptr<edm::ValueMap<float> > product_type;
123  typedef std::vector<float> product_values;
124  edm::Handle<CandView> to_isolate;
125  edm::Handle<CandView> isolate_with;
126  ev.getByToken(_to_isolate,to_isolate);
127  ev.getByToken(_isolate_with,isolate_with);
129 
130  // the list of value vectors indexed as "to_isolate"
131  std::array<std::vector<product_values>,kNPFTypes> the_values;
132  // get extra event info and setup value cache
133  unsigned i = 0;
134  for( const auto& isolators_for_type : _isolation_types ) {
135  the_values[i++].resize(isolators_for_type.size());
136  for( const auto& isolator : isolators_for_type ) {
137  isolator->getEventInfo(ev);
138  }
139  }
140  reco::PFCandidate helper; // to translate pdg id to type
141  // loop over the candidates we are isolating and fill the values
142  for( size_t c = 0; c < to_isolate->size(); ++c ) {
143  auto cand_to_isolate = to_isolate->ptrAt(c);
144  std::array<std::vector<float>,kNPFTypes> cand_values;
145  unsigned k = 0;
146  for( const auto& isolators_for_type : _isolation_types ) {
147  cand_values[k].resize(isolators_for_type.size());
148  for( auto& value : cand_values[k] ) value = 0.0;
149  ++k;
150  }
151  for( size_t ic = 0; ic < isolate_with->size(); ++ic ) {
152  auto isocand = isolate_with->ptrAt(ic);
153  edm::Ptr<pat::PackedCandidate> aspackedCandidate(isocand);
154  auto isotype = helper.translatePdgIdToType(isocand->pdgId());
155  const auto& isolations = _isolation_types[isotype];
156  for( unsigned i = 0; i < isolations.size(); ++ i ) {
157  if( isolations[i]->isInIsolationCone(cand_to_isolate,isocand) ) {
158  double puppiWeight = 0.;
159  if (!useValueMapForPUPPI && !usePUPPINoLepton) puppiWeight = aspackedCandidate -> puppiWeight(); // if miniAOD, take puppiWeight directly from the object
160  else if (!useValueMapForPUPPI && usePUPPINoLepton) puppiWeight = aspackedCandidate -> puppiWeightNoLep(); // if miniAOD, take puppiWeightNoLep directly from the object
161  else puppiWeight = (*puppiValueMap)[isocand]; // if AOD, take puppiWeight from the valueMap
162  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.
163  }
164  }
165  }
166  // add this candidate to isolation value list
167  for( unsigned i = 0; i < kNPFTypes; ++i ) {
168  for( unsigned j = 0; j < cand_values[i].size(); ++j ) {
169  the_values[i][j].push_back(cand_values[i][j]);
170  }
171  }
172  }
173  // fill and put all products
174  for( unsigned i = 0; i < kNPFTypes; ++ i ) {
175  for( unsigned j = 0; j < the_values[i].size(); ++j ) {
176  product_type the_product( new edm::ValueMap<float> );
177  edm::ValueMap<float>::Filler fillerprod(*the_product);
178  fillerprod.insert(to_isolate,
179  the_values[i][j].begin(),
180  the_values[i][j].end());
181  fillerprod.fill();
182  ev.put(std::move(the_product),_product_names[i][j]);
183  }
184  }
185  }
186 
187 
188 // ParameterSet description for module
190 {
192  iDesc.setComment("PUPPI isolation sum producer");
193 
194  iDesc.add<edm::InputTag>("srcToIsolate", edm::InputTag("no default"))->setComment("calculate isolation for this collection");
195  iDesc.add<edm::InputTag>("srcForIsolationCone", edm::InputTag("no default"))->setComment("collection for the isolation calculation: like particleFlow ");
196  iDesc.add<edm::InputTag>("puppiValueMap", edm::InputTag("puppi"))->setComment("source for puppi, if left empty weight from packedCandidate is taken");
197 
198  edm::ParameterSetDescription descIsoConeDefinitions;
199  descIsoConeDefinitions.add<std::string>("isolationAlgo", "no default");
200  descIsoConeDefinitions.add<double>("coneSize", 0.3);
201  descIsoConeDefinitions.add<std::string>("isolateAgainst", "no default");
202  descIsoConeDefinitions.add<std::vector<unsigned>>("miniAODVertexCodes", {2,3});
203  descIsoConeDefinitions.addOptional<double>("VetoConeSizeBarrel", 0.0);
204  descIsoConeDefinitions.addOptional<double>("VetoConeSizeEndcaps", 0.0);
205  descIsoConeDefinitions.addOptional<int>("vertexIndex",0);
206  descIsoConeDefinitions.addOptional<edm::InputTag>("particleBasedIsolation",edm::InputTag("no default"))->setComment("map for footprint removal that is used for photons");
207 
208 
209  std::vector<edm::ParameterSet> isolationConeDefinitions;
210  edm::ParameterSet chargedHadrons, neutralHadrons,photons;
211  isolationConeDefinitions.push_back(chargedHadrons);
212  isolationConeDefinitions.push_back(neutralHadrons);
213  isolationConeDefinitions.push_back(photons);
214  iDesc.addVPSet("isolationConeDefinitions", descIsoConeDefinitions, isolationConeDefinitions);
215  iDesc.addOptional<bool>("usePUPPINoLepton",false);
216 
217  descriptions.add("CITKPFIsolationSumProducerForPUPPI", iDesc);
218 }
219 
220 }
int i
Definition: DBlmapReader.cc:9
tuple array
Definition: mps_check.py:181
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
const std::vector< edm::ParameterSet > & isoDefs
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:52
bool ev
edm::EDGetTokenT< edm::ValueMap< float > > puppiValueMapToken_
#define constexpr
Definition: Event.h:16
std::unordered_map< std::string, int > TypeMap
void setComment(std::string const &value)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
def move
Definition: eostools.py:510
int j
Definition: DBlmapReader.cc:9
#define end
Definition: vmac.h:37
ParameterDescriptionBase * add(U const &iLabel, T const &value)
virtual void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) overridefinal
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
virtual void produce(edm::Event &, const edm::EventSetup &) overridefinal
volatile std::atomic< bool > shutdown_flag false
T get(const Candidate &c)
Definition: component.h:55