CMS 3D CMS Logo

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&);
35  ~VersionedIdProducer() override {}
36 
37  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 // constants, enums and typedefs
49 //
50 
51 //
52 // static data member definitions
53 //
54 
55 //
56 // constructors and destructor
57 //
58 template <class PhysicsObjectPtr, class SelectorType>
60  constexpr char bitmap_label[] = "Bitmap";
61 
62  verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
63 
64  physicsObjectSrc_ = consumes<Collection>(iConfig.getParameter<edm::InputTag>("physicsObjectSrc"));
65 
66  const std::vector<edm::ParameterSet>& ids = iConfig.getParameterSetVector("physicsObjectIDs");
67  for (const auto& id : ids) {
68  const std::string& idMD5 = id.getParameter<std::string>("idMD5");
69  const edm::ParameterSet& the_id = id.getParameterSet("idDefinition");
70  const std::string& idname = the_id.getParameter<std::string>("idName");
71  std::string calculated_md5;
72  ids_.emplace_back(new SelectorType(the_id));
73  calculated_md5 = ids_.back()->md5String();
74  ids_.back()->setConsumes(consumesCollector());
75  if (ids_.back()->cutFlowSize() == 0) {
76  throw cms::Exception("InvalidCutFlow") << "Post-processing cutflow size is zero! You may have configured"
77  << " the python incorrectly!";
78  }
79 
80  if (idMD5 != calculated_md5) {
81  edm::LogInfo("IdConfigurationNotValidated") << "ID: " << ids_.back()->name() << "\n"
82  << "The expected md5: " << idMD5 << " does not match the md5\n"
83  << "calculated by the ID: " << calculated_md5 << " please\n"
84  << "update your python configuration or determine the source\n"
85  << "of transcription error!";
86  }
87 
88  std::stringstream idmsg;
89 
90  // dump whatever information about the ID we have
91  idmsg << "Instantiated ID: " << idname << std::endl << "with MD5 hash: " << idMD5 << std::endl;
92  const bool isPOGApproved = id.getUntrackedParameter<bool>("isPOGApproved", false);
93  if (isPOGApproved) {
94  idmsg << "This ID is POG approved!" << std::endl;
95  } else {
96  idmsg << "This ID is not POG approved and likely under development!!!\n"
97  << "Please make sure to report your progress with this ID "
98  << "at the next relevant POG meeting." << std::endl;
99  }
100 
101  if (!isPOGApproved) {
102  edm::LogWarning("IdInformation") << idmsg.str();
103  } else {
104  edm::LogInfo("IdInformation") << idmsg.str();
105  }
106 
107  produces<std::string>(idname);
108  produces<edm::ValueMap<bool>>(idname);
109  produces<edm::ValueMap<float>>(idname); // for PAT
110  produces<edm::ValueMap<unsigned>>(idname);
111  produces<edm::ValueMap<unsigned>>(idname + std::string(bitmap_label));
112  produces<edm::ValueMap<vid::CutFlowResult>>(idname);
113  produces<pat::UserDataCollection>(idname);
114  produces<edm::ValueMap<edm::Ptr<pat::UserData>>>(idname);
115  }
116 }
117 
118 template <class PhysicsObjectPtr, class SelectorType>
120  constexpr char bitmap_label[] = "Bitmap";
121 
122  edm::Handle<Collection> physicsObjectsHandle;
123  iEvent.getByToken(physicsObjectSrc_, physicsObjectsHandle);
124 
125  const Collection& physicsobjects = *physicsObjectsHandle;
126 
127  for (const auto& id : ids_) {
128  auto outPass = std::make_unique<edm::ValueMap<bool>>();
129  auto outPassf = std::make_unique<edm::ValueMap<float>>();
130  auto outHowFar = std::make_unique<edm::ValueMap<unsigned>>();
131  auto outBitmap = std::make_unique<edm::ValueMap<unsigned>>();
132  auto out_cfrs = std::make_unique<edm::ValueMap<vid::CutFlowResult>>();
133  auto out_usrd = std::make_unique<pat::UserDataCollection>();
134 
135  std::vector<bool> passfail;
136  std::vector<float> passfailf;
137  std::vector<unsigned> howfar;
138  std::vector<unsigned> bitmap;
139  std::vector<vid::CutFlowResult> cfrs;
140 
141  for (size_t i = 0; i < physicsobjects.size(); ++i) {
142  auto po = physicsobjects.ptrAt(i);
143  passfail.push_back((*id)(po, iEvent));
144  passfailf.push_back(passfail.back());
145  howfar.push_back(id->howFarInCutFlow());
146  bitmap.push_back(id->bitMap());
147  cfrs.push_back(id->cutFlowResult());
148  out_usrd->push_back(pat::UserData::make(cfrs.back(), false));
149  }
150 
151  edm::ValueMap<bool>::Filler fillerpassfail(*outPass);
152  fillerpassfail.insert(physicsObjectsHandle, passfail.begin(), passfail.end());
153  fillerpassfail.fill();
154 
155  edm::ValueMap<float>::Filler fillerpassfailf(*outPassf);
156  fillerpassfailf.insert(physicsObjectsHandle, passfailf.begin(), passfailf.end());
157  fillerpassfailf.fill();
158 
159  edm::ValueMap<unsigned>::Filler fillerhowfar(*outHowFar);
160  fillerhowfar.insert(physicsObjectsHandle, howfar.begin(), howfar.end());
161  fillerhowfar.fill();
162 
163  edm::ValueMap<unsigned>::Filler fillerbitmap(*outBitmap);
164  fillerbitmap.insert(physicsObjectsHandle, bitmap.begin(), bitmap.end());
165  fillerbitmap.fill();
166 
167  edm::ValueMap<vid::CutFlowResult>::Filler fillercfr(*out_cfrs);
168  fillercfr.insert(physicsObjectsHandle, cfrs.begin(), cfrs.end());
169  fillercfr.fill();
170 
171  iEvent.put(std::move(outPass), id->name());
172  iEvent.put(std::move(outPassf), id->name());
173  iEvent.put(std::move(outHowFar), id->name());
174  iEvent.put(std::move(outBitmap), id->name() + std::string(bitmap_label));
175  iEvent.put(std::move(out_cfrs), id->name());
176  iEvent.put(std::make_unique<std::string>(id->md5String()), id->name());
177  auto usrd_handle = iEvent.put(std::move(out_usrd), id->name());
178  //now add the value map of ptrs to user datas
179  auto out_usrdptrs = std::make_unique<edm::ValueMap<edm::Ptr<pat::UserData>>>();
180  std::vector<edm::Ptr<pat::UserData>> usrdptrs;
181  for (unsigned i = 0; i < usrd_handle->size(); ++i) {
182  usrdptrs.push_back(edm::Ptr<pat::UserData>(usrd_handle, i));
183  }
184 
185  edm::ValueMap<edm::Ptr<pat::UserData>>::Filler fillerusrdptrs(*out_usrdptrs);
186  fillerusrdptrs.insert(physicsObjectsHandle, usrdptrs.begin(), usrdptrs.end());
187  fillerusrdptrs.fill();
188 
189  iEvent.put(std::move(out_usrdptrs), id->name());
190  }
191 }
192 
193 #endif
edm::helper::Filler::insert
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
mps_fire.i
i
Definition: mps_fire.py:428
VersionedIdProducer::~VersionedIdProducer
~VersionedIdProducer() override
Definition: VersionedIdProducer.h:35
VersionedIdProducer::physicsObjectSrc_
TokenType physicsObjectSrc_
Definition: VersionedIdProducer.h:42
edm::EDGetTokenT< Collection >
cutBasedElectronHLTPreselecition_Summer16_V1_cff.isPOGApproved
isPOGApproved
for now until we have a database...
Definition: cutBasedElectronHLTPreselecition_Summer16_V1_cff.py:97
edm::helper::Filler::fill
void fill()
Definition: ValueMap.h:65
EDProducer.h
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
versionedElectronIDProducer_cfi.idMD5
idMD5
Definition: versionedElectronIDProducer_cfi.py:13
edm::Handle
Definition: AssociativeIterator.h:50
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
VersionedIdProducer::verbose_
bool verbose_
Definition: VersionedIdProducer.h:41
MakerMacros.h
VersionedIdProducer::VersionedIdProducer
VersionedIdProducer(const edm::ParameterSet &)
Definition: VersionedIdProducer.h:59
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::View::size
size_type size() const
edm::View
Definition: CaloClusterFwd.h:14
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
UserData.h
iEvent
int iEvent
Definition: GenABIO.cc:224
RefToPtr.h
VersionedSelector.h
edm::stream::EDProducer
Definition: EDProducer.h:38
edm::EventSetup
Definition: EventSetup.h:57
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
VersionedIdProducer::PhysicsObjectType
typename PhysicsObjectPtr::value_type PhysicsObjectType
Definition: VersionedIdProducer.h:29
edm::Ptr
Definition: AssociationVector.h:31
eostools.move
def move(src, dest)
Definition: eostools.py:511
VIDCutFlowResult.h
VersionedIdProducer::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: VersionedIdProducer.h:119
VersionedIdProducer::ids_
std::vector< std::unique_ptr< SelectorType > > ids_
Definition: VersionedIdProducer.h:44
Frameworkfwd.h
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:31
pat::UserData::make
static std::unique_ptr< UserData > make(const T &value, bool transientOnly=false)
edm::ValueMap
Definition: ValueMap.h:107
Exception
Definition: hltDiff.cc:246
VersionedIdProducer
Definition: VersionedIdProducer.h:27
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::ParameterSet::getParameterSetVector
VParameterSet const & getParameterSetVector(std::string const &name) const
Definition: ParameterSet.cc:2160
edm::helper::Filler
Definition: ValueMap.h:22
View.h
ParameterSet.h
edm::Event
Definition: Event.h:73
edm::View::ptrAt
Ptr< value_type > ptrAt(size_type i) const
edm::InputTag
Definition: InputTag.h:15
edm::ParameterSet::getParameterSet
ParameterSet const & getParameterSet(std::string const &) const
Definition: ParameterSet.cc:2128