CMS 3D CMS Logo

LowPtGsfElectronSeedValueMapsProducer.cc
Go to the documentation of this file.
19 
20 #include <vector>
21 #include <string>
22 
24 public:
26 
27  void produce(edm::Event&, const edm::EventSetup&) override;
28 
30 
31 private:
34  std::vector<std::string> names_;
35  const bool rekey_;
37  std::vector<edm::EDGetTokenT<edm::ValueMap<float> > > floatValueMaps_;
38 };
39 
41 //
43  : gsfTracks_(),
45  names_(),
46  rekey_(conf.getParameter<bool>("rekey")),
47  gsfElectrons_(),
48  floatValueMaps_() {
49  if (rekey_) {
50  gsfElectrons_ = consumes<reco::GsfElectronCollection>(conf.getParameter<edm::InputTag>("gsfElectrons"));
51  std::vector<edm::InputTag> tags = conf.getParameter<std::vector<edm::InputTag> >("floatValueMaps");
52  for (const auto& tag : tags) {
54  tags, [this](edm::InputTag const& tag) { return consumes<edm::ValueMap<float> >(tag); });
55  names_.push_back(tag.instance());
56  produces<edm::ValueMap<float> >(tag.instance());
57  }
58  } else {
59  gsfTracks_ = consumes<reco::GsfTrackCollection>(conf.getParameter<edm::InputTag>("gsfTracks"));
60  preIdsValueMap_ = consumes<edm::ValueMap<reco::PreIdRef> >(conf.getParameter<edm::InputTag>("preIdsValueMap"));
61  names_ = conf.getParameter<std::vector<std::string> >("ModelNames");
62  for (const auto& name : names_) {
63  produces<edm::ValueMap<float> >(name);
64  }
65  }
66 }
67 
69 //
71  if (rekey_ == false) {
72  // TRANSFORM VALUEMAP OF PREID OBJECTS KEYED BY KF TRACK ...
73  // .. INTO VALUEMAP OF FLOATS (BDT SCORE) KEYED BY GSF TRACK ...
74 
75  // Retrieve GsfTracks from Event
76  auto gsfTracks = event.getHandle(gsfTracks_);
77 
78  // Retrieve PreIds from Event
79  auto preIdsValueMap = event.getHandle(preIdsValueMap_);
80 
81  // Iterate through GsfTracks, extract BDT output, and store result in ValueMap for each model
82  std::vector<std::vector<float> > output;
83  for (unsigned int iname = 0; iname < names_.size(); ++iname) {
84  output.push_back(std::vector<float>(gsfTracks->size(), -999.));
85  }
86  auto const& gsfTracksV = *gsfTracks;
87  for (unsigned int igsf = 0; igsf < gsfTracksV.size(); igsf++) {
88  const reco::GsfTrack& gsf = gsfTracksV[igsf];
89  if (gsf.extra().isNonnull() && gsf.extra()->seedRef().isNonnull()) {
90  reco::ElectronSeedRef seed = gsf.extra()->seedRef().castTo<reco::ElectronSeedRef>();
91  if (seed.isNonnull() && seed->ctfTrack().isNonnull()) {
92  const reco::PreIdRef preid = (*preIdsValueMap)[seed->ctfTrack()];
93  if (preid.isNonnull()) {
94  for (unsigned int iname = 0; iname < names_.size(); ++iname) {
95  output[iname][igsf] = preid->mva(iname);
96  }
97  }
98  }
99  }
100  }
101 
102  // Create and put ValueMap in Event
103  for (unsigned int iname = 0; iname < names_.size(); ++iname) {
104  auto ptr = std::make_unique<edm::ValueMap<float> >(edm::ValueMap<float>());
106  filler.insert(gsfTracks, output[iname].begin(), output[iname].end());
107  filler.fill();
108  event.put(std::move(ptr), names_[iname]);
109  }
110 
111  } else {
112  // TRANSFORM VALUEMAP OF FLOATS (BDT SCORE) KEYED BY GSF TRACK ...
113  // .. INTO VALUEMAP OF FLOATS (BDT SCORE) KEYED BY GSF ELECTRON ...
114 
115  // Retrieve GsfElectrons from Event
116  auto gsfElectrons = event.getHandle(gsfElectrons_);
117 
118  // Retrieve float ValueMaps from Event
119  for (unsigned int idx = 0; idx < names_.size(); ++idx) {
120  // Extract ValueMap from Event
121  auto const& floatValueMap = event.get(floatValueMaps_[idx]);
122 
123  // Store BDT scores in vector
124  std::vector<float> output(gsfElectrons->size(), -99.);
125  auto const& gsfElectronsV = *gsfElectrons;
126  for (unsigned int iele = 0; iele < gsfElectronsV.size(); iele++) {
127  const reco::GsfElectron& ele = gsfElectronsV[iele];
129  output[iele] = floatValueMap[gsf];
130  }
131  // Create and put ValueMap in Event
132  auto ptr = std::make_unique<edm::ValueMap<float> >(edm::ValueMap<float>());
134  filler.insert(gsfElectrons, output.begin(), output.end());
135  filler.fill();
136  event.put(std::move(ptr), names_[idx]);
137  }
138  }
139 }
140 
142 //
145  desc.add<edm::InputTag>("gsfTracks", edm::InputTag("lowPtGsfEleGsfTracks"));
146  desc.add<edm::InputTag>("preIdsValueMap", edm::InputTag("lowPtGsfElectronSeeds"));
147  desc.add<std::vector<std::string> >("ModelNames", {"unbiased", "ptbiased"});
148  desc.add<bool>("rekey", false);
149  desc.add<edm::InputTag>("gsfElectrons", edm::InputTag());
150  desc.add<std::vector<edm::InputTag> >("floatValueMaps", std::vector<edm::InputTag>());
151  descriptions.add("lowPtGsfElectronSeedValueMaps", desc);
152 }
153 
155 //
T getParameter(std::string const &) const
GsfTrackRef gsfTrack() const override
reference to a GsfTrack
Definition: GsfElectron.h:186
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
const TrackExtraRef & extra() const
reference to "extra" object
Definition: Track.h:194
edm::EDGetTokenT< edm::ValueMap< reco::PreIdRef > > preIdsValueMap_
edm::EDGetTokenT< reco::GsfTrackCollection > gsfTracks_
std::vector< edm::EDGetTokenT< edm::ValueMap< float > > > floatValueMaps_
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
#define end
Definition: vmac.h:39
ParameterDescriptionBase * add(U const &iLabel, T const &value)
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
void add(std::string const &label, ParameterSetDescription const &psetDescription)
edm::EDGetTokenT< reco::GsfElectronCollection > gsfElectrons_
static void fillDescriptions(edm::ConfigurationDescriptions &)
#define begin
Definition: vmac.h:32
std::string const & instance() const
Definition: InputTag.h:37
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1
void produce(edm::Event &, const edm::EventSetup &) override