1 #ifndef PhysicsTools_PatAlgos_BaseMVAValueMapProducer 2 #define PhysicsTools_PatAlgos_BaseMVAValueMapProducer 37 #include "TMVA/Factory.h" 38 #include "TMVA/Reader.h" 62 src_(consumes<
edm::
View<
T>>(iConfig.getParameter<
edm::InputTag>(
"src"))),
96 tensorflow::SessionOptions sessionOptions;
101 throw cms::Exception(
"ConfigError") <<
"Only 'TF' and 'TMVA' backends are supported\n";
103 if(
tmva_) produces<edm::ValueMap<float>>();
106 produces<edm::ValueMap<float>>(
n);
133 std::vector<std::pair<std::string,StringObjectFunction<T,true>>>
funcs_;
153 template <
typename T>
161 for(
auto &
v : mvaOut)
v.reserve(src->size());
163 for(
auto const &
o: *src) {
173 tensorflow::TensorShape input_size {1,(
long long int)
positions_.size()} ;
175 input_tensors.resize(1);
177 for(
size_t j =0; j <
values_.size();j++) {
178 input_tensors[0].second.matrix<
float>()(0,j) =
values_[j];
180 std::vector<tensorflow::Tensor>
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));
192 for(
auto &
m : mvaOut) {
195 filler.
insert(src,
m.begin(),
m.end());
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");
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");
227 template <
typename T>
232 if (
typeid(
T) ==
typeid(
pat::Jet)) modname+=
"Jet";
233 else if (
typeid(
T) ==
typeid(
pat::Muon)) modname+=
"Muon";
235 modname+=
"BaseMVAValueMapProducer";
236 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_
static const HistoName names[]
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_
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
std::pair< std::string, Tensor > NamedTensor
std::map< std::string, size_t > positions_
std::vector< std::string > variablesOrder_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
edm::EDGetTokenT< edm::View< T > > src_
void setThreading(SessionOptions &sessionOptions, int nThreads, const std::string &singleThreadPool="no_threads")
void setLogging(const std::string &level="3")
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
tensorflow::Session * session_
Analysis-level electron class.
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)
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::pair< std::string, StringObjectFunction< T, true > > > funcs_
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