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 
44 
45 #include <string>
46 //
47 // class declaration
48 //
49 
50 
51 template <typename T>
53  public:
54  explicit BaseMVAValueMapProducer(const edm::ParameterSet &iConfig):
55  src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
56  variablesOrder_(iConfig.getParameter<std::vector<std::string>>("variablesOrder")),
57  name_(iConfig.getParameter<std::string>("name")),
58  backend_(iConfig.getParameter<std::string>("backend")),
59  weightfilename_(iConfig.getParameter<edm::FileInPath>("weightFile").fullPath()),
60  isClassifier_(iConfig.getParameter<bool>("isClassifier")),
61  tmva_(backend_=="TMVA"),tf_(backend_=="TF")
62  {
63  if(tmva_) reader_=new TMVA::Reader();
64  edm::ParameterSet const & varsPSet = iConfig.getParameter<edm::ParameterSet>("variables");
65  for (const std::string & vname : varsPSet.getParameterNamesForType<std::string>()) {
66  funcs_.emplace_back(std::pair<std::string,StringObjectFunction<T,true>>(vname,varsPSet.getParameter<std::string>(vname)));
67  }
68 
69  values_.resize(variablesOrder_.size());
70  size_t i=0;
71  for(const auto & v : variablesOrder_){
72  positions_[v]=i;
73  if(tmva_) reader_->AddVariable(v,(&values_.front())+i);
74  i++;
75  }
76 // reader_.BookMVA(name_,iConfig.getParameter<edm::FileInPath>("weightFile").fullPath() );
77  if(tmva_)
78  {
80  }else if(tf_) {
83  inputTensorName_=iConfig.getParameter<std::string>("inputTensorName");
84  outputTensorName_=iConfig.getParameter<std::string>("outputTensorName");
85  output_names_=iConfig.getParameter<std::vector<std::string>>("outputNames");
86  for(const auto & s : iConfig.getParameter<std::vector<std::string>>("outputFormulas")) { output_formulas_.push_back(StringObjectFunction<std::vector<float>>(s));}
87  size_t nThreads = iConfig.getParameter<unsigned int>("nThreads");
88  std::string singleThreadPool = iConfig.getParameter<std::string>("singleThreadPool");
89  tensorflow::SessionOptions sessionOptions;
90  tensorflow::setThreading(sessionOptions, nThreads, singleThreadPool);
91  session_ = tensorflow::createSession(graph_, sessionOptions);
92 
93  } else {
94  throw cms::Exception("ConfigError") << "Only 'TF' and 'TMVA' backends are supported\n";
95  }
96  if(tmva_) produces<edm::ValueMap<float>>();
97  else {
98  for(const auto & n : output_names_){
99  produces<edm::ValueMap<float>>(n);
100  }
101  }
102 
103  }
105 
106  void setValue(const std::string var,float val) {
107  if(positions_.find(var)!=positions_.end())
108  values_[positions_[var]]=val;
109  }
110 
112  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
113 
114  private:
115  void beginStream(edm::StreamID) override {};
116  void produce(edm::Event&, const edm::EventSetup&) override;
117  void endStream() override {};
118 
121  virtual void fillAdditionalVariables(const T&) {}
122 
123 
125  std::map<std::string,size_t> positions_;
126  std::vector<std::pair<std::string,StringObjectFunction<T,true>>> funcs_;
127  std::vector<std::string> variablesOrder_;
128  std::vector<float> values_;
129  TMVA::Reader * reader_;
130  tensorflow::GraphDef* graph_;
131  tensorflow::Session* session_;
132 
137  bool tmva_;
138  bool tf_;
141  std::vector<std::string> output_names_;
142  std::vector<StringObjectFunction<std::vector<float>>> output_formulas_;
143 
144 };
145 
146 template <typename T>
147 void
149 {
151  iEvent.getByToken(src_, src);
152  readAdditionalCollections(iEvent,iSetup);
153  std::vector<std::vector<float>> mvaOut((tmva_)?1:output_names_.size());
154  for( auto & v : mvaOut) v.reserve(src->size());
155 
156  for(auto const & o: *src) {
157  for(auto const & p : funcs_ ){
158  setValue(p.first,p.second(o));
159  }
161  if(tmva_){
162  mvaOut[0].push_back(isClassifier_ ? reader_->EvaluateMVA(name_) : reader_->EvaluateRegression(name_)[0]);
163  }
164  if(tf_){
165  //currently support only one input sensor to reuse the TMVA like config
166  tensorflow::TensorShape input_size {1,(long long int)positions_.size()} ;
167  tensorflow::NamedTensorList input_tensors;
168  input_tensors.resize(1);
169  input_tensors[0] = tensorflow::NamedTensor(inputTensorName_, tensorflow::Tensor(tensorflow::DT_FLOAT, input_size));
170  for(size_t j =0; j < values_.size();j++) {
171  input_tensors[0].second.matrix<float>()(0,j) = values_[j];
172  }
173  std::vector<tensorflow::Tensor> outputs;
174  std::vector<std::string> names; names.push_back(outputTensorName_);
175  tensorflow::run(session_, input_tensors, names, &outputs);
176  std::vector<float> tmpOut;
177  for(int k=0;k<outputs.at(0).matrix<float>().dimension(1);k++) tmpOut.push_back(outputs.at(0).matrix<float>()(0, k));
178  for(size_t k=0;k<output_names_.size();k++) mvaOut[k].push_back(output_formulas_[k](tmpOut));
179 
180  }
181 
182 
183  }
184  size_t k=0;
185  for( auto & m : mvaOut) {
186  std::unique_ptr<edm::ValueMap<float>> mvaV(new edm::ValueMap<float>());
188  filler.insert(src,m.begin(),m.end());
189  filler.fill();
190  iEvent.put(std::move(mvaV),(tmva_)?"":output_names_[k]);
191  k++;
192  }
193 
194 }
195 
196 template <typename T>
200  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
201  desc.add<std::vector<std::string>>("variablesOrder")->setComment("ordered list of MVA input variable names");
202  desc.add<std::string>("name")->setComment("output score variable name");
203  desc.add<bool>("isClassifier")->setComment("is a classifier discriminator");
205  variables.setAllowAnything();
206  desc.add<edm::ParameterSetDescription>("variables", variables)->setComment("list of input variable definitions");
207  desc.add<edm::FileInPath>("weightFile")->setComment("xml weight file");
208  desc.add<std::string>("backend","TMVA")->setComment("TMVA or TF");
209  desc.add<std::string>("inputTensorName","")->setComment("Name of tensorflow input tensor in the model");
210  desc.add<std::string>("outputTensorName","")->setComment("Name of tensorflow output tensor in the model");
211  desc.add<std::vector<std::string>>("outputNames",std::vector<std::string>())->setComment("Names of the output values to be used in the output valuemap");
212  desc.add<std::vector<std::string>>("outputFormulas",std::vector<std::string>())->setComment("Formulas to be used to post process the output");
213  desc.add<unsigned int>("nThreads",1)->setComment("number of threads");
214  desc.add<std::string>("singleThreadPool", "no_threads");
215 
216 
217  return desc;
218 }
219 
220 template <typename T>
221 void
224  std::string modname;
225  if (typeid(T) == typeid(pat::Jet)) modname+="Jet";
226  else if (typeid(T) == typeid(pat::Muon)) modname+="Muon";
227  else if (typeid(T) == typeid(pat::Electron)) modname+="Ele";
228  modname+="BaseMVAValueMapProducer";
229  descriptions.add(modname,desc);
230 }
231 
232 
233 
234 #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:127
std::vector< StringObjectFunction< std::vector< float > > > output_formulas_
static const HistoName names[]
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
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
void setValue(const std::string var, float val)
tensorflow::GraphDef * graph_
BaseMVAValueMapProducer(const edm::ParameterSet &iConfig)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:194
std::pair< std::string, Tensor > NamedTensor
Definition: TensorFlow.h:25
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)
int k[5][pyjets_maxn]
singleThreadPool
Definition: jets_cff.py:291
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:50
std::vector< std::pair< std::string, StringObjectFunction< T, true > > > funcs_
def move(src, dest)
Definition: eostools.py:510
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