CMS 3D CMS Logo

JetTagProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: JetTagProducer
4 // Class: JetTagProducer
5 //
13 //
14 // Original Author: Andrea Rizzi
15 // Created: Thu Apr 6 09:56:23 CEST 2006
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 #include <string>
23 #include <vector>
24 #include <map>
25 
26 // user include files
37 
41 
42 #include "JetTagProducer.h"
43 
44 using namespace std;
45 using namespace reco;
46 using namespace edm;
47 
48 //
49 // constructors and destructor
50 //
52  m_jetTagComputer(iConfig.getParameter<string>("jetTagComputer"))
53 {
54  std::vector<edm::InputTag> m_tagInfos = iConfig.getParameter< vector<InputTag> >("tagInfos");
55  nTagInfos = m_tagInfos.size();
56  for(unsigned int i = 0; i < nTagInfos; i++) {
57  token_tagInfos.push_back( consumes<View<BaseTagInfo> >(m_tagInfos[i]) );
58  }
59 
60  produces<JetTagCollection>();
61 }
62 
64 {
65 }
66 
67 //
68 // member functions
69 //
70 
71 // map helper - for some reason RefToBase lacks operator < (...)
72 namespace {
73  struct JetRefCompare {
74  inline bool operator () (const RefToBase<Jet> &j1,
75  const RefToBase<Jet> &j2) const
76  { return j1.id() < j2.id() || (j1.id() == j2.id() && j1.key() < j2.key()); }
77  };
78 }
79 
80 // ------------ method called to produce the data ------------
81 void
83 {
85  iSetup.get<JetTagComputerRecord>().get( m_jetTagComputer, computer );
86 
87  if (recordWatcher_.check(iSetup) ) {
88  unsigned int nLabels = computer->getInputLabels().size();
89  if (nLabels == 0) ++nLabels;
90  if (nTagInfos != nLabels) {
91 
92  vector<string> inputLabels(computer->getInputLabels());
93  // backward compatible case, use default tagInfo
94  if (inputLabels.empty())
95  inputLabels.push_back("tagInfo");
96  std::string message("VInputTag size mismatch - the following taginfo "
97  "labels are needed:\n");
98  for(vector<string>::const_iterator iter = inputLabels.begin();
99  iter != inputLabels.end(); ++iter)
100  message += "\"" + *iter + "\"\n";
101  throw edm::Exception(errors::Configuration) << message;
102  }
103  }
104 
105  // now comes the tricky part:
106  // we need to collect all requested TagInfos belonging to the same jet
107 
108  typedef vector<const BaseTagInfo*> TagInfoPtrs;
109  typedef RefToBase<Jet> JetRef;
110  typedef map<JetRef, TagInfoPtrs, JetRefCompare> JetToTagInfoMap;
111 
112  JetToTagInfoMap jetToTagInfos;
113 
114  // retrieve all requested TagInfos
115  vector< Handle< View<BaseTagInfo> > > tagInfoHandles(nTagInfos);
116  for(unsigned int i = 0; i < nTagInfos; i++) {
117  Handle< View<BaseTagInfo> > &tagInfoHandle = tagInfoHandles[i];
118  iEvent.getByToken(token_tagInfos[i], tagInfoHandle);
119 
120  for(View<BaseTagInfo>::const_iterator iter = tagInfoHandle->begin();
121  iter != tagInfoHandle->end(); iter++) {
122  TagInfoPtrs &tagInfos = jetToTagInfos[iter->jet()];
123  if (tagInfos.empty())
124  tagInfos.resize(nTagInfos);
125 
126  tagInfos[i] = &*iter;
127  }
128  }
129 
130  // take first tagInfo
131  Handle< View<BaseTagInfo> > &tagInfoHandle = tagInfoHandles[0];
132  std::unique_ptr<JetTagCollection> jetTagCollection;
133  if (!tagInfoHandle.product()->empty()) {
134  RefToBase<Jet> jj = tagInfoHandle->begin()->jet();
135  jetTagCollection = std::make_unique<JetTagCollection>(edm::makeRefToBaseProdFrom(jj, iEvent));
136  } else
137  jetTagCollection = std::make_unique<JetTagCollection>();
138 
139  // now loop over the map and compute all JetTags
140  for(JetToTagInfoMap::const_iterator iter = jetToTagInfos.begin();
141  iter != jetToTagInfos.end(); iter++) {
142  const TagInfoPtrs &tagInfoPtrs = iter->second;
143 
145  float discriminator = (*computer)(helper);
146 
147  (*jetTagCollection)[iter->first] = discriminator;
148  }
149 
150  iEvent.put(std::move(jetTagCollection));
151 }
152 
153 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
154 void
156 
158  desc.add<std::string>("jetTagComputer","combinedMVAComputer");
159  {
160  std::vector<edm::InputTag> tagInfos;
161  tagInfos.push_back(edm::InputTag("impactParameterTagInfos"));
162  tagInfos.push_back(edm::InputTag("inclusiveSecondaryVertexFinderTagInfos"));
163  tagInfos.push_back(edm::InputTag("softPFMuonsTagInfos"));
164  tagInfos.push_back(edm::InputTag("softPFElectronsTagInfos"));
165  desc.add<std::vector<edm::InputTag> >("tagInfos",tagInfos);
166  }
167  descriptions.addDefault(desc);
168 }
169 
170 // define it as plugin
JetTagProducer(const edm::ParameterSet &)
T getParameter(std::string const &) const
void produce(edm::Event &, const edm::EventSetup &) override
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
Definition: helper.py:1
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::string m_jetTagComputer
ProductID id() const
Definition: RefToBase.h:242
unsigned int nTagInfos
edm::Ref< JetBxCollection > JetRef
Definition: Jet.h:13
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void addDefault(ParameterSetDescription const &psetDescription)
const std::vector< std::string > & getInputLabels() const
size_t key() const
Definition: RefToBase.h:250
std::vector< edm::EDGetTokenT< edm::View< reco::BaseTagInfo > > > token_tagInfos
ParameterDescriptionBase * add(U const &iLabel, T const &value)
T const * product() const
Definition: Handle.h:74
edm::ESWatcher< JetTagComputerRecord > recordWatcher_
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:52
RefToBaseProd< T > makeRefToBaseProdFrom(RefToBase< T > const &iRef, Event const &iEvent)
fixed size matrix
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
T get() const
Definition: EventSetup.h:71
~JetTagProducer() override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
def move(src, dest)
Definition: eostools.py:511