7 using namespace egammaTools;
11 const std::vector<std::string>& availableVars)
12 : cfg_(cfg), modelSelector_(modelSelector), nModels_(cfg_.modelsFiles.
size()), graphDefs_(cfg_.modelsFiles.
size()) {
29 std::vector<tensorflow::Session*> sessions;
34 LogDebug(
"EgammaDNNHelper") <<
"TF sessions started";
41 std::vector<ScalerConfiguration>
features;
44 if (inputfile_scaler.fail()) {
45 throw cms::Exception(
"MissingFile") <<
"Scaler file for Electron PFid DNN not found";
51 while (inputfile_scaler >> varName >> type_str >> par1 >> par2) {
52 if (type_str ==
"stdscale")
54 else if (type_str ==
"minmax")
56 else if (type_str ==
"custom1")
62 auto match =
std::find(availableVars.begin(), availableVars.end(), varName);
65 <<
"Requested variable (" << varName <<
") not available between DNN inputs";
70 inputfile_scaler.close();
77 const std::map<std::string, float>&
variables)
const {
86 inputs.push_back((variables.at(varName) - par1) / par2);
88 inputs.push_back((variables.at(varName) - par1) / (par2 - par1));
90 inputs.push_back(2 * (variables.at(varName) - par1) / (par2 - par1) - 1.);
92 inputs.push_back(variables.at(varName));
97 return std::make_pair(modelIndex, inputs);
101 const std::vector<tensorflow::Session*>& sessions)
const {
116 std::vector<std::vector<int>> indexMap(
nModels_);
117 std::vector<std::vector<float>> inputsVectors(nCandidates);
120 LogDebug(
"EgammaDNNHelper") <<
"Working on " << nCandidates <<
" candidates";
124 LogDebug(
"EgammaDNNHelper") <<
"Working on candidate: " << icand;
126 counts[model_index] += 1;
127 indexMap[model_index].push_back(icand);
128 inputsVectors[icand] =
inputs;
133 std::vector<tensorflow::Tensor> input_tensors(
nModels_);
135 std::vector<float*> input_tensors_pointer(
nModels_);
137 LogDebug(
"EgammaDNNHelper") <<
"Initializing TF input " <<
i <<
" with rows:" << counts[
i]
139 input_tensors[
i] = tensorflow::Tensor{tensorflow::DT_FLOAT, {counts[
i], nInputs_[
i]}};
140 input_tensors_pointer[
i] = input_tensors[
i].flat<
float>().
data();
145 LogDebug(
"EgammaDNNHelper") <<
"Loading TF input tensor for model: " <<
m;
146 float*
T = input_tensors_pointer[
m];
147 for (
size_t cand_index : indexMap[
m]) {
149 *T = inputsVectors[cand_index][
k];
156 std::vector<std::pair<int, std::vector<float>>> outputs;
161 std::vector<tensorflow::Tensor>
output;
162 LogDebug(
"EgammaDNNHelper") <<
"Run model: " <<
m <<
" with " << counts[
m] <<
" electrons";
165 const auto&
r = output[0].tensor<float, 2>();
167 for (
uint b = 0;
b < counts[
m];
b++) {
172 const auto cand_index = indexMap[
m][
b];
173 outputs.push_back(std::make_pair(cand_index, result));
177 std::sort(outputs.begin(), outputs.end());
178 std::vector<std::vector<float>> final_outputs(outputs.size());
179 std::transform(outputs.begin(), outputs.end(), final_outputs.begin(), [](
auto a) {
return a.second; });
181 return final_outputs;
Session * createSession(SessionOptions &sessionOptions)
GraphDef * loadGraphDef(const std::string &pbFile)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
std::vector< float > features(const reco::PreId &ecal, const reco::PreId &hcal, double rho, const reco::BeamSpot &spot, noZS::EcalClusterLazyTools &ecalTools)
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
char data[epos_bytes_allocation]
std::string fullPath() const
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
tuple size
Write out results.