1 #ifndef PhysicsTools_PatAlgos_BaseMVAValueMapProducer 2 #define PhysicsTools_PatAlgos_BaseMVAValueMapProducer 37 #include "TMVA/Factory.h" 38 #include "TMVA/Reader.h" 89 tensorflow::SessionOptions sessionOptions;
94 throw cms::Exception(
"ConfigError") <<
"Only 'TF' and 'TMVA' backends are supported\n";
96 if(
tmva_) produces<edm::ValueMap<float>>();
99 produces<edm::ValueMap<float>>(
n);
126 std::vector<std::pair<std::string,StringObjectFunction<T,true>>>
funcs_;
146 template <
typename T>
154 for(
auto &
v : mvaOut)
v.reserve(src->size());
156 for(
auto const &
o: *src) {
166 tensorflow::TensorShape input_size {1,(
long long int)
positions_.size()} ;
168 input_tensors.resize(1);
170 for(
size_t j =0; j <
values_.size();j++) {
171 input_tensors[0].second.matrix<
float>()(0,j) =
values_[j];
173 std::vector<tensorflow::Tensor>
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));
185 for(
auto &
m : mvaOut) {
188 filler.
insert(src,
m.begin(),
m.end());
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");
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");
220 template <
typename T>
225 if (
typeid(
T) ==
typeid(
pat::Jet)) modname+=
"Jet";
226 else if (
typeid(
T) ==
typeid(
pat::Muon)) modname+=
"Muon";
228 modname+=
"BaseMVAValueMapProducer";
229 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)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
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