Imperial Analysis
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ICElectronConversionCalculator.cc
Go to the documentation of this file.
2 #include <vector>
3 #include "FWCore/Framework/interface/Event.h"
4 #include "FWCore/Framework/interface/EventSetup.h"
5 #include "FWCore/Framework/interface/MakerMacros.h"
6 #include "FWCore/ParameterSet/interface/ParameterSet.h"
7 #include "FWCore/Utilities/interface/InputTag.h"
8 #include "DataFormats/Common/interface/Handle.h"
9 #include "DataFormats/Common/interface/ValueMap.h"
11 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
12 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
13 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
14 #include "DataFormats/EgammaCandidates/interface/Conversion.h"
15 #include "RecoEgamma/EgammaTools/interface/ConversionTools.h"
17 
19  const edm::ParameterSet& config)
20  : input_((config.getParameter<edm::InputTag>("input"))),
21  input_beamspot_((config.getParameter<edm::InputTag>("beamspot"))),
22  input_conversions_((config.getParameter<edm::InputTag>("conversions"))) {
23  consumes<edm::View<reco::GsfElectron>>(input_);
24  consumes<reco::BeamSpot>(input_beamspot_);
25  consumes<reco::ConversionCollection>(input_conversions_);
26  produces<edm::ValueMap<bool> >();
27 }
28 
30 
31 void ICElectronConversionCalculator::produce(edm::Event& event,
32  const edm::EventSetup& setup) {
33  std::auto_ptr<edm::ValueMap<bool> > product(new edm::ValueMap<bool>());
34  // Use an edm::View here so that this will work on a reco::GsfElectron or
35  // pat::Electron collection
36  edm::Handle<edm::View<reco::GsfElectron> > elecs_handle;
37  event.getByLabel(input_, elecs_handle);
38 
39  edm::Handle<reco::BeamSpot> beamspot_handle;
40  event.getByLabel(input_beamspot_, beamspot_handle);
41 
42  edm::Handle<reco::ConversionCollection> conversions_handle;
43  event.getByLabel(input_conversions_, conversions_handle);
44 
45  // Have to be careful producing a ValueMap from an edm::View. When we call
46  // filler.insert(handle,...) the ValueMap will use the ProductID of the handle
47  // to create the map keys. If the View points to a concrete collection (i.e.
48  // vector<T>) then this ID will correctly refer to this collection. However if
49  // the View points to an edm::RefVector<T> we will get the ID of the RefVector
50  // not the underlying concrete collection the RefVector points to. Then later
51  // when we go to retrieve values from the map we will get a product not found
52  // error. The solution is to get a Ref via the View `refAt` method, and use
53  // this to create an edm::RefToBaseProd, which will always carry the ID of the
54  // concrete collection and can be used in place of the handle in
55  // filler.insert(...).
56  // 14/11 - as RefToBaseProd doesn't exist in CMSSW_7_6_X anymore,
57  // need a different solution. Unfortunately filler.insert(handle,...) calls
58  // handle.id() and handle->size() hence the ridiculous double struct construction -
59  // the first struct stores the size of the RefVector, the second a pointer to the first
60  // plus the id of the underlying collection, so that indeed filler.insert can call both
61  // .id() and ->size()
62 
63 
67  unsigned n=0;
68  if(elecs_handle->size()>0){
69  csize.handle_ = elecs_handle;
70  csize_ptr = &csize;
71  c_id_size.collsizepointer = csize_ptr;
72  n=elecs_handle->size();
73  }
74  std::vector<bool> values(n,false);
75 
76  for (unsigned i = 0; i < n; ++i) {
77  reco::GsfElectron const& src = elecs_handle->at(i);
78  values[i] = ConversionTools::hasMatchedConversion(
79  src, conversions_handle, beamspot_handle->position(),
80  true, 2.0, 1e-6, 0);
81  }
82 
83  edm::ValueMap<bool>::Filler filler(*product);
84  // We need to pass a CollectionIDAndSize here, only exists
85  // if the collection has at least one object (see above)
86  if (n > 0) {
87  filler.insert(c_id_size, values.begin(), values.end());
88  filler.fill();
89  }
90 
91  event.put(product);
92 }
93 
94 void ICElectronConversionCalculator::beginJob() {}
95 
96 void ICElectronConversionCalculator::endJob() {}
97 
98 // define this as a plug-in
DEFINE_FWK_MODULE(ICElectronConversionCalculator)
size_t size() const
Produces an edm::ValueMap for the electron conversion-rejection flag.
edm::Handle< edm::View< T > > handle_
CollectionSize< T > * collsizepointer
Definition: Consumes.h:19
ICElectronConversionCalculator(const edm::ParameterSet &)