1 #ifndef PhysicsTools_PatAlgos_BaseMVAValueMapProducer 2 #define PhysicsTools_PatAlgos_BaseMVAValueMapProducer 35 #include "TMVA/Factory.h" 36 #include "TMVA/Reader.h" 60 :
src_(consumes<
edm::
View<
T>>(iConfig.getParameter<
edm::InputTag>(
"src"))),
97 throw cms::Exception(
"ConfigError") <<
"Only 'TF', 'ONNX' and 'TMVA' backends are supported\n";
103 for (
const auto&
s : iConfig.
getParameter<std::vector<std::string>>(
"outputFormulas")) {
108 produces<edm::ValueMap<float>>();
111 produces<edm::ValueMap<float>>(
n);
136 std::vector<std::pair<std::string, StringObjectFunction<T, true>>>
funcs_;
143 std::unique_ptr<cms::Ort::ONNXRuntime>
ort_;
159 template <
typename T>
165 for (
auto&
v : mvaOut)
166 v.reserve(src->size());
169 std::vector<float>
data;
170 data.reserve(src->size() *
positions_.size());
171 for (
auto const&
o : *src) {
182 tensorflow::TensorShape input_size{(
long long int)src->size(), (
long long int)
positions_.size()};
184 input_tensors.resize(1);
188 for (
unsigned i = 0;
i < data.size(); ++
i) {
189 input_tensors[0].second.flat<
float>()(
i) = data[
i];
191 std::vector<tensorflow::Tensor> output_tensors;
193 for (
unsigned i = 0;
i < output_tensors.at(0).NumElements(); ++
i) {
194 outputs.push_back(output_tensors.at(0).flat<
float>()(
i));
201 const unsigned outdim = outputs.size() / src->size();
202 for (
unsigned i = 0;
i < src->size(); ++
i) {
203 std::vector<float> tmpOut(outputs.begin() +
i * outdim, outputs.begin() + (
i + 1) * outdim);
209 for (
auto const&
o : *src) {
218 std::vector<float> tmpOut;
221 tensorflow::TensorShape input_size{1, (
long long int)
positions_.size()};
223 input_tensors.resize(1);
226 for (
size_t j = 0; j <
values_.size(); j++) {
227 input_tensors[0].second.matrix<
float>()(0, j) =
values_[j];
229 std::vector<tensorflow::Tensor>
outputs;
231 for (
int k = 0;
k < outputs.at(0).matrix<
float>().
dimension(1);
k++)
232 tmpOut.push_back(outputs.at(0).matrix<
float>()(0,
k));
243 for (
auto&
m : mvaOut) {
246 filler.
insert(src,
m.begin(),
m.end());
254 template <
typename T>
257 desc.
add<
edm::InputTag>(
"src")->setComment(
"input physics object collection");
258 desc.
add<std::vector<std::string>>(
"variablesOrder")->setComment(
"ordered list of MVA input variable names");
259 desc.
add<
std::string>(
"name")->setComment(
"output score variable name");
260 desc.
add<
bool>(
"isClassifier")->setComment(
"is a classifier discriminator");
265 desc.
add<
std::string>(
"backend",
"TMVA")->setComment(
"TMVA, TF or ONNX");
266 desc.
add<
std::string>(
"inputTensorName",
"")->setComment(
"Name of tensorflow input tensor in the model");
267 desc.
add<
std::string>(
"outputTensorName",
"")->setComment(
"Name of tensorflow output tensor in the model");
268 desc.
add<std::vector<std::string>>(
"outputNames", std::vector<std::string>())
269 ->
setComment(
"Names of the output values to be used in the output valuemap");
270 desc.
add<std::vector<std::string>>(
"outputFormulas", std::vector<std::string>())
271 ->
setComment(
"Formulas to be used to post process the output");
272 desc.
add<
unsigned int>(
"nThreads", 1)->setComment(
"number of threads");
274 desc.
add<
bool>(
"batch_eval",
false)->setComment(
"Run inference in batch instead of per-object");
275 desc.
add<
bool>(
"disableONNXGraphOpt",
false)->setComment(
"Disable ONNX runtime graph optimization");
280 template <
typename T>
290 modname +=
"BaseMVAValueMapProducer";
291 descriptions.
add(modname, desc);
std::vector< float > values_
Session * createSession(SessionOptions &sessionOptions)
static edm::ParameterSetDescription getDescription()
void endStream() override
T getParameter(std::string const &) const
void setComment(std::string const &value)
virtual void fillAdditionalVariables(const T &)
std::vector< NamedTensor > NamedTensorList
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
std::vector< StringObjectFunction< std::vector< float > > > output_formulas_
std::string inputTensorName_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
void setAllowAnything()
allow any parameter label/value pairs
GraphDef * loadGraphDef(const std::string &pbFile)
void insert(const H &h, I begin, I end)
void produce(edm::Event &, const edm::EventSetup &) override
std::string outputTensorName_
std::string weightfilename_
std::vector< std::vector< float > > FloatArrays
void setValue(const std::string var, float val)
tensorflow::GraphDef * graph_
std::unique_ptr< cms::Ort::ONNXRuntime > ort_
BaseMVAValueMapProducer(const edm::ParameterSet &iConfig)
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
std::pair< std::string, Tensor > NamedTensor
std::string singleThreadPool_
std::vector< std::pair< std::string, StringObjectFunction< T, true > > > funcs_
std::vector< std::string > variablesOrder_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
edm::EDGetTokenT< edm::View< T > > src_
void setLogging(const std::string &level="3")
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
tensorflow::Session * session_
Analysis-level electron class.
std::map< std::string, size_t > positions_
Analysis-level calorimeter jet class.
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)
char data[epos_bytes_allocation]
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, const std::vector< std::string > &targetNodes, std::vector< Tensor > *outputs)
~BaseMVAValueMapProducer() override
void beginStream(edm::StreamID) override
uint32_t dimension(pat::CandKinResolution::Parametrization parametrization)
Returns the number of free parameters in a parametrization (3 or 4)
Analysis-level muon class.
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