CMS 3D CMS Logo

BaseMVAValueMapProducer.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_PatAlgos_BaseMVAValueMapProducer
2 #define PhysicsTools_PatAlgos_BaseMVAValueMapProducer
3 
4 // -*- C++ -*-
5 //
6 // Package: PhysicsTools/PatAlgos
7 // Class: BaseMVAValueMapProducer
8 //
16 //
17 // Original Author: Andre Rizzi
18 // Created: Mon, 07 Sep 2017 09:18:03 GMT
19 //
20 //
21 
22 
23 // system include files
24 #include <memory>
25 
26 // user include files
29 
32 
35 
36 
37 #include "TMVA/Factory.h"
38 #include "TMVA/Reader.h"
39 
43 
44 #include <string>
45 //
46 // class declaration
47 //
48 
49 template <typename T>
51  public:
52  explicit BaseMVAValueMapProducer(const edm::ParameterSet &iConfig):
53  src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
54  variablesOrder_(iConfig.getParameter<std::vector<std::string>>("variablesOrder")),
55  name_(iConfig.getParameter<std::string>("name")),
56  isClassifier_(iConfig.getParameter<bool>("isClassifier"))
57  {
58  edm::ParameterSet const & varsPSet = iConfig.getParameter<edm::ParameterSet>("variables");
59  for (const std::string & vname : varsPSet.getParameterNamesForType<std::string>()) {
60  funcs_.emplace_back(std::pair<std::string,StringObjectFunction<T,true>>(vname,varsPSet.getParameter<std::string>(vname)));
61  }
62 
63  values_.resize(variablesOrder_.size());
64  size_t i=0;
65  for(const auto & v : variablesOrder_){
66  positions_[v]=i;
67  reader_.AddVariable(v,(&values_.front())+i);
68  i++;
69  }
70 // reader_.BookMVA(name_,iConfig.getParameter<edm::FileInPath>("weightFile").fullPath() );
72  produces<edm::ValueMap<float>>();
73 
74  }
76 
77  void setValue(const std::string var,float val) {
79  }
80 
82  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
83 
84  private:
85  void beginStream(edm::StreamID) override {};
86  void produce(edm::Event&, const edm::EventSetup&) override;
87  void endStream() override {};
88 
91  virtual void fillAdditionalVariables(const T&) {}
92 
93 
95  std::map<std::string,size_t> positions_;
96  std::vector<std::pair<std::string,StringObjectFunction<T,true>>> funcs_;
97  std::vector<std::string> variablesOrder_;
98  std::vector<float> values_;
99  TMVA::Reader reader_;
102 
103 };
104 
105 template <typename T>
106 void
108 {
110  iEvent.getByToken(src_, src);
111  readAdditionalCollections(iEvent,iSetup);
112 
113  std::vector<float> mvaOut;
114  mvaOut.reserve(src->size());
115  for(auto const & o: *src) {
116  for(auto const & p : funcs_ ){
117  values_[positions_[p.first]]=p.second(o);
118  }
120  mvaOut.push_back(isClassifier_ ? reader_.EvaluateMVA(name_) : reader_.EvaluateRegression(name_)[0]);
121  }
122  std::unique_ptr<edm::ValueMap<float>> mvaV(new edm::ValueMap<float>());
124  filler.insert(src,mvaOut.begin(),mvaOut.end());
125  filler.fill();
126  iEvent.put(std::move(mvaV));
127 
128 }
129 
130 template <typename T>
134  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
135  desc.add<std::vector<std::string>>("variablesOrder")->setComment("ordered list of MVA input variable names");
136  desc.add<std::string>("name")->setComment("output score variable name");
137  desc.add<bool>("isClassifier")->setComment("is a classifier discriminator");
139  variables.setAllowAnything();
140  desc.add<edm::ParameterSetDescription>("variables", variables)->setComment("list of input variable definitions");
141  desc.add<edm::FileInPath>("weightFile")->setComment("xml weight file");
142  return desc;
143 }
144 
145 template <typename T>
146 void
149  std::string modname;
150  if (typeid(T) == typeid(pat::Jet)) modname+="Jet";
151  else if (typeid(T) == typeid(pat::Muon)) modname+="Muon";
152  else if (typeid(T) == typeid(pat::Electron)) modname+="Ele";
153  modname+="BaseMVAValueMapProducer";
154  descriptions.add(modname,desc);
155 }
156 
157 
158 
159 #endif
std::vector< float > values_
static edm::ParameterSetDescription getDescription()
T getParameter(std::string const &) const
virtual void fillAdditionalVariables(const T &)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
void setAllowAnything()
allow any parameter label/value pairs
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
void produce(edm::Event &, const edm::EventSetup &) override
void setValue(const std::string var, float val)
BaseMVAValueMapProducer(const edm::ParameterSet &iConfig)
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:194
int iEvent
Definition: GenABIO.cc:230
std::map< std::string, size_t > positions_
std::vector< std::string > variablesOrder_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
edm::EDGetTokenT< edm::View< T > > src_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Analysis-level electron class.
Definition: Electron.h:52
Analysis-level calorimeter jet class.
Definition: Jet.h:80
void add(std::string const &label, ParameterSetDescription const &psetDescription)
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
HLT enums.
std::string fullPath() const
Definition: FileInPath.cc:197
void beginStream(edm::StreamID) override
long double T
Analysis-level muon class.
Definition: Muon.h:50
std::vector< std::pair< std::string, StringObjectFunction< T, true > > > funcs_
def move(src, dest)
Definition: eostools.py:510
virtual void readAdditionalCollections(edm::Event &, const edm::EventSetup &)
to be implemented in derived classes, filling values for additional variables