test
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( const auto& cand_to_isolate : to_isolate->ptrVector() ) {
136  std::array<std::vector<float>,kNPFTypes> cand_values;
137  unsigned k = 0;
138  for( const auto& isolators_for_type : _isolation_types ) {
139  cand_values[k].resize(isolators_for_type.size());
140  for( auto& value : cand_values[k] ) value = 0.0;
141  ++k;
142  }
143  for( const auto& isocand : isolate_with->ptrVector() ) {
144  auto isotype = helper.translatePdgIdToType(isocand->pdgId());
145  const auto& isolations = _isolation_types[isotype];
146  for( unsigned i = 0; i < isolations.size(); ++ i ) {
147  if( isolations[i]->isInIsolationCone(cand_to_isolate,isocand) ) {
148  cand_values[isotype][i] += isocand->pt();
149  }
150  }
151  }
152  // add this candidate to isolation value list
153  for( unsigned i = 0; i < kNPFTypes; ++i ) {
154  for( unsigned j = 0; j < cand_values[i].size(); ++j ) {
155  the_values[i][j].push_back(cand_values[i][j]);
156  }
157  }
158  }
159  // fill and put all products
160  for( unsigned i = 0; i < kNPFTypes; ++ i ) {
161  for( unsigned j = 0; j < the_values[i].size(); ++j ) {
162  product_type the_product( new edm::ValueMap<float> );
163  edm::ValueMap<float>::Filler fillerprod(*the_product);
164  fillerprod.insert(to_isolate,
165  the_values[i][j].begin(),
166  the_values[i][j].end());
167  fillerprod.fill();
168  ev.put(the_product,_product_names[i][j]);
169  }
170  }
171  }
172 }
173 
174 #endif
int i
Definition: DBlmapReader.cc:9
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:434
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
list namespace
Definition: asciidump.py:379
bool ev
void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) overridefinal
#define constexpr
list _isolation_types
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
edm::EDGetTokenT< CandView > _to_isolate
int j
Definition: DBlmapReader.cc:9
std::unordered_map< std::string, int > TypeMap
#define end
Definition: vmac.h:37
int k[5][pyjets_maxn]
string const
Definition: compareJSON.py:14
void produce(edm::Event &, const edm::EventSetup &) overridefinal
ParticleType translatePdgIdToType(int pdgid) const
Definition: PFCandidate.cc:220
#define private
Definition: FWFileEntry.h:17
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:38
#define begin
Definition: vmac.h:30
const std::vector< edm::ParameterSet > & isoDefs
T get(const Candidate &c)
Definition: component.h:55
edm::EDGetTokenT< CandView > _isolate_with
std::array< IsoTypes, kNPFTypes > _isolation_types