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 
46 
51 
52 #include <string>
53 //
54 // class declaration
55 //
56 
57 
58 template <typename T>
60  public:
61  explicit BaseMVAValueMapProducer(const edm::ParameterSet &iConfig):
62  src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
63  variablesOrder_(iConfig.getParameter<std::vector<std::string>>("variablesOrder")),
64  name_(iConfig.getParameter<std::string>("name")),
65  backend_(iConfig.getParameter<std::string>("backend")),
66  weightfilename_(iConfig.getParameter<edm::FileInPath>("weightFile").fullPath()),
67  isClassifier_(iConfig.getParameter<bool>("isClassifier")),
68  tmva_(backend_=="TMVA"),tf_(backend_=="TF")
69  {
70  if(tmva_) reader_=new TMVA::Reader();
71  edm::ParameterSet const & varsPSet = iConfig.getParameter<edm::ParameterSet>("variables");
72  for (const std::string & vname : varsPSet.getParameterNamesForType<std::string>()) {
73  funcs_.emplace_back(std::pair<std::string,StringObjectFunction<T,true>>(vname,varsPSet.getParameter<std::string>(vname)));
74  }
75 
76  values_.resize(variablesOrder_.size());
77  size_t i=0;
78  for(const auto & v : variablesOrder_){
79  positions_[v]=i;
80  if(tmva_) reader_->AddVariable(v,(&values_.front())+i);
81  i++;
82  }
83 // reader_.BookMVA(name_,iConfig.getParameter<edm::FileInPath>("weightFile").fullPath() );
84  if(tmva_)
85  {
87  }else if(tf_) {
90  inputTensorName_=iConfig.getParameter<std::string>("inputTensorName");
91  outputTensorName_=iConfig.getParameter<std::string>("outputTensorName");
92  output_names_=iConfig.getParameter<std::vector<std::string>>("outputNames");
93  for(const auto & s : iConfig.getParameter<std::vector<std::string>>("outputFormulas")) { output_formulas_.push_back(StringObjectFunction<std::vector<float>>(s));}
94  size_t nThreads = iConfig.getParameter<unsigned int>("nThreads");
95  std::string singleThreadPool = iConfig.getParameter<std::string>("singleThreadPool");
96  tensorflow::SessionOptions sessionOptions;
97  tensorflow::setThreading(sessionOptions, nThreads, singleThreadPool);
98  session_ = tensorflow::createSession(graph_, sessionOptions);
99 
100  } else {
101  throw cms::Exception("ConfigError") << "Only 'TF' and 'TMVA' backends are supported\n";
102  }
103  if(tmva_) produces<edm::ValueMap<float>>();
104  else {
105  for(const auto & n : output_names_){
106  produces<edm::ValueMap<float>>(n);
107  }
108  }
109 
110  }
112 
113  void setValue(const std::string var,float val) {
114  if(positions_.find(var)!=positions_.end())
115  values_[positions_[var]]=val;
116  }
117 
119  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
120 
121  private:
122  void beginStream(edm::StreamID) override {};
123  void produce(edm::Event&, const edm::EventSetup&) override;
124  void endStream() override {};
125 
128  virtual void fillAdditionalVariables(const T&) {}
129 
130 
132  std::map<std::string,size_t> positions_;
133  std::vector<std::pair<std::string,StringObjectFunction<T,true>>> funcs_;
134  std::vector<std::string> variablesOrder_;
135  std::vector<float> values_;
136  TMVA::Reader * reader_;
137  tensorflow::GraphDef* graph_;
138  tensorflow::Session* session_;
139 
144  bool tmva_;
145  bool tf_;
148  std::vector<std::string> output_names_;
149  std::vector<StringObjectFunction<std::vector<float>>> output_formulas_;
150 
151 };
152 
153 template <typename T>
154 void
156 {
158  iEvent.getByToken(src_, src);
159  readAdditionalCollections(iEvent,iSetup);
160  std::vector<std::vector<float>> mvaOut((tmva_)?1:output_names_.size());
161  for( auto & v : mvaOut) v.reserve(src->size());
162 
163  for(auto const & o: *src) {
164  for(auto const & p : funcs_ ){
165  setValue(p.first,p.second(o));
166  }
168  if(tmva_){
169  mvaOut[0].push_back(isClassifier_ ? reader_->EvaluateMVA(name_) : reader_->EvaluateRegression(name_)[0]);
170  }
171  if(tf_){
172  //currently support only one input sensor to reuse the TMVA like config
173  tensorflow::TensorShape input_size {1,(long long int)positions_.size()} ;
174  tensorflow::NamedTensorList input_tensors;
175  input_tensors.resize(1);
176  input_tensors[0] = tensorflow::NamedTensor(inputTensorName_, tensorflow::Tensor(tensorflow::DT_FLOAT, input_size));
177  for(size_t j =0; j < values_.size();j++) {
178  input_tensors[0].second.matrix<float>()(0,j) = values_[j];
179  }
180  std::vector<tensorflow::Tensor> outputs;
181  std::vector<std::string> names; names.push_back(outputTensorName_);
182  tensorflow::run(session_, input_tensors, names, &outputs);
183  std::vector<float> tmpOut;
184  for(int k=0;k<outputs.at(0).matrix<float>().dimension(1);k++) tmpOut.push_back(outputs.at(0).matrix<float>()(0, k));
185  for(size_t k=0;k<output_names_.size();k++) mvaOut[k].push_back(output_formulas_[k](tmpOut));
186 
187  }
188 
189 
190  }
191  size_t k=0;
192  for( auto & m : mvaOut) {
193  std::unique_ptr<edm::ValueMap<float>> mvaV(new edm::ValueMap<float>());
195  filler.insert(src,m.begin(),m.end());
196  filler.fill();
197  iEvent.put(std::move(mvaV),(tmva_)?"":output_names_[k]);
198  k++;
199  }
200 
201 }
202 
203 template <typename T>
207  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
208  desc.add<std::vector<std::string>>("variablesOrder")->setComment("ordered list of MVA input variable names");
209  desc.add<std::string>("name")->setComment("output score variable name");
210  desc.add<bool>("isClassifier")->setComment("is a classifier discriminator");
212  variables.setAllowAnything();
213  desc.add<edm::ParameterSetDescription>("variables", variables)->setComment("list of input variable definitions");
214  desc.add<edm::FileInPath>("weightFile")->setComment("xml weight file");
215  desc.add<std::string>("backend","TMVA")->setComment("TMVA or TF");
216  desc.add<std::string>("inputTensorName","")->setComment("Name of tensorflow input tensor in the model");
217  desc.add<std::string>("outputTensorName","")->setComment("Name of tensorflow output tensor in the model");
218  desc.add<std::vector<std::string>>("outputNames",std::vector<std::string>())->setComment("Names of the output values to be used in the output valuemap");
219  desc.add<std::vector<std::string>>("outputFormulas",std::vector<std::string>())->setComment("Formulas to be used to post process the output");
220  desc.add<unsigned int>("nThreads",1)->setComment("number of threads");
221  desc.add<std::string>("singleThreadPool", "no_threads");
222 
223 
224  return desc;
225 }
226 
227 template <typename T>
228 void
231  std::string modname;
232  if (typeid(T) == typeid(pat::Jet)) modname+="Jet";
233  else if (typeid(T) == typeid(pat::Muon)) modname+="Muon";
234  else if (typeid(T) == typeid(pat::Electron)) modname+="Ele";
235  modname+="BaseMVAValueMapProducer";
236  descriptions.add(modname,desc);
237 }
238 
239 
240 
241 #endif
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:87
static edm::ParameterSetDescription getDescription()
T getParameter(std::string const &) const
void setComment(std::string const &value)
virtual void fillAdditionalVariables(const T &)
std::vector< NamedTensor > NamedTensorList
Definition: TensorFlow.h:26
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
std::vector< StringObjectFunction< std::vector< float > > > output_formulas_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
void setAllowAnything()
allow any parameter label/value pairs
GraphDef * loadGraphDef(const std::string &pbFile)
Definition: TensorFlow.cc:68
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
void produce(edm::Event &, const edm::EventSetup &) override
const std::string names[nVars_]
void setValue(const std::string var, float val)
tensorflow::GraphDef * graph_
BaseMVAValueMapProducer(const edm::ParameterSet &iConfig)
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:169
std::pair< std::string, Tensor > NamedTensor
Definition: TensorFlow.h:25
int iEvent
Definition: GenABIO.cc:224
std::map< std::string, size_t > positions_
std::vector< std::string > variablesOrder_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
int k[5][pyjets_maxn]
singleThreadPool
Definition: jets_cff.py:335
edm::EDGetTokenT< edm::View< T > > src_
void setThreading(SessionOptions &sessionOptions, int nThreads, const std::string &singleThreadPool="no_threads")
Definition: TensorFlow.cc:19
void setLogging(const std::string &level="3")
Definition: TensorFlow.cc:14
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
tensorflow::Session * session_
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.
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, const std::vector< std::string > &targetNodes, std::vector< Tensor > *outputs)
Definition: TensorFlow.cc:210
void beginStream(edm::StreamID) override
long double T
uint32_t dimension(pat::CandKinResolution::Parametrization parametrization)
Returns the number of free parameters in a parametrization (3 or 4)
Analysis-level muon class.
Definition: Muon.h:51
std::vector< std::pair< std::string, StringObjectFunction< T, true > > > funcs_
def move(src, dest)
Definition: eostools.py:511
std::vector< std::string > output_names_
virtual void readAdditionalCollections(edm::Event &, const edm::EventSetup &)
to be implemented in derived classes, filling values for additional variables