|
|
Go to the documentation of this file. 1 #ifndef PhysicsTools_PatAlgos_BaseMVAValueMapProducer
2 #define PhysicsTools_PatAlgos_BaseMVAValueMapProducer
35 #include "TMVA/Factory.h"
36 #include "TMVA/Reader.h"
93 for (
const auto&
s : iConfig.
getParameter<std::vector<std::string>>(
"outputFormulas")) {
100 throw cms::Exception(
"ConfigError") <<
"Only 'TF' and 'TMVA' backends are supported\n";
103 produces<edm::ValueMap<float>>();
106 produces<edm::ValueMap<float>>(
n);
131 std::vector<std::pair<std::string, StringObjectFunction<T, true>>>
funcs_;
151 template <
typename T>
155 readAdditionalCollections(
iEvent, iSetup);
156 std::vector<std::vector<float>> mvaOut((tmva_) ? 1 : output_names_.size());
157 for (
auto&
v : mvaOut)
158 v.reserve(
src->size());
160 for (
auto const&
o : *
src) {
161 for (
auto const&
p : funcs_) {
164 fillAdditionalVariables(
o);
166 mvaOut[0].push_back(isClassifier_ ? reader_->EvaluateMVA(name_) : reader_->EvaluateRegression(name_)[0]);
170 tensorflow::TensorShape input_size{1, (
long long int)positions_.size()};
172 input_tensors.resize(1);
175 for (
size_t j = 0;
j < values_.size();
j++) {
176 input_tensors[0].second.matrix<
float>()(0,
j) = values_[
j];
178 std::vector<tensorflow::Tensor>
outputs;
179 std::vector<std::string>
names;
180 names.push_back(outputTensorName_);
182 std::vector<float> tmpOut;
184 tmpOut.push_back(
outputs.at(0).matrix<
float>()(0,
k));
185 for (
size_t k = 0;
k < output_names_.size();
k++)
186 mvaOut[
k].push_back(output_formulas_[
k](tmpOut));
190 for (
auto&
m : mvaOut) {
200 template <
typename T>
203 desc.
add<
edm::InputTag>(
"src")->setComment(
"input physics object collection");
204 desc.
add<std::vector<std::string>>(
"variablesOrder")->setComment(
"ordered list of MVA input variable names");
205 desc.
add<
std::string>(
"name")->setComment(
"output score variable name");
206 desc.
add<
bool>(
"isClassifier")->setComment(
"is a classifier discriminator");
211 desc.
add<
std::string>(
"backend",
"TMVA")->setComment(
"TMVA or TF");
212 desc.
add<
std::string>(
"inputTensorName",
"")->setComment(
"Name of tensorflow input tensor in the model");
213 desc.
add<
std::string>(
"outputTensorName",
"")->setComment(
"Name of tensorflow output tensor in the model");
214 desc.
add<std::vector<std::string>>(
"outputNames", std::vector<std::string>())
215 ->setComment(
"Names of the output values to be used in the output valuemap");
216 desc.
add<std::vector<std::string>>(
"outputFormulas", std::vector<std::string>())
217 ->setComment(
"Formulas to be used to post process the output");
218 desc.
add<
unsigned int>(
"nThreads", 1)->setComment(
"number of threads");
224 template <
typename T>
234 modname +=
"BaseMVAValueMapProducer";
std::vector< std::string > output_names_
Session * createSession(SessionOptions &sessionOptions)
uint32_t dimension(pat::CandKinResolution::Parametrization parametrization)
Returns the number of free parameters in a parametrization (3 or 4)
virtual void readAdditionalCollections(edm::Event &, const edm::EventSetup &)
to be implemented in derived classes, filling values for additional variables
bool setValue(Container &, const reco::JetBaseRef &, const JetExtendedData &)
associate jet with value. Returns false and associate nothing if jet is already associated
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void produce(edm::Event &, const edm::EventSetup &) override
void endStream() override
std::vector< StringObjectFunction< std::vector< float > > > output_formulas_
std::pair< std::string, Tensor > NamedTensor
void setValue(const std::string var, float val)
std::vector< float > values_
std::string outputTensorName_
BaseMVAValueMapProducer(const edm::ParameterSet &iConfig)
Analysis-level muon class.
std::string inputTensorName_
tensorflow::GraphDef * graph_
std::string weightfilename_
std::vector< std::pair< std::string, StringObjectFunction< T, true > > > funcs_
Analysis-level calorimeter jet class.
void add(std::string const &label, ParameterSetDescription const &psetDescription)
const std::string names[nVars_]
std::vector< std::string > variablesOrder_
edm::EDGetTokenT< edm::View< T > > src_
std::string singleThreadPool_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< NamedTensor > NamedTensorList
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
void setLogging(const std::string &level="3")
tensorflow::Session * session_
~BaseMVAValueMapProducer() override
GraphDef * loadGraphDef(const std::string &pbFile)
T getParameter(std::string const &) const
std::map< std::string, size_t > positions_
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
static edm::ParameterSetDescription getDescription()
Analysis-level electron class.
void beginStream(edm::StreamID) override
virtual void fillAdditionalVariables(const T &)