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 
20 // system include files
21 #include <memory>
22 
23 // user include files
26 
29 
32 
35 
37 
38 //
39 // class declaration
40 //
41 
42 template <typename T>
44 
45 public:
46 
47  explicit VIDNestedWPBitmapProducer(const edm::ParameterSet &iConfig):
48  src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
49  isInit_(false)
50  {
51  auto const& vwp = iConfig.getParameter<std::vector<std::string>>("WorkingPoints");
52  for (auto const& wp : vwp) {
53  src_bitmaps_.push_back(consumes<edm::ValueMap<unsigned int> >(edm::InputTag(wp+std::string("Bitmap"))));
55  }
56  nWP = src_bitmaps_.size();
57  produces<edm::ValueMap<int>>();
58  }
59 
60  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
61 
62 private:
63 
64  void produce(edm::Event&, const edm::EventSetup&) override;
65 
66  // ----------member data ---------------------------
67 
69  std::vector<edm::EDGetTokenT<edm::ValueMap<unsigned int> > > src_bitmaps_;
70  std::vector<edm::EDGetTokenT<edm::ValueMap<vid::CutFlowResult> > > src_cutflows_;
71 
72  unsigned int nWP;
73  unsigned int nBits;
74  unsigned int nCuts = 0;
75  std::vector<unsigned int> res_;
76  bool isInit_;
77 
78  void initNCuts(unsigned int);
79 
80 };
81 
82 
83 template <typename T>
84 void
86 {
87 
89  iEvent.getByToken(src_, src);
90  std::vector<edm::Handle<edm::ValueMap<unsigned int>>> src_bitmaps(nWP);
91  for (unsigned int i=0; i<nWP; i++) iEvent.getByToken(src_bitmaps_[i], src_bitmaps[i]);
92  std::vector<edm::Handle<edm::ValueMap<vid::CutFlowResult>>> src_cutflows(nWP);
93  for (unsigned int i=0; i<nWP; i++) iEvent.getByToken(src_cutflows_[i], src_cutflows[i]);
94 
95  std::vector<unsigned int> res;
96 
97  for (auto const& obj : src->ptrs()) {
98  for (unsigned int j=0; j<nWP; j++){
99  auto cutflow = (*(src_cutflows[j]))[obj];
100  if (!isInit_) initNCuts(cutflow.cutFlowSize());
101  if (cutflow.cutFlowSize()!=nCuts) throw cms::Exception("Configuration","Trying to compress VID bitmaps for cutflows of different size");
102  auto bitmap = (*(src_bitmaps[j]))[obj];
103  for (unsigned int k=0; k<nCuts; k++){
104  if (j==0) res_[k] = 0;
105  if (bitmap>>k & 1) {
106  if (res_[k]!=j) throw cms::Exception("Configuration","Trying to compress VID bitmaps which are not nested in the correct order for all cuts");
107  res_[k]++;
108  }
109  }
110  }
111 
112  int out = 0;
113  for (unsigned int k=0; k<nCuts; k++) out |= (res_[k] << (nBits*k));
114  res.push_back(out);
115  }
116 
117  auto resV = std::make_unique<edm::ValueMap<int>>();
119  filler.insert(src,res.begin(),res.end());
120  filler.fill();
121 
122  iEvent.put(std::move(resV));
123 
124 }
125 
126 template <typename T>
127 void
129  nCuts = n;
130  nBits = ceil(log2(nWP+1));
131  if (nBits*nCuts>sizeof(int)*8) throw cms::Exception("Configuration","Integer cannot contain the compressed VID bitmap information");
132  res_.resize(nCuts,0);
133  isInit_ = true;
134 }
135 
136 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
137 template <typename T>
138 void
141  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
142  desc.add<std::vector<std::string>>("WorkingPoints")->setComment("working points to be saved in the bitmask");
143  std::string modname;
144  if (typeid(T) == typeid(pat::Electron)) modname+="Ele";
145  else if (typeid(T) == typeid(pat::Photon)) modname+="Pho";
146  modname+="VIDNestedWPBitmapProducer";
147  descriptions.add(modname,desc);
148 }
149 
150 
153 
154 //define this as a plug-in
T getParameter(std::string const &) const
Analysis-level Photon class.
Definition: Photon.h:47
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
void produce(edm::Event &, const edm::EventSetup &) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::vector< edm::EDGetTokenT< edm::ValueMap< vid::CutFlowResult > > > src_cutflows_
Definition: Electron.h:6
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< unsigned int > res_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
int k[5][pyjets_maxn]
VIDNestedWPBitmapProducer< pat::Electron > EleVIDNestedWPBitmapProducer
VIDNestedWPBitmapProducer(const edm::ParameterSet &iConfig)
edm::EDGetTokenT< edm::View< T > > src_
Analysis-level electron class.
Definition: Electron.h:52
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HLT enums.
VIDNestedWPBitmapProducer< pat::Photon > PhoVIDNestedWPBitmapProducer
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)