test
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  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;
43  typedef edm::View<reco::Candidate> CandView;
44  const TypeMap _typeMap;
45  edm::EDGetTokenT<CandView> _to_isolate, _isolate_with;
46  edm::EDGetTokenT<edm::ValueMap<float> > puppiValueMapToken_;//for puppiValueMap
47  edm::Handle<edm::ValueMap<float>> puppiValueMap;//puppiValueMap
48  // indexed by pf candidate type
49  std::array<IsoTypes,kNPFTypes> _isolation_types;
50  std::array<std::vector<std::string>,kNPFTypes> _product_names;
52  };
53 }
54 
56 
57 DEFINE_FWK_MODULE(CITKPFIsolationSumProducerForPUPPI);
58 
59 namespace citk {
61  _typeMap( { {"h+",1},
62  {"h0",5},
63  {"gamma",4},
64  {"electron",2},
65  {"muon",3},
66  {"HFh",6},
67  {"HFgamma",7} } ){
68  _to_isolate =
69  consumes<CandView>(c.getParameter<edm::InputTag>("srcToIsolate"));
70  _isolate_with =
71  consumes<CandView>(c.getParameter<edm::InputTag>("srcForIsolationCone"));
72  if (c.getParameter<edm::InputTag>("puppiValueMap").label().size() != 0) {
73  puppiValueMapToken_ = mayConsume<edm::ValueMap<float>>(c.getParameter<edm::InputTag>("puppiValueMap")); //getting token for puppiValueMap
74  useValueMapForPUPPI = true;
75  }
76  else useValueMapForPUPPI = false;
77  const std::vector<edm::ParameterSet>& isoDefs =
78  c.getParameterSetVector("isolationConeDefinitions");
79  for( const auto& isodef : isoDefs ) {
80  const std::string& name =
81  isodef.getParameter<std::string>("isolationAlgo");
82  const float coneSize = isodef.getParameter<double>("coneSize");
83  char buf[50];
84  std::sprintf(buf,"DR%.2f",coneSize);
85  std::string coneName(buf);
86  auto decimal = coneName.find('.');
87  if( decimal != std::string::npos ) coneName.erase(decimal,1);
88  const std::string& isotype =
89  isodef.getParameter<std::string>("isolateAgainst");
90  IsolationConeDefinitionBase* theisolator =
91  CITKIsolationConeDefinitionFactory::get()->create(name,isodef);
92  theisolator->setConsumes(consumesCollector());
93  const auto thetype = _typeMap.find(isotype);
94  if( thetype == _typeMap.end() ) {
95  throw cms::Exception("InvalidIsolationType")
96  << "Isolation type: " << isotype << " is not available in the "
97  << "list of allowed isolations!.";
98  }
99  _isolation_types[thetype->second].emplace_back(theisolator);
100  const std::string dash("-");
101  std::string pname = isotype+dash+coneName+dash+theisolator->additionalCode();
102  _product_names[thetype->second].emplace_back(pname);
103  produces<edm::ValueMap<float> >(pname);
104  }
105  }
106 
109  const edm::EventSetup& es) {
110  for( const auto& isolators_for_type : _isolation_types ) {
111  for( const auto& isolator : isolators_for_type ) {
112  isolator->getEventSetupInfo(es);
113  }
114  }
115  }
116 
119  typedef std::auto_ptr<edm::ValueMap<float> > product_type;
120  typedef std::vector<float> product_values;
121  edm::Handle<CandView> to_isolate;
122  edm::Handle<CandView> isolate_with;
123  ev.getByToken(_to_isolate,to_isolate);
124  ev.getByToken(_isolate_with,isolate_with);
126 
127  // the list of value vectors indexed as "to_isolate"
128  std::array<std::vector<product_values>,kNPFTypes> the_values;
129  // get extra event info and setup value cache
130  unsigned i = 0;
131  for( const auto& isolators_for_type : _isolation_types ) {
132  the_values[i++].resize(isolators_for_type.size());
133  for( const auto& isolator : isolators_for_type ) {
134  isolator->getEventInfo(ev);
135  }
136  }
137  reco::PFCandidate helper; // to translate pdg id to type
138  // loop over the candidates we are isolating and fill the values
139  for( size_t c = 0; c < to_isolate->size(); ++c ) {
140  auto cand_to_isolate = to_isolate->ptrAt(c);
141  std::array<std::vector<float>,kNPFTypes> cand_values;
142  unsigned k = 0;
143  for( const auto& isolators_for_type : _isolation_types ) {
144  cand_values[k].resize(isolators_for_type.size());
145  for( auto& value : cand_values[k] ) value = 0.0;
146  ++k;
147  }
148  for( size_t ic = 0; ic < isolate_with->size(); ++ic ) {
149  auto isocand = isolate_with->ptrAt(ic);
150  edm::Ptr<pat::PackedCandidate> aspackedCandidate(isocand);
151  auto isotype = helper.translatePdgIdToType(isocand->pdgId());
152  const auto& isolations = _isolation_types[isotype];
153  for( unsigned i = 0; i < isolations.size(); ++ i ) {
154  if( isolations[i]->isInIsolationCone(cand_to_isolate,isocand) ) {
155  double puppiWeight = 0.;
156  if (!useValueMapForPUPPI) puppiWeight = aspackedCandidate -> puppiWeight(); // if miniAOD, take puppiWeight directly from the object
157  else puppiWeight = (*puppiValueMap)[isocand]; // if AOD, take puppiWeight from the valueMap
158  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.
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,
175  the_values[i][j].begin(),
176  the_values[i][j].end());
177  fillerprod.fill();
178  ev.put(the_product,_product_names[i][j]);
179  }
180  }
181  }
182 }
int i
Definition: DBlmapReader.cc:9
tuple array
Definition: mps_check.py:181
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
#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
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
int j
Definition: DBlmapReader.cc:9
#define end
Definition: vmac.h:37
virtual void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) overridefinal
string const
Definition: compareJSON.py:14
ParticleType translatePdgIdToType(int pdgid) const
Definition: PFCandidate.cc:224
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
T get(const Candidate &c)
Definition: component.h:55