CMS 3D CMS Logo

CITKPFIsolationSumProducer.cc
Go to the documentation of this file.
1 #ifndef PhysicsTools_IsolationAlgos_CITKIsolationSumProducer_H
2 #define PhysicsTools_IsolationAlgos_CITKIsolationSumProducer_H
3 
7 
15 
20 
24 
25 #include <string>
26 #include <unordered_map>
27 
28 namespace edm { class Event; }
29 namespace edm { class EventSetup; }
30 
31 namespace citk {
33 
34  public:
36 
38 
39  void beginLuminosityBlock(const edm::LuminosityBlock&,
40  const edm::EventSetup&) final;
41 
42  void produce(edm::Event&, const edm::EventSetup&) final;
43 
44  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
45 
46  private:
47  // datamembers
48  static constexpr unsigned kNPFTypes = 8;
49  typedef std::unordered_map<std::string,int> TypeMap;
50  typedef std::vector<std::unique_ptr<IsolationConeDefinitionBase> > IsoTypes;
52  const TypeMap _typeMap;
54  // indexed by pf candidate type
55  std::array<IsoTypes,kNPFTypes> _isolation_types;
56  std::array<std::vector<std::string>,kNPFTypes> _product_names;
57  };
58 }
59 
61 
63 
64 namespace citk {
65  PFIsolationSumProducer::PFIsolationSumProducer(const edm::ParameterSet& c) :
66  _typeMap( { {"h+",1},
67  {"h0",5},
68  {"gamma",4},
69  {"electron",2},
70  {"muon",3},
71  {"HFh",6},
72  {"HFgamma",7} } ){
73  _to_isolate =
74  consumes<CandView>(c.getParameter<edm::InputTag>("srcToIsolate"));
75  _isolate_with =
76  consumes<CandView>(c.getParameter<edm::InputTag>("srcForIsolationCone"));
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  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::unique_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);
125  // the list of value vectors indexed as "to_isolate"
126  std::array<std::vector<product_values>,kNPFTypes> the_values;
127  // get extra event info and setup value cache
128  unsigned i = 0;
129  for( const auto& isolators_for_type : _isolation_types ) {
130  the_values[i++].resize(isolators_for_type.size());
131  for( const auto& isolator : isolators_for_type ) {
132  isolator->getEventInfo(ev);
133  }
134  }
135  reco::PFCandidate helper; // to translate pdg id to type
136  // loop over the candidates we are isolating and fill the values
137  for( size_t c = 0; c < to_isolate->size(); ++c ) {
138  auto cand_to_isolate = to_isolate->ptrAt(c);
139  std::array<std::vector<float>,kNPFTypes> cand_values;
140  unsigned k = 0;
141  for( const auto& isolators_for_type : _isolation_types ) {
142  cand_values[k].resize(isolators_for_type.size());
143  for( auto& value : cand_values[k] ) value = 0.0;
144  ++k;
145  }
146  for( size_t ic = 0; ic < isolate_with->size(); ++ic ) {
147  auto isocand = isolate_with->ptrAt(ic);
148  auto isotype = helper.translatePdgIdToType(isocand->pdgId());
149  const auto& isolations = _isolation_types[isotype];
150  for( unsigned i = 0; i < isolations.size(); ++ i ) {
151  if( isolations[i]->isInIsolationCone(cand_to_isolate,isocand) ) {
152  cand_values[isotype][i] += isocand->pt();
153  }
154  }
155  }
156  // add this candidate to isolation value list
157  for( unsigned i = 0; i < kNPFTypes; ++i ) {
158  for( unsigned j = 0; j < cand_values[i].size(); ++j ) {
159  the_values[i][j].push_back(cand_values[i][j]);
160  }
161  }
162  }
163  // fill and put all products
164  for( unsigned i = 0; i < kNPFTypes; ++ i ) {
165  for( unsigned j = 0; j < the_values[i].size(); ++j ) {
166  product_type the_product( new edm::ValueMap<float> );
167  edm::ValueMap<float>::Filler fillerprod(*the_product);
168  fillerprod.insert(to_isolate,
169  the_values[i][j].begin(),
170  the_values[i][j].end());
171  fillerprod.fill();
172  ev.put(std::move(the_product),_product_names[i][j]);
173  }
174  }
175  }
176 
177 // ParameterSet description for module
180  iDesc.setComment("isolation sum producer");
181 
182  iDesc.add<edm::InputTag>("srcToIsolate", edm::InputTag("no default"))->setComment("calculate isolation for this collection");
183  iDesc.add<edm::InputTag>("srcForIsolationCone", edm::InputTag("no default"))->setComment("collection for the isolation calculation: like particleFlow ");
184 
185  edm::ParameterSetDescription descIsoConeDefinitions;
186  descIsoConeDefinitions.add<std::string>("isolationAlgo", "no default");
187  descIsoConeDefinitions.add<double>("coneSize", 0.3);
188  descIsoConeDefinitions.add<std::string>("isolateAgainst", "no default");
189  descIsoConeDefinitions.add<std::vector<unsigned>>("miniAODVertexCodes", {2,3});
190  descIsoConeDefinitions.addOptional<double>("VetoConeSizeBarrel", 0.0);
191  descIsoConeDefinitions.addOptional<double>("VetoConeSizeEndcaps", 0.0);
192  descIsoConeDefinitions.addOptional<int>("vertexIndex",0);
193  descIsoConeDefinitions.addOptional<edm::InputTag>("particleBasedIsolation",edm::InputTag("no default"))->setComment("map for footprint removal that is used for photons");
194 
195 
196  std::vector<edm::ParameterSet> isolationConeDefinitions;
197  edm::ParameterSet chargedHadrons, neutralHadrons,photons;
198  isolationConeDefinitions.push_back(chargedHadrons);
199  isolationConeDefinitions.push_back(neutralHadrons);
200  isolationConeDefinitions.push_back(photons);
201  iDesc.addVPSet("isolationConeDefinitions", descIsoConeDefinitions, isolationConeDefinitions);
202 
203  descriptions.add("CITKPFIsolationSumProducer", iDesc);
204  }
205 }
206 
207 #endif
std::array< std::vector< std::string >, kNPFTypes > _product_names
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
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)
std::vector< std::unique_ptr< IsolationConeDefinitionBase > > IsoTypes
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
Ptr< value_type > ptrAt(size_type i) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
citk::PFIsolationSumProducer CITKPFIsolationSumProducer
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
size_type size() const
bool ev
#define constexpr
void setComment(std::string const &value)
edm::View< reco::Candidate > CandView
void produce(edm::Event &, const edm::EventSetup &) final
edm::EDGetTokenT< CandView > _to_isolate
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::unordered_map< std::string, int > TypeMap
#define end
Definition: vmac.h:39
Definition: value.py:1
ParameterDescriptionBase * add(U const &iLabel, T const &value)
int k[5][pyjets_maxn]
void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) final
void add(std::string const &label, ParameterSetDescription const &psetDescription)
ParticleType translatePdgIdToType(int pdgid) const
Definition: PFCandidate.cc:233
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
#define begin
Definition: vmac.h:32
HLT enums.
def move(src, dest)
Definition: eostools.py:510
T get(const Candidate &c)
Definition: component.h:55
edm::EDGetTokenT< CandView > _isolate_with
std::array< IsoTypes, kNPFTypes > _isolation_types