CMS 3D CMS Logo

VIDNestedWPBitmapProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PhysicsTools/NanoAOD
4 // Class: VIDNestedWPBitmapProducer
5 //
13 //
14 // Original Author: Marco Peruzzi
15 // Created: Mon, 04 Sep 2017 22:43:53 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
25 
28 
31 
34 
37 
38 //
39 // class declaration
40 //
41 
42 namespace {
43 
44  //will fail for non-electrons/photons (which so far are not used for this class)
45  //when we want to use non-electrons/photons, template specalisation will be necessary
46  template <typename T>
47  bool equal(const T& lhs, const T& rhs) {
48  return lhs.superCluster()->seed()->seed().rawId() == rhs.superCluster()->seed()->seed().rawId();
49  }
50 
51  //returns a edm::Ptr to the object matching the passed in object in object coll
52  //if objColl is invalid, passes the original pointer back
53  //if valid but not found, returns null
54  template <typename T>
55  edm::Ptr<T> getObjInColl(edm::Ptr<T> obj, edm::Handle<edm::View<T>> objColl) {
56  if (objColl.isValid()) {
57  for (auto& objToMatch : objColl->ptrs()) {
58  if (equal(*obj, *objToMatch)) {
59  return objToMatch;
60  }
61  }
62  return edm::Ptr<T>(objColl.id());
63  }
64  return obj;
65  }
66 
67 } // namespace
68 
69 template <typename T>
71 public:
73  : src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
74  srcForIDToken_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("srcForID"))),
75  isInit_(false) {
76  auto const& vwp = iConfig.getParameter<std::vector<std::string>>("WorkingPoints");
77  for (auto const& wp : vwp) {
78  src_bitmaps_.push_back(consumes<edm::ValueMap<unsigned int>>(edm::InputTag(wp + std::string("Bitmap"))));
80  }
81  nWP = src_bitmaps_.size();
82  produces<edm::ValueMap<int>>();
83  }
84 
85  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
86 
87 private:
88  void produce(edm::Event&, const edm::EventSetup&) override;
89 
90  // ----------member data ---------------------------
91 
94  std::vector<edm::EDGetTokenT<edm::ValueMap<unsigned int>>> src_bitmaps_;
95  std::vector<edm::EDGetTokenT<edm::ValueMap<vid::CutFlowResult>>> src_cutflows_;
96 
97  unsigned int nWP;
98  unsigned int nBits;
99  unsigned int nCuts = 0;
100  std::vector<unsigned int> res_;
101  bool isInit_;
102 
103  void initNCuts(unsigned int);
104 };
105 
106 template <typename T>
109  iEvent.getByToken(src_, src);
110 
111  auto srcForIDHandle = iEvent.getHandle(srcForIDToken_);
112 
113  std::vector<edm::Handle<edm::ValueMap<unsigned int>>> src_bitmaps(nWP);
114  for (unsigned int i = 0; i < nWP; i++)
115  iEvent.getByToken(src_bitmaps_[i], src_bitmaps[i]);
116  std::vector<edm::Handle<edm::ValueMap<vid::CutFlowResult>>> src_cutflows(nWP);
117  for (unsigned int i = 0; i < nWP; i++)
118  iEvent.getByToken(src_cutflows_[i], src_cutflows[i]);
119 
120  std::vector<unsigned int> res;
121 
122  for (auto const& obj : src->ptrs()) {
123  auto objForID = getObjInColl(obj, srcForIDHandle);
124  for (unsigned int j = 0; j < nWP; j++) {
125  auto cutflow = (*(src_cutflows[j]))[objForID];
126  if (!isInit_)
127  initNCuts(cutflow.cutFlowSize());
128  if (cutflow.cutFlowSize() != nCuts)
129  throw cms::Exception("Configuration", "Trying to compress VID bitmaps for cutflows of different size");
130  auto bitmap = (*(src_bitmaps[j]))[objForID];
131  for (unsigned int k = 0; k < nCuts; k++) {
132  if (j == 0)
133  res_[k] = 0;
134  if (bitmap >> k & 1) {
135  if (res_[k] != j)
136  throw cms::Exception(
137  "Configuration",
138  "Trying to compress VID bitmaps which are not nested in the correct order for all cuts");
139  res_[k]++;
140  }
141  }
142  }
143 
144  int out = 0;
145  for (unsigned int k = 0; k < nCuts; k++)
146  out |= (res_[k] << (nBits * k));
147  res.push_back(out);
148  }
149 
150  auto resV = std::make_unique<edm::ValueMap<int>>();
152  filler.insert(src, res.begin(), res.end());
153  filler.fill();
154 
155  iEvent.put(std::move(resV));
156 }
157 
158 template <typename T>
160  nCuts = n;
161  nBits = ceil(log2(nWP + 1));
162  if (nBits * nCuts > sizeof(int) * 8)
163  throw cms::Exception("Configuration", "Integer cannot contain the compressed VID bitmap information");
164  res_.resize(nCuts, 0);
165  isInit_ = true;
166 }
167 
168 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
169 template <typename T>
172  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
173  desc.add<edm::InputTag>("srcForID", edm::InputTag())->setComment("physics object collection the ID value maps are ");
174  desc.add<std::vector<std::string>>("WorkingPoints")->setComment("working points to be saved in the bitmask");
176  if (typeid(T) == typeid(reco::GsfElectron))
177  modname += "Ele";
178  else if (typeid(T) == typeid(reco::Photon))
179  modname += "Pho";
180  modname += "VIDNestedWPBitmapProducer";
181  descriptions.add(modname, desc);
182 }
183 
186 
187 //define this as a plug-in
constexpr int32_t ceil(float num)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void produce(edm::Event &, const edm::EventSetup &) override
std::vector< edm::EDGetTokenT< edm::ValueMap< vid::CutFlowResult > > > src_cutflows_
VIDNestedWPBitmapProducer< reco::Photon > PhoVIDNestedWPBitmapProducer
Definition: Electron.h:6
VIDNestedWPBitmapProducer< reco::GsfElectron > EleVIDNestedWPBitmapProducer
bool equal(const T &first, const T &second)
Definition: Equal.h:32
int iEvent
Definition: GenABIO.cc:224
std::vector< unsigned int > res_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
VIDNestedWPBitmapProducer(const edm::ParameterSet &iConfig)
edm::EDGetTokenT< edm::View< T > > srcForIDToken_
edm::EDGetTokenT< edm::View< T > > src_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HLT enums.
long double T
std::vector< edm::EDGetTokenT< edm::ValueMap< unsigned int > > > src_bitmaps_
def move(src, dest)
Definition: eostools.py:511
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)