CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
VersionedIdProducer.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_SelectorUtils_VersionedIdProducer_h
2 #define PhysicsTools_SelectorUtils_VersionedIdProducer_h
3 // system include files
4 #include <memory>
5 
6 // user include files
9 
12 
14 
17 
20 
22 
23 #include <memory>
24 #include <sstream>
25 
26 template< class PhysicsObjectPtr , class SelectorType=VersionedSelector<PhysicsObjectPtr> >
28 public:
30 
33 
34  explicit VersionedIdProducer(const edm::ParameterSet&);
36 
37  virtual void produce(edm::Event&, const edm::EventSetup&) override;
38 
39 private:
40  // ----------member data ---------------------------
41  bool verbose_;
43 
44  std::vector<std::unique_ptr<SelectorType> > ids_;
45 
46 };
47 
48 //
49 // constants, enums and typedefs
50 //
51 
52 //
53 // static data member definitions
54 //
55 
56 //
57 // constructors and destructor
58 //
59 template< class PhysicsObjectPtr , class SelectorType >
62  constexpr char bitmap_label[] = "Bitmap";
63 
64  verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
65 
66  physicsObjectSrc_ =
67  consumes<Collection>(iConfig.getParameter<edm::InputTag>("physicsObjectSrc"));
68 
69  const std::vector<edm::ParameterSet>& ids =
70  iConfig.getParameterSetVector("physicsObjectIDs");
71  for(const auto& id : ids ) {
72  const std::string& idMD5 =
73  id.getParameter<std::string>("idMD5");
74  const edm::ParameterSet& the_id =
75  id.getParameterSet("idDefinition");
76  const std::string& idname =
77  the_id.getParameter<std::string>("idName");
78  std::string calculated_md5;
79  ids_.emplace_back( new SelectorType(the_id) );
80  calculated_md5 = ids_.back()->md5String();
81  ids_.back()->setConsumes(consumesCollector());
82  if( ids_.back()->cutFlowSize() == 0 ) {
83  throw cms::Exception("InvalidCutFlow")
84  << "Post-processing cutflow size is zero! You may have configured"
85  << " the python incorrectly!";
86  }
87 
88  if( idMD5 != calculated_md5 ) {
89  edm::LogInfo("IdConfigurationNotValidated")
90  << "ID: " << ids_.back()->name() << "\n"
91  << "The expected md5: " << idMD5 << " does not match the md5\n"
92  << "calculated by the ID: " << calculated_md5 << " please\n"
93  << "update your python configuration or determine the source\n"
94  << "of transcription error!";
95  }
96 
97  std::stringstream idmsg;
98 
99  // dump whatever information about the ID we have
100  idmsg << "Instantiated ID: " << idname << std::endl
101  << "with MD5 hash: " << idMD5 << std::endl;
102  const bool isPOGApproved =
103  id.getUntrackedParameter<bool>("isPOGApproved",false);
104  if( isPOGApproved ) {
105  idmsg << "This ID is POG approved!" << std::endl;
106  } else {
107  idmsg << "This ID is not POG approved and likely under development!!!\n"
108  << "Please make sure to report your progress with this ID "
109  << "at the next relevant POG meeting." << std::endl;
110  }
111 
112  if( !isPOGApproved ) {
113  edm::LogWarning("IdInformation")
114  << idmsg.str();
115  } else {
116  edm::LogInfo("IdInformation")
117  << idmsg.str();
118  }
119 
120  produces<std::string>(idname);
121  produces<edm::ValueMap<bool> >(idname);
122  produces<edm::ValueMap<float> >(idname); // for PAT
123  produces<edm::ValueMap<unsigned> >(idname);
124  produces<edm::ValueMap<unsigned> >(idname+std::string(bitmap_label));
125  produces<edm::ValueMap<vid::CutFlowResult> >(idname);
126  produces<pat::UserDataCollection>(idname);
127  produces<edm::ValueMap<edm::Ptr<pat::UserData> > >(idname);
128  }
129 }
130 
131 template< class PhysicsObjectPtr , class SelectorType >
134  constexpr char bitmap_label[] = "Bitmap";
135 
136  edm::Handle<Collection> physicsObjectsHandle;
137  iEvent.getByToken(physicsObjectSrc_,physicsObjectsHandle);
138 
139  const Collection& physicsobjects = *physicsObjectsHandle;
140 
141  for( const auto& id : ids_ ) {
142  std::auto_ptr<edm::ValueMap<bool> > outPass(new edm::ValueMap<bool>() );
143  std::auto_ptr<edm::ValueMap<float> > outPassf(new edm::ValueMap<float>() );
144  std::auto_ptr<edm::ValueMap<unsigned> > outHowFar(new edm::ValueMap<unsigned>() );
145  std::auto_ptr<edm::ValueMap<unsigned> > outBitmap(new edm::ValueMap<unsigned>() );
146  std::auto_ptr<edm::ValueMap<vid::CutFlowResult> > out_cfrs(new edm::ValueMap<vid::CutFlowResult>() );
147  std::auto_ptr<pat::UserDataCollection> out_usrd(new pat::UserDataCollection);
148 
149  std::vector<bool> passfail;
150  std::vector<float> passfailf;
151  std::vector<unsigned> howfar;
152  std::vector<unsigned> bitmap;
153  std::vector<vid::CutFlowResult> cfrs;
154 
155  for(size_t i = 0; i < physicsobjects.size(); ++i) {
156  auto po = physicsobjects.ptrAt(i);
157  passfail.push_back((*id)(po,iEvent));
158  passfailf.push_back(passfail.back());
159  howfar.push_back(id->howFarInCutFlow());
160  bitmap.push_back(id->bitMap());
161  cfrs.push_back(id->cutFlowResult());
162  out_usrd->push_back(pat::UserData::make(cfrs.back(),false));
163  }
164 
165  edm::ValueMap<bool>::Filler fillerpassfail(*outPass);
166  fillerpassfail.insert(physicsObjectsHandle, passfail.begin(), passfail.end());
167  fillerpassfail.fill();
168 
169  edm::ValueMap<float>::Filler fillerpassfailf(*outPassf);
170  fillerpassfailf.insert(physicsObjectsHandle, passfailf.begin(), passfailf.end());
171  fillerpassfailf.fill();
172 
173  edm::ValueMap<unsigned>::Filler fillerhowfar(*outHowFar);
174  fillerhowfar.insert(physicsObjectsHandle, howfar.begin(), howfar.end() );
175  fillerhowfar.fill();
176 
177  edm::ValueMap<unsigned>::Filler fillerbitmap(*outBitmap);
178  fillerbitmap.insert(physicsObjectsHandle, bitmap.begin(), bitmap.end() );
179  fillerbitmap.fill();
180 
181  edm::ValueMap<vid::CutFlowResult>::Filler fillercfr(*out_cfrs);
182  fillercfr.insert(physicsObjectsHandle, cfrs.begin(), cfrs.end() );
183  fillercfr.fill();
184 
185  iEvent.put(outPass,id->name());
186  iEvent.put(outPassf,id->name());
187  iEvent.put(outHowFar,id->name());
188  iEvent.put(outBitmap,id->name()+std::string(bitmap_label));
189  iEvent.put(out_cfrs,id->name());
190  iEvent.put(std::auto_ptr<std::string>(new std::string(id->md5String())),
191  id->name());
192  auto usrd_handle = iEvent.put(out_usrd,id->name());
193  //now add the value map of ptrs to user datas
194  std::auto_ptr<edm::ValueMap<edm::Ptr<pat::UserData> > > out_usrdptrs(new edm::ValueMap<edm::Ptr<pat::UserData> >);
195  std::vector<edm::Ptr<pat::UserData> > usrdptrs;
196  for( unsigned i = 0; i < usrd_handle->size(); ++i ){
197  usrdptrs.push_back(edm::Ptr<pat::UserData>(usrd_handle,i));
198  }
199 
200  edm::ValueMap<edm::Ptr<pat::UserData> >::Filler fillerusrdptrs(*out_usrdptrs);
201  fillerusrdptrs.insert(physicsObjectsHandle, usrdptrs.begin(), usrdptrs.end());
202  fillerusrdptrs.fill();
203 
204  iEvent.put(out_usrdptrs,id->name());
205  }
206 }
207 
208 #endif
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
VParameterSet const & getParameterSetVector(std::string const &name) const
std::vector< std::unique_ptr< SelectorType > > ids_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
Ptr< value_type > ptrAt(size_type i) const
static std::auto_ptr< UserData > make(const T &value, bool transientOnly=false)
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
size_type size() const
#define constexpr
typename PhysicsObjectPtr::value_type PhysicsObjectType
int iEvent
Definition: GenABIO.cc:230
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
virtual void produce(edm::Event &, const edm::EventSetup &) override
VersionedIdProducer(const edm::ParameterSet &)
ParameterSet const & getParameterSet(std::string const &) const