CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
40  const edm::EventSetup&) override final;
41 
42  void produce(edm::Event&, const edm::EventSetup&) override final;
43 
44  private:
45  // datamembers
46  static constexpr unsigned kNPFTypes = 8;
47  typedef std::unordered_map<std::string,int> TypeMap;
48  typedef std::vector<std::unique_ptr<IsolationConeDefinitionBase> > IsoTypes;
49  typedef edm::View<reco::Candidate> CandView;
50  const TypeMap _typeMap;
51  edm::EDGetTokenT<CandView> _to_isolate, _isolate_with;
52  // indexed by pf candidate type
53  std::array<IsoTypes,kNPFTypes> _isolation_types;
54  std::array<std::vector<std::string>,kNPFTypes> _product_names;
55  };
56 }
57 
59 
60 DEFINE_FWK_MODULE(CITKPFIsolationSumProducer);
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  const std::vector<edm::ParameterSet>& isoDefs =
76  c.getParameterSetVector("isolationConeDefinitions");
77  for( const auto& isodef : isoDefs ) {
78  const std::string& name =
79  isodef.getParameter<std::string>("isolationAlgo");
80  const float coneSize = isodef.getParameter<double>("coneSize");
81  char buf[50];
82  sprintf(buf,"DR%.2f",coneSize);
83  std::string coneName(buf);
84  auto decimal = coneName.find('.');
85  if( decimal != std::string::npos ) coneName.erase(decimal,1);
86  const std::string& isotype =
87  isodef.getParameter<std::string>("isolateAgainst");
88  IsolationConeDefinitionBase* theisolator =
89  CITKIsolationConeDefinitionFactory::get()->create(name,isodef);
90  theisolator->setConsumes(consumesCollector());
91  const auto thetype = _typeMap.find(isotype);
92  if( thetype == _typeMap.end() ) {
93  throw cms::Exception("InvalidIsolationType")
94  << "Isolation type: " << isotype << " is not available in the "
95  << "list of allowed isolations!.";
96  }
97  _isolation_types[thetype->second].emplace_back(theisolator);
98  const std::string dash("-");
99  std::string pname = isotype+dash+coneName+dash+theisolator->additionalCode();
100  _product_names[thetype->second].emplace_back(pname);
101  produces<edm::ValueMap<float> >(pname);
102  }
103  }
104 
107  const edm::EventSetup& es) {
108  for( const auto& isolators_for_type : _isolation_types ) {
109  for( const auto& isolator : isolators_for_type ) {
110  isolator->getEventSetupInfo(es);
111  }
112  }
113  }
114 
117  typedef std::auto_ptr<edm::ValueMap<float> > product_type;
118  typedef std::vector<float> product_values;
119  edm::Handle<CandView> to_isolate;
120  edm::Handle<CandView> isolate_with;
121  ev.getByToken(_to_isolate,to_isolate);
122  ev.getByToken(_isolate_with,isolate_with);
123  // the list of value vectors indexed as "to_isolate"
124  std::array<std::vector<product_values>,kNPFTypes> the_values;
125  // get extra event info and setup value cache
126  unsigned i = 0;
127  for( const auto& isolators_for_type : _isolation_types ) {
128  the_values[i++].resize(isolators_for_type.size());
129  for( const auto& isolator : isolators_for_type ) {
130  isolator->getEventInfo(ev);
131  }
132  }
133  reco::PFCandidate helper; // to translate pdg id to type
134  // loop over the candidates we are isolating and fill the values
135  for( size_t c = 0; c < to_isolate->size(); ++c ) {
136  auto cand_to_isolate = to_isolate->ptrAt(c);
137  std::array<std::vector<float>,kNPFTypes> cand_values;
138  unsigned k = 0;
139  for( const auto& isolators_for_type : _isolation_types ) {
140  cand_values[k].resize(isolators_for_type.size());
141  for( auto& value : cand_values[k] ) value = 0.0;
142  ++k;
143  }
144  for( size_t ic = 0; ic < isolate_with->size(); ++ic ) {
145  auto isocand = isolate_with->ptrAt(ic);
146  auto isotype = helper.translatePdgIdToType(isocand->pdgId());
147  const auto& isolations = _isolation_types[isotype];
148  for( unsigned i = 0; i < isolations.size(); ++ i ) {
149  if( isolations[i]->isInIsolationCone(cand_to_isolate,isocand) ) {
150  cand_values[isotype][i] += isocand->pt();
151  }
152  }
153  }
154  // add this candidate to isolation value list
155  for( unsigned i = 0; i < kNPFTypes; ++i ) {
156  for( unsigned j = 0; j < cand_values[i].size(); ++j ) {
157  the_values[i][j].push_back(cand_values[i][j]);
158  }
159  }
160  }
161  // fill and put all products
162  for( unsigned i = 0; i < kNPFTypes; ++ i ) {
163  for( unsigned j = 0; j < the_values[i].size(); ++j ) {
164  product_type the_product( new edm::ValueMap<float> );
165  edm::ValueMap<float>::Filler fillerprod(*the_product);
166  fillerprod.insert(to_isolate,
167  the_values[i][j].begin(),
168  the_values[i][j].end());
169  fillerprod.fill();
170  ev.put(the_product,_product_names[i][j]);
171  }
172  }
173  }
174 }
175 
176 #endif
int i
Definition: DBlmapReader.cc:9
tuple array
Definition: mps_check.py:181
std::array< std::vector< std::string >, kNPFTypes > _product_names
PFIsolationSumProducer(const edm::ParameterSet &)
std::vector< std::unique_ptr< IsolationConeDefinitionBase > > IsoTypes
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
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
bool ev
void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) overridefinal
#define constexpr
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
edm::EDGetTokenT< CandView > _to_isolate
int j
Definition: DBlmapReader.cc:9
std::unordered_map< std::string, int > TypeMap
#define end
Definition: vmac.h:37
string const
Definition: compareJSON.py:14
void produce(edm::Event &, const edm::EventSetup &) overridefinal
ParticleType translatePdgIdToType(int pdgid) const
Definition: PFCandidate.cc:224
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
#define begin
Definition: vmac.h:30
T get(const Candidate &c)
Definition: component.h:55
edm::EDGetTokenT< CandView > _isolate_with
std::array< IsoTypes, kNPFTypes > _isolation_types