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());
170 std::vector<float>
data;
171 data.reserve(src->size() *
positions_.size());
172 for (
auto const&
o : *src) {
183 tensorflow::TensorShape input_size{(
long long int)src->size(), (
long long int)
positions_.size()};
185 input_tensors.resize(1);
189 for (
unsigned i = 0;
i < data.size(); ++
i) {
190 input_tensors[0].second.flat<
float>()(
i) = data[
i];
192 std::vector<tensorflow::Tensor> output_tensors;
194 for (
unsigned i = 0;
i < output_tensors.at(0).NumElements(); ++
i) {
195 outputs.push_back(output_tensors.at(0).flat<
float>()(
i));
202 const unsigned outdim = outputs.size() / src->size();
203 for (
unsigned i = 0;
i < src->size(); ++
i) {
204 std::vector<float> tmpOut(outputs.begin() +
i * outdim, outputs.begin() + (
i + 1) * outdim);
211 for (
auto const&
o : *src) {
219 std::vector<float> tmpOut;
222 tensorflow::TensorShape input_size{1, (
long long int)
positions_.size()};
224 input_tensors.resize(1);
227 for (
size_t j = 0; j <
values_.size(); j++) {
228 input_tensors[0].second.matrix<
float>()(0, j) =
values_[j];
230 std::vector<tensorflow::Tensor>
outputs;
232 for (
int k = 0;
k < outputs.at(0).matrix<
float>().
dimension(1);
k++)
233 tmpOut.push_back(outputs.at(0).matrix<
float>()(0,
k));
246 for (
auto&
m : mvaOut) {
249 filler.
insert(src,
m.begin(),
m.end());
256 template <
typename T>
259 desc.
add<
edm::InputTag>(
"src")->setComment(
"input physics object collection");
260 desc.
add<std::vector<std::string>>(
"variablesOrder")->setComment(
"ordered list of MVA input variable names");
261 desc.
add<
std::string>(
"name")->setComment(
"output score variable name");
262 desc.
add<
bool>(
"isClassifier")->setComment(
"is a classifier discriminator");
267 desc.
add<
std::string>(
"backend",
"TMVA")->setComment(
"TMVA, TF or ONNX");
268 desc.
add<
std::string>(
"inputTensorName",
"")->setComment(
"Name of tensorflow input tensor in the model");
269 desc.
add<
std::string>(
"outputTensorName",
"")->setComment(
"Name of tensorflow output tensor in the model");
270 desc.
add<std::vector<std::string>>(
"outputNames", std::vector<std::string>())
271 ->
setComment(
"Names of the output values to be used in the output valuemap");
272 desc.
add<std::vector<std::string>>(
"outputFormulas", std::vector<std::string>())
273 ->
setComment(
"Formulas to be used to post process the output");
274 desc.
add<
unsigned int>(
"nThreads", 1)->setComment(
"number of threads");
276 desc.
add<
bool>(
"batch_eval",
false)->setComment(
"Run inference in batch instead of per-object");
277 desc.
add<
bool>(
"disableONNXGraphOpt",
false)->setComment(
"Disable ONNX runtime graph optimization");
282 template <
typename T>
292 modname +=
"BaseMVAValueMapProducer";
293 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