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  public:
45  explicit VIDNestedWPBitmapProducer(const edm::ParameterSet &iConfig):
46  src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
47  isInit_(false)
48  {
49  auto vwp = iConfig.getParameter<std::vector<std::string>>("WorkingPoints");
50  for (auto wp : vwp) {
53  }
54  nWP = src_bitmaps_.size();
55  produces<edm::ValueMap<int>>();
56  }
58 
59  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
60 
61  private:
62  void beginStream(edm::StreamID) override {};
63  void produce(edm::Event&, const edm::EventSetup&) override;
64  void endStream() override {};
65 
66  //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
67  //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
68  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
69  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
70 
71  // ----------member data ---------------------------
72 
74  std::vector<edm::EDGetTokenT<edm::ValueMap<unsigned int> > > src_bitmaps_;
75  std::vector<edm::EDGetTokenT<edm::ValueMap<vid::CutFlowResult> > > src_cutflows_;
76 
77  unsigned int nWP;
78  unsigned int nBits;
79  unsigned int nCuts;
80  std::vector<unsigned int> res_;
81  bool isInit_;
82 
83  void initNCuts(unsigned int);
84 
85 };
86 
87 //
88 // constants, enums and typedefs
89 //
90 
91 
92 //
93 // static data member definitions
94 //
95 
96 template <typename T>
97 void
99 {
100 
102  iEvent.getByToken(src_, src);
103  std::vector<edm::Handle<edm::ValueMap<unsigned int>>> src_bitmaps(nWP);
104  for (unsigned int i=0; i<nWP; i++) iEvent.getByToken(src_bitmaps_[i], src_bitmaps[i]);
105  std::vector<edm::Handle<edm::ValueMap<vid::CutFlowResult>>> src_cutflows(nWP);
106  for (unsigned int i=0; i<nWP; i++) iEvent.getByToken(src_cutflows_[i], src_cutflows[i]);
107 
108  std::vector<unsigned int> res;
109 
110  auto npho = src->size();
111  for (unsigned int i=0; i<npho; i++){
112  auto obj = src->ptrAt(i);
113  for (unsigned int j=0; j<nWP; j++){
114  auto cutflow = (*(src_cutflows[j]))[obj];
115  if (!isInit_) initNCuts(cutflow.cutFlowSize());
116  if (cutflow.cutFlowSize()!=nCuts) throw cms::Exception("Configuration","Trying to compress VID bitmaps for cutflows of different size");
117  auto bitmap = (*(src_bitmaps[j]))[obj];
118  for (unsigned int k=0; k<nCuts; k++){
119  if (j==0) res_[k] = 0;
120  if (bitmap>>k & 1) {
121  if (res_[k]!=j) throw cms::Exception("Configuration","Trying to compress VID bitmaps which are not nested in the correct order for all cuts");
122  res_[k]++;
123  }
124  }
125  }
126 
127  int out = 0;
128  for (unsigned int k=0; k<nCuts; k++) out |= (res_[k] << (nBits*k));
129  res.push_back(out);
130  }
131 
132 
133  std::unique_ptr<edm::ValueMap<int>> resV(new edm::ValueMap<int>());
135  filler.insert(src,res.begin(),res.end());
136  filler.fill();
137 
138  iEvent.put(std::move(resV));
139 
140 }
141 
142 template <typename T>
143 void
145  nCuts = n;
146  nBits = ceil(log2(nWP+1));
147  if (nBits*nCuts>sizeof(int)*8) throw cms::Exception("Configuration","Integer cannot contain the compressed VID bitmap information");
148  res_.resize(nCuts,0);
149  isInit_ = true;
150 }
151 
152 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
153 template <typename T>
154 void
157  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
158  desc.add<std::vector<std::string>>("WorkingPoints")->setComment("working points to be saved in the bitmask");
159  std::string modname;
160  if (typeid(T) == typeid(pat::Electron)) modname+="Ele";
161  else if (typeid(T) == typeid(pat::Photon)) modname+="Pho";
162  modname+="VIDNestedWPBitmapProducer";
163  descriptions.add(modname,desc);
164 }
165 
166 
169 
170 //define this as a plug-in
173 
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:136
void produce(edm::Event &, const edm::EventSetup &) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
std::vector< edm::EDGetTokenT< edm::ValueMap< vid::CutFlowResult > > > src_cutflows_
Definition: Electron.h:6
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
int iEvent
Definition: GenABIO.cc:230
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.
void beginStream(edm::StreamID) override
VIDNestedWPBitmapProducer< pat::Photon > PhoVIDNestedWPBitmapProducer
long double T
std::vector< edm::EDGetTokenT< edm::ValueMap< unsigned int > > > src_bitmaps_
def move(src, dest)
Definition: eostools.py:510
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)